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