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 
91  public static function singleton() {
92  return MediaWikiServices::getInstance()->getLinkCache();
93  }
94 
106  public function forUpdate( $update = null ) {
107  return wfSetVar( $this->mForUpdate, $update );
108  }
109 
114  public function getGoodLinkID( $title ) {
115  $info = $this->goodLinks->get( $title );
116  if ( !$info ) {
117  return 0;
118  }
119  return $info['id'];
120  }
121 
129  public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
130  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
131  $info = $this->goodLinks->get( $dbkey );
132  if ( !$info ) {
133  return null;
134  }
135  return $info[$field];
136  }
137 
142  public function isBadLink( $title ) {
143  // Use get() to ensure it records as used for LRU.
144  return $this->badLinks->has( $title );
145  }
146 
158  public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
159  $revision = 0, $model = null, $lang = null
160  ) {
161  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
162  $this->goodLinks->set( $dbkey, [
163  'id' => (int)$id,
164  'length' => (int)$len,
165  'redirect' => (int)$redir,
166  'revision' => (int)$revision,
167  'model' => $model ? (string)$model : null,
168  'lang' => $lang ? (string)$lang : null,
169  'restrictions' => null
170  ] );
171  }
172 
180  public function addGoodLinkObjFromRow( LinkTarget $target, $row ) {
181  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
182  $this->goodLinks->set( $dbkey, [
183  'id' => intval( $row->page_id ),
184  'length' => intval( $row->page_len ),
185  'redirect' => intval( $row->page_is_redirect ),
186  'revision' => intval( $row->page_latest ),
187  'model' => !empty( $row->page_content_model )
188  ? strval( $row->page_content_model )
189  : null,
190  'lang' => !empty( $row->page_lang )
191  ? strval( $row->page_lang )
192  : null,
193  'restrictions' => !empty( $row->page_restrictions )
194  ? strval( $row->page_restrictions )
195  : null
196  ] );
197  }
198 
202  public function addBadLinkObj( LinkTarget $target ) {
203  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
204  if ( !$this->isBadLink( $dbkey ) ) {
205  $this->badLinks->set( $dbkey, 1 );
206  }
207  }
208 
212  public function clearBadLink( $title ) {
213  $this->badLinks->clear( $title );
214  }
215 
219  public function clearLink( LinkTarget $target ) {
220  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
221  $this->badLinks->clear( $dbkey );
222  $this->goodLinks->clear( $dbkey );
223  }
224 
231  public static function getSelectFields() {
232  global $wgPageLanguageUseDB;
233 
234  $fields = [
235  'page_id',
236  'page_len',
237  'page_is_redirect',
238  'page_latest',
239  'page_restrictions',
240  'page_content_model',
241  ];
242 
243  if ( $wgPageLanguageUseDB ) {
244  $fields[] = 'page_lang';
245  }
246 
247  return $fields;
248  }
249 
256  public function addLinkObj( LinkTarget $nt ) {
257  $key = $this->titleFormatter->getPrefixedDBkey( $nt );
258  if ( $this->isBadLink( $key ) || $nt->isExternal() || $nt->getNamespace() < 0 ) {
259  return 0;
260  }
261  $id = $this->getGoodLinkID( $key );
262  if ( $id != 0 ) {
263  return $id;
264  }
265 
266  if ( $key === '' ) {
267  return 0;
268  }
269 
270  // Only query database, when load balancer is provided by service wiring
271  // This maybe not happen when running as part of the installer
272  if ( $this->loadBalancer === null ) {
273  return 0;
274  }
275 
276  // Cache template/file pages as they are less often viewed but heavily used
277  if ( $this->mForUpdate ) {
278  $row = $this->fetchPageRow( $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_MASTER ), $nt );
279  } elseif ( $this->isCacheable( $nt ) ) {
280  // These pages are often transcluded heavily, so cache them
282  $row = $cache->getWithSetCallback(
283  $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
284  $cache::TTL_DAY,
285  function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
286  $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
287  $setOpts += Database::getCacheSetOptions( $dbr );
288 
289  $row = $this->fetchPageRow( $dbr, $nt );
290  $mtime = $row ? wfTimestamp( TS_UNIX, $row->page_touched ) : false;
291  $ttl = $cache->adaptiveTTL( $mtime, $ttl );
292 
293  return $row;
294  }
295  );
296  } else {
297  $row = $this->fetchPageRow( $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA ), $nt );
298  }
299 
300  if ( $row ) {
301  $this->addGoodLinkObjFromRow( $nt, $row );
302  $id = intval( $row->page_id );
303  } else {
304  $this->addBadLinkObj( $nt );
305  $id = 0;
306  }
307 
308  return $id;
309  }
310 
318  if ( $this->isCacheable( $t ) ) {
319  return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
320  }
321 
322  return [];
323  }
324 
325  private function isCacheable( LinkTarget $title ) {
326  $ns = $title->getNamespace();
327  if ( in_array( $ns, [ NS_TEMPLATE, NS_FILE, NS_CATEGORY, NS_MEDIAWIKI ] ) ) {
328  return true;
329  }
330  // Focus on transcluded pages more than the main content
331  if ( $this->nsInfo->isContent( $ns ) ) {
332  return false;
333  }
334  // Non-talk extension namespaces (e.g. NS_MODULE)
335  return ( $ns >= 100 && $this->nsInfo->isSubject( $ns ) );
336  }
337 
338  private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {
339  $fields = self::getSelectFields();
340  if ( $this->isCacheable( $nt ) ) {
341  $fields[] = 'page_touched';
342  }
343 
344  return $db->selectRow(
345  'page',
346  $fields,
347  [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
348  __METHOD__
349  );
350  }
351 
358  public function invalidateTitle( LinkTarget $title ) {
359  if ( $this->isCacheable( $title ) ) {
361  $cache->delete(
362  $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
363  );
364  }
365  }
366 
370  public function clear() {
371  $this->goodLinks->clear();
372  $this->badLinks->clear();
373  }
374 }
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:50
LinkCache\$goodLinks
MapCacheLRU $goodLinks
Definition: LinkCache.php:37
LinkCache\addGoodLinkObjFromRow
addGoodLinkObjFromRow(LinkTarget $target, $row)
Same as above with better interface.
Definition: LinkCache.php:180
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:162
$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:1550
LinkCache\fetchPageRow
fetchPageRow(IDatabase $db, LinkTarget $nt)
Definition: LinkCache.php:338
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1815
LinkCache\getMutableCacheKeys
getMutableCacheKeys(WANObjectCache $cache, LinkTarget $t)
Definition: LinkCache.php:317
NS_FILE
const NS_FILE
Definition: Defines.php:75
NS_TEMPLATE
const NS_TEMPLATE
Definition: Defines.php:79
LinkCache\getGoodLinkFieldObj
getGoodLinkFieldObj(LinkTarget $target, $field)
Get a field of a title object from cache.
Definition: LinkCache.php:129
LinkCache\forUpdate
forUpdate( $update=null)
General accessor to get/set whether the master DB should be used.
Definition: LinkCache.php:106
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:231
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:142
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1027
LinkCache\$titleFormatter
TitleFormatter $titleFormatter
Definition: LinkCache.php:47
LinkCache\addBadLinkObj
addBadLinkObj(LinkTarget $target)
Definition: LinkCache.php:202
MapCacheLRU
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:38
$title
$title
Definition: testCompression.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:83
LinkCache\MAX_SIZE
const MAX_SIZE
How many Titles to store.
Definition: LinkCache.php:59
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:9181
LinkCache\$nsInfo
NamespaceInfo $nsInfo
Definition: LinkCache.php:50
LinkCache\clearBadLink
clearBadLink( $title)
Definition: LinkCache.php:212
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:325
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:125
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
LinkCache\getGoodLinkID
getGoodLinkID( $title)
Definition: LinkCache.php:114
LinkCache\invalidateTitle
invalidateTitle(LinkTarget $title)
Purge the link cache for a title.
Definition: LinkCache.php:358
$cache
$cache
Definition: mcc.php:33
LinkCache\clear
clear()
Clears cache.
Definition: LinkCache.php:370
LinkCache\singleton
static singleton()
Get an instance of this class.
Definition: LinkCache.php:91
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:35
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:77
$t
$t
Definition: testCompression.php:74
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:256
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:158
LinkCache\$wanCache
WANObjectCache $wanCache
Definition: LinkCache.php:41
LinkCache\clearLink
clearLink(LinkTarget $target)
Definition: LinkCache.php:219
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81