Refactor deferrable updates into classes & interfaces, also add helper method for...
[lhc/web/wiklou.git] / includes / cache / HTMLCacheUpdate.php
1 <?php
2
3 /**
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
6 * the job queue.
7 *
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:
12 *
13 * memory: 48 bytes per row
14 * time: 16us per row for the query plus processing
15 *
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.
19 *
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
22 * link.
23 *
24 * @ingroup Cache
25 */
26 class HTMLCacheUpdate implements DeferrableUpdate {
27 /**
28 * @var Title
29 */
30 public $mTitle;
31
32 public $mTable, $mPrefix, $mStart, $mEnd;
33 public $mRowsPerJob, $mRowsPerQuery;
34
35 function __construct( $titleTo, $table, $start = false, $end = false ) {
36 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
37
38 $this->mTitle = $titleTo;
39 $this->mTable = $table;
40 $this->mStart = $start;
41 $this->mEnd = $end;
42 $this->mRowsPerJob = $wgUpdateRowsPerJob;
43 $this->mRowsPerQuery = $wgUpdateRowsPerQuery;
44 $this->mCache = $this->mTitle->getBacklinkCache();
45 }
46
47 public function doUpdate() {
48 if ( $this->mStart || $this->mEnd ) {
49 $this->doPartialUpdate();
50 return;
51 }
52
53 if ( $this->mTable === 'globaltemplatelinks' ) {
54 global $wgEnableInterwikiTemplatesTracking;
55
56 if ( $wgEnableInterwikiTemplatesTracking ) {
57 $distantPageArray = $this->mCache->getDistantTemplateLinks( 'globaltemplatelinks' );
58 $this->invalidateDistantTitles( $distantPageArray );
59 }
60 return;
61 }
62
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
67 $this->insertJobs();
68 } else {
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 );
76 } else {
77 $this->invalidateTitles( $titleArray );
78 }
79 }
80 wfRunHooks( 'HTMLCacheUpdate::doUpdate', array($this->mTitle) );
81 }
82
83 /**
84 * Update some of the backlinks, defined by a page ID range
85 */
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 );
91 } else {
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 );
96 }
97 }
98
99 /**
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.
103 *
104 * @param $titleArray array
105 */
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.
110 $jobs = array();
111 $start = $this->mStart; # start of the current job
112 $numTitles = 0;
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
119 $params = array(
120 'table' => $this->mTable,
121 'start' => $start,
122 'end' => $id - 1
123 );
124 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
125 $start = $id;
126 $numTitles = 0;
127 }
128 $numTitles++;
129 }
130 # Last job
131 $params = array(
132 'table' => $this->mTable,
133 'start' => $start,
134 'end' => $this->mEnd
135 );
136 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
137 wfDebug( __METHOD__.": repartitioning into " . count( $jobs ) . " jobs\n" );
138
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 );
145 return;
146 }
147
148 Job::batchInsert( $jobs );
149 }
150
151 protected function insertJobs() {
152 $batches = $this->mCache->partition( $this->mTable, $this->mRowsPerJob );
153 if ( !$batches ) {
154 return;
155 }
156 $jobs = array();
157 foreach ( $batches as $batch ) {
158 $params = array(
159 'table' => $this->mTable,
160 'start' => $batch[0],
161 'end' => $batch[1],
162 );
163 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
164 }
165 Job::batchInsert( $jobs );
166 }
167
168 /**
169 * Invalidate an array (or iterator) of Title objects, right now
170 */
171 protected function invalidateTitles( $titleArray ) {
172 global $wgUseFileCache, $wgUseSquid;
173
174 $dbw = wfGetDB( DB_MASTER );
175 $timestamp = $dbw->timestamp();
176
177 # Get all IDs in this query into an array
178 $ids = array();
179 foreach ( $titleArray as $title ) {
180 $ids[] = $title->getArticleID();
181 }
182
183 if ( !$ids ) {
184 return;
185 }
186
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 IN (' . $dbw->makeList( $batch ) . ')' ),
193 __METHOD__
194 );
195 }
196
197 # Update squid
198 if ( $wgUseSquid ) {
199 $u = SquidUpdate::newFromTitles( $titleArray );
200 $u->doUpdate();
201 }
202
203 # Update file cache
204 if ( $wgUseFileCache ) {
205 foreach ( $titleArray as $title ) {
206 HTMLFileCache::clearFileCache( $title );
207 }
208 }
209 }
210
211 /**
212 * Invalidate an array of distant pages, given the wiki ID and page ID of those pages
213 */
214 protected function invalidateDistantTitles( $distantPageArray ) {
215 global $wgUseSquid;
216
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();
225 }
226 $pagesByWiki[$wikiid][] = $row->gtl_from_page;
227 $titleArray[] = Title::makeTitle( $row->gtl_from_namespace, $row->gtl_from_title, '', $row->gil_interwiki );
228 }
229
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 ) . ')' ),
238 __METHOD__
239 );
240 }
241 }
242
243 # Update squid
244 if ( $wgUseSquid ) {
245 $u = SquidUpdate::newFromTitles( $titleArray );
246 $u->doUpdate();
247 }
248 }
249 }
250
251 /**
252 * Job wrapper for HTMLCacheUpdate. Gets run whenever a related
253 * job gets called from the queue.
254 *
255 * @ingroup JobQueue
256 */
257 class HTMLCacheUpdateJob extends Job {
258 var $table, $start, $end;
259
260 /**
261 * Construct a job
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
265 */
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'];
271 }
272
273 public function run() {
274 $update = new HTMLCacheUpdate( $this->title, $this->table, $this->start, $this->end );
275 $update->doUpdate();
276 return true;
277 }
278 }