xref: /dokuwiki/inc/fulltext.php (revision 134f4ab222dddaf47588c908d0c2b81a3da2b76b)
1f5eb7cf0SAndreas Gohr<?php
2f5eb7cf0SAndreas Gohr/**
3f5eb7cf0SAndreas Gohr * DokuWiki fulltextsearch functions using the index
4f5eb7cf0SAndreas Gohr *
5f5eb7cf0SAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6f5eb7cf0SAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
7f5eb7cf0SAndreas Gohr */
8f5eb7cf0SAndreas Gohr
9f5eb7cf0SAndreas Gohr  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
10f5eb7cf0SAndreas Gohr  require_once(DOKU_INC.'inc/indexer.php');
11f5eb7cf0SAndreas Gohr
12f5eb7cf0SAndreas Gohr
13f5eb7cf0SAndreas Gohr/**
14f5eb7cf0SAndreas Gohr * The fulltext search
15f5eb7cf0SAndreas Gohr *
16f5eb7cf0SAndreas Gohr * Returns a list of matching documents for the given query
17506fa893SAndreas Gohr *
18f5eb7cf0SAndreas Gohr */
19506fa893SAndreas Gohrfunction ft_pageSearch($query,&$poswords){
20f5eb7cf0SAndreas Gohr    $q = ft_queryParser($query);
21f5eb7cf0SAndreas Gohr
22506fa893SAndreas Gohr    // use this for higlighting later:
23506fa893SAndreas Gohr    $poswords = join(' ',$q['and']);
24506fa893SAndreas Gohr
25f5eb7cf0SAndreas Gohr    // lookup all words found in the query
26f5eb7cf0SAndreas Gohr    $words  = array_merge($q['and'],$q['not']);
27f5eb7cf0SAndreas Gohr    if(!count($words)) return array();
28f5eb7cf0SAndreas Gohr    $result = idx_lookup($words);
29f5eb7cf0SAndreas Gohr
30f5eb7cf0SAndreas Gohr    // merge search results with query
31f5eb7cf0SAndreas Gohr    foreach($q['and'] as $pos => $w){
32f5eb7cf0SAndreas Gohr        $q['and'][$pos] = $result[$w];
33f5eb7cf0SAndreas Gohr    }
34f5eb7cf0SAndreas Gohr    // create a list of unwanted docs
35f5eb7cf0SAndreas Gohr    $not = array();
36f5eb7cf0SAndreas Gohr    foreach($q['not'] as $pos => $w){
37f5eb7cf0SAndreas Gohr        $not = array_merge($not,array_keys($result[$w]));
38f5eb7cf0SAndreas Gohr    }
39f5eb7cf0SAndreas Gohr
40506fa893SAndreas Gohr    // combine and-words
41f5eb7cf0SAndreas Gohr    if(count($q['and']) > 1){
42f5eb7cf0SAndreas Gohr        $docs = ft_resultCombine($q['and']);
43f5eb7cf0SAndreas Gohr    }else{
44f5eb7cf0SAndreas Gohr        $docs = $q['and'][0];
45f5eb7cf0SAndreas Gohr    }
46f5eb7cf0SAndreas Gohr    if(!count($docs)) return array();
47f5eb7cf0SAndreas Gohr
48f5eb7cf0SAndreas Gohr    // remove negative matches
49f5eb7cf0SAndreas Gohr    foreach($not as $n){
50f5eb7cf0SAndreas Gohr        unset($docs[$n]);
51f5eb7cf0SAndreas Gohr    }
52f5eb7cf0SAndreas Gohr
53f5eb7cf0SAndreas Gohr    if(!count($docs)) return array();
54f5eb7cf0SAndreas Gohr
55f5eb7cf0SAndreas Gohr    // handle phrases
56f5eb7cf0SAndreas Gohr    if(count($q['phrases'])){
57f5eb7cf0SAndreas Gohr        //build a regexp
58f5eb7cf0SAndreas Gohr        $q['phrases'] = array_map('utf8_strtolower',$q['phrases']);
59f5eb7cf0SAndreas Gohr        $q['phrases'] = array_map('preg_quote',$q['phrases']);
60f5eb7cf0SAndreas Gohr        $regex = '('.join('|',$q['phrases']).')';
61f5eb7cf0SAndreas Gohr
62f5eb7cf0SAndreas Gohr        // check the source of all documents for the exact phrases
63f5eb7cf0SAndreas Gohr        foreach(array_keys($docs) as $id){
64f5eb7cf0SAndreas Gohr            $text  = utf8_strtolower(rawWiki($id));
65506fa893SAndreas Gohr            if(!preg_match('/'.$regex.'/usi',$text)){
66f5eb7cf0SAndreas Gohr                unset($docs[$id]); // no hit - remove
67f5eb7cf0SAndreas Gohr            }
68f5eb7cf0SAndreas Gohr        }
69f5eb7cf0SAndreas Gohr    }
70f5eb7cf0SAndreas Gohr
71f5eb7cf0SAndreas Gohr    if(!count($docs)) return array();
72f5eb7cf0SAndreas Gohr
7363773904SAndreas Gohr    // check ACL permissions
7463773904SAndreas Gohr    foreach(array_keys($docs) as $doc){
7563773904SAndreas Gohr        if(auth_quickaclcheck($doc) < AUTH_READ){
7663773904SAndreas Gohr            unset($docs[$doc]);
7763773904SAndreas Gohr        }
7863773904SAndreas Gohr    }
7963773904SAndreas Gohr
8063773904SAndreas Gohr    if(!count($docs)) return array();
8163773904SAndreas Gohr
82f5eb7cf0SAndreas Gohr    // if there are any hits left, sort them by count
83f5eb7cf0SAndreas Gohr    arsort($docs);
84f5eb7cf0SAndreas Gohr
85f5eb7cf0SAndreas Gohr    return $docs;
86f5eb7cf0SAndreas Gohr}
87f5eb7cf0SAndreas Gohr
88f5eb7cf0SAndreas Gohr/**
8954f4c056SAndreas Gohr * Returns the backlinks for a given page
9054f4c056SAndreas Gohr *
9154f4c056SAndreas Gohr * Does a quick lookup with the fulltext index, then
9254f4c056SAndreas Gohr * evaluates the instructions of the found pages
9354f4c056SAndreas Gohr */
9454f4c056SAndreas Gohrfunction ft_backlinks($id){
9554f4c056SAndreas Gohr    global $conf;
9654f4c056SAndreas Gohr    $result = array();
9754f4c056SAndreas Gohr
9854f4c056SAndreas Gohr    // quick lookup of the pagename
9954f4c056SAndreas Gohr    $page    = noNS($id);
1003cbaa9a4SAndreas Gohr    $sw      = array(); // we don't use stopwords here
1013cbaa9a4SAndreas Gohr    $matches = idx_lookup(idx_tokenizer($page,$sw));  //pagename may contain specials (_ or .)
1023cbaa9a4SAndreas Gohr    $docs = ft_resultCombine(array_values($matches));
1033cbaa9a4SAndreas Gohr    if(!count($docs)) return $result;
10454f4c056SAndreas Gohr    require_once(DOKU_INC.'inc/parserutils.php');
10554f4c056SAndreas Gohr
10654f4c056SAndreas Gohr    // check instructions for matching links
1073cbaa9a4SAndreas Gohr    foreach(array_keys($docs) as $match){
10854f4c056SAndreas Gohr        $instructions = p_cached_instructions(wikiFN($match),true);
10954f4c056SAndreas Gohr        if(is_null($instructions)) continue;
11054f4c056SAndreas Gohr
11154f4c056SAndreas Gohr        $match_ns =  getNS($match);
11254f4c056SAndreas Gohr
11354f4c056SAndreas Gohr        foreach($instructions as $ins){
11454f4c056SAndreas Gohr            if($ins[0] == 'internallink' || ($conf['camelcase'] && $ins[0] == 'camelcaselink') ){
11554f4c056SAndreas Gohr                $link = $ins[1][0];
11654f4c056SAndreas Gohr                resolve_pageid($match_ns,$link,$exists); //exists is not used
11754f4c056SAndreas Gohr                if($link == $id){
11854f4c056SAndreas Gohr                    //we have a match - finish
11954f4c056SAndreas Gohr                    $result[] = $match;
12054f4c056SAndreas Gohr                    break;
12154f4c056SAndreas Gohr                }
12254f4c056SAndreas Gohr            }
12354f4c056SAndreas Gohr        }
12454f4c056SAndreas Gohr    }
12554f4c056SAndreas Gohr
12663773904SAndreas Gohr    if(!count($result)) return $result;
12763773904SAndreas Gohr
12863773904SAndreas Gohr    // check ACL permissions
12963773904SAndreas Gohr    foreach(array_keys($result) as $idx){
13063773904SAndreas Gohr        if(auth_quickaclcheck($result[$idx]) < AUTH_READ){
13163773904SAndreas Gohr            unset($result[$idx]);
13263773904SAndreas Gohr        }
13363773904SAndreas Gohr    }
13463773904SAndreas Gohr
13554f4c056SAndreas Gohr    sort($result);
13654f4c056SAndreas Gohr    return $result;
13754f4c056SAndreas Gohr}
13854f4c056SAndreas Gohr
13954f4c056SAndreas Gohr/**
140506fa893SAndreas Gohr * Quicksearch for pagenames
141506fa893SAndreas Gohr *
142506fa893SAndreas Gohr * By default it only matches the pagename and ignores the
143506fa893SAndreas Gohr * namespace. This can be changed with the second parameter
144506fa893SAndreas Gohr *
145506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
146506fa893SAndreas Gohr */
147506fa893SAndreas Gohrfunction ft_pageLookup($id,$pageonly=true){
148506fa893SAndreas Gohr    global $conf;
149506fa893SAndreas Gohr    $id    = preg_quote($id,'/');
150506fa893SAndreas Gohr    $pages = file($conf['cachedir'].'/page.idx');
151506fa893SAndreas Gohr    $pages = array_values(preg_grep('/'.$id.'/',$pages));
152506fa893SAndreas Gohr
153506fa893SAndreas Gohr    $cnt = count($pages);
154506fa893SAndreas Gohr    for($i=0; $i<$cnt; $i++){
155506fa893SAndreas Gohr        if($pageonly){
156506fa893SAndreas Gohr            if(!preg_match('/'.$id.'/',noNS($pages[$i]))){
157506fa893SAndreas Gohr                unset($pages[$i]);
158506fa893SAndreas Gohr                continue;
159506fa893SAndreas Gohr            }
160506fa893SAndreas Gohr        }
161506fa893SAndreas Gohr        if(!@file_exists(wikiFN($pages[$i]))){
162506fa893SAndreas Gohr            unset($pages[$i]);
163506fa893SAndreas Gohr            continue;
164506fa893SAndreas Gohr        }
165506fa893SAndreas Gohr    }
16663773904SAndreas Gohr
16763773904SAndreas Gohr    if(!count($pages)) return array();
16863773904SAndreas Gohr
16963773904SAndreas Gohr    // check ACL permissions
17063773904SAndreas Gohr    foreach(array_keys($pages) as $idx){
17163773904SAndreas Gohr        if(auth_quickaclcheck($pages[$idx]) < AUTH_READ){
17263773904SAndreas Gohr            unset($pages[$idx]);
17363773904SAndreas Gohr        }
17463773904SAndreas Gohr    }
17563773904SAndreas Gohr
176506fa893SAndreas Gohr    sort($pages);
177506fa893SAndreas Gohr    return $pages;
178506fa893SAndreas Gohr}
179506fa893SAndreas Gohr
180506fa893SAndreas Gohr/**
181506fa893SAndreas Gohr * Creates a snippet extract
182506fa893SAndreas Gohr *
183506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
184506fa893SAndreas Gohr */
185506fa893SAndreas Gohrfunction ft_snippet($id,$poswords){
186506fa893SAndreas Gohr    $poswords = preg_quote($poswords,'#');
187506fa893SAndreas Gohr    $re       = '('.str_replace(' ','|',$poswords).')';
188506fa893SAndreas Gohr    $text     = rawWiki($id);
189506fa893SAndreas Gohr    //FIXME caseinsensitive matching doesn't work with UTF-8!?
190506fa893SAndreas Gohr    preg_match_all('#(.{0,50})'.$re.'(.{0,50})#iu',$text,$matches,PREG_SET_ORDER);
191506fa893SAndreas Gohr
192506fa893SAndreas Gohr    $cnt = 0;
193506fa893SAndreas Gohr    $snippet = '';
194506fa893SAndreas Gohr    foreach($matches as $match){
195506fa893SAndreas Gohr        $snippet .= '...'.htmlspecialchars($match[1]);
196506fa893SAndreas Gohr        $snippet .= '<span class="search_hit">';
197506fa893SAndreas Gohr        $snippet .= htmlspecialchars($match[2]);
198506fa893SAndreas Gohr        $snippet .= '</span>';
199506fa893SAndreas Gohr        $snippet .= htmlspecialchars($match[3]).'... ';
200506fa893SAndreas Gohr        if($cnt++ == 2) break;
201506fa893SAndreas Gohr    }
202506fa893SAndreas Gohr
203506fa893SAndreas Gohr    return $snippet;
204506fa893SAndreas Gohr}
205506fa893SAndreas Gohr
206506fa893SAndreas Gohr/**
207f5eb7cf0SAndreas Gohr * Combine found documents and sum up their scores
208f5eb7cf0SAndreas Gohr *
209f5eb7cf0SAndreas Gohr * This function is used to combine searched words with a logical
210f5eb7cf0SAndreas Gohr * AND. Only documents available in all arrays are returned.
211f5eb7cf0SAndreas Gohr *
212f5eb7cf0SAndreas Gohr * based upon PEAR's PHP_Compat function for array_intersect_key()
213f5eb7cf0SAndreas Gohr *
214f5eb7cf0SAndreas Gohr * @param array $args An array of page arrays
215f5eb7cf0SAndreas Gohr */
216f5eb7cf0SAndreas Gohrfunction ft_resultCombine($args){
217f5eb7cf0SAndreas Gohr    $array_count = count($args);
218*134f4ab2SAndreas Gohr    if($array_count == 1){
219*134f4ab2SAndreas Gohr        return $args[0];
220*134f4ab2SAndreas Gohr    }
221*134f4ab2SAndreas Gohr
222f5eb7cf0SAndreas Gohr    $result = array();
223f5eb7cf0SAndreas Gohr    foreach ($args[0] as $key1 => $value1) {
224f5eb7cf0SAndreas Gohr        for ($i = 1; $i !== $array_count; $i++) {
225f5eb7cf0SAndreas Gohr            foreach ($args[$i] as $key2 => $value2) {
226f5eb7cf0SAndreas Gohr                if ((string) $key1 === (string) $key2) {
227f5eb7cf0SAndreas Gohr                    if(!isset($result[$key1])) $result[$key1] = $value1;
228f5eb7cf0SAndreas Gohr                    $result[$key1] += $value2;
229f5eb7cf0SAndreas Gohr                }
230f5eb7cf0SAndreas Gohr            }
231f5eb7cf0SAndreas Gohr        }
232f5eb7cf0SAndreas Gohr    }
233f5eb7cf0SAndreas Gohr    return $result;
234f5eb7cf0SAndreas Gohr}
235f5eb7cf0SAndreas Gohr
236f5eb7cf0SAndreas Gohr/**
237f5eb7cf0SAndreas Gohr * Builds an array of search words from a query
238f5eb7cf0SAndreas Gohr *
239f5eb7cf0SAndreas Gohr * @todo support OR and parenthesises?
240f5eb7cf0SAndreas Gohr */
241f5eb7cf0SAndreas Gohrfunction ft_queryParser($query){
242f5eb7cf0SAndreas Gohr    global $conf;
243f5eb7cf0SAndreas Gohr    $swfile   = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
244f5eb7cf0SAndreas Gohr    if(@file_exists($swfile)){
245f5eb7cf0SAndreas Gohr        $stopwords = file($swfile);
246f5eb7cf0SAndreas Gohr    }else{
247f5eb7cf0SAndreas Gohr        $stopwords = array();
248f5eb7cf0SAndreas Gohr    }
249f5eb7cf0SAndreas Gohr
250f5eb7cf0SAndreas Gohr    $q = array();
251f5eb7cf0SAndreas Gohr    $q['query']   = $query;
252f5eb7cf0SAndreas Gohr    $q['phrases'] = array();
253f5eb7cf0SAndreas Gohr    $q['and']     = array();
254f5eb7cf0SAndreas Gohr    $q['not']     = array();
255f5eb7cf0SAndreas Gohr
256f5eb7cf0SAndreas Gohr    // handle phrase searches
257f5eb7cf0SAndreas Gohr    while(preg_match('/"(.*?)"/',$query,$match)){
258f5eb7cf0SAndreas Gohr        $q['phrases'][] = $match[0];
259f5eb7cf0SAndreas Gohr        $q['and'] = array_merge(idx_tokenizer($match[0],$stopwords));
260f5eb7cf0SAndreas Gohr        $query = preg_replace('/"(.*?)"/','',$query,1);
261f5eb7cf0SAndreas Gohr    }
262f5eb7cf0SAndreas Gohr
263f5eb7cf0SAndreas Gohr    $words = explode(' ',$query);
264f5eb7cf0SAndreas Gohr    foreach($words as $w){
265f5eb7cf0SAndreas Gohr        if($w{0} == '-'){
266f5eb7cf0SAndreas Gohr            $token = idx_tokenizer($w,$stopwords);
267f5eb7cf0SAndreas Gohr            if(count($token)) $q['not'] = array_merge($q['not'],$token);
268f5eb7cf0SAndreas Gohr        }else{
269f5eb7cf0SAndreas Gohr            $token = idx_tokenizer($w,$stopwords);
270f5eb7cf0SAndreas Gohr            if(count($token)) $q['and'] = array_merge($q['and'],$token);
271f5eb7cf0SAndreas Gohr        }
272f5eb7cf0SAndreas Gohr    }
273f5eb7cf0SAndreas Gohr
274f5eb7cf0SAndreas Gohr    return $q;
275f5eb7cf0SAndreas Gohr}
276f5eb7cf0SAndreas Gohr
277506fa893SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
278