| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Zend Framework 4 * 5 * LICENSE 6 * 7 * This source file is subject to the new BSD license that is bundled 8 * with this package in the file LICENSE.txt. 9 * It is also available through the world-wide-web at this URL: 10 * http://framework.zend.com/license/new-bsd 11 * If you did not receive a copy of the license and are unable to 12 * obtain it through the world-wide-web, please send an email 13 * to license@zend.com so we can send you a copy immediately. 14 * 15 * @category Zend 16 * @package Zend_Search_Lucene 17 * @subpackage Search 18 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) 19 * @license http://framework.zend.com/license/new-bsd New BSD License 20 */ 21 22 23 /** Zend_Search_Lucene_Search_Query */ 24 require_once 'Zend/Search/Lucene/Search/Query.php'; 25 26 /** Zend_Search_Lucene_Search_Query_MultiTerm */ 27 require_once 'Zend/Search/Lucene/Search/Query/MultiTerm.php'; 28 29 30 /** 31 * @category Zend 32 * @package Zend_Search_Lucene 33 * @subpackage Search 34 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) 35 * @license http://framework.zend.com/license/new-bsd New BSD License 36 */ 37 class Zend_Search_Lucene_Search_Query_Range extends Zend_Search_Lucene_Search_Query 38 { 39 /** 40 * Lower term. 41 * 42 * @var Zend_Search_Lucene_Index_Term 43 */ 44 private $_lowerTerm; 45 46 /** 47 * Upper term. 48 * 49 * @var Zend_Search_Lucene_Index_Term 50 */ 51 private $_upperTerm; 52 53 54 /** 55 * Search field 56 * 57 * @var string 58 */ 59 private $_field; 60 61 /** 62 * Inclusive 63 * 64 * @var boolean 65 */ 66 private $_inclusive; 67 68 /** 69 * Matched terms. 70 * 71 * Matched terms list. 72 * It's filled during the search (rewrite operation) and may be used for search result 73 * post-processing 74 * 75 * Array of Zend_Search_Lucene_Index_Term objects 76 * 77 * @var array 78 */ 79 private $_matches; 80 81 82 /** 83 * Zend_Search_Lucene_Search_Query_Range constructor. 84 * 85 * @param Zend_Search_Lucene_Index_Term|null $lowerTerm 86 * @param Zend_Search_Lucene_Index_Term|null $upperTerm 87 * @param boolean $inclusive 88 * @throws Zend_Search_Lucene_Exception 89 */ 90 public function __construct($lowerTerm, $upperTerm, $inclusive) 91 { 92 if ($lowerTerm === null && $upperTerm === null) { 93 throw new Zend_Search_Lucene_Exception('At least one term must be non-null'); 94 } 95 if ($lowerTerm !== null && $upperTerm !== null && $lowerTerm->field != $upperTerm->field) { 96 throw new Zend_Search_Lucene_Exception('Both terms must be for the same field'); 97 } 98 99 $this->_field = ($lowerTerm !== null)? $lowerTerm->field : $upperTerm->field; 100 $this->_lowerTerm = $lowerTerm; 101 $this->_upperTerm = $upperTerm; 102 $this->_inclusive = $inclusive; 103 } 104 105 /** 106 * Get query field name 107 * 108 * @return string|null 109 */ 110 public function getField() 111 { 112 return $this->_field; 113 } 114 115 /** 116 * Get lower term 117 * 118 * @return Zend_Search_Lucene_Index_Term|null 119 */ 120 public function getLowerTerm() 121 { 122 return $this->_lowerTerm; 123 } 124 125 /** 126 * Get upper term 127 * 128 * @return Zend_Search_Lucene_Index_Term|null 129 */ 130 public function getUpperTerm() 131 { 132 return $this->_upperTerm; 133 } 134 135 /** 136 * Get upper term 137 * 138 * @return boolean 139 */ 140 public function isInclusive() 141 { 142 return $this->_inclusive; 143 } 144 145 /** 146 * Re-write query into primitive queries in the context of specified index 147 * 148 * @param Zend_Search_Lucene_Interface $index 149 * @return Zend_Search_Lucene_Search_Query 150 */ 151 public function rewrite(Zend_Search_Lucene_Interface $index) 152 { 153 $this->_matches = array(); 154 155 if ($this->_field === null) { 156 // Search through all fields 157 $fields = $index->getFieldNames(true /* indexed fields list */); 158 } else { 159 $fields = array($this->_field); 160 } 161 162 foreach ($fields as $field) { 163 $index->resetTermsStream(); 164 165 if ($this->_lowerTerm !== null) { 166 $lowerTerm = new Zend_Search_Lucene_Index_Term($this->_lowerTerm->text, $field); 167 168 $index->skipTo($lowerTerm); 169 170 if (!$this->_inclusive && 171 $index->currentTerm() == $lowerTerm) { 172 // Skip lower term 173 $index->nextTerm(); 174 } 175 } else { 176 $index->skipTo(new Zend_Search_Lucene_Index_Term('', $field)); 177 } 178 179 180 if ($this->_upperTerm !== null) { 181 // Walk up to the upper term 182 $upperTerm = new Zend_Search_Lucene_Index_Term($this->_upperTerm->text, $field); 183 184 while ($index->currentTerm() !== null && 185 $index->currentTerm()->field == $field && 186 $index->currentTerm()->text < $upperTerm->text) { 187 $this->_matches[] = $index->currentTerm(); 188 $index->nextTerm(); 189 } 190 191 if ($this->_inclusive && $index->currentTerm() == $upperTerm) { 192 // Include upper term into result 193 $this->_matches[] = $upperTerm; 194 } 195 } else { 196 // Walk up to the end of field data 197 while ($index->currentTerm() !== null && $index->currentTerm()->field == $field) { 198 $this->_matches[] = $index->currentTerm(); 199 $index->nextTerm(); 200 } 201 } 202 203 $index->closeTermsStream(); 204 } 205 206 if (count($this->_matches) == 0) { 207 return new Zend_Search_Lucene_Search_Query_Empty(); 208 } else if (count($this->_matches) == 1) { 209 return new Zend_Search_Lucene_Search_Query_Term(reset($this->_matches)); 210 } else { 211 $rewrittenQuery = new Zend_Search_Lucene_Search_Query_MultiTerm(); 212 213 foreach ($this->_matches as $matchedTerm) { 214 $rewrittenQuery->addTerm($matchedTerm); 215 } 216 217 return $rewrittenQuery; 218 } 219 } 220 221 /** 222 * Optimize query in the context of specified index 223 * 224 * @param Zend_Search_Lucene_Interface $index 225 * @return Zend_Search_Lucene_Search_Query 226 */ 227 public function optimize(Zend_Search_Lucene_Interface $index) 228 { 229 throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)'); 230 } 231 232 /** 233 * Return query terms 234 * 235 * @return array 236 * @throws Zend_Search_Lucene_Exception 237 */ 238 public function getQueryTerms() 239 { 240 if ($this->_matches === null) { 241 throw new Zend_Search_Lucene_Exception('Search has to be performed first to get matched terms'); 242 } 243 244 return $this->_matches; 245 } 246 247 /** 248 * Constructs an appropriate Weight implementation for this query. 249 * 250 * @param Zend_Search_Lucene_Interface $reader 251 * @return Zend_Search_Lucene_Search_Weight 252 * @throws Zend_Search_Lucene_Exception 253 */ 254 public function createWeight(Zend_Search_Lucene_Interface $reader) 255 { 256 throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)'); 257 } 258 259 260 /** 261 * Execute query in context of index reader 262 * It also initializes necessary internal structures 263 * 264 * @param Zend_Search_Lucene_Interface $reader 265 * @throws Zend_Search_Lucene_Exception 266 */ 267 public function execute(Zend_Search_Lucene_Interface $reader) 268 { 269 throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)'); 270 } 271 272 /** 273 * Get document ids likely matching the query 274 * 275 * It's an array with document ids as keys (performance considerations) 276 * 277 * @return array 278 * @throws Zend_Search_Lucene_Exception 279 */ 280 public function matchedDocs() 281 { 282 throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)'); 283 } 284 285 /** 286 * Score specified document 287 * 288 * @param integer $docId 289 * @param Zend_Search_Lucene_Interface $reader 290 * @return float 291 * @throws Zend_Search_Lucene_Exception 292 */ 293 public function score($docId, Zend_Search_Lucene_Interface $reader) 294 { 295 throw new Zend_Search_Lucene_Exception('Range query should not be directly used for search. Use $query->rewrite($index)'); 296 } 297 298 /** 299 * Highlight query terms 300 * 301 * @param integer &$colorIndex 302 * @param Zend_Search_Lucene_Document_Html $doc 303 */ 304 public function highlightMatchesDOM(Zend_Search_Lucene_Document_Html $doc, &$colorIndex) 305 { 306 $words = array(); 307 308 foreach ($this->_matches as $term) { 309 $words[] = $term->text; 310 } 311 312 $doc->highlight($words, $this->_getHighlightColor($colorIndex)); 313 } 314 315 /** 316 * Print a query 317 * 318 * @return string 319 */ 320 public function __toString() 321 { 322 // It's used only for query visualisation, so we don't care about characters escaping 323 return (($this->_field === null)? '' : $this->_field . ':') 324 . (($this->_inclusive)? '[' : '{') 325 . (($this->_lowerTerm !== null)? $this->_lowerTerm->text : 'null') 326 . ' TO ' 327 . (($this->_upperTerm !== null)? $this->_upperTerm->text : 'null') 328 . (($this->_inclusive)? ']' : '}'); 329 } 330 } 331
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |