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 137d939e4eSMichael Hamanndefine('INDEXER_VERSION', 5); 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 * 578cd4c12fSAndreas Gohr * @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; 698605afb1SMichael Hamann $version = INDEXER_VERSION; 708605afb1SMichael Hamann 71d0d6fe1bSTom N Harris // DokuWiki version is included for the convenience of plugins 72d0d6fe1bSTom N Harris $data = array('dokuwiki'=>$version); 738605afb1SMichael Hamann trigger_event('INDEXER_VERSION_GET', $data, null, false); 74d0d6fe1bSTom N Harris unset($data['dokuwiki']); // this needs to be first 75d0d6fe1bSTom N Harris ksort($data); 76d0d6fe1bSTom N Harris foreach ($data as $plugin=>$vers) 77d0d6fe1bSTom N Harris $version .= '+'.$plugin.'='.$vers; 78d0d6fe1bSTom N Harris $indexer_version = $version; 79d0d6fe1bSTom N Harris } 80d0d6fe1bSTom N Harris return $indexer_version; 817c2ef4e8STom N Harris} 827c2ef4e8STom N Harris 837c2ef4e8STom N Harris/** 84d5b23302STom N Harris * Measure the length of a string. 85d5b23302STom N Harris * Differs from strlen in handling of asian characters. 86d5b23302STom N Harris * 87d5b23302STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 88d5b23302STom N Harris */ 89d5b23302STom N Harrisfunction wordlen($w){ 90d5b23302STom N Harris $l = strlen($w); 91d5b23302STom N Harris // If left alone, all chinese "words" will get put into w3.idx 92d5b23302STom N Harris // So the "length" of a "word" is faked 934b9792c6STom N Harris if(preg_match_all('/[\xE2-\xEF]/',$w,$leadbytes)) { 944b9792c6STom N Harris foreach($leadbytes[0] as $b) 954b9792c6STom N Harris $l += ord($b) - 0xE1; 964b9792c6STom N Harris } 97d5b23302STom N Harris return $l; 98d5b23302STom N Harris} 99d5b23302STom N Harris 100d5b23302STom N Harris/** 10100803e56STom N Harris * Class that encapsulates operations on the indexer database. 102579b0f7eSTNHarris * 103579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 104579b0f7eSTNHarris */ 10500803e56STom N Harrisclass Doku_Indexer { 106579b0f7eSTNHarris 107579b0f7eSTNHarris /** 10800803e56STom N Harris * Adds the contents of a page to the fulltext index 109dd35e9c9SAndreas Gohr * 11000803e56STom N Harris * The added text replaces previous words for the same page. 11100803e56STom N Harris * An empty value erases the page. 11200803e56STom N Harris * 11300803e56STom N Harris * @param string $page a page name 11400803e56STom N Harris * @param string $text the body of the page 11500803e56STom N Harris * @return boolean the function completed successfully 11600803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 117dd35e9c9SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 118dd35e9c9SAndreas Gohr */ 11900803e56STom N Harris public function addPageWords($page, $text) { 12080fb93f6STom N Harris if (!$this->lock()) 1219b41be24STom N Harris return "locked"; 12200803e56STom N Harris 12300803e56STom N Harris // load known documents 12480fb93f6STom N Harris $pid = $this->addIndexKey('page', '', $page); 1255981eb09STom N Harris if ($pid === false) { 12680fb93f6STom N Harris $this->unlock(); 12700803e56STom N Harris return false; 12800803e56STom N Harris } 12900803e56STom N Harris 13000803e56STom N Harris $pagewords = array(); 13100803e56STom N Harris // get word usage in page 13280fb93f6STom N Harris $words = $this->getPageWords($text); 13300803e56STom N Harris if ($words === false) { 13480fb93f6STom N Harris $this->unlock(); 13500803e56STom N Harris return false; 13600803e56STom N Harris } 13700803e56STom N Harris 13800803e56STom N Harris if (!empty($words)) { 13900803e56STom N Harris foreach (array_keys($words) as $wlen) { 14080fb93f6STom N Harris $index = $this->getIndex('i', $wlen); 14100803e56STom N Harris foreach ($words[$wlen] as $wid => $freq) { 14200803e56STom N Harris $idx = ($wid<count($index)) ? $index[$wid] : ''; 14380fb93f6STom N Harris $index[$wid] = $this->updateTuple($idx, $pid, $freq); 14400803e56STom N Harris $pagewords[] = "$wlen*$wid"; 14500803e56STom N Harris } 14680fb93f6STom N Harris if (!$this->saveIndex('i', $wlen, $index)) { 14780fb93f6STom N Harris $this->unlock(); 14800803e56STom N Harris return false; 14900803e56STom N Harris } 15000803e56STom N Harris } 15100803e56STom N Harris } 15200803e56STom N Harris 15300803e56STom N Harris // Remove obsolete index entries 15480fb93f6STom N Harris $pageword_idx = $this->getIndexKey('pageword', '', $pid); 15500803e56STom N Harris if ($pageword_idx !== '') { 15600803e56STom N Harris $oldwords = explode(':',$pageword_idx); 15700803e56STom N Harris $delwords = array_diff($oldwords, $pagewords); 15800803e56STom N Harris $upwords = array(); 15900803e56STom N Harris foreach ($delwords as $word) { 16000803e56STom N Harris if ($word != '') { 16100803e56STom N Harris list($wlen,$wid) = explode('*', $word); 16200803e56STom N Harris $wid = (int)$wid; 16300803e56STom N Harris $upwords[$wlen][] = $wid; 16400803e56STom N Harris } 16500803e56STom N Harris } 16600803e56STom N Harris foreach ($upwords as $wlen => $widx) { 16780fb93f6STom N Harris $index = $this->getIndex('i', $wlen); 16800803e56STom N Harris foreach ($widx as $wid) { 16980fb93f6STom N Harris $index[$wid] = $this->updateTuple($index[$wid], $pid, 0); 17000803e56STom N Harris } 17180fb93f6STom N Harris $this->saveIndex('i', $wlen, $index); 17200803e56STom N Harris } 17300803e56STom N Harris } 17400803e56STom N Harris // Save the reverse index 17500803e56STom N Harris $pageword_idx = join(':', $pagewords); 17680fb93f6STom N Harris if (!$this->saveIndexKey('pageword', '', $pid, $pageword_idx)) { 17780fb93f6STom N Harris $this->unlock(); 17800803e56STom N Harris return false; 17900803e56STom N Harris } 18000803e56STom N Harris 18180fb93f6STom N Harris $this->unlock(); 182dd35e9c9SAndreas Gohr return true; 183dd35e9c9SAndreas Gohr } 184dd35e9c9SAndreas Gohr 185dd35e9c9SAndreas Gohr /** 18600803e56STom N Harris * Split the words in a page and add them to the index. 18744ca0adfSAndreas Gohr * 188b9d8cc1eSTom N Harris * @param string $text content of the page 189b9d8cc1eSTom N Harris * @return array list of word IDs and number of times used 19044ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 19117f42b01SChris Smith * @author Christopher Smith <chris@jalakai.co.uk> 19200803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 193b4ce25e9SAndreas Gohr */ 19480fb93f6STom N Harris protected function getPageWords($text) { 19544ca0adfSAndreas Gohr global $conf; 19644ca0adfSAndreas Gohr 19700803e56STom N Harris $tokens = $this->tokenizer($text); 19817f42b01SChris Smith $tokens = array_count_values($tokens); // count the frequency of each token 19917f42b01SChris Smith 20017f42b01SChris Smith $words = array(); 2014e1bf408STom N Harris foreach ($tokens as $w=>$c) { 202d5b23302STom N Harris $l = wordlen($w); 203579b0f7eSTNHarris if (isset($words[$l])){ 2044e1bf408STom N Harris $words[$l][$w] = $c + (isset($words[$l][$w]) ? $words[$l][$w] : 0); 20517f42b01SChris Smith }else{ 2064e1bf408STom N Harris $words[$l] = array($w => $c); 20717f42b01SChris Smith } 20817f42b01SChris Smith } 20917f42b01SChris Smith 210579b0f7eSTNHarris // arrive here with $words = array(wordlen => array(word => frequency)) 211e5e50383SMichael Hamann $word_idx_modified = false; 212b4ce25e9SAndreas Gohr $index = array(); //resulting index 213579b0f7eSTNHarris foreach (array_keys($words) as $wlen) { 21480fb93f6STom N Harris $word_idx = $this->getIndex('w', $wlen); 215579b0f7eSTNHarris foreach ($words[$wlen] as $word => $freq) { 21600803e56STom N Harris $wid = array_search($word, $word_idx); 21700803e56STom N Harris if ($wid === false) { 218d5b23302STom N Harris $wid = count($word_idx); 21900803e56STom N Harris $word_idx[] = $word; 220e5e50383SMichael Hamann $word_idx_modified = true; 221b4ce25e9SAndreas Gohr } 222579b0f7eSTNHarris if (!isset($index[$wlen])) 223579b0f7eSTNHarris $index[$wlen] = array(); 224579b0f7eSTNHarris $index[$wlen][$wid] = $freq; 22544ca0adfSAndreas Gohr } 22600803e56STom N Harris // save back the word index 22780fb93f6STom N Harris if ($word_idx_modified && !$this->saveIndex('w', $wlen, $word_idx)) 22844ca0adfSAndreas Gohr return false; 22944ca0adfSAndreas Gohr } 230b4ce25e9SAndreas Gohr 231b4ce25e9SAndreas Gohr return $index; 232b4ce25e9SAndreas Gohr } 233b4ce25e9SAndreas Gohr 23444ca0adfSAndreas Gohr /** 235e1e1a7e0SMichael Hamann * Add/update keys to/of the metadata index. 23644ca0adfSAndreas Gohr * 23700803e56STom N Harris * Adding new keys does not remove other keys for the page. 23800803e56STom N Harris * An empty value will erase the key. 23900803e56STom N Harris * The $key parameter can be an array to add multiple keys. $value will 24000803e56STom N Harris * not be used if $key is an array. 24144ca0adfSAndreas Gohr * 24200803e56STom N Harris * @param string $page a page name 24300803e56STom N Harris * @param mixed $key a key string or array of key=>value pairs 24400803e56STom N Harris * @param mixed $value the value or list of values 24500803e56STom N Harris * @return boolean the function completed successfully 24600803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 247e1e1a7e0SMichael Hamann * @author Michael Hamann <michael@content-space.de> 24844ca0adfSAndreas Gohr */ 24900803e56STom N Harris public function addMetaKeys($page, $key, $value=null) { 25000803e56STom N Harris if (!is_array($key)) { 25100803e56STom N Harris $key = array($key => $value); 25200803e56STom N Harris } elseif (!is_null($value)) { 25300803e56STom N Harris // $key is array, but $value is not null 25400803e56STom N Harris trigger_error("array passed to addMetaKeys but value is not null", E_USER_WARNING); 25500803e56STom N Harris } 25600803e56STom N Harris 25780fb93f6STom N Harris if (!$this->lock()) 258e1e1a7e0SMichael Hamann return "locked"; 259b4ce25e9SAndreas Gohr 260488dd6ceSAndreas Gohr // load known documents 26180fb93f6STom N Harris $pid = $this->addIndexKey('page', '', $page); 26200803e56STom N Harris if ($pid === false) { 26380fb93f6STom N Harris $this->unlock(); 264579b0f7eSTNHarris return false; 26544ca0adfSAndreas Gohr } 26600803e56STom N Harris 267c1209673STom N Harris // Special handling for titles so the index file is simpler 268c1209673STom N Harris if (array_key_exists('title', $key)) { 269c1209673STom N Harris $value = $key['title']; 270c1209673STom N Harris if (is_array($value)) 271c1209673STom N Harris $value = $value[0]; 27280fb93f6STom N Harris $this->saveIndexKey('title', '', $pid, $value); 273c1209673STom N Harris unset($key['title']); 274c1209673STom N Harris } 275c1209673STom N Harris 27600803e56STom N Harris foreach ($key as $name => $values) { 27700803e56STom N Harris $metaname = idx_cleanName($name); 27880fb93f6STom N Harris $this->addIndexKey('metadata', '', $metaname); 279b9d8cc1eSTom N Harris $metaidx = $this->getIndex($metaname.'_i', ''); 280b9d8cc1eSTom N Harris $metawords = $this->getIndex($metaname.'_w', ''); 28100803e56STom N Harris $addwords = false; 282e1e1a7e0SMichael Hamann 283e1e1a7e0SMichael Hamann if (!is_array($values)) $values = array($values); 284e1e1a7e0SMichael Hamann 285b9d8cc1eSTom N Harris $val_idx = $this->getIndexKey($metaname.'_p', '', $pid); 286e1e1a7e0SMichael Hamann if ($val_idx != '') { 287e1e1a7e0SMichael Hamann $val_idx = explode(':', $val_idx); 288e1e1a7e0SMichael Hamann // -1 means remove, 0 keep, 1 add 289e1e1a7e0SMichael Hamann $val_idx = array_combine($val_idx, array_fill(0, count($val_idx), -1)); 290e1e1a7e0SMichael Hamann } else { 291e1e1a7e0SMichael Hamann $val_idx = array(); 292e1e1a7e0SMichael Hamann } 293e1e1a7e0SMichael Hamann 29400803e56STom N Harris foreach ($values as $val) { 29500803e56STom N Harris $val = (string)$val; 29600803e56STom N Harris if ($val !== "") { 29700803e56STom N Harris $id = array_search($val, $metawords); 29800803e56STom N Harris if ($id === false) { 29900803e56STom N Harris $id = count($metawords); 30000803e56STom N Harris $metawords[$id] = $val; 30100803e56STom N Harris $addwords = true; 302d5b23302STom N Harris } 303e1e1a7e0SMichael Hamann // test if value is already in the index 304e1e1a7e0SMichael Hamann if (isset($val_idx[$id]) && $val_idx[$id] <= 0) 305e1e1a7e0SMichael Hamann $val_idx[$id] = 0; 306e1e1a7e0SMichael Hamann else // else add it 307e1e1a7e0SMichael Hamann $val_idx[$id] = 1; 30844ca0adfSAndreas Gohr } 309579b0f7eSTNHarris } 310e1e1a7e0SMichael Hamann 31100803e56STom N Harris if ($addwords) 31280fb93f6STom N Harris $this->saveIndex($metaname.'_w', '', $metawords); 313e1e1a7e0SMichael Hamann $vals_changed = false; 314e1e1a7e0SMichael Hamann foreach ($val_idx as $id => $action) { 315e1e1a7e0SMichael Hamann if ($action == -1) { 31680fb93f6STom N Harris $metaidx[$id] = $this->updateTuple($metaidx[$id], $pid, 0); 317e1e1a7e0SMichael Hamann $vals_changed = true; 318e1e1a7e0SMichael Hamann unset($val_idx[$id]); 319e1e1a7e0SMichael Hamann } elseif ($action == 1) { 32080fb93f6STom N Harris $metaidx[$id] = $this->updateTuple($metaidx[$id], $pid, 1); 321e1e1a7e0SMichael Hamann $vals_changed = true; 322e1e1a7e0SMichael Hamann } 323e1e1a7e0SMichael Hamann } 324e1e1a7e0SMichael Hamann 325e1e1a7e0SMichael Hamann if ($vals_changed) { 32680fb93f6STom N Harris $this->saveIndex($metaname.'_i', '', $metaidx); 327e1e1a7e0SMichael Hamann $val_idx = implode(':', array_keys($val_idx)); 32880fb93f6STom N Harris $this->saveIndexKey($metaname.'_p', '', $pid, $val_idx); 329b6344591STom N Harris } 330e1e1a7e0SMichael Hamann 33100803e56STom N Harris unset($metaidx); 33200803e56STom N Harris unset($metawords); 333a0c5c349STom N Harris } 334e1e1a7e0SMichael Hamann 33580fb93f6STom N Harris $this->unlock(); 336579b0f7eSTNHarris return true; 33744ca0adfSAndreas Gohr } 33844ca0adfSAndreas Gohr 33944ca0adfSAndreas Gohr /** 34000803e56STom N Harris * Remove a page from the index 34144ca0adfSAndreas Gohr * 34200803e56STom N Harris * Erases entries in all known indexes. 34344ca0adfSAndreas Gohr * 34400803e56STom N Harris * @param string $page a page name 34500803e56STom N Harris * @return boolean the function completed successfully 34600803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 34744ca0adfSAndreas Gohr */ 34800803e56STom N Harris public function deletePage($page) { 34980fb93f6STom N Harris if (!$this->lock()) 350bbc85ee4STom N Harris return "locked"; 351bbc85ee4STom N Harris 352bbc85ee4STom N Harris // load known documents 353c55c59e3SMichael Hamann $pid = $this->addIndexKey('page', '', $page); 3545981eb09STom N Harris if ($pid === false) { 35580fb93f6STom N Harris $this->unlock(); 356bbc85ee4STom N Harris return false; 357bbc85ee4STom N Harris } 358bbc85ee4STom N Harris 359bbc85ee4STom N Harris // Remove obsolete index entries 36080fb93f6STom N Harris $pageword_idx = $this->getIndexKey('pageword', '', $pid); 361bbc85ee4STom N Harris if ($pageword_idx !== '') { 362bbc85ee4STom N Harris $delwords = explode(':',$pageword_idx); 363bbc85ee4STom N Harris $upwords = array(); 364bbc85ee4STom N Harris foreach ($delwords as $word) { 365bbc85ee4STom N Harris if ($word != '') { 366bbc85ee4STom N Harris list($wlen,$wid) = explode('*', $word); 367bbc85ee4STom N Harris $wid = (int)$wid; 368bbc85ee4STom N Harris $upwords[$wlen][] = $wid; 369bbc85ee4STom N Harris } 370bbc85ee4STom N Harris } 371bbc85ee4STom N Harris foreach ($upwords as $wlen => $widx) { 37280fb93f6STom N Harris $index = $this->getIndex('i', $wlen); 373bbc85ee4STom N Harris foreach ($widx as $wid) { 37480fb93f6STom N Harris $index[$wid] = $this->updateTuple($index[$wid], $pid, 0); 375bbc85ee4STom N Harris } 37680fb93f6STom N Harris $this->saveIndex('i', $wlen, $index); 377bbc85ee4STom N Harris } 378bbc85ee4STom N Harris } 379bbc85ee4STom N Harris // Save the reverse index 38080fb93f6STom N Harris if (!$this->saveIndexKey('pageword', '', $pid, "")) { 38180fb93f6STom N Harris $this->unlock(); 382bbc85ee4STom N Harris return false; 383bbc85ee4STom N Harris } 384bbc85ee4STom N Harris 38580fb93f6STom N Harris $this->saveIndexKey('title', '', $pid, ""); 38680fb93f6STom N Harris $keyidx = $this->getIndex('metadata', ''); 3870604da34STom N Harris foreach ($keyidx as $metaname) { 38880fb93f6STom N Harris $val_idx = explode(':', $this->getIndexKey($metaname.'_p', '', $pid)); 38980fb93f6STom N Harris $meta_idx = $this->getIndex($metaname.'_i', ''); 3900604da34STom N Harris foreach ($val_idx as $id) { 391c55c59e3SMichael Hamann if ($id === '') continue; 39280fb93f6STom N Harris $meta_idx[$id] = $this->updateTuple($meta_idx[$id], $pid, 0); 3930604da34STom N Harris } 39480fb93f6STom N Harris $this->saveIndex($metaname.'_i', '', $meta_idx); 39580fb93f6STom N Harris $this->saveIndexKey($metaname.'_p', '', $pid, ''); 3960604da34STom N Harris } 397bbc85ee4STom N Harris 39880fb93f6STom N Harris $this->unlock(); 399bbc85ee4STom N Harris return true; 400d5b23302STom N Harris } 40144ca0adfSAndreas Gohr 402d5b23302STom N Harris /** 40300803e56STom N Harris * Split the text into words for fulltext search 404d5b23302STom N Harris * 40500803e56STom N Harris * TODO: does this also need &$stopwords ? 406d5b23302STom N Harris * 4078cd4c12fSAndreas Gohr * @triggers INDEXER_TEXT_PREPARE 4088cd4c12fSAndreas Gohr * This event allows plugins to modify the text before it gets tokenized. 4098cd4c12fSAndreas Gohr * Plugins intercepting this event should also intercept INDEX_VERSION_GET 4108cd4c12fSAndreas Gohr * 41100803e56STom N Harris * @param string $text plain text 41200803e56STom N Harris * @param boolean $wc are wildcards allowed? 41300803e56STom N Harris * @return array list of words in the text 414d5b23302STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 415d5b23302STom N Harris * @author Andreas Gohr <andi@splitbrain.org> 416d5b23302STom N Harris */ 41700803e56STom N Harris public function tokenizer($text, $wc=false) { 41822952965SYoBoY global $conf; 41900803e56STom N Harris $words = array(); 42000803e56STom N Harris $wc = ($wc) ? '' : '\*'; 42100803e56STom N Harris $stopwords =& idx_get_stopwords(); 42200803e56STom N Harris 4238cd4c12fSAndreas Gohr // prepare the text to be tokenized 4248cd4c12fSAndreas Gohr $evt = new Doku_Event('INDEXER_TEXT_PREPARE', $text); 4258cd4c12fSAndreas Gohr if ($evt->advise_before(true)) { 42600803e56STom N Harris if (preg_match('/[^0-9A-Za-z ]/u', $text)) { 42700803e56STom N Harris // handle asian chars as single words (may fail on older PHP version) 42800803e56STom N Harris $asia = @preg_replace('/('.IDX_ASIAN.')/u', ' \1 ', $text); 42900803e56STom N Harris if (!is_null($asia)) $text = $asia; // recover from regexp falure 43022952965SYoBoY } 43122952965SYoBoY } 4328cd4c12fSAndreas Gohr $evt->advise_after(); 4338cd4c12fSAndreas Gohr unset($evt); 4348cd4c12fSAndreas Gohr 435f77fc90dSMichael Hamann $text = strtr($text, 4364f0030ddSAndreas Gohr array( 4374f0030ddSAndreas Gohr "\r" => ' ', 4384f0030ddSAndreas Gohr "\n" => ' ', 4394f0030ddSAndreas Gohr "\t" => ' ', 4404f0030ddSAndreas Gohr "\xC2\xAD" => '', //soft-hyphen 4414f0030ddSAndreas Gohr ) 4424f0030ddSAndreas Gohr ); 44300803e56STom N Harris if (preg_match('/[^0-9A-Za-z ]/u', $text)) 44400803e56STom N Harris $text = utf8_stripspecials($text, ' ', '\._\-:'.$wc); 44522952965SYoBoY 44600803e56STom N Harris $wordlist = explode(' ', $text); 4475f27cb0eSMichael Hamann foreach ($wordlist as $i => $word) { 4485f27cb0eSMichael Hamann $wordlist[$i] = (preg_match('/[^0-9A-Za-z]/u', $word)) ? 44900803e56STom N Harris utf8_strtolower($word) : strtolower($word); 4505f27cb0eSMichael Hamann } 4515f27cb0eSMichael Hamann 4525f27cb0eSMichael Hamann foreach ($wordlist as $i => $word) { 453675bf41fSTom N Harris if ((!is_numeric($word) && strlen($word) < IDX_MINWORDLENGTH) 454675bf41fSTom N Harris || array_search($word, $stopwords) !== false) 455675bf41fSTom N Harris unset($wordlist[$i]); 45622952965SYoBoY } 457675bf41fSTom N Harris return array_values($wordlist); 45822952965SYoBoY } 45922952965SYoBoY 46022952965SYoBoY /** 46100803e56STom N Harris * Find pages in the fulltext index containing the words, 462579b0f7eSTNHarris * 46300803e56STom N Harris * The search words must be pre-tokenized, meaning only letters and 46400803e56STom N Harris * numbers with an optional wildcard 465579b0f7eSTNHarris * 46600803e56STom N Harris * The returned array will have the original tokens as key. The values 46700803e56STom N Harris * in the returned list is an array with the page names as keys and the 468b00bd361STom N Harris * number of times that token appears on the page as value. 46900803e56STom N Harris * 4709b41be24STom N Harris * @param arrayref $tokens list of words to search for 47100803e56STom N Harris * @return array list of page names with usage counts 47200803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 47300803e56STom N Harris * @author Andreas Gohr <andi@splitbrain.org> 474579b0f7eSTNHarris */ 4759b41be24STom N Harris public function lookup(&$tokens) { 47600803e56STom N Harris $result = array(); 47780fb93f6STom N Harris $wids = $this->getIndexWords($tokens, $result); 47800803e56STom N Harris if (empty($wids)) return array(); 47900803e56STom N Harris // load known words and documents 48080fb93f6STom N Harris $page_idx = $this->getIndex('page', ''); 48100803e56STom N Harris $docs = array(); 48200803e56STom N Harris foreach (array_keys($wids) as $wlen) { 48300803e56STom N Harris $wids[$wlen] = array_unique($wids[$wlen]); 48480fb93f6STom N Harris $index = $this->getIndex('i', $wlen); 48500803e56STom N Harris foreach($wids[$wlen] as $ixid) { 48600803e56STom N Harris if ($ixid < count($index)) 48780fb93f6STom N Harris $docs["$wlen*$ixid"] = $this->parseTuples($page_idx, $index[$ixid]); 488d5b23302STom N Harris } 489d5b23302STom N Harris } 49000803e56STom N Harris // merge found pages into final result array 49100803e56STom N Harris $final = array(); 49200803e56STom N Harris foreach ($result as $word => $res) { 49300803e56STom N Harris $final[$word] = array(); 49400803e56STom N Harris foreach ($res as $wid) { 495952ab260SMichael Hamann // handle the case when ($ixid < count($index)) has been false 496952ab260SMichael Hamann // and thus $docs[$wid] hasn't been set. 497952ab260SMichael Hamann if (!isset($docs[$wid])) continue; 49800803e56STom N Harris $hits = &$docs[$wid]; 49900803e56STom N Harris foreach ($hits as $hitkey => $hitcnt) { 50000803e56STom N Harris // make sure the document still exists 50100803e56STom N Harris if (!page_exists($hitkey, '', false)) continue; 50200803e56STom N Harris if (!isset($final[$word][$hitkey])) 50300803e56STom N Harris $final[$word][$hitkey] = $hitcnt; 50400803e56STom N Harris else 50500803e56STom N Harris $final[$word][$hitkey] += $hitcnt; 506d5b23302STom N Harris } 507579b0f7eSTNHarris } 508579b0f7eSTNHarris } 50900803e56STom N Harris return $final; 510579b0f7eSTNHarris } 511579b0f7eSTNHarris 512579b0f7eSTNHarris /** 51300803e56STom N Harris * Find pages containing a metadata key. 514d5b23302STom N Harris * 51500803e56STom N Harris * The metadata values are compared as case-sensitive strings. Pass a 51600803e56STom N Harris * callback function that returns true or false to use a different 517b00bd361STom N Harris * comparison function. The function will be called with the $value being 518b00bd361STom N Harris * searched for as the first argument, and the word in the index as the 5191538718dSTom N Harris * second argument. The function preg_match can be used directly if the 5201538718dSTom N Harris * values are regexes. 521d5b23302STom N Harris * 52200803e56STom N Harris * @param string $key name of the metadata key to look for 5231538718dSTom N Harris * @param string $value search term to look for, must be a string or array of strings 52400803e56STom N Harris * @param callback $func comparison function 525b00bd361STom N Harris * @return array lists with page names, keys are query values if $value is array 52600803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 527cd763a5bSMichael Hamann * @author Michael Hamann <michael@content-space.de> 52800803e56STom N Harris */ 529b00bd361STom N Harris public function lookupKey($key, &$value, $func=null) { 530f078bb00STom N Harris if (!is_array($value)) 531f078bb00STom N Harris $value_array = array($value); 532f078bb00STom N Harris else 533f078bb00STom N Harris $value_array =& $value; 534cd763a5bSMichael Hamann 535c1209673STom N Harris // the matching ids for the provided value(s) 536c1209673STom N Harris $value_ids = array(); 537c1209673STom N Harris 538c1209673STom N Harris $metaname = idx_cleanName($key); 539c1209673STom N Harris 540c1209673STom N Harris // get all words in order to search the matching ids 541c1209673STom N Harris if ($key == 'title') { 54280fb93f6STom N Harris $words = $this->getIndex('title', ''); 543c1209673STom N Harris } else { 544b9d8cc1eSTom N Harris $words = $this->getIndex($metaname.'_w', ''); 545c1209673STom N Harris } 546c1209673STom N Harris 547f078bb00STom N Harris if (!is_null($func)) { 5481538718dSTom N Harris foreach ($value_array as $val) { 549f078bb00STom N Harris foreach ($words as $i => $word) { 5501538718dSTom N Harris if (call_user_func_array($func, array($val, $word))) 551c1209673STom N Harris $value_ids[$i][] = $val; 552f078bb00STom N Harris } 553f078bb00STom N Harris } 554f078bb00STom N Harris } else { 555f078bb00STom N Harris foreach ($value_array as $val) { 556f078bb00STom N Harris $xval = $val; 557b6d540bdSTom N Harris $caret = '^'; 558b6d540bdSTom N Harris $dollar = '$'; 559f078bb00STom N Harris // check for wildcards 560f078bb00STom N Harris if (substr($xval, 0, 1) == '*') { 561f078bb00STom N Harris $xval = substr($xval, 1); 562b6d540bdSTom N Harris $caret = ''; 563f078bb00STom N Harris } 564f078bb00STom N Harris if (substr($xval, -1, 1) == '*') { 565f078bb00STom N Harris $xval = substr($xval, 0, -1); 566b6d540bdSTom N Harris $dollar = ''; 567f078bb00STom N Harris } 568b6d540bdSTom N Harris if (!$caret || !$dollar) { 569f078bb00STom N Harris $re = $caret.preg_quote($xval, '/').$dollar; 570f078bb00STom N Harris foreach(array_keys(preg_grep('/'.$re.'/', $words)) as $i) 571c1209673STom N Harris $value_ids[$i][] = $val; 572cd763a5bSMichael Hamann } else { 573f078bb00STom N Harris if (($i = array_search($val, $words)) !== false) 574c1209673STom N Harris $value_ids[$i][] = $val; 575cd763a5bSMichael Hamann } 576cd763a5bSMichael Hamann } 577cd763a5bSMichael Hamann } 578cd763a5bSMichael Hamann 579cd763a5bSMichael Hamann unset($words); // free the used memory 580cd763a5bSMichael Hamann 5817233c152SMichael Hamann // initialize the result so it won't be null 582c1209673STom N Harris $result = array(); 5837233c152SMichael Hamann foreach ($value_array as $val) { 5847233c152SMichael Hamann $result[$val] = array(); 5857233c152SMichael Hamann } 5867233c152SMichael Hamann 58780fb93f6STom N Harris $page_idx = $this->getIndex('page', ''); 588cd763a5bSMichael Hamann 589c1209673STom N Harris // Special handling for titles 590c1209673STom N Harris if ($key == 'title') { 591c1209673STom N Harris foreach ($value_ids as $pid => $val_list) { 592c1209673STom N Harris $page = $page_idx[$pid]; 593c1209673STom N Harris foreach ($val_list as $val) { 594c1209673STom N Harris $result[$val][] = $page; 595c1209673STom N Harris } 596c1209673STom N Harris } 597c1209673STom N Harris } else { 598c1209673STom N Harris // load all lines and pages so the used lines can be taken and matched with the pages 599b9d8cc1eSTom N Harris $lines = $this->getIndex($metaname.'_i', ''); 600c1209673STom N Harris 601c1209673STom N Harris foreach ($value_ids as $value_id => $val_list) { 602cd763a5bSMichael Hamann // parse the tuples of the form page_id*1:page2_id*1 and so on, return value 603cd763a5bSMichael Hamann // is an array with page_id => 1, page2_id => 1 etc. so take the keys only 60480fb93f6STom N Harris $pages = array_keys($this->parseTuples($page_idx, $lines[$value_id])); 605c1209673STom N Harris foreach ($val_list as $val) { 606c1209673STom N Harris $result[$val] = array_merge($result[$val], $pages); 607c1209673STom N Harris } 608c1209673STom N Harris } 609cd763a5bSMichael Hamann } 610f078bb00STom N Harris if (!is_array($value)) $result = $result[$value]; 611cd763a5bSMichael Hamann return $result; 61200803e56STom N Harris } 61300803e56STom N Harris 61400803e56STom N Harris /** 61500803e56STom N Harris * Find the index ID of each search term. 61600803e56STom N Harris * 61700803e56STom N Harris * The query terms should only contain valid characters, with a '*' at 61800803e56STom N Harris * either the beginning or end of the word (or both). 61900803e56STom N Harris * The $result parameter can be used to merge the index locations with 62000803e56STom N Harris * the appropriate query term. 62100803e56STom N Harris * 6229b41be24STom N Harris * @param arrayref $words The query terms. 62300803e56STom N Harris * @param arrayref $result Set to word => array("length*id" ...) 624d5b23302STom N Harris * @return array Set to length => array(id ...) 625d5b23302STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 626d5b23302STom N Harris */ 62780fb93f6STom N Harris protected function getIndexWords(&$words, &$result) { 628d5b23302STom N Harris $tokens = array(); 629d5b23302STom N Harris $tokenlength = array(); 630d5b23302STom N Harris $tokenwild = array(); 631d5b23302STom N Harris foreach ($words as $word) { 632d5b23302STom N Harris $result[$word] = array(); 633b6d540bdSTom N Harris $caret = '^'; 634b6d540bdSTom N Harris $dollar = '$'; 635d5b23302STom N Harris $xword = $word; 636d5b23302STom N Harris $wlen = wordlen($word); 637d5b23302STom N Harris 638d5b23302STom N Harris // check for wildcards 639d5b23302STom N Harris if (substr($xword, 0, 1) == '*') { 640d5b23302STom N Harris $xword = substr($xword, 1); 641b6d540bdSTom N Harris $caret = ''; 642d5b23302STom N Harris $wlen -= 1; 643d5b23302STom N Harris } 644d5b23302STom N Harris if (substr($xword, -1, 1) == '*') { 645d5b23302STom N Harris $xword = substr($xword, 0, -1); 646b6d540bdSTom N Harris $dollar = ''; 647d5b23302STom N Harris $wlen -= 1; 648d5b23302STom N Harris } 649b6d540bdSTom N Harris if ($wlen < IDX_MINWORDLENGTH && $caret && $dollar && !is_numeric($xword)) 65000803e56STom N Harris continue; 65100803e56STom N Harris if (!isset($tokens[$xword])) 652d5b23302STom N Harris $tokenlength[$wlen][] = $xword; 653b6d540bdSTom N Harris if (!$caret || !$dollar) { 654f078bb00STom N Harris $re = $caret.preg_quote($xword, '/').$dollar; 65500803e56STom N Harris $tokens[$xword][] = array($word, '/'.$re.'/'); 65600803e56STom N Harris if (!isset($tokenwild[$xword])) 65700803e56STom N Harris $tokenwild[$xword] = $wlen; 65800803e56STom N Harris } else { 659d5b23302STom N Harris $tokens[$xword][] = array($word, null); 660d5b23302STom N Harris } 66100803e56STom N Harris } 662d5b23302STom N Harris asort($tokenwild); 66300803e56STom N Harris // $tokens = array( base word => array( [ query term , regexp ] ... ) ... ) 664d5b23302STom N Harris // $tokenlength = array( base word length => base word ... ) 665d5b23302STom N Harris // $tokenwild = array( base word => base word length ... ) 666d5b23302STom N Harris $length_filter = empty($tokenwild) ? $tokenlength : min(array_keys($tokenlength)); 66780fb93f6STom N Harris $indexes_known = $this->indexLengths($length_filter); 668d5b23302STom N Harris if (!empty($tokenwild)) sort($indexes_known); 669d5b23302STom N Harris // get word IDs 670d5b23302STom N Harris $wids = array(); 671d5b23302STom N Harris foreach ($indexes_known as $ixlen) { 67280fb93f6STom N Harris $word_idx = $this->getIndex('w', $ixlen); 673d5b23302STom N Harris // handle exact search 674d5b23302STom N Harris if (isset($tokenlength[$ixlen])) { 675d5b23302STom N Harris foreach ($tokenlength[$ixlen] as $xword) { 67600803e56STom N Harris $wid = array_search($xword, $word_idx); 67700803e56STom N Harris if ($wid !== false) { 678d5b23302STom N Harris $wids[$ixlen][] = $wid; 679d5b23302STom N Harris foreach ($tokens[$xword] as $w) 680d5b23302STom N Harris $result[$w[0]][] = "$ixlen*$wid"; 681d5b23302STom N Harris } 682d5b23302STom N Harris } 683d5b23302STom N Harris } 684d5b23302STom N Harris // handle wildcard search 685d5b23302STom N Harris foreach ($tokenwild as $xword => $wlen) { 686d5b23302STom N Harris if ($wlen >= $ixlen) break; 687d5b23302STom N Harris foreach ($tokens[$xword] as $w) { 688d5b23302STom N Harris if (is_null($w[1])) continue; 689d5b23302STom N Harris foreach(array_keys(preg_grep($w[1], $word_idx)) as $wid) { 690d5b23302STom N Harris $wids[$ixlen][] = $wid; 691d5b23302STom N Harris $result[$w[0]][] = "$ixlen*$wid"; 692d5b23302STom N Harris } 693d5b23302STom N Harris } 694d5b23302STom N Harris } 695d5b23302STom N Harris } 696d5b23302STom N Harris return $wids; 697d5b23302STom N Harris } 698d5b23302STom N Harris 699d5b23302STom N Harris /** 70000803e56STom N Harris * Return a list of all pages 701c1209673STom N Harris * Warning: pages may not exist! 702488dd6ceSAndreas Gohr * 70300803e56STom N Harris * @param string $key list only pages containing the metadata key (optional) 70400803e56STom N Harris * @return array list of page names 70500803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 70600803e56STom N Harris */ 70700803e56STom N Harris public function getPages($key=null) { 70880fb93f6STom N Harris $page_idx = $this->getIndex('page', ''); 70900803e56STom N Harris if (is_null($key)) return $page_idx; 710c1209673STom N Harris 711c1209673STom N Harris $metaname = idx_cleanName($key); 712c1209673STom N Harris 713c1209673STom N Harris // Special handling for titles 714c1209673STom N Harris if ($key == 'title') { 71580fb93f6STom N Harris $title_idx = $this->getIndex('title', ''); 716c1209673STom N Harris array_splice($page_idx, count($title_idx)); 717c1209673STom N Harris foreach ($title_idx as $i => $title) 718c1209673STom N Harris if ($title === "") unset($page_idx[$i]); 719675bf41fSTom N Harris return array_values($page_idx); 720c1209673STom N Harris } 721c1209673STom N Harris 722c1209673STom N Harris $pages = array(); 723b9d8cc1eSTom N Harris $lines = $this->getIndex($metaname.'_i', ''); 724c1209673STom N Harris foreach ($lines as $line) { 72580fb93f6STom N Harris $pages = array_merge($pages, $this->parseTuples($page_idx, $line)); 726c1209673STom N Harris } 727c1209673STom N Harris return array_keys($pages); 72800803e56STom N Harris } 72900803e56STom N Harris 73000803e56STom N Harris /** 73100803e56STom N Harris * Return a list of words sorted by number of times used 73200803e56STom N Harris * 73300803e56STom N Harris * @param int $min bottom frequency threshold 73400803e56STom N Harris * @param int $max upper frequency limit. No limit if $max<$min 735b8c040dbSTom N Harris * @param int $length minimum length of words to count 73600803e56STom N Harris * @param string $key metadata key to list. Uses the fulltext index if not given 73700803e56STom N Harris * @return array list of words as the keys and frequency as values 73800803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 73900803e56STom N Harris */ 740b8c040dbSTom N Harris public function histogram($min=1, $max=0, $minlen=3, $key=null) { 741175193d2STom N Harris if ($min < 1) 742175193d2STom N Harris $min = 1; 743175193d2STom N Harris if ($max < $min) 744175193d2STom N Harris $max = 0; 745175193d2STom N Harris 746175193d2STom N Harris $result = array(); 747175193d2STom N Harris 748175193d2STom N Harris if ($key == 'title') { 74980fb93f6STom N Harris $index = $this->getIndex('title', ''); 750175193d2STom N Harris $index = array_count_values($index); 751175193d2STom N Harris foreach ($index as $val => $cnt) { 752b8c040dbSTom N Harris if ($cnt >= $min && (!$max || $cnt <= $max) && strlen($val) >= $minlen) 753175193d2STom N Harris $result[$val] = $cnt; 754175193d2STom N Harris } 755175193d2STom N Harris } 756175193d2STom N Harris elseif (!is_null($key)) { 757175193d2STom N Harris $metaname = idx_cleanName($key); 75880fb93f6STom N Harris $index = $this->getIndex($metaname.'_i', ''); 759175193d2STom N Harris $val_idx = array(); 760175193d2STom N Harris foreach ($index as $wid => $line) { 76180fb93f6STom N Harris $freq = $this->countTuples($line); 762b8c040dbSTom N Harris if ($freq >= $min && (!$max || $freq <= $max) && strlen($val) >= $minlen) 763175193d2STom N Harris $val_idx[$wid] = $freq; 764175193d2STom N Harris } 765175193d2STom N Harris if (!empty($val_idx)) { 76680fb93f6STom N Harris $words = $this->getIndex($metaname.'_w', ''); 767175193d2STom N Harris foreach ($val_idx as $wid => $freq) 768175193d2STom N Harris $result[$words[$wid]] = $freq; 769175193d2STom N Harris } 770175193d2STom N Harris } 771175193d2STom N Harris else { 772175193d2STom N Harris $lengths = idx_listIndexLengths(); 773175193d2STom N Harris foreach ($lengths as $length) { 774b8c040dbSTom N Harris if ($length < $minlen) continue; 77580fb93f6STom N Harris $index = $this->getIndex('i', $length); 776175193d2STom N Harris $words = null; 777175193d2STom N Harris foreach ($index as $wid => $line) { 77880fb93f6STom N Harris $freq = $this->countTuples($line); 779175193d2STom N Harris if ($freq >= $min && (!$max || $freq <= $max)) { 780175193d2STom N Harris if ($words === null) 78180fb93f6STom N Harris $words = $this->getIndex('w', $length); 782175193d2STom N Harris $result[$words[$wid]] = $freq; 783175193d2STom N Harris } 784175193d2STom N Harris } 785175193d2STom N Harris } 786175193d2STom N Harris } 787175193d2STom N Harris 788175193d2STom N Harris arsort($result); 789175193d2STom N Harris return $result; 79000803e56STom N Harris } 79100803e56STom N Harris 79200803e56STom N Harris /** 79300803e56STom N Harris * Lock the indexer. 79400803e56STom N Harris * 79500803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 79600803e56STom N Harris */ 79780fb93f6STom N Harris protected function lock() { 79800803e56STom N Harris global $conf; 79900803e56STom N Harris $status = true; 800f77fc90dSMichael Hamann $run = 0; 80100803e56STom N Harris $lock = $conf['lockdir'].'/_indexer.lock'; 80200803e56STom N Harris while (!@mkdir($lock, $conf['dmode'])) { 80300803e56STom N Harris usleep(50); 804f77fc90dSMichael Hamann if(is_dir($lock) && time()-@filemtime($lock) > 60*5){ 805f77fc90dSMichael Hamann // looks like a stale lock - remove it 806f77fc90dSMichael Hamann if (!@rmdir($lock)) { 807f77fc90dSMichael Hamann $status = "removing the stale lock failed"; 808f77fc90dSMichael Hamann return false; 80900803e56STom N Harris } else { 810f77fc90dSMichael Hamann $status = "stale lock removed"; 811f77fc90dSMichael Hamann } 812f77fc90dSMichael Hamann }elseif($run++ == 1000){ 813f77fc90dSMichael Hamann // we waited 5 seconds for that lock 81400803e56STom N Harris return false; 81500803e56STom N Harris } 81600803e56STom N Harris } 81700803e56STom N Harris if ($conf['dperm']) 81800803e56STom N Harris chmod($lock, $conf['dperm']); 81900803e56STom N Harris return $status; 82000803e56STom N Harris } 82100803e56STom N Harris 82200803e56STom N Harris /** 82300803e56STom N Harris * Release the indexer lock. 82400803e56STom N Harris * 82500803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 82600803e56STom N Harris */ 82780fb93f6STom N Harris protected function unlock() { 82800803e56STom N Harris global $conf; 82900803e56STom N Harris @rmdir($conf['lockdir'].'/_indexer.lock'); 83000803e56STom N Harris return true; 83100803e56STom N Harris } 83200803e56STom N Harris 83300803e56STom N Harris /** 83400803e56STom N Harris * Retrieve the entire index. 83500803e56STom N Harris * 836b9d8cc1eSTom N Harris * The $suffix argument is for an index that is split into 837b9d8cc1eSTom N Harris * multiple parts. Different index files should use different 838b9d8cc1eSTom N Harris * base names. 839b9d8cc1eSTom N Harris * 840b9d8cc1eSTom N Harris * @param string $idx name of the index 841b9d8cc1eSTom N Harris * @param string $suffix subpart identifier 842b9d8cc1eSTom N Harris * @return array list of lines without CR or LF 84300803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 84400803e56STom N Harris */ 84580fb93f6STom N Harris protected function getIndex($idx, $suffix) { 84600803e56STom N Harris global $conf; 84700803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; 848d64516f5SMichael Hamann if (!@file_exists($fn)) return array(); 849d64516f5SMichael Hamann return file($fn, FILE_IGNORE_NEW_LINES); 85000803e56STom N Harris } 85100803e56STom N Harris 85200803e56STom N Harris /** 85300803e56STom N Harris * Replace the contents of the index with an array. 85400803e56STom N Harris * 855b9d8cc1eSTom N Harris * @param string $idx name of the index 856b9d8cc1eSTom N Harris * @param string $suffix subpart identifier 857b9d8cc1eSTom N Harris * @param arrayref $linex list of lines without LF 85800803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 85900803e56STom N Harris */ 86080fb93f6STom N Harris protected function saveIndex($idx, $suffix, &$lines) { 86100803e56STom N Harris global $conf; 86200803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix; 86300803e56STom N Harris $fh = @fopen($fn.'.tmp', 'w'); 86400803e56STom N Harris if (!$fh) return false; 86500803e56STom N Harris fwrite($fh, join("\n", $lines)); 8662e1018bcSMichael Hamann if (!empty($lines)) 8672e1018bcSMichael Hamann fwrite($fh, "\n"); 86800803e56STom N Harris fclose($fh); 86900803e56STom N Harris if (isset($conf['fperm'])) 87000803e56STom N Harris chmod($fn.'.tmp', $conf['fperm']); 87100803e56STom N Harris io_rename($fn.'.tmp', $fn.'.idx'); 87200803e56STom N Harris if ($suffix !== '') 87380fb93f6STom N Harris $this->cacheIndexDir($idx, $suffix, empty($lines)); 87400803e56STom N Harris return true; 87500803e56STom N Harris } 87600803e56STom N Harris 87700803e56STom N Harris /** 87800803e56STom N Harris * Retrieve a line from the index. 87900803e56STom N Harris * 880b9d8cc1eSTom N Harris * @param string $idx name of the index 881b9d8cc1eSTom N Harris * @param string $suffix subpart identifier 882b9d8cc1eSTom N Harris * @param int $id the line number 883b9d8cc1eSTom N Harris * @return string a line with trailing whitespace removed 88400803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 88500803e56STom N Harris */ 88680fb93f6STom N Harris protected function getIndexKey($idx, $suffix, $id) { 88700803e56STom N Harris global $conf; 88800803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; 88900803e56STom N Harris if (!@file_exists($fn)) return ''; 89000803e56STom N Harris $fh = @fopen($fn, 'r'); 89100803e56STom N Harris if (!$fh) return ''; 89200803e56STom N Harris $ln = -1; 89300803e56STom N Harris while (($line = fgets($fh)) !== false) { 89400803e56STom N Harris if (++$ln == $id) break; 89500803e56STom N Harris } 89600803e56STom N Harris fclose($fh); 89700803e56STom N Harris return rtrim((string)$line); 89800803e56STom N Harris } 89900803e56STom N Harris 90000803e56STom N Harris /** 90100803e56STom N Harris * Write a line into the index. 90200803e56STom N Harris * 903b9d8cc1eSTom N Harris * @param string $idx name of the index 904b9d8cc1eSTom N Harris * @param string $suffix subpart identifier 905b9d8cc1eSTom N Harris * @param int $id the line number 906b9d8cc1eSTom N Harris * @param string $line line to write 90700803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 90800803e56STom N Harris */ 90980fb93f6STom N Harris protected function saveIndexKey($idx, $suffix, $id, $line) { 91000803e56STom N Harris global $conf; 91100803e56STom N Harris if (substr($line, -1) != "\n") 91200803e56STom N Harris $line .= "\n"; 91300803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix; 91400803e56STom N Harris $fh = @fopen($fn.'.tmp', 'w'); 915dbb771bbSAdrian Lang if (!$fh) return false; 91600803e56STom N Harris $ih = @fopen($fn.'.idx', 'r'); 91700803e56STom N Harris if ($ih) { 91800803e56STom N Harris $ln = -1; 91900803e56STom N Harris while (($curline = fgets($ih)) !== false) { 92000803e56STom N Harris fwrite($fh, (++$ln == $id) ? $line : $curline); 92100803e56STom N Harris } 9224373c7b5SMichael Hamann if ($id > $ln) { 9234373c7b5SMichael Hamann while ($id > ++$ln) 9244373c7b5SMichael Hamann fwrite($fh, "\n"); 92500803e56STom N Harris fwrite($fh, $line); 9264373c7b5SMichael Hamann } 92700803e56STom N Harris fclose($ih); 92800803e56STom N Harris } else { 9294373c7b5SMichael Hamann $ln = -1; 9304373c7b5SMichael Hamann while ($id > ++$ln) 9314373c7b5SMichael Hamann fwrite($fh, "\n"); 93200803e56STom N Harris fwrite($fh, $line); 93300803e56STom N Harris } 93400803e56STom N Harris fclose($fh); 93500803e56STom N Harris if (isset($conf['fperm'])) 93600803e56STom N Harris chmod($fn.'.tmp', $conf['fperm']); 93700803e56STom N Harris io_rename($fn.'.tmp', $fn.'.idx'); 93800803e56STom N Harris if ($suffix !== '') 93980fb93f6STom N Harris $this->cacheIndexDir($idx, $suffix); 94000803e56STom N Harris return true; 94100803e56STom N Harris } 94200803e56STom N Harris 94300803e56STom N Harris /** 94400803e56STom N Harris * Retrieve or insert a value in the index. 94500803e56STom N Harris * 946b9d8cc1eSTom N Harris * @param string $idx name of the index 947b9d8cc1eSTom N Harris * @param string $suffix subpart identifier 948b9d8cc1eSTom N Harris * @param string $value line to find in the index 949b9d8cc1eSTom N Harris * @return int line number of the value in the index 95000803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 95100803e56STom N Harris */ 95280fb93f6STom N Harris protected function addIndexKey($idx, $suffix, $value) { 95380fb93f6STom N Harris $index = $this->getIndex($idx, $suffix); 95400803e56STom N Harris $id = array_search($value, $index); 95500803e56STom N Harris if ($id === false) { 95600803e56STom N Harris $id = count($index); 95700803e56STom N Harris $index[$id] = $value; 95880fb93f6STom N Harris if (!$this->saveIndex($idx, $suffix, $index)) { 95900803e56STom N Harris trigger_error("Failed to write $idx index", E_USER_ERROR); 96000803e56STom N Harris return false; 96100803e56STom N Harris } 96200803e56STom N Harris } 96300803e56STom N Harris return $id; 96400803e56STom N Harris } 96500803e56STom N Harris 96680fb93f6STom N Harris protected function cacheIndexDir($idx, $suffix, $delete=false) { 96700803e56STom N Harris global $conf; 96800803e56STom N Harris if ($idx == 'i') 96900803e56STom N Harris $cachename = $conf['indexdir'].'/lengths'; 97000803e56STom N Harris else 97100803e56STom N Harris $cachename = $conf['indexdir'].'/'.$idx.'lengths'; 97200803e56STom N Harris $lengths = @file($cachename.'.idx', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 97300803e56STom N Harris if ($lengths === false) $lengths = array(); 97400803e56STom N Harris $old = array_search((string)$suffix, $lengths); 97500803e56STom N Harris if (empty($lines)) { 97600803e56STom N Harris if ($old === false) return; 97700803e56STom N Harris unset($lengths[$old]); 97800803e56STom N Harris } else { 97900803e56STom N Harris if ($old !== false) return; 98000803e56STom N Harris $lengths[] = $suffix; 98100803e56STom N Harris sort($lengths); 98200803e56STom N Harris } 98300803e56STom N Harris $fh = @fopen($cachename.'.tmp', 'w'); 98400803e56STom N Harris if (!$fh) { 98500803e56STom N Harris trigger_error("Failed to write index cache", E_USER_ERROR); 98600803e56STom N Harris return; 98700803e56STom N Harris } 98800803e56STom N Harris @fwrite($fh, implode("\n", $lengths)); 98900803e56STom N Harris @fclose($fh); 99000803e56STom N Harris if (isset($conf['fperm'])) 99100803e56STom N Harris chmod($cachename.'.tmp', $conf['fperm']); 99200803e56STom N Harris io_rename($cachename.'.tmp', $cachename.'.idx'); 99300803e56STom N Harris } 99400803e56STom N Harris 99500803e56STom N Harris /** 99600803e56STom N Harris * Get the list of lengths indexed in the wiki. 99700803e56STom N Harris * 99800803e56STom N Harris * Read the index directory or a cache file and returns 99900803e56STom N Harris * a sorted array of lengths of the words used in the wiki. 100000803e56STom N Harris * 100100803e56STom N Harris * @author YoBoY <yoboy.leguesh@gmail.com> 100200803e56STom N Harris */ 100380fb93f6STom N Harris protected function listIndexLengths() { 100400803e56STom N Harris global $conf; 100500803e56STom N Harris $cachename = $conf['indexdir'].'/lengths'; 100600803e56STom N Harris clearstatcache(); 100700803e56STom N Harris if (@file_exists($cachename.'.idx')) { 100800803e56STom N Harris $lengths = @file($cachename.'.idx', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 100900803e56STom N Harris if ($lengths !== false) { 101000803e56STom N Harris $idx = array(); 101100803e56STom N Harris foreach ($lengths as $length) 101200803e56STom N Harris $idx[] = (int)$length; 101300803e56STom N Harris return $idx; 101400803e56STom N Harris } 101500803e56STom N Harris } 101600803e56STom N Harris 101700803e56STom N Harris $dir = @opendir($conf['indexdir']); 101800803e56STom N Harris if ($dir === false) 101900803e56STom N Harris return array(); 102000803e56STom N Harris $lengths[] = array(); 102100803e56STom N Harris while (($f = readdir($dir)) !== false) { 102200803e56STom N Harris if (substr($f, 0, 1) == 'i' && substr($f, -4) == '.idx') { 102300803e56STom N Harris $i = substr($f, 1, -4); 102400803e56STom N Harris if (is_numeric($i)) 102500803e56STom N Harris $lengths[] = (int)$i; 102600803e56STom N Harris } 102700803e56STom N Harris } 102800803e56STom N Harris closedir($dir); 102900803e56STom N Harris sort($lengths); 103000803e56STom N Harris // save this in a file 103100803e56STom N Harris $fh = @fopen($cachename.'.tmp', 'w'); 103200803e56STom N Harris if (!$fh) { 103300803e56STom N Harris trigger_error("Failed to write index cache", E_USER_ERROR); 103400803e56STom N Harris return; 103500803e56STom N Harris } 103600803e56STom N Harris @fwrite($fh, implode("\n", $lengths)); 103700803e56STom N Harris @fclose($fh); 103800803e56STom N Harris if (isset($conf['fperm'])) 103900803e56STom N Harris chmod($cachename.'.tmp', $conf['fperm']); 104000803e56STom N Harris io_rename($cachename.'.tmp', $cachename.'.idx'); 104100803e56STom N Harris 104200803e56STom N Harris return $lengths; 104300803e56STom N Harris } 104400803e56STom N Harris 104500803e56STom N Harris /** 104600803e56STom N Harris * Get the word lengths that have been indexed. 104700803e56STom N Harris * 104800803e56STom N Harris * Reads the index directory and returns an array of lengths 104900803e56STom N Harris * that there are indices for. 105000803e56STom N Harris * 105100803e56STom N Harris * @author YoBoY <yoboy.leguesh@gmail.com> 105200803e56STom N Harris */ 105380fb93f6STom N Harris protected function indexLengths($filter) { 105400803e56STom N Harris global $conf; 105500803e56STom N Harris $idx = array(); 105600803e56STom N Harris if (is_array($filter)) { 105700803e56STom N Harris // testing if index files exist only 105800803e56STom N Harris $path = $conf['indexdir']."/i"; 105900803e56STom N Harris foreach ($filter as $key => $value) { 106000803e56STom N Harris if (@file_exists($path.$key.'.idx')) 106100803e56STom N Harris $idx[] = $key; 106200803e56STom N Harris } 106300803e56STom N Harris } else { 106400803e56STom N Harris $lengths = idx_listIndexLengths(); 106500803e56STom N Harris foreach ($lengths as $key => $length) { 106600803e56STom N Harris // keep all the values equal or superior 106700803e56STom N Harris if ((int)$length >= (int)$filter) 106800803e56STom N Harris $idx[] = $length; 106900803e56STom N Harris } 107000803e56STom N Harris } 107100803e56STom N Harris return $idx; 107200803e56STom N Harris } 107300803e56STom N Harris 107400803e56STom N Harris /** 107500803e56STom N Harris * Insert or replace a tuple in a line. 107600803e56STom N Harris * 107700803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 107800803e56STom N Harris */ 107980fb93f6STom N Harris protected function updateTuple($line, $id, $count) { 108000803e56STom N Harris $newLine = $line; 108100803e56STom N Harris if ($newLine !== '') 108200803e56STom N Harris $newLine = preg_replace('/(^|:)'.preg_quote($id,'/').'\*\d*/', '', $newLine); 108300803e56STom N Harris $newLine = trim($newLine, ':'); 108400803e56STom N Harris if ($count) { 1085d64516f5SMichael Hamann if (strlen($newLine) > 0) 108600803e56STom N Harris return "$id*$count:".$newLine; 108700803e56STom N Harris else 108800803e56STom N Harris return "$id*$count".$newLine; 108900803e56STom N Harris } 109000803e56STom N Harris return $newLine; 109100803e56STom N Harris } 109200803e56STom N Harris 109300803e56STom N Harris /** 109400803e56STom N Harris * Split a line into an array of tuples. 109500803e56STom N Harris * 109600803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 109700803e56STom N Harris * @author Andreas Gohr <andi@splitbrain.org> 109800803e56STom N Harris */ 109980fb93f6STom N Harris protected function parseTuples(&$keys, $line) { 110000803e56STom N Harris $result = array(); 110100803e56STom N Harris if ($line == '') return $result; 110200803e56STom N Harris $parts = explode(':', $line); 110300803e56STom N Harris foreach ($parts as $tuple) { 1104675bf41fSTom N Harris if ($tuple === '') continue; 110500803e56STom N Harris list($key, $cnt) = explode('*', $tuple); 1106d64516f5SMichael Hamann if (!$cnt) continue; 110700803e56STom N Harris $key = $keys[$key]; 110800803e56STom N Harris if (!$key) continue; 110900803e56STom N Harris $result[$key] = $cnt; 111000803e56STom N Harris } 111100803e56STom N Harris return $result; 111200803e56STom N Harris } 1113175193d2STom N Harris 1114175193d2STom N Harris /** 1115175193d2STom N Harris * Sum the counts in a list of tuples. 1116175193d2STom N Harris * 1117175193d2STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 1118175193d2STom N Harris */ 111980fb93f6STom N Harris protected function countTuples($line) { 1120175193d2STom N Harris $freq = 0; 1121175193d2STom N Harris $parts = explode(':', $line); 1122175193d2STom N Harris foreach ($parts as $tuple) { 1123675bf41fSTom N Harris if ($tuple === '') continue; 1124175193d2STom N Harris list($pid, $cnt) = explode('*', $tuple); 1125175193d2STom N Harris $freq += (int)$cnt; 1126175193d2STom N Harris } 1127175193d2STom N Harris return $freq; 1128175193d2STom N Harris } 112900803e56STom N Harris} 113000803e56STom N Harris 113100803e56STom N Harris/** 113200803e56STom N Harris * Create an instance of the indexer. 113300803e56STom N Harris * 1134*b3d1090eSMichael Hamann * @return Doku_Indexer a Doku_Indexer 113500803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 113600803e56STom N Harris */ 11379b41be24STom N Harrisfunction idx_get_indexer() { 113800803e56STom N Harris static $Indexer = null; 113900803e56STom N Harris if (is_null($Indexer)) { 114000803e56STom N Harris $Indexer = new Doku_Indexer(); 114100803e56STom N Harris } 114200803e56STom N Harris return $Indexer; 114300803e56STom N Harris} 114400803e56STom N Harris 114500803e56STom N Harris/** 114600803e56STom N Harris * Returns words that will be ignored. 114700803e56STom N Harris * 114800803e56STom N Harris * @return array list of stop words 114900803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 115000803e56STom N Harris */ 115100803e56STom N Harrisfunction & idx_get_stopwords() { 115200803e56STom N Harris static $stopwords = null; 115300803e56STom N Harris if (is_null($stopwords)) { 115400803e56STom N Harris global $conf; 115500803e56STom N Harris $swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt'; 115600803e56STom N Harris if(@file_exists($swfile)){ 115700803e56STom N Harris $stopwords = file($swfile, FILE_IGNORE_NEW_LINES); 115800803e56STom N Harris }else{ 115900803e56STom N Harris $stopwords = array(); 116000803e56STom N Harris } 116100803e56STom N Harris } 116200803e56STom N Harris return $stopwords; 116300803e56STom N Harris} 116400803e56STom N Harris 116500803e56STom N Harris/** 116600803e56STom N Harris * Adds/updates the search index for the given page 116700803e56STom N Harris * 116800803e56STom N Harris * Locking is handled internally. 116900803e56STom N Harris * 117000803e56STom N Harris * @param string $page name of the page to index 11719b41be24STom N Harris * @param boolean $verbose print status messages 1172d041f8dbSMichael Hamann * @param boolean $force force reindexing even when the index is up to date 117300803e56STom N Harris * @return boolean the function completed successfully 117400803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 117500803e56STom N Harris */ 1176d041f8dbSMichael Hamannfunction idx_addPage($page, $verbose=false, $force=false) { 11779b41be24STom N Harris $idxtag = metaFN($page,'.indexed'); 1178a23ac4d7SMichael Hamann // check if page was deleted but is still in the index 1179bbc85ee4STom N Harris if (!page_exists($page)) { 1180bbc85ee4STom N Harris if (!@file_exists($idxtag)) { 1181bbc85ee4STom N Harris if ($verbose) print("Indexer: $page does not exist, ignoring".DOKU_LF); 1182bbc85ee4STom N Harris return false; 1183bbc85ee4STom N Harris } 1184bbc85ee4STom N Harris $Indexer = idx_get_indexer(); 1185bbc85ee4STom N Harris $result = $Indexer->deletePage($page); 1186bbc85ee4STom N Harris if ($result === "locked") { 1187bbc85ee4STom N Harris if ($verbose) print("Indexer: locked".DOKU_LF); 1188bbc85ee4STom N Harris return false; 1189bbc85ee4STom N Harris } 1190bbc85ee4STom N Harris @unlink($idxtag); 1191bbc85ee4STom N Harris return $result; 1192bbc85ee4STom N Harris } 1193a23ac4d7SMichael Hamann 1194a23ac4d7SMichael Hamann // check if indexing needed 1195a23ac4d7SMichael Hamann if(!$force && @file_exists($idxtag)){ 1196a23ac4d7SMichael Hamann if(trim(io_readFile($idxtag)) == idx_get_version()){ 1197a23ac4d7SMichael Hamann $last = @filemtime($idxtag); 1198a23ac4d7SMichael Hamann if($last > @filemtime(wikiFN($page))){ 1199a23ac4d7SMichael Hamann if ($verbose) print("Indexer: index for $page up to date".DOKU_LF); 1200a23ac4d7SMichael Hamann return false; 1201a23ac4d7SMichael Hamann } 1202a23ac4d7SMichael Hamann } 1203a23ac4d7SMichael Hamann } 1204a23ac4d7SMichael Hamann 120565aa8490SMichael Hamann $indexenabled = p_get_metadata($page, 'internal index', METADATA_RENDER_UNLIMITED); 1206bbc85ee4STom N Harris if ($indexenabled === false) { 1207bbc85ee4STom N Harris $result = false; 1208bbc85ee4STom N Harris if (@file_exists($idxtag)) { 1209bbc85ee4STom N Harris $Indexer = idx_get_indexer(); 1210bbc85ee4STom N Harris $result = $Indexer->deletePage($page); 1211bbc85ee4STom N Harris if ($result === "locked") { 1212bbc85ee4STom N Harris if ($verbose) print("Indexer: locked".DOKU_LF); 1213bbc85ee4STom N Harris return false; 1214bbc85ee4STom N Harris } 1215bbc85ee4STom N Harris @unlink($idxtag); 1216bbc85ee4STom N Harris } 1217bbc85ee4STom N Harris if ($verbose) print("Indexer: index disabled for $page".DOKU_LF); 1218bbc85ee4STom N Harris return $result; 1219bbc85ee4STom N Harris } 1220bbc85ee4STom N Harris 122100803e56STom N Harris $body = ''; 122239d6fd30SMichael Hamann $metadata = array(); 122365aa8490SMichael Hamann $metadata['title'] = p_get_metadata($page, 'title', METADATA_RENDER_UNLIMITED); 122465aa8490SMichael Hamann if (($references = p_get_metadata($page, 'relation references', METADATA_RENDER_UNLIMITED)) !== null) 122539d6fd30SMichael Hamann $metadata['relation_references'] = array_keys($references); 1226a424180eSMichael Hamann else 1227a424180eSMichael Hamann $metadata['relation_references'] = array(); 122839d6fd30SMichael Hamann $data = compact('page', 'body', 'metadata'); 122900803e56STom N Harris $evt = new Doku_Event('INDEXER_PAGE_ADD', $data); 123039d6fd30SMichael Hamann if ($evt->advise_before()) $data['body'] = $data['body'] . " " . rawWiki($page); 123100803e56STom N Harris $evt->advise_after(); 123200803e56STom N Harris unset($evt); 123339d6fd30SMichael Hamann extract($data); 123400803e56STom N Harris 12359b41be24STom N Harris $Indexer = idx_get_indexer(); 12369b41be24STom N Harris $result = $Indexer->addPageWords($page, $body); 1237e1e1a7e0SMichael Hamann if ($result === "locked") { 12389b41be24STom N Harris if ($verbose) print("Indexer: locked".DOKU_LF); 12399b41be24STom N Harris return false; 12409b41be24STom N Harris } 1241320f489aSMichael Hamann 1242320f489aSMichael Hamann if ($result) { 124339d6fd30SMichael Hamann $result = $Indexer->addMetaKeys($page, $metadata); 1244320f489aSMichael Hamann if ($result === "locked") { 1245320f489aSMichael Hamann if ($verbose) print("Indexer: locked".DOKU_LF); 1246320f489aSMichael Hamann return false; 1247320f489aSMichael Hamann } 1248320f489aSMichael Hamann } 1249320f489aSMichael Hamann 12509b41be24STom N Harris if ($result) 12519b41be24STom N Harris io_saveFile(metaFN($page,'.indexed'), idx_get_version()); 12529b41be24STom N Harris if ($verbose) { 12539b41be24STom N Harris print("Indexer: finished".DOKU_LF); 12549b41be24STom N Harris return true; 12559b41be24STom N Harris } 12569b41be24STom N Harris return $result; 125700803e56STom N Harris} 125800803e56STom N Harris 125900803e56STom N Harris/** 126000803e56STom N Harris * Find tokens in the fulltext index 126100803e56STom N Harris * 126200803e56STom N Harris * Takes an array of words and will return a list of matching 126300803e56STom N Harris * pages for each one. 1264488dd6ceSAndreas Gohr * 126563773904SAndreas Gohr * Important: No ACL checking is done here! All results are 126663773904SAndreas Gohr * returned, regardless of permissions 126763773904SAndreas Gohr * 12689b41be24STom N Harris * @param arrayref $words list of words to search for 126900803e56STom N Harris * @return array list of pages found, associated with the search terms 1270488dd6ceSAndreas Gohr */ 12719b41be24STom N Harrisfunction idx_lookup(&$words) { 12729b41be24STom N Harris $Indexer = idx_get_indexer(); 127300803e56STom N Harris return $Indexer->lookup($words); 1274488dd6ceSAndreas Gohr} 1275488dd6ceSAndreas Gohr 1276488dd6ceSAndreas Gohr/** 127700803e56STom N Harris * Split a string into tokens 1278488dd6ceSAndreas Gohr * 1279488dd6ceSAndreas Gohr */ 128000803e56STom N Harrisfunction idx_tokenizer($string, $wc=false) { 12819b41be24STom N Harris $Indexer = idx_get_indexer(); 128200803e56STom N Harris return $Indexer->tokenizer($string, $wc); 1283488dd6ceSAndreas Gohr} 128400803e56STom N Harris 128500803e56STom N Harris/* For compatibility */ 1286488dd6ceSAndreas Gohr 1287f5eb7cf0SAndreas Gohr/** 128800803e56STom N Harris * Read the list of words in an index (if it exists). 1289f5eb7cf0SAndreas Gohr * 12904e1bf408STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 1291f5eb7cf0SAndreas Gohr */ 129200803e56STom N Harrisfunction idx_getIndex($idx, $suffix) { 12931c07b9e6STom N Harris global $conf; 129400803e56STom N Harris $fn = $conf['indexdir'].'/'.$idx.$suffix.'.idx'; 129500803e56STom N Harris if (!@file_exists($fn)) return array(); 129600803e56STom N Harris return file($fn); 129700803e56STom N Harris} 1298f5eb7cf0SAndreas Gohr 129900803e56STom N Harris/** 130000803e56STom N Harris * Get the list of lengths indexed in the wiki. 130100803e56STom N Harris * 130200803e56STom N Harris * Read the index directory or a cache file and returns 130300803e56STom N Harris * a sorted array of lengths of the words used in the wiki. 130400803e56STom N Harris * 130500803e56STom N Harris * @author YoBoY <yoboy.leguesh@gmail.com> 130600803e56STom N Harris */ 130700803e56STom N Harrisfunction idx_listIndexLengths() { 130800803e56STom N Harris global $conf; 130900803e56STom N Harris // testing what we have to do, create a cache file or not. 131000803e56STom N Harris if ($conf['readdircache'] == 0) { 131100803e56STom N Harris $docache = false; 13121c07b9e6STom N Harris } else { 131300803e56STom N Harris clearstatcache(); 131400803e56STom N Harris if (@file_exists($conf['indexdir'].'/lengths.idx') 131500803e56STom N Harris && (time() < @filemtime($conf['indexdir'].'/lengths.idx') + $conf['readdircache'])) { 131600803e56STom N Harris if (($lengths = @file($conf['indexdir'].'/lengths.idx', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)) !== false) { 131700803e56STom N Harris $idx = array(); 131800803e56STom N Harris foreach ($lengths as $length) { 131900803e56STom N Harris $idx[] = (int)$length; 132000803e56STom N Harris } 132100803e56STom N Harris return $idx; 1322f5eb7cf0SAndreas Gohr } 13231c07b9e6STom N Harris } 132400803e56STom N Harris $docache = true; 132500803e56STom N Harris } 13264e1bf408STom N Harris 132700803e56STom N Harris if ($conf['readdircache'] == 0 || $docache) { 132800803e56STom N Harris $dir = @opendir($conf['indexdir']); 132900803e56STom N Harris if ($dir === false) 133000803e56STom N Harris return array(); 133126f7dbf5SMichael Hamann $idx = array(); 133200803e56STom N Harris while (($f = readdir($dir)) !== false) { 133300803e56STom N Harris if (substr($f, 0, 1) == 'i' && substr($f, -4) == '.idx') { 133400803e56STom N Harris $i = substr($f, 1, -4); 133500803e56STom N Harris if (is_numeric($i)) 133600803e56STom N Harris $idx[] = (int)$i; 133700803e56STom N Harris } 133800803e56STom N Harris } 133900803e56STom N Harris closedir($dir); 134000803e56STom N Harris sort($idx); 134100803e56STom N Harris // save this in a file 134200803e56STom N Harris if ($docache) { 134300803e56STom N Harris $handle = @fopen($conf['indexdir'].'/lengths.idx', 'w'); 134400803e56STom N Harris @fwrite($handle, implode("\n", $idx)); 134500803e56STom N Harris @fclose($handle); 134600803e56STom N Harris } 134700803e56STom N Harris return $idx; 134800803e56STom N Harris } 134900803e56STom N Harris 135000803e56STom N Harris return array(); 135100803e56STom N Harris} 135200803e56STom N Harris 135300803e56STom N Harris/** 135400803e56STom N Harris * Get the word lengths that have been indexed. 135500803e56STom N Harris * 135600803e56STom N Harris * Reads the index directory and returns an array of lengths 135700803e56STom N Harris * that there are indices for. 135800803e56STom N Harris * 135900803e56STom N Harris * @author YoBoY <yoboy.leguesh@gmail.com> 136000803e56STom N Harris */ 136100803e56STom N Harrisfunction idx_indexLengths($filter) { 136200803e56STom N Harris global $conf; 136300803e56STom N Harris $idx = array(); 136400803e56STom N Harris if (is_array($filter)) { 136500803e56STom N Harris // testing if index files exist only 136600803e56STom N Harris $path = $conf['indexdir']."/i"; 136700803e56STom N Harris foreach ($filter as $key => $value) { 136800803e56STom N Harris if (@file_exists($path.$key.'.idx')) 136900803e56STom N Harris $idx[] = $key; 137000803e56STom N Harris } 1371f5eb7cf0SAndreas Gohr } else { 137200803e56STom N Harris $lengths = idx_listIndexLengths(); 137300803e56STom N Harris foreach ($lengths as $key => $length) { 137400803e56STom N Harris // keep all the values equal or superior 137500803e56STom N Harris if ((int)$length >= (int)$filter) 137600803e56STom N Harris $idx[] = $length; 1377f5eb7cf0SAndreas Gohr } 137800803e56STom N Harris } 137900803e56STom N Harris return $idx; 1380f5eb7cf0SAndreas Gohr} 1381f5eb7cf0SAndreas Gohr 138200803e56STom N Harris/** 138300803e56STom N Harris * Clean a name of a key for use as a file name. 138400803e56STom N Harris * 138500803e56STom N Harris * Romanizes non-latin characters, then strips away anything that's 138600803e56STom N Harris * not a letter, number, or underscore. 138700803e56STom N Harris * 138800803e56STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 138900803e56STom N Harris */ 139000803e56STom N Harrisfunction idx_cleanName($name) { 139100803e56STom N Harris $name = utf8_romanize(trim((string)$name)); 139200803e56STom N Harris $name = preg_replace('#[ \./\\:-]+#', '_', $name); 139300803e56STom N Harris $name = preg_replace('/[^A-Za-z0-9_]/', '', $name); 139400803e56STom N Harris return strtolower($name); 1395f5eb7cf0SAndreas Gohr} 1396f5eb7cf0SAndreas Gohr 139700803e56STom N Harris//Setup VIM: ex: et ts=4 : 1398