2006-06-18 12:42:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to invalidate the HTML cache of all the pages linking to a given title.
|
|
|
|
|
* Small numbers of links will be done immediately, large numbers are pushed onto
|
|
|
|
|
* the job queue.
|
|
|
|
|
*
|
2008-04-14 07:45:50 +00:00
|
|
|
* This class is designed to work efficiently with small numbers of links, and
|
2006-06-18 12:42:16 +00:00
|
|
|
* to work reasonably well with up to ~10^5 links. Above ~10^6 links, the memory
|
|
|
|
|
* and time requirements of loading all backlinked IDs in doUpdate() might become
|
|
|
|
|
* prohibitive. The requirements measured at Wikimedia are approximately:
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
2006-06-18 12:42:16 +00:00
|
|
|
* memory: 48 bytes per row
|
|
|
|
|
* time: 16us per row for the query plus processing
|
|
|
|
|
*
|
|
|
|
|
* The reason this query is done is to support partitioning of the job
|
2008-04-14 07:45:50 +00:00
|
|
|
* by backlinked ID. The memory issue could be allieviated by doing this query in
|
2006-06-18 12:42:16 +00:00
|
|
|
* batches, but of course LIMIT with an offset is inefficient on the DB side.
|
|
|
|
|
*
|
2008-04-14 07:45:50 +00:00
|
|
|
* The class is nevertheless a vast improvement on the previous method of using
|
2011-03-15 16:58:06 +00:00
|
|
|
* File::getLinksTo() and Title::touchArray(), which uses about 2KB of memory per
|
2006-06-18 12:42:16 +00:00
|
|
|
* link.
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Cache
|
2006-06-18 12:42:16 +00:00
|
|
|
*/
|
2011-09-10 06:50:30 +00:00
|
|
|
class HTMLCacheUpdate implements DeferrableUpdate {
|
2011-03-07 14:45:11 +00:00
|
|
|
/**
|
|
|
|
|
* @var Title
|
|
|
|
|
*/
|
|
|
|
|
public $mTitle;
|
|
|
|
|
|
|
|
|
|
public $mTable, $mPrefix, $mStart, $mEnd;
|
2006-06-18 12:42:16 +00:00
|
|
|
public $mRowsPerJob, $mRowsPerQuery;
|
|
|
|
|
|
2011-10-28 18:11:47 +00:00
|
|
|
/**
|
|
|
|
|
* @param $titleTo
|
|
|
|
|
* @param $table
|
|
|
|
|
* @param $start bool
|
|
|
|
|
* @param $end bool
|
|
|
|
|
*/
|
2009-08-12 05:00:30 +00:00
|
|
|
function __construct( $titleTo, $table, $start = false, $end = false ) {
|
2006-06-18 12:42:16 +00:00
|
|
|
global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
|
|
|
|
|
|
|
|
|
|
$this->mTitle = $titleTo;
|
|
|
|
|
$this->mTable = $table;
|
2009-08-12 05:00:30 +00:00
|
|
|
$this->mStart = $start;
|
|
|
|
|
$this->mEnd = $end;
|
2006-06-18 12:42:16 +00:00
|
|
|
$this->mRowsPerJob = $wgUpdateRowsPerJob;
|
|
|
|
|
$this->mRowsPerQuery = $wgUpdateRowsPerQuery;
|
2009-02-16 14:26:34 +00:00
|
|
|
$this->mCache = $this->mTitle->getBacklinkCache();
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-22 22:36:02 +00:00
|
|
|
public function doUpdate() {
|
2009-08-12 05:00:30 +00:00
|
|
|
if ( $this->mStart || $this->mEnd ) {
|
|
|
|
|
$this->doPartialUpdate();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-07-06 10:12:41 +00:00
|
|
|
|
2009-08-12 05:00:30 +00:00
|
|
|
# Get an estimate of the number of rows from the BacklinkCache
|
|
|
|
|
$numRows = $this->mCache->getNumLinks( $this->mTable );
|
|
|
|
|
if ( $numRows > $this->mRowsPerJob * 2 ) {
|
|
|
|
|
# Do fast cached partition
|
|
|
|
|
$this->insertJobs();
|
|
|
|
|
} else {
|
|
|
|
|
# Get the links from the DB
|
|
|
|
|
$titleArray = $this->mCache->getLinks( $this->mTable );
|
|
|
|
|
# Check if the row count estimate was correct
|
|
|
|
|
if ( $titleArray->count() > $this->mRowsPerJob * 2 ) {
|
|
|
|
|
# Not correct, do accurate partition
|
|
|
|
|
wfDebug( __METHOD__.": row count estimate was incorrect, repartitioning\n" );
|
|
|
|
|
$this->insertJobsFromTitles( $titleArray );
|
2006-06-18 12:42:16 +00:00
|
|
|
} else {
|
2009-08-12 05:00:30 +00:00
|
|
|
$this->invalidateTitles( $titleArray );
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-12 05:00:30 +00:00
|
|
|
/**
|
|
|
|
|
* Update some of the backlinks, defined by a page ID range
|
|
|
|
|
*/
|
|
|
|
|
protected function doPartialUpdate() {
|
|
|
|
|
$titleArray = $this->mCache->getLinks( $this->mTable, $this->mStart, $this->mEnd );
|
|
|
|
|
if ( $titleArray->count() <= $this->mRowsPerJob * 2 ) {
|
|
|
|
|
# This partition is small enough, do the update
|
|
|
|
|
$this->invalidateTitles( $titleArray );
|
|
|
|
|
} else {
|
|
|
|
|
# Partitioning was excessively inaccurate. Divide the job further.
|
2010-12-19 04:31:15 +00:00
|
|
|
# This can occur when a large number of links are added in a short
|
2009-08-12 05:00:30 +00:00
|
|
|
# period of time, say by updating a heavily-used template.
|
|
|
|
|
$this->insertJobsFromTitles( $titleArray );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Partition the current range given by $this->mStart and $this->mEnd,
|
|
|
|
|
* using a pre-calculated title array which gives the links in that range.
|
|
|
|
|
* Queue the resulting jobs.
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
|
|
|
|
* @param $titleArray array
|
2009-08-12 05:00:30 +00:00
|
|
|
*/
|
|
|
|
|
protected function insertJobsFromTitles( $titleArray ) {
|
|
|
|
|
# We make subpartitions in the sense that the start of the first job
|
|
|
|
|
# will be the start of the parent partition, and the end of the last
|
|
|
|
|
# job will be the end of the parent partition.
|
|
|
|
|
$jobs = array();
|
|
|
|
|
$start = $this->mStart; # start of the current job
|
|
|
|
|
$numTitles = 0;
|
|
|
|
|
foreach ( $titleArray as $title ) {
|
|
|
|
|
$id = $title->getArticleID();
|
2010-12-19 04:31:15 +00:00
|
|
|
# $numTitles is now the number of titles in the current job not
|
2009-08-12 05:00:30 +00:00
|
|
|
# including the current ID
|
|
|
|
|
if ( $numTitles >= $this->mRowsPerJob ) {
|
|
|
|
|
# Add a job up to but not including the current ID
|
|
|
|
|
$params = array(
|
|
|
|
|
'table' => $this->mTable,
|
|
|
|
|
'start' => $start,
|
|
|
|
|
'end' => $id - 1
|
|
|
|
|
);
|
|
|
|
|
$jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
|
|
|
|
|
$start = $id;
|
|
|
|
|
$numTitles = 0;
|
|
|
|
|
}
|
|
|
|
|
$numTitles++;
|
|
|
|
|
}
|
|
|
|
|
# Last job
|
|
|
|
|
$params = array(
|
|
|
|
|
'table' => $this->mTable,
|
|
|
|
|
'start' => $start,
|
|
|
|
|
'end' => $this->mEnd
|
|
|
|
|
);
|
|
|
|
|
$jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
|
|
|
|
|
wfDebug( __METHOD__.": repartitioning into " . count( $jobs ) . " jobs\n" );
|
|
|
|
|
|
|
|
|
|
if ( count( $jobs ) < 2 ) {
|
|
|
|
|
# I don't think this is possible at present, but handling this case
|
2010-12-19 04:31:15 +00:00
|
|
|
# makes the code a bit more robust against future code updates and
|
2009-08-12 05:00:30 +00:00
|
|
|
# avoids a potential infinite loop of repartitioning
|
|
|
|
|
wfDebug( __METHOD__.": repartitioning failed!\n" );
|
|
|
|
|
$this->invalidateTitles( $titleArray );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Job::batchInsert( $jobs );
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-28 18:11:47 +00:00
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2009-02-16 14:26:34 +00:00
|
|
|
protected function insertJobs() {
|
|
|
|
|
$batches = $this->mCache->partition( $this->mTable, $this->mRowsPerJob );
|
|
|
|
|
if ( !$batches ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-08-12 05:00:30 +00:00
|
|
|
$jobs = array();
|
2009-02-16 14:26:34 +00:00
|
|
|
foreach ( $batches as $batch ) {
|
2007-06-21 19:11:24 +00:00
|
|
|
$params = array(
|
|
|
|
|
'table' => $this->mTable,
|
2009-02-16 14:26:34 +00:00
|
|
|
'start' => $batch[0],
|
|
|
|
|
'end' => $batch[1],
|
2007-06-21 19:11:24 +00:00
|
|
|
);
|
|
|
|
|
$jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
2009-02-16 14:26:34 +00:00
|
|
|
Job::batchInsert( $jobs );
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-12 05:00:30 +00:00
|
|
|
/**
|
|
|
|
|
* Invalidate an array (or iterator) of Title objects, right now
|
2011-10-28 18:11:47 +00:00
|
|
|
* @param $titleArray array
|
2009-08-12 05:00:30 +00:00
|
|
|
*/
|
|
|
|
|
protected function invalidateTitles( $titleArray ) {
|
|
|
|
|
global $wgUseFileCache, $wgUseSquid;
|
2006-06-18 12:42:16 +00:00
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2006-06-18 12:42:16 +00:00
|
|
|
$timestamp = $dbw->timestamp();
|
|
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
# Get all IDs in this query into an array
|
|
|
|
|
$ids = array();
|
|
|
|
|
foreach ( $titleArray as $title ) {
|
|
|
|
|
$ids[] = $title->getArticleID();
|
|
|
|
|
}
|
2009-08-12 05:00:30 +00:00
|
|
|
|
|
|
|
|
if ( !$ids ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
# Update page_touched
|
2009-08-12 05:00:30 +00:00
|
|
|
$batches = array_chunk( $ids, $this->mRowsPerQuery );
|
|
|
|
|
foreach ( $batches as $batch ) {
|
|
|
|
|
$dbw->update( 'page',
|
|
|
|
|
array( 'page_touched' => $timestamp ),
|
2011-09-19 18:46:57 +00:00
|
|
|
array( 'page_id' => $batch ),
|
2009-08-12 05:00:30 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
# Update squid
|
|
|
|
|
if ( $wgUseSquid ) {
|
|
|
|
|
$u = SquidUpdate::newFromTitles( $titleArray );
|
|
|
|
|
$u->doUpdate();
|
|
|
|
|
}
|
2006-06-18 12:42:16 +00:00
|
|
|
|
2009-02-16 14:26:34 +00:00
|
|
|
# Update file cache
|
|
|
|
|
if ( $wgUseFileCache ) {
|
|
|
|
|
foreach ( $titleArray as $title ) {
|
|
|
|
|
HTMLFileCache::clearFileCache( $title );
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-24 13:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
/**
|
2008-09-13 06:21:18 +00:00
|
|
|
* Job wrapper for HTMLCacheUpdate. Gets run whenever a related
|
|
|
|
|
* job gets called from the queue.
|
2010-12-19 04:31:15 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup JobQueue
|
2007-04-04 05:22:37 +00:00
|
|
|
*/
|
2006-06-18 12:42:16 +00:00
|
|
|
class HTMLCacheUpdateJob extends Job {
|
|
|
|
|
var $table, $start, $end;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a job
|
2010-01-07 21:44:06 +00:00
|
|
|
* @param $title Title: the title linked to
|
|
|
|
|
* @param $params Array: job parameters (table, start and end page_ids)
|
2010-01-16 15:02:58 +00:00
|
|
|
* @param $id Integer: job id
|
2006-06-18 12:42:16 +00:00
|
|
|
*/
|
2007-06-21 19:11:24 +00:00
|
|
|
function __construct( $title, $params, $id = 0 ) {
|
2006-06-19 02:29:21 +00:00
|
|
|
parent::__construct( 'htmlCacheUpdate', $title, $params, $id );
|
2007-06-21 19:11:24 +00:00
|
|
|
$this->table = $params['table'];
|
|
|
|
|
$this->start = $params['start'];
|
|
|
|
|
$this->end = $params['end'];
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-23 23:22:24 +00:00
|
|
|
public function run() {
|
2009-08-12 05:00:30 +00:00
|
|
|
$update = new HTMLCacheUpdate( $this->title, $this->table, $this->start, $this->end );
|
|
|
|
|
$update->doUpdate();
|
2006-06-18 12:42:16 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|