91a0930ff0b235f18e524586b7341143dfc5b5fa
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 * File::getLinksTo() and Title::touchArray(), which uses about 2KB of memory per
26 class HTMLCacheUpdate
implements DeferrableUpdate
{
32 public $mTable, $mPrefix, $mStart, $mEnd;
33 public $mRowsPerJob, $mRowsPerQuery;
35 function __construct( $titleTo, $table, $start = false, $end = false ) {
36 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
38 $this->mTitle
= $titleTo;
39 $this->mTable
= $table;
40 $this->mStart
= $start;
42 $this->mRowsPerJob
= $wgUpdateRowsPerJob;
43 $this->mRowsPerQuery
= $wgUpdateRowsPerQuery;
44 $this->mCache
= $this->mTitle
->getBacklinkCache();
47 public function doUpdate() {
48 if ( $this->mStart ||
$this->mEnd
) {
49 $this->doPartialUpdate();
53 if ( $this->mTable
=== 'globaltemplatelinks' ) {
54 global $wgEnableInterwikiTemplatesTracking;
56 if ( $wgEnableInterwikiTemplatesTracking ) {
57 $distantPageArray = $this->mCache
->getDistantTemplateLinks( 'globaltemplatelinks' );
58 $this->invalidateDistantTitles( $distantPageArray );
63 # Get an estimate of the number of rows from the BacklinkCache
64 $numRows = $this->mCache
->getNumLinks( $this->mTable
);
65 if ( $numRows > $this->mRowsPerJob
* 2 ) {
66 # Do fast cached partition
69 # Get the links from the DB
70 $titleArray = $this->mCache
->getLinks( $this->mTable
);
71 # Check if the row count estimate was correct
72 if ( $titleArray->count() > $this->mRowsPerJob
* 2 ) {
73 # Not correct, do accurate partition
74 wfDebug( __METHOD__
.": row count estimate was incorrect, repartitioning\n" );
75 $this->insertJobsFromTitles( $titleArray );
77 $this->invalidateTitles( $titleArray );
80 wfRunHooks( 'HTMLCacheUpdate::doUpdate', array($this->mTitle
) );
84 * Update some of the backlinks, defined by a page ID range
86 protected function doPartialUpdate() {
87 $titleArray = $this->mCache
->getLinks( $this->mTable
, $this->mStart
, $this->mEnd
);
88 if ( $titleArray->count() <= $this->mRowsPerJob
* 2 ) {
89 # This partition is small enough, do the update
90 $this->invalidateTitles( $titleArray );
92 # Partitioning was excessively inaccurate. Divide the job further.
93 # This can occur when a large number of links are added in a short
94 # period of time, say by updating a heavily-used template.
95 $this->insertJobsFromTitles( $titleArray );
100 * Partition the current range given by $this->mStart and $this->mEnd,
101 * using a pre-calculated title array which gives the links in that range.
102 * Queue the resulting jobs.
104 * @param $titleArray array
106 protected function insertJobsFromTitles( $titleArray ) {
107 # We make subpartitions in the sense that the start of the first job
108 # will be the start of the parent partition, and the end of the last
109 # job will be the end of the parent partition.
111 $start = $this->mStart
; # start of the current job
113 foreach ( $titleArray as $title ) {
114 $id = $title->getArticleID();
115 # $numTitles is now the number of titles in the current job not
116 # including the current ID
117 if ( $numTitles >= $this->mRowsPerJob
) {
118 # Add a job up to but not including the current ID
120 'table' => $this->mTable
,
124 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
132 'table' => $this->mTable
,
136 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
137 wfDebug( __METHOD__
.": repartitioning into " . count( $jobs ) . " jobs\n" );
139 if ( count( $jobs ) < 2 ) {
140 # I don't think this is possible at present, but handling this case
141 # makes the code a bit more robust against future code updates and
142 # avoids a potential infinite loop of repartitioning
143 wfDebug( __METHOD__
.": repartitioning failed!\n" );
144 $this->invalidateTitles( $titleArray );
148 Job
::batchInsert( $jobs );
151 protected function insertJobs() {
152 $batches = $this->mCache
->partition( $this->mTable
, $this->mRowsPerJob
);
157 foreach ( $batches as $batch ) {
159 'table' => $this->mTable
,
160 'start' => $batch[0],
163 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle
, $params );
165 Job
::batchInsert( $jobs );
169 * Invalidate an array (or iterator) of Title objects, right now
171 protected function invalidateTitles( $titleArray ) {
172 global $wgUseFileCache, $wgUseSquid;
174 $dbw = wfGetDB( DB_MASTER
);
175 $timestamp = $dbw->timestamp();
177 # Get all IDs in this query into an array
179 foreach ( $titleArray as $title ) {
180 $ids[] = $title->getArticleID();
187 # Update page_touched
188 $batches = array_chunk( $ids, $this->mRowsPerQuery
);
189 foreach ( $batches as $batch ) {
190 $dbw->update( 'page',
191 array( 'page_touched' => $timestamp ),
192 array( 'page_id' => $batch ),
199 $u = SquidUpdate
::newFromTitles( $titleArray );
204 if ( $wgUseFileCache ) {
205 foreach ( $titleArray as $title ) {
206 HTMLFileCache
::clearFileCache( $title );
212 * Invalidate an array of distant pages, given the wiki ID and page ID of those pages
214 protected function invalidateDistantTitles( $distantPageArray ) {
217 $pagesByWiki = array();
218 $titleArray = array();
219 # Sort by WikiID in $pagesByWiki
220 # Create the distant titles for Squid in $titleArray
221 foreach ( $distantPageArray as $row ) {
222 $wikiid = $row->gtl_from_wiki
;
223 if( !isset( $pagesByWiki[$wikiid] ) ) {
224 $pagesByWiki[$wikiid] = array();
226 $pagesByWiki[$wikiid][] = $row->gtl_from_page
;
227 $titleArray[] = Title
::makeTitle( $row->gtl_from_namespace
, $row->gtl_from_title
, '', $row->gil_interwiki
);
230 foreach ( $pagesByWiki as $wikiid => $pages ) {
231 $dbw = wfGetDB( DB_MASTER
, array( ), $wikiid );
232 $timestamp = $dbw->timestamp();
233 $batches = array_chunk( $pages, $this->mRowsPerQuery
);
234 foreach ( $batches as $batch ) {
235 $dbw->update( 'page',
236 array( 'page_touched' => $timestamp ),
237 array( 'page_id IN (' . $dbw->makeList( $batch ) . ')' ),
245 $u = SquidUpdate
::newFromTitles( $titleArray );
252 * Job wrapper for HTMLCacheUpdate. Gets run whenever a related
253 * job gets called from the queue.
257 class HTMLCacheUpdateJob
extends Job
{
258 var $table, $start, $end;
262 * @param $title Title: the title linked to
263 * @param $params Array: job parameters (table, start and end page_ids)
264 * @param $id Integer: job id
266 function __construct( $title, $params, $id = 0 ) {
267 parent
::__construct( 'htmlCacheUpdate', $title, $params, $id );
268 $this->table
= $params['table'];
269 $this->start
= $params['start'];
270 $this->end
= $params['end'];
273 public function run() {
274 $update = new HTMLCacheUpdate( $this->title
, $this->table
, $this->start
, $this->end
);