MediaWiki  master
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(
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 
93  public function forUpdate( $update = null ) {
94  return wfSetVar( $this->mForUpdate, $update );
95  }
96 
101  public function getGoodLinkID( $title ) {
102  $info = $this->goodLinks->get( $title );
103  if ( !$info ) {
104  return 0;
105  }
106  return $info['id'];
107  }
108 
116  public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
117  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
118  $info = $this->goodLinks->get( $dbkey );
119  if ( !$info ) {
120  return null;
121  }
122  return $info[$field];
123  }
124 
129  public function isBadLink( $title ) {
130  // Use get() to ensure it records as used for LRU.
131  return $this->badLinks->has( $title );
132  }
133 
145  public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
146  $revision = 0, $model = null, $lang = null
147  ) {
148  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
149  $this->goodLinks->set( $dbkey, [
150  'id' => (int)$id,
151  'length' => (int)$len,
152  'redirect' => (int)$redir,
153  'revision' => (int)$revision,
154  'model' => $model ? (string)$model : null,
155  'lang' => $lang ? (string)$lang : null,
156  'restrictions' => null
157  ] );
158  }
159 
167  public function addGoodLinkObjFromRow( LinkTarget $target, $row ) {
168  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
169  $this->goodLinks->set( $dbkey, [
170  'id' => intval( $row->page_id ),
171  'length' => intval( $row->page_len ),
172  'redirect' => intval( $row->page_is_redirect ),
173  'revision' => intval( $row->page_latest ),
174  'model' => !empty( $row->page_content_model )
175  ? strval( $row->page_content_model )
176  : null,
177  'lang' => !empty( $row->page_lang )
178  ? strval( $row->page_lang )
179  : null,
180  'restrictions' => !empty( $row->page_restrictions )
181  ? strval( $row->page_restrictions )
182  : null
183  ] );
184  }
185 
189  public function addBadLinkObj( LinkTarget $target ) {
190  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
191  if ( !$this->isBadLink( $dbkey ) ) {
192  $this->badLinks->set( $dbkey, 1 );
193  }
194  }
195 
199  public function clearBadLink( $title ) {
200  $this->badLinks->clear( $title );
201  }
202 
206  public function clearLink( LinkTarget $target ) {
207  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
208  $this->badLinks->clear( $dbkey );
209  $this->goodLinks->clear( $dbkey );
210  }
211 
218  public static function getSelectFields() {
220 
221  $fields = [
222  'page_id',
223  'page_len',
224  'page_is_redirect',
225  'page_latest',
226  'page_restrictions'
227  ];
228  if ( $wgContentHandlerUseDB ) {
229  $fields[] = 'page_content_model';
230  }
231  if ( $wgPageLanguageUseDB ) {
232  $fields[] = 'page_lang';
233  }
234 
235  return $fields;
236  }
237 
244  public function addLinkObj( LinkTarget $nt ) {
245  $key = $this->titleFormatter->getPrefixedDBkey( $nt );
246  if ( $this->isBadLink( $key ) || $nt->isExternal() || $nt->getNamespace() < 0 ) {
247  return 0;
248  }
249  $id = $this->getGoodLinkID( $key );
250  if ( $id != 0 ) {
251  return $id;
252  }
253 
254  if ( $key === '' ) {
255  return 0;
256  }
257 
258  // Cache template/file pages as they are less often viewed but heavily used
259  if ( $this->mForUpdate ) {
260  $row = $this->fetchPageRow( wfGetDB( DB_MASTER ), $nt );
261  } elseif ( $this->isCacheable( $nt ) ) {
262  // These pages are often transcluded heavily, so cache them
264  $row = $cache->getWithSetCallback(
265  $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
266  $cache::TTL_DAY,
267  function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
268  $dbr = wfGetDB( DB_REPLICA );
269  $setOpts += Database::getCacheSetOptions( $dbr );
270 
271  $row = $this->fetchPageRow( $dbr, $nt );
272  $mtime = $row ? wfTimestamp( TS_UNIX, $row->page_touched ) : false;
273  $ttl = $cache->adaptiveTTL( $mtime, $ttl );
274 
275  return $row;
276  }
277  );
278  } else {
279  $row = $this->fetchPageRow( wfGetDB( DB_REPLICA ), $nt );
280  }
281 
282  if ( $row ) {
283  $this->addGoodLinkObjFromRow( $nt, $row );
284  $id = intval( $row->page_id );
285  } else {
286  $this->addBadLinkObj( $nt );
287  $id = 0;
288  }
289 
290  return $id;
291  }
292 
300  if ( $this->isCacheable( $t ) ) {
301  return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
302  }
303 
304  return [];
305  }
306 
307  private function isCacheable( LinkTarget $title ) {
308  $ns = $title->getNamespace();
309  if ( in_array( $ns, [ NS_TEMPLATE, NS_FILE, NS_CATEGORY ] ) ) {
310  return true;
311  }
312  // Focus on transcluded pages more than the main content
313  if ( $this->nsInfo->isContent( $ns ) ) {
314  return false;
315  }
316  // Non-talk extension namespaces (e.g. NS_MODULE)
317  return ( $ns >= 100 && $this->nsInfo->isSubject( $ns ) );
318  }
319 
320  private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {
321  $fields = self::getSelectFields();
322  if ( $this->isCacheable( $nt ) ) {
323  $fields[] = 'page_touched';
324  }
325 
326  return $db->selectRow(
327  'page',
328  $fields,
329  [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
330  __METHOD__
331  );
332  }
333 
340  public function invalidateTitle( LinkTarget $title ) {
341  if ( $this->isCacheable( $title ) ) {
343  $cache->delete(
344  $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
345  );
346  }
347  }
348 
352  public function clear() {
353  $this->goodLinks->clear();
354  $this->badLinks->clear();
355  }
356 }
addLinkObj(LinkTarget $nt)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:244
isBadLink( $title)
Definition: LinkCache.php:129
addBadLinkObj(LinkTarget $target)
Definition: LinkCache.php:189
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
if(!isset( $args[0])) $lang
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
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
const NS_TEMPLATE
Definition: Defines.php:70
clearBadLink( $title)
Definition: LinkCache.php:199
const DB_MASTER
Definition: defines.php:26
getNamespace()
Get the namespace index.
clearLink(LinkTarget $target)
Definition: LinkCache.php:206
isExternal()
Whether this LinkTarget has an interwiki component.
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:145
$wgContentHandlerUseDB
Set to false to disable use of the database fields introduced by the ContentHandler facility...
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Single row SELECT wrapper.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
forUpdate( $update=null)
General accessor to get/set whether the master DB should be used.
Definition: LinkCache.php:93
TitleFormatter $titleFormatter
Definition: LinkCache.php:46
fetchPageRow(IDatabase $db, LinkTarget $nt)
Definition: LinkCache.php:320
static singleton()
Get an instance of this class.
Definition: LinkCache.php:79
getMutableCacheKeys(WANObjectCache $cache, LinkTarget $t)
Definition: LinkCache.php:299
invalidateTitle(LinkTarget $title)
Purge the link cache for a title.
Definition: LinkCache.php:340
clear()
Clears cache.
Definition: LinkCache.php:352
MapCacheLRU $badLinks
Definition: LinkCache.php:38
getGoodLinkFieldObj(LinkTarget $target, $field)
Get a field of a title object from cache.
Definition: LinkCache.php:116
getDBkey()
Get the main part with underscores.
$cache
Definition: mcc.php:33
const NS_CATEGORY
Definition: Defines.php:74
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
isCacheable(LinkTarget $title)
Definition: LinkCache.php:307
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
const NS_FILE
Definition: Defines.php:66
const MAX_SIZE
How many Titles to store.
Definition: LinkCache.php:55
__construct(TitleFormatter $titleFormatter, WANObjectCache $cache, NamespaceInfo $nsInfo=null)
Definition: LinkCache.php:57
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:218
bool $wgPageLanguageUseDB
Enable page language feature Allows setting page language in database.
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
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...
WANObjectCache $wanCache
Definition: LinkCache.php:40
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
getGoodLinkID( $title)
Definition: LinkCache.php:101
MapCacheLRU $goodLinks
Definition: LinkCache.php:36
const DB_REPLICA
Definition: defines.php:25
bool $mForUpdate
Definition: LinkCache.php:43
NamespaceInfo $nsInfo
Definition: LinkCache.php:49
makeKey( $class, $component=null)
addGoodLinkObjFromRow(LinkTarget $target, $row)
Same as above with better interface.
Definition: LinkCache.php:167