1b4ce25e9SAndreas Gohr<?php 2b4ce25e9SAndreas Gohr/** 3fcd3bb7cSAndreas Gohr * Functions to create the fulltext search index 4b4ce25e9SAndreas Gohr * 5b4ce25e9SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6b4ce25e9SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 700803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 8b4ce25e9SAndreas Gohr */ 9b4ce25e9SAndreas Gohr 10fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 11b4ce25e9SAndreas Gohr 127c2ef4e8STom N Harris// Version tag used to force rebuild on upgrade 13287bc287STom N Harrisdefine('INDEXER_VERSION', 4); 147c2ef4e8STom N Harris 1533815ce2SChris Smith// set the minimum token length to use in the index (note, this doesn't apply to numeric tokens) 16d3fb3219SAndreas Gohrif (!defined('IDX_MINWORDLENGTH')) define('IDX_MINWORDLENGTH',2); 1733815ce2SChris Smith 1893a60ad2SAndreas Gohr// Asian characters are handled as words. The following regexp defines the 1993a60ad2SAndreas Gohr// Unicode-Ranges for Asian characters 2093a60ad2SAndreas Gohr// Ranges taken from http://en.wikipedia.org/wiki/Unicode_block 2193a60ad2SAndreas Gohr// I'm no language expert. If you think some ranges are wrongly chosen or 2293a60ad2SAndreas Gohr// a range is missing, please contact me 23d5b23302STom N Harrisdefine('IDX_ASIAN1','[\x{0E00}-\x{0E7F}]'); // Thai 24d5b23302STom N Harrisdefine('IDX_ASIAN2','['. 25d5b23302STom N Harris '\x{2E80}-\x{3040}'. // CJK -> Hangul 26d5b23302STom N Harris '\x{309D}-\x{30A0}'. 27a0c5c349STom N Harris '\x{30FD}-\x{31EF}\x{3200}-\x{D7AF}'. 2893a60ad2SAndreas Gohr '\x{F900}-\x{FAFF}'. // CJK Compatibility Ideographs 2993a60ad2SAndreas Gohr '\x{FE30}-\x{FE4F}'. // CJK Compatibility Forms 30ae6c4ec0SDanny Lin "\xF0\xA0\x80\x80-\xF0\xAA\x9B\x9F". // CJK Extension B 31ae6c4ec0SDanny Lin "\xF0\xAA\x9C\x80-\xF0\xAB\x9C\xBF". // CJK Extension C 32ae6c4ec0SDanny Lin "\xF0\xAB\x9D\x80-\xF0\xAB\xA0\x9F". // CJK Extension D 33ae6c4ec0SDanny Lin "\xF0\xAF\xA0\x80-\xF0\xAF\xAB\xBF". // CJK Compatibility Supplement 3493a60ad2SAndreas Gohr ']'); 35d5b23302STom N Harrisdefine('IDX_ASIAN3','['. // Hiragana/Katakana (can be two characters) 36d5b23302STom N Harris '\x{3042}\x{3044}\x{3046}\x{3048}'. 37d5b23302STom N Harris '\x{304A}-\x{3062}\x{3064}-\x{3082}'. 38d5b23302STom N Harris '\x{3084}\x{3086}\x{3088}-\x{308D}'. 39d5b23302STom N Harris '\x{308F}-\x{3094}'. 40d5b23302STom N Harris '\x{30A2}\x{30A4}\x{30A6}\x{30A8}'. 41d5b23302STom N Harris '\x{30AA}-\x{30C2}\x{30C4}-\x{30E2}'. 42d5b23302STom N Harris '\x{30E4}\x{30E6}\x{30E8}-\x{30ED}'. 43d5b23302STom N Harris '\x{30EF}-\x{30F4}\x{30F7}-\x{30FA}'. 44d5b23302STom N Harris ']['. 45d5b23302STom N Harris '\x{3041}\x{3043}\x{3045}\x{3047}\x{3049}'. 46d5b23302STom N Harris '\x{3063}\x{3083}\x{3085}\x{3087}\x{308E}\x{3095}-\x{309C}'. 47d5b23302STom N Harris '\x{30A1}\x{30A3}\x{30A5}\x{30A7}\x{30A9}'. 48d5b23302STom N Harris '\x{30C3}\x{30E3}\x{30E5}\x{30E7}\x{30EE}\x{30F5}\x{30F6}\x{30FB}\x{30FC}'. 49d5b23302STom N Harris '\x{31F0}-\x{31FF}'. 50d5b23302STom N Harris ']?'); 51699b8a0bSAndreas Gohrdefine('IDX_ASIAN', '(?:'.IDX_ASIAN1.'|'.IDX_ASIAN2.'|'.IDX_ASIAN3.')'); 5293a60ad2SAndreas Gohr 53b4ce25e9SAndreas Gohr/** 547c2ef4e8STom N Harris * Version of the indexer taking into consideration the external tokenizer. 557c2ef4e8STom N Harris * The indexer is only compatible with data written by the same version. 567c2ef4e8STom N Harris * 57d0d6fe1bSTom N Harris * Triggers INDEXER_VERSION_GET 58d0d6fe1bSTom N Harris * Plugins that modify what gets indexed should hook this event and 59d0d6fe1bSTom N Harris * add their version info to the event data like so: 60d0d6fe1bSTom N Harris * $data[$plugin_name] = $plugin_version; 61d0d6fe1bSTom N Harris * 627c2ef4e8STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 638605afb1SMichael Hamann * @author Michael Hamann <michael@content-space.de> 647c2ef4e8STom N Harris */ 657c2ef4e8STom N Harrisfunction idx_get_version(){ 66d0d6fe1bSTom N Harris static $indexer_version = null; 67d0d6fe1bSTom N Harris if ($indexer_version == null) { 687c2ef4e8STom N Harris global $conf; 697c2ef4e8STom N Harris if($conf['external_tokenizer']) 708605afb1SMichael Hamann $version = INDEXER_VERSION . '+' . trim($conf['tokenizer_cmd']); 717c2ef4e8STom N Harris else 728605afb1SMichael Hamann $version = INDEXER_VERSION; 738605afb1SMichael Hamann 74d0d6fe1bSTom N Harris // DokuWiki version is included for the convenience of plugins 75d0d6fe1bSTom N Harris $data = array('dokuwiki'=>$version); 768605afb1SMichael Hamann trigger_event('INDEXER_VERSION_GET', $data, null, false); 77d0d6fe1bSTom N Harris unset($data['dokuwiki']); // this needs to be first 78d0d6fe1bSTom N Harris ksort($data); 79d0d6fe1bSTom N Harris foreach ($data as $plugin=>$vers) 80d0d6fe1bSTom N Harris $version .= '+'.$plugin.'='.$vers; 81d0d6fe1bSTom N Harris $indexer_version = $version; 82d0d6fe1bSTom N Harris } 83d0d6fe1bSTom N Harris return $indexer_version; 847c2ef4e8STom N Harris} 857c2ef4e8STom N Harris 867c2ef4e8STom N Harris/** 87d5b23302STom N Harris * Measure the length of a string. 88d5b23302STom N Harris * Differs from strlen in handling of asian characters. 89d5b23302STom N Harris * 90d5b23302STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 91d5b23302STom N Harris */ 92d5b23302STom N Harrisfunction wordlen($w){ 93d5b23302STom N Harris $l = strlen($w); 94d5b23302STom N Harris // If left alone, all chinese "words" will get put into w3.idx 95d5b23302STom N Harris // So the "length" of a "word" is faked 964b9792c6STom N Harris if(preg_match_all('/[\xE2-\xEF]/',$w,$leadbytes)) { 974b9792c6STom N Harris foreach($leadbytes[0] as $b) 984b9792c6STom N Harris $l += ord($b) - 0xE1; 994b9792c6STom N Harris } 100d5b23302STom N Harris return $l; 101d5b23302STom N Harris} 102d5b23302STom N Harris 103d5b23302STom N Harris/** 10400803e56STom N Harris * Class that encapsulates operations on the indexer database. 105579b0f7eSTNHarris * 106579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 107579b0f7eSTNHarris */ 10800803e56STom N Harrisclass Doku_Indexer { 109579b0f7eSTNHarris 110579b0f7eSTNHarris /** 11100803e56STom N Harris * Adds the contents of a page to the fulltext index 112dd35e9c9SAndreas Gohr * 11300803e56STom N Harris * The added text replaces previous words for the same page. 11400803e56STom N Harris * An empty value erases the page. 11500803e56STom N Harris * 11600803e56STom N Harris * @param string $page a page name 11700803e56STom N Harris * @param string $text the body of the page 11800803e56STom N Harris * @return boolean the function completed successfully 11900803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 120dd35e9c9SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 121dd35e9c9SAndreas Gohr */ 12200803e56STom N Harris public function addPageWords($page, $text) { 1239b41be24STom N Harris if (!$this->_lock()) 1249b41be24STom N Harris return "locked"; 12500803e56STom N Harris 12600803e56STom N Harris // load known documents 1275981eb09STom N Harris $pid = $this->_addIndexKey('page', '', $page); 1285981eb09STom N Harris if ($pid === false) { 12900803e56STom N Harris $this->_unlock(); 13000803e56STom N Harris return false; 13100803e56STom N Harris } 13200803e56STom N Harris 13300803e56STom N Harris $pagewords = array(); 13400803e56STom N Harris // get word usage in page 13500803e56STom N Harris $words = $this->_getPageWords($text); 13600803e56STom N Harris if ($words === false) { 13700803e56STom N Harris $this->_unlock(); 13800803e56STom N Harris return false; 13900803e56STom N Harris } 14000803e56STom N Harris 14100803e56STom N Harris if (!empty($words)) { 14200803e56STom N Harris foreach (array_keys($words) as $wlen) { 14300803e56STom N Harris $index = $this->_getIndex('i', $wlen); 14400803e56STom N Harris foreach ($words[$wlen] as $wid => $freq) { 14500803e56STom N Harris $idx = ($wid<count($index)) ? $index[$wid] : ''; 14600803e56STom N Harris $index[$wid] = $this->_updateTuple($idx, $pid, $freq); 14700803e56STom N Harris $pagewords[] = "$wlen*$wid"; 14800803e56STom N Harris } 14900803e56STom N Harris if (!$this->_saveIndex('i', $wlen, $index)) { 15000803e56STom N Harris $this->_unlock(); 15100803e56STom N Harris return false; 15200803e56STom N Harris } 15300803e56STom N Harris } 15400803e56STom N Harris } 15500803e56STom N Harris 15600803e56STom N Harris // Remove obsolete index entries 15700803e56STom N Harris $pageword_idx = $this->_getIndexKey('pageword', '', $pid); 15800803e56STom N Harris if ($pageword_idx !== '') { 15900803e56STom N Harris $oldwords = explode(':',$pageword_idx); 16000803e56STom N Harris $delwords = array_diff($oldwords, $pagewords); 16100803e56STom N Harris $upwords = array(); 16200803e56STom N Harris foreach ($delwords as $word) { 16300803e56STom N Harris if ($word != '') { 16400803e56STom N Harris list($wlen,$wid) = explode('*', $word); 16500803e56STom N Harris $wid = (int)$wid; 16600803e56STom N Harris $upwords[$wlen][] = $wid; 16700803e56STom N Harris } 16800803e56STom N Harris } 16900803e56STom N Harris foreach ($upwords as $wlen => $widx) { 17000803e56STom N Harris $index = $this->_getIndex('i', $wlen); 17100803e56STom N Harris foreach ($widx as $wid) { 17200803e56STom N Harris $index[$wid] = $this->_updateTuple($index[$wid], $pid, 0); 17300803e56STom N Harris } 17400803e56STom N Harris $this->_saveIndex('i', $wlen, $index); 17500803e56STom N Harris } 17600803e56STom N Harris } 17700803e56STom N Harris // Save the reverse index 17800803e56STom N Harris $pageword_idx = join(':', $pagewords); 17900803e56STom N Harris if (!$this->_saveIndexKey('pageword', '', $pid, $pageword_idx)) { 18000803e56STom N Harris $this->_unlock(); 18100803e56STom N Harris return false; 18200803e56STom N Harris } 18300803e56STom N Harris 18400803e56STom N Harris $this->_unlock(); 185dd35e9c9SAndreas Gohr return true; 186dd35e9c9SAndreas Gohr } 187dd35e9c9SAndreas Gohr 188dd35e9c9SAndreas Gohr /** 18900803e56STom N Harris * Split the words in a page and add them to the index. 19044ca0adfSAndreas Gohr * 19144ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 19217f42b01SChris Smith * @author Christopher Smith <chris@jalakai.co.uk> 19300803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 194b4ce25e9SAndreas Gohr */ 19500803e56STom N Harris private function _getPageWords($text) { 19644ca0adfSAndreas Gohr global $conf; 19744ca0adfSAndreas Gohr 19800803e56STom N Harris $tokens = $this->tokenizer($text); 19917f42b01SChris Smith $tokens = array_count_values($tokens); // count the frequency of each token 20017f42b01SChris Smith 20117f42b01SChris Smith $words = array(); 2024e1bf408STom N Harris foreach ($tokens as $w=>$c) { 203d5b23302STom N Harris $l = wordlen($w); 204579b0f7eSTNHarris if (isset($words[$l])){ 2054e1bf408STom N Harris $words[$l][$w] = $c + (isset($words[$l][$w]) ? $words[$l][$w] : 0); 20617f42b01SChris Smith }else{ 2074e1bf408STom N Harris $words[$l] = array($w => $c); 20817f42b01SChris Smith } 20917f42b01SChris Smith } 21017f42b01SChris Smith 211579b0f7eSTNHarris // arrive here with $words = array(wordlen => array(word => frequency)) 212e5e50383SMichael Hamann $word_idx_modified = false; 213b4ce25e9SAndreas Gohr $index = array(); //resulting index 214579b0f7eSTNHarris foreach (array_keys($words) as $wlen) { 21500803e56STom N Harris $word_idx = $this->_getIndex('w', $wlen); 216579b0f7eSTNHarris foreach ($words[$wlen] as $word => $freq) { 21700803e56STom N Harris $wid = array_search($word, $word_idx); 21800803e56STom N Harris if ($wid === false) { 219d5b23302STom N Harris $wid = count($word_idx); 22000803e56STom N Harris $word_idx[] = $word; 221e5e50383SMichael Hamann $word_idx_modified = true; 222b4ce25e9SAndreas Gohr } 223579b0f7eSTNHarris if (!isset($index[$wlen])) 224579b0f7eSTNHarris $index[$wlen] = array(); 225579b0f7eSTNHarris $index[$wlen][$wid] = $freq; 22644ca0adfSAndreas Gohr } 22700803e56STom N Harris // save back the word index 22800803e56STom N Harris if ($word_idx_modified && !$this->_saveIndex('w', $wlen, $word_idx)) 22944ca0adfSAndreas Gohr return false; 23044ca0adfSAndreas Gohr } 231b4ce25e9SAndreas Gohr 232b4ce25e9SAndreas Gohr return $index; 233b4ce25e9SAndreas Gohr } 234b4ce25e9SAndreas Gohr 23544ca0adfSAndreas Gohr /** 236e1e1a7e0SMichael Hamann * Add/update keys to/of the metadata index. 23744ca0adfSAndreas Gohr * 23800803e56STom N Harris * Adding new keys does not remove other keys for the page. 23900803e56STom N Harris * An empty value will erase the key. 24000803e56STom N Harris * The $key parameter can be an array to add multiple keys. $value will 24100803e56STom N Harris * not be used if $key is an array. 24244ca0adfSAndreas Gohr * 24300803e56STom N Harris * @param string $page a page name 24400803e56STom N Harris * @param mixed $key a key string or array of key=>value pairs 24500803e56STom N Harris * @param mixed $value the value or list of values 24600803e56STom N Harris * @return boolean the function completed successfully 24700803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 248e1e1a7e0SMichael Hamann * @author Michael Hamann <michael@content-space.de> 24944ca0adfSAndreas Gohr */ 25000803e56STom N Harris public function addMetaKeys($page, $key, $value=null) { 25100803e56STom N Harris if (!is_array($key)) { 25200803e56STom N Harris $key = array($key => $value); 25300803e56STom N Harris } elseif (!is_null($value)) { 25400803e56STom N Harris // $key is array, but $value is not null 25500803e56STom N Harris trigger_error("array passed to addMetaKeys but value is not null", E_USER_WARNING); 25600803e56STom N Harris } 25700803e56STom N Harris 258e1e1a7e0SMichael Hamann if (!$this->_lock()) 259e1e1a7e0SMichael Hamann return "locked"; 260b4ce25e9SAndreas Gohr 261488dd6ceSAndreas Gohr // load known documents 26200803e56STom N Harris $pid = $this->_addIndexKey('page', '', $page); 26300803e56STom N Harris if ($pid === false) { 26400803e56STom N Harris $this->_unlock(); 265579b0f7eSTNHarris return false; 26644ca0adfSAndreas Gohr } 26700803e56STom N Harris 268c1209673STom N Harris // Special handling for titles so the index file is simpler 269c1209673STom N Harris if (array_key_exists('title', $key)) { 270c1209673STom N Harris $value = $key['title']; 271c1209673STom N Harris if (is_array($value)) 272c1209673STom N Harris $value = $value[0]; 273c1209673STom N Harris $this->_saveIndexKey('title', '', $pid, $value); 274c1209673STom N Harris unset($key['title']); 275c1209673STom N Harris } 276c1209673STom N Harris 27700803e56STom N Harris foreach ($key as $name => $values) { 27800803e56STom N Harris $metaname = idx_cleanName($name); 2790604da34STom N Harris $this->_addIndexKey('metadata', '', $metaname); 28000803e56STom N Harris $metaidx = $this->_getIndex($metaname, '_i'); 28100803e56STom N Harris $metawords = $this->_getIndex($metaname, '_w'); 28200803e56STom N Harris $addwords = false; 283e1e1a7e0SMichael Hamann 284e1e1a7e0SMichael Hamann if (!is_array($values)) $values = array($values); 285e1e1a7e0SMichael Hamann 286e1e1a7e0SMichael Hamann $val_idx = $this->_getIndexKey($metaname, '_p', $pid); 287e1e1a7e0SMichael Hamann if ($val_idx != '') { 288e1e1a7e0SMichael Hamann $val_idx = explode(':', $val_idx); 289e1e1a7e0SMichael Hamann // -1 means remove, 0 keep, 1 add 290e1e1a7e0SMichael Hamann $val_idx = array_combine($val_idx, array_fill(0, count($val_idx), -1)); 291e1e1a7e0SMichael Hamann } else { 292e1e1a7e0SMichael Hamann $val_idx = array(); 293e1e1a7e0SMichael Hamann } 294e1e1a7e0SMichael Hamann 295e1e1a7e0SMichael Hamann 29600803e56STom N Harris foreach ($values as $val) { 29700803e56STom N Harris $val = (string)$val; 29800803e56STom N Harris if ($val !== "") { 29900803e56STom N Harris $id = array_search($val, $metawords); 30000803e56STom N Harris if ($id === false) { 30100803e56STom N Harris $id = count($metawords); 30200803e56STom N Harris $metawords[$id] = $val; 30300803e56STom N Harris $addwords = true; 304d5b23302STom N Harris } 305e1e1a7e0SMichael Hamann // test if value is already in the index 306e1e1a7e0SMichael Hamann if (isset($val_idx[$id]) && $val_idx[$id] <= 0) 307e1e1a7e0SMichael Hamann $val_idx[$id] = 0; 308e1e1a7e0SMichael Hamann else // else add it 309e1e1a7e0SMichael Hamann $val_idx[$id] = 1; 31044ca0adfSAndreas Gohr } 311579b0f7eSTNHarris } 312e1e1a7e0SMichael Hamann 31300803e56STom N Harris if ($addwords) 31400803e56STom N Harris $this->_saveIndex($metaname.'_w', '', $metawords); 315e1e1a7e0SMichael Hamann $vals_changed = false; 316e1e1a7e0SMichael Hamann foreach ($val_idx as $id => $action) { 317e1e1a7e0SMichael Hamann if ($action == -1) { 318e1e1a7e0SMichael Hamann $metaidx[$id] = $this->_updateTuple($metaidx[$id], $pid, 0); 319e1e1a7e0SMichael Hamann $vals_changed = true; 320e1e1a7e0SMichael Hamann unset($val_idx[$id]); 321e1e1a7e0SMichael Hamann } elseif ($action == 1) { 322e1e1a7e0SMichael Hamann $metaidx[$id] = $this->_updateTuple($metaidx[$id], $pid, 1); 323e1e1a7e0SMichael Hamann $vals_changed = true; 324e1e1a7e0SMichael Hamann } 325e1e1a7e0SMichael Hamann } 326e1e1a7e0SMichael Hamann 327e1e1a7e0SMichael Hamann if ($vals_changed) { 32800803e56STom N Harris $this->_saveIndex($metaname.'_i', '', $metaidx); 329e1e1a7e0SMichael Hamann $val_idx = implode(':', array_keys($val_idx)); 330e1e1a7e0SMichael Hamann $this->_saveIndexKey($metaname.'_p', '', $pid, $val_idx); 331b6344591STom N Harris } 332e1e1a7e0SMichael Hamann 33300803e56STom N Harris unset($metaidx); 33400803e56STom N Harris unset($metawords); 335a0c5c349STom N Harris } 336e1e1a7e0SMichael Hamann 337e1e1a7e0SMichael Hamann $this->_unlock(); 338579b0f7eSTNHarris return true; 33944ca0adfSAndreas Gohr } 34044ca0adfSAndreas Gohr 34144ca0adfSAndreas Gohr /** 34200803e56STom N Harris * Remove a page from the index 34344ca0adfSAndreas Gohr * 34400803e56STom N Harris * Erases entries in all known indexes. 34544ca0adfSAndreas Gohr * 34600803e56STom N Harris * @param string $page a page name 34700803e56STom N Harris * @return boolean the function completed successfully 34800803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 34944ca0adfSAndreas Gohr */ 35000803e56STom N Harris public function deletePage($page) { 351bbc85ee4STom N Harris if (!$this->_lock()) 352bbc85ee4STom N Harris return "locked"; 353bbc85ee4STom N Harris 354bbc85ee4STom N Harris // load known documents 3555981eb09STom N Harris $pid = $this->_getIndexKey('page', '', $page); 3565981eb09STom N Harris if ($pid === false) { 357bbc85ee4STom N Harris $this->_unlock(); 358bbc85ee4STom N Harris return false; 359bbc85ee4STom N Harris } 360bbc85ee4STom N Harris 361bbc85ee4STom N Harris // Remove obsolete index entries 362bbc85ee4STom N Harris $pageword_idx = $this->_getIndexKey('pageword', '', $pid); 363bbc85ee4STom N Harris if ($pageword_idx !== '') { 364bbc85ee4STom N Harris $delwords = explode(':',$pageword_idx); 365bbc85ee4STom N Harris $upwords = array(); 366bbc85ee4STom N Harris foreach ($delwords as $word) { 367bbc85ee4STom N Harris if ($word != '') { 368bbc85ee4STom N Harris list($wlen,$wid) = explode('*', $word); 369bbc85ee4STom N Harris $wid = (int)$wid; 370bbc85ee4STom N Harris $upwords[$wlen][] = $wid; 371bbc85ee4STom N Harris } 372bbc85ee4STom N Harris } 373bbc85ee4STom N Harris foreach ($upwords as $wlen => $widx) { 374bbc85ee4STom N Harris $index = $this->_getIndex('i', $wlen); 375bbc85ee4STom N Harris foreach ($widx as $wid) { 376bbc85ee4STom N Harris $index[$wid] = $this->_updateTuple($index[$wid], $pid, 0); 377bbc85ee4STom N Harris } 378bbc85ee4STom N Harris $this->_saveIndex('i', $wlen, $index); 379bbc85ee4STom N Harris } 380bbc85ee4STom N Harris } 381bbc85ee4STom N Harris // Save the reverse index 382bbc85ee4STom N Harris if (!$this->_saveIndexKey('pageword', '', $pid, "")) { 383bbc85ee4STom N Harris $this->_unlock(); 384bbc85ee4STom N Harris return false; 385bbc85ee4STom N Harris } 386bbc85ee4STom N Harris 387c1209673STom N Harris $this->_saveIndexKey('title', '', $pid, ""); 3880604da34STom N Harris $keyidx = $this->_getIndex('metadata', ''); 3890604da34STom N Harris foreach ($keyidx as $metaname) { 3900604da34STom N Harris $val_idx = explode(':', $this->_getIndexKey($metaname.'_p', '', $pid)); 3910604da34STom N Harris $meta_idx = $this->_getIndex($metaname.'_i', ''); 3920604da34STom N Harris foreach ($val_idx as $id) { 3930604da34STom N Harris $meta_idx[$id] = $this->_updateTuple($meta_idx[$id], $pid, 0); 3940604da34STom N Harris } 3950604da34STom N Harris $this->_saveIndex($metaname.'_i', '', $meta_idx); 3960604da34STom N Harris $this->_saveIndexKey($metaname.'_p', '', $pid, ''); 3970604da34STom N Harris } 398bbc85ee4STom N Harris 399bbc85ee4STom N Harris $this->_unlock(); 400bbc85ee4STom N Harris return true; 401d5b23302STom N Harris } 40244ca0adfSAndreas Gohr 403d5b23302STom N Harris /** 40400803e56STom N Harris * Split the text into words for fulltext search 405d5b23302STom N Harris * 40600803e56STom N Harris * TODO: does this also need &$stopwords ? 407d5b23302STom N Harris * 40800803e56STom N Harris * @param string $text plain text 40900803e56STom N Harris * @param boolean $wc are wildcards allowed? 41000803e56STom N Harris * @return array list of words in the text 411d5b23302STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 412d5b23302STom N Harris * @author Andreas Gohr <andi@splitbrain.org> 413d5b23302STom N Harris */ 41400803e56STom N Harris public function tokenizer($text, $wc=false) { 41522952965SYoBoY global $conf; 41600803e56STom N Harris $words = array(); 41700803e56STom N Harris $wc = ($wc) ? '' : '\*'; 41800803e56STom N Harris $stopwords =& idx_get_stopwords(); 41900803e56STom N Harris 42000803e56STom N Harris if ($conf['external_tokenizer'] && $conf['tokenizer_cmd'] != '') { 42100803e56STom N Harris if (0 == io_exec($conf['tokenizer_cmd'], $text, $output)) 42200803e56STom N Harris $text = $output; 42322952965SYoBoY } else { 42400803e56STom N Harris if (preg_match('/[^0-9A-Za-z ]/u', $text)) { 42500803e56STom N Harris // handle asian chars as single words (may fail on older PHP version) 42600803e56STom N Harris $asia = @preg_replace('/('.IDX_ASIAN.')/u', ' \1 ', $text); 42700803e56STom N Harris if (!is_null($asia)) $text = $asia; // recover from regexp falure 42822952965SYoBoY } 42922952965SYoBoY } 430f77fc90dSMichael Hamann $text = strtr($text, 4314f0030ddSAndreas Gohr array( 4324f0030ddSAndreas Gohr "\r" => ' ', 4334f0030ddSAndreas Gohr "\n" => ' ', 4344f0030ddSAndreas Gohr "\t" => ' ', 4354f0030ddSAndreas Gohr "\xC2\xAD" => '', //soft-hyphen 4364f0030ddSAndreas Gohr ) 4374f0030ddSAndreas Gohr ); 43800803e56STom N Harris if (preg_match('/[^0-9A-Za-z ]/u', $text)) 43900803e56STom N Harris $text = utf8_stripspecials($text, ' ', '\._\-:'.$wc); 44022952965SYoBoY 44100803e56STom N Harris $wordlist = explode(' ', $text); 442675bf41fSTom N Harris foreach ($wordlist as $i => &$word) { 44300803e56STom N Harris $word = (preg_match('/[^0-9A-Za-z]/u', $word)) ? 44400803e56STom N Harris utf8_strtolower($word) : strtolower($word); 445675bf41fSTom N Harris if ((!is_numeric($word) && strlen($word) < IDX_MINWORDLENGTH) 446675bf41fSTom N Harris || array_search($word, $stopwords) !== false) 447675bf41fSTom N Harris unset($wordlist[$i]); 44822952965SYoBoY } 449675bf41fSTom N Harris return array_values($wordlist); 45022952965SYoBoY } 45122952965SYoBoY 45222952965SYoBoY /** 45300803e56STom N Harris * Find pages in the fulltext index containing the words, 454579b0f7eSTNHarris * 45500803e56STom N Harris * The search words must be pre-tokenized, meaning only letters and 45600803e56STom N Harris * numbers with an optional wildcard 457579b0f7eSTNHarris * 45800803e56STom N Harris * The returned array will have the original tokens as key. The values 45900803e56STom N Harris * in the returned list is an array with the page names as keys and the 460b00bd361STom N Harris * number of times that token appears on the page as value. 46100803e56STom N Harris * 4629b41be24STom N Harris * @param arrayref $tokens list of words to search for 46300803e56STom N Harris * @return array list of page names with usage counts 46400803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 46500803e56STom N Harris * @author Andreas Gohr <andi@splitbrain.org> 466579b0f7eSTNHarris */ 4679b41be24STom N Harris public function lookup(&$tokens) { 46800803e56STom N Harris $result = array(); 46900803e56STom N Harris $wids = $this->_getIndexWords($tokens, $result); 47000803e56STom N Harris if (empty($wids)) return array(); 47100803e56STom N Harris // load known words and documents 47200803e56STom N Harris $page_idx = $this->_getIndex('page', ''); 47300803e56STom N Harris $docs = array(); 47400803e56STom N Harris foreach (array_keys($wids) as $wlen) { 47500803e56STom N Harris $wids[$wlen] = array_unique($wids[$wlen]); 47600803e56STom N Harris $index = $this->_getIndex('i', $wlen); 47700803e56STom N Harris foreach($wids[$wlen] as $ixid) { 47800803e56STom N Harris if ($ixid < count($index)) 47900803e56STom N Harris $docs["$wlen*$ixid"] = $this->_parseTuples($page_idx, $index[$ixid]); 480d5b23302STom N Harris } 481d5b23302STom N Harris } 48200803e56STom N Harris // merge found pages into final result array 48300803e56STom N Harris $final = array(); 48400803e56STom N Harris foreach ($result as $word => $res) { 48500803e56STom N Harris $final[$word] = array(); 48600803e56STom N Harris foreach ($res as $wid) { 48700803e56STom N Harris $hits = &$docs[$wid]; 48800803e56STom N Harris foreach ($hits as $hitkey => $hitcnt) { 48900803e56STom N Harris // make sure the document still exists 49000803e56STom N Harris if (!page_exists($hitkey, '', false)) continue; 49100803e56STom N Harris if (!isset($final[$word][$hitkey])) 49200803e56STom N Harris $final[$word][$hitkey] = $hitcnt; 49300803e56STom N Harris else 49400803e56STom N Harris $final[$word][$hitkey] += $hitcnt; 495d5b23302STom N Harris } 496579b0f7eSTNHarris } 497579b0f7eSTNHarris } 49800803e56STom N Harris return $final; 499579b0f7eSTNHarris } 500579b0f7eSTNHarris 501579b0f7eSTNHarris /** 50200803e56STom N Harris * Find pages containing a metadata key. 503d5b23302STom N Harris * 50400803e56STom N Harris * The metadata values are compared as case-sensitive strings. Pass a 50500803e56STom N Harris * callback function that returns true or false to use a different 506b00bd361STom N Harris * comparison function. The function will be called with the $value being 507b00bd361STom N Harris * searched for as the first argument, and the word in the index as the 508*1538718dSTom N Harris * second argument. The function preg_match can be used directly if the 509*1538718dSTom N Harris * values are regexes. 510d5b23302STom N Harris * 51100803e56STom N Harris * @param string $key name of the metadata key to look for 512*1538718dSTom N Harris * @param string $value search term to look for, must be a string or array of strings 51300803e56STom N Harris * @param callback $func comparison function 514b00bd361STom N Harris * @return array lists with page names, keys are query values if $value is array 51500803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 516cd763a5bSMichael Hamann * @author Michael Hamann <michael@content-space.de> 51700803e56STom N Harris */ 518b00bd361STom N Harris public function lookupKey($key, &$value, $func=null) { 519f078bb00STom N Harris if (!is_array($value)) 520f078bb00STom N Harris $value_array = array($value); 521f078bb00STom N Harris else 522f078bb00STom N Harris $value_array =& $value; 523cd763a5bSMichael Hamann 524c1209673STom N Harris // the matching ids for the provided value(s) 525c1209673STom N Harris $value_ids = array(); 526c1209673STom N Harris 527c1209673STom N Harris $metaname = idx_cleanName($key); 528c1209673STom N Harris 529c1209673STom N Harris // get all words in order to search the matching ids 530c1209673STom N Harris if ($key == 'title') { 531c1209673STom N Harris $words = $this->_getIndex('title', ''); 532c1209673STom N Harris } else { 533c1209673STom N Harris $words = $this->_getIndex($metaname, '_w'); 534c1209673STom N Harris } 535c1209673STom N Harris 536f078bb00STom N Harris if (!is_null($func)) { 537*1538718dSTom N Harris foreach ($value_array as $val) { 538f078bb00STom N Harris foreach ($words as $i => $word) { 539*1538718dSTom N Harris if (call_user_func_array($func, array($val, $word))) 540c1209673STom N Harris $value_ids[$i][] = $val; 541f078bb00STom N Harris } 542f078bb00STom N Harris } 543f078bb00STom N Harris } else { 544f078bb00STom N Harris foreach ($value_array as $val) { 545f078bb00STom N Harris $xval = $val; 546f078bb00STom N Harris $caret = false; 547f078bb00STom N Harris $dollar = false; 548f078bb00STom N Harris // check for wildcards 549f078bb00STom N Harris if (substr($xval, 0, 1) == '*') { 550f078bb00STom N Harris $xval = substr($xval, 1); 551f078bb00STom N Harris $caret = '^'; 552f078bb00STom N Harris } 553f078bb00STom N Harris if (substr($xval, -1, 1) == '*') { 554f078bb00STom N Harris $xval = substr($xval, 0, -1); 555f078bb00STom N Harris $dollar = '$'; 556f078bb00STom N Harris } 557f078bb00STom N Harris if ($caret || $dollar) { 558f078bb00STom N Harris $re = $caret.preg_quote($xval, '/').$dollar; 559f078bb00STom N Harris foreach(array_keys(preg_grep('/'.$re.'/', $words)) as $i) 560c1209673STom N Harris $value_ids[$i][] = $val; 561cd763a5bSMichael Hamann } else { 562f078bb00STom N Harris if (($i = array_search($val, $words)) !== false) 563c1209673STom N Harris $value_ids[$i][] = $val; 564cd763a5bSMichael Hamann } 565cd763a5bSMichael Hamann } 566cd763a5bSMichael Hamann } 567cd763a5bSMichael Hamann 568cd763a5bSMichael Hamann unset($words); // free the used memory 569cd763a5bSMichael Hamann 5707233c152SMichael Hamann // initialize the result so it won't be null 571c1209673STom N Harris $result = array(); 5727233c152SMichael Hamann foreach ($value_array as $val) { 5737233c152SMichael Hamann $result[$val] = array(); 5747233c152SMichael Hamann } 5757233c152SMichael Hamann 576cd763a5bSMichael Hamann $page_idx = $this->_getIndex('page', ''); 577cd763a5bSMichael Hamann 578c1209673STom N Harris // Special handling for titles 579c1209673STom N Harris if ($key == 'title') { 580c1209673STom N Harris foreach ($value_ids as $pid => $val_list) { 581c1209673STom N Harris $page = $page_idx[$pid]; 582c1209673STom N Harris foreach ($val_list as $val) { 583c1209673STom N Harris $result[$val][] = $page; 584c1209673STom N Harris } 585c1209673STom N Harris } 586c1209673STom N Harris } else { 587c1209673STom N Harris // load all lines and pages so the used lines can be taken and matched with the pages 588c1209673STom N Harris $lines = $this->_getIndex($metaname, '_i'); 589c1209673STom N Harris 590c1209673STom N Harris foreach ($value_ids as $value_id => $val_list) { 591cd763a5bSMichael Hamann // parse the tuples of the form page_id*1:page2_id*1 and so on, return value 592cd763a5bSMichael Hamann // is an array with page_id => 1, page2_id => 1 etc. so take the keys only 593c1209673STom N Harris $pages = array_keys($this->_parseTuples($page_idx, $lines[$value_id])); 594c1209673STom N Harris foreach ($val_list as $val) { 595c1209673STom N Harris $result[$val] = array_merge($result[$val], $pages); 596c1209673STom N Harris } 597c1209673STom N Harris } 598cd763a5bSMichael Hamann } 599f078bb00STom N Harris if (!is_array($value)) $result = $result[$value]; 600cd763a5bSMichael Hamann return $result; 60100803e56STom N Harris } 60200803e56STom N Harris 60300803e56STom N Harris /** 60400803e56STom N Harris * Find the index ID of each search term. 60500803e56STom N Harris * 60600803e56STom N Harris * The query terms should only contain valid characters, with a '*' at 60700803e56STom N Harris * either the beginning or end of the word (or both). 60800803e56STom N Harris * The $result parameter can be used to merge the index locations with 60900803e56STom N Harris * the appropriate query term. 61000803e56STom N Harris * 6119b41be24STom N Harris * @param arrayref $words The query terms. 61200803e56STom N Harris * @param arrayref $result Set to word => array("length*id" ...) 613d5b23302STom N Harris * @return array Set to length => array(id ...) 614d5b23302STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 615d5b23302STom N Harris */ 6169b41be24STom N Harris private function _getIndexWords(&$words, &$result) { 617d5b23302STom N Harris $tokens = array(); 618d5b23302STom N Harris $tokenlength = array(); 619d5b23302STom N Harris $tokenwild = array(); 620d5b23302STom N Harris foreach ($words as $word) { 621d5b23302STom N Harris $result[$word] = array(); 62200803e56STom N Harris $caret = false; 62300803e56STom N Harris $dollar = false; 624d5b23302STom N Harris $xword = $word; 625d5b23302STom N Harris $wlen = wordlen($word); 626d5b23302STom N Harris 627d5b23302STom N Harris // check for wildcards 628d5b23302STom N Harris if (substr($xword, 0, 1) == '*') { 629d5b23302STom N Harris $xword = substr($xword, 1); 630f078bb00STom N Harris $caret = '^'; 631d5b23302STom N Harris $wlen -= 1; 632d5b23302STom N Harris } 633d5b23302STom N Harris if (substr($xword, -1, 1) == '*') { 634d5b23302STom N Harris $xword = substr($xword, 0, -1); 635f078bb00STom N Harris $dollar = '$'; 636d5b23302STom N Harris $wlen -= 1; 637d5b23302STom N Harris } 63800803e56STom N Harris if ($wlen < IDX_MINWORDLENGTH && !$caret && !$dollar && !is_numeric($xword)) 63900803e56STom N Harris continue; 64000803e56STom N Harris if (!isset($tokens[$xword])) 641d5b23302STom N Harris $tokenlength[$wlen][] = $xword; 64200803e56STom N Harris if ($caret || $dollar) { 643f078bb00STom N Harris $re = $caret.preg_quote($xword, '/').$dollar; 64400803e56STom N Harris $tokens[$xword][] = array($word, '/'.$re.'/'); 64500803e56STom N Harris if (!isset($tokenwild[$xword])) 64600803e56STom N Harris $tokenwild[$xword] = $wlen; 64700803e56STom N Harris } else { 648d5b23302STom N Harris $tokens[$xword][] = array($word, null); 649d5b23302STom N Harris } 65000803e56STom N Harris } 651d5b23302STom N Harris asort($tokenwild); 65200803e56STom N Harris // $tokens = array( base word => array( [ query term , regexp ] ... ) ... ) 653d5b23302STom N Harris // $tokenlength = array( base word length => base word ... ) 654d5b23302STom N Harris // $tokenwild = array( base word => base word length ... ) 655d5b23302STom N Harris $length_filter = empty($tokenwild) ? $tokenlength : min(array_keys($tokenlength)); 65600803e56STom N Harris $indexes_known = $this->_indexLengths($length_filter); 657d5b23302STom N Harris if (!empty($tokenwild)) sort($indexes_known); 658d5b23302STom N Harris // get word IDs 659d5b23302STom N Harris $wids = array(); 660d5b23302STom N Harris foreach ($indexes_known as $ixlen) { 66100803e56STom N Harris $word_idx = $this->_getIndex('w', $ixlen); 662d5b23302STom N Harris // handle exact search 663d5b23302STom N Harris if (isset($tokenlength[$ixlen])) { 664d5b23302STom N Harris foreach ($tokenlength[$ixlen] as $xword) { 66500803e56STom N Harris $wid = array_search($xword, $word_idx); 66600803e56STom N Harris if ($wid !== false) { 667d5b23302STom N Harris $wids[$ixlen][] = $wid; 668d5b23302STom N Harris foreach ($tokens[$xword] as $w) 669d5b23302STom N Harris $result[$w[0]][] = "$ixlen*$wid"; 670d5b23302STom N Harris } 671d5b23302STom N Harris } 672d5b23302STom N Harris } 673d5b23302STom N Harris // handle wildcard search 674d5b23302STom N Harris foreach ($tokenwild as $xword => $wlen) { 675d5b23302STom N Harris if ($wlen >= $ixlen) break; 676d5b23302STom N Harris foreach ($tokens[$xword] as $w) { 677d5b23302STom N Harris if (is_null($w[1])) continue; 678d5b23302STom N Harris foreach(array_keys(preg_grep($w[1], $word_idx)) as $wid) { 679d5b23302STom N Harris $wids[$ixlen][] = $wid; 680d5b23302STom N Harris $result[$w[0]][] = "$ixlen*$wid"; 681d5b23302STom N Harris } 682d5b23302STom N Harris } 683d5b23302STom N Harris } 684d5b23302STom N Harris } 685d5b23302STom N Harris return $wids; 686d5b23302STom N Harris } 687d5b23302STom N Harris 688d5b23302STom N Harris /** 68900803e56STom N Harris * Return a list of all pages 690c1209673STom N Harris * Warning: pages may not exist! 691488dd6ceSAndreas Gohr * 69200803e56STom N Harris * @param string $key list only pages containing the metadata key (optional) 69300803e56STom N Harris * @return array list of page names 69400803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 69500803e56STom N Harris */ 69600803e56STom N Harris public function getPages($key=null) { 69700803e56STom N Harris $page_idx = $this->_getIndex('page', ''); 69800803e56STom N Harris if (is_null($key)) return $page_idx; 699c1209673STom N Harris 700c1209673STom N Harris $metaname = idx_cleanName($key); 701c1209673STom N Harris 702c1209673STom N Harris // Special handling for titles 703c1209673STom N Harris if ($key == 'title') { 704c1209673STom N Harris $title_idx = $this->_getIndex('title', ''); 705c1209673STom N Harris array_splice($page_idx, count($title_idx)); 706c1209673STom N Harris foreach ($title_idx as $i => $title) 707c1209673STom N Harris if ($title === "") unset($page_idx[$i]); 708675bf41fSTom N Harris return array_values($page_idx); 709c1209673STom N Harris } 710c1209673STom N Harris 711c1209673STom N Harris $pages = array(); 712c1209673STom N Harris $lines = $this->_getIndex($metaname, '_i'); 713c1209673STom N Harris foreach ($lines as $line) { 714c1209673STom N Harris $pages = array_merge($pages, $this->_parseTuples($page_idx, $line)); 715c1209673STom N Harris } 716c1209673STom N Harris return array_keys($pages); 71700803e56STom N Harris } 71800803e56STom N Harris 71900803e56STom N Harris /** 72000803e56STom N Harris * Return a list of words sorted by number of times used 72100803e56STom N Harris * 72200803e56STom N Harris * @param int $min bottom frequency threshold 72300803e56STom N Harris * @param int $max upper frequency limit. No limit if $max<$min 724b8c040dbSTom N Harris * @param int $length minimum length of words to count 72500803e56STom N Harris * @param string $key metadata key to list. Uses the fulltext index if not given 72600803e56STom N Harris * @return array list of words as the keys and frequency as values 72700803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 72800803e56STom N Harris */ 729b8c040dbSTom N Harris public function histogram($min=1, $max=0, $minlen=3, $key=null) { 730175193d2STom N Harris if ($min < 1) 731175193d2STom N Harris $min = 1; 732175193d2STom N Harris if ($max < $min) 733175193d2STom N Harris $max = 0; 734175193d2STom N Harris 735175193d2STom N Harris $result = array(); 736175193d2STom N Harris 737175193d2STom N Harris if ($key == 'title') { 738175193d2STom N Harris $index = $this->_getIndex('title', ''); 739175193d2STom N Harris $index = array_count_values($index); 740175193d2STom N Harris foreach ($index as $val => $cnt) { 741b8c040dbSTom N Harris if ($cnt >= $min && (!$max || $cnt <= $max) && strlen($val) >= $minlen) 742175193d2STom N Harris $result[$val] = $cnt; 743175193d2STom N Harris } 744175193d2STom N Harris } 745175193d2STom N Harris elseif (!is_null($key)) { 746175193d2STom N Harris $metaname = idx_cleanName($key); 747175193d2STom N Harris $index = $this->_getIndex($metaname.'_i', ''); 748175193d2STom N Harris $val_idx = array(); 749175193d2STom N Harris foreach ($index as $wid => $line) { 750175193d2STom N Harris $freq = $this->_countTuples($line); 751b8c040dbSTom N Harris if ($freq >= $min && (!$max || $freq <= $max) && strlen($val) >= $minlen) 752175193d2STom N Harris $val_idx[$wid] = $freq; 753175193d2STom N Harris } 754175193d2STom N Harris if (!empty($val_idx)) { 755175193d2STom N Harris $words = $this->_getIndex($metaname.'_w', ''); 756175193d2STom N Harris foreach ($val_idx as $wid => $freq) 757175193d2STom N Harris $result[$words[$wid]] = $freq; 758175193d2STom N Harris } 759175193d2STom N Harris } 760175193d2STom N Harris else { 761175193d2STom N Harris $lengths = idx_listIndexLengths(); 762175193d2STom N Harris foreach ($lengths as $length) { 763b8c040dbSTom N Harris if ($length < $minlen) continue; 764175193d2STom N Harris $index = $this->_getIndex('i', $length); 765175193d2STom N Harris $words = null; 766175193d2STom N Harris foreach ($index as $wid => $line) { 767175193d2STom N Harris $freq = $this->_countTuples($line); 768175193d2STom N Harris if ($freq >= $min && (!$max || $freq <= $max)) { 769175193d2STom N Harris if ($words === null) 770175193d2STom N Harris $words = $this->_getIndex('w', $length); 771175193d2STom N Harris $result[$words[$wid]] = $freq; 772175193d2STom N Harris } 773175193d2STom N Harris } 774175193d2STom N Harris } 775175193d2STom N Harris } 776175193d2STom N Harris 777175193d2STom N Harris arsort($result); 778175193d2STom N Harris return $result; 77900803e56STom N Harris } 78000803e56STom N Harris 78100803e56STom N Harris /** 78200803e56STom N Harris * Lock the indexer. 78300803e56STom N Harris * 78400803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 78500803e56STom N Harris */ 78600803e56STom N Harris private function _lock() { 78700803e56STom N Harris global $conf; 78800803e56STom N Harris $status = true; 789f77fc90dSMichael Hamann $run = 0; 79000803e56STom N Harris $lock = $conf['lockdir'].'/_indexer.lock'; 79100803e56STom N Harris while (!@mkdir($lock, $conf['dmode'])) { 79200803e56STom N Harris usleep(50); 793f77fc90dSMichael Hamann if(is_dir($lock) && time()-@filemtime($lock) > 60*5){ 794f77fc90dSMichael Hamann // looks like a stale lock - remove it 795f77fc90dSMichael Hamann if (!@rmdir($lock)) { 796f77fc90dSMichael Hamann $status = "removing the stale lock failed"; 797f77fc90dSMichael Hamann return false; 79800803e56STom N Harris } else { 799f77fc90dSMichael Hamann $status = "stale lock removed"; 800f77fc90dSMichael Hamann } 801f77fc90dSMichael Hamann }elseif($run++ == 1000){ 802f77fc90dSMichael Hamann // we waited 5 seconds for that lock 80300803e56STom N Harris return false; 80400803e56STom N Harris } 80500803e56STom N Harris } 80600803e56STom N Harris if ($conf['dperm']) 80700803e56STom N Harris chmod($lock, $conf['dperm']); 80800803e56STom N Harris return $status; 80900803e56STom N Harris } 81000803e56STom N Harris 81100803e56STom N Harris /** 81200803e56STom N Harris * Release the indexer lock. 81300803e56STom N Harris * 81400803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 81500803e56STom N Harris */ 81600803e56STom N Harris private function _unlock() { 81700803e56STom N Harris global $conf; 81800803e56STom N Harris @rmdir($conf['lockdir'].'/_indexer.lock'); 81900803e56STom N Harris return true; 82000803e56STom N Harris } 82100803e56STom N Harris 82200803e56STom N Harris /** 82300803e56STom N Harris * Retrieve the entire index. 82400803e56STom N Harris * 82500803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 82600803e56STom N Harris */ 82700803e56STom N Harris private function _getIndex($idx, $suffix) { 82800803e56STom N Harris global $conf; 82900803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; 830d64516f5SMichael Hamann if (!@file_exists($fn)) return array(); 831d64516f5SMichael Hamann return file($fn, FILE_IGNORE_NEW_LINES); 83200803e56STom N Harris } 83300803e56STom N Harris 83400803e56STom N Harris /** 83500803e56STom N Harris * Replace the contents of the index with an array. 83600803e56STom N Harris * 83700803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 83800803e56STom N Harris */ 83900803e56STom N Harris private function _saveIndex($idx, $suffix, &$lines) { 84000803e56STom N Harris global $conf; 84100803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix; 84200803e56STom N Harris $fh = @fopen($fn.'.tmp', 'w'); 84300803e56STom N Harris if (!$fh) return false; 84400803e56STom N Harris fwrite($fh, join("\n", $lines)); 84500803e56STom N Harris fclose($fh); 84600803e56STom N Harris if (isset($conf['fperm'])) 84700803e56STom N Harris chmod($fn.'.tmp', $conf['fperm']); 84800803e56STom N Harris io_rename($fn.'.tmp', $fn.'.idx'); 84900803e56STom N Harris if ($suffix !== '') 85000803e56STom N Harris $this->_cacheIndexDir($idx, $suffix, empty($lines)); 85100803e56STom N Harris return true; 85200803e56STom N Harris } 85300803e56STom N Harris 85400803e56STom N Harris /** 85500803e56STom N Harris * Retrieve a line from the index. 85600803e56STom N Harris * 85700803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 85800803e56STom N Harris */ 85900803e56STom N Harris private function _getIndexKey($idx, $suffix, $id) { 86000803e56STom N Harris global $conf; 86100803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; 86200803e56STom N Harris if (!@file_exists($fn)) return ''; 86300803e56STom N Harris $fh = @fopen($fn, 'r'); 86400803e56STom N Harris if (!$fh) return ''; 86500803e56STom N Harris $ln = -1; 86600803e56STom N Harris while (($line = fgets($fh)) !== false) { 86700803e56STom N Harris if (++$ln == $id) break; 86800803e56STom N Harris } 86900803e56STom N Harris fclose($fh); 87000803e56STom N Harris return rtrim((string)$line); 87100803e56STom N Harris } 87200803e56STom N Harris 87300803e56STom N Harris /** 87400803e56STom N Harris * Write a line into the index. 87500803e56STom N Harris * 87600803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 87700803e56STom N Harris */ 87800803e56STom N Harris private function _saveIndexKey($idx, $suffix, $id, $line) { 87900803e56STom N Harris global $conf; 88000803e56STom N Harris if (substr($line, -1) != "\n") 88100803e56STom N Harris $line .= "\n"; 88200803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix; 88300803e56STom N Harris $fh = @fopen($fn.'.tmp', 'w'); 88400803e56STom N Harris if (!fh) return false; 88500803e56STom N Harris $ih = @fopen($fn.'.idx', 'r'); 88600803e56STom N Harris if ($ih) { 88700803e56STom N Harris $ln = -1; 88800803e56STom N Harris while (($curline = fgets($ih)) !== false) { 88900803e56STom N Harris fwrite($fh, (++$ln == $id) ? $line : $curline); 89000803e56STom N Harris } 8914373c7b5SMichael Hamann if ($id > $ln) { 8924373c7b5SMichael Hamann while ($id > ++$ln) 8934373c7b5SMichael Hamann fwrite($fh, "\n"); 89400803e56STom N Harris fwrite($fh, $line); 8954373c7b5SMichael Hamann } 89600803e56STom N Harris fclose($ih); 89700803e56STom N Harris } else { 8984373c7b5SMichael Hamann $ln = -1; 8994373c7b5SMichael Hamann while ($id > ++$ln) 9004373c7b5SMichael Hamann fwrite($fh, "\n"); 90100803e56STom N Harris fwrite($fh, $line); 90200803e56STom N Harris } 90300803e56STom N Harris fclose($fh); 90400803e56STom N Harris if (isset($conf['fperm'])) 90500803e56STom N Harris chmod($fn.'.tmp', $conf['fperm']); 90600803e56STom N Harris io_rename($fn.'.tmp', $fn.'.idx'); 90700803e56STom N Harris if ($suffix !== '') 90800803e56STom N Harris $this->_cacheIndexDir($idx, $suffix); 90900803e56STom N Harris return true; 91000803e56STom N Harris } 91100803e56STom N Harris 91200803e56STom N Harris /** 91300803e56STom N Harris * Retrieve or insert a value in the index. 91400803e56STom N Harris * 91500803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 91600803e56STom N Harris */ 91700803e56STom N Harris private function _addIndexKey($idx, $suffix, $value) { 91800803e56STom N Harris $index = $this->_getIndex($idx, $suffix); 91900803e56STom N Harris $id = array_search($value, $index); 92000803e56STom N Harris if ($id === false) { 92100803e56STom N Harris $id = count($index); 92200803e56STom N Harris $index[$id] = $value; 92300803e56STom N Harris if (!$this->_saveIndex($idx, $suffix, $index)) { 92400803e56STom N Harris trigger_error("Failed to write $idx index", E_USER_ERROR); 92500803e56STom N Harris return false; 92600803e56STom N Harris } 92700803e56STom N Harris } 92800803e56STom N Harris return $id; 92900803e56STom N Harris } 93000803e56STom N Harris 93100803e56STom N Harris private function _cacheIndexDir($idx, $suffix, $delete=false) { 93200803e56STom N Harris global $conf; 93300803e56STom N Harris if ($idx == 'i') 93400803e56STom N Harris $cachename = $conf['indexdir'].'/lengths'; 93500803e56STom N Harris else 93600803e56STom N Harris $cachename = $conf['indexdir'].'/'.$idx.'lengths'; 93700803e56STom N Harris $lengths = @file($cachename.'.idx', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 93800803e56STom N Harris if ($lengths === false) $lengths = array(); 93900803e56STom N Harris $old = array_search((string)$suffix, $lengths); 94000803e56STom N Harris if (empty($lines)) { 94100803e56STom N Harris if ($old === false) return; 94200803e56STom N Harris unset($lengths[$old]); 94300803e56STom N Harris } else { 94400803e56STom N Harris if ($old !== false) return; 94500803e56STom N Harris $lengths[] = $suffix; 94600803e56STom N Harris sort($lengths); 94700803e56STom N Harris } 94800803e56STom N Harris $fh = @fopen($cachename.'.tmp', 'w'); 94900803e56STom N Harris if (!$fh) { 95000803e56STom N Harris trigger_error("Failed to write index cache", E_USER_ERROR); 95100803e56STom N Harris return; 95200803e56STom N Harris } 95300803e56STom N Harris @fwrite($fh, implode("\n", $lengths)); 95400803e56STom N Harris @fclose($fh); 95500803e56STom N Harris if (isset($conf['fperm'])) 95600803e56STom N Harris chmod($cachename.'.tmp', $conf['fperm']); 95700803e56STom N Harris io_rename($cachename.'.tmp', $cachename.'.idx'); 95800803e56STom N Harris } 95900803e56STom N Harris 96000803e56STom N Harris /** 96100803e56STom N Harris * Get the list of lengths indexed in the wiki. 96200803e56STom N Harris * 96300803e56STom N Harris * Read the index directory or a cache file and returns 96400803e56STom N Harris * a sorted array of lengths of the words used in the wiki. 96500803e56STom N Harris * 96600803e56STom N Harris * @author YoBoY <yoboy.leguesh@gmail.com> 96700803e56STom N Harris */ 96800803e56STom N Harris private function _listIndexLengths() { 96900803e56STom N Harris global $conf; 97000803e56STom N Harris $cachename = $conf['indexdir'].'/lengths'; 97100803e56STom N Harris clearstatcache(); 97200803e56STom N Harris if (@file_exists($cachename.'.idx')) { 97300803e56STom N Harris $lengths = @file($cachename.'.idx', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 97400803e56STom N Harris if ($lengths !== false) { 97500803e56STom N Harris $idx = array(); 97600803e56STom N Harris foreach ($lengths as $length) 97700803e56STom N Harris $idx[] = (int)$length; 97800803e56STom N Harris return $idx; 97900803e56STom N Harris } 98000803e56STom N Harris } 98100803e56STom N Harris 98200803e56STom N Harris $dir = @opendir($conf['indexdir']); 98300803e56STom N Harris if ($dir === false) 98400803e56STom N Harris return array(); 98500803e56STom N Harris $lengths[] = array(); 98600803e56STom N Harris while (($f = readdir($dir)) !== false) { 98700803e56STom N Harris if (substr($f, 0, 1) == 'i' && substr($f, -4) == '.idx') { 98800803e56STom N Harris $i = substr($f, 1, -4); 98900803e56STom N Harris if (is_numeric($i)) 99000803e56STom N Harris $lengths[] = (int)$i; 99100803e56STom N Harris } 99200803e56STom N Harris } 99300803e56STom N Harris closedir($dir); 99400803e56STom N Harris sort($lengths); 99500803e56STom N Harris // save this in a file 99600803e56STom N Harris $fh = @fopen($cachename.'.tmp', 'w'); 99700803e56STom N Harris if (!$fh) { 99800803e56STom N Harris trigger_error("Failed to write index cache", E_USER_ERROR); 99900803e56STom N Harris return; 100000803e56STom N Harris } 100100803e56STom N Harris @fwrite($fh, implode("\n", $lengths)); 100200803e56STom N Harris @fclose($fh); 100300803e56STom N Harris if (isset($conf['fperm'])) 100400803e56STom N Harris chmod($cachename.'.tmp', $conf['fperm']); 100500803e56STom N Harris io_rename($cachename.'.tmp', $cachename.'.idx'); 100600803e56STom N Harris 100700803e56STom N Harris return $lengths; 100800803e56STom N Harris } 100900803e56STom N Harris 101000803e56STom N Harris /** 101100803e56STom N Harris * Get the word lengths that have been indexed. 101200803e56STom N Harris * 101300803e56STom N Harris * Reads the index directory and returns an array of lengths 101400803e56STom N Harris * that there are indices for. 101500803e56STom N Harris * 101600803e56STom N Harris * @author YoBoY <yoboy.leguesh@gmail.com> 101700803e56STom N Harris */ 101800803e56STom N Harris private function _indexLengths($filter) { 101900803e56STom N Harris global $conf; 102000803e56STom N Harris $idx = array(); 102100803e56STom N Harris if (is_array($filter)) { 102200803e56STom N Harris // testing if index files exist only 102300803e56STom N Harris $path = $conf['indexdir']."/i"; 102400803e56STom N Harris foreach ($filter as $key => $value) { 102500803e56STom N Harris if (@file_exists($path.$key.'.idx')) 102600803e56STom N Harris $idx[] = $key; 102700803e56STom N Harris } 102800803e56STom N Harris } else { 102900803e56STom N Harris $lengths = idx_listIndexLengths(); 103000803e56STom N Harris foreach ($lengths as $key => $length) { 103100803e56STom N Harris // keep all the values equal or superior 103200803e56STom N Harris if ((int)$length >= (int)$filter) 103300803e56STom N Harris $idx[] = $length; 103400803e56STom N Harris } 103500803e56STom N Harris } 103600803e56STom N Harris return $idx; 103700803e56STom N Harris } 103800803e56STom N Harris 103900803e56STom N Harris /** 104000803e56STom N Harris * Insert or replace a tuple in a line. 104100803e56STom N Harris * 104200803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 104300803e56STom N Harris */ 104400803e56STom N Harris private function _updateTuple($line, $id, $count) { 104500803e56STom N Harris $newLine = $line; 104600803e56STom N Harris if ($newLine !== '') 104700803e56STom N Harris $newLine = preg_replace('/(^|:)'.preg_quote($id,'/').'\*\d*/', '', $newLine); 104800803e56STom N Harris $newLine = trim($newLine, ':'); 104900803e56STom N Harris if ($count) { 1050d64516f5SMichael Hamann if (strlen($newLine) > 0) 105100803e56STom N Harris return "$id*$count:".$newLine; 105200803e56STom N Harris else 105300803e56STom N Harris return "$id*$count".$newLine; 105400803e56STom N Harris } 105500803e56STom N Harris return $newLine; 105600803e56STom N Harris } 105700803e56STom N Harris 105800803e56STom N Harris /** 105900803e56STom N Harris * Split a line into an array of tuples. 106000803e56STom N Harris * 106100803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 106200803e56STom N Harris * @author Andreas Gohr <andi@splitbrain.org> 106300803e56STom N Harris */ 106400803e56STom N Harris private function _parseTuples(&$keys, $line) { 106500803e56STom N Harris $result = array(); 106600803e56STom N Harris if ($line == '') return $result; 106700803e56STom N Harris $parts = explode(':', $line); 106800803e56STom N Harris foreach ($parts as $tuple) { 1069675bf41fSTom N Harris if ($tuple === '') continue; 107000803e56STom N Harris list($key, $cnt) = explode('*', $tuple); 1071d64516f5SMichael Hamann if (!$cnt) continue; 107200803e56STom N Harris $key = $keys[$key]; 107300803e56STom N Harris if (!$key) continue; 107400803e56STom N Harris $result[$key] = $cnt; 107500803e56STom N Harris } 107600803e56STom N Harris return $result; 107700803e56STom N Harris } 1078175193d2STom N Harris 1079175193d2STom N Harris /** 1080175193d2STom N Harris * Sum the counts in a list of tuples. 1081175193d2STom N Harris * 1082175193d2STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 1083175193d2STom N Harris */ 1084175193d2STom N Harris private function _countTuples($line) { 1085175193d2STom N Harris $freq = 0; 1086175193d2STom N Harris $parts = explode(':', $line); 1087175193d2STom N Harris foreach ($parts as $tuple) { 1088675bf41fSTom N Harris if ($tuple === '') continue; 1089175193d2STom N Harris list($pid, $cnt) = explode('*', $tuple); 1090175193d2STom N Harris $freq += (int)$cnt; 1091175193d2STom N Harris } 1092175193d2STom N Harris return $freq; 1093175193d2STom N Harris } 109400803e56STom N Harris} 109500803e56STom N Harris 109600803e56STom N Harris/** 109700803e56STom N Harris * Create an instance of the indexer. 109800803e56STom N Harris * 109900803e56STom N Harris * @return object a Doku_Indexer 110000803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 110100803e56STom N Harris */ 11029b41be24STom N Harrisfunction idx_get_indexer() { 110300803e56STom N Harris static $Indexer = null; 110400803e56STom N Harris if (is_null($Indexer)) { 110500803e56STom N Harris $Indexer = new Doku_Indexer(); 110600803e56STom N Harris } 110700803e56STom N Harris return $Indexer; 110800803e56STom N Harris} 110900803e56STom N Harris 111000803e56STom N Harris/** 111100803e56STom N Harris * Returns words that will be ignored. 111200803e56STom N Harris * 111300803e56STom N Harris * @return array list of stop words 111400803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 111500803e56STom N Harris */ 111600803e56STom N Harrisfunction & idx_get_stopwords() { 111700803e56STom N Harris static $stopwords = null; 111800803e56STom N Harris if (is_null($stopwords)) { 111900803e56STom N Harris global $conf; 112000803e56STom N Harris $swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt'; 112100803e56STom N Harris if(@file_exists($swfile)){ 112200803e56STom N Harris $stopwords = file($swfile, FILE_IGNORE_NEW_LINES); 112300803e56STom N Harris }else{ 112400803e56STom N Harris $stopwords = array(); 112500803e56STom N Harris } 112600803e56STom N Harris } 112700803e56STom N Harris return $stopwords; 112800803e56STom N Harris} 112900803e56STom N Harris 113000803e56STom N Harris/** 113100803e56STom N Harris * Adds/updates the search index for the given page 113200803e56STom N Harris * 113300803e56STom N Harris * Locking is handled internally. 113400803e56STom N Harris * 113500803e56STom N Harris * @param string $page name of the page to index 11369b41be24STom N Harris * @param boolean $verbose print status messages 113700803e56STom N Harris * @return boolean the function completed successfully 113800803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 113900803e56STom N Harris */ 11409b41be24STom N Harrisfunction idx_addPage($page, $verbose=false) { 11419b41be24STom N Harris // check if indexing needed 11429b41be24STom N Harris $idxtag = metaFN($page,'.indexed'); 11439b41be24STom N Harris if(@file_exists($idxtag)){ 11449b41be24STom N Harris if(trim(io_readFile($idxtag)) == idx_get_version()){ 11459b41be24STom N Harris $last = @filemtime($idxtag); 11469b41be24STom N Harris if($last > @filemtime(wikiFN($ID))){ 11479b41be24STom N Harris if ($verbose) print("Indexer: index for $page up to date".DOKU_LF); 11489b41be24STom N Harris return false; 11499b41be24STom N Harris } 11509b41be24STom N Harris } 11519b41be24STom N Harris } 11529b41be24STom N Harris 1153bbc85ee4STom N Harris if (!page_exists($page)) { 1154bbc85ee4STom N Harris if (!@file_exists($idxtag)) { 1155bbc85ee4STom N Harris if ($verbose) print("Indexer: $page does not exist, ignoring".DOKU_LF); 1156bbc85ee4STom N Harris return false; 1157bbc85ee4STom N Harris } 1158bbc85ee4STom N Harris $Indexer = idx_get_indexer(); 1159bbc85ee4STom N Harris $result = $Indexer->deletePage($page); 1160bbc85ee4STom N Harris if ($result === "locked") { 1161bbc85ee4STom N Harris if ($verbose) print("Indexer: locked".DOKU_LF); 1162bbc85ee4STom N Harris return false; 1163bbc85ee4STom N Harris } 1164bbc85ee4STom N Harris @unlink($idxtag); 1165bbc85ee4STom N Harris return $result; 1166bbc85ee4STom N Harris } 1167bbc85ee4STom N Harris $indexenabled = p_get_metadata($page, 'internal index', false); 1168bbc85ee4STom N Harris if ($indexenabled === false) { 1169bbc85ee4STom N Harris $result = false; 1170bbc85ee4STom N Harris if (@file_exists($idxtag)) { 1171bbc85ee4STom N Harris $Indexer = idx_get_indexer(); 1172bbc85ee4STom N Harris $result = $Indexer->deletePage($page); 1173bbc85ee4STom N Harris if ($result === "locked") { 1174bbc85ee4STom N Harris if ($verbose) print("Indexer: locked".DOKU_LF); 1175bbc85ee4STom N Harris return false; 1176bbc85ee4STom N Harris } 1177bbc85ee4STom N Harris @unlink($idxtag); 1178bbc85ee4STom N Harris } 1179bbc85ee4STom N Harris if ($verbose) print("Indexer: index disabled for $page".DOKU_LF); 1180bbc85ee4STom N Harris return $result; 1181bbc85ee4STom N Harris } 1182bbc85ee4STom N Harris 118300803e56STom N Harris $body = ''; 118400803e56STom N Harris $data = array($page, $body); 118500803e56STom N Harris $evt = new Doku_Event('INDEXER_PAGE_ADD', $data); 118600803e56STom N Harris if ($evt->advise_before()) $data[1] = $data[1] . " " . rawWiki($page); 118700803e56STom N Harris $evt->advise_after(); 118800803e56STom N Harris unset($evt); 118900803e56STom N Harris list($page,$body) = $data; 119000803e56STom N Harris 11919b41be24STom N Harris $Indexer = idx_get_indexer(); 11929b41be24STom N Harris $result = $Indexer->addPageWords($page, $body); 1193e1e1a7e0SMichael Hamann if ($result === "locked") { 11949b41be24STom N Harris if ($verbose) print("Indexer: locked".DOKU_LF); 11959b41be24STom N Harris return false; 11969b41be24STom N Harris } 1197320f489aSMichael Hamann 1198320f489aSMichael Hamann if ($result) { 1199320f489aSMichael Hamann $data = array('page' => $page, 'metadata' => array()); 1200320f489aSMichael Hamann 1201bbc85ee4STom N Harris $data['metadata']['title'] = p_get_metadata($page, 'title', false); 1202bbc85ee4STom N Harris if (($references = p_get_metadata($page, 'relation references', false)) !== null) 1203320f489aSMichael Hamann $data['metadata']['relation_references'] = array_keys($references); 1204320f489aSMichael Hamann 1205320f489aSMichael Hamann $evt = new Doku_Event('INDEXER_METADATA_INDEX', $data); 1206320f489aSMichael Hamann if ($evt->advise_before()) { 1207320f489aSMichael Hamann $result = $Indexer->addMetaKeys($page, $data['metadata']); 1208320f489aSMichael Hamann if ($result === "locked") { 1209320f489aSMichael Hamann if ($verbose) print("Indexer: locked".DOKU_LF); 1210320f489aSMichael Hamann return false; 1211320f489aSMichael Hamann } 1212320f489aSMichael Hamann } 1213320f489aSMichael Hamann $evt->advise_after(); 1214320f489aSMichael Hamann unset($evt); 1215320f489aSMichael Hamann } 1216320f489aSMichael Hamann 12179b41be24STom N Harris if ($result) 12189b41be24STom N Harris io_saveFile(metaFN($page,'.indexed'), idx_get_version()); 12199b41be24STom N Harris if ($verbose) { 12209b41be24STom N Harris print("Indexer: finished".DOKU_LF); 12219b41be24STom N Harris return true; 12229b41be24STom N Harris } 12239b41be24STom N Harris return $result; 122400803e56STom N Harris} 122500803e56STom N Harris 122600803e56STom N Harris/** 122700803e56STom N Harris * Find tokens in the fulltext index 122800803e56STom N Harris * 122900803e56STom N Harris * Takes an array of words and will return a list of matching 123000803e56STom N Harris * pages for each one. 1231488dd6ceSAndreas Gohr * 123263773904SAndreas Gohr * Important: No ACL checking is done here! All results are 123363773904SAndreas Gohr * returned, regardless of permissions 123463773904SAndreas Gohr * 12359b41be24STom N Harris * @param arrayref $words list of words to search for 123600803e56STom N Harris * @return array list of pages found, associated with the search terms 1237488dd6ceSAndreas Gohr */ 12389b41be24STom N Harrisfunction idx_lookup(&$words) { 12399b41be24STom N Harris $Indexer = idx_get_indexer(); 124000803e56STom N Harris return $Indexer->lookup($words); 1241488dd6ceSAndreas Gohr} 1242488dd6ceSAndreas Gohr 1243488dd6ceSAndreas Gohr/** 124400803e56STom N Harris * Split a string into tokens 1245488dd6ceSAndreas Gohr * 1246488dd6ceSAndreas Gohr */ 124700803e56STom N Harrisfunction idx_tokenizer($string, $wc=false) { 12489b41be24STom N Harris $Indexer = idx_get_indexer(); 124900803e56STom N Harris return $Indexer->tokenizer($string, $wc); 1250488dd6ceSAndreas Gohr} 125100803e56STom N Harris 125200803e56STom N Harris/* For compatibility */ 1253488dd6ceSAndreas Gohr 1254f5eb7cf0SAndreas Gohr/** 125500803e56STom N Harris * Read the list of words in an index (if it exists). 1256f5eb7cf0SAndreas Gohr * 12574e1bf408STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 1258f5eb7cf0SAndreas Gohr */ 125900803e56STom N Harrisfunction idx_getIndex($idx, $suffix) { 12601c07b9e6STom N Harris global $conf; 126100803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; 126200803e56STom N Harris if (!@file_exists($fn)) return array(); 126300803e56STom N Harris return file($fn); 126400803e56STom N Harris} 1265f5eb7cf0SAndreas Gohr 126600803e56STom N Harris/** 126700803e56STom N Harris * Get the list of lengths indexed in the wiki. 126800803e56STom N Harris * 126900803e56STom N Harris * Read the index directory or a cache file and returns 127000803e56STom N Harris * a sorted array of lengths of the words used in the wiki. 127100803e56STom N Harris * 127200803e56STom N Harris * @author YoBoY <yoboy.leguesh@gmail.com> 127300803e56STom N Harris */ 127400803e56STom N Harrisfunction idx_listIndexLengths() { 127500803e56STom N Harris global $conf; 127600803e56STom N Harris // testing what we have to do, create a cache file or not. 127700803e56STom N Harris if ($conf['readdircache'] == 0) { 127800803e56STom N Harris $docache = false; 12791c07b9e6STom N Harris } else { 128000803e56STom N Harris clearstatcache(); 128100803e56STom N Harris if (@file_exists($conf['indexdir'].'/lengths.idx') 128200803e56STom N Harris && (time() < @filemtime($conf['indexdir'].'/lengths.idx') + $conf['readdircache'])) { 128300803e56STom N Harris if (($lengths = @file($conf['indexdir'].'/lengths.idx', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)) !== false) { 128400803e56STom N Harris $idx = array(); 128500803e56STom N Harris foreach ($lengths as $length) { 128600803e56STom N Harris $idx[] = (int)$length; 128700803e56STom N Harris } 128800803e56STom N Harris return $idx; 1289f5eb7cf0SAndreas Gohr } 12901c07b9e6STom N Harris } 129100803e56STom N Harris $docache = true; 129200803e56STom N Harris } 12934e1bf408STom N Harris 129400803e56STom N Harris if ($conf['readdircache'] == 0 || $docache) { 129500803e56STom N Harris $dir = @opendir($conf['indexdir']); 129600803e56STom N Harris if ($dir === false) 129700803e56STom N Harris return array(); 129800803e56STom N Harris $idx[] = array(); 129900803e56STom N Harris while (($f = readdir($dir)) !== false) { 130000803e56STom N Harris if (substr($f, 0, 1) == 'i' && substr($f, -4) == '.idx') { 130100803e56STom N Harris $i = substr($f, 1, -4); 130200803e56STom N Harris if (is_numeric($i)) 130300803e56STom N Harris $idx[] = (int)$i; 130400803e56STom N Harris } 130500803e56STom N Harris } 130600803e56STom N Harris closedir($dir); 130700803e56STom N Harris sort($idx); 130800803e56STom N Harris // save this in a file 130900803e56STom N Harris if ($docache) { 131000803e56STom N Harris $handle = @fopen($conf['indexdir'].'/lengths.idx', 'w'); 131100803e56STom N Harris @fwrite($handle, implode("\n", $idx)); 131200803e56STom N Harris @fclose($handle); 131300803e56STom N Harris } 131400803e56STom N Harris return $idx; 131500803e56STom N Harris } 131600803e56STom N Harris 131700803e56STom N Harris return array(); 131800803e56STom N Harris} 131900803e56STom N Harris 132000803e56STom N Harris/** 132100803e56STom N Harris * Get the word lengths that have been indexed. 132200803e56STom N Harris * 132300803e56STom N Harris * Reads the index directory and returns an array of lengths 132400803e56STom N Harris * that there are indices for. 132500803e56STom N Harris * 132600803e56STom N Harris * @author YoBoY <yoboy.leguesh@gmail.com> 132700803e56STom N Harris */ 132800803e56STom N Harrisfunction idx_indexLengths($filter) { 132900803e56STom N Harris global $conf; 133000803e56STom N Harris $idx = array(); 133100803e56STom N Harris if (is_array($filter)) { 133200803e56STom N Harris // testing if index files exist only 133300803e56STom N Harris $path = $conf['indexdir']."/i"; 133400803e56STom N Harris foreach ($filter as $key => $value) { 133500803e56STom N Harris if (@file_exists($path.$key.'.idx')) 133600803e56STom N Harris $idx[] = $key; 133700803e56STom N Harris } 1338f5eb7cf0SAndreas Gohr } else { 133900803e56STom N Harris $lengths = idx_listIndexLengths(); 134000803e56STom N Harris foreach ($lengths as $key => $length) { 134100803e56STom N Harris // keep all the values equal or superior 134200803e56STom N Harris if ((int)$length >= (int)$filter) 134300803e56STom N Harris $idx[] = $length; 1344f5eb7cf0SAndreas Gohr } 134500803e56STom N Harris } 134600803e56STom N Harris return $idx; 1347f5eb7cf0SAndreas Gohr} 1348f5eb7cf0SAndreas Gohr 134900803e56STom N Harris/** 135000803e56STom N Harris * Clean a name of a key for use as a file name. 135100803e56STom N Harris * 135200803e56STom N Harris * Romanizes non-latin characters, then strips away anything that's 135300803e56STom N Harris * not a letter, number, or underscore. 135400803e56STom N Harris * 135500803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 135600803e56STom N Harris */ 135700803e56STom N Harrisfunction idx_cleanName($name) { 135800803e56STom N Harris $name = utf8_romanize(trim((string)$name)); 135900803e56STom N Harris $name = preg_replace('#[ \./\\:-]+#', '_', $name); 136000803e56STom N Harris $name = preg_replace('/[^A-Za-z0-9_]/', '', $name); 136100803e56STom N Harris return strtolower($name); 1362f5eb7cf0SAndreas Gohr} 1363f5eb7cf0SAndreas Gohr 136400803e56STom N Harris//Setup VIM: ex: et ts=4 : 1365