MediaWiki  1.30.2
LinkCache.php
Go to the documentation of this file.
1 <?php
28 
34 class LinkCache {
36  private $mGoodLinks;
38  private $mBadLinks;
40  private $wanCache;
41 
43  private $mForUpdate = false;
44 
46  private $titleFormatter;
47 
52  const MAX_SIZE = 10000;
53 
55  $this->mGoodLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
56  $this->mBadLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
57  $this->wanCache = $cache;
58  $this->titleFormatter = $titleFormatter;
59  }
60 
67  public static function singleton() {
68  return MediaWikiServices::getInstance()->getLinkCache();
69  }
70 
81  public function forUpdate( $update = null ) {
82  return wfSetVar( $this->mForUpdate, $update );
83  }
84 
89  public function getGoodLinkID( $title ) {
90  $info = $this->mGoodLinks->get( $title );
91  if ( !$info ) {
92  return 0;
93  }
94  return $info['id'];
95  }
96 
104  public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
105  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
106  $info = $this->mGoodLinks->get( $dbkey );
107  if ( !$info ) {
108  return null;
109  }
110  return $info[$field];
111  }
112 
117  public function isBadLink( $title ) {
118  // Use get() to ensure it records as used for LRU.
119  return $this->mBadLinks->get( $title ) !== false;
120  }
121 
133  public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
134  $revision = 0, $model = null, $lang = null
135  ) {
136  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
137  $this->mGoodLinks->set( $dbkey, [
138  'id' => (int)$id,
139  'length' => (int)$len,
140  'redirect' => (int)$redir,
141  'revision' => (int)$revision,
142  'model' => $model ? (string)$model : null,
143  'lang' => $lang ? (string)$lang : null,
144  ] );
145  }
146 
154  public function addGoodLinkObjFromRow( LinkTarget $target, $row ) {
155  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
156  $this->mGoodLinks->set( $dbkey, [
157  'id' => intval( $row->page_id ),
158  'length' => intval( $row->page_len ),
159  'redirect' => intval( $row->page_is_redirect ),
160  'revision' => intval( $row->page_latest ),
161  'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
162  'lang' => !empty( $row->page_lang ) ? strval( $row->page_lang ) : null,
163  ] );
164  }
165 
169  public function addBadLinkObj( LinkTarget $target ) {
170  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
171  if ( !$this->isBadLink( $dbkey ) ) {
172  $this->mBadLinks->set( $dbkey, 1 );
173  }
174  }
175 
179  public function clearBadLink( $title ) {
180  $this->mBadLinks->delete( $title );
181  }
182 
186  public function clearLink( LinkTarget $target ) {
187  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
188  $this->mBadLinks->delete( $dbkey );
189  $this->mGoodLinks->delete( $dbkey );
190  }
191 
199  public function addLink( $title ) {
200  $nt = Title::newFromDBkey( $title );
201  if ( !$nt ) {
202  return 0;
203  }
204  return $this->addLinkObj( $nt );
205  }
206 
213  public static function getSelectFields() {
215 
216  $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
217  if ( $wgContentHandlerUseDB ) {
218  $fields[] = 'page_content_model';
219  }
220  if ( $wgPageLanguageUseDB ) {
221  $fields[] = 'page_lang';
222  }
223 
224  return $fields;
225  }
226 
233  public function addLinkObj( LinkTarget $nt ) {
234  $key = $this->titleFormatter->getPrefixedDBkey( $nt );
235  if ( $this->isBadLink( $key ) || $nt->isExternal()
236  || $nt->inNamespace( NS_SPECIAL )
237  ) {
238  return 0;
239  }
240  $id = $this->getGoodLinkID( $key );
241  if ( $id != 0 ) {
242  return $id;
243  }
244 
245  if ( $key === '' ) {
246  return 0;
247  }
248 
249  // Cache template/file pages as they are less often viewed but heavily used
250  if ( $this->mForUpdate ) {
251  $row = $this->fetchPageRow( wfGetDB( DB_MASTER ), $nt );
252  } elseif ( $this->isCacheable( $nt ) ) {
253  // These pages are often transcluded heavily, so cache them
255  $row = $cache->getWithSetCallback(
256  $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
257  $cache::TTL_DAY,
258  function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
259  $dbr = wfGetDB( DB_REPLICA );
260  $setOpts += Database::getCacheSetOptions( $dbr );
261 
262  $row = $this->fetchPageRow( $dbr, $nt );
263  $mtime = $row ? wfTimestamp( TS_UNIX, $row->page_touched ) : false;
264  $ttl = $cache->adaptiveTTL( $mtime, $ttl );
265 
266  return $row;
267  }
268  );
269  } else {
270  $row = $this->fetchPageRow( wfGetDB( DB_REPLICA ), $nt );
271  }
272 
273  if ( $row ) {
274  $this->addGoodLinkObjFromRow( $nt, $row );
275  $id = intval( $row->page_id );
276  } else {
277  $this->addBadLinkObj( $nt );
278  $id = 0;
279  }
280 
281  return $id;
282  }
283 
291  if ( $this->isCacheable( $t ) ) {
292  return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
293  }
294 
295  return [];
296  }
297 
298  private function isCacheable( LinkTarget $title ) {
299  return ( $title->inNamespace( NS_TEMPLATE ) || $title->inNamespace( NS_FILE ) );
300  }
301 
302  private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {
303  $fields = self::getSelectFields();
304  if ( $this->isCacheable( $nt ) ) {
305  $fields[] = 'page_touched';
306  }
307 
308  return $db->selectRow(
309  'page',
310  $fields,
311  [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
312  __METHOD__
313  );
314  }
315 
322  public function invalidateTitle( LinkTarget $title ) {
323  if ( $this->isCacheable( $title ) ) {
325  $cache->delete(
326  $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
327  );
328  }
329  }
330 
334  public function clear() {
335  $this->mGoodLinks->clear();
336  $this->mBadLinks->clear();
337  }
338 }
LinkCache\$mBadLinks
HashBagOStuff $mBadLinks
Definition: LinkCache.php:38
LinkCache\addLink
addLink( $title)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:199
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:45
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
LinkCache\addGoodLinkObjFromRow
addGoodLinkObjFromRow(LinkTarget $target, $row)
Same as above with better interface.
Definition: LinkCache.php:154
$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:1759
LinkCache\fetchPageRow
fetchPageRow(IDatabase $db, LinkTarget $nt)
Definition: LinkCache.php:302
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2040
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
NS_FILE
const NS_FILE
Definition: Defines.php:71
NS_TEMPLATE
const NS_TEMPLATE
Definition: Defines.php:75
LinkCache\getGoodLinkFieldObj
getGoodLinkFieldObj(LinkTarget $target, $field)
Get a field of a title object from cache.
Definition: LinkCache.php:104
MediaWiki\Linker\LinkTarget\inNamespace
inNamespace( $ns)
Convenience function to test if it is in the namespace.
LinkCache\forUpdate
forUpdate( $update=null)
General accessor to get/set whether the master DB should be used.
Definition: LinkCache.php:81
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:8480
LinkCache\getSelectFields
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:213
LinkCache\getMutableCacheKeys
getMutableCacheKeys(WANObjectCache $cache, TitleValue $t)
Definition: LinkCache.php:290
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:54
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
LinkCache\isBadLink
isBadLink( $title)
Definition: LinkCache.php:117
LinkCache\$titleFormatter
TitleFormatter $titleFormatter
Definition: LinkCache.php:46
LinkCache\__construct
__construct(TitleFormatter $titleFormatter, WANObjectCache $cache)
Definition: LinkCache.php:54
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2856
LinkCache\addBadLinkObj
addBadLinkObj(LinkTarget $target)
Definition: LinkCache.php:169
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
LinkCache\MAX_SIZE
const MAX_SIZE
How many Titles to store.
Definition: LinkCache.php:52
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:8563
LinkCache\clearBadLink
clearBadLink( $title)
Definition: LinkCache.php:179
LinkCache\$mForUpdate
bool $mForUpdate
Definition: LinkCache.php:43
LinkCache\isCacheable
isCacheable(LinkTarget $title)
Definition: LinkCache.php:298
Wikimedia\Rdbms\IDatabase\selectRow
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Single row SELECT wrapper.
LinkCache\$mGoodLinks
HashBagOStuff $mGoodLinks
Definition: LinkCache.php:36
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:80
Title\newFromDBkey
static newFromDBkey( $key)
Create a new Title from a prefixed DB key.
Definition: Title.php:209
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
LinkCache\getGoodLinkID
getGoodLinkID( $title)
Definition: LinkCache.php:89
LinkCache\invalidateTitle
invalidateTitle(LinkTarget $title)
Purge the link cache for a title.
Definition: LinkCache.php:322
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$cache
$cache
Definition: mcc.php:33
ObjectCache\getMainWANInstance
static getMainWANInstance()
Get the main WAN cache object.
Definition: ObjectCache.php:370
LinkCache\clear
clear()
Clears cache.
Definition: LinkCache.php:334
LinkCache\singleton
static singleton()
Get an instance of this class.
Definition: LinkCache.php:67
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:35
$t
$t
Definition: testCompression.php:67
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:27
LinkCache\addLinkObj
addLinkObj(LinkTarget $nt)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:233
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:133
LinkCache\$wanCache
WANObjectCache $wanCache
Definition: LinkCache.php:40
LinkCache\clearLink
clearLink(LinkTarget $target)
Definition: LinkCache.php:186
array
the array() calling protocol came about after MediaWiki 1.4rc1.
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:36