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 15b4ce25e9SAndreas Gohr/** 1644ca0adfSAndreas Gohr * Split a page into words 1744ca0adfSAndreas Gohr * 1844ca0adfSAndreas Gohr * Returns an array of of word counts, false if an error occured 1944ca0adfSAndreas Gohr * 2044ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 21*17f42b01SChris Smith * @author Christopher Smith <chris@jalakai.co.uk> 22b4ce25e9SAndreas Gohr */ 2344ca0adfSAndreas Gohrfunction idx_getPageWords($page){ 2444ca0adfSAndreas Gohr global $conf; 2544ca0adfSAndreas Gohr $word_idx = file($conf['cachedir'].'/word.idx'); 267367b368SAndreas Gohr $swfile = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt'; 277367b368SAndreas Gohr if(@file_exists($swfile)){ 287367b368SAndreas Gohr $stopwords = file($swfile); 297367b368SAndreas Gohr }else{ 307367b368SAndreas Gohr $stopwords = array(); 317367b368SAndreas Gohr } 3244ca0adfSAndreas Gohr 3344ca0adfSAndreas Gohr $body = rawWiki($page); 34*17f42b01SChris Smith $body = strtr($body, "\r\n\t", ' '); 35*17f42b01SChris Smith $tokens = explode(' ', $body); 36*17f42b01SChris Smith $tokens = array_count_values($tokens); // count the frequency of each token 37*17f42b01SChris Smith 38*17f42b01SChris Smith $words = array(); 39*17f42b01SChris Smith foreach ($tokens as $word => $count) { 40*17f42b01SChris Smith $word = utf8_strtolower($word); 41*17f42b01SChris Smith 42*17f42b01SChris Smith // simple filter to restrict use of utf8_stripspecials 43*17f42b01SChris Smith if (preg_match('/\W/', $word)) { 44*17f42b01SChris Smith $arr = explode(' ', utf8_stripspecials($word,' ','._\-:')); 45*17f42b01SChris Smith $arr = array_count_values($arr); 46*17f42b01SChris Smith 47*17f42b01SChris Smith foreach ($arr as $w => $c) { 48*17f42b01SChris Smith if (!is_numeric($w) && strlen($w) < 3) continue; 49*17f42b01SChris Smith $words[$w] = $c + (isset($words[$w]) ? $words[$w] : 0); 50*17f42b01SChris Smith } 51*17f42b01SChris Smith } else { 52*17f42b01SChris Smith if (!is_numeric($w) && strlen($w) < 3) continue; 53*17f42b01SChris Smith $words[$word] = $count + (isset($words[$word]) ? $words[$word] : 0); 54*17f42b01SChris Smith } 55*17f42b01SChris Smith } 56*17f42b01SChris Smith 57*17f42b01SChris Smith // arrive here with $words = array(word => frequency) 58b4ce25e9SAndreas Gohr 59b4ce25e9SAndreas Gohr $index = array(); //resulting index 60*17f42b01SChris Smith foreach ($words as $word => $freq) { 61*17f42b01SChris Smith if (is_int(array_search("$word\n",$stopwords))) continue; 6244ca0adfSAndreas Gohr $wid = array_search("$word\n",$word_idx); 6344ca0adfSAndreas Gohr if(!is_int($wid)){ 6444ca0adfSAndreas Gohr $word_idx[] = "$word\n"; 6544ca0adfSAndreas Gohr $wid = count($word_idx)-1; 66b4ce25e9SAndreas Gohr } 67*17f42b01SChris Smith $index[$wid] = $freq; 6844ca0adfSAndreas Gohr } 6944ca0adfSAndreas Gohr 7044ca0adfSAndreas Gohr // save back word index 7144ca0adfSAndreas Gohr $fh = fopen($conf['cachedir'].'/word.idx','w'); 7244ca0adfSAndreas Gohr if(!$fh){ 7344ca0adfSAndreas Gohr trigger_error("Failed to write word.idx", E_USER_ERROR); 7444ca0adfSAndreas Gohr return false; 7544ca0adfSAndreas Gohr } 7644ca0adfSAndreas Gohr fwrite($fh,join('',$word_idx)); 7744ca0adfSAndreas Gohr fclose($fh); 78b4ce25e9SAndreas Gohr 79b4ce25e9SAndreas Gohr return $index; 80b4ce25e9SAndreas Gohr} 81b4ce25e9SAndreas Gohr 8244ca0adfSAndreas Gohr/** 8344ca0adfSAndreas Gohr * Adds/updates the search for the given page 8444ca0adfSAndreas Gohr * 8544ca0adfSAndreas Gohr * This is the core function of the indexer which does most 8644ca0adfSAndreas Gohr * of the work. This function needs to be called with proper 8744ca0adfSAndreas Gohr * locking! 8844ca0adfSAndreas Gohr * 8944ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 9044ca0adfSAndreas Gohr */ 9144ca0adfSAndreas Gohrfunction idx_addPage($page){ 9244ca0adfSAndreas Gohr global $conf; 93b4ce25e9SAndreas Gohr 9444ca0adfSAndreas Gohr // load known words and documents 9544ca0adfSAndreas Gohr $page_idx = file($conf['cachedir'].'/page.idx'); 9644ca0adfSAndreas Gohr 9744ca0adfSAndreas Gohr // get page id (this is the linenumber in page.idx) 9844ca0adfSAndreas Gohr $pid = array_search("$page\n",$page_idx); 9944ca0adfSAndreas Gohr if(!is_int($pid)){ 10044ca0adfSAndreas Gohr $page_idx[] = "$page\n"; 10144ca0adfSAndreas Gohr $pid = count($page_idx)-1; 10244ca0adfSAndreas Gohr // page was new - write back 10344ca0adfSAndreas Gohr $fh = fopen($conf['cachedir'].'/page.idx','w'); 10444ca0adfSAndreas Gohr if(!$fh) return false; 10544ca0adfSAndreas Gohr fwrite($fh,join('',$page_idx)); 10644ca0adfSAndreas Gohr fclose($fh); 10744ca0adfSAndreas Gohr } 10844ca0adfSAndreas Gohr 10944ca0adfSAndreas Gohr // get word usage in page 11044ca0adfSAndreas Gohr $words = idx_getPageWords($page); 11144ca0adfSAndreas Gohr if($words === false) return false; 11244ca0adfSAndreas Gohr if(!count($words)) return true; 11344ca0adfSAndreas Gohr 11444ca0adfSAndreas Gohr // Open index and temp file 11544ca0adfSAndreas Gohr $idx = fopen($conf['cachedir'].'/index.idx','r'); 11644ca0adfSAndreas Gohr $tmp = fopen($conf['cachedir'].'/index.tmp','w'); 11744ca0adfSAndreas Gohr if(!$idx || !$tmp){ 11844ca0adfSAndreas Gohr trigger_error("Failed to open index files", E_USER_ERROR); 11944ca0adfSAndreas Gohr return false; 12044ca0adfSAndreas Gohr } 12144ca0adfSAndreas Gohr 12244ca0adfSAndreas Gohr // copy from index to temp file, modifying were needed 12344ca0adfSAndreas Gohr $lno = 0; 12444ca0adfSAndreas Gohr $line = ''; 12544ca0adfSAndreas Gohr while (!feof($idx)) { 12644ca0adfSAndreas Gohr // read full line 12744ca0adfSAndreas Gohr $line .= fgets($idx, 4096); 12844ca0adfSAndreas Gohr if(substr($line,-1) != "\n") continue; 12944ca0adfSAndreas Gohr 13044ca0adfSAndreas Gohr // write a new Line to temp file 13144ca0adfSAndreas Gohr idx_writeIndexLine($tmp,$line,$pid,$words[$lno]); 13244ca0adfSAndreas Gohr 13344ca0adfSAndreas Gohr $line = ''; // reset line buffer 13444ca0adfSAndreas Gohr $lno++; // increase linecounter 13544ca0adfSAndreas Gohr } 13644ca0adfSAndreas Gohr fclose($idx); 13744ca0adfSAndreas Gohr 13844ca0adfSAndreas Gohr // add missing lines (usually index and word should contain 13944ca0adfSAndreas Gohr // the same number of lines, however if the page contained 14044ca0adfSAndreas Gohr // new words the word file has some more lines which need to 14144ca0adfSAndreas Gohr // be added here 14244ca0adfSAndreas Gohr $word_idx = file($conf['cachedir'].'/word.idx'); 14344ca0adfSAndreas Gohr $wcnt = count($word_idx); 14444ca0adfSAndreas Gohr for($lno; $lno<$wcnt; $lno++){ 14544ca0adfSAndreas Gohr idx_writeIndexLine($tmp,'',$pid,$words[$lno]); 14644ca0adfSAndreas Gohr } 14744ca0adfSAndreas Gohr 14844ca0adfSAndreas Gohr // close the temp file and move it over to be the new one 14944ca0adfSAndreas Gohr fclose($tmp); 15044ca0adfSAndreas Gohr return rename($conf['cachedir'].'/index.tmp', 15144ca0adfSAndreas Gohr $conf['cachedir'].'/index.idx'); 15244ca0adfSAndreas Gohr} 15344ca0adfSAndreas Gohr 15444ca0adfSAndreas Gohr/** 15544ca0adfSAndreas Gohr * Write a new index line to the filehandle 15644ca0adfSAndreas Gohr * 15744ca0adfSAndreas Gohr * This function writes an line for the index file to the 15844ca0adfSAndreas Gohr * given filehandle. It removes the given document from 15944ca0adfSAndreas Gohr * the given line and readds it when $count is >0. 16044ca0adfSAndreas Gohr * 16144ca0adfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 16244ca0adfSAndreas Gohr */ 16344ca0adfSAndreas Gohrfunction idx_writeIndexLine($fh,$line,$pid,$count){ 16444ca0adfSAndreas Gohr $line = trim($line); 16544ca0adfSAndreas Gohr 16644ca0adfSAndreas Gohr if($line != ''){ 16744ca0adfSAndreas Gohr $parts = explode(':',$line); 16844ca0adfSAndreas Gohr // remove doc from given line 16944ca0adfSAndreas Gohr foreach($parts as $part){ 17044ca0adfSAndreas Gohr if($part == '') continue; 17144ca0adfSAndreas Gohr list($doc,$cnt) = explode('*',$part); 17244ca0adfSAndreas Gohr if($doc != $pid){ 17344ca0adfSAndreas Gohr fwrite($fh,"$doc*$cnt:"); 17444ca0adfSAndreas Gohr } 17544ca0adfSAndreas Gohr } 17644ca0adfSAndreas Gohr } 17744ca0adfSAndreas Gohr 17844ca0adfSAndreas Gohr // add doc 17944ca0adfSAndreas Gohr if ($count){ 18044ca0adfSAndreas Gohr fwrite($fh,"$pid*$count"); 18144ca0adfSAndreas Gohr } 18244ca0adfSAndreas Gohr 18344ca0adfSAndreas Gohr // add newline 18444ca0adfSAndreas Gohr fwrite($fh,"\n"); 18544ca0adfSAndreas Gohr} 186b4ce25e9SAndreas Gohr 187b4ce25e9SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 : 188