* Follow-up r84610: don't assume a Parser object is attached
[lhc/web/wiklou.git] / includes / 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
27 {
28 /**
29 * @var Title
30 */
31 public $mTitle;
32
33 public $mTable, $mPrefix, $mStart, $mEnd;
34 public $mRowsPerJob, $mRowsPerQuery;
35
36 function __construct( $titleTo, $table, $start = false, $end = false ) {
37 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
38
39 $this->mTitle = $titleTo;
40 $this->mTable = $table;
41 $this->mStart = $start;
42 $this->mEnd = $end;
43 $this->mRowsPerJob = $wgUpdateRowsPerJob;
44 $this->mRowsPerQuery = $wgUpdateRowsPerQuery;
45 $this->mCache = $this->mTitle->getBacklinkCache();
46 }
47
48 public function doUpdate() {
49 if ( $this->mStart || $this->mEnd ) {
50 $this->doPartialUpdate();
51 return;
52 }
53
54 # Get an estimate of the number of rows from the BacklinkCache
55 $numRows = $this->mCache->getNumLinks( $this->mTable );
56 if ( $numRows > $this->mRowsPerJob * 2 ) {
57 # Do fast cached partition
58 $this->insertJobs();
59 } else {
60 # Get the links from the DB
61 $titleArray = $this->mCache->getLinks( $this->mTable );
62 # Check if the row count estimate was correct
63 if ( $titleArray->count() > $this->mRowsPerJob * 2 ) {
64 # Not correct, do accurate partition
65 wfDebug( __METHOD__.": row count estimate was incorrect, repartitioning\n" );
66 $this->insertJobsFromTitles( $titleArray );
67 } else {
68 $this->invalidateTitles( $titleArray );
69 }
70 }
71 }
72
73 /**
74 * Update some of the backlinks, defined by a page ID range
75 */
76 protected function doPartialUpdate() {
77 $titleArray = $this->mCache->getLinks( $this->mTable, $this->mStart, $this->mEnd );
78 if ( $titleArray->count() <= $this->mRowsPerJob * 2 ) {
79 # This partition is small enough, do the update
80 $this->invalidateTitles( $titleArray );
81 } else {
82 # Partitioning was excessively inaccurate. Divide the job further.
83 # This can occur when a large number of links are added in a short
84 # period of time, say by updating a heavily-used template.
85 $this->insertJobsFromTitles( $titleArray );
86 }
87 }
88
89 /**
90 * Partition the current range given by $this->mStart and $this->mEnd,
91 * using a pre-calculated title array which gives the links in that range.
92 * Queue the resulting jobs.
93 */
94 protected function insertJobsFromTitles( $titleArray ) {
95 # We make subpartitions in the sense that the start of the first job
96 # will be the start of the parent partition, and the end of the last
97 # job will be the end of the parent partition.
98 $jobs = array();
99 $start = $this->mStart; # start of the current job
100 $numTitles = 0;
101 foreach ( $titleArray as $title ) {
102 $id = $title->getArticleID();
103 # $numTitles is now the number of titles in the current job not
104 # including the current ID
105 if ( $numTitles >= $this->mRowsPerJob ) {
106 # Add a job up to but not including the current ID
107 $params = array(
108 'table' => $this->mTable,
109 'start' => $start,
110 'end' => $id - 1
111 );
112 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
113 $start = $id;
114 $numTitles = 0;
115 }
116 $numTitles++;
117 }
118 # Last job
119 $params = array(
120 'table' => $this->mTable,
121 'start' => $start,
122 'end' => $this->mEnd
123 );
124 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
125 wfDebug( __METHOD__.": repartitioning into " . count( $jobs ) . " jobs\n" );
126
127 if ( count( $jobs ) < 2 ) {
128 # I don't think this is possible at present, but handling this case
129 # makes the code a bit more robust against future code updates and
130 # avoids a potential infinite loop of repartitioning
131 wfDebug( __METHOD__.": repartitioning failed!\n" );
132 $this->invalidateTitles( $titleArray );
133 return;
134 }
135
136 Job::batchInsert( $jobs );
137 }
138
139 protected function insertJobs() {
140 $batches = $this->mCache->partition( $this->mTable, $this->mRowsPerJob );
141 if ( !$batches ) {
142 return;
143 }
144 $jobs = array();
145 foreach ( $batches as $batch ) {
146 $params = array(
147 'table' => $this->mTable,
148 'start' => $batch[0],
149 'end' => $batch[1],
150 );
151 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
152 }
153 Job::batchInsert( $jobs );
154 }
155
156 /**
157 * Invalidate a range of pages, right now
158 * @deprecated
159 */
160 public function invalidate( $startId = false, $endId = false ) {
161 $titleArray = $this->mCache->getLinks( $this->mTable, $startId, $endId );
162 $this->invalidateTitles( $titleArray );
163 }
164
165 /**
166 * Invalidate an array (or iterator) of Title objects, right now
167 */
168 protected function invalidateTitles( $titleArray ) {
169 global $wgUseFileCache, $wgUseSquid;
170
171 $dbw = wfGetDB( DB_MASTER );
172 $timestamp = $dbw->timestamp();
173
174 # Get all IDs in this query into an array
175 $ids = array();
176 foreach ( $titleArray as $title ) {
177 $ids[] = $title->getArticleID();
178 }
179
180 if ( !$ids ) {
181 return;
182 }
183
184 # Update page_touched
185 $batches = array_chunk( $ids, $this->mRowsPerQuery );
186 foreach ( $batches as $batch ) {
187 $dbw->update( 'page',
188 array( 'page_touched' => $timestamp ),
189 array( 'page_id IN (' . $dbw->makeList( $batch ) . ')' ),
190 __METHOD__
191 );
192 }
193
194 # Update squid
195 if ( $wgUseSquid ) {
196 $u = SquidUpdate::newFromTitles( $titleArray );
197 $u->doUpdate();
198 }
199
200 # Update file cache
201 if ( $wgUseFileCache ) {
202 foreach ( $titleArray as $title ) {
203 HTMLFileCache::clearFileCache( $title );
204 }
205 }
206 }
207
208 }
209
210 /**
211 * Job wrapper for HTMLCacheUpdate. Gets run whenever a related
212 * job gets called from the queue.
213 *
214 * @ingroup JobQueue
215 */
216 class HTMLCacheUpdateJob extends Job {
217 var $table, $start, $end;
218
219 /**
220 * Construct a job
221 * @param $title Title: the title linked to
222 * @param $params Array: job parameters (table, start and end page_ids)
223 * @param $id Integer: job id
224 */
225 function __construct( $title, $params, $id = 0 ) {
226 parent::__construct( 'htmlCacheUpdate', $title, $params, $id );
227 $this->table = $params['table'];
228 $this->start = $params['start'];
229 $this->end = $params['end'];
230 }
231
232 public function run() {
233 $update = new HTMLCacheUpdate( $this->title, $this->table, $this->start, $this->end );
234 $update->doUpdate();
235 return true;
236 }
237 }