From: <ru...@us...> - 2009-06-04 08:13:08
|
Revision: 6853 http://phpwiki.svn.sourceforge.net/phpwiki/?rev=6853&view=rev Author: rurban Date: 2009-06-04 08:12:59 +0000 (Thu, 04 Jun 2009) Log Message: ----------- Add phrase_starts_with ^S != S* ^x matches only beginning of phrase, not of word x* matches any word beginning with x Modified Paths: -------------- trunk/lib/TextSearchQuery.php trunk/lib/plugin/TitleSearch.php Modified: trunk/lib/TextSearchQuery.php =================================================================== --- trunk/lib/TextSearchQuery.php 2009-06-03 14:48:58 UTC (rev 6852) +++ trunk/lib/TextSearchQuery.php 2009-06-04 08:12:59 UTC (rev 6853) @@ -13,8 +13,11 @@ * 'word' OR the substring 'page'. * <dt> auto-detect regex hints, glob-style or regex-style, and converts them * to PCRE and SQL matchers: - * <dd> "^word$" => EXACT(word) - * <dd> "^word" => STARTS_WITH(word) + * <dd> "^word$" => EXACT(phrase) + * <dd> "^word" => STARTS_WITH(phrase) + * <dd> "word$" => ENDS_WITH(phrase) + * <dd> "^word" ... => STARTS_WITH(word) + * <dd> "word$" ... => ENDS_WITH(word) * <dd> "word*" => STARTS_WITH(word) * <dd> "*word" => ENDS_WITH(word) * <dd> "/^word.* /" => REGEX(^word.*) @@ -651,7 +654,7 @@ } /** - * A word. exact or substring? + * A word. Exact or substring? */ class TextSearchQuery_node_word extends TextSearchQuery_node @@ -678,6 +681,7 @@ function regexp() { return '(?=.*)'; } function sql() { return '%'; } } + class TextSearchQuery_node_starts_with extends TextSearchQuery_node_word { var $op = "STARTS_WITH"; @@ -686,6 +690,12 @@ function sql () { return $this->_sql_quote($this->word).'%'; } } +// ^word: full phrase starts with +class TextSearchQuery_phrase_starts_with +extends TextSearchQuery_node_starts_with { + function regexp() { return '(?=^' . preg_quote($this->word, '/') . ')'; } +} + class TextSearchQuery_node_ends_with extends TextSearchQuery_node_word { var $op = "ENDS_WITH"; @@ -694,6 +704,12 @@ function sql () { return '%'.$this->_sql_quote($this->word); } } +// word$: full phrase ends with +class TextSearchQuery_phrase_ends_with +extends TextSearchQuery_node_ends_with { + function regexp() { return '(?=' . preg_quote($this->word, '/') . '$)'; } +} + class TextSearchQuery_node_exact extends TextSearchQuery_node_word { var $op = "EXACT"; @@ -935,7 +951,7 @@ * /"[^"]*"/ WORD * /'[^']*'/ WORD * - * ^WORD STARTS_WITH + * ^WORD TextSearchQuery_phrase_starts_with * WORD* STARTS_WITH * *WORD ENDS_WITH * ^WORD$ EXACT @@ -972,6 +988,12 @@ else return false; } + if ($is_toplevel and count($list) == 1) { + if ($this->lexer->query_str[0] == '^') + return new TextSearchQuery_phrase_starts_with($list[0]->word); + else + return $list[0]; + } return new TextSearchQuery_node_and($list); } @@ -1038,7 +1060,14 @@ if ( $accept & $const and (($word = $this->lexer->get($const)) !== false)) { - $classname = "TextSearchQuery_node_".strtolower($tok); + // phrase or word level? + if ($tok == 'STARTS_WITH' and $this->lexer->query_str[0] == '^') + $classname = "TextSearchQuery_phrase_".strtolower($tok); + elseif ($tok == 'ENDS_WITH' and + string_ends_with($this->lexer->query_str,'$')) + $classname = "TextSearchQuery_phrase_".strtolower($tok); + else + $classname = "TextSearchQuery_node_".strtolower($tok); return new $classname($word); } } @@ -1051,6 +1080,7 @@ $regex=TSQ_REGEX_AUTO) { $this->tokens = $this->tokenize($query_str, $case_exact, $regex); + $this->query_str = $query_str; $this->pos = 0; } Modified: trunk/lib/plugin/TitleSearch.php =================================================================== --- trunk/lib/plugin/TitleSearch.php 2009-06-03 14:48:58 UTC (rev 6852) +++ trunk/lib/plugin/TitleSearch.php 2009-06-04 08:12:59 UTC (rev 6853) @@ -77,6 +77,8 @@ return HTML(); } + // ^S != S* ^ matches only beginning of phrase, not of word. + // x* matches any word beginning with x $query = new TextSearchQuery($args['s'], $args['case_exact'], $args['regex']); $pages = $dbi->titleSearch($query,$args['sortby'],$args['limit'],$args['exclude']); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |