xref: /dokuwiki/inc/fulltext.php (revision a05e297acbd41dc059369b143e2cadf281a581a1)
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
900976812SAndreas Gohr  if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/');
10f5eb7cf0SAndreas Gohr  require_once(DOKU_INC.'inc/indexer.php');
11f5eb7cf0SAndreas Gohr
12f5eb7cf0SAndreas Gohr
13f5eb7cf0SAndreas Gohr/**
14d7d7bed5SAndreas Gohr * Wrapper around preg_quote adding the default delimiter
15d7d7bed5SAndreas Gohr */
16d7d7bed5SAndreas Gohrfunction ft_preg_quote_cb($string){
17d7d7bed5SAndreas Gohr    return preg_quote($string,'/');
18d7d7bed5SAndreas Gohr}
19d7d7bed5SAndreas Gohr
20d7d7bed5SAndreas Gohr/**
21f5eb7cf0SAndreas Gohr * The fulltext search
22f5eb7cf0SAndreas Gohr *
23f5eb7cf0SAndreas Gohr * Returns a list of matching documents for the given query
24506fa893SAndreas Gohr *
25f5eb7cf0SAndreas Gohr */
2660c15d7dSAndreas Gohrfunction ft_pageSearch($query,&$regex){
27f5eb7cf0SAndreas Gohr    $q = ft_queryParser($query);
2860c15d7dSAndreas Gohr
2960c15d7dSAndreas Gohr    // remember for hilighting later
3060c15d7dSAndreas Gohr    $regex = str_replace('*','',join('|',$q['words']));
31506fa893SAndreas Gohr
32f5eb7cf0SAndreas Gohr    // lookup all words found in the query
33f5eb7cf0SAndreas Gohr    $words  = array_merge($q['and'],$q['not']);
34f5eb7cf0SAndreas Gohr    if(!count($words)) return array();
35f5eb7cf0SAndreas Gohr    $result = idx_lookup($words);
3605082375SAndreas Gohr    if(!count($result)) return array();
37f5eb7cf0SAndreas Gohr
38f5eb7cf0SAndreas Gohr    // merge search results with query
39f5eb7cf0SAndreas Gohr    foreach($q['and'] as $pos => $w){
40f5eb7cf0SAndreas Gohr        $q['and'][$pos] = $result[$w];
41f5eb7cf0SAndreas Gohr    }
42f5eb7cf0SAndreas Gohr    // create a list of unwanted docs
43f5eb7cf0SAndreas Gohr    $not = array();
44f5eb7cf0SAndreas Gohr    foreach($q['not'] as $pos => $w){
45f5eb7cf0SAndreas Gohr        $not = array_merge($not,array_keys($result[$w]));
46f5eb7cf0SAndreas Gohr    }
47f5eb7cf0SAndreas Gohr
48506fa893SAndreas Gohr    // combine and-words
49f5eb7cf0SAndreas Gohr    if(count($q['and']) > 1){
50f5eb7cf0SAndreas Gohr        $docs = ft_resultCombine($q['and']);
51f5eb7cf0SAndreas Gohr    }else{
52f5eb7cf0SAndreas Gohr        $docs = $q['and'][0];
53f5eb7cf0SAndreas Gohr    }
54f5eb7cf0SAndreas Gohr    if(!count($docs)) return array();
55f5eb7cf0SAndreas Gohr
560dc92c6fSAndreas Gohr    // create a list of hidden pages in the result
570dc92c6fSAndreas Gohr    $hidden = array();
580dc92c6fSAndreas Gohr    $hidden = array_filter(array_keys($docs),'isHiddenPage');
590dc92c6fSAndreas Gohr    $not = array_merge($not,$hidden);
600dc92c6fSAndreas Gohr
61d0ab54f6SMichael Klier chi@chimeric.de    // filter unmatched namespaces
62d0ab54f6SMichael Klier chi@chimeric.de    if(!empty($q['ns'])) {
63a219c1f0SMichael Klier chi@chimeric.de        $pattern = implode('|^',$q['ns']);
64d0ab54f6SMichael Klier chi@chimeric.de        foreach($docs as $key => $val) {
65a219c1f0SMichael Klier chi@chimeric.de            if(!preg_match('/^'.$pattern.'/',$key)) {
66d0ab54f6SMichael Klier chi@chimeric.de                unset($docs[$key]);
67d0ab54f6SMichael Klier chi@chimeric.de            }
68d0ab54f6SMichael Klier chi@chimeric.de        }
69d0ab54f6SMichael Klier chi@chimeric.de    }
70d0ab54f6SMichael Klier chi@chimeric.de
71f5eb7cf0SAndreas Gohr    // remove negative matches
72f5eb7cf0SAndreas Gohr    foreach($not as $n){
73f5eb7cf0SAndreas Gohr        unset($docs[$n]);
74f5eb7cf0SAndreas Gohr    }
75f5eb7cf0SAndreas Gohr
76f5eb7cf0SAndreas Gohr    if(!count($docs)) return array();
77f5eb7cf0SAndreas Gohr    // handle phrases
78f5eb7cf0SAndreas Gohr    if(count($q['phrases'])){
79f5eb7cf0SAndreas Gohr        //build a regexp
80f5eb7cf0SAndreas Gohr        $q['phrases'] = array_map('utf8_strtolower',$q['phrases']);
81d7d7bed5SAndreas Gohr        $q['phrases'] = array_map('ft_preg_quote_cb',$q['phrases']);
8260c15d7dSAndreas Gohr        // use this for higlighting later:
8360c15d7dSAndreas Gohr        if($regex !== '') $regex .= '|';
8460c15d7dSAndreas Gohr        $regex .= join('|',$q['phrases']);
85f5eb7cf0SAndreas Gohr        // check the source of all documents for the exact phrases
86f5eb7cf0SAndreas Gohr        foreach(array_keys($docs) as $id){
87f5eb7cf0SAndreas Gohr            $text  = utf8_strtolower(rawWiki($id));
88a21136cdSAndreas Gohr            foreach($q['phrases'] as $phrase){
89a21136cdSAndreas Gohr                if(!preg_match('/'.$phrase.'/usi',$text)){
90f5eb7cf0SAndreas Gohr                    unset($docs[$id]); // no hit - remove
91a21136cdSAndreas Gohr                    break;
92a21136cdSAndreas Gohr                }
93f5eb7cf0SAndreas Gohr            }
94f5eb7cf0SAndreas Gohr        }
95f5eb7cf0SAndreas Gohr    }
96f5eb7cf0SAndreas Gohr
97f5eb7cf0SAndreas Gohr    if(!count($docs)) return array();
98f5eb7cf0SAndreas Gohr
9963773904SAndreas Gohr    // check ACL permissions
10063773904SAndreas Gohr    foreach(array_keys($docs) as $doc){
10163773904SAndreas Gohr        if(auth_quickaclcheck($doc) < AUTH_READ){
10263773904SAndreas Gohr            unset($docs[$doc]);
10363773904SAndreas Gohr        }
10463773904SAndreas Gohr    }
10563773904SAndreas Gohr
10663773904SAndreas Gohr    if(!count($docs)) return array();
10763773904SAndreas Gohr
108f5eb7cf0SAndreas Gohr    // if there are any hits left, sort them by count
109f5eb7cf0SAndreas Gohr    arsort($docs);
110f5eb7cf0SAndreas Gohr
111f5eb7cf0SAndreas Gohr    return $docs;
112f5eb7cf0SAndreas Gohr}
113f5eb7cf0SAndreas Gohr
114f5eb7cf0SAndreas Gohr/**
11554f4c056SAndreas Gohr * Returns the backlinks for a given page
11654f4c056SAndreas Gohr *
11754f4c056SAndreas Gohr * Does a quick lookup with the fulltext index, then
11854f4c056SAndreas Gohr * evaluates the instructions of the found pages
11954f4c056SAndreas Gohr */
12054f4c056SAndreas Gohrfunction ft_backlinks($id){
12154f4c056SAndreas Gohr    global $conf;
1226b06b652Schris    $swfile   = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
1236b06b652Schris    $stopwords = @file_exists($swfile) ? file($swfile) : array();
1246b06b652Schris
12554f4c056SAndreas Gohr    $result = array();
12654f4c056SAndreas Gohr
12754f4c056SAndreas Gohr    // quick lookup of the pagename
12854f4c056SAndreas Gohr    $page    = noNS($id);
1296b06b652Schris    $matches = idx_lookup(idx_tokenizer($page,$stopwords));  // pagename may contain specials (_ or .)
1300dc92c6fSAndreas Gohr    $docs    = array_keys(ft_resultCombine(array_values($matches)));
1310dc92c6fSAndreas Gohr    $docs    = array_filter($docs,'isVisiblePage'); // discard hidden pages
1323cbaa9a4SAndreas Gohr    if(!count($docs)) return $result;
13354f4c056SAndreas Gohr    require_once(DOKU_INC.'inc/parserutils.php');
13454f4c056SAndreas Gohr
13510ffc9ddSAndreas Gohr    // check metadata for matching links
1360dc92c6fSAndreas Gohr    foreach($docs as $match){
13710ffc9ddSAndreas Gohr        // metadata relation reference links are already resolved
1386b06b652Schris        $links = p_get_metadata($match,'relation references');
1393be6e394Schris        if (isset($links[$id])) $result[] = $match;
14054f4c056SAndreas Gohr    }
14154f4c056SAndreas Gohr
14263773904SAndreas Gohr    if(!count($result)) return $result;
14363773904SAndreas Gohr
14463773904SAndreas Gohr    // check ACL permissions
14563773904SAndreas Gohr    foreach(array_keys($result) as $idx){
14663773904SAndreas Gohr        if(auth_quickaclcheck($result[$idx]) < AUTH_READ){
14763773904SAndreas Gohr            unset($result[$idx]);
14863773904SAndreas Gohr        }
14963773904SAndreas Gohr    }
15063773904SAndreas Gohr
15154f4c056SAndreas Gohr    sort($result);
15254f4c056SAndreas Gohr    return $result;
15354f4c056SAndreas Gohr}
15454f4c056SAndreas Gohr
15554f4c056SAndreas Gohr/**
156*a05e297aSAndreas Gohr * Returns the pages that use a given media file
157*a05e297aSAndreas Gohr *
158*a05e297aSAndreas Gohr * Does a quick lookup with the fulltext index, then
159*a05e297aSAndreas Gohr * evaluates the instructions of the found pages
160*a05e297aSAndreas Gohr *
161*a05e297aSAndreas Gohr * Aborts after $max found results
162*a05e297aSAndreas Gohr */
163*a05e297aSAndreas Gohrfunction ft_mediause($id,$max){
164*a05e297aSAndreas Gohr    global $conf;
165*a05e297aSAndreas Gohr    $swfile   = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
166*a05e297aSAndreas Gohr    $stopwords = @file_exists($swfile) ? file($swfile) : array();
167*a05e297aSAndreas Gohr
168*a05e297aSAndreas Gohr    if(!$max) $max = 1; // need to find at least one
169*a05e297aSAndreas Gohr
170*a05e297aSAndreas Gohr    $result = array();
171*a05e297aSAndreas Gohr
172*a05e297aSAndreas Gohr    // quick lookup of the mediafile
173*a05e297aSAndreas Gohr    $media   = noNS($id);
174*a05e297aSAndreas Gohr    $matches = idx_lookup(idx_tokenizer($media,$stopwords));
175*a05e297aSAndreas Gohr    $docs    = array_keys(ft_resultCombine(array_values($matches)));
176*a05e297aSAndreas Gohr    if(!count($docs)) return $result;
177*a05e297aSAndreas Gohr
178*a05e297aSAndreas Gohr    // go through all found pages
179*a05e297aSAndreas Gohr    $found = 0;
180*a05e297aSAndreas Gohr    $pcre  = preg_quote($media,'/');
181*a05e297aSAndreas Gohr    foreach($docs as $doc){
182*a05e297aSAndreas Gohr        $ns = getNS($doc);
183*a05e297aSAndreas Gohr        preg_match_all('/\{\{([^|}]*'.$pcre.'[^|}]*)(|[^}]+)?\}\}/i',rawWiki($doc),$matches);
184*a05e297aSAndreas Gohr        foreach($matches[1] as $img){
185*a05e297aSAndreas Gohr            $img = trim($img);
186*a05e297aSAndreas Gohr            if(preg_match('/^https?:\/\//i',$img)) continue; // skip external images
187*a05e297aSAndreas Gohr            list($img) = explode('?',$img);                  // remove any parameters
188*a05e297aSAndreas Gohr            resolve_mediaid($ns,$img,$exists);               // resolve the possibly relative img
189*a05e297aSAndreas Gohr
190*a05e297aSAndreas Gohr            if($img == $id){                                 // we have a match
191*a05e297aSAndreas Gohr                $result[] = $doc;
192*a05e297aSAndreas Gohr                $found++;
193*a05e297aSAndreas Gohr                break;
194*a05e297aSAndreas Gohr            }
195*a05e297aSAndreas Gohr        }
196*a05e297aSAndreas Gohr        if($found >= $max) break;
197*a05e297aSAndreas Gohr    }
198*a05e297aSAndreas Gohr
199*a05e297aSAndreas Gohr    sort($result);
200*a05e297aSAndreas Gohr    return $result;
201*a05e297aSAndreas Gohr}
202*a05e297aSAndreas Gohr
203*a05e297aSAndreas Gohr
204*a05e297aSAndreas Gohr
205*a05e297aSAndreas Gohr/**
206506fa893SAndreas Gohr * Quicksearch for pagenames
207506fa893SAndreas Gohr *
208506fa893SAndreas Gohr * By default it only matches the pagename and ignores the
209506fa893SAndreas Gohr * namespace. This can be changed with the second parameter
210506fa893SAndreas Gohr *
211506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
212506fa893SAndreas Gohr */
213506fa893SAndreas Gohrfunction ft_pageLookup($id,$pageonly=true){
214506fa893SAndreas Gohr    global $conf;
215506fa893SAndreas Gohr    $id    = preg_quote($id,'/');
216579b0f7eSTNHarris    $pages = file($conf['indexdir'].'/page.idx');
2176798a86aSAndreas Gohr    if($id) $pages = array_values(preg_grep('/'.$id.'/',$pages));
218506fa893SAndreas Gohr
219506fa893SAndreas Gohr    $cnt = count($pages);
220506fa893SAndreas Gohr    for($i=0; $i<$cnt; $i++){
221506fa893SAndreas Gohr        if($pageonly){
222506fa893SAndreas Gohr            if(!preg_match('/'.$id.'/',noNS($pages[$i]))){
223506fa893SAndreas Gohr                unset($pages[$i]);
224506fa893SAndreas Gohr                continue;
225506fa893SAndreas Gohr            }
226506fa893SAndreas Gohr        }
227103c256aSChris Smith        if(!page_exists($pages[$i])){
228506fa893SAndreas Gohr            unset($pages[$i]);
229506fa893SAndreas Gohr            continue;
230506fa893SAndreas Gohr        }
231506fa893SAndreas Gohr    }
23263773904SAndreas Gohr
2330dc92c6fSAndreas Gohr    $pages = array_filter($pages,'isVisiblePage'); // discard hidden pages
23463773904SAndreas Gohr    if(!count($pages)) return array();
23563773904SAndreas Gohr
23663773904SAndreas Gohr    // check ACL permissions
23763773904SAndreas Gohr    foreach(array_keys($pages) as $idx){
23863773904SAndreas Gohr        if(auth_quickaclcheck($pages[$idx]) < AUTH_READ){
23963773904SAndreas Gohr            unset($pages[$idx]);
24063773904SAndreas Gohr        }
24163773904SAndreas Gohr    }
24263773904SAndreas Gohr
2436798a86aSAndreas Gohr    $pages = array_map('trim',$pages);
244506fa893SAndreas Gohr    sort($pages);
245506fa893SAndreas Gohr    return $pages;
246506fa893SAndreas Gohr}
247506fa893SAndreas Gohr
248506fa893SAndreas Gohr/**
249506fa893SAndreas Gohr * Creates a snippet extract
250506fa893SAndreas Gohr *
251506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
252506fa893SAndreas Gohr */
25360c15d7dSAndreas Gohrfunction ft_snippet($id,$re){
254506fa893SAndreas Gohr    $text     = rawWiki($id);
255ced0762eSchris    $match = array();
256ced0762eSchris    $snippets = array();
2579ee93076Schris    $utf8_offset = $offset = $end = 0;
258ced0762eSchris    $len = utf8_strlen($text);
2599ee93076Schris
260ced0762eSchris    for ($cnt=3; $cnt--;) {
26160c15d7dSAndreas Gohr      if (!preg_match('#('.$re.')#iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) break;
262ced0762eSchris
263ced0762eSchris      list($str,$idx) = $match[0];
264ced0762eSchris
265ced0762eSchris      // convert $idx (a byte offset) into a utf8 character offset
266ced0762eSchris      $utf8_idx = utf8_strlen(substr($text,0,$idx));
267ced0762eSchris      $utf8_len = utf8_strlen($str);
268ced0762eSchris
269ced0762eSchris      // establish context, 100 bytes surrounding the match string
270ced0762eSchris      // first look to see if we can go 100 either side,
271ced0762eSchris      // then drop to 50 adding any excess if the other side can't go to 50,
272ced0762eSchris      $pre = min($utf8_idx-$utf8_offset,100);
273ced0762eSchris      $post = min($len-$utf8_idx-$utf8_len,100);
274ced0762eSchris
275ced0762eSchris      if ($pre>50 && $post>50) {
276ced0762eSchris        $pre = $post = 50;
277ced0762eSchris      } else if ($pre>50) {
278ced0762eSchris        $pre = min($pre,100-$post);
279ced0762eSchris      } else if ($post>50) {
280ced0762eSchris        $post = min($post, 100-$pre);
281ced0762eSchris      } else {
282ced0762eSchris        // both are less than 50, means the context is the whole string
28310ffc9ddSAndreas Gohr        // make it so and break out of this loop - there is no need for the
28410ffc9ddSAndreas Gohr        // complex snippet calculations
285ced0762eSchris        $snippets = array($text);
286ced0762eSchris        break;
287ced0762eSchris      }
288ced0762eSchris
28910ffc9ddSAndreas Gohr      // establish context start and end points, try to append to previous
29010ffc9ddSAndreas Gohr      // context if possible
2919ee93076Schris      $start = $utf8_idx - $pre;
292ced0762eSchris      $append = ($start < $end) ? $end : false;  // still the end of the previous context snippet
2939ee93076Schris      $end = $utf8_idx + $utf8_len + $post;      // now set it to the end of this context
294ced0762eSchris
295ced0762eSchris      if ($append) {
296ced0762eSchris        $snippets[count($snippets)-1] .= utf8_substr($text,$append,$end-$append);
297ced0762eSchris      } else {
298ced0762eSchris        $snippets[] = utf8_substr($text,$start,$end-$start);
299ced0762eSchris      }
300ced0762eSchris
301ced0762eSchris      // set $offset for next match attempt
30210ffc9ddSAndreas Gohr      //   substract strlen to avoid splitting a potential search success,
30310ffc9ddSAndreas Gohr      //   this is an approximation as the search pattern may match strings
30410ffc9ddSAndreas Gohr      //   of varying length and it will fail if the context snippet
305ced0762eSchris      //   boundary breaks a matching string longer than the current match
3069ee93076Schris      $utf8_offset = $utf8_idx + $post;
3079ee93076Schris      $offset = $idx + strlen(utf8_substr($text,$utf8_idx,$post));
3089ee93076Schris      $offset = utf8_correctIdx($text,$offset);
3099ee93076Schris    }
3109ee93076Schris
311ced0762eSchris    $m = "\1";
31260c15d7dSAndreas Gohr    $snippets = preg_replace('#('.$re.')#iu',$m.'$1'.$m,$snippets);
313ed7ecb79SAnika Henke    $snippet = preg_replace('#'.$m.'([^'.$m.']*?)'.$m.'#iu','<strong class="search_hit">$1</strong>',hsc(join('... ',$snippets)));
314bd2cb6fcSchris
3155953e889Schris    return $snippet;
316506fa893SAndreas Gohr}
317506fa893SAndreas Gohr
318506fa893SAndreas Gohr/**
319f5eb7cf0SAndreas Gohr * Combine found documents and sum up their scores
320f5eb7cf0SAndreas Gohr *
321f5eb7cf0SAndreas Gohr * This function is used to combine searched words with a logical
322f5eb7cf0SAndreas Gohr * AND. Only documents available in all arrays are returned.
323f5eb7cf0SAndreas Gohr *
324f5eb7cf0SAndreas Gohr * based upon PEAR's PHP_Compat function for array_intersect_key()
325f5eb7cf0SAndreas Gohr *
326f5eb7cf0SAndreas Gohr * @param array $args An array of page arrays
327f5eb7cf0SAndreas Gohr */
328f5eb7cf0SAndreas Gohrfunction ft_resultCombine($args){
329f5eb7cf0SAndreas Gohr    $array_count = count($args);
330134f4ab2SAndreas Gohr    if($array_count == 1){
331134f4ab2SAndreas Gohr        return $args[0];
332134f4ab2SAndreas Gohr    }
333134f4ab2SAndreas Gohr
334f5eb7cf0SAndreas Gohr    $result = array();
33509c27a6dSGuy Brand    if ($array_count > 1) {
336a21136cdSAndreas Gohr      foreach ($args[0] as $key => $value) {
337a21136cdSAndreas Gohr        $result[$key] = $value;
338f5eb7cf0SAndreas Gohr        for ($i = 1; $i !== $array_count; $i++) {
339a21136cdSAndreas Gohr            if (!isset($args[$i][$key])) {
340a21136cdSAndreas Gohr                unset($result[$key]);
341a21136cdSAndreas Gohr                break;
342f5eb7cf0SAndreas Gohr            }
343a21136cdSAndreas Gohr            $result[$key] += $args[$i][$key];
344f5eb7cf0SAndreas Gohr        }
345f5eb7cf0SAndreas Gohr      }
34609c27a6dSGuy Brand    }
347f5eb7cf0SAndreas Gohr    return $result;
348f5eb7cf0SAndreas Gohr}
349f5eb7cf0SAndreas Gohr
350f5eb7cf0SAndreas Gohr/**
351f5eb7cf0SAndreas Gohr * Builds an array of search words from a query
352f5eb7cf0SAndreas Gohr *
353f5eb7cf0SAndreas Gohr * @todo support OR and parenthesises?
354f5eb7cf0SAndreas Gohr */
355f5eb7cf0SAndreas Gohrfunction ft_queryParser($query){
356f5eb7cf0SAndreas Gohr    global $conf;
357f5eb7cf0SAndreas Gohr    $swfile   = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
358f5eb7cf0SAndreas Gohr    if(@file_exists($swfile)){
359f5eb7cf0SAndreas Gohr        $stopwords = file($swfile);
360f5eb7cf0SAndreas Gohr    }else{
361f5eb7cf0SAndreas Gohr        $stopwords = array();
362f5eb7cf0SAndreas Gohr    }
363f5eb7cf0SAndreas Gohr
364f5eb7cf0SAndreas Gohr    $q = array();
365f5eb7cf0SAndreas Gohr    $q['query']   = $query;
366a219c1f0SMichael Klier chi@chimeric.de    $q['ns']      = array();
367f5eb7cf0SAndreas Gohr    $q['phrases'] = array();
36860c15d7dSAndreas Gohr    $q['words']   = array();
369f5eb7cf0SAndreas Gohr    $q['and']     = array();
370f5eb7cf0SAndreas Gohr    $q['not']     = array();
371f5eb7cf0SAndreas Gohr
372d0ab54f6SMichael Klier chi@chimeric.de    // strip namespace from query
373a219c1f0SMichael Klier chi@chimeric.de    if(preg_match('/([^@]*)@(.*)/',$query,$match))  {
374d0ab54f6SMichael Klier chi@chimeric.de        $query = $match[1];
375a219c1f0SMichael Klier chi@chimeric.de        $q['ns'] = explode('@',preg_replace("/ /",'',$match[2]));
376d0ab54f6SMichael Klier chi@chimeric.de    }
377d0ab54f6SMichael Klier chi@chimeric.de
378f5eb7cf0SAndreas Gohr    // handle phrase searches
379f5eb7cf0SAndreas Gohr    while(preg_match('/"(.*?)"/',$query,$match)){
38093a60ad2SAndreas Gohr        $q['phrases'][] = $match[1];
381235cf363SAndreas Gohr        $q['and'] = array_merge($q['and'], idx_tokenizer($match[0],$stopwords));
382f5eb7cf0SAndreas Gohr        $query = preg_replace('/"(.*?)"/','',$query,1);
383f5eb7cf0SAndreas Gohr    }
384f5eb7cf0SAndreas Gohr
385f5eb7cf0SAndreas Gohr    $words = explode(' ',$query);
386f5eb7cf0SAndreas Gohr    foreach($words as $w){
387f5eb7cf0SAndreas Gohr        if($w{0} == '-'){
388ad81d431SAndreas Gohr            $token = idx_tokenizer($w,$stopwords,true);
389f5eb7cf0SAndreas Gohr            if(count($token)) $q['not'] = array_merge($q['not'],$token);
390f5eb7cf0SAndreas Gohr        }else{
39193a60ad2SAndreas Gohr            // asian "words" need to be searched as phrases
39260c15d7dSAndreas Gohr            if(@preg_match_all('/(('.IDX_ASIAN.')+)/u',$w,$matches)){
39393a60ad2SAndreas Gohr                $q['phrases'] = array_merge($q['phrases'],$matches[1]);
39493a60ad2SAndreas Gohr
39593a60ad2SAndreas Gohr            }
396ad81d431SAndreas Gohr            $token = idx_tokenizer($w,$stopwords,true);
39760c15d7dSAndreas Gohr            if(count($token)){
39860c15d7dSAndreas Gohr                $q['and']   = array_merge($q['and'],$token);
39960c15d7dSAndreas Gohr                $q['words'] = array_merge($q['words'],$token);
40060c15d7dSAndreas Gohr            }
401f5eb7cf0SAndreas Gohr        }
402f5eb7cf0SAndreas Gohr    }
403f5eb7cf0SAndreas Gohr
404f5eb7cf0SAndreas Gohr    return $q;
405f5eb7cf0SAndreas Gohr}
406f5eb7cf0SAndreas Gohr
407506fa893SAndreas Gohr//Setup VIM: ex: et ts=4 enc=utf-8 :
408