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/** 29579b0f7eSTNHarris * Write a list of strings to an index file. 30579b0f7eSTNHarris * 31579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 32579b0f7eSTNHarris */ 33579b0f7eSTNHarrisfunction idx_saveIndex($pre, $wlen, $idx){ 34579b0f7eSTNHarris global $conf; 35579b0f7eSTNHarris $fn = $conf['indexdir'].'/'.$pre.$wlen; 36579b0f7eSTNHarris $fh = @fopen($fn.'.tmp','w'); 37579b0f7eSTNHarris if(!$fh) return false; 38579b0f7eSTNHarris fwrite($fh,join('',$idx)); 39579b0f7eSTNHarris fclose($fh); 40579b0f7eSTNHarris if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']); 41579b0f7eSTNHarris io_rename($fn.'.tmp', $fn.'.idx'); 42579b0f7eSTNHarris return true; 43579b0f7eSTNHarris} 44579b0f7eSTNHarris 45579b0f7eSTNHarris/** 46579b0f7eSTNHarris * Read the list of words in an index (if it exists). 47579b0f7eSTNHarris * 48579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 49579b0f7eSTNHarris */ 50579b0f7eSTNHarrisfunction idx_getIndex($pre, $wlen){ 51579b0f7eSTNHarris global $conf; 52579b0f7eSTNHarris $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx'; 53579b0f7eSTNHarris if(!@file_exists($fn)) return array(); 54579b0f7eSTNHarris return file($fn); 55579b0f7eSTNHarris} 56579b0f7eSTNHarris 57579b0f7eSTNHarris/** 58579b0f7eSTNHarris * Create an empty index file if it doesn't exist yet. 59579b0f7eSTNHarris * 60579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 61579b0f7eSTNHarris */ 62579b0f7eSTNHarrisfunction idx_touchIndex($pre, $wlen){ 63579b0f7eSTNHarris global $conf; 64579b0f7eSTNHarris $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx'; 65579b0f7eSTNHarris if(!@file_exists($fn)){ 66579b0f7eSTNHarris touch($fn); 67579b0f7eSTNHarris if($conf['fperm']) chmod($fn, $conf['fperm']); 68579b0f7eSTNHarris } 69579b0f7eSTNHarris} 70579b0f7eSTNHarris 71579b0f7eSTNHarris/** 7244ca0adfSAndreas Gohr * Split a page into words 7344ca0adfSAndreas Gohr * 74579b0f7eSTNHarris * Returns an array of word counts, false if an error occured. 75579b0f7eSTNHarris * 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 993fc667cfSchris if (!empty($links)) { 1006b06b652Schris $tmp = join(' ',array_keys($links)); // make a single string 1016b06b652Schris $tmp = strtr($tmp, ':', ' '); // replace namespace separator with a space 1026b06b652Schris $link_tokens = array_unique(explode(' ', $tmp)); // break into tokens 1036b06b652Schris 1046b06b652Schris foreach ($link_tokens as $link_token) { 1056b06b652Schris if (isset($tokens[$link_token])) continue; 1066b06b652Schris $tokens[$link_token] = 1; 1076b06b652Schris } 1086b06b652Schris } 1093fc667cfSchris } 1106b06b652Schris 11117f42b01SChris Smith $words = array(); 11217f42b01SChris Smith foreach ($tokens as $word => $count) { 113579b0f7eSTNHarris $arr = idx_tokenizer($word,$stopwords); 11417f42b01SChris Smith $arr = array_count_values($arr); 11517f42b01SChris Smith foreach ($arr as $w => $c) { 116579b0f7eSTNHarris $l = strlen($w); 117579b0f7eSTNHarris if(isset($words[$l])){ 118*b2bc63f0SAndreas Gohr $words[$l][$w] = $c * $count + (isset($words[$l][$w]) ? $words[$l][$w] : 0); 11917f42b01SChris Smith }else{ 120579b0f7eSTNHarris $words[$l] = array($w => $c * $count); 121579b0f7eSTNHarris } 12217f42b01SChris Smith } 12317f42b01SChris Smith } 12417f42b01SChris Smith 125579b0f7eSTNHarris // arrive here with $words = array(wordlen => array(word => frequency)) 126b4ce25e9SAndreas Gohr 127b4ce25e9SAndreas Gohr $index = array(); //resulting index 128579b0f7eSTNHarris foreach (array_keys($words) as $wlen){ 129579b0f7eSTNHarris $word_idx = idx_getIndex('w',$wlen); 130579b0f7eSTNHarris foreach ($words[$wlen] as $word => $freq) { 13144ca0adfSAndreas Gohr $wid = array_search("$word\n",$word_idx); 13244ca0adfSAndreas Gohr if(!is_int($wid)){ 13344ca0adfSAndreas Gohr $word_idx[] = "$word\n"; 13444ca0adfSAndreas Gohr $wid = count($word_idx)-1; 135b4ce25e9SAndreas Gohr } 136579b0f7eSTNHarris if(!isset($index[$wlen])) 137579b0f7eSTNHarris $index[$wlen] = array(); 138579b0f7eSTNHarris $index[$wlen][$wid] = $freq; 13944ca0adfSAndreas Gohr } 14044ca0adfSAndreas Gohr 14144ca0adfSAndreas Gohr // save back word index 142579b0f7eSTNHarris if(!idx_saveIndex('w',$wlen,$word_idx)){ 143579b0f7eSTNHarris trigger_error("Failed to write word index", E_USER_ERROR); 14444ca0adfSAndreas Gohr return false; 14544ca0adfSAndreas Gohr } 146579b0f7eSTNHarris } 147b4ce25e9SAndreas Gohr 148b4ce25e9SAndreas Gohr return $index; 149b4ce25e9SAndreas Gohr} 150b4ce25e9SAndreas Gohr 15144ca0adfSAndreas Gohr/** 15244ca0adfSAndreas Gohr * Adds/updates the search for the given page 15344ca0adfSAndreas Gohr * 15444ca0adfSAndreas Gohr * This is the core function of the indexer which does most 15544ca0adfSAndreas Gohr * of the work. This function needs to be called with proper 15644ca0adfSAndreas Gohr * locking! 15744ca0adfSAndreas Gohr * 15844ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 15944ca0adfSAndreas Gohr */ 16044ca0adfSAndreas Gohrfunction idx_addPage($page){ 16144ca0adfSAndreas Gohr global $conf; 162b4ce25e9SAndreas Gohr 163488dd6ceSAndreas Gohr // load known documents 164579b0f7eSTNHarris $page_idx = idx_getIndex('page',''); 16544ca0adfSAndreas Gohr 16644ca0adfSAndreas Gohr // get page id (this is the linenumber in page.idx) 16744ca0adfSAndreas Gohr $pid = array_search("$page\n",$page_idx); 16844ca0adfSAndreas Gohr if(!is_int($pid)){ 16944ca0adfSAndreas Gohr $page_idx[] = "$page\n"; 17044ca0adfSAndreas Gohr $pid = count($page_idx)-1; 17144ca0adfSAndreas Gohr // page was new - write back 172579b0f7eSTNHarris if (!idx_saveIndex('page','',$page_idx)) 173579b0f7eSTNHarris return false; 17444ca0adfSAndreas Gohr } 17544ca0adfSAndreas Gohr 17644ca0adfSAndreas Gohr // get word usage in page 17744ca0adfSAndreas Gohr $words = idx_getPageWords($page); 17844ca0adfSAndreas Gohr if($words === false) return false; 17944ca0adfSAndreas Gohr if(!count($words)) return true; 18044ca0adfSAndreas Gohr 181579b0f7eSTNHarris foreach(array_keys($words) as $wlen){ 18244ca0adfSAndreas Gohr // Open index and temp file 183579b0f7eSTNHarris $fn = $conf['indexdir']."/i$wlen"; 184579b0f7eSTNHarris idx_touchIndex('i',$wlen); 185579b0f7eSTNHarris $idx = fopen($fn.'.idx','r'); 186579b0f7eSTNHarris $tmp = fopen($fn.'.tmp','w'); 18744ca0adfSAndreas Gohr if(!$idx || !$tmp){ 18844ca0adfSAndreas Gohr trigger_error("Failed to open index files", E_USER_ERROR); 18944ca0adfSAndreas Gohr return false; 19044ca0adfSAndreas Gohr } 19144ca0adfSAndreas Gohr 192579b0f7eSTNHarris // copy from index to temp file, modifying where needed 19344ca0adfSAndreas Gohr $lno = 0; 19444ca0adfSAndreas Gohr $line = ''; 19544ca0adfSAndreas Gohr while (!feof($idx)) { 19644ca0adfSAndreas Gohr // read full line 19744ca0adfSAndreas Gohr $line .= fgets($idx, 4096); 19844ca0adfSAndreas Gohr if(substr($line,-1) != "\n") continue; 19944ca0adfSAndreas Gohr 20044ca0adfSAndreas Gohr // write a new Line to temp file 201579b0f7eSTNHarris idx_writeIndexLine($tmp,$line,$pid,$words[$wlen][$lno]); 20244ca0adfSAndreas Gohr 20344ca0adfSAndreas Gohr $line = ''; // reset line buffer 20444ca0adfSAndreas Gohr $lno++; // increase linecounter 20544ca0adfSAndreas Gohr } 20644ca0adfSAndreas Gohr fclose($idx); 20744ca0adfSAndreas Gohr 20844ca0adfSAndreas Gohr // add missing lines (usually index and word should contain 20944ca0adfSAndreas Gohr // the same number of lines, however if the page contained 21044ca0adfSAndreas Gohr // new words the word file has some more lines which need to 21144ca0adfSAndreas Gohr // be added here 212579b0f7eSTNHarris $word_idx = idx_getIndex('w',$wlen); 21344ca0adfSAndreas Gohr $wcnt = count($word_idx); 21444ca0adfSAndreas Gohr for($lno; $lno<$wcnt; $lno++){ 215579b0f7eSTNHarris idx_writeIndexLine($tmp,'',$pid,$words[$wlen][$lno]); 21644ca0adfSAndreas Gohr } 21744ca0adfSAndreas Gohr 21844ca0adfSAndreas Gohr // close the temp file and move it over to be the new one 21944ca0adfSAndreas Gohr fclose($tmp); 220579b0f7eSTNHarris if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']); 2219684e36cSAndreas Gohr // try rename first (fast) fallback to copy (slow) 222579b0f7eSTNHarris io_rename($fn.'.tmp', $fn.'.idx'); 223579b0f7eSTNHarris } 224579b0f7eSTNHarris 225579b0f7eSTNHarris return true; 22644ca0adfSAndreas Gohr} 22744ca0adfSAndreas Gohr 22844ca0adfSAndreas Gohr/** 22944ca0adfSAndreas Gohr * Write a new index line to the filehandle 23044ca0adfSAndreas Gohr * 23144ca0adfSAndreas Gohr * This function writes an line for the index file to the 23244ca0adfSAndreas Gohr * given filehandle. It removes the given document from 23344ca0adfSAndreas Gohr * the given line and readds it when $count is >0. 23444ca0adfSAndreas Gohr * 23544ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 23644ca0adfSAndreas Gohr */ 23744ca0adfSAndreas Gohrfunction idx_writeIndexLine($fh,$line,$pid,$count){ 23844ca0adfSAndreas Gohr $line = trim($line); 23944ca0adfSAndreas Gohr 24044ca0adfSAndreas Gohr if($line != ''){ 24144ca0adfSAndreas Gohr $parts = explode(':',$line); 24244ca0adfSAndreas Gohr // remove doc from given line 24344ca0adfSAndreas Gohr foreach($parts as $part){ 24444ca0adfSAndreas Gohr if($part == '') continue; 24544ca0adfSAndreas Gohr list($doc,$cnt) = explode('*',$part); 24644ca0adfSAndreas Gohr if($doc != $pid){ 24744ca0adfSAndreas Gohr fwrite($fh,"$doc*$cnt:"); 24844ca0adfSAndreas Gohr } 24944ca0adfSAndreas Gohr } 25044ca0adfSAndreas Gohr } 25144ca0adfSAndreas Gohr 25244ca0adfSAndreas Gohr // add doc 25344ca0adfSAndreas Gohr if ($count){ 25444ca0adfSAndreas Gohr fwrite($fh,"$pid*$count"); 25544ca0adfSAndreas Gohr } 25644ca0adfSAndreas Gohr 25744ca0adfSAndreas Gohr // add newline 25844ca0adfSAndreas Gohr fwrite($fh,"\n"); 25944ca0adfSAndreas Gohr} 260b4ce25e9SAndreas Gohr 261488dd6ceSAndreas Gohr/** 262579b0f7eSTNHarris * Get the word lengths that have been indexed. 263579b0f7eSTNHarris * 264579b0f7eSTNHarris * Reads the index directory and returns an array of lengths 265579b0f7eSTNHarris * that there are indices for. 266579b0f7eSTNHarris * 267579b0f7eSTNHarris * @author Tom N Harris <tnharris@whoopdedo.org> 268579b0f7eSTNHarris */ 269579b0f7eSTNHarrisfunction idx_indexLengths($minlen){ 270579b0f7eSTNHarris global $conf; 271579b0f7eSTNHarris $dir = @opendir($conf['indexdir']); 272579b0f7eSTNHarris if($dir===false) 273579b0f7eSTNHarris return array(); 274579b0f7eSTNHarris $idx = array(); 275579b0f7eSTNHarris // Exact match first. 276579b0f7eSTNHarris if(@file_exists($conf['indexdir']."/i$minlen.idx")) 277579b0f7eSTNHarris $idx[] = $minlen; 278579b0f7eSTNHarris while (($f = readdir($dir)) !== false) { 279579b0f7eSTNHarris if (substr($f,0,1) == 'i' && substr($f,-4) == '.idx'){ 280579b0f7eSTNHarris $i = substr($f,1,-4); 281579b0f7eSTNHarris if (is_numeric($i) && $i > $minlen) 282579b0f7eSTNHarris $idx[] = $i; 283579b0f7eSTNHarris } 284579b0f7eSTNHarris } 285579b0f7eSTNHarris closedir($dir); 286579b0f7eSTNHarris return $idx; 287579b0f7eSTNHarris} 288579b0f7eSTNHarris 289579b0f7eSTNHarris/** 290488dd6ceSAndreas Gohr * Lookup words in index 291488dd6ceSAndreas Gohr * 292488dd6ceSAndreas Gohr * Takes an array of word and will return a list of matching 293488dd6ceSAndreas Gohr * documents for each one. 294488dd6ceSAndreas Gohr * 29563773904SAndreas Gohr * Important: No ACL checking is done here! All results are 29663773904SAndreas Gohr * returned, regardless of permissions 29763773904SAndreas Gohr * 298488dd6ceSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 299488dd6ceSAndreas Gohr */ 300488dd6ceSAndreas Gohrfunction idx_lookup($words){ 301488dd6ceSAndreas Gohr global $conf; 302488dd6ceSAndreas Gohr 303488dd6ceSAndreas Gohr $result = array(); 304488dd6ceSAndreas Gohr 305488dd6ceSAndreas Gohr // load known words and documents 306579b0f7eSTNHarris $page_idx = idx_getIndex('page',''); 307488dd6ceSAndreas Gohr 308488dd6ceSAndreas Gohr // get word IDs 309488dd6ceSAndreas Gohr $wids = array(); 310488dd6ceSAndreas Gohr foreach($words as $word){ 311ad81d431SAndreas Gohr $result[$word] = array(); 312ad81d431SAndreas Gohr $wild = 0; 313ad81d431SAndreas Gohr $xword = $word; 314579b0f7eSTNHarris $wlen = strlen($word); 315ad81d431SAndreas Gohr 316ad81d431SAndreas Gohr // check for wildcards 317ad81d431SAndreas Gohr if(substr($xword,0,1) == '*'){ 318ad81d431SAndreas Gohr $xword = substr($xword,1); 319ad81d431SAndreas Gohr $wild = 1; 3209ee93076Schris $ptn = '/'.preg_quote($xword,'/').'$/'; 321579b0f7eSTNHarris $wlen -= 1; 3229ee93076Schris# $l = -1*strlen($xword)-1; 323ad81d431SAndreas Gohr } 324ad81d431SAndreas Gohr if(substr($xword,-1,1) == '*'){ 325ad81d431SAndreas Gohr $xword = substr($xword,0,-1); 326ad81d431SAndreas Gohr $wild += 2; 327579b0f7eSTNHarris $wlen -= 1; 328ad81d431SAndreas Gohr } 329579b0f7eSTNHarris if ($wlen < 3 && $wild == 0 && !is_numeric($xword)) continue; 330ad81d431SAndreas Gohr 331ad81d431SAndreas Gohr // look for the ID(s) for the given word 332ad81d431SAndreas Gohr if($wild){ // handle wildcard search 333579b0f7eSTNHarris foreach (idx_indexLengths($wlen) as $ixlen){ 334579b0f7eSTNHarris $word_idx = idx_getIndex('w',$ixlen); 335ad81d431SAndreas Gohr $cnt = count($word_idx); 336ad81d431SAndreas Gohr for($wid=0; $wid<$cnt; $wid++){ 337ad81d431SAndreas Gohr $iword = $word_idx[$wid]; 338ad81d431SAndreas Gohr if( (($wild==3) && is_int(strpos($iword,$xword))) || 3399ee93076Schris# (($wild==1) && ("$xword\n" == substr($iword,$l))) || 3409ee93076Schris (($wild==1) && preg_match($ptn,$iword)) || 3419ee93076Schris# (($wild==2) && ($xword == substr($iword,0,strlen($xword)))) 3429ee93076Schris (($wild==2) && (0 === strpos($iword,$xword))) 3439ee93076Schris 344ad81d431SAndreas Gohr ){ 345579b0f7eSTNHarris if(!isset($wids[$ixlen])) $wids[$ixlen] = array(); 346579b0f7eSTNHarris $wids[$ixlen][] = $wid; 347579b0f7eSTNHarris $result[$word][] = "$ixlen*$wid"; 348579b0f7eSTNHarris } 349ad81d431SAndreas Gohr } 350ad81d431SAndreas Gohr } 351ad81d431SAndreas Gohr }else{ // handle exact search 352579b0f7eSTNHarris $word_idx = idx_getIndex('w',$wlen); 353488dd6ceSAndreas Gohr $wid = array_search("$word\n",$word_idx); 354488dd6ceSAndreas Gohr if(is_int($wid)){ 355579b0f7eSTNHarris $wids[$wlen] = array($wid); 356579b0f7eSTNHarris $result[$word][] = "$wlen*$wid"; 357f5eb7cf0SAndreas Gohr }else{ 358f5eb7cf0SAndreas Gohr $result[$word] = array(); 359488dd6ceSAndreas Gohr } 360488dd6ceSAndreas Gohr } 361ad81d431SAndreas Gohr } 362579b0f7eSTNHarris 363579b0f7eSTNHarris $docs = array(); // hold docs found 364579b0f7eSTNHarris foreach(array_keys($wids) as $wlen){ 365579b0f7eSTNHarris sort($wids[$wlen]); 366579b0f7eSTNHarris $wids[$wlen] = array_unique($wids[$wlen]); 367488dd6ceSAndreas Gohr 368488dd6ceSAndreas Gohr // Open index 369579b0f7eSTNHarris idx_touchIndex('i',$wlen); 370579b0f7eSTNHarris $idx = fopen($conf['indexdir']."/i$wlen.idx",'r'); 371488dd6ceSAndreas Gohr if(!$idx){ 372ad81d431SAndreas Gohr msg("Failed to open index file",-1); 373488dd6ceSAndreas Gohr return false; 374488dd6ceSAndreas Gohr } 375488dd6ceSAndreas Gohr 376488dd6ceSAndreas Gohr // Walk the index til the lines are found 377488dd6ceSAndreas Gohr $lno = 0; 378488dd6ceSAndreas Gohr $line = ''; 379579b0f7eSTNHarris $ixids =& $wids[$wlen]; 380579b0f7eSTNHarris $srch = array_shift($ixids); // which word do we look for? 381488dd6ceSAndreas Gohr while (!feof($idx)) { 382488dd6ceSAndreas Gohr // read full line 383488dd6ceSAndreas Gohr $line .= fgets($idx, 4096); 384488dd6ceSAndreas Gohr if(substr($line,-1) != "\n") continue; 385488dd6ceSAndreas Gohr if($lno > $srch) break; // shouldn't happen 386488dd6ceSAndreas Gohr 387488dd6ceSAndreas Gohr // do we want this line? 388488dd6ceSAndreas Gohr if($lno == $srch){ 389488dd6ceSAndreas Gohr // add docs to list 390579b0f7eSTNHarris $docs["$wlen*$srch"] = idx_parseIndexLine($page_idx,$line); 391488dd6ceSAndreas Gohr 392579b0f7eSTNHarris $srch = array_shift($ixids); // next word to look up 393488dd6ceSAndreas Gohr if($srch == null) break; // no more words 394488dd6ceSAndreas Gohr } 395488dd6ceSAndreas Gohr 396488dd6ceSAndreas Gohr $line = ''; // reset line buffer 397488dd6ceSAndreas Gohr $lno++; // increase linecounter 398488dd6ceSAndreas Gohr } 399488dd6ceSAndreas Gohr fclose($idx); 400579b0f7eSTNHarris } 401488dd6ceSAndreas Gohr 402488dd6ceSAndreas Gohr 403ad81d431SAndreas Gohr // merge found pages into final result array 404ad81d431SAndreas Gohr $final = array(); 405ad81d431SAndreas Gohr foreach(array_keys($result) as $word){ 406ad81d431SAndreas Gohr $final[$word] = array(); 407ad81d431SAndreas Gohr foreach($result[$word] as $wid){ 408ad81d431SAndreas Gohr $hits = &$docs[$wid]; 409ad81d431SAndreas Gohr foreach ($hits as $hitkey => $hitcnt) { 410ad81d431SAndreas Gohr $final[$word][$hitkey] = $hitcnt + $final[$word][$hitkey]; 411ad81d431SAndreas Gohr } 412ad81d431SAndreas Gohr } 413ad81d431SAndreas Gohr } 414ad81d431SAndreas Gohr return $final; 415488dd6ceSAndreas Gohr} 416488dd6ceSAndreas Gohr 417488dd6ceSAndreas Gohr/** 418488dd6ceSAndreas Gohr * Returns a list of documents and counts from a index line 419488dd6ceSAndreas Gohr * 420488dd6ceSAndreas Gohr * It omits docs with a count of 0 and pages that no longer 421488dd6ceSAndreas Gohr * exist. 422488dd6ceSAndreas Gohr * 423488dd6ceSAndreas Gohr * @param array $page_idx The list of known pages 424488dd6ceSAndreas Gohr * @param string $line A line from the main index 425488dd6ceSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 426488dd6ceSAndreas Gohr */ 427488dd6ceSAndreas Gohrfunction idx_parseIndexLine(&$page_idx,$line){ 428488dd6ceSAndreas Gohr $result = array(); 429488dd6ceSAndreas Gohr 430488dd6ceSAndreas Gohr $line = trim($line); 431f5eb7cf0SAndreas Gohr if($line == '') return $result; 432488dd6ceSAndreas Gohr 433488dd6ceSAndreas Gohr $parts = explode(':',$line); 434488dd6ceSAndreas Gohr foreach($parts as $part){ 435488dd6ceSAndreas Gohr if($part == '') continue; 436488dd6ceSAndreas Gohr list($doc,$cnt) = explode('*',$part); 437488dd6ceSAndreas Gohr if(!$cnt) continue; 438488dd6ceSAndreas Gohr $doc = trim($page_idx[$doc]); 439488dd6ceSAndreas Gohr if(!$doc) continue; 440488dd6ceSAndreas Gohr // make sure the document still exists 4410d8ea614Schris if(!@file_exists(wikiFN($doc,'',false))) continue; 442488dd6ceSAndreas Gohr 443488dd6ceSAndreas Gohr $result[$doc] = $cnt; 444488dd6ceSAndreas Gohr } 445488dd6ceSAndreas Gohr return $result; 446488dd6ceSAndreas Gohr} 447488dd6ceSAndreas Gohr 448f5eb7cf0SAndreas Gohr/** 449f5eb7cf0SAndreas Gohr * Tokenizes a string into an array of search words 450f5eb7cf0SAndreas Gohr * 451f5eb7cf0SAndreas Gohr * Uses the same algorithm as idx_getPageWords() 452f5eb7cf0SAndreas Gohr * 453ad81d431SAndreas Gohr * @param string $string the query as given by the user 454ad81d431SAndreas Gohr * @param arrayref $stopwords array of stopwords 455ad81d431SAndreas Gohr * @param boolean $wc are wildcards allowed? 456ad81d431SAndreas Gohr * 457f5eb7cf0SAndreas Gohr * @todo make combined function to use alone or in getPageWords 458f5eb7cf0SAndreas Gohr */ 459ad81d431SAndreas Gohrfunction idx_tokenizer($string,&$stopwords,$wc=false){ 460f5eb7cf0SAndreas Gohr $words = array(); 4614efb9a42SAndreas Gohr $wc = ($wc) ? '' : $wc = '\*'; 462f5eb7cf0SAndreas Gohr 463f5eb7cf0SAndreas Gohr if(preg_match('/[^0-9A-Za-z]/u', $string)){ 46491bb5faaSAndreas Gohr // handle asian chars as single words (may fail on older PHP version) 46591bb5faaSAndreas Gohr $asia = @preg_replace('/('.IDX_ASIAN.')/u','\1 ',$string); 46691bb5faaSAndreas Gohr if(!is_null($asia)) $string = $asia; //recover from regexp failure 46793a60ad2SAndreas Gohr 4684efb9a42SAndreas Gohr $arr = explode(' ', utf8_stripspecials($string,' ','\._\-:'.$wc)); 469f5eb7cf0SAndreas Gohr foreach ($arr as $w) { 470f5eb7cf0SAndreas Gohr if (!is_numeric($w) && strlen($w) < 3) continue; 471f5eb7cf0SAndreas Gohr $w = utf8_strtolower($w); 4723cbaa9a4SAndreas Gohr if($stopwords && is_int(array_search("$w\n",$stopwords))) continue; 473f5eb7cf0SAndreas Gohr $words[] = $w; 474f5eb7cf0SAndreas Gohr } 475f5eb7cf0SAndreas Gohr }else{ 476f5eb7cf0SAndreas Gohr $w = $string; 477f5eb7cf0SAndreas Gohr if (!is_numeric($w) && strlen($w) < 3) return $words; 478f5eb7cf0SAndreas Gohr $w = strtolower($w); 479f5eb7cf0SAndreas Gohr if(is_int(array_search("$w\n",$stopwords))) return $words; 480f5eb7cf0SAndreas Gohr $words[] = $w; 481f5eb7cf0SAndreas Gohr } 482f5eb7cf0SAndreas Gohr 483f5eb7cf0SAndreas Gohr return $words; 484f5eb7cf0SAndreas Gohr} 485f5eb7cf0SAndreas Gohr 486b4ce25e9SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 487