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 15*93a60ad2SAndreas Gohr// Asian characters are handled as words. The following regexp defines the 16*93a60ad2SAndreas Gohr// Unicode-Ranges for Asian characters 17*93a60ad2SAndreas Gohr// Ranges taken from http://en.wikipedia.org/wiki/Unicode_block 18*93a60ad2SAndreas Gohr// I'm no language expert. If you think some ranges are wrongly chosen or 19*93a60ad2SAndreas Gohr// a range is missing, please contact me 20*93a60ad2SAndreas Gohrdefine(IDX_ASIAN,'['. 21*93a60ad2SAndreas Gohr '\x{0E00}-\x{0E7F}'. // Thai 22*93a60ad2SAndreas Gohr '\x{2E80}-\x{D7AF}'. // CJK -> Hangul 23*93a60ad2SAndreas Gohr '\x{F900}-\x{FAFF}'. // CJK Compatibility Ideographs 24*93a60ad2SAndreas Gohr '\x{FE30}-\x{FE4F}'. // CJK Compatibility Forms 25*93a60ad2SAndreas Gohr ']'); 26*93a60ad2SAndreas Gohr 27*93a60ad2SAndreas Gohr 28b4ce25e9SAndreas Gohr/** 2944ca0adfSAndreas Gohr * Split a page into words 3044ca0adfSAndreas Gohr * 3144ca0adfSAndreas Gohr * Returns an array of of word counts, false if an error occured 3244ca0adfSAndreas Gohr * 3344ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 3417f42b01SChris Smith * @author Christopher Smith <chris@jalakai.co.uk> 35b4ce25e9SAndreas Gohr */ 3644ca0adfSAndreas Gohrfunction idx_getPageWords($page){ 3744ca0adfSAndreas Gohr global $conf; 3844ca0adfSAndreas Gohr $word_idx = file($conf['cachedir'].'/word.idx'); 397367b368SAndreas Gohr $swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt'; 407367b368SAndreas Gohr if(@file_exists($swfile)){ 417367b368SAndreas Gohr $stopwords = file($swfile); 427367b368SAndreas Gohr }else{ 437367b368SAndreas Gohr $stopwords = array(); 447367b368SAndreas Gohr } 4544ca0adfSAndreas Gohr 4644ca0adfSAndreas Gohr $body = rawWiki($page); 4717f42b01SChris Smith $body = strtr($body, "\r\n\t", ' '); 4817f42b01SChris Smith $tokens = explode(' ', $body); 4917f42b01SChris Smith $tokens = array_count_values($tokens); // count the frequency of each token 5017f42b01SChris Smith 5117f42b01SChris Smith $words = array(); 5217f42b01SChris Smith foreach ($tokens as $word => $count) { 5317f42b01SChris Smith // simple filter to restrict use of utf8_stripspecials 54bc54ab52Schris if (preg_match('/[^0-9A-Za-z]/u', $word)) { 55*93a60ad2SAndreas Gohr // handle asian chars as single words 56*93a60ad2SAndreas Gohr $word = preg_replace('/('.IDX_ASIAN.')/u','\1 ',$word); 5717f42b01SChris Smith $arr = explode(' ', utf8_stripspecials($word,' ','._\-:')); 5817f42b01SChris Smith $arr = array_count_values($arr); 5917f42b01SChris Smith 6017f42b01SChris Smith foreach ($arr as $w => $c) { 6117f42b01SChris Smith if (!is_numeric($w) && strlen($w) < 3) continue; 62bc54ab52Schris $w = utf8_strtolower($w); 6317f42b01SChris Smith $words[$w] = $c + (isset($words[$w]) ? $words[$w] : 0); 6417f42b01SChris Smith } 6517f42b01SChris Smith } else { 66d18f28deSAndreas Gohr if (!is_numeric($word) && strlen($word) < 3) continue; 67bc54ab52Schris $word = strtolower($word); 6817f42b01SChris Smith $words[$word] = $count + (isset($words[$word]) ? $words[$word] : 0); 6917f42b01SChris Smith } 7017f42b01SChris Smith } 7117f42b01SChris Smith 7217f42b01SChris Smith // arrive here with $words = array(word => frequency) 73b4ce25e9SAndreas Gohr 74b4ce25e9SAndreas Gohr $index = array(); //resulting index 7517f42b01SChris Smith foreach ($words as $word => $freq) { 7617f42b01SChris Smith if (is_int(array_search("$word\n",$stopwords))) continue; 7744ca0adfSAndreas Gohr $wid = array_search("$word\n",$word_idx); 7844ca0adfSAndreas Gohr if(!is_int($wid)){ 7944ca0adfSAndreas Gohr $word_idx[] = "$word\n"; 8044ca0adfSAndreas Gohr $wid = count($word_idx)-1; 81b4ce25e9SAndreas Gohr } 8217f42b01SChris Smith $index[$wid] = $freq; 8344ca0adfSAndreas Gohr } 8444ca0adfSAndreas Gohr 8544ca0adfSAndreas Gohr // save back word index 8644ca0adfSAndreas Gohr $fh = fopen($conf['cachedir'].'/word.idx','w'); 8744ca0adfSAndreas Gohr if(!$fh){ 8844ca0adfSAndreas Gohr trigger_error("Failed to write word.idx", E_USER_ERROR); 8944ca0adfSAndreas Gohr return false; 9044ca0adfSAndreas Gohr } 9144ca0adfSAndreas Gohr fwrite($fh,join('',$word_idx)); 9244ca0adfSAndreas Gohr fclose($fh); 93b4ce25e9SAndreas Gohr 94b4ce25e9SAndreas Gohr return $index; 95b4ce25e9SAndreas Gohr} 96b4ce25e9SAndreas Gohr 9744ca0adfSAndreas Gohr/** 9844ca0adfSAndreas Gohr * Adds/updates the search for the given page 9944ca0adfSAndreas Gohr * 10044ca0adfSAndreas Gohr * This is the core function of the indexer which does most 10144ca0adfSAndreas Gohr * of the work. This function needs to be called with proper 10244ca0adfSAndreas Gohr * locking! 10344ca0adfSAndreas Gohr * 10444ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10544ca0adfSAndreas Gohr */ 10644ca0adfSAndreas Gohrfunction idx_addPage($page){ 10744ca0adfSAndreas Gohr global $conf; 108b4ce25e9SAndreas Gohr 109488dd6ceSAndreas Gohr // load known documents 11044ca0adfSAndreas Gohr $page_idx = file($conf['cachedir'].'/page.idx'); 11144ca0adfSAndreas Gohr 11244ca0adfSAndreas Gohr // get page id (this is the linenumber in page.idx) 11344ca0adfSAndreas Gohr $pid = array_search("$page\n",$page_idx); 11444ca0adfSAndreas Gohr if(!is_int($pid)){ 11544ca0adfSAndreas Gohr $page_idx[] = "$page\n"; 11644ca0adfSAndreas Gohr $pid = count($page_idx)-1; 11744ca0adfSAndreas Gohr // page was new - write back 11844ca0adfSAndreas Gohr $fh = fopen($conf['cachedir'].'/page.idx','w'); 11944ca0adfSAndreas Gohr if(!$fh) return false; 12044ca0adfSAndreas Gohr fwrite($fh,join('',$page_idx)); 12144ca0adfSAndreas Gohr fclose($fh); 12244ca0adfSAndreas Gohr } 12344ca0adfSAndreas Gohr 12444ca0adfSAndreas Gohr // get word usage in page 12544ca0adfSAndreas Gohr $words = idx_getPageWords($page); 12644ca0adfSAndreas Gohr if($words === false) return false; 12744ca0adfSAndreas Gohr if(!count($words)) return true; 12844ca0adfSAndreas Gohr 12944ca0adfSAndreas Gohr // Open index and temp file 13044ca0adfSAndreas Gohr $idx = fopen($conf['cachedir'].'/index.idx','r'); 13144ca0adfSAndreas Gohr $tmp = fopen($conf['cachedir'].'/index.tmp','w'); 13244ca0adfSAndreas Gohr if(!$idx || !$tmp){ 13344ca0adfSAndreas Gohr trigger_error("Failed to open index files", E_USER_ERROR); 13444ca0adfSAndreas Gohr return false; 13544ca0adfSAndreas Gohr } 13644ca0adfSAndreas Gohr 13744ca0adfSAndreas Gohr // copy from index to temp file, modifying were needed 13844ca0adfSAndreas Gohr $lno = 0; 13944ca0adfSAndreas Gohr $line = ''; 14044ca0adfSAndreas Gohr while (!feof($idx)) { 14144ca0adfSAndreas Gohr // read full line 14244ca0adfSAndreas Gohr $line .= fgets($idx, 4096); 14344ca0adfSAndreas Gohr if(substr($line,-1) != "\n") continue; 14444ca0adfSAndreas Gohr 14544ca0adfSAndreas Gohr // write a new Line to temp file 14644ca0adfSAndreas Gohr idx_writeIndexLine($tmp,$line,$pid,$words[$lno]); 14744ca0adfSAndreas Gohr 14844ca0adfSAndreas Gohr $line = ''; // reset line buffer 14944ca0adfSAndreas Gohr $lno++; // increase linecounter 15044ca0adfSAndreas Gohr } 15144ca0adfSAndreas Gohr fclose($idx); 15244ca0adfSAndreas Gohr 15344ca0adfSAndreas Gohr // add missing lines (usually index and word should contain 15444ca0adfSAndreas Gohr // the same number of lines, however if the page contained 15544ca0adfSAndreas Gohr // new words the word file has some more lines which need to 15644ca0adfSAndreas Gohr // be added here 15744ca0adfSAndreas Gohr $word_idx = file($conf['cachedir'].'/word.idx'); 15844ca0adfSAndreas Gohr $wcnt = count($word_idx); 15944ca0adfSAndreas Gohr for($lno; $lno<$wcnt; $lno++){ 16044ca0adfSAndreas Gohr idx_writeIndexLine($tmp,'',$pid,$words[$lno]); 16144ca0adfSAndreas Gohr } 16244ca0adfSAndreas Gohr 16344ca0adfSAndreas Gohr // close the temp file and move it over to be the new one 16444ca0adfSAndreas Gohr fclose($tmp); 1659684e36cSAndreas Gohr // try rename first (fast) fallback to copy (slow) 1669684e36cSAndreas Gohr if(@rename($conf['cachedir'].'/index.tmp', 1679684e36cSAndreas Gohr $conf['cachedir'].'/index.idx')){ 1689684e36cSAndreas Gohr return true; 1699684e36cSAndreas Gohr }elseif(copy($conf['cachedir'].'/index.tmp', 170d7c3763dSDave Doyle $conf['cachedir'].'/index.idx')){ 171d7c3763dSDave Doyle unlink($conf['cachedir'].'/index.tmp'); 172d7c3763dSDave Doyle return true; 173d7c3763dSDave Doyle } 1749684e36cSAndreas Gohr return false; 17544ca0adfSAndreas Gohr} 17644ca0adfSAndreas Gohr 17744ca0adfSAndreas Gohr/** 17844ca0adfSAndreas Gohr * Write a new index line to the filehandle 17944ca0adfSAndreas Gohr * 18044ca0adfSAndreas Gohr * This function writes an line for the index file to the 18144ca0adfSAndreas Gohr * given filehandle. It removes the given document from 18244ca0adfSAndreas Gohr * the given line and readds it when $count is >0. 18344ca0adfSAndreas Gohr * 18444ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 18544ca0adfSAndreas Gohr */ 18644ca0adfSAndreas Gohrfunction idx_writeIndexLine($fh,$line,$pid,$count){ 18744ca0adfSAndreas Gohr $line = trim($line); 18844ca0adfSAndreas Gohr 18944ca0adfSAndreas Gohr if($line != ''){ 19044ca0adfSAndreas Gohr $parts = explode(':',$line); 19144ca0adfSAndreas Gohr // remove doc from given line 19244ca0adfSAndreas Gohr foreach($parts as $part){ 19344ca0adfSAndreas Gohr if($part == '') continue; 19444ca0adfSAndreas Gohr list($doc,$cnt) = explode('*',$part); 19544ca0adfSAndreas Gohr if($doc != $pid){ 19644ca0adfSAndreas Gohr fwrite($fh,"$doc*$cnt:"); 19744ca0adfSAndreas Gohr } 19844ca0adfSAndreas Gohr } 19944ca0adfSAndreas Gohr } 20044ca0adfSAndreas Gohr 20144ca0adfSAndreas Gohr // add doc 20244ca0adfSAndreas Gohr if ($count){ 20344ca0adfSAndreas Gohr fwrite($fh,"$pid*$count"); 20444ca0adfSAndreas Gohr } 20544ca0adfSAndreas Gohr 20644ca0adfSAndreas Gohr // add newline 20744ca0adfSAndreas Gohr fwrite($fh,"\n"); 20844ca0adfSAndreas Gohr} 209b4ce25e9SAndreas Gohr 210488dd6ceSAndreas Gohr/** 211488dd6ceSAndreas Gohr * Lookup words in index 212488dd6ceSAndreas Gohr * 213488dd6ceSAndreas Gohr * Takes an array of word and will return a list of matching 214488dd6ceSAndreas Gohr * documents for each one. 215488dd6ceSAndreas Gohr * 21663773904SAndreas Gohr * Important: No ACL checking is done here! All results are 21763773904SAndreas Gohr * returned, regardless of permissions 21863773904SAndreas Gohr * 219488dd6ceSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 220488dd6ceSAndreas Gohr */ 221488dd6ceSAndreas Gohrfunction idx_lookup($words){ 222488dd6ceSAndreas Gohr global $conf; 223488dd6ceSAndreas Gohr 224488dd6ceSAndreas Gohr $result = array(); 225488dd6ceSAndreas Gohr 226488dd6ceSAndreas Gohr // load known words and documents 227488dd6ceSAndreas Gohr $page_idx = file($conf['cachedir'].'/page.idx'); 228488dd6ceSAndreas Gohr $word_idx = file($conf['cachedir'].'/word.idx'); 229488dd6ceSAndreas Gohr 230488dd6ceSAndreas Gohr // get word IDs 231488dd6ceSAndreas Gohr $wids = array(); 232488dd6ceSAndreas Gohr foreach($words as $word){ 233488dd6ceSAndreas Gohr $wid = array_search("$word\n",$word_idx); 234488dd6ceSAndreas Gohr if(is_int($wid)){ 235488dd6ceSAndreas Gohr $wids[] = $wid; 236f5eb7cf0SAndreas Gohr $result[$word] = $wid; 237f5eb7cf0SAndreas Gohr }else{ 238f5eb7cf0SAndreas Gohr $result[$word] = array(); 239488dd6ceSAndreas Gohr } 240488dd6ceSAndreas Gohr } 241488dd6ceSAndreas Gohr sort($wids); 242f5eb7cf0SAndreas Gohr $wids = array_unique($wids); 243488dd6ceSAndreas Gohr 244488dd6ceSAndreas Gohr // Open index 245488dd6ceSAndreas Gohr $idx = fopen($conf['cachedir'].'/index.idx','r'); 246488dd6ceSAndreas Gohr if(!$idx){ 247488dd6ceSAndreas Gohr msg("Failed to open index files",-1); 248488dd6ceSAndreas Gohr return false; 249488dd6ceSAndreas Gohr } 250488dd6ceSAndreas Gohr 251488dd6ceSAndreas Gohr // Walk the index til the lines are found 252488dd6ceSAndreas Gohr $docs = array(); // hold docs found 253488dd6ceSAndreas Gohr $lno = 0; 254488dd6ceSAndreas Gohr $line = ''; 255488dd6ceSAndreas Gohr $srch = array_shift($wids); // which word do we look for? 256488dd6ceSAndreas Gohr while (!feof($idx)) { 257488dd6ceSAndreas Gohr // read full line 258488dd6ceSAndreas Gohr $line .= fgets($idx, 4096); 259488dd6ceSAndreas Gohr if(substr($line,-1) != "\n") continue; 260488dd6ceSAndreas Gohr if($lno > $srch) break; // shouldn't happen 261488dd6ceSAndreas Gohr 262488dd6ceSAndreas Gohr 263488dd6ceSAndreas Gohr // do we want this line? 264488dd6ceSAndreas Gohr if($lno == $srch){ 265488dd6ceSAndreas Gohr // add docs to list 266488dd6ceSAndreas Gohr $docs[$srch] = idx_parseIndexLine($page_idx,$line); 267488dd6ceSAndreas Gohr 268488dd6ceSAndreas Gohr $srch = array_shift($wids); // next word to look up 269488dd6ceSAndreas Gohr if($srch == null) break; // no more words 270488dd6ceSAndreas Gohr } 271488dd6ceSAndreas Gohr 272488dd6ceSAndreas Gohr $line = ''; // reset line buffer 273488dd6ceSAndreas Gohr $lno++; // increase linecounter 274488dd6ceSAndreas Gohr } 275488dd6ceSAndreas Gohr fclose($idx); 276488dd6ceSAndreas Gohr 277f5eb7cf0SAndreas Gohr // merge found pages into result array 278f5eb7cf0SAndreas Gohr foreach(array_keys($result) as $word){ 279f5eb7cf0SAndreas Gohr if(is_int($result[$word])){ 280f5eb7cf0SAndreas Gohr $result[$word] = $docs[$result[$word]]; 281488dd6ceSAndreas Gohr } 282488dd6ceSAndreas Gohr } 283488dd6ceSAndreas Gohr 284f5eb7cf0SAndreas Gohr return $result; 285488dd6ceSAndreas Gohr} 286488dd6ceSAndreas Gohr 287488dd6ceSAndreas Gohr/** 288488dd6ceSAndreas Gohr * Returns a list of documents and counts from a index line 289488dd6ceSAndreas Gohr * 290488dd6ceSAndreas Gohr * It omits docs with a count of 0 and pages that no longer 291488dd6ceSAndreas Gohr * exist. 292488dd6ceSAndreas Gohr * 293488dd6ceSAndreas Gohr * @param array $page_idx The list of known pages 294488dd6ceSAndreas Gohr * @param string $line A line from the main index 295488dd6ceSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 296488dd6ceSAndreas Gohr */ 297488dd6ceSAndreas Gohrfunction idx_parseIndexLine(&$page_idx,$line){ 298488dd6ceSAndreas Gohr $result = array(); 299488dd6ceSAndreas Gohr 300488dd6ceSAndreas Gohr $line = trim($line); 301f5eb7cf0SAndreas Gohr if($line == '') return $result; 302488dd6ceSAndreas Gohr 303488dd6ceSAndreas Gohr $parts = explode(':',$line); 304488dd6ceSAndreas Gohr foreach($parts as $part){ 305488dd6ceSAndreas Gohr if($part == '') continue; 306488dd6ceSAndreas Gohr list($doc,$cnt) = explode('*',$part); 307488dd6ceSAndreas Gohr if(!$cnt) continue; 308488dd6ceSAndreas Gohr $doc = trim($page_idx[$doc]); 309488dd6ceSAndreas Gohr if(!$doc) continue; 310488dd6ceSAndreas Gohr // make sure the document still exists 311488dd6ceSAndreas Gohr if(!@file_exists(wikiFN($doc))) continue; 312488dd6ceSAndreas Gohr 313488dd6ceSAndreas Gohr $result[$doc] = $cnt; 314488dd6ceSAndreas Gohr } 315488dd6ceSAndreas Gohr return $result; 316488dd6ceSAndreas Gohr} 317488dd6ceSAndreas Gohr 318f5eb7cf0SAndreas Gohr/** 319f5eb7cf0SAndreas Gohr * Tokenizes a string into an array of search words 320f5eb7cf0SAndreas Gohr * 321f5eb7cf0SAndreas Gohr * Uses the same algorithm as idx_getPageWords() 322f5eb7cf0SAndreas Gohr * 323f5eb7cf0SAndreas Gohr * @todo make combined function to use alone or in getPageWords 324f5eb7cf0SAndreas Gohr */ 325f5eb7cf0SAndreas Gohrfunction idx_tokenizer($string,&$stopwords){ 326f5eb7cf0SAndreas Gohr $words = array(); 327f5eb7cf0SAndreas Gohr 328f5eb7cf0SAndreas Gohr if(preg_match('/[^0-9A-Za-z]/u', $string)){ 329*93a60ad2SAndreas Gohr #handle asian chars as single words 330*93a60ad2SAndreas Gohr $string = preg_replace('/('.IDX_ASIAN.')/u','\1 ',$string); 331*93a60ad2SAndreas Gohr 332f5eb7cf0SAndreas Gohr $arr = explode(' ', utf8_stripspecials($string,' ','._\-:')); 333f5eb7cf0SAndreas Gohr foreach ($arr as $w) { 334f5eb7cf0SAndreas Gohr if (!is_numeric($w) && strlen($w) < 3) continue; 335f5eb7cf0SAndreas Gohr $w = utf8_strtolower($w); 3363cbaa9a4SAndreas Gohr if($stopwords && is_int(array_search("$w\n",$stopwords))) continue; 337f5eb7cf0SAndreas Gohr $words[] = $w; 338f5eb7cf0SAndreas Gohr } 339f5eb7cf0SAndreas Gohr }else{ 340f5eb7cf0SAndreas Gohr $w = $string; 341f5eb7cf0SAndreas Gohr if (!is_numeric($w) && strlen($w) < 3) return $words; 342f5eb7cf0SAndreas Gohr $w = strtolower($w); 343f5eb7cf0SAndreas Gohr if(is_int(array_search("$w\n",$stopwords))) return $words; 344f5eb7cf0SAndreas Gohr $words[] = $w; 345f5eb7cf0SAndreas Gohr } 346f5eb7cf0SAndreas Gohr 347f5eb7cf0SAndreas Gohr return $words; 348f5eb7cf0SAndreas Gohr} 349f5eb7cf0SAndreas Gohr 350b4ce25e9SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 351