MediaWiki  REL1_19
LinkBatch.php
Go to the documentation of this file.
00001 <?php
00002 
00009 class LinkBatch {
00013         var $data = array();
00014 
00018         protected $caller;
00019 
00020         function __construct( $arr = array() ) {
00021                 foreach( $arr as $item ) {
00022                         $this->addObj( $item );
00023                 }
00024         }
00025 
00033         public function setCaller( $caller ) {
00034                 $this->caller = $caller;
00035         }
00036 
00040         public function addObj( $title ) {
00041                 if ( is_object( $title ) ) {
00042                         $this->add( $title->getNamespace(), $title->getDBkey() );
00043                 } else {
00044                         wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
00045                 }
00046         }
00047 
00048         public function add( $ns, $dbkey ) {
00049                 if ( $ns < 0 ) {
00050                         return;
00051                 }
00052                 if ( !array_key_exists( $ns, $this->data ) ) {
00053                         $this->data[$ns] = array();
00054                 }
00055 
00056                 $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
00057         }
00058 
00065         public function setArray( $array ) {
00066                 $this->data = $array;
00067         }
00068 
00074         public function isEmpty() {
00075                 return ($this->getSize() == 0);
00076         }
00077 
00083         public function getSize() {
00084                 return count( $this->data );
00085         }
00086 
00092         public function execute() {
00093                 $linkCache = LinkCache::singleton();
00094                 return $this->executeInto( $linkCache );
00095         }
00096 
00104         protected function executeInto( &$cache ) {
00105                 wfProfileIn( __METHOD__ );
00106                 $res = $this->doQuery();
00107                 $this->doGenderQuery();
00108                 $ids = $this->addResultToCache( $cache, $res );
00109                 wfProfileOut( __METHOD__ );
00110                 return $ids;
00111         }
00112 
00123         public function addResultToCache( $cache, $res ) {
00124                 if ( !$res ) {
00125                         return array();
00126                 }
00127 
00128                 // For each returned entry, add it to the list of good links, and remove it from $remaining
00129 
00130                 $ids = array();
00131                 $remaining = $this->data;
00132                 foreach ( $res as $row ) {
00133                         $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00134                         $cache->addGoodLinkObjFromRow( $title, $row );
00135                         $ids[$title->getPrefixedDBkey()] = $row->page_id;
00136                         unset( $remaining[$row->page_namespace][$row->page_title] );
00137                 }
00138 
00139                 // The remaining links in $data are bad links, register them as such
00140                 foreach ( $remaining as $ns => $dbkeys ) {
00141                         foreach ( $dbkeys as $dbkey => $unused ) {
00142                                 $title = Title::makeTitle( $ns, $dbkey );
00143                                 $cache->addBadLinkObj( $title );
00144                                 $ids[$title->getPrefixedDBkey()] = 0;
00145                         }
00146                 }
00147                 return $ids;
00148         }
00149 
00154         public function doQuery() {
00155                 if ( $this->isEmpty() ) {
00156                         return false;
00157                 }
00158                 wfProfileIn( __METHOD__ );
00159 
00160                 // This is similar to LinkHolderArray::replaceInternal
00161                 $dbr = wfGetDB( DB_SLAVE );
00162                 $table = 'page';
00163                 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
00164                         'page_is_redirect', 'page_latest' );
00165                 $conds = $this->constructSet( 'page', $dbr );
00166 
00167                 // Do query
00168                 $caller = __METHOD__;
00169                 if ( strval( $this->caller ) !== '' ) {
00170                         $caller .= " (for {$this->caller})";
00171                 }
00172                 $res = $dbr->select( $table, $fields, $conds, $caller );
00173                 wfProfileOut( __METHOD__ );
00174                 return $res;
00175         }
00176 
00182         public function doGenderQuery() {
00183                 if ( $this->isEmpty() ) {
00184                         return false;
00185                 }
00186 
00187                 global $wgContLang;
00188                 if ( !$wgContLang->needsGenderDistinction() ) {
00189                         return false;
00190                 }
00191 
00192                 $genderCache = GenderCache::singleton();
00193                 $genderCache->dolinkBatch( $this->data, $this->caller );
00194                 return true;
00195         }
00196 
00204         public function constructSet( $prefix, $db ) {
00205                 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
00206         }
00207 }