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 );
66 wfRunHooks( 'HTMLCacheUpdate::doUpdate', array($this->mTitle
) );
70 * Update some of the backlinks, defined by a page ID range
72 protected function doPartialUpdate() {
73 $titleArray = $this->mCache
->getLinks( $this->mTable
, $this->mStart
, $this->mEnd
);
74 if ( $titleArray->count() <= $this->mRowsPerJob
* 2 ) {
75 # This partition is small enough, do the update
76 $this->invalidateTitles( $titleArray );
78 # Partitioning was excessively inaccurate. Divide the job further.
79 # This can occur when a large number of links are added in a short
80 # period of time, say by updating a heavily-used template.
81 $this->insertJobsFromTitles( $titleArray );
86 * Partition the current range given by $this->mStart and $this->mEnd,
87 * using a pre-calculated title array which gives the links in that range.
88 * Queue the resulting jobs.
90 protected function insertJobsFromTitles( $titleArray ) {
91 # We make subpartitions in the sense that the start of the first job
92 # will be the start of the parent partition, and the end of the last
93 # job will be the end of the parent partition.
95 $start = $this->mStart
; # start of the current job
97 foreach ( $titleArray as $title ) {
98 $id = $title->getArticleID();
99 # $numTitles is now the number of titles in the current job not
100 # including the current ID
101 if ( $numTitles >= $this->mRowsPerJob
) {
102 # Add a job up to but not including the current ID
104 'table' => $this->mTable
,
108 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
116 'table' => $this->mTable
,
120 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
121 wfDebug( __METHOD__
.": repartitioning into " . count( $jobs ) . " jobs\n" );
123 if ( count( $jobs ) < 2 ) {
124 # I don't think this is possible at present, but handling this case
125 # makes the code a bit more robust against future code updates and
126 # avoids a potential infinite loop of repartitioning
127 wfDebug( __METHOD__
.": repartitioning failed!\n" );
128 $this->invalidateTitles( $titleArray );
132 Job
::batchInsert( $jobs );
135 protected function insertJobs() {
136 $batches = $this->mCache
->partition( $this->mTable
, $this->mRowsPerJob
);
141 foreach ( $batches as $batch ) {
143 'table' => $this->mTable
,
144 'start' => $batch[0],
147 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
149 Job
::batchInsert( $jobs );
153 * Invalidate a range of pages, right now
156 public function invalidate( $startId = false, $endId = false ) {
157 $titleArray = $this->mCache
->getLinks( $this->mTable
, $startId, $endId );
158 $this->invalidateTitles( $titleArray );
162 * Invalidate an array (or iterator) of Title objects, right now
164 protected function invalidateTitles( $titleArray ) {
165 global $wgUseFileCache, $wgUseSquid;
167 $dbw = wfGetDB( DB_MASTER
);
168 $timestamp = $dbw->timestamp();
170 # Get all IDs in this query into an array
172 foreach ( $titleArray as $title ) {
173 $ids[] = $title->getArticleID();
180 # Update page_touched
181 $batches = array_chunk( $ids, $this->mRowsPerQuery
);
182 foreach ( $batches as $batch ) {
183 $dbw->update( 'page',
184 array( 'page_touched' => $timestamp ),
185 array( 'page_id IN (' . $dbw->makeList( $batch ) . ')' ),
192 $u = SquidUpdate
::newFromTitles( $titleArray );
197 if ( $wgUseFileCache ) {
198 foreach ( $titleArray as $title ) {
199 HTMLFileCache
::clearFileCache( $title );
207 * Job wrapper for HTMLCacheUpdate. Gets run whenever a related
208 * job gets called from the queue.
212 class HTMLCacheUpdateJob
extends Job
{
213 var $table, $start, $end;
217 * @param $title Title: the title linked to
218 * @param $params Array: job parameters (table, start and end page_ids)
219 * @param $id Integer: job id
221 function __construct( $title, $params, $id = 0 ) {
222 parent
::__construct( 'htmlCacheUpdate', $title, $params, $id );
223 $this->table
= $params['table'];
224 $this->start
= $params['start'];
225 $this->end
= $params['end'];
228 public function run() {
229 $update = new HTMLCacheUpdate( $this->title
, $this->table
, $this->start
, $this->end
);