LinkBatch: Set visibility and document constructor
[lhc/web/wiklou.git] / includes / cache / LinkBatch.php
index d6c610a..8a4d061 100644 (file)
@@ -20,6 +20,8 @@
  * @file
  * @ingroup Cache
  */
+use MediaWiki\Linker\LinkTarget;
+use MediaWiki\MediaWikiServices;
 
 /**
  * Class representing a list of titles
@@ -31,14 +33,18 @@ class LinkBatch {
        /**
         * 2-d array, first index namespace, second index dbkey, value arbitrary
         */
-       public $data = array();
+       public $data = [];
 
        /**
         * For debugging which method is using this class.
         */
        protected $caller;
 
-       function __construct( $arr = array() ) {
+       /**
+        * LinkBatch constructor.
+        * @param LinkTarget[] $arr Initial items to be added to the batch
+        */
+       public function __construct( $arr = [] ) {
                foreach ( $arr as $item ) {
                        $this->addObj( $item );
                }
@@ -56,13 +62,13 @@ class LinkBatch {
        }
 
        /**
-        * @param Title|TitleValue $title
+        * @param LinkTarget $linkTarget
         */
-       public function addObj( $title ) {
-               if ( is_object( $title ) ) {
-                       $this->add( $title->getNamespace(), $title->getDBkey() );
+       public function addObj( $linkTarget ) {
+               if ( is_object( $linkTarget ) ) {
+                       $this->add( $linkTarget->getNamespace(), $linkTarget->getDBkey() );
                } else {
-                       wfDebug( "Warning: LinkBatch::addObj got invalid Title or TitleValue object\n" );
+                       wfDebug( "Warning: LinkBatch::addObj got invalid LinkTarget object\n" );
                }
        }
 
@@ -71,11 +77,11 @@ class LinkBatch {
         * @param string $dbkey
         */
        public function add( $ns, $dbkey ) {
-               if ( $ns < 0 ) {
-                       return;
+               if ( $ns < 0 || $dbkey === '' ) {
+                       return; // T137083
                }
                if ( !array_key_exists( $ns, $this->data ) ) {
-                       $this->data[$ns] = array();
+                       $this->data[$ns] = [];
                }
 
                $this->data[$ns][strtr( $dbkey, ' ', '_' )] = 1;
@@ -115,7 +121,7 @@ class LinkBatch {
         * @return array Mapping PDBK to ID
         */
        public function execute() {
-               $linkCache = LinkCache::singleton();
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
 
                return $this->executeInto( $linkCache );
        }
@@ -147,26 +153,29 @@ class LinkBatch {
         */
        public function addResultToCache( $cache, $res ) {
                if ( !$res ) {
-                       return array();
+                       return [];
                }
 
+               $titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter();
                // For each returned entry, add it to the list of good links, and remove it from $remaining
 
-               $ids = array();
+               $ids = [];
                $remaining = $this->data;
                foreach ( $res as $row ) {
-                       $title = Title::makeTitle( $row->page_namespace, $row->page_title );
+                       $title = new TitleValue( (int)$row->page_namespace, $row->page_title );
                        $cache->addGoodLinkObjFromRow( $title, $row );
-                       $ids[$title->getPrefixedDBkey()] = $row->page_id;
+                       $pdbk = $titleFormatter->getPrefixedDBkey( $title );
+                       $ids[$pdbk] = $row->page_id;
                        unset( $remaining[$row->page_namespace][$row->page_title] );
                }
 
                // The remaining links in $data are bad links, register them as such
                foreach ( $remaining as $ns => $dbkeys ) {
                        foreach ( $dbkeys as $dbkey => $unused ) {
-                               $title = Title::makeTitle( $ns, $dbkey );
+                               $title = new TitleValue( (int)$ns, (string)$dbkey );
                                $cache->addBadLinkObj( $title );
-                               $ids[$title->getPrefixedDBkey()] = 0;
+                               $pdbk = $titleFormatter->getPrefixedDBkey( $title );
+                               $ids[$pdbk] = 0;
                        }
                }
 
@@ -178,21 +187,17 @@ class LinkBatch {
         * @return bool|ResultWrapper
         */
        public function doQuery() {
-               global $wgContentHandlerUseDB;
-
                if ( $this->isEmpty() ) {
                        return false;
                }
 
                // This is similar to LinkHolderArray::replaceInternal
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_REPLICA );
                $table = 'page';
-               $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
-                       'page_is_redirect', 'page_latest' );
-
-               if ( $wgContentHandlerUseDB ) {
-                       $fields[] = 'page_content_model';
-               }
+               $fields = array_merge(
+                       LinkCache::getSelectFields(),
+                       [ 'page_namespace', 'page_title' ]
+               );
 
                $conds = $this->constructSet( 'page', $dbr );
 
@@ -221,7 +226,7 @@ class LinkBatch {
                        return false;
                }
 
-               $genderCache = GenderCache::singleton();
+               $genderCache = MediaWikiServices::getInstance()->getGenderCache();
                $genderCache->doLinkBatch( $this->data, $this->caller );
 
                return true;