59 private $titleFormatter;
63 private $loadBalancer;
68 private const MAX_SIZE = 10000;
71 private const ROW = 0;
73 private const FLAGS = 1;
88 $this->wanCache = $cache;
89 $this->titleFormatter = $titleFormatter;
90 $this->nsInfo = $nsInfo;
91 $this->loadBalancer = $loadBalancer;
92 $this->logger =
new NullLogger();
98 public function setLogger( LoggerInterface $logger ) {
99 $this->logger = $logger;
107 private function getCacheKey( $page, $passThrough =
false ) {
108 if ( is_string( $page ) ) {
109 if ( $passThrough ) {
112 throw new InvalidArgumentException(
'They key may not be given as a string here' );
116 if ( is_array( $page ) ) {
117 $namespace = $page[
'page_namespace'];
118 $dbkey = $page[
'page_title'];
119 return strtr( $this->titleFormatter->formatTitle( $namespace, $dbkey ),
' ',
'_' );
122 if ( $page instanceof PageReference && $page->getWikiId() !== PageReference::LOCAL ) {
125 'cross-wiki page reference',
127 'page-wiki' => $page->getWikiId(),
128 'page-reference' => $this->titleFormatter->getFullText( $page )
134 if ( $page instanceof PageIdentity && !$page->canExist() ) {
136 $this->logger->warning(
137 'non-proper page reference: {page-reference}',
138 [
'page-reference' => $this->titleFormatter->getFullText( $page ) ]
143 if ( $page instanceof LinkTarget
144 && ( $page->isExternal() || $page->getText() ===
'' || $page->getNamespace() < 0 )
148 $this->logger->warning(
149 'link to non-proper page: {page-link}',
150 [
'page-link' => $this->titleFormatter->getFullText( $page ) ]
155 return $this->titleFormatter->getPrefixedDBkey( $page );
169 if ( $key ===
null ) {
173 $entry = $this->entries->get( $key );
178 $row = $entry[self::ROW];
180 return $row ? (int)$row->page_id : 0;
197 if ( $key ===
null ) {
201 $entry = $this->entries->get( $key );
206 $row = $entry[self::ROW];
213 return (
int)$row->page_id;
215 return (
int)$row->page_len;
217 return (
int)$row->page_is_redirect;
219 return (
int)$row->page_latest;
221 return !empty( $row->page_content_model )
222 ? (string)$row->page_content_model
225 return !empty( $row->page_lang )
226 ? (string)$row->page_lang
229 throw new InvalidArgumentException(
"Unknown field: $field" );
244 if ( $key ===
null ) {
248 $entry = $this->entries->get( $key );
250 return ( $entry && !$entry[self::ROW] );
272 int $queryFlags = IDBAccessObject::READ_NORMAL
275 if ( $key ===
null ) {
279 foreach ( self::getSelectFields() as $field ) {
280 if ( !property_exists( $row, $field ) ) {
281 throw new InvalidArgumentException(
"Missing field: $field" );
285 $this->entries->set( $key, [ self::ROW => $row, self::FLAGS => $queryFlags ] );
302 public function addBadLinkObj( $page,
int $queryFlags = IDBAccessObject::READ_NORMAL ) {
304 if ( $key ===
null ) {
308 $this->entries->set( $key, [ self::ROW =>
null, self::FLAGS => $queryFlags ] );
321 if ( $key ===
null ) {
325 $entry = $this->entries->get( $key );
326 if ( $entry && !$entry[self::ROW] ) {
327 $this->entries->clear( $key );
341 if ( $key !==
null ) {
342 $this->entries->clear( $key );
356 $fields = array_merge(
360 'page_content_model',
364 if ( $pageLanguageUseDB ) {
365 $fields[] =
'page_lang';
385 public function addLinkObj( $page,
int $queryFlags = IDBAccessObject::READ_NORMAL ) {
387 $page->getNamespace(),
389 [ $this,
'fetchPageRow' ],
393 return $row ? (int)$row->page_id : 0;
404 private function getGoodLinkRowInternal(
406 callable $fetchCallback =
null,
407 int $queryFlags = IDBAccessObject::READ_NORMAL
409 $callerShouldAddGoodLink = false;
412 if ( $key ===
null ) {
413 return [ $callerShouldAddGoodLink, null ];
419 $entry = $this->entries->get( $key );
420 if ( $entry && $entry[self::FLAGS] >= $queryFlags ) {
421 return [ $callerShouldAddGoodLink, $entry[self::ROW] ?: null ];
424 if ( !$fetchCallback ) {
425 return [ $callerShouldAddGoodLink, null ];
428 $callerShouldAddGoodLink =
true;
430 $wanCacheKey = $this->getPersistentCacheKey( $link );
431 if ( $wanCacheKey !==
null && !( $queryFlags & IDBAccessObject::READ_LATEST ) ) {
433 $row = $this->wanCache->getWithSetCallback(
435 WANObjectCache::TTL_DAY,
436 function ( $curValue, &$ttl, array &$setOpts ) use ( $fetchCallback, $ns, $dbkey ) {
437 $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
440 $row = $fetchCallback( $dbr, $ns, $dbkey, [] );
441 $mtime = $row ? (int)
wfTimestamp( TS_UNIX, $row->page_touched ) : false;
442 $ttl = $this->wanCache->adaptiveTTL( $mtime, $ttl );
450 $dbr = $this->loadBalancer->getConnectionRef( $mode );
451 $row = $fetchCallback( $dbr, $ns, $dbkey, $options );
454 return [ $callerShouldAddGoodLink, $row ?: null ];
474 callable $fetchCallback =
null,
475 int $queryFlags = IDBAccessObject::READ_NORMAL
478 if ( $link ===
null ) {
482 [ $shouldAddGoodLink, $row ] = $this->getGoodLinkRowInternal(
489 if ( $shouldAddGoodLink ) {
491 $this->addGoodLinkObjFromRow( $link, $row, $queryFlags );
492 }
catch ( InvalidArgumentException $e ) {
494 $this->invalidateTitle( $link );
495 [ , $row ] = $this->getGoodLinkRowInternal(
500 $this->addGoodLinkObjFromRow( $link, $row, $queryFlags );
504 $this->addBadLinkObj( $link );
514 private function getPersistentCacheKey( $page ) {
516 if ( $this->
getCacheKey( $page ) ===
null || !$this->usePersistentCache( $page ) ) {
520 return $this->wanCache->makeKey(
522 $page->getNamespace(),
523 sha1( $page->getDBkey()
531 private function usePersistentCache( $pageOrNamespace ) {
532 $ns = is_int( $pageOrNamespace ) ? $pageOrNamespace : $pageOrNamespace->getNamespace();
537 if ( $this->nsInfo->isContent( $ns ) ) {
541 return ( $ns >= 100 && $this->nsInfo->isSubject( $ns ) );
551 private function fetchPageRow( IDatabase $db,
int $ns,
string $dbkey, $options = [] ) {
552 $queryBuilder = $db->newSelectQueryBuilder()
553 ->select( self::getSelectFields() )
555 ->where( [
'page_namespace' => $ns,
'page_title' => $dbkey ] )
556 ->options( $options );
558 return $queryBuilder->caller( __METHOD__ )->fetchRow();
569 $wanCacheKey = $this->getPersistentCacheKey( $page );
570 if ( $wanCacheKey !==
null ) {
571 $this->wanCache->delete( $wanCacheKey );
574 $this->clearLink( $page );
581 $this->entries->clear();