Fix for bug 13004, in which the Postgres full-text search has too many results,
[lhc/web/wiklou.git] / includes / LinkBatch.php
index fed9359..db1114c 100644 (file)
@@ -3,9 +3,8 @@
 /**
  * Class representing a list of titles
  * The execute() method checks them all for existence and adds them to a LinkCache object
- +
- * @package MediaWiki
- * @subpackage Cache
+ *
+ * @addtogroup Cache
  */
 class LinkBatch {
        /**
@@ -13,7 +12,7 @@ class LinkBatch {
         */
        var $data = array();
 
-       function LinkBatch( $arr = array() ) {
+       function __construct( $arr = array() ) {
                foreach( $arr as $item ) {
                        $this->addObj( $item );
                }
@@ -35,7 +34,7 @@ class LinkBatch {
                        $this->data[$ns] = array();
                }
 
-               $this->data[$ns][$dbkey] = 1;
+               $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
        }
 
        /**
@@ -46,13 +45,27 @@ class LinkBatch {
                $this->data = $array;
        }
 
+       /**
+        * Returns true if no pages have been added, false otherwise.
+        */
+       function isEmpty() {
+               return ($this->getSize() == 0);
+       }
+
+       /**
+        * Returns the size of the batch.
+        */
+       function getSize() {
+               return count( $this->data );
+       }
+
        /**
         * Do the query and add the results to the LinkCache object
         * Return an array mapping PDBK to ID
         */
         function execute() {
                $linkCache =& LinkCache::singleton();
-               $this->executeInto( $linkCache );
+               return $this->executeInto( $linkCache );
         }
 
        /**
@@ -60,7 +73,7 @@ class LinkBatch {
         * Return an array mapping PDBK to ID
         */
        function executeInto( &$cache ) {
-               $fname = 'LinkBatch::execute';
+               $fname = 'LinkBatch::executeInto';
                wfProfileIn( $fname );
                // Do query
                $res = $this->doQuery();
@@ -83,7 +96,7 @@ class LinkBatch {
 
                // The remaining links in $data are bad links, register them as such
                foreach ( $remaining as $ns => $dbkeys ) {
-                       foreach ( $dbkeys as $dbkey => $nothing ) {
+                       foreach ( $dbkeys as $dbkey => $unused ) {
                                $title = Title::makeTitle( $ns, $dbkey );
                                $cache->addBadLinkObj( $title );
                                $ids[$title->getPrefixedDBkey()] = 0;
@@ -97,20 +110,20 @@ class LinkBatch {
         * Perform the existence test query, return a ResultWrapper with page_id fields
         */
        function doQuery() {
-               $fname = 'LinkBatch::execute';
-               $namespaces = array();
+               $fname = 'LinkBatch::doQuery';
 
-               if ( !count( $this->data ) ) {
+               if ( $this->isEmpty() ) {
                        return false;
                }
                wfProfileIn( $fname );
 
                // Construct query
                // This is very similar to Parser::replaceLinkHolders
-               $dbr =& wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_SLAVE );
                $page = $dbr->tableName( 'page' );
                $set = $this->constructSet( 'page', $dbr );
                if ( $set === false ) {
+                       wfProfileOut( $fname );
                        return false;
                }
                $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
@@ -125,9 +138,9 @@ class LinkBatch {
         * Construct a WHERE clause which will match all the given titles.
         * Give the appropriate table's field name prefix ('page', 'pl', etc).
         *
-        * @param string $prefix
+        * @param $prefix String: ??
         * @return string
-        * @access public
+        * @public
         */
        function constructSet( $prefix, &$db ) {
                $first = true;
@@ -143,19 +156,26 @@ class LinkBatch {
                        } else {
                                $sql .= ' OR ';
                        }
-                       $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
-
-                       $firstTitle = true;
-                       foreach( $dbkeys as $dbkey => $nothing ) {
-                               if ( $firstTitle ) {
-                                       $firstTitle = false;
-                               } else {
-                                       $sql .= ',';
+                       
+                       if (count($dbkeys)==1) { // avoid multiple-reference syntax if simple equality can be used
+                               $singleKey = array_keys($dbkeys);
+                               $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title=".
+                                       $db->addQuotes($singleKey[0]).
+                                       ")";
+                       } else {
+                               $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
+                               
+                               $firstTitle = true;
+                               foreach( $dbkeys as $dbkey => $unused ) {
+                                       if ( $firstTitle ) {
+                                               $firstTitle = false;
+                                       } else {
+                                               $sql .= ',';
+                                       }
+                                       $sql .= $db->addQuotes( $dbkey );
                                }
-                               $sql .= $db->addQuotes( $dbkey );
+                               $sql .= '))';
                        }
-
-                       $sql .= '))';
                }
                if ( $first && $firstTitle ) {
                        # No titles added
@@ -166,4 +186,4 @@ class LinkBatch {
        }
 }
 
-?>
+