Completed
Push — scrutinizer ( c2ef4a...84e9d0 )
by Fabio
22:07
created
demos/quickstart/protected/controls/Comments/CommentBlock.php 3 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -93,6 +93,9 @@
 block discarded – undo
93 93
 		$param->IsValid = $this->password->Text == 'Prado';
94 94
 	}
95 95
 
96
+	/**
97
+	 * @param CommentRecord $record
98
+	 */
96 99
 	protected function format_message($record)
97 100
 	{
98 101
 		$username=htmlspecialchars($record->username);
Please login to merge, or discard this patch.
Spacing   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -10,7 +10,7 @@  discard block
 block discarded – undo
10 10
 
11 11
 class CommentRecord extends TActiveRecord
12 12
 {
13
-	const TABLE='qs_comments';
13
+	const TABLE = 'qs_comments';
14 14
 
15 15
 	public $id;
16 16
 	public $username;
@@ -19,7 +19,7 @@  discard block
 block discarded – undo
19 19
 	public $block_id;
20 20
 	public $content;
21 21
 
22
-	public static function finder($className=__CLASS__)
22
+	public static function finder($className = __CLASS__)
23 23
 	{
24 24
 		return parent::finder($className);
25 25
 	}
@@ -31,18 +31,18 @@  discard block
 block discarded – undo
31 31
 
32 32
 	function onLoad($param)
33 33
 	{
34
-		if(!$this->Page->IsCallBack)
34
+		if (!$this->Page->IsCallBack)
35 35
 		{
36 36
 			$count = array();
37 37
 			$data = $this->getCommentData();
38
-			foreach($data as $r)
38
+			foreach ($data as $r)
39 39
 			{
40
-				if(!isset($count[$r->block_id]))
41
-					$count[$r->block_id]=0;
40
+				if (!isset($count[$r->block_id]))
41
+					$count[$r->block_id] = 0;
42 42
 				$count[$r->block_id]++;
43 43
 			}
44
-			$js = "var comment_count = ".TJavascript::encode($count).";\n";
45
-			$this->Page->ClientScript->registerBeginScript('count',$js);
44
+			$js = "var comment_count = " . TJavascript::encode($count) . ";\n";
45
+			$this->Page->ClientScript->registerBeginScript('count', $js);
46 46
 			$this->comments->dataSource = $data;
47 47
 			$this->comments->dataBind();
48 48
 		}
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 
56 56
 	function add_comment($sender, $param)
57 57
 	{
58
-		if(!$this->Page->IsValid)
58
+		if (!$this->Page->IsValid)
59 59
 			return;
60 60
 		$record = new CommentRecord;
61 61
 		$record->username = $this->username->Text;
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
 		$cc->appendContent('comment-list', $this->format_message($record));
72 72
 		$cc->callClientFunction('hide_add_comment');
73 73
 		$cc->callClientFunction('increment_count_tag', $record->block_id);
74
-		if(!$this->Page->IsCallBack)
74
+		if (!$this->Page->IsCallBack)
75 75
 		{
76 76
 			$this->comments->dataSource = $this->getCommentData();
77 77
 			$this->comments->dataBind();
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
 
81 81
 	protected function getCurrentPagePath()
82 82
 	{
83
-		if(is_null($this->_page))
83
+		if (is_null($this->_page))
84 84
 		{
85 85
 			$page = str_replace($this->Service->BasePath, '', $this->Page->Template->TemplateFile);
86 86
 			$this->_page = str_replace('\\', '/', $page);
@@ -95,8 +95,8 @@  discard block
 block discarded – undo
95 95
 
96 96
 	protected function format_message($record)
97 97
 	{
98
-		$username=htmlspecialchars($record->username);
99
-		$content=nl2br(htmlspecialchars($record->content));
98
+		$username = htmlspecialchars($record->username);
99
+		$content = nl2br(htmlspecialchars($record->content));
100 100
 		return <<<EOD
101 101
 	<div class="comment c-{$record->block_id}">
102 102
 		<span><strong>{$username}</strong> on {$record->date_added}.</span>
Please login to merge, or discard this patch.
Braces   +6 added lines, -4 removed lines patch added patch discarded remove patch
@@ -37,8 +37,9 @@  discard block
 block discarded – undo
37 37
 			$data = $this->getCommentData();
38 38
 			foreach($data as $r)
39 39
 			{
40
-				if(!isset($count[$r->block_id]))
41
-					$count[$r->block_id]=0;
40
+				if(!isset($count[$r->block_id])) {
41
+									$count[$r->block_id]=0;
42
+				}
42 43
 				$count[$r->block_id]++;
43 44
 			}
44 45
 			$js = "var comment_count = ".TJavascript::encode($count).";\n";
@@ -55,8 +56,9 @@  discard block
 block discarded – undo
55 56
 
56 57
 	function add_comment($sender, $param)
57 58
 	{
58
-		if(!$this->Page->IsValid)
59
-			return;
59
+		if(!$this->Page->IsValid) {
60
+					return;
61
+		}
60 62
 		$record = new CommentRecord;
61 63
 		$record->username = $this->username->Text;
62 64
 		$record->date_added = date('Y-m-d h:i:s');
Please login to merge, or discard this patch.
demos/quickstart/protected/index/Zend/Search/Lucene.php 4 patches
Doc Comments   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -268,7 +268,7 @@  discard block
 block discarded – undo
268 268
      * Returns a Zend_Search_Lucene_Document object for the document
269 269
      * number $id in this index.
270 270
      *
271
-     * @param integer|Zend_Search_Lucene_Search_QueryHit $id
271
+     * @param integer $id
272 272
      * @return Zend_Search_Lucene_Document
273 273
      */
274 274
     public function getDocument($id)
@@ -553,7 +553,6 @@  discard block
 block discarded – undo
553 553
      * or the number of the document to delete.
554 554
      *
555 555
      * @todo Implementation
556
-     * @param mixed $item_to_del
557 556
      */
558 557
     public function delete($doc)
559 558
     {}
Please login to merge, or discard this patch.
Indentation   +504 added lines, -504 removed lines patch added patch discarded remove patch
@@ -59,511 +59,511 @@
 block discarded – undo
59 59
  */
60 60
 class Zend_Search_Lucene
61 61
 {
62
-    /**
63
-     * File system adapter.
64
-     *
65
-     * @var Zend_Search_Lucene_Storage_Directory
66
-     */
67
-    private $_directory = null;
68
-
69
-    /**
70
-     * File system adapter closing option
71
-     *
72
-     * @var boolean
73
-     */
74
-    private $_closeDirOnExit = true;
75
-
76
-    /**
77
-     * Writer for this index, not instantiated unless required.
78
-     *
79
-     * @var Zend_Search_Lucene_Index_Writer
80
-     */
81
-    private $_writer = null;
82
-
83
-    /**
84
-     * Array of Zend_Search_Lucene_Index_SegmentInfo objects for this index.
85
-     *
86
-     * @var array Zend_Search_Lucene_Index_SegmentInfo
87
-     */
88
-    private $_segmentInfos = array();
89
-
90
-    /**
91
-     * Number of documents in this index.
92
-     *
93
-     * @var integer
94
-     */
95
-    private $_docCount = 0;
96
-
97
-
98
-    /**
99
-     * Opens the index.
100
-     *
101
-     * IndexReader constructor needs Directory as a parameter. It should be
102
-     * a string with a path to the index folder or a Directory object.
103
-     *
104
-     * @param mixed $directory
105
-     * @throws Zend_Search_Lucene_Exception
106
-     */
107
-    public function __construct($directory = null, $create = false)
108
-    {
109
-        if ($directory === null) {
110
-            throw new Zend_Search_Exception('No index directory specified');
111
-        }
112
-
113
-        if ($directory instanceof Zend_Search_Lucene_Storage_Directory_Filesystem) {
114
-            $this->_directory      = $directory;
115
-            $this->_closeDirOnExit = false;
116
-        } else {
117
-            $this->_directory      = new Zend_Search_Lucene_Storage_Directory_Filesystem($directory);
118
-            $this->_closeDirOnExit = true;
119
-        }
120
-
121
-        if ($create) {
122
-            $this->_writer = new Zend_Search_Lucene_Index_Writer($this->_directory, true);
123
-        } else {
124
-            $this->_writer = null;
125
-        }
126
-
127
-        $this->_segmentInfos = array();
128
-
129
-        $segmentsFile = $this->_directory->getFileObject('segments');
130
-
131
-        $format = $segmentsFile->readInt();
132
-
133
-        if ($format != (int)0xFFFFFFFF) {
134
-            throw new Zend_Search_Lucene_Exception('Wrong segments file format');
135
-        }
136
-
137
-        // read version
138
-        $segmentsFile->readLong();
139
-
140
-        // read counter
141
-        $segmentsFile->readInt();
142
-
143
-        $segments = $segmentsFile->readInt();
144
-
145
-        $this->_docCount = 0;
146
-
147
-        // read segmentInfos
148
-        for ($count = 0; $count < $segments; $count++) {
149
-            $segName = $segmentsFile->readString();
150
-            $segSize = $segmentsFile->readInt();
151
-            $this->_docCount += $segSize;
152
-
153
-            $this->_segmentInfos[$count] =
154
-                                new Zend_Search_Lucene_Index_SegmentInfo($segName,
155
-                                                                         $segSize,
156
-                                                                         $this->_directory);
157
-        }
158
-    }
159
-
160
-
161
-    /**
162
-     * Object destructor
163
-     */
164
-    public function __destruct()
165
-    {
166
-        $this->commit();
167
-
168
-        if ($this->_closeDirOnExit) {
169
-            $this->_directory->close();
170
-        }
171
-    }
172
-
173
-    /**
174
-     * Returns an instance of Zend_Search_Lucene_Index_Writer for the index
175
-     *
176
-     * @return Zend_Search_Lucene_Index_Writer
177
-     */
178
-    public function getIndexWriter()
179
-    {
180
-        if (!$this->_writer instanceof Zend_Search_Lucene_Index_Writer) {
181
-            $this->_writer = new Zend_Search_Lucene_Index_Writer($this->_directory);
182
-        }
183
-
184
-        return $this->_writer;
185
-    }
186
-
187
-
188
-    /**
189
-     * Returns the Zend_Search_Lucene_Storage_Directory instance for this index.
190
-     *
191
-     * @return Zend_Search_Lucene_Storage_Directory
192
-     */
193
-    public function getDirectory()
194
-    {
195
-        return $this->_directory;
196
-    }
197
-
198
-
199
-    /**
200
-     * Returns the total number of documents in this index.
201
-     *
202
-     * @return integer
203
-     */
204
-    public function count()
205
-    {
206
-        return $this->_docCount;
207
-    }
208
-
209
-
210
-    /**
211
-     * Performs a query against the index and returns an array
212
-     * of Zend_Search_Lucene_Search_QueryHit objects.
213
-     * Input is a string or Zend_Search_Lucene_Search_Query.
214
-     *
215
-     * @param mixed $query
216
-     * @return array ZSearchHit
217
-     */
218
-    public function find($query)
219
-    {
220
-        if (is_string($query)) {
221
-            $query = Zend_Search_Lucene_Search_QueryParser::parse($query);
222
-        }
223
-
224
-        if (!$query instanceof Zend_Search_Lucene_Search_Query) {
225
-            throw new Zend_Search_Lucene_Exception('Query must be a string or Zend_Search_Lucene_Search_Query object');
226
-        }
227
-
228
-        $this->commit();
229
-
230
-        $hits = array();
231
-        $scores = array();
232
-
233
-        $docNum = $this->count();
234
-        for( $count=0; $count < $docNum; $count++ ) {
235
-            $docScore = $query->score( $count, $this);
236
-            if( $docScore != 0 ) {
237
-                $hit = new Zend_Search_Lucene_Search_QueryHit($this);
238
-                $hit->id = $count;
239
-                $hit->score = $docScore;
240
-
241
-                $hits[] = $hit;
242
-                $scores[] = $docScore;
243
-            }
244
-        }
245
-        array_multisort($scores, SORT_DESC, SORT_REGULAR, $hits);
246
-
247
-        return $hits;
248
-    }
249
-
250
-
251
-    /**
252
-     * Returns a list of all unique field names that exist in this index.
253
-     *
254
-     * @param boolean $indexed
255
-     * @return array
256
-     */
257
-    public function getFieldNames($indexed = false)
258
-    {
259
-        $result = array();
260
-        foreach( $this->_segmentInfos as $segmentInfo ) {
261
-            $result = array_merge($result, $segmentInfo->getFields($indexed));
262
-        }
263
-        return $result;
264
-    }
265
-
266
-
267
-    /**
268
-     * Returns a Zend_Search_Lucene_Document object for the document
269
-     * number $id in this index.
270
-     *
271
-     * @param integer|Zend_Search_Lucene_Search_QueryHit $id
272
-     * @return Zend_Search_Lucene_Document
273
-     */
274
-    public function getDocument($id)
275
-    {
276
-        if ($id instanceof Zend_Search_Lucene_Search_QueryHit) {
277
-            /* @var $id Zend_Search_Lucene_Search_QueryHit */
278
-            $id = $id->id;
279
-        }
280
-
281
-        if ($id >= $this->_docCount) {
282
-            /**
283
-             * @todo exception here?
284
-             */
285
-            return null;
286
-        }
287
-
288
-        $segCount = 0;
289
-        $nextSegmentStartId = $this->_segmentInfos[ 0 ]->count();
290
-        while( $nextSegmentStartId <= $id ) {
291
-               $segCount++;
292
-               $nextSegmentStartId += $this->_segmentInfos[ $segCount ]->count();
293
-        }
294
-        $segmentStartId = $nextSegmentStartId - $this->_segmentInfos[ $segCount ]->count();
295
-
296
-        $fdxFile = $this->_segmentInfos[ $segCount ]->openCompoundFile('.fdx');
297
-        $fdxFile->seek( ($id-$segmentStartId)*8, SEEK_CUR );
298
-        $fieldValuesPosition = $fdxFile->readLong();
299
-
300
-        $fdtFile = $this->_segmentInfos[ $segCount ]->openCompoundFile('.fdt');
301
-        $fdtFile->seek( $fieldValuesPosition, SEEK_CUR );
302
-        $fieldCount = $fdtFile->readVInt();
303
-
304
-        $doc = new Zend_Search_Lucene_Document();
305
-        for( $count = 0; $count < $fieldCount; $count++ ) {
306
-            $fieldNum = $fdtFile->readVInt();
307
-            $bits = $fdtFile->readByte();
308
-
309
-            $fieldInfo = $this->_segmentInfos[ $segCount ]->getField($fieldNum);
310
-
311
-            if( !($bits & 2) ) { // Text data
312
-                $field = new Zend_Search_Lucene_Field($fieldInfo->name,
313
-                                                      $fdtFile->readString(),
314
-                                                      true,
315
-                                                      $fieldInfo->isIndexed,
316
-                                                      $bits & 1 );
317
-            } else {
318
-                $field = new Zend_Search_Lucene_Field($fieldInfo->name,
319
-                                                      $fdtFile->readBinary(),
320
-                                                      true,
321
-                                                      $fieldInfo->isIndexed,
322
-                                                      $bits & 1 );
323
-            }
324
-
325
-            $doc->addField($field);
326
-        }
327
-
328
-        return $doc;
329
-    }
330
-
331
-
332
-    /**
333
-     * Returns an array of all the documents which contain term.
334
-     *
335
-     * @param Zend_Search_Lucene_Index_Term $term
336
-     * @return array
337
-     */
338
-    public function termDocs(Zend_Search_Lucene_Index_Term $term)
339
-    {
340
-        $result = array();
341
-        $segmentStartDocId = 0;
342
-
343
-        foreach ($this->_segmentInfos as $segInfo) {
344
-            $termInfo = $segInfo->getTermInfo($term);
345
-
346
-            if (!$termInfo instanceof Zend_Search_Lucene_Index_TermInfo) {
347
-                $segmentStartDocId += $segInfo->count();
348
-                continue;
349
-            }
350
-
351
-            $frqFile = $segInfo->openCompoundFile('.frq');
352
-            $frqFile->seek($termInfo->freqPointer,SEEK_CUR);
353
-            $docId = 0;
354
-            for( $count=0; $count < $termInfo->docFreq; $count++ ) {
355
-                $docDelta = $frqFile->readVInt();
356
-                if( $docDelta % 2 == 1 ) {
357
-                    $docId += ($docDelta-1)/2;
358
-                } else {
359
-                    $docId += $docDelta/2;
360
-                    // read freq
361
-                    $frqFile->readVInt();
362
-                }
363
-                $result[] = $segmentStartDocId + $docId;
364
-            }
365
-
366
-            $segmentStartDocId += $segInfo->count();
367
-        }
368
-
369
-        return $result;
370
-    }
371
-
372
-
373
-    /**
374
-     * Returns an array of all term positions in the documents.
375
-     * Return array structure: array( docId => array( pos1, pos2, ...), ...)
376
-     *
377
-     * @param Zend_Search_Lucene_Index_Term $term
378
-     * @return array
379
-     */
380
-    public function termPositions(Zend_Search_Lucene_Index_Term $term)
381
-    {
382
-        $result = array();
383
-        $segmentStartDocId = 0;
384
-        foreach( $this->_segmentInfos as $segInfo ) {
385
-            $termInfo = $segInfo->getTermInfo($term);
386
-
387
-            if (!$termInfo instanceof Zend_Search_Lucene_Index_TermInfo) {
388
-                $segmentStartDocId += $segInfo->count();
389
-                continue;
390
-            }
391
-
392
-            $frqFile = $segInfo->openCompoundFile('.frq');
393
-            $frqFile->seek($termInfo->freqPointer,SEEK_CUR);
394
-            $freqs = array();
395
-            $docId = 0;
396
-
397
-            for( $count = 0; $count < $termInfo->docFreq; $count++ ) {
398
-                $docDelta = $frqFile->readVInt();
399
-                if( $docDelta % 2 == 1 ) {
400
-                    $docId += ($docDelta-1)/2;
401
-                    $freqs[ $docId ] = 1;
402
-                } else {
403
-                    $docId += $docDelta/2;
404
-                    $freqs[ $docId ] = $frqFile->readVInt();
405
-                }
406
-            }
407
-
408
-            $prxFile = $segInfo->openCompoundFile('.prx');
409
-            $prxFile->seek($termInfo->proxPointer,SEEK_CUR);
410
-            foreach ($freqs as $docId => $freq) {
411
-                $termPosition = 0;
412
-                $positions = array();
413
-
414
-                for ($count = 0; $count < $freq; $count++ ) {
415
-                    $termPosition += $prxFile->readVInt();
416
-                    $positions[] = $termPosition;
417
-                }
418
-                $result[ $segmentStartDocId + $docId ] = $positions;
419
-            }
420
-
421
-            $segmentStartDocId += $segInfo->count();
422
-        }
423
-
424
-        return $result;
425
-    }
426
-
427
-
428
-    /**
429
-     * Returns the number of documents in this index containing the $term.
430
-     *
431
-     * @param Zend_Search_Lucene_Index_Term $term
432
-     * @return integer
433
-     */
434
-    public function docFreq(Zend_Search_Lucene_Index_Term $term)
435
-    {
436
-        $result = 0;
437
-        foreach ($this->_segmentInfos as $segInfo) {
438
-            $termInfo = $segInfo->getTermInfo($term);
439
-            if ($termInfo !== null) {
440
-                $result += $termInfo->docFreq;
441
-            }
442
-        }
443
-
444
-        return $result;
445
-    }
446
-
447
-
448
-    /**
449
-     * Retrive similarity used by index reader
450
-     *
451
-     * @return Zend_Search_Lucene_Search_Similarity
452
-     */
453
-    public function getSimilarity()
454
-    {
455
-        return Zend_Search_Lucene_Search_Similarity::getDefault();
456
-    }
457
-
458
-
459
-    /**
460
-     * Returns a normalization factor for "field, document" pair.
461
-     *
462
-     * @param integer $id
463
-     * @param string $fieldName
464
-     * @return Zend_Search_Lucene_Document
465
-     */
466
-    public function norm( $id, $fieldName )
467
-    {
468
-        if( $id >= $this->_docCount )
469
-            return null;
470
-
471
-        $segCount = 0;
472
-        $nextSegmentStartId = $this->_segmentInfos[ 0 ]->count();
473
-        while( $nextSegmentStartId <= $id ) {
474
-               $segCount++;
475
-               $nextSegmentStartId += $this->_segmentInfos[ $segCount ]->count();
476
-        }
477
-
478
-        $segmentStartId = $nextSegmentStartId - $this->_segmentInfos[ $segCount ]->count();
479
-
480
-        return $this->_segmentInfos[ $segCount ]->norm($id - $segmentStartId, $fieldName);
481
-    }
482
-
483
-
484
-    /**
485
-     * Adds a document to this index.
486
-     *
487
-     * @param Zend_Search_Lucene_Document $document
488
-     */
489
-    public function addDocument(Zend_Search_Lucene_Document $document)
490
-    {
491
-        if (!$this->_writer instanceof Zend_Search_Lucene_Index_Writer) {
492
-            $this->_writer = new Zend_Search_Lucene_Index_Writer($this->_directory);
493
-        }
494
-
495
-        $this->_writer->addDocument($document);
496
-    }
497
-
498
-
499
-    /**
500
-     * Commit changes resulting from delete() or undeleteAll() operations.
501
-     *
502
-     * @todo delete() and undeleteAll processing.
503
-     */
504
-    public function commit()
505
-    {
506
-        if ($this->_writer !== null) {
507
-            foreach ($this->_writer->commit() as $segmentName => $segmentInfo) {
508
-                if ($segmentInfo !== null) {
509
-                    $this->_segmentInfos[] = $segmentInfo;
510
-                    $this->_docCount += $segmentInfo->count();
511
-                } else {
512
-                    foreach ($this->_segmentInfos as $segId => $segInfo) {
513
-                        if ($segInfo->getName() == $segmentName) {
514
-                            unset($this->_segmentInfos[$segId]);
515
-                        }
516
-                    }
517
-                }
518
-            }
519
-        }
520
-    }
521
-
522
-
523
-    /*************************************************************************
62
+	/**
63
+	 * File system adapter.
64
+	 *
65
+	 * @var Zend_Search_Lucene_Storage_Directory
66
+	 */
67
+	private $_directory = null;
68
+
69
+	/**
70
+	 * File system adapter closing option
71
+	 *
72
+	 * @var boolean
73
+	 */
74
+	private $_closeDirOnExit = true;
75
+
76
+	/**
77
+	 * Writer for this index, not instantiated unless required.
78
+	 *
79
+	 * @var Zend_Search_Lucene_Index_Writer
80
+	 */
81
+	private $_writer = null;
82
+
83
+	/**
84
+	 * Array of Zend_Search_Lucene_Index_SegmentInfo objects for this index.
85
+	 *
86
+	 * @var array Zend_Search_Lucene_Index_SegmentInfo
87
+	 */
88
+	private $_segmentInfos = array();
89
+
90
+	/**
91
+	 * Number of documents in this index.
92
+	 *
93
+	 * @var integer
94
+	 */
95
+	private $_docCount = 0;
96
+
97
+
98
+	/**
99
+	 * Opens the index.
100
+	 *
101
+	 * IndexReader constructor needs Directory as a parameter. It should be
102
+	 * a string with a path to the index folder or a Directory object.
103
+	 *
104
+	 * @param mixed $directory
105
+	 * @throws Zend_Search_Lucene_Exception
106
+	 */
107
+	public function __construct($directory = null, $create = false)
108
+	{
109
+		if ($directory === null) {
110
+			throw new Zend_Search_Exception('No index directory specified');
111
+		}
112
+
113
+		if ($directory instanceof Zend_Search_Lucene_Storage_Directory_Filesystem) {
114
+			$this->_directory      = $directory;
115
+			$this->_closeDirOnExit = false;
116
+		} else {
117
+			$this->_directory      = new Zend_Search_Lucene_Storage_Directory_Filesystem($directory);
118
+			$this->_closeDirOnExit = true;
119
+		}
120
+
121
+		if ($create) {
122
+			$this->_writer = new Zend_Search_Lucene_Index_Writer($this->_directory, true);
123
+		} else {
124
+			$this->_writer = null;
125
+		}
126
+
127
+		$this->_segmentInfos = array();
128
+
129
+		$segmentsFile = $this->_directory->getFileObject('segments');
130
+
131
+		$format = $segmentsFile->readInt();
132
+
133
+		if ($format != (int)0xFFFFFFFF) {
134
+			throw new Zend_Search_Lucene_Exception('Wrong segments file format');
135
+		}
136
+
137
+		// read version
138
+		$segmentsFile->readLong();
139
+
140
+		// read counter
141
+		$segmentsFile->readInt();
142
+
143
+		$segments = $segmentsFile->readInt();
144
+
145
+		$this->_docCount = 0;
146
+
147
+		// read segmentInfos
148
+		for ($count = 0; $count < $segments; $count++) {
149
+			$segName = $segmentsFile->readString();
150
+			$segSize = $segmentsFile->readInt();
151
+			$this->_docCount += $segSize;
152
+
153
+			$this->_segmentInfos[$count] =
154
+								new Zend_Search_Lucene_Index_SegmentInfo($segName,
155
+																		 $segSize,
156
+																		 $this->_directory);
157
+		}
158
+	}
159
+
160
+
161
+	/**
162
+	 * Object destructor
163
+	 */
164
+	public function __destruct()
165
+	{
166
+		$this->commit();
167
+
168
+		if ($this->_closeDirOnExit) {
169
+			$this->_directory->close();
170
+		}
171
+	}
172
+
173
+	/**
174
+	 * Returns an instance of Zend_Search_Lucene_Index_Writer for the index
175
+	 *
176
+	 * @return Zend_Search_Lucene_Index_Writer
177
+	 */
178
+	public function getIndexWriter()
179
+	{
180
+		if (!$this->_writer instanceof Zend_Search_Lucene_Index_Writer) {
181
+			$this->_writer = new Zend_Search_Lucene_Index_Writer($this->_directory);
182
+		}
183
+
184
+		return $this->_writer;
185
+	}
186
+
187
+
188
+	/**
189
+	 * Returns the Zend_Search_Lucene_Storage_Directory instance for this index.
190
+	 *
191
+	 * @return Zend_Search_Lucene_Storage_Directory
192
+	 */
193
+	public function getDirectory()
194
+	{
195
+		return $this->_directory;
196
+	}
197
+
198
+
199
+	/**
200
+	 * Returns the total number of documents in this index.
201
+	 *
202
+	 * @return integer
203
+	 */
204
+	public function count()
205
+	{
206
+		return $this->_docCount;
207
+	}
208
+
209
+
210
+	/**
211
+	 * Performs a query against the index and returns an array
212
+	 * of Zend_Search_Lucene_Search_QueryHit objects.
213
+	 * Input is a string or Zend_Search_Lucene_Search_Query.
214
+	 *
215
+	 * @param mixed $query
216
+	 * @return array ZSearchHit
217
+	 */
218
+	public function find($query)
219
+	{
220
+		if (is_string($query)) {
221
+			$query = Zend_Search_Lucene_Search_QueryParser::parse($query);
222
+		}
223
+
224
+		if (!$query instanceof Zend_Search_Lucene_Search_Query) {
225
+			throw new Zend_Search_Lucene_Exception('Query must be a string or Zend_Search_Lucene_Search_Query object');
226
+		}
227
+
228
+		$this->commit();
229
+
230
+		$hits = array();
231
+		$scores = array();
232
+
233
+		$docNum = $this->count();
234
+		for( $count=0; $count < $docNum; $count++ ) {
235
+			$docScore = $query->score( $count, $this);
236
+			if( $docScore != 0 ) {
237
+				$hit = new Zend_Search_Lucene_Search_QueryHit($this);
238
+				$hit->id = $count;
239
+				$hit->score = $docScore;
240
+
241
+				$hits[] = $hit;
242
+				$scores[] = $docScore;
243
+			}
244
+		}
245
+		array_multisort($scores, SORT_DESC, SORT_REGULAR, $hits);
246
+
247
+		return $hits;
248
+	}
249
+
250
+
251
+	/**
252
+	 * Returns a list of all unique field names that exist in this index.
253
+	 *
254
+	 * @param boolean $indexed
255
+	 * @return array
256
+	 */
257
+	public function getFieldNames($indexed = false)
258
+	{
259
+		$result = array();
260
+		foreach( $this->_segmentInfos as $segmentInfo ) {
261
+			$result = array_merge($result, $segmentInfo->getFields($indexed));
262
+		}
263
+		return $result;
264
+	}
265
+
266
+
267
+	/**
268
+	 * Returns a Zend_Search_Lucene_Document object for the document
269
+	 * number $id in this index.
270
+	 *
271
+	 * @param integer|Zend_Search_Lucene_Search_QueryHit $id
272
+	 * @return Zend_Search_Lucene_Document
273
+	 */
274
+	public function getDocument($id)
275
+	{
276
+		if ($id instanceof Zend_Search_Lucene_Search_QueryHit) {
277
+			/* @var $id Zend_Search_Lucene_Search_QueryHit */
278
+			$id = $id->id;
279
+		}
280
+
281
+		if ($id >= $this->_docCount) {
282
+			/**
283
+			 * @todo exception here?
284
+			 */
285
+			return null;
286
+		}
287
+
288
+		$segCount = 0;
289
+		$nextSegmentStartId = $this->_segmentInfos[ 0 ]->count();
290
+		while( $nextSegmentStartId <= $id ) {
291
+			   $segCount++;
292
+			   $nextSegmentStartId += $this->_segmentInfos[ $segCount ]->count();
293
+		}
294
+		$segmentStartId = $nextSegmentStartId - $this->_segmentInfos[ $segCount ]->count();
295
+
296
+		$fdxFile = $this->_segmentInfos[ $segCount ]->openCompoundFile('.fdx');
297
+		$fdxFile->seek( ($id-$segmentStartId)*8, SEEK_CUR );
298
+		$fieldValuesPosition = $fdxFile->readLong();
299
+
300
+		$fdtFile = $this->_segmentInfos[ $segCount ]->openCompoundFile('.fdt');
301
+		$fdtFile->seek( $fieldValuesPosition, SEEK_CUR );
302
+		$fieldCount = $fdtFile->readVInt();
303
+
304
+		$doc = new Zend_Search_Lucene_Document();
305
+		for( $count = 0; $count < $fieldCount; $count++ ) {
306
+			$fieldNum = $fdtFile->readVInt();
307
+			$bits = $fdtFile->readByte();
308
+
309
+			$fieldInfo = $this->_segmentInfos[ $segCount ]->getField($fieldNum);
310
+
311
+			if( !($bits & 2) ) { // Text data
312
+				$field = new Zend_Search_Lucene_Field($fieldInfo->name,
313
+													  $fdtFile->readString(),
314
+													  true,
315
+													  $fieldInfo->isIndexed,
316
+													  $bits & 1 );
317
+			} else {
318
+				$field = new Zend_Search_Lucene_Field($fieldInfo->name,
319
+													  $fdtFile->readBinary(),
320
+													  true,
321
+													  $fieldInfo->isIndexed,
322
+													  $bits & 1 );
323
+			}
324
+
325
+			$doc->addField($field);
326
+		}
327
+
328
+		return $doc;
329
+	}
330
+
331
+
332
+	/**
333
+	 * Returns an array of all the documents which contain term.
334
+	 *
335
+	 * @param Zend_Search_Lucene_Index_Term $term
336
+	 * @return array
337
+	 */
338
+	public function termDocs(Zend_Search_Lucene_Index_Term $term)
339
+	{
340
+		$result = array();
341
+		$segmentStartDocId = 0;
342
+
343
+		foreach ($this->_segmentInfos as $segInfo) {
344
+			$termInfo = $segInfo->getTermInfo($term);
345
+
346
+			if (!$termInfo instanceof Zend_Search_Lucene_Index_TermInfo) {
347
+				$segmentStartDocId += $segInfo->count();
348
+				continue;
349
+			}
350
+
351
+			$frqFile = $segInfo->openCompoundFile('.frq');
352
+			$frqFile->seek($termInfo->freqPointer,SEEK_CUR);
353
+			$docId = 0;
354
+			for( $count=0; $count < $termInfo->docFreq; $count++ ) {
355
+				$docDelta = $frqFile->readVInt();
356
+				if( $docDelta % 2 == 1 ) {
357
+					$docId += ($docDelta-1)/2;
358
+				} else {
359
+					$docId += $docDelta/2;
360
+					// read freq
361
+					$frqFile->readVInt();
362
+				}
363
+				$result[] = $segmentStartDocId + $docId;
364
+			}
365
+
366
+			$segmentStartDocId += $segInfo->count();
367
+		}
368
+
369
+		return $result;
370
+	}
371
+
372
+
373
+	/**
374
+	 * Returns an array of all term positions in the documents.
375
+	 * Return array structure: array( docId => array( pos1, pos2, ...), ...)
376
+	 *
377
+	 * @param Zend_Search_Lucene_Index_Term $term
378
+	 * @return array
379
+	 */
380
+	public function termPositions(Zend_Search_Lucene_Index_Term $term)
381
+	{
382
+		$result = array();
383
+		$segmentStartDocId = 0;
384
+		foreach( $this->_segmentInfos as $segInfo ) {
385
+			$termInfo = $segInfo->getTermInfo($term);
386
+
387
+			if (!$termInfo instanceof Zend_Search_Lucene_Index_TermInfo) {
388
+				$segmentStartDocId += $segInfo->count();
389
+				continue;
390
+			}
391
+
392
+			$frqFile = $segInfo->openCompoundFile('.frq');
393
+			$frqFile->seek($termInfo->freqPointer,SEEK_CUR);
394
+			$freqs = array();
395
+			$docId = 0;
396
+
397
+			for( $count = 0; $count < $termInfo->docFreq; $count++ ) {
398
+				$docDelta = $frqFile->readVInt();
399
+				if( $docDelta % 2 == 1 ) {
400
+					$docId += ($docDelta-1)/2;
401
+					$freqs[ $docId ] = 1;
402
+				} else {
403
+					$docId += $docDelta/2;
404
+					$freqs[ $docId ] = $frqFile->readVInt();
405
+				}
406
+			}
407
+
408
+			$prxFile = $segInfo->openCompoundFile('.prx');
409
+			$prxFile->seek($termInfo->proxPointer,SEEK_CUR);
410
+			foreach ($freqs as $docId => $freq) {
411
+				$termPosition = 0;
412
+				$positions = array();
413
+
414
+				for ($count = 0; $count < $freq; $count++ ) {
415
+					$termPosition += $prxFile->readVInt();
416
+					$positions[] = $termPosition;
417
+				}
418
+				$result[ $segmentStartDocId + $docId ] = $positions;
419
+			}
420
+
421
+			$segmentStartDocId += $segInfo->count();
422
+		}
423
+
424
+		return $result;
425
+	}
426
+
427
+
428
+	/**
429
+	 * Returns the number of documents in this index containing the $term.
430
+	 *
431
+	 * @param Zend_Search_Lucene_Index_Term $term
432
+	 * @return integer
433
+	 */
434
+	public function docFreq(Zend_Search_Lucene_Index_Term $term)
435
+	{
436
+		$result = 0;
437
+		foreach ($this->_segmentInfos as $segInfo) {
438
+			$termInfo = $segInfo->getTermInfo($term);
439
+			if ($termInfo !== null) {
440
+				$result += $termInfo->docFreq;
441
+			}
442
+		}
443
+
444
+		return $result;
445
+	}
446
+
447
+
448
+	/**
449
+	 * Retrive similarity used by index reader
450
+	 *
451
+	 * @return Zend_Search_Lucene_Search_Similarity
452
+	 */
453
+	public function getSimilarity()
454
+	{
455
+		return Zend_Search_Lucene_Search_Similarity::getDefault();
456
+	}
457
+
458
+
459
+	/**
460
+	 * Returns a normalization factor for "field, document" pair.
461
+	 *
462
+	 * @param integer $id
463
+	 * @param string $fieldName
464
+	 * @return Zend_Search_Lucene_Document
465
+	 */
466
+	public function norm( $id, $fieldName )
467
+	{
468
+		if( $id >= $this->_docCount )
469
+			return null;
470
+
471
+		$segCount = 0;
472
+		$nextSegmentStartId = $this->_segmentInfos[ 0 ]->count();
473
+		while( $nextSegmentStartId <= $id ) {
474
+			   $segCount++;
475
+			   $nextSegmentStartId += $this->_segmentInfos[ $segCount ]->count();
476
+		}
477
+
478
+		$segmentStartId = $nextSegmentStartId - $this->_segmentInfos[ $segCount ]->count();
479
+
480
+		return $this->_segmentInfos[ $segCount ]->norm($id - $segmentStartId, $fieldName);
481
+	}
482
+
483
+
484
+	/**
485
+	 * Adds a document to this index.
486
+	 *
487
+	 * @param Zend_Search_Lucene_Document $document
488
+	 */
489
+	public function addDocument(Zend_Search_Lucene_Document $document)
490
+	{
491
+		if (!$this->_writer instanceof Zend_Search_Lucene_Index_Writer) {
492
+			$this->_writer = new Zend_Search_Lucene_Index_Writer($this->_directory);
493
+		}
494
+
495
+		$this->_writer->addDocument($document);
496
+	}
497
+
498
+
499
+	/**
500
+	 * Commit changes resulting from delete() or undeleteAll() operations.
501
+	 *
502
+	 * @todo delete() and undeleteAll processing.
503
+	 */
504
+	public function commit()
505
+	{
506
+		if ($this->_writer !== null) {
507
+			foreach ($this->_writer->commit() as $segmentName => $segmentInfo) {
508
+				if ($segmentInfo !== null) {
509
+					$this->_segmentInfos[] = $segmentInfo;
510
+					$this->_docCount += $segmentInfo->count();
511
+				} else {
512
+					foreach ($this->_segmentInfos as $segId => $segInfo) {
513
+						if ($segInfo->getName() == $segmentName) {
514
+							unset($this->_segmentInfos[$segId]);
515
+						}
516
+					}
517
+				}
518
+			}
519
+		}
520
+	}
521
+
522
+
523
+	/*************************************************************************
524 524
     @todo UNIMPLEMENTED
525 525
     *************************************************************************/
526 526
 
527
-    /**
528
-     * Returns an array of all terms in this index.
529
-     *
530
-     * @todo Implementation
531
-     * @return array
532
-     */
533
-    public function terms()
534
-    {
535
-        return array();
536
-    }
537
-
538
-
539
-    /**
540
-     * Returns true if any documents have been deleted from this index.
541
-     *
542
-     * @todo Implementation
543
-     * @return boolean
544
-     */
545
-    public function hasDeletions()
546
-    {
547
-        return false;
548
-    }
549
-
550
-
551
-    /**
552
-     * Deletes a document from the index.  $doc may contain a Zend_Search_Lucene_Document
553
-     * or the number of the document to delete.
554
-     *
555
-     * @todo Implementation
556
-     * @param mixed $item_to_del
557
-     */
558
-    public function delete($doc)
559
-    {}
560
-
561
-
562
-    /**
563
-     * Undeletes all documents currently marked as deleted in this index.
564
-     *
565
-     * @todo Implementation
566
-     */
567
-    public function undeleteAll()
568
-    {}
527
+	/**
528
+	 * Returns an array of all terms in this index.
529
+	 *
530
+	 * @todo Implementation
531
+	 * @return array
532
+	 */
533
+	public function terms()
534
+	{
535
+		return array();
536
+	}
537
+
538
+
539
+	/**
540
+	 * Returns true if any documents have been deleted from this index.
541
+	 *
542
+	 * @todo Implementation
543
+	 * @return boolean
544
+	 */
545
+	public function hasDeletions()
546
+	{
547
+		return false;
548
+	}
549
+
550
+
551
+	/**
552
+	 * Deletes a document from the index.  $doc may contain a Zend_Search_Lucene_Document
553
+	 * or the number of the document to delete.
554
+	 *
555
+	 * @todo Implementation
556
+	 * @param mixed $item_to_del
557
+	 */
558
+	public function delete($doc)
559
+	{}
560
+
561
+
562
+	/**
563
+	 * Undeletes all documents currently marked as deleted in this index.
564
+	 *
565
+	 * @todo Implementation
566
+	 */
567
+	public function undeleteAll()
568
+	{}
569 569
 }
570 570
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -130,7 +130,7 @@  discard block
 block discarded – undo
130 130
 
131 131
         $format = $segmentsFile->readInt();
132 132
 
133
-        if ($format != (int)0xFFFFFFFF) {
133
+        if ($format != (int) 0xFFFFFFFF) {
134 134
             throw new Zend_Search_Lucene_Exception('Wrong segments file format');
135 135
         }
136 136
 
@@ -231,9 +231,9 @@  discard block
 block discarded – undo
231 231
         $scores = array();
232 232
 
233 233
         $docNum = $this->count();
234
-        for( $count=0; $count < $docNum; $count++ ) {
235
-            $docScore = $query->score( $count, $this);
236
-            if( $docScore != 0 ) {
234
+        for ($count = 0; $count < $docNum; $count++) {
235
+            $docScore = $query->score($count, $this);
236
+            if ($docScore != 0) {
237 237
                 $hit = new Zend_Search_Lucene_Search_QueryHit($this);
238 238
                 $hit->id = $count;
239 239
                 $hit->score = $docScore;
@@ -257,7 +257,7 @@  discard block
 block discarded – undo
257 257
     public function getFieldNames($indexed = false)
258 258
     {
259 259
         $result = array();
260
-        foreach( $this->_segmentInfos as $segmentInfo ) {
260
+        foreach ($this->_segmentInfos as $segmentInfo) {
261 261
             $result = array_merge($result, $segmentInfo->getFields($indexed));
262 262
         }
263 263
         return $result;
@@ -286,40 +286,40 @@  discard block
 block discarded – undo
286 286
         }
287 287
 
288 288
         $segCount = 0;
289
-        $nextSegmentStartId = $this->_segmentInfos[ 0 ]->count();
290
-        while( $nextSegmentStartId <= $id ) {
289
+        $nextSegmentStartId = $this->_segmentInfos[0]->count();
290
+        while ($nextSegmentStartId <= $id) {
291 291
                $segCount++;
292
-               $nextSegmentStartId += $this->_segmentInfos[ $segCount ]->count();
292
+               $nextSegmentStartId += $this->_segmentInfos[$segCount]->count();
293 293
         }
294
-        $segmentStartId = $nextSegmentStartId - $this->_segmentInfos[ $segCount ]->count();
294
+        $segmentStartId = $nextSegmentStartId - $this->_segmentInfos[$segCount]->count();
295 295
 
296
-        $fdxFile = $this->_segmentInfos[ $segCount ]->openCompoundFile('.fdx');
297
-        $fdxFile->seek( ($id-$segmentStartId)*8, SEEK_CUR );
296
+        $fdxFile = $this->_segmentInfos[$segCount]->openCompoundFile('.fdx');
297
+        $fdxFile->seek(($id - $segmentStartId) * 8, SEEK_CUR);
298 298
         $fieldValuesPosition = $fdxFile->readLong();
299 299
 
300
-        $fdtFile = $this->_segmentInfos[ $segCount ]->openCompoundFile('.fdt');
301
-        $fdtFile->seek( $fieldValuesPosition, SEEK_CUR );
300
+        $fdtFile = $this->_segmentInfos[$segCount]->openCompoundFile('.fdt');
301
+        $fdtFile->seek($fieldValuesPosition, SEEK_CUR);
302 302
         $fieldCount = $fdtFile->readVInt();
303 303
 
304 304
         $doc = new Zend_Search_Lucene_Document();
305
-        for( $count = 0; $count < $fieldCount; $count++ ) {
305
+        for ($count = 0; $count < $fieldCount; $count++) {
306 306
             $fieldNum = $fdtFile->readVInt();
307 307
             $bits = $fdtFile->readByte();
308 308
 
309
-            $fieldInfo = $this->_segmentInfos[ $segCount ]->getField($fieldNum);
309
+            $fieldInfo = $this->_segmentInfos[$segCount]->getField($fieldNum);
310 310
 
311
-            if( !($bits & 2) ) { // Text data
311
+            if (!($bits & 2)) { // Text data
312 312
                 $field = new Zend_Search_Lucene_Field($fieldInfo->name,
313 313
                                                       $fdtFile->readString(),
314 314
                                                       true,
315 315
                                                       $fieldInfo->isIndexed,
316
-                                                      $bits & 1 );
316
+                                                      $bits & 1);
317 317
             } else {
318 318
                 $field = new Zend_Search_Lucene_Field($fieldInfo->name,
319 319
                                                       $fdtFile->readBinary(),
320 320
                                                       true,
321 321
                                                       $fieldInfo->isIndexed,
322
-                                                      $bits & 1 );
322
+                                                      $bits & 1);
323 323
             }
324 324
 
325 325
             $doc->addField($field);
@@ -349,14 +349,14 @@  discard block
 block discarded – undo
349 349
             }
350 350
 
351 351
             $frqFile = $segInfo->openCompoundFile('.frq');
352
-            $frqFile->seek($termInfo->freqPointer,SEEK_CUR);
352
+            $frqFile->seek($termInfo->freqPointer, SEEK_CUR);
353 353
             $docId = 0;
354
-            for( $count=0; $count < $termInfo->docFreq; $count++ ) {
354
+            for ($count = 0; $count < $termInfo->docFreq; $count++) {
355 355
                 $docDelta = $frqFile->readVInt();
356
-                if( $docDelta % 2 == 1 ) {
357
-                    $docId += ($docDelta-1)/2;
356
+                if ($docDelta % 2 == 1) {
357
+                    $docId += ($docDelta - 1) / 2;
358 358
                 } else {
359
-                    $docId += $docDelta/2;
359
+                    $docId += $docDelta / 2;
360 360
                     // read freq
361 361
                     $frqFile->readVInt();
362 362
                 }
@@ -381,7 +381,7 @@  discard block
 block discarded – undo
381 381
     {
382 382
         $result = array();
383 383
         $segmentStartDocId = 0;
384
-        foreach( $this->_segmentInfos as $segInfo ) {
384
+        foreach ($this->_segmentInfos as $segInfo) {
385 385
             $termInfo = $segInfo->getTermInfo($term);
386 386
 
387 387
             if (!$termInfo instanceof Zend_Search_Lucene_Index_TermInfo) {
@@ -390,32 +390,32 @@  discard block
 block discarded – undo
390 390
             }
391 391
 
392 392
             $frqFile = $segInfo->openCompoundFile('.frq');
393
-            $frqFile->seek($termInfo->freqPointer,SEEK_CUR);
393
+            $frqFile->seek($termInfo->freqPointer, SEEK_CUR);
394 394
             $freqs = array();
395 395
             $docId = 0;
396 396
 
397
-            for( $count = 0; $count < $termInfo->docFreq; $count++ ) {
397
+            for ($count = 0; $count < $termInfo->docFreq; $count++) {
398 398
                 $docDelta = $frqFile->readVInt();
399
-                if( $docDelta % 2 == 1 ) {
400
-                    $docId += ($docDelta-1)/2;
401
-                    $freqs[ $docId ] = 1;
399
+                if ($docDelta % 2 == 1) {
400
+                    $docId += ($docDelta - 1) / 2;
401
+                    $freqs[$docId] = 1;
402 402
                 } else {
403
-                    $docId += $docDelta/2;
404
-                    $freqs[ $docId ] = $frqFile->readVInt();
403
+                    $docId += $docDelta / 2;
404
+                    $freqs[$docId] = $frqFile->readVInt();
405 405
                 }
406 406
             }
407 407
 
408 408
             $prxFile = $segInfo->openCompoundFile('.prx');
409
-            $prxFile->seek($termInfo->proxPointer,SEEK_CUR);
409
+            $prxFile->seek($termInfo->proxPointer, SEEK_CUR);
410 410
             foreach ($freqs as $docId => $freq) {
411 411
                 $termPosition = 0;
412 412
                 $positions = array();
413 413
 
414
-                for ($count = 0; $count < $freq; $count++ ) {
414
+                for ($count = 0; $count < $freq; $count++) {
415 415
                     $termPosition += $prxFile->readVInt();
416 416
                     $positions[] = $termPosition;
417 417
                 }
418
-                $result[ $segmentStartDocId + $docId ] = $positions;
418
+                $result[$segmentStartDocId + $docId] = $positions;
419 419
             }
420 420
 
421 421
             $segmentStartDocId += $segInfo->count();
@@ -463,21 +463,21 @@  discard block
 block discarded – undo
463 463
      * @param string $fieldName
464 464
      * @return Zend_Search_Lucene_Document
465 465
      */
466
-    public function norm( $id, $fieldName )
466
+    public function norm($id, $fieldName)
467 467
     {
468
-        if( $id >= $this->_docCount )
468
+        if ($id >= $this->_docCount)
469 469
             return null;
470 470
 
471 471
         $segCount = 0;
472
-        $nextSegmentStartId = $this->_segmentInfos[ 0 ]->count();
473
-        while( $nextSegmentStartId <= $id ) {
472
+        $nextSegmentStartId = $this->_segmentInfos[0]->count();
473
+        while ($nextSegmentStartId <= $id) {
474 474
                $segCount++;
475
-               $nextSegmentStartId += $this->_segmentInfos[ $segCount ]->count();
475
+               $nextSegmentStartId += $this->_segmentInfos[$segCount]->count();
476 476
         }
477 477
 
478
-        $segmentStartId = $nextSegmentStartId - $this->_segmentInfos[ $segCount ]->count();
478
+        $segmentStartId = $nextSegmentStartId - $this->_segmentInfos[$segCount]->count();
479 479
 
480
-        return $this->_segmentInfos[ $segCount ]->norm($id - $segmentStartId, $fieldName);
480
+        return $this->_segmentInfos[$segCount]->norm($id - $segmentStartId, $fieldName);
481 481
     }
482 482
 
483 483
 
Please login to merge, or discard this patch.
Braces   +3 added lines, -2 removed lines patch added patch discarded remove patch
@@ -465,8 +465,9 @@
 block discarded – undo
465 465
      */
466 466
     public function norm( $id, $fieldName )
467 467
     {
468
-        if( $id >= $this->_docCount )
469
-            return null;
468
+        if( $id >= $this->_docCount ) {
469
+                    return null;
470
+        }
470 471
 
471 472
         $segCount = 0;
472 473
         $nextSegmentStartId = $this->_segmentInfos[ 0 ]->count();
Please login to merge, or discard this patch.
demos/quickstart/protected/index/Zend/Search/Lucene/Analysis/Analyzer.php 2 patches
Doc Comments   -1 removed lines patch added patch discarded remove patch
@@ -68,7 +68,6 @@
 block discarded – undo
68 68
     /**
69 69
      * Set the default Analyzer implementation used by indexing code.
70 70
      *
71
-     * @param Zend_Search_Lucene_Analysis_Analyzer $similarity
72 71
      */
73 72
     static public function setDefault(Zend_Search_Lucene_Analysis_Analyzer $analyzer)
74 73
     {
Please login to merge, or discard this patch.
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -48,47 +48,47 @@
 block discarded – undo
48 48
 
49 49
 abstract class Zend_Search_Lucene_Analysis_Analyzer
50 50
 {
51
-    /**
52
-     * The Analyzer implementation used by default.
53
-     *
54
-     * @var Zend_Search_Lucene_Analysis_Analyzer
55
-     */
56
-    static private $_defaultImpl;
57
-
58
-    /**
59
-     * Tokenize text to a terms
60
-     * Returns array of Zend_Search_Lucene_Analysis_Token objects
61
-     *
62
-     * @param string $data
63
-     * @return array
64
-     */
65
-    abstract public function tokenize($data);
66
-
67
-
68
-    /**
69
-     * Set the default Analyzer implementation used by indexing code.
70
-     *
71
-     * @param Zend_Search_Lucene_Analysis_Analyzer $similarity
72
-     */
73
-    static public function setDefault(Zend_Search_Lucene_Analysis_Analyzer $analyzer)
74
-    {
75
-        self::$_defaultImpl = $analyzer;
76
-    }
77
-
78
-
79
-    /**
80
-     * Return the default Analyzer implementation used by indexing code.
81
-     *
82
-     * @return Zend_Search_Lucene_Analysis_Analyzer
83
-     */
84
-    static public function getDefault()
85
-    {
86
-        if (!self::$_defaultImpl instanceof Zend_Search_Lucene_Analysis_Analyzer) {
87
-            self::$_defaultImpl = new Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive();
88
-        }
89
-
90
-        return self::$_defaultImpl;
91
-    }
51
+	/**
52
+	 * The Analyzer implementation used by default.
53
+	 *
54
+	 * @var Zend_Search_Lucene_Analysis_Analyzer
55
+	 */
56
+	static private $_defaultImpl;
57
+
58
+	/**
59
+	 * Tokenize text to a terms
60
+	 * Returns array of Zend_Search_Lucene_Analysis_Token objects
61
+	 *
62
+	 * @param string $data
63
+	 * @return array
64
+	 */
65
+	abstract public function tokenize($data);
66
+
67
+
68
+	/**
69
+	 * Set the default Analyzer implementation used by indexing code.
70
+	 *
71
+	 * @param Zend_Search_Lucene_Analysis_Analyzer $similarity
72
+	 */
73
+	static public function setDefault(Zend_Search_Lucene_Analysis_Analyzer $analyzer)
74
+	{
75
+		self::$_defaultImpl = $analyzer;
76
+	}
77
+
78
+
79
+	/**
80
+	 * Return the default Analyzer implementation used by indexing code.
81
+	 *
82
+	 * @return Zend_Search_Lucene_Analysis_Analyzer
83
+	 */
84
+	static public function getDefault()
85
+	{
86
+		if (!self::$_defaultImpl instanceof Zend_Search_Lucene_Analysis_Analyzer) {
87
+			self::$_defaultImpl = new Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive();
88
+		}
89
+
90
+		return self::$_defaultImpl;
91
+	}
92 92
 
93 93
 }
94 94
 
Please login to merge, or discard this patch.
demos/quickstart/protected/index/Zend/Search/Lucene/Field.php 2 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -47,6 +47,9 @@
 block discarded – undo
47 47
 
48 48
     public $boost = 1.0;
49 49
 
50
+    /**
51
+     * @param boolean $isStored
52
+     */
50 53
     public function __construct($name, $stringValue, $isStored, $isIndexed, $isTokenized, $isBinary = false)
51 54
     {
52 55
         $this->name        = $name;
Please login to merge, or discard this patch.
Indentation   +95 added lines, -95 removed lines patch added patch discarded remove patch
@@ -34,101 +34,101 @@
 block discarded – undo
34 34
  */
35 35
 class Zend_Search_Lucene_Field
36 36
 {
37
-    public $kind;
38
-
39
-    public $name        = 'body';
40
-    public $stringValue = null;
41
-    public $isStored    = false;
42
-    public $isIndexed   = true;
43
-    public $isTokenized = true;
44
-    public $isBinary    = false;
45
-
46
-    public $storeTermVector = false;
47
-
48
-    public $boost = 1.0;
49
-
50
-    public function __construct($name, $stringValue, $isStored, $isIndexed, $isTokenized, $isBinary = false)
51
-    {
52
-        $this->name        = $name;
53
-        $this->stringValue = $stringValue;
54
-        $this->isStored    = $isStored;
55
-        $this->isIndexed   = $isIndexed;
56
-        $this->isTokenized = $isTokenized;
57
-        $this->isBinary    = $isBinary;
58
-
59
-        $this->storeTermVector = false;
60
-        $this->boost           = 1.0;
61
-    }
62
-
63
-
64
-    /**
65
-     * Constructs a String-valued Field that is not tokenized, but is indexed
66
-     * and stored.  Useful for non-text fields, e.g. date or url.
67
-     *
68
-     * @param string $name
69
-     * @param string $value
70
-     * @return Zend_Search_Lucene_Field
71
-     */
72
-    static public function Keyword($name, $value)
73
-    {
74
-        return new self($name, $value, true, true, false);
75
-    }
76
-
77
-
78
-    /**
79
-     * Constructs a String-valued Field that is not tokenized nor indexed,
80
-     * but is stored in the index, for return with hits.
81
-     *
82
-     * @param string $name
83
-     * @param string $value
84
-     * @return Zend_Search_Lucene_Field
85
-     */
86
-    static public function UnIndexed($name, $value)
87
-    {
88
-        return new self($name, $value, true, false, false);
89
-    }
90
-
91
-
92
-    /**
93
-     * Constructs a Binary String valued Field that is not tokenized nor indexed,
94
-     * but is stored in the index, for return with hits.
95
-     *
96
-     * @param string $name
97
-     * @param string $value
98
-     * @return Zend_Search_Lucene_Field
99
-     */
100
-    static public function Binary($name, $value)
101
-    {
102
-        return new self($name, $value, true, false, false, true);
103
-    }
104
-
105
-    /**
106
-     * Constructs a String-valued Field that is tokenized and indexed,
107
-     * and is stored in the index, for return with hits.  Useful for short text
108
-     * fields, like "title" or "subject". Term vector will not be stored for this field.
109
-     *
110
-     * @param string $name
111
-     * @param string $value
112
-     * @return Zend_Search_Lucene_Field
113
-     */
114
-    static public function Text($name, $value)
115
-    {
116
-        return new self($name, $value, true, true, true);
117
-    }
118
-
119
-
120
-    /**
121
-     * Constructs a String-valued Field that is tokenized and indexed,
122
-     * but that is not stored in the index.
123
-     *
124
-     * @param string $name
125
-     * @param string $value
126
-     * @return Zend_Search_Lucene_Field
127
-     */
128
-    static public function UnStored($name, $value)
129
-    {
130
-        return new self($name, $value, false, true, true);
131
-    }
37
+	public $kind;
38
+
39
+	public $name        = 'body';
40
+	public $stringValue = null;
41
+	public $isStored    = false;
42
+	public $isIndexed   = true;
43
+	public $isTokenized = true;
44
+	public $isBinary    = false;
45
+
46
+	public $storeTermVector = false;
47
+
48
+	public $boost = 1.0;
49
+
50
+	public function __construct($name, $stringValue, $isStored, $isIndexed, $isTokenized, $isBinary = false)
51
+	{
52
+		$this->name        = $name;
53
+		$this->stringValue = $stringValue;
54
+		$this->isStored    = $isStored;
55
+		$this->isIndexed   = $isIndexed;
56
+		$this->isTokenized = $isTokenized;
57
+		$this->isBinary    = $isBinary;
58
+
59
+		$this->storeTermVector = false;
60
+		$this->boost           = 1.0;
61
+	}
62
+
63
+
64
+	/**
65
+	 * Constructs a String-valued Field that is not tokenized, but is indexed
66
+	 * and stored.  Useful for non-text fields, e.g. date or url.
67
+	 *
68
+	 * @param string $name
69
+	 * @param string $value
70
+	 * @return Zend_Search_Lucene_Field
71
+	 */
72
+	static public function Keyword($name, $value)
73
+	{
74
+		return new self($name, $value, true, true, false);
75
+	}
76
+
77
+
78
+	/**
79
+	 * Constructs a String-valued Field that is not tokenized nor indexed,
80
+	 * but is stored in the index, for return with hits.
81
+	 *
82
+	 * @param string $name
83
+	 * @param string $value
84
+	 * @return Zend_Search_Lucene_Field
85
+	 */
86
+	static public function UnIndexed($name, $value)
87
+	{
88
+		return new self($name, $value, true, false, false);
89
+	}
90
+
91
+
92
+	/**
93
+	 * Constructs a Binary String valued Field that is not tokenized nor indexed,
94
+	 * but is stored in the index, for return with hits.
95
+	 *
96
+	 * @param string $name
97
+	 * @param string $value
98
+	 * @return Zend_Search_Lucene_Field
99
+	 */
100
+	static public function Binary($name, $value)
101
+	{
102
+		return new self($name, $value, true, false, false, true);
103
+	}
104
+
105
+	/**
106
+	 * Constructs a String-valued Field that is tokenized and indexed,
107
+	 * and is stored in the index, for return with hits.  Useful for short text
108
+	 * fields, like "title" or "subject". Term vector will not be stored for this field.
109
+	 *
110
+	 * @param string $name
111
+	 * @param string $value
112
+	 * @return Zend_Search_Lucene_Field
113
+	 */
114
+	static public function Text($name, $value)
115
+	{
116
+		return new self($name, $value, true, true, true);
117
+	}
118
+
119
+
120
+	/**
121
+	 * Constructs a String-valued Field that is tokenized and indexed,
122
+	 * but that is not stored in the index.
123
+	 *
124
+	 * @param string $name
125
+	 * @param string $value
126
+	 * @return Zend_Search_Lucene_Field
127
+	 */
128
+	static public function UnStored($name, $value)
129
+	{
130
+		return new self($name, $value, false, true, true);
131
+	}
132 132
 
133 133
 }
134 134
 
Please login to merge, or discard this patch.
demos/quickstart/protected/index/Zend/Search/Lucene/Index/FieldInfo.php 3 patches
Doc Comments   +4 added lines patch added patch discarded remove patch
@@ -32,6 +32,10 @@
 block discarded – undo
32 32
     public $number;
33 33
     public $storeTermVector;
34 34
 
35
+    /**
36
+     * @param string $name
37
+     * @param integer $number
38
+     */
35 39
     public function __construct( $name, $isIndexed, $number, $storeTermVector )
36 40
     {
37 41
         $this->name            = $name;
Please login to merge, or discard this patch.
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -27,17 +27,17 @@
 block discarded – undo
27 27
  */
28 28
 class Zend_Search_Lucene_Index_FieldInfo
29 29
 {
30
-    public $name;
31
-    public $isIndexed;
32
-    public $number;
33
-    public $storeTermVector;
30
+	public $name;
31
+	public $isIndexed;
32
+	public $number;
33
+	public $storeTermVector;
34 34
 
35
-    public function __construct( $name, $isIndexed, $number, $storeTermVector )
36
-    {
37
-        $this->name            = $name;
38
-        $this->isIndexed       = $isIndexed;
39
-        $this->number          = $number;
40
-        $this->storeTermVector = $storeTermVector;
41
-    }
35
+	public function __construct( $name, $isIndexed, $number, $storeTermVector )
36
+	{
37
+		$this->name            = $name;
38
+		$this->isIndexed       = $isIndexed;
39
+		$this->number          = $number;
40
+		$this->storeTermVector = $storeTermVector;
41
+	}
42 42
 }
43 43
 
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -32,7 +32,7 @@
 block discarded – undo
32 32
     public $number;
33 33
     public $storeTermVector;
34 34
 
35
-    public function __construct( $name, $isIndexed, $number, $storeTermVector )
35
+    public function __construct($name, $isIndexed, $number, $storeTermVector)
36 36
     {
37 37
         $this->name            = $name;
38 38
         $this->isIndexed       = $isIndexed;
Please login to merge, or discard this patch.
demos/quickstart/protected/index/Zend/Search/Lucene/Index/SegmentInfo.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -391,7 +391,7 @@
 block discarded – undo
391 391
      *
392 392
      * @param integer $id
393 393
      * @param string $fieldName
394
-     * @return string
394
+     * @return null|double
395 395
      */
396 396
     public function norm($id, $fieldName)
397 397
     {
Please login to merge, or discard this patch.
Indentation   +377 added lines, -377 removed lines patch added patch discarded remove patch
@@ -31,382 +31,382 @@
 block discarded – undo
31 31
  */
32 32
 class Zend_Search_Lucene_Index_SegmentInfo
33 33
 {
34
-    /**
35
-     * Number of docs in a segment
36
-     *
37
-     * @var integer
38
-     */
39
-    private $_docCount;
40
-
41
-    /**
42
-     * Segment name
43
-     *
44
-     * @var string
45
-     */
46
-    private $_name;
47
-
48
-    /**
49
-     * Term Dictionary Index
50
-     * Array of the Zend_Search_Lucene_Index_Term objects
51
-     * Corresponding Zend_Search_Lucene_Index_TermInfo object stored in the $_termDictionaryInfos
52
-     *
53
-     * @var array
54
-     */
55
-    private $_termDictionary;
56
-
57
-    /**
58
-     * Term Dictionary Index TermInfos
59
-     * Array of the Zend_Search_Lucene_Index_TermInfo objects
60
-     *
61
-     * @var array
62
-     */
63
-    private $_termDictionaryInfos;
64
-
65
-    /**
66
-     * Segment fields. Array of Zend_Search_Lucene_Index_FieldInfo objects for this segment
67
-     *
68
-     * @var array
69
-     */
70
-    private $_fields;
71
-
72
-    /**
73
-     * Field positions in a dictionary.
74
-     * (Term dictionary contains filelds ordered by names)
75
-     *
76
-     * @var array
77
-     */
78
-    private $_fieldsDicPositions;
79
-
80
-
81
-    /**
82
-     * Associative array where the key is the file name and the value is data offset
83
-     * in a compound segment file (.csf).
84
-     *
85
-     * @var array
86
-     */
87
-    private $_segFiles;
88
-
89
-    /**
90
-     * File system adapter.
91
-     *
92
-     * @var Zend_Search_Lucene_Storage_Directory_Filesystem
93
-     */
94
-    private $_directory;
95
-
96
-    /**
97
-     * Normalization factors.
98
-     * An array fieldName => normVector
99
-     * normVector is a binary string.
100
-     * Each byte corresponds to an indexed document in a segment and
101
-     * encodes normalization factor (float value, encoded by
102
-     * Zend_Search_Lucene_Search_Similarity::encodeNorm())
103
-     *
104
-     * @var array
105
-     */
106
-    private $_norms = array();
107
-
108
-    /**
109
-     * Zend_Search_Lucene_Index_SegmentInfo constructor needs Segmentname,
110
-     * Documents count and Directory as a parameter.
111
-     *
112
-     * @param string $name
113
-     * @param integer $docCount
114
-     * @param Zend_Search_Lucene_Storage_Directory $directory
115
-     */
116
-    public function __construct($name, $docCount, $directory)
117
-    {
118
-        $this->_name = $name;
119
-        $this->_docCount = $docCount;
120
-        $this->_directory = $directory;
121
-        $this->_termDictionary = null;
122
-
123
-        $this->_segFiles = array();
124
-        $cfsFile = $this->_directory->getFileObject($name . '.cfs');
125
-        $segFilesCount = $cfsFile->readVInt();
126
-
127
-        for ($count = 0; $count < $segFilesCount; $count++) {
128
-            $dataOffset = $cfsFile->readLong();
129
-            $fileName = $cfsFile->readString();
130
-            $this->_segFiles[$fileName] = $dataOffset;
131
-        }
132
-
133
-        $fnmFile = $this->openCompoundFile('.fnm');
134
-        $fieldsCount = $fnmFile->readVInt();
135
-        $fieldNames = array();
136
-        $fieldNums  = array();
137
-        $this->_fields = array();
138
-        for ($count=0; $count < $fieldsCount; $count++) {
139
-            $fieldName = $fnmFile->readString();
140
-            $fieldBits = $fnmFile->readByte();
141
-            $this->_fields[$count] = new Zend_Search_Lucene_Index_FieldInfo($fieldName,
142
-                                                                            $fieldBits & 1,
143
-                                                                            $count,
144
-                                                                            $fieldBits & 2 );
145
-            if ($fieldBits & 0x10) {
146
-                // norms are omitted for the indexed field
147
-                $this->_norms[$count] = str_repeat(chr(Zend_Search_Lucene_Search_Similarity::encodeNorm(1.0)), $docCount);
148
-            }
149
-
150
-            $fieldNums[$count]  = $count;
151
-            $fieldNames[$count] = $fieldName;
152
-        }
153
-        array_multisort($fieldNames, SORT_ASC, SORT_REGULAR, $fieldNums);
154
-        $this->_fieldsDicPositions = array_flip($fieldNums);
155
-    }
156
-
157
-    /**
158
-     * Opens index file stoted within compound index file
159
-     *
160
-     * @param string $extension
161
-     * @throws Zend_Search_Lucene_Exception
162
-     * @return Zend_Search_Lucene_Storage_File
163
-     */
164
-    public function openCompoundFile($extension)
165
-    {
166
-        $filename = $this->_name . $extension;
167
-
168
-        if( !isset($this->_segFiles[ $filename ]) ) {
169
-            throw new Zend_Search_Lucene_Exception('Index compound file doesn\'t contain '
170
-                                       . $filename . ' file.' );
171
-        }
172
-
173
-        $file = $this->_directory->getFileObject( $this->_name.".cfs" );
174
-        $file->seek( $this->_segFiles[ $filename ] );
175
-        return $file;
176
-    }
177
-
178
-    /**
179
-     * Returns field index or -1 if field is not found
180
-     *
181
-     * @param string $fieldName
182
-     * @return integer
183
-     */
184
-    public function getFieldNum($fieldName)
185
-    {
186
-        foreach( $this->_fields as $field ) {
187
-            if( $field->name == $fieldName ) {
188
-                return $field->number;
189
-            }
190
-        }
191
-
192
-        return -1;
193
-    }
194
-
195
-    /**
196
-     * Returns field info for specified field
197
-     *
198
-     * @param integer $fieldNum
199
-     * @return ZSearchFieldInfo
200
-     */
201
-    public function getField($fieldNum)
202
-    {
203
-        return $this->_fields[$fieldNum];
204
-    }
205
-
206
-    /**
207
-     * Returns array of fields.
208
-     * if $indexed parameter is true, then returns only indexed fields.
209
-     *
210
-     * @param boolean $indexed
211
-     * @return array
212
-     */
213
-    public function getFields($indexed = false)
214
-    {
215
-        $result = array();
216
-        foreach( $this->_fields as $field ) {
217
-            if( (!$indexed) || $field->isIndexed ) {
218
-                $result[ $field->name ] = $field->name;
219
-            }
220
-        }
221
-        return $result;
222
-    }
223
-
224
-    /**
225
-     * Returns the total number of documents in this segment.
226
-     *
227
-     * @return integer
228
-     */
229
-    public function count()
230
-    {
231
-        return $this->_docCount;
232
-    }
233
-
234
-
235
-    /**
236
-     * Loads Term dictionary from TermInfoIndex file
237
-     */
238
-    protected function _loadDictionary()
239
-    {
240
-        if ($this->_termDictionary !== null) {
241
-            return;
242
-        }
243
-
244
-        $this->_termDictionary = array();
245
-        $this->_termDictionaryInfos = array();
246
-
247
-        $tiiFile = $this->openCompoundFile('.tii');
248
-        $tiVersion = $tiiFile->readInt();
249
-        if ($tiVersion != (int)0xFFFFFFFE) {
250
-            throw new Zend_Search_Lucene_Exception('Wrong TermInfoIndexFile file format');
251
-        }
252
-
253
-        $indexTermCount = $tiiFile->readLong();
254
-                          $tiiFile->readInt();  // IndexInterval
255
-        $skipInterval   = $tiiFile->readInt();
256
-
257
-        $prevTerm     = '';
258
-        $freqPointer  =  0;
259
-        $proxPointer  =  0;
260
-        $indexPointer =  0;
261
-        for ($count = 0; $count < $indexTermCount; $count++) {
262
-            $termPrefixLength = $tiiFile->readVInt();
263
-            $termSuffix       = $tiiFile->readString();
264
-            $termValue        = substr( $prevTerm, 0, $termPrefixLength ) . $termSuffix;
265
-
266
-            $termFieldNum     = $tiiFile->readVInt();
267
-            $docFreq          = $tiiFile->readVInt();
268
-            $freqPointer     += $tiiFile->readVInt();
269
-            $proxPointer     += $tiiFile->readVInt();
270
-            if( $docFreq >= $skipInterval ) {
271
-                $skipDelta = $tiiFile->readVInt();
272
-            } else {
273
-                $skipDelta = 0;
274
-            }
275
-
276
-            $indexPointer += $tiiFile->readVInt();
277
-
278
-            $this->_termDictionary[] =  new Zend_Search_Lucene_Index_Term($termValue,$termFieldNum);
279
-            $this->_termDictionaryInfos[] =
280
-                new Zend_Search_Lucene_Index_TermInfo($docFreq, $freqPointer, $proxPointer, $skipDelta, $indexPointer);
281
-            $prevTerm = $termValue;
282
-        }
283
-    }
284
-
285
-
286
-    /**
287
-     * Return segment name
288
-     *
289
-     * @return string
290
-     */
291
-    public function getName()
292
-    {
293
-        return $this->_name;
294
-    }
295
-
296
-
297
-    /**
298
-     * Scans terms dictionary and returns term info
299
-     *
300
-     * @param Zend_Search_Lucene_Index_Term $term
301
-     * @return Zend_Search_Lucene_Index_TermInfo
302
-     */
303
-    public function getTermInfo($term)
304
-    {
305
-        $this->_loadDictionary();
306
-
307
-        $searchField = $this->getFieldNum($term->field);
308
-
309
-        if ($searchField == -1) {
310
-            return null;
311
-        }
312
-        $searchDicField = $this->_fieldsDicPositions[$searchField];
313
-
314
-        // search for appropriate value in dictionary
315
-        $lowIndex = 0;
316
-        $highIndex = count($this->_termDictionary)-1;
317
-        while ($highIndex >= $lowIndex) {
318
-            // $mid = ($highIndex - $lowIndex)/2;
319
-            $mid = ($highIndex + $lowIndex) >> 1;
320
-            $midTerm = $this->_termDictionary[$mid];
321
-
322
-            $delta = $searchDicField - $this->_fieldsDicPositions[$midTerm->field];
323
-            if ($delta == 0) {
324
-                $delta = strcmp($term->text, $midTerm->text);
325
-            }
326
-
327
-            if ($delta < 0) {
328
-                $highIndex = $mid-1;
329
-            } elseif ($delta > 0) {
330
-                $lowIndex  = $mid+1;
331
-            } else {
332
-                return $this->_termDictionaryInfos[$mid]; // We got it!
333
-            }
334
-        }
335
-
336
-        if ($highIndex == -1) {
337
-            // Term is out of the dictionary range
338
-            return null;
339
-        }
340
-
341
-        $prevPosition = $highIndex;
342
-        $prevTerm = $this->_termDictionary[$prevPosition];
343
-        $prevTermInfo = $this->_termDictionaryInfos[ $prevPosition ];
344
-
345
-        $tisFile = $this->openCompoundFile('.tis');
346
-        $tiVersion = $tisFile->readInt();
347
-        if ($tiVersion != (int)0xFFFFFFFE) {
348
-            throw new Zend_Search_Lucene_Exception('Wrong TermInfoFile file format');
349
-        }
350
-
351
-        $termCount     = $tisFile->readLong();
352
-        $indexInterval = $tisFile->readInt();
353
-        $skipInterval  = $tisFile->readInt();
354
-
355
-        $tisFile->seek($prevTermInfo->indexPointer - 20 /* header size*/, SEEK_CUR);
356
-
357
-        $termValue    = $prevTerm->text;
358
-        $termFieldNum = $prevTerm->field;
359
-        $freqPointer = $prevTermInfo->freqPointer;
360
-        $proxPointer = $prevTermInfo->proxPointer;
361
-        for ($count = $prevPosition*$indexInterval + 1;
362
-             $count < $termCount &&
363
-             ( $this->_fieldsDicPositions[ $termFieldNum ] < $searchDicField ||
364
-              ($this->_fieldsDicPositions[ $termFieldNum ] == $searchDicField &&
365
-               strcmp($termValue, $term->text) < 0) );
366
-             $count++) {
367
-            $termPrefixLength = $tisFile->readVInt();
368
-            $termSuffix       = $tisFile->readString();
369
-            $termFieldNum     = $tisFile->readVInt();
370
-            $termValue        = substr( $termValue, 0, $termPrefixLength ) . $termSuffix;
371
-
372
-            $docFreq      = $tisFile->readVInt();
373
-            $freqPointer += $tisFile->readVInt();
374
-            $proxPointer += $tisFile->readVInt();
375
-            if( $docFreq >= $skipInterval ) {
376
-                $skipOffset = $tisFile->readVInt();
377
-            } else {
378
-                $skipOffset = 0;
379
-            }
380
-        }
381
-
382
-        if ($termFieldNum == $searchField && $termValue == $term->text) {
383
-            return new Zend_Search_Lucene_Index_TermInfo($docFreq, $freqPointer, $proxPointer, $skipOffset);
384
-        } else {
385
-            return null;
386
-        }
387
-    }
388
-
389
-    /**
390
-     * Returns normalization factor for specified documents
391
-     *
392
-     * @param integer $id
393
-     * @param string $fieldName
394
-     * @return string
395
-     */
396
-    public function norm($id, $fieldName)
397
-    {
398
-        $fieldNum = $this->getFieldNum($fieldName);
399
-
400
-        if ( !($this->_fields[$fieldNum]->isIndexed) ) {
401
-            return null;
402
-        }
403
-
404
-        if ( !isset( $this->_norms[$fieldNum] )) {
405
-            $fFile = $this->openCompoundFile('.f' . $fieldNum);
406
-            $this->_norms[$fieldNum] = $fFile->readBytes($this->_docCount);
407
-        }
408
-
409
-        return Zend_Search_Lucene_Search_Similarity::decodeNorm( ord($this->_norms[$fieldNum]{$id}) );
410
-    }
34
+	/**
35
+	 * Number of docs in a segment
36
+	 *
37
+	 * @var integer
38
+	 */
39
+	private $_docCount;
40
+
41
+	/**
42
+	 * Segment name
43
+	 *
44
+	 * @var string
45
+	 */
46
+	private $_name;
47
+
48
+	/**
49
+	 * Term Dictionary Index
50
+	 * Array of the Zend_Search_Lucene_Index_Term objects
51
+	 * Corresponding Zend_Search_Lucene_Index_TermInfo object stored in the $_termDictionaryInfos
52
+	 *
53
+	 * @var array
54
+	 */
55
+	private $_termDictionary;
56
+
57
+	/**
58
+	 * Term Dictionary Index TermInfos
59
+	 * Array of the Zend_Search_Lucene_Index_TermInfo objects
60
+	 *
61
+	 * @var array
62
+	 */
63
+	private $_termDictionaryInfos;
64
+
65
+	/**
66
+	 * Segment fields. Array of Zend_Search_Lucene_Index_FieldInfo objects for this segment
67
+	 *
68
+	 * @var array
69
+	 */
70
+	private $_fields;
71
+
72
+	/**
73
+	 * Field positions in a dictionary.
74
+	 * (Term dictionary contains filelds ordered by names)
75
+	 *
76
+	 * @var array
77
+	 */
78
+	private $_fieldsDicPositions;
79
+
80
+
81
+	/**
82
+	 * Associative array where the key is the file name and the value is data offset
83
+	 * in a compound segment file (.csf).
84
+	 *
85
+	 * @var array
86
+	 */
87
+	private $_segFiles;
88
+
89
+	/**
90
+	 * File system adapter.
91
+	 *
92
+	 * @var Zend_Search_Lucene_Storage_Directory_Filesystem
93
+	 */
94
+	private $_directory;
95
+
96
+	/**
97
+	 * Normalization factors.
98
+	 * An array fieldName => normVector
99
+	 * normVector is a binary string.
100
+	 * Each byte corresponds to an indexed document in a segment and
101
+	 * encodes normalization factor (float value, encoded by
102
+	 * Zend_Search_Lucene_Search_Similarity::encodeNorm())
103
+	 *
104
+	 * @var array
105
+	 */
106
+	private $_norms = array();
107
+
108
+	/**
109
+	 * Zend_Search_Lucene_Index_SegmentInfo constructor needs Segmentname,
110
+	 * Documents count and Directory as a parameter.
111
+	 *
112
+	 * @param string $name
113
+	 * @param integer $docCount
114
+	 * @param Zend_Search_Lucene_Storage_Directory $directory
115
+	 */
116
+	public function __construct($name, $docCount, $directory)
117
+	{
118
+		$this->_name = $name;
119
+		$this->_docCount = $docCount;
120
+		$this->_directory = $directory;
121
+		$this->_termDictionary = null;
122
+
123
+		$this->_segFiles = array();
124
+		$cfsFile = $this->_directory->getFileObject($name . '.cfs');
125
+		$segFilesCount = $cfsFile->readVInt();
126
+
127
+		for ($count = 0; $count < $segFilesCount; $count++) {
128
+			$dataOffset = $cfsFile->readLong();
129
+			$fileName = $cfsFile->readString();
130
+			$this->_segFiles[$fileName] = $dataOffset;
131
+		}
132
+
133
+		$fnmFile = $this->openCompoundFile('.fnm');
134
+		$fieldsCount = $fnmFile->readVInt();
135
+		$fieldNames = array();
136
+		$fieldNums  = array();
137
+		$this->_fields = array();
138
+		for ($count=0; $count < $fieldsCount; $count++) {
139
+			$fieldName = $fnmFile->readString();
140
+			$fieldBits = $fnmFile->readByte();
141
+			$this->_fields[$count] = new Zend_Search_Lucene_Index_FieldInfo($fieldName,
142
+																			$fieldBits & 1,
143
+																			$count,
144
+																			$fieldBits & 2 );
145
+			if ($fieldBits & 0x10) {
146
+				// norms are omitted for the indexed field
147
+				$this->_norms[$count] = str_repeat(chr(Zend_Search_Lucene_Search_Similarity::encodeNorm(1.0)), $docCount);
148
+			}
149
+
150
+			$fieldNums[$count]  = $count;
151
+			$fieldNames[$count] = $fieldName;
152
+		}
153
+		array_multisort($fieldNames, SORT_ASC, SORT_REGULAR, $fieldNums);
154
+		$this->_fieldsDicPositions = array_flip($fieldNums);
155
+	}
156
+
157
+	/**
158
+	 * Opens index file stoted within compound index file
159
+	 *
160
+	 * @param string $extension
161
+	 * @throws Zend_Search_Lucene_Exception
162
+	 * @return Zend_Search_Lucene_Storage_File
163
+	 */
164
+	public function openCompoundFile($extension)
165
+	{
166
+		$filename = $this->_name . $extension;
167
+
168
+		if( !isset($this->_segFiles[ $filename ]) ) {
169
+			throw new Zend_Search_Lucene_Exception('Index compound file doesn\'t contain '
170
+									   . $filename . ' file.' );
171
+		}
172
+
173
+		$file = $this->_directory->getFileObject( $this->_name.".cfs" );
174
+		$file->seek( $this->_segFiles[ $filename ] );
175
+		return $file;
176
+	}
177
+
178
+	/**
179
+	 * Returns field index or -1 if field is not found
180
+	 *
181
+	 * @param string $fieldName
182
+	 * @return integer
183
+	 */
184
+	public function getFieldNum($fieldName)
185
+	{
186
+		foreach( $this->_fields as $field ) {
187
+			if( $field->name == $fieldName ) {
188
+				return $field->number;
189
+			}
190
+		}
191
+
192
+		return -1;
193
+	}
194
+
195
+	/**
196
+	 * Returns field info for specified field
197
+	 *
198
+	 * @param integer $fieldNum
199
+	 * @return ZSearchFieldInfo
200
+	 */
201
+	public function getField($fieldNum)
202
+	{
203
+		return $this->_fields[$fieldNum];
204
+	}
205
+
206
+	/**
207
+	 * Returns array of fields.
208
+	 * if $indexed parameter is true, then returns only indexed fields.
209
+	 *
210
+	 * @param boolean $indexed
211
+	 * @return array
212
+	 */
213
+	public function getFields($indexed = false)
214
+	{
215
+		$result = array();
216
+		foreach( $this->_fields as $field ) {
217
+			if( (!$indexed) || $field->isIndexed ) {
218
+				$result[ $field->name ] = $field->name;
219
+			}
220
+		}
221
+		return $result;
222
+	}
223
+
224
+	/**
225
+	 * Returns the total number of documents in this segment.
226
+	 *
227
+	 * @return integer
228
+	 */
229
+	public function count()
230
+	{
231
+		return $this->_docCount;
232
+	}
233
+
234
+
235
+	/**
236
+	 * Loads Term dictionary from TermInfoIndex file
237
+	 */
238
+	protected function _loadDictionary()
239
+	{
240
+		if ($this->_termDictionary !== null) {
241
+			return;
242
+		}
243
+
244
+		$this->_termDictionary = array();
245
+		$this->_termDictionaryInfos = array();
246
+
247
+		$tiiFile = $this->openCompoundFile('.tii');
248
+		$tiVersion = $tiiFile->readInt();
249
+		if ($tiVersion != (int)0xFFFFFFFE) {
250
+			throw new Zend_Search_Lucene_Exception('Wrong TermInfoIndexFile file format');
251
+		}
252
+
253
+		$indexTermCount = $tiiFile->readLong();
254
+						  $tiiFile->readInt();  // IndexInterval
255
+		$skipInterval   = $tiiFile->readInt();
256
+
257
+		$prevTerm     = '';
258
+		$freqPointer  =  0;
259
+		$proxPointer  =  0;
260
+		$indexPointer =  0;
261
+		for ($count = 0; $count < $indexTermCount; $count++) {
262
+			$termPrefixLength = $tiiFile->readVInt();
263
+			$termSuffix       = $tiiFile->readString();
264
+			$termValue        = substr( $prevTerm, 0, $termPrefixLength ) . $termSuffix;
265
+
266
+			$termFieldNum     = $tiiFile->readVInt();
267
+			$docFreq          = $tiiFile->readVInt();
268
+			$freqPointer     += $tiiFile->readVInt();
269
+			$proxPointer     += $tiiFile->readVInt();
270
+			if( $docFreq >= $skipInterval ) {
271
+				$skipDelta = $tiiFile->readVInt();
272
+			} else {
273
+				$skipDelta = 0;
274
+			}
275
+
276
+			$indexPointer += $tiiFile->readVInt();
277
+
278
+			$this->_termDictionary[] =  new Zend_Search_Lucene_Index_Term($termValue,$termFieldNum);
279
+			$this->_termDictionaryInfos[] =
280
+				new Zend_Search_Lucene_Index_TermInfo($docFreq, $freqPointer, $proxPointer, $skipDelta, $indexPointer);
281
+			$prevTerm = $termValue;
282
+		}
283
+	}
284
+
285
+
286
+	/**
287
+	 * Return segment name
288
+	 *
289
+	 * @return string
290
+	 */
291
+	public function getName()
292
+	{
293
+		return $this->_name;
294
+	}
295
+
296
+
297
+	/**
298
+	 * Scans terms dictionary and returns term info
299
+	 *
300
+	 * @param Zend_Search_Lucene_Index_Term $term
301
+	 * @return Zend_Search_Lucene_Index_TermInfo
302
+	 */
303
+	public function getTermInfo($term)
304
+	{
305
+		$this->_loadDictionary();
306
+
307
+		$searchField = $this->getFieldNum($term->field);
308
+
309
+		if ($searchField == -1) {
310
+			return null;
311
+		}
312
+		$searchDicField = $this->_fieldsDicPositions[$searchField];
313
+
314
+		// search for appropriate value in dictionary
315
+		$lowIndex = 0;
316
+		$highIndex = count($this->_termDictionary)-1;
317
+		while ($highIndex >= $lowIndex) {
318
+			// $mid = ($highIndex - $lowIndex)/2;
319
+			$mid = ($highIndex + $lowIndex) >> 1;
320
+			$midTerm = $this->_termDictionary[$mid];
321
+
322
+			$delta = $searchDicField - $this->_fieldsDicPositions[$midTerm->field];
323
+			if ($delta == 0) {
324
+				$delta = strcmp($term->text, $midTerm->text);
325
+			}
326
+
327
+			if ($delta < 0) {
328
+				$highIndex = $mid-1;
329
+			} elseif ($delta > 0) {
330
+				$lowIndex  = $mid+1;
331
+			} else {
332
+				return $this->_termDictionaryInfos[$mid]; // We got it!
333
+			}
334
+		}
335
+
336
+		if ($highIndex == -1) {
337
+			// Term is out of the dictionary range
338
+			return null;
339
+		}
340
+
341
+		$prevPosition = $highIndex;
342
+		$prevTerm = $this->_termDictionary[$prevPosition];
343
+		$prevTermInfo = $this->_termDictionaryInfos[ $prevPosition ];
344
+
345
+		$tisFile = $this->openCompoundFile('.tis');
346
+		$tiVersion = $tisFile->readInt();
347
+		if ($tiVersion != (int)0xFFFFFFFE) {
348
+			throw new Zend_Search_Lucene_Exception('Wrong TermInfoFile file format');
349
+		}
350
+
351
+		$termCount     = $tisFile->readLong();
352
+		$indexInterval = $tisFile->readInt();
353
+		$skipInterval  = $tisFile->readInt();
354
+
355
+		$tisFile->seek($prevTermInfo->indexPointer - 20 /* header size*/, SEEK_CUR);
356
+
357
+		$termValue    = $prevTerm->text;
358
+		$termFieldNum = $prevTerm->field;
359
+		$freqPointer = $prevTermInfo->freqPointer;
360
+		$proxPointer = $prevTermInfo->proxPointer;
361
+		for ($count = $prevPosition*$indexInterval + 1;
362
+			 $count < $termCount &&
363
+			 ( $this->_fieldsDicPositions[ $termFieldNum ] < $searchDicField ||
364
+			  ($this->_fieldsDicPositions[ $termFieldNum ] == $searchDicField &&
365
+			   strcmp($termValue, $term->text) < 0) );
366
+			 $count++) {
367
+			$termPrefixLength = $tisFile->readVInt();
368
+			$termSuffix       = $tisFile->readString();
369
+			$termFieldNum     = $tisFile->readVInt();
370
+			$termValue        = substr( $termValue, 0, $termPrefixLength ) . $termSuffix;
371
+
372
+			$docFreq      = $tisFile->readVInt();
373
+			$freqPointer += $tisFile->readVInt();
374
+			$proxPointer += $tisFile->readVInt();
375
+			if( $docFreq >= $skipInterval ) {
376
+				$skipOffset = $tisFile->readVInt();
377
+			} else {
378
+				$skipOffset = 0;
379
+			}
380
+		}
381
+
382
+		if ($termFieldNum == $searchField && $termValue == $term->text) {
383
+			return new Zend_Search_Lucene_Index_TermInfo($docFreq, $freqPointer, $proxPointer, $skipOffset);
384
+		} else {
385
+			return null;
386
+		}
387
+	}
388
+
389
+	/**
390
+	 * Returns normalization factor for specified documents
391
+	 *
392
+	 * @param integer $id
393
+	 * @param string $fieldName
394
+	 * @return string
395
+	 */
396
+	public function norm($id, $fieldName)
397
+	{
398
+		$fieldNum = $this->getFieldNum($fieldName);
399
+
400
+		if ( !($this->_fields[$fieldNum]->isIndexed) ) {
401
+			return null;
402
+		}
403
+
404
+		if ( !isset( $this->_norms[$fieldNum] )) {
405
+			$fFile = $this->openCompoundFile('.f' . $fieldNum);
406
+			$this->_norms[$fieldNum] = $fFile->readBytes($this->_docCount);
407
+		}
408
+
409
+		return Zend_Search_Lucene_Search_Similarity::decodeNorm( ord($this->_norms[$fieldNum]{$id}) );
410
+	}
411 411
 }
412 412
 
Please login to merge, or discard this patch.
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -135,13 +135,13 @@  discard block
 block discarded – undo
135 135
         $fieldNames = array();
136 136
         $fieldNums  = array();
137 137
         $this->_fields = array();
138
-        for ($count=0; $count < $fieldsCount; $count++) {
138
+        for ($count = 0; $count < $fieldsCount; $count++) {
139 139
             $fieldName = $fnmFile->readString();
140 140
             $fieldBits = $fnmFile->readByte();
141 141
             $this->_fields[$count] = new Zend_Search_Lucene_Index_FieldInfo($fieldName,
142 142
                                                                             $fieldBits & 1,
143 143
                                                                             $count,
144
-                                                                            $fieldBits & 2 );
144
+                                                                            $fieldBits & 2);
145 145
             if ($fieldBits & 0x10) {
146 146
                 // norms are omitted for the indexed field
147 147
                 $this->_norms[$count] = str_repeat(chr(Zend_Search_Lucene_Search_Similarity::encodeNorm(1.0)), $docCount);
@@ -165,13 +165,13 @@  discard block
 block discarded – undo
165 165
     {
166 166
         $filename = $this->_name . $extension;
167 167
 
168
-        if( !isset($this->_segFiles[ $filename ]) ) {
168
+        if (!isset($this->_segFiles[$filename])) {
169 169
             throw new Zend_Search_Lucene_Exception('Index compound file doesn\'t contain '
170
-                                       . $filename . ' file.' );
170
+                                       . $filename . ' file.');
171 171
         }
172 172
 
173
-        $file = $this->_directory->getFileObject( $this->_name.".cfs" );
174
-        $file->seek( $this->_segFiles[ $filename ] );
173
+        $file = $this->_directory->getFileObject($this->_name . ".cfs");
174
+        $file->seek($this->_segFiles[$filename]);
175 175
         return $file;
176 176
     }
177 177
 
@@ -183,8 +183,8 @@  discard block
 block discarded – undo
183 183
      */
184 184
     public function getFieldNum($fieldName)
185 185
     {
186
-        foreach( $this->_fields as $field ) {
187
-            if( $field->name == $fieldName ) {
186
+        foreach ($this->_fields as $field) {
187
+            if ($field->name == $fieldName) {
188 188
                 return $field->number;
189 189
             }
190 190
         }
@@ -213,9 +213,9 @@  discard block
 block discarded – undo
213 213
     public function getFields($indexed = false)
214 214
     {
215 215
         $result = array();
216
-        foreach( $this->_fields as $field ) {
217
-            if( (!$indexed) || $field->isIndexed ) {
218
-                $result[ $field->name ] = $field->name;
216
+        foreach ($this->_fields as $field) {
217
+            if ((!$indexed) || $field->isIndexed) {
218
+                $result[$field->name] = $field->name;
219 219
             }
220 220
         }
221 221
         return $result;
@@ -246,28 +246,28 @@  discard block
 block discarded – undo
246 246
 
247 247
         $tiiFile = $this->openCompoundFile('.tii');
248 248
         $tiVersion = $tiiFile->readInt();
249
-        if ($tiVersion != (int)0xFFFFFFFE) {
249
+        if ($tiVersion != (int) 0xFFFFFFFE) {
250 250
             throw new Zend_Search_Lucene_Exception('Wrong TermInfoIndexFile file format');
251 251
         }
252 252
 
253 253
         $indexTermCount = $tiiFile->readLong();
254
-                          $tiiFile->readInt();  // IndexInterval
254
+                          $tiiFile->readInt(); // IndexInterval
255 255
         $skipInterval   = $tiiFile->readInt();
256 256
 
257 257
         $prevTerm     = '';
258
-        $freqPointer  =  0;
259
-        $proxPointer  =  0;
260
-        $indexPointer =  0;
258
+        $freqPointer  = 0;
259
+        $proxPointer  = 0;
260
+        $indexPointer = 0;
261 261
         for ($count = 0; $count < $indexTermCount; $count++) {
262 262
             $termPrefixLength = $tiiFile->readVInt();
263 263
             $termSuffix       = $tiiFile->readString();
264
-            $termValue        = substr( $prevTerm, 0, $termPrefixLength ) . $termSuffix;
264
+            $termValue        = substr($prevTerm, 0, $termPrefixLength) . $termSuffix;
265 265
 
266 266
             $termFieldNum     = $tiiFile->readVInt();
267 267
             $docFreq          = $tiiFile->readVInt();
268 268
             $freqPointer     += $tiiFile->readVInt();
269 269
             $proxPointer     += $tiiFile->readVInt();
270
-            if( $docFreq >= $skipInterval ) {
270
+            if ($docFreq >= $skipInterval) {
271 271
                 $skipDelta = $tiiFile->readVInt();
272 272
             } else {
273 273
                 $skipDelta = 0;
@@ -275,7 +275,7 @@  discard block
 block discarded – undo
275 275
 
276 276
             $indexPointer += $tiiFile->readVInt();
277 277
 
278
-            $this->_termDictionary[] =  new Zend_Search_Lucene_Index_Term($termValue,$termFieldNum);
278
+            $this->_termDictionary[] = new Zend_Search_Lucene_Index_Term($termValue, $termFieldNum);
279 279
             $this->_termDictionaryInfos[] =
280 280
                 new Zend_Search_Lucene_Index_TermInfo($docFreq, $freqPointer, $proxPointer, $skipDelta, $indexPointer);
281 281
             $prevTerm = $termValue;
@@ -313,7 +313,7 @@  discard block
 block discarded – undo
313 313
 
314 314
         // search for appropriate value in dictionary
315 315
         $lowIndex = 0;
316
-        $highIndex = count($this->_termDictionary)-1;
316
+        $highIndex = count($this->_termDictionary) - 1;
317 317
         while ($highIndex >= $lowIndex) {
318 318
             // $mid = ($highIndex - $lowIndex)/2;
319 319
             $mid = ($highIndex + $lowIndex) >> 1;
@@ -325,9 +325,9 @@  discard block
 block discarded – undo
325 325
             }
326 326
 
327 327
             if ($delta < 0) {
328
-                $highIndex = $mid-1;
328
+                $highIndex = $mid - 1;
329 329
             } elseif ($delta > 0) {
330
-                $lowIndex  = $mid+1;
330
+                $lowIndex  = $mid + 1;
331 331
             } else {
332 332
                 return $this->_termDictionaryInfos[$mid]; // We got it!
333 333
             }
@@ -340,11 +340,11 @@  discard block
 block discarded – undo
340 340
 
341 341
         $prevPosition = $highIndex;
342 342
         $prevTerm = $this->_termDictionary[$prevPosition];
343
-        $prevTermInfo = $this->_termDictionaryInfos[ $prevPosition ];
343
+        $prevTermInfo = $this->_termDictionaryInfos[$prevPosition];
344 344
 
345 345
         $tisFile = $this->openCompoundFile('.tis');
346 346
         $tiVersion = $tisFile->readInt();
347
-        if ($tiVersion != (int)0xFFFFFFFE) {
347
+        if ($tiVersion != (int) 0xFFFFFFFE) {
348 348
             throw new Zend_Search_Lucene_Exception('Wrong TermInfoFile file format');
349 349
         }
350 350
 
@@ -358,21 +358,21 @@  discard block
 block discarded – undo
358 358
         $termFieldNum = $prevTerm->field;
359 359
         $freqPointer = $prevTermInfo->freqPointer;
360 360
         $proxPointer = $prevTermInfo->proxPointer;
361
-        for ($count = $prevPosition*$indexInterval + 1;
361
+        for ($count = $prevPosition * $indexInterval + 1;
362 362
              $count < $termCount &&
363
-             ( $this->_fieldsDicPositions[ $termFieldNum ] < $searchDicField ||
364
-              ($this->_fieldsDicPositions[ $termFieldNum ] == $searchDicField &&
365
-               strcmp($termValue, $term->text) < 0) );
363
+             ($this->_fieldsDicPositions[$termFieldNum] < $searchDicField ||
364
+              ($this->_fieldsDicPositions[$termFieldNum] == $searchDicField &&
365
+               strcmp($termValue, $term->text) < 0));
366 366
              $count++) {
367 367
             $termPrefixLength = $tisFile->readVInt();
368 368
             $termSuffix       = $tisFile->readString();
369 369
             $termFieldNum     = $tisFile->readVInt();
370
-            $termValue        = substr( $termValue, 0, $termPrefixLength ) . $termSuffix;
370
+            $termValue        = substr($termValue, 0, $termPrefixLength) . $termSuffix;
371 371
 
372 372
             $docFreq      = $tisFile->readVInt();
373 373
             $freqPointer += $tisFile->readVInt();
374 374
             $proxPointer += $tisFile->readVInt();
375
-            if( $docFreq >= $skipInterval ) {
375
+            if ($docFreq >= $skipInterval) {
376 376
                 $skipOffset = $tisFile->readVInt();
377 377
             } else {
378 378
                 $skipOffset = 0;
@@ -397,16 +397,16 @@  discard block
 block discarded – undo
397 397
     {
398 398
         $fieldNum = $this->getFieldNum($fieldName);
399 399
 
400
-        if ( !($this->_fields[$fieldNum]->isIndexed) ) {
400
+        if (!($this->_fields[$fieldNum]->isIndexed)) {
401 401
             return null;
402 402
         }
403 403
 
404
-        if ( !isset( $this->_norms[$fieldNum] )) {
404
+        if (!isset($this->_norms[$fieldNum])) {
405 405
             $fFile = $this->openCompoundFile('.f' . $fieldNum);
406 406
             $this->_norms[$fieldNum] = $fFile->readBytes($this->_docCount);
407 407
         }
408 408
 
409
-        return Zend_Search_Lucene_Search_Similarity::decodeNorm( ord($this->_norms[$fieldNum]{$id}) );
409
+        return Zend_Search_Lucene_Search_Similarity::decodeNorm(ord($this->_norms[$fieldNum]{$id}));
410 410
     }
411 411
 }
412 412
 
Please login to merge, or discard this patch.
demos/quickstart/protected/index/Zend/Search/Lucene/Index/TermInfo.php 2 patches
Doc Comments   +5 added lines patch added patch discarded remove patch
@@ -65,6 +65,11 @@
 block discarded – undo
65 65
      */
66 66
     public $indexPointer;
67 67
 
68
+    /**
69
+     * @param integer $docFreq
70
+     * @param integer $skipOffset
71
+     * @param integer $indexPointer
72
+     */
68 73
     public function __construct($docFreq, $freqPointer, $proxPointer, $skipOffset, $indexPointer = null)
69 74
     {
70 75
         $this->docFreq      = $docFreq;
Please login to merge, or discard this patch.
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -29,49 +29,49 @@
 block discarded – undo
29 29
  */
30 30
 class Zend_Search_Lucene_Index_TermInfo
31 31
 {
32
-    /**
33
-     * The number of documents which contain the term.
34
-     *
35
-     * @var integer
36
-     */
37
-    public $docFreq;
32
+	/**
33
+	 * The number of documents which contain the term.
34
+	 *
35
+	 * @var integer
36
+	 */
37
+	public $docFreq;
38 38
 
39
-    /**
40
-     * Data offset in a Frequencies file.
41
-     *
42
-     * @var integer
43
-     */
44
-    public $freqPointer;
39
+	/**
40
+	 * Data offset in a Frequencies file.
41
+	 *
42
+	 * @var integer
43
+	 */
44
+	public $freqPointer;
45 45
 
46
-    /**
47
-     * Data offset in a Positions file.
48
-     *
49
-     * @var integer
50
-     */
51
-    public $proxPointer;
46
+	/**
47
+	 * Data offset in a Positions file.
48
+	 *
49
+	 * @var integer
50
+	 */
51
+	public $proxPointer;
52 52
 
53
-    /**
54
-     * ScipData offset in a Frequencies file.
55
-     *
56
-     * @var integer
57
-     */
58
-    public $skipOffset;
53
+	/**
54
+	 * ScipData offset in a Frequencies file.
55
+	 *
56
+	 * @var integer
57
+	 */
58
+	public $skipOffset;
59 59
 
60
-    /**
61
-     * Term offset of the _next_ term in a TermDictionary file.
62
-     * Used only for Term Index
63
-     *
64
-     * @var integer
65
-     */
66
-    public $indexPointer;
60
+	/**
61
+	 * Term offset of the _next_ term in a TermDictionary file.
62
+	 * Used only for Term Index
63
+	 *
64
+	 * @var integer
65
+	 */
66
+	public $indexPointer;
67 67
 
68
-    public function __construct($docFreq, $freqPointer, $proxPointer, $skipOffset, $indexPointer = null)
69
-    {
70
-        $this->docFreq      = $docFreq;
71
-        $this->freqPointer  = $freqPointer;
72
-        $this->proxPointer  = $proxPointer;
73
-        $this->skipOffset   = $skipOffset;
74
-        $this->indexPointer = $indexPointer;
75
-    }
68
+	public function __construct($docFreq, $freqPointer, $proxPointer, $skipOffset, $indexPointer = null)
69
+	{
70
+		$this->docFreq      = $docFreq;
71
+		$this->freqPointer  = $freqPointer;
72
+		$this->proxPointer  = $proxPointer;
73
+		$this->skipOffset   = $skipOffset;
74
+		$this->indexPointer = $indexPointer;
75
+	}
76 76
 }
77 77
 
Please login to merge, or discard this patch.
demos/quickstart/protected/index/Zend/Search/Lucene/Search/Query/Phrase.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -258,7 +258,7 @@
 block discarded – undo
258 258
      * Score calculator for exact phrase queries (terms sequence is fixed)
259 259
      *
260 260
      * @param integer $docId
261
-     * @return float
261
+     * @return integer
262 262
      */
263 263
     public function _exactPhraseFreq($docId)
264 264
     {
Please login to merge, or discard this patch.
Indentation   +374 added lines, -374 removed lines patch added patch discarded remove patch
@@ -40,385 +40,385 @@
 block discarded – undo
40 40
  */
41 41
 class Zend_Search_Lucene_Search_Query_Phrase extends Zend_Search_Lucene_Search_Query
42 42
 {
43
-    /**
44
-     * Terms to find.
45
-     * Array of Zend_Search_Lucene_Index_Term objects.
46
-     *
47
-     * @var array
48
-     */
49
-    private $_terms;
50
-
51
-    /**
52
-     * Term positions (relative positions of terms within the phrase).
53
-     * Array of integers
54
-     *
55
-     * @var array
56
-     */
57
-    private $_offsets;
58
-
59
-    /**
60
-     * Sets the number of other words permitted between words in query phrase.
61
-     * If zero, then this is an exact phrase search.  For larger values this works
62
-     * like a WITHIN or NEAR operator.
63
-     *
64
-     * The slop is in fact an edit-distance, where the units correspond to
65
-     * moves of terms in the query phrase out of position.  For example, to switch
66
-     * the order of two words requires two moves (the first move places the words
67
-     * atop one another), so to permit re-orderings of phrases, the slop must be
68
-     * at least two.
69
-     * More exact matches are scored higher than sloppier matches, thus search
70
-     * results are sorted by exactness.
71
-     *
72
-     * The slop is zero by default, requiring exact matches.
73
-     *
74
-     * @var unknown_type
75
-     */
76
-    private $_slop;
77
-
78
-    /**
79
-     * Result vector.
80
-     * Bitset or array of document IDs
81
-     * (depending from Bitset extension availability).
82
-     *
83
-     * @var mixed
84
-     */
85
-    private $_resVector = null;
86
-
87
-    /**
88
-     * Terms positions vectors.
89
-     * Array of Arrays:
90
-     * term1Id => (docId => array( pos1, pos2, ... ), ...)
91
-     * term2Id => (docId => array( pos1, pos2, ... ), ...)
92
-     *
93
-     * @var array
94
-     */
95
-    private $_termsPositions = array();
96
-
97
-    /**
98
-     * Class constructor.  Create a new prase query.
99
-     *
100
-     * @param string $field    Field to search.
101
-     * @param array  $terms    Terms to search Array of strings.
102
-     * @param array  $offsets  Relative term positions. Array of integers.
103
-     * @throws Zend_Search_Lucene_Exception
104
-     */
105
-    public function __construct($terms = null, $offsets = null, $field = null)
106
-    {
107
-        $this->_slop = 0;
108
-
109
-        if (is_array($terms)) {
110
-            $this->_terms = array();
111
-            foreach ($terms as $termId => $termText) {
112
-                $this->_terms[$termId] = ($field !== null)? new Zend_Search_Lucene_Index_Term($termText, $field):
113
-                                                            new Zend_Search_Lucene_Index_Term($termText);
114
-            }
115
-        } else if ($terms === null) {
116
-            $this->_terms = array();
117
-        } else {
118
-            throw new Zend_Search_Lucene_Exception('terms argument must be array of strings or null');
119
-        }
120
-
121
-        if (is_array($offsets)) {
122
-            if (count($this->_terms) != count($offsets)) {
123
-                throw new Zend_Search_Lucene_Exception('terms and offsets arguments must have the same size.');
124
-            }
125
-            $this->_offsets = $offsets;
126
-        } else if ($offsets === null) {
127
-            $this->_offsets = array();
128
-            foreach ($this->_terms as $termId => $term) {
129
-                $position = count($this->_offsets);
130
-                $this->_offsets[$termId] = $position;
131
-            }
132
-        } else {
133
-            throw new Zend_Search_Lucene_Exception('offsets argument must be array of strings or null');
134
-        }
135
-    }
136
-
137
-    /**
138
-     * Set slop
139
-     *
140
-     * @param integer $slop
141
-     */
142
-    public function setSlop($slop)
143
-    {
144
-        $this->_slop = $slop;
145
-    }
146
-
147
-
148
-    /**
149
-     * Get slop
150
-     *
151
-     * @return integer
152
-     */
153
-    public function getSlop()
154
-    {
155
-        return $this->_slop;
156
-    }
157
-
158
-
159
-    /**
160
-     * Adds a term to the end of the query phrase.
161
-     * The relative position of the term is specified explicitly or the one immediately
162
-     * after the last term added.
163
-     *
164
-     * @param Zend_Search_Lucene_Index_Term $term
165
-     * @param integer $position
166
-     */
167
-    public function addTerm(Zend_Search_Lucene_Index_Term $term, $position = null) {
168
-        if ((count($this->_terms) != 0)&&(end($this->_terms)->field != $term->field)) {
169
-            throw new Zend_Search_Lucene_Exception('All phrase terms must be in the same field: ' .
170
-                                                   $term->field . ':' . $term->text);
171
-        }
172
-
173
-        $this->_terms[] = $term;
174
-        if ($position !== null) {
175
-            $this->_offsets[] = $position;
176
-        } else if (count($this->_offsets) != 0) {
177
-            $this->_offsets[] = end($this->_offsets) + 1;
178
-        } else {
179
-            $this->_offsets[] = 0;
180
-        }
181
-    }
182
-
183
-
184
-    /**
185
-     * Returns query term
186
-     *
187
-     * @return array
188
-     */
189
-    public function getTerms()
190
-    {
191
-        return $this->_terms;
192
-    }
193
-
194
-
195
-    /**
196
-     * Set weight for specified term
197
-     *
198
-     * @param integer $num
199
-     * @param Zend_Search_Lucene_Search_Weight_Term $weight
200
-     */
201
-    public function setWeight($num, $weight)
202
-    {
203
-        $this->_weights[$num] = $weight;
204
-    }
205
-
206
-
207
-    /**
208
-     * Constructs an appropriate Weight implementation for this query.
209
-     *
210
-     * @param Zend_Search_Lucene $reader
211
-     * @return Zend_Search_Lucene_Search_Weight
212
-     */
213
-    protected function _createWeight($reader)
214
-    {
215
-        return new Zend_Search_Lucene_Search_Weight_Phrase($this, $reader);
216
-    }
217
-
218
-
219
-    /**
220
-     * Calculate result vector
221
-     *
222
-     * @param Zend_Search_Lucene $reader
223
-     */
224
-    private function _calculateResult($reader)
225
-    {
226
-        if (extension_loaded('bitset')) {
227
-            foreach( $this->_terms as $termId=>$term ) {
228
-                if($this->_resVector === null) {
229
-                    $this->_resVector = bitset_from_array($reader->termDocs($term));
230
-                } else {
231
-                    $this->_resVector = bitset_intersection(
232
-                                $this->_resVector,
233
-                                bitset_from_array($reader->termDocs($term)) );
234
-                }
235
-
236
-                $this->_termsPositions[$termId] = $reader->termPositions($term);
237
-            }
238
-        } else {
239
-            foreach( $this->_terms as $termId=>$term ) {
240
-                if($this->_resVector === null) {
241
-                    $this->_resVector = array_flip($reader->termDocs($term));
242
-                } else {
243
-                    $termDocs = array_flip($reader->termDocs($term));
244
-                    foreach($this->_resVector as $key=>$value) {
245
-                        if (!isset( $termDocs[$key] )) {
246
-                            unset( $this->_resVector[$key] );
247
-                        }
248
-                    }
249
-                }
250
-
251
-                $this->_termsPositions[$termId] = $reader->termPositions($term);
252
-            }
253
-        }
254
-    }
255
-
256
-
257
-    /**
258
-     * Score calculator for exact phrase queries (terms sequence is fixed)
259
-     *
260
-     * @param integer $docId
261
-     * @return float
262
-     */
263
-    public function _exactPhraseFreq($docId)
264
-    {
265
-        $freq = 0;
266
-
267
-        // Term Id with lowest cardinality
268
-        $lowCardTermId = null;
269
-
270
-        // Calculate $lowCardTermId
271
-        foreach ($this->_terms as $termId => $term) {
272
-            if ($lowCardTermId === null ||
273
-                count($this->_termsPositions[$termId][$docId]) <
274
-                count($this->_termsPositions[$lowCardTermId][$docId]) ) {
275
-                    $lowCardTermId = $termId;
276
-                }
277
-        }
278
-
279
-        // Walk through positions of the term with lowest cardinality
280
-        foreach ($this->_termsPositions[$lowCardTermId][$docId] as $lowCardPos) {
281
-            // We expect phrase to be found
282
-            $freq++;
283
-
284
-            // Walk through other terms
285
-            foreach ($this->_terms as $termId => $term) {
286
-                if ($termId != $lowCardTermId) {
287
-                    $expectedPosition = $lowCardPos +
288
-                                            ($this->_offsets[$termId] -
289
-                                             $this->_offsets[$lowCardTermId]);
290
-
291
-                    if (!in_array($expectedPosition, $this->_termsPositions[$termId][$docId])) {
292
-                        $freq--;  // Phrase wasn't found.
293
-                        break;
294
-                    }
295
-                }
296
-            }
297
-        }
298
-
299
-        return $freq;
300
-    }
301
-
302
-    /**
303
-     * Score calculator for sloppy phrase queries (terms sequence is fixed)
304
-     *
305
-     * @param integer $docId
306
-     * @param Zend_Search_Lucene $reader
307
-     * @return float
308
-     */
309
-    public function _sloppyPhraseFreq($docId, Zend_Search_Lucene $reader)
310
-    {
311
-        $freq = 0;
312
-
313
-        $phraseQueue = array();
314
-        $phraseQueue[0] = array(); // empty phrase
315
-        $lastTerm = null;
316
-
317
-        // Walk through the terms to create phrases.
318
-        foreach ($this->_terms as $termId => $term) {
319
-            $queueSize = count($phraseQueue);
320
-            $firstPass = true;
321
-
322
-            // Walk through the term positions.
323
-            // Each term position produces a set of phrases.
324
-            foreach ($this->_termsPositions[$termId][$docId] as $termPosition ) {
325
-                if ($firstPass) {
326
-                    for ($count = 0; $count < $queueSize; $count++) {
327
-                        $phraseQueue[$count][$termId] = $termPosition;
328
-                    }
329
-                } else {
330
-                    for ($count = 0; $count < $queueSize; $count++) {
331
-                        if ($lastTerm !== null &&
332
-                            abs( $termPosition - $phraseQueue[$count][$lastTerm] -
333
-                                 ($this->_offsets[$termId] - $this->_offsets[$lastTerm])) > $this->_slop) {
334
-                            continue;
335
-                        }
336
-
337
-                        $newPhraseId = count($phraseQueue);
338
-                        $phraseQueue[$newPhraseId]          = $phraseQueue[$count];
339
-                        $phraseQueue[$newPhraseId][$termId] = $termPosition;
340
-                    }
341
-
342
-                }
343
-
344
-                $firstPass = false;
345
-            }
346
-            $lastTerm = $termId;
347
-        }
348
-
349
-
350
-        foreach ($phraseQueue as $phrasePos) {
351
-            $minDistance = null;
352
-
353
-            for ($shift = -$this->_slop; $shift <= $this->_slop; $shift++) {
354
-                $distance = 0;
355
-                $start = reset($phrasePos) - reset($this->_offsets) + $shift;
356
-
357
-                foreach ($this->_terms as $termId => $term) {
358
-                    $distance += abs($phrasePos[$termId] - $this->_offsets[$termId] - $start);
359
-
360
-                    if($distance > $this->_slop) {
361
-                        break;
362
-                    }
363
-                }
364
-
365
-                if ($minDistance === null || $distance < $minDistance) {
366
-                    $minDistance = $distance;
367
-                }
368
-            }
369
-
370
-            if ($minDistance <= $this->_slop) {
371
-                $freq += $reader->getSimilarity()->sloppyFreq($minDistance);
372
-            }
373
-        }
374
-
375
-        return $freq;
376
-    }
377
-
378
-
379
-    /**
380
-     * Score specified document
381
-     *
382
-     * @param integer $docId
383
-     * @param Zend_Search_Lucene $reader
384
-     * @return float
385
-     */
386
-    public function score($docId, $reader)
387
-    {
388
-        // optimize zero-term case
389
-        if (count($this->_terms) == 0) {
390
-            return 0;
391
-        }
392
-
393
-        if($this->_resVector === null) {
394
-            $this->_calculateResult($reader);
395
-            $this->_initWeight($reader);
396
-        }
397
-
398
-        if ( (extension_loaded('bitset')) ?
399
-                bitset_in($this->_resVector, $docId) :
400
-                isset($this->_resVector[$docId])  ) {
401
-            if ($this->_slop == 0) {
402
-                $freq = $this->_exactPhraseFreq($docId);
403
-            } else {
404
-                $freq = $this->_sloppyPhraseFreq($docId, $reader);
405
-            }
43
+	/**
44
+	 * Terms to find.
45
+	 * Array of Zend_Search_Lucene_Index_Term objects.
46
+	 *
47
+	 * @var array
48
+	 */
49
+	private $_terms;
50
+
51
+	/**
52
+	 * Term positions (relative positions of terms within the phrase).
53
+	 * Array of integers
54
+	 *
55
+	 * @var array
56
+	 */
57
+	private $_offsets;
58
+
59
+	/**
60
+	 * Sets the number of other words permitted between words in query phrase.
61
+	 * If zero, then this is an exact phrase search.  For larger values this works
62
+	 * like a WITHIN or NEAR operator.
63
+	 *
64
+	 * The slop is in fact an edit-distance, where the units correspond to
65
+	 * moves of terms in the query phrase out of position.  For example, to switch
66
+	 * the order of two words requires two moves (the first move places the words
67
+	 * atop one another), so to permit re-orderings of phrases, the slop must be
68
+	 * at least two.
69
+	 * More exact matches are scored higher than sloppier matches, thus search
70
+	 * results are sorted by exactness.
71
+	 *
72
+	 * The slop is zero by default, requiring exact matches.
73
+	 *
74
+	 * @var unknown_type
75
+	 */
76
+	private $_slop;
77
+
78
+	/**
79
+	 * Result vector.
80
+	 * Bitset or array of document IDs
81
+	 * (depending from Bitset extension availability).
82
+	 *
83
+	 * @var mixed
84
+	 */
85
+	private $_resVector = null;
86
+
87
+	/**
88
+	 * Terms positions vectors.
89
+	 * Array of Arrays:
90
+	 * term1Id => (docId => array( pos1, pos2, ... ), ...)
91
+	 * term2Id => (docId => array( pos1, pos2, ... ), ...)
92
+	 *
93
+	 * @var array
94
+	 */
95
+	private $_termsPositions = array();
96
+
97
+	/**
98
+	 * Class constructor.  Create a new prase query.
99
+	 *
100
+	 * @param string $field    Field to search.
101
+	 * @param array  $terms    Terms to search Array of strings.
102
+	 * @param array  $offsets  Relative term positions. Array of integers.
103
+	 * @throws Zend_Search_Lucene_Exception
104
+	 */
105
+	public function __construct($terms = null, $offsets = null, $field = null)
106
+	{
107
+		$this->_slop = 0;
108
+
109
+		if (is_array($terms)) {
110
+			$this->_terms = array();
111
+			foreach ($terms as $termId => $termText) {
112
+				$this->_terms[$termId] = ($field !== null)? new Zend_Search_Lucene_Index_Term($termText, $field):
113
+															new Zend_Search_Lucene_Index_Term($termText);
114
+			}
115
+		} else if ($terms === null) {
116
+			$this->_terms = array();
117
+		} else {
118
+			throw new Zend_Search_Lucene_Exception('terms argument must be array of strings or null');
119
+		}
120
+
121
+		if (is_array($offsets)) {
122
+			if (count($this->_terms) != count($offsets)) {
123
+				throw new Zend_Search_Lucene_Exception('terms and offsets arguments must have the same size.');
124
+			}
125
+			$this->_offsets = $offsets;
126
+		} else if ($offsets === null) {
127
+			$this->_offsets = array();
128
+			foreach ($this->_terms as $termId => $term) {
129
+				$position = count($this->_offsets);
130
+				$this->_offsets[$termId] = $position;
131
+			}
132
+		} else {
133
+			throw new Zend_Search_Lucene_Exception('offsets argument must be array of strings or null');
134
+		}
135
+	}
136
+
137
+	/**
138
+	 * Set slop
139
+	 *
140
+	 * @param integer $slop
141
+	 */
142
+	public function setSlop($slop)
143
+	{
144
+		$this->_slop = $slop;
145
+	}
146
+
147
+
148
+	/**
149
+	 * Get slop
150
+	 *
151
+	 * @return integer
152
+	 */
153
+	public function getSlop()
154
+	{
155
+		return $this->_slop;
156
+	}
157
+
158
+
159
+	/**
160
+	 * Adds a term to the end of the query phrase.
161
+	 * The relative position of the term is specified explicitly or the one immediately
162
+	 * after the last term added.
163
+	 *
164
+	 * @param Zend_Search_Lucene_Index_Term $term
165
+	 * @param integer $position
166
+	 */
167
+	public function addTerm(Zend_Search_Lucene_Index_Term $term, $position = null) {
168
+		if ((count($this->_terms) != 0)&&(end($this->_terms)->field != $term->field)) {
169
+			throw new Zend_Search_Lucene_Exception('All phrase terms must be in the same field: ' .
170
+												   $term->field . ':' . $term->text);
171
+		}
172
+
173
+		$this->_terms[] = $term;
174
+		if ($position !== null) {
175
+			$this->_offsets[] = $position;
176
+		} else if (count($this->_offsets) != 0) {
177
+			$this->_offsets[] = end($this->_offsets) + 1;
178
+		} else {
179
+			$this->_offsets[] = 0;
180
+		}
181
+	}
182
+
183
+
184
+	/**
185
+	 * Returns query term
186
+	 *
187
+	 * @return array
188
+	 */
189
+	public function getTerms()
190
+	{
191
+		return $this->_terms;
192
+	}
193
+
194
+
195
+	/**
196
+	 * Set weight for specified term
197
+	 *
198
+	 * @param integer $num
199
+	 * @param Zend_Search_Lucene_Search_Weight_Term $weight
200
+	 */
201
+	public function setWeight($num, $weight)
202
+	{
203
+		$this->_weights[$num] = $weight;
204
+	}
205
+
206
+
207
+	/**
208
+	 * Constructs an appropriate Weight implementation for this query.
209
+	 *
210
+	 * @param Zend_Search_Lucene $reader
211
+	 * @return Zend_Search_Lucene_Search_Weight
212
+	 */
213
+	protected function _createWeight($reader)
214
+	{
215
+		return new Zend_Search_Lucene_Search_Weight_Phrase($this, $reader);
216
+	}
217
+
218
+
219
+	/**
220
+	 * Calculate result vector
221
+	 *
222
+	 * @param Zend_Search_Lucene $reader
223
+	 */
224
+	private function _calculateResult($reader)
225
+	{
226
+		if (extension_loaded('bitset')) {
227
+			foreach( $this->_terms as $termId=>$term ) {
228
+				if($this->_resVector === null) {
229
+					$this->_resVector = bitset_from_array($reader->termDocs($term));
230
+				} else {
231
+					$this->_resVector = bitset_intersection(
232
+								$this->_resVector,
233
+								bitset_from_array($reader->termDocs($term)) );
234
+				}
235
+
236
+				$this->_termsPositions[$termId] = $reader->termPositions($term);
237
+			}
238
+		} else {
239
+			foreach( $this->_terms as $termId=>$term ) {
240
+				if($this->_resVector === null) {
241
+					$this->_resVector = array_flip($reader->termDocs($term));
242
+				} else {
243
+					$termDocs = array_flip($reader->termDocs($term));
244
+					foreach($this->_resVector as $key=>$value) {
245
+						if (!isset( $termDocs[$key] )) {
246
+							unset( $this->_resVector[$key] );
247
+						}
248
+					}
249
+				}
250
+
251
+				$this->_termsPositions[$termId] = $reader->termPositions($term);
252
+			}
253
+		}
254
+	}
255
+
256
+
257
+	/**
258
+	 * Score calculator for exact phrase queries (terms sequence is fixed)
259
+	 *
260
+	 * @param integer $docId
261
+	 * @return float
262
+	 */
263
+	public function _exactPhraseFreq($docId)
264
+	{
265
+		$freq = 0;
266
+
267
+		// Term Id with lowest cardinality
268
+		$lowCardTermId = null;
269
+
270
+		// Calculate $lowCardTermId
271
+		foreach ($this->_terms as $termId => $term) {
272
+			if ($lowCardTermId === null ||
273
+				count($this->_termsPositions[$termId][$docId]) <
274
+				count($this->_termsPositions[$lowCardTermId][$docId]) ) {
275
+					$lowCardTermId = $termId;
276
+				}
277
+		}
278
+
279
+		// Walk through positions of the term with lowest cardinality
280
+		foreach ($this->_termsPositions[$lowCardTermId][$docId] as $lowCardPos) {
281
+			// We expect phrase to be found
282
+			$freq++;
283
+
284
+			// Walk through other terms
285
+			foreach ($this->_terms as $termId => $term) {
286
+				if ($termId != $lowCardTermId) {
287
+					$expectedPosition = $lowCardPos +
288
+											($this->_offsets[$termId] -
289
+											 $this->_offsets[$lowCardTermId]);
290
+
291
+					if (!in_array($expectedPosition, $this->_termsPositions[$termId][$docId])) {
292
+						$freq--;  // Phrase wasn't found.
293
+						break;
294
+					}
295
+				}
296
+			}
297
+		}
298
+
299
+		return $freq;
300
+	}
301
+
302
+	/**
303
+	 * Score calculator for sloppy phrase queries (terms sequence is fixed)
304
+	 *
305
+	 * @param integer $docId
306
+	 * @param Zend_Search_Lucene $reader
307
+	 * @return float
308
+	 */
309
+	public function _sloppyPhraseFreq($docId, Zend_Search_Lucene $reader)
310
+	{
311
+		$freq = 0;
312
+
313
+		$phraseQueue = array();
314
+		$phraseQueue[0] = array(); // empty phrase
315
+		$lastTerm = null;
316
+
317
+		// Walk through the terms to create phrases.
318
+		foreach ($this->_terms as $termId => $term) {
319
+			$queueSize = count($phraseQueue);
320
+			$firstPass = true;
321
+
322
+			// Walk through the term positions.
323
+			// Each term position produces a set of phrases.
324
+			foreach ($this->_termsPositions[$termId][$docId] as $termPosition ) {
325
+				if ($firstPass) {
326
+					for ($count = 0; $count < $queueSize; $count++) {
327
+						$phraseQueue[$count][$termId] = $termPosition;
328
+					}
329
+				} else {
330
+					for ($count = 0; $count < $queueSize; $count++) {
331
+						if ($lastTerm !== null &&
332
+							abs( $termPosition - $phraseQueue[$count][$lastTerm] -
333
+								 ($this->_offsets[$termId] - $this->_offsets[$lastTerm])) > $this->_slop) {
334
+							continue;
335
+						}
336
+
337
+						$newPhraseId = count($phraseQueue);
338
+						$phraseQueue[$newPhraseId]          = $phraseQueue[$count];
339
+						$phraseQueue[$newPhraseId][$termId] = $termPosition;
340
+					}
341
+
342
+				}
343
+
344
+				$firstPass = false;
345
+			}
346
+			$lastTerm = $termId;
347
+		}
348
+
349
+
350
+		foreach ($phraseQueue as $phrasePos) {
351
+			$minDistance = null;
352
+
353
+			for ($shift = -$this->_slop; $shift <= $this->_slop; $shift++) {
354
+				$distance = 0;
355
+				$start = reset($phrasePos) - reset($this->_offsets) + $shift;
356
+
357
+				foreach ($this->_terms as $termId => $term) {
358
+					$distance += abs($phrasePos[$termId] - $this->_offsets[$termId] - $start);
359
+
360
+					if($distance > $this->_slop) {
361
+						break;
362
+					}
363
+				}
364
+
365
+				if ($minDistance === null || $distance < $minDistance) {
366
+					$minDistance = $distance;
367
+				}
368
+			}
369
+
370
+			if ($minDistance <= $this->_slop) {
371
+				$freq += $reader->getSimilarity()->sloppyFreq($minDistance);
372
+			}
373
+		}
374
+
375
+		return $freq;
376
+	}
377
+
378
+
379
+	/**
380
+	 * Score specified document
381
+	 *
382
+	 * @param integer $docId
383
+	 * @param Zend_Search_Lucene $reader
384
+	 * @return float
385
+	 */
386
+	public function score($docId, $reader)
387
+	{
388
+		// optimize zero-term case
389
+		if (count($this->_terms) == 0) {
390
+			return 0;
391
+		}
392
+
393
+		if($this->_resVector === null) {
394
+			$this->_calculateResult($reader);
395
+			$this->_initWeight($reader);
396
+		}
397
+
398
+		if ( (extension_loaded('bitset')) ?
399
+				bitset_in($this->_resVector, $docId) :
400
+				isset($this->_resVector[$docId])  ) {
401
+			if ($this->_slop == 0) {
402
+				$freq = $this->_exactPhraseFreq($docId);
403
+			} else {
404
+				$freq = $this->_sloppyPhraseFreq($docId, $reader);
405
+			}
406 406
 
407 407
 /*
408 408
             return $reader->getSimilarity()->tf($freq) *
409 409
                    $this->_weight->getValue() *
410 410
                    $reader->norm($docId, reset($this->_terms)->field);
411 411
 */
412
-            if ($freq != 0) {
413
-                $tf = $reader->getSimilarity()->tf($freq);
414
-                $weight = $this->_weight->getValue();
415
-                $norm = $reader->norm($docId, reset($this->_terms)->field);
416
-
417
-                return $tf*$weight*$norm;
418
-            }
419
-        } else {
420
-            return 0;
421
-        }
422
-    }
412
+			if ($freq != 0) {
413
+				$tf = $reader->getSimilarity()->tf($freq);
414
+				$weight = $this->_weight->getValue();
415
+				$norm = $reader->norm($docId, reset($this->_terms)->field);
416
+
417
+				return $tf*$weight*$norm;
418
+			}
419
+		} else {
420
+			return 0;
421
+		}
422
+	}
423 423
 }
424 424
 
Please login to merge, or discard this patch.
Spacing   +18 added lines, -20 removed lines patch added patch discarded remove patch
@@ -109,8 +109,7 @@  discard block
 block discarded – undo
109 109
         if (is_array($terms)) {
110 110
             $this->_terms = array();
111 111
             foreach ($terms as $termId => $termText) {
112
-                $this->_terms[$termId] = ($field !== null)? new Zend_Search_Lucene_Index_Term($termText, $field):
113
-                                                            new Zend_Search_Lucene_Index_Term($termText);
112
+                $this->_terms[$termId] = ($field !== null) ? new Zend_Search_Lucene_Index_Term($termText, $field) : new Zend_Search_Lucene_Index_Term($termText);
114 113
             }
115 114
         } else if ($terms === null) {
116 115
             $this->_terms = array();
@@ -165,7 +164,7 @@  discard block
 block discarded – undo
165 164
      * @param integer $position
166 165
      */
167 166
     public function addTerm(Zend_Search_Lucene_Index_Term $term, $position = null) {
168
-        if ((count($this->_terms) != 0)&&(end($this->_terms)->field != $term->field)) {
167
+        if ((count($this->_terms) != 0) && (end($this->_terms)->field != $term->field)) {
169 168
             throw new Zend_Search_Lucene_Exception('All phrase terms must be in the same field: ' .
170 169
                                                    $term->field . ':' . $term->text);
171 170
         }
@@ -224,8 +223,8 @@  discard block
 block discarded – undo
224 223
     private function _calculateResult($reader)
225 224
     {
226 225
         if (extension_loaded('bitset')) {
227
-            foreach( $this->_terms as $termId=>$term ) {
228
-                if($this->_resVector === null) {
226
+            foreach ($this->_terms as $termId=>$term) {
227
+                if ($this->_resVector === null) {
229 228
                     $this->_resVector = bitset_from_array($reader->termDocs($term));
230 229
                 } else {
231 230
                     $this->_resVector = bitset_intersection(
@@ -236,14 +235,14 @@  discard block
 block discarded – undo
236 235
                 $this->_termsPositions[$termId] = $reader->termPositions($term);
237 236
             }
238 237
         } else {
239
-            foreach( $this->_terms as $termId=>$term ) {
240
-                if($this->_resVector === null) {
238
+            foreach ($this->_terms as $termId=>$term) {
239
+                if ($this->_resVector === null) {
241 240
                     $this->_resVector = array_flip($reader->termDocs($term));
242 241
                 } else {
243 242
                     $termDocs = array_flip($reader->termDocs($term));
244
-                    foreach($this->_resVector as $key=>$value) {
245
-                        if (!isset( $termDocs[$key] )) {
246
-                            unset( $this->_resVector[$key] );
243
+                    foreach ($this->_resVector as $key=>$value) {
244
+                        if (!isset($termDocs[$key])) {
245
+                            unset($this->_resVector[$key]);
247 246
                         }
248 247
                     }
249 248
                 }
@@ -271,7 +270,7 @@  discard block
 block discarded – undo
271 270
         foreach ($this->_terms as $termId => $term) {
272 271
             if ($lowCardTermId === null ||
273 272
                 count($this->_termsPositions[$termId][$docId]) <
274
-                count($this->_termsPositions[$lowCardTermId][$docId]) ) {
273
+                count($this->_termsPositions[$lowCardTermId][$docId])) {
275 274
                     $lowCardTermId = $termId;
276 275
                 }
277 276
         }
@@ -289,7 +288,7 @@  discard block
 block discarded – undo
289 288
                                              $this->_offsets[$lowCardTermId]);
290 289
 
291 290
                     if (!in_array($expectedPosition, $this->_termsPositions[$termId][$docId])) {
292
-                        $freq--;  // Phrase wasn't found.
291
+                        $freq--; // Phrase wasn't found.
293 292
                         break;
294 293
                     }
295 294
                 }
@@ -321,7 +320,7 @@  discard block
 block discarded – undo
321 320
 
322 321
             // Walk through the term positions.
323 322
             // Each term position produces a set of phrases.
324
-            foreach ($this->_termsPositions[$termId][$docId] as $termPosition ) {
323
+            foreach ($this->_termsPositions[$termId][$docId] as $termPosition) {
325 324
                 if ($firstPass) {
326 325
                     for ($count = 0; $count < $queueSize; $count++) {
327 326
                         $phraseQueue[$count][$termId] = $termPosition;
@@ -329,7 +328,7 @@  discard block
 block discarded – undo
329 328
                 } else {
330 329
                     for ($count = 0; $count < $queueSize; $count++) {
331 330
                         if ($lastTerm !== null &&
332
-                            abs( $termPosition - $phraseQueue[$count][$lastTerm] -
331
+                            abs($termPosition - $phraseQueue[$count][$lastTerm] -
333 332
                                  ($this->_offsets[$termId] - $this->_offsets[$lastTerm])) > $this->_slop) {
334 333
                             continue;
335 334
                         }
@@ -357,7 +356,7 @@  discard block
 block discarded – undo
357 356
                 foreach ($this->_terms as $termId => $term) {
358 357
                     $distance += abs($phrasePos[$termId] - $this->_offsets[$termId] - $start);
359 358
 
360
-                    if($distance > $this->_slop) {
359
+                    if ($distance > $this->_slop) {
361 360
                         break;
362 361
                     }
363 362
                 }
@@ -390,14 +389,13 @@  discard block
 block discarded – undo
390 389
             return 0;
391 390
         }
392 391
 
393
-        if($this->_resVector === null) {
392
+        if ($this->_resVector === null) {
394 393
             $this->_calculateResult($reader);
395 394
             $this->_initWeight($reader);
396 395
         }
397 396
 
398
-        if ( (extension_loaded('bitset')) ?
399
-                bitset_in($this->_resVector, $docId) :
400
-                isset($this->_resVector[$docId])  ) {
397
+        if ((extension_loaded('bitset')) ?
398
+                bitset_in($this->_resVector, $docId) : isset($this->_resVector[$docId])) {
401 399
             if ($this->_slop == 0) {
402 400
                 $freq = $this->_exactPhraseFreq($docId);
403 401
             } else {
@@ -414,7 +412,7 @@  discard block
 block discarded – undo
414 412
                 $weight = $this->_weight->getValue();
415 413
                 $norm = $reader->norm($docId, reset($this->_terms)->field);
416 414
 
417
-                return $tf*$weight*$norm;
415
+                return $tf * $weight * $norm;
418 416
             }
419 417
         } else {
420 418
             return 0;
Please login to merge, or discard this patch.
demos/quickstart/protected/index/Zend/Search/Lucene/Search/QueryToken.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -74,7 +74,7 @@
 block discarded – undo
74 74
      * IndexReader constructor needs token type and token text as a parameters.
75 75
      *
76 76
      * @param $tokType integer
77
-     * @param $tokText string
77
+     * @param string $tokText string
78 78
      */
79 79
     public function __construct($tokType, $tokText)
80 80
     {
Please login to merge, or discard this patch.
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -31,72 +31,72 @@
 block discarded – undo
31 31
  */
32 32
 class Zend_Search_Lucene_Search_QueryToken
33 33
 {
34
-    /**
35
-     * Token type Word.
36
-     */
37
-    const TOKTYPE_WORD = 0;
34
+	/**
35
+	 * Token type Word.
36
+	 */
37
+	const TOKTYPE_WORD = 0;
38 38
 
39
-    /**
40
-     * Token type Field.
41
-     * Field indicator in 'field:word' pair
42
-     */
43
-    const TOKTYPE_FIELD = 1;
39
+	/**
40
+	 * Token type Field.
41
+	 * Field indicator in 'field:word' pair
42
+	 */
43
+	const TOKTYPE_FIELD = 1;
44 44
 
45
-    /**
46
-     * Token type Sign.
47
-     * '+' (required) or '-' (absentee) sign
48
-     */
49
-    const TOKTYPE_SIGN = 2;
45
+	/**
46
+	 * Token type Sign.
47
+	 * '+' (required) or '-' (absentee) sign
48
+	 */
49
+	const TOKTYPE_SIGN = 2;
50 50
 
51
-    /**
52
-     * Token type Bracket.
53
-     * '(' or ')'
54
-     */
55
-    const TOKTYPE_BRACKET = 3;
51
+	/**
52
+	 * Token type Bracket.
53
+	 * '(' or ')'
54
+	 */
55
+	const TOKTYPE_BRACKET = 3;
56 56
 
57 57
 
58
-    /**
59
-     * Token type.
60
-     *
61
-     * @var integer
62
-     */
63
-    public $type;
58
+	/**
59
+	 * Token type.
60
+	 *
61
+	 * @var integer
62
+	 */
63
+	public $type;
64 64
 
65
-    /**
66
-     * Token text.
67
-     *
68
-     * @var integer
69
-     */
70
-    public $text;
65
+	/**
66
+	 * Token text.
67
+	 *
68
+	 * @var integer
69
+	 */
70
+	public $text;
71 71
 
72 72
 
73
-    /**
74
-     * IndexReader constructor needs token type and token text as a parameters.
75
-     *
76
-     * @param $tokType integer
77
-     * @param $tokText string
78
-     */
79
-    public function __construct($tokType, $tokText)
80
-    {
81
-        switch ($tokType) {
82
-            case self::TOKTYPE_BRACKET:
83
-                // fall through to the next case
84
-            case self::TOKTYPE_FIELD:
85
-                // fall through to the next case
86
-            case self::TOKTYPE_SIGN:
87
-                // fall through to the next case
88
-            case self::TOKTYPE_WORD:
89
-                break;
90
-            default:
91
-                throw new Zend_Search_Lucene_Exception("Unrecognized token type \"$tokType\".");
92
-        }
73
+	/**
74
+	 * IndexReader constructor needs token type and token text as a parameters.
75
+	 *
76
+	 * @param $tokType integer
77
+	 * @param $tokText string
78
+	 */
79
+	public function __construct($tokType, $tokText)
80
+	{
81
+		switch ($tokType) {
82
+			case self::TOKTYPE_BRACKET:
83
+				// fall through to the next case
84
+			case self::TOKTYPE_FIELD:
85
+				// fall through to the next case
86
+			case self::TOKTYPE_SIGN:
87
+				// fall through to the next case
88
+			case self::TOKTYPE_WORD:
89
+				break;
90
+			default:
91
+				throw new Zend_Search_Lucene_Exception("Unrecognized token type \"$tokType\".");
92
+		}
93 93
 
94
-        if (!strlen($tokText)) {
95
-            throw new Zend_Search_Lucene_Exception('Token text must be supplied.');
96
-        }
94
+		if (!strlen($tokText)) {
95
+			throw new Zend_Search_Lucene_Exception('Token text must be supplied.');
96
+		}
97 97
 
98
-        $this->type = $tokType;
99
-        $this->text = $tokText;
100
-    }
98
+		$this->type = $tokType;
99
+		$this->text = $tokText;
100
+	}
101 101
 }
102 102
 
Please login to merge, or discard this patch.