From 546d3a9994d9f365f75e55f2f22601dc4941f5d5 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Mon, 10 Mar 2008 22:49:39 +0100 Subject: another change in highlight handling Now higlighting phrases are passed as an array which then is quoted correctly when used in a regexp. This should make phrase highlighting work completely correct. Please everyone test it. darcs-hash:20080310214939-7ad00-1abefb02dde40edeead50b4fa5c866c46b95ca3a.gz --- inc/fulltext.php | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'inc/fulltext.php') diff --git a/inc/fulltext.php b/inc/fulltext.php index a0be280bf..59053cb59 100644 --- a/inc/fulltext.php +++ b/inc/fulltext.php @@ -10,24 +10,21 @@ require_once(DOKU_INC.'inc/indexer.php'); -/** - * Wrapper around preg_quote adding the default delimiter - */ -function ft_preg_quote_cb($string){ - return preg_quote($string,'/'); -} - /** * The fulltext search * * Returns a list of matching documents for the given query * */ -function ft_pageSearch($query,&$regex){ +function ft_pageSearch($query,&$highlight){ $q = ft_queryParser($query); + $highlight = array(); + // remember for hilighting later - $regex = str_replace('*','',join('|',$q['words'])); + foreach($q['words'] as $wrd){ + $highlight[] = str_replace('*','',$wrd); + } // lookup all words found in the query $words = array_merge($q['and'],$q['not']); @@ -76,12 +73,10 @@ function ft_pageSearch($query,&$regex){ if(!count($docs)) return array(); // handle phrases if(count($q['phrases'])){ - //build a regexp $q['phrases'] = array_map('utf8_strtolower',$q['phrases']); - $q['phrases'] = array_map('ft_preg_quote_cb',$q['phrases']); // use this for higlighting later: - if($regex !== '') $regex .= '|'; - $regex .= join('|',$q['phrases']); + $highlight = array_merge($highlight,$q['phrases']); + $q['phrases'] = array_map('preg_quote_cb',$q['phrases']); // check the source of all documents for the exact phrases foreach(array_keys($docs) as $id){ $text = utf8_strtolower(rawWiki($id)); @@ -250,13 +245,16 @@ function ft_pageLookup($id,$pageonly=true){ * * @author Andreas Gohr */ -function ft_snippet($id,$re){ +function ft_snippet($id,$highlight){ $text = rawWiki($id); $match = array(); $snippets = array(); $utf8_offset = $offset = $end = 0; $len = utf8_strlen($text); + // build a regexp from the phrases to highlight + $re = join('|',array_map('preg_quote_cb',array_filter((array) $highlight))); + for ($cnt=3; $cnt--;) { if (!preg_match('#('.$re.')#iu',$text,$match,PREG_OFFSET_CAPTURE,$offset)) break; -- cgit v1.2.3