4 * Class to invalidate the HTML cache of all the pages linking to a given title.
5 * Small numbers of links will be done immediately, large numbers are pushed onto
8 * This class is designed to work efficiently with small numbers of links, and
9 * to work reasonably well with up to ~10^5 links. Above ~10^6 links, the memory
10 * and time requirements of loading all backlinked IDs in doUpdate() might become
11 * prohibitive. The requirements measured at Wikimedia are approximately:
13 * memory: 48 bytes per row
14 * time: 16us per row for the query plus processing
16 * The reason this query is done is to support partitioning of the job
17 * by backlinked ID. The memory issue could be allieviated by doing this query in
18 * batches, but of course LIMIT with an offset is inefficient on the DB side.
20 * The class is nevertheless a vast improvement on the previous method of using
21 * Image::getLinksTo() and Title::touchArray(), which uses about 2KB of memory per
28 public $mTitle, $mTable, $mPrefix, $mStart, $mEnd;
29 public $mRowsPerJob, $mRowsPerQuery;
31 function __construct( $titleTo, $table, $start = false, $end = false ) {
32 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
34 $this->mTitle
= $titleTo;
35 $this->mTable
= $table;
36 $this->mStart
= $start;
38 $this->mRowsPerJob
= $wgUpdateRowsPerJob;
39 $this->mRowsPerQuery
= $wgUpdateRowsPerQuery;
40 $this->mCache
= $this->mTitle
->getBacklinkCache();
43 public function doUpdate() {
44 if ( $this->mStart ||
$this->mEnd
) {
45 $this->doPartialUpdate();
49 # Get an estimate of the number of rows from the BacklinkCache
50 $numRows = $this->mCache
->getNumLinks( $this->mTable
);
51 if ( $numRows > $this->mRowsPerJob
* 2 ) {
52 # Do fast cached partition
55 # Get the links from the DB
56 $titleArray = $this->mCache
->getLinks( $this->mTable
);
57 # Check if the row count estimate was correct
58 if ( $titleArray->count() > $this->mRowsPerJob
* 2 ) {
59 # Not correct, do accurate partition
60 wfDebug( __METHOD__
.": row count estimate was incorrect, repartitioning\n" );
61 $this->insertJobsFromTitles( $titleArray );
63 $this->invalidateTitles( $titleArray );
69 * Update some of the backlinks, defined by a page ID range
71 protected function doPartialUpdate() {
72 $titleArray = $this->mCache
->getLinks( $this->mTable
, $this->mStart
, $this->mEnd
);
73 if ( $titleArray->count() <= $this->mRowsPerJob
* 2 ) {
74 # This partition is small enough, do the update
75 $this->invalidateTitles( $titleArray );
77 # Partitioning was excessively inaccurate. Divide the job further.
78 # This can occur when a large number of links are added in a short
79 # period of time, say by updating a heavily-used template.
80 $this->insertJobsFromTitles( $titleArray );
85 * Partition the current range given by $this->mStart and $this->mEnd,
86 * using a pre-calculated title array which gives the links in that range.
87 * Queue the resulting jobs.
89 protected function insertJobsFromTitles( $titleArray ) {
90 # We make subpartitions in the sense that the start of the first job
91 # will be the start of the parent partition, and the end of the last
92 # job will be the end of the parent partition.
94 $start = $this->mStart
; # start of the current job
96 foreach ( $titleArray as $title ) {
97 $id = $title->getArticleID();
98 # $numTitles is now the number of titles in the current job not
99 # including the current ID
100 if ( $numTitles >= $this->mRowsPerJob
) {
101 # Add a job up to but not including the current ID
103 'table' => $this->mTable
,
107 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
115 'table' => $this->mTable
,
119 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
120 wfDebug( __METHOD__
.": repartitioning into " . count( $jobs ) . " jobs\n" );
122 if ( count( $jobs ) < 2 ) {
123 # I don't think this is possible at present, but handling this case
124 # makes the code a bit more robust against future code updates and
125 # avoids a potential infinite loop of repartitioning
126 wfDebug( __METHOD__
.": repartitioning failed!\n" );
127 $this->invalidateTitles( $titleArray );
131 Job
::batchInsert( $jobs );
134 protected function insertJobs() {
135 $batches = $this->mCache
->partition( $this->mTable
, $this->mRowsPerJob
);
140 foreach ( $batches as $batch ) {
142 'table' => $this->mTable
,
143 'start' => $batch[0],
146 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
148 Job
::batchInsert( $jobs );
152 * Invalidate a range of pages, right now
155 public function invalidate( $startId = false, $endId = false ) {
156 $titleArray = $this->mCache
->getLinks( $this->mTable
, $startId, $endId );
157 $this->invalidateTitles( $titleArray );
161 * Invalidate an array (or iterator) of Title objects, right now
163 protected function invalidateTitles( $titleArray ) {
164 global $wgUseFileCache, $wgUseSquid;
166 $dbw = wfGetDB( DB_MASTER
);
167 $timestamp = $dbw->timestamp();
169 # Get all IDs in this query into an array
171 foreach ( $titleArray as $title ) {
172 $ids[] = $title->getArticleID();
179 # Update page_touched
180 $batches = array_chunk( $ids, $this->mRowsPerQuery
);
181 foreach ( $batches as $batch ) {
182 $dbw->update( 'page',
183 array( 'page_touched' => $timestamp ),
184 array( 'page_id IN (' . $dbw->makeList( $batch ) . ')' ),
191 $u = SquidUpdate
::newFromTitles( $titleArray );
196 if ( $wgUseFileCache ) {
197 foreach ( $titleArray as $title ) {
198 HTMLFileCache
::clearFileCache( $title );
206 * Job wrapper for HTMLCacheUpdate. Gets run whenever a related
207 * job gets called from the queue.
211 class HTMLCacheUpdateJob
extends Job
{
212 var $table, $start, $end;
216 * @param $title Title: the title linked to
217 * @param $params Array: job parameters (table, start and end page_ids)
218 * @param $id Integer: job id
220 function __construct( $title, $params, $id = 0 ) {
221 parent
::__construct( 'htmlCacheUpdate', $title, $params, $id );
222 $this->table
= $params['table'];
223 $this->start
= $params['start'];
224 $this->end
= $params['end'];
227 public function run() {
228 $update = new HTMLCacheUpdate( $this->title
, $this->table
, $this->start
, $this->end
);