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