MediaWiki  master
LinkCache.php
Go to the documentation of this file.
1 <?php
29 
35 class LinkCache {
37  private $goodLinks;
39  private $badLinks;
41  private $wanCache;
42 
44  private $mForUpdate = false;
45 
47  private $titleFormatter;
48 
50  private $nsInfo;
51 
53  private $loadBalancer;
54 
59  private const MAX_SIZE = 10000;
60 
67  public function __construct(
70  NamespaceInfo $nsInfo = null,
72  ) {
73  if ( !$nsInfo ) {
74  wfDeprecated( __METHOD__ . ' with no NamespaceInfo argument', '1.34' );
75  $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
76  }
77  $this->goodLinks = new MapCacheLRU( self::MAX_SIZE );
78  $this->badLinks = new MapCacheLRU( self::MAX_SIZE );
79  $this->wanCache = $cache;
80  $this->titleFormatter = $titleFormatter;
81  $this->nsInfo = $nsInfo;
82  $this->loadBalancer = $loadBalancer;
83  }
84 
92  public static function singleton() {
93  wfDeprecated( __METHOD__, '1.28' );
94  return MediaWikiServices::getInstance()->getLinkCache();
95  }
96 
108  public function forUpdate( $update = null ) {
109  return wfSetVar( $this->mForUpdate, $update );
110  }
111 
116  public function getGoodLinkID( $title ) {
117  $info = $this->goodLinks->get( $title );
118  if ( !$info ) {
119  return 0;
120  }
121  return $info['id'];
122  }
123 
131  public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
132  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
133  $info = $this->goodLinks->get( $dbkey );
134  if ( !$info ) {
135  return null;
136  }
137  return $info[$field];
138  }
139 
144  public function isBadLink( $title ) {
145  // Use get() to ensure it records as used for LRU.
146  return $this->badLinks->has( $title );
147  }
148 
160  public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
161  $revision = 0, $model = null, $lang = null
162  ) {
163  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
164  $this->goodLinks->set( $dbkey, [
165  'id' => (int)$id,
166  'length' => (int)$len,
167  'redirect' => (int)$redir,
168  'revision' => (int)$revision,
169  'model' => $model ? (string)$model : null,
170  'lang' => $lang ? (string)$lang : null,
171  'restrictions' => null
172  ] );
173  }
174 
182  public function addGoodLinkObjFromRow( LinkTarget $target, $row ) {
183  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
184  $this->goodLinks->set( $dbkey, [
185  'id' => intval( $row->page_id ),
186  'length' => intval( $row->page_len ),
187  'redirect' => intval( $row->page_is_redirect ),
188  'revision' => intval( $row->page_latest ),
189  'model' => !empty( $row->page_content_model )
190  ? strval( $row->page_content_model )
191  : null,
192  'lang' => !empty( $row->page_lang )
193  ? strval( $row->page_lang )
194  : null,
195  'restrictions' => !empty( $row->page_restrictions )
196  ? strval( $row->page_restrictions )
197  : null
198  ] );
199  }
200 
204  public function addBadLinkObj( LinkTarget $target ) {
205  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
206  if ( !$this->isBadLink( $dbkey ) ) {
207  $this->badLinks->set( $dbkey, 1 );
208  }
209  }
210 
214  public function clearBadLink( $title ) {
215  $this->badLinks->clear( $title );
216  }
217 
221  public function clearLink( LinkTarget $target ) {
222  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
223  $this->badLinks->clear( $dbkey );
224  $this->goodLinks->clear( $dbkey );
225  }
226 
233  public static function getSelectFields() {
234  global $wgPageLanguageUseDB;
235 
236  $fields = [
237  'page_id',
238  'page_len',
239  'page_is_redirect',
240  'page_latest',
241  'page_restrictions',
242  'page_content_model',
243  ];
244 
245  if ( $wgPageLanguageUseDB ) {
246  $fields[] = 'page_lang';
247  }
248 
249  return $fields;
250  }
251 
258  public function addLinkObj( LinkTarget $nt ) {
259  $key = $this->titleFormatter->getPrefixedDBkey( $nt );
260  if ( $this->isBadLink( $key ) || $nt->isExternal() || $nt->getNamespace() < 0 ) {
261  return 0;
262  }
263  $id = $this->getGoodLinkID( $key );
264  if ( $id != 0 ) {
265  return $id;
266  }
267 
268  if ( $key === '' ) {
269  return 0;
270  }
271 
272  // Only query database, when load balancer is provided by service wiring
273  // This maybe not happen when running as part of the installer
274  if ( $this->loadBalancer === null ) {
275  return 0;
276  }
277 
278  // Cache template/file pages as they are less often viewed but heavily used
279  if ( $this->mForUpdate ) {
280  $row = $this->fetchPageRow( $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_PRIMARY ), $nt );
281  } elseif ( $this->isCacheable( $nt ) ) {
282  // These pages are often transcluded heavily, so cache them
284  $row = $cache->getWithSetCallback(
285  $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
286  $cache::TTL_DAY,
287  function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
288  $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
289  $setOpts += Database::getCacheSetOptions( $dbr );
290 
291  $row = $this->fetchPageRow( $dbr, $nt );
292  $mtime = $row ? wfTimestamp( TS_UNIX, $row->page_touched ) : false;
293  $ttl = $cache->adaptiveTTL( $mtime, $ttl );
294 
295  return $row;
296  }
297  );
298  } else {
299  $row = $this->fetchPageRow( $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA ), $nt );
300  }
301 
302  if ( $row ) {
303  $this->addGoodLinkObjFromRow( $nt, $row );
304  $id = intval( $row->page_id );
305  } else {
306  $this->addBadLinkObj( $nt );
307  $id = 0;
308  }
309 
310  return $id;
311  }
312 
320  if ( $this->isCacheable( $t ) ) {
321  return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
322  }
323 
324  return [];
325  }
326 
327  private function isCacheable( LinkTarget $title ) {
328  $ns = $title->getNamespace();
329  if ( in_array( $ns, [ NS_TEMPLATE, NS_FILE, NS_CATEGORY, NS_MEDIAWIKI ] ) ) {
330  return true;
331  }
332  // Focus on transcluded pages more than the main content
333  if ( $this->nsInfo->isContent( $ns ) ) {
334  return false;
335  }
336  // Non-talk extension namespaces (e.g. NS_MODULE)
337  return ( $ns >= 100 && $this->nsInfo->isSubject( $ns ) );
338  }
339 
340  private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {
341  $fields = self::getSelectFields();
342  if ( $this->isCacheable( $nt ) ) {
343  $fields[] = 'page_touched';
344  }
345 
346  return $db->selectRow(
347  'page',
348  $fields,
349  [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
350  __METHOD__
351  );
352  }
353 
360  public function invalidateTitle( LinkTarget $title ) {
361  if ( $this->isCacheable( $title ) ) {
363  $cache->delete(
364  $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
365  );
366  }
367  }
368 
372  public function clear() {
373  $this->goodLinks->clear();
374  $this->badLinks->clear();
375  }
376 }
LinkCache\__construct
__construct(TitleFormatter $titleFormatter, WANObjectCache $cache, NamespaceInfo $nsInfo=null, ILoadBalancer $loadBalancer=null)
Definition: LinkCache.php:67
LinkCache
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:35
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:52
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:72
LinkCache\$goodLinks
MapCacheLRU $goodLinks
Definition: LinkCache.php:37
LinkCache\addGoodLinkObjFromRow
addGoodLinkObjFromRow(LinkTarget $target, $row)
Same as above with better interface.
Definition: LinkCache.php:182
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:180
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
wfSetVar
wfSetVar(&$dest, $source, $force=false)
Sets dest to source and returns the original value of dest If source is NULL, it just returns the val...
Definition: GlobalFunctions.php:1512
LinkCache\fetchPageRow
fetchPageRow(IDatabase $db, LinkTarget $nt)
Definition: LinkCache.php:340
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1692
LinkCache\getMutableCacheKeys
getMutableCacheKeys(WANObjectCache $cache, LinkTarget $t)
Definition: LinkCache.php:319
LinkCache\getGoodLinkFieldObj
getGoodLinkFieldObj(LinkTarget $target, $field)
Get a field of a title object from cache.
Definition: LinkCache.php:131
LinkCache\forUpdate
forUpdate( $update=null)
General accessor to get/set whether the master DB should be used.
Definition: LinkCache.php:108
MediaWiki\Linker\LinkTarget\isExternal
isExternal()
Whether this LinkTarget has an interwiki component.
LinkCache\getSelectFields
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:233
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
LinkCache\$badLinks
MapCacheLRU $badLinks
Definition: LinkCache.php:39
$dbr
$dbr
Definition: testCompression.php:54
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
LinkCache\isBadLink
isBadLink( $title)
Definition: LinkCache.php:144
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:996
LinkCache\$titleFormatter
TitleFormatter $titleFormatter
Definition: LinkCache.php:47
LinkCache\addBadLinkObj
addBadLinkObj(LinkTarget $target)
Definition: LinkCache.php:204
NS_TEMPLATE
const NS_TEMPLATE
Definition: Defines.php:74
MapCacheLRU
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:37
$title
$title
Definition: testCompression.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
LinkCache\MAX_SIZE
const MAX_SIZE
How many Titles to store.
Definition: LinkCache.php:59
$wgPageLanguageUseDB
bool $wgPageLanguageUseDB
Enable page language feature Allows setting page language in database.
Definition: DefaultSettings.php:2512
LinkCache\$nsInfo
NamespaceInfo $nsInfo
Definition: LinkCache.php:50
LinkCache\clearBadLink
clearBadLink( $title)
Definition: LinkCache.php:214
LinkCache\$loadBalancer
ILoadBalancer null $loadBalancer
Definition: LinkCache.php:53
LinkCache\$mForUpdate
bool $mForUpdate
Definition: LinkCache.php:44
LinkCache\isCacheable
isCacheable(LinkTarget $title)
Definition: LinkCache.php:327
Wikimedia\Rdbms\IDatabase\selectRow
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Wrapper to IDatabase::select() that only fetches one row (via LIMIT)
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:129
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
LinkCache\getGoodLinkID
getGoodLinkID( $title)
Definition: LinkCache.php:116
LinkCache\invalidateTitle
invalidateTitle(LinkTarget $title)
Purge the link cache for a title.
Definition: LinkCache.php:360
$cache
$cache
Definition: mcc.php:33
LinkCache\clear
clear()
Clears cache.
Definition: LinkCache.php:372
LinkCache\singleton
static singleton()
Get an instance of this class.
Definition: LinkCache.php:92
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:35
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Definition: NamespaceInfo.php:35
$t
$t
Definition: testCompression.php:74
NS_FILE
const NS_FILE
Definition: Defines.php:70
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
LinkCache\addLinkObj
addLinkObj(LinkTarget $nt)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:258
LinkCache\addGoodLinkObj
addGoodLinkObj( $id, LinkTarget $target, $len=-1, $redir=null, $revision=0, $model=null, $lang=null)
Add a link for the title to the link cache.
Definition: LinkCache.php:160
LinkCache\$wanCache
WANObjectCache $wanCache
Definition: LinkCache.php:41
LinkCache\clearLink
clearLink(LinkTarget $target)
Definition: LinkCache.php:221
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81