code style: indent fixes
[dokuwiki.git] / inc / fulltext.php
blobd3450711f1b3aef4b32ee40c6af00b4e59c60714
1 <?php
2 /**
3 * DokuWiki fulltextsearch functions using the index
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
8 use dokuwiki\Utf8\Asian;
9 use dokuwiki\Search\Indexer;
10 use dokuwiki\Extension\Event;
11 use dokuwiki\Utf8\Clean;
12 use dokuwiki\Utf8\PhpString;
13 use dokuwiki\Utf8\Sort;
15 /**
16 * create snippets for the first few results only
18 if (!defined('FT_SNIPPET_NUMBER')) define('FT_SNIPPET_NUMBER', 15);
20 /**
21 * The fulltext search
23 * Returns a list of matching documents for the given query
25 * refactored into ft_pageSearch(), _ft_pageSearch() and trigger_event()
27 * @param string $query
28 * @param array $highlight
29 * @param string $sort
30 * @param int|string $after only show results with mtime after this date, accepts timestap or strtotime arguments
31 * @param int|string $before only show results with mtime before this date, accepts timestap or strtotime arguments
33 * @return array
35 function ft_pageSearch($query, &$highlight, $sort = null, $after = null, $before = null)
38 if ($sort === null) {
39 $sort = 'hits';
41 $data = [
42 'query' => $query,
43 'sort' => $sort,
44 'after' => $after,
45 'before' => $before
47 $data['highlight'] =& $highlight;
49 return Event::createAndTrigger('SEARCH_QUERY_FULLPAGE', $data, '_ft_pageSearch');
52 /**
53 * Returns a list of matching documents for the given query
55 * @author Andreas Gohr <andi@splitbrain.org>
56 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
58 * @param array $data event data
59 * @return array matching documents
61 function _ft_pageSearch(&$data)
63 $Indexer = idx_get_indexer();
65 // parse the given query
66 $q = ft_queryParser($Indexer, $data['query']);
67 $data['highlight'] = $q['highlight'];
69 if (empty($q['parsed_ary'])) return [];
71 // lookup all words found in the query
72 $lookup = $Indexer->lookup($q['words']);
74 // get all pages in this dokuwiki site (!: includes nonexistent pages)
75 $pages_all = [];
76 foreach ($Indexer->getPages() as $id) {
77 $pages_all[$id] = 0; // base: 0 hit
80 // process the query
81 $stack = [];
82 foreach ($q['parsed_ary'] as $token) {
83 switch (substr($token, 0, 3)) {
84 case 'W+:':
85 case 'W-:':
86 case 'W_:': // word
87 $word = substr($token, 3);
88 if (isset($lookup[$word])) {
89 $stack[] = (array)$lookup[$word];
91 break;
92 case 'P+:':
93 case 'P-:': // phrase
94 $phrase = substr($token, 3);
95 // since phrases are always parsed as ((W1)(W2)...(P)),
96 // the end($stack) always points the pages that contain
97 // all words in this phrase
98 $pages = end($stack);
99 $pages_matched = [];
100 foreach (array_keys($pages) as $id) {
101 $evdata = [
102 'id' => $id,
103 'phrase' => $phrase,
104 'text' => rawWiki($id)
106 $evt = new Event('FULLTEXT_PHRASE_MATCH', $evdata);
107 if ($evt->advise_before() && $evt->result !== true) {
108 $text = PhpString::strtolower($evdata['text']);
109 if (strpos($text, $phrase) !== false) {
110 $evt->result = true;
113 $evt->advise_after();
114 if ($evt->result === true) {
115 $pages_matched[$id] = 0; // phrase: always 0 hit
118 $stack[] = $pages_matched;
119 break;
120 case 'N+:':
121 case 'N-:': // namespace
122 $ns = cleanID(substr($token, 3)) . ':';
123 $pages_matched = [];
124 foreach (array_keys($pages_all) as $id) {
125 if (strpos($id, $ns) === 0) {
126 $pages_matched[$id] = 0; // namespace: always 0 hit
129 $stack[] = $pages_matched;
130 break;
131 case 'AND': // and operation
132 [$pages1, $pages2] = array_splice($stack, -2);
133 $stack[] = ft_resultCombine([$pages1, $pages2]);
134 break;
135 case 'OR': // or operation
136 [$pages1, $pages2] = array_splice($stack, -2);
137 $stack[] = ft_resultUnite([$pages1, $pages2]);
138 break;
139 case 'NOT': // not operation (unary)
140 $pages = array_pop($stack);
141 $stack[] = ft_resultComplement([$pages_all, $pages]);
142 break;
145 $docs = array_pop($stack);
147 if (empty($docs)) return [];
149 // check: settings, acls, existence
150 foreach (array_keys($docs) as $id) {
151 if (isHiddenPage($id) || auth_quickaclcheck($id) < AUTH_READ || !page_exists($id, '', false)) {
152 unset($docs[$id]);
156 $docs = _ft_filterResultsByTime($docs, $data['after'], $data['before']);
158 if ($data['sort'] === 'mtime') {
159 uksort($docs, 'ft_pagemtimesorter');
160 } else {
161 // sort docs by count
162 uksort($docs, 'ft_pagesorter');
163 arsort($docs);
166 return $docs;
170 * Returns the backlinks for a given page
172 * Uses the metadata index.
174 * @param string $id The id for which links shall be returned
175 * @param bool $ignore_perms Ignore the fact that pages are hidden or read-protected
176 * @return array The pages that contain links to the given page
178 function ft_backlinks($id, $ignore_perms = false)
180 $result = idx_get_indexer()->lookupKey('relation_references', $id);
182 if ($result === []) return $result;
184 // check ACL permissions
185 foreach (array_keys($result) as $idx) {
186 if (
187 (!$ignore_perms && (
188 isHiddenPage($result[$idx]) || auth_quickaclcheck($result[$idx]) < AUTH_READ
189 )) || !page_exists($result[$idx], '', false)
191 unset($result[$idx]);
195 Sort::sort($result);
196 return $result;
200 * Returns the pages that use a given media file
202 * Uses the relation media metadata property and the metadata index.
204 * Note that before 2013-07-31 the second parameter was the maximum number of results and
205 * permissions were ignored. That's why the parameter is now checked to be explicitely set
206 * to true (with type bool) in order to be compatible with older uses of the function.
208 * @param string $id The media id to look for
209 * @param bool $ignore_perms Ignore hidden pages and acls (optional, default: false)
210 * @return array A list of pages that use the given media file
212 function ft_mediause($id, $ignore_perms = false)
214 $result = idx_get_indexer()->lookupKey('relation_media', $id);
216 if ($result === []) return $result;
218 // check ACL permissions
219 foreach (array_keys($result) as $idx) {
220 if (
221 (!$ignore_perms && (
222 isHiddenPage($result[$idx]) || auth_quickaclcheck($result[$idx]) < AUTH_READ
223 )) || !page_exists($result[$idx], '', false)
225 unset($result[$idx]);
229 Sort::sort($result);
230 return $result;
235 * Quicksearch for pagenames
237 * By default it only matches the pagename and ignores the
238 * namespace. This can be changed with the second parameter.
239 * The third parameter allows to search in titles as well.
241 * The function always returns titles as well
243 * @triggers SEARCH_QUERY_PAGELOOKUP
244 * @author Andreas Gohr <andi@splitbrain.org>
245 * @author Adrian Lang <lang@cosmocode.de>
247 * @param string $id page id
248 * @param bool $in_ns match against namespace as well?
249 * @param bool $in_title search in title?
250 * @param int|string $after only show results with mtime after this date, accepts timestap or strtotime arguments
251 * @param int|string $before only show results with mtime before this date, accepts timestap or strtotime arguments
253 * @return string[]
255 function ft_pageLookup($id, $in_ns = false, $in_title = false, $after = null, $before = null)
257 $data = [
258 'id' => $id,
259 'in_ns' => $in_ns,
260 'in_title' => $in_title,
261 'after' => $after,
262 'before' => $before
264 $data['has_titles'] = true; // for plugin backward compatibility check
265 return Event::createAndTrigger('SEARCH_QUERY_PAGELOOKUP', $data, '_ft_pageLookup');
269 * Returns list of pages as array(pageid => First Heading)
271 * @param array &$data event data
272 * @return string[]
274 function _ft_pageLookup(&$data)
276 // split out original parameters
277 $id = $data['id'];
278 $Indexer = idx_get_indexer();
279 $parsedQuery = ft_queryParser($Indexer, $id);
280 if (count($parsedQuery['ns']) > 0) {
281 $ns = cleanID($parsedQuery['ns'][0]) . ':';
282 $id = implode(' ', $parsedQuery['highlight']);
284 if (count($parsedQuery['notns']) > 0) {
285 $notns = cleanID($parsedQuery['notns'][0]) . ':';
286 $id = implode(' ', $parsedQuery['highlight']);
289 $in_ns = $data['in_ns'];
290 $in_title = $data['in_title'];
291 $cleaned = cleanID($id);
293 $Indexer = idx_get_indexer();
294 $page_idx = $Indexer->getPages();
296 $pages = [];
297 if ($id !== '' && $cleaned !== '') {
298 foreach ($page_idx as $p_id) {
299 if ((strpos($in_ns ? $p_id : noNSorNS($p_id), $cleaned) !== false)) {
300 if (!isset($pages[$p_id]))
301 $pages[$p_id] = p_get_first_heading($p_id, METADATA_DONT_RENDER);
304 if ($in_title) {
305 foreach ($Indexer->lookupKey('title', $id, '_ft_pageLookupTitleCompare') as $p_id) {
306 if (!isset($pages[$p_id]))
307 $pages[$p_id] = p_get_first_heading($p_id, METADATA_DONT_RENDER);
312 if (isset($ns)) {
313 foreach (array_keys($pages) as $p_id) {
314 if (strpos($p_id, $ns) !== 0) {
315 unset($pages[$p_id]);
319 if (isset($notns)) {
320 foreach (array_keys($pages) as $p_id) {
321 if (strpos($p_id, $notns) === 0) {
322 unset($pages[$p_id]);
327 // discard hidden pages
328 // discard nonexistent pages
329 // check ACL permissions
330 foreach (array_keys($pages) as $idx) {
331 if (
332 !isVisiblePage($idx) || !page_exists($idx) ||
333 auth_quickaclcheck($idx) < AUTH_READ
335 unset($pages[$idx]);
339 $pages = _ft_filterResultsByTime($pages, $data['after'], $data['before']);
341 uksort($pages, 'ft_pagesorter');
342 return $pages;
347 * @param array $results search results in the form pageid => value
348 * @param int|string $after only returns results with mtime after this date, accepts timestap or strtotime arguments
349 * @param int|string $before only returns results with mtime after this date, accepts timestap or strtotime arguments
351 * @return array
353 function _ft_filterResultsByTime(array $results, $after, $before)
355 if ($after || $before) {
356 $after = is_int($after) ? $after : strtotime($after);
357 $before = is_int($before) ? $before : strtotime($before);
359 foreach (array_keys($results) as $id) {
360 $mTime = filemtime(wikiFN($id));
361 if ($after && $after > $mTime) {
362 unset($results[$id]);
363 continue;
365 if ($before && $before < $mTime) {
366 unset($results[$id]);
371 return $results;
375 * Tiny helper function for comparing the searched title with the title
376 * from the search index. This function is a wrapper around stripos with
377 * adapted argument order and return value.
379 * @param string $search searched title
380 * @param string $title title from index
381 * @return bool
383 function _ft_pageLookupTitleCompare($search, $title)
385 if (Clean::isASCII($search)) {
386 $pos = stripos($title, $search);
387 } else {
388 $pos = PhpString::strpos(
389 PhpString::strtolower($title),
390 PhpString::strtolower($search)
394 return $pos !== false;
398 * Sort pages based on their namespace level first, then on their string
399 * values. This makes higher hierarchy pages rank higher than lower hierarchy
400 * pages.
402 * @param string $a
403 * @param string $b
404 * @return int Returns < 0 if $a is less than $b; > 0 if $a is greater than $b, and 0 if they are equal.
406 function ft_pagesorter($a, $b)
408 $ac = count(explode(':', $a));
409 $bc = count(explode(':', $b));
410 if ($ac < $bc) {
411 return -1;
412 } elseif ($ac > $bc) {
413 return 1;
415 return Sort::strcmp($a, $b);
419 * Sort pages by their mtime, from newest to oldest
421 * @param string $a
422 * @param string $b
424 * @return int Returns < 0 if $a is newer than $b, > 0 if $b is newer than $a and 0 if they are of the same age
426 function ft_pagemtimesorter($a, $b)
428 $mtimeA = filemtime(wikiFN($a));
429 $mtimeB = filemtime(wikiFN($b));
430 return $mtimeB - $mtimeA;
434 * Creates a snippet extract
436 * @author Andreas Gohr <andi@splitbrain.org>
437 * @triggers FULLTEXT_SNIPPET_CREATE
439 * @param string $id page id
440 * @param array $highlight
441 * @return mixed
443 function ft_snippet($id, $highlight)
445 $text = rawWiki($id);
446 $text = str_replace("\xC2\xAD", '', $text);
447 // remove soft-hyphens
448 $evdata = [
449 'id' => $id,
450 'text' => &$text,
451 'highlight' => &$highlight,
452 'snippet' => ''
455 $evt = new Event('FULLTEXT_SNIPPET_CREATE', $evdata);
456 if ($evt->advise_before()) {
457 $match = [];
458 $snippets = [];
459 $utf8_offset = 0;
460 $offset = 0;
461 $end = 0;
462 $len = PhpString::strlen($text);
464 // build a regexp from the phrases to highlight
465 $re1 = '(' .
466 implode(
467 '|',
468 array_map(
469 'ft_snippet_re_preprocess',
470 array_map(
471 'preg_quote_cb',
472 array_filter((array) $highlight)
476 ')';
477 $re2 = "$re1.{0,75}(?!\\1)$re1";
478 $re3 = "$re1.{0,45}(?!\\1)$re1.{0,45}(?!\\1)(?!\\2)$re1";
480 for ($cnt=4; $cnt--;) {
481 if (0) {
482 } elseif (preg_match('/'.$re3.'/iu', $text, $match, PREG_OFFSET_CAPTURE, $offset)) {
483 } elseif (preg_match('/'.$re2.'/iu', $text, $match, PREG_OFFSET_CAPTURE, $offset)) {
484 } elseif (preg_match('/'.$re1.'/iu', $text, $match, PREG_OFFSET_CAPTURE, $offset)) {
485 } else {
486 break;
489 [$str, $idx] = $match[0];
491 // convert $idx (a byte offset) into a utf8 character offset
492 $utf8_idx = PhpString::strlen(substr($text, 0, $idx));
493 $utf8_len = PhpString::strlen($str);
495 // establish context, 100 bytes surrounding the match string
496 // first look to see if we can go 100 either side,
497 // then drop to 50 adding any excess if the other side can't go to 50,
498 $pre = min($utf8_idx-$utf8_offset, 100);
499 $post = min($len-$utf8_idx-$utf8_len, 100);
501 if ($pre>50 && $post>50) {
502 $pre = 50;
503 $post = 50;
504 } elseif ($pre>50) {
505 $pre = min($pre, 100-$post);
506 } elseif ($post>50) {
507 $post = min($post, 100-$pre);
508 } elseif ($offset == 0) {
509 // both are less than 50, means the context is the whole string
510 // make it so and break out of this loop - there is no need for the
511 // complex snippet calculations
512 $snippets = [$text];
513 break;
516 // establish context start and end points, try to append to previous
517 // context if possible
518 $start = $utf8_idx - $pre;
519 $append = ($start < $end) ? $end : false; // still the end of the previous context snippet
520 $end = $utf8_idx + $utf8_len + $post; // now set it to the end of this context
522 if ($append) {
523 $snippets[count($snippets)-1] .= PhpString::substr($text, $append, $end-$append);
524 } else {
525 $snippets[] = PhpString::substr($text, $start, $end-$start);
528 // set $offset for next match attempt
529 // continue matching after the current match
530 // if the current match is not the longest possible match starting at the current offset
531 // this prevents further matching of this snippet but for possible matches of length
532 // smaller than match length + context (at least 50 characters) this match is part of the context
533 $utf8_offset = $utf8_idx + $utf8_len;
534 $offset = $idx + strlen(PhpString::substr($text, $utf8_idx, $utf8_len));
535 $offset = Clean::correctIdx($text, $offset);
538 $m = "\1";
539 $snippets = preg_replace('/'.$re1.'/iu', $m.'$1'.$m, $snippets);
540 $snippet = preg_replace(
541 '/' . $m . '([^' . $m . ']*?)' . $m . '/iu',
542 '<strong class="search_hit">$1</strong>',
543 hsc(implode('... ', $snippets))
546 $evdata['snippet'] = $snippet;
548 $evt->advise_after();
549 unset($evt);
551 return $evdata['snippet'];
555 * Wraps a search term in regex boundary checks.
557 * @param string $term
558 * @return string
560 function ft_snippet_re_preprocess($term)
562 // do not process asian terms where word boundaries are not explicit
563 if (Asian::isAsianWords($term)) return $term;
565 if (UTF8_PROPERTYSUPPORT) {
566 // unicode word boundaries
567 // see http://stackoverflow.com/a/2449017/172068
568 $BL = '(?<!\pL)';
569 $BR = '(?!\pL)';
570 } else {
571 // not as correct as above, but at least won't break
572 $BL = '\b';
573 $BR = '\b';
576 if (substr($term, 0, 2) == '\\*') {
577 $term = substr($term, 2);
578 } else {
579 $term = $BL.$term;
582 if (substr($term, -2, 2) == '\\*') {
583 $term = substr($term, 0, -2);
584 } else {
585 $term .= $BR;
588 if ($term == $BL || $term == $BR || $term == $BL.$BR) $term = '';
589 return $term;
593 * Combine found documents and sum up their scores
595 * This function is used to combine searched words with a logical
596 * AND. Only documents available in all arrays are returned.
598 * based upon PEAR's PHP_Compat function for array_intersect_key()
600 * @param array $args An array of page arrays
601 * @return array
603 function ft_resultCombine($args)
605 $array_count = count($args);
606 if ($array_count == 1) {
607 return $args[0];
610 $result = [];
611 if ($array_count > 1) {
612 foreach ($args[0] as $key => $value) {
613 $result[$key] = $value;
614 for ($i = 1; $i !== $array_count; $i++) {
615 if (!isset($args[$i][$key])) {
616 unset($result[$key]);
617 break;
619 $result[$key] += $args[$i][$key];
623 return $result;
627 * Unites found documents and sum up their scores
629 * based upon ft_resultCombine() function
631 * @param array $args An array of page arrays
632 * @return array
634 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
636 function ft_resultUnite($args)
638 $array_count = count($args);
639 if ($array_count === 1) {
640 return $args[0];
643 $result = $args[0];
644 for ($i = 1; $i !== $array_count; $i++) {
645 foreach (array_keys($args[$i]) as $id) {
646 $result[$id] += $args[$i][$id];
649 return $result;
653 * Computes the difference of documents using page id for comparison
655 * nearly identical to PHP5's array_diff_key()
657 * @param array $args An array of page arrays
658 * @return array
660 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
662 function ft_resultComplement($args)
664 $array_count = count($args);
665 if ($array_count === 1) {
666 return $args[0];
669 $result = $args[0];
670 foreach (array_keys($result) as $id) {
671 for ($i = 1; $i !== $array_count; $i++) {
672 if (isset($args[$i][$id])) unset($result[$id]);
675 return $result;
679 * Parses a search query and builds an array of search formulas
681 * @author Andreas Gohr <andi@splitbrain.org>
682 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
684 * @param Indexer $Indexer
685 * @param string $query search query
686 * @return array of search formulas
688 function ft_queryParser($Indexer, $query)
691 * parse a search query and transform it into intermediate representation
693 * in a search query, you can use the following expressions:
695 * words:
696 * include
697 * -exclude
698 * phrases:
699 * "phrase to be included"
700 * -"phrase you want to exclude"
701 * namespaces:
702 * @include:namespace (or ns:include:namespace)
703 * ^exclude:namespace (or -ns:exclude:namespace)
704 * groups:
705 * ()
706 * -()
707 * operators:
708 * and ('and' is the default operator: you can always omit this)
709 * or (or pipe symbol '|', lower precedence than 'and')
711 * e.g. a query [ aa "bb cc" @dd:ee ] means "search pages which contain
712 * a word 'aa', a phrase 'bb cc' and are within a namespace 'dd:ee'".
713 * this query is equivalent to [ -(-aa or -"bb cc" or -ns:dd:ee) ]
714 * as long as you don't mind hit counts.
716 * intermediate representation consists of the following parts:
718 * ( ) - group
719 * AND - logical and
720 * OR - logical or
721 * NOT - logical not
722 * W+:, W-:, W_: - word (underscore: no need to highlight)
723 * P+:, P-: - phrase (minus sign: logically in NOT group)
724 * N+:, N-: - namespace
726 $parsed_query = '';
727 $parens_level = 0;
728 $terms = preg_split(
729 '/(-?".*?")/u',
730 PhpString::strtolower($query),
732 PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
735 foreach ($terms as $term) {
736 $parsed = '';
737 if (preg_match('/^(-?)"(.+)"$/u', $term, $matches)) {
738 // phrase-include and phrase-exclude
739 $not = $matches[1] ? 'NOT' : '';
740 $parsed = $not.ft_termParser($Indexer, $matches[2], false, true);
741 } else {
742 // fix incomplete phrase
743 $term = str_replace('"', ' ', $term);
745 // fix parentheses
746 $term = str_replace(')', ' ) ', $term);
747 $term = str_replace('(', ' ( ', $term);
748 $term = str_replace('- (', ' -(', $term);
750 // treat pipe symbols as 'OR' operators
751 $term = str_replace('|', ' or ', $term);
753 // treat ideographic spaces (U+3000) as search term separators
754 // FIXME: some more separators?
755 $term = preg_replace('/[ \x{3000}]+/u', ' ', $term);
756 $term = trim($term);
757 if ($term === '') continue;
759 $tokens = explode(' ', $term);
760 foreach ($tokens as $token) {
761 if ($token === '(') {
762 // parenthesis-include-open
763 $parsed .= '(';
764 ++$parens_level;
765 } elseif ($token === '-(') {
766 // parenthesis-exclude-open
767 $parsed .= 'NOT(';
768 ++$parens_level;
769 } elseif ($token === ')') {
770 // parenthesis-any-close
771 if ($parens_level === 0) continue;
772 $parsed .= ')';
773 $parens_level--;
774 } elseif ($token === 'and') {
775 // logical-and (do nothing)
776 } elseif ($token === 'or') {
777 // logical-or
778 $parsed .= 'OR';
779 } elseif (preg_match('/^(?:\^|-ns:)(.+)$/u', $token, $matches)) {
780 // namespace-exclude
781 $parsed .= 'NOT(N+:'.$matches[1].')';
782 } elseif (preg_match('/^(?:@|ns:)(.+)$/u', $token, $matches)) {
783 // namespace-include
784 $parsed .= '(N+:'.$matches[1].')';
785 } elseif (preg_match('/^-(.+)$/', $token, $matches)) {
786 // word-exclude
787 $parsed .= 'NOT('.ft_termParser($Indexer, $matches[1]).')';
788 } else {
789 // word-include
790 $parsed .= ft_termParser($Indexer, $token);
794 $parsed_query .= $parsed;
797 // cleanup (very sensitive)
798 $parsed_query .= str_repeat(')', $parens_level);
799 do {
800 $parsed_query_old = $parsed_query;
801 $parsed_query = preg_replace('/(NOT)?\(\)/u', '', $parsed_query);
802 } while ($parsed_query !== $parsed_query_old);
803 $parsed_query = preg_replace('/(NOT|OR)+\)/u', ')', $parsed_query);
804 $parsed_query = preg_replace('/(OR)+/u', 'OR', $parsed_query);
805 $parsed_query = preg_replace('/\(OR/u', '(', $parsed_query);
806 $parsed_query = preg_replace('/^OR|OR$/u', '', $parsed_query);
807 $parsed_query = preg_replace('/\)(NOT)?\(/u', ')AND$1(', $parsed_query);
809 // adjustment: make highlightings right
810 $parens_level = 0;
811 $notgrp_levels = [];
812 $parsed_query_new = '';
813 $tokens = preg_split('/(NOT\(|[()])/u', $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
814 foreach ($tokens as $token) {
815 if ($token === 'NOT(') {
816 $notgrp_levels[] = ++$parens_level;
817 } elseif ($token === '(') {
818 ++$parens_level;
819 } elseif ($token === ')') {
820 if ($parens_level-- === end($notgrp_levels)) array_pop($notgrp_levels);
821 } elseif (count($notgrp_levels) % 2 === 1) {
822 // turn highlight-flag off if terms are logically in "NOT" group
823 $token = preg_replace('/([WPN])\+\:/u', '$1-:', $token);
825 $parsed_query_new .= $token;
827 $parsed_query = $parsed_query_new;
830 * convert infix notation string into postfix (Reverse Polish notation) array
831 * by Shunting-yard algorithm
833 * see: http://en.wikipedia.org/wiki/Reverse_Polish_notation
834 * see: http://en.wikipedia.org/wiki/Shunting-yard_algorithm
836 $parsed_ary = [];
837 $ope_stack = [];
838 $ope_precedence = [')' => 1, 'OR' => 2, 'AND' => 3, 'NOT' => 4, '(' => 5];
839 $ope_regex = '/([()]|OR|AND|NOT)/u';
841 $tokens = preg_split($ope_regex, $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
842 foreach ($tokens as $token) {
843 if (preg_match($ope_regex, $token)) {
844 // operator
845 $last_ope = end($ope_stack);
846 while ($last_ope !== false && $ope_precedence[$token] <= $ope_precedence[$last_ope] && $last_ope != '(') {
847 $parsed_ary[] = array_pop($ope_stack);
848 $last_ope = end($ope_stack);
850 if ($token == ')') {
851 array_pop($ope_stack); // this array_pop always deletes '('
852 } else {
853 $ope_stack[] = $token;
855 } else {
856 // operand
857 $token_decoded = str_replace(['OP', 'CP'], ['(', ')'], $token);
858 $parsed_ary[] = $token_decoded;
861 $parsed_ary = array_values([...$parsed_ary, ...array_reverse($ope_stack)]);
863 // cleanup: each double "NOT" in RPN array actually does nothing
864 $parsed_ary_count = count($parsed_ary);
865 for ($i = 1; $i < $parsed_ary_count; ++$i) {
866 if ($parsed_ary[$i] === 'NOT' && $parsed_ary[$i - 1] === 'NOT') {
867 unset($parsed_ary[$i], $parsed_ary[$i - 1]);
870 $parsed_ary = array_values($parsed_ary);
872 // build return value
873 $q = [];
874 $q['query'] = $query;
875 $q['parsed_str'] = $parsed_query;
876 $q['parsed_ary'] = $parsed_ary;
878 foreach ($q['parsed_ary'] as $token) {
879 if (strlen($token) < 3 || $token[2] !== ':') continue;
880 $body = substr($token, 3);
882 switch (substr($token, 0, 3)) {
883 case 'N+:':
884 $q['ns'][] = $body; // for backward compatibility
885 break;
886 case 'N-:':
887 $q['notns'][] = $body; // for backward compatibility
888 break;
889 case 'W_:':
890 $q['words'][] = $body;
891 break;
892 case 'W-:':
893 $q['words'][] = $body;
894 $q['not'][] = $body; // for backward compatibility
895 break;
896 case 'W+:':
897 $q['words'][] = $body;
898 $q['highlight'][] = $body;
899 $q['and'][] = $body; // for backward compatibility
900 break;
901 case 'P-:':
902 $q['phrases'][] = $body;
903 break;
904 case 'P+:':
905 $q['phrases'][] = $body;
906 $q['highlight'][] = $body;
907 break;
910 foreach (['words', 'phrases', 'highlight', 'ns', 'notns', 'and', 'not'] as $key) {
911 $q[$key] = empty($q[$key]) ? [] : array_values(array_unique($q[$key]));
914 return $q;
918 * Transforms given search term into intermediate representation
920 * This function is used in ft_queryParser() and not for general purpose use.
922 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
924 * @param Indexer $Indexer
925 * @param string $term
926 * @param bool $consider_asian
927 * @param bool $phrase_mode
928 * @return string
930 function ft_termParser($Indexer, $term, $consider_asian = true, $phrase_mode = false)
932 $parsed = '';
933 if ($consider_asian) {
934 // successive asian characters need to be searched as a phrase
935 $words = Asian::splitAsianWords($term);
936 foreach ($words as $word) {
937 $phrase_mode = $phrase_mode ? true : Asian::isAsianWords($word);
938 $parsed .= ft_termParser($Indexer, $word, false, $phrase_mode);
940 } else {
941 $term_noparen = str_replace(['(', ')'], ' ', $term);
942 $words = $Indexer->tokenizer($term_noparen, true);
944 // W_: no need to highlight
945 if (empty($words)) {
946 $parsed = '()'; // important: do not remove
947 } elseif ($words[0] === $term) {
948 $parsed = '(W+:'.$words[0].')';
949 } elseif ($phrase_mode) {
950 $term_encoded = str_replace(['(', ')'], ['OP', 'CP'], $term);
951 $parsed = '((W_:'.implode(')(W_:', $words).')(P+:'.$term_encoded.'))';
952 } else {
953 $parsed = '((W+:'.implode(')(W+:', $words).'))';
956 return $parsed;
960 * Recreate a search query string based on parsed parts, doesn't support negated phrases and `OR` searches
962 * @param array $and
963 * @param array $not
964 * @param array $phrases
965 * @param array $ns
966 * @param array $notns
968 * @return string
970 function ft_queryUnparser_simple(array $and, array $not, array $phrases, array $ns, array $notns)
972 $query = implode(' ', $and);
973 if ($not !== []) {
974 $query .= ' -' . implode(' -', $not);
977 if ($phrases !== []) {
978 $query .= ' "' . implode('" "', $phrases) . '"';
981 if ($ns !== []) {
982 $query .= ' @' . implode(' @', $ns);
985 if ($notns !== []) {
986 $query .= ' ^' . implode(' ^', $notns);
989 return $query;
992 //Setup VIM: ex: et ts=4 :