File indexing completed on 2025-01-19 05:21:27

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Search_Lucene
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** Zend_Search_Lucene_Interface */
0023 // require_once 'Zend/Search/Lucene/Interface.php';
0024 
0025 
0026 /**
0027  * Proxy class intended to be used in userland.
0028  *
0029  * It tracks, when index object goes out of scope and forces ndex closing
0030  *
0031  * @category   Zend
0032  * @package    Zend_Search_Lucene
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 class Zend_Search_Lucene_Proxy implements Zend_Search_Lucene_Interface
0037 {
0038     /**
0039      * Index object
0040      *
0041      * @var Zend_Search_Lucene_Interface
0042      */
0043     private $_index;
0044 
0045     /**
0046      * Object constructor
0047      *
0048      * @param Zend_Search_Lucene_Interface $index
0049      */
0050     public function __construct(Zend_Search_Lucene_Interface $index)
0051     {
0052         $this->_index = $index;
0053         $this->_index->addReference();
0054     }
0055 
0056     /**
0057      * Object destructor
0058      */
0059     public function __destruct()
0060     {
0061         if ($this->_index !== null) {
0062             // This code is invoked if Zend_Search_Lucene_Interface object constructor throws an exception
0063             $this->_index->removeReference();
0064         }
0065         $this->_index = null;
0066     }
0067 
0068     /**
0069      * Get current generation number
0070      *
0071      * Returns generation number
0072      * 0 means pre-2.1 index format
0073      * -1 means there are no segments files.
0074      *
0075      * @param Zend_Search_Lucene_Storage_Directory $directory
0076      * @return integer
0077      * @throws Zend_Search_Lucene_Exception
0078      */
0079     public static function getActualGeneration(Zend_Search_Lucene_Storage_Directory $directory)
0080     {
0081         Zend_Search_Lucene::getActualGeneration($directory);
0082     }
0083 
0084     /**
0085      * Get segments file name
0086      *
0087      * @param integer $generation
0088      * @return string
0089      */
0090     public static function getSegmentFileName($generation)
0091     {
0092         Zend_Search_Lucene::getSegmentFileName($generation);
0093     }
0094 
0095     /**
0096      * Get index format version
0097      *
0098      * @return integer
0099      */
0100     public function getFormatVersion()
0101     {
0102         return $this->_index->getFormatVersion();
0103     }
0104 
0105     /**
0106      * Set index format version.
0107      * Index is converted to this format at the nearest upfdate time
0108      *
0109      * @param int $formatVersion
0110      * @throws Zend_Search_Lucene_Exception
0111      */
0112     public function setFormatVersion($formatVersion)
0113     {
0114         $this->_index->setFormatVersion($formatVersion);
0115     }
0116 
0117     /**
0118      * Returns the Zend_Search_Lucene_Storage_Directory instance for this index.
0119      *
0120      * @return Zend_Search_Lucene_Storage_Directory
0121      */
0122     public function getDirectory()
0123     {
0124         return $this->_index->getDirectory();
0125     }
0126 
0127     /**
0128      * Returns the total number of documents in this index (including deleted documents).
0129      *
0130      * @return integer
0131      */
0132     public function count()
0133     {
0134         return $this->_index->count();
0135     }
0136 
0137     /**
0138      * Returns one greater than the largest possible document number.
0139      * This may be used to, e.g., determine how big to allocate a structure which will have
0140      * an element for every document number in an index.
0141      *
0142      * @return integer
0143      */
0144     public function maxDoc()
0145     {
0146         return $this->_index->maxDoc();
0147     }
0148 
0149     /**
0150      * Returns the total number of non-deleted documents in this index.
0151      *
0152      * @return integer
0153      */
0154     public function numDocs()
0155     {
0156         return $this->_index->numDocs();
0157     }
0158 
0159     /**
0160      * Checks, that document is deleted
0161      *
0162      * @param integer $id
0163      * @return boolean
0164      * @throws Zend_Search_Lucene_Exception    Exception is thrown if $id is out of the range
0165      */
0166     public function isDeleted($id)
0167     {
0168         return $this->_index->isDeleted($id);
0169     }
0170 
0171     /**
0172      * Set default search field.
0173      *
0174      * Null means, that search is performed through all fields by default
0175      *
0176      * Default value is null
0177      *
0178      * @param string $fieldName
0179      */
0180     public static function setDefaultSearchField($fieldName)
0181     {
0182         Zend_Search_Lucene::setDefaultSearchField($fieldName);
0183     }
0184 
0185     /**
0186      * Get default search field.
0187      *
0188      * Null means, that search is performed through all fields by default
0189      *
0190      * @return string
0191      */
0192     public static function getDefaultSearchField()
0193     {
0194         return Zend_Search_Lucene::getDefaultSearchField();
0195     }
0196 
0197     /**
0198      * Set result set limit.
0199      *
0200      * 0 (default) means no limit
0201      *
0202      * @param integer $limit
0203      */
0204     public static function setResultSetLimit($limit)
0205     {
0206         Zend_Search_Lucene::setResultSetLimit($limit);
0207     }
0208 
0209     /**
0210      * Set result set limit.
0211      *
0212      * 0 means no limit
0213      *
0214      * @return integer
0215      */
0216     public static function getResultSetLimit()
0217     {
0218         return Zend_Search_Lucene::getResultSetLimit();
0219     }
0220 
0221     /**
0222      * Retrieve index maxBufferedDocs option
0223      *
0224      * maxBufferedDocs is a minimal number of documents required before
0225      * the buffered in-memory documents are written into a new Segment
0226      *
0227      * Default value is 10
0228      *
0229      * @return integer
0230      */
0231     public function getMaxBufferedDocs()
0232     {
0233         return $this->_index->getMaxBufferedDocs();
0234     }
0235 
0236     /**
0237      * Set index maxBufferedDocs option
0238      *
0239      * maxBufferedDocs is a minimal number of documents required before
0240      * the buffered in-memory documents are written into a new Segment
0241      *
0242      * Default value is 10
0243      *
0244      * @param integer $maxBufferedDocs
0245      */
0246     public function setMaxBufferedDocs($maxBufferedDocs)
0247     {
0248         $this->_index->setMaxBufferedDocs($maxBufferedDocs);
0249     }
0250 
0251 
0252     /**
0253      * Retrieve index maxMergeDocs option
0254      *
0255      * maxMergeDocs is a largest number of documents ever merged by addDocument().
0256      * Small values (e.g., less than 10,000) are best for interactive indexing,
0257      * as this limits the length of pauses while indexing to a few seconds.
0258      * Larger values are best for batched indexing and speedier searches.
0259      *
0260      * Default value is PHP_INT_MAX
0261      *
0262      * @return integer
0263      */
0264     public function getMaxMergeDocs()
0265     {
0266         return $this->_index->getMaxMergeDocs();
0267     }
0268 
0269     /**
0270      * Set index maxMergeDocs option
0271      *
0272      * maxMergeDocs is a largest number of documents ever merged by addDocument().
0273      * Small values (e.g., less than 10,000) are best for interactive indexing,
0274      * as this limits the length of pauses while indexing to a few seconds.
0275      * Larger values are best for batched indexing and speedier searches.
0276      *
0277      * Default value is PHP_INT_MAX
0278      *
0279      * @param integer $maxMergeDocs
0280      */
0281     public function setMaxMergeDocs($maxMergeDocs)
0282     {
0283         $this->_index->setMaxMergeDocs($maxMergeDocs);
0284     }
0285 
0286 
0287     /**
0288      * Retrieve index mergeFactor option
0289      *
0290      * mergeFactor determines how often segment indices are merged by addDocument().
0291      * With smaller values, less RAM is used while indexing,
0292      * and searches on unoptimized indices are faster,
0293      * but indexing speed is slower.
0294      * With larger values, more RAM is used during indexing,
0295      * and while searches on unoptimized indices are slower,
0296      * indexing is faster.
0297      * Thus larger values (> 10) are best for batch index creation,
0298      * and smaller values (< 10) for indices that are interactively maintained.
0299      *
0300      * Default value is 10
0301      *
0302      * @return integer
0303      */
0304     public function getMergeFactor()
0305     {
0306         return $this->_index->getMergeFactor();
0307     }
0308 
0309     /**
0310      * Set index mergeFactor option
0311      *
0312      * mergeFactor determines how often segment indices are merged by addDocument().
0313      * With smaller values, less RAM is used while indexing,
0314      * and searches on unoptimized indices are faster,
0315      * but indexing speed is slower.
0316      * With larger values, more RAM is used during indexing,
0317      * and while searches on unoptimized indices are slower,
0318      * indexing is faster.
0319      * Thus larger values (> 10) are best for batch index creation,
0320      * and smaller values (< 10) for indices that are interactively maintained.
0321      *
0322      * Default value is 10
0323      *
0324      * @param integer $maxMergeDocs
0325      */
0326     public function setMergeFactor($mergeFactor)
0327     {
0328         $this->_index->setMergeFactor($mergeFactor);
0329     }
0330 
0331     /**
0332      * Performs a query against the index and returns an array
0333      * of Zend_Search_Lucene_Search_QueryHit objects.
0334      * Input is a string or Zend_Search_Lucene_Search_Query.
0335      *
0336      * @param mixed $query
0337      * @return array Zend_Search_Lucene_Search_QueryHit
0338      * @throws Zend_Search_Lucene_Exception
0339      */
0340     public function find($query)
0341     {
0342         // actual parameter list
0343         $parameters = func_get_args();
0344 
0345         // invoke $this->_index->find() method with specified parameters
0346         return call_user_func_array(array(&$this->_index, 'find'), $parameters);
0347     }
0348 
0349     /**
0350      * Returns a list of all unique field names that exist in this index.
0351      *
0352      * @param boolean $indexed
0353      * @return array
0354      */
0355     public function getFieldNames($indexed = false)
0356     {
0357         return $this->_index->getFieldNames($indexed);
0358     }
0359 
0360     /**
0361      * Returns a Zend_Search_Lucene_Document object for the document
0362      * number $id in this index.
0363      *
0364      * @param integer|Zend_Search_Lucene_Search_QueryHit $id
0365      * @return Zend_Search_Lucene_Document
0366      */
0367     public function getDocument($id)
0368     {
0369         return $this->_index->getDocument($id);
0370     }
0371 
0372     /**
0373      * Returns true if index contain documents with specified term.
0374      *
0375      * Is used for query optimization.
0376      *
0377      * @param Zend_Search_Lucene_Index_Term $term
0378      * @return boolean
0379      */
0380     public function hasTerm(Zend_Search_Lucene_Index_Term $term)
0381     {
0382         return $this->_index->hasTerm($term);
0383     }
0384 
0385     /**
0386      * Returns IDs of all the documents containing term.
0387      *
0388      * @param Zend_Search_Lucene_Index_Term $term
0389      * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
0390      * @return array
0391      */
0392     public function termDocs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
0393     {
0394         return $this->_index->termDocs($term, $docsFilter);
0395     }
0396 
0397     /**
0398      * Returns documents filter for all documents containing term.
0399      *
0400      * It performs the same operation as termDocs, but return result as
0401      * Zend_Search_Lucene_Index_DocsFilter object
0402      *
0403      * @param Zend_Search_Lucene_Index_Term $term
0404      * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
0405      * @return Zend_Search_Lucene_Index_DocsFilter
0406      */
0407     public function termDocsFilter(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
0408     {
0409         return $this->_index->termDocsFilter($term, $docsFilter);
0410     }
0411 
0412     /**
0413      * Returns an array of all term freqs.
0414      * Return array structure: array( docId => freq, ...)
0415      *
0416      * @param Zend_Search_Lucene_Index_Term $term
0417      * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
0418      * @return integer
0419      */
0420     public function termFreqs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
0421     {
0422         return $this->_index->termFreqs($term, $docsFilter);
0423     }
0424 
0425     /**
0426      * Returns an array of all term positions in the documents.
0427      * Return array structure: array( docId => array( pos1, pos2, ...), ...)
0428      *
0429      * @param Zend_Search_Lucene_Index_Term $term
0430      * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
0431      * @return array
0432      */
0433     public function termPositions(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
0434     {
0435         return $this->_index->termPositions($term, $docsFilter);
0436     }
0437 
0438     /**
0439      * Returns the number of documents in this index containing the $term.
0440      *
0441      * @param Zend_Search_Lucene_Index_Term $term
0442      * @return integer
0443      */
0444     public function docFreq(Zend_Search_Lucene_Index_Term $term)
0445     {
0446         return $this->_index->docFreq($term);
0447     }
0448 
0449     /**
0450      * Retrive similarity used by index reader
0451      *
0452      * @return Zend_Search_Lucene_Search_Similarity
0453      */
0454     public function getSimilarity()
0455     {
0456         return $this->_index->getSimilarity();
0457     }
0458 
0459     /**
0460      * Returns a normalization factor for "field, document" pair.
0461      *
0462      * @param integer $id
0463      * @param string $fieldName
0464      * @return float
0465      */
0466     public function norm($id, $fieldName)
0467     {
0468         return $this->_index->norm($id, $fieldName);
0469     }
0470 
0471     /**
0472      * Returns true if any documents have been deleted from this index.
0473      *
0474      * @return boolean
0475      */
0476     public function hasDeletions()
0477     {
0478         return $this->_index->hasDeletions();
0479     }
0480 
0481     /**
0482      * Deletes a document from the index.
0483      * $id is an internal document id
0484      *
0485      * @param integer|Zend_Search_Lucene_Search_QueryHit $id
0486      * @throws Zend_Search_Lucene_Exception
0487      */
0488     public function delete($id)
0489     {
0490         return $this->_index->delete($id);
0491     }
0492 
0493     /**
0494      * Adds a document to this index.
0495      *
0496      * @param Zend_Search_Lucene_Document $document
0497      */
0498     public function addDocument(Zend_Search_Lucene_Document $document)
0499     {
0500         $this->_index->addDocument($document);
0501     }
0502 
0503     /**
0504      * Commit changes resulting from delete() or undeleteAll() operations.
0505      */
0506     public function commit()
0507     {
0508         $this->_index->commit();
0509     }
0510 
0511     /**
0512      * Optimize index.
0513      *
0514      * Merges all segments into one
0515      */
0516     public function optimize()
0517     {
0518         $this->_index->optimize();
0519     }
0520 
0521     /**
0522      * Returns an array of all terms in this index.
0523      *
0524      * @return array
0525      */
0526     public function terms()
0527     {
0528         return $this->_index->terms();
0529     }
0530 
0531 
0532     /**
0533      * Reset terms stream.
0534      */
0535     public function resetTermsStream()
0536     {
0537         $this->_index->resetTermsStream();
0538     }
0539 
0540     /**
0541      * Skip terms stream up to specified term preffix.
0542      *
0543      * Prefix contains fully specified field info and portion of searched term
0544      *
0545      * @param Zend_Search_Lucene_Index_Term $prefix
0546      */
0547     public function skipTo(Zend_Search_Lucene_Index_Term $prefix)
0548     {
0549         return $this->_index->skipTo($prefix);
0550     }
0551 
0552     /**
0553      * Scans terms dictionary and returns next term
0554      *
0555      * @return Zend_Search_Lucene_Index_Term|null
0556      */
0557     public function nextTerm()
0558     {
0559         return $this->_index->nextTerm();
0560     }
0561 
0562     /**
0563      * Returns term in current position
0564      *
0565      * @return Zend_Search_Lucene_Index_Term|null
0566      */
0567     public function currentTerm()
0568     {
0569         return $this->_index->currentTerm();
0570     }
0571 
0572     /**
0573      * Close terms stream
0574      *
0575      * Should be used for resources clean up if stream is not read up to the end
0576      */
0577     public function closeTermsStream()
0578     {
0579         $this->_index->closeTermsStream();
0580     }
0581 
0582 
0583     /**
0584      * Undeletes all documents currently marked as deleted in this index.
0585      */
0586     public function undeleteAll()
0587     {
0588         return $this->_index->undeleteAll();
0589     }
0590 
0591     /**
0592      * Add reference to the index object
0593      *
0594      * @internal
0595      */
0596     public function addReference()
0597     {
0598         return $this->_index->addReference();
0599     }
0600 
0601     /**
0602      * Remove reference from the index object
0603      *
0604      * When reference count becomes zero, index is closed and resources are cleaned up
0605      *
0606      * @internal
0607      */
0608     public function removeReference()
0609     {
0610         return $this->_index->removeReference();
0611     }
0612 }