MediaWiki  REL1_22
LinkBatch.php
Go to the documentation of this file.
00001 <?php
00030 class LinkBatch {
00034     var $data = array();
00035 
00039     protected $caller;
00040 
00041     function __construct( $arr = array() ) {
00042         foreach ( $arr as $item ) {
00043             $this->addObj( $item );
00044         }
00045     }
00046 
00054     public function setCaller( $caller ) {
00055         $this->caller = $caller;
00056     }
00057 
00061     public function addObj( $title ) {
00062         if ( is_object( $title ) ) {
00063             $this->add( $title->getNamespace(), $title->getDBkey() );
00064         } else {
00065             wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
00066         }
00067     }
00068 
00074     public function add( $ns, $dbkey ) {
00075         if ( $ns < 0 ) {
00076             return;
00077         }
00078         if ( !array_key_exists( $ns, $this->data ) ) {
00079             $this->data[$ns] = array();
00080         }
00081 
00082         $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
00083     }
00084 
00091     public function setArray( $array ) {
00092         $this->data = $array;
00093     }
00094 
00100     public function isEmpty() {
00101         return $this->getSize() == 0;
00102     }
00103 
00109     public function getSize() {
00110         return count( $this->data );
00111     }
00112 
00118     public function execute() {
00119         $linkCache = LinkCache::singleton();
00120         return $this->executeInto( $linkCache );
00121     }
00122 
00130     protected function executeInto( &$cache ) {
00131         wfProfileIn( __METHOD__ );
00132         $res = $this->doQuery();
00133         $this->doGenderQuery();
00134         $ids = $this->addResultToCache( $cache, $res );
00135         wfProfileOut( __METHOD__ );
00136         return $ids;
00137     }
00138 
00149     public function addResultToCache( $cache, $res ) {
00150         if ( !$res ) {
00151             return array();
00152         }
00153 
00154         // For each returned entry, add it to the list of good links, and remove it from $remaining
00155 
00156         $ids = array();
00157         $remaining = $this->data;
00158         foreach ( $res as $row ) {
00159             $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00160             $cache->addGoodLinkObjFromRow( $title, $row );
00161             $ids[$title->getPrefixedDBkey()] = $row->page_id;
00162             unset( $remaining[$row->page_namespace][$row->page_title] );
00163         }
00164 
00165         // The remaining links in $data are bad links, register them as such
00166         foreach ( $remaining as $ns => $dbkeys ) {
00167             foreach ( $dbkeys as $dbkey => $unused ) {
00168                 $title = Title::makeTitle( $ns, $dbkey );
00169                 $cache->addBadLinkObj( $title );
00170                 $ids[$title->getPrefixedDBkey()] = 0;
00171             }
00172         }
00173         return $ids;
00174     }
00175 
00180     public function doQuery() {
00181         if ( $this->isEmpty() ) {
00182             return false;
00183         }
00184         wfProfileIn( __METHOD__ );
00185 
00186         // This is similar to LinkHolderArray::replaceInternal
00187         $dbr = wfGetDB( DB_SLAVE );
00188         $table = 'page';
00189         $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
00190             'page_is_redirect', 'page_latest' );
00191         $conds = $this->constructSet( 'page', $dbr );
00192 
00193         // Do query
00194         $caller = __METHOD__;
00195         if ( strval( $this->caller ) !== '' ) {
00196             $caller .= " (for {$this->caller})";
00197         }
00198         $res = $dbr->select( $table, $fields, $conds, $caller );
00199         wfProfileOut( __METHOD__ );
00200         return $res;
00201     }
00202 
00208     public function doGenderQuery() {
00209         if ( $this->isEmpty() ) {
00210             return false;
00211         }
00212 
00213         global $wgContLang;
00214         if ( !$wgContLang->needsGenderDistinction() ) {
00215             return false;
00216         }
00217 
00218         $genderCache = GenderCache::singleton();
00219         $genderCache->doLinkBatch( $this->data, $this->caller );
00220         return true;
00221     }
00222 
00230     public function constructSet( $prefix, $db ) {
00231         return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
00232     }
00233 }