MediaWiki  1.28.0
LinkCache.php
Go to the documentation of this file.
1 <?php
25 
31 class LinkCache {
33  private $mGoodLinks;
35  private $mBadLinks;
37  private $wanCache;
38 
40  private $mForUpdate = false;
41 
43  private $titleFormatter;
44 
49  const MAX_SIZE = 10000;
50 
52  $this->mGoodLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
53  $this->mBadLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
54  $this->wanCache = $cache;
55  $this->titleFormatter = $titleFormatter;
56  }
57 
64  public static function singleton() {
65  return MediaWikiServices::getInstance()->getLinkCache();
66  }
67 
78  public function forUpdate( $update = null ) {
79  return wfSetVar( $this->mForUpdate, $update );
80  }
81 
86  public function getGoodLinkID( $title ) {
87  $info = $this->mGoodLinks->get( $title );
88  if ( !$info ) {
89  return 0;
90  }
91  return $info['id'];
92  }
93 
101  public function getGoodLinkFieldObj( LinkTarget $target, $field ) {
102  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
103  $info = $this->mGoodLinks->get( $dbkey );
104  if ( !$info ) {
105  return null;
106  }
107  return $info[$field];
108  }
109 
114  public function isBadLink( $title ) {
115  // Use get() to ensure it records as used for LRU.
116  return $this->mBadLinks->get( $title ) !== false;
117  }
118 
130  public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null,
131  $revision = 0, $model = null, $lang = null
132  ) {
133  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
134  $this->mGoodLinks->set( $dbkey, [
135  'id' => (int)$id,
136  'length' => (int)$len,
137  'redirect' => (int)$redir,
138  'revision' => (int)$revision,
139  'model' => $model ? (string)$model : null,
140  'lang' => $lang ? (string)$lang : null,
141  ] );
142  }
143 
151  public function addGoodLinkObjFromRow( LinkTarget $target, $row ) {
152  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
153  $this->mGoodLinks->set( $dbkey, [
154  'id' => intval( $row->page_id ),
155  'length' => intval( $row->page_len ),
156  'redirect' => intval( $row->page_is_redirect ),
157  'revision' => intval( $row->page_latest ),
158  'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
159  'lang' => !empty( $row->page_lang ) ? strval( $row->page_lang ) : null,
160  ] );
161  }
162 
166  public function addBadLinkObj( LinkTarget $target ) {
167  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
168  if ( !$this->isBadLink( $dbkey ) ) {
169  $this->mBadLinks->set( $dbkey, 1 );
170  }
171  }
172 
176  public function clearBadLink( $title ) {
177  $this->mBadLinks->delete( $title );
178  }
179 
183  public function clearLink( LinkTarget $target ) {
184  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
185  $this->mBadLinks->delete( $dbkey );
186  $this->mGoodLinks->delete( $dbkey );
187  }
188 
196  public function addLink( $title ) {
197  $nt = Title::newFromDBkey( $title );
198  if ( !$nt ) {
199  return 0;
200  }
201  return $this->addLinkObj( $nt );
202  }
203 
210  public static function getSelectFields() {
211  global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
212 
213  $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
214  if ( $wgContentHandlerUseDB ) {
215  $fields[] = 'page_content_model';
216  }
217  if ( $wgPageLanguageUseDB ) {
218  $fields[] = 'page_lang';
219  }
220 
221  return $fields;
222  }
223 
230  public function addLinkObj( LinkTarget $nt ) {
231  $key = $this->titleFormatter->getPrefixedDBkey( $nt );
232  if ( $this->isBadLink( $key ) || $nt->isExternal()
233  || $nt->inNamespace( NS_SPECIAL )
234  ) {
235  return 0;
236  }
237  $id = $this->getGoodLinkID( $key );
238  if ( $id != 0 ) {
239  return $id;
240  }
241 
242  if ( $key === '' ) {
243  return 0;
244  }
245 
246  // Cache template/file pages as they are less often viewed but heavily used
247  if ( $this->mForUpdate ) {
248  $row = $this->fetchPageRow( wfGetDB( DB_MASTER ), $nt );
249  } elseif ( $this->isCacheable( $nt ) ) {
250  // These pages are often transcluded heavily, so cache them
252  $row = $cache->getWithSetCallback(
253  $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
254  $cache::TTL_DAY,
255  function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
256  $dbr = wfGetDB( DB_REPLICA );
257  $setOpts += Database::getCacheSetOptions( $dbr );
258 
259  $row = $this->fetchPageRow( $dbr, $nt );
260  $mtime = $row ? wfTimestamp( TS_UNIX, $row->page_touched ) : false;
261  $ttl = $cache->adaptiveTTL( $mtime, $ttl );
262 
263  return $row;
264  }
265  );
266  } else {
267  $row = $this->fetchPageRow( wfGetDB( DB_REPLICA ), $nt );
268  }
269 
270  if ( $row ) {
271  $this->addGoodLinkObjFromRow( $nt, $row );
272  $id = intval( $row->page_id );
273  } else {
274  $this->addBadLinkObj( $nt );
275  $id = 0;
276  }
277 
278  return $id;
279  }
280 
281  private function isCacheable( LinkTarget $title ) {
282  return ( $title->inNamespace( NS_TEMPLATE ) || $title->inNamespace( NS_FILE ) );
283  }
284 
285  private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {
286  $fields = self::getSelectFields();
287  if ( $this->isCacheable( $nt ) ) {
288  $fields[] = 'page_touched';
289  }
290 
291  return $db->selectRow(
292  'page',
293  $fields,
294  [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
295  __METHOD__
296  );
297  }
298 
305  public function invalidateTitle( LinkTarget $title ) {
306  if ( $this->isCacheable( $title ) ) {
308  $cache->delete(
309  $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
310  );
311  }
312  }
313 
317  public function clear() {
318  $this->mGoodLinks->clear();
319  $this->mBadLinks->clear();
320  }
321 }
addLinkObj(LinkTarget $nt)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:230
static getMainWANInstance()
Get the main WAN cache object.
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
selectRow($table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Single row SELECT wrapper.
addBadLinkObj(LinkTarget $target)
Definition: LinkCache.php:166
the array() calling protocol came about after MediaWiki 1.4rc1.
HashBagOStuff $mGoodLinks
Definition: LinkCache.php:33
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static getCacheSetOptions(IDatabase $db1)
Merge the result of getSessionLagStatus() for several DBs using the most pessimistic values to estima...
Definition: Database.php:3025
if(!isset($args[0])) $lang
const NS_SPECIAL
Definition: Defines.php:45
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
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
forUpdate($update=null)
General accessor to get/set whether the master DB should be used.
Definition: LinkCache.php:78
const NS_TEMPLATE
Definition: Defines.php:66
const DB_MASTER
Definition: defines.php:23
getGoodLinkID($title)
Definition: LinkCache.php:86
getNamespace()
Get the namespace index.
clearLink(LinkTarget $target)
Definition: LinkCache.php:183
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
isExternal()
Whether this LinkTarget has an interwiki component.
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
TitleFormatter $titleFormatter
Definition: LinkCache.php:43
fetchPageRow(IDatabase $db, LinkTarget $nt)
Definition: LinkCache.php:285
static singleton()
Get an instance of this class.
Definition: LinkCache.php:64
invalidateTitle(LinkTarget $title)
Purge the link cache for a title.
Definition: LinkCache.php:305
clear()
Clears cache.
Definition: LinkCache.php:317
static newFromDBkey($key)
Create a new Title from a prefixed DB key.
Definition: Title.php:206
getGoodLinkFieldObj(LinkTarget $target, $field)
Get a field of a title object from cache.
Definition: LinkCache.php:101
getDBkey()
Get the main part with underscores.
$cache
Definition: mcc.php:33
isCacheable(LinkTarget $title)
Definition: LinkCache.php:281
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
clearBadLink($title)
Definition: LinkCache.php:176
const NS_FILE
Definition: Defines.php:62
const MAX_SIZE
How many Titles to store.
Definition: LinkCache.php:49
addLink($title)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:196
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:210
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:130
isBadLink($title)
Definition: LinkCache.php:114
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...
HashBagOStuff $mBadLinks
Definition: LinkCache.php:35
WANObjectCache $wanCache
Definition: LinkCache.php:37
__construct(TitleFormatter $titleFormatter, WANObjectCache $cache)
Definition: LinkCache.php:51
inNamespace($ns)
Convenience function to test if it is in the namespace.
const DB_REPLICA
Definition: defines.php:22
bool $mForUpdate
Definition: LinkCache.php:40
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:34
addGoodLinkObjFromRow(LinkTarget $target, $row)
Same as above with better interface.
Definition: LinkCache.php:151