xref: /dokuwiki/inc/fulltext.php (revision 52c5b974b116db2544ff873cad595809e7e04580)
1<?php
2/**
3 * DokuWiki fulltextsearch functions using the index
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9if(!defined('DOKU_INC')) die('meh.');
10require_once(DOKU_INC.'inc/indexer.php');
11
12
13/**
14 * The fulltext search
15 *
16 * Returns a list of matching documents for the given query
17 *
18 * refactored into ft_pageSearch(), _ft_pageSearch() and trigger_event()
19 *
20 */
21function ft_pageSearch($query,&$highlight){
22
23  $data['query'] = $query;
24  $data['highlight'] =& $highlight;
25
26  return trigger_event('SEARCH_QUERY_FULLPAGE', $data, '_ft_pageSearch');
27}
28function _ft_pageSearch(&$data){
29    // split out original parameters
30    $query = $data['query'];
31    $highlight =& $data['highlight'];
32
33    $q = ft_queryParser($query);
34
35    $highlight = array();
36
37    // remember for hilighting later
38    foreach($q['words'] as $wrd){
39        $highlight[] =  str_replace('*','',$wrd);
40    }
41
42    // lookup all words found in the query
43    $words  = array_merge($q['and'],$q['not']);
44    if(!count($words)) return array();
45    $result = idx_lookup($words);
46    if(!count($result)) return array();
47
48    // merge search results with query
49    foreach($q['and'] as $pos => $w){
50        $q['and'][$pos] = $result[$w];
51    }
52    // create a list of unwanted docs
53    $not = array();
54    foreach($q['not'] as $pos => $w){
55        $not = array_merge($not,array_keys($result[$w]));
56    }
57
58    // combine and-words
59    if(count($q['and']) > 1){
60        $docs = ft_resultCombine($q['and']);
61    }else{
62        $docs = $q['and'][0];
63    }
64    if(!count($docs)) return array();
65
66    // create a list of hidden pages in the result
67    $hidden = array();
68    $hidden = array_filter(array_keys($docs),'isHiddenPage');
69    $not = array_merge($not,$hidden);
70
71    // filter unmatched namespaces
72    if(!empty($q['ns'])) {
73        $pattern = implode('|^',$q['ns']);
74        foreach($docs as $key => $val) {
75            if(!preg_match('/^'.$pattern.'/',$key)) {
76                unset($docs[$key]);
77            }
78        }
79    }
80
81    // filter unwanted namespaces
82    if(!empty($q['notns'])) {
83        $pattern = implode('|^',$q['notns']);
84        foreach($docs as $key => $val) {
85            if(preg_match('/^'.$pattern.'/',$key)) {
86                unset($docs[$key]);
87            }
88        }
89    }
90
91    // remove negative matches
92    foreach($not as $n){
93        unset($docs[$n]);
94    }
95
96    if(!count($docs)) return array();
97    // handle phrases
98    if(count($q['phrases'])){
99        $q['phrases'] = array_map('utf8_strtolower',$q['phrases']);
100        // use this for higlighting later:
101        $highlight = array_merge($highlight,$q['phrases']);
102        $q['phrases'] = array_map('preg_quote_cb',$q['phrases']);
103        // check the source of all documents for the exact phrases
104        foreach(array_keys($docs) as $id){
105            $text  = utf8_strtolower(rawWiki($id));
106            foreach($q['phrases'] as $phrase){
107                if(!preg_match('/'.$phrase.'/usi',$text)){
108                    unset($docs[$id]); // no hit - remove
109                    break;
110                }
111            }
112        }
113    }
114
115    if(!count($docs)) return array();
116
117    // check ACL permissions
118    foreach(array_keys($docs) as $doc){
119        if(auth_quickaclcheck($doc) < AUTH_READ){
120            unset($docs[$doc]);
121        }
122    }
123
124    if(!count($docs)) return array();
125
126    // if there are any hits left, sort them by count
127    arsort($docs);
128
129    return $docs;
130}
131
132/**
133 * Returns the backlinks for a given page
134 *
135 * Does a quick lookup with the fulltext index, then
136 * evaluates the instructions of the found pages
137 */
138function ft_backlinks($id){
139    global $conf;
140    $swfile   = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
141    $stopwords = @file_exists($swfile) ? file($swfile) : array();
142
143    $result = array();
144
145    // quick lookup of the pagename
146    $page    = noNS($id);
147    $matches = idx_lookup(idx_tokenizer($page,$stopwords));  // pagename may contain specials (_ or .)
148    $docs    = array_keys(ft_resultCombine(array_values($matches)));
149    $docs    = array_filter($docs,'isVisiblePage'); // discard hidden pages
150    if(!count($docs)) return $result;
151    require_once(DOKU_INC.'inc/parserutils.php');
152
153    // check metadata for matching links
154    foreach($docs as $match){
155        // metadata relation reference links are already resolved
156        $links = p_get_metadata($match,'relation references');
157        if (isset($links[$id])) $result[] = $match;
158    }
159
160    if(!count($result)) return $result;
161
162    // check ACL permissions
163    foreach(array_keys($result) as $idx){
164        if(auth_quickaclcheck($result[$idx]) < AUTH_READ){
165            unset($result[$idx]);
166        }
167    }
168
169    sort($result);
170    return $result;
171}
172
173/**
174 * Returns the pages that use a given media file
175 *
176 * Does a quick lookup with the fulltext index, then
177 * evaluates the instructions of the found pages
178 *
179 * Aborts after $max found results
180 */
181function ft_mediause($id,$max){
182    global $conf;
183    $swfile   = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
184    $stopwords = @file_exists($swfile) ? file($swfile) : array();
185
186    if(!$max) $max = 1; // need to find at least one
187
188    $result = array();
189
190    // quick lookup of the mediafile
191    $media   = noNS($id);
192    $matches = idx_lookup(idx_tokenizer($media,$stopwords));
193    $docs    = array_keys(ft_resultCombine(array_values($matches)));
194    if(!count($docs)) return $result;
195
196    // go through all found pages
197    $found = 0;
198    $pcre  = preg_quote($media,'/');
199    foreach($docs as $doc){
200        $ns = getNS($doc);
201        preg_match_all('/\{\{([^|}]*'.$pcre.'[^|}]*)(|[^}]+)?\}\}/i',rawWiki($doc),$matches);
202        foreach($matches[1] as $img){
203            $img = trim($img);
204            if(preg_match('/^https?:\/\//i',$img)) continue; // skip external images
205            list($img) = explode('?',$img);                  // remove any parameters
206            resolve_mediaid($ns,$img,$exists);               // resolve the possibly relative img
207
208            if($img == $id){                                 // we have a match
209                $result[] = $doc;
210                $found++;
211                break;
212            }
213        }
214        if($found >= $max) break;
215    }
216
217    sort($result);
218    return $result;
219}
220
221
222
223/**
224 * Quicksearch for pagenames
225 *
226 * By default it only matches the pagename and ignores the
227 * namespace. This can be changed with the second parameter
228 *
229 * refactored into ft_pageLookup(), _ft_pageLookup() and trigger_event()
230 *
231 * @author Andreas Gohr <andi@splitbrain.org>
232 */
233function ft_pageLookup($id,$pageonly=true){
234    $data = array('id' => $id, 'pageonly' => $pageonly);
235    return trigger_event('SEARCH_QUERY_PAGELOOKUP',$data,'_ft_pageLookup');
236}
237
238function _ft_pageLookup(&$data){
239    // split out original parameterrs
240    $id = $data['id'];
241    $pageonly = $data['pageonly'];
242
243    global $conf;
244    $id    = preg_quote($id,'/');
245    $pages = file($conf['indexdir'].'/page.idx');
246    if($id) $pages = array_values(preg_grep('/'.$id.'/',$pages));
247
248    $cnt = count($pages);
249    for($i=0; $i<$cnt; $i++){
250        if($pageonly){
251            if(!preg_match('/'.$id.'/',noNS($pages[$i]))){
252                unset($pages[$i]);
253                continue;
254            }
255        }
256        if(!page_exists($pages[$i])){
257            unset($pages[$i]);
258            continue;
259        }
260    }
261
262    $pages = array_filter($pages,'isVisiblePage'); // discard hidden pages
263    if(!count($pages)) return array();
264
265    // check ACL permissions
266    foreach(array_keys($pages) as $idx){
267        if(auth_quickaclcheck(trim($pages[$idx])) < AUTH_READ){
268            unset($pages[$idx]);
269        }
270    }
271
272    $pages = array_map('trim',$pages);
273    usort($pages,'ft_pagesorter');
274    return $pages;
275}
276
277/**
278 * Sort pages based on their namespace level first, then on their string
279 * values. This makes higher hierarchy pages rank higher than lower hierarchy
280 * pages.
281 */
282function ft_pagesorter($a, $b){
283    $ac = count(explode(':',$a));
284    $bc = count(explode(':',$b));
285    if($ac < $bc){
286        return -1;
287    }elseif($ac > $bc){
288        return 1;
289    }
290    return strcmp ($a,$b);
291}
292
293/**
294 * Creates a snippet extract
295 *
296 * @author Andreas Gohr <andi@splitbrain.org>
297 */
298function ft_snippet($id,$highlight){
299    $text     = rawWiki($id);
300    $match = array();
301    $snippets = array();
302    $utf8_offset = $offset = $end = 0;
303    $len = utf8_strlen($text);
304
305    // build a regexp from the phrases to highlight
306    $re = join('|',array_map('preg_quote_cb',array_filter((array) $highlight)));
307
308    for ($cnt=3; $cnt--;) {
309      if (!preg_match('#('.$re.')#iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) break;
310
311      list($str,$idx) = $match[0];
312
313      // convert $idx (a byte offset) into a utf8 character offset
314      $utf8_idx = utf8_strlen(substr($text,0,$idx));
315      $utf8_len = utf8_strlen($str);
316
317      // establish context, 100 bytes surrounding the match string
318      // first look to see if we can go 100 either side,
319      // then drop to 50 adding any excess if the other side can't go to 50,
320      $pre = min($utf8_idx-$utf8_offset,100);
321      $post = min($len-$utf8_idx-$utf8_len,100);
322
323      if ($pre>50 && $post>50) {
324        $pre = $post = 50;
325      } else if ($pre>50) {
326        $pre = min($pre,100-$post);
327      } else if ($post>50) {
328        $post = min($post, 100-$pre);
329      } else {
330        // both are less than 50, means the context is the whole string
331        // make it so and break out of this loop - there is no need for the
332        // complex snippet calculations
333        $snippets = array($text);
334        break;
335      }
336
337      // establish context start and end points, try to append to previous
338      // context if possible
339      $start = $utf8_idx - $pre;
340      $append = ($start < $end) ? $end : false;  // still the end of the previous context snippet
341      $end = $utf8_idx + $utf8_len + $post;      // now set it to the end of this context
342
343      if ($append) {
344        $snippets[count($snippets)-1] .= utf8_substr($text,$append,$end-$append);
345      } else {
346        $snippets[] = utf8_substr($text,$start,$end-$start);
347      }
348
349      // set $offset for next match attempt
350      //   substract strlen to avoid splitting a potential search success,
351      //   this is an approximation as the search pattern may match strings
352      //   of varying length and it will fail if the context snippet
353      //   boundary breaks a matching string longer than the current match
354      $utf8_offset = $utf8_idx + $post;
355      $offset = $idx + strlen(utf8_substr($text,$utf8_idx,$post));
356      $offset = utf8_correctIdx($text,$offset);
357    }
358
359    $m = "\1";
360    $snippets = preg_replace('#('.$re.')#iu',$m.'$1'.$m,$snippets);
361    $snippet = preg_replace('#'.$m.'([^'.$m.']*?)'.$m.'#iu','<strong class="search_hit">$1</strong>',hsc(join('... ',$snippets)));
362
363    return $snippet;
364}
365
366/**
367 * Combine found documents and sum up their scores
368 *
369 * This function is used to combine searched words with a logical
370 * AND. Only documents available in all arrays are returned.
371 *
372 * based upon PEAR's PHP_Compat function for array_intersect_key()
373 *
374 * @param array $args An array of page arrays
375 */
376function ft_resultCombine($args){
377    $array_count = count($args);
378    if($array_count == 1){
379        return $args[0];
380    }
381
382    $result = array();
383    if ($array_count > 1) {
384      foreach ($args[0] as $key => $value) {
385        $result[$key] = $value;
386        for ($i = 1; $i !== $array_count; $i++) {
387            if (!isset($args[$i][$key])) {
388                unset($result[$key]);
389                break;
390            }
391            $result[$key] += $args[$i][$key];
392        }
393      }
394    }
395    return $result;
396}
397
398/**
399 * Builds an array of search words from a query
400 *
401 * @todo support OR and parenthesises?
402 */
403function ft_queryParser($query){
404    global $conf;
405    $swfile   = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
406    if(@file_exists($swfile)){
407        $stopwords = file($swfile);
408    }else{
409        $stopwords = array();
410    }
411
412    $q = array();
413    $q['query']   = $query;
414    $q['ns']      = array();
415    $q['notns']   = array();
416    $q['phrases'] = array();
417    $q['words']   = array();
418    $q['and']     = array();
419    $q['not']     = array();
420
421    // handle phrase searches
422    while(preg_match('/"(.*?)"/',$query,$match)){
423        $q['phrases'][] = $match[1];
424        $q['and'] = array_merge($q['and'], idx_tokenizer($match[0],$stopwords));
425        $query = preg_replace('/"(.*?)"/','',$query,1);
426    }
427
428    $words = explode(' ',$query);
429    foreach($words as $w){
430        if($w{0} == '-'){
431            $token = idx_tokenizer($w,$stopwords,true);
432            if(count($token)) $q['not'] = array_merge($q['not'],$token);
433        } else if ($w{0} == '@') { // Namespace to search?
434            $w = substr($w,1);
435            $q['ns'] = array_merge($q['ns'],(array)$w);
436        } else if ($w{0} == '^') { // Namespace not to search?
437            $w = substr($w,1);
438            $q['notns'] = array_merge($q['notns'],(array)$w);
439        }else{
440            // asian "words" need to be searched as phrases
441            if(@preg_match_all('/(('.IDX_ASIAN.')+)/u',$w,$matches)){
442                $q['phrases'] = array_merge($q['phrases'],$matches[1]);
443
444            }
445            $token = idx_tokenizer($w,$stopwords,true);
446            if(count($token)){
447                $q['and']   = array_merge($q['and'],$token);
448                $q['words'] = array_merge($q['words'],$token);
449            }
450        }
451    }
452
453    return $q;
454}
455
456//Setup VIM: ex: et ts=4 enc=utf-8 :
457