1<?php 2/** 3 * Functions to create the fulltext search index 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 * @author Tom N Harris <tnharris@whoopdedo.org> 8 */ 9 10if(!defined('DOKU_INC')) die('meh.'); 11 12// Version tag used to force rebuild on upgrade 13define('INDEXER_VERSION', 6); 14 15// set the minimum token length to use in the index (note, this doesn't apply to numeric tokens) 16if (!defined('IDX_MINWORDLENGTH')) define('IDX_MINWORDLENGTH',2); 17 18// Asian characters are handled as words. The following regexp defines the 19// Unicode-Ranges for Asian characters 20// Ranges taken from http://en.wikipedia.org/wiki/Unicode_block 21// I'm no language expert. If you think some ranges are wrongly chosen or 22// a range is missing, please contact me 23define('IDX_ASIAN1','[\x{0E00}-\x{0E7F}]'); // Thai 24define('IDX_ASIAN2','['. 25 '\x{2E80}-\x{3040}'. // CJK -> Hangul 26 '\x{309D}-\x{30A0}'. 27 '\x{30FD}-\x{31EF}\x{3200}-\x{D7AF}'. 28 '\x{F900}-\x{FAFF}'. // CJK Compatibility Ideographs 29 '\x{FE30}-\x{FE4F}'. // CJK Compatibility Forms 30 "\xF0\xA0\x80\x80-\xF0\xAA\x9B\x9F". // CJK Extension B 31 "\xF0\xAA\x9C\x80-\xF0\xAB\x9C\xBF". // CJK Extension C 32 "\xF0\xAB\x9D\x80-\xF0\xAB\xA0\x9F". // CJK Extension D 33 "\xF0\xAF\xA0\x80-\xF0\xAF\xAB\xBF". // CJK Compatibility Supplement 34 ']'); 35define('IDX_ASIAN3','['. // Hiragana/Katakana (can be two characters) 36 '\x{3042}\x{3044}\x{3046}\x{3048}'. 37 '\x{304A}-\x{3062}\x{3064}-\x{3082}'. 38 '\x{3084}\x{3086}\x{3088}-\x{308D}'. 39 '\x{308F}-\x{3094}'. 40 '\x{30A2}\x{30A4}\x{30A6}\x{30A8}'. 41 '\x{30AA}-\x{30C2}\x{30C4}-\x{30E2}'. 42 '\x{30E4}\x{30E6}\x{30E8}-\x{30ED}'. 43 '\x{30EF}-\x{30F4}\x{30F7}-\x{30FA}'. 44 ']['. 45 '\x{3041}\x{3043}\x{3045}\x{3047}\x{3049}'. 46 '\x{3063}\x{3083}\x{3085}\x{3087}\x{308E}\x{3095}-\x{309C}'. 47 '\x{30A1}\x{30A3}\x{30A5}\x{30A7}\x{30A9}'. 48 '\x{30C3}\x{30E3}\x{30E5}\x{30E7}\x{30EE}\x{30F5}\x{30F6}\x{30FB}\x{30FC}'. 49 '\x{31F0}-\x{31FF}'. 50 ']?'); 51define('IDX_ASIAN', '(?:'.IDX_ASIAN1.'|'.IDX_ASIAN2.'|'.IDX_ASIAN3.')'); 52 53/** 54 * Version of the indexer taking into consideration the external tokenizer. 55 * The indexer is only compatible with data written by the same version. 56 * 57 * @triggers INDEXER_VERSION_GET 58 * Plugins that modify what gets indexed should hook this event and 59 * add their version info to the event data like so: 60 * $data[$plugin_name] = $plugin_version; 61 * 62 * @author Tom N Harris <tnharris@whoopdedo.org> 63 * @author Michael Hamann <michael@content-space.de> 64 */ 65function idx_get_version(){ 66 static $indexer_version = null; 67 if ($indexer_version == null) { 68 $version = INDEXER_VERSION; 69 70 // DokuWiki version is included for the convenience of plugins 71 $data = array('dokuwiki'=>$version); 72 trigger_event('INDEXER_VERSION_GET', $data, null, false); 73 unset($data['dokuwiki']); // this needs to be first 74 ksort($data); 75 foreach ($data as $plugin=>$vers) 76 $version .= '+'.$plugin.'='.$vers; 77 $indexer_version = $version; 78 } 79 return $indexer_version; 80} 81 82/** 83 * Measure the length of a string. 84 * Differs from strlen in handling of asian characters. 85 * 86 * @author Tom N Harris <tnharris@whoopdedo.org> 87 */ 88function wordlen($w){ 89 $l = strlen($w); 90 // If left alone, all chinese "words" will get put into w3.idx 91 // So the "length" of a "word" is faked 92 if(preg_match_all('/[\xE2-\xEF]/',$w,$leadbytes)) { 93 foreach($leadbytes[0] as $b) 94 $l += ord($b) - 0xE1; 95 } 96 return $l; 97} 98 99/** 100 * Class that encapsulates operations on the indexer database. 101 * 102 * @author Tom N Harris <tnharris@whoopdedo.org> 103 */ 104class Doku_Indexer { 105 /** 106 * @var array $pidCache Cache for getPID() 107 */ 108 protected $pidCache = array(); 109 110 /** 111 * Adds the contents of a page to the fulltext index 112 * 113 * The added text replaces previous words for the same page. 114 * An empty value erases the page. 115 * 116 * @param string $page a page name 117 * @param string $text the body of the page 118 * @return boolean the function completed successfully 119 * @author Tom N Harris <tnharris@whoopdedo.org> 120 * @author Andreas Gohr <andi@splitbrain.org> 121 */ 122 public function addPageWords($page, $text) { 123 if (!$this->lock()) 124 return "locked"; 125 126 // load known documents 127 $pid = $this->getPIDNoLock($page); 128 if ($pid === false) { 129 $this->unlock(); 130 return false; 131 } 132 133 $pagewords = array(); 134 // get word usage in page 135 $words = $this->getPageWords($text); 136 if ($words === false) { 137 $this->unlock(); 138 return false; 139 } 140 141 if (!empty($words)) { 142 foreach (array_keys($words) as $wlen) { 143 $index = $this->getIndex('i', $wlen); 144 foreach ($words[$wlen] as $wid => $freq) { 145 $idx = ($wid<count($index)) ? $index[$wid] : ''; 146 $index[$wid] = $this->updateTuple($idx, $pid, $freq); 147 $pagewords[] = "$wlen*$wid"; 148 } 149 if (!$this->saveIndex('i', $wlen, $index)) { 150 $this->unlock(); 151 return false; 152 } 153 } 154 } 155 156 // Remove obsolete index entries 157 $pageword_idx = $this->getIndexKey('pageword', '', $pid); 158 if ($pageword_idx !== '') { 159 $oldwords = explode(':',$pageword_idx); 160 $delwords = array_diff($oldwords, $pagewords); 161 $upwords = array(); 162 foreach ($delwords as $word) { 163 if ($word != '') { 164 list($wlen,$wid) = explode('*', $word); 165 $wid = (int)$wid; 166 $upwords[$wlen][] = $wid; 167 } 168 } 169 foreach ($upwords as $wlen => $widx) { 170 $index = $this->getIndex('i', $wlen); 171 foreach ($widx as $wid) { 172 $index[$wid] = $this->updateTuple($index[$wid], $pid, 0); 173 } 174 $this->saveIndex('i', $wlen, $index); 175 } 176 } 177 // Save the reverse index 178 $pageword_idx = join(':', $pagewords); 179 if (!$this->saveIndexKey('pageword', '', $pid, $pageword_idx)) { 180 $this->unlock(); 181 return false; 182 } 183 184 $this->unlock(); 185 return true; 186 } 187 188 /** 189 * Split the words in a page and add them to the index. 190 * 191 * @param string $text content of the page 192 * @return array list of word IDs and number of times used 193 * @author Andreas Gohr <andi@splitbrain.org> 194 * @author Christopher Smith <chris@jalakai.co.uk> 195 * @author Tom N Harris <tnharris@whoopdedo.org> 196 */ 197 protected function getPageWords($text) { 198 199 $tokens = $this->tokenizer($text); 200 $tokens = array_count_values($tokens); // count the frequency of each token 201 202 $words = array(); 203 foreach ($tokens as $w=>$c) { 204 $l = wordlen($w); 205 if (isset($words[$l])){ 206 $words[$l][$w] = $c + (isset($words[$l][$w]) ? $words[$l][$w] : 0); 207 }else{ 208 $words[$l] = array($w => $c); 209 } 210 } 211 212 // arrive here with $words = array(wordlen => array(word => frequency)) 213 $word_idx_modified = false; 214 $index = array(); //resulting index 215 foreach (array_keys($words) as $wlen) { 216 $word_idx = $this->getIndex('w', $wlen); 217 foreach ($words[$wlen] as $word => $freq) { 218 $wid = array_search($word, $word_idx); 219 if ($wid === false) { 220 $wid = count($word_idx); 221 $word_idx[] = $word; 222 $word_idx_modified = true; 223 } 224 if (!isset($index[$wlen])) 225 $index[$wlen] = array(); 226 $index[$wlen][$wid] = $freq; 227 } 228 // save back the word index 229 if ($word_idx_modified && !$this->saveIndex('w', $wlen, $word_idx)) 230 return false; 231 } 232 233 return $index; 234 } 235 236 /** 237 * Add/update keys to/of the metadata index. 238 * 239 * Adding new keys does not remove other keys for the page. 240 * An empty value will erase the key. 241 * The $key parameter can be an array to add multiple keys. $value will 242 * not be used if $key is an array. 243 * 244 * @param string $page a page name 245 * @param mixed $key a key string or array of key=>value pairs 246 * @param mixed $value the value or list of values 247 * @return boolean the function completed successfully 248 * @author Tom N Harris <tnharris@whoopdedo.org> 249 * @author Michael Hamann <michael@content-space.de> 250 */ 251 public function addMetaKeys($page, $key, $value=null) { 252 if (!is_array($key)) { 253 $key = array($key => $value); 254 } elseif (!is_null($value)) { 255 // $key is array, but $value is not null 256 trigger_error("array passed to addMetaKeys but value is not null", E_USER_WARNING); 257 } 258 259 if (!$this->lock()) 260 return "locked"; 261 262 // load known documents 263 $pid = $this->getPIDNoLock($page); 264 if ($pid === false) { 265 $this->unlock(); 266 return false; 267 } 268 269 // Special handling for titles so the index file is simpler 270 if (array_key_exists('title', $key)) { 271 $value = $key['title']; 272 if (is_array($value)) 273 $value = $value[0]; 274 $this->saveIndexKey('title', '', $pid, $value); 275 unset($key['title']); 276 } 277 278 foreach ($key as $name => $values) { 279 $metaname = idx_cleanName($name); 280 $this->addIndexKey('metadata', '', $metaname); 281 $metaidx = $this->getIndex($metaname.'_i', ''); 282 $metawords = $this->getIndex($metaname.'_w', ''); 283 $addwords = false; 284 285 if (!is_array($values)) $values = array($values); 286 287 $val_idx = $this->getIndexKey($metaname.'_p', '', $pid); 288 if ($val_idx != '') { 289 $val_idx = explode(':', $val_idx); 290 // -1 means remove, 0 keep, 1 add 291 $val_idx = array_combine($val_idx, array_fill(0, count($val_idx), -1)); 292 } else { 293 $val_idx = array(); 294 } 295 296 foreach ($values as $val) { 297 $val = (string)$val; 298 if ($val !== "") { 299 $id = array_search($val, $metawords); 300 if ($id === false) { 301 $id = count($metawords); 302 $metawords[$id] = $val; 303 $addwords = true; 304 } 305 // test if value is already in the index 306 if (isset($val_idx[$id]) && $val_idx[$id] <= 0) 307 $val_idx[$id] = 0; 308 else // else add it 309 $val_idx[$id] = 1; 310 } 311 } 312 313 if ($addwords) 314 $this->saveIndex($metaname.'_w', '', $metawords); 315 $vals_changed = false; 316 foreach ($val_idx as $id => $action) { 317 if ($action == -1) { 318 $metaidx[$id] = $this->updateTuple($metaidx[$id], $pid, 0); 319 $vals_changed = true; 320 unset($val_idx[$id]); 321 } elseif ($action == 1) { 322 $metaidx[$id] = $this->updateTuple($metaidx[$id], $pid, 1); 323 $vals_changed = true; 324 } 325 } 326 327 if ($vals_changed) { 328 $this->saveIndex($metaname.'_i', '', $metaidx); 329 $val_idx = implode(':', array_keys($val_idx)); 330 $this->saveIndexKey($metaname.'_p', '', $pid, $val_idx); 331 } 332 333 unset($metaidx); 334 unset($metawords); 335 } 336 337 $this->unlock(); 338 return true; 339 } 340 341 /** 342 * Rename a page in the search index without changing the indexed content. This function doesn't check if the 343 * old or new name exists in the filesystem. It returns an error if the old page isn't in the page list of the 344 * indexer and it deletes all previously indexed content of the new page. 345 * 346 * @param string $oldpage The old page name 347 * @param string $newpage The new page name 348 * @return string|bool If the page was successfully renamed, can be a message in the case of an error 349 */ 350 public function renamePage($oldpage, $newpage) { 351 if (!$this->lock()) return 'locked'; 352 353 $pages = $this->getPages(); 354 355 $id = array_search($oldpage, $pages); 356 if ($id === false) { 357 $this->unlock(); 358 return 'page is not in index'; 359 } 360 361 $new_id = array_search($newpage, $pages); 362 if ($new_id !== false) { 363 // make sure the page is not in the index anymore 364 if ($this->deletePageNoLock($newpage) !== true) { 365 return false; 366 } 367 368 $pages[$new_id] = 'deleted:'.time().rand(0, 9999); 369 } 370 371 $pages[$id] = $newpage; 372 373 // update index 374 if (!$this->saveIndex('page', '', $pages)) { 375 $this->unlock(); 376 return false; 377 } 378 379 // reset the pid cache 380 $this->pidCache = array(); 381 382 $this->unlock(); 383 return true; 384 } 385 386 /** 387 * Renames a meta value in the index. This doesn't change the meta value in the pages, it assumes that all pages 388 * will be updated. 389 * 390 * @param string $key The metadata key of which a value shall be changed 391 * @param string $oldvalue The old value that shall be renamed 392 * @param string $newvalue The new value to which the old value shall be renamed, can exist (then values will be merged) 393 * @return bool|string If renaming the value has been successful, false or error message on error. 394 */ 395 public function renameMetaValue($key, $oldvalue, $newvalue) { 396 if (!$this->lock()) return 'locked'; 397 398 // change the relation references index 399 $metavalues = $this->getIndex($key, '_w'); 400 $oldid = array_search($oldvalue, $metavalues); 401 if ($oldid !== false) { 402 $newid = array_search($newvalue, $metavalues); 403 if ($newid !== false) { 404 // free memory 405 unset ($metavalues); 406 407 // okay, now we have two entries for the same value. we need to merge them. 408 $indexline = $this->getIndexKey($key.'_i', '', $oldid); 409 if ($indexline != '') { 410 $newindexline = $this->getIndexKey($key.'_i', '', $newid); 411 $pagekeys = $this->getIndex($key.'_p', ''); 412 $parts = explode(':', $indexline); 413 foreach ($parts as $part) { 414 list($id, $count) = explode('*', $part); 415 $newindexline = $this->updateTuple($newindexline, $id, $count); 416 417 $keyline = explode(':', $pagekeys[$id]); 418 // remove old meta value 419 $keyline = array_diff($keyline, array($oldid)); 420 // add new meta value when not already present 421 if (!in_array($newid, $keyline)) { 422 array_push($keyline, $newid); 423 } 424 $pagekeys[$id] = implode(':', $keyline); 425 } 426 $this->saveIndex($key.'_p', '', $pagekeys); 427 unset($pagekeys); 428 $this->saveIndexKey($key.'_i', '', $oldid, ''); 429 $this->saveIndexKey($key.'_i', '', $newid, $newindexline); 430 } 431 } else { 432 $metavalues[$oldid] = $newvalue; 433 if (!$this->saveIndex($key.'_w', '', $metavalues)) { 434 $this->unlock(); 435 return false; 436 } 437 } 438 } 439 440 $this->unlock(); 441 return true; 442 } 443 444 /** 445 * Remove a page from the index 446 * 447 * Erases entries in all known indexes. 448 * 449 * @param string $page a page name 450 * @return boolean the function completed successfully 451 * @author Tom N Harris <tnharris@whoopdedo.org> 452 */ 453 public function deletePage($page) { 454 if (!$this->lock()) 455 return "locked"; 456 457 $result = $this->deletePageNoLock($page); 458 459 $this->unlock(); 460 461 return $result; 462 } 463 464 /** 465 * Remove a page from the index without locking the index, only use this function if the index is already locked 466 * 467 * Erases entries in all known indexes. 468 * 469 * @param string $page a page name 470 * @return boolean the function completed successfully 471 * @author Tom N Harris <tnharris@whoopdedo.org> 472 */ 473 protected function deletePageNoLock($page) { 474 // load known documents 475 $pid = $this->getPIDNoLock($page); 476 if ($pid === false) { 477 return false; 478 } 479 480 // Remove obsolete index entries 481 $pageword_idx = $this->getIndexKey('pageword', '', $pid); 482 if ($pageword_idx !== '') { 483 $delwords = explode(':',$pageword_idx); 484 $upwords = array(); 485 foreach ($delwords as $word) { 486 if ($word != '') { 487 list($wlen,$wid) = explode('*', $word); 488 $wid = (int)$wid; 489 $upwords[$wlen][] = $wid; 490 } 491 } 492 foreach ($upwords as $wlen => $widx) { 493 $index = $this->getIndex('i', $wlen); 494 foreach ($widx as $wid) { 495 $index[$wid] = $this->updateTuple($index[$wid], $pid, 0); 496 } 497 $this->saveIndex('i', $wlen, $index); 498 } 499 } 500 // Save the reverse index 501 if (!$this->saveIndexKey('pageword', '', $pid, "")) { 502 return false; 503 } 504 505 $this->saveIndexKey('title', '', $pid, ""); 506 $keyidx = $this->getIndex('metadata', ''); 507 foreach ($keyidx as $metaname) { 508 $val_idx = explode(':', $this->getIndexKey($metaname.'_p', '', $pid)); 509 $meta_idx = $this->getIndex($metaname.'_i', ''); 510 foreach ($val_idx as $id) { 511 if ($id === '') continue; 512 $meta_idx[$id] = $this->updateTuple($meta_idx[$id], $pid, 0); 513 } 514 $this->saveIndex($metaname.'_i', '', $meta_idx); 515 $this->saveIndexKey($metaname.'_p', '', $pid, ''); 516 } 517 518 return true; 519 } 520 521 /** 522 * Clear the whole index 523 * 524 * @return bool If the index has been cleared successfully 525 */ 526 public function clear() { 527 global $conf; 528 529 if (!$this->lock()) return false; 530 531 @unlink($conf['indexdir'].'/page.idx'); 532 @unlink($conf['indexdir'].'/title.idx'); 533 @unlink($conf['indexdir'].'/pageword.idx'); 534 @unlink($conf['indexdir'].'/metadata.idx'); 535 $dir = @opendir($conf['indexdir']); 536 if($dir!==false){ 537 while(($f = readdir($dir)) !== false){ 538 if(substr($f,-4)=='.idx' && 539 (substr($f,0,1)=='i' || substr($f,0,1)=='w' 540 || substr($f,-6)=='_w.idx' || substr($f,-6)=='_i.idx' || substr($f,-6)=='_p.idx')) 541 @unlink($conf['indexdir']."/$f"); 542 } 543 } 544 @unlink($conf['indexdir'].'/lengths.idx'); 545 546 // clear the pid cache 547 $this->pidCache = array(); 548 549 $this->unlock(); 550 return true; 551 } 552 553 /** 554 * Split the text into words for fulltext search 555 * 556 * TODO: does this also need &$stopwords ? 557 * 558 * @triggers INDEXER_TEXT_PREPARE 559 * This event allows plugins to modify the text before it gets tokenized. 560 * Plugins intercepting this event should also intercept INDEX_VERSION_GET 561 * 562 * @param string $text plain text 563 * @param boolean $wc are wildcards allowed? 564 * @return array list of words in the text 565 * @author Tom N Harris <tnharris@whoopdedo.org> 566 * @author Andreas Gohr <andi@splitbrain.org> 567 */ 568 public function tokenizer($text, $wc=false) { 569 $wc = ($wc) ? '' : '\*'; 570 $stopwords =& idx_get_stopwords(); 571 572 // prepare the text to be tokenized 573 $evt = new Doku_Event('INDEXER_TEXT_PREPARE', $text); 574 if ($evt->advise_before(true)) { 575 if (preg_match('/[^0-9A-Za-z ]/u', $text)) { 576 // handle asian chars as single words (may fail on older PHP version) 577 $asia = @preg_replace('/('.IDX_ASIAN.')/u', ' \1 ', $text); 578 if (!is_null($asia)) $text = $asia; // recover from regexp falure 579 } 580 } 581 $evt->advise_after(); 582 unset($evt); 583 584 $text = strtr($text, 585 array( 586 "\r" => ' ', 587 "\n" => ' ', 588 "\t" => ' ', 589 "\xC2\xAD" => '', //soft-hyphen 590 ) 591 ); 592 if (preg_match('/[^0-9A-Za-z ]/u', $text)) 593 $text = utf8_stripspecials($text, ' ', '\._\-:'.$wc); 594 595 $wordlist = explode(' ', $text); 596 foreach ($wordlist as $i => $word) { 597 $wordlist[$i] = (preg_match('/[^0-9A-Za-z]/u', $word)) ? 598 utf8_strtolower($word) : strtolower($word); 599 } 600 601 foreach ($wordlist as $i => $word) { 602 if ((!is_numeric($word) && strlen($word) < IDX_MINWORDLENGTH) 603 || array_search($word, $stopwords) !== false) 604 unset($wordlist[$i]); 605 } 606 return array_values($wordlist); 607 } 608 609 /** 610 * Get the numeric PID of a page 611 * 612 * @param string $page The page to get the PID for 613 * @return bool|int The page id on success, false on error 614 */ 615 public function getPID($page) { 616 // return PID without locking when it is in the cache 617 if (isset($this->pidCache[$page])) return $this->pidCache[$page]; 618 619 if (!$this->lock()) 620 return false; 621 622 // load known documents 623 $pid = $this->getPIDNoLock($page); 624 if ($pid === false) { 625 $this->unlock(); 626 return false; 627 } 628 629 $this->unlock(); 630 return $pid; 631 } 632 633 /** 634 * Get the numeric PID of a page without locking the index. 635 * Only use this function when the index is already locked. 636 * 637 * @param string $page The page to get the PID for 638 * @return bool|int The page id on success, false on error 639 */ 640 protected function getPIDNoLock($page) { 641 // avoid expensive addIndexKey operation for the most recently requested pages by using a cache 642 if (isset($this->pidCache[$page])) return $this->pidCache[$page]; 643 $pid = $this->addIndexKey('page', '', $page); 644 // limit cache to 10 entries by discarding the oldest element as in DokuWiki usually only the most recently 645 // added item will be requested again 646 if (count($this->pidCache) > 10) array_shift($this->pidCache); 647 $this->pidCache[$page] = $pid; 648 return $pid; 649 } 650 651 /** 652 * Get the page id of a numeric PID 653 * 654 * @param int $pid The PID to get the page id for 655 * @return string The page id 656 */ 657 public function getPageFromPID($pid) { 658 return $this->getIndexKey('page', '', $pid); 659 } 660 661 /** 662 * Find pages in the fulltext index containing the words, 663 * 664 * The search words must be pre-tokenized, meaning only letters and 665 * numbers with an optional wildcard 666 * 667 * The returned array will have the original tokens as key. The values 668 * in the returned list is an array with the page names as keys and the 669 * number of times that token appears on the page as value. 670 * 671 * @param array $tokens list of words to search for 672 * @return array list of page names with usage counts 673 * @author Tom N Harris <tnharris@whoopdedo.org> 674 * @author Andreas Gohr <andi@splitbrain.org> 675 */ 676 public function lookup(&$tokens) { 677 $result = array(); 678 $wids = $this->getIndexWords($tokens, $result); 679 if (empty($wids)) return array(); 680 // load known words and documents 681 $page_idx = $this->getIndex('page', ''); 682 $docs = array(); 683 foreach (array_keys($wids) as $wlen) { 684 $wids[$wlen] = array_unique($wids[$wlen]); 685 $index = $this->getIndex('i', $wlen); 686 foreach($wids[$wlen] as $ixid) { 687 if ($ixid < count($index)) 688 $docs["$wlen*$ixid"] = $this->parseTuples($page_idx, $index[$ixid]); 689 } 690 } 691 // merge found pages into final result array 692 $final = array(); 693 foreach ($result as $word => $res) { 694 $final[$word] = array(); 695 foreach ($res as $wid) { 696 // handle the case when ($ixid < count($index)) has been false 697 // and thus $docs[$wid] hasn't been set. 698 if (!isset($docs[$wid])) continue; 699 $hits = &$docs[$wid]; 700 foreach ($hits as $hitkey => $hitcnt) { 701 // make sure the document still exists 702 if (!page_exists($hitkey, '', false)) continue; 703 if (!isset($final[$word][$hitkey])) 704 $final[$word][$hitkey] = $hitcnt; 705 else 706 $final[$word][$hitkey] += $hitcnt; 707 } 708 } 709 } 710 return $final; 711 } 712 713 /** 714 * Find pages containing a metadata key. 715 * 716 * The metadata values are compared as case-sensitive strings. Pass a 717 * callback function that returns true or false to use a different 718 * comparison function. The function will be called with the $value being 719 * searched for as the first argument, and the word in the index as the 720 * second argument. The function preg_match can be used directly if the 721 * values are regexes. 722 * 723 * @param string $key name of the metadata key to look for 724 * @param string $value search term to look for, must be a string or array of strings 725 * @param callback $func comparison function 726 * @return array lists with page names, keys are query values if $value is array 727 * @author Tom N Harris <tnharris@whoopdedo.org> 728 * @author Michael Hamann <michael@content-space.de> 729 */ 730 public function lookupKey($key, &$value, $func=null) { 731 if (!is_array($value)) 732 $value_array = array($value); 733 else 734 $value_array =& $value; 735 736 // the matching ids for the provided value(s) 737 $value_ids = array(); 738 739 $metaname = idx_cleanName($key); 740 741 // get all words in order to search the matching ids 742 if ($key == 'title') { 743 $words = $this->getIndex('title', ''); 744 } else { 745 $words = $this->getIndex($metaname.'_w', ''); 746 } 747 748 if (!is_null($func)) { 749 foreach ($value_array as $val) { 750 foreach ($words as $i => $word) { 751 if (call_user_func_array($func, array($val, $word))) 752 $value_ids[$i][] = $val; 753 } 754 } 755 } else { 756 foreach ($value_array as $val) { 757 $xval = $val; 758 $caret = '^'; 759 $dollar = '$'; 760 // check for wildcards 761 if (substr($xval, 0, 1) == '*') { 762 $xval = substr($xval, 1); 763 $caret = ''; 764 } 765 if (substr($xval, -1, 1) == '*') { 766 $xval = substr($xval, 0, -1); 767 $dollar = ''; 768 } 769 if (!$caret || !$dollar) { 770 $re = $caret.preg_quote($xval, '/').$dollar; 771 foreach(array_keys(preg_grep('/'.$re.'/', $words)) as $i) 772 $value_ids[$i][] = $val; 773 } else { 774 if (($i = array_search($val, $words)) !== false) 775 $value_ids[$i][] = $val; 776 } 777 } 778 } 779 780 unset($words); // free the used memory 781 782 // initialize the result so it won't be null 783 $result = array(); 784 foreach ($value_array as $val) { 785 $result[$val] = array(); 786 } 787 788 $page_idx = $this->getIndex('page', ''); 789 790 // Special handling for titles 791 if ($key == 'title') { 792 foreach ($value_ids as $pid => $val_list) { 793 $page = $page_idx[$pid]; 794 foreach ($val_list as $val) { 795 $result[$val][] = $page; 796 } 797 } 798 } else { 799 // load all lines and pages so the used lines can be taken and matched with the pages 800 $lines = $this->getIndex($metaname.'_i', ''); 801 802 foreach ($value_ids as $value_id => $val_list) { 803 // parse the tuples of the form page_id*1:page2_id*1 and so on, return value 804 // is an array with page_id => 1, page2_id => 1 etc. so take the keys only 805 $pages = array_keys($this->parseTuples($page_idx, $lines[$value_id])); 806 foreach ($val_list as $val) { 807 $result[$val] = array_merge($result[$val], $pages); 808 } 809 } 810 } 811 if (!is_array($value)) $result = $result[$value]; 812 return $result; 813 } 814 815 /** 816 * Find the index ID of each search term. 817 * 818 * The query terms should only contain valid characters, with a '*' at 819 * either the beginning or end of the word (or both). 820 * The $result parameter can be used to merge the index locations with 821 * the appropriate query term. 822 * 823 * @param array $words The query terms. 824 * @param array $result Set to word => array("length*id" ...) 825 * @return array Set to length => array(id ...) 826 * @author Tom N Harris <tnharris@whoopdedo.org> 827 */ 828 protected function getIndexWords(&$words, &$result) { 829 $tokens = array(); 830 $tokenlength = array(); 831 $tokenwild = array(); 832 foreach ($words as $word) { 833 $result[$word] = array(); 834 $caret = '^'; 835 $dollar = '$'; 836 $xword = $word; 837 $wlen = wordlen($word); 838 839 // check for wildcards 840 if (substr($xword, 0, 1) == '*') { 841 $xword = substr($xword, 1); 842 $caret = ''; 843 $wlen -= 1; 844 } 845 if (substr($xword, -1, 1) == '*') { 846 $xword = substr($xword, 0, -1); 847 $dollar = ''; 848 $wlen -= 1; 849 } 850 if ($wlen < IDX_MINWORDLENGTH && $caret && $dollar && !is_numeric($xword)) 851 continue; 852 if (!isset($tokens[$xword])) 853 $tokenlength[$wlen][] = $xword; 854 if (!$caret || !$dollar) { 855 $re = $caret.preg_quote($xword, '/').$dollar; 856 $tokens[$xword][] = array($word, '/'.$re.'/'); 857 if (!isset($tokenwild[$xword])) 858 $tokenwild[$xword] = $wlen; 859 } else { 860 $tokens[$xword][] = array($word, null); 861 } 862 } 863 asort($tokenwild); 864 // $tokens = array( base word => array( [ query term , regexp ] ... ) ... ) 865 // $tokenlength = array( base word length => base word ... ) 866 // $tokenwild = array( base word => base word length ... ) 867 $length_filter = empty($tokenwild) ? $tokenlength : min(array_keys($tokenlength)); 868 $indexes_known = $this->indexLengths($length_filter); 869 if (!empty($tokenwild)) sort($indexes_known); 870 // get word IDs 871 $wids = array(); 872 foreach ($indexes_known as $ixlen) { 873 $word_idx = $this->getIndex('w', $ixlen); 874 // handle exact search 875 if (isset($tokenlength[$ixlen])) { 876 foreach ($tokenlength[$ixlen] as $xword) { 877 $wid = array_search($xword, $word_idx); 878 if ($wid !== false) { 879 $wids[$ixlen][] = $wid; 880 foreach ($tokens[$xword] as $w) 881 $result[$w[0]][] = "$ixlen*$wid"; 882 } 883 } 884 } 885 // handle wildcard search 886 foreach ($tokenwild as $xword => $wlen) { 887 if ($wlen >= $ixlen) break; 888 foreach ($tokens[$xword] as $w) { 889 if (is_null($w[1])) continue; 890 foreach(array_keys(preg_grep($w[1], $word_idx)) as $wid) { 891 $wids[$ixlen][] = $wid; 892 $result[$w[0]][] = "$ixlen*$wid"; 893 } 894 } 895 } 896 } 897 return $wids; 898 } 899 900 /** 901 * Return a list of all pages 902 * Warning: pages may not exist! 903 * 904 * @param string $key list only pages containing the metadata key (optional) 905 * @return array list of page names 906 * @author Tom N Harris <tnharris@whoopdedo.org> 907 */ 908 public function getPages($key=null) { 909 $page_idx = $this->getIndex('page', ''); 910 if (is_null($key)) return $page_idx; 911 912 $metaname = idx_cleanName($key); 913 914 // Special handling for titles 915 if ($key == 'title') { 916 $title_idx = $this->getIndex('title', ''); 917 array_splice($page_idx, count($title_idx)); 918 foreach ($title_idx as $i => $title) 919 if ($title === "") unset($page_idx[$i]); 920 return array_values($page_idx); 921 } 922 923 $pages = array(); 924 $lines = $this->getIndex($metaname.'_i', ''); 925 foreach ($lines as $line) { 926 $pages = array_merge($pages, $this->parseTuples($page_idx, $line)); 927 } 928 return array_keys($pages); 929 } 930 931 /** 932 * Return a list of words sorted by number of times used 933 * 934 * @param int $min bottom frequency threshold 935 * @param int $max upper frequency limit. No limit if $max<$min 936 * @param int $minlen minimum length of words to count 937 * @param string $key metadata key to list. Uses the fulltext index if not given 938 * @return array list of words as the keys and frequency as values 939 * @author Tom N Harris <tnharris@whoopdedo.org> 940 */ 941 public function histogram($min=1, $max=0, $minlen=3, $key=null) { 942 if ($min < 1) 943 $min = 1; 944 if ($max < $min) 945 $max = 0; 946 947 $result = array(); 948 949 if ($key == 'title') { 950 $index = $this->getIndex('title', ''); 951 $index = array_count_values($index); 952 foreach ($index as $val => $cnt) { 953 if ($cnt >= $min && (!$max || $cnt <= $max) && strlen($val) >= $minlen) 954 $result[$val] = $cnt; 955 } 956 } 957 elseif (!is_null($key)) { 958 $metaname = idx_cleanName($key); 959 $index = $this->getIndex($metaname.'_i', ''); 960 $val_idx = array(); 961 foreach ($index as $wid => $line) { 962 $freq = $this->countTuples($line); 963 if ($freq >= $min && (!$max || $freq <= $max)) 964 $val_idx[$wid] = $freq; 965 } 966 if (!empty($val_idx)) { 967 $words = $this->getIndex($metaname.'_w', ''); 968 foreach ($val_idx as $wid => $freq) { 969 if (strlen($words[$wid]) >= $minlen) 970 $result[$words[$wid]] = $freq; 971 } 972 } 973 } 974 else { 975 $lengths = idx_listIndexLengths(); 976 foreach ($lengths as $length) { 977 if ($length < $minlen) continue; 978 $index = $this->getIndex('i', $length); 979 $words = null; 980 foreach ($index as $wid => $line) { 981 $freq = $this->countTuples($line); 982 if ($freq >= $min && (!$max || $freq <= $max)) { 983 if ($words === null) 984 $words = $this->getIndex('w', $length); 985 $result[$words[$wid]] = $freq; 986 } 987 } 988 } 989 } 990 991 arsort($result); 992 return $result; 993 } 994 995 /** 996 * Lock the indexer. 997 * 998 * @author Tom N Harris <tnharris@whoopdedo.org> 999 */ 1000 protected function lock() { 1001 global $conf; 1002 $status = true; 1003 $run = 0; 1004 $lock = $conf['lockdir'].'/_indexer.lock'; 1005 while (!@mkdir($lock, $conf['dmode'])) { 1006 usleep(50); 1007 if(is_dir($lock) && time()-@filemtime($lock) > 60*5){ 1008 // looks like a stale lock - remove it 1009 if (!@rmdir($lock)) { 1010 $status = "removing the stale lock failed"; 1011 return false; 1012 } else { 1013 $status = "stale lock removed"; 1014 } 1015 }elseif($run++ == 1000){ 1016 // we waited 5 seconds for that lock 1017 return false; 1018 } 1019 } 1020 if (!empty($conf['dperm'])) { 1021 chmod($lock, $conf['dperm']); 1022 } 1023 return $status; 1024 } 1025 1026 /** 1027 * Release the indexer lock. 1028 * 1029 * @author Tom N Harris <tnharris@whoopdedo.org> 1030 */ 1031 protected function unlock() { 1032 global $conf; 1033 @rmdir($conf['lockdir'].'/_indexer.lock'); 1034 return true; 1035 } 1036 1037 /** 1038 * Retrieve the entire index. 1039 * 1040 * The $suffix argument is for an index that is split into 1041 * multiple parts. Different index files should use different 1042 * base names. 1043 * 1044 * @param string $idx name of the index 1045 * @param string $suffix subpart identifier 1046 * @return array list of lines without CR or LF 1047 * @author Tom N Harris <tnharris@whoopdedo.org> 1048 */ 1049 protected function getIndex($idx, $suffix) { 1050 global $conf; 1051 $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; 1052 if (!@file_exists($fn)) return array(); 1053 return file($fn, FILE_IGNORE_NEW_LINES); 1054 } 1055 1056 /** 1057 * Replace the contents of the index with an array. 1058 * 1059 * @param string $idx name of the index 1060 * @param string $suffix subpart identifier 1061 * @param array $lines list of lines without LF 1062 * @return bool If saving succeeded 1063 * @author Tom N Harris <tnharris@whoopdedo.org> 1064 */ 1065 protected function saveIndex($idx, $suffix, &$lines) { 1066 global $conf; 1067 $fn = $conf['indexdir'].'/'.$idx.$suffix; 1068 $fh = @fopen($fn.'.tmp', 'w'); 1069 if (!$fh) return false; 1070 fwrite($fh, join("\n", $lines)); 1071 if (!empty($lines)) 1072 fwrite($fh, "\n"); 1073 fclose($fh); 1074 if (isset($conf['fperm'])) 1075 chmod($fn.'.tmp', $conf['fperm']); 1076 io_rename($fn.'.tmp', $fn.'.idx'); 1077 return true; 1078 } 1079 1080 /** 1081 * Retrieve a line from the index. 1082 * 1083 * @param string $idx name of the index 1084 * @param string $suffix subpart identifier 1085 * @param int $id the line number 1086 * @return string a line with trailing whitespace removed 1087 * @author Tom N Harris <tnharris@whoopdedo.org> 1088 */ 1089 protected function getIndexKey($idx, $suffix, $id) { 1090 global $conf; 1091 $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; 1092 if (!@file_exists($fn)) return ''; 1093 $fh = @fopen($fn, 'r'); 1094 if (!$fh) return ''; 1095 $ln = -1; 1096 while (($line = fgets($fh)) !== false) { 1097 if (++$ln == $id) break; 1098 } 1099 fclose($fh); 1100 return rtrim((string)$line); 1101 } 1102 1103 /** 1104 * Write a line into the index. 1105 * 1106 * @param string $idx name of the index 1107 * @param string $suffix subpart identifier 1108 * @param int $id the line number 1109 * @param string $line line to write 1110 * @return bool If saving succeeded 1111 * @author Tom N Harris <tnharris@whoopdedo.org> 1112 */ 1113 protected function saveIndexKey($idx, $suffix, $id, $line) { 1114 global $conf; 1115 if (substr($line, -1) != "\n") 1116 $line .= "\n"; 1117 $fn = $conf['indexdir'].'/'.$idx.$suffix; 1118 $fh = @fopen($fn.'.tmp', 'w'); 1119 if (!$fh) return false; 1120 $ih = @fopen($fn.'.idx', 'r'); 1121 if ($ih) { 1122 $ln = -1; 1123 while (($curline = fgets($ih)) !== false) { 1124 fwrite($fh, (++$ln == $id) ? $line : $curline); 1125 } 1126 if ($id > $ln) { 1127 while ($id > ++$ln) 1128 fwrite($fh, "\n"); 1129 fwrite($fh, $line); 1130 } 1131 fclose($ih); 1132 } else { 1133 $ln = -1; 1134 while ($id > ++$ln) 1135 fwrite($fh, "\n"); 1136 fwrite($fh, $line); 1137 } 1138 fclose($fh); 1139 if (isset($conf['fperm'])) 1140 chmod($fn.'.tmp', $conf['fperm']); 1141 io_rename($fn.'.tmp', $fn.'.idx'); 1142 return true; 1143 } 1144 1145 /** 1146 * Retrieve or insert a value in the index. 1147 * 1148 * @param string $idx name of the index 1149 * @param string $suffix subpart identifier 1150 * @param string $value line to find in the index 1151 * @return int|bool line number of the value in the index or false if writing the index failed 1152 * @author Tom N Harris <tnharris@whoopdedo.org> 1153 */ 1154 protected function addIndexKey($idx, $suffix, $value) { 1155 $index = $this->getIndex($idx, $suffix); 1156 $id = array_search($value, $index); 1157 if ($id === false) { 1158 $id = count($index); 1159 $index[$id] = $value; 1160 if (!$this->saveIndex($idx, $suffix, $index)) { 1161 trigger_error("Failed to write $idx index", E_USER_ERROR); 1162 return false; 1163 } 1164 } 1165 return $id; 1166 } 1167 1168 /** 1169 * Get the list of lengths indexed in the wiki. 1170 * 1171 * Read the index directory or a cache file and returns 1172 * a sorted array of lengths of the words used in the wiki. 1173 * 1174 * @author YoBoY <yoboy.leguesh@gmail.com> 1175 */ 1176 protected function listIndexLengths() { 1177 return idx_listIndexLengths(); 1178 } 1179 1180 /** 1181 * Get the word lengths that have been indexed. 1182 * 1183 * Reads the index directory and returns an array of lengths 1184 * that there are indices for. 1185 * 1186 * @author YoBoY <yoboy.leguesh@gmail.com> 1187 */ 1188 protected function indexLengths($filter) { 1189 global $conf; 1190 $idx = array(); 1191 if (is_array($filter)) { 1192 // testing if index files exist only 1193 $path = $conf['indexdir']."/i"; 1194 foreach ($filter as $key => $value) { 1195 if (@file_exists($path.$key.'.idx')) 1196 $idx[] = $key; 1197 } 1198 } else { 1199 $lengths = idx_listIndexLengths(); 1200 foreach ($lengths as $key => $length) { 1201 // keep all the values equal or superior 1202 if ((int)$length >= (int)$filter) 1203 $idx[] = $length; 1204 } 1205 } 1206 return $idx; 1207 } 1208 1209 /** 1210 * Insert or replace a tuple in a line. 1211 * 1212 * @author Tom N Harris <tnharris@whoopdedo.org> 1213 */ 1214 protected function updateTuple($line, $id, $count) { 1215 $newLine = $line; 1216 if ($newLine !== '') 1217 $newLine = preg_replace('/(^|:)'.preg_quote($id,'/').'\*\d*/', '', $newLine); 1218 $newLine = trim($newLine, ':'); 1219 if ($count) { 1220 if (strlen($newLine) > 0) 1221 return "$id*$count:".$newLine; 1222 else 1223 return "$id*$count".$newLine; 1224 } 1225 return $newLine; 1226 } 1227 1228 /** 1229 * Split a line into an array of tuples. 1230 * 1231 * @author Tom N Harris <tnharris@whoopdedo.org> 1232 * @author Andreas Gohr <andi@splitbrain.org> 1233 */ 1234 protected function parseTuples(&$keys, $line) { 1235 $result = array(); 1236 if ($line == '') return $result; 1237 $parts = explode(':', $line); 1238 foreach ($parts as $tuple) { 1239 if ($tuple === '') continue; 1240 list($key, $cnt) = explode('*', $tuple); 1241 if (!$cnt) continue; 1242 $key = $keys[$key]; 1243 if (!$key) continue; 1244 $result[$key] = $cnt; 1245 } 1246 return $result; 1247 } 1248 1249 /** 1250 * Sum the counts in a list of tuples. 1251 * 1252 * @author Tom N Harris <tnharris@whoopdedo.org> 1253 */ 1254 protected function countTuples($line) { 1255 $freq = 0; 1256 $parts = explode(':', $line); 1257 foreach ($parts as $tuple) { 1258 if ($tuple === '') continue; 1259 list($pid, $cnt) = explode('*', $tuple); 1260 $freq += (int)$cnt; 1261 } 1262 return $freq; 1263 } 1264} 1265 1266/** 1267 * Create an instance of the indexer. 1268 * 1269 * @return Doku_Indexer a Doku_Indexer 1270 * @author Tom N Harris <tnharris@whoopdedo.org> 1271 */ 1272function idx_get_indexer() { 1273 static $Indexer; 1274 if (!isset($Indexer)) { 1275 $Indexer = new Doku_Indexer(); 1276 } 1277 return $Indexer; 1278} 1279 1280/** 1281 * Returns words that will be ignored. 1282 * 1283 * @return array list of stop words 1284 * @author Tom N Harris <tnharris@whoopdedo.org> 1285 */ 1286function & idx_get_stopwords() { 1287 static $stopwords = null; 1288 if (is_null($stopwords)) { 1289 global $conf; 1290 $swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt'; 1291 if(@file_exists($swfile)){ 1292 $stopwords = file($swfile, FILE_IGNORE_NEW_LINES); 1293 }else{ 1294 $stopwords = array(); 1295 } 1296 } 1297 return $stopwords; 1298} 1299 1300/** 1301 * Adds/updates the search index for the given page 1302 * 1303 * Locking is handled internally. 1304 * 1305 * @param string $page name of the page to index 1306 * @param boolean $verbose print status messages 1307 * @param boolean $force force reindexing even when the index is up to date 1308 * @return boolean the function completed successfully 1309 * @author Tom N Harris <tnharris@whoopdedo.org> 1310 */ 1311function idx_addPage($page, $verbose=false, $force=false) { 1312 $idxtag = metaFN($page,'.indexed'); 1313 // check if page was deleted but is still in the index 1314 if (!page_exists($page)) { 1315 if (!@file_exists($idxtag)) { 1316 if ($verbose) print("Indexer: $page does not exist, ignoring".DOKU_LF); 1317 return false; 1318 } 1319 $Indexer = idx_get_indexer(); 1320 $result = $Indexer->deletePage($page); 1321 if ($result === "locked") { 1322 if ($verbose) print("Indexer: locked".DOKU_LF); 1323 return false; 1324 } 1325 @unlink($idxtag); 1326 return $result; 1327 } 1328 1329 // check if indexing needed 1330 if(!$force && @file_exists($idxtag)){ 1331 if(trim(io_readFile($idxtag)) == idx_get_version()){ 1332 $last = @filemtime($idxtag); 1333 if($last > @filemtime(wikiFN($page))){ 1334 if ($verbose) print("Indexer: index for $page up to date".DOKU_LF); 1335 return false; 1336 } 1337 } 1338 } 1339 1340 $indexenabled = p_get_metadata($page, 'internal index', METADATA_RENDER_UNLIMITED); 1341 if ($indexenabled === false) { 1342 $result = false; 1343 if (@file_exists($idxtag)) { 1344 $Indexer = idx_get_indexer(); 1345 $result = $Indexer->deletePage($page); 1346 if ($result === "locked") { 1347 if ($verbose) print("Indexer: locked".DOKU_LF); 1348 return false; 1349 } 1350 @unlink($idxtag); 1351 } 1352 if ($verbose) print("Indexer: index disabled for $page".DOKU_LF); 1353 return $result; 1354 } 1355 1356 $Indexer = idx_get_indexer(); 1357 $pid = $Indexer->getPID($page); 1358 if ($pid === false) { 1359 if ($verbose) print("Indexer: getting the PID failed for $page".DOKU_LF); 1360 return false; 1361 } 1362 $body = ''; 1363 $metadata = array(); 1364 $metadata['title'] = p_get_metadata($page, 'title', METADATA_RENDER_UNLIMITED); 1365 if (($references = p_get_metadata($page, 'relation references', METADATA_RENDER_UNLIMITED)) !== null) 1366 $metadata['relation_references'] = array_keys($references); 1367 else 1368 $metadata['relation_references'] = array(); 1369 1370 if (($media = p_get_metadata($page, 'relation media', METADATA_RENDER_UNLIMITED)) !== null) 1371 $metadata['relation_media'] = array_keys($media); 1372 else 1373 $metadata['relation_media'] = array(); 1374 1375 $data = compact('page', 'body', 'metadata', 'pid'); 1376 $evt = new Doku_Event('INDEXER_PAGE_ADD', $data); 1377 if ($evt->advise_before()) $data['body'] = $data['body'] . " " . rawWiki($page); 1378 $evt->advise_after(); 1379 unset($evt); 1380 extract($data); 1381 1382 $result = $Indexer->addPageWords($page, $body); 1383 if ($result === "locked") { 1384 if ($verbose) print("Indexer: locked".DOKU_LF); 1385 return false; 1386 } 1387 1388 if ($result) { 1389 $result = $Indexer->addMetaKeys($page, $metadata); 1390 if ($result === "locked") { 1391 if ($verbose) print("Indexer: locked".DOKU_LF); 1392 return false; 1393 } 1394 } 1395 1396 if ($result) 1397 io_saveFile(metaFN($page,'.indexed'), idx_get_version()); 1398 if ($verbose) { 1399 print("Indexer: finished".DOKU_LF); 1400 return true; 1401 } 1402 return $result; 1403} 1404 1405/** 1406 * Find tokens in the fulltext index 1407 * 1408 * Takes an array of words and will return a list of matching 1409 * pages for each one. 1410 * 1411 * Important: No ACL checking is done here! All results are 1412 * returned, regardless of permissions 1413 * 1414 * @param array $words list of words to search for 1415 * @return array list of pages found, associated with the search terms 1416 */ 1417function idx_lookup(&$words) { 1418 $Indexer = idx_get_indexer(); 1419 return $Indexer->lookup($words); 1420} 1421 1422/** 1423 * Split a string into tokens 1424 * 1425 */ 1426function idx_tokenizer($string, $wc=false) { 1427 $Indexer = idx_get_indexer(); 1428 return $Indexer->tokenizer($string, $wc); 1429} 1430 1431/* For compatibility */ 1432 1433/** 1434 * Read the list of words in an index (if it exists). 1435 * 1436 * @author Tom N Harris <tnharris@whoopdedo.org> 1437 */ 1438function idx_getIndex($idx, $suffix) { 1439 global $conf; 1440 $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; 1441 if (!@file_exists($fn)) return array(); 1442 return file($fn); 1443} 1444 1445/** 1446 * Get the list of lengths indexed in the wiki. 1447 * 1448 * Read the index directory or a cache file and returns 1449 * a sorted array of lengths of the words used in the wiki. 1450 * 1451 * @author YoBoY <yoboy.leguesh@gmail.com> 1452 */ 1453function idx_listIndexLengths() { 1454 global $conf; 1455 // testing what we have to do, create a cache file or not. 1456 if ($conf['readdircache'] == 0) { 1457 $docache = false; 1458 } else { 1459 clearstatcache(); 1460 if (@file_exists($conf['indexdir'].'/lengths.idx') 1461 && (time() < @filemtime($conf['indexdir'].'/lengths.idx') + $conf['readdircache'])) { 1462 if (($lengths = @file($conf['indexdir'].'/lengths.idx', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)) !== false) { 1463 $idx = array(); 1464 foreach ($lengths as $length) { 1465 $idx[] = (int)$length; 1466 } 1467 return $idx; 1468 } 1469 } 1470 $docache = true; 1471 } 1472 1473 if ($conf['readdircache'] == 0 || $docache) { 1474 $dir = @opendir($conf['indexdir']); 1475 if ($dir === false) 1476 return array(); 1477 $idx = array(); 1478 while (($f = readdir($dir)) !== false) { 1479 if (substr($f, 0, 1) == 'i' && substr($f, -4) == '.idx') { 1480 $i = substr($f, 1, -4); 1481 if (is_numeric($i)) 1482 $idx[] = (int)$i; 1483 } 1484 } 1485 closedir($dir); 1486 sort($idx); 1487 // save this in a file 1488 if ($docache) { 1489 $handle = @fopen($conf['indexdir'].'/lengths.idx', 'w'); 1490 @fwrite($handle, implode("\n", $idx)); 1491 @fclose($handle); 1492 } 1493 return $idx; 1494 } 1495 1496 return array(); 1497} 1498 1499/** 1500 * Get the word lengths that have been indexed. 1501 * 1502 * Reads the index directory and returns an array of lengths 1503 * that there are indices for. 1504 * 1505 * @author YoBoY <yoboy.leguesh@gmail.com> 1506 */ 1507function idx_indexLengths($filter) { 1508 global $conf; 1509 $idx = array(); 1510 if (is_array($filter)) { 1511 // testing if index files exist only 1512 $path = $conf['indexdir']."/i"; 1513 foreach ($filter as $key => $value) { 1514 if (@file_exists($path.$key.'.idx')) 1515 $idx[] = $key; 1516 } 1517 } else { 1518 $lengths = idx_listIndexLengths(); 1519 foreach ($lengths as $key => $length) { 1520 // keep all the values equal or superior 1521 if ((int)$length >= (int)$filter) 1522 $idx[] = $length; 1523 } 1524 } 1525 return $idx; 1526} 1527 1528/** 1529 * Clean a name of a key for use as a file name. 1530 * 1531 * Romanizes non-latin characters, then strips away anything that's 1532 * not a letter, number, or underscore. 1533 * 1534 * @author Tom N Harris <tnharris@whoopdedo.org> 1535 */ 1536function idx_cleanName($name) { 1537 $name = utf8_romanize(trim((string)$name)); 1538 $name = preg_replace('#[ \./\\:-]+#', '_', $name); 1539 $name = preg_replace('/[^A-Za-z0-9_]/', '', $name); 1540 return strtolower($name); 1541} 1542 1543//Setup VIM: ex: et ts=4 : 1544