30 use Psr\Log\LoggerAwareInterface;
31 use Psr\Log\LoggerInterface;
32 use Psr\Log\NullLogger;
48 private $titleFormatter;
52 private $loadBalancer;
57 private const MAX_SIZE = 10000;
60 private const ROW = 0;
62 private const FLAGS = 1;
77 $this->wanCache = $cache;
78 $this->titleFormatter = $titleFormatter;
79 $this->nsInfo = $nsInfo;
80 $this->loadBalancer = $loadBalancer;
81 $this->logger =
new NullLogger();
87 public function setLogger( LoggerInterface $logger ) {
88 $this->logger = $logger;
96 private function getCacheKey( $page, $passThrough =
false ) {
97 if ( is_string( $page ) ) {
101 throw new InvalidArgumentException(
'They key may not be given as a string here' );
105 if ( is_array( $page ) ) {
106 $namespace = $page[
'page_namespace'];
107 $dbkey = $page[
'page_title'];
108 return strtr( $this->titleFormatter->formatTitle( $namespace, $dbkey ),
' ',
'_' );
114 'cross-wiki page reference',
116 'page-wiki' => $page->getWikiId(),
117 'page-reference' => $this->titleFormatter->getFullText( $page )
125 $this->logger->warning(
126 'non-proper page reference: {page-reference}',
127 [
'page-reference' => $this->titleFormatter->getFullText( $page ) ]
133 && ( $page->isExternal() || $page->getText() ===
'' || $page->getNamespace() < 0 )
137 $this->logger->warning(
138 'link to non-proper page: {page-link}',
139 [
'page-link' => $this->titleFormatter->getFullText( $page ) ]
144 return $this->titleFormatter->getPrefixedDBkey( $page );
157 $key = $this->getCacheKey( $page,
true );
158 if ( $key ===
null ) {
162 $entry = $this->entries->get( $key );
167 $row = $entry[self::ROW];
169 return $row ? (int)$row->page_id : 0;
185 $key = $this->getCacheKey( $page );
186 if ( $key ===
null ) {
190 $entry = $this->entries->get( $key );
195 $row = $entry[self::ROW];
202 return (
int)$row->page_id;
204 return (
int)$row->page_len;
206 return (
int)$row->page_is_redirect;
208 return (
int)$row->page_latest;
210 return !empty( $row->page_content_model )
211 ? (string)$row->page_content_model
214 return !empty( $row->page_lang )
215 ? (string)$row->page_lang
218 throw new InvalidArgumentException(
"Unknown field: $field" );
232 $key = $this->getCacheKey( $page,
true );
233 if ( $key ===
null ) {
237 $entry = $this->entries->get( $key );
239 return ( $entry && !$entry[self::ROW] );
258 $revision = 0, $model =
null,
$lang =
null
262 'page_id' => (
int)$id,
263 'page_namespace' => $page->getNamespace(),
264 'page_title' => $page->getDBkey(),
265 'page_len' => (
int)$len,
266 'page_is_redirect' => (
int)$redir,
267 'page_latest' => (
int)$revision,
268 'page_content_model' => $model ? (
string)$model :
null,
271 'page_touched' =>
'',
294 int $queryFlags = IDBAccessObject::READ_NORMAL
296 $key = $this->getCacheKey( $page );
297 if ( $key ===
null ) {
301 foreach ( self::getSelectFields() as $field ) {
302 if ( !property_exists( $row, $field ) ) {
303 throw new InvalidArgumentException(
"Missing field: $field" );
307 $this->entries->set( $key, [ self::ROW => $row, self::FLAGS => $queryFlags ] );
324 public function addBadLinkObj( $page,
int $queryFlags = IDBAccessObject::READ_NORMAL ) {
325 $key = $this->getCacheKey( $page );
326 if ( $key ===
null ) {
330 $this->entries->set( $key, [ self::ROW =>
null, self::FLAGS => $queryFlags ] );
342 $key = $this->getCacheKey( $page,
true );
343 if ( $key ===
null ) {
347 $entry = $this->entries->get( $key );
348 if ( $entry && !$entry[self::ROW] ) {
349 $this->entries->clear( $key );
362 $key = $this->getCacheKey( $page );
363 if ( $key !==
null ) {
364 $this->entries->clear( $key );
375 $pageLanguageUseDB = MediaWikiServices::getInstance()->getMainConfig()
376 ->get( MainConfigNames::PageLanguageUseDB );
378 $fields = array_merge(
379 PageStoreRecord::REQUIRED_FIELDS,
382 'page_content_model',
386 if ( $pageLanguageUseDB ) {
387 $fields[] =
'page_lang';
407 public function addLinkObj( $page,
int $queryFlags = IDBAccessObject::READ_NORMAL ) {
409 $page->getNamespace(),
411 [ $this,
'fetchPageRow' ],
415 return $row ? (int)$row->page_id : 0;
426 private function getGoodLinkRowInternal(
428 callable $fetchCallback =
null,
429 int $queryFlags = IDBAccessObject::READ_NORMAL
431 $callerShouldAddGoodLink = false;
433 $key = $this->getCacheKey( $link );
434 if ( $key ===
null ) {
435 return [ $callerShouldAddGoodLink, null ];
441 $entry = $this->entries->get( $key );
442 if ( $entry && $entry[self::FLAGS] >= $queryFlags ) {
443 return [ $callerShouldAddGoodLink, $entry[self::ROW] ?: null ];
446 if ( !$fetchCallback ) {
447 return [ $callerShouldAddGoodLink, null ];
450 $callerShouldAddGoodLink =
true;
452 $wanCacheKey = $this->getPersistentCacheKey( $link );
453 if ( $wanCacheKey !==
null && !( $queryFlags & IDBAccessObject::READ_LATEST ) ) {
455 $row = $this->wanCache->getWithSetCallback(
457 WANObjectCache::TTL_DAY,
458 function ( $curValue, &$ttl, array &$setOpts ) use ( $fetchCallback, $ns, $dbkey ) {
460 $setOpts += Database::getCacheSetOptions(
$dbr );
462 $row = $fetchCallback(
$dbr, $ns, $dbkey, [] );
463 $mtime = $row ? (int)
wfTimestamp( TS_UNIX, $row->page_touched ) : false;
464 $ttl = $this->wanCache->adaptiveTTL( $mtime, $ttl );
472 $dbr = $this->loadBalancer->getConnectionRef( $mode );
473 $row = $fetchCallback(
$dbr, $ns, $dbkey, $options );
476 return [ $callerShouldAddGoodLink, $row ?: null ];
496 callable $fetchCallback =
null,
497 int $queryFlags = IDBAccessObject::READ_NORMAL
500 if ( $link ===
null ) {
504 [ $shouldAddGoodLink, $row ] = $this->getGoodLinkRowInternal(
511 if ( $shouldAddGoodLink ) {
513 $this->addGoodLinkObjFromRow( $link, $row, $queryFlags );
514 }
catch ( InvalidArgumentException $e ) {
516 $this->invalidateTitle( $link );
517 [ , $row ] = $this->getGoodLinkRowInternal(
522 $this->addGoodLinkObjFromRow( $link, $row, $queryFlags );
526 $this->addBadLinkObj( $link );
536 private function getPersistentCacheKey( $page ) {
538 if ( $this->getCacheKey( $page ) ===
null ) {
542 if ( !$this->usePersistentCache( $page ) ) {
546 return $this->wanCache->makeKey(
548 $page->getNamespace(),
549 sha1( $page->getDBkey()
557 private function usePersistentCache( $pageOrNamespace ) {
558 $ns = is_int( $pageOrNamespace ) ? $pageOrNamespace : $pageOrNamespace->getNamespace();
563 if ( $this->nsInfo->isContent( $ns ) ) {
567 return ( $ns >= 100 && $this->nsInfo->isSubject( $ns ) );
577 private function fetchPageRow(
IDatabase $db,
int $ns,
string $dbkey, $options = [] ) {
579 ->select( self::getSelectFields() )
581 ->where( [
'page_namespace' => $ns,
'page_title' => $dbkey ] )
582 ->options( $options );
584 return $queryBuilder->caller( __METHOD__ )->fetchRow();
595 $wanCacheKey = $this->getPersistentCacheKey( $page );
596 if ( $wanCacheKey !==
null ) {
597 $this->wanCache->delete( $wanCacheKey );
600 $this->clearLink( $page );
607 $this->entries->clear();
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
Cache for article titles (prefixed DB keys) and ids linked from one source.
__construct(TitleFormatter $titleFormatter, WANObjectCache $cache, NamespaceInfo $nsInfo, ILoadBalancer $loadBalancer=null)
isBadLink( $page)
Check if a page is known to be missing based on the process cache.
invalidateTitle( $page)
Purge the persistent link cache for a title.
addLinkObj( $page, int $queryFlags=IDBAccessObject::READ_NORMAL)
Add a title to the link cache, return the page_id or zero if non-existent.
clearLink( $page)
Clear information about a page from the process cache.
addGoodLinkObjFromRow( $page, stdClass $row, int $queryFlags=IDBAccessObject::READ_NORMAL)
Add information about an existing page to the process cache.
clearBadLink( $page)
Clear information about a page being missing from the process cache.
addGoodLinkObj( $id, $page, $len=-1, $redir=null, $revision=0, $model=null, $lang=null)
Add information about an existing page to the process cache.
addBadLinkObj( $page, int $queryFlags=IDBAccessObject::READ_NORMAL)
Add information about a missing page to the process cache.
getGoodLinkID( $page)
Get the ID of a page known to the process cache.
setLogger(LoggerInterface $logger)
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).
static getSelectFields()
Fields that LinkCache needs to select.
getGoodLinkFieldObj( $page, string $field)
Get the field of a page known to the process cache.
Handles a simple LRU key/value map with a maximum number of entries.
A class containing constants representing the names of configuration variables.
Immutable data record representing an editable page on a wiki.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents a page (or page fragment) title within MediaWiki.
getDBkey()
Get the main part of the link target, in canonical database form.
getNamespace()
Get the namespace index.
Multi-datacenter aware caching interface.
Interface for objects (potentially) representing an editable wiki page.
canExist()
Checks whether this PageIdentity represents a "proper" page, meaning that it could exist as an editab...
if(!isset( $args[0])) $lang