00001 <?php
00002
00009 class LinkBatch {
00013 var $data = array();
00014
00015 function __construct( $arr = array() ) {
00016 foreach( $arr as $item ) {
00017 $this->addObj( $item );
00018 }
00019 }
00020
00021 public function addObj( $title ) {
00022 if ( is_object( $title ) ) {
00023 $this->add( $title->getNamespace(), $title->getDBkey() );
00024 } else {
00025 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
00026 }
00027 }
00028
00029 public function add( $ns, $dbkey ) {
00030 if ( $ns < 0 ) {
00031 return;
00032 }
00033 if ( !array_key_exists( $ns, $this->data ) ) {
00034 $this->data[$ns] = array();
00035 }
00036
00037 $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
00038 }
00039
00044 public function setArray( $array ) {
00045 $this->data = $array;
00046 }
00047
00051 public function isEmpty() {
00052 return ($this->getSize() == 0);
00053 }
00054
00058 public function getSize() {
00059 return count( $this->data );
00060 }
00061
00066 public function execute() {
00067 $linkCache = LinkCache::singleton();
00068 return $this->executeInto( $linkCache );
00069 }
00070
00075 protected function executeInto( &$cache ) {
00076 wfProfileIn( __METHOD__ );
00077 $res = $this->doQuery();
00078 $ids = $this->addResultToCache( $cache, $res );
00079 wfProfileOut( __METHOD__ );
00080 return $ids;
00081 }
00082
00089 public function addResultToCache( $cache, $res ) {
00090 if ( !$res ) {
00091 return array();
00092 }
00093
00094
00095
00096 $ids = array();
00097 $remaining = $this->data;
00098 while ( $row = $res->fetchObject() ) {
00099 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00100 $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect );
00101 $ids[$title->getPrefixedDBkey()] = $row->page_id;
00102 unset( $remaining[$row->page_namespace][$row->page_title] );
00103 }
00104
00105
00106 foreach ( $remaining as $ns => $dbkeys ) {
00107 foreach ( $dbkeys as $dbkey => $unused ) {
00108 $title = Title::makeTitle( $ns, $dbkey );
00109 $cache->addBadLinkObj( $title );
00110 $ids[$title->getPrefixedDBkey()] = 0;
00111 }
00112 }
00113 return $ids;
00114 }
00115
00119 public function doQuery() {
00120 if ( $this->isEmpty() ) {
00121 return false;
00122 }
00123 wfProfileIn( __METHOD__ );
00124
00125
00126
00127 $dbr = wfGetDB( DB_SLAVE );
00128 $page = $dbr->tableName( 'page' );
00129 $set = $this->constructSet( 'page', $dbr );
00130 if ( $set === false ) {
00131 wfProfileOut( __METHOD__ );
00132 return false;
00133 }
00134 $sql = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect FROM $page WHERE $set";
00135
00136
00137 $res = $dbr->query( $sql, __METHOD__ );
00138 wfProfileOut( __METHOD__ );
00139 return $res;
00140 }
00141
00149 public function constructSet( $prefix, &$db ) {
00150 $first = true;
00151 $firstTitle = true;
00152 $sql = '';
00153 foreach ( $this->data as $ns => $dbkeys ) {
00154 if ( !count( $dbkeys ) ) {
00155 continue;
00156 }
00157
00158 if ( $first ) {
00159 $first = false;
00160 } else {
00161 $sql .= ' OR ';
00162 }
00163
00164 if (count($dbkeys)==1) {
00165 $singleKey = array_keys($dbkeys);
00166 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title=".
00167 $db->addQuotes($singleKey[0]).
00168 ")";
00169 } else {
00170 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
00171
00172 $firstTitle = true;
00173 foreach( $dbkeys as $dbkey => $unused ) {
00174 if ( $firstTitle ) {
00175 $firstTitle = false;
00176 } else {
00177 $sql .= ',';
00178 }
00179 $sql .= $db->addQuotes( $dbkey );
00180 }
00181 $sql .= '))';
00182 }
00183 }
00184 if ( $first && $firstTitle ) {
00185 # No titles added
00186 return false;
00187 } else {
00188 return $sql;
00189 }
00190 }
00191 }