File indexing completed on 2024-05-19 05:59:21

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  * Created: 27.06.2017
0024  */
0025 class Default_Model_Solr
0026 {
0027 
0028     public $_pagination = null;
0029 
0030     /**
0031      * @param $input
0032      * @return null|string|string[]
0033      */
0034     static public function escape($input)
0035     {
0036         $pattern = '/(\+|-|&&|\|\||!|\(|\)|\{|}|\[|]|\^|"|~|\*|\?|:|\\\)/';
0037 
0038         return preg_replace($pattern, '\\\$1', $input);
0039     }
0040 
0041     /**
0042      * @param array $op
0043      * @return null
0044      * @throws Zend_Exception
0045      * @throws Zend_Paginator_Exception
0046      * @throws Zend_Service_Solr_HttpTransportException
0047      * @throws Zend_Service_Solr_InvalidArgumentException
0048      */
0049     public function search($op = array())
0050     {
0051         $output = null;
0052 
0053         $solr = $this->get_solr_connection();
0054         
0055         if (false === $solr->ping()) {           
0056             echo 'connection to solr server can not established';
0057             die;
0058             Zend_Registry::get('logger')->warn('connection to solr server can not established');
0059             return $output;
0060         }
0061 
0062         
0063 
0064         $params = array(
0065             'defType'           => 'dismax',
0066             'wt'                => 'json',
0067             'fl'                => '*,score',
0068             'df'                => 'title',
0069             'qf'                => empty($op['qf']) ? 'title_gen^5 title^5 title_prefix^5 description_gen description description_split username cat_title tags package_names arch_names license_names' : $op['qf'],
0070 //          'bq'                => 'changed_at:[NOW-1YEAR TO NOW/DAY]',
0071 //          'bf'                => 'if(lt(laplace_score,50),-10,10)',
0072 //          'bf'                => 'product(recip(ms(NOW/HOUR,changed_at),3.16e-11,0.2,0.2),1300)',
0073 //          'bf'                => 'product(recip(ms(NOW/HOUR,changed_at),3.16e-11,0.2,0.2),40)',
0074             'bf'                => 'product(recip(ms(NOW/HOUR,changed_at),3.16e-11,0.2,0.2),36)',    
0075 //          'sort'              => 'changed_at desc',
0076             'hl'                => 'on',
0077             'hl.fl'             => 'description,tags,package_names,arch_names,license_names',
0078             'facet'             => 'true',
0079             'facet.field'       => array('project_category_id', 'tags','package_names','arch_names','license_names'),
0080             'facet.mincount'    => '1',
0081 //          'facet.limit'       => '10',
0082             'facet.sort'        => 'count',
0083             'facet.range'       => 'laplace_score',
0084             'facet.range.start' => '0',
0085             'facet.range.end'   => '100',
0086             'facet.range.gap'   => '10',
0087             'spellcheck'        => 'true',
0088         );
0089 
0090     
0091         
0092         $params = $this->setStoreFilter($params,$op);
0093         $params = $this->addAnyFilter($params, $op);
0094 
0095         $offset = ((int)$op['page'] - 1) * (int)$op['count'];
0096 
0097         $query = trim($op['q']);
0098 
0099 
0100 
0101         if($offset<0) $offset = 0;
0102         $results = $solr->search($query, $offset, $op['count'], $params);
0103 
0104         $output['response'] = (array)$results->response;
0105         $output['responseHeader'] = (array)$results->responseHeader;
0106         $output['facet_counts'] = (array)$results->facet_counts;
0107         $output['facet_fields'] = (array)$results->facet_counts->facet_fields;
0108         $output['facet_ranges'] = (array)$results->facet_counts->facet_ranges;
0109         $output['hits'] = (array)$results->response->docs;
0110         //$output['highlighting'] =(array)$results->highlighting;
0111         $output['highlighting'] = json_decode(json_encode($results->highlighting), true);
0112         
0113 
0114         $pagination_array = array();
0115         if (isset($output['response']['numFound'])) {
0116             $pagination_array = array_combine(
0117                 range(0, $output['response']['numFound'] - 1),
0118                 range(1, $output['response']['numFound'])
0119             );
0120         }
0121 
0122         $pagination = Zend_Paginator::factory($pagination_array);
0123         $pagination->setCurrentPageNumber($op['page']);
0124         $pagination->setItemCountPerPage($op['count']);
0125         $pagination->setPageRange(9);
0126 
0127         $this->_pagination = $pagination;
0128 
0129         return $output;
0130     }
0131 
0132     public function isExist($project_id)
0133     {
0134         $solr = $this->get_solr_connection();
0135         $query = '*:*';
0136         $params = array('fq' => 'id:'.$project_id);
0137         $results = $solr->search($query, 0, 1, $params);
0138         if(sizeof($results->response->docs)==1)
0139         {
0140             return true;
0141         }
0142         else{
0143             return false;
0144         }
0145 
0146     }
0147     /**
0148      * @return Zend_Service_Solr
0149      * @throws Zend_Exception
0150      */
0151     private function get_solr_connection()
0152     {
0153         $config = Zend_Registry::get('config');
0154         $config_search = $config->settings->search;
0155 
0156         return new Zend_Service_Solr ($config_search->host, $config_search->port,
0157             $config_search->http_path); // Configure
0158     }
0159 
0160     /**
0161      * @param $params
0162      * @return mixed
0163      * @throws Zend_Exception
0164      */
0165     private function setStoreFilter($params,$op)
0166     {        
0167      
0168         // if(isset($op['store']))
0169         // {
0170         //     $storename = $op['store'];
0171         //     $storemodel = new Default_Model_DbTable_ConfigStore(); 
0172         //     $store = $storemodel->fetchDomainObjectsByName($storename);
0173         //     $currentStoreConfig = new Default_Model_ConfigStore($store['host']);
0174         // }
0175         // else
0176         // {
0177         //     $currentStoreConfig = Zend_Registry::get('store_config');                                  
0178         // }               
0179 
0180         // if (substr($currentStoreConfig->order, -1) <> 1) {
0181         //         return $params;
0182         // }
0183         // if(isset($currentStoreConfig->package_type)){            
0184         //     $pid = $currentStoreConfig->package_type;
0185         //     $t = new Default_Model_DbTable_Tags();
0186         //     $tag = $t->fetchRow($t->select()->where('tag_id='.$pid));           
0187         //     $params['fq'] = array_merge($params['fq'], array('package_names:' . $tag['tag_name']));   
0188         // }
0189 
0190         // $currentStoreConfig = Zend_Registry::get('store_config');  
0191 
0192         if(isset($op['store']))
0193         {
0194             $storename = $op['store'];
0195             $storemodel = new Default_Model_DbTable_ConfigStore(); 
0196             $store = $storemodel->fetchDomainObjectsByName($storename);
0197             $currentStoreConfig = new Default_Model_ConfigStore($store['host']);
0198         }
0199         else
0200         {
0201             $currentStoreConfig = Zend_Registry::get('store_config');                                  
0202         }  
0203         if($currentStoreConfig->store_id)
0204         {
0205             $params['fq'] = array('stores:(' . $currentStoreConfig->store_id . ')');        
0206         }        
0207         $csmodel  = new Default_Model_ConfigStoreTags();
0208         $packageFilter = $csmodel->getPackageTagsForStore($currentStoreConfig->store_id);       
0209         if($packageFilter)
0210         {
0211             $pkg = '';
0212             foreach ($packageFilter as $t) {
0213                 $pkg=$pkg.' '.$t['tag_name'];
0214             }
0215             $pkg = trim($pkg);
0216             $params['fq'] = array_merge($params['fq'], array('package_names:(' . $pkg.')')); 
0217         }
0218         return $params;
0219     }
0220 
0221     /**
0222      * @param array $params
0223      * @param array $op
0224      *
0225      * @return array
0226      */
0227     private function addAnyFilter($params, $op)
0228     {
0229         if (empty($op['fq'])) {
0230             return $params;
0231         }
0232         if (empty($params['fq'])) {
0233             $params['fq'] = $op['fq'];
0234         } else {
0235             $params['fq'] = array_merge($params['fq'], $op['fq']);
0236         }
0237 
0238         return $params;
0239     }
0240 
0241     /**
0242      * @return null|Zend_Paginator
0243      * @throws Zend_Paginator_Exception
0244      */
0245     public function getPagination()
0246     {
0247         if (isset($this->_pagination)) {
0248             return $this->_pagination;
0249         }
0250 
0251         return Zend_Paginator::factory(array());
0252     }
0253 
0254     /**
0255      *
0256      * Get spell
0257      *
0258      * @param array $op
0259      * @return mixed
0260      * @throws Zend_Exception
0261      * @throws Zend_Service_Solr_HttpTransportException
0262      * @throws Zend_Service_Solr_InvalidArgumentException
0263      */
0264     public function spell($op = array())
0265     {
0266         $solr = $this->get_solr_connection();
0267 
0268         if (false === $solr->ping()) {
0269             Zend_Registry::get('logger')->warn('connection to solr server can not established');
0270 
0271             return $op['q'];
0272         }
0273 
0274         $results = $solr->spell($op['q']);
0275         $results = json_decode($results, true);
0276 
0277         return $results['spellcheck'];
0278     }
0279 
0280     /**
0281      * @param $object
0282      * @return array
0283      */
0284     private function object_to_array($object)
0285     {
0286         if (is_array($object) || is_object($object)) {
0287             $result = array();
0288             foreach ($object as $key => $value) {
0289                 $result[$key] = $this->object_to_array($value);
0290             }
0291 
0292             return $result;
0293         }
0294 
0295         return $object;
0296     }
0297 
0298 }