Add the other existing $skin.css/.js to the message files too to be consistent
[lhc/web/wiklou.git] / includes / LinkBatch.php
index e0f0f6f..bdc4b43 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
+ *
+ * @ingroup Cache
  */
 class LinkBatch {
        /**
@@ -13,13 +12,13 @@ class LinkBatch {
         */
        var $data = array();
 
-       function LinkBatch( $arr = array() ) {
+       function __construct( $arr = array() ) {
                foreach( $arr as $item ) {
                        $this->addObj( $item );
                }
        }
 
-       function addObj( $title ) {
+       public function addObj( $title ) {
                if ( is_object( $title ) ) {
                        $this->add( $title->getNamespace(), $title->getDBkey() );
                } else {
@@ -27,7 +26,7 @@ class LinkBatch {
                }
        }
 
-       function add( $ns, $dbkey ) {
+       public function add( $ns, $dbkey ) {
                if ( $ns < 0 ) {
                        return;
                }
@@ -35,28 +34,28 @@ class LinkBatch {
                        $this->data[$ns] = array();
                }
 
-               $this->data[$ns][$dbkey] = 1;
+               $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
        }
 
        /**
         * Set the link list to a given 2-d array
         * First key is the namespace, second is the DB key, value arbitrary
         */
-       function setArray( $array ) {
+       public function setArray( $array ) {
                $this->data = $array;
        }
 
        /**
         * Returns true if no pages have been added, false otherwise.
         */
-       function isEmpty() {
+       public function isEmpty() {
                return ($this->getSize() == 0);
        }
 
        /**
         * Returns the size of the batch.
         */
-       function getSize() {
+       public function getSize() {
                return count( $this->data );
        }
 
@@ -64,22 +63,31 @@ class LinkBatch {
         * 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 );
+        public function execute() {
+               $linkCache = LinkCache::singleton();
+               return $this->executeInto( $linkCache );
         }
 
        /**
         * Do the query and add the results to a given LinkCache object
         * Return an array mapping PDBK to ID
         */
-       function executeInto( &$cache ) {
-               $fname = 'LinkBatch::executeInto';
-               wfProfileIn( $fname );
-               // Do query
+       protected function executeInto( &$cache ) {
+               wfProfileIn( __METHOD__ );
                $res = $this->doQuery();
+               $ids = $this->addResultToCache( $cache, $res );
+               wfProfileOut( __METHOD__ );
+               return $ids;
+       }
+
+       /**
+        * Add a ResultWrapper containing IDs and titles to a LinkCache object.
+        * As normal, titles will go into the static Title cache field.
+        * This function *also* stores extra fields of the title used for link
+        * parsing to avoid extra DB queries.
+        */
+       public function addResultToCache( $cache, $res ) {
                if ( !$res ) {
-                       wfProfileOut( $fname );
                        return array();
                }
 
@@ -89,62 +97,56 @@ class LinkBatch {
                $remaining = $this->data;
                while ( $row = $res->fetchObject() ) {
                        $title = Title::makeTitle( $row->page_namespace, $row->page_title );
-                       $cache->addGoodLinkObj( $row->page_id, $title );
+                       $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect );
                        $ids[$title->getPrefixedDBkey()] = $row->page_id;
                        unset( $remaining[$row->page_namespace][$row->page_title] );
                }
-               $res->free();
 
                // 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;
                        }
                }
-               wfProfileOut( $fname );
                return $ids;
        }
 
        /**
         * Perform the existence test query, return a ResultWrapper with page_id fields
         */
-       function doQuery() {
-               $fname = 'LinkBatch::doQuery';
-               $namespaces = array();
-
+       public function doQuery() {
                if ( $this->isEmpty() ) {
                        return false;
                }
-               wfProfileIn( $fname );
+               wfProfileIn( __METHOD__ );
 
                // 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 );
+                       wfProfileOut( __METHOD__ );
                        return false;
                }
-               $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
+               $sql = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect FROM $page WHERE $set";
 
                // Do query
-               $res = new ResultWrapper( $dbr,  $dbr->query( $sql, $fname ) );
-               wfProfileOut( $fname );
+               $res = new ResultWrapper( $dbr,  $dbr->query( $sql, __METHOD__ ) );
+               wfProfileOut( __METHOD__ );
                return $res;
        }
 
        /**
         * Construct a WHERE clause which will match all the given titles.
-        * Give the appropriate table's field name prefix ('page', 'pl', etc).
         *
-        * @param $prefix String: ??
+        * @param string $prefix the appropriate table's field name prefix ('page', 'pl', etc)
         * @return string
         * @public
         */
-       function constructSet( $prefix, &$db ) {
+       public function constructSet( $prefix, &$db ) {
                $first = true;
                $firstTitle = true;
                $sql = '';
@@ -158,19 +160,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
@@ -180,5 +189,3 @@ class LinkBatch {
                }
        }
 }
-
-?>