should be <=, not <
[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;
29 public $mRowsPerJob, $mRowsPerQuery;
30
31 function __construct( $titleTo, $table ) {
32 global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
33
34 $this->mTitle = $titleTo;
35 $this->mTable = $table;
36 $this->mRowsPerJob = $wgUpdateRowsPerJob;
37 $this->mRowsPerQuery = $wgUpdateRowsPerQuery;
38 }
39
40 public function doUpdate() {
41 # Fetch the IDs
42 $cond = $this->getToCondition();
43 $dbr = wfGetDB( DB_SLAVE );
44 $res = $dbr->select( $this->mTable, $this->getFromField(), $cond, __METHOD__ );
45
46 if ( $dbr->numRows( $res ) != 0 ) {
47 if ( $dbr->numRows( $res ) > $this->mRowsPerJob ) {
48 $this->insertJobs( $res );
49 } else {
50 $this->invalidateIDs( $res );
51 }
52 }
53 wfRunHooks( 'HTMLCacheUpdate::doUpdate', array($this->mTitle) );
54 }
55
56 function insertJobs( ResultWrapper $res ) {
57 $numRows = $res->numRows();
58 $numBatches = ceil( $numRows / $this->mRowsPerJob );
59 $realBatchSize = $numRows / $numBatches;
60 $start = false;
61 $jobs = array();
62 do {
63 for ( $i = 0; $i <= $realBatchSize - 1; $i++ ) {
64 $row = $res->fetchRow();
65 if ( $row ) {
66 $id = $row[0];
67 } else {
68 $id = false;
69 break;
70 }
71 }
72
73 $params = array(
74 'table' => $this->mTable,
75 'start' => $start,
76 'end' => ( $id !== false ? $id - 1 : false ),
77 );
78 $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
79
80 $start = $id;
81 } while ( $start );
82
83 Job::batchInsert( $jobs );
84 }
85
86 function getPrefix() {
87 static $prefixes = array(
88 'pagelinks' => 'pl',
89 'imagelinks' => 'il',
90 'categorylinks' => 'cl',
91 'templatelinks' => 'tl',
92 'redirect' => 'rd',
93
94 # Not needed
95 # 'externallinks' => 'el',
96 # 'langlinks' => 'll'
97 );
98
99 if ( is_null( $this->mPrefix ) ) {
100 $this->mPrefix = $prefixes[$this->mTable];
101 if ( is_null( $this->mPrefix ) ) {
102 throw new MWException( "Invalid table type \"{$this->mTable}\" in " . __CLASS__ );
103 }
104 }
105 return $this->mPrefix;
106 }
107
108 function getFromField() {
109 return $this->getPrefix() . '_from';
110 }
111
112 function getToCondition() {
113 $prefix = $this->getPrefix();
114 switch ( $this->mTable ) {
115 case 'pagelinks':
116 case 'templatelinks':
117 case 'redirect':
118 return array(
119 "{$prefix}_namespace" => $this->mTitle->getNamespace(),
120 "{$prefix}_title" => $this->mTitle->getDBkey()
121 );
122 case 'imagelinks':
123 return array( 'il_to' => $this->mTitle->getDBkey() );
124 case 'categorylinks':
125 return array( 'cl_to' => $this->mTitle->getDBkey() );
126 }
127 throw new MWException( 'Invalid table type in ' . __CLASS__ );
128 }
129
130 /**
131 * Invalidate a set of IDs, right now
132 */
133 function invalidateIDs( ResultWrapper $res ) {
134 global $wgUseFileCache, $wgUseSquid;
135
136 if ( $res->numRows() == 0 ) {
137 return;
138 }
139
140 $dbw = wfGetDB( DB_MASTER );
141 $timestamp = $dbw->timestamp();
142 $done = false;
143
144 while ( !$done ) {
145 # Get all IDs in this query into an array
146 $ids = array();
147 for ( $i = 0; $i < $this->mRowsPerQuery; $i++ ) {
148 $row = $res->fetchRow();
149 if ( $row ) {
150 $ids[] = $row[0];
151 } else {
152 $done = true;
153 break;
154 }
155 }
156
157 if ( !count( $ids ) ) {
158 break;
159 }
160
161 # Update page_touched
162 $dbw->update( 'page',
163 array( 'page_touched' => $timestamp ),
164 array( 'page_id IN (' . $dbw->makeList( $ids ) . ')' ),
165 __METHOD__
166 );
167
168 # Update squid
169 if ( $wgUseSquid || $wgUseFileCache ) {
170 $titles = Title::newFromIDs( $ids );
171 if ( $wgUseSquid ) {
172 $u = SquidUpdate::newFromTitles( $titles );
173 $u->doUpdate();
174 }
175
176 # Update file cache
177 if ( $wgUseFileCache ) {
178 foreach ( $titles as $title ) {
179 $cm = new HTMLFileCache($title);
180 @unlink($cm->fileCacheName());
181 }
182 }
183 }
184 }
185 }
186 }
187
188 /**
189 * @todo document (e.g. one-sentence top-level class description).
190 * @ingroup JobQueue
191 */
192 class HTMLCacheUpdateJob extends Job {
193 var $table, $start, $end;
194
195 /**
196 * Construct a job
197 * @param Title $title The title linked to
198 * @param array $params Job parameters (table, start and end page_ids)
199 * @param integer $id job_id
200 */
201 function __construct( $title, $params, $id = 0 ) {
202 parent::__construct( 'htmlCacheUpdate', $title, $params, $id );
203 $this->table = $params['table'];
204 $this->start = $params['start'];
205 $this->end = $params['end'];
206 }
207
208 function run() {
209 $update = new HTMLCacheUpdate( $this->title, $this->table );
210
211 $fromField = $update->getFromField();
212 $conds = $update->getToCondition();
213 if ( $this->start ) {
214 $conds[] = "$fromField >= {$this->start}";
215 }
216 if ( $this->end ) {
217 $conds[] = "$fromField <= {$this->end}";
218 }
219
220 $dbr = wfGetDB( DB_SLAVE );
221 $res = $dbr->select( $this->table, $fromField, $conds, __METHOD__ );
222 $update->invalidateIDs( $res );
223
224 return true;
225 }
226 }