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 * 2342ea7f44SGerrit Uitslag * @param string $query 2442ea7f44SGerrit Uitslag * @param array $highlight 2542ea7f44SGerrit Uitslag * @return array 26f5eb7cf0SAndreas Gohr */ 27546d3a99SAndreas Gohrfunction ft_pageSearch($query,&$highlight){ 286840140fSChris Smith 2959bc3b48SGerrit Uitslag $data = array(); 306840140fSChris Smith $data['query'] = $query; 316840140fSChris Smith $data['highlight'] =& $highlight; 326840140fSChris Smith 336840140fSChris Smith return trigger_event('SEARCH_QUERY_FULLPAGE', $data, '_ft_pageSearch'); 346840140fSChris Smith} 35865c2687SKazutaka Miyasaka 36865c2687SKazutaka Miyasaka/** 37865c2687SKazutaka Miyasaka * Returns a list of matching documents for the given query 38865c2687SKazutaka Miyasaka * 39865c2687SKazutaka Miyasaka * @author Andreas Gohr <andi@splitbrain.org> 40865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 4142ea7f44SGerrit Uitslag * 4242ea7f44SGerrit Uitslag * @param array $data event data 4342ea7f44SGerrit Uitslag * @return array matching documents 44865c2687SKazutaka Miyasaka */ 456840140fSChris Smithfunction _ft_pageSearch(&$data) { 469b41be24STom N Harris $Indexer = idx_get_indexer(); 479b41be24STom N Harris 48865c2687SKazutaka Miyasaka // parse the given query 499b41be24STom N Harris $q = ft_queryParser($Indexer, $data['query']); 50865c2687SKazutaka Miyasaka $data['highlight'] = $q['highlight']; 516840140fSChris Smith 52865c2687SKazutaka Miyasaka if (empty($q['parsed_ary'])) return array(); 53506fa893SAndreas Gohr 54f5eb7cf0SAndreas Gohr // lookup all words found in the query 559b41be24STom N Harris $lookup = $Indexer->lookup($q['words']); 56f5eb7cf0SAndreas Gohr 57865c2687SKazutaka Miyasaka // get all pages in this dokuwiki site (!: includes nonexistent pages) 58865c2687SKazutaka Miyasaka $pages_all = array(); 599b41be24STom N Harris foreach ($Indexer->getPages() as $id) { 609b41be24STom N Harris $pages_all[$id] = 0; // base: 0 hit 61f5eb7cf0SAndreas Gohr } 62f5eb7cf0SAndreas Gohr 63865c2687SKazutaka Miyasaka // process the query 64865c2687SKazutaka Miyasaka $stack = array(); 65865c2687SKazutaka Miyasaka foreach ($q['parsed_ary'] as $token) { 66865c2687SKazutaka Miyasaka switch (substr($token, 0, 3)) { 67865c2687SKazutaka Miyasaka case 'W+:': 682f502d70SKazutaka Miyasaka case 'W-:': 692f502d70SKazutaka Miyasaka case 'W_:': // word 70865c2687SKazutaka Miyasaka $word = substr($token, 3); 71865c2687SKazutaka Miyasaka $stack[] = (array) $lookup[$word]; 72865c2687SKazutaka Miyasaka break; 732f502d70SKazutaka Miyasaka case 'P+:': 742f502d70SKazutaka Miyasaka case 'P-:': // phrase 75865c2687SKazutaka Miyasaka $phrase = substr($token, 3); 76865c2687SKazutaka Miyasaka // since phrases are always parsed as ((W1)(W2)...(P)), 77865c2687SKazutaka Miyasaka // the end($stack) always points the pages that contain 78865c2687SKazutaka Miyasaka // all words in this phrase 79865c2687SKazutaka Miyasaka $pages = end($stack); 80865c2687SKazutaka Miyasaka $pages_matched = array(); 81865c2687SKazutaka Miyasaka foreach(array_keys($pages) as $id){ 82a7e8b43eSMichael Hamann $evdata = array( 83a7e8b43eSMichael Hamann 'id' => $id, 84a7e8b43eSMichael Hamann 'phrase' => $phrase, 85a7e8b43eSMichael Hamann 'text' => rawWiki($id) 86a7e8b43eSMichael Hamann ); 87a7e8b43eSMichael Hamann $evt = new Doku_Event('FULLTEXT_PHRASE_MATCH',$evdata); 88a7e8b43eSMichael Hamann if ($evt->advise_before() && $evt->result !== true) { 89a7e8b43eSMichael Hamann $text = utf8_strtolower($evdata['text']); 90865c2687SKazutaka Miyasaka if (strpos($text, $phrase) !== false) { 91a7e8b43eSMichael Hamann $evt->result = true; 92a7e8b43eSMichael Hamann } 93a7e8b43eSMichael Hamann } 94a7e8b43eSMichael Hamann $evt->advise_after(); 95a7e8b43eSMichael Hamann if ($evt->result === true) { 96865c2687SKazutaka Miyasaka $pages_matched[$id] = 0; // phrase: always 0 hit 97865c2687SKazutaka Miyasaka } 98865c2687SKazutaka Miyasaka } 99865c2687SKazutaka Miyasaka $stack[] = $pages_matched; 100865c2687SKazutaka Miyasaka break; 1012f502d70SKazutaka Miyasaka case 'N+:': 1022f502d70SKazutaka Miyasaka case 'N-:': // namespace 103de3383c6SMichael Große $ns = cleanID(substr($token, 3)) . ':'; 104865c2687SKazutaka Miyasaka $pages_matched = array(); 105865c2687SKazutaka Miyasaka foreach (array_keys($pages_all) as $id) { 106865c2687SKazutaka Miyasaka if (strpos($id, $ns) === 0) { 107865c2687SKazutaka Miyasaka $pages_matched[$id] = 0; // namespace: always 0 hit 108865c2687SKazutaka Miyasaka } 109865c2687SKazutaka Miyasaka } 110865c2687SKazutaka Miyasaka $stack[] = $pages_matched; 111865c2687SKazutaka Miyasaka break; 112865c2687SKazutaka Miyasaka case 'AND': // and operation 113865c2687SKazutaka Miyasaka list($pages1, $pages2) = array_splice($stack, -2); 114865c2687SKazutaka Miyasaka $stack[] = ft_resultCombine(array($pages1, $pages2)); 115865c2687SKazutaka Miyasaka break; 116865c2687SKazutaka Miyasaka case 'OR': // or operation 117865c2687SKazutaka Miyasaka list($pages1, $pages2) = array_splice($stack, -2); 118865c2687SKazutaka Miyasaka $stack[] = ft_resultUnite(array($pages1, $pages2)); 119865c2687SKazutaka Miyasaka break; 120865c2687SKazutaka Miyasaka case 'NOT': // not operation (unary) 121865c2687SKazutaka Miyasaka $pages = array_pop($stack); 122865c2687SKazutaka Miyasaka $stack[] = ft_resultComplement(array($pages_all, $pages)); 123a21136cdSAndreas Gohr break; 124a21136cdSAndreas Gohr } 125f5eb7cf0SAndreas Gohr } 126865c2687SKazutaka Miyasaka $docs = array_pop($stack); 127865c2687SKazutaka Miyasaka 128865c2687SKazutaka Miyasaka if (empty($docs)) return array(); 129865c2687SKazutaka Miyasaka 130865c2687SKazutaka Miyasaka // check: settings, acls, existence 131865c2687SKazutaka Miyasaka foreach (array_keys($docs) as $id) { 132865c2687SKazutaka Miyasaka if (isHiddenPage($id) || auth_quickaclcheck($id) < AUTH_READ || !page_exists($id, '', false)) { 133865c2687SKazutaka Miyasaka unset($docs[$id]); 134f5eb7cf0SAndreas Gohr } 135f5eb7cf0SAndreas Gohr } 136f5eb7cf0SAndreas Gohr 137*1b48999cSMichael Große $docs = _ft_filterResultsByTime($docs); 138865c2687SKazutaka Miyasaka // sort docs by count 139f5eb7cf0SAndreas Gohr arsort($docs); 140f5eb7cf0SAndreas Gohr 141f5eb7cf0SAndreas Gohr return $docs; 142f5eb7cf0SAndreas Gohr} 143f5eb7cf0SAndreas Gohr 144f5eb7cf0SAndreas Gohr/** 14554f4c056SAndreas Gohr * Returns the backlinks for a given page 14654f4c056SAndreas Gohr * 147320f489aSMichael Hamann * Uses the metadata index. 14807ff0babSMichael Hamann * 14907ff0babSMichael Hamann * @param string $id The id for which links shall be returned 15007ff0babSMichael Hamann * @param bool $ignore_perms Ignore the fact that pages are hidden or read-protected 15107ff0babSMichael Hamann * @return array The pages that contain links to the given page 15254f4c056SAndreas Gohr */ 15307ff0babSMichael Hamannfunction ft_backlinks($id, $ignore_perms = false){ 154320f489aSMichael Hamann $result = idx_get_indexer()->lookupKey('relation_references', $id); 15554f4c056SAndreas Gohr 15663773904SAndreas Gohr if(!count($result)) return $result; 15763773904SAndreas Gohr 15863773904SAndreas Gohr // check ACL permissions 15963773904SAndreas Gohr foreach(array_keys($result) as $idx){ 16007ff0babSMichael Hamann if(($ignore_perms !== true && ( 16107ff0babSMichael Hamann isHiddenPage($result[$idx]) || auth_quickaclcheck($result[$idx]) < AUTH_READ 16207ff0babSMichael Hamann )) || !page_exists($result[$idx], '', false)){ 16363773904SAndreas Gohr unset($result[$idx]); 16463773904SAndreas Gohr } 16563773904SAndreas Gohr } 16663773904SAndreas Gohr 16754f4c056SAndreas Gohr sort($result); 16854f4c056SAndreas Gohr return $result; 16954f4c056SAndreas Gohr} 17054f4c056SAndreas Gohr 17154f4c056SAndreas Gohr/** 172a05e297aSAndreas Gohr * Returns the pages that use a given media file 173a05e297aSAndreas Gohr * 174ffec1009SMichael Hamann * Uses the relation media metadata property and the metadata index. 175a05e297aSAndreas Gohr * 176ffec1009SMichael Hamann * Note that before 2013-07-31 the second parameter was the maximum number of results and 177ffec1009SMichael Hamann * permissions were ignored. That's why the parameter is now checked to be explicitely set 178ffec1009SMichael Hamann * to true (with type bool) in order to be compatible with older uses of the function. 179ffec1009SMichael Hamann * 180ffec1009SMichael Hamann * @param string $id The media id to look for 181ffec1009SMichael Hamann * @param bool $ignore_perms Ignore hidden pages and acls (optional, default: false) 182ffec1009SMichael Hamann * @return array A list of pages that use the given media file 183a05e297aSAndreas Gohr */ 184ffec1009SMichael Hamannfunction ft_mediause($id, $ignore_perms = false){ 185ffec1009SMichael Hamann $result = idx_get_indexer()->lookupKey('relation_media', $id); 186a05e297aSAndreas Gohr 187ffec1009SMichael Hamann if(!count($result)) return $result; 188a05e297aSAndreas Gohr 189ffec1009SMichael Hamann // check ACL permissions 190ffec1009SMichael Hamann foreach(array_keys($result) as $idx){ 191ffec1009SMichael Hamann if(($ignore_perms !== true && ( 192ffec1009SMichael Hamann isHiddenPage($result[$idx]) || auth_quickaclcheck($result[$idx]) < AUTH_READ 193ffec1009SMichael Hamann )) || !page_exists($result[$idx], '', false)){ 194ffec1009SMichael Hamann unset($result[$idx]); 195a05e297aSAndreas Gohr } 196a05e297aSAndreas Gohr } 197a05e297aSAndreas Gohr 198a05e297aSAndreas Gohr sort($result); 199a05e297aSAndreas Gohr return $result; 200a05e297aSAndreas Gohr} 201a05e297aSAndreas Gohr 202a05e297aSAndreas Gohr 203a05e297aSAndreas Gohr 204a05e297aSAndreas Gohr/** 205506fa893SAndreas Gohr * Quicksearch for pagenames 206506fa893SAndreas Gohr * 207506fa893SAndreas Gohr * By default it only matches the pagename and ignores the 20880423ab6SAdrian Lang * namespace. This can be changed with the second parameter. 20980423ab6SAdrian Lang * The third parameter allows to search in titles as well. 210506fa893SAndreas Gohr * 2118d22f1e9SAndreas Gohr * The function always returns titles as well 2126840140fSChris Smith * 2138d22f1e9SAndreas Gohr * @triggers SEARCH_QUERY_PAGELOOKUP 214506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2158d22f1e9SAndreas Gohr * @author Adrian Lang <lang@cosmocode.de> 21642ea7f44SGerrit Uitslag * 21742ea7f44SGerrit Uitslag * @param string $id page id 21842ea7f44SGerrit Uitslag * @param bool $in_ns match against namespace as well? 21942ea7f44SGerrit Uitslag * @param bool $in_title search in title? 22042ea7f44SGerrit Uitslag * @return string[] 221506fa893SAndreas Gohr */ 2228d22f1e9SAndreas Gohrfunction ft_pageLookup($id, $in_ns=false, $in_title=false){ 2238d22f1e9SAndreas Gohr $data = compact('id', 'in_ns', 'in_title'); 2248d22f1e9SAndreas Gohr $data['has_titles'] = true; // for plugin backward compatibility check 2256840140fSChris Smith return trigger_event('SEARCH_QUERY_PAGELOOKUP', $data, '_ft_pageLookup'); 2266840140fSChris Smith} 2276840140fSChris Smith 22842ea7f44SGerrit Uitslag/** 22942ea7f44SGerrit Uitslag * Returns list of pages as array(pageid => First Heading) 23042ea7f44SGerrit Uitslag * 23142ea7f44SGerrit Uitslag * @param array &$data event data 23242ea7f44SGerrit Uitslag * @return string[] 23342ea7f44SGerrit Uitslag */ 2346840140fSChris Smithfunction _ft_pageLookup(&$data){ 23580423ab6SAdrian Lang // split out original parameters 2366840140fSChris Smith $id = $data['id']; 237940f24fcSMichael Große $Indexer = idx_get_indexer(); 238940f24fcSMichael Große $parsedQuery = ft_queryParser($Indexer, $id); 239940f24fcSMichael Große if (count($parsedQuery['ns']) > 0) { 240940f24fcSMichael Große $ns = cleanID($parsedQuery['ns'][0]) . ':'; 241940f24fcSMichael Große $id = implode(' ', $parsedQuery['highlight']); 242b0f6db0cSAdrian Lang } 243b0f6db0cSAdrian Lang 2448d22f1e9SAndreas Gohr $in_ns = $data['in_ns']; 2458d22f1e9SAndreas Gohr $in_title = $data['in_title']; 24680423ab6SAdrian Lang $cleaned = cleanID($id); 2479b41be24STom N Harris 2489b41be24STom N Harris $Indexer = idx_get_indexer(); 2499b41be24STom N Harris $page_idx = $Indexer->getPages(); 2509b41be24STom N Harris 2519b41be24STom N Harris $pages = array(); 2525479a8c3SAndreas Gohr if ($id !== '' && $cleaned !== '') { 2539b41be24STom N Harris foreach ($page_idx as $p_id) { 2549b41be24STom N Harris if ((strpos($in_ns ? $p_id : noNSorNS($p_id), $cleaned) !== false)) { 2559b41be24STom N Harris if (!isset($pages[$p_id])) 25667c15eceSMichael Hamann $pages[$p_id] = p_get_first_heading($p_id, METADATA_DONT_RENDER); 257506fa893SAndreas Gohr } 258506fa893SAndreas Gohr } 259f078bb00STom N Harris if ($in_title) { 260c66f16a3SMichael Hamann foreach ($Indexer->lookupKey('title', $id, '_ft_pageLookupTitleCompare') as $p_id) { 261f078bb00STom N Harris if (!isset($pages[$p_id])) 26267c15eceSMichael Hamann $pages[$p_id] = p_get_first_heading($p_id, METADATA_DONT_RENDER); 263f078bb00STom N Harris } 264f078bb00STom N Harris } 265d0bdf765SAdrian Lang } 2660c074a52SMichael Hamann 267d0bdf765SAdrian Lang if (isset($ns)) { 2680c074a52SMichael Hamann foreach (array_keys($pages) as $p_id) { 2690c074a52SMichael Hamann if (strpos($p_id, $ns) !== 0) { 2700c074a52SMichael Hamann unset($pages[$p_id]); 271d0bdf765SAdrian Lang } 272d0bdf765SAdrian Lang } 273506fa893SAndreas Gohr } 27463773904SAndreas Gohr 27580423ab6SAdrian Lang // discard hidden pages 27680423ab6SAdrian Lang // discard nonexistent pages 27763773904SAndreas Gohr // check ACL permissions 27863773904SAndreas Gohr foreach(array_keys($pages) as $idx){ 27980423ab6SAdrian Lang if(!isVisiblePage($idx) || !page_exists($idx) || 28080423ab6SAdrian Lang auth_quickaclcheck($idx) < AUTH_READ) { 28163773904SAndreas Gohr unset($pages[$idx]); 28263773904SAndreas Gohr } 28363773904SAndreas Gohr } 28463773904SAndreas Gohr 285*1b48999cSMichael Große $pages = _ft_filterResultsByTime($pages); 286*1b48999cSMichael Große 2873d2017d9SAdrian Lang uksort($pages,'ft_pagesorter'); 2888d22f1e9SAndreas Gohr return $pages; 289506fa893SAndreas Gohr} 290506fa893SAndreas Gohr 291*1b48999cSMichael Große 292*1b48999cSMichael Große/** 293*1b48999cSMichael Große * @param array $results search results in the form pageid => value 294*1b48999cSMichael Große * 295*1b48999cSMichael Große * @return array 296*1b48999cSMichael Große */ 297*1b48999cSMichael Großefunction _ft_filterResultsByTime(array $results) { 298*1b48999cSMichael Große global $INPUT; 299*1b48999cSMichael Große if ($INPUT->has('after') || $INPUT->has('before')) { 300*1b48999cSMichael Große $after = $INPUT->str('after'); 301*1b48999cSMichael Große $after = is_int($after) ? $after : strtotime($after); 302*1b48999cSMichael Große 303*1b48999cSMichael Große $before = $INPUT->str('before'); 304*1b48999cSMichael Große $before = is_int($before) ? $before : strtotime($before); 305*1b48999cSMichael Große 306*1b48999cSMichael Große foreach ($results as $id => $value) { 307*1b48999cSMichael Große $mTime = filemtime(wikiFN($id)); 308*1b48999cSMichael Große if ($after && $after > $mTime) { 309*1b48999cSMichael Große unset($results[$id]); 310*1b48999cSMichael Große continue; 311*1b48999cSMichael Große } 312*1b48999cSMichael Große if ($before && $before < $mTime) { 313*1b48999cSMichael Große unset($results[$id]); 314*1b48999cSMichael Große } 315*1b48999cSMichael Große } 316*1b48999cSMichael Große } 317*1b48999cSMichael Große 318*1b48999cSMichael Große return $results; 319*1b48999cSMichael Große} 320*1b48999cSMichael Große 321506fa893SAndreas Gohr/** 322c66f16a3SMichael Hamann * Tiny helper function for comparing the searched title with the title 323c66f16a3SMichael Hamann * from the search index. This function is a wrapper around stripos with 324c66f16a3SMichael Hamann * adapted argument order and return value. 32542ea7f44SGerrit Uitslag * 32642ea7f44SGerrit Uitslag * @param string $search searched title 32742ea7f44SGerrit Uitslag * @param string $title title from index 32842ea7f44SGerrit Uitslag * @return bool 329c66f16a3SMichael Hamann */ 330c66f16a3SMichael Hamannfunction _ft_pageLookupTitleCompare($search, $title) { 331c66f16a3SMichael Hamann return stripos($title, $search) !== false; 332c66f16a3SMichael Hamann} 333c66f16a3SMichael Hamann 334c66f16a3SMichael Hamann/** 335f31eb72bSAndreas Gohr * Sort pages based on their namespace level first, then on their string 336f31eb72bSAndreas Gohr * values. This makes higher hierarchy pages rank higher than lower hierarchy 337f31eb72bSAndreas Gohr * pages. 33842ea7f44SGerrit Uitslag * 33942ea7f44SGerrit Uitslag * @param string $a 34042ea7f44SGerrit Uitslag * @param string $b 34142ea7f44SGerrit Uitslag * @return int Returns < 0 if $a is less than $b; > 0 if $a is greater than $b, and 0 if they are equal. 342f31eb72bSAndreas Gohr */ 343f31eb72bSAndreas Gohrfunction ft_pagesorter($a, $b){ 344f31eb72bSAndreas Gohr $ac = count(explode(':',$a)); 345f31eb72bSAndreas Gohr $bc = count(explode(':',$b)); 346f31eb72bSAndreas Gohr if($ac < $bc){ 347f31eb72bSAndreas Gohr return -1; 348f31eb72bSAndreas Gohr }elseif($ac > $bc){ 349f31eb72bSAndreas Gohr return 1; 350f31eb72bSAndreas Gohr } 351f31eb72bSAndreas Gohr return strcmp ($a,$b); 352f31eb72bSAndreas Gohr} 353f31eb72bSAndreas Gohr 354f31eb72bSAndreas Gohr/** 355506fa893SAndreas Gohr * Creates a snippet extract 356506fa893SAndreas Gohr * 357506fa893SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 35860e91a17SAndreas Gohr * @triggers FULLTEXT_SNIPPET_CREATE 35942ea7f44SGerrit Uitslag * 36042ea7f44SGerrit Uitslag * @param string $id page id 36142ea7f44SGerrit Uitslag * @param array $highlight 36242ea7f44SGerrit Uitslag * @return mixed 363506fa893SAndreas Gohr */ 364546d3a99SAndreas Gohrfunction ft_snippet($id,$highlight){ 365506fa893SAndreas Gohr $text = rawWiki($id); 3664f0030ddSAndreas Gohr $text = str_replace("\xC2\xAD",'',$text); // remove soft-hyphens 36760e91a17SAndreas Gohr $evdata = array( 36860e91a17SAndreas Gohr 'id' => $id, 36960e91a17SAndreas Gohr 'text' => &$text, 37060e91a17SAndreas Gohr 'highlight' => &$highlight, 37160e91a17SAndreas Gohr 'snippet' => '', 37260e91a17SAndreas Gohr ); 37360e91a17SAndreas Gohr 37460e91a17SAndreas Gohr $evt = new Doku_Event('FULLTEXT_SNIPPET_CREATE',$evdata); 37560e91a17SAndreas Gohr if ($evt->advise_before()) { 376ced0762eSchris $match = array(); 377ced0762eSchris $snippets = array(); 3789ee93076Schris $utf8_offset = $offset = $end = 0; 379ced0762eSchris $len = utf8_strlen($text); 3809ee93076Schris 381546d3a99SAndreas Gohr // build a regexp from the phrases to highlight 3822237b4faSAndreas Gohr $re1 = '('.join('|',array_map('ft_snippet_re_preprocess', array_map('preg_quote_cb',array_filter((array) $highlight)))).')'; 383b571ff2dSChuck Kollars $re2 = "$re1.{0,75}(?!\\1)$re1"; 384b571ff2dSChuck Kollars $re3 = "$re1.{0,45}(?!\\1)$re1.{0,45}(?!\\1)(?!\\2)$re1"; 385546d3a99SAndreas Gohr 386b571ff2dSChuck Kollars for ($cnt=4; $cnt--;) { 387b571ff2dSChuck Kollars if (0) { 388b571ff2dSChuck Kollars } else if (preg_match('/'.$re3.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) { 389b571ff2dSChuck Kollars } else if (preg_match('/'.$re2.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) { 390b571ff2dSChuck Kollars } else if (preg_match('/'.$re1.'/iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) { 391b571ff2dSChuck Kollars } else { 392b571ff2dSChuck Kollars break; 393b571ff2dSChuck Kollars } 394ced0762eSchris 395ced0762eSchris list($str,$idx) = $match[0]; 396ced0762eSchris 397ced0762eSchris // convert $idx (a byte offset) into a utf8 character offset 398ced0762eSchris $utf8_idx = utf8_strlen(substr($text,0,$idx)); 399ced0762eSchris $utf8_len = utf8_strlen($str); 400ced0762eSchris 401ced0762eSchris // establish context, 100 bytes surrounding the match string 402ced0762eSchris // first look to see if we can go 100 either side, 403ced0762eSchris // then drop to 50 adding any excess if the other side can't go to 50, 404ced0762eSchris $pre = min($utf8_idx-$utf8_offset,100); 405ced0762eSchris $post = min($len-$utf8_idx-$utf8_len,100); 406ced0762eSchris 407ced0762eSchris if ($pre>50 && $post>50) { 408ced0762eSchris $pre = $post = 50; 409ced0762eSchris } else if ($pre>50) { 410ced0762eSchris $pre = min($pre,100-$post); 411ced0762eSchris } else if ($post>50) { 412ced0762eSchris $post = min($post, 100-$pre); 413ef3e3cddSMichael Hamann } else if ($offset == 0) { 414ced0762eSchris // both are less than 50, means the context is the whole string 41510ffc9ddSAndreas Gohr // make it so and break out of this loop - there is no need for the 41610ffc9ddSAndreas Gohr // complex snippet calculations 417ced0762eSchris $snippets = array($text); 418ced0762eSchris break; 419ced0762eSchris } 420ced0762eSchris 42110ffc9ddSAndreas Gohr // establish context start and end points, try to append to previous 42210ffc9ddSAndreas Gohr // context if possible 4239ee93076Schris $start = $utf8_idx - $pre; 424ced0762eSchris $append = ($start < $end) ? $end : false; // still the end of the previous context snippet 4259ee93076Schris $end = $utf8_idx + $utf8_len + $post; // now set it to the end of this context 426ced0762eSchris 427ced0762eSchris if ($append) { 428ced0762eSchris $snippets[count($snippets)-1] .= utf8_substr($text,$append,$end-$append); 429ced0762eSchris } else { 430ced0762eSchris $snippets[] = utf8_substr($text,$start,$end-$start); 431ced0762eSchris } 432ced0762eSchris 433ced0762eSchris // set $offset for next match attempt 43443d58b76SMichael Hamann // continue matching after the current match 43543d58b76SMichael Hamann // if the current match is not the longest possible match starting at the current offset 43643d58b76SMichael Hamann // this prevents further matching of this snippet but for possible matches of length 43743d58b76SMichael Hamann // smaller than match length + context (at least 50 characters) this match is part of the context 43843d58b76SMichael Hamann $utf8_offset = $utf8_idx + $utf8_len; 43943d58b76SMichael Hamann $offset = $idx + strlen(utf8_substr($text,$utf8_idx,$utf8_len)); 4409ee93076Schris $offset = utf8_correctIdx($text,$offset); 4419ee93076Schris } 4429ee93076Schris 443ced0762eSchris $m = "\1"; 444b571ff2dSChuck Kollars $snippets = preg_replace('/'.$re1.'/iu',$m.'$1'.$m,$snippets); 445b571ff2dSChuck Kollars $snippet = preg_replace('/'.$m.'([^'.$m.']*?)'.$m.'/iu','<strong class="search_hit">$1</strong>',hsc(join('... ',$snippets))); 446bd2cb6fcSchris 44760e91a17SAndreas Gohr $evdata['snippet'] = $snippet; 44860e91a17SAndreas Gohr } 44960e91a17SAndreas Gohr $evt->advise_after(); 45060e91a17SAndreas Gohr unset($evt); 45160e91a17SAndreas Gohr 45260e91a17SAndreas Gohr return $evdata['snippet']; 453506fa893SAndreas Gohr} 454506fa893SAndreas Gohr 455506fa893SAndreas Gohr/** 45626eb848cSGina Haeussge * Wraps a search term in regex boundary checks. 45742ea7f44SGerrit Uitslag * 45842ea7f44SGerrit Uitslag * @param string $term 45942ea7f44SGerrit Uitslag * @return string 46026eb848cSGina Haeussge */ 4612237b4faSAndreas Gohrfunction ft_snippet_re_preprocess($term) { 46235594613SKazutaka Miyasaka // do not process asian terms where word boundaries are not explicit 46335594613SKazutaka Miyasaka if(preg_match('/'.IDX_ASIAN.'/u',$term)){ 46435594613SKazutaka Miyasaka return $term; 46535594613SKazutaka Miyasaka } 46635594613SKazutaka Miyasaka 4673161005dSAndreas Gohr if (UTF8_PROPERTYSUPPORT) { 46884e581a6SAndreas Gohr // unicode word boundaries 46984e581a6SAndreas Gohr // see http://stackoverflow.com/a/2449017/172068 47084e581a6SAndreas Gohr $BL = '(?<!\pL)'; 47184e581a6SAndreas Gohr $BR = '(?!\pL)'; 4723161005dSAndreas Gohr } else { 4733161005dSAndreas Gohr // not as correct as above, but at least won't break 4743161005dSAndreas Gohr $BL = '\b'; 4753161005dSAndreas Gohr $BR = '\b'; 4763161005dSAndreas Gohr } 4773161005dSAndreas Gohr 4782237b4faSAndreas Gohr if(substr($term,0,2) == '\\*'){ 4792237b4faSAndreas Gohr $term = substr($term,2); 4802237b4faSAndreas Gohr }else{ 48184e581a6SAndreas Gohr $term = $BL.$term; 4822237b4faSAndreas Gohr } 4832237b4faSAndreas Gohr 4842237b4faSAndreas Gohr if(substr($term,-2,2) == '\\*'){ 4852237b4faSAndreas Gohr $term = substr($term,0,-2); 4862237b4faSAndreas Gohr }else{ 48784e581a6SAndreas Gohr $term = $term.$BR; 4882237b4faSAndreas Gohr } 4898a803caeSAndreas Gohr 49084e581a6SAndreas Gohr if($term == $BL || $term == $BR || $term == $BL.$BR) $term = ''; 4912237b4faSAndreas Gohr return $term; 49226eb848cSGina Haeussge} 49326eb848cSGina Haeussge 49426eb848cSGina Haeussge/** 495f5eb7cf0SAndreas Gohr * Combine found documents and sum up their scores 496f5eb7cf0SAndreas Gohr * 497f5eb7cf0SAndreas Gohr * This function is used to combine searched words with a logical 498f5eb7cf0SAndreas Gohr * AND. Only documents available in all arrays are returned. 499f5eb7cf0SAndreas Gohr * 500f5eb7cf0SAndreas Gohr * based upon PEAR's PHP_Compat function for array_intersect_key() 501f5eb7cf0SAndreas Gohr * 502f5eb7cf0SAndreas Gohr * @param array $args An array of page arrays 50342ea7f44SGerrit Uitslag * @return array 504f5eb7cf0SAndreas Gohr */ 505f5eb7cf0SAndreas Gohrfunction ft_resultCombine($args){ 506f5eb7cf0SAndreas Gohr $array_count = count($args); 507134f4ab2SAndreas Gohr if($array_count == 1){ 508134f4ab2SAndreas Gohr return $args[0]; 509134f4ab2SAndreas Gohr } 510134f4ab2SAndreas Gohr 511f5eb7cf0SAndreas Gohr $result = array(); 51209c27a6dSGuy Brand if ($array_count > 1) { 513a21136cdSAndreas Gohr foreach ($args[0] as $key => $value) { 514a21136cdSAndreas Gohr $result[$key] = $value; 515f5eb7cf0SAndreas Gohr for ($i = 1; $i !== $array_count; $i++) { 516a21136cdSAndreas Gohr if (!isset($args[$i][$key])) { 517a21136cdSAndreas Gohr unset($result[$key]); 518a21136cdSAndreas Gohr break; 519f5eb7cf0SAndreas Gohr } 520a21136cdSAndreas Gohr $result[$key] += $args[$i][$key]; 521f5eb7cf0SAndreas Gohr } 522f5eb7cf0SAndreas Gohr } 52309c27a6dSGuy Brand } 524f5eb7cf0SAndreas Gohr return $result; 525f5eb7cf0SAndreas Gohr} 526f5eb7cf0SAndreas Gohr 527f5eb7cf0SAndreas Gohr/** 528865c2687SKazutaka Miyasaka * Unites found documents and sum up their scores 529f5eb7cf0SAndreas Gohr * 530865c2687SKazutaka Miyasaka * based upon ft_resultCombine() function 531865c2687SKazutaka Miyasaka * 532865c2687SKazutaka Miyasaka * @param array $args An array of page arrays 53342ea7f44SGerrit Uitslag * @return array 53442ea7f44SGerrit Uitslag * 535865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 536865c2687SKazutaka Miyasaka */ 537865c2687SKazutaka Miyasakafunction ft_resultUnite($args) { 538865c2687SKazutaka Miyasaka $array_count = count($args); 539865c2687SKazutaka Miyasaka if ($array_count === 1) { 540865c2687SKazutaka Miyasaka return $args[0]; 541865c2687SKazutaka Miyasaka } 542865c2687SKazutaka Miyasaka 543865c2687SKazutaka Miyasaka $result = $args[0]; 544865c2687SKazutaka Miyasaka for ($i = 1; $i !== $array_count; $i++) { 545865c2687SKazutaka Miyasaka foreach (array_keys($args[$i]) as $id) { 546865c2687SKazutaka Miyasaka $result[$id] += $args[$i][$id]; 547865c2687SKazutaka Miyasaka } 548865c2687SKazutaka Miyasaka } 549865c2687SKazutaka Miyasaka return $result; 550865c2687SKazutaka Miyasaka} 551865c2687SKazutaka Miyasaka 552865c2687SKazutaka Miyasaka/** 553865c2687SKazutaka Miyasaka * Computes the difference of documents using page id for comparison 554865c2687SKazutaka Miyasaka * 555865c2687SKazutaka Miyasaka * nearly identical to PHP5's array_diff_key() 556865c2687SKazutaka Miyasaka * 557865c2687SKazutaka Miyasaka * @param array $args An array of page arrays 55842ea7f44SGerrit Uitslag * @return array 55942ea7f44SGerrit Uitslag * 560865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 561865c2687SKazutaka Miyasaka */ 562865c2687SKazutaka Miyasakafunction ft_resultComplement($args) { 563865c2687SKazutaka Miyasaka $array_count = count($args); 564865c2687SKazutaka Miyasaka if ($array_count === 1) { 565865c2687SKazutaka Miyasaka return $args[0]; 566865c2687SKazutaka Miyasaka } 567865c2687SKazutaka Miyasaka 568865c2687SKazutaka Miyasaka $result = $args[0]; 569865c2687SKazutaka Miyasaka foreach (array_keys($result) as $id) { 570865c2687SKazutaka Miyasaka for ($i = 1; $i !== $array_count; $i++) { 571865c2687SKazutaka Miyasaka if (isset($args[$i][$id])) unset($result[$id]); 572865c2687SKazutaka Miyasaka } 573865c2687SKazutaka Miyasaka } 574865c2687SKazutaka Miyasaka return $result; 575865c2687SKazutaka Miyasaka} 576865c2687SKazutaka Miyasaka 577865c2687SKazutaka Miyasaka/** 578865c2687SKazutaka Miyasaka * Parses a search query and builds an array of search formulas 579865c2687SKazutaka Miyasaka * 580865c2687SKazutaka Miyasaka * @author Andreas Gohr <andi@splitbrain.org> 581865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 58242ea7f44SGerrit Uitslag * 58342ea7f44SGerrit Uitslag * @param Doku_Indexer $Indexer 58442ea7f44SGerrit Uitslag * @param string $query search query 58542ea7f44SGerrit Uitslag * @return array of search formulas 586f5eb7cf0SAndreas Gohr */ 5879b41be24STom N Harrisfunction ft_queryParser($Indexer, $query){ 588865c2687SKazutaka Miyasaka /** 589865c2687SKazutaka Miyasaka * parse a search query and transform it into intermediate representation 590865c2687SKazutaka Miyasaka * 591865c2687SKazutaka Miyasaka * in a search query, you can use the following expressions: 592865c2687SKazutaka Miyasaka * 593865c2687SKazutaka Miyasaka * words: 594865c2687SKazutaka Miyasaka * include 595865c2687SKazutaka Miyasaka * -exclude 596865c2687SKazutaka Miyasaka * phrases: 597865c2687SKazutaka Miyasaka * "phrase to be included" 598865c2687SKazutaka Miyasaka * -"phrase you want to exclude" 599865c2687SKazutaka Miyasaka * namespaces: 600865c2687SKazutaka Miyasaka * @include:namespace (or ns:include:namespace) 601865c2687SKazutaka Miyasaka * ^exclude:namespace (or -ns:exclude:namespace) 602865c2687SKazutaka Miyasaka * groups: 603865c2687SKazutaka Miyasaka * () 604865c2687SKazutaka Miyasaka * -() 605865c2687SKazutaka Miyasaka * operators: 606865c2687SKazutaka Miyasaka * and ('and' is the default operator: you can always omit this) 6077871d415SKazutaka Miyasaka * or (or pipe symbol '|', lower precedence than 'and') 608865c2687SKazutaka Miyasaka * 609865c2687SKazutaka Miyasaka * e.g. a query [ aa "bb cc" @dd:ee ] means "search pages which contain 610865c2687SKazutaka Miyasaka * a word 'aa', a phrase 'bb cc' and are within a namespace 'dd:ee'". 611865c2687SKazutaka Miyasaka * this query is equivalent to [ -(-aa or -"bb cc" or -ns:dd:ee) ] 612865c2687SKazutaka Miyasaka * as long as you don't mind hit counts. 613865c2687SKazutaka Miyasaka * 614865c2687SKazutaka Miyasaka * intermediate representation consists of the following parts: 615865c2687SKazutaka Miyasaka * 616865c2687SKazutaka Miyasaka * ( ) - group 617865c2687SKazutaka Miyasaka * AND - logical and 618865c2687SKazutaka Miyasaka * OR - logical or 619865c2687SKazutaka Miyasaka * NOT - logical not 6202f502d70SKazutaka Miyasaka * W+:, W-:, W_: - word (underscore: no need to highlight) 6212f502d70SKazutaka Miyasaka * P+:, P-: - phrase (minus sign: logically in NOT group) 6222f502d70SKazutaka Miyasaka * N+:, N-: - namespace 623865c2687SKazutaka Miyasaka */ 624865c2687SKazutaka Miyasaka $parsed_query = ''; 625865c2687SKazutaka Miyasaka $parens_level = 0; 626865c2687SKazutaka Miyasaka $terms = preg_split('/(-?".*?")/u', utf8_strtolower($query), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 627865c2687SKazutaka Miyasaka 628865c2687SKazutaka Miyasaka foreach ($terms as $term) { 629865c2687SKazutaka Miyasaka $parsed = ''; 630865c2687SKazutaka Miyasaka if (preg_match('/^(-?)"(.+)"$/u', $term, $matches)) { 631865c2687SKazutaka Miyasaka // phrase-include and phrase-exclude 632865c2687SKazutaka Miyasaka $not = $matches[1] ? 'NOT' : ''; 6339b41be24STom N Harris $parsed = $not.ft_termParser($Indexer, $matches[2], false, true); 634f5eb7cf0SAndreas Gohr } else { 635865c2687SKazutaka Miyasaka // fix incomplete phrase 636865c2687SKazutaka Miyasaka $term = str_replace('"', ' ', $term); 637865c2687SKazutaka Miyasaka 638865c2687SKazutaka Miyasaka // fix parentheses 639865c2687SKazutaka Miyasaka $term = str_replace(')' , ' ) ', $term); 640865c2687SKazutaka Miyasaka $term = str_replace('(' , ' ( ', $term); 641865c2687SKazutaka Miyasaka $term = str_replace('- (', ' -(', $term); 642865c2687SKazutaka Miyasaka 6437871d415SKazutaka Miyasaka // treat pipe symbols as 'OR' operators 6447871d415SKazutaka Miyasaka $term = str_replace('|', ' or ', $term); 6457871d415SKazutaka Miyasaka 646865c2687SKazutaka Miyasaka // treat ideographic spaces (U+3000) as search term separators 647865c2687SKazutaka Miyasaka // FIXME: some more separators? 648865c2687SKazutaka Miyasaka $term = preg_replace('/[ \x{3000}]+/u', ' ', $term); 649865c2687SKazutaka Miyasaka $term = trim($term); 650865c2687SKazutaka Miyasaka if ($term === '') continue; 651865c2687SKazutaka Miyasaka 652865c2687SKazutaka Miyasaka $tokens = explode(' ', $term); 653865c2687SKazutaka Miyasaka foreach ($tokens as $token) { 654865c2687SKazutaka Miyasaka if ($token === '(') { 655865c2687SKazutaka Miyasaka // parenthesis-include-open 656865c2687SKazutaka Miyasaka $parsed .= '('; 657865c2687SKazutaka Miyasaka ++$parens_level; 658865c2687SKazutaka Miyasaka } elseif ($token === '-(') { 659865c2687SKazutaka Miyasaka // parenthesis-exclude-open 660865c2687SKazutaka Miyasaka $parsed .= 'NOT('; 661865c2687SKazutaka Miyasaka ++$parens_level; 662865c2687SKazutaka Miyasaka } elseif ($token === ')') { 663865c2687SKazutaka Miyasaka // parenthesis-any-close 664865c2687SKazutaka Miyasaka if ($parens_level === 0) continue; 665865c2687SKazutaka Miyasaka $parsed .= ')'; 666865c2687SKazutaka Miyasaka $parens_level--; 667865c2687SKazutaka Miyasaka } elseif ($token === 'and') { 668865c2687SKazutaka Miyasaka // logical-and (do nothing) 669865c2687SKazutaka Miyasaka } elseif ($token === 'or') { 670865c2687SKazutaka Miyasaka // logical-or 671865c2687SKazutaka Miyasaka $parsed .= 'OR'; 672865c2687SKazutaka Miyasaka } elseif (preg_match('/^(?:\^|-ns:)(.+)$/u', $token, $matches)) { 673865c2687SKazutaka Miyasaka // namespace-exclude 6742f502d70SKazutaka Miyasaka $parsed .= 'NOT(N+:'.$matches[1].')'; 675865c2687SKazutaka Miyasaka } elseif (preg_match('/^(?:@|ns:)(.+)$/u', $token, $matches)) { 676865c2687SKazutaka Miyasaka // namespace-include 6772f502d70SKazutaka Miyasaka $parsed .= '(N+:'.$matches[1].')'; 678865c2687SKazutaka Miyasaka } elseif (preg_match('/^-(.+)$/', $token, $matches)) { 679865c2687SKazutaka Miyasaka // word-exclude 6809b41be24STom N Harris $parsed .= 'NOT('.ft_termParser($Indexer, $matches[1]).')'; 681865c2687SKazutaka Miyasaka } else { 682865c2687SKazutaka Miyasaka // word-include 6839b41be24STom N Harris $parsed .= ft_termParser($Indexer, $token); 684865c2687SKazutaka Miyasaka } 685865c2687SKazutaka Miyasaka } 686865c2687SKazutaka Miyasaka } 687865c2687SKazutaka Miyasaka $parsed_query .= $parsed; 688f5eb7cf0SAndreas Gohr } 689f5eb7cf0SAndreas Gohr 690865c2687SKazutaka Miyasaka // cleanup (very sensitive) 691865c2687SKazutaka Miyasaka $parsed_query .= str_repeat(')', $parens_level); 692865c2687SKazutaka Miyasaka do { 693865c2687SKazutaka Miyasaka $parsed_query_old = $parsed_query; 694865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/(NOT)?\(\)/u', '', $parsed_query); 695865c2687SKazutaka Miyasaka } while ($parsed_query !== $parsed_query_old); 696865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/(NOT|OR)+\)/u', ')' , $parsed_query); 697865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/(OR)+/u' , 'OR' , $parsed_query); 698865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/\(OR/u' , '(' , $parsed_query); 699865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/^OR|OR$/u' , '' , $parsed_query); 700865c2687SKazutaka Miyasaka $parsed_query = preg_replace('/\)(NOT)?\(/u' , ')AND$1(', $parsed_query); 701865c2687SKazutaka Miyasaka 7022f502d70SKazutaka Miyasaka // adjustment: make highlightings right 7032f502d70SKazutaka Miyasaka $parens_level = 0; 7042f502d70SKazutaka Miyasaka $notgrp_levels = array(); 7052f502d70SKazutaka Miyasaka $parsed_query_new = ''; 7062f502d70SKazutaka Miyasaka $tokens = preg_split('/(NOT\(|[()])/u', $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 7072f502d70SKazutaka Miyasaka foreach ($tokens as $token) { 7082f502d70SKazutaka Miyasaka if ($token === 'NOT(') { 7092f502d70SKazutaka Miyasaka $notgrp_levels[] = ++$parens_level; 7102f502d70SKazutaka Miyasaka } elseif ($token === '(') { 7112f502d70SKazutaka Miyasaka ++$parens_level; 7122f502d70SKazutaka Miyasaka } elseif ($token === ')') { 7132f502d70SKazutaka Miyasaka if ($parens_level-- === end($notgrp_levels)) array_pop($notgrp_levels); 7142f502d70SKazutaka Miyasaka } elseif (count($notgrp_levels) % 2 === 1) { 7152f502d70SKazutaka Miyasaka // turn highlight-flag off if terms are logically in "NOT" group 7162f502d70SKazutaka Miyasaka $token = preg_replace('/([WPN])\+\:/u', '$1-:', $token); 7172f502d70SKazutaka Miyasaka } 7182f502d70SKazutaka Miyasaka $parsed_query_new .= $token; 7192f502d70SKazutaka Miyasaka } 7202f502d70SKazutaka Miyasaka $parsed_query = $parsed_query_new; 7212f502d70SKazutaka Miyasaka 722865c2687SKazutaka Miyasaka /** 723865c2687SKazutaka Miyasaka * convert infix notation string into postfix (Reverse Polish notation) array 724865c2687SKazutaka Miyasaka * by Shunting-yard algorithm 725865c2687SKazutaka Miyasaka * 726865c2687SKazutaka Miyasaka * see: http://en.wikipedia.org/wiki/Reverse_Polish_notation 727865c2687SKazutaka Miyasaka * see: http://en.wikipedia.org/wiki/Shunting-yard_algorithm 728865c2687SKazutaka Miyasaka */ 729865c2687SKazutaka Miyasaka $parsed_ary = array(); 730865c2687SKazutaka Miyasaka $ope_stack = array(); 731865c2687SKazutaka Miyasaka $ope_precedence = array(')' => 1, 'OR' => 2, 'AND' => 3, 'NOT' => 4, '(' => 5); 732865c2687SKazutaka Miyasaka $ope_regex = '/([()]|OR|AND|NOT)/u'; 733865c2687SKazutaka Miyasaka 734865c2687SKazutaka Miyasaka $tokens = preg_split($ope_regex, $parsed_query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 735865c2687SKazutaka Miyasaka foreach ($tokens as $token) { 736865c2687SKazutaka Miyasaka if (preg_match($ope_regex, $token)) { 737865c2687SKazutaka Miyasaka // operator 738865c2687SKazutaka Miyasaka $last_ope = end($ope_stack); 73967d812e0SMarius van Witzenburg while ($last_ope !== false && $ope_precedence[$token] <= $ope_precedence[$last_ope] && $last_ope != '(') { 740865c2687SKazutaka Miyasaka $parsed_ary[] = array_pop($ope_stack); 741865c2687SKazutaka Miyasaka $last_ope = end($ope_stack); 742865c2687SKazutaka Miyasaka } 743865c2687SKazutaka Miyasaka if ($token == ')') { 744865c2687SKazutaka Miyasaka array_pop($ope_stack); // this array_pop always deletes '(' 745865c2687SKazutaka Miyasaka } else { 746865c2687SKazutaka Miyasaka $ope_stack[] = $token; 747865c2687SKazutaka Miyasaka } 748865c2687SKazutaka Miyasaka } else { 749865c2687SKazutaka Miyasaka // operand 750865c2687SKazutaka Miyasaka $token_decoded = str_replace(array('OP', 'CP'), array('(', ')'), $token); 751865c2687SKazutaka Miyasaka $parsed_ary[] = $token_decoded; 752865c2687SKazutaka Miyasaka } 753865c2687SKazutaka Miyasaka } 754865c2687SKazutaka Miyasaka $parsed_ary = array_values(array_merge($parsed_ary, array_reverse($ope_stack))); 755865c2687SKazutaka Miyasaka 756865c2687SKazutaka Miyasaka // cleanup: each double "NOT" in RPN array actually does nothing 757865c2687SKazutaka Miyasaka $parsed_ary_count = count($parsed_ary); 758865c2687SKazutaka Miyasaka for ($i = 1; $i < $parsed_ary_count; ++$i) { 759865c2687SKazutaka Miyasaka if ($parsed_ary[$i] === 'NOT' && $parsed_ary[$i - 1] === 'NOT') { 760865c2687SKazutaka Miyasaka unset($parsed_ary[$i], $parsed_ary[$i - 1]); 761865c2687SKazutaka Miyasaka } 762865c2687SKazutaka Miyasaka } 763865c2687SKazutaka Miyasaka $parsed_ary = array_values($parsed_ary); 764865c2687SKazutaka Miyasaka 765865c2687SKazutaka Miyasaka // build return value 766f5eb7cf0SAndreas Gohr $q = array(); 767f5eb7cf0SAndreas Gohr $q['query'] = $query; 768865c2687SKazutaka Miyasaka $q['parsed_str'] = $parsed_query; 769865c2687SKazutaka Miyasaka $q['parsed_ary'] = $parsed_ary; 770f5eb7cf0SAndreas Gohr 771865c2687SKazutaka Miyasaka foreach ($q['parsed_ary'] as $token) { 772865c2687SKazutaka Miyasaka if ($token[2] !== ':') continue; 773865c2687SKazutaka Miyasaka $body = substr($token, 3); 774865c2687SKazutaka Miyasaka 775865c2687SKazutaka Miyasaka switch (substr($token, 0, 3)) { 7762f502d70SKazutaka Miyasaka case 'N+:': 777865c2687SKazutaka Miyasaka $q['ns'][] = $body; // for backward compatibility 778865c2687SKazutaka Miyasaka break; 7792f502d70SKazutaka Miyasaka case 'N-:': 7802f502d70SKazutaka Miyasaka $q['notns'][] = $body; // for backward compatibility 7812f502d70SKazutaka Miyasaka break; 7822f502d70SKazutaka Miyasaka case 'W_:': 7832f502d70SKazutaka Miyasaka $q['words'][] = $body; 7842f502d70SKazutaka Miyasaka break; 785865c2687SKazutaka Miyasaka case 'W-:': 786865c2687SKazutaka Miyasaka $q['words'][] = $body; 7872f502d70SKazutaka Miyasaka $q['not'][] = $body; // for backward compatibility 788865c2687SKazutaka Miyasaka break; 789865c2687SKazutaka Miyasaka case 'W+:': 790865c2687SKazutaka Miyasaka $q['words'][] = $body; 7912237b4faSAndreas Gohr $q['highlight'][] = $body; 7922f502d70SKazutaka Miyasaka $q['and'][] = $body; // for backward compatibility 793865c2687SKazutaka Miyasaka break; 7942f502d70SKazutaka Miyasaka case 'P-:': 7952f502d70SKazutaka Miyasaka $q['phrases'][] = $body; 7962f502d70SKazutaka Miyasaka break; 7972f502d70SKazutaka Miyasaka case 'P+:': 798865c2687SKazutaka Miyasaka $q['phrases'][] = $body; 7992237b4faSAndreas Gohr $q['highlight'][] = $body; 800865c2687SKazutaka Miyasaka break; 801865c2687SKazutaka Miyasaka } 802865c2687SKazutaka Miyasaka } 8032f502d70SKazutaka Miyasaka foreach (array('words', 'phrases', 'highlight', 'ns', 'notns', 'and', 'not') as $key) { 804865c2687SKazutaka Miyasaka $q[$key] = empty($q[$key]) ? array() : array_values(array_unique($q[$key])); 805f5eb7cf0SAndreas Gohr } 806f5eb7cf0SAndreas Gohr 807f5eb7cf0SAndreas Gohr return $q; 808f5eb7cf0SAndreas Gohr} 809f5eb7cf0SAndreas Gohr 810865c2687SKazutaka Miyasaka/** 811865c2687SKazutaka Miyasaka * Transforms given search term into intermediate representation 812865c2687SKazutaka Miyasaka * 813865c2687SKazutaka Miyasaka * This function is used in ft_queryParser() and not for general purpose use. 814865c2687SKazutaka Miyasaka * 815865c2687SKazutaka Miyasaka * @author Kazutaka Miyasaka <kazmiya@gmail.com> 81642ea7f44SGerrit Uitslag * 81742ea7f44SGerrit Uitslag * @param Doku_Indexer $Indexer 81842ea7f44SGerrit Uitslag * @param string $term 81942ea7f44SGerrit Uitslag * @param bool $consider_asian 82042ea7f44SGerrit Uitslag * @param bool $phrase_mode 82142ea7f44SGerrit Uitslag * @return string 822865c2687SKazutaka Miyasaka */ 8239b41be24STom N Harrisfunction ft_termParser($Indexer, $term, $consider_asian = true, $phrase_mode = false) { 824865c2687SKazutaka Miyasaka $parsed = ''; 825865c2687SKazutaka Miyasaka if ($consider_asian) { 826865c2687SKazutaka Miyasaka // successive asian characters need to be searched as a phrase 827865c2687SKazutaka Miyasaka $words = preg_split('/('.IDX_ASIAN.'+)/u', $term, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 828865c2687SKazutaka Miyasaka foreach ($words as $word) { 8296ac2077aSKazutaka Miyasaka $phrase_mode = $phrase_mode ? true : preg_match('/'.IDX_ASIAN.'/u', $word); 8309b41be24STom N Harris $parsed .= ft_termParser($Indexer, $word, false, $phrase_mode); 831865c2687SKazutaka Miyasaka } 832865c2687SKazutaka Miyasaka } else { 833865c2687SKazutaka Miyasaka $term_noparen = str_replace(array('(', ')'), ' ', $term); 8349b41be24STom N Harris $words = $Indexer->tokenizer($term_noparen, true); 835865c2687SKazutaka Miyasaka 8362f502d70SKazutaka Miyasaka // W_: no need to highlight 837865c2687SKazutaka Miyasaka if (empty($words)) { 838865c2687SKazutaka Miyasaka $parsed = '()'; // important: do not remove 839865c2687SKazutaka Miyasaka } elseif ($words[0] === $term) { 840865c2687SKazutaka Miyasaka $parsed = '(W+:'.$words[0].')'; 841865c2687SKazutaka Miyasaka } elseif ($phrase_mode) { 842865c2687SKazutaka Miyasaka $term_encoded = str_replace(array('(', ')'), array('OP', 'CP'), $term); 8432f502d70SKazutaka Miyasaka $parsed = '((W_:'.implode(')(W_:', $words).')(P+:'.$term_encoded.'))'; 844865c2687SKazutaka Miyasaka } else { 845865c2687SKazutaka Miyasaka $parsed = '((W+:'.implode(')(W+:', $words).'))'; 846865c2687SKazutaka Miyasaka } 847865c2687SKazutaka Miyasaka } 848865c2687SKazutaka Miyasaka return $parsed; 849865c2687SKazutaka Miyasaka} 850865c2687SKazutaka Miyasaka 85144156e11SMichael Große/** 85244156e11SMichael Große * Recreate a search query string based on parsed parts, doesn't support negated phrases and `OR` searches 85344156e11SMichael Große * 85444156e11SMichael Große * @param array $and 85544156e11SMichael Große * @param array $not 85644156e11SMichael Große * @param array $phrases 85744156e11SMichael Große * @param array $ns 85844156e11SMichael Große * @param array $notns 85944156e11SMichael Große * 86044156e11SMichael Große * @return string 86144156e11SMichael Große */ 86244156e11SMichael Großefunction ft_queryUnparser_simple(array $and, array $not, array $phrases, array $ns, array $notns) { 86344156e11SMichael Große $query = implode(' ', $and); 86444156e11SMichael Große if (!empty($not)) { 86544156e11SMichael Große $query .= ' -' . implode(' -', $not); 86644156e11SMichael Große } 86744156e11SMichael Große 86844156e11SMichael Große if (!empty($phrases)) { 86944156e11SMichael Große $query .= ' "' . implode('" "', $phrases) . '"'; 87044156e11SMichael Große } 87144156e11SMichael Große 87244156e11SMichael Große if (!empty($ns)) { 87344156e11SMichael Große $query .= ' @' . implode(' @', $ns); 87444156e11SMichael Große } 87544156e11SMichael Große 87644156e11SMichael Große if (!empty($notns)) { 87744156e11SMichael Große $query .= ' ^' . implode(' ^', $notns); 87844156e11SMichael Große } 87944156e11SMichael Große 88044156e11SMichael Große return $query; 88144156e11SMichael Große} 88244156e11SMichael Große 883e3776c06SMichael Hamann//Setup VIM: ex: et ts=4 : 884