* Remove manual query building in search mysql
authorNiklas Laxström <nikerabbit@users.mediawiki.org>
Fri, 22 Apr 2011 13:28:03 +0000 (13:28 +0000)
committerNiklas Laxström <nikerabbit@users.mediawiki.org>
Fri, 22 Apr 2011 13:28:03 +0000 (13:28 +0000)
* Remove reference to mysql3, no longer supported
* Changed method signatures, I found no class extending this class in extensions
* Tested lightly and returns same results

includes/search/SearchMySQL.php

index d68fca0..5dff901 100644 (file)
@@ -171,12 +171,24 @@ class SearchMySQL extends SearchEngine {
        protected function searchInternal( $term, $fulltext ) {
                global $wgCountTotalSearchHits;
 
+               // This seems of place, why is this called with empty term?
+               if ( trim( $term ) === '' ) return null;
+
                $filteredTerm = $this->filter( $term );
-               $resultSet = $this->db->query( $this->getQuery( $filteredTerm, $fulltext ) );
+               $query = $this->getQuery( $filteredTerm, $fulltext );
+               $resultSet = $this->db->select(
+                       $query['tables'], $query['fields'], $query['conds'],
+                       __METHOD__, $query['options'], $query['joins']
+               );
 
                $total = null;
                if( $wgCountTotalSearchHits ) {
-                       $totalResult = $this->db->query( $this->getCountQuery( $filteredTerm, $fulltext ) );
+                       $query = $this->getCountQuery( $filteredTerm, $fulltext );
+                       $totalResult = $this->db->select(
+                               $query['tables'], $query['fields'], $query['conds'],
+                               __METHOD__, $query['options'], $query['joins']
+                       );
+
                        $row = $totalResult->fetchObject();
                        if( $row ) {
                                $total = intval( $row->c );
@@ -189,72 +201,61 @@ class SearchMySQL extends SearchEngine {
 
 
        /**
-        * Return a partial WHERE clause to exclude redirects, if so set
-        * @return String
+        * Add redirect conditions
+        * @param $query Array
+        * @since 1.18 (changed)
         */
-       function queryRedirect() {
-               if( $this->showRedirects ) {
-                       return '';
-               } else {
-                       return 'page_is_redirect=0';
+       function queryRedirect( &$query ) {
+               if( !$this->showRedirects ) {
+                       $query['conds']['page_is_redirect'] = 0;
                }
        }
 
        /**
-        * Return a partial WHERE clause to limit the search to the given namespaces
-        * @return String
+        * Add namespace conditions
+        * @param $query Array
+        * @since 1.18 (changed)
         */
-       function queryNamespaces() {
-               if( is_null($this->namespaces) )
-                       return '';  # search all
-               if ( !count( $this->namespaces ) ) {
-                       $namespaces = '0';
-               } else {
-                       $namespaces = $this->db->makeList( $this->namespaces );
+       function queryNamespaces( &$query ) {
+               if ( is_array( $this->namespaces ) ) {
+                       if ( count( $this->namespaces ) === 0 ) {
+                               $this->namespaces[] = '0';
+                       }
+                       $query['conds']['page_namespace'] = $this->namespaces;
                }
-               return 'page_namespace IN (' . $namespaces . ')';
        }
 
        /**
-        * Return a LIMIT clause to limit results on the query.
-        * @return String
+        * Add limit options
+        * @param $query Array
+        * @since 1.18
         */
-       function queryLimit() {
-               return $this->db->limitResult( '', $this->limit, $this->offset );
+       protected function limitResult( &$query ) {
+               $query['options']['LIMIT'] = $this->limit;
+               $query['options']['OFFSET'] = $this->offset;
        }
 
        /**
-        * Does not do anything for generic search engine
-        * subclasses may define this though
-        * @return String
-        */
-       function queryRanking( $filteredTerm, $fulltext ) {
-               return '';
-       }
-
-       /**
-        * Construct the full SQL query to do the search.
+        * Construct the SQL query to do the search.
         * The guts shoulds be constructed in queryMain()
         * @param $filteredTerm String
         * @param $fulltext Boolean
+        * @return Array
+        * @since 1.18 (changed)
         */
        function getQuery( $filteredTerm, $fulltext ) {
-               $query = $this->queryMain( $filteredTerm, $fulltext ) . ' ';
-
-               $redir = $this->queryRedirect();
-
-               if ( $redir ) {
-                       $query .= 'AND ' . $redir . ' ';
-               }
-
-               $namespace = $this->queryNamespaces();
-
-               if ( $namespace ) {
-                       $query .= 'AND ' . $namespace . ' ';
-               }
+               $query = array(
+                       'tables' => array(),
+                       'fields' => array(),
+                       'conds' => array(),
+                       'options' => array(),
+                       'joins' => array(),
+               );
 
-               $query .= $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
-                       $this->queryLimit();
+               $this->queryMain( $query, $filteredTerm, $fulltext );
+               $this->queryRedirect( $query );
+               $this->queryNamespaces( $query );
+               $this->limitResult( $query );
 
                return $query;
        }
@@ -270,40 +271,40 @@ class SearchMySQL extends SearchEngine {
 
        /**
         * Get the base part of the search query.
-        * The actual match syntax will depend on the server
-        * version; MySQL 3 and MySQL 4 have different capabilities
-        * in their fulltext search indexes.
         *
         * @param $filteredTerm String
         * @param $fulltext Boolean
-        * @return String
+        * @since 1.18 (changed)
         */
-       function queryMain( $filteredTerm, $fulltext ) {
+       function queryMain( &$query, $filteredTerm, $fulltext ) {
                $match = $this->parseQuery( $filteredTerm, $fulltext );
-               $page        = $this->db->tableName( 'page' );
-               $searchindex = $this->db->tableName( 'searchindex' );
-               return 'SELECT page_id, page_namespace, page_title ' .
-                       "FROM $page,$searchindex " .
-                       'WHERE page_id=si_page AND ' . $match;
+               $query['tables'][] = 'page';
+               $query['tables'][] = 'searchindex';
+               $query['fields'][] = 'page_id';
+               $query['fields'][] = 'page_namespace';
+               $query['fields'][] = 'page_title';
+               $query['conds'][] = 'page_id=si_page';
+               $query['conds'][] = $match;
        }
 
+       /**
+        * @since 1.18 (changed)
+        */
        function getCountQuery( $filteredTerm, $fulltext ) {
                $match = $this->parseQuery( $filteredTerm, $fulltext );
 
-               $redir = $this->queryRedirect();
-               $namespaces = $this->queryNamespaces();
-               $conditions = array( 'page_id=si_page', $match );
-               if ( $redir !== '' ) {
-                       $conditions[] = $redir;
-               }
-               if ( $namespaces !== '' ) {
-                       $conditions[] = $namespaces;
-               }
-               return $this->db->selectSQLText( array( 'page', 'searchindex' ),
-                       'COUNT(*) AS c',
-                       $conditions,
-                       __METHOD__
+               $query = array(
+                       'tables' => array( 'page', 'searchindex' ),
+                       'fields' => array( 'COUNT(*) as c' ),
+                       'conds' => array( 'page_id=si_page', $match ),
+                       'options' => array(),
+                       'joins' => array(),
                );
+
+               $this->queryRedirect( $query );
+               $this->queryNamespaces( $query );
+
+               return $query;
        }
 
        /**