Results of my work on new-installer and Pg.
[lhc/web/wiklou.git] / includes / search / SearchPostgres.php
index 81e9e65..c06978e 100644 (file)
@@ -1,23 +1,25 @@
 <?php
-# Copyright (C) 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
-# http://www.mediawiki.org/
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-# http://www.gnu.org/copyleft/gpl.html
-
 /**
+ * PostgreSQL search engine
+ *
+ * Copyright © 2006-2007 Greg Sabino Mullane <greg@turnstep.com>
+ * http://www.mediawiki.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Search
  */
  * @ingroup Search
  */
 class SearchPostgres extends SearchEngine {
-
+       /**
+        * Creates an instance of this class
+        * @param $db DatabaseSqlite: database object
+        */
        function __construct( $db ) {
-               $this->db = $db;
+               parent::__construct( $db );
        }
 
        /**
@@ -129,29 +134,25 @@ class SearchPostgres extends SearchEngine {
 
        /**
         * Construct the full SQL query to do the search.
-        * @param $filteredTerm String
+        * @param $term String
         * @param $fulltext String
+        * @param $colname
         */
        function searchQuery( $term, $fulltext, $colname ) {
-               global $wgDBversion;
-
-               if ( !isset( $wgDBversion ) ) {
-                       $this->db->getServerVersion();
-                       $wgDBversion = $this->db->numeric_version;
-               }
-               $prefix = $wgDBversion < 8.3 ? "'default'," : '';
+               $postgresVersion = $this->db->getServerVersion();
 
                # Get the SQL fragment for the given term
                $searchstring = $this->parseQuery( $term );
 
                ## We need a separate query here so gin does not complain about empty searches
-               $SQL = "SELECT to_tsquery($prefix $searchstring)";
+               $SQL = "SELECT to_tsquery($searchstring)";
                $res = $this->db->doQuery($SQL);
                if (!$res) {
                        ## TODO: Better output (example to catch: one 'two)
                        die ("Sorry, that was not a valid search string. Please go back and try again");
                }
-               $top = pg_fetch_result($res,0,0);
+               $top = $res->fetchRow();
+               $top = $top[0];
 
                if ($top === "") { ## e.g. if only stopwords are used XXX return something better
                        $query = "SELECT page_id, page_namespace, page_title, 0 AS score ".
@@ -166,12 +167,10 @@ class SearchPostgres extends SearchEngine {
                                }
                        }
 
-                       $rankscore = $wgDBversion > 8.2 ? 5 : 1;
-                       $rank = $wgDBversion < 8.3 ? 'rank' : 'ts_rank';
                        $query = "SELECT page_id, page_namespace, page_title, ".
-                       "$rank($fulltext, to_tsquery($prefix $searchstring), $rankscore) AS score ".
+                       "ts_rank($fulltext, to_tsquery($searchstring), 5) AS score ".
                        "FROM page p, revision r, pagecontent c WHERE p.page_latest = r.rev_id " .
-                       "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($prefix $searchstring)";
+                       "AND r.rev_text_id = c.old_id AND $fulltext @@ to_tsquery($searchstring)";
                }
 
                ## Redirects
@@ -204,7 +203,7 @@ class SearchPostgres extends SearchEngine {
                $SQL = "UPDATE pagecontent SET textvector = NULL WHERE old_id IN ".
                                "(SELECT rev_text_id FROM revision WHERE rev_page = " . intval( $pageid ) . 
                                " ORDER BY rev_text_id DESC OFFSET 1)";
-               $this->db->doQuery($SQL);
+               $this->db->query($SQL);
                return true;
        }
 
@@ -230,18 +229,9 @@ class PostgresSearchResult extends SearchResult {
 /**
  * @ingroup Search
  */
-class PostgresSearchResultSet extends SearchResultSet {
+class PostgresSearchResultSet extends SqlSearchResultSet {
        function __construct( $resultSet, $terms ) {
-               $this->mResultSet = $resultSet;
-               $this->mTerms = $terms;
-       }
-
-       function termMatches() {
-               return $this->mTerms;
-       }
-
-       function numRows() {
-               return $this->mResultSet->numRows();
+               parent::__construct( $resultSet, $terms );
        }
 
        function next() {