MediaWiki  REL1_24
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 
00073     public function add( $ns, $dbkey ) {
00074         if ( $ns < 0 ) {
00075             return;
00076         }
00077         if ( !array_key_exists( $ns, $this->data ) ) {
00078             $this->data[$ns] = array();
00079         }
00080 
00081         $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
00082     }
00083 
00090     public function setArray( $array ) {
00091         $this->data = $array;
00092     }
00093 
00099     public function isEmpty() {
00100         return $this->getSize() == 0;
00101     }
00102 
00108     public function getSize() {
00109         return count( $this->data );
00110     }
00111 
00117     public function execute() {
00118         $linkCache = LinkCache::singleton();
00119 
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 
00137         return $ids;
00138     }
00139 
00150     public function addResultToCache( $cache, $res ) {
00151         if ( !$res ) {
00152             return array();
00153         }
00154 
00155         // For each returned entry, add it to the list of good links, and remove it from $remaining
00156 
00157         $ids = array();
00158         $remaining = $this->data;
00159         foreach ( $res as $row ) {
00160             $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00161             $cache->addGoodLinkObjFromRow( $title, $row );
00162             $ids[$title->getPrefixedDBkey()] = $row->page_id;
00163             unset( $remaining[$row->page_namespace][$row->page_title] );
00164         }
00165 
00166         // The remaining links in $data are bad links, register them as such
00167         foreach ( $remaining as $ns => $dbkeys ) {
00168             foreach ( $dbkeys as $dbkey => $unused ) {
00169                 $title = Title::makeTitle( $ns, $dbkey );
00170                 $cache->addBadLinkObj( $title );
00171                 $ids[$title->getPrefixedDBkey()] = 0;
00172             }
00173         }
00174 
00175         return $ids;
00176     }
00177 
00182     public function doQuery() {
00183         global $wgContentHandlerUseDB;
00184 
00185         if ( $this->isEmpty() ) {
00186             return false;
00187         }
00188         wfProfileIn( __METHOD__ );
00189 
00190         // This is similar to LinkHolderArray::replaceInternal
00191         $dbr = wfGetDB( DB_SLAVE );
00192         $table = 'page';
00193         $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
00194             'page_is_redirect', 'page_latest' );
00195 
00196         if ( $wgContentHandlerUseDB ) {
00197             $fields[] = 'page_content_model';
00198         }
00199 
00200         $conds = $this->constructSet( 'page', $dbr );
00201 
00202         // Do query
00203         $caller = __METHOD__;
00204         if ( strval( $this->caller ) !== '' ) {
00205             $caller .= " (for {$this->caller})";
00206         }
00207         $res = $dbr->select( $table, $fields, $conds, $caller );
00208         wfProfileOut( __METHOD__ );
00209 
00210         return $res;
00211     }
00212 
00218     public function doGenderQuery() {
00219         if ( $this->isEmpty() ) {
00220             return false;
00221         }
00222 
00223         global $wgContLang;
00224         if ( !$wgContLang->needsGenderDistinction() ) {
00225             return false;
00226         }
00227 
00228         $genderCache = GenderCache::singleton();
00229         $genderCache->doLinkBatch( $this->data, $this->caller );
00230 
00231         return true;
00232     }
00233 
00241     public function constructSet( $prefix, $db ) {
00242         return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
00243     }
00244 }