MediaWiki  1.27.2
LinkCache.php
Go to the documentation of this file.
1 <?php
29 class LinkCache {
33  private $mGoodLinks;
37  private $mBadLinks;
38  private $mForUpdate = false;
39 
44  const MAX_SIZE = 10000;
45 
49  protected static $instance;
50 
51  public function __construct() {
52  $this->mGoodLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
53  $this->mBadLinks = new HashBagOStuff( [ 'maxKeys' => self::MAX_SIZE ] );
54  }
55 
61  public static function singleton() {
62  if ( !self::$instance ) {
63  self::$instance = new LinkCache;
64  }
65 
66  return self::$instance;
67  }
68 
76  public static function destroySingleton() {
77  self::$instance = null;
78  }
79 
89  public static function setSingleton( LinkCache $instance ) {
90  self::$instance = $instance;
91  }
92 
103  public function forUpdate( $update = null ) {
104  return wfSetVar( $this->mForUpdate, $update );
105  }
106 
111  public function getGoodLinkID( $title ) {
112  $info = $this->mGoodLinks->get( $title );
113  if ( !$info ) {
114  return 0;
115  }
116  return $info['id'];
117  }
118 
126  public function getGoodLinkFieldObj( Title $title, $field ) {
127  $dbkey = $title->getPrefixedDBkey();
128  $info = $this->mGoodLinks->get( $dbkey );
129  if ( !$info ) {
130  return null;
131  }
132  return $info[$field];
133  }
134 
139  public function isBadLink( $title ) {
140  // Use get() to ensure it records as used for LRU.
141  return $this->mBadLinks->get( $title ) !== false;
142  }
143 
155  public function addGoodLinkObj( $id, Title $title, $len = -1, $redir = null,
156  $revision = 0, $model = null, $lang = null
157  ) {
158  $dbkey = $title->getPrefixedDBkey();
159  $this->mGoodLinks->set( $dbkey, [
160  'id' => (int)$id,
161  'length' => (int)$len,
162  'redirect' => (int)$redir,
163  'revision' => (int)$revision,
164  'model' => $model ? (string)$model : null,
165  'lang' => $lang ? (string)$lang : null,
166  ] );
167  }
168 
176  public function addGoodLinkObjFromRow( Title $title, $row ) {
177  $dbkey = $title->getPrefixedDBkey();
178  $this->mGoodLinks->set( $dbkey, [
179  'id' => intval( $row->page_id ),
180  'length' => intval( $row->page_len ),
181  'redirect' => intval( $row->page_is_redirect ),
182  'revision' => intval( $row->page_latest ),
183  'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
184  'lang' => !empty( $row->page_lang ) ? strval( $row->page_lang ) : null,
185  ] );
186  }
187 
191  public function addBadLinkObj( Title $title ) {
192  $dbkey = $title->getPrefixedDBkey();
193  if ( !$this->isBadLink( $dbkey ) ) {
194  $this->mBadLinks->set( $dbkey, 1 );
195  }
196  }
197 
201  public function clearBadLink( $title ) {
202  $this->mBadLinks->delete( $title );
203  }
204 
208  public function clearLink( Title $title ) {
209  $dbkey = $title->getPrefixedDBkey();
210  $this->mBadLinks->delete( $dbkey );
211  $this->mGoodLinks->delete( $dbkey );
212  }
213 
220  public function addLink( $title ) {
221  $nt = Title::newFromDBkey( $title );
222  if ( !$nt ) {
223  return 0;
224  }
225  return $this->addLinkObj( $nt );
226  }
227 
234  public function addLinkObj( Title $nt ) {
235  global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
236 
237  $key = $nt->getPrefixedDBkey();
238  if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
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  // Some fields heavily used for linking...
251  $db = $this->mForUpdate ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
252 
253  $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
254  if ( $wgContentHandlerUseDB ) {
255  $fields[] = 'page_content_model';
256  }
257  if ( $wgPageLanguageUseDB ) {
258  $fields[] = 'page_lang';
259  }
260 
261  $row = $db->selectRow( 'page', $fields,
262  [ 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ],
263  __METHOD__
264  );
265 
266  if ( $row !== false ) {
267  $this->addGoodLinkObjFromRow( $nt, $row );
268  $id = intval( $row->page_id );
269  } else {
270  $this->addBadLinkObj( $nt );
271  $id = 0;
272  }
273 
274  return $id;
275  }
276 
280  public function clear() {
281  $this->mGoodLinks->clear();
282  $this->mBadLinks->clear();
283  }
284 }
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
getGoodLinkFieldObj(Title $title, $field)
Get a field of a title object from cache.
Definition: LinkCache.php:126
HashBagOStuff $mGoodLinks
Definition: LinkCache.php:33
if(!isset($args[0])) $lang
addGoodLinkObjFromRow(Title $title, $row)
Same as above with better interface.
Definition: LinkCache.php:176
Represents a title within MediaWiki.
Definition: Title.php:34
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:103
getGoodLinkID($title)
Definition: LinkCache.php:111
isExternal()
Is this Title interwiki?
Definition: Title.php:810
static LinkCache $instance
Definition: LinkCache.php:49
static singleton()
Get an instance of this class.
Definition: LinkCache.php:61
getDBkey()
Get the main part with underscores.
Definition: Title.php:911
clear()
Clears cache.
Definition: LinkCache.php:280
static newFromDBkey($key)
Create a new Title from a prefixed DB key.
Definition: Title.php:221
const DB_SLAVE
Definition: Defines.php:46
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
static setSingleton(LinkCache $instance)
Set the singleton instance to a given object.
Definition: LinkCache.php:89
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:934
clearBadLink($title)
Definition: LinkCache.php:201
const MAX_SIZE
How many Titles to store.
Definition: LinkCache.php:44
addLink($title)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:220
addLinkObj(Title $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:139
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:37
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:29
__construct()
Definition: LinkCache.php:51
static destroySingleton()
Destroy the singleton instance.
Definition: LinkCache.php:76
addGoodLinkObj($id, Title $title, $len=-1, $redir=null, $revision=0, $model=null, $lang=null)
Add a link for the title to the link cache.
Definition: LinkCache.php:155
const DB_MASTER
Definition: Defines.php:47
addBadLinkObj(Title $title)
Definition: LinkCache.php:191
clearLink(Title $title)
Definition: LinkCache.php:208
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1437