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