From 5191ecc572eb11876d0d3f9cc68af99db35ac715 Mon Sep 17 00:00:00 2001 From: rurban Date: Tue, 23 Nov 2004 13:35:49 +0000 Subject: [PATCH] add case_exact search git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@4166 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/PageList.php | 18 +++++++++++------- lib/TextSearchQuery.php | 30 ++++++++++++++++++------------ lib/WikiDB.php | 25 ++++++++++++++----------- lib/WikiDB/backend.php | 10 +++++----- lib/WikiDB/backend/ADODB.php | 7 +++++-- lib/WikiDB/backend/PearDB.php | 34 +++++++++++++++++++++++++--------- lib/WikiDB/backend/cvs.php | 3 ++- lib/WikiDB/backend/dbaBase.php | 5 ++--- lib/WikiDB/backend/file.php | 7 +++++-- lib/plugin/TitleSearch.php | 30 ++++++++++++++++++------------ lib/plugin/WantedPages.php | 13 ++++++++++--- 11 files changed, 115 insertions(+), 67 deletions(-) diff --git a/lib/PageList.php b/lib/PageList.php index 09ebf45b2..10ffe851b 100644 --- a/lib/PageList.php +++ b/lib/PageList.php @@ -1,4 +1,4 @@ -getDbh(); - $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit); + // $dbi->titleSearch($input); + $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude); while ($pagehandle = $allPagehandles->next()) { $allPages[] = $pagehandle->getName(); } @@ -820,7 +821,7 @@ class PageList { } } - function allPagesByAuthor(&$wildcard, $include_empty=false, $sortby=false, $limit=false) { + function allPagesByAuthor($wildcard, $include_empty=false, $sortby=false, $limit=false) { $dbi = $GLOBALS['request']->getDbh(); $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit); $allPages = array(); @@ -845,7 +846,7 @@ class PageList { return $allPages; } - function allPagesByOwner(&$wildcard, $include_empty=false, $sortby=false, $limit=false) { + function allPagesByOwner($wildcard, $include_empty=false, $sortby=false, $limit=false) { $dbi = $GLOBALS['request']->getDbh(); $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit); $allPages = array(); @@ -869,7 +870,7 @@ class PageList { return $allPages; } - function allPagesByCreator(&$wildcard, $include_empty=false, $sortby=false, $limit=false) { + function allPagesByCreator($wildcard, $include_empty=false, $sortby=false, $limit=false) { $dbi = $GLOBALS['request']->getDbh(); $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit); $allPages = array(); @@ -1433,6 +1434,9 @@ extends PageList { } // $Log: not supported by cvs2svn $ +// Revision 1.122 2004/11/21 11:59:15 rurban +// remove final \n to be ob_cache independent +// // Revision 1.121 2004/11/20 17:35:47 rurban // improved WantedPages SQL backends // PageList::sortby new 3rd arg valid_fields (override db fields) diff --git a/lib/TextSearchQuery.php b/lib/TextSearchQuery.php index df23d3386..79ecd5a52 100644 --- a/lib/TextSearchQuery.php +++ b/lib/TextSearchQuery.php @@ -1,4 +1,4 @@ -_tree = $parser->parse($search_query); + $this->_tree = $parser->parse($search_query, $case_exact); $this->_optimize(); } @@ -108,6 +113,7 @@ class TextSearchQuery { * TODO: support db-specific extensions, like MATCH AGAINST or REGEX * mysql => 4.0.1 can also do Google: MATCH AGAINST IN BOOLEAN MODE * How? WikiDB backend method? + * Case-sensitivity option. * * Example usage: *
@@ -456,8 +462,8 @@ class TextSearchQuery_Parser
      * '[^']*'		WORD
      */
 
-    function parse ($search_expr) {
-        $this->lexer = new TextSearchQuery_Lexer($search_expr);
+    function parse ($search_expr, $case_exact=false) {
+        $this->lexer = new TextSearchQuery_Lexer($search_expr, $case_exact);
         $tree = $this->get_list('toplevel');
         assert($this->lexer->eof());
         unset($this->lexer);
@@ -538,8 +544,8 @@ class TextSearchQuery_Parser
 }
 
 class TextSearchQuery_Lexer {
-    function TextSearchQuery_Lexer ($query_str) {
-        $this->tokens = $this->tokenize($query_str);
+    function TextSearchQuery_Lexer ($query_str, $case_exact=false) {
+        $this->tokens = $this->tokenize($query_str, $case_exact);
         $this->pos = 0;
     }
 
@@ -555,16 +561,16 @@ class TextSearchQuery_Lexer {
         return $this->pos == count($this->tokens);
     }
 
-    function tokenize($string) {
+    function tokenize($string, $case_exact=false) {
         $tokens = array();
-        $buf = strtolower(ltrim($string));
+        $buf = $case_exact ? ltrim($string) : strtolower(ltrim($string));
         while (!empty($buf)) {
-            if (preg_match('/^(and|or)\b\s*/', $buf, $m)) {
-                $val = $m[1];
+            if (preg_match('/^(and|or)\b\s*/i', $buf, $m)) {
+                $val = strtolower($m[1]);
                 $type = TSQ_TOK_BINOP;
             }
-            elseif (preg_match('/^(-|not\b)\s*/', $buf, $m)) {
-                $val = $m[1];
+            elseif (preg_match('/^(-|not\b)\s*/i', $buf, $m)) {
+                $val = strtolower($m[1]);
                 $type = TSQ_TOK_NOT;
             }
             elseif (preg_match('/^([()])\s*/', $buf, $m)) {
diff --git a/lib/WikiDB.php b/lib/WikiDB.php
index 45658a57c..ed134455e 100644
--- a/lib/WikiDB.php
+++ b/lib/WikiDB.php
@@ -1,5 +1,5 @@
 _backend, 'numPages'))
             // FIXME: currently are all args ignored.
-            $count = $this->_backend->numPages($filter, $exclude);
+            $count = $this->_backend->numPages($include_empty, $exclude);
         else {
             // FIXME: exclude ignored.
-            $iter = $this->getAllPages($filter, false, false, $exclude);
+            $iter = $this->getAllPages($include_empty, false, false, $exclude);
             $count = $iter->count();
             $iter->free();
         }
@@ -323,15 +323,15 @@ class WikiDB {
      * Pages are returned in alphabetical order whenever it is
      * practical to do so.
      *
-     * FIXME: should titleSearch and fullSearch be combined?  I think so.
+     * FIXME: clarify $search syntax. provide glob=>TextSearchQuery converters
      *
      * @access public
      * @param TextSearchQuery $search A TextSearchQuery object
      * @return WikiDB_PageIterator A WikiDB_PageIterator containing the matching pages.
      * @see TextSearchQuery
      */
-    function titleSearch($search) {
-        $result = $this->_backend->text_search($search);
+    function titleSearch($search, $case_exact=false) {
+        $result = $this->_backend->text_search($search, false, $case_exact);
         return new WikiDB_PageIterator($this, $result);
     }
 
@@ -351,8 +351,8 @@ class WikiDB {
      * @return WikiDB_PageIterator A WikiDB_PageIterator containing the matching pages.
      * @see TextSearchQuery
      */
-    function fullSearch($search) {
-        $result = $this->_backend->text_search($search, 'full_text');
+    function fullSearch($search, $case_exact=false) {
+        $result = $this->_backend->text_search($search, 'full_text', $case_exact);
         return new WikiDB_PageIterator($this, $result);
     }
 
@@ -2092,6 +2092,9 @@ function _sql_debuglog_shutdown_function() {
 }
 
 // $Log: not supported by cvs2svn $
+// Revision 1.107  2004/11/21 11:59:16  rurban
+// remove final \n to be ob_cache independent
+//
 // Revision 1.106  2004/11/20 17:35:56  rurban
 // improved WantedPages SQL backends
 // PageList::sortby new 3rd arg valid_fields (override db fields)
diff --git a/lib/WikiDB/backend.php b/lib/WikiDB/backend.php
index 6418255da..466356200 100644
--- a/lib/WikiDB/backend.php
+++ b/lib/WikiDB/backend.php
@@ -1,5 +1,5 @@
 get_all_pages(false, $orderby, $limit);
+        $pages = $this->get_all_pages(false, $sortby, $limit);
         
         return new WikiDB_backend_dumb_MostPopularIter($this, $pages, $limit);
     }
@@ -352,7 +352,7 @@ class WikiDB_backend
         // It is expected that most backends will overload
         // method with something more efficient.
         include_once('lib/WikiDB/backend/dumb/MostRecentIter.php');
-        $pages = $this->get_all_pages(true,'mtime DESC');
+        $pages = $this->get_all_pages(true, '-mtime');
         return new WikiDB_backend_dumb_MostRecentIter($this, $pages, $params);
     }
 
diff --git a/lib/WikiDB/backend/ADODB.php b/lib/WikiDB/backend/ADODB.php
index b4627cfd5..059c1d38e 100644
--- a/lib/WikiDB/backend/ADODB.php
+++ b/lib/WikiDB/backend/ADODB.php
@@ -1,5 +1,5 @@
 
+?>
\ No newline at end of file
diff --git a/lib/WikiDB/backend/PearDB.php b/lib/WikiDB/backend/PearDB.php
index e8ee9711d..c557138cf 100644
--- a/lib/WikiDB/backend/PearDB.php
+++ b/lib/WikiDB/backend/PearDB.php
@@ -1,5 +1,5 @@
 _dbh;
         extract($this->_table_names);
         
         $table = "$nonempty_tbl, $page_tbl";
         $join_clause = "$nonempty_tbl.id=$page_tbl.id";
         $fields = $this->page_tbl_fields;
-        $callback = new WikiMethodCb($this, '_sql_match_clause');
-        
-        if ($fullsearch) {
+
+        if ($fulltext) {
             $table .= ", $recent_tbl";
             $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
 
@@ -564,9 +563,14 @@ extends WikiDB_backend
             $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
 
             $fields .= ", $page_tbl.pagedata as pagedata, " . $this->version_tbl_fields;
-            $callback = new WikiMethodCb($this, '_fullsearch_sql_match_clause');
+            $callback = new WikiMethodCb($this, $case_exact 
+                                         ? '_fullsearch_sql_casematch_clause'
+                                         : '_fullsearch_sql_match_clause');
+        } else {
+            $callback = new WikiMethodCb($this, $case_exact
+                                         ? '_sql_casematch_clause'
+                                         : '_sql_match_clause');
         }
-        
         $search_clause = $search->makeSqlClause($callback);
         
         $result = $dbh->query("SELECT $fields FROM $table"
@@ -588,7 +592,11 @@ extends WikiDB_backend
         // http://bugs.mysql.com/bug.php?id=1491
         return "LOWER(pagename) LIKE '%$word%'";
     }
-
+    function _sql_casematch_clause($word) {
+        $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
+        $word = $this->_dbh->escapeSimple($word);
+        return "pagename LIKE '%$word%'";
+    }
     function _fullsearch_sql_match_clause($word) {
         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
         $word = $this->_dbh->escapeSimple($word);
@@ -596,6 +604,11 @@ extends WikiDB_backend
         //Mysql 4.1.1 has a bug which fails here if word is lowercased.
         return "LOWER(pagename) LIKE '%$word%' OR content LIKE '%$word%'";
     }
+    function _fullsearch_sql_casematch_clause($word) {
+        $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
+        $word = $this->_dbh->escapeSimple($word);
+        return "pagename LIKE '%$word%' OR content LIKE '%$word%'";
+    }
 
     /**
      * Find highest or lowest hit counts.
@@ -1081,6 +1094,9 @@ extends WikiDB_backend_PearDB_generic_iter
 }
 
 // $Log: not supported by cvs2svn $
+// Revision 1.70  2004/11/21 11:59:26  rurban
+// remove final \n to be ob_cache independent
+//
 // Revision 1.69  2004/11/20 17:49:39  rurban
 // add fast exclude support to SQL get_all_pages
 //
@@ -1219,4 +1235,4 @@ extends WikiDB_backend_PearDB_generic_iter
 // c-hanging-comment-ender-p: nil
 // indent-tabs-mode: nil
 // End:   
-?>
+?>
\ No newline at end of file
diff --git a/lib/WikiDB/backend/cvs.php b/lib/WikiDB/backend/cvs.php
index f29c42f5d..40098c1ce 100644
--- a/lib/WikiDB/backend/cvs.php
+++ b/lib/WikiDB/backend/cvs.php
@@ -1,5 +1,5 @@
 set($pagename, (int)$latest . ':' . (int)$flags . ":$pagedata");
     }
 
-    //FIXME: support limit, exclude
     function get_all_pages($include_empty = false, $sortby=false, $limit=false, $exclude=false) {
         $pagedb = &$this->_pagedb;
         $pages = array();
@@ -221,7 +220,7 @@ extends WikiDB_backend
                 assert(!empty($page));
                 continue;
             }
-            if ($exclude and in_array($page,$exclude)) continue; 
+            if ($exclude and in_array($page, $exclude)) continue; 
             if ($limit and count($pages) > $limit) break;
             if (!$include_empty) {
             	if (!($data = $pagedb->get($page))) continue;
diff --git a/lib/WikiDB/backend/file.php b/lib/WikiDB/backend/file.php
index b7bcddab8..7274faa63 100644
--- a/lib/WikiDB/backend/file.php
+++ b/lib/WikiDB/backend/file.php
@@ -1,5 +1,5 @@
 _cache->_id_cache[] instead of wikidb->_iwpcache, to effectively
@@ -822,4 +825,4 @@ class WikiDB_backend_file_iter extends WikiDB_backend_iterator
 // c-hanging-comment-ender-p: nil
 // indent-tabs-mode: nil
 // End:
-?>
+?>
\ No newline at end of file
diff --git a/lib/plugin/TitleSearch.php b/lib/plugin/TitleSearch.php
index 72accdaba..3c3490951 100644
--- a/lib/plugin/TitleSearch.php
+++ b/lib/plugin/TitleSearch.php
@@ -1,5 +1,5 @@
  false,
-                     'auto_redirect' => false,
-                     'noheader'      => false,
-                     'exclude'       => '',
-                     'info'          => false
-                     );
+        return array_merge
+            (
+             PageList::supportedArgs(), // paging and more.
+             array('s'             => false,
+                   'auto_redirect' => false,
+                   'noheader'      => false,
+                   'exclude'       => '',
+                   'info'          => false,
+                   'caseexact'     => false
+                   ));
     }
     // info arg allows multiple columns
     // info=mtime,hits,summary,version,author,locked,minor
@@ -59,11 +63,10 @@ extends WikiPlugin
 
         extract($args);
 
-        $query = new TextSearchQuery($s);
-        $pages = $dbi->titleSearch($query);
-
-        $pagelist = new PageList($info, $exclude);
+        $query = new TextSearchQuery($s, $caseexact);
+        $pages = $dbi->titleSearch($query, $caseexact);
 
+        $pagelist = new PageList($info, $exclude, $args);
         while ($page = $pages->next()) {
             $pagelist->addPage($page);
             $last_name = $page->getName();
@@ -85,6 +88,9 @@ extends WikiPlugin
 };
 
 // $Log: not supported by cvs2svn $
+// Revision 1.21  2004/02/17 12:11:36  rurban
+// added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
+//
 // Revision 1.20  2003/11/02 20:42:35  carstenklapp
 // Allow for easy page creation when search returns no matches.
 // Based on cuthbertcat's patch, SF#655090 2002-12-17.
diff --git a/lib/plugin/WantedPages.php b/lib/plugin/WantedPages.php
index 444e81a57..a5750d7ac 100644
--- a/lib/plugin/WantedPages.php
+++ b/lib/plugin/WantedPages.php
@@ -1,5 +1,5 @@
 
+?>
\ No newline at end of file
-- 
2.45.0