3 * Common DokuWiki functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
9 if(!defined('DOKU_INC')) die('meh.');
10 require_once(DOKU_INC
.'inc/io.php');
11 require_once(DOKU_INC
.'inc/utf8.php');
12 require_once(DOKU_INC
.'inc/parserutils.php');
14 // set the minimum token length to use in the index (note, this doesn't apply to numeric tokens)
15 if (!defined('IDX_MINWORDLENGTH')) define('IDX_MINWORDLENGTH',2);
17 // Asian characters are handled as words. The following regexp defines the
18 // Unicode-Ranges for Asian characters
19 // Ranges taken from http://en.wikipedia.org/wiki/Unicode_block
20 // I'm no language expert. If you think some ranges are wrongly chosen or
21 // a range is missing, please contact me
22 define('IDX_ASIAN1','[\x{0E00}-\x{0E7F}]'); // Thai
23 define('IDX_ASIAN2','['.
24 '\x{2E80}-\x{3040}'. // CJK -> Hangul
26 '\x{30FD}-\x{31EF}\x{3200}-\x{D7AF}'.
27 '\x{F900}-\x{FAFF}'. // CJK Compatibility Ideographs
28 '\x{FE30}-\x{FE4F}'. // CJK Compatibility Forms
30 define('IDX_ASIAN3','['. // Hiragana/Katakana (can be two characters)
31 '\x{3042}\x{3044}\x{3046}\x{3048}'.
32 '\x{304A}-\x{3062}\x{3064}-\x{3082}'.
33 '\x{3084}\x{3086}\x{3088}-\x{308D}'.
35 '\x{30A2}\x{30A4}\x{30A6}\x{30A8}'.
36 '\x{30AA}-\x{30C2}\x{30C4}-\x{30E2}'.
37 '\x{30E4}\x{30E6}\x{30E8}-\x{30ED}'.
38 '\x{30EF}-\x{30F4}\x{30F7}-\x{30FA}'.
40 '\x{3041}\x{3043}\x{3045}\x{3047}\x{3049}'.
41 '\x{3063}\x{3083}\x{3085}\x{3087}\x{308E}\x{3095}-\x{309C}'.
42 '\x{30A1}\x{30A3}\x{30A5}\x{30A7}\x{30A9}'.
43 '\x{30C3}\x{30E3}\x{30E5}\x{30E7}\x{30EE}\x{30F5}\x{30F6}\x{30FB}\x{30FC}'.
46 define('IDX_ASIAN', '(?:'.IDX_ASIAN1
.'|'.IDX_ASIAN2
.'|'.IDX_ASIAN3
.')');
49 * Measure the length of a string.
50 * Differs from strlen in handling of asian characters.
52 * @author Tom N Harris <tnharris@whoopdedo.org>
56 // If left alone, all chinese "words" will get put into w3.idx
57 // So the "length" of a "word" is faked
58 if(preg_match('/'.IDX_ASIAN2
.'/u',$w))
59 $l +
= ord($w) - 0xE1; // Lead bytes from 0xE2-0xEF
64 * Write a list of strings to an index file.
66 * @author Tom N Harris <tnharris@whoopdedo.org>
68 function idx_saveIndex($pre, $wlen, &$idx){
70 $fn = $conf['indexdir'].'/'.$pre.$wlen;
71 $fh = @fopen
($fn.'.tmp','w');
72 if(!$fh) return false;
73 foreach ($idx as $line) {
77 if(isset($conf['fperm'])) chmod($fn.'.tmp', $conf['fperm']);
78 io_rename($fn.'.tmp', $fn.'.idx');
83 * Append a given line to an index file.
85 * @author Andreas Gohr <andi@splitbrain.org>
87 function idx_appendIndex($pre, $wlen, $line){
89 $fn = $conf['indexdir'].'/'.$pre.$wlen;
90 $fh = @fopen
($fn.'.idx','a');
91 if(!$fh) return false;
98 * Read the list of words in an index (if it exists).
100 * @author Tom N Harris <tnharris@whoopdedo.org>
102 function idx_getIndex($pre, $wlen){
104 $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx';
105 if(!@file_exists
($fn)) return array();
110 * Create an empty index file if it doesn't exist yet.
112 * FIXME: This function isn't currently used. It will probably be removed soon.
114 * @author Tom N Harris <tnharris@whoopdedo.org>
116 function idx_touchIndex($pre, $wlen){
118 $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx';
119 if(!@file_exists
($fn)){
121 if($conf['fperm']) chmod($fn, $conf['fperm']);
126 * Read a line ending with \n.
127 * Returns false on EOF.
129 * @author Tom N Harris <tnharris@whoopdedo.org>
131 function _freadline($fh) {
132 if (feof($fh)) return false;
134 while (($buf = fgets($fh,4096)) !== false) {
136 if (substr($buf,-1) == "\n") break;
138 if ($ln === '') return false;
139 if (substr($ln,-1) != "\n") $ln .= "\n";
144 * Write a line to an index file.
146 * @author Tom N Harris <tnharris@whoopdedo.org>
148 function idx_saveIndexLine($pre, $wlen, $idx, $line){
150 if(substr($line,-1) != "\n") $line .= "\n";
151 $fn = $conf['indexdir'].'/'.$pre.$wlen;
152 $fh = @fopen
($fn.'.tmp','w');
153 if(!$fh) return false;
154 $ih = @fopen
($fn.'.idx','r');
157 while (($curline = _freadline($ih)) !== false) {
161 fwrite($fh, $curline);
172 if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']);
173 io_rename($fn.'.tmp', $fn.'.idx');
178 * Read a single line from an index (if it exists).
180 * @author Tom N Harris <tnharris@whoopdedo.org>
182 function idx_getIndexLine($pre, $wlen, $idx){
184 $fn = $conf['indexdir'].'/'.$pre.$wlen.'.idx';
185 if(!@file_exists
($fn)) return '';
186 $fh = @fopen
($fn,'r');
189 while (($line = _freadline($fh)) !== false) {
190 if (++
$ln == $idx) break;
197 * Split a page into words
199 * Returns an array of word counts, false if an error occurred.
200 * Array is keyed on the word length, then the word index.
202 * @author Andreas Gohr <andi@splitbrain.org>
203 * @author Christopher Smith <chris@jalakai.co.uk>
205 function idx_getPageWords($page){
207 $swfile = DOKU_INC
.'inc/lang/'.$conf['lang'].'/stopwords.txt';
208 if(@file_exists
($swfile)){
209 $stopwords = file($swfile);
211 $stopwords = array();
215 $data = array($page, $body);
216 $evt = new Doku_Event('INDEXER_PAGE_ADD', $data);
217 if ($evt->advise_before()) $data[1] .= rawWiki($page);
218 $evt->advise_after();
221 list($page,$body) = $data;
223 $body = strtr($body, "\r\n\t", ' ');
224 $tokens = explode(' ', $body);
225 $tokens = array_count_values($tokens); // count the frequency of each token
227 // ensure the deaccented or romanised page names of internal links are added to the token array
228 // (this is necessary for the backlink function -- there maybe a better way!)
229 if ($conf['deaccent']) {
230 $links = p_get_metadata($page,'relation references');
232 if (!empty($links)) {
233 $tmp = join(' ',array_keys($links)); // make a single string
234 $tmp = strtr($tmp, ':', ' '); // replace namespace separator with a space
235 $link_tokens = array_unique(explode(' ', $tmp)); // break into tokens
237 foreach ($link_tokens as $link_token) {
238 if (isset($tokens[$link_token])) continue;
239 $tokens[$link_token] = 1;
245 foreach ($tokens as $word => $count) {
246 $arr = idx_tokenizer($word,$stopwords);
247 $arr = array_count_values($arr);
248 foreach ($arr as $w => $c) {
250 if(isset($words[$l])){
251 $words[$l][$w] = $c * $count +
(isset($words[$l][$w]) ?
$words[$l][$w] : 0);
253 $words[$l] = array($w => $c * $count);
258 // arrive here with $words = array(wordlen => array(word => frequency))
260 $index = array(); //resulting index
261 foreach (array_keys($words) as $wlen){
262 $word_idx = idx_getIndex('w',$wlen);
263 foreach ($words[$wlen] as $word => $freq) {
264 $wid = array_search("$word\n",$word_idx);
266 $wid = count($word_idx);
267 $word_idx[] = "$word\n";
269 if(!isset($index[$wlen]))
270 $index[$wlen] = array();
271 $index[$wlen][$wid] = $freq;
274 // save back word index
275 if(!idx_saveIndex('w',$wlen,$word_idx)){
276 trigger_error("Failed to write word index", E_USER_ERROR
);
285 * Adds/updates the search for the given page
287 * This is the core function of the indexer which does most
288 * of the work. This function needs to be called with proper
291 * @author Andreas Gohr <andi@splitbrain.org>
293 function idx_addPage($page){
296 // load known documents
297 $page_idx = idx_getIndex('page','');
299 // get page id (this is the linenumber in page.idx)
300 $pid = array_search("$page\n",$page_idx);
302 $pid = count($page_idx);
303 // page was new - write back
304 if (!idx_appendIndex('page','',"$page\n")){
305 trigger_error("Failed to write page index", E_USER_ERROR
);
309 unset($page_idx); // free memory
311 $pagewords = array();
312 // get word usage in page
313 $words = idx_getPageWords($page);
314 if($words === false) return false;
317 foreach(array_keys($words) as $wlen){
318 $index = idx_getIndex('i',$wlen);
319 foreach($words[$wlen] as $wid => $freq){
320 if($wid<count($index)){
321 $index[$wid] = idx_updateIndexLine($index[$wid],$pid,$freq);
323 // New words **should** have been added in increasing order
324 // starting with the first unassigned index.
325 // If someone can show how this isn't true, then I'll need to sort
326 // or do something special.
327 $index[$wid] = idx_updateIndexLine('',$pid,$freq);
329 $pagewords[] = "$wlen*$wid";
331 // save back word index
332 if(!idx_saveIndex('i',$wlen,$index)){
333 trigger_error("Failed to write index", E_USER_ERROR
);
339 // Remove obsolete index entries
340 $pageword_idx = trim(idx_getIndexLine('pageword','',$pid));
341 if ($pageword_idx !== '') {
342 $oldwords = explode(':',$pageword_idx);
343 $delwords = array_diff($oldwords, $pagewords);
345 foreach ($delwords as $word) {
346 if($word=='') continue;
347 list($wlen,$wid) = explode('*',$word);
349 $upwords[$wlen][] = $wid;
351 foreach ($upwords as $wlen => $widx) {
352 $index = idx_getIndex('i',$wlen);
353 foreach ($widx as $wid) {
354 $index[$wid] = idx_updateIndexLine($index[$wid],$pid,0);
356 idx_saveIndex('i',$wlen,$index);
359 // Save the reverse index
360 $pageword_idx = join(':',$pagewords)."\n";
361 if(!idx_saveIndexLine('pageword','',$pid,$pageword_idx)){
362 trigger_error("Failed to write word index", E_USER_ERROR
);
370 * Write a new index line to the filehandle
372 * This function writes an line for the index file to the
373 * given filehandle. It removes the given document from
374 * the given line and readds it when $count is >0.
376 * @deprecated - see idx_updateIndexLine
377 * @author Andreas Gohr <andi@splitbrain.org>
379 function idx_writeIndexLine($fh,$line,$pid,$count){
380 fwrite($fh,idx_updateIndexLine($line,$pid,$count));
384 * Modify an index line with new information
386 * This returns a line of the index. It removes the
387 * given document from the line and readds it if
390 * @author Tom N Harris <tnharris@whoopdedo.org>
391 * @author Andreas Gohr <andi@splitbrain.org>
393 function idx_updateIndexLine($line,$pid,$count){
397 $parts = explode(':',$line);
398 // remove doc from given line
399 foreach($parts as $part){
400 if($part == '') continue;
401 list($doc,$cnt) = explode('*',$part);
410 $updated[] = "$pid*$count";
413 return join(':',$updated)."\n";
417 * Get the word lengths that have been indexed.
419 * Reads the index directory and returns an array of lengths
420 * that there are indices for.
422 * @author Tom N Harris <tnharris@whoopdedo.org>
424 function idx_indexLengths(&$filter){
426 $dir = @opendir
($conf['indexdir']);
430 if(is_array($filter)){
431 while (($f = readdir($dir)) !== false) {
432 if (substr($f,0,1) == 'i' && substr($f,-4) == '.idx'){
433 $i = substr($f,1,-4);
434 if (is_numeric($i) && isset($filter[(int)$i]))
439 // Exact match first.
440 if(@file_exists
($conf['indexdir']."/i$filter.idx"))
442 while (($f = readdir($dir)) !== false) {
443 if (substr($f,0,1) == 'i' && substr($f,-4) == '.idx'){
444 $i = substr($f,1,-4);
445 if (is_numeric($i) && $i > $filter)
455 * Find the the index number of each search term.
457 * This will group together words that appear in the same index.
458 * So it should perform better, because it only opens each index once.
459 * Actually, it's not that great. (in my experience) Probably because of the disk cache.
460 * And the sorted function does more work, making it slightly slower in some cases.
462 * @param array $words The query terms. Words should only contain valid characters,
463 * with a '*' at either the beginning or end of the word (or both)
464 * @param arrayref $result Set to word => array("length*id" ...), use this to merge the
465 * index locations with the appropriate query term.
466 * @return array Set to length => array(id ...)
468 * @author Tom N Harris <tnharris@whoopdedo.org>
470 function idx_getIndexWordsSorted($words,&$result){
471 // parse and sort tokens
473 $tokenlength = array();
474 $tokenwild = array();
475 foreach($words as $word){
476 $result[$word] = array();
479 $wlen = wordlen($word);
481 // check for wildcards
482 if(substr($xword,0,1) == '*'){
483 $xword = substr($xword,1);
487 if(substr($xword,-1,1) == '*'){
488 $xword = substr($xword,0,-1);
492 if ($wlen < IDX_MINWORDLENGTH
&& $wild == 0 && !is_numeric($xword)) continue;
493 if(!isset($tokens[$xword])){
494 $tokenlength[$wlen][] = $xword;
497 $ptn = preg_quote($xword,'/');
498 if(($wild&1) == 0) $ptn = '^'.$ptn;
499 if(($wild&2) == 0) $ptn = $ptn.'$';
500 $tokens[$xword][] = array($word, '/'.$ptn.'/');
501 if(!isset($tokenwild[$xword])) $tokenwild[$xword] = $wlen;
503 $tokens[$xword][] = array($word, null);
506 // $tokens = array( base word => array( [ query word , grep pattern ] ... ) ... )
507 // $tokenlength = array( base word length => base word ... )
508 // $tokenwild = array( base word => base word length ... )
510 $length_filter = empty($tokenwild) ?
$tokenlength : min(array_keys($tokenlength));
511 $indexes_known = idx_indexLengths($length_filter);
512 if(!empty($tokenwild)) sort($indexes_known);
515 foreach($indexes_known as $ixlen){
516 $word_idx = idx_getIndex('w',$ixlen);
517 // handle exact search
518 if(isset($tokenlength[$ixlen])){
519 foreach($tokenlength[$ixlen] as $xword){
520 $wid = array_search("$xword\n",$word_idx);
522 $wids[$ixlen][] = $wid;
523 foreach($tokens[$xword] as $w)
524 $result[$w[0]][] = "$ixlen*$wid";
528 // handle wildcard search
529 foreach($tokenwild as $xword => $wlen){
530 if($wlen >= $ixlen) break;
531 foreach($tokens[$xword] as $w){
532 if(is_null($w[1])) continue;
533 foreach(array_keys(preg_grep($w[1],$word_idx)) as $wid){
534 $wids[$ixlen][] = $wid;
535 $result[$w[0]][] = "$ixlen*$wid";
544 * Lookup words in index
546 * Takes an array of word and will return a list of matching
547 * documents for each one.
549 * Important: No ACL checking is done here! All results are
550 * returned, regardless of permissions
552 * @author Andreas Gohr <andi@splitbrain.org>
554 function idx_lookup($words){
559 $wids = idx_getIndexWordsSorted($words, $result);
560 if(empty($wids)) return array();
562 // load known words and documents
563 $page_idx = idx_getIndex('page','');
565 $docs = array(); // hold docs found
566 foreach(array_keys($wids) as $wlen){
567 $wids[$wlen] = array_unique($wids[$wlen]);
568 $index = idx_getIndex('i',$wlen);
569 foreach($wids[$wlen] as $ixid){
570 if($ixid < count($index))
571 $docs["$wlen*$ixid"] = idx_parseIndexLine($page_idx,$index[$ixid]);
575 // merge found pages into final result array
577 foreach($result as $word => $res){
578 $final[$word] = array();
579 foreach($res as $wid){
580 $hits = &$docs[$wid];
581 foreach ($hits as $hitkey => $hitcnt) {
582 if (!isset($final[$word][$hitkey])) {
583 $final[$word][$hitkey] = $hitcnt;
585 $final[$word][$hitkey] +
= $hitcnt;
594 * Returns a list of documents and counts from a index line
596 * It omits docs with a count of 0 and pages that no longer
599 * @param array $page_idx The list of known pages
600 * @param string $line A line from the main index
601 * @author Andreas Gohr <andi@splitbrain.org>
603 function idx_parseIndexLine(&$page_idx,$line){
607 if($line == '') return $result;
609 $parts = explode(':',$line);
610 foreach($parts as $part){
611 if($part == '') continue;
612 list($doc,$cnt) = explode('*',$part);
614 $doc = trim($page_idx[$doc]);
616 // make sure the document still exists
617 if(!page_exists($doc,'',false)) continue;
619 $result[$doc] = $cnt;
625 * Tokenizes a string into an array of search words
627 * Uses the same algorithm as idx_getPageWords()
629 * @param string $string the query as given by the user
630 * @param arrayref $stopwords array of stopwords
631 * @param boolean $wc are wildcards allowed?
633 function idx_tokenizer($string,&$stopwords,$wc=false){
635 $wc = ($wc) ?
'' : $wc = '\*';
637 if(preg_match('/[^0-9A-Za-z]/u', $string)){
638 // handle asian chars as single words (may fail on older PHP version)
639 $asia = @preg_replace
('/('.IDX_ASIAN
.')/u',' \1 ',$string);
640 if(!is_null($asia)) $string = $asia; //recover from regexp failure
642 $arr = explode(' ', utf8_stripspecials($string,' ','\._\-:'.$wc));
643 foreach ($arr as $w) {
644 if (!is_numeric($w) && strlen($w) < IDX_MINWORDLENGTH
) continue;
645 $w = utf8_strtolower($w);
646 if($stopwords && is_int(array_search("$w\n",$stopwords))) continue;
651 if (!is_numeric($w) && strlen($w) < IDX_MINWORDLENGTH
) return $words;
653 if(is_int(array_search("$w\n",$stopwords))) return $words;
661 * Create a pagewords index from the existing index.
663 * @author Tom N Harris <tnharris@whoopdedo.org>
665 function idx_upgradePageWords(){
667 $page_idx = idx_getIndex('page','');
668 if (empty($page_idx)) return;
669 $pagewords = array();
670 $len = count($page_idx);
671 for ($n=0;$n<$len;$n++
){
672 $pagewords[] = array();
677 foreach (idx_indexLengths($n) as $wlen) {
678 $lines = idx_getIndex('i',$wlen);
679 $len = count($lines);
680 for ($wid=0;$wid<$len;$wid++
) {
681 $wkey = "$wlen*$wid";
682 foreach (explode(':',trim($lines[$wid])) as $part) {
683 if($part == '') continue;
684 list($doc,$cnt) = explode('*',$part);
685 $pagewords[(int)$doc][] = $wkey;
690 $fn = $conf['indexdir'].'/pageword';
691 $fh = @fopen
($fn.'.tmp','w');
693 trigger_error("Failed to write word index", E_USER_ERROR
);
696 foreach ($pagewords as $line){
697 fwrite($fh, join(':',$line)."\n");
700 if($conf['fperm']) chmod($fn.'.tmp', $conf['fperm']);
701 io_rename($fn.'.tmp', $fn.'.idx');
705 //Setup VIM: ex: et ts=4 enc=utf-8 :