File indexing completed on 2024-12-22 05:36:24

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 /** @deprecated  */
0024 class Local_Search_Provider_Lucene implements Local_Search_ProviderInterface
0025 {
0026 
0027     /** @var  Zend_Search_Lucene */
0028     protected $_index;
0029     /** @var Zend_Config */
0030     protected $config;
0031     /** @var Zend_Log|Zend_Log_Writer_Abstract */
0032     protected $logger;
0033 
0034     /**
0035      * @param array|Zend_config        $config
0036      * @param Zend_Log_Writer_Abstract $logger
0037      *
0038      * @throws Exception
0039      */
0040     function __construct($config, $logger)
0041     {
0042         if (false == isset($config)) {
0043             throw new Exception(__CLASS__ . ' needs config object in constructor');
0044         }
0045 
0046         if ($config instanceof Zend_Config) {
0047             $this->config = $config;
0048         } else if (is_array($config)) {
0049             $this->config = new Zend_Config($config);
0050         }
0051 
0052         if ($logger instanceof Zend_Log) {
0053             $this->logger = $logger;
0054         }
0055 
0056         Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
0057     }
0058 
0059     /**
0060      * @throws Zend_Db_Table_Row_Exception
0061      * @deprecated
0062      */
0063     public function createIndex()
0064     {
0065         $tableProject = new Default_Model_Project();
0066         $rowSetProject = $tableProject->fetchAll('status = ' . Default_Model_DbTable_Project::PROJECT_ACTIVE . ' AND type_id = '
0067             . Default_Model_DbTable_Project::PROJECT_TYPE_STANDARD, 'created_at desc');
0068 
0069         $tableMember = new Default_Model_Member();
0070         $tableCategory = new Default_Model_DbTable_ProjectCategory();
0071         $helperBuildProductUrl = new Default_View_Helper_BuildProductUrl();
0072 
0073         $this->_index = Zend_Search_Lucene::create($this->config->path);
0074 
0075         /** @var Zend_Db_Table_Row $project */
0076         foreach ($rowSetProject as $project) {
0077             $member = $project->findDependentRowset($tableMember, 'Owner')->current();
0078             $category = $project->findDependentRowset($tableCategory, 'Category')->current();
0079 
0080             if (null != $member->username) {
0081 
0082                 $doc = new Zend_Search_Lucene_Document();
0083 
0084                 $doc->addField(Zend_Search_Lucene_Field::keyword('project_id', $project->project_id));
0085                 $doc->addField(Zend_Search_Lucene_Field::keyword('member_id', $member->member_id));
0086                 $doc->addField(Zend_Search_Lucene_Field::keyword('project_category_id', $project->project_category_id));
0087 
0088                 $doc->addField(Zend_Search_Lucene_Field::text('title', $project->title, 'UTF-8'));
0089                 $doc->addField(Zend_Search_Lucene_Field::text('description', $project->description, 'UTF-8'));
0090                 $doc->addField(Zend_Search_Lucene_Field::text('username', $member->username, 'UTF-8'));
0091                 $doc->addField(Zend_Search_Lucene_Field::text('category', $category->title, 'UTF-8'));
0092 
0093                 $isUpdate = ($project->type_id == Default_Model_DbTable_Project::PROJECT_TYPE_UPDATE);
0094                 if ($isUpdate) {
0095                     $showUrl = $helperBuildProductUrl->buildProductUrl($project->pid) . '#anker_' . $project->project_id;
0096                     $plingUrl = $helperBuildProductUrl->buildProductUrl($project->pid, 'pling');
0097                 } else {
0098                     $showUrl = $helperBuildProductUrl->buildProductUrl($project->project_id);
0099                     $plingUrl = $helperBuildProductUrl->buildProductUrl($project->project_id, 'pling');
0100                 }
0101 
0102                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('showUrl', $showUrl));
0103                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('plingUrl', $plingUrl));
0104 
0105                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('uuid', $project->uuid));
0106                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('type_id', $project->type_id));
0107                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('pid', $project->pid));
0108                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('image_small', $project->image_small));
0109 
0110                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('facebook_code', $project->facebook_code));
0111                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('twitter_code', $project->twitter_code));
0112                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('google_code', $project->google_code));
0113                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('link_1', $project->link_1));
0114                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('ppload_collection_id', $project->ppload_collection_id));
0115 
0116                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('validated', $project->validated));
0117                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('amount', $project->amount));
0118                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('claimable', $project->claimable));
0119                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('claimed_by_member', $project->claimed_by_member));
0120                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('created_at', $project->created_at));
0121                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('changed_at', $project->changed_at));
0122 
0123                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('profile_image_url', $member->profile_image_url));
0124                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('paypal_mail', $member->paypal_mail));
0125                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('dwolla_id', $member->dwolla_id));
0126                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('mail', $member->mail));
0127                 $doc->addField(Zend_Search_Lucene_Field::unIndexed('roleId', $member->roleId));
0128 
0129                 $this->_index->addDocument($doc);
0130             }
0131         }
0132 
0133         $this->_index->commit();
0134     }
0135 
0136     /**
0137      * @param $storeId
0138      * @param $searchIndexId
0139      *
0140      * @throws Zend_Exception
0141      * @deprecated
0142      */
0143     public function createStoreSearchIndex($storeId, $searchIndexId)
0144     {
0145         Zend_Registry::get('logger')->debug(__METHOD__ . ' - ' . print_r(func_get_args(), true));
0146 
0147         $this->initStoreForSearchEngine($searchIndexId);
0148 
0149         $dataPath = $this->config->path;
0150         $searchIndexEngine = Zend_Search_Lucene::create($dataPath . $searchIndexId);
0151 
0152         $elementsForIndex = $this->fetchElementsForIndex($storeId);
0153 
0154         $this->createSearchIndex($searchIndexEngine, $elementsForIndex);
0155     }
0156 
0157     /**
0158      * @param $searchIndexId
0159      *
0160      * @throws Exception
0161      */
0162     private function initStoreForSearchEngine($searchIndexId)
0163     {
0164         $dataPath = $this->config->path;
0165 
0166         if (false == file_exists($dataPath)) {
0167             throw new Exception('DataPath for search engine does not exist or has no rights: ' . $dataPath);
0168         }
0169         if (false == is_writable($dataPath)) {
0170             throw new Exception('DataPath for search engine not writable: ' . $dataPath);
0171         }
0172         $pathSearchIndex = $dataPath . DIRECTORY_SEPARATOR . $searchIndexId;
0173         if (file_exists($pathSearchIndex)) {
0174             if (false == is_writable($pathSearchIndex)) {
0175                 throw new Exception($dataPath . DIRECTORY_SEPARATOR . $searchIndexId . ' is not writable');
0176             }
0177         } else {
0178             if (false == mkdir($dataPath . $searchIndexId)) {
0179                 throw new Exception($dataPath . $searchIndexId . ' could not created');
0180             }
0181         }
0182     }
0183 
0184     /**
0185      * @param $storeId
0186      *
0187      * @return array
0188      * @throws Zend_Cache_Exception
0189      * @throws Zend_Db_Statement_Exception
0190      * @throws Zend_Exception
0191      */
0192     private function fetchElementsForIndex($storeId)
0193     {
0194         $storeCategories = $this->fetchCategoriesForStore($storeId);
0195 
0196         return $this->fetchElementsForCategories($storeCategories);
0197     }
0198 
0199     /**
0200      * Returns all category ids which stored in database for this storeId. When
0201      * nothing was found, it returns all main categories in database.
0202      *
0203      * @param int $storeId
0204      *
0205      * @return array
0206      * @throws Zend_Cache_Exception
0207      * @throws Zend_Db_Statement_Exception
0208      */
0209     private function fetchCategoriesForStore($storeId)
0210     {
0211         $modelStoreCategories = new Default_Model_DbTable_ConfigStoreCategory();
0212         $resultSet = $modelStoreCategories->fetchAllCategoriesForStore($storeId);
0213         if (count($resultSet) > 0) {
0214             return $resultSet;
0215         }
0216         $modelCategories = new Default_Model_DbTable_ProjectCategory();
0217         $resultSet = $modelCategories->fetchMainCatIdsOrdered();
0218 
0219         return $resultSet;
0220     }
0221 
0222     /**
0223      * @param $storeCategories
0224      *
0225      * @return array
0226      * @throws Zend_Db_Statement_Exception
0227      * @throws Zend_Exception
0228      */
0229     private function fetchElementsForCategories($storeCategories)
0230     {
0231         $modelProduct = new Default_Model_Project();
0232 
0233         return $modelProduct->fetchProductsForCategories($storeCategories);
0234     }
0235 
0236     /**
0237      * @param Zend_Search_Lucene_Interface $searchIndexEngine
0238      * @param array                        $elementsForIndex
0239      */
0240     private function createSearchIndex($searchIndexEngine, $elementsForIndex)
0241     {
0242         foreach ($elementsForIndex as $element) {
0243             $doc = $this->createIndexDocument($element);
0244             $searchIndexEngine->addDocument($doc);
0245         }
0246     }
0247 
0248     /**
0249      * @param array $element
0250      *
0251      * @return Zend_Search_Lucene_Document
0252      */
0253     protected function createIndexDocument($element)
0254     {
0255         $doc = new Zend_Search_Lucene_Document();
0256 
0257         $doc->addField(Zend_Search_Lucene_Field::keyword('project_id', $element['project_id']));
0258         $doc->addField(Zend_Search_Lucene_Field::keyword('member_id', $element['member_id']));
0259         $doc->addField(Zend_Search_Lucene_Field::keyword('project_category_id', $element['project_category_id']));
0260 
0261         $doc->addField(Zend_Search_Lucene_Field::text('title', $element['title'], 'UTF-8'));
0262         $doc->addField(Zend_Search_Lucene_Field::text('description', $element['description'], 'UTF-8'));
0263         $doc->addField(Zend_Search_Lucene_Field::text('username', $element['username'], 'UTF-8'));
0264         $doc->addField(Zend_Search_Lucene_Field::text('category', $element['cat_title'], 'UTF-8'));
0265 
0266         $isUpdate = ($element['type_id'] == Default_Model_DbTable_Project::PROJECT_TYPE_UPDATE);
0267         $helperBuildProductUrl = new Default_View_Helper_BuildProductUrl();
0268         if ($isUpdate) {
0269             $showUrl = $helperBuildProductUrl->buildProductUrl($element['pid']) . '#anker_' . $element['project_id'];
0270             $plingUrl = $helperBuildProductUrl->buildProductUrl($element['pid'], 'pling');
0271         } else {
0272             $showUrl = $helperBuildProductUrl->buildProductUrl($element['project_id']);
0273             $plingUrl = $helperBuildProductUrl->buildProductUrl($element['project_id'], 'pling');
0274         }
0275 
0276         $doc->addField(Zend_Search_Lucene_Field::unIndexed('showUrl', $showUrl));
0277         $doc->addField(Zend_Search_Lucene_Field::unIndexed('plingUrl', $plingUrl));
0278 
0279         $doc->addField(Zend_Search_Lucene_Field::unIndexed('uuid', $element['uuid']));
0280         $doc->addField(Zend_Search_Lucene_Field::unIndexed('type_id', $element['type_id']));
0281         $doc->addField(Zend_Search_Lucene_Field::unIndexed('pid', $element['pid']));
0282         $doc->addField(Zend_Search_Lucene_Field::unIndexed('image_small', $element['image_small']));
0283 
0284         $doc->addField(Zend_Search_Lucene_Field::unIndexed('facebook_code', $element['facebook_code']));
0285         $doc->addField(Zend_Search_Lucene_Field::unIndexed('twitter_code', $element['twitter_code']));
0286         $doc->addField(Zend_Search_Lucene_Field::unIndexed('google_code', $element['google_code']));
0287         $doc->addField(Zend_Search_Lucene_Field::unIndexed('link_1', $element['link_1']));
0288         $doc->addField(Zend_Search_Lucene_Field::unIndexed('ppload_collection_id', $element['ppload_collection_id']));
0289 
0290         $doc->addField(Zend_Search_Lucene_Field::unIndexed('validated', $element['validated']));
0291         $doc->addField(Zend_Search_Lucene_Field::unIndexed('amount', $element['amount']));
0292         $doc->addField(Zend_Search_Lucene_Field::unIndexed('claimable', $element['claimable']));
0293         $doc->addField(Zend_Search_Lucene_Field::unIndexed('claimed_by_member', $element['claimed_by_member']));
0294         $doc->addField(Zend_Search_Lucene_Field::unIndexed('created_at', $element['created_at']));
0295         $doc->addField(Zend_Search_Lucene_Field::unIndexed('changed_at', $element['changed_at']));
0296         $doc->addField(Zend_Search_Lucene_Field::unIndexed('project_changed_at', $element['project_changed_at']));
0297 
0298         $doc->addField(Zend_Search_Lucene_Field::unIndexed('profile_image_url', $element['profile_image_url']));
0299         $doc->addField(Zend_Search_Lucene_Field::unIndexed('paypal_mail', $element['paypal_mail']));
0300         $doc->addField(Zend_Search_Lucene_Field::unIndexed('dwolla_id', $element['dwolla_id']));
0301         $doc->addField(Zend_Search_Lucene_Field::unIndexed('mail', $element['mail']));
0302         $doc->addField(Zend_Search_Lucene_Field::unIndexed('roleId', $element['roleId']));
0303 
0304         $doc->addField(Zend_Search_Lucene_Field::unIndexed('version', $element['version']));
0305         $doc->addField(Zend_Search_Lucene_Field::unIndexed('count_likes', $element['count_likes']));
0306         $doc->addField(Zend_Search_Lucene_Field::unIndexed('count_dislikes', $element['count_dislikes']));
0307         $doc->addField(Zend_Search_Lucene_Field::unIndexed('count_comments', $element['count_comments']));
0308         $doc->addField(Zend_Search_Lucene_Field::unIndexed('count_downloads_hive', $element['count_downloads_hive']));
0309 
0310         //$doc->addField(Zend_Search_Lucene_Field::unIndexed('amount_received', $element['amount_received']));
0311         //$doc->addField(Zend_Search_Lucene_Field::unIndexed('count_plings', $element['count_plings']));
0312         //$doc->addField(Zend_Search_Lucene_Field::unIndexed('count_plingers', $element['count_plingers']));
0313         //$doc->addField(Zend_Search_Lucene_Field::unIndexed('latest_pling', $element['latest_pling']));
0314 
0315         $doc->addField(Zend_Search_Lucene_Field::unIndexed('laplace_score', $element['laplace_score']));
0316 
0317         $doc->addField(Zend_Search_Lucene_Field::unIndexed('source_id', $element['source_id']));
0318         $doc->addField(Zend_Search_Lucene_Field::unIndexed('source_pk', $element['source_pk']));
0319 
0320         return $doc;
0321     }
0322 
0323 }