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 266840140fSChris Smith $data['query'] = $query; 276840140fSChris Smith $data['highlight'] =& $highlight; 286840140fSChris Smith 296840140fSChris Smith return trigger_event('SEARCH_QUERY_FULLPAGE', $data, '_ft_pageSearch'); 306840140fSChris Smith} 31865c2687SKazutaka Miyasaka 32865c2687SKazutaka Miyasaka/** 33865c2687SKazutaka Miyasaka * Returns a list of matching documents for the given query 34865c2687SKazutaka Miyasaka * 35865c2687SKazutaka Miyasaka * @author Andreas Gohr <andi@splitbrain.org> 36865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 37865c2687SKazutaka Miyasaka */ 386840140fSChris Smithfunction _ft_pageSearch(&$data) { 399b41be24STom N Harris $Indexer = idx_get_indexer(); 409b41be24STom N Harris 41865c2687SKazutaka Miyasaka // parse the given query 429b41be24STom N Harris $q = ft_queryParser($Indexer, $data['query']); 43865c2687SKazutaka Miyasaka $data['highlight'] = $q['highlight']; 446840140fSChris Smith 45865c2687SKazutaka Miyasaka if (empty($q['parsed_ary'])) return array(); 46506fa893SAndreas Gohr 47f5eb7cf0SAndreas Gohr // lookup all words found in the query 489b41be24STom N Harris $lookup = $Indexer->lookup($q['words']); 49f5eb7cf0SAndreas Gohr 50865c2687SKazutaka Miyasaka // get all pages in this dokuwiki site (!: includes nonexistent pages) 51865c2687SKazutaka Miyasaka $pages_all = array(); 529b41be24STom N Harris foreach ($Indexer->getPages() as $id) { 539b41be24STom N Harris $pages_all[$id] = 0; // base: 0 hit 54f5eb7cf0SAndreas Gohr } 55f5eb7cf0SAndreas Gohr 56865c2687SKazutaka Miyasaka // process the query 57865c2687SKazutaka Miyasaka $stack = array(); 58865c2687SKazutaka Miyasaka foreach ($q['parsed_ary'] as $token) { 59865c2687SKazutaka Miyasaka switch (substr($token, 0, 3)) { 60865c2687SKazutaka Miyasaka case 'W+:': 612f502d70SKazutaka Miyasaka case 'W-:': 622f502d70SKazutaka Miyasaka case 'W_:': // word 63865c2687SKazutaka Miyasaka $word = substr($token, 3); 64865c2687SKazutaka Miyasaka $stack[] = (array) $lookup[$word]; 65865c2687SKazutaka Miyasaka break; 662f502d70SKazutaka Miyasaka case 'P+:': 672f502d70SKazutaka Miyasaka case 'P-:': // phrase 68865c2687SKazutaka Miyasaka $phrase = substr($token, 3); 69865c2687SKazutaka Miyasaka // since phrases are always parsed as ((W1)(W2)...(P)), 70865c2687SKazutaka Miyasaka // the end($stack) always points the pages that contain 71865c2687SKazutaka Miyasaka // all words in this phrase 72865c2687SKazutaka Miyasaka $pages = end($stack); 73865c2687SKazutaka Miyasaka $pages_matched = array(); 74865c2687SKazutaka Miyasaka foreach(array_keys($pages) as $id){ 75f5eb7cf0SAndreas Gohr $text = utf8_strtolower(rawWiki($id)); 76865c2687SKazutaka Miyasaka if (strpos($text, $phrase) !== false) { 77865c2687SKazutaka Miyasaka $pages_matched[$id] = 0; // phrase: always 0 hit 78865c2687SKazutaka Miyasaka } 79865c2687SKazutaka Miyasaka } 80865c2687SKazutaka Miyasaka $stack[] = $pages_matched; 81865c2687SKazutaka Miyasaka break; 822f502d70SKazutaka Miyasaka case 'N+:': 832f502d70SKazutaka Miyasaka case 'N-:': // namespace 84865c2687SKazutaka Miyasaka $ns = substr($token, 3); 85865c2687SKazutaka Miyasaka $pages_matched = array(); 86865c2687SKazutaka Miyasaka foreach (array_keys($pages_all) as $id) { 87865c2687SKazutaka Miyasaka if (strpos($id, $ns) === 0) { 88865c2687SKazutaka Miyasaka $pages_matched[$id] = 0; // namespace: always 0 hit 89865c2687SKazutaka Miyasaka } 90865c2687SKazutaka Miyasaka } 91865c2687SKazutaka Miyasaka $stack[] = $pages_matched; 92865c2687SKazutaka Miyasaka break; 93865c2687SKazutaka Miyasaka case 'AND': // and operation 94865c2687SKazutaka Miyasaka list($pages1, $pages2) = array_splice($stack, -2); 95865c2687SKazutaka Miyasaka $stack[] = ft_resultCombine(array($pages1, $pages2)); 96865c2687SKazutaka Miyasaka break; 97865c2687SKazutaka Miyasaka case 'OR': // or operation 98865c2687SKazutaka Miyasaka list($pages1, $pages2) = array_splice($stack, -2); 99865c2687SKazutaka Miyasaka $stack[] = ft_resultUnite(array($pages1, $pages2)); 100865c2687SKazutaka Miyasaka break; 101865c2687SKazutaka Miyasaka case 'NOT': // not operation (unary) 102865c2687SKazutaka Miyasaka $pages = array_pop($stack); 103865c2687SKazutaka Miyasaka $stack[] = ft_resultComplement(array($pages_all, $pages)); 104a21136cdSAndreas Gohr break; 105a21136cdSAndreas Gohr } 106f5eb7cf0SAndreas Gohr } 107865c2687SKazutaka Miyasaka $docs = array_pop($stack); 108865c2687SKazutaka Miyasaka 109865c2687SKazutaka Miyasaka if (empty($docs)) return array(); 110865c2687SKazutaka Miyasaka 111865c2687SKazutaka Miyasaka // check: settings, acls, existence 112865c2687SKazutaka Miyasaka foreach (array_keys($docs) as $id) { 113865c2687SKazutaka Miyasaka if (isHiddenPage($id) || auth_quickaclcheck($id) < AUTH_READ || !page_exists($id, '', false)) { 114865c2687SKazutaka Miyasaka unset($docs[$id]); 115f5eb7cf0SAndreas Gohr } 116f5eb7cf0SAndreas Gohr } 117f5eb7cf0SAndreas Gohr 118865c2687SKazutaka Miyasaka // sort docs by count 119f5eb7cf0SAndreas Gohr arsort($docs); 120f5eb7cf0SAndreas Gohr 121f5eb7cf0SAndreas Gohr return $docs; 122f5eb7cf0SAndreas Gohr} 123f5eb7cf0SAndreas Gohr 124f5eb7cf0SAndreas Gohr/** 12554f4c056SAndreas Gohr * Returns the backlinks for a given page 12654f4c056SAndreas Gohr * 127320f489aSMichael Hamann * Uses the metadata index. 12807ff0babSMichael Hamann * 12907ff0babSMichael Hamann * @param string $id The id for which links shall be returned 13007ff0babSMichael Hamann * @param bool $ignore_perms Ignore the fact that pages are hidden or read-protected 13107ff0babSMichael Hamann * @return array The pages that contain links to the given page 13254f4c056SAndreas Gohr */ 13307ff0babSMichael Hamannfunction ft_backlinks($id, $ignore_perms = false){ 134320f489aSMichael Hamann $result = idx_get_indexer()->lookupKey('relation_references', $id); 13554f4c056SAndreas Gohr 13663773904SAndreas Gohr if(!count($result)) return $result; 13763773904SAndreas Gohr 13863773904SAndreas Gohr // check ACL permissions 13963773904SAndreas Gohr foreach(array_keys($result) as $idx){ 14007ff0babSMichael Hamann if(($ignore_perms !== true && ( 14107ff0babSMichael Hamann isHiddenPage($result[$idx]) || auth_quickaclcheck($result[$idx]) < AUTH_READ 14207ff0babSMichael Hamann )) || !page_exists($result[$idx], '', false)){ 14363773904SAndreas Gohr unset($result[$idx]); 14463773904SAndreas Gohr } 14563773904SAndreas Gohr } 14663773904SAndreas Gohr 14754f4c056SAndreas Gohr sort($result); 14854f4c056SAndreas Gohr return $result; 14954f4c056SAndreas Gohr} 15054f4c056SAndreas Gohr 15154f4c056SAndreas Gohr/** 152a05e297aSAndreas Gohr * Returns the pages that use a given media file 153a05e297aSAndreas Gohr * 154*ffec1009SMichael Hamann * Uses the relation media metadata property and the metadata index. 155a05e297aSAndreas Gohr * 156*ffec1009SMichael Hamann * Note that before 2013-07-31 the second parameter was the maximum number of results and 157*ffec1009SMichael Hamann * permissions were ignored. That's why the parameter is now checked to be explicitely set 158*ffec1009SMichael Hamann * to true (with type bool) in order to be compatible with older uses of the function. 159*ffec1009SMichael Hamann * 160*ffec1009SMichael Hamann * @param string $id The media id to look for 161*ffec1009SMichael Hamann * @param bool $ignore_perms Ignore hidden pages and acls (optional, default: false) 162*ffec1009SMichael Hamann * @return array A list of pages that use the given media file 163a05e297aSAndreas Gohr */ 164*ffec1009SMichael Hamannfunction ft_mediause($id, $ignore_perms = false){ 165*ffec1009SMichael Hamann $result = idx_get_indexer()->lookupKey('relation_media', $id); 166a05e297aSAndreas Gohr 167*ffec1009SMichael Hamann if(!count($result)) return $result; 168a05e297aSAndreas Gohr 169*ffec1009SMichael Hamann // check ACL permissions 170*ffec1009SMichael Hamann foreach(array_keys($result) as $idx){ 171*ffec1009SMichael Hamann if(($ignore_perms !== true && ( 172*ffec1009SMichael Hamann isHiddenPage($result[$idx]) || auth_quickaclcheck($result[$idx]) < AUTH_READ 173*ffec1009SMichael Hamann )) || !page_exists($result[$idx], '', false)){ 174*ffec1009SMichael Hamann unset($result[$idx]); 175a05e297aSAndreas Gohr } 176a05e297aSAndreas Gohr } 177a05e297aSAndreas Gohr 178a05e297aSAndreas Gohr sort($result); 179a05e297aSAndreas Gohr return $result; 180a05e297aSAndreas Gohr} 181a05e297aSAndreas Gohr 182a05e297aSAndreas Gohr 183a05e297aSAndreas Gohr 184a05e297aSAndreas Gohr/** 185506fa893SAndreas Gohr * Quicksearch for pagenames 186506fa893SAndreas Gohr * 187506fa893SAndreas Gohr * By default it only matches the pagename and ignores the 18880423ab6SAdrian Lang * namespace. This can be changed with the second parameter. 18980423ab6SAdrian Lang * The third parameter allows to search in titles as well. 190506fa893SAndreas Gohr * 1918d22f1e9SAndreas Gohr * The function always returns titles as well 1926840140fSChris Smith * 1938d22f1e9SAndreas Gohr * @triggers SEARCH_QUERY_PAGELOOKUP 194506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1958d22f1e9SAndreas Gohr * @author Adrian Lang <lang@cosmocode.de> 196506fa893SAndreas Gohr */ 1978d22f1e9SAndreas Gohrfunction ft_pageLookup($id, $in_ns=false, $in_title=false){ 1988d22f1e9SAndreas Gohr $data = compact('id', 'in_ns', 'in_title'); 1998d22f1e9SAndreas Gohr $data['has_titles'] = true; // for plugin backward compatibility check 2006840140fSChris Smith return trigger_event('SEARCH_QUERY_PAGELOOKUP', $data, '_ft_pageLookup'); 2016840140fSChris Smith} 2026840140fSChris Smith 2036840140fSChris Smithfunction _ft_pageLookup(&$data){ 20480423ab6SAdrian Lang // split out original parameters 2056840140fSChris Smith $id = $data['id']; 206b0f6db0cSAdrian Lang if (preg_match('/(?:^| )@(\w+)/', $id, $matches)) { 207b0f6db0cSAdrian Lang $ns = cleanID($matches[1]) . ':'; 208b0f6db0cSAdrian Lang $id = str_replace($matches[0], '', $id); 209b0f6db0cSAdrian Lang } 210b0f6db0cSAdrian Lang 2118d22f1e9SAndreas Gohr $in_ns = $data['in_ns']; 2128d22f1e9SAndreas Gohr $in_title = $data['in_title']; 21380423ab6SAdrian Lang $cleaned = cleanID($id); 2149b41be24STom N Harris 2159b41be24STom N Harris $Indexer = idx_get_indexer(); 2169b41be24STom N Harris $page_idx = $Indexer->getPages(); 2179b41be24STom N Harris 2189b41be24STom N Harris $pages = array(); 2195479a8c3SAndreas Gohr if ($id !== '' && $cleaned !== '') { 2209b41be24STom N Harris foreach ($page_idx as $p_id) { 2219b41be24STom N Harris if ((strpos($in_ns ? $p_id : noNSorNS($p_id), $cleaned) !== false)) { 2229b41be24STom N Harris if (!isset($pages[$p_id])) 22367c15eceSMichael Hamann $pages[$p_id] = p_get_first_heading($p_id, METADATA_DONT_RENDER); 224506fa893SAndreas Gohr } 225506fa893SAndreas Gohr } 226f078bb00STom N Harris if ($in_title) { 227c66f16a3SMichael Hamann foreach ($Indexer->lookupKey('title', $id, '_ft_pageLookupTitleCompare') as $p_id) { 228f078bb00STom N Harris if (!isset($pages[$p_id])) 22967c15eceSMichael Hamann $pages[$p_id] = p_get_first_heading($p_id, METADATA_DONT_RENDER); 230f078bb00STom N Harris } 231f078bb00STom N Harris } 232d0bdf765SAdrian Lang } 2330c074a52SMichael Hamann 234d0bdf765SAdrian Lang if (isset($ns)) { 2350c074a52SMichael Hamann foreach (array_keys($pages) as $p_id) { 2360c074a52SMichael Hamann if (strpos($p_id, $ns) !== 0) { 2370c074a52SMichael Hamann unset($pages[$p_id]); 238d0bdf765SAdrian Lang } 239d0bdf765SAdrian Lang } 240506fa893SAndreas Gohr } 24163773904SAndreas Gohr 24280423ab6SAdrian Lang // discard hidden pages 24380423ab6SAdrian Lang // discard nonexistent pages 24463773904SAndreas Gohr // check ACL permissions 24563773904SAndreas Gohr foreach(array_keys($pages) as $idx){ 24680423ab6SAdrian Lang if(!isVisiblePage($idx) || !page_exists($idx) || 24780423ab6SAdrian Lang auth_quickaclcheck($idx) < AUTH_READ) { 24863773904SAndreas Gohr unset($pages[$idx]); 24963773904SAndreas Gohr } 25063773904SAndreas Gohr } 25163773904SAndreas Gohr 2523d2017d9SAdrian Lang uksort($pages,'ft_pagesorter'); 2538d22f1e9SAndreas Gohr return $pages; 254506fa893SAndreas Gohr} 255506fa893SAndreas Gohr 256506fa893SAndreas Gohr/** 257c66f16a3SMichael Hamann * Tiny helper function for comparing the searched title with the title 258c66f16a3SMichael Hamann * from the search index. This function is a wrapper around stripos with 259c66f16a3SMichael Hamann * adapted argument order and return value. 260c66f16a3SMichael Hamann */ 261c66f16a3SMichael Hamannfunction _ft_pageLookupTitleCompare($search, $title) { 262c66f16a3SMichael Hamann return stripos($title, $search) !== false; 263c66f16a3SMichael Hamann} 264c66f16a3SMichael Hamann 265c66f16a3SMichael Hamann/** 266f31eb72bSAndreas Gohr * Sort pages based on their namespace level first, then on their string 267f31eb72bSAndreas Gohr * values. This makes higher hierarchy pages rank higher than lower hierarchy 268f31eb72bSAndreas Gohr * pages. 269f31eb72bSAndreas Gohr */ 270f31eb72bSAndreas Gohrfunction ft_pagesorter($a, $b){ 271f31eb72bSAndreas Gohr $ac = count(explode(':',$a)); 272f31eb72bSAndreas Gohr $bc = count(explode(':',$b)); 273f31eb72bSAndreas Gohr if($ac < $bc){ 274f31eb72bSAndreas Gohr return -1; 275f31eb72bSAndreas Gohr }elseif($ac > $bc){ 276f31eb72bSAndreas Gohr return 1; 277f31eb72bSAndreas Gohr } 278f31eb72bSAndreas Gohr return strcmp ($a,$b); 279f31eb72bSAndreas Gohr} 280f31eb72bSAndreas Gohr 281f31eb72bSAndreas Gohr/** 282506fa893SAndreas Gohr * Creates a snippet extract 283506fa893SAndreas Gohr * 284506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 28560e91a17SAndreas Gohr * @triggers FULLTEXT_SNIPPET_CREATE 286506fa893SAndreas Gohr */ 287546d3a99SAndreas Gohrfunction ft_snippet($id,$highlight){ 288506fa893SAndreas Gohr $text = rawWiki($id); 2894f0030ddSAndreas Gohr $text = str_replace("\xC2\xAD",'',$text); // remove soft-hyphens 29060e91a17SAndreas Gohr $evdata = array( 29160e91a17SAndreas Gohr 'id' => $id, 29260e91a17SAndreas Gohr 'text' => &$text, 29360e91a17SAndreas Gohr 'highlight' => &$highlight, 29460e91a17SAndreas Gohr 'snippet' => '', 29560e91a17SAndreas Gohr ); 29660e91a17SAndreas Gohr 29760e91a17SAndreas Gohr $evt = new Doku_Event('FULLTEXT_SNIPPET_CREATE',$evdata); 29860e91a17SAndreas Gohr if ($evt->advise_before()) { 299ced0762eSchris $match = array(); 300ced0762eSchris $snippets = array(); 3019ee93076Schris $utf8_offset = $offset = $end = 0; 302ced0762eSchris $len = utf8_strlen($text); 3039ee93076Schris 304546d3a99SAndreas Gohr // build a regexp from the phrases to highlight 3052237b4faSAndreas Gohr $re1 = '('.join('|',array_map('ft_snippet_re_preprocess', array_map('preg_quote_cb',array_filter((array) $highlight)))).')'; 306b571ff2dSChuck Kollars $re2 = "$re1.{0,75}(?!\\1)$re1"; 307b571ff2dSChuck Kollars $re3 = "$re1.{0,45}(?!\\1)$re1.{0,45}(?!\\1)(?!\\2)$re1"; 308546d3a99SAndreas Gohr 309b571ff2dSChuck Kollars for ($cnt=4; $cnt--;) { 310b571ff2dSChuck Kollars if (0) { 311b571ff2dSChuck Kollars } else if (preg_match('/'.$re3.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) { 312b571ff2dSChuck Kollars } else if (preg_match('/'.$re2.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) { 313b571ff2dSChuck Kollars } else if (preg_match('/'.$re1.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) { 314b571ff2dSChuck Kollars } else { 315b571ff2dSChuck Kollars break; 316b571ff2dSChuck Kollars } 317ced0762eSchris 318ced0762eSchris list($str,$idx) = $match[0]; 319ced0762eSchris 320ced0762eSchris // convert $idx (a byte offset) into a utf8 character offset 321ced0762eSchris $utf8_idx = utf8_strlen(substr($text,0,$idx)); 322ced0762eSchris $utf8_len = utf8_strlen($str); 323ced0762eSchris 324ced0762eSchris // establish context, 100 bytes surrounding the match string 325ced0762eSchris // first look to see if we can go 100 either side, 326ced0762eSchris // then drop to 50 adding any excess if the other side can't go to 50, 327ced0762eSchris $pre = min($utf8_idx-$utf8_offset,100); 328ced0762eSchris $post = min($len-$utf8_idx-$utf8_len,100); 329ced0762eSchris 330ced0762eSchris if ($pre>50 && $post>50) { 331ced0762eSchris $pre = $post = 50; 332ced0762eSchris } else if ($pre>50) { 333ced0762eSchris $pre = min($pre,100-$post); 334ced0762eSchris } else if ($post>50) { 335ced0762eSchris $post = min($post, 100-$pre); 336ced0762eSchris } else { 337ced0762eSchris // both are less than 50, means the context is the whole string 33810ffc9ddSAndreas Gohr // make it so and break out of this loop - there is no need for the 33910ffc9ddSAndreas Gohr // complex snippet calculations 340ced0762eSchris $snippets = array($text); 341ced0762eSchris break; 342ced0762eSchris } 343ced0762eSchris 34410ffc9ddSAndreas Gohr // establish context start and end points, try to append to previous 34510ffc9ddSAndreas Gohr // context if possible 3469ee93076Schris $start = $utf8_idx - $pre; 347ced0762eSchris $append = ($start < $end) ? $end : false; // still the end of the previous context snippet 3489ee93076Schris $end = $utf8_idx + $utf8_len + $post; // now set it to the end of this context 349ced0762eSchris 350ced0762eSchris if ($append) { 351ced0762eSchris $snippets[count($snippets)-1] .= utf8_substr($text,$append,$end-$append); 352ced0762eSchris } else { 353ced0762eSchris $snippets[] = utf8_substr($text,$start,$end-$start); 354ced0762eSchris } 355ced0762eSchris 356ced0762eSchris // set $offset for next match attempt 35710ffc9ddSAndreas Gohr // substract strlen to avoid splitting a potential search success, 35810ffc9ddSAndreas Gohr // this is an approximation as the search pattern may match strings 35910ffc9ddSAndreas Gohr // of varying length and it will fail if the context snippet 360ced0762eSchris // boundary breaks a matching string longer than the current match 3619ee93076Schris $utf8_offset = $utf8_idx + $post; 3629ee93076Schris $offset = $idx + strlen(utf8_substr($text,$utf8_idx,$post)); 3639ee93076Schris $offset = utf8_correctIdx($text,$offset); 3649ee93076Schris } 3659ee93076Schris 366ced0762eSchris $m = "\1"; 367b571ff2dSChuck Kollars $snippets = preg_replace('/'.$re1.'/iu',$m.'$1'.$m,$snippets); 368b571ff2dSChuck Kollars $snippet = preg_replace('/'.$m.'([^'.$m.']*?)'.$m.'/iu','<strong class="search_hit">$1</strong>',hsc(join('... ',$snippets))); 369bd2cb6fcSchris 37060e91a17SAndreas Gohr $evdata['snippet'] = $snippet; 37160e91a17SAndreas Gohr } 37260e91a17SAndreas Gohr $evt->advise_after(); 37360e91a17SAndreas Gohr unset($evt); 37460e91a17SAndreas Gohr 37560e91a17SAndreas Gohr return $evdata['snippet']; 376506fa893SAndreas Gohr} 377506fa893SAndreas Gohr 378506fa893SAndreas Gohr/** 37926eb848cSGina Haeussge * Wraps a search term in regex boundary checks. 38026eb848cSGina Haeussge */ 3812237b4faSAndreas Gohrfunction ft_snippet_re_preprocess($term) { 38235594613SKazutaka Miyasaka // do not process asian terms where word boundaries are not explicit 38335594613SKazutaka Miyasaka if(preg_match('/'.IDX_ASIAN.'/u',$term)){ 38435594613SKazutaka Miyasaka return $term; 38535594613SKazutaka Miyasaka } 38635594613SKazutaka Miyasaka 3873161005dSAndreas Gohr if (UTF8_PROPERTYSUPPORT) { 38884e581a6SAndreas Gohr // unicode word boundaries 38984e581a6SAndreas Gohr // see http://stackoverflow.com/a/2449017/172068 39084e581a6SAndreas Gohr $BL = '(?<!\pL)'; 39184e581a6SAndreas Gohr $BR = '(?!\pL)'; 3923161005dSAndreas Gohr } else { 3933161005dSAndreas Gohr // not as correct as above, but at least won't break 3943161005dSAndreas Gohr $BL = '\b'; 3953161005dSAndreas Gohr $BR = '\b'; 3963161005dSAndreas Gohr } 3973161005dSAndreas Gohr 39884e581a6SAndreas Gohr 3992237b4faSAndreas Gohr if(substr($term,0,2) == '\\*'){ 4002237b4faSAndreas Gohr $term = substr($term,2); 4012237b4faSAndreas Gohr }else{ 40284e581a6SAndreas Gohr $term = $BL.$term; 4032237b4faSAndreas Gohr } 4042237b4faSAndreas Gohr 4052237b4faSAndreas Gohr if(substr($term,-2,2) == '\\*'){ 4062237b4faSAndreas Gohr $term = substr($term,0,-2); 4072237b4faSAndreas Gohr }else{ 40884e581a6SAndreas Gohr $term = $term.$BR; 4092237b4faSAndreas Gohr } 4108a803caeSAndreas Gohr 41184e581a6SAndreas Gohr if($term == $BL || $term == $BR || $term == $BL.$BR) $term = ''; 4122237b4faSAndreas Gohr return $term; 41326eb848cSGina Haeussge} 41426eb848cSGina Haeussge 41526eb848cSGina Haeussge/** 416f5eb7cf0SAndreas Gohr * Combine found documents and sum up their scores 417f5eb7cf0SAndreas Gohr * 418f5eb7cf0SAndreas Gohr * This function is used to combine searched words with a logical 419f5eb7cf0SAndreas Gohr * AND. Only documents available in all arrays are returned. 420f5eb7cf0SAndreas Gohr * 421f5eb7cf0SAndreas Gohr * based upon PEAR's PHP_Compat function for array_intersect_key() 422f5eb7cf0SAndreas Gohr * 423f5eb7cf0SAndreas Gohr * @param array $args An array of page arrays 424f5eb7cf0SAndreas Gohr */ 425f5eb7cf0SAndreas Gohrfunction ft_resultCombine($args){ 426f5eb7cf0SAndreas Gohr $array_count = count($args); 427134f4ab2SAndreas Gohr if($array_count == 1){ 428134f4ab2SAndreas Gohr return $args[0]; 429134f4ab2SAndreas Gohr } 430134f4ab2SAndreas Gohr 431f5eb7cf0SAndreas Gohr $result = array(); 43209c27a6dSGuy Brand if ($array_count > 1) { 433a21136cdSAndreas Gohr foreach ($args[0] as $key => $value) { 434a21136cdSAndreas Gohr $result[$key] = $value; 435f5eb7cf0SAndreas Gohr for ($i = 1; $i !== $array_count; $i++) { 436a21136cdSAndreas Gohr if (!isset($args[$i][$key])) { 437a21136cdSAndreas Gohr unset($result[$key]); 438a21136cdSAndreas Gohr break; 439f5eb7cf0SAndreas Gohr } 440a21136cdSAndreas Gohr $result[$key] += $args[$i][$key]; 441f5eb7cf0SAndreas Gohr } 442f5eb7cf0SAndreas Gohr } 44309c27a6dSGuy Brand } 444f5eb7cf0SAndreas Gohr return $result; 445f5eb7cf0SAndreas Gohr} 446f5eb7cf0SAndreas Gohr 447f5eb7cf0SAndreas Gohr/** 448865c2687SKazutaka Miyasaka * Unites found documents and sum up their scores 449f5eb7cf0SAndreas Gohr * 450865c2687SKazutaka Miyasaka * based upon ft_resultCombine() function 451865c2687SKazutaka Miyasaka * 452865c2687SKazutaka Miyasaka * @param array $args An array of page arrays 453865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 454865c2687SKazutaka Miyasaka */ 455865c2687SKazutaka Miyasakafunction ft_resultUnite($args) { 456865c2687SKazutaka Miyasaka $array_count = count($args); 457865c2687SKazutaka Miyasaka if ($array_count === 1) { 458865c2687SKazutaka Miyasaka return $args[0]; 459865c2687SKazutaka Miyasaka } 460865c2687SKazutaka Miyasaka 461865c2687SKazutaka Miyasaka $result = $args[0]; 462865c2687SKazutaka Miyasaka for ($i = 1; $i !== $array_count; $i++) { 463865c2687SKazutaka Miyasaka foreach (array_keys($args[$i]) as $id) { 464865c2687SKazutaka Miyasaka $result[$id] += $args[$i][$id]; 465865c2687SKazutaka Miyasaka } 466865c2687SKazutaka Miyasaka } 467865c2687SKazutaka Miyasaka return $result; 468865c2687SKazutaka Miyasaka} 469865c2687SKazutaka Miyasaka 470865c2687SKazutaka Miyasaka/** 471865c2687SKazutaka Miyasaka * Computes the difference of documents using page id for comparison 472865c2687SKazutaka Miyasaka * 473865c2687SKazutaka Miyasaka * nearly identical to PHP5's array_diff_key() 474865c2687SKazutaka Miyasaka * 475865c2687SKazutaka Miyasaka * @param array $args An array of page arrays 476865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 477865c2687SKazutaka Miyasaka */ 478865c2687SKazutaka Miyasakafunction ft_resultComplement($args) { 479865c2687SKazutaka Miyasaka $array_count = count($args); 480865c2687SKazutaka Miyasaka if ($array_count === 1) { 481865c2687SKazutaka Miyasaka return $args[0]; 482865c2687SKazutaka Miyasaka } 483865c2687SKazutaka Miyasaka 484865c2687SKazutaka Miyasaka $result = $args[0]; 485865c2687SKazutaka Miyasaka foreach (array_keys($result) as $id) { 486865c2687SKazutaka Miyasaka for ($i = 1; $i !== $array_count; $i++) { 487865c2687SKazutaka Miyasaka if (isset($args[$i][$id])) unset($result[$id]); 488865c2687SKazutaka Miyasaka } 489865c2687SKazutaka Miyasaka } 490865c2687SKazutaka Miyasaka return $result; 491865c2687SKazutaka Miyasaka} 492865c2687SKazutaka Miyasaka 493865c2687SKazutaka Miyasaka/** 494865c2687SKazutaka Miyasaka * Parses a search query and builds an array of search formulas 495865c2687SKazutaka Miyasaka * 496865c2687SKazutaka Miyasaka * @author Andreas Gohr <andi@splitbrain.org> 497865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 498f5eb7cf0SAndreas Gohr */ 4999b41be24STom N Harrisfunction ft_queryParser($Indexer, $query){ 500865c2687SKazutaka Miyasaka /** 501865c2687SKazutaka Miyasaka * parse a search query and transform it into intermediate representation 502865c2687SKazutaka Miyasaka * 503865c2687SKazutaka Miyasaka * in a search query, you can use the following expressions: 504865c2687SKazutaka Miyasaka * 505865c2687SKazutaka Miyasaka * words: 506865c2687SKazutaka Miyasaka * include 507865c2687SKazutaka Miyasaka * -exclude 508865c2687SKazutaka Miyasaka * phrases: 509865c2687SKazutaka Miyasaka * "phrase to be included" 510865c2687SKazutaka Miyasaka * -"phrase you want to exclude" 511865c2687SKazutaka Miyasaka * namespaces: 512865c2687SKazutaka Miyasaka * @include:namespace (or ns:include:namespace) 513865c2687SKazutaka Miyasaka * ^exclude:namespace (or -ns:exclude:namespace) 514865c2687SKazutaka Miyasaka * groups: 515865c2687SKazutaka Miyasaka * () 516865c2687SKazutaka Miyasaka * -() 517865c2687SKazutaka Miyasaka * operators: 518865c2687SKazutaka Miyasaka * and ('and' is the default operator: you can always omit this) 5197871d415SKazutaka Miyasaka * or (or pipe symbol '|', lower precedence than 'and') 520865c2687SKazutaka Miyasaka * 521865c2687SKazutaka Miyasaka * e.g. a query [ aa "bb cc" @dd:ee ] means "search pages which contain 522865c2687SKazutaka Miyasaka * a word 'aa', a phrase 'bb cc' and are within a namespace 'dd:ee'". 523865c2687SKazutaka Miyasaka * this query is equivalent to [ -(-aa or -"bb cc" or -ns:dd:ee) ] 524865c2687SKazutaka Miyasaka * as long as you don't mind hit counts. 525865c2687SKazutaka Miyasaka * 526865c2687SKazutaka Miyasaka * intermediate representation consists of the following parts: 527865c2687SKazutaka Miyasaka * 528865c2687SKazutaka Miyasaka * ( ) - group 529865c2687SKazutaka Miyasaka * AND - logical and 530865c2687SKazutaka Miyasaka * OR - logical or 531865c2687SKazutaka Miyasaka * NOT - logical not 5322f502d70SKazutaka Miyasaka * W+:, W-:, W_: - word (underscore: no need to highlight) 5332f502d70SKazutaka Miyasaka * P+:, P-: - phrase (minus sign: logically in NOT group) 5342f502d70SKazutaka Miyasaka * N+:, N-: - namespace 535865c2687SKazutaka Miyasaka */ 536865c2687SKazutaka Miyasaka $parsed_query = ''; 537865c2687SKazutaka Miyasaka $parens_level = 0; 538865c2687SKazutaka Miyasaka $terms = preg_split('/(-?".*?")/u', utf8_strtolower($query), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 539865c2687SKazutaka Miyasaka 540865c2687SKazutaka Miyasaka foreach ($terms as $term) { 541865c2687SKazutaka Miyasaka $parsed = ''; 542865c2687SKazutaka Miyasaka if (preg_match('/^(-?)"(.+)"$/u', $term, $matches)) { 543865c2687SKazutaka Miyasaka // phrase-include and phrase-exclude 544865c2687SKazutaka Miyasaka $not = $matches[1] ? 'NOT' : ''; 5459b41be24STom N Harris $parsed = $not.ft_termParser($Indexer, $matches[2], false, true); 546f5eb7cf0SAndreas Gohr } else { 547865c2687SKazutaka Miyasaka // fix incomplete phrase 548865c2687SKazutaka Miyasaka $term = str_replace('"', ' ', $term); 549865c2687SKazutaka Miyasaka 550865c2687SKazutaka Miyasaka // fix parentheses 551865c2687SKazutaka Miyasaka $term = str_replace(')' , ' ) ', $term); 552865c2687SKazutaka Miyasaka $term = str_replace('(' , ' ( ', $term); 553865c2687SKazutaka Miyasaka $term = str_replace('- (', ' -(', $term); 554865c2687SKazutaka Miyasaka 5557871d415SKazutaka Miyasaka // treat pipe symbols as 'OR' operators 5567871d415SKazutaka Miyasaka $term = str_replace('|', ' or ', $term); 5577871d415SKazutaka Miyasaka 558865c2687SKazutaka Miyasaka // treat ideographic spaces (U+3000) as search term separators 559865c2687SKazutaka Miyasaka // FIXME: some more separators? 560865c2687SKazutaka Miyasaka $term = preg_replace('/[ \x{3000}]+/u', ' ', $term); 561865c2687SKazutaka Miyasaka $term = trim($term); 562865c2687SKazutaka Miyasaka if ($term === '') continue; 563865c2687SKazutaka Miyasaka 564865c2687SKazutaka Miyasaka $tokens = explode(' ', $term); 565865c2687SKazutaka Miyasaka foreach ($tokens as $token) { 566865c2687SKazutaka Miyasaka if ($token === '(') { 567865c2687SKazutaka Miyasaka // parenthesis-include-open 568865c2687SKazutaka Miyasaka $parsed .= '('; 569865c2687SKazutaka Miyasaka ++$parens_level; 570865c2687SKazutaka Miyasaka } elseif ($token === '-(') { 571865c2687SKazutaka Miyasaka // parenthesis-exclude-open 572865c2687SKazutaka Miyasaka $parsed .= 'NOT('; 573865c2687SKazutaka Miyasaka ++$parens_level; 574865c2687SKazutaka Miyasaka } elseif ($token === ')') { 575865c2687SKazutaka Miyasaka // parenthesis-any-close 576865c2687SKazutaka Miyasaka if ($parens_level === 0) continue; 577865c2687SKazutaka Miyasaka $parsed .= ')'; 578865c2687SKazutaka Miyasaka $parens_level--; 579865c2687SKazutaka Miyasaka } elseif ($token === 'and') { 580865c2687SKazutaka Miyasaka // logical-and (do nothing) 581865c2687SKazutaka Miyasaka } elseif ($token === 'or') { 582865c2687SKazutaka Miyasaka // logical-or 583865c2687SKazutaka Miyasaka $parsed .= 'OR'; 584865c2687SKazutaka Miyasaka } elseif (preg_match('/^(?:\^|-ns:)(.+)$/u', $token, $matches)) { 585865c2687SKazutaka Miyasaka // namespace-exclude 5862f502d70SKazutaka Miyasaka $parsed .= 'NOT(N+:'.$matches[1].')'; 587865c2687SKazutaka Miyasaka } elseif (preg_match('/^(?:@|ns:)(.+)$/u', $token, $matches)) { 588865c2687SKazutaka Miyasaka // namespace-include 5892f502d70SKazutaka Miyasaka $parsed .= '(N+:'.$matches[1].')'; 590865c2687SKazutaka Miyasaka } elseif (preg_match('/^-(.+)$/', $token, $matches)) { 591865c2687SKazutaka Miyasaka // word-exclude 5929b41be24STom N Harris $parsed .= 'NOT('.ft_termParser($Indexer, $matches[1]).')'; 593865c2687SKazutaka Miyasaka } else { 594865c2687SKazutaka Miyasaka // word-include 5959b41be24STom N Harris $parsed .= ft_termParser($Indexer, $token); 596865c2687SKazutaka Miyasaka } 597865c2687SKazutaka Miyasaka } 598865c2687SKazutaka Miyasaka } 599865c2687SKazutaka Miyasaka $parsed_query .= $parsed; 600f5eb7cf0SAndreas Gohr } 601f5eb7cf0SAndreas Gohr 602865c2687SKazutaka Miyasaka // cleanup (very sensitive) 603865c2687SKazutaka Miyasaka $parsed_query .= str_repeat(')', $parens_level); 604865c2687SKazutaka Miyasaka do { 605865c2687SKazutaka Miyasaka $parsed_query_old = $parsed_query; 606865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/(NOT)?\(\)/u', '', $parsed_query); 607865c2687SKazutaka Miyasaka } while ($parsed_query !== $parsed_query_old); 608865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/(NOT|OR)+\)/u', ')' , $parsed_query); 609865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/(OR)+/u' , 'OR' , $parsed_query); 610865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/\(OR/u' , '(' , $parsed_query); 611865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/^OR|OR$/u' , '' , $parsed_query); 612865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/\)(NOT)?\(/u' , ')AND$1(', $parsed_query); 613865c2687SKazutaka Miyasaka 6142f502d70SKazutaka Miyasaka // adjustment: make highlightings right 6152f502d70SKazutaka Miyasaka $parens_level = 0; 6162f502d70SKazutaka Miyasaka $notgrp_levels = array(); 6172f502d70SKazutaka Miyasaka $parsed_query_new = ''; 6182f502d70SKazutaka Miyasaka $tokens = preg_split('/(NOT\(|[()])/u', $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 6192f502d70SKazutaka Miyasaka foreach ($tokens as $token) { 6202f502d70SKazutaka Miyasaka if ($token === 'NOT(') { 6212f502d70SKazutaka Miyasaka $notgrp_levels[] = ++$parens_level; 6222f502d70SKazutaka Miyasaka } elseif ($token === '(') { 6232f502d70SKazutaka Miyasaka ++$parens_level; 6242f502d70SKazutaka Miyasaka } elseif ($token === ')') { 6252f502d70SKazutaka Miyasaka if ($parens_level-- === end($notgrp_levels)) array_pop($notgrp_levels); 6262f502d70SKazutaka Miyasaka } elseif (count($notgrp_levels) % 2 === 1) { 6272f502d70SKazutaka Miyasaka // turn highlight-flag off if terms are logically in "NOT" group 6282f502d70SKazutaka Miyasaka $token = preg_replace('/([WPN])\+\:/u', '$1-:', $token); 6292f502d70SKazutaka Miyasaka } 6302f502d70SKazutaka Miyasaka $parsed_query_new .= $token; 6312f502d70SKazutaka Miyasaka } 6322f502d70SKazutaka Miyasaka $parsed_query = $parsed_query_new; 6332f502d70SKazutaka Miyasaka 634865c2687SKazutaka Miyasaka /** 635865c2687SKazutaka Miyasaka * convert infix notation string into postfix (Reverse Polish notation) array 636865c2687SKazutaka Miyasaka * by Shunting-yard algorithm 637865c2687SKazutaka Miyasaka * 638865c2687SKazutaka Miyasaka * see: http://en.wikipedia.org/wiki/Reverse_Polish_notation 639865c2687SKazutaka Miyasaka * see: http://en.wikipedia.org/wiki/Shunting-yard_algorithm 640865c2687SKazutaka Miyasaka */ 641865c2687SKazutaka Miyasaka $parsed_ary = array(); 642865c2687SKazutaka Miyasaka $ope_stack = array(); 643865c2687SKazutaka Miyasaka $ope_precedence = array(')' => 1, 'OR' => 2, 'AND' => 3, 'NOT' => 4, '(' => 5); 644865c2687SKazutaka Miyasaka $ope_regex = '/([()]|OR|AND|NOT)/u'; 645865c2687SKazutaka Miyasaka 646865c2687SKazutaka Miyasaka $tokens = preg_split($ope_regex, $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 647865c2687SKazutaka Miyasaka foreach ($tokens as $token) { 648865c2687SKazutaka Miyasaka if (preg_match($ope_regex, $token)) { 649865c2687SKazutaka Miyasaka // operator 650865c2687SKazutaka Miyasaka $last_ope = end($ope_stack); 651865c2687SKazutaka Miyasaka while ($ope_precedence[$token] <= $ope_precedence[$last_ope] && $last_ope != '(') { 652865c2687SKazutaka Miyasaka $parsed_ary[] = array_pop($ope_stack); 653865c2687SKazutaka Miyasaka $last_ope = end($ope_stack); 654865c2687SKazutaka Miyasaka } 655865c2687SKazutaka Miyasaka if ($token == ')') { 656865c2687SKazutaka Miyasaka array_pop($ope_stack); // this array_pop always deletes '(' 657865c2687SKazutaka Miyasaka } else { 658865c2687SKazutaka Miyasaka $ope_stack[] = $token; 659865c2687SKazutaka Miyasaka } 660865c2687SKazutaka Miyasaka } else { 661865c2687SKazutaka Miyasaka // operand 662865c2687SKazutaka Miyasaka $token_decoded = str_replace(array('OP', 'CP'), array('(', ')'), $token); 663865c2687SKazutaka Miyasaka $parsed_ary[] = $token_decoded; 664865c2687SKazutaka Miyasaka } 665865c2687SKazutaka Miyasaka } 666865c2687SKazutaka Miyasaka $parsed_ary = array_values(array_merge($parsed_ary, array_reverse($ope_stack))); 667865c2687SKazutaka Miyasaka 668865c2687SKazutaka Miyasaka // cleanup: each double "NOT" in RPN array actually does nothing 669865c2687SKazutaka Miyasaka $parsed_ary_count = count($parsed_ary); 670865c2687SKazutaka Miyasaka for ($i = 1; $i < $parsed_ary_count; ++$i) { 671865c2687SKazutaka Miyasaka if ($parsed_ary[$i] === 'NOT' && $parsed_ary[$i - 1] === 'NOT') { 672865c2687SKazutaka Miyasaka unset($parsed_ary[$i], $parsed_ary[$i - 1]); 673865c2687SKazutaka Miyasaka } 674865c2687SKazutaka Miyasaka } 675865c2687SKazutaka Miyasaka $parsed_ary = array_values($parsed_ary); 676865c2687SKazutaka Miyasaka 677865c2687SKazutaka Miyasaka // build return value 678f5eb7cf0SAndreas Gohr $q = array(); 679f5eb7cf0SAndreas Gohr $q['query'] = $query; 680865c2687SKazutaka Miyasaka $q['parsed_str'] = $parsed_query; 681865c2687SKazutaka Miyasaka $q['parsed_ary'] = $parsed_ary; 682f5eb7cf0SAndreas Gohr 683865c2687SKazutaka Miyasaka foreach ($q['parsed_ary'] as $token) { 684865c2687SKazutaka Miyasaka if ($token[2] !== ':') continue; 685865c2687SKazutaka Miyasaka $body = substr($token, 3); 686865c2687SKazutaka Miyasaka 687865c2687SKazutaka Miyasaka switch (substr($token, 0, 3)) { 6882f502d70SKazutaka Miyasaka case 'N+:': 689865c2687SKazutaka Miyasaka $q['ns'][] = $body; // for backward compatibility 690865c2687SKazutaka Miyasaka break; 6912f502d70SKazutaka Miyasaka case 'N-:': 6922f502d70SKazutaka Miyasaka $q['notns'][] = $body; // for backward compatibility 6932f502d70SKazutaka Miyasaka break; 6942f502d70SKazutaka Miyasaka case 'W_:': 6952f502d70SKazutaka Miyasaka $q['words'][] = $body; 6962f502d70SKazutaka Miyasaka break; 697865c2687SKazutaka Miyasaka case 'W-:': 698865c2687SKazutaka Miyasaka $q['words'][] = $body; 6992f502d70SKazutaka Miyasaka $q['not'][] = $body; // for backward compatibility 700865c2687SKazutaka Miyasaka break; 701865c2687SKazutaka Miyasaka case 'W+:': 702865c2687SKazutaka Miyasaka $q['words'][] = $body; 7032237b4faSAndreas Gohr $q['highlight'][] = $body; 7042f502d70SKazutaka Miyasaka $q['and'][] = $body; // for backward compatibility 705865c2687SKazutaka Miyasaka break; 7062f502d70SKazutaka Miyasaka case 'P-:': 7072f502d70SKazutaka Miyasaka $q['phrases'][] = $body; 7082f502d70SKazutaka Miyasaka break; 7092f502d70SKazutaka Miyasaka case 'P+:': 710865c2687SKazutaka Miyasaka $q['phrases'][] = $body; 7112237b4faSAndreas Gohr $q['highlight'][] = $body; 712865c2687SKazutaka Miyasaka break; 713865c2687SKazutaka Miyasaka } 714865c2687SKazutaka Miyasaka } 7152f502d70SKazutaka Miyasaka foreach (array('words', 'phrases', 'highlight', 'ns', 'notns', 'and', 'not') as $key) { 716865c2687SKazutaka Miyasaka $q[$key] = empty($q[$key]) ? array() : array_values(array_unique($q[$key])); 717f5eb7cf0SAndreas Gohr } 718f5eb7cf0SAndreas Gohr 719f5eb7cf0SAndreas Gohr return $q; 720f5eb7cf0SAndreas Gohr} 721f5eb7cf0SAndreas Gohr 722865c2687SKazutaka Miyasaka/** 723865c2687SKazutaka Miyasaka * Transforms given search term into intermediate representation 724865c2687SKazutaka Miyasaka * 725865c2687SKazutaka Miyasaka * This function is used in ft_queryParser() and not for general purpose use. 726865c2687SKazutaka Miyasaka * 727865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 728865c2687SKazutaka Miyasaka */ 7299b41be24STom N Harrisfunction ft_termParser($Indexer, $term, $consider_asian = true, $phrase_mode = false) { 730865c2687SKazutaka Miyasaka $parsed = ''; 731865c2687SKazutaka Miyasaka if ($consider_asian) { 732865c2687SKazutaka Miyasaka // successive asian characters need to be searched as a phrase 733865c2687SKazutaka Miyasaka $words = preg_split('/('.IDX_ASIAN.'+)/u', $term, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 734865c2687SKazutaka Miyasaka foreach ($words as $word) { 7356ac2077aSKazutaka Miyasaka $phrase_mode = $phrase_mode ? true : preg_match('/'.IDX_ASIAN.'/u', $word); 7369b41be24STom N Harris $parsed .= ft_termParser($Indexer, $word, false, $phrase_mode); 737865c2687SKazutaka Miyasaka } 738865c2687SKazutaka Miyasaka } else { 739865c2687SKazutaka Miyasaka $term_noparen = str_replace(array('(', ')'), ' ', $term); 7409b41be24STom N Harris $words = $Indexer->tokenizer($term_noparen, true); 741865c2687SKazutaka Miyasaka 7422f502d70SKazutaka Miyasaka // W_: no need to highlight 743865c2687SKazutaka Miyasaka if (empty($words)) { 744865c2687SKazutaka Miyasaka $parsed = '()'; // important: do not remove 745865c2687SKazutaka Miyasaka } elseif ($words[0] === $term) { 746865c2687SKazutaka Miyasaka $parsed = '(W+:'.$words[0].')'; 747865c2687SKazutaka Miyasaka } elseif ($phrase_mode) { 748865c2687SKazutaka Miyasaka $term_encoded = str_replace(array('(', ')'), array('OP', 'CP'), $term); 7492f502d70SKazutaka Miyasaka $parsed = '((W_:'.implode(')(W_:', $words).')(P+:'.$term_encoded.'))'; 750865c2687SKazutaka Miyasaka } else { 751865c2687SKazutaka Miyasaka $parsed = '((W+:'.implode(')(W+:', $words).'))'; 752865c2687SKazutaka Miyasaka } 753865c2687SKazutaka Miyasaka } 754865c2687SKazutaka Miyasaka return $parsed; 755865c2687SKazutaka Miyasaka} 756865c2687SKazutaka Miyasaka 757e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 758