MediaWiki  1.34.0
LinkCache.php
Go to the documentation of this file.
1 <?php
28 
34 class LinkCache {
36  private $goodLinks;
38  private $badLinks;
40  private $wanCache;
41 
43  private $mForUpdate = false;
44 
46  private $titleFormatter;
47 
49  private $nsInfo;
50 
55  const MAX_SIZE = 10000;
56 
57  public function __construct(
60  NamespaceInfo $nsInfo = null
61  ) {
62  if ( !$nsInfo ) {
63  wfDeprecated( __METHOD__ . ' with no NamespaceInfo argument', '1.34' );
64  $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
65  }
66  $this->goodLinks = new MapCacheLRU( self::MAX_SIZE );
67  $this->badLinks = new MapCacheLRU( self::MAX_SIZE );
68  $this->wanCache = $cache;
69  $this->titleFormatter = $titleFormatter;
70  $this->nsInfo = $nsInfo;
71  }
72 
79  public static function singleton() {
80  return MediaWikiServices::getInstance()->getLinkCache();
81  }
82 
94  public function forUpdate( $update = null ) {
95  return wfSetVar( $this->mForUpdate, $update );
96  }
97 
102  public function getGoodLinkID( $title ) {
103  $info = $this->goodLinks->get( $title );
104  if ( !$info ) {
105  return 0;
106  }
107  return $info['id'];
108  }
109 
117  public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
118  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
119  $info = $this->goodLinks->get( $dbkey );
120  if ( !$info ) {
121  return null;
122  }
123  return $info[$field];
124  }
125 
130  public function isBadLink( $title ) {
131  // Use get() to ensure it records as used for LRU.
132  return $this->badLinks->has( $title );
133  }
134 
146  public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
147  $revision = 0, $model = null, $lang = null
148  ) {
149  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
150  $this->goodLinks->set( $dbkey, [
151  'id' => (int)$id,
152  'length' => (int)$len,
153  'redirect' => (int)$redir,
154  'revision' => (int)$revision,
155  'model' => $model ? (string)$model : null,
156  'lang' => $lang ? (string)$lang : null,
157  'restrictions' => null
158  ] );
159  }
160 
168  public function addGoodLinkObjFromRow( LinkTarget $target, $row ) {
169  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
170  $this->goodLinks->set( $dbkey, [
171  'id' => intval( $row->page_id ),
172  'length' => intval( $row->page_len ),
173  'redirect' => intval( $row->page_is_redirect ),
174  'revision' => intval( $row->page_latest ),
175  'model' => !empty( $row->page_content_model )
176  ? strval( $row->page_content_model )
177  : null,
178  'lang' => !empty( $row->page_lang )
179  ? strval( $row->page_lang )
180  : null,
181  'restrictions' => !empty( $row->page_restrictions )
182  ? strval( $row->page_restrictions )
183  : null
184  ] );
185  }
186 
190  public function addBadLinkObj( LinkTarget $target ) {
191  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
192  if ( !$this->isBadLink( $dbkey ) ) {
193  $this->badLinks->set( $dbkey, 1 );
194  }
195  }
196 
200  public function clearBadLink( $title ) {
201  $this->badLinks->clear( $title );
202  }
203 
207  public function clearLink( LinkTarget $target ) {
208  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
209  $this->badLinks->clear( $dbkey );
210  $this->goodLinks->clear( $dbkey );
211  }
212 
219  public static function getSelectFields() {
221 
222  $fields = [
223  'page_id',
224  'page_len',
225  'page_is_redirect',
226  'page_latest',
227  'page_restrictions'
228  ];
229  if ( $wgContentHandlerUseDB ) {
230  $fields[] = 'page_content_model';
231  }
232  if ( $wgPageLanguageUseDB ) {
233  $fields[] = 'page_lang';
234  }
235 
236  return $fields;
237  }
238 
245  public function addLinkObj( LinkTarget $nt ) {
246  $key = $this->titleFormatter->getPrefixedDBkey( $nt );
247  if ( $this->isBadLink( $key ) || $nt->isExternal() || $nt->getNamespace() < 0 ) {
248  return 0;
249  }
250  $id = $this->getGoodLinkID( $key );
251  if ( $id != 0 ) {
252  return $id;
253  }
254 
255  if ( $key === '' ) {
256  return 0;
257  }
258 
259  // Cache template/file pages as they are less often viewed but heavily used
260  if ( $this->mForUpdate ) {
261  $row = $this->fetchPageRow( wfGetDB( DB_MASTER ), $nt );
262  } elseif ( $this->isCacheable( $nt ) ) {
263  // These pages are often transcluded heavily, so cache them
265  $row = $cache->getWithSetCallback(
266  $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
267  $cache::TTL_DAY,
268  function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
269  $dbr = wfGetDB( DB_REPLICA );
270  $setOpts += Database::getCacheSetOptions( $dbr );
271 
272  $row = $this->fetchPageRow( $dbr, $nt );
273  $mtime = $row ? wfTimestamp( TS_UNIX, $row->page_touched ) : false;
274  $ttl = $cache->adaptiveTTL( $mtime, $ttl );
275 
276  return $row;
277  }
278  );
279  } else {
280  $row = $this->fetchPageRow( wfGetDB( DB_REPLICA ), $nt );
281  }
282 
283  if ( $row ) {
284  $this->addGoodLinkObjFromRow( $nt, $row );
285  $id = intval( $row->page_id );
286  } else {
287  $this->addBadLinkObj( $nt );
288  $id = 0;
289  }
290 
291  return $id;
292  }
293 
301  if ( $this->isCacheable( $t ) ) {
302  return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
303  }
304 
305  return [];
306  }
307 
308  private function isCacheable( LinkTarget $title ) {
309  $ns = $title->getNamespace();
310  if ( in_array( $ns, [ NS_TEMPLATE, NS_FILE, NS_CATEGORY, NS_MEDIAWIKI ] ) ) {
311  return true;
312  }
313  // Focus on transcluded pages more than the main content
314  if ( $this->nsInfo->isContent( $ns ) ) {
315  return false;
316  }
317  // Non-talk extension namespaces (e.g. NS_MODULE)
318  return ( $ns >= 100 && $this->nsInfo->isSubject( $ns ) );
319  }
320 
321  private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {
322  $fields = self::getSelectFields();
323  if ( $this->isCacheable( $nt ) ) {
324  $fields[] = 'page_touched';
325  }
326 
327  return $db->selectRow(
328  'page',
329  $fields,
330  [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
331  __METHOD__
332  );
333  }
334 
341  public function invalidateTitle( LinkTarget $title ) {
342  if ( $this->isCacheable( $title ) ) {
344  $cache->delete(
345  $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
346  );
347  }
348  }
349 
353  public function clear() {
354  $this->goodLinks->clear();
355  $this->badLinks->clear();
356  }
357 }
LinkCache\__construct
__construct(TitleFormatter $titleFormatter, WANObjectCache $cache, NamespaceInfo $nsInfo=null)
Definition: LinkCache.php:57
LinkCache
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:34
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:49
LinkCache\$goodLinks
MapCacheLRU $goodLinks
Definition: LinkCache.php:36
LinkCache\addGoodLinkObjFromRow
addGoodLinkObjFromRow(LinkTarget $target, $row)
Same as above with better interface.
Definition: LinkCache.php:168
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
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:1607
LinkCache\fetchPageRow
fetchPageRow(IDatabase $db, LinkTarget $nt)
Definition: LinkCache.php:321
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
LinkCache\getMutableCacheKeys
getMutableCacheKeys(WANObjectCache $cache, LinkTarget $t)
Definition: LinkCache.php:300
NS_FILE
const NS_FILE
Definition: Defines.php:66
NS_TEMPLATE
const NS_TEMPLATE
Definition: Defines.php:70
LinkCache\getGoodLinkFieldObj
getGoodLinkFieldObj(LinkTarget $target, $field)
Get a field of a title object from cache.
Definition: LinkCache.php:117
LinkCache\forUpdate
forUpdate( $update=null)
General accessor to get/set whether the master DB should be used.
Definition: LinkCache.php:94
MediaWiki\Linker\LinkTarget\isExternal
isExternal()
Whether this LinkTarget has an interwiki component.
$wgContentHandlerUseDB
$wgContentHandlerUseDB
Set to false to disable use of the database fields introduced by the ContentHandler facility.
Definition: DefaultSettings.php:8642
LinkCache\getSelectFields
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:219
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:38
$dbr
$dbr
Definition: testCompression.php:50
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
LinkCache\isBadLink
isBadLink( $title)
Definition: LinkCache.php:130
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1044
LinkCache\$titleFormatter
TitleFormatter $titleFormatter
Definition: LinkCache.php:46
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
LinkCache\addBadLinkObj
addBadLinkObj(LinkTarget $target)
Definition: LinkCache.php:190
MapCacheLRU
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:37
$t
$t
Definition: make-normalization-table.php:143
$title
$title
Definition: testCompression.php:34
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:74
LinkCache\MAX_SIZE
const MAX_SIZE
How many Titles to store.
Definition: LinkCache.php:55
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$wgPageLanguageUseDB
bool $wgPageLanguageUseDB
Enable page language feature Allows setting page language in database.
Definition: DefaultSettings.php:8730
LinkCache\$nsInfo
NamespaceInfo $nsInfo
Definition: LinkCache.php:49
LinkCache\clearBadLink
clearBadLink( $title)
Definition: LinkCache.php:200
LinkCache\$mForUpdate
bool $mForUpdate
Definition: LinkCache.php:43
LinkCache\isCacheable
isCacheable(LinkTarget $title)
Definition: LinkCache.php:308
Wikimedia\Rdbms\IDatabase\selectRow
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Wrapper to IDatabase::select() that only fetches one row (via LIMIT)
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:116
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
LinkCache\getGoodLinkID
getGoodLinkID( $title)
Definition: LinkCache.php:102
LinkCache\invalidateTitle
invalidateTitle(LinkTarget $title)
Purge the link cache for a title.
Definition: LinkCache.php:341
$cache
$cache
Definition: mcc.php:33
LinkCache\clear
clear()
Clears cache.
Definition: LinkCache.php:353
LinkCache\singleton
static singleton()
Get an instance of this class.
Definition: LinkCache.php:79
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:34
NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Definition: NamespaceInfo.php:33
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:68
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:245
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:146
LinkCache\$wanCache
WANObjectCache $wanCache
Definition: LinkCache.php:40
LinkCache\clearLink
clearLink(LinkTarget $target)
Definition: LinkCache.php:207