1b4ce25e9SAndreas Gohr<?php 2b4ce25e9SAndreas Gohr/** 3b4ce25e9SAndreas Gohr * Common DokuWiki functions 4b4ce25e9SAndreas Gohr * 5b4ce25e9SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6b4ce25e9SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7b4ce25e9SAndreas Gohr */ 8b4ce25e9SAndreas Gohr 9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 10b4ce25e9SAndreas Gohrrequire_once(DOKU_INC.'inc/io.php'); 11b4ce25e9SAndreas Gohrrequire_once(DOKU_INC.'inc/utf8.php'); 12b4ce25e9SAndreas Gohrrequire_once(DOKU_INC.'inc/parserutils.php'); 13b4ce25e9SAndreas Gohr 1433815ce2SChris Smith// set the minimum token length to use in the index (note, this doesn't apply to numeric tokens) 15d3fb3219SAndreas Gohrif (!defined('IDX_MINWORDLENGTH')) define('IDX_MINWORDLENGTH',2); 1633815ce2SChris Smith 1793a60ad2SAndreas Gohr// Asian characters are handled as words. The following regexp defines the 1893a60ad2SAndreas Gohr// Unicode-Ranges for Asian characters 1993a60ad2SAndreas Gohr// Ranges taken from http://en.wikipedia.org/wiki/Unicode_block 2093a60ad2SAndreas Gohr// I'm no language expert. If you think some ranges are wrongly chosen or 2193a60ad2SAndreas Gohr// a range is missing, please contact me 22d5b23302STom N Harrisdefine('IDX_ASIAN1','[\x{0E00}-\x{0E7F}]'); // Thai 23d5b23302STom N Harrisdefine('IDX_ASIAN2','['. 24d5b23302STom N Harris '\x{2E80}-\x{3040}'. // CJK -> Hangul 25d5b23302STom N Harris '\x{309D}-\x{30A0}'. 26a0c5c349STom N Harris '\x{30FD}-\x{31EF}\x{3200}-\x{D7AF}'. 2793a60ad2SAndreas Gohr '\x{F900}-\x{FAFF}'. // CJK Compatibility Ideographs 2893a60ad2SAndreas Gohr '\x{FE30}-\x{FE4F}'. // CJK Compatibility Forms 2993a60ad2SAndreas Gohr ']'); 30d5b23302STom N Harrisdefine('IDX_ASIAN3','['. // Hiragana/Katakana (can be two characters) 31d5b23302STom N Harris '\x{3042}\x{3044}\x{3046}\x{3048}'. 32d5b23302STom N Harris '\x{304A}-\x{3062}\x{3064}-\x{3082}'. 33d5b23302STom N Harris '\x{3084}\x{3086}\x{3088}-\x{308D}'. 34d5b23302STom N Harris '\x{308F}-\x{3094}'. 35d5b23302STom N Harris '\x{30A2}\x{30A4}\x{30A6}\x{30A8}'. 36d5b23302STom N Harris '\x{30AA}-\x{30C2}\x{30C4}-\x{30E2}'. 37d5b23302STom N Harris '\x{30E4}\x{30E6}\x{30E8}-\x{30ED}'. 38d5b23302STom N Harris '\x{30EF}-\x{30F4}\x{30F7}-\x{30FA}'. 39d5b23302STom N Harris ']['. 40d5b23302STom N Harris '\x{3041}\x{3043}\x{3045}\x{3047}\x{3049}'. 41d5b23302STom N Harris '\x{3063}\x{3083}\x{3085}\x{3087}\x{308E}\x{3095}-\x{309C}'. 42d5b23302STom N Harris '\x{30A1}\x{30A3}\x{30A5}\x{30A7}\x{30A9}'. 43d5b23302STom N Harris '\x{30C3}\x{30E3}\x{30E5}\x{30E7}\x{30EE}\x{30F5}\x{30F6}\x{30FB}\x{30FC}'. 44d5b23302STom N Harris '\x{31F0}-\x{31FF}'. 45d5b23302STom N Harris ']?'); 46699b8a0bSAndreas Gohrdefine('IDX_ASIAN', '(?:'.IDX_ASIAN1.'|'.IDX_ASIAN2.'|'.IDX_ASIAN3.')'); 4793a60ad2SAndreas Gohr 48b4ce25e9SAndreas Gohr/** 49d5b23302STom N Harris * Measure the length of a string. 50d5b23302STom N Harris * Differs from strlen in handling of asian characters. 51d5b23302STom N Harris * 52d5b23302STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 53d5b23302STom N Harris */ 54d5b23302STom N Harrisfunction wordlen($w){ 55d5b23302STom N Harris $l = strlen($w); 56d5b23302STom N Harris // If left alone, all chinese "words" will get put into w3.idx 57d5b23302STom N Harris // So the "length" of a "word" is faked 58d5b23302STom N Harris if(preg_match('/'.IDX_ASIAN2.'/u',$w)) 59d5b23302STom N Harris $l += ord($w) - 0xE1; // Lead bytes from 0xE2-0xEF 60d5b23302STom N Harris return $l; 61d5b23302STom N Harris} 62d5b23302STom N Harris 63d5b23302STom N Harris/** 64579b0f7eSTNHarris * Write a list of strings to an index file. 65579b0f7eSTNHarris * 66579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 67579b0f7eSTNHarris */ 68b6344591STom N Harrisfunction idx_saveIndex($pre, $wlen, &$idx){ 69579b0f7eSTNHarris global $conf; 70579b0f7eSTNHarris $fn = $conf['indexdir'].'/'.$pre.$wlen; 71579b0f7eSTNHarris $fh = @fopen($fn.'.tmp','w'); 72579b0f7eSTNHarris if(!$fh) return false; 73b6344591STom N Harris foreach ($idx as $line) { 74b6344591STom N Harris fwrite($fh,$line); 75b6344591STom N Harris } 76579b0f7eSTNHarris fclose($fh); 77*c66972f2SAdrian Lang if(isset($conf['fperm'])) chmod($fn.'.tmp', $conf['fperm']); 78579b0f7eSTNHarris io_rename($fn.'.tmp', $fn.'.idx'); 79579b0f7eSTNHarris return true; 80579b0f7eSTNHarris} 81579b0f7eSTNHarris 82579b0f7eSTNHarris/** 83dd35e9c9SAndreas Gohr * Append a given line to an index file. 84dd35e9c9SAndreas Gohr * 85dd35e9c9SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 86dd35e9c9SAndreas Gohr */ 87dd35e9c9SAndreas Gohrfunction idx_appendIndex($pre, $wlen, $line){ 88dd35e9c9SAndreas Gohr global $conf; 89dd35e9c9SAndreas Gohr $fn = $conf['indexdir'].'/'.$pre.$wlen; 90dd35e9c9SAndreas Gohr $fh = @fopen($fn.'.idx','a'); 91dd35e9c9SAndreas Gohr if(!$fh) return false; 92dd35e9c9SAndreas Gohr fwrite($fh,$line); 93dd35e9c9SAndreas Gohr fclose($fh); 94dd35e9c9SAndreas Gohr return true; 95dd35e9c9SAndreas Gohr} 96dd35e9c9SAndreas Gohr 97dd35e9c9SAndreas Gohr/** 98579b0f7eSTNHarris * Read the list of words in an index (if it exists). 99579b0f7eSTNHarris * 100579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 101579b0f7eSTNHarris */ 102579b0f7eSTNHarrisfunction idx_getIndex($pre, $wlen){ 103579b0f7eSTNHarris global $conf; 104579b0f7eSTNHarris $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx'; 105579b0f7eSTNHarris if(!@file_exists($fn)) return array(); 106579b0f7eSTNHarris return file($fn); 107579b0f7eSTNHarris} 108579b0f7eSTNHarris 109579b0f7eSTNHarris/** 110579b0f7eSTNHarris * Create an empty index file if it doesn't exist yet. 111579b0f7eSTNHarris * 112b6344591STom N Harris * FIXME: This function isn't currently used. It will probably be removed soon. 113b6344591STom N Harris * 114579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 115579b0f7eSTNHarris */ 116579b0f7eSTNHarrisfunction idx_touchIndex($pre, $wlen){ 117579b0f7eSTNHarris global $conf; 118579b0f7eSTNHarris $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx'; 119579b0f7eSTNHarris if(!@file_exists($fn)){ 120579b0f7eSTNHarris touch($fn); 121579b0f7eSTNHarris if($conf['fperm']) chmod($fn, $conf['fperm']); 122579b0f7eSTNHarris } 123579b0f7eSTNHarris} 124579b0f7eSTNHarris 125579b0f7eSTNHarris/** 126b6344591STom N Harris * Read a line ending with \n. 127b6344591STom N Harris * Returns false on EOF. 128b6344591STom N Harris * 129b6344591STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 130b6344591STom N Harris */ 131b6344591STom N Harrisfunction _freadline($fh) { 132b6344591STom N Harris if (feof($fh)) return false; 133b6344591STom N Harris $ln = ''; 134b6344591STom N Harris while (($buf = fgets($fh,4096)) !== false) { 135b6344591STom N Harris $ln .= $buf; 136b6344591STom N Harris if (substr($buf,-1) == "\n") break; 137b6344591STom N Harris } 138b6344591STom N Harris if ($ln === '') return false; 139b6344591STom N Harris if (substr($ln,-1) != "\n") $ln .= "\n"; 140b6344591STom N Harris return $ln; 141b6344591STom N Harris} 142b6344591STom N Harris 143b6344591STom N Harris/** 144b6344591STom N Harris * Write a line to an index file. 145b6344591STom N Harris * 146b6344591STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 147b6344591STom N Harris */ 148b6344591STom N Harrisfunction idx_saveIndexLine($pre, $wlen, $idx, $line){ 149b6344591STom N Harris global $conf; 150b6344591STom N Harris if(substr($line,-1) != "\n") $line .= "\n"; 151b6344591STom N Harris $fn = $conf['indexdir'].'/'.$pre.$wlen; 152b6344591STom N Harris $fh = @fopen($fn.'.tmp','w'); 153b6344591STom N Harris if(!$fh) return false; 154b6344591STom N Harris $ih = @fopen($fn.'.idx','r'); 155b6344591STom N Harris if ($ih) { 156b6344591STom N Harris $ln = -1; 157b6344591STom N Harris while (($curline = _freadline($ih)) !== false) { 158b6344591STom N Harris if (++$ln == $idx) { 159b6344591STom N Harris fwrite($fh, $line); 160b6344591STom N Harris } else { 161b6344591STom N Harris fwrite($fh, $curline); 162b6344591STom N Harris } 163b6344591STom N Harris } 164b6344591STom N Harris if ($idx > $ln) { 165b6344591STom N Harris fwrite($fh,$line); 166b6344591STom N Harris } 167b6344591STom N Harris fclose($ih); 168b6344591STom N Harris } else { 169b6344591STom N Harris fwrite($fh,$line); 170b6344591STom N Harris } 171b6344591STom N Harris fclose($fh); 172b6344591STom N Harris if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']); 173b6344591STom N Harris io_rename($fn.'.tmp', $fn.'.idx'); 174b6344591STom N Harris return true; 175b6344591STom N Harris} 176b6344591STom N Harris 177b6344591STom N Harris/** 178b6344591STom N Harris * Read a single line from an index (if it exists). 179b6344591STom N Harris * 180b6344591STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 181b6344591STom N Harris */ 182b6344591STom N Harrisfunction idx_getIndexLine($pre, $wlen, $idx){ 183b6344591STom N Harris global $conf; 184b6344591STom N Harris $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx'; 185b6344591STom N Harris if(!@file_exists($fn)) return ''; 186b6344591STom N Harris $fh = @fopen($fn,'r'); 187b6344591STom N Harris if(!$fh) return ''; 188b6344591STom N Harris $ln = -1; 189b6344591STom N Harris while (($line = _freadline($fh)) !== false) { 190b6344591STom N Harris if (++$ln == $idx) break; 191b6344591STom N Harris } 192b6344591STom N Harris fclose($fh); 193b6344591STom N Harris return "$line"; 194b6344591STom N Harris} 195b6344591STom N Harris 196b6344591STom N Harris/** 19744ca0adfSAndreas Gohr * Split a page into words 19844ca0adfSAndreas Gohr * 199c9db30f9SAndreas Gohr * Returns an array of word counts, false if an error occurred. 200579b0f7eSTNHarris * Array is keyed on the word length, then the word index. 20144ca0adfSAndreas Gohr * 20244ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 20317f42b01SChris Smith * @author Christopher Smith <chris@jalakai.co.uk> 204b4ce25e9SAndreas Gohr */ 20544ca0adfSAndreas Gohrfunction idx_getPageWords($page){ 20644ca0adfSAndreas Gohr global $conf; 2077367b368SAndreas Gohr $swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt'; 2087367b368SAndreas Gohr if(@file_exists($swfile)){ 2097367b368SAndreas Gohr $stopwords = file($swfile); 2107367b368SAndreas Gohr }else{ 2117367b368SAndreas Gohr $stopwords = array(); 2127367b368SAndreas Gohr } 21344ca0adfSAndreas Gohr 214a6a30c1aSEsther Brunner $body = ''; 21515018435SAndreas Gohr $data = array($page, $body); 21615018435SAndreas Gohr $evt = new Doku_Event('INDEXER_PAGE_ADD', $data); 21715018435SAndreas Gohr if ($evt->advise_before()) $data[1] .= rawWiki($page); 218a6a30c1aSEsther Brunner $evt->advise_after(); 219a6a30c1aSEsther Brunner unset($evt); 220a6a30c1aSEsther Brunner 22115018435SAndreas Gohr list($page,$body) = $data; 22215018435SAndreas Gohr 22317f42b01SChris Smith $body = strtr($body, "\r\n\t", ' '); 22417f42b01SChris Smith $tokens = explode(' ', $body); 22517f42b01SChris Smith $tokens = array_count_values($tokens); // count the frequency of each token 22617f42b01SChris Smith 2276b06b652Schris // ensure the deaccented or romanised page names of internal links are added to the token array 2286b06b652Schris // (this is necessary for the backlink function -- there maybe a better way!) 2296b06b652Schris if ($conf['deaccent']) { 2306b06b652Schris $links = p_get_metadata($page,'relation references'); 2316b06b652Schris 2323fc667cfSchris if (!empty($links)) { 2336b06b652Schris $tmp = join(' ',array_keys($links)); // make a single string 2346b06b652Schris $tmp = strtr($tmp, ':', ' '); // replace namespace separator with a space 2356b06b652Schris $link_tokens = array_unique(explode(' ', $tmp)); // break into tokens 2366b06b652Schris 2376b06b652Schris foreach ($link_tokens as $link_token) { 2386b06b652Schris if (isset($tokens[$link_token])) continue; 2396b06b652Schris $tokens[$link_token] = 1; 2406b06b652Schris } 2416b06b652Schris } 2423fc667cfSchris } 2436b06b652Schris 24417f42b01SChris Smith $words = array(); 24517f42b01SChris Smith foreach ($tokens as $word => $count) { 246579b0f7eSTNHarris $arr = idx_tokenizer($word,$stopwords); 24717f42b01SChris Smith $arr = array_count_values($arr); 24817f42b01SChris Smith foreach ($arr as $w => $c) { 249d5b23302STom N Harris $l = wordlen($w); 250579b0f7eSTNHarris if(isset($words[$l])){ 251b2bc63f0SAndreas Gohr $words[$l][$w] = $c * $count + (isset($words[$l][$w]) ? $words[$l][$w] : 0); 25217f42b01SChris Smith }else{ 253579b0f7eSTNHarris $words[$l] = array($w => $c * $count); 254579b0f7eSTNHarris } 25517f42b01SChris Smith } 25617f42b01SChris Smith } 25717f42b01SChris Smith 258579b0f7eSTNHarris // arrive here with $words = array(wordlen => array(word => frequency)) 259b4ce25e9SAndreas Gohr 260b4ce25e9SAndreas Gohr $index = array(); //resulting index 261579b0f7eSTNHarris foreach (array_keys($words) as $wlen){ 262579b0f7eSTNHarris $word_idx = idx_getIndex('w',$wlen); 263579b0f7eSTNHarris foreach ($words[$wlen] as $word => $freq) { 26444ca0adfSAndreas Gohr $wid = array_search("$word\n",$word_idx); 26544ca0adfSAndreas Gohr if(!is_int($wid)){ 266d5b23302STom N Harris $wid = count($word_idx); 26744ca0adfSAndreas Gohr $word_idx[] = "$word\n"; 268b4ce25e9SAndreas Gohr } 269579b0f7eSTNHarris if(!isset($index[$wlen])) 270579b0f7eSTNHarris $index[$wlen] = array(); 271579b0f7eSTNHarris $index[$wlen][$wid] = $freq; 27244ca0adfSAndreas Gohr } 27344ca0adfSAndreas Gohr 27444ca0adfSAndreas Gohr // save back word index 275579b0f7eSTNHarris if(!idx_saveIndex('w',$wlen,$word_idx)){ 276579b0f7eSTNHarris trigger_error("Failed to write word index", E_USER_ERROR); 27744ca0adfSAndreas Gohr return false; 27844ca0adfSAndreas Gohr } 279579b0f7eSTNHarris } 280b4ce25e9SAndreas Gohr 281b4ce25e9SAndreas Gohr return $index; 282b4ce25e9SAndreas Gohr} 283b4ce25e9SAndreas Gohr 28444ca0adfSAndreas Gohr/** 28544ca0adfSAndreas Gohr * Adds/updates the search for the given page 28644ca0adfSAndreas Gohr * 28744ca0adfSAndreas Gohr * This is the core function of the indexer which does most 28844ca0adfSAndreas Gohr * of the work. This function needs to be called with proper 28944ca0adfSAndreas Gohr * locking! 29044ca0adfSAndreas Gohr * 29144ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 29244ca0adfSAndreas Gohr */ 29344ca0adfSAndreas Gohrfunction idx_addPage($page){ 29444ca0adfSAndreas Gohr global $conf; 295b4ce25e9SAndreas Gohr 296488dd6ceSAndreas Gohr // load known documents 297579b0f7eSTNHarris $page_idx = idx_getIndex('page',''); 29844ca0adfSAndreas Gohr 29944ca0adfSAndreas Gohr // get page id (this is the linenumber in page.idx) 30044ca0adfSAndreas Gohr $pid = array_search("$page\n",$page_idx); 30144ca0adfSAndreas Gohr if(!is_int($pid)){ 302c5418046SAndreas Gohr $pid = count($page_idx); 30344ca0adfSAndreas Gohr // page was new - write back 304dd35e9c9SAndreas Gohr if (!idx_appendIndex('page','',"$page\n")){ 305d5b23302STom N Harris trigger_error("Failed to write page index", E_USER_ERROR); 306579b0f7eSTNHarris return false; 30744ca0adfSAndreas Gohr } 308d5b23302STom N Harris } 309dd35e9c9SAndreas Gohr unset($page_idx); // free memory 31044ca0adfSAndreas Gohr 311a0c5c349STom N Harris $pagewords = array(); 31244ca0adfSAndreas Gohr // get word usage in page 31344ca0adfSAndreas Gohr $words = idx_getPageWords($page); 31444ca0adfSAndreas Gohr if($words === false) return false; 31544ca0adfSAndreas Gohr 316a0c5c349STom N Harris if(!empty($words)) { 317579b0f7eSTNHarris foreach(array_keys($words) as $wlen){ 318d5b23302STom N Harris $index = idx_getIndex('i',$wlen); 319d5b23302STom N Harris foreach($words[$wlen] as $wid => $freq){ 320d5b23302STom N Harris if($wid<count($index)){ 321d5b23302STom N Harris $index[$wid] = idx_updateIndexLine($index[$wid],$pid,$freq); 322d5b23302STom N Harris }else{ 323d5b23302STom N Harris // New words **should** have been added in increasing order 324d5b23302STom N Harris // starting with the first unassigned index. 325d5b23302STom N Harris // If someone can show how this isn't true, then I'll need to sort 326d5b23302STom N Harris // or do something special. 327d5b23302STom N Harris $index[$wid] = idx_updateIndexLine('',$pid,$freq); 328d5b23302STom N Harris } 329a0c5c349STom N Harris $pagewords[] = "$wlen*$wid"; 330d5b23302STom N Harris } 331d5b23302STom N Harris // save back word index 332d5b23302STom N Harris if(!idx_saveIndex('i',$wlen,$index)){ 333d5b23302STom N Harris trigger_error("Failed to write index", E_USER_ERROR); 33444ca0adfSAndreas Gohr return false; 33544ca0adfSAndreas Gohr } 336579b0f7eSTNHarris } 337a0c5c349STom N Harris } 338a0c5c349STom N Harris 339a0c5c349STom N Harris // Remove obsolete index entries 340b6344591STom N Harris $pageword_idx = trim(idx_getIndexLine('pageword','',$pid)); 341b6344591STom N Harris if ($pageword_idx !== '') { 342b6344591STom N Harris $oldwords = explode(':',$pageword_idx); 343a0c5c349STom N Harris $delwords = array_diff($oldwords, $pagewords); 344b6344591STom N Harris $upwords = array(); 345a0c5c349STom N Harris foreach ($delwords as $word) { 346a0c5c349STom N Harris if($word=='') continue; 347a0c5c349STom N Harris list($wlen,$wid) = explode('*',$word); 348a0c5c349STom N Harris $wid = (int)$wid; 349b6344591STom N Harris $upwords[$wlen][] = $wid; 350b6344591STom N Harris } 351b6344591STom N Harris foreach ($upwords as $wlen => $widx) { 352a0c5c349STom N Harris $index = idx_getIndex('i',$wlen); 353b6344591STom N Harris foreach ($widx as $wid) { 354a0c5c349STom N Harris $index[$wid] = idx_updateIndexLine($index[$wid],$pid,0); 355b6344591STom N Harris } 356a0c5c349STom N Harris idx_saveIndex('i',$wlen,$index); 357a0c5c349STom N Harris } 358b6344591STom N Harris } 359a0c5c349STom N Harris // Save the reverse index 360b6344591STom N Harris $pageword_idx = join(':',$pagewords)."\n"; 361b6344591STom N Harris if(!idx_saveIndexLine('pageword','',$pid,$pageword_idx)){ 362a0c5c349STom N Harris trigger_error("Failed to write word index", E_USER_ERROR); 363a0c5c349STom N Harris return false; 364a0c5c349STom N Harris } 365579b0f7eSTNHarris 366579b0f7eSTNHarris return true; 36744ca0adfSAndreas Gohr} 36844ca0adfSAndreas Gohr 36944ca0adfSAndreas Gohr/** 37044ca0adfSAndreas Gohr * Write a new index line to the filehandle 37144ca0adfSAndreas Gohr * 37244ca0adfSAndreas Gohr * This function writes an line for the index file to the 37344ca0adfSAndreas Gohr * given filehandle. It removes the given document from 37444ca0adfSAndreas Gohr * the given line and readds it when $count is >0. 37544ca0adfSAndreas Gohr * 376d5b23302STom N Harris * @deprecated - see idx_updateIndexLine 37744ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 37844ca0adfSAndreas Gohr */ 37944ca0adfSAndreas Gohrfunction idx_writeIndexLine($fh,$line,$pid,$count){ 380d5b23302STom N Harris fwrite($fh,idx_updateIndexLine($line,$pid,$count)); 381d5b23302STom N Harris} 38244ca0adfSAndreas Gohr 383d5b23302STom N Harris/** 384d5b23302STom N Harris * Modify an index line with new information 385d5b23302STom N Harris * 386d5b23302STom N Harris * This returns a line of the index. It removes the 387d5b23302STom N Harris * given document from the line and readds it if 388d5b23302STom N Harris * $count is >0. 389d5b23302STom N Harris * 390d5b23302STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 391d5b23302STom N Harris * @author Andreas Gohr <andi@splitbrain.org> 392d5b23302STom N Harris */ 393d5b23302STom N Harrisfunction idx_updateIndexLine($line,$pid,$count){ 394d5b23302STom N Harris $line = trim($line); 395d5b23302STom N Harris $updated = array(); 39644ca0adfSAndreas Gohr if($line != ''){ 39744ca0adfSAndreas Gohr $parts = explode(':',$line); 39844ca0adfSAndreas Gohr // remove doc from given line 39944ca0adfSAndreas Gohr foreach($parts as $part){ 40044ca0adfSAndreas Gohr if($part == '') continue; 40144ca0adfSAndreas Gohr list($doc,$cnt) = explode('*',$part); 40244ca0adfSAndreas Gohr if($doc != $pid){ 403d5b23302STom N Harris $updated[] = $part; 40444ca0adfSAndreas Gohr } 40544ca0adfSAndreas Gohr } 40644ca0adfSAndreas Gohr } 40744ca0adfSAndreas Gohr 40844ca0adfSAndreas Gohr // add doc 40944ca0adfSAndreas Gohr if ($count){ 410d5b23302STom N Harris $updated[] = "$pid*$count"; 41144ca0adfSAndreas Gohr } 41244ca0adfSAndreas Gohr 413d5b23302STom N Harris return join(':',$updated)."\n"; 41444ca0adfSAndreas Gohr} 415b4ce25e9SAndreas Gohr 416488dd6ceSAndreas Gohr/** 417579b0f7eSTNHarris * Get the word lengths that have been indexed. 418579b0f7eSTNHarris * 419579b0f7eSTNHarris * Reads the index directory and returns an array of lengths 420579b0f7eSTNHarris * that there are indices for. 421579b0f7eSTNHarris * 422579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 423579b0f7eSTNHarris */ 424d5b23302STom N Harrisfunction idx_indexLengths(&$filter){ 425579b0f7eSTNHarris global $conf; 426579b0f7eSTNHarris $dir = @opendir($conf['indexdir']); 427579b0f7eSTNHarris if($dir===false) 428579b0f7eSTNHarris return array(); 429579b0f7eSTNHarris $idx = array(); 430d5b23302STom N Harris if(is_array($filter)){ 431579b0f7eSTNHarris while (($f = readdir($dir)) !== false) { 432579b0f7eSTNHarris if (substr($f,0,1) == 'i' && substr($f,-4) == '.idx'){ 433579b0f7eSTNHarris $i = substr($f,1,-4); 434d5b23302STom N Harris if (is_numeric($i) && isset($filter[(int)$i])) 435d5b23302STom N Harris $idx[] = (int)$i; 436d5b23302STom N Harris } 437d5b23302STom N Harris } 438d5b23302STom N Harris }else{ 439d5b23302STom N Harris // Exact match first. 440d5b23302STom N Harris if(@file_exists($conf['indexdir']."/i$filter.idx")) 441d5b23302STom N Harris $idx[] = $filter; 442d5b23302STom N Harris while (($f = readdir($dir)) !== false) { 443d5b23302STom N Harris if (substr($f,0,1) == 'i' && substr($f,-4) == '.idx'){ 444d5b23302STom N Harris $i = substr($f,1,-4); 445d5b23302STom N Harris if (is_numeric($i) && $i > $filter) 446d5b23302STom N Harris $idx[] = (int)$i; 447d5b23302STom N Harris } 448579b0f7eSTNHarris } 449579b0f7eSTNHarris } 450579b0f7eSTNHarris closedir($dir); 451579b0f7eSTNHarris return $idx; 452579b0f7eSTNHarris} 453579b0f7eSTNHarris 454579b0f7eSTNHarris/** 455d5b23302STom N Harris * Find the the index number of each search term. 456d5b23302STom N Harris * 457adb16d4fSAndreas Gohr * This will group together words that appear in the same index. 458d5b23302STom N Harris * So it should perform better, because it only opens each index once. 459d5b23302STom N Harris * Actually, it's not that great. (in my experience) Probably because of the disk cache. 460d5b23302STom N Harris * And the sorted function does more work, making it slightly slower in some cases. 461d5b23302STom N Harris * 462d5b23302STom N Harris * @param array $words The query terms. Words should only contain valid characters, 463d5b23302STom N Harris * with a '*' at either the beginning or end of the word (or both) 464d5b23302STom N Harris * @param arrayref $result Set to word => array("length*id" ...), use this to merge the 465d5b23302STom N Harris * index locations with the appropriate query term. 466d5b23302STom N Harris * @return array Set to length => array(id ...) 467d5b23302STom N Harris * 468d5b23302STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 469d5b23302STom N Harris */ 470d5b23302STom N Harrisfunction idx_getIndexWordsSorted($words,&$result){ 471d5b23302STom N Harris // parse and sort tokens 472d5b23302STom N Harris $tokens = array(); 473d5b23302STom N Harris $tokenlength = array(); 474d5b23302STom N Harris $tokenwild = array(); 475d5b23302STom N Harris foreach($words as $word){ 476d5b23302STom N Harris $result[$word] = array(); 477d5b23302STom N Harris $wild = 0; 478d5b23302STom N Harris $xword = $word; 479d5b23302STom N Harris $wlen = wordlen($word); 480d5b23302STom N Harris 481d5b23302STom N Harris // check for wildcards 482d5b23302STom N Harris if(substr($xword,0,1) == '*'){ 483d5b23302STom N Harris $xword = substr($xword,1); 484d5b23302STom N Harris $wild |= 1; 485d5b23302STom N Harris $wlen -= 1; 486d5b23302STom N Harris } 487d5b23302STom N Harris if(substr($xword,-1,1) == '*'){ 488d5b23302STom N Harris $xword = substr($xword,0,-1); 489d5b23302STom N Harris $wild |= 2; 490d5b23302STom N Harris $wlen -= 1; 491d5b23302STom N Harris } 49233815ce2SChris Smith if ($wlen < IDX_MINWORDLENGTH && $wild == 0 && !is_numeric($xword)) continue; 493d5b23302STom N Harris if(!isset($tokens[$xword])){ 494d5b23302STom N Harris $tokenlength[$wlen][] = $xword; 495d5b23302STom N Harris } 496d5b23302STom N Harris if($wild){ 497d5b23302STom N Harris $ptn = preg_quote($xword,'/'); 498d5b23302STom N Harris if(($wild&1) == 0) $ptn = '^'.$ptn; 499d5b23302STom N Harris if(($wild&2) == 0) $ptn = $ptn.'$'; 500d5b23302STom N Harris $tokens[$xword][] = array($word, '/'.$ptn.'/'); 501d5b23302STom N Harris if(!isset($tokenwild[$xword])) $tokenwild[$xword] = $wlen; 502d5b23302STom N Harris }else 503d5b23302STom N Harris $tokens[$xword][] = array($word, null); 504d5b23302STom N Harris } 505d5b23302STom N Harris asort($tokenwild); 506d5b23302STom N Harris // $tokens = array( base word => array( [ query word , grep pattern ] ... ) ... ) 507d5b23302STom N Harris // $tokenlength = array( base word length => base word ... ) 508d5b23302STom N Harris // $tokenwild = array( base word => base word length ... ) 509d5b23302STom N Harris 510d5b23302STom N Harris $length_filter = empty($tokenwild) ? $tokenlength : min(array_keys($tokenlength)); 511d5b23302STom N Harris $indexes_known = idx_indexLengths($length_filter); 512d5b23302STom N Harris if(!empty($tokenwild)) sort($indexes_known); 513d5b23302STom N Harris // get word IDs 514d5b23302STom N Harris $wids = array(); 515d5b23302STom N Harris foreach($indexes_known as $ixlen){ 516d5b23302STom N Harris $word_idx = idx_getIndex('w',$ixlen); 517d5b23302STom N Harris // handle exact search 518d5b23302STom N Harris if(isset($tokenlength[$ixlen])){ 519d5b23302STom N Harris foreach($tokenlength[$ixlen] as $xword){ 520d5b23302STom N Harris $wid = array_search("$xword\n",$word_idx); 521d5b23302STom N Harris if(is_int($wid)){ 522d5b23302STom N Harris $wids[$ixlen][] = $wid; 523d5b23302STom N Harris foreach($tokens[$xword] as $w) 524d5b23302STom N Harris $result[$w[0]][] = "$ixlen*$wid"; 525d5b23302STom N Harris } 526d5b23302STom N Harris } 527d5b23302STom N Harris } 528d5b23302STom N Harris // handle wildcard search 529d5b23302STom N Harris foreach($tokenwild as $xword => $wlen){ 530d5b23302STom N Harris if($wlen >= $ixlen) break; 531d5b23302STom N Harris foreach($tokens[$xword] as $w){ 532d5b23302STom N Harris if(is_null($w[1])) continue; 533d5b23302STom N Harris foreach(array_keys(preg_grep($w[1],$word_idx)) as $wid){ 534d5b23302STom N Harris $wids[$ixlen][] = $wid; 535d5b23302STom N Harris $result[$w[0]][] = "$ixlen*$wid"; 536d5b23302STom N Harris } 537d5b23302STom N Harris } 538d5b23302STom N Harris } 539d5b23302STom N Harris } 540d5b23302STom N Harris return $wids; 541d5b23302STom N Harris} 542d5b23302STom N Harris 543d5b23302STom N Harris/** 544488dd6ceSAndreas Gohr * Lookup words in index 545488dd6ceSAndreas Gohr * 546488dd6ceSAndreas Gohr * Takes an array of word and will return a list of matching 547488dd6ceSAndreas Gohr * documents for each one. 548488dd6ceSAndreas Gohr * 54963773904SAndreas Gohr * Important: No ACL checking is done here! All results are 55063773904SAndreas Gohr * returned, regardless of permissions 55163773904SAndreas Gohr * 552488dd6ceSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 553488dd6ceSAndreas Gohr */ 554488dd6ceSAndreas Gohrfunction idx_lookup($words){ 555488dd6ceSAndreas Gohr global $conf; 556488dd6ceSAndreas Gohr 557488dd6ceSAndreas Gohr $result = array(); 558488dd6ceSAndreas Gohr 559d5b23302STom N Harris $wids = idx_getIndexWordsSorted($words, $result); 560d5b23302STom N Harris if(empty($wids)) return array(); 561d5b23302STom N Harris 562488dd6ceSAndreas Gohr // load known words and documents 563579b0f7eSTNHarris $page_idx = idx_getIndex('page',''); 564488dd6ceSAndreas Gohr 565579b0f7eSTNHarris $docs = array(); // hold docs found 566579b0f7eSTNHarris foreach(array_keys($wids) as $wlen){ 567579b0f7eSTNHarris $wids[$wlen] = array_unique($wids[$wlen]); 568d5b23302STom N Harris $index = idx_getIndex('i',$wlen); 569d5b23302STom N Harris foreach($wids[$wlen] as $ixid){ 570d5b23302STom N Harris if($ixid < count($index)) 571d5b23302STom N Harris $docs["$wlen*$ixid"] = idx_parseIndexLine($page_idx,$index[$ixid]); 572488dd6ceSAndreas Gohr } 573488dd6ceSAndreas Gohr } 574488dd6ceSAndreas Gohr 575ad81d431SAndreas Gohr // merge found pages into final result array 576ad81d431SAndreas Gohr $final = array(); 577*c66972f2SAdrian Lang foreach($result as $word => $res){ 578ad81d431SAndreas Gohr $final[$word] = array(); 579*c66972f2SAdrian Lang foreach($res as $wid){ 580ad81d431SAndreas Gohr $hits = &$docs[$wid]; 581ad81d431SAndreas Gohr foreach ($hits as $hitkey => $hitcnt) { 582*c66972f2SAdrian Lang if (!isset($final[$word][$hitkey])) { 583*c66972f2SAdrian Lang $final[$word][$hitkey] = $hitcnt; 584*c66972f2SAdrian Lang } else { 585*c66972f2SAdrian Lang $final[$word][$hitkey] += $hitcnt; 586*c66972f2SAdrian Lang } 587ad81d431SAndreas Gohr } 588ad81d431SAndreas Gohr } 589ad81d431SAndreas Gohr } 590ad81d431SAndreas Gohr return $final; 591488dd6ceSAndreas Gohr} 592488dd6ceSAndreas Gohr 593488dd6ceSAndreas Gohr/** 594488dd6ceSAndreas Gohr * Returns a list of documents and counts from a index line 595488dd6ceSAndreas Gohr * 596488dd6ceSAndreas Gohr * It omits docs with a count of 0 and pages that no longer 597488dd6ceSAndreas Gohr * exist. 598488dd6ceSAndreas Gohr * 599488dd6ceSAndreas Gohr * @param array $page_idx The list of known pages 600488dd6ceSAndreas Gohr * @param string $line A line from the main index 601488dd6ceSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 602488dd6ceSAndreas Gohr */ 603488dd6ceSAndreas Gohrfunction idx_parseIndexLine(&$page_idx,$line){ 604488dd6ceSAndreas Gohr $result = array(); 605488dd6ceSAndreas Gohr 606488dd6ceSAndreas Gohr $line = trim($line); 607f5eb7cf0SAndreas Gohr if($line == '') return $result; 608488dd6ceSAndreas Gohr 609488dd6ceSAndreas Gohr $parts = explode(':',$line); 610488dd6ceSAndreas Gohr foreach($parts as $part){ 611488dd6ceSAndreas Gohr if($part == '') continue; 612488dd6ceSAndreas Gohr list($doc,$cnt) = explode('*',$part); 613488dd6ceSAndreas Gohr if(!$cnt) continue; 614488dd6ceSAndreas Gohr $doc = trim($page_idx[$doc]); 615488dd6ceSAndreas Gohr if(!$doc) continue; 616488dd6ceSAndreas Gohr // make sure the document still exists 617103c256aSChris Smith if(!page_exists($doc,'',false)) continue; 618488dd6ceSAndreas Gohr 619488dd6ceSAndreas Gohr $result[$doc] = $cnt; 620488dd6ceSAndreas Gohr } 621488dd6ceSAndreas Gohr return $result; 622488dd6ceSAndreas Gohr} 623488dd6ceSAndreas Gohr 624f5eb7cf0SAndreas Gohr/** 625f5eb7cf0SAndreas Gohr * Tokenizes a string into an array of search words 626f5eb7cf0SAndreas Gohr * 627f5eb7cf0SAndreas Gohr * Uses the same algorithm as idx_getPageWords() 628f5eb7cf0SAndreas Gohr * 629ad81d431SAndreas Gohr * @param string $string the query as given by the user 630ad81d431SAndreas Gohr * @param arrayref $stopwords array of stopwords 631ad81d431SAndreas Gohr * @param boolean $wc are wildcards allowed? 632f5eb7cf0SAndreas Gohr */ 633ad81d431SAndreas Gohrfunction idx_tokenizer($string,&$stopwords,$wc=false){ 634f5eb7cf0SAndreas Gohr $words = array(); 6354efb9a42SAndreas Gohr $wc = ($wc) ? '' : $wc = '\*'; 636f5eb7cf0SAndreas Gohr 637f5eb7cf0SAndreas Gohr if(preg_match('/[^0-9A-Za-z]/u', $string)){ 63891bb5faaSAndreas Gohr // handle asian chars as single words (may fail on older PHP version) 63960c15d7dSAndreas Gohr $asia = @preg_replace('/('.IDX_ASIAN.')/u',' \1 ',$string); 64091bb5faaSAndreas Gohr if(!is_null($asia)) $string = $asia; //recover from regexp failure 64193a60ad2SAndreas Gohr 6424efb9a42SAndreas Gohr $arr = explode(' ', utf8_stripspecials($string,' ','\._\-:'.$wc)); 643f5eb7cf0SAndreas Gohr foreach ($arr as $w) { 64433815ce2SChris Smith if (!is_numeric($w) && strlen($w) < IDX_MINWORDLENGTH) continue; 645f5eb7cf0SAndreas Gohr $w = utf8_strtolower($w); 6463cbaa9a4SAndreas Gohr if($stopwords && is_int(array_search("$w\n",$stopwords))) continue; 647f5eb7cf0SAndreas Gohr $words[] = $w; 648f5eb7cf0SAndreas Gohr } 649f5eb7cf0SAndreas Gohr }else{ 650f5eb7cf0SAndreas Gohr $w = $string; 65133815ce2SChris Smith if (!is_numeric($w) && strlen($w) < IDX_MINWORDLENGTH) return $words; 652f5eb7cf0SAndreas Gohr $w = strtolower($w); 653f5eb7cf0SAndreas Gohr if(is_int(array_search("$w\n",$stopwords))) return $words; 654f5eb7cf0SAndreas Gohr $words[] = $w; 655f5eb7cf0SAndreas Gohr } 656f5eb7cf0SAndreas Gohr 657f5eb7cf0SAndreas Gohr return $words; 658f5eb7cf0SAndreas Gohr} 659f5eb7cf0SAndreas Gohr 660a0c5c349STom N Harris/** 661a0c5c349STom N Harris * Create a pagewords index from the existing index. 662a0c5c349STom N Harris * 663a0c5c349STom N Harris * @author Tom N Harris <tnharris@whoopdedo.org> 664a0c5c349STom N Harris */ 665a0c5c349STom N Harrisfunction idx_upgradePageWords(){ 666a0c5c349STom N Harris global $conf; 667a0c5c349STom N Harris $page_idx = idx_getIndex('page',''); 668a0c5c349STom N Harris if (empty($page_idx)) return; 669a0c5c349STom N Harris $pagewords = array(); 670dd35e9c9SAndreas Gohr $len = count($page_idx); 671db959ae3SAndreas Gohr for ($n=0;$n<$len;$n++){ 672db959ae3SAndreas Gohr $pagewords[] = array(); 673db959ae3SAndreas Gohr } 674a0c5c349STom N Harris unset($page_idx); 675a0c5c349STom N Harris 676a0c5c349STom N Harris $n=0; 677a0c5c349STom N Harris foreach (idx_indexLengths($n) as $wlen) { 678a0c5c349STom N Harris $lines = idx_getIndex('i',$wlen); 679dd35e9c9SAndreas Gohr $len = count($lines); 680dd35e9c9SAndreas Gohr for ($wid=0;$wid<$len;$wid++) { 681a0c5c349STom N Harris $wkey = "$wlen*$wid"; 682a0c5c349STom N Harris foreach (explode(':',trim($lines[$wid])) as $part) { 683a0c5c349STom N Harris if($part == '') continue; 684a0c5c349STom N Harris list($doc,$cnt) = explode('*',$part); 685a0c5c349STom N Harris $pagewords[(int)$doc][] = $wkey; 686a0c5c349STom N Harris } 687a0c5c349STom N Harris } 688a0c5c349STom N Harris } 689a0c5c349STom N Harris 690b6344591STom N Harris $fn = $conf['indexdir'].'/pageword'; 691b6344591STom N Harris $fh = @fopen($fn.'.tmp','w'); 692b6344591STom N Harris if (!$fh){ 693a0c5c349STom N Harris trigger_error("Failed to write word index", E_USER_ERROR); 694a0c5c349STom N Harris return false; 695a0c5c349STom N Harris } 696b6344591STom N Harris foreach ($pagewords as $line){ 697b6344591STom N Harris fwrite($fh, join(':',$line)."\n"); 698b6344591STom N Harris } 699b6344591STom N Harris fclose($fh); 700b6344591STom N Harris if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']); 701b6344591STom N Harris io_rename($fn.'.tmp', $fn.'.idx'); 702a0c5c349STom N Harris return true; 703a0c5c349STom N Harris} 704a0c5c349STom N Harris 705b4ce25e9SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 706