xref: /dokuwiki/inc/fulltext.php (revision 1f4a73412e359187bcd0ad71ffe2e6298738ab10)
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}
28
29/**
30 * Returns a list of matching documents for the given query
31 *
32 * @author Andreas Gohr <andi@splitbrain.org>
33 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
34 */
35function _ft_pageSearch(&$data) {
36    // parse the given query
37    $q = ft_queryParser($data['query']);
38    $data['highlight'] = $q['highlight'];
39
40    if (empty($q['parsed_ary'])) return array();
41
42    // lookup all words found in the query
43    $lookup = idx_lookup($q['words']);
44
45    // get all pages in this dokuwiki site (!: includes nonexistent pages)
46    $pages_all = array();
47    foreach (idx_getIndex('page', '') as $id) {
48        $pages_all[trim($id)] = 0; // base: 0 hit
49    }
50
51    // process the query
52    $stack = array();
53    foreach ($q['parsed_ary'] as $token) {
54        switch (substr($token, 0, 3)) {
55            case 'W+:':
56            case 'W-:': // word
57                $word    = substr($token, 3);
58                $stack[] = (array) $lookup[$word];
59                break;
60            case 'P_:': // phrase
61                $phrase = substr($token, 3);
62                // since phrases are always parsed as ((W1)(W2)...(P)),
63                // the end($stack) always points the pages that contain
64                // all words in this phrase
65                $pages  = end($stack);
66                $pages_matched = array();
67                foreach(array_keys($pages) as $id){
68                    $text = utf8_strtolower(rawWiki($id));
69                    if (strpos($text, $phrase) !== false) {
70                        $pages_matched[$id] = 0; // phrase: always 0 hit
71                    }
72                }
73                $stack[] = $pages_matched;
74                break;
75            case 'N_:': // namespace
76                $ns = substr($token, 3);
77                $pages_matched = array();
78                foreach (array_keys($pages_all) as $id) {
79                    if (strpos($id, $ns) === 0) {
80                        $pages_matched[$id] = 0; // namespace: always 0 hit
81                    }
82                }
83                $stack[] = $pages_matched;
84                break;
85            case 'AND': // and operation
86                list($pages1, $pages2) = array_splice($stack, -2);
87                $stack[] = ft_resultCombine(array($pages1, $pages2));
88                break;
89            case 'OR':  // or operation
90                list($pages1, $pages2) = array_splice($stack, -2);
91                $stack[] = ft_resultUnite(array($pages1, $pages2));
92                break;
93            case 'NOT': // not operation (unary)
94                $pages   = array_pop($stack);
95                $stack[] = ft_resultComplement(array($pages_all, $pages));
96                break;
97        }
98    }
99    $docs = array_pop($stack);
100
101    if (empty($docs)) return array();
102
103    // check: settings, acls, existence
104    foreach (array_keys($docs) as $id) {
105        if (isHiddenPage($id) || auth_quickaclcheck($id) < AUTH_READ || !page_exists($id, '', false)) {
106            unset($docs[$id]);
107        }
108    }
109
110    // sort docs by count
111    arsort($docs);
112
113    return $docs;
114}
115
116/**
117 * Returns the backlinks for a given page
118 *
119 * Does a quick lookup with the fulltext index, then
120 * evaluates the instructions of the found pages
121 */
122function ft_backlinks($id){
123    global $conf;
124    $swfile   = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
125    $stopwords = @file_exists($swfile) ? file($swfile) : array();
126
127    $result = array();
128
129    // quick lookup of the pagename
130    $page    = noNS($id);
131    $matches = idx_lookup(idx_tokenizer($page,$stopwords));  // pagename may contain specials (_ or .)
132    $docs    = array_keys(ft_resultCombine(array_values($matches)));
133    $docs    = array_filter($docs,'isVisiblePage'); // discard hidden pages
134    if(!count($docs)) return $result;
135    require_once(DOKU_INC.'inc/parserutils.php');
136
137    // check metadata for matching links
138    foreach($docs as $match){
139        // metadata relation reference links are already resolved
140        $links = p_get_metadata($match,'relation references');
141        if (isset($links[$id])) $result[] = $match;
142    }
143
144    if(!count($result)) return $result;
145
146    // check ACL permissions
147    foreach(array_keys($result) as $idx){
148        if(auth_quickaclcheck($result[$idx]) < AUTH_READ){
149            unset($result[$idx]);
150        }
151    }
152
153    sort($result);
154    return $result;
155}
156
157/**
158 * Returns the pages that use a given media file
159 *
160 * Does a quick lookup with the fulltext index, then
161 * evaluates the instructions of the found pages
162 *
163 * Aborts after $max found results
164 */
165function ft_mediause($id,$max){
166    global $conf;
167    $swfile   = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
168    $stopwords = @file_exists($swfile) ? file($swfile) : array();
169
170    if(!$max) $max = 1; // need to find at least one
171
172    $result = array();
173
174    // quick lookup of the mediafile
175    $media   = noNS($id);
176    $matches = idx_lookup(idx_tokenizer($media,$stopwords));
177    $docs    = array_keys(ft_resultCombine(array_values($matches)));
178    if(!count($docs)) return $result;
179
180    // go through all found pages
181    $found = 0;
182    $pcre  = preg_quote($media,'/');
183    foreach($docs as $doc){
184        $ns = getNS($doc);
185        preg_match_all('/\{\{([^|}]*'.$pcre.'[^|}]*)(|[^}]+)?\}\}/i',rawWiki($doc),$matches);
186        foreach($matches[1] as $img){
187            $img = trim($img);
188            if(preg_match('/^https?:\/\//i',$img)) continue; // skip external images
189            list($img) = explode('?',$img);                  // remove any parameters
190            resolve_mediaid($ns,$img,$exists);               // resolve the possibly relative img
191
192            if($img == $id){                                 // we have a match
193                $result[] = $doc;
194                $found++;
195                break;
196            }
197        }
198        if($found >= $max) break;
199    }
200
201    sort($result);
202    return $result;
203}
204
205
206
207/**
208 * Quicksearch for pagenames
209 *
210 * By default it only matches the pagename and ignores the
211 * namespace. This can be changed with the second parameter
212 *
213 * refactored into ft_pageLookup(), _ft_pageLookup() and trigger_event()
214 *
215 * @author Andreas Gohr <andi@splitbrain.org>
216 */
217function ft_pageLookup($id,$pageonly=true){
218    $data = array('id' => $id, 'pageonly' => $pageonly);
219    return trigger_event('SEARCH_QUERY_PAGELOOKUP',$data,'_ft_pageLookup');
220}
221
222function _ft_pageLookup(&$data){
223    // split out original parameterrs
224    $id = $data['id'];
225    $pageonly = $data['pageonly'];
226
227    global $conf;
228    $id    = preg_quote($id,'/');
229    $pages = file($conf['indexdir'].'/page.idx');
230    if($id) $pages = array_values(preg_grep('/'.$id.'/',$pages));
231
232    $cnt = count($pages);
233    for($i=0; $i<$cnt; $i++){
234        if($pageonly){
235            if(!preg_match('/'.$id.'/',noNS($pages[$i]))){
236                unset($pages[$i]);
237                continue;
238            }
239        }
240        if(!page_exists($pages[$i])){
241            unset($pages[$i]);
242            continue;
243        }
244    }
245
246    $pages = array_filter($pages,'isVisiblePage'); // discard hidden pages
247    if(!count($pages)) return array();
248
249    // check ACL permissions
250    foreach(array_keys($pages) as $idx){
251        if(auth_quickaclcheck(trim($pages[$idx])) < AUTH_READ){
252            unset($pages[$idx]);
253        }
254    }
255
256    $pages = array_map('trim',$pages);
257    usort($pages,'ft_pagesorter');
258    return $pages;
259}
260
261/**
262 * Sort pages based on their namespace level first, then on their string
263 * values. This makes higher hierarchy pages rank higher than lower hierarchy
264 * pages.
265 */
266function ft_pagesorter($a, $b){
267    $ac = count(explode(':',$a));
268    $bc = count(explode(':',$b));
269    if($ac < $bc){
270        return -1;
271    }elseif($ac > $bc){
272        return 1;
273    }
274    return strcmp ($a,$b);
275}
276
277/**
278 * Creates a snippet extract
279 *
280 * @author Andreas Gohr <andi@splitbrain.org>
281 * @triggers FULLTEXT_SNIPPET_CREATE
282 */
283function ft_snippet($id,$highlight){
284    $text = rawWiki($id);
285    $evdata = array(
286                'id'        => $id,
287                'text'      => &$text,
288                'highlight' => &$highlight,
289                'snippet'   => '',
290              );
291
292    $evt = new Doku_Event('FULLTEXT_SNIPPET_CREATE',$evdata);
293    if ($evt->advise_before()) {
294        $match = array();
295        $snippets = array();
296        $utf8_offset = $offset = $end = 0;
297        $len = utf8_strlen($text);
298
299        // build a regexp from the phrases to highlight
300        $re1 = '('.join('|',array_map('preg_quote_cb',array_filter((array) $highlight))).')';
301        $re2 = "$re1.{0,75}(?!\\1)$re1";
302        $re3 = "$re1.{0,45}(?!\\1)$re1.{0,45}(?!\\1)(?!\\2)$re1";
303
304        for ($cnt=4; $cnt--;) {
305          if (0) {
306          } else if (preg_match('/'.$re3.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) {
307          } else if (preg_match('/'.$re2.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) {
308          } else if (preg_match('/'.$re1.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) {
309          } else {
310            break;
311          }
312
313          list($str,$idx) = $match[0];
314
315          // convert $idx (a byte offset) into a utf8 character offset
316          $utf8_idx = utf8_strlen(substr($text,0,$idx));
317          $utf8_len = utf8_strlen($str);
318
319          // establish context, 100 bytes surrounding the match string
320          // first look to see if we can go 100 either side,
321          // then drop to 50 adding any excess if the other side can't go to 50,
322          $pre = min($utf8_idx-$utf8_offset,100);
323          $post = min($len-$utf8_idx-$utf8_len,100);
324
325          if ($pre>50 && $post>50) {
326            $pre = $post = 50;
327          } else if ($pre>50) {
328            $pre = min($pre,100-$post);
329          } else if ($post>50) {
330            $post = min($post, 100-$pre);
331          } else {
332            // both are less than 50, means the context is the whole string
333            // make it so and break out of this loop - there is no need for the
334            // complex snippet calculations
335            $snippets = array($text);
336            break;
337          }
338
339          // establish context start and end points, try to append to previous
340          // context if possible
341          $start = $utf8_idx - $pre;
342          $append = ($start < $end) ? $end : false;  // still the end of the previous context snippet
343          $end = $utf8_idx + $utf8_len + $post;      // now set it to the end of this context
344
345          if ($append) {
346            $snippets[count($snippets)-1] .= utf8_substr($text,$append,$end-$append);
347          } else {
348            $snippets[] = utf8_substr($text,$start,$end-$start);
349          }
350
351          // set $offset for next match attempt
352          //   substract strlen to avoid splitting a potential search success,
353          //   this is an approximation as the search pattern may match strings
354          //   of varying length and it will fail if the context snippet
355          //   boundary breaks a matching string longer than the current match
356          $utf8_offset = $utf8_idx + $post;
357          $offset = $idx + strlen(utf8_substr($text,$utf8_idx,$post));
358          $offset = utf8_correctIdx($text,$offset);
359        }
360
361        $m = "\1";
362        $snippets = preg_replace('/'.$re1.'/iu',$m.'$1'.$m,$snippets);
363        $snippet = preg_replace('/'.$m.'([^'.$m.']*?)'.$m.'/iu','<strong class="search_hit">$1</strong>',hsc(join('... ',$snippets)));
364
365        $evdata['snippet'] = $snippet;
366    }
367    $evt->advise_after();
368    unset($evt);
369
370    return $evdata['snippet'];
371}
372
373/**
374 * Combine found documents and sum up their scores
375 *
376 * This function is used to combine searched words with a logical
377 * AND. Only documents available in all arrays are returned.
378 *
379 * based upon PEAR's PHP_Compat function for array_intersect_key()
380 *
381 * @param array $args An array of page arrays
382 */
383function ft_resultCombine($args){
384    $array_count = count($args);
385    if($array_count == 1){
386        return $args[0];
387    }
388
389    $result = array();
390    if ($array_count > 1) {
391      foreach ($args[0] as $key => $value) {
392        $result[$key] = $value;
393        for ($i = 1; $i !== $array_count; $i++) {
394            if (!isset($args[$i][$key])) {
395                unset($result[$key]);
396                break;
397            }
398            $result[$key] += $args[$i][$key];
399        }
400      }
401    }
402    return $result;
403}
404
405/**
406 * Unites found documents and sum up their scores
407 *
408 * based upon ft_resultCombine() function
409 *
410 * @param array $args An array of page arrays
411 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
412 */
413function ft_resultUnite($args) {
414    $array_count = count($args);
415    if ($array_count === 1) {
416        return $args[0];
417    }
418
419    $result = $args[0];
420    for ($i = 1; $i !== $array_count; $i++) {
421        foreach (array_keys($args[$i]) as $id) {
422            $result[$id] += $args[$i][$id];
423        }
424    }
425    return $result;
426}
427
428/**
429 * Computes the difference of documents using page id for comparison
430 *
431 * nearly identical to PHP5's array_diff_key()
432 *
433 * @param array $args An array of page arrays
434 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
435 */
436function ft_resultComplement($args) {
437    $array_count = count($args);
438    if ($array_count === 1) {
439        return $args[0];
440    }
441
442    $result = $args[0];
443    foreach (array_keys($result) as $id) {
444        for ($i = 1; $i !== $array_count; $i++) {
445            if (isset($args[$i][$id])) unset($result[$id]);
446        }
447    }
448    return $result;
449}
450
451/**
452 * Parses a search query and builds an array of search formulas
453 *
454 * @author Andreas Gohr <andi@splitbrain.org>
455 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
456 */
457function ft_queryParser($query){
458    global $conf;
459    $swfile    = DOKU_INC.'inc/lang/'.$conf['lang'].'/stopwords.txt';
460    $stopwords = @file_exists($swfile) ? file($swfile) : array();
461
462    /**
463     * parse a search query and transform it into intermediate representation
464     *
465     * in a search query, you can use the following expressions:
466     *
467     *   words:
468     *     include
469     *     -exclude
470     *   phrases:
471     *     "phrase to be included"
472     *     -"phrase you want to exclude"
473     *   namespaces:
474     *     @include:namespace (or ns:include:namespace)
475     *     ^exclude:namespace (or -ns:exclude:namespace)
476     *   groups:
477     *     ()
478     *     -()
479     *   operators:
480     *     and ('and' is the default operator: you can always omit this)
481     *     or  (or pipe symbol '|', lower precedence than 'and')
482     *
483     * e.g. a query [ aa "bb cc" @dd:ee ] means "search pages which contain
484     *      a word 'aa', a phrase 'bb cc' and are within a namespace 'dd:ee'".
485     *      this query is equivalent to [ -(-aa or -"bb cc" or -ns:dd:ee) ]
486     *      as long as you don't mind hit counts.
487     *
488     * intermediate representation consists of the following parts:
489     *
490     *   ( ) - group
491     *   AND - logical and
492     *   OR  - logical or
493     *   NOT - logical not
494     *   W+: - word (needs to be highlighted)
495     *   W-: - word (no need to highlight)
496     *   P_: - phrase
497     *   N_: - namespace
498     */
499    $parsed_query = '';
500    $parens_level = 0;
501    $terms = preg_split('/(-?".*?")/u', utf8_strtolower($query), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
502
503    foreach ($terms as $term) {
504        $parsed = '';
505        if (preg_match('/^(-?)"(.+)"$/u', $term, $matches)) {
506            // phrase-include and phrase-exclude
507            $not = $matches[1] ? 'NOT' : '';
508            $parsed = $not.ft_termParser($matches[2], $stopwords, false, true);
509        } else {
510            // fix incomplete phrase
511            $term = str_replace('"', ' ', $term);
512
513            // fix parentheses
514            $term = str_replace(')'  , ' ) ', $term);
515            $term = str_replace('('  , ' ( ', $term);
516            $term = str_replace('- (', ' -(', $term);
517
518            // treat pipe symbols as 'OR' operators
519            $term = str_replace('|', ' or ', $term);
520
521            // treat ideographic spaces (U+3000) as search term separators
522            // FIXME: some more separators?
523            $term = preg_replace('/[ \x{3000}]+/u', ' ',  $term);
524            $term = trim($term);
525            if ($term === '') continue;
526
527            $tokens = explode(' ', $term);
528            foreach ($tokens as $token) {
529                if ($token === '(') {
530                    // parenthesis-include-open
531                    $parsed .= '(';
532                    ++$parens_level;
533                } elseif ($token === '-(') {
534                    // parenthesis-exclude-open
535                    $parsed .= 'NOT(';
536                    ++$parens_level;
537                } elseif ($token === ')') {
538                    // parenthesis-any-close
539                    if ($parens_level === 0) continue;
540                    $parsed .= ')';
541                    $parens_level--;
542                } elseif ($token === 'and') {
543                    // logical-and (do nothing)
544                } elseif ($token === 'or') {
545                    // logical-or
546                    $parsed .= 'OR';
547                } elseif (preg_match('/^(?:\^|-ns:)(.+)$/u', $token, $matches)) {
548                    // namespace-exclude
549                    $parsed .= 'NOT(N_:'.$matches[1].')';
550                } elseif (preg_match('/^(?:@|ns:)(.+)$/u', $token, $matches)) {
551                    // namespace-include
552                    $parsed .= '(N_:'.$matches[1].')';
553                } elseif (preg_match('/^-(.+)$/', $token, $matches)) {
554                    // word-exclude
555                    $parsed .= 'NOT('.ft_termParser($matches[1], $stopwords).')';
556                } else {
557                    // word-include
558                    $parsed .= ft_termParser($token, $stopwords);
559                }
560            }
561        }
562        $parsed_query .= $parsed;
563    }
564
565    // cleanup (very sensitive)
566    $parsed_query .= str_repeat(')', $parens_level);
567    do {
568        $parsed_query_old = $parsed_query;
569        $parsed_query = preg_replace('/(NOT)?\(\)/u', '', $parsed_query);
570    } while ($parsed_query !== $parsed_query_old);
571    $parsed_query = preg_replace('/(NOT|OR)+\)/u', ')'      , $parsed_query);
572    $parsed_query = preg_replace('/(OR)+/u'      , 'OR'     , $parsed_query);
573    $parsed_query = preg_replace('/\(OR/u'       , '('      , $parsed_query);
574    $parsed_query = preg_replace('/^OR|OR$/u'    , ''       , $parsed_query);
575    $parsed_query = preg_replace('/\)(NOT)?\(/u' , ')AND$1(', $parsed_query);
576
577    /**
578     * convert infix notation string into postfix (Reverse Polish notation) array
579     * by Shunting-yard algorithm
580     *
581     * see: http://en.wikipedia.org/wiki/Reverse_Polish_notation
582     * see: http://en.wikipedia.org/wiki/Shunting-yard_algorithm
583     */
584    $parsed_ary     = array();
585    $ope_stack      = array();
586    $ope_precedence = array(')' => 1, 'OR' => 2, 'AND' => 3, 'NOT' => 4, '(' => 5);
587    $ope_regex      = '/([()]|OR|AND|NOT)/u';
588
589    $tokens = preg_split($ope_regex, $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
590    foreach ($tokens as $token) {
591        if (preg_match($ope_regex, $token)) {
592            // operator
593            $last_ope = end($ope_stack);
594            while ($ope_precedence[$token] <= $ope_precedence[$last_ope] && $last_ope != '(') {
595                $parsed_ary[] = array_pop($ope_stack);
596                $last_ope = end($ope_stack);
597            }
598            if ($token == ')') {
599                array_pop($ope_stack); // this array_pop always deletes '('
600            } else {
601                $ope_stack[] = $token;
602            }
603        } else {
604            // operand
605            $token_decoded = str_replace(array('OP', 'CP'), array('(', ')'), $token);
606            $parsed_ary[] = $token_decoded;
607        }
608    }
609    $parsed_ary = array_values(array_merge($parsed_ary, array_reverse($ope_stack)));
610
611    // cleanup: each double "NOT" in RPN array actually does nothing
612    $parsed_ary_count = count($parsed_ary);
613    for ($i = 1; $i < $parsed_ary_count; ++$i) {
614        if ($parsed_ary[$i] === 'NOT' && $parsed_ary[$i - 1] === 'NOT') {
615            unset($parsed_ary[$i], $parsed_ary[$i - 1]);
616        }
617    }
618    $parsed_ary = array_values($parsed_ary);
619
620    // build return value
621    $q = array();
622    $q['query']      = $query;
623    $q['parsed_str'] = $parsed_query;
624    $q['parsed_ary'] = $parsed_ary;
625
626    foreach ($q['parsed_ary'] as $token) {
627        if ($token[2] !== ':') continue;
628        $body = substr($token, 3);
629
630        switch (substr($token, 0, 3)) {
631            case 'N_:':
632                $q['ns'][]        = $body; // for backward compatibility
633                break;
634            case 'W-:':
635                $q['words'][]     = $body;
636                break;
637            case 'W+:':
638                $q['words'][]     = $body;
639                $q['highlight'][] = str_replace('*', '', $body);
640                break;
641            case 'P_:':
642                $q['phrases'][]   = $body;
643                $q['highlight'][] = str_replace('*', '', $body);
644                break;
645        }
646    }
647    foreach (array('words', 'phrases', 'highlight', 'ns') as $key) {
648        $q[$key] = empty($q[$key]) ? array() : array_values(array_unique($q[$key]));
649    }
650
651    // keep backward compatibility (to some extent)
652    // this part can be deleted if no plugins use ft_queryParser() directly
653    $q['and']   = $q['words'];
654    $q['not']   = array(); // difficult to set: imagine [ aaa -(bbb -ccc) ]
655    $q['notns'] = array(); // same as above
656
657    return $q;
658}
659
660/**
661 * Transforms given search term into intermediate representation
662 *
663 * This function is used in ft_queryParser() and not for general purpose use.
664 *
665 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
666 */
667function ft_termParser($term, &$stopwords, $consider_asian = true, $phrase_mode = false) {
668    $parsed = '';
669    if ($consider_asian) {
670        // successive asian characters need to be searched as a phrase
671        $words = preg_split('/('.IDX_ASIAN.'+)/u', $term, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
672        foreach ($words as $word) {
673            if (preg_match('/'.IDX_ASIAN.'/u', $word)) $phrase_mode = true;
674            $parsed .= ft_termParser($word, $stopwords, false, $phrase_mode);
675        }
676    } else {
677        $term_noparen = str_replace(array('(', ')'), ' ', $term);
678        $words = idx_tokenizer($term_noparen, $stopwords, true);
679
680        // W+: needs to be highlighted, W-: no need to highlight
681        if (empty($words)) {
682            $parsed = '()'; // important: do not remove
683        } elseif ($words[0] === $term) {
684            $parsed = '(W+:'.$words[0].')';
685        } elseif ($phrase_mode) {
686            $term_encoded = str_replace(array('(', ')'), array('OP', 'CP'), $term);
687            $parsed = '((W-:'.implode(')(W-:', $words).')(P_:'.$term_encoded.'))';
688        } else {
689            $parsed = '((W+:'.implode(')(W+:', $words).'))';
690        }
691    }
692    return $parsed;
693}
694
695//Setup VIM: ex: et ts=4 enc=utf-8 :
696