104 if ( !self::$instance || !self::$instance->
title->equals(
$title ) ) {
105 self::$instance =
new self(
$title );
107 return self::$instance;
118 return [
'partitionCache',
'fullResultCache',
'title' ];
125 $this->partitionCache = [];
126 $this->fullResultCache = [];
145 if ( !isset( $this->db ) ) {
160 public function getLinks( $table, $startId =
false, $endId =
false, $max = INF ) {
173 protected function queryLinks( $table, $startId, $endId, $max, $select =
'all' ) {
175 $fromField = $this->
getPrefix( $table ) .
'_from';
177 if ( !$startId && !$endId && is_infinite( $max )
178 && isset( $this->fullResultCache[$table] )
180 wfDebug( __METHOD__ .
": got results from cache\n" );
181 $res = $this->fullResultCache[$table];
183 wfDebug( __METHOD__ .
": got results from DB\n" );
188 $conds[] =
"$fromField >= " . intval( $startId );
191 $conds[] =
"$fromField <= " . intval( $endId );
193 $options = [
'ORDER BY' => $fromField ];
194 if ( is_finite( $max ) && $max > 0 ) {
198 if ( $select ===
'ids' ) {
202 [ $this->
getPrefix( $table ) .
'_from AS page_id' ],
203 array_filter( $conds,
function ( $clause ) {
204 return !preg_match(
'/(\b|=)page_id(\b|=)/', $clause );
213 [
'page_namespace',
'page_title',
'page_id' ],
216 array_merge( [
'STRAIGHT_JOIN' ],
$options )
220 if ( $select ===
'all' && !$startId && !$endId &&
$res->numRows() < $max ) {
222 $this->fullResultCache[$table] =
$res;
224 wfDebug( __METHOD__ .
": results from DB were uncacheable\n" );
240 'imagelinks' =>
'il',
241 'categorylinks' =>
'cl',
242 'templatelinks' =>
'tl',
246 if ( isset( $prefixes[$table] ) ) {
247 return $prefixes[$table];
250 Hooks::run(
'BacklinkCacheGetPrefix', [ $table, &$prefix ] );
254 throw new MWException(
"Invalid table \"$table\" in " . __CLASS__ );
271 case 'templatelinks':
273 "{$prefix}_namespace" => $this->
title->getNamespace(),
274 "{$prefix}_title" => $this->
title->getDBkey(),
275 "page_id={$prefix}_from"
280 "{$prefix}_namespace" => $this->
title->getNamespace(),
281 "{$prefix}_title" => $this->
title->getDBkey(),
282 $this->
getDB()->makeList( [
283 "{$prefix}_interwiki" =>
'',
284 "{$prefix}_interwiki IS NULL",
286 "page_id={$prefix}_from"
290 case 'categorylinks':
292 "{$prefix}_to" => $this->
title->getDBkey(),
293 "page_id={$prefix}_from"
298 Hooks::run(
'BacklinkCacheGetConditions', [ $table, $this->
title, &$conds ] );
300 throw new MWException(
"Invalid table \"$table\" in " . __CLASS__ );
323 global $wgUpdateRowsPerJob;
327 if ( isset( $this->partitionCache[$table] ) ) {
328 $entry = reset( $this->partitionCache[$table] );
330 return min( $max, $entry[
'numRows'] );
334 if ( isset( $this->fullResultCache[$table] ) ) {
335 return min( $max, $this->fullResultCache[$table]->numRows() );
338 $memcKey =
wfMemcKey(
'numbacklinks', md5( $this->
title->getPrefixedDBkey() ), $table );
343 return min( $max,
$count );
347 if ( is_infinite( $max ) ) {
350 $this->
partition( $table, $wgUpdateRowsPerJob );
351 return $this->partitionCache[$table][$wgUpdateRowsPerJob][
'numRows'];
360 return min( $max,
$count );
374 if ( isset( $this->partitionCache[$table][$batchSize] ) ) {
375 wfDebug( __METHOD__ .
": got from partition cache\n" );
377 return $this->partitionCache[$table][$batchSize][
'batches'];
381 $this->partitionCache[$table][$batchSize] =
false;
382 $cacheEntry =& $this->partitionCache[$table][$batchSize];
385 if ( isset( $this->fullResultCache[$table] ) ) {
386 $cacheEntry = $this->
partitionResult( $this->fullResultCache[$table], $batchSize );
387 wfDebug( __METHOD__ .
": got from full result cache\n" );
389 return $cacheEntry[
'batches'];
394 md5( $this->
title->getPrefixedDBkey() ),
400 $memcValue =
$cache->get( $memcKey );
401 if ( is_array( $memcValue ) ) {
402 $cacheEntry = $memcValue;
403 wfDebug( __METHOD__ .
": got from memcached $memcKey\n" );
405 return $cacheEntry[
'batches'];
409 $cacheEntry = [
'numRows' => 0,
'batches' => [] ];
412 $selectSize = max( $batchSize, 200000 - ( 200000 % $batchSize ) );
415 $res = $this->
queryLinks( $table, $start,
false, $selectSize,
'ids' );
418 $cacheEntry[
'numRows'] += $partitions[
'numRows'];
419 $cacheEntry[
'batches'] = array_merge( $cacheEntry[
'batches'], $partitions[
'batches'] );
420 if ( count( $partitions[
'batches'] ) ) {
421 list( , $lEnd ) = end( $partitions[
'batches'] );
424 }
while ( $partitions[
'numRows'] >= $selectSize );
426 if ( count( $cacheEntry[
'batches'] ) ) {
427 $cacheEntry[
'batches'][0][0] =
false;
428 $cacheEntry[
'batches'][count( $cacheEntry[
'batches'] ) - 1][1] =
false;
432 $cache->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
435 $memcKey =
wfMemcKey(
'numbacklinks', md5( $this->
title->getPrefixedDBkey() ), $table );
436 $cache->set( $memcKey, $cacheEntry[
'numRows'], self::CACHE_EXPIRY );
438 wfDebug( __METHOD__ .
": got from database\n" );
440 return $cacheEntry[
'batches'];
453 $numRows =
$res->numRows();
454 $numBatches = ceil( $numRows / $batchSize );
456 for ( $i = 0; $i < $numBatches; $i++ ) {
457 if ( $i == 0 && $isComplete ) {
460 $rowNum = $i * $batchSize;
461 $res->seek( $rowNum );
462 $row =
$res->fetchObject();
463 $start = (int)$row->page_id;
466 if ( $i == ( $numBatches - 1 ) && $isComplete ) {
469 $rowNum = min( $numRows - 1, ( $i + 1 ) * $batchSize - 1 );
470 $res->seek( $rowNum );
471 $row =
$res->fetchObject();
472 $end = (int)$row->page_id;
476 if ( $start && $end && $start > $end ) {
477 throw new MWException( __METHOD__ .
': Internal error: query result out of order' );
480 $batches[] = [ $start, $end ];
483 return [
'numRows' => $numRows,
'batches' => $batches ];
497 $resSets[] =
$dbr->select(
498 [
'templatelinks',
'page_restrictions',
'page' ],
499 [
'page_namespace',
'page_title',
'page_id' ],
501 'tl_namespace' => $this->
title->getNamespace(),
502 'tl_title' => $this->
title->getDBkey(),
511 $resSets[] =
$dbr->select(
512 [
'imagelinks',
'page_restrictions',
'page' ],
513 [
'page_namespace',
'page_title',
'page_id' ],
515 'il_to' => $this->
title->getDBkey(),
527 foreach ( $resSets
as $res ) {
528 foreach ( $res
as $row ) {
529 $mergedRes[$row->page_id] = $row;
static getMainWANInstance()
Get the main WAN cache object.
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
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
getCascadeProtectedLinks()
Get a Title iterator for cascade-protected template/file use backlinks.
__sleep()
Serialization handler, diasallows to serialize the database to prevent failures after this class is d...
__construct(Title $title)
Create a new BacklinkCache.
getNumLinks($table, $max=INF)
Get the approximate number of backlinks.
getPrefix($table)
Get the field name prefix for a given table.
when a variable name is used in a it is silently declared as a new local masking the global
getConditions($table)
Get the SQL condition array for selecting backlinks, with a join on the page table.
setDB($db)
Set the Database object to use.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
static BacklinkCache $instance
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
$db
Local copy of a database object.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
static newFromResult($res)
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
partitionResult($res, $batchSize, $isComplete=true)
Partition a DB result with backlinks in it into batches.
ResultWrapper[] $fullResultCache
Contains the whole links from a database result.
array[] $partitionCache
Multi dimensions array representing batches.
clear()
Clear locally stored data and database object.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
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.
Overloads the relevant methods of the real ResultsWrapper so it doesn't go anywhere near an actual da...
Class for fetching backlink lists, approximate backlink counts and partitions.
$title
Local copy of a Title object.
wfMemcKey()
Make a cache key for the local wiki.
getDB()
Get the replica DB connection to the database When non existing, will initialize the connection...
partition($table, $batchSize)
Partition the backlinks into batches.
hasLinks($table)
Check if there are any backlinks.