MediaWiki  1.23.2
BacklinkCache.php
Go to the documentation of this file.
1 <?php
46  protected static $cache;
47 
59  protected $partitionCache = array();
60 
68  protected $fullResultCache = array();
69 
77  protected $db;
78 
82  protected $title;
83 
84  const CACHE_EXPIRY = 3600;
85 
91  public function __construct( Title $title ) {
92  $this->title = $title;
93  }
94 
103  public static function get( Title $title ) {
104  if ( !self::$cache ) { // init cache
105  self::$cache = new ProcessCacheLRU( 1 );
106  }
107  $dbKey = $title->getPrefixedDBkey();
108  if ( !self::$cache->has( $dbKey, 'obj', 3600 ) ) {
109  self::$cache->set( $dbKey, 'obj', new self( $title ) );
110  }
111 
112  return self::$cache->get( $dbKey, 'obj' );
113  }
114 
122  function __sleep() {
123  return array( 'partitionCache', 'fullResultCache', 'title' );
124  }
125 
129  public function clear() {
130  $this->partitionCache = array();
131  $this->fullResultCache = array();
132  unset( $this->db );
133  }
134 
140  public function setDB( $db ) {
141  $this->db = $db;
142  }
143 
149  protected function getDB() {
150  if ( !isset( $this->db ) ) {
151  $this->db = wfGetDB( DB_SLAVE );
152  }
153 
154  return $this->db;
155  }
156 
165  public function getLinks( $table, $startId = false, $endId = false, $max = INF ) {
166  return TitleArray::newFromResult( $this->queryLinks( $table, $startId, $endId, $max ) );
167  }
168 
178  protected function queryLinks( $table, $startId, $endId, $max, $select = 'all' ) {
179  wfProfileIn( __METHOD__ );
180 
181  $fromField = $this->getPrefix( $table ) . '_from';
182 
183  if ( !$startId && !$endId && is_infinite( $max )
184  && isset( $this->fullResultCache[$table] )
185  ) {
186  wfDebug( __METHOD__ . ": got results from cache\n" );
187  $res = $this->fullResultCache[$table];
188  } else {
189  wfDebug( __METHOD__ . ": got results from DB\n" );
190  $conds = $this->getConditions( $table );
191  // Use the from field in the condition rather than the joined page_id,
192  // because databases are stupid and don't necessarily propagate indexes.
193  if ( $startId ) {
194  $conds[] = "$fromField >= " . intval( $startId );
195  }
196  if ( $endId ) {
197  $conds[] = "$fromField <= " . intval( $endId );
198  }
199  $options = array( 'ORDER BY' => $fromField );
200  if ( is_finite( $max ) && $max > 0 ) {
201  $options['LIMIT'] = $max;
202  }
203 
204  if ( $select === 'ids' ) {
205  // Just select from the backlink table and ignore the page JOIN
206  $res = $this->getDB()->select(
207  $table,
208  array( $this->getPrefix( $table ) . '_from AS page_id' ),
209  array_filter( $conds, function ( $clause ) { // kind of janky
210  return !preg_match( '/(\b|=)page_id(\b|=)/', $clause );
211  } ),
212  __METHOD__,
213  $options
214  );
215  } else {
216  // Select from the backlink table and JOIN with page title information
217  $res = $this->getDB()->select(
218  array( $table, 'page' ),
219  array( 'page_namespace', 'page_title', 'page_id' ),
220  $conds,
221  __METHOD__,
222  array_merge( array( 'STRAIGHT_JOIN' ), $options )
223  );
224  }
225 
226  if ( $select === 'all' && !$startId && !$endId && $res->numRows() < $max ) {
227  // The full results fit within the limit, so cache them
228  $this->fullResultCache[$table] = $res;
229  } else {
230  wfDebug( __METHOD__ . ": results from DB were uncacheable\n" );
231  }
232  }
233 
234  wfProfileOut( __METHOD__ );
235 
236  return $res;
237  }
238 
245  protected function getPrefix( $table ) {
246  static $prefixes = array(
247  'pagelinks' => 'pl',
248  'imagelinks' => 'il',
249  'categorylinks' => 'cl',
250  'templatelinks' => 'tl',
251  'redirect' => 'rd',
252  );
253 
254  if ( isset( $prefixes[$table] ) ) {
255  return $prefixes[$table];
256  } else {
257  $prefix = null;
258  wfRunHooks( 'BacklinkCacheGetPrefix', array( $table, &$prefix ) );
259  if ( $prefix ) {
260  return $prefix;
261  } else {
262  throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
263  }
264  }
265  }
266 
274  protected function getConditions( $table ) {
275  $prefix = $this->getPrefix( $table );
276 
277  switch ( $table ) {
278  case 'pagelinks':
279  case 'templatelinks':
280  $conds = array(
281  "{$prefix}_namespace" => $this->title->getNamespace(),
282  "{$prefix}_title" => $this->title->getDBkey(),
283  "page_id={$prefix}_from"
284  );
285  break;
286  case 'redirect':
287  $conds = array(
288  "{$prefix}_namespace" => $this->title->getNamespace(),
289  "{$prefix}_title" => $this->title->getDBkey(),
290  $this->getDb()->makeList( array(
291  "{$prefix}_interwiki" => '',
292  "{$prefix}_interwiki IS NULL",
293  ), LIST_OR ),
294  "page_id={$prefix}_from"
295  );
296  break;
297  case 'imagelinks':
298  case 'categorylinks':
299  $conds = array(
300  "{$prefix}_to" => $this->title->getDBkey(),
301  "page_id={$prefix}_from"
302  );
303  break;
304  default:
305  $conds = null;
306  wfRunHooks( 'BacklinkCacheGetConditions', array( $table, $this->title, &$conds ) );
307  if ( !$conds ) {
308  throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
309  }
310  }
311 
312  return $conds;
313  }
314 
320  public function hasLinks( $table ) {
321  return ( $this->getNumLinks( $table, 1 ) > 0 );
322  }
323 
330  public function getNumLinks( $table, $max = INF ) {
331  global $wgMemc, $wgUpdateRowsPerJob;
332 
333  // 1) try partition cache ...
334  if ( isset( $this->partitionCache[$table] ) ) {
335  $entry = reset( $this->partitionCache[$table] );
336 
337  return min( $max, $entry['numRows'] );
338  }
339 
340  // 2) ... then try full result cache ...
341  if ( isset( $this->fullResultCache[$table] ) ) {
342  return min( $max, $this->fullResultCache[$table]->numRows() );
343  }
344 
345  $memcKey = wfMemcKey( 'numbacklinks', md5( $this->title->getPrefixedDBkey() ), $table );
346 
347  // 3) ... fallback to memcached ...
348  $count = $wgMemc->get( $memcKey );
349  if ( $count ) {
350  return min( $max, $count );
351  }
352 
353  // 4) fetch from the database ...
354  if ( is_infinite( $max ) ) { // no limit at all
355  // Use partition() since it will batch the query and skip the JOIN.
356  // Use $wgUpdateRowsPerJob just to encourage cache reuse for jobs.
357  $this->partition( $table, $wgUpdateRowsPerJob ); // updates $this->partitionCache
358  return $this->partitionCache[$table][$wgUpdateRowsPerJob]['numRows'];
359  } else { // probably some sane limit
360  // Fetch the full title info, since the caller will likely need it next
361  $count = $this->getLinks( $table, false, false, $max )->count();
362  if ( $count < $max ) { // full count
363  $wgMemc->set( $memcKey, $count, self::CACHE_EXPIRY );
364  }
365  }
366 
367  return min( $max, $count );
368  }
369 
379  public function partition( $table, $batchSize ) {
380  global $wgMemc;
381 
382  // 1) try partition cache ...
383  if ( isset( $this->partitionCache[$table][$batchSize] ) ) {
384  wfDebug( __METHOD__ . ": got from partition cache\n" );
385 
386  return $this->partitionCache[$table][$batchSize]['batches'];
387  }
388 
389  $this->partitionCache[$table][$batchSize] = false;
390  $cacheEntry =& $this->partitionCache[$table][$batchSize];
391 
392  // 2) ... then try full result cache ...
393  if ( isset( $this->fullResultCache[$table] ) ) {
394  $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize );
395  wfDebug( __METHOD__ . ": got from full result cache\n" );
396 
397  return $cacheEntry['batches'];
398  }
399 
400  $memcKey = wfMemcKey(
401  'backlinks',
402  md5( $this->title->getPrefixedDBkey() ),
403  $table,
404  $batchSize
405  );
406 
407  // 3) ... fallback to memcached ...
408  $memcValue = $wgMemc->get( $memcKey );
409  if ( is_array( $memcValue ) ) {
410  $cacheEntry = $memcValue;
411  wfDebug( __METHOD__ . ": got from memcached $memcKey\n" );
412 
413  return $cacheEntry['batches'];
414  }
415 
416  // 4) ... finally fetch from the slow database :(
417  $cacheEntry = array( 'numRows' => 0, 'batches' => array() ); // final result
418  // Do the selects in batches to avoid client-side OOMs (bug 43452).
419  // Use a LIMIT that plays well with $batchSize to keep equal sized partitions.
420  $selectSize = max( $batchSize, 200000 - ( 200000 % $batchSize ) );
421  $start = false;
422  do {
423  $res = $this->queryLinks( $table, $start, false, $selectSize, 'ids' );
424  $partitions = $this->partitionResult( $res, $batchSize, false );
425  // Merge the link count and range partitions for this chunk
426  $cacheEntry['numRows'] += $partitions['numRows'];
427  $cacheEntry['batches'] = array_merge( $cacheEntry['batches'], $partitions['batches'] );
428  if ( count( $partitions['batches'] ) ) {
429  list( , $lEnd ) = end( $partitions['batches'] );
430  $start = $lEnd + 1; // pick up after this inclusive range
431  }
432  } while ( $partitions['numRows'] >= $selectSize );
433  // Make sure the first range has start=false and the last one has end=false
434  if ( count( $cacheEntry['batches'] ) ) {
435  $cacheEntry['batches'][0][0] = false;
436  $cacheEntry['batches'][count( $cacheEntry['batches'] ) - 1][1] = false;
437  }
438 
439  // Save partitions to memcached
440  $wgMemc->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
441 
442  // Save backlink count to memcached
443  $memcKey = wfMemcKey( 'numbacklinks', md5( $this->title->getPrefixedDBkey() ), $table );
444  $wgMemc->set( $memcKey, $cacheEntry['numRows'], self::CACHE_EXPIRY );
445 
446  wfDebug( __METHOD__ . ": got from database\n" );
447 
448  return $cacheEntry['batches'];
449  }
450 
459  protected function partitionResult( $res, $batchSize, $isComplete = true ) {
460  $batches = array();
461  $numRows = $res->numRows();
462  $numBatches = ceil( $numRows / $batchSize );
463 
464  for ( $i = 0; $i < $numBatches; $i++ ) {
465  if ( $i == 0 && $isComplete ) {
466  $start = false;
467  } else {
468  $rowNum = $i * $batchSize;
469  $res->seek( $rowNum );
470  $row = $res->fetchObject();
471  $start = (int)$row->page_id;
472  }
473 
474  if ( $i == ( $numBatches - 1 ) && $isComplete ) {
475  $end = false;
476  } else {
477  $rowNum = min( $numRows - 1, ( $i + 1 ) * $batchSize - 1 );
478  $res->seek( $rowNum );
479  $row = $res->fetchObject();
480  $end = (int)$row->page_id;
481  }
482 
483  # Sanity check order
484  if ( $start && $end && $start > $end ) {
485  throw new MWException( __METHOD__ . ': Internal error: query result out of order' );
486  }
487 
488  $batches[] = array( $start, $end );
489  }
490 
491  return array( 'numRows' => $numRows, 'batches' => $batches );
492  }
493 }
BacklinkCache\getPrefix
getPrefix( $table)
Get the field name prefix for a given table.
Definition: BacklinkCache.php:245
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
TitleArray\newFromResult
static newFromResult( $res)
Definition: TitleArray.php:38
$wgMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $wgMemc
Definition: globals.txt:25
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
BacklinkCache\getDB
getDB()
Get the slave connection to the database When non existing, will initialize the connection.
Definition: BacklinkCache.php:149
BacklinkCache
Class for fetching backlink lists, approximate backlink counts and partitions.
Definition: BacklinkCache.php:44
BacklinkCache\$title
$title
Local copy of a Title object.
Definition: BacklinkCache.php:82
BacklinkCache\getNumLinks
getNumLinks( $table, $max=INF)
Get the approximate number of backlinks.
Definition: BacklinkCache.php:330
title
to move a page</td >< td > &*You are moving the page across *A non empty talk page already exists under the new or *You uncheck the box below In those you will have to move or merge the page manually if desired</td >< td > be sure to &You are responsible for making sure that links continue to point where they are supposed to go Note that the page will &a page at the new title
Definition: All_system_messages.txt:2703
LIST_OR
const LIST_OR
Definition: Defines.php:206
MWException
MediaWiki exception.
Definition: MWException.php:26
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3571
BacklinkCache\partition
partition( $table, $batchSize)
Partition the backlinks into batches.
Definition: BacklinkCache.php:379
BacklinkCache\$cache
static $cache
Definition: BacklinkCache.php:46
BacklinkCache\queryLinks
queryLinks( $table, $startId, $endId, $max, $select='all')
Get the backlinks for a given table.
Definition: BacklinkCache.php:178
BacklinkCache\__sleep
__sleep()
Serialization handler, diasallows to serialize the database to prevent failures after this class is d...
Definition: BacklinkCache.php:122
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
BacklinkCache\partitionResult
partitionResult( $res, $batchSize, $isComplete=true)
Partition a DB result with backlinks in it into batches.
Definition: BacklinkCache.php:459
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4001
BacklinkCache\$partitionCache
$partitionCache
Multi dimensions array representing batches.
Definition: BacklinkCache.php:59
BacklinkCache\$fullResultCache
$fullResultCache
Contains the whole links from a database result.
Definition: BacklinkCache.php:68
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
BacklinkCache\clear
clear()
Clear locally stored data and database object.
Definition: BacklinkCache.php:129
BacklinkCache\CACHE_EXPIRY
const CACHE_EXPIRY
Definition: BacklinkCache.php:84
$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
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:933
BacklinkCache\$db
$db
Local copy of a database object.
Definition: BacklinkCache.php:77
BacklinkCache\__construct
__construct(Title $title)
Create a new BacklinkCache.
Definition: BacklinkCache.php:91
BacklinkCache\hasLinks
hasLinks( $table)
Check if there are any backlinks.
Definition: BacklinkCache.php:320
$count
$count
Definition: UtfNormalTest2.php:96
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:35
$cache
$cache
Definition: mcc.php:32
BacklinkCache\setDB
setDB( $db)
Set the Database object to use.
Definition: BacklinkCache.php:140
BacklinkCache\getConditions
getConditions( $table)
Get the SQL condition array for selecting backlinks, with a join on the page table.
Definition: BacklinkCache.php:274
ProcessCacheLRU
Handles per process caching of items.
Definition: ProcessCacheLRU.php:28
$res
$res
Definition: database.txt:21
BacklinkCache\getLinks
getLinks( $table, $startId=false, $endId=false, $max=INF)
Get the backlinks for a given table.
Definition: BacklinkCache.php:165