MediaWiki  REL1_23
LinkBatch.php
Go to the documentation of this file.
00001 <?php
00030 class LinkBatch {
00034     public $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 
00121         return $this->executeInto( $linkCache );
00122     }
00123 
00131     protected function executeInto( &$cache ) {
00132         wfProfileIn( __METHOD__ );
00133         $res = $this->doQuery();
00134         $this->doGenderQuery();
00135         $ids = $this->addResultToCache( $cache, $res );
00136         wfProfileOut( __METHOD__ );
00137 
00138         return $ids;
00139     }
00140 
00151     public function addResultToCache( $cache, $res ) {
00152         if ( !$res ) {
00153             return array();
00154         }
00155 
00156         // For each returned entry, add it to the list of good links, and remove it from $remaining
00157 
00158         $ids = array();
00159         $remaining = $this->data;
00160         foreach ( $res as $row ) {
00161             $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00162             $cache->addGoodLinkObjFromRow( $title, $row );
00163             $ids[$title->getPrefixedDBkey()] = $row->page_id;
00164             unset( $remaining[$row->page_namespace][$row->page_title] );
00165         }
00166 
00167         // The remaining links in $data are bad links, register them as such
00168         foreach ( $remaining as $ns => $dbkeys ) {
00169             foreach ( $dbkeys as $dbkey => $unused ) {
00170                 $title = Title::makeTitle( $ns, $dbkey );
00171                 $cache->addBadLinkObj( $title );
00172                 $ids[$title->getPrefixedDBkey()] = 0;
00173             }
00174         }
00175 
00176         return $ids;
00177     }
00178 
00183     public function doQuery() {
00184         if ( $this->isEmpty() ) {
00185             return false;
00186         }
00187         wfProfileIn( __METHOD__ );
00188 
00189         // This is similar to LinkHolderArray::replaceInternal
00190         $dbr = wfGetDB( DB_SLAVE );
00191         $table = 'page';
00192         $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
00193             'page_is_redirect', 'page_latest' );
00194         $conds = $this->constructSet( 'page', $dbr );
00195 
00196         // Do query
00197         $caller = __METHOD__;
00198         if ( strval( $this->caller ) !== '' ) {
00199             $caller .= " (for {$this->caller})";
00200         }
00201         $res = $dbr->select( $table, $fields, $conds, $caller );
00202         wfProfileOut( __METHOD__ );
00203 
00204         return $res;
00205     }
00206 
00212     public function doGenderQuery() {
00213         if ( $this->isEmpty() ) {
00214             return false;
00215         }
00216 
00217         global $wgContLang;
00218         if ( !$wgContLang->needsGenderDistinction() ) {
00219             return false;
00220         }
00221 
00222         $genderCache = GenderCache::singleton();
00223         $genderCache->doLinkBatch( $this->data, $this->caller );
00224 
00225         return true;
00226     }
00227 
00235     public function constructSet( $prefix, $db ) {
00236         return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
00237     }
00238 }