1f5eb7cf0SAndreas Gohr<?php 2f5eb7cf0SAndreas Gohr/** 3f5eb7cf0SAndreas Gohr * DokuWiki fulltextsearch functions using the index 4f5eb7cf0SAndreas Gohr * 5f5eb7cf0SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6f5eb7cf0SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7f5eb7cf0SAndreas Gohr */ 8f5eb7cf0SAndreas Gohr 9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 10f5eb7cf0SAndreas Gohr 11bd0293e7SAndreas Gohr/** 12bd0293e7SAndreas Gohr * create snippets for the first few results only 13bd0293e7SAndreas Gohr */ 14bd0293e7SAndreas Gohrif(!defined('FT_SNIPPET_NUMBER')) define('FT_SNIPPET_NUMBER',15); 15f5eb7cf0SAndreas Gohr 16f5eb7cf0SAndreas Gohr/** 17f5eb7cf0SAndreas Gohr * The fulltext search 18f5eb7cf0SAndreas Gohr * 19f5eb7cf0SAndreas Gohr * Returns a list of matching documents for the given query 20506fa893SAndreas Gohr * 216840140fSChris Smith * refactored into ft_pageSearch(), _ft_pageSearch() and trigger_event() 226840140fSChris Smith * 23f5eb7cf0SAndreas Gohr */ 24546d3a99SAndreas Gohrfunction ft_pageSearch($query,&$highlight){ 256840140fSChris Smith 26*59bc3b48SGerrit Uitslag $data = array(); 276840140fSChris Smith $data['query'] = $query; 286840140fSChris Smith $data['highlight'] =& $highlight; 296840140fSChris Smith 306840140fSChris Smith return trigger_event('SEARCH_QUERY_FULLPAGE', $data, '_ft_pageSearch'); 316840140fSChris Smith} 32865c2687SKazutaka Miyasaka 33865c2687SKazutaka Miyasaka/** 34865c2687SKazutaka Miyasaka * Returns a list of matching documents for the given query 35865c2687SKazutaka Miyasaka * 36865c2687SKazutaka Miyasaka * @author Andreas Gohr <andi@splitbrain.org> 37865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 38865c2687SKazutaka Miyasaka */ 396840140fSChris Smithfunction _ft_pageSearch(&$data) { 409b41be24STom N Harris $Indexer = idx_get_indexer(); 419b41be24STom N Harris 42865c2687SKazutaka Miyasaka // parse the given query 439b41be24STom N Harris $q = ft_queryParser($Indexer, $data['query']); 44865c2687SKazutaka Miyasaka $data['highlight'] = $q['highlight']; 456840140fSChris Smith 46865c2687SKazutaka Miyasaka if (empty($q['parsed_ary'])) return array(); 47506fa893SAndreas Gohr 48f5eb7cf0SAndreas Gohr // lookup all words found in the query 499b41be24STom N Harris $lookup = $Indexer->lookup($q['words']); 50f5eb7cf0SAndreas Gohr 51865c2687SKazutaka Miyasaka // get all pages in this dokuwiki site (!: includes nonexistent pages) 52865c2687SKazutaka Miyasaka $pages_all = array(); 539b41be24STom N Harris foreach ($Indexer->getPages() as $id) { 549b41be24STom N Harris $pages_all[$id] = 0; // base: 0 hit 55f5eb7cf0SAndreas Gohr } 56f5eb7cf0SAndreas Gohr 57865c2687SKazutaka Miyasaka // process the query 58865c2687SKazutaka Miyasaka $stack = array(); 59865c2687SKazutaka Miyasaka foreach ($q['parsed_ary'] as $token) { 60865c2687SKazutaka Miyasaka switch (substr($token, 0, 3)) { 61865c2687SKazutaka Miyasaka case 'W+:': 622f502d70SKazutaka Miyasaka case 'W-:': 632f502d70SKazutaka Miyasaka case 'W_:': // word 64865c2687SKazutaka Miyasaka $word = substr($token, 3); 65865c2687SKazutaka Miyasaka $stack[] = (array) $lookup[$word]; 66865c2687SKazutaka Miyasaka break; 672f502d70SKazutaka Miyasaka case 'P+:': 682f502d70SKazutaka Miyasaka case 'P-:': // phrase 69865c2687SKazutaka Miyasaka $phrase = substr($token, 3); 70865c2687SKazutaka Miyasaka // since phrases are always parsed as ((W1)(W2)...(P)), 71865c2687SKazutaka Miyasaka // the end($stack) always points the pages that contain 72865c2687SKazutaka Miyasaka // all words in this phrase 73865c2687SKazutaka Miyasaka $pages = end($stack); 74865c2687SKazutaka Miyasaka $pages_matched = array(); 75865c2687SKazutaka Miyasaka foreach(array_keys($pages) as $id){ 76a7e8b43eSMichael Hamann $evdata = array( 77a7e8b43eSMichael Hamann 'id' => $id, 78a7e8b43eSMichael Hamann 'phrase' => $phrase, 79a7e8b43eSMichael Hamann 'text' => rawWiki($id) 80a7e8b43eSMichael Hamann ); 81a7e8b43eSMichael Hamann $evt = new Doku_Event('FULLTEXT_PHRASE_MATCH',$evdata); 82a7e8b43eSMichael Hamann if ($evt->advise_before() && $evt->result !== true) { 83a7e8b43eSMichael Hamann $text = utf8_strtolower($evdata['text']); 84865c2687SKazutaka Miyasaka if (strpos($text, $phrase) !== false) { 85a7e8b43eSMichael Hamann $evt->result = true; 86a7e8b43eSMichael Hamann } 87a7e8b43eSMichael Hamann } 88a7e8b43eSMichael Hamann $evt->advise_after(); 89a7e8b43eSMichael Hamann if ($evt->result === true) { 90865c2687SKazutaka Miyasaka $pages_matched[$id] = 0; // phrase: always 0 hit 91865c2687SKazutaka Miyasaka } 92865c2687SKazutaka Miyasaka } 93865c2687SKazutaka Miyasaka $stack[] = $pages_matched; 94865c2687SKazutaka Miyasaka break; 952f502d70SKazutaka Miyasaka case 'N+:': 962f502d70SKazutaka Miyasaka case 'N-:': // namespace 97865c2687SKazutaka Miyasaka $ns = substr($token, 3); 98865c2687SKazutaka Miyasaka $pages_matched = array(); 99865c2687SKazutaka Miyasaka foreach (array_keys($pages_all) as $id) { 100865c2687SKazutaka Miyasaka if (strpos($id, $ns) === 0) { 101865c2687SKazutaka Miyasaka $pages_matched[$id] = 0; // namespace: always 0 hit 102865c2687SKazutaka Miyasaka } 103865c2687SKazutaka Miyasaka } 104865c2687SKazutaka Miyasaka $stack[] = $pages_matched; 105865c2687SKazutaka Miyasaka break; 106865c2687SKazutaka Miyasaka case 'AND': // and operation 107865c2687SKazutaka Miyasaka list($pages1, $pages2) = array_splice($stack, -2); 108865c2687SKazutaka Miyasaka $stack[] = ft_resultCombine(array($pages1, $pages2)); 109865c2687SKazutaka Miyasaka break; 110865c2687SKazutaka Miyasaka case 'OR': // or operation 111865c2687SKazutaka Miyasaka list($pages1, $pages2) = array_splice($stack, -2); 112865c2687SKazutaka Miyasaka $stack[] = ft_resultUnite(array($pages1, $pages2)); 113865c2687SKazutaka Miyasaka break; 114865c2687SKazutaka Miyasaka case 'NOT': // not operation (unary) 115865c2687SKazutaka Miyasaka $pages = array_pop($stack); 116865c2687SKazutaka Miyasaka $stack[] = ft_resultComplement(array($pages_all, $pages)); 117a21136cdSAndreas Gohr break; 118a21136cdSAndreas Gohr } 119f5eb7cf0SAndreas Gohr } 120865c2687SKazutaka Miyasaka $docs = array_pop($stack); 121865c2687SKazutaka Miyasaka 122865c2687SKazutaka Miyasaka if (empty($docs)) return array(); 123865c2687SKazutaka Miyasaka 124865c2687SKazutaka Miyasaka // check: settings, acls, existence 125865c2687SKazutaka Miyasaka foreach (array_keys($docs) as $id) { 126865c2687SKazutaka Miyasaka if (isHiddenPage($id) || auth_quickaclcheck($id) < AUTH_READ || !page_exists($id, '', false)) { 127865c2687SKazutaka Miyasaka unset($docs[$id]); 128f5eb7cf0SAndreas Gohr } 129f5eb7cf0SAndreas Gohr } 130f5eb7cf0SAndreas Gohr 131865c2687SKazutaka Miyasaka // sort docs by count 132f5eb7cf0SAndreas Gohr arsort($docs); 133f5eb7cf0SAndreas Gohr 134f5eb7cf0SAndreas Gohr return $docs; 135f5eb7cf0SAndreas Gohr} 136f5eb7cf0SAndreas Gohr 137f5eb7cf0SAndreas Gohr/** 13854f4c056SAndreas Gohr * Returns the backlinks for a given page 13954f4c056SAndreas Gohr * 140320f489aSMichael Hamann * Uses the metadata index. 14107ff0babSMichael Hamann * 14207ff0babSMichael Hamann * @param string $id The id for which links shall be returned 14307ff0babSMichael Hamann * @param bool $ignore_perms Ignore the fact that pages are hidden or read-protected 14407ff0babSMichael Hamann * @return array The pages that contain links to the given page 14554f4c056SAndreas Gohr */ 14607ff0babSMichael Hamannfunction ft_backlinks($id, $ignore_perms = false){ 147320f489aSMichael Hamann $result = idx_get_indexer()->lookupKey('relation_references', $id); 14854f4c056SAndreas Gohr 14963773904SAndreas Gohr if(!count($result)) return $result; 15063773904SAndreas Gohr 15163773904SAndreas Gohr // check ACL permissions 15263773904SAndreas Gohr foreach(array_keys($result) as $idx){ 15307ff0babSMichael Hamann if(($ignore_perms !== true && ( 15407ff0babSMichael Hamann isHiddenPage($result[$idx]) || auth_quickaclcheck($result[$idx]) < AUTH_READ 15507ff0babSMichael Hamann )) || !page_exists($result[$idx], '', false)){ 15663773904SAndreas Gohr unset($result[$idx]); 15763773904SAndreas Gohr } 15863773904SAndreas Gohr } 15963773904SAndreas Gohr 16054f4c056SAndreas Gohr sort($result); 16154f4c056SAndreas Gohr return $result; 16254f4c056SAndreas Gohr} 16354f4c056SAndreas Gohr 16454f4c056SAndreas Gohr/** 165a05e297aSAndreas Gohr * Returns the pages that use a given media file 166a05e297aSAndreas Gohr * 167ffec1009SMichael Hamann * Uses the relation media metadata property and the metadata index. 168a05e297aSAndreas Gohr * 169ffec1009SMichael Hamann * Note that before 2013-07-31 the second parameter was the maximum number of results and 170ffec1009SMichael Hamann * permissions were ignored. That's why the parameter is now checked to be explicitely set 171ffec1009SMichael Hamann * to true (with type bool) in order to be compatible with older uses of the function. 172ffec1009SMichael Hamann * 173ffec1009SMichael Hamann * @param string $id The media id to look for 174ffec1009SMichael Hamann * @param bool $ignore_perms Ignore hidden pages and acls (optional, default: false) 175ffec1009SMichael Hamann * @return array A list of pages that use the given media file 176a05e297aSAndreas Gohr */ 177ffec1009SMichael Hamannfunction ft_mediause($id, $ignore_perms = false){ 178ffec1009SMichael Hamann $result = idx_get_indexer()->lookupKey('relation_media', $id); 179a05e297aSAndreas Gohr 180ffec1009SMichael Hamann if(!count($result)) return $result; 181a05e297aSAndreas Gohr 182ffec1009SMichael Hamann // check ACL permissions 183ffec1009SMichael Hamann foreach(array_keys($result) as $idx){ 184ffec1009SMichael Hamann if(($ignore_perms !== true && ( 185ffec1009SMichael Hamann isHiddenPage($result[$idx]) || auth_quickaclcheck($result[$idx]) < AUTH_READ 186ffec1009SMichael Hamann )) || !page_exists($result[$idx], '', false)){ 187ffec1009SMichael Hamann unset($result[$idx]); 188a05e297aSAndreas Gohr } 189a05e297aSAndreas Gohr } 190a05e297aSAndreas Gohr 191a05e297aSAndreas Gohr sort($result); 192a05e297aSAndreas Gohr return $result; 193a05e297aSAndreas Gohr} 194a05e297aSAndreas Gohr 195a05e297aSAndreas Gohr 196a05e297aSAndreas Gohr 197a05e297aSAndreas Gohr/** 198506fa893SAndreas Gohr * Quicksearch for pagenames 199506fa893SAndreas Gohr * 200506fa893SAndreas Gohr * By default it only matches the pagename and ignores the 20180423ab6SAdrian Lang * namespace. This can be changed with the second parameter. 20280423ab6SAdrian Lang * The third parameter allows to search in titles as well. 203506fa893SAndreas Gohr * 2048d22f1e9SAndreas Gohr * The function always returns titles as well 2056840140fSChris Smith * 2068d22f1e9SAndreas Gohr * @triggers SEARCH_QUERY_PAGELOOKUP 207506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2088d22f1e9SAndreas Gohr * @author Adrian Lang <lang@cosmocode.de> 209506fa893SAndreas Gohr */ 2108d22f1e9SAndreas Gohrfunction ft_pageLookup($id, $in_ns=false, $in_title=false){ 2118d22f1e9SAndreas Gohr $data = compact('id', 'in_ns', 'in_title'); 2128d22f1e9SAndreas Gohr $data['has_titles'] = true; // for plugin backward compatibility check 2136840140fSChris Smith return trigger_event('SEARCH_QUERY_PAGELOOKUP', $data, '_ft_pageLookup'); 2146840140fSChris Smith} 2156840140fSChris Smith 2166840140fSChris Smithfunction _ft_pageLookup(&$data){ 21780423ab6SAdrian Lang // split out original parameters 2186840140fSChris Smith $id = $data['id']; 219ecebd72fSGerrit Uitslag if (preg_match('/(?:^| )(?:@|ns:)([\w:]+)/', $id, $matches)) { 220b0f6db0cSAdrian Lang $ns = cleanID($matches[1]) . ':'; 221b0f6db0cSAdrian Lang $id = str_replace($matches[0], '', $id); 222b0f6db0cSAdrian Lang } 223b0f6db0cSAdrian Lang 2248d22f1e9SAndreas Gohr $in_ns = $data['in_ns']; 2258d22f1e9SAndreas Gohr $in_title = $data['in_title']; 22680423ab6SAdrian Lang $cleaned = cleanID($id); 2279b41be24STom N Harris 2289b41be24STom N Harris $Indexer = idx_get_indexer(); 2299b41be24STom N Harris $page_idx = $Indexer->getPages(); 2309b41be24STom N Harris 2319b41be24STom N Harris $pages = array(); 2325479a8c3SAndreas Gohr if ($id !== '' && $cleaned !== '') { 2339b41be24STom N Harris foreach ($page_idx as $p_id) { 2349b41be24STom N Harris if ((strpos($in_ns ? $p_id : noNSorNS($p_id), $cleaned) !== false)) { 2359b41be24STom N Harris if (!isset($pages[$p_id])) 23667c15eceSMichael Hamann $pages[$p_id] = p_get_first_heading($p_id, METADATA_DONT_RENDER); 237506fa893SAndreas Gohr } 238506fa893SAndreas Gohr } 239f078bb00STom N Harris if ($in_title) { 240c66f16a3SMichael Hamann foreach ($Indexer->lookupKey('title', $id, '_ft_pageLookupTitleCompare') as $p_id) { 241f078bb00STom N Harris if (!isset($pages[$p_id])) 24267c15eceSMichael Hamann $pages[$p_id] = p_get_first_heading($p_id, METADATA_DONT_RENDER); 243f078bb00STom N Harris } 244f078bb00STom N Harris } 245d0bdf765SAdrian Lang } 2460c074a52SMichael Hamann 247d0bdf765SAdrian Lang if (isset($ns)) { 2480c074a52SMichael Hamann foreach (array_keys($pages) as $p_id) { 2490c074a52SMichael Hamann if (strpos($p_id, $ns) !== 0) { 2500c074a52SMichael Hamann unset($pages[$p_id]); 251d0bdf765SAdrian Lang } 252d0bdf765SAdrian Lang } 253506fa893SAndreas Gohr } 25463773904SAndreas Gohr 25580423ab6SAdrian Lang // discard hidden pages 25680423ab6SAdrian Lang // discard nonexistent pages 25763773904SAndreas Gohr // check ACL permissions 25863773904SAndreas Gohr foreach(array_keys($pages) as $idx){ 25980423ab6SAdrian Lang if(!isVisiblePage($idx) || !page_exists($idx) || 26080423ab6SAdrian Lang auth_quickaclcheck($idx) < AUTH_READ) { 26163773904SAndreas Gohr unset($pages[$idx]); 26263773904SAndreas Gohr } 26363773904SAndreas Gohr } 26463773904SAndreas Gohr 2653d2017d9SAdrian Lang uksort($pages,'ft_pagesorter'); 2668d22f1e9SAndreas Gohr return $pages; 267506fa893SAndreas Gohr} 268506fa893SAndreas Gohr 269506fa893SAndreas Gohr/** 270c66f16a3SMichael Hamann * Tiny helper function for comparing the searched title with the title 271c66f16a3SMichael Hamann * from the search index. This function is a wrapper around stripos with 272c66f16a3SMichael Hamann * adapted argument order and return value. 273c66f16a3SMichael Hamann */ 274c66f16a3SMichael Hamannfunction _ft_pageLookupTitleCompare($search, $title) { 275c66f16a3SMichael Hamann return stripos($title, $search) !== false; 276c66f16a3SMichael Hamann} 277c66f16a3SMichael Hamann 278c66f16a3SMichael Hamann/** 279f31eb72bSAndreas Gohr * Sort pages based on their namespace level first, then on their string 280f31eb72bSAndreas Gohr * values. This makes higher hierarchy pages rank higher than lower hierarchy 281f31eb72bSAndreas Gohr * pages. 282f31eb72bSAndreas Gohr */ 283f31eb72bSAndreas Gohrfunction ft_pagesorter($a, $b){ 284f31eb72bSAndreas Gohr $ac = count(explode(':',$a)); 285f31eb72bSAndreas Gohr $bc = count(explode(':',$b)); 286f31eb72bSAndreas Gohr if($ac < $bc){ 287f31eb72bSAndreas Gohr return -1; 288f31eb72bSAndreas Gohr }elseif($ac > $bc){ 289f31eb72bSAndreas Gohr return 1; 290f31eb72bSAndreas Gohr } 291f31eb72bSAndreas Gohr return strcmp ($a,$b); 292f31eb72bSAndreas Gohr} 293f31eb72bSAndreas Gohr 294f31eb72bSAndreas Gohr/** 295506fa893SAndreas Gohr * Creates a snippet extract 296506fa893SAndreas Gohr * 297506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 29860e91a17SAndreas Gohr * @triggers FULLTEXT_SNIPPET_CREATE 299506fa893SAndreas Gohr */ 300546d3a99SAndreas Gohrfunction ft_snippet($id,$highlight){ 301506fa893SAndreas Gohr $text = rawWiki($id); 3024f0030ddSAndreas Gohr $text = str_replace("\xC2\xAD",'',$text); // remove soft-hyphens 30360e91a17SAndreas Gohr $evdata = array( 30460e91a17SAndreas Gohr 'id' => $id, 30560e91a17SAndreas Gohr 'text' => &$text, 30660e91a17SAndreas Gohr 'highlight' => &$highlight, 30760e91a17SAndreas Gohr 'snippet' => '', 30860e91a17SAndreas Gohr ); 30960e91a17SAndreas Gohr 31060e91a17SAndreas Gohr $evt = new Doku_Event('FULLTEXT_SNIPPET_CREATE',$evdata); 31160e91a17SAndreas Gohr if ($evt->advise_before()) { 312ced0762eSchris $match = array(); 313ced0762eSchris $snippets = array(); 3149ee93076Schris $utf8_offset = $offset = $end = 0; 315ced0762eSchris $len = utf8_strlen($text); 3169ee93076Schris 317546d3a99SAndreas Gohr // build a regexp from the phrases to highlight 3182237b4faSAndreas Gohr $re1 = '('.join('|',array_map('ft_snippet_re_preprocess', array_map('preg_quote_cb',array_filter((array) $highlight)))).')'; 319b571ff2dSChuck Kollars $re2 = "$re1.{0,75}(?!\\1)$re1"; 320b571ff2dSChuck Kollars $re3 = "$re1.{0,45}(?!\\1)$re1.{0,45}(?!\\1)(?!\\2)$re1"; 321546d3a99SAndreas Gohr 322b571ff2dSChuck Kollars for ($cnt=4; $cnt--;) { 323b571ff2dSChuck Kollars if (0) { 324b571ff2dSChuck Kollars } else if (preg_match('/'.$re3.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) { 325b571ff2dSChuck Kollars } else if (preg_match('/'.$re2.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) { 326b571ff2dSChuck Kollars } else if (preg_match('/'.$re1.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) { 327b571ff2dSChuck Kollars } else { 328b571ff2dSChuck Kollars break; 329b571ff2dSChuck Kollars } 330ced0762eSchris 331ced0762eSchris list($str,$idx) = $match[0]; 332ced0762eSchris 333ced0762eSchris // convert $idx (a byte offset) into a utf8 character offset 334ced0762eSchris $utf8_idx = utf8_strlen(substr($text,0,$idx)); 335ced0762eSchris $utf8_len = utf8_strlen($str); 336ced0762eSchris 337ced0762eSchris // establish context, 100 bytes surrounding the match string 338ced0762eSchris // first look to see if we can go 100 either side, 339ced0762eSchris // then drop to 50 adding any excess if the other side can't go to 50, 340ced0762eSchris $pre = min($utf8_idx-$utf8_offset,100); 341ced0762eSchris $post = min($len-$utf8_idx-$utf8_len,100); 342ced0762eSchris 343ced0762eSchris if ($pre>50 && $post>50) { 344ced0762eSchris $pre = $post = 50; 345ced0762eSchris } else if ($pre>50) { 346ced0762eSchris $pre = min($pre,100-$post); 347ced0762eSchris } else if ($post>50) { 348ced0762eSchris $post = min($post, 100-$pre); 349ef3e3cddSMichael Hamann } else if ($offset == 0) { 350ced0762eSchris // both are less than 50, means the context is the whole string 35110ffc9ddSAndreas Gohr // make it so and break out of this loop - there is no need for the 35210ffc9ddSAndreas Gohr // complex snippet calculations 353ced0762eSchris $snippets = array($text); 354ced0762eSchris break; 355ced0762eSchris } 356ced0762eSchris 35710ffc9ddSAndreas Gohr // establish context start and end points, try to append to previous 35810ffc9ddSAndreas Gohr // context if possible 3599ee93076Schris $start = $utf8_idx - $pre; 360ced0762eSchris $append = ($start < $end) ? $end : false; // still the end of the previous context snippet 3619ee93076Schris $end = $utf8_idx + $utf8_len + $post; // now set it to the end of this context 362ced0762eSchris 363ced0762eSchris if ($append) { 364ced0762eSchris $snippets[count($snippets)-1] .= utf8_substr($text,$append,$end-$append); 365ced0762eSchris } else { 366ced0762eSchris $snippets[] = utf8_substr($text,$start,$end-$start); 367ced0762eSchris } 368ced0762eSchris 369ced0762eSchris // set $offset for next match attempt 37043d58b76SMichael Hamann // continue matching after the current match 37143d58b76SMichael Hamann // if the current match is not the longest possible match starting at the current offset 37243d58b76SMichael Hamann // this prevents further matching of this snippet but for possible matches of length 37343d58b76SMichael Hamann // smaller than match length + context (at least 50 characters) this match is part of the context 37443d58b76SMichael Hamann $utf8_offset = $utf8_idx + $utf8_len; 37543d58b76SMichael Hamann $offset = $idx + strlen(utf8_substr($text,$utf8_idx,$utf8_len)); 3769ee93076Schris $offset = utf8_correctIdx($text,$offset); 3779ee93076Schris } 3789ee93076Schris 379ced0762eSchris $m = "\1"; 380b571ff2dSChuck Kollars $snippets = preg_replace('/'.$re1.'/iu',$m.'$1'.$m,$snippets); 381b571ff2dSChuck Kollars $snippet = preg_replace('/'.$m.'([^'.$m.']*?)'.$m.'/iu','<strong class="search_hit">$1</strong>',hsc(join('... ',$snippets))); 382bd2cb6fcSchris 38360e91a17SAndreas Gohr $evdata['snippet'] = $snippet; 38460e91a17SAndreas Gohr } 38560e91a17SAndreas Gohr $evt->advise_after(); 38660e91a17SAndreas Gohr unset($evt); 38760e91a17SAndreas Gohr 38860e91a17SAndreas Gohr return $evdata['snippet']; 389506fa893SAndreas Gohr} 390506fa893SAndreas Gohr 391506fa893SAndreas Gohr/** 39226eb848cSGina Haeussge * Wraps a search term in regex boundary checks. 39326eb848cSGina Haeussge */ 3942237b4faSAndreas Gohrfunction ft_snippet_re_preprocess($term) { 39535594613SKazutaka Miyasaka // do not process asian terms where word boundaries are not explicit 39635594613SKazutaka Miyasaka if(preg_match('/'.IDX_ASIAN.'/u',$term)){ 39735594613SKazutaka Miyasaka return $term; 39835594613SKazutaka Miyasaka } 39935594613SKazutaka Miyasaka 4003161005dSAndreas Gohr if (UTF8_PROPERTYSUPPORT) { 40184e581a6SAndreas Gohr // unicode word boundaries 40284e581a6SAndreas Gohr // see http://stackoverflow.com/a/2449017/172068 40384e581a6SAndreas Gohr $BL = '(?<!\pL)'; 40484e581a6SAndreas Gohr $BR = '(?!\pL)'; 4053161005dSAndreas Gohr } else { 4063161005dSAndreas Gohr // not as correct as above, but at least won't break 4073161005dSAndreas Gohr $BL = '\b'; 4083161005dSAndreas Gohr $BR = '\b'; 4093161005dSAndreas Gohr } 4103161005dSAndreas Gohr 4112237b4faSAndreas Gohr if(substr($term,0,2) == '\\*'){ 4122237b4faSAndreas Gohr $term = substr($term,2); 4132237b4faSAndreas Gohr }else{ 41484e581a6SAndreas Gohr $term = $BL.$term; 4152237b4faSAndreas Gohr } 4162237b4faSAndreas Gohr 4172237b4faSAndreas Gohr if(substr($term,-2,2) == '\\*'){ 4182237b4faSAndreas Gohr $term = substr($term,0,-2); 4192237b4faSAndreas Gohr }else{ 42084e581a6SAndreas Gohr $term = $term.$BR; 4212237b4faSAndreas Gohr } 4228a803caeSAndreas Gohr 42384e581a6SAndreas Gohr if($term == $BL || $term == $BR || $term == $BL.$BR) $term = ''; 4242237b4faSAndreas Gohr return $term; 42526eb848cSGina Haeussge} 42626eb848cSGina Haeussge 42726eb848cSGina Haeussge/** 428f5eb7cf0SAndreas Gohr * Combine found documents and sum up their scores 429f5eb7cf0SAndreas Gohr * 430f5eb7cf0SAndreas Gohr * This function is used to combine searched words with a logical 431f5eb7cf0SAndreas Gohr * AND. Only documents available in all arrays are returned. 432f5eb7cf0SAndreas Gohr * 433f5eb7cf0SAndreas Gohr * based upon PEAR's PHP_Compat function for array_intersect_key() 434f5eb7cf0SAndreas Gohr * 435f5eb7cf0SAndreas Gohr * @param array $args An array of page arrays 436f5eb7cf0SAndreas Gohr */ 437f5eb7cf0SAndreas Gohrfunction ft_resultCombine($args){ 438f5eb7cf0SAndreas Gohr $array_count = count($args); 439134f4ab2SAndreas Gohr if($array_count == 1){ 440134f4ab2SAndreas Gohr return $args[0]; 441134f4ab2SAndreas Gohr } 442134f4ab2SAndreas Gohr 443f5eb7cf0SAndreas Gohr $result = array(); 44409c27a6dSGuy Brand if ($array_count > 1) { 445a21136cdSAndreas Gohr foreach ($args[0] as $key => $value) { 446a21136cdSAndreas Gohr $result[$key] = $value; 447f5eb7cf0SAndreas Gohr for ($i = 1; $i !== $array_count; $i++) { 448a21136cdSAndreas Gohr if (!isset($args[$i][$key])) { 449a21136cdSAndreas Gohr unset($result[$key]); 450a21136cdSAndreas Gohr break; 451f5eb7cf0SAndreas Gohr } 452a21136cdSAndreas Gohr $result[$key] += $args[$i][$key]; 453f5eb7cf0SAndreas Gohr } 454f5eb7cf0SAndreas Gohr } 45509c27a6dSGuy Brand } 456f5eb7cf0SAndreas Gohr return $result; 457f5eb7cf0SAndreas Gohr} 458f5eb7cf0SAndreas Gohr 459f5eb7cf0SAndreas Gohr/** 460865c2687SKazutaka Miyasaka * Unites found documents and sum up their scores 461f5eb7cf0SAndreas Gohr * 462865c2687SKazutaka Miyasaka * based upon ft_resultCombine() function 463865c2687SKazutaka Miyasaka * 464865c2687SKazutaka Miyasaka * @param array $args An array of page arrays 465865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 466865c2687SKazutaka Miyasaka */ 467865c2687SKazutaka Miyasakafunction ft_resultUnite($args) { 468865c2687SKazutaka Miyasaka $array_count = count($args); 469865c2687SKazutaka Miyasaka if ($array_count === 1) { 470865c2687SKazutaka Miyasaka return $args[0]; 471865c2687SKazutaka Miyasaka } 472865c2687SKazutaka Miyasaka 473865c2687SKazutaka Miyasaka $result = $args[0]; 474865c2687SKazutaka Miyasaka for ($i = 1; $i !== $array_count; $i++) { 475865c2687SKazutaka Miyasaka foreach (array_keys($args[$i]) as $id) { 476865c2687SKazutaka Miyasaka $result[$id] += $args[$i][$id]; 477865c2687SKazutaka Miyasaka } 478865c2687SKazutaka Miyasaka } 479865c2687SKazutaka Miyasaka return $result; 480865c2687SKazutaka Miyasaka} 481865c2687SKazutaka Miyasaka 482865c2687SKazutaka Miyasaka/** 483865c2687SKazutaka Miyasaka * Computes the difference of documents using page id for comparison 484865c2687SKazutaka Miyasaka * 485865c2687SKazutaka Miyasaka * nearly identical to PHP5's array_diff_key() 486865c2687SKazutaka Miyasaka * 487865c2687SKazutaka Miyasaka * @param array $args An array of page arrays 488865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 489865c2687SKazutaka Miyasaka */ 490865c2687SKazutaka Miyasakafunction ft_resultComplement($args) { 491865c2687SKazutaka Miyasaka $array_count = count($args); 492865c2687SKazutaka Miyasaka if ($array_count === 1) { 493865c2687SKazutaka Miyasaka return $args[0]; 494865c2687SKazutaka Miyasaka } 495865c2687SKazutaka Miyasaka 496865c2687SKazutaka Miyasaka $result = $args[0]; 497865c2687SKazutaka Miyasaka foreach (array_keys($result) as $id) { 498865c2687SKazutaka Miyasaka for ($i = 1; $i !== $array_count; $i++) { 499865c2687SKazutaka Miyasaka if (isset($args[$i][$id])) unset($result[$id]); 500865c2687SKazutaka Miyasaka } 501865c2687SKazutaka Miyasaka } 502865c2687SKazutaka Miyasaka return $result; 503865c2687SKazutaka Miyasaka} 504865c2687SKazutaka Miyasaka 505865c2687SKazutaka Miyasaka/** 506865c2687SKazutaka Miyasaka * Parses a search query and builds an array of search formulas 507865c2687SKazutaka Miyasaka * 508865c2687SKazutaka Miyasaka * @author Andreas Gohr <andi@splitbrain.org> 509865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 510f5eb7cf0SAndreas Gohr */ 5119b41be24STom N Harrisfunction ft_queryParser($Indexer, $query){ 512865c2687SKazutaka Miyasaka /** 513865c2687SKazutaka Miyasaka * parse a search query and transform it into intermediate representation 514865c2687SKazutaka Miyasaka * 515865c2687SKazutaka Miyasaka * in a search query, you can use the following expressions: 516865c2687SKazutaka Miyasaka * 517865c2687SKazutaka Miyasaka * words: 518865c2687SKazutaka Miyasaka * include 519865c2687SKazutaka Miyasaka * -exclude 520865c2687SKazutaka Miyasaka * phrases: 521865c2687SKazutaka Miyasaka * "phrase to be included" 522865c2687SKazutaka Miyasaka * -"phrase you want to exclude" 523865c2687SKazutaka Miyasaka * namespaces: 524865c2687SKazutaka Miyasaka * @include:namespace (or ns:include:namespace) 525865c2687SKazutaka Miyasaka * ^exclude:namespace (or -ns:exclude:namespace) 526865c2687SKazutaka Miyasaka * groups: 527865c2687SKazutaka Miyasaka * () 528865c2687SKazutaka Miyasaka * -() 529865c2687SKazutaka Miyasaka * operators: 530865c2687SKazutaka Miyasaka * and ('and' is the default operator: you can always omit this) 5317871d415SKazutaka Miyasaka * or (or pipe symbol '|', lower precedence than 'and') 532865c2687SKazutaka Miyasaka * 533865c2687SKazutaka Miyasaka * e.g. a query [ aa "bb cc" @dd:ee ] means "search pages which contain 534865c2687SKazutaka Miyasaka * a word 'aa', a phrase 'bb cc' and are within a namespace 'dd:ee'". 535865c2687SKazutaka Miyasaka * this query is equivalent to [ -(-aa or -"bb cc" or -ns:dd:ee) ] 536865c2687SKazutaka Miyasaka * as long as you don't mind hit counts. 537865c2687SKazutaka Miyasaka * 538865c2687SKazutaka Miyasaka * intermediate representation consists of the following parts: 539865c2687SKazutaka Miyasaka * 540865c2687SKazutaka Miyasaka * ( ) - group 541865c2687SKazutaka Miyasaka * AND - logical and 542865c2687SKazutaka Miyasaka * OR - logical or 543865c2687SKazutaka Miyasaka * NOT - logical not 5442f502d70SKazutaka Miyasaka * W+:, W-:, W_: - word (underscore: no need to highlight) 5452f502d70SKazutaka Miyasaka * P+:, P-: - phrase (minus sign: logically in NOT group) 5462f502d70SKazutaka Miyasaka * N+:, N-: - namespace 547865c2687SKazutaka Miyasaka */ 548865c2687SKazutaka Miyasaka $parsed_query = ''; 549865c2687SKazutaka Miyasaka $parens_level = 0; 550865c2687SKazutaka Miyasaka $terms = preg_split('/(-?".*?")/u', utf8_strtolower($query), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 551865c2687SKazutaka Miyasaka 552865c2687SKazutaka Miyasaka foreach ($terms as $term) { 553865c2687SKazutaka Miyasaka $parsed = ''; 554865c2687SKazutaka Miyasaka if (preg_match('/^(-?)"(.+)"$/u', $term, $matches)) { 555865c2687SKazutaka Miyasaka // phrase-include and phrase-exclude 556865c2687SKazutaka Miyasaka $not = $matches[1] ? 'NOT' : ''; 5579b41be24STom N Harris $parsed = $not.ft_termParser($Indexer, $matches[2], false, true); 558f5eb7cf0SAndreas Gohr } else { 559865c2687SKazutaka Miyasaka // fix incomplete phrase 560865c2687SKazutaka Miyasaka $term = str_replace('"', ' ', $term); 561865c2687SKazutaka Miyasaka 562865c2687SKazutaka Miyasaka // fix parentheses 563865c2687SKazutaka Miyasaka $term = str_replace(')' , ' ) ', $term); 564865c2687SKazutaka Miyasaka $term = str_replace('(' , ' ( ', $term); 565865c2687SKazutaka Miyasaka $term = str_replace('- (', ' -(', $term); 566865c2687SKazutaka Miyasaka 5677871d415SKazutaka Miyasaka // treat pipe symbols as 'OR' operators 5687871d415SKazutaka Miyasaka $term = str_replace('|', ' or ', $term); 5697871d415SKazutaka Miyasaka 570865c2687SKazutaka Miyasaka // treat ideographic spaces (U+3000) as search term separators 571865c2687SKazutaka Miyasaka // FIXME: some more separators? 572865c2687SKazutaka Miyasaka $term = preg_replace('/[ \x{3000}]+/u', ' ', $term); 573865c2687SKazutaka Miyasaka $term = trim($term); 574865c2687SKazutaka Miyasaka if ($term === '') continue; 575865c2687SKazutaka Miyasaka 576865c2687SKazutaka Miyasaka $tokens = explode(' ', $term); 577865c2687SKazutaka Miyasaka foreach ($tokens as $token) { 578865c2687SKazutaka Miyasaka if ($token === '(') { 579865c2687SKazutaka Miyasaka // parenthesis-include-open 580865c2687SKazutaka Miyasaka $parsed .= '('; 581865c2687SKazutaka Miyasaka ++$parens_level; 582865c2687SKazutaka Miyasaka } elseif ($token === '-(') { 583865c2687SKazutaka Miyasaka // parenthesis-exclude-open 584865c2687SKazutaka Miyasaka $parsed .= 'NOT('; 585865c2687SKazutaka Miyasaka ++$parens_level; 586865c2687SKazutaka Miyasaka } elseif ($token === ')') { 587865c2687SKazutaka Miyasaka // parenthesis-any-close 588865c2687SKazutaka Miyasaka if ($parens_level === 0) continue; 589865c2687SKazutaka Miyasaka $parsed .= ')'; 590865c2687SKazutaka Miyasaka $parens_level--; 591865c2687SKazutaka Miyasaka } elseif ($token === 'and') { 592865c2687SKazutaka Miyasaka // logical-and (do nothing) 593865c2687SKazutaka Miyasaka } elseif ($token === 'or') { 594865c2687SKazutaka Miyasaka // logical-or 595865c2687SKazutaka Miyasaka $parsed .= 'OR'; 596865c2687SKazutaka Miyasaka } elseif (preg_match('/^(?:\^|-ns:)(.+)$/u', $token, $matches)) { 597865c2687SKazutaka Miyasaka // namespace-exclude 5982f502d70SKazutaka Miyasaka $parsed .= 'NOT(N+:'.$matches[1].')'; 599865c2687SKazutaka Miyasaka } elseif (preg_match('/^(?:@|ns:)(.+)$/u', $token, $matches)) { 600865c2687SKazutaka Miyasaka // namespace-include 6012f502d70SKazutaka Miyasaka $parsed .= '(N+:'.$matches[1].')'; 602865c2687SKazutaka Miyasaka } elseif (preg_match('/^-(.+)$/', $token, $matches)) { 603865c2687SKazutaka Miyasaka // word-exclude 6049b41be24STom N Harris $parsed .= 'NOT('.ft_termParser($Indexer, $matches[1]).')'; 605865c2687SKazutaka Miyasaka } else { 606865c2687SKazutaka Miyasaka // word-include 6079b41be24STom N Harris $parsed .= ft_termParser($Indexer, $token); 608865c2687SKazutaka Miyasaka } 609865c2687SKazutaka Miyasaka } 610865c2687SKazutaka Miyasaka } 611865c2687SKazutaka Miyasaka $parsed_query .= $parsed; 612f5eb7cf0SAndreas Gohr } 613f5eb7cf0SAndreas Gohr 614865c2687SKazutaka Miyasaka // cleanup (very sensitive) 615865c2687SKazutaka Miyasaka $parsed_query .= str_repeat(')', $parens_level); 616865c2687SKazutaka Miyasaka do { 617865c2687SKazutaka Miyasaka $parsed_query_old = $parsed_query; 618865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/(NOT)?\(\)/u', '', $parsed_query); 619865c2687SKazutaka Miyasaka } while ($parsed_query !== $parsed_query_old); 620865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/(NOT|OR)+\)/u', ')' , $parsed_query); 621865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/(OR)+/u' , 'OR' , $parsed_query); 622865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/\(OR/u' , '(' , $parsed_query); 623865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/^OR|OR$/u' , '' , $parsed_query); 624865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/\)(NOT)?\(/u' , ')AND$1(', $parsed_query); 625865c2687SKazutaka Miyasaka 6262f502d70SKazutaka Miyasaka // adjustment: make highlightings right 6272f502d70SKazutaka Miyasaka $parens_level = 0; 6282f502d70SKazutaka Miyasaka $notgrp_levels = array(); 6292f502d70SKazutaka Miyasaka $parsed_query_new = ''; 6302f502d70SKazutaka Miyasaka $tokens = preg_split('/(NOT\(|[()])/u', $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 6312f502d70SKazutaka Miyasaka foreach ($tokens as $token) { 6322f502d70SKazutaka Miyasaka if ($token === 'NOT(') { 6332f502d70SKazutaka Miyasaka $notgrp_levels[] = ++$parens_level; 6342f502d70SKazutaka Miyasaka } elseif ($token === '(') { 6352f502d70SKazutaka Miyasaka ++$parens_level; 6362f502d70SKazutaka Miyasaka } elseif ($token === ')') { 6372f502d70SKazutaka Miyasaka if ($parens_level-- === end($notgrp_levels)) array_pop($notgrp_levels); 6382f502d70SKazutaka Miyasaka } elseif (count($notgrp_levels) % 2 === 1) { 6392f502d70SKazutaka Miyasaka // turn highlight-flag off if terms are logically in "NOT" group 6402f502d70SKazutaka Miyasaka $token = preg_replace('/([WPN])\+\:/u', '$1-:', $token); 6412f502d70SKazutaka Miyasaka } 6422f502d70SKazutaka Miyasaka $parsed_query_new .= $token; 6432f502d70SKazutaka Miyasaka } 6442f502d70SKazutaka Miyasaka $parsed_query = $parsed_query_new; 6452f502d70SKazutaka Miyasaka 646865c2687SKazutaka Miyasaka /** 647865c2687SKazutaka Miyasaka * convert infix notation string into postfix (Reverse Polish notation) array 648865c2687SKazutaka Miyasaka * by Shunting-yard algorithm 649865c2687SKazutaka Miyasaka * 650865c2687SKazutaka Miyasaka * see: http://en.wikipedia.org/wiki/Reverse_Polish_notation 651865c2687SKazutaka Miyasaka * see: http://en.wikipedia.org/wiki/Shunting-yard_algorithm 652865c2687SKazutaka Miyasaka */ 653865c2687SKazutaka Miyasaka $parsed_ary = array(); 654865c2687SKazutaka Miyasaka $ope_stack = array(); 655865c2687SKazutaka Miyasaka $ope_precedence = array(')' => 1, 'OR' => 2, 'AND' => 3, 'NOT' => 4, '(' => 5); 656865c2687SKazutaka Miyasaka $ope_regex = '/([()]|OR|AND|NOT)/u'; 657865c2687SKazutaka Miyasaka 658865c2687SKazutaka Miyasaka $tokens = preg_split($ope_regex, $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 659865c2687SKazutaka Miyasaka foreach ($tokens as $token) { 660865c2687SKazutaka Miyasaka if (preg_match($ope_regex, $token)) { 661865c2687SKazutaka Miyasaka // operator 662865c2687SKazutaka Miyasaka $last_ope = end($ope_stack); 663865c2687SKazutaka Miyasaka while ($ope_precedence[$token] <= $ope_precedence[$last_ope] && $last_ope != '(') { 664865c2687SKazutaka Miyasaka $parsed_ary[] = array_pop($ope_stack); 665865c2687SKazutaka Miyasaka $last_ope = end($ope_stack); 666865c2687SKazutaka Miyasaka } 667865c2687SKazutaka Miyasaka if ($token == ')') { 668865c2687SKazutaka Miyasaka array_pop($ope_stack); // this array_pop always deletes '(' 669865c2687SKazutaka Miyasaka } else { 670865c2687SKazutaka Miyasaka $ope_stack[] = $token; 671865c2687SKazutaka Miyasaka } 672865c2687SKazutaka Miyasaka } else { 673865c2687SKazutaka Miyasaka // operand 674865c2687SKazutaka Miyasaka $token_decoded = str_replace(array('OP', 'CP'), array('(', ')'), $token); 675865c2687SKazutaka Miyasaka $parsed_ary[] = $token_decoded; 676865c2687SKazutaka Miyasaka } 677865c2687SKazutaka Miyasaka } 678865c2687SKazutaka Miyasaka $parsed_ary = array_values(array_merge($parsed_ary, array_reverse($ope_stack))); 679865c2687SKazutaka Miyasaka 680865c2687SKazutaka Miyasaka // cleanup: each double "NOT" in RPN array actually does nothing 681865c2687SKazutaka Miyasaka $parsed_ary_count = count($parsed_ary); 682865c2687SKazutaka Miyasaka for ($i = 1; $i < $parsed_ary_count; ++$i) { 683865c2687SKazutaka Miyasaka if ($parsed_ary[$i] === 'NOT' && $parsed_ary[$i - 1] === 'NOT') { 684865c2687SKazutaka Miyasaka unset($parsed_ary[$i], $parsed_ary[$i - 1]); 685865c2687SKazutaka Miyasaka } 686865c2687SKazutaka Miyasaka } 687865c2687SKazutaka Miyasaka $parsed_ary = array_values($parsed_ary); 688865c2687SKazutaka Miyasaka 689865c2687SKazutaka Miyasaka // build return value 690f5eb7cf0SAndreas Gohr $q = array(); 691f5eb7cf0SAndreas Gohr $q['query'] = $query; 692865c2687SKazutaka Miyasaka $q['parsed_str'] = $parsed_query; 693865c2687SKazutaka Miyasaka $q['parsed_ary'] = $parsed_ary; 694f5eb7cf0SAndreas Gohr 695865c2687SKazutaka Miyasaka foreach ($q['parsed_ary'] as $token) { 696865c2687SKazutaka Miyasaka if ($token[2] !== ':') continue; 697865c2687SKazutaka Miyasaka $body = substr($token, 3); 698865c2687SKazutaka Miyasaka 699865c2687SKazutaka Miyasaka switch (substr($token, 0, 3)) { 7002f502d70SKazutaka Miyasaka case 'N+:': 701865c2687SKazutaka Miyasaka $q['ns'][] = $body; // for backward compatibility 702865c2687SKazutaka Miyasaka break; 7032f502d70SKazutaka Miyasaka case 'N-:': 7042f502d70SKazutaka Miyasaka $q['notns'][] = $body; // for backward compatibility 7052f502d70SKazutaka Miyasaka break; 7062f502d70SKazutaka Miyasaka case 'W_:': 7072f502d70SKazutaka Miyasaka $q['words'][] = $body; 7082f502d70SKazutaka Miyasaka break; 709865c2687SKazutaka Miyasaka case 'W-:': 710865c2687SKazutaka Miyasaka $q['words'][] = $body; 7112f502d70SKazutaka Miyasaka $q['not'][] = $body; // for backward compatibility 712865c2687SKazutaka Miyasaka break; 713865c2687SKazutaka Miyasaka case 'W+:': 714865c2687SKazutaka Miyasaka $q['words'][] = $body; 7152237b4faSAndreas Gohr $q['highlight'][] = $body; 7162f502d70SKazutaka Miyasaka $q['and'][] = $body; // for backward compatibility 717865c2687SKazutaka Miyasaka break; 7182f502d70SKazutaka Miyasaka case 'P-:': 7192f502d70SKazutaka Miyasaka $q['phrases'][] = $body; 7202f502d70SKazutaka Miyasaka break; 7212f502d70SKazutaka Miyasaka case 'P+:': 722865c2687SKazutaka Miyasaka $q['phrases'][] = $body; 7232237b4faSAndreas Gohr $q['highlight'][] = $body; 724865c2687SKazutaka Miyasaka break; 725865c2687SKazutaka Miyasaka } 726865c2687SKazutaka Miyasaka } 7272f502d70SKazutaka Miyasaka foreach (array('words', 'phrases', 'highlight', 'ns', 'notns', 'and', 'not') as $key) { 728865c2687SKazutaka Miyasaka $q[$key] = empty($q[$key]) ? array() : array_values(array_unique($q[$key])); 729f5eb7cf0SAndreas Gohr } 730f5eb7cf0SAndreas Gohr 731f5eb7cf0SAndreas Gohr return $q; 732f5eb7cf0SAndreas Gohr} 733f5eb7cf0SAndreas Gohr 734865c2687SKazutaka Miyasaka/** 735865c2687SKazutaka Miyasaka * Transforms given search term into intermediate representation 736865c2687SKazutaka Miyasaka * 737865c2687SKazutaka Miyasaka * This function is used in ft_queryParser() and not for general purpose use. 738865c2687SKazutaka Miyasaka * 739865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 740865c2687SKazutaka Miyasaka */ 7419b41be24STom N Harrisfunction ft_termParser($Indexer, $term, $consider_asian = true, $phrase_mode = false) { 742865c2687SKazutaka Miyasaka $parsed = ''; 743865c2687SKazutaka Miyasaka if ($consider_asian) { 744865c2687SKazutaka Miyasaka // successive asian characters need to be searched as a phrase 745865c2687SKazutaka Miyasaka $words = preg_split('/('.IDX_ASIAN.'+)/u', $term, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 746865c2687SKazutaka Miyasaka foreach ($words as $word) { 7476ac2077aSKazutaka Miyasaka $phrase_mode = $phrase_mode ? true : preg_match('/'.IDX_ASIAN.'/u', $word); 7489b41be24STom N Harris $parsed .= ft_termParser($Indexer, $word, false, $phrase_mode); 749865c2687SKazutaka Miyasaka } 750865c2687SKazutaka Miyasaka } else { 751865c2687SKazutaka Miyasaka $term_noparen = str_replace(array('(', ')'), ' ', $term); 7529b41be24STom N Harris $words = $Indexer->tokenizer($term_noparen, true); 753865c2687SKazutaka Miyasaka 7542f502d70SKazutaka Miyasaka // W_: no need to highlight 755865c2687SKazutaka Miyasaka if (empty($words)) { 756865c2687SKazutaka Miyasaka $parsed = '()'; // important: do not remove 757865c2687SKazutaka Miyasaka } elseif ($words[0] === $term) { 758865c2687SKazutaka Miyasaka $parsed = '(W+:'.$words[0].')'; 759865c2687SKazutaka Miyasaka } elseif ($phrase_mode) { 760865c2687SKazutaka Miyasaka $term_encoded = str_replace(array('(', ')'), array('OP', 'CP'), $term); 7612f502d70SKazutaka Miyasaka $parsed = '((W_:'.implode(')(W_:', $words).')(P+:'.$term_encoded.'))'; 762865c2687SKazutaka Miyasaka } else { 763865c2687SKazutaka Miyasaka $parsed = '((W+:'.implode(')(W+:', $words).'))'; 764865c2687SKazutaka Miyasaka } 765865c2687SKazutaka Miyasaka } 766865c2687SKazutaka Miyasaka return $parsed; 767865c2687SKazutaka Miyasaka} 768865c2687SKazutaka Miyasaka 769e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 770