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 9b4ce25e9SAndreas Gohr if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 10b4ce25e9SAndreas Gohr require_once(DOKU_CONF.'dokuwiki.php'); 11b4ce25e9SAndreas Gohr require_once(DOKU_INC.'inc/io.php'); 12b4ce25e9SAndreas Gohr require_once(DOKU_INC.'inc/utf8.php'); 13b4ce25e9SAndreas Gohr require_once(DOKU_INC.'inc/parserutils.php'); 14b4ce25e9SAndreas Gohr 1593a60ad2SAndreas Gohr// Asian characters are handled as words. The following regexp defines the 1693a60ad2SAndreas Gohr// Unicode-Ranges for Asian characters 1793a60ad2SAndreas Gohr// Ranges taken from http://en.wikipedia.org/wiki/Unicode_block 1893a60ad2SAndreas Gohr// I'm no language expert. If you think some ranges are wrongly chosen or 1993a60ad2SAndreas Gohr// a range is missing, please contact me 2091bb5faaSAndreas Gohrdefine('IDX_ASIAN','['. 2193a60ad2SAndreas Gohr '\x{0E00}-\x{0E7F}'. // Thai 2293a60ad2SAndreas Gohr '\x{2E80}-\x{D7AF}'. // CJK -> Hangul 2393a60ad2SAndreas Gohr '\x{F900}-\x{FAFF}'. // CJK Compatibility Ideographs 2493a60ad2SAndreas Gohr '\x{FE30}-\x{FE4F}'. // CJK Compatibility Forms 2593a60ad2SAndreas Gohr ']'); 2693a60ad2SAndreas Gohr 2793a60ad2SAndreas Gohr 28b4ce25e9SAndreas Gohr/** 29*579b0f7eSTNHarris * Write a list of strings to an index file. 30*579b0f7eSTNHarris * 31*579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 32*579b0f7eSTNHarris */ 33*579b0f7eSTNHarrisfunction idx_saveIndex($pre, $wlen, $idx){ 34*579b0f7eSTNHarris global $conf; 35*579b0f7eSTNHarris $fn = $conf['indexdir'].'/'.$pre.$wlen; 36*579b0f7eSTNHarris $fh = @fopen($fn.'.tmp','w'); 37*579b0f7eSTNHarris if(!$fh) return false; 38*579b0f7eSTNHarris fwrite($fh,join('',$idx)); 39*579b0f7eSTNHarris fclose($fh); 40*579b0f7eSTNHarris if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']); 41*579b0f7eSTNHarris io_rename($fn.'.tmp', $fn.'.idx'); 42*579b0f7eSTNHarris return true; 43*579b0f7eSTNHarris} 44*579b0f7eSTNHarris 45*579b0f7eSTNHarris/** 46*579b0f7eSTNHarris * Read the list of words in an index (if it exists). 47*579b0f7eSTNHarris * 48*579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 49*579b0f7eSTNHarris */ 50*579b0f7eSTNHarrisfunction idx_getIndex($pre, $wlen){ 51*579b0f7eSTNHarris global $conf; 52*579b0f7eSTNHarris $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx'; 53*579b0f7eSTNHarris if(!@file_exists($fn)) return array(); 54*579b0f7eSTNHarris return file($fn); 55*579b0f7eSTNHarris} 56*579b0f7eSTNHarris 57*579b0f7eSTNHarris/** 58*579b0f7eSTNHarris * Create an empty index file if it doesn't exist yet. 59*579b0f7eSTNHarris * 60*579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 61*579b0f7eSTNHarris */ 62*579b0f7eSTNHarrisfunction idx_touchIndex($pre, $wlen){ 63*579b0f7eSTNHarris global $conf; 64*579b0f7eSTNHarris $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx'; 65*579b0f7eSTNHarris if(!@file_exists($fn)){ 66*579b0f7eSTNHarris touch($fn); 67*579b0f7eSTNHarris if($conf['fperm']) chmod($fn, $conf['fperm']); 68*579b0f7eSTNHarris } 69*579b0f7eSTNHarris} 70*579b0f7eSTNHarris 71*579b0f7eSTNHarris/** 7244ca0adfSAndreas Gohr * Split a page into words 7344ca0adfSAndreas Gohr * 74*579b0f7eSTNHarris * Returns an array of word counts, false if an error occured. 75*579b0f7eSTNHarris * Array is keyed on the word length, then the word index. 7644ca0adfSAndreas Gohr * 7744ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7817f42b01SChris Smith * @author Christopher Smith <chris@jalakai.co.uk> 79b4ce25e9SAndreas Gohr */ 8044ca0adfSAndreas Gohrfunction idx_getPageWords($page){ 8144ca0adfSAndreas Gohr global $conf; 827367b368SAndreas Gohr $swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt'; 837367b368SAndreas Gohr if(@file_exists($swfile)){ 847367b368SAndreas Gohr $stopwords = file($swfile); 857367b368SAndreas Gohr }else{ 867367b368SAndreas Gohr $stopwords = array(); 877367b368SAndreas Gohr } 8844ca0adfSAndreas Gohr 8944ca0adfSAndreas Gohr $body = rawWiki($page); 9017f42b01SChris Smith $body = strtr($body, "\r\n\t", ' '); 9117f42b01SChris Smith $tokens = explode(' ', $body); 9217f42b01SChris Smith $tokens = array_count_values($tokens); // count the frequency of each token 9317f42b01SChris Smith 946b06b652Schris// ensure the deaccented or romanised page names of internal links are added to the token array 956b06b652Schris// (this is necessary for the backlink function -- there maybe a better way!) 966b06b652Schris if ($conf['deaccent']) { 976b06b652Schris $links = p_get_metadata($page,'relation references'); 986b06b652Schris 996b06b652Schris $tmp = join(' ',array_keys($links)); // make a single string 1006b06b652Schris $tmp = strtr($tmp, ':', ' '); // replace namespace separator with a space 1016b06b652Schris $link_tokens = array_unique(explode(' ', $tmp)); // break into tokens 1026b06b652Schris 1036b06b652Schris foreach ($link_tokens as $link_token) { 1046b06b652Schris if (isset($tokens[$link_token])) continue; 1056b06b652Schris $tokens[$link_token] = 1; 1066b06b652Schris } 1076b06b652Schris } 1086b06b652Schris 10917f42b01SChris Smith $words = array(); 11017f42b01SChris Smith foreach ($tokens as $word => $count) { 111*579b0f7eSTNHarris $arr = idx_tokenizer($word,$stopwords); 11217f42b01SChris Smith $arr = array_count_values($arr); 11317f42b01SChris Smith foreach ($arr as $w => $c) { 114*579b0f7eSTNHarris $l = strlen($w); 115*579b0f7eSTNHarris if(isset($words[$l])){ 116*579b0f7eSTNHarris $words[$l][$w] = $c * $count + (isset($words[$l][$w])) ? $words[$l][$w] : 0; 11717f42b01SChris Smith }else{ 118*579b0f7eSTNHarris $words[$l] = array($w => $c * $count); 119*579b0f7eSTNHarris } 12017f42b01SChris Smith } 12117f42b01SChris Smith } 12217f42b01SChris Smith 123*579b0f7eSTNHarris // arrive here with $words = array(wordlen => array(word => frequency)) 124b4ce25e9SAndreas Gohr 125b4ce25e9SAndreas Gohr $index = array(); //resulting index 126*579b0f7eSTNHarris foreach (array_keys($words) as $wlen){ 127*579b0f7eSTNHarris $word_idx = idx_getIndex('w',$wlen); 128*579b0f7eSTNHarris foreach ($words[$wlen] as $word => $freq) { 12944ca0adfSAndreas Gohr $wid = array_search("$word\n",$word_idx); 13044ca0adfSAndreas Gohr if(!is_int($wid)){ 13144ca0adfSAndreas Gohr $word_idx[] = "$word\n"; 13244ca0adfSAndreas Gohr $wid = count($word_idx)-1; 133b4ce25e9SAndreas Gohr } 134*579b0f7eSTNHarris if(!isset($index[$wlen])) 135*579b0f7eSTNHarris $index[$wlen] = array(); 136*579b0f7eSTNHarris $index[$wlen][$wid] = $freq; 13744ca0adfSAndreas Gohr } 13844ca0adfSAndreas Gohr 13944ca0adfSAndreas Gohr // save back word index 140*579b0f7eSTNHarris if(!idx_saveIndex('w',$wlen,$word_idx)){ 141*579b0f7eSTNHarris trigger_error("Failed to write word index", E_USER_ERROR); 14244ca0adfSAndreas Gohr return false; 14344ca0adfSAndreas Gohr } 144*579b0f7eSTNHarris } 145b4ce25e9SAndreas Gohr 146b4ce25e9SAndreas Gohr return $index; 147b4ce25e9SAndreas Gohr} 148b4ce25e9SAndreas Gohr 14944ca0adfSAndreas Gohr/** 15044ca0adfSAndreas Gohr * Adds/updates the search for the given page 15144ca0adfSAndreas Gohr * 15244ca0adfSAndreas Gohr * This is the core function of the indexer which does most 15344ca0adfSAndreas Gohr * of the work. This function needs to be called with proper 15444ca0adfSAndreas Gohr * locking! 15544ca0adfSAndreas Gohr * 15644ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 15744ca0adfSAndreas Gohr */ 15844ca0adfSAndreas Gohrfunction idx_addPage($page){ 15944ca0adfSAndreas Gohr global $conf; 160b4ce25e9SAndreas Gohr 161488dd6ceSAndreas Gohr // load known documents 162*579b0f7eSTNHarris $page_idx = idx_getIndex('page',''); 16344ca0adfSAndreas Gohr 16444ca0adfSAndreas Gohr // get page id (this is the linenumber in page.idx) 16544ca0adfSAndreas Gohr $pid = array_search("$page\n",$page_idx); 16644ca0adfSAndreas Gohr if(!is_int($pid)){ 16744ca0adfSAndreas Gohr $page_idx[] = "$page\n"; 16844ca0adfSAndreas Gohr $pid = count($page_idx)-1; 16944ca0adfSAndreas Gohr // page was new - write back 170*579b0f7eSTNHarris if (!idx_saveIndex('page','',$page_idx)) 171*579b0f7eSTNHarris return false; 17244ca0adfSAndreas Gohr } 17344ca0adfSAndreas Gohr 17444ca0adfSAndreas Gohr // get word usage in page 17544ca0adfSAndreas Gohr $words = idx_getPageWords($page); 17644ca0adfSAndreas Gohr if($words === false) return false; 17744ca0adfSAndreas Gohr if(!count($words)) return true; 17844ca0adfSAndreas Gohr 179*579b0f7eSTNHarris foreach(array_keys($words) as $wlen){ 18044ca0adfSAndreas Gohr // Open index and temp file 181*579b0f7eSTNHarris $fn = $conf['indexdir']."/i$wlen"; 182*579b0f7eSTNHarris idx_touchIndex('i',$wlen); 183*579b0f7eSTNHarris $idx = fopen($fn.'.idx','r'); 184*579b0f7eSTNHarris $tmp = fopen($fn.'.tmp','w'); 18544ca0adfSAndreas Gohr if(!$idx || !$tmp){ 18644ca0adfSAndreas Gohr trigger_error("Failed to open index files", E_USER_ERROR); 18744ca0adfSAndreas Gohr return false; 18844ca0adfSAndreas Gohr } 18944ca0adfSAndreas Gohr 190*579b0f7eSTNHarris // copy from index to temp file, modifying where needed 19144ca0adfSAndreas Gohr $lno = 0; 19244ca0adfSAndreas Gohr $line = ''; 19344ca0adfSAndreas Gohr while (!feof($idx)) { 19444ca0adfSAndreas Gohr // read full line 19544ca0adfSAndreas Gohr $line .= fgets($idx, 4096); 19644ca0adfSAndreas Gohr if(substr($line,-1) != "\n") continue; 19744ca0adfSAndreas Gohr 19844ca0adfSAndreas Gohr // write a new Line to temp file 199*579b0f7eSTNHarris idx_writeIndexLine($tmp,$line,$pid,$words[$wlen][$lno]); 20044ca0adfSAndreas Gohr 20144ca0adfSAndreas Gohr $line = ''; // reset line buffer 20244ca0adfSAndreas Gohr $lno++; // increase linecounter 20344ca0adfSAndreas Gohr } 20444ca0adfSAndreas Gohr fclose($idx); 20544ca0adfSAndreas Gohr 20644ca0adfSAndreas Gohr // add missing lines (usually index and word should contain 20744ca0adfSAndreas Gohr // the same number of lines, however if the page contained 20844ca0adfSAndreas Gohr // new words the word file has some more lines which need to 20944ca0adfSAndreas Gohr // be added here 210*579b0f7eSTNHarris $word_idx = idx_getIndex('w',$wlen); 21144ca0adfSAndreas Gohr $wcnt = count($word_idx); 21244ca0adfSAndreas Gohr for($lno; $lno<$wcnt; $lno++){ 213*579b0f7eSTNHarris idx_writeIndexLine($tmp,'',$pid,$words[$wlen][$lno]); 21444ca0adfSAndreas Gohr } 21544ca0adfSAndreas Gohr 21644ca0adfSAndreas Gohr // close the temp file and move it over to be the new one 21744ca0adfSAndreas Gohr fclose($tmp); 218*579b0f7eSTNHarris if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']); 2199684e36cSAndreas Gohr // try rename first (fast) fallback to copy (slow) 220*579b0f7eSTNHarris io_rename($fn.'.tmp', $fn.'.idx'); 221*579b0f7eSTNHarris } 222*579b0f7eSTNHarris 223*579b0f7eSTNHarris return true; 22444ca0adfSAndreas Gohr} 22544ca0adfSAndreas Gohr 22644ca0adfSAndreas Gohr/** 22744ca0adfSAndreas Gohr * Write a new index line to the filehandle 22844ca0adfSAndreas Gohr * 22944ca0adfSAndreas Gohr * This function writes an line for the index file to the 23044ca0adfSAndreas Gohr * given filehandle. It removes the given document from 23144ca0adfSAndreas Gohr * the given line and readds it when $count is >0. 23244ca0adfSAndreas Gohr * 23344ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 23444ca0adfSAndreas Gohr */ 23544ca0adfSAndreas Gohrfunction idx_writeIndexLine($fh,$line,$pid,$count){ 23644ca0adfSAndreas Gohr $line = trim($line); 23744ca0adfSAndreas Gohr 23844ca0adfSAndreas Gohr if($line != ''){ 23944ca0adfSAndreas Gohr $parts = explode(':',$line); 24044ca0adfSAndreas Gohr // remove doc from given line 24144ca0adfSAndreas Gohr foreach($parts as $part){ 24244ca0adfSAndreas Gohr if($part == '') continue; 24344ca0adfSAndreas Gohr list($doc,$cnt) = explode('*',$part); 24444ca0adfSAndreas Gohr if($doc != $pid){ 24544ca0adfSAndreas Gohr fwrite($fh,"$doc*$cnt:"); 24644ca0adfSAndreas Gohr } 24744ca0adfSAndreas Gohr } 24844ca0adfSAndreas Gohr } 24944ca0adfSAndreas Gohr 25044ca0adfSAndreas Gohr // add doc 25144ca0adfSAndreas Gohr if ($count){ 25244ca0adfSAndreas Gohr fwrite($fh,"$pid*$count"); 25344ca0adfSAndreas Gohr } 25444ca0adfSAndreas Gohr 25544ca0adfSAndreas Gohr // add newline 25644ca0adfSAndreas Gohr fwrite($fh,"\n"); 25744ca0adfSAndreas Gohr} 258b4ce25e9SAndreas Gohr 259488dd6ceSAndreas Gohr/** 260*579b0f7eSTNHarris * Get the word lengths that have been indexed. 261*579b0f7eSTNHarris * 262*579b0f7eSTNHarris * Reads the index directory and returns an array of lengths 263*579b0f7eSTNHarris * that there are indices for. 264*579b0f7eSTNHarris * 265*579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 266*579b0f7eSTNHarris */ 267*579b0f7eSTNHarrisfunction idx_indexLengths($minlen){ 268*579b0f7eSTNHarris global $conf; 269*579b0f7eSTNHarris $dir = @opendir($conf['indexdir']); 270*579b0f7eSTNHarris if($dir===false) 271*579b0f7eSTNHarris return array(); 272*579b0f7eSTNHarris $idx = array(); 273*579b0f7eSTNHarris // Exact match first. 274*579b0f7eSTNHarris if(@file_exists($conf['indexdir']."/i$minlen.idx")) 275*579b0f7eSTNHarris $idx[] = $minlen; 276*579b0f7eSTNHarris while (($f = readdir($dir)) !== false) { 277*579b0f7eSTNHarris if (substr($f,0,1) == 'i' && substr($f,-4) == '.idx'){ 278*579b0f7eSTNHarris $i = substr($f,1,-4); 279*579b0f7eSTNHarris if (is_numeric($i) && $i > $minlen) 280*579b0f7eSTNHarris $idx[] = $i; 281*579b0f7eSTNHarris } 282*579b0f7eSTNHarris } 283*579b0f7eSTNHarris closedir($dir); 284*579b0f7eSTNHarris return $idx; 285*579b0f7eSTNHarris} 286*579b0f7eSTNHarris 287*579b0f7eSTNHarris/** 288488dd6ceSAndreas Gohr * Lookup words in index 289488dd6ceSAndreas Gohr * 290488dd6ceSAndreas Gohr * Takes an array of word and will return a list of matching 291488dd6ceSAndreas Gohr * documents for each one. 292488dd6ceSAndreas Gohr * 29363773904SAndreas Gohr * Important: No ACL checking is done here! All results are 29463773904SAndreas Gohr * returned, regardless of permissions 29563773904SAndreas Gohr * 296488dd6ceSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 297488dd6ceSAndreas Gohr */ 298488dd6ceSAndreas Gohrfunction idx_lookup($words){ 299488dd6ceSAndreas Gohr global $conf; 300488dd6ceSAndreas Gohr 301488dd6ceSAndreas Gohr $result = array(); 302488dd6ceSAndreas Gohr 303488dd6ceSAndreas Gohr // load known words and documents 304*579b0f7eSTNHarris $page_idx = idx_getIndex('page',''); 305488dd6ceSAndreas Gohr 306488dd6ceSAndreas Gohr // get word IDs 307488dd6ceSAndreas Gohr $wids = array(); 308488dd6ceSAndreas Gohr foreach($words as $word){ 309ad81d431SAndreas Gohr $result[$word] = array(); 310ad81d431SAndreas Gohr $wild = 0; 311ad81d431SAndreas Gohr $xword = $word; 312*579b0f7eSTNHarris $wlen = strlen($word); 313ad81d431SAndreas Gohr 314ad81d431SAndreas Gohr // check for wildcards 315ad81d431SAndreas Gohr if(substr($xword,0,1) == '*'){ 316ad81d431SAndreas Gohr $xword = substr($xword,1); 317ad81d431SAndreas Gohr $wild = 1; 3189ee93076Schris $ptn = '/'.preg_quote($xword,'/').'$/'; 319*579b0f7eSTNHarris $wlen -= 1; 3209ee93076Schris# $l = -1*strlen($xword)-1; 321ad81d431SAndreas Gohr } 322ad81d431SAndreas Gohr if(substr($xword,-1,1) == '*'){ 323ad81d431SAndreas Gohr $xword = substr($xword,0,-1); 324ad81d431SAndreas Gohr $wild += 2; 325*579b0f7eSTNHarris $wlen -= 1; 326ad81d431SAndreas Gohr } 327*579b0f7eSTNHarris if ($wlen < 3 && $wild == 0 && !is_numeric($xword)) continue; 328ad81d431SAndreas Gohr 329ad81d431SAndreas Gohr // look for the ID(s) for the given word 330ad81d431SAndreas Gohr if($wild){ // handle wildcard search 331*579b0f7eSTNHarris foreach (idx_indexLengths($wlen) as $ixlen){ 332*579b0f7eSTNHarris $word_idx = idx_getIndex('w',$ixlen); 333ad81d431SAndreas Gohr $cnt = count($word_idx); 334ad81d431SAndreas Gohr for($wid=0; $wid<$cnt; $wid++){ 335ad81d431SAndreas Gohr $iword = $word_idx[$wid]; 336ad81d431SAndreas Gohr if( (($wild==3) && is_int(strpos($iword,$xword))) || 3379ee93076Schris# (($wild==1) && ("$xword\n" == substr($iword,$l))) || 3389ee93076Schris (($wild==1) && preg_match($ptn,$iword)) || 3399ee93076Schris# (($wild==2) && ($xword == substr($iword,0,strlen($xword)))) 3409ee93076Schris (($wild==2) && (0 === strpos($iword,$xword))) 3419ee93076Schris 342ad81d431SAndreas Gohr ){ 343*579b0f7eSTNHarris if(!isset($wids[$ixlen])) $wids[$ixlen] = array(); 344*579b0f7eSTNHarris $wids[$ixlen][] = $wid; 345*579b0f7eSTNHarris $result[$word][] = "$ixlen*$wid"; 346*579b0f7eSTNHarris } 347ad81d431SAndreas Gohr } 348ad81d431SAndreas Gohr } 349ad81d431SAndreas Gohr }else{ // handle exact search 350*579b0f7eSTNHarris $word_idx = idx_getIndex('w',$wlen); 351488dd6ceSAndreas Gohr $wid = array_search("$word\n",$word_idx); 352488dd6ceSAndreas Gohr if(is_int($wid)){ 353*579b0f7eSTNHarris $wids[$wlen] = array($wid); 354*579b0f7eSTNHarris $result[$word][] = "$wlen*$wid"; 355f5eb7cf0SAndreas Gohr }else{ 356f5eb7cf0SAndreas Gohr $result[$word] = array(); 357488dd6ceSAndreas Gohr } 358488dd6ceSAndreas Gohr } 359ad81d431SAndreas Gohr } 360*579b0f7eSTNHarris 361*579b0f7eSTNHarris $docs = array(); // hold docs found 362*579b0f7eSTNHarris foreach(array_keys($wids) as $wlen){ 363*579b0f7eSTNHarris sort($wids[$wlen]); 364*579b0f7eSTNHarris $wids[$wlen] = array_unique($wids[$wlen]); 365488dd6ceSAndreas Gohr 366488dd6ceSAndreas Gohr // Open index 367*579b0f7eSTNHarris idx_touchIndex('i',$wlen); 368*579b0f7eSTNHarris $idx = fopen($conf['indexdir']."/i$wlen.idx",'r'); 369488dd6ceSAndreas Gohr if(!$idx){ 370ad81d431SAndreas Gohr msg("Failed to open index file",-1); 371488dd6ceSAndreas Gohr return false; 372488dd6ceSAndreas Gohr } 373488dd6ceSAndreas Gohr 374488dd6ceSAndreas Gohr // Walk the index til the lines are found 375488dd6ceSAndreas Gohr $lno = 0; 376488dd6ceSAndreas Gohr $line = ''; 377*579b0f7eSTNHarris $ixids =& $wids[$wlen]; 378*579b0f7eSTNHarris $srch = array_shift($ixids); // which word do we look for? 379488dd6ceSAndreas Gohr while (!feof($idx)) { 380488dd6ceSAndreas Gohr // read full line 381488dd6ceSAndreas Gohr $line .= fgets($idx, 4096); 382488dd6ceSAndreas Gohr if(substr($line,-1) != "\n") continue; 383488dd6ceSAndreas Gohr if($lno > $srch) break; // shouldn't happen 384488dd6ceSAndreas Gohr 385488dd6ceSAndreas Gohr // do we want this line? 386488dd6ceSAndreas Gohr if($lno == $srch){ 387488dd6ceSAndreas Gohr // add docs to list 388*579b0f7eSTNHarris $docs["$wlen*$srch"] = idx_parseIndexLine($page_idx,$line); 389488dd6ceSAndreas Gohr 390*579b0f7eSTNHarris $srch = array_shift($ixids); // next word to look up 391488dd6ceSAndreas Gohr if($srch == null) break; // no more words 392488dd6ceSAndreas Gohr } 393488dd6ceSAndreas Gohr 394488dd6ceSAndreas Gohr $line = ''; // reset line buffer 395488dd6ceSAndreas Gohr $lno++; // increase linecounter 396488dd6ceSAndreas Gohr } 397488dd6ceSAndreas Gohr fclose($idx); 398*579b0f7eSTNHarris } 399488dd6ceSAndreas Gohr 400488dd6ceSAndreas Gohr 401ad81d431SAndreas Gohr // merge found pages into final result array 402ad81d431SAndreas Gohr $final = array(); 403ad81d431SAndreas Gohr foreach(array_keys($result) as $word){ 404ad81d431SAndreas Gohr $final[$word] = array(); 405ad81d431SAndreas Gohr foreach($result[$word] as $wid){ 406ad81d431SAndreas Gohr $hits = &$docs[$wid]; 407ad81d431SAndreas Gohr foreach ($hits as $hitkey => $hitcnt) { 408ad81d431SAndreas Gohr $final[$word][$hitkey] = $hitcnt + $final[$word][$hitkey]; 409ad81d431SAndreas Gohr } 410ad81d431SAndreas Gohr } 411ad81d431SAndreas Gohr } 412ad81d431SAndreas Gohr return $final; 413488dd6ceSAndreas Gohr} 414488dd6ceSAndreas Gohr 415488dd6ceSAndreas Gohr/** 416488dd6ceSAndreas Gohr * Returns a list of documents and counts from a index line 417488dd6ceSAndreas Gohr * 418488dd6ceSAndreas Gohr * It omits docs with a count of 0 and pages that no longer 419488dd6ceSAndreas Gohr * exist. 420488dd6ceSAndreas Gohr * 421488dd6ceSAndreas Gohr * @param array $page_idx The list of known pages 422488dd6ceSAndreas Gohr * @param string $line A line from the main index 423488dd6ceSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 424488dd6ceSAndreas Gohr */ 425488dd6ceSAndreas Gohrfunction idx_parseIndexLine(&$page_idx,$line){ 426488dd6ceSAndreas Gohr $result = array(); 427488dd6ceSAndreas Gohr 428488dd6ceSAndreas Gohr $line = trim($line); 429f5eb7cf0SAndreas Gohr if($line == '') return $result; 430488dd6ceSAndreas Gohr 431488dd6ceSAndreas Gohr $parts = explode(':',$line); 432488dd6ceSAndreas Gohr foreach($parts as $part){ 433488dd6ceSAndreas Gohr if($part == '') continue; 434488dd6ceSAndreas Gohr list($doc,$cnt) = explode('*',$part); 435488dd6ceSAndreas Gohr if(!$cnt) continue; 436488dd6ceSAndreas Gohr $doc = trim($page_idx[$doc]); 437488dd6ceSAndreas Gohr if(!$doc) continue; 438488dd6ceSAndreas Gohr // make sure the document still exists 4390d8ea614Schris if(!@file_exists(wikiFN($doc,'',false))) continue; 440488dd6ceSAndreas Gohr 441488dd6ceSAndreas Gohr $result[$doc] = $cnt; 442488dd6ceSAndreas Gohr } 443488dd6ceSAndreas Gohr return $result; 444488dd6ceSAndreas Gohr} 445488dd6ceSAndreas Gohr 446f5eb7cf0SAndreas Gohr/** 447f5eb7cf0SAndreas Gohr * Tokenizes a string into an array of search words 448f5eb7cf0SAndreas Gohr * 449f5eb7cf0SAndreas Gohr * Uses the same algorithm as idx_getPageWords() 450f5eb7cf0SAndreas Gohr * 451ad81d431SAndreas Gohr * @param string $string the query as given by the user 452ad81d431SAndreas Gohr * @param arrayref $stopwords array of stopwords 453ad81d431SAndreas Gohr * @param boolean $wc are wildcards allowed? 454ad81d431SAndreas Gohr * 455f5eb7cf0SAndreas Gohr * @todo make combined function to use alone or in getPageWords 456f5eb7cf0SAndreas Gohr */ 457ad81d431SAndreas Gohrfunction idx_tokenizer($string,&$stopwords,$wc=false){ 458f5eb7cf0SAndreas Gohr $words = array(); 4594efb9a42SAndreas Gohr $wc = ($wc) ? '' : $wc = '\*'; 460f5eb7cf0SAndreas Gohr 461f5eb7cf0SAndreas Gohr if(preg_match('/[^0-9A-Za-z]/u', $string)){ 46291bb5faaSAndreas Gohr // handle asian chars as single words (may fail on older PHP version) 46391bb5faaSAndreas Gohr $asia = @preg_replace('/('.IDX_ASIAN.')/u','\1 ',$string); 46491bb5faaSAndreas Gohr if(!is_null($asia)) $string = $asia; //recover from regexp failure 46593a60ad2SAndreas Gohr 4664efb9a42SAndreas Gohr $arr = explode(' ', utf8_stripspecials($string,' ','\._\-:'.$wc)); 467f5eb7cf0SAndreas Gohr foreach ($arr as $w) { 468f5eb7cf0SAndreas Gohr if (!is_numeric($w) && strlen($w) < 3) continue; 469f5eb7cf0SAndreas Gohr $w = utf8_strtolower($w); 4703cbaa9a4SAndreas Gohr if($stopwords && is_int(array_search("$w\n",$stopwords))) continue; 471f5eb7cf0SAndreas Gohr $words[] = $w; 472f5eb7cf0SAndreas Gohr } 473f5eb7cf0SAndreas Gohr }else{ 474f5eb7cf0SAndreas Gohr $w = $string; 475f5eb7cf0SAndreas Gohr if (!is_numeric($w) && strlen($w) < 3) return $words; 476f5eb7cf0SAndreas Gohr $w = strtolower($w); 477f5eb7cf0SAndreas Gohr if(is_int(array_search("$w\n",$stopwords))) return $words; 478f5eb7cf0SAndreas Gohr $words[] = $w; 479f5eb7cf0SAndreas Gohr } 480f5eb7cf0SAndreas Gohr 481f5eb7cf0SAndreas Gohr return $words; 482f5eb7cf0SAndreas Gohr} 483f5eb7cf0SAndreas Gohr 484b4ce25e9SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 485