MediaWiki  1.23.0
LinkCache.php
Go to the documentation of this file.
1 <?php
29 class LinkCache {
30  // Increment $mClassVer whenever old serialized versions of this class
31  // becomes incompatible with the new version.
32  private $mClassVer = 4;
33 
34  private $mGoodLinks = array();
35  private $mGoodLinkFields = array();
36  private $mBadLinks = array();
37  private $mForUpdate = false;
38 
42  protected static $instance;
43 
49  static function &singleton() {
50  if ( self::$instance ) {
51  return self::$instance;
52  }
53  self::$instance = new LinkCache;
54 
55  return self::$instance;
56  }
57 
63  static function destroySingleton() {
64  self::$instance = null;
65  }
66 
74  static function setSingleton( LinkCache $instance ) {
75  self::$instance = $instance;
76  }
77 
84  public function forUpdate( $update = null ) {
85  return wfSetVar( $this->mForUpdate, $update );
86  }
87 
92  public function getGoodLinkID( $title ) {
93  if ( array_key_exists( $title, $this->mGoodLinks ) ) {
94  return $this->mGoodLinks[$title];
95  } else {
96  return 0;
97  }
98  }
99 
107  public function getGoodLinkFieldObj( $title, $field ) {
108  $dbkey = $title->getPrefixedDBkey();
109  if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
110  return $this->mGoodLinkFields[$dbkey][$field];
111  } else {
112  return null;
113  }
114  }
115 
120  public function isBadLink( $title ) {
121  return array_key_exists( $title, $this->mBadLinks );
122  }
123 
134  public function addGoodLinkObj( $id, $title, $len = -1, $redir = null,
135  $revision = 0, $model = 0
136  ) {
137  $dbkey = $title->getPrefixedDBkey();
138  $this->mGoodLinks[$dbkey] = (int)$id;
139  $this->mGoodLinkFields[$dbkey] = array(
140  'length' => (int)$len,
141  'redirect' => (int)$redir,
142  'revision' => (int)$revision,
143  'model' => (int)$model
144  );
145  }
146 
154  public function addGoodLinkObjFromRow( $title, $row ) {
155  $dbkey = $title->getPrefixedDBkey();
156  $this->mGoodLinks[$dbkey] = intval( $row->page_id );
157  $this->mGoodLinkFields[$dbkey] = array(
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  );
163  }
164 
168  public function addBadLinkObj( $title ) {
169  $dbkey = $title->getPrefixedDBkey();
170  if ( !$this->isBadLink( $dbkey ) ) {
171  $this->mBadLinks[$dbkey] = 1;
172  }
173  }
174 
175  public function clearBadLink( $title ) {
176  unset( $this->mBadLinks[$title] );
177  }
178 
182  public function clearLink( $title ) {
183  $dbkey = $title->getPrefixedDBkey();
184  unset( $this->mBadLinks[$dbkey] );
185  unset( $this->mGoodLinks[$dbkey] );
186  unset( $this->mGoodLinkFields[$dbkey] );
187  }
188 
189  public function getGoodLinks() {
190  return $this->mGoodLinks;
191  }
192 
193  public function getBadLinks() {
194  return array_keys( $this->mBadLinks );
195  }
196 
203  public function addLink( $title ) {
204  $nt = Title::newFromDBkey( $title );
205  if ( $nt ) {
206  return $this->addLinkObj( $nt );
207  } else {
208  return 0;
209  }
210  }
211 
218  public function addLinkObj( $nt ) {
219  global $wgAntiLockFlags, $wgContentHandlerUseDB;
220 
221  wfProfileIn( __METHOD__ );
222 
223  $key = $nt->getPrefixedDBkey();
224  if ( $this->isBadLink( $key ) || $nt->isExternal() ) {
225  wfProfileOut( __METHOD__ );
226 
227  return 0;
228  }
229  $id = $this->getGoodLinkID( $key );
230  if ( $id != 0 ) {
231  wfProfileOut( __METHOD__ );
232 
233  return $id;
234  }
235 
236  if ( $key === '' ) {
237  wfProfileOut( __METHOD__ );
238 
239  return 0;
240  }
241 
242  # Some fields heavily used for linking...
243  if ( $this->mForUpdate ) {
244  $db = wfGetDB( DB_MASTER );
245  if ( !( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) ) {
246  $options = array( 'FOR UPDATE' );
247  } else {
248  $options = array();
249  }
250  } else {
251  $db = wfGetDB( DB_SLAVE );
252  $options = array();
253  }
254 
255  $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
256  if ( $wgContentHandlerUseDB ) {
257  $f[] = 'page_content_model';
258  }
259 
260  $s = $db->selectRow( 'page', $f,
261  array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
262  __METHOD__, $options );
263  # Set fields...
264  if ( $s !== false ) {
265  $this->addGoodLinkObjFromRow( $nt, $s );
266  $id = intval( $s->page_id );
267  } else {
268  $this->addBadLinkObj( $nt );
269  $id = 0;
270  }
271 
272  wfProfileOut( __METHOD__ );
273 
274  return $id;
275  }
276 
280  public function clear() {
281  $this->mGoodLinks = array();
282  $this->mGoodLinkFields = array();
283  $this->mBadLinks = array();
284  }
285 }
LinkCache\addLink
addLink( $title)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:203
LinkCache
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition: LinkCache.php:29
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LinkCache\$mBadLinks
$mBadLinks
Definition: LinkCache.php:36
LinkCache\addGoodLinkObj
addGoodLinkObj( $id, $title, $len=-1, $redir=null, $revision=0, $model=0)
Add a link for the title to the link cache.
Definition: LinkCache.php:134
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:2139
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
ALF_NO_LINK_LOCK
const ALF_NO_LINK_LOCK
Definition: Defines.php:160
$f
$f
Definition: UtfNormalTest2.php:38
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
LinkCache\$instance
static $instance
Definition: LinkCache.php:42
LinkCache\addLinkObj
addLinkObj( $nt)
Add a title to the link cache, return the page_id or zero if non-existent.
Definition: LinkCache.php:218
LinkCache\clearLink
clearLink( $title)
Definition: LinkCache.php:182
$s
$s
Definition: mergeMessageFileList.php:156
LinkCache\forUpdate
forUpdate( $update=null)
General accessor to get/set whether SELECT FOR UPDATE should be used.
Definition: LinkCache.php:84
LinkCache\$mForUpdate
$mForUpdate
Definition: LinkCache.php:37
LinkCache\addBadLinkObj
addBadLinkObj( $title)
Definition: LinkCache.php:168
LinkCache\isBadLink
isBadLink( $title)
Definition: LinkCache.php:120
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
LinkCache\setSingleton
static setSingleton(LinkCache $instance)
Set the singleton instance to a given object.
Definition: LinkCache.php:74
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
LinkCache\addGoodLinkObjFromRow
addGoodLinkObjFromRow( $title, $row)
Same as above with better interface.
Definition: LinkCache.php:154
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
LinkCache\clearBadLink
clearBadLink( $title)
Definition: LinkCache.php:175
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
LinkCache\getGoodLinkFieldObj
getGoodLinkFieldObj( $title, $field)
Get a field of a title object from cache.
Definition: LinkCache.php:107
Title\newFromDBkey
static newFromDBkey( $key)
Create a new Title from a prefixed DB key.
Definition: Title.php:152
LinkCache\getBadLinks
getBadLinks()
Definition: LinkCache.php:193
LinkCache\$mClassVer
$mClassVer
Definition: LinkCache.php:32
LinkCache\getGoodLinkID
getGoodLinkID( $title)
Definition: LinkCache.php:92
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
LinkCache\$mGoodLinkFields
$mGoodLinkFields
Definition: LinkCache.php:35
LinkCache\destroySingleton
static destroySingleton()
Destroy the singleton instance, a new one will be created next time singleton() is called.
Definition: LinkCache.php:63
LinkCache\clear
clear()
Clears cache.
Definition: LinkCache.php:280
LinkCache\getGoodLinks
getGoodLinks()
Definition: LinkCache.php:189
LinkCache\singleton
static & singleton()
Get an instance of this class.
Definition: LinkCache.php:49
LinkCache\$mGoodLinks
$mGoodLinks
Definition: LinkCache.php:34