Add bug and comment for r35609: * (bug 13434) Show a warning when hash identical...
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 /**
3 * See docs/deferred.txt
4 *
5 * @todo document (e.g. one-sentence top-level class description).
6 */
7 class LinksUpdate {
8
9 /**@{{
10 * @private
11 */
12 var $mId, //!< Page ID of the article linked from
13 $mTitle, //!< Title object of the article linked from
14 $mLinks, //!< Map of title strings to IDs for the links in the document
15 $mImages, //!< DB keys of the images used, in the array key only
16 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
17 $mExternals, //!< URLs of external links, array key only
18 $mCategories, //!< Map of category names to sort keys
19 $mInterlangs, //!< Map of language codes to titles
20 $mProperties, //!< Map of arbitrary name to value
21 $mDb, //!< Database connection reference
22 $mOptions, //!< SELECT options to be used (array)
23 $mRecursive; //!< Whether to queue jobs for recursive updates
24 /**@}}*/
25
26 /**
27 * Constructor
28 *
29 * @param Title $title Title of the page we're updating
30 * @param ParserOutput $parserOutput Output from a full parse of this page
31 * @param bool $recursive Queue jobs for recursive updates?
32 */
33 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
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 throw new MWException( "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->mParserOutput = $parserOutput;
51 $this->mLinks = $parserOutput->getLinks();
52 $this->mImages = $parserOutput->getImages();
53 $this->mTemplates = $parserOutput->getTemplates();
54 $this->mExternals = $parserOutput->getExternalLinks();
55 $this->mCategories = $parserOutput->getCategories();
56 $this->mProperties = $parserOutput->getProperties();
57
58 # Convert the format of the interlanguage links
59 # I didn't want to change it in the ParserOutput, because that array is passed all
60 # the way back to the skin, so either a skin API break would be required, or an
61 # inefficient back-conversion.
62 $ill = $parserOutput->getLanguageLinks();
63 $this->mInterlangs = array();
64 foreach ( $ill as $link ) {
65 list( $key, $title ) = explode( ':', $link, 2 );
66 $this->mInterlangs[$key] = $title;
67 }
68
69 $this->mRecursive = $recursive;
70
71 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
72 }
73
74 /**
75 * Update link tables with outgoing links from an updated article
76 */
77 function doUpdate() {
78 global $wgUseDumbLinkUpdate;
79
80 wfRunHooks( 'LinksUpdate', array( &$this ) );
81 if ( $wgUseDumbLinkUpdate ) {
82 $this->doDumbUpdate();
83 } else {
84 $this->doIncrementalUpdate();
85 }
86 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
87
88 }
89
90 function doIncrementalUpdate() {
91 wfProfileIn( __METHOD__ );
92
93 # Page links
94 $existing = $this->getExistingLinks();
95 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
96 $this->getLinkInsertions( $existing ) );
97
98 # Image links
99 $existing = $this->getExistingImages();
100 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
101 $this->getImageInsertions( $existing ) );
102
103 # Invalidate all image description pages which had links added or removed
104 $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
105 $this->invalidateImageDescriptions( $imageUpdates );
106
107 # External links
108 $existing = $this->getExistingExternals();
109 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
110 $this->getExternalInsertions( $existing ) );
111
112 # Language links
113 $existing = $this->getExistingInterlangs();
114 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
115 $this->getInterlangInsertions( $existing ) );
116
117 # Template links
118 $existing = $this->getExistingTemplates();
119 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
120 $this->getTemplateInsertions( $existing ) );
121
122 # Category links
123 $existing = $this->getExistingCategories();
124 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
125 $this->getCategoryInsertions( $existing ) );
126
127 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
128 $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
129 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories );
130 $categoryUpdates = $categoryInserts + $categoryDeletes;
131 $this->invalidateCategories( $categoryUpdates );
132 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
133
134 # Page properties
135 $existing = $this->getExistingProperties();
136 $this->incrTableUpdate( 'page_props', 'pp', $this->getPropertyDeletions( $existing ),
137 $this->getPropertyInsertions( $existing ) );
138
139 # Invalidate the necessary pages
140 $changed = array_diff_assoc( $existing, $this->mProperties ) + array_diff_assoc( $this->mProperties, $existing );
141 $this->invalidateProperties( $changed );
142
143 # Refresh links of all pages including this page
144 # This will be in a separate transaction
145 if ( $this->mRecursive ) {
146 $this->queueRecursiveJobs();
147 }
148
149 wfProfileOut( __METHOD__ );
150 }
151
152 /**
153 * Link update which clears the previous entries and inserts new ones
154 * May be slower or faster depending on level of lock contention and write speed of DB
155 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
156 */
157 function doDumbUpdate() {
158 wfProfileIn( __METHOD__ );
159
160 # Refresh category pages and image description pages
161 $existing = $this->getExistingCategories();
162 $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
163 $categoryDeletes = array_diff_assoc( $existing, $this->mCategoties );
164 $categoryUpdates = $categoryInserts + $categoryDeletes;
165 $existing = $this->getExistingImages();
166 $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
167
168 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
169 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
170 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
171 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
172 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
173 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(),'ll_from' );
174 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
175
176 # Update the cache of all the category pages and image description
177 # pages which were changed, and fix the category table count
178 $this->invalidateCategories( $categoryUpdates );
179 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
180 $this->invalidateImageDescriptions( $imageUpdates );
181
182 # Refresh links of all pages including this page
183 # This will be in a separate transaction
184 if ( $this->mRecursive ) {
185 $this->queueRecursiveJobs();
186 }
187
188 wfProfileOut( __METHOD__ );
189 }
190
191 function queueRecursiveJobs() {
192 wfProfileIn( __METHOD__ );
193
194 $batchSize = 100;
195 $dbr = wfGetDB( DB_SLAVE );
196 $res = $dbr->select( array( 'templatelinks', 'page' ),
197 array( 'page_namespace', 'page_title' ),
198 array(
199 'page_id=tl_from',
200 'tl_namespace' => $this->mTitle->getNamespace(),
201 'tl_title' => $this->mTitle->getDBkey()
202 ), __METHOD__
203 );
204
205 $done = false;
206 while ( !$done ) {
207 $jobs = array();
208 for ( $i = 0; $i < $batchSize; $i++ ) {
209 $row = $dbr->fetchObject( $res );
210 if ( !$row ) {
211 $done = true;
212 break;
213 }
214 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
215 $jobs[] = new RefreshLinksJob( $title, '' );
216 }
217 Job::batchInsert( $jobs );
218 }
219 $dbr->freeResult( $res );
220 wfProfileOut( __METHOD__ );
221 }
222
223 /**
224 * Invalidate the cache of a list of pages from a single namespace
225 *
226 * @param integer $namespace
227 * @param array $dbkeys
228 */
229 function invalidatePages( $namespace, $dbkeys ) {
230 if ( !count( $dbkeys ) ) {
231 return;
232 }
233
234 /**
235 * Determine which pages need to be updated
236 * This is necessary to prevent the job queue from smashing the DB with
237 * large numbers of concurrent invalidations of the same page
238 */
239 $now = $this->mDb->timestamp();
240 $ids = array();
241 $res = $this->mDb->select( 'page', array( 'page_id' ),
242 array(
243 'page_namespace' => $namespace,
244 'page_title IN (' . $this->mDb->makeList( $dbkeys ) . ')',
245 'page_touched < ' . $this->mDb->addQuotes( $now )
246 ), __METHOD__
247 );
248 while ( $row = $this->mDb->fetchObject( $res ) ) {
249 $ids[] = $row->page_id;
250 }
251 if ( !count( $ids ) ) {
252 return;
253 }
254
255 /**
256 * Do the update
257 * We still need the page_touched condition, in case the row has changed since
258 * the non-locking select above.
259 */
260 $this->mDb->update( 'page', array( 'page_touched' => $now ),
261 array(
262 'page_id IN (' . $this->mDb->makeList( $ids ) . ')',
263 'page_touched < ' . $this->mDb->addQuotes( $now )
264 ), __METHOD__
265 );
266 }
267
268 function invalidateCategories( $cats ) {
269 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
270 }
271
272 /**
273 * Update all the appropriate counts in the category table.
274 * @param $added associative array of category name => sort key
275 * @param $deleted associative array of category name => sort key
276 */
277 function updateCategoryCounts( $added, $deleted ) {
278 $a = new Article($this->mTitle);
279 $a->updateCategoryCounts(
280 array_keys( $added ), array_keys( $deleted )
281 );
282 }
283
284 function invalidateImageDescriptions( $images ) {
285 $this->invalidatePages( NS_IMAGE, array_keys( $images ) );
286 }
287
288 function dumbTableUpdate( $table, $insertions, $fromField ) {
289 $this->mDb->delete( $table, array( $fromField => $this->mId ), __METHOD__ );
290 if ( count( $insertions ) ) {
291 # The link array was constructed without FOR UPDATE, so there may
292 # be collisions. This may cause minor link table inconsistencies,
293 # which is better than crippling the site with lock contention.
294 $this->mDb->insert( $table, $insertions, __METHOD__, array( 'IGNORE' ) );
295 }
296 }
297
298 /**
299 * Make a WHERE clause from a 2-d NS/dbkey array
300 *
301 * @param array $arr 2-d array indexed by namespace and DB key
302 * @param string $prefix Field name prefix, without the underscore
303 */
304 function makeWhereFrom2d( &$arr, $prefix ) {
305 $lb = new LinkBatch;
306 $lb->setArray( $arr );
307 return $lb->constructSet( $prefix, $this->mDb );
308 }
309
310 /**
311 * Update a table by doing a delete query then an insert query
312 * @private
313 */
314 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
315 if ( $table == 'page_props' ) {
316 $fromField = 'pp_page';
317 } else {
318 $fromField = "{$prefix}_from";
319 }
320 $where = array( $fromField => $this->mId );
321 if ( $table == 'pagelinks' || $table == 'templatelinks' ) {
322 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
323 if ( $clause ) {
324 $where[] = $clause;
325 } else {
326 $where = false;
327 }
328 } else {
329 if ( $table == 'langlinks' ) {
330 $toField = 'll_lang';
331 } elseif ( $table == 'page_props' ) {
332 $toField = 'pp_propname';
333 } else {
334 $toField = $prefix . '_to';
335 }
336 if ( count( $deletions ) ) {
337 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
338 } else {
339 $where = false;
340 }
341 }
342 if ( $where ) {
343 $this->mDb->delete( $table, $where, __METHOD__ );
344 }
345 if ( count( $insertions ) ) {
346 $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
347 }
348 }
349
350
351 /**
352 * Get an array of pagelinks insertions for passing to the DB
353 * Skips the titles specified by the 2-D array $existing
354 * @private
355 */
356 function getLinkInsertions( $existing = array() ) {
357 $arr = array();
358 foreach( $this->mLinks as $ns => $dbkeys ) {
359 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
360 # in GlobalFunctions.php
361 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
362 foreach ( $diffs as $dbk => $id ) {
363 $arr[] = array(
364 'pl_from' => $this->mId,
365 'pl_namespace' => $ns,
366 'pl_title' => $dbk
367 );
368 }
369 }
370 return $arr;
371 }
372
373 /**
374 * Get an array of template insertions. Like getLinkInsertions()
375 * @private
376 */
377 function getTemplateInsertions( $existing = array() ) {
378 $arr = array();
379 foreach( $this->mTemplates as $ns => $dbkeys ) {
380 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
381 foreach ( $diffs as $dbk => $id ) {
382 $arr[] = array(
383 'tl_from' => $this->mId,
384 'tl_namespace' => $ns,
385 'tl_title' => $dbk
386 );
387 }
388 }
389 return $arr;
390 }
391
392 /**
393 * Get an array of image insertions
394 * Skips the names specified in $existing
395 * @private
396 */
397 function getImageInsertions( $existing = array() ) {
398 $arr = array();
399 $diffs = array_diff_key( $this->mImages, $existing );
400 foreach( $diffs as $iname => $dummy ) {
401 $arr[] = array(
402 'il_from' => $this->mId,
403 'il_to' => $iname
404 );
405 }
406 return $arr;
407 }
408
409 /**
410 * Get an array of externallinks insertions. Skips the names specified in $existing
411 * @private
412 */
413 function getExternalInsertions( $existing = array() ) {
414 $arr = array();
415 $diffs = array_diff_key( $this->mExternals, $existing );
416 foreach( $diffs as $url => $dummy ) {
417 $arr[] = array(
418 'el_from' => $this->mId,
419 'el_to' => $url,
420 'el_index' => wfMakeUrlIndex( $url ),
421 );
422 }
423 return $arr;
424 }
425
426 /**
427 * Get an array of category insertions
428 * @param array $existing Array mapping existing category names to sort keys. If both
429 * match a link in $this, the link will be omitted from the output
430 * @private
431 */
432 function getCategoryInsertions( $existing = array() ) {
433 $diffs = array_diff_assoc( $this->mCategories, $existing );
434 $arr = array();
435 foreach ( $diffs as $name => $sortkey ) {
436 $arr[] = array(
437 'cl_from' => $this->mId,
438 'cl_to' => $name,
439 'cl_sortkey' => $sortkey,
440 'cl_timestamp' => $this->mDb->timestamp()
441 );
442 }
443 return $arr;
444 }
445
446 /**
447 * Get an array of interlanguage link insertions
448 * @param array $existing Array mapping existing language codes to titles
449 * @private
450 */
451 function getInterlangInsertions( $existing = array() ) {
452 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
453 $arr = array();
454 foreach( $diffs as $lang => $title ) {
455 $arr[] = array(
456 'll_from' => $this->mId,
457 'll_lang' => $lang,
458 'll_title' => $title
459 );
460 }
461 return $arr;
462 }
463
464 /**
465 * Get an array of page property insertions
466 */
467 function getPropertyInsertions( $existing = array() ) {
468 $diffs = array_diff_assoc( $this->mProperties, $existing );
469 $arr = array();
470 foreach ( $diffs as $name => $value ) {
471 $arr[] = array(
472 'pp_page' => $this->mId,
473 'pp_propname' => $name,
474 'pp_value' => $value,
475 );
476 }
477 return $arr;
478 }
479
480
481 /**
482 * Given an array of existing links, returns those links which are not in $this
483 * and thus should be deleted.
484 * @private
485 */
486 function getLinkDeletions( $existing ) {
487 $del = array();
488 foreach ( $existing as $ns => $dbkeys ) {
489 if ( isset( $this->mLinks[$ns] ) ) {
490 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
491 } else {
492 $del[$ns] = $existing[$ns];
493 }
494 }
495 return $del;
496 }
497
498 /**
499 * Given an array of existing templates, returns those templates which are not in $this
500 * and thus should be deleted.
501 * @private
502 */
503 function getTemplateDeletions( $existing ) {
504 $del = array();
505 foreach ( $existing as $ns => $dbkeys ) {
506 if ( isset( $this->mTemplates[$ns] ) ) {
507 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
508 } else {
509 $del[$ns] = $existing[$ns];
510 }
511 }
512 return $del;
513 }
514
515 /**
516 * Given an array of existing images, returns those images which are not in $this
517 * and thus should be deleted.
518 * @private
519 */
520 function getImageDeletions( $existing ) {
521 return array_diff_key( $existing, $this->mImages );
522 }
523
524 /**
525 * Given an array of existing external links, returns those links which are not
526 * in $this and thus should be deleted.
527 * @private
528 */
529 function getExternalDeletions( $existing ) {
530 return array_diff_key( $existing, $this->mExternals );
531 }
532
533 /**
534 * Given an array of existing categories, returns those categories which are not in $this
535 * and thus should be deleted.
536 * @private
537 */
538 function getCategoryDeletions( $existing ) {
539 return array_diff_assoc( $existing, $this->mCategories );
540 }
541
542 /**
543 * Given an array of existing interlanguage links, returns those links which are not
544 * in $this and thus should be deleted.
545 * @private
546 */
547 function getInterlangDeletions( $existing ) {
548 return array_diff_assoc( $existing, $this->mInterlangs );
549 }
550
551 /**
552 * Get array of properties which should be deleted.
553 * @private
554 */
555 function getPropertyDeletions( $existing ) {
556 return array_diff_assoc( $existing, $this->mProperties );
557 }
558
559 /**
560 * Get an array of existing links, as a 2-D array
561 * @private
562 */
563 function getExistingLinks() {
564 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
565 array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
566 $arr = array();
567 while ( $row = $this->mDb->fetchObject( $res ) ) {
568 if ( !isset( $arr[$row->pl_namespace] ) ) {
569 $arr[$row->pl_namespace] = array();
570 }
571 $arr[$row->pl_namespace][$row->pl_title] = 1;
572 }
573 $this->mDb->freeResult( $res );
574 return $arr;
575 }
576
577 /**
578 * Get an array of existing templates, as a 2-D array
579 * @private
580 */
581 function getExistingTemplates() {
582 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
583 array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
584 $arr = array();
585 while ( $row = $this->mDb->fetchObject( $res ) ) {
586 if ( !isset( $arr[$row->tl_namespace] ) ) {
587 $arr[$row->tl_namespace] = array();
588 }
589 $arr[$row->tl_namespace][$row->tl_title] = 1;
590 }
591 $this->mDb->freeResult( $res );
592 return $arr;
593 }
594
595 /**
596 * Get an array of existing images, image names in the keys
597 * @private
598 */
599 function getExistingImages() {
600 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
601 array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
602 $arr = array();
603 while ( $row = $this->mDb->fetchObject( $res ) ) {
604 $arr[$row->il_to] = 1;
605 }
606 $this->mDb->freeResult( $res );
607 return $arr;
608 }
609
610 /**
611 * Get an array of existing external links, URLs in the keys
612 * @private
613 */
614 function getExistingExternals() {
615 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
616 array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
617 $arr = array();
618 while ( $row = $this->mDb->fetchObject( $res ) ) {
619 $arr[$row->el_to] = 1;
620 }
621 $this->mDb->freeResult( $res );
622 return $arr;
623 }
624
625 /**
626 * Get an array of existing categories, with the name in the key and sort key in the value.
627 * @private
628 */
629 function getExistingCategories() {
630 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
631 array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
632 $arr = array();
633 while ( $row = $this->mDb->fetchObject( $res ) ) {
634 $arr[$row->cl_to] = $row->cl_sortkey;
635 }
636 $this->mDb->freeResult( $res );
637 return $arr;
638 }
639
640 /**
641 * Get an array of existing interlanguage links, with the language code in the key and the
642 * title in the value.
643 * @private
644 */
645 function getExistingInterlangs() {
646 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
647 array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
648 $arr = array();
649 while ( $row = $this->mDb->fetchObject( $res ) ) {
650 $arr[$row->ll_lang] = $row->ll_title;
651 }
652 return $arr;
653 }
654
655 /**
656 * Get an array of existing categories, with the name in the key and sort key in the value.
657 * @private
658 */
659 function getExistingProperties() {
660 $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
661 array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
662 $arr = array();
663 while ( $row = $this->mDb->fetchObject( $res ) ) {
664 $arr[$row->pp_propname] = $row->pp_value;
665 }
666 $this->mDb->freeResult( $res );
667 return $arr;
668 }
669
670
671 /**
672 * Return the title object of the page being updated
673 */
674 function getTitle() {
675 return $this->mTitle;
676 }
677
678 /**
679 * Invalidate any necessary link lists related to page property changes
680 */
681 function invalidateProperties( $changed ) {
682 global $wgPagePropLinkInvalidations;
683
684 foreach ( $changed as $name => $value ) {
685 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
686 $inv = $wgPagePropLinkInvalidations[$name];
687 if ( !is_array( $inv ) ) {
688 $inv = array( $inv );
689 }
690 foreach ( $inv as $table ) {
691 $update = new HTMLCacheUpdate( $this->mTitle, $table );
692 $update->doUpdate();
693 }
694 }
695 }
696 }
697 }