MediaWiki  master
LinkCache Class Reference

Cache for article titles (prefixed DB keys) and ids linked from one source. More...

Inheritance diagram for LinkCache:
Collaboration diagram for LinkCache:

Public Member Functions

 __construct (TitleFormatter $titleFormatter, WANObjectCache $cache, NamespaceInfo $nsInfo=null, ILoadBalancer $loadBalancer=null)
 
 addBadLinkObj ( $page)
 
 addGoodLinkObj ( $id, $page, $len=-1, $redir=null, $revision=0, $model=null, $lang=null)
 Add information about an existing page to the cache. More...
 
 addGoodLinkObjFromRow ( $page, stdClass $row, int $queryFlags=IDBAccessObject::READ_NORMAL)
 Same as above with better interface. More...
 
 addLinkObj ( $page, int $queryFlags=IDBAccessObject::READ_NORMAL)
 Add a title to the link cache, return the page_id or zero if non-existent. More...
 
 clear ()
 Clears cache. More...
 
 clearBadLink ( $page)
 
 clearLink ( $page)
 
 forUpdate ( $update=null)
 General accessor to get/set whether the primary DB should be used. More...
 
 getGoodLinkFieldObj ( $page, string $field)
 Get a field of a page from the cache. More...
 
 getGoodLinkID ( $page)
 Returns the ID of the given page, if information about this page has been cached. More...
 
 getGoodLinkRow (int $ns, string $dbkey, callable $fetchCallback=null, int $queryFlags=IDBAccessObject::READ_NORMAL)
 Returns the row for the page if the page exists (subject to race conditions). More...
 
 getMutableCacheKeys (WANObjectCache $cache, $page)
 
 invalidateTitle ( $page)
 Purge the persistent link cache for a title. More...
 
 isBadLink ( $page)
 Returns true if the fact that this page does not exist had been added to the cache. More...
 
 setLogger (LoggerInterface $logger)
 

Static Public Member Functions

static getSelectFields ()
 Fields that LinkCache needs to select. More...
 

Private Member Functions

 fetchPageRow (IDatabase $db, int $ns, string $dbkey, $options=[])
 
 getCacheKey ( $page, $passThrough=false)
 
 usePersistentCache ( $pageOrNamespace)
 

Private Attributes

MapCacheLRU $badLinks
 
MapCacheLRU $goodLinks
 
ILoadBalancer null $loadBalancer
 
LoggerInterface $logger
 
bool $mForUpdate = false
 
NamespaceInfo $nsInfo
 
TitleFormatter $titleFormatter
 
WANObjectCache $wanCache
 
const MAX_SIZE = 10000
 How many Titles to store. More...
 

Detailed Description

Cache for article titles (prefixed DB keys) and ids linked from one source.

Definition at line 40 of file LinkCache.php.

Constructor & Destructor Documentation

◆ __construct()

LinkCache::__construct ( TitleFormatter  $titleFormatter,
WANObjectCache  $cache,
NamespaceInfo  $nsInfo = null,
ILoadBalancer  $loadBalancer = null 
)
Parameters
TitleFormatter$titleFormatter
WANObjectCache$cache
NamespaceInfo | null$nsInfoNull for backward compatibility, but deprecated
ILoadBalancer | null$loadBalancerUse null when no database is set up, for example on installation

Definition at line 75 of file LinkCache.php.

References $cache, $loadBalancer, $nsInfo, $titleFormatter, and wfDeprecated().

Member Function Documentation

◆ addBadLinkObj()

LinkCache::addBadLinkObj (   $page)
Parameters
LinkTarget | PageReference | array$pageThe page to set cached info for. Can be given as an object or an associative array containing the page_namespace and page_title fields. In MediaWiki 1.36 and earlier, only LinkTarget was accepted.

Definition at line 339 of file LinkCache.php.

References getCacheKey(), and isBadLink().

Referenced by getGoodLinkRow().

◆ addGoodLinkObj()

LinkCache::addGoodLinkObj (   $id,
  $page,
  $len = -1,
  $redir = null,
  $revision = 0,
  $model = null,
  $lang = null 
)

Add information about an existing page to the cache.

Deprecated:
since 1.37, use addGoodLinkObjFromRow() instead. PHPUnit tests must use LinkCacheTestTrait::addGoodLinkObject().
Parameters
int$idPage's ID
LinkTarget | PageReference$pageThe page to set cached info for. In MediaWiki 1.36 and earlier, only LinkTarget was accepted.
int$lenText's length
int | null$redirWhether the page is a redirect
int$revisionLatest revision's ID
string | null$modelLatest revision's content model ID
string | null$langLanguage code of the page, if not the content language

Definition at line 282 of file LinkCache.php.

References $lang, and addGoodLinkObjFromRow().

◆ addGoodLinkObjFromRow()

LinkCache::addGoodLinkObjFromRow (   $page,
stdClass  $row,
int  $queryFlags = IDBAccessObject::READ_NORMAL 
)

Same as above with better interface.

Parameters
LinkTarget | PageReference | array$pageThe page to set cached info for. Can be given as an object or an associative array containing the page_namespace and page_title fields. In MediaWiki 1.36 and earlier, only LinkTarget was accepted.
stdClass$rowObject which has all fields returned by getSelectFields().
int$queryFlagsThe query flags used to retrieve the row, IDBAccessObject::READ_XXX
Since
1.19

Definition at line 313 of file LinkCache.php.

References getCacheKey().

Referenced by addGoodLinkObj(), and getGoodLinkRow().

◆ addLinkObj()

LinkCache::addLinkObj (   $page,
int  $queryFlags = IDBAccessObject::READ_NORMAL 
)

Add a title to the link cache, return the page_id or zero if non-existent.

This causes the link to be looked up in the database if it is not yet cached.

Deprecated:
since 1.37, use PageStore::getPageForLink() instead.
Parameters
LinkTarget | PageReference | array$pageThe page to load. Can be given as an object or an associative array containing the page_namespace and page_title fields. In MediaWiki 1.36 and earlier, only LinkTarget was accepted.
int$queryFlagsIDBAccessObject::READ_XXX
Returns
int Page ID or zero

Definition at line 415 of file LinkCache.php.

References getGoodLinkRow().

◆ clear()

LinkCache::clear ( )

Clears cache.

Definition at line 587 of file LinkCache.php.

◆ clearBadLink()

LinkCache::clearBadLink (   $page)
Parameters
LinkTarget | PageReference | array | string$pageThe page to clear cached info for, as an object, an array containing the page_namespace and page_title fields, or a prefixed DB key. In MediaWiki 1.36 and earlier, only a string was accepted. In MediaWiki 1.36 and earlier, only a string was accepted.

Definition at line 353 of file LinkCache.php.

References getCacheKey().

◆ clearLink()

LinkCache::clearLink (   $page)
Parameters
LinkTarget | PageReference | array$pageThe page to clear cached info for. Can be given as an object or an associative array containing the page_namespace and page_title fields. In MediaWiki 1.36 and earlier, only LinkTarget was accepted.

Definition at line 367 of file LinkCache.php.

References getCacheKey().

Referenced by invalidateTitle().

◆ fetchPageRow()

LinkCache::fetchPageRow ( IDatabase  $db,
int  $ns,
string  $dbkey,
  $options = [] 
)
private
Parameters
IDatabase$db
int$ns
string$dbkey
array$optionsQuery options, see IDatabase::select() for details.
Returns
stdClass|false

Definition at line 551 of file LinkCache.php.

References getSelectFields(), Wikimedia\Rdbms\IDatabase\selectRow(), and usePersistentCache().

◆ forUpdate()

LinkCache::forUpdate (   $update = null)

General accessor to get/set whether the primary DB should be used.

This used to also set the FOR UPDATE option (locking the rows read in order to avoid link table inconsistency), which was later removed for performance on wikis with a high edit rate.

Parameters
bool | null$update
Returns
bool
Deprecated:
Since 1.34. Use PageStore::getPageForLink with IDBAccessObject::READ_LATEST.

Definition at line 112 of file LinkCache.php.

References wfDeprecated(), and wfSetVar().

◆ getCacheKey()

LinkCache::getCacheKey (   $page,
  $passThrough = false 
)
private
Parameters
LinkTarget | PageReference | array | string$page
bool$passThroughReturn $page if $page is a string
Returns
?string the cache key

Definition at line 123 of file LinkCache.php.

References Page\PageIdentity\canExist(), and Page\PageReference\getWikiId().

Referenced by addBadLinkObj(), addGoodLinkObjFromRow(), clearBadLink(), clearLink(), getGoodLinkFieldObj(), getGoodLinkID(), getGoodLinkRow(), getMutableCacheKeys(), and isBadLink().

◆ getGoodLinkFieldObj()

LinkCache::getGoodLinkFieldObj (   $page,
string  $field 
)

Get a field of a page from the cache.

If this link is not a cached good title, it will return NULL.

Parameters
LinkTarget | PageReference | array$pageThe page to get cached info for. Can be given as an object or an associative array containing the page_namespace and page_title fields. In MediaWiki 1.36 and earlier, only LinkTarget was accepted.
string$field( 'id', 'length', 'redirect', 'revision', 'model', 'lang', 'restrictions' )
Returns
string|int|null The field value, or null if the page was not cached or does not exist or is not a proper page (e.g. a special page or interwiki link).

Definition at line 207 of file LinkCache.php.

References getCacheKey(), and isBadLink().

◆ getGoodLinkID()

LinkCache::getGoodLinkID (   $page)

Returns the ID of the given page, if information about this page has been cached.

Parameters
LinkTarget | PageReference | array | string$pageThe page to get the ID for, as an object, an array containing the page_namespace and page_title fields, or a prefixed DB key. In MediaWiki 1.36 and earlier, only a string was accepted.
Returns
int Page ID, or zero if the page was not cached or does not exist or is not a proper page (e.g. a special page or an interwiki link).

Definition at line 183 of file LinkCache.php.

References getCacheKey().

◆ getGoodLinkRow()

LinkCache::getGoodLinkRow ( int  $ns,
string  $dbkey,
callable  $fetchCallback = null,
int  $queryFlags = IDBAccessObject::READ_NORMAL 
)

Returns the row for the page if the page exists (subject to race conditions).

The row will be returned from local cache or WAN cache if possible, or it will be looked up using the callback provided.

Parameters
int$ns
string$dbkey
callable | null$fetchCallbackA callback that will retrieve the link row with the signature ( IDatabase $db, int $ns, string $dbkey, array $queryOptions ): ?stdObj.
int$queryFlagsIDBAccessObject::READ_XXX
Returns
stdClass|null
Access: internal
for use by PageStore. Other code should use a PageLookup instead.

Definition at line 440 of file LinkCache.php.

References $dbr, addBadLinkObj(), addGoodLinkObjFromRow(), DB_REPLICA, getCacheKey(), DBAccessObjectUtils\getDBOptions(), isBadLink(), TitleValue\tryNew(), usePersistentCache(), and wfTimestamp().

Referenced by addLinkObj().

◆ getMutableCacheKeys()

LinkCache::getMutableCacheKeys ( WANObjectCache  $cache,
  $page 
)
Parameters
WANObjectCache$cache
LinkTarget | Pagereference$pageIn MediaWiki 1.36 and earlier, only LinkTarget was accepted.
Returns
string[]
Since
1.28

Definition at line 511 of file LinkCache.php.

References $cache, getCacheKey(), and usePersistentCache().

◆ getSelectFields()

◆ invalidateTitle()

LinkCache::invalidateTitle (   $page)

Purge the persistent link cache for a title.

Parameters
LinkTarget | PageReference$pageIn MediaWiki 1.36 and earlier, only LinkTarget was accepted.
Since
1.28

Definition at line 573 of file LinkCache.php.

References $cache, $wanCache, clearLink(), and usePersistentCache().

◆ isBadLink()

LinkCache::isBadLink (   $page)

Returns true if the fact that this page does not exist had been added to the cache.

Parameters
LinkTarget | PageReference | array | string$pageThe page to get cached info for, as an object, an array containing the page_namespace and page_title fields, or a prefixed DB key. In MediaWiki 1.36 and earlier, only a string was accepted. In MediaWiki 1.36 and earlier, only a string was accepted.
Returns
bool True if the page is known to not exist.

Definition at line 258 of file LinkCache.php.

References getCacheKey().

Referenced by addBadLinkObj(), getGoodLinkFieldObj(), and getGoodLinkRow().

◆ setLogger()

LinkCache::setLogger ( LoggerInterface  $logger)
Parameters
LoggerInterface$logger

Definition at line 97 of file LinkCache.php.

References $logger.

◆ usePersistentCache()

LinkCache::usePersistentCache (   $pageOrNamespace)
private
Parameters
LinkTarget | PageReference | int$pageOrNamespace
Returns
bool

Definition at line 530 of file LinkCache.php.

References NS_CATEGORY, NS_FILE, NS_MEDIAWIKI, and NS_TEMPLATE.

Referenced by fetchPageRow(), getGoodLinkRow(), getMutableCacheKeys(), and invalidateTitle().

Member Data Documentation

◆ $badLinks

MapCacheLRU LinkCache::$badLinks
private

Definition at line 44 of file LinkCache.php.

◆ $goodLinks

MapCacheLRU LinkCache::$goodLinks
private

Definition at line 42 of file LinkCache.php.

◆ $loadBalancer

ILoadBalancer null LinkCache::$loadBalancer
private

Definition at line 58 of file LinkCache.php.

Referenced by __construct().

◆ $logger

LoggerInterface LinkCache::$logger
private

Definition at line 61 of file LinkCache.php.

Referenced by setLogger().

◆ $mForUpdate

bool LinkCache::$mForUpdate = false
private

Definition at line 49 of file LinkCache.php.

◆ $nsInfo

NamespaceInfo LinkCache::$nsInfo
private

Definition at line 55 of file LinkCache.php.

Referenced by __construct().

◆ $titleFormatter

TitleFormatter LinkCache::$titleFormatter
private

Definition at line 52 of file LinkCache.php.

Referenced by __construct().

◆ $wanCache

WANObjectCache LinkCache::$wanCache
private

Definition at line 46 of file LinkCache.php.

Referenced by invalidateTitle().

◆ MAX_SIZE

const LinkCache::MAX_SIZE = 10000
private

How many Titles to store.

There are two caches, so the amount actually stored in memory can be up to twice this.

Definition at line 67 of file LinkCache.php.


The documentation for this class was generated from the following file: