Added job table, for deferred processing of jobs. The immediate application is to...
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 * @package MediaWiki
5 */
6
7 /**
8 * @todo document
9 * @package MediaWiki
10 */
11 class LinksUpdate {
12
13 /**#@+
14 * @access private
15 */
16 var $mId, # Page ID of the article linked from
17 $mTitle, # Title object of the article linked from
18 $mLinks, # Map of title strings to IDs for the links in the document
19 $mImages, # DB keys of the images used, in the array key only
20 $mTemplates, # Map of title strings to IDs for the template references, including broken ones
21 $mExternals, # URLs of external links, array key only
22 $mCategories, # Map of category names to sort keys
23 $mDb, # Database connection reference
24 $mOptions; # SELECT options to be used (array)
25 /**#@-*/
26
27 /**
28 * Constructor
29 * Initialize private variables
30 * @param integer $id
31 * @param string $title
32 */
33 function LinksUpdate( $title, $parserOutput ) {
34 global $wgAntiLockFlags;
35
36 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
37 $this->mOptions = array();
38 } else {
39 $this->mOptions = array( 'FOR UPDATE' );
40 }
41 $this->mDb =& wfGetDB( DB_MASTER );
42
43 if ( !is_object( $title ) ) {
44 wfDebugDieBacktrace( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
45 "Please see Article::editUpdates() for an invocation example.\n" );
46 }
47 $this->mTitle = $title;
48 $this->mId = $title->getArticleID();
49
50 $this->mLinks = $parserOutput->getLinks();
51 $this->mImages = $parserOutput->getImages();
52 $this->mTemplates = $parserOutput->getTemplates();
53 $this->mExternals = $parserOutput->getExternalLinks();
54 $this->mCategories = $parserOutput->getCategories();
55
56 }
57
58 /**
59 * Update link tables with outgoing links from an updated article
60 */
61 function doUpdate() {
62 global $wgUseDumbLinkUpdate;
63 if ( $wgUseDumbLinkUpdate ) {
64 $this->doDumbUpdate();
65 } else {
66 $this->doIncrementalUpdate();
67 }
68 }
69
70 function doIncrementalUpdate() {
71 $fname = 'LinksUpdate::doIncrementalUpdate';
72 wfProfileIn( $fname );
73
74 # Page links
75 $existing = $this->getExistingLinks();
76 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
77 $this->getLinkInsertions( $existing ) );
78
79 # Image links
80 $existing = $this->getExistingImages();
81 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
82 $this->getImageInsertions( $existing ) );
83
84 # External links
85 $existing = $this->getExistingExternals();
86 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
87 $this->getExternalInsertions( $existing ) );
88
89 # Template links
90 $existing = $this->getExistingTemplates();
91 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
92 $this->getTemplateInsertions( $existing ) );
93
94 # Refresh links of all pages including this page
95 $tlto = $this->mTitle->getTemplateLinksTo();
96 if ( count( $tlto ) ) {
97 require_once( 'JobQueue.php' );
98 Job::queueLinksJobs( $tlto );
99 }
100
101 # Category links
102 $existing = $this->getExistingCategories();
103 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
104 $this->getCategoryInsertions( $existing ) );
105
106 # I think this works out to a set XOR operation, the idea is to invalidate all
107 # categories which were added, deleted or changed
108 # FIXME: surely there's a more appropriate place to put this update?
109 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
110 $this->invalidateCategories( $categoryUpdates );
111
112 wfProfileOut( $fname );
113 }
114
115 /**
116 * Link update which clears the previous entries and inserts new ones
117 * May be slower or faster depending on level of lock contention and write speed of DB
118 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
119 */
120 function doDumbUpdate() {
121 $fname = 'LinksUpdate::doDumbUpdate';
122 wfProfileIn( $fname );
123
124 # Refresh category pages
125 $existing = $this->getExistingCategories();
126 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
127
128 # Refresh links of all pages including this page
129 $tlto = $this->mTitle->getTemplateLinksTo();
130 if ( count( $tlto ) ) {
131 require_once( 'JobQueue.php' );
132 Job::queueLinksJobs( $tlto );
133 }
134
135 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
136 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
137 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
138 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
139 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
140
141 # Update the cache of all the category pages
142 $this->invalidateCategories( $categoryUpdates );
143
144 wfProfileOut( $fname );
145 }
146
147 function invalidateCategories( $cats ) {
148 $fname = 'LinksUpdate::invalidateCategories';
149 if ( count( $cats ) ) {
150 $this->mDb->update( 'page', array( 'page_touched' => $this->mDb->timestamp() ),
151 array(
152 'page_namespace' => NS_CATEGORY,
153 'page_title IN (' . $this->mDb->makeList( array_keys( $cats ) ) . ')'
154 ), $fname
155 );
156 }
157 }
158
159 function dumbTableUpdate( $table, $insertions, $fromField ) {
160 $fname = 'LinksUpdate::dumbTableUpdate';
161 $this->mDb->delete( $table, array( $fromField => $this->mId ), $fname );
162 if ( count( $insertions ) ) {
163 # The link array was constructed without FOR UPDATE, so there may be collisions
164 # Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
165 # sure it's better than without IGNORE
166 $this->mDb->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
167 }
168 }
169
170 /**
171 * Make a WHERE clause from a 2-d NS/dbkey array
172 *
173 * @param array $arr 2-d array indexed by namespace and DB key
174 * @param string $prefix Field name prefix, without the underscore
175 */
176 function makeWhereFrom2d( &$arr, $prefix ) {
177 $lb = new LinkBatch;
178 $lb->setArray( $arr );
179 return $lb->constructSet( $prefix, $this->mDb );
180 }
181
182 /**
183 * Update a table by doing a delete query then an insert query
184 * @access private
185 */
186 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
187 $fname = 'LinksUpdate::incrTableUpdate';
188 $where = array( "{$prefix}_from" => $this->mId );
189 if ( $table == 'pagelinks' || $table == 'templatelinks' ) {
190 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
191 if ( $clause ) {
192 $where[] = $clause;
193 } else {
194 $where = false;
195 }
196 } else {
197 if ( count( $deletions ) ) {
198 $where[] = "{$prefix}_to IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
199 } else {
200 $where = false;
201 }
202 }
203 if ( $where ) {
204 $this->mDb->delete( $table, $where, $fname );
205 }
206 if ( count( $insertions ) ) {
207 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
208 }
209 }
210
211
212 /**
213 * Get an array of pagelinks insertions for passing to the DB
214 * Skips the titles specified by the 2-D array $existing
215 * @access private
216 */
217 function getLinkInsertions( $existing = array() ) {
218 $arr = array();
219 foreach( $this->mLinks as $ns => $dbkeys ) {
220 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
221 # in GlobalFunctions.php
222 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
223 foreach ( $diffs as $dbk => $id ) {
224 $arr[] = array(
225 'pl_from' => $this->mId,
226 'pl_namespace' => $ns,
227 'pl_title' => $dbk
228 );
229 }
230 }
231 return $arr;
232 }
233
234 /**
235 * Get an array of template insertions. Like getLinkInsertions()
236 * @access private
237 */
238 function getTemplateInsertions( $existing = array() ) {
239 $arr = array();
240 foreach( $this->mTemplates as $ns => $dbkeys ) {
241 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
242 foreach ( $diffs as $dbk => $id ) {
243 $arr[] = array(
244 'tl_from' => $this->mId,
245 'tl_namespace' => $ns,
246 'tl_title' => $dbk
247 );
248 }
249 }
250 return $arr;
251 }
252
253 /**
254 * Get an array of image insertions
255 * Skips the names specified in $existing
256 * @access private
257 */
258 function getImageInsertions( $existing = array() ) {
259 $arr = array();
260 $diffs = array_diff_key( $this->mImages, $existing );
261 foreach( $diffs as $iname => $dummy ) {
262 $arr[] = array(
263 'il_from' => $this->mId,
264 'il_to' => $iname
265 );
266 }
267 return $arr;
268 }
269
270 /**
271 * Get an array of externallinks insertions. Skips the names specified in $existing
272 * @access private
273 */
274 function getExternalInsertions( $existing = array() ) {
275 $arr = array();
276 $diffs = array_diff_key( $this->mExternals, $existing );
277 foreach( $diffs as $url => $dummy ) {
278 $arr[] = array(
279 'el_from' => $this->mId,
280 'el_to' => $url,
281 'el_index' => wfMakeUrlIndex( $url ),
282 );
283 }
284 return $arr;
285 }
286
287 /**
288 * Get an array of category insertions
289 * @param array $existing Array mapping existing category names to sort keys. If both
290 * match a link in $this, the link will be omitted from the output
291 * @access private
292 */
293 function getCategoryInsertions( $existing = array() ) {
294 $diffs = array_diff_assoc( $this->mCategories, $existing );
295 $arr = array();
296 foreach ( $diffs as $name => $sortkey ) {
297 $arr[] = array(
298 'cl_from' => $this->mId,
299 'cl_to' => $name,
300 'cl_sortkey' => $sortkey
301 );
302 }
303 return $arr;
304 }
305
306 /**
307 * Given an array of existing links, returns those links which are not in $this
308 * and thus should be deleted.
309 * @access private
310 */
311 function getLinkDeletions( $existing ) {
312 $del = array();
313 foreach ( $existing as $ns => $dbkeys ) {
314 if ( isset( $this->mLinks[$ns] ) ) {
315 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
316 } else {
317 $del[$ns] = $existing[$ns];
318 }
319 }
320 return $del;
321 }
322
323 /**
324 * Given an array of existing templates, returns those templates which are not in $this
325 * and thus should be deleted.
326 * @access private
327 */
328 function getTemplateDeletions( $existing ) {
329 $del = array();
330 foreach ( $existing as $ns => $dbkeys ) {
331 if ( isset( $this->mTemplates[$ns] ) ) {
332 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
333 } else {
334 $del[$ns] = $existing[$ns];
335 }
336 }
337 return $del;
338 }
339
340 /**
341 * Given an array of existing images, returns those images which are not in $this
342 * and thus should be deleted.
343 * @access private
344 */
345 function getImageDeletions( $existing ) {
346 return array_diff_key( $existing, $this->mImages );
347 }
348
349 /**
350 * Given an array of existing external links, returns those links which are not
351 * in $this and thus should be deleted.
352 * @access private
353 */
354 function getExternalDeletions( $existing ) {
355 return array_diff_key( $existing, $this->mExternals );
356 }
357
358 /**
359 * Given an array of existing categories, returns those categories which are not in $this
360 * and thus should be deleted.
361 * @access private
362 */
363 function getCategoryDeletions( $existing ) {
364 return array_diff_assoc( $existing, $this->mCategories );
365 }
366
367 /**
368 * Get an array of existing links, as a 2-D array
369 * @access private
370 */
371 function getExistingLinks() {
372 $fname = 'LinksUpdate::getExistingLinks';
373 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
374 array( 'pl_from' => $this->mId ), $fname, $this->mOptions );
375 $arr = array();
376 while ( $row = $this->mDb->fetchObject( $res ) ) {
377 if ( !isset( $arr[$row->pl_namespace] ) ) {
378 $arr[$row->pl_namespace] = array();
379 }
380 $arr[$row->pl_namespace][$row->pl_title] = 1;
381 }
382 $this->mDb->freeResult( $res );
383 return $arr;
384 }
385
386 /**
387 * Get an array of existing templates, as a 2-D array
388 * @access private
389 */
390 function getExistingTemplates() {
391 $fname = 'LinksUpdate::getExistingTemplates';
392 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
393 array( 'tl_from' => $this->mId ), $fname, $this->mOptions );
394 $arr = array();
395 while ( $row = $this->mDb->fetchObject( $res ) ) {
396 if ( !isset( $arr[$row->tl_namespace] ) ) {
397 $arr[$row->tl_namespace] = array();
398 }
399 $arr[$row->tl_namespace][$row->tl_title] = 1;
400 }
401 $this->mDb->freeResult( $res );
402 return $arr;
403 }
404
405 /**
406 * Get an array of existing images, image names in the keys
407 * @access private
408 */
409 function getExistingImages() {
410 $fname = 'LinksUpdate::getExistingImages';
411 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
412 array( 'il_from' => $this->mId ), $fname, $this->mOptions );
413 $arr = array();
414 while ( $row = $this->mDb->fetchObject( $res ) ) {
415 $arr[$row->il_to] = 1;
416 }
417 $this->mDb->freeResult( $res );
418 return $arr;
419 }
420
421 /**
422 * Get an array of existing external links, URLs in the keys
423 * @access private
424 */
425 function getExistingExternals() {
426 $fname = 'LinksUpdate::getExistingExternals';
427 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
428 array( 'el_from' => $this->mId ), $fname, $this->mOptions );
429 $arr = array();
430 while ( $row = $this->mDb->fetchObject( $res ) ) {
431 $arr[$row->el_to] = 1;
432 }
433 $this->mDb->freeResult( $res );
434 return $arr;
435 }
436
437 /**
438 * Get an array of existing categories, with the name in the key and sort key in the value.
439 * @access private
440 */
441 function getExistingCategories() {
442 $fname = 'LinksUpdate::getExistingCategories';
443 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
444 array( 'cl_from' => $this->mId ), $fname, $this->mOptions );
445 $arr = array();
446 while ( $row = $this->mDb->fetchObject( $res ) ) {
447 $arr[$row->cl_to] = $row->cl_sortkey;
448 }
449 $this->mDb->freeResult( $res );
450 return $arr;
451 }
452 }
453 ?>