Don't look for pipes in the root node.
[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 * Image::getLinksTo() and Title::touchArray(), which uses about 2KB of memory per
22 * link.
23 *
24 * @ingroup Cache
25 */
26 class HTMLCacheUpdate
27 {
28 public $mTitle, $mTable, $mPrefix, $mStart, $mEnd;
29 public $mRowsPerJob, $mRowsPerQuery;
30
31 function __construct( $titleTo, $table, $start = false, $end = false ) {
32 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
33
34 $this->mTitle = $titleTo;
35 $this->mTable = $table;
36 $this->mStart = $start;
37 $this->mEnd = $end;
38 $this->mRowsPerJob = $wgUpdateRowsPerJob;
39 $this->mRowsPerQuery = $wgUpdateRowsPerQuery;
40 $this->mCache = $this->mTitle->getBacklinkCache();
41 }
42
43 public function doUpdate() {
44 if ( $this->mStart || $this->mEnd ) {
45 $this->doPartialUpdate();
46 return;
47 }
48
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
53 $this->insertJobs();
54 } else {
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 );
62 } else {
63 $this->invalidateTitles( $titleArray );
64 }
65 }
66 }
67
68 /**
69 * Update some of the backlinks, defined by a page ID range
70 */
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 );
76 } else {
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 );
81 }
82 }
83
84 /**
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.
88 */
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.
93 $jobs = array();
94 $start = $this->mStart; # start of the current job
95 $numTitles = 0;
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
102 $params = array(
103 'table' => $this->mTable,
104 'start' => $start,
105 'end' => $id - 1
106 );
107 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
108 $start = $id;
109 $numTitles = 0;
110 }
111 $numTitles++;
112 }
113 # Last job
114 $params = array(
115 'table' => $this->mTable,
116 'start' => $start,
117 'end' => $this->mEnd
118 );
119 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
120 wfDebug( __METHOD__.": repartitioning into " . count( $jobs ) . " jobs\n" );
121
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 );
128 return;
129 }
130
131 Job::batchInsert( $jobs );
132 }
133
134 protected function insertJobs() {
135 $batches = $this->mCache->partition( $this->mTable, $this->mRowsPerJob );
136 if ( !$batches ) {
137 return;
138 }
139 $jobs = array();
140 foreach ( $batches as $batch ) {
141 $params = array(
142 'table' => $this->mTable,
143 'start' => $batch[0],
144 'end' => $batch[1],
145 );
146 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
147 }
148 Job::batchInsert( $jobs );
149 }
150
151 /**
152 * Invalidate a range of pages, right now
153 * @deprecated
154 */
155 public function invalidate( $startId = false, $endId = false ) {
156 $titleArray = $this->mCache->getLinks( $this->mTable, $startId, $endId );
157 $this->invalidateTitles( $titleArray );
158 }
159
160 /**
161 * Invalidate an array (or iterator) of Title objects, right now
162 */
163 protected function invalidateTitles( $titleArray ) {
164 global $wgUseFileCache, $wgUseSquid;
165
166 $dbw = wfGetDB( DB_MASTER );
167 $timestamp = $dbw->timestamp();
168
169 # Get all IDs in this query into an array
170 $ids = array();
171 foreach ( $titleArray as $title ) {
172 $ids[] = $title->getArticleID();
173 }
174
175 if ( !$ids ) {
176 return;
177 }
178
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 ) . ')' ),
185 __METHOD__
186 );
187 }
188
189 # Update squid
190 if ( $wgUseSquid ) {
191 $u = SquidUpdate::newFromTitles( $titleArray );
192 $u->doUpdate();
193 }
194
195 # Update file cache
196 if ( $wgUseFileCache ) {
197 foreach ( $titleArray as $title ) {
198 HTMLFileCache::clearFileCache( $title );
199 }
200 }
201 }
202
203 }
204
205 /**
206 * Job wrapper for HTMLCacheUpdate. Gets run whenever a related
207 * job gets called from the queue.
208 *
209 * @ingroup JobQueue
210 */
211 class HTMLCacheUpdateJob extends Job {
212 var $table, $start, $end;
213
214 /**
215 * Construct a job
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
219 */
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'];
225 }
226
227 public function run() {
228 $update = new HTMLCacheUpdate( $this->title, $this->table, $this->start, $this->end );
229 $update->doUpdate();
230 return true;
231 }
232 }