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 
52  const MAX_SIZE = 10000;
53 
55  $this->goodLinks = new MapCacheLRU( self::MAX_SIZE );
56  $this->badLinks = new MapCacheLRU( 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->goodLinks->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->goodLinks->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->badLinks->has( $title );
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->goodLinks->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->goodLinks->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->badLinks->set( $dbkey, 1 );
173  }
174  }
175 
179  public function clearBadLink( $title ) {
180  $this->badLinks->clear( $title );
181  }
182 
186  public function clearLink( LinkTarget $target ) {
187  $dbkey = $this->titleFormatter->getPrefixedDBkey( $target );
188  $this->badLinks->clear( $dbkey );
189  $this->goodLinks->clear( $dbkey );
190  }
191 
199  public function addLink( $title ) {
200  wfDeprecated( __METHOD__, '1.27' );
201  $nt = Title::newFromDBkey( $title );
202  if ( !$nt ) {
203  return 0;
204  }
205  return $this->addLinkObj( $nt );
206  }
207 
214  public static function getSelectFields() {
216 
217  $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
218  if ( $wgContentHandlerUseDB ) {
219  $fields[] = 'page_content_model';
220  }
221  if ( $wgPageLanguageUseDB ) {
222  $fields[] = 'page_lang';
223  }
224 
225  return $fields;
226  }
227 
234  public function addLinkObj( LinkTarget $nt ) {
235  $key = $this->titleFormatter->getPrefixedDBkey( $nt );
236  if ( $this->isBadLink( $key ) || $nt->isExternal()
237  || $nt->inNamespace( NS_SPECIAL )
238  ) {
239  return 0;
240  }
241  $id = $this->getGoodLinkID( $key );
242  if ( $id != 0 ) {
243  return $id;
244  }
245 
246  if ( $key === '' ) {
247  return 0;
248  }
249 
250  // Cache template/file pages as they are less often viewed but heavily used
251  if ( $this->mForUpdate ) {
252  $row = $this->fetchPageRow( wfGetDB( DB_MASTER ), $nt );
253  } elseif ( $this->isCacheable( $nt ) ) {
254  // These pages are often transcluded heavily, so cache them
256  $row = $cache->getWithSetCallback(
257  $cache->makeKey( 'page', $nt->getNamespace(), sha1( $nt->getDBkey() ) ),
258  $cache::TTL_DAY,
259  function ( $curValue, &$ttl, array &$setOpts ) use ( $cache, $nt ) {
260  $dbr = wfGetDB( DB_REPLICA );
261  $setOpts += Database::getCacheSetOptions( $dbr );
262 
263  $row = $this->fetchPageRow( $dbr, $nt );
264  $mtime = $row ? wfTimestamp( TS_UNIX, $row->page_touched ) : false;
265  $ttl = $cache->adaptiveTTL( $mtime, $ttl );
266 
267  return $row;
268  }
269  );
270  } else {
271  $row = $this->fetchPageRow( wfGetDB( DB_REPLICA ), $nt );
272  }
273 
274  if ( $row ) {
275  $this->addGoodLinkObjFromRow( $nt, $row );
276  $id = intval( $row->page_id );
277  } else {
278  $this->addBadLinkObj( $nt );
279  $id = 0;
280  }
281 
282  return $id;
283  }
284 
292  if ( $this->isCacheable( $t ) ) {
293  return [ $cache->makeKey( 'page', $t->getNamespace(), sha1( $t->getDBkey() ) ) ];
294  }
295 
296  return [];
297  }
298 
299  private function isCacheable( LinkTarget $title ) {
300  return ( $title->inNamespace( NS_TEMPLATE ) || $title->inNamespace( NS_FILE ) );
301  }
302 
303  private function fetchPageRow( IDatabase $db, LinkTarget $nt ) {
304  $fields = self::getSelectFields();
305  if ( $this->isCacheable( $nt ) ) {
306  $fields[] = 'page_touched';
307  }
308 
309  return $db->selectRow(
310  'page',
311  $fields,
312  [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
313  __METHOD__
314  );
315  }
316 
323  public function invalidateTitle( LinkTarget $title ) {
324  if ( $this->isCacheable( $title ) ) {
326  $cache->delete(
327  $cache->makeKey( 'page', $title->getNamespace(), sha1( $title->getDBkey() ) )
328  );
329  }
330  }
331 
335  public function clear() {
336  $this->goodLinks->clear();
337  $this->badLinks->clear();
338  }
339 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
addLinkObj(LinkTarget $nt)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:234
isBadLink( $title)
Definition: LinkCache.php:117
addBadLinkObj(LinkTarget $target)
Definition: LinkCache.php:169
addLink( $title)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:199
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.
inNamespace( $ns)
Convenience function to test if it is in the namespace.
const NS_SPECIAL
Definition: Defines.php:53
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:74
clearBadLink( $title)
Definition: LinkCache.php:179
const DB_MASTER
Definition: defines.php:26
getNamespace()
Get the namespace index.
clearLink(LinkTarget $target)
Definition: LinkCache.php:186
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:133
$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:81
TitleFormatter $titleFormatter
Definition: LinkCache.php:46
fetchPageRow(IDatabase $db, LinkTarget $nt)
Definition: LinkCache.php:303
static singleton()
Get an instance of this class.
Definition: LinkCache.php:67
getMutableCacheKeys(WANObjectCache $cache, LinkTarget $t)
Definition: LinkCache.php:291
invalidateTitle(LinkTarget $title)
Purge the link cache for a title.
Definition: LinkCache.php:323
clear()
Clears cache.
Definition: LinkCache.php:335
MapCacheLRU $badLinks
Definition: LinkCache.php:38
getGoodLinkFieldObj(LinkTarget $target, $field)
Get a field of a title object from cache.
Definition: LinkCache.php:104
getDBkey()
Get the main part with underscores.
$cache
Definition: mcc.php:33
isCacheable(LinkTarget $title)
Definition: LinkCache.php:299
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:936
const NS_FILE
Definition: Defines.php:70
const MAX_SIZE
How many Titles to store.
Definition: LinkCache.php:52
static getSelectFields()
Fields that LinkCache needs to select.
Definition: LinkCache.php:214
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...
static newFromDBkey( $key)
Create a new Title from a prefixed DB key.
Definition: Title.php:221
WANObjectCache $wanCache
Definition: LinkCache.php:40
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
__construct(TitleFormatter $titleFormatter, WANObjectCache $cache)
Definition: LinkCache.php:54
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
getGoodLinkID( $title)
Definition: LinkCache.php:89
MapCacheLRU $goodLinks
Definition: LinkCache.php:36
const DB_REPLICA
Definition: defines.php:25
bool $mForUpdate
Definition: LinkCache.php:43
makeKey( $class, $component=null)
addGoodLinkObjFromRow(LinkTarget $target, $row)
Same as above with better interface.
Definition: LinkCache.php:154