107 if ( !self::$instance || !self::$instance->title->equals( $title ) ) {
108 self::$instance =
new self(
$title );
121 return [
'partitionCache',
'fullResultCache',
'title' ];
128 $this->partitionCache = [];
129 $this->fullResultCache = [];
148 if ( !isset( $this->db ) ) {
163 public function getLinks( $table, $startId =
false, $endId =
false, $max = INF ) {
176 protected function queryLinks( $table, $startId, $endId, $max, $select =
'all' ) {
177 $fromField = $this->
getPrefix( $table ) .
'_from';
179 if ( !$startId && !$endId && is_infinite( $max )
180 && isset( $this->fullResultCache[$table] )
182 wfDebug( __METHOD__ .
": got results from cache\n" );
183 $res = $this->fullResultCache[$table];
185 wfDebug( __METHOD__ .
": got results from DB\n" );
190 $conds[] =
"$fromField >= " . intval( $startId );
193 $conds[] =
"$fromField <= " . intval( $endId );
195 $options = [
'ORDER BY' => $fromField ];
196 if ( is_finite( $max ) && $max > 0 ) {
200 if ( $select ===
'ids' ) {
204 [ $this->
getPrefix( $table ) .
'_from AS page_id' ],
205 array_filter( $conds,
function ( $clause ) {
206 return !preg_match(
'/(\b|=)page_id(\b|=)/', $clause );
215 [
'page_namespace',
'page_title',
'page_id' ],
218 array_merge( [
'STRAIGHT_JOIN' ],
$options )
222 if ( $select ===
'all' && !$startId && !$endId &&
$res->numRows() < $max ) {
224 $this->fullResultCache[$table] =
$res;
226 wfDebug( __METHOD__ .
": results from DB were uncacheable\n" );
242 'imagelinks' =>
'il',
243 'categorylinks' =>
'cl',
244 'templatelinks' =>
'tl',
248 if ( isset( $prefixes[$table] ) ) {
249 return $prefixes[$table];
252 Hooks::run(
'BacklinkCacheGetPrefix', [ $table, &$prefix ] );
256 throw new MWException(
"Invalid table \"$table\" in " . __CLASS__ );
273 case 'templatelinks':
275 "{$prefix}_namespace" => $this->title->getNamespace(),
276 "{$prefix}_title" => $this->title->getDBkey(),
277 "page_id={$prefix}_from"
282 "{$prefix}_namespace" => $this->title->getNamespace(),
283 "{$prefix}_title" => $this->title->getDBkey(),
284 $this->
getDB()->makeList( [
285 "{$prefix}_interwiki" =>
'',
286 "{$prefix}_interwiki IS NULL",
288 "page_id={$prefix}_from"
292 case 'categorylinks':
294 "{$prefix}_to" => $this->title->getDBkey(),
295 "page_id={$prefix}_from"
300 Hooks::run(
'BacklinkCacheGetConditions', [ $table, $this->title, &$conds ] );
302 throw new MWException(
"Invalid table \"$table\" in " . __CLASS__ );
327 $cache = ObjectCache::getMainWANInstance();
329 if ( isset( $this->partitionCache[$table] ) ) {
330 $entry = reset( $this->partitionCache[$table] );
332 return min( $max, $entry[
'numRows'] );
336 if ( isset( $this->fullResultCache[$table] ) ) {
337 return min( $max, $this->fullResultCache[$table]->numRows() );
340 $memcKey =
$cache->makeKey(
342 md5( $this->title->getPrefixedDBkey() ),
347 $count =
$cache->get( $memcKey );
349 return min( $max, $count );
353 if ( is_infinite( $max ) ) {
360 $count = $this->
getLinks( $table,
false,
false, $max )->count();
361 if ( $count < $max ) {
362 $cache->set( $memcKey, $count, self::CACHE_EXPIRY );
366 return min( $max, $count );
380 if ( isset( $this->partitionCache[$table][$batchSize] ) ) {
381 wfDebug( __METHOD__ .
": got from partition cache\n" );
383 return $this->partitionCache[$table][$batchSize][
'batches'];
386 $cache = ObjectCache::getMainWANInstance();
387 $this->partitionCache[$table][$batchSize] =
false;
388 $cacheEntry =& $this->partitionCache[$table][$batchSize];
391 if ( isset( $this->fullResultCache[$table] ) ) {
392 $cacheEntry = $this->
partitionResult( $this->fullResultCache[$table], $batchSize );
393 wfDebug( __METHOD__ .
": got from full result cache\n" );
395 return $cacheEntry[
'batches'];
398 $memcKey =
$cache->makeKey(
400 md5( $this->title->getPrefixedDBkey() ),
406 $memcValue =
$cache->get( $memcKey );
407 if ( is_array( $memcValue ) ) {
408 $cacheEntry = $memcValue;
409 wfDebug( __METHOD__ .
": got from memcached $memcKey\n" );
411 return $cacheEntry[
'batches'];
415 $cacheEntry = [
'numRows' => 0,
'batches' => [] ];
418 $selectSize = max( $batchSize, 200000 - ( 200000 % $batchSize ) );
421 $res = $this->
queryLinks( $table, $start,
false, $selectSize,
'ids' );
424 $cacheEntry[
'numRows'] += $partitions[
'numRows'];
425 $cacheEntry[
'batches'] = array_merge( $cacheEntry[
'batches'], $partitions[
'batches'] );
426 if ( count( $partitions[
'batches'] ) ) {
427 list( , $lEnd ) = end( $partitions[
'batches'] );
430 }
while ( $partitions[
'numRows'] >= $selectSize );
432 if ( count( $cacheEntry[
'batches'] ) ) {
433 $cacheEntry[
'batches'][0][0] =
false;
434 $cacheEntry[
'batches'][count( $cacheEntry[
'batches'] ) - 1][1] =
false;
438 $cache->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
441 $memcKey =
$cache->makeKey(
443 md5( $this->title->getPrefixedDBkey() ),
446 $cache->set( $memcKey, $cacheEntry[
'numRows'], self::CACHE_EXPIRY );
448 wfDebug( __METHOD__ .
": got from database\n" );
450 return $cacheEntry[
'batches'];
463 $numRows =
$res->numRows();
464 $numBatches = ceil( $numRows / $batchSize );
466 for ( $i = 0; $i < $numBatches; $i++ ) {
467 if ( $i == 0 && $isComplete ) {
470 $rowNum = $i * $batchSize;
471 $res->seek( $rowNum );
472 $row =
$res->fetchObject();
473 $start = (int)$row->page_id;
476 if ( $i == ( $numBatches - 1 ) && $isComplete ) {
479 $rowNum = min( $numRows - 1, ( $i + 1 ) * $batchSize - 1 );
480 $res->seek( $rowNum );
481 $row =
$res->fetchObject();
482 $end = (int)$row->page_id;
486 if ( $start && $end && $start > $end ) {
487 throw new MWException( __METHOD__ .
': Internal error: query result out of order' );
490 $batches[] = [ $start, $end ];
493 return [
'numRows' => $numRows,
'batches' => $batches ];
507 $resSets[] =
$dbr->select(
508 [
'templatelinks',
'page_restrictions',
'page' ],
509 [
'page_namespace',
'page_title',
'page_id' ],
511 'tl_namespace' => $this->title->getNamespace(),
512 'tl_title' => $this->title->getDBkey(),
520 if ( $this->title->getNamespace() ==
NS_FILE ) {
521 $resSets[] =
$dbr->select(
522 [
'imagelinks',
'page_restrictions',
'page' ],
523 [
'page_namespace',
'page_title',
'page_id' ],
525 'il_to' => $this->title->getDBkey(),
537 foreach ( $resSets as
$res ) {
538 foreach (
$res as $row ) {
539 $mergedRes[$row->page_id] = $row;
$wgUpdateRowsPerJob
Number of rows to update per job.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Class for fetching backlink lists, approximate backlink counts and partitions.
getCascadeProtectedLinks()
Get a Title iterator for cascade-protected template/file use backlinks.
array[] $partitionCache
Multi dimensions array representing batches.
queryLinks( $table, $startId, $endId, $max, $select='all')
Get the backlinks for a given table.
getLinks( $table, $startId=false, $endId=false, $max=INF)
Get the backlinks for a given table.
partition( $table, $batchSize)
Partition the backlinks into batches.
getPrefix( $table)
Get the field name prefix for a given table.
partitionResult( $res, $batchSize, $isComplete=true)
Partition a DB result with backlinks in it into batches.
__construct(Title $title)
Create a new BacklinkCache.
clear()
Clear locally stored data and database object.
getDB()
Get the replica DB connection to the database When non existing, will initialize the connection.
__sleep()
Serialization handler, diasallows to serialize the database to prevent failures after this class is d...
getNumLinks( $table, $max=INF)
Get the approximate number of backlinks.
hasLinks( $table)
Check if there are any backlinks.
setDB( $db)
Set the Database object to use.
$db
Local copy of a database object.
ResultWrapper[] $fullResultCache
Contains the whole links from a database result.
getConditions( $table)
Get the SQL condition array for selecting backlinks, with a join on the page table.
$title
Local copy of a Title object.
static BacklinkCache $instance
static newFromResult( $res)
Represents a title within MediaWiki.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options