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