Merge "mediawiki.mixins: Move .box-sizing mixin from mediawiki.ui utilities"
[lhc/web/wiklou.git] / includes / search / SearchSqlite.php
index ee30d9b..6b1a6b2 100644 (file)
  * Search engine hook for SQLite
  * @ingroup Search
  */
-class SearchSqlite extends SearchEngine {
-
-       /**
-        * @var DatabaseSqlite
-        */
-       protected $db;
-
-       /**
-        * Creates an instance of this class
-        * @param $db DatabaseSqlite: database object
-        */
-       function __construct( $db ) {
-               parent::__construct( $db );
-       }
-
+class SearchSqlite extends SearchDatabase {
        /**
         * Whether fulltext search is supported by current schema
-        * @return Boolean
+        * @return bool
         */
        function fulltextSearchSupported() {
                return $this->db->checkForEnabledSearch();
@@ -56,17 +42,17 @@ class SearchSqlite extends SearchEngine {
         */
        function parseQuery( $filteredText, $fulltext ) {
                global $wgContLang;
-               $lc = SearchEngine::legalSearchChars(); // Minus format chars
+               $lc = $this->legalSearchChars(); // Minus format chars
                $searchon = '';
                $this->searchTerms = array();
 
                $m = array();
-               if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
+               if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
                                $filteredText, $m, PREG_SET_ORDER ) ) {
-                       foreach( $m as $bits ) {
+                       foreach ( $m as $bits ) {
                                @list( /* all */, $modifier, $term, $nonQuoted, $wildcard ) = $bits;
 
-                               if( $nonQuoted != '' ) {
+                               if ( $nonQuoted != '' ) {
                                        $term = $nonQuoted;
                                        $quote = '';
                                } else {
@@ -74,14 +60,14 @@ class SearchSqlite extends SearchEngine {
                                        $quote = '"';
                                }
 
-                               if( $searchon !== '' ) {
+                               if ( $searchon !== '' ) {
                                        $searchon .= ' ';
                                }
 
                                // Some languages such as Serbian store the input form in the search index,
                                // so we may need to search for matches in multiple writing system variants.
                                $convertedVariants = $wgContLang->autoConvertToAllVariants( $term );
-                               if( is_array( $convertedVariants ) ) {
+                               if ( is_array( $convertedVariants ) ) {
                                        $variants = array_unique( array_values( $convertedVariants ) );
                                } else {
                                        $variants = array( $term );
@@ -101,10 +87,11 @@ class SearchSqlite extends SearchEngine {
                                $strippedVariants = array_unique( $strippedVariants );
 
                                $searchon .= $modifier;
-                               if( count( $strippedVariants ) > 1 )
+                               if ( count( $strippedVariants ) > 1 ) {
                                        $searchon .= '(';
-                               foreach( $strippedVariants as $stripped ) {
-                                       if( $nonQuoted && strpos( $stripped, ' ' ) !== false ) {
+                               }
+                               foreach ( $strippedVariants as $stripped ) {
+                                       if ( $nonQuoted && strpos( $stripped, ' ' ) !== false ) {
                                                // Hack for Chinese: we need to toss in quotes for
                                                // multiple-character phrases since normalizeForSearch()
                                                // added spaces between them to make word breaks.
@@ -112,8 +99,9 @@ class SearchSqlite extends SearchEngine {
                                        }
                                        $searchon .= "$quote$stripped$quote$wildcard ";
                                }
-                               if( count( $strippedVariants ) > 1 )
+                               if ( count( $strippedVariants ) > 1 ) {
                                        $searchon .= ')';
+                               }
 
                                // Match individual terms or quoted phrase in result highlighting...
                                // Note that variants will be introduced in a later stage for highlighting!
@@ -134,8 +122,8 @@ class SearchSqlite extends SearchEngine {
                global $wgContLang;
 
                $regex = preg_quote( $string, '/' );
-               if( $wgContLang->hasWordBreaks() ) {
-                       if( $wildcard ) {
+               if ( $wgContLang->hasWordBreaks() ) {
+                       if ( $wildcard ) {
                                // Don't cut off the final bit!
                                $regex = "\b$regex";
                        } else {
@@ -156,8 +144,8 @@ class SearchSqlite extends SearchEngine {
        /**
         * Perform a full text search query and return a result set.
         *
-        * @param string $term raw search term
-        * @return SqliteSearchResultSet
+        * @param string $term Raw search term
+        * @return SqlSearchResultSet
         */
        function searchText( $term ) {
                return $this->searchInternal( $term, true );
@@ -166,8 +154,8 @@ class SearchSqlite extends SearchEngine {
        /**
         * Perform a title-only search query and return a result set.
         *
-        * @param string $term raw search term
-        * @return SqliteSearchResultSet
+        * @param string $term Raw search term
+        * @return SqlSearchResultSet
         */
        function searchTitle( $term ) {
                return $this->searchInternal( $term, false );
@@ -184,37 +172,26 @@ class SearchSqlite extends SearchEngine {
                $resultSet = $this->db->query( $this->getQuery( $filteredTerm, $fulltext ) );
 
                $total = null;
-               if( $wgCountTotalSearchHits ) {
+               if ( $wgCountTotalSearchHits ) {
                        $totalResult = $this->db->query( $this->getCountQuery( $filteredTerm, $fulltext ) );
                        $row = $totalResult->fetchObject();
-                       if( $row ) {
+                       if ( $row ) {
                                $total = intval( $row->c );
                        }
                        $totalResult->free();
                }
 
-               return new SqliteSearchResultSet( $resultSet, $this->searchTerms, $total );
-       }
-
-       /**
-        * Return a partial WHERE clause to exclude redirects, if so set
-        * @return String
-        */
-       function queryRedirect() {
-               if( $this->showRedirects ) {
-                       return '';
-               } else {
-                       return 'AND page_is_redirect=0';
-               }
+               return new SqlSearchResultSet( $resultSet, $this->searchTerms, $total );
        }
 
        /**
         * Return a partial WHERE clause to limit the search to the given namespaces
-        * @return String
+        * @return string
         */
        function queryNamespaces() {
-               if( is_null( $this->namespaces ) )
+               if ( is_null( $this->namespaces ) ) {
                        return '';  # search all
+               }
                if ( !count( $this->namespaces ) ) {
                        $namespaces = '0';
                } else {
@@ -225,8 +202,8 @@ class SearchSqlite extends SearchEngine {
 
        /**
         * Returns a query with limit for number of results set.
-        * @param $sql String:
-        * @return String
+        * @param string $sql
+        * @return string
         */
        function limitResult( $sql ) {
                return $this->db->limitResult( $sql, $this->limit, $this->offset );
@@ -235,22 +212,21 @@ class SearchSqlite extends SearchEngine {
        /**
         * Construct the full SQL query to do the search.
         * The guts shoulds be constructed in queryMain()
-        * @param $filteredTerm String
-        * @param $fulltext Boolean
-        * @return String
+        * @param string $filteredTerm
+        * @param bool $fulltext
+        * @return string
         */
        function getQuery( $filteredTerm, $fulltext ) {
                return $this->limitResult(
                        $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
-                       $this->queryRedirect() . ' ' .
                        $this->queryNamespaces()
                );
        }
 
        /**
         * Picks which field to index on, depending on what type of query.
-        * @param $fulltext Boolean
-        * @return String
+        * @param bool $fulltext
+        * @return string
         */
        function getIndexField( $fulltext ) {
                return $fulltext ? 'si_text' : 'si_title';
@@ -259,9 +235,9 @@ class SearchSqlite extends SearchEngine {
        /**
         * Get the base part of the search query.
         *
-        * @param $filteredTerm String
-        * @param $fulltext Boolean
-        * @return String
+        * @param string $filteredTerm
+        * @param bool $fulltext
+        * @return string
         */
        function queryMain( $filteredTerm, $fulltext ) {
                $match = $this->parseQuery( $filteredTerm, $fulltext );
@@ -278,8 +254,7 @@ class SearchSqlite extends SearchEngine {
                $searchindex = $this->db->tableName( 'searchindex' );
                return "SELECT COUNT(*) AS c " .
                        "FROM $page,$searchindex " .
-                       "WHERE page_id=$searchindex.rowid AND $match" .
-                       $this->queryRedirect() . ' ' .
+                       "WHERE page_id=$searchindex.rowid AND $match " .
                        $this->queryNamespaces();
        }
 
@@ -287,15 +262,15 @@ class SearchSqlite extends SearchEngine {
         * Create or update the search index record for the given page.
         * Title and text should be pre-processed.
         *
-        * @param $id Integer
-        * @param $title String
-        * @param $text String
+        * @param int $id
+        * @param string $title
+        * @param string $text
         */
        function update( $id, $title, $text ) {
                if ( !$this->fulltextSearchSupported() ) {
                        return;
                }
-               // @todo: find a method to do it in a single request,
+               // @todo find a method to do it in a single request,
                // couldn't do it so far due to typelessness of FTS3 tables.
                $dbw = wfGetDB( DB_MASTER );
 
@@ -313,8 +288,8 @@ class SearchSqlite extends SearchEngine {
         * Update a search index record's title only.
         * Title should be pre-processed.
         *
-        * @param $id Integer
-        * @param $title String
+        * @param int $id
+        * @param string $title
         */
        function updateTitle( $id, $title ) {
                if ( !$this->fulltextSearchSupported() ) {
@@ -324,21 +299,7 @@ class SearchSqlite extends SearchEngine {
 
                $dbw->update( 'searchindex',
                        array( 'si_title' => $title ),
-                       array( 'rowid'  => $id ),
+                       array( 'rowid' => $id ),
                        __METHOD__ );
        }
 }
-
-/**
- * @ingroup Search
- */
-class SqliteSearchResultSet extends SqlSearchResultSet {
-       function __construct( $resultSet, $terms, $totalHits=null ) {
-               parent::__construct( $resultSet, $terms );
-               $this->mTotalHits = $totalHits;
-       }
-
-       function getTotalHits() {
-               return $this->mTotalHits;
-       }
-}