00001 <?php
00002
00010 class BacklinkCache {
00011 var $partitionCache = array();
00012 var $fullResultCache = array();
00013 var $title;
00014 var $db;
00015
00016 const CACHE_EXPIRY = 3600;
00017
00021 function __construct( $title ) {
00022 $this->title = $title;
00023 }
00024
00028 function clear() {
00029 $this->partitionCache = array();
00030 $this->fullResultCache = array();
00031 unset( $this->db );
00032 }
00033
00037 public function setDB( $db ) {
00038 $this->db = $db;
00039 }
00040
00041 protected function getDB() {
00042 if ( !isset( $this->db ) ) {
00043 $this->db = wfGetDB( DB_SLAVE );
00044 }
00045 return $this->db;
00046 }
00047
00053 public function getLinks( $table, $startId = false, $endId = false ) {
00054 wfProfileIn( __METHOD__ );
00055
00056 if ( $startId || $endId ) {
00057
00058 wfDebug( __METHOD__.": from DB (uncacheable range)\n" );
00059 $conds = $this->getConditions( $table );
00060
00061
00062 $fromField = $this->getPrefix( $table ) . '_from';
00063 if ( $startId ) {
00064 $conds[] = "$fromField >= " . intval( $startId );
00065 }
00066 if ( $endId ) {
00067 $conds[] = "$fromField <= " . intval( $endId );
00068 }
00069 $res = $this->getDB()->select(
00070 array( $table, 'page' ),
00071 array( 'page_namespace', 'page_title', 'page_id'),
00072 $conds,
00073 __METHOD__,
00074 array('STRAIGHT_JOIN') );
00075 $ta = TitleArray::newFromResult( $res );
00076 wfProfileOut( __METHOD__ );
00077 return $ta;
00078 }
00079
00080 if ( !isset( $this->fullResultCache[$table] ) ) {
00081 wfDebug( __METHOD__.": from DB\n" );
00082 $res = $this->getDB()->select(
00083 array( $table, 'page' ),
00084 array( 'page_namespace', 'page_title', 'page_id' ),
00085 $this->getConditions( $table ),
00086 __METHOD__,
00087 array('STRAIGHT_JOIN') );
00088 $this->fullResultCache[$table] = $res;
00089 }
00090 $ta = TitleArray::newFromResult( $this->fullResultCache[$table] );
00091 wfProfileOut( __METHOD__ );
00092 return $ta;
00093 }
00094
00098 protected function getPrefix( $table ) {
00099 static $prefixes = array(
00100 'pagelinks' => 'pl',
00101 'imagelinks' => 'il',
00102 'categorylinks' => 'cl',
00103 'templatelinks' => 'tl',
00104 'redirect' => 'rd',
00105 );
00106 if ( isset( $prefixes[$table] ) ) {
00107 return $prefixes[$table];
00108 } else {
00109 throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
00110 }
00111 }
00112
00116 protected function getConditions( $table ) {
00117 $prefix = $this->getPrefix( $table );
00118 switch ( $table ) {
00119 case 'pagelinks':
00120 case 'templatelinks':
00121 case 'redirect':
00122 $conds = array(
00123 "{$prefix}_namespace" => $this->title->getNamespace(),
00124 "{$prefix}_title" => $this->title->getDBkey(),
00125 "page_id={$prefix}_from"
00126 );
00127 break;
00128 case 'imagelinks':
00129 $conds = array(
00130 'il_to' => $this->title->getDBkey(),
00131 'page_id=il_from'
00132 );
00133 break;
00134 case 'categorylinks':
00135 $conds = array(
00136 'cl_to' => $this->title->getDBkey(),
00137 'page_id=cl_from',
00138 );
00139 break;
00140 default:
00141 throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
00142 }
00143 return $conds;
00144 }
00145
00149 public function getNumLinks( $table ) {
00150 if ( isset( $this->fullResultCache[$table] ) ) {
00151 return $this->fullResultCache[$table]->numRows();
00152 }
00153 if ( isset( $this->partitionCache[$table] ) ) {
00154 $entry = reset( $this->partitionCache[$table] );
00155 return $entry['numRows'];
00156 }
00157 $titleArray = $this->getLinks( $table );
00158 return $titleArray->count();
00159 }
00160
00170 public function partition( $table, $batchSize ) {
00171
00172 if ( isset( $this->partitionCache[$table][$batchSize] ) ) {
00173 wfDebug( __METHOD__.": got from partition cache\n" );
00174 return $this->partitionCache[$table][$batchSize]['batches'];
00175 }
00176 $this->partitionCache[$table][$batchSize] = false;
00177 $cacheEntry =& $this->partitionCache[$table][$batchSize];
00178
00179
00180 if ( isset( $this->fullResultCache[$table] ) ) {
00181 $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize );
00182 wfDebug( __METHOD__.": got from full result cache\n" );
00183 return $cacheEntry['batches'];
00184 }
00185
00186 global $wgMemc;
00187 $memcKey = wfMemcKey( 'backlinks', md5( $this->title->getPrefixedDBkey() ),
00188 $table, $batchSize );
00189 $memcValue = $wgMemc->get( $memcKey );
00190 if ( is_array( $memcValue ) ) {
00191 $cacheEntry = $memcValue;
00192 wfDebug( __METHOD__.": got from memcached $memcKey\n" );
00193 return $cacheEntry['batches'];
00194 }
00195
00196 $this->getLinks( $table );
00197 $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize );
00198
00199 $wgMemc->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
00200 wfDebug( __METHOD__.": got from database\n" );
00201 return $cacheEntry['batches'];
00202 }
00203
00207 protected function partitionResult( $res, $batchSize ) {
00208 $batches = array();
00209 $numRows = $res->numRows();
00210 $numBatches = ceil( $numRows / $batchSize );
00211 for ( $i = 0; $i < $numBatches; $i++ ) {
00212 if ( $i == 0 ) {
00213 $start = false;
00214 } else {
00215 $rowNum = intval( $numRows * $i / $numBatches );
00216 $res->seek( $rowNum );
00217 $row = $res->fetchObject();
00218 $start = $row->page_id;
00219 }
00220 if ( $i == $numBatches - 1 ) {
00221 $end = false;
00222 } else {
00223 $rowNum = intval( $numRows * ( $i + 1 ) / $numBatches );
00224 $res->seek( $rowNum );
00225 $row = $res->fetchObject();
00226 $end = $row->page_id - 1;
00227 }
00228 $batches[] = array( $start, $end );
00229 }
00230 return array( 'numRows' => $numRows, 'batches' => $batches );
00231 }
00232 }