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 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+:, W-:, W_: - word (underscore: no need to highlight) 495 * P+:, P-: - phrase (minus sign: logically in NOT group) 496 * N+:, N-: - namespace 497 */ 498 $parsed_query = ''; 499 $parens_level = 0; 500 $terms = preg_split('/(-?".*?")/u', utf8_strtolower($query), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 501 502 foreach ($terms as $term) { 503 $parsed = ''; 504 if (preg_match('/^(-?)"(.+)"$/u', $term, $matches)) { 505 // phrase-include and phrase-exclude 506 $not = $matches[1] ? 'NOT' : ''; 507 $parsed = $not.ft_termParser($matches[2], $stopwords, false, true); 508 } else { 509 // fix incomplete phrase 510 $term = str_replace('"', ' ', $term); 511 512 // fix parentheses 513 $term = str_replace(')' , ' ) ', $term); 514 $term = str_replace('(' , ' ( ', $term); 515 $term = str_replace('- (', ' -(', $term); 516 517 // treat pipe symbols as 'OR' operators 518 $term = str_replace('|', ' or ', $term); 519 520 // treat ideographic spaces (U+3000) as search term separators 521 // FIXME: some more separators? 522 $term = preg_replace('/[ \x{3000}]+/u', ' ', $term); 523 $term = trim($term); 524 if ($term === '') continue; 525 526 $tokens = explode(' ', $term); 527 foreach ($tokens as $token) { 528 if ($token === '(') { 529 // parenthesis-include-open 530 $parsed .= '('; 531 ++$parens_level; 532 } elseif ($token === '-(') { 533 // parenthesis-exclude-open 534 $parsed .= 'NOT('; 535 ++$parens_level; 536 } elseif ($token === ')') { 537 // parenthesis-any-close 538 if ($parens_level === 0) continue; 539 $parsed .= ')'; 540 $parens_level--; 541 } elseif ($token === 'and') { 542 // logical-and (do nothing) 543 } elseif ($token === 'or') { 544 // logical-or 545 $parsed .= 'OR'; 546 } elseif (preg_match('/^(?:\^|-ns:)(.+)$/u', $token, $matches)) { 547 // namespace-exclude 548 $parsed .= 'NOT(N+:'.$matches[1].')'; 549 } elseif (preg_match('/^(?:@|ns:)(.+)$/u', $token, $matches)) { 550 // namespace-include 551 $parsed .= '(N+:'.$matches[1].')'; 552 } elseif (preg_match('/^-(.+)$/', $token, $matches)) { 553 // word-exclude 554 $parsed .= 'NOT('.ft_termParser($matches[1], $stopwords).')'; 555 } else { 556 // word-include 557 $parsed .= ft_termParser($token, $stopwords); 558 } 559 } 560 } 561 $parsed_query .= $parsed; 562 } 563 564 // cleanup (very sensitive) 565 $parsed_query .= str_repeat(')', $parens_level); 566 do { 567 $parsed_query_old = $parsed_query; 568 $parsed_query = preg_replace('/(NOT)?\(\)/u', '', $parsed_query); 569 } while ($parsed_query !== $parsed_query_old); 570 $parsed_query = preg_replace('/(NOT|OR)+\)/u', ')' , $parsed_query); 571 $parsed_query = preg_replace('/(OR)+/u' , 'OR' , $parsed_query); 572 $parsed_query = preg_replace('/\(OR/u' , '(' , $parsed_query); 573 $parsed_query = preg_replace('/^OR|OR$/u' , '' , $parsed_query); 574 $parsed_query = preg_replace('/\)(NOT)?\(/u' , ')AND$1(', $parsed_query); 575 576 // adjustment: make highlightings right 577 $parens_level = 0; 578 $notgrp_levels = array(); 579 $parsed_query_new = ''; 580 $tokens = preg_split('/(NOT\(|[()])/u', $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 581 foreach ($tokens as $token) { 582 if ($token === 'NOT(') { 583 $notgrp_levels[] = ++$parens_level; 584 } elseif ($token === '(') { 585 ++$parens_level; 586 } elseif ($token === ')') { 587 if ($parens_level-- === end($notgrp_levels)) array_pop($notgrp_levels); 588 } elseif (count($notgrp_levels) % 2 === 1) { 589 // turn highlight-flag off if terms are logically in "NOT" group 590 $token = preg_replace('/([WPN])\+\:/u', '$1-:', $token); 591 } 592 $parsed_query_new .= $token; 593 } 594 $parsed_query = $parsed_query_new; 595 596 /** 597 * convert infix notation string into postfix (Reverse Polish notation) array 598 * by Shunting-yard algorithm 599 * 600 * see: http://en.wikipedia.org/wiki/Reverse_Polish_notation 601 * see: http://en.wikipedia.org/wiki/Shunting-yard_algorithm 602 */ 603 $parsed_ary = array(); 604 $ope_stack = array(); 605 $ope_precedence = array(')' => 1, 'OR' => 2, 'AND' => 3, 'NOT' => 4, '(' => 5); 606 $ope_regex = '/([()]|OR|AND|NOT)/u'; 607 608 $tokens = preg_split($ope_regex, $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 609 foreach ($tokens as $token) { 610 if (preg_match($ope_regex, $token)) { 611 // operator 612 $last_ope = end($ope_stack); 613 while ($ope_precedence[$token] <= $ope_precedence[$last_ope] && $last_ope != '(') { 614 $parsed_ary[] = array_pop($ope_stack); 615 $last_ope = end($ope_stack); 616 } 617 if ($token == ')') { 618 array_pop($ope_stack); // this array_pop always deletes '(' 619 } else { 620 $ope_stack[] = $token; 621 } 622 } else { 623 // operand 624 $token_decoded = str_replace(array('OP', 'CP'), array('(', ')'), $token); 625 $parsed_ary[] = $token_decoded; 626 } 627 } 628 $parsed_ary = array_values(array_merge($parsed_ary, array_reverse($ope_stack))); 629 630 // cleanup: each double "NOT" in RPN array actually does nothing 631 $parsed_ary_count = count($parsed_ary); 632 for ($i = 1; $i < $parsed_ary_count; ++$i) { 633 if ($parsed_ary[$i] === 'NOT' && $parsed_ary[$i - 1] === 'NOT') { 634 unset($parsed_ary[$i], $parsed_ary[$i - 1]); 635 } 636 } 637 $parsed_ary = array_values($parsed_ary); 638 639 // build return value 640 $q = array(); 641 $q['query'] = $query; 642 $q['parsed_str'] = $parsed_query; 643 $q['parsed_ary'] = $parsed_ary; 644 645 foreach ($q['parsed_ary'] as $token) { 646 if ($token[2] !== ':') continue; 647 $body = substr($token, 3); 648 649 switch (substr($token, 0, 3)) { 650 case 'N+:': 651 $q['ns'][] = $body; // for backward compatibility 652 break; 653 case 'N-:': 654 $q['notns'][] = $body; // for backward compatibility 655 break; 656 case 'W_:': 657 $q['words'][] = $body; 658 break; 659 case 'W-:': 660 $q['words'][] = $body; 661 $q['not'][] = $body; // for backward compatibility 662 break; 663 case 'W+:': 664 $q['words'][] = $body; 665 $q['highlight'][] = str_replace('*', '', $body); 666 $q['and'][] = $body; // for backward compatibility 667 break; 668 case 'P-:': 669 $q['phrases'][] = $body; 670 break; 671 case 'P+:': 672 $q['phrases'][] = $body; 673 $q['highlight'][] = str_replace('*', '', $body); 674 break; 675 } 676 } 677 foreach (array('words', 'phrases', 'highlight', 'ns', 'notns', 'and', 'not') as $key) { 678 $q[$key] = empty($q[$key]) ? array() : array_values(array_unique($q[$key])); 679 } 680 681 return $q; 682} 683 684/** 685 * Transforms given search term into intermediate representation 686 * 687 * This function is used in ft_queryParser() and not for general purpose use. 688 * 689 * @author Kazutaka Miyasaka <kazmiya@gmail.com> 690 */ 691function ft_termParser($term, &$stopwords, $consider_asian = true, $phrase_mode = false) { 692 $parsed = ''; 693 if ($consider_asian) { 694 // successive asian characters need to be searched as a phrase 695 $words = preg_split('/('.IDX_ASIAN.'+)/u', $term, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 696 foreach ($words as $word) { 697 if (preg_match('/'.IDX_ASIAN.'/u', $word)) $phrase_mode = true; 698 $parsed .= ft_termParser($word, $stopwords, false, $phrase_mode); 699 } 700 } else { 701 $term_noparen = str_replace(array('(', ')'), ' ', $term); 702 $words = idx_tokenizer($term_noparen, $stopwords, true); 703 704 // W_: no need to highlight 705 if (empty($words)) { 706 $parsed = '()'; // important: do not remove 707 } elseif ($words[0] === $term) { 708 $parsed = '(W+:'.$words[0].')'; 709 } elseif ($phrase_mode) { 710 $term_encoded = str_replace(array('(', ')'), array('OP', 'CP'), $term); 711 $parsed = '((W_:'.implode(')(W_:', $words).')(P+:'.$term_encoded.'))'; 712 } else { 713 $parsed = '((W+:'.implode(')(W+:', $words).'))'; 714 } 715 } 716 return $parsed; 717} 718 719//Setup VIM: ex: et ts=4 enc=utf-8 : 720