3 * See docs/deferred.txt
5 * @todo document (e.g. one-sentence top-level class description).
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
29 * @param $title Title of the page we're updating
30 * @param $parserOutput ParserOutput: output from a full parse of this page
31 * @param $recursive Boolean: queue jobs for recursive updates?
33 function __construct( $title, $parserOutput, $recursive = true ) {
34 global $wgAntiLockFlags;
36 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
37 $this->mOptions
= array();
39 $this->mOptions
= array( 'FOR UPDATE' );
41 $this->mDb
= wfGetDB( DB_MASTER
);
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" );
47 $this->mTitle
= $title;
48 $this->mId
= $title->getArticleID();
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 $this->mInterwikis
= $parserOutput->getInterwikiLinks();
59 # Convert the format of the interlanguage links
60 # I didn't want to change it in the ParserOutput, because that array is passed all
61 # the way back to the skin, so either a skin API break would be required, or an
62 # inefficient back-conversion.
63 $ill = $parserOutput->getLanguageLinks();
64 $this->mInterlangs
= array();
65 foreach ( $ill as $link ) {
66 list( $key, $title ) = explode( ':', $link, 2 );
67 $this->mInterlangs
[$key] = $title;
70 $this->mRecursive
= $recursive;
72 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
76 * Update link tables with outgoing links from an updated article
78 public function doUpdate() {
79 global $wgUseDumbLinkUpdate;
81 wfRunHooks( 'LinksUpdate', array( &$this ) );
82 if ( $wgUseDumbLinkUpdate ) {
83 $this->doDumbUpdate();
85 $this->doIncrementalUpdate();
87 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
90 protected function doIncrementalUpdate() {
91 wfProfileIn( __METHOD__
);
94 $existing = $this->getExistingLinks();
95 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
96 $this->getLinkInsertions( $existing ) );
99 $existing = $this->getExistingImages();
101 $imageDeletes = $this->getImageDeletions( $existing );
102 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes, $this->getImageInsertions( $existing ) );
104 # Invalidate all image description pages which had links added or removed
105 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existing );
106 $this->invalidateImageDescriptions( $imageUpdates );
109 $existing = $this->getExistingExternals();
110 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
111 $this->getExternalInsertions( $existing ) );
114 $existing = $this->getExistingInterlangs();
115 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
116 $this->getInterlangInsertions( $existing ) );
118 # Inline interwiki links
119 $existing = $this->getExistingInterwikis();
120 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
121 $this->getInterwikiInsertions( $existing ) );
124 $existing = $this->getExistingTemplates();
125 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
126 $this->getTemplateInsertions( $existing ) );
129 $existing = $this->getExistingCategories();
131 $categoryDeletes = $this->getCategoryDeletions( $existing );
133 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes, $this->getCategoryInsertions( $existing ) );
135 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
136 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
137 $categoryUpdates = $categoryInserts +
$categoryDeletes;
138 $this->invalidateCategories( $categoryUpdates );
139 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
142 $existing = $this->getExistingProperties();
144 $propertiesDeletes = $this->getPropertyDeletions( $existing );
146 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes, $this->getPropertyInsertions( $existing ) );
148 # Invalidate the necessary pages
149 $changed = $propertiesDeletes +
array_diff_assoc( $this->mProperties
, $existing );
150 $this->invalidateProperties( $changed );
152 # Refresh links of all pages including this page
153 # This will be in a separate transaction
154 if ( $this->mRecursive
) {
155 $this->queueRecursiveJobs();
158 wfProfileOut( __METHOD__
);
162 * Link update which clears the previous entries and inserts new ones
163 * May be slower or faster depending on level of lock contention and write speed of DB
164 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
166 protected function doDumbUpdate() {
167 wfProfileIn( __METHOD__
);
169 # Refresh category pages and image description pages
170 $existing = $this->getExistingCategories();
171 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
172 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories
);
173 $categoryUpdates = $categoryInserts +
$categoryDeletes;
174 $existing = $this->getExistingImages();
175 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
177 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
178 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
179 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
180 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
181 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
182 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(),'ll_from' );
183 $this->dumbTableUpdate( 'iwlinks', $this->getInterwikiInsertions(),'iwl_from' );
184 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
186 # Update the cache of all the category pages and image description
187 # pages which were changed, and fix the category table count
188 $this->invalidateCategories( $categoryUpdates );
189 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
190 $this->invalidateImageDescriptions( $imageUpdates );
192 # Refresh links of all pages including this page
193 # This will be in a separate transaction
194 if ( $this->mRecursive
) {
195 $this->queueRecursiveJobs();
198 wfProfileOut( __METHOD__
);
201 function queueRecursiveJobs() {
202 global $wgUpdateRowsPerJob;
203 wfProfileIn( __METHOD__
);
205 $cache = $this->mTitle
->getBacklinkCache();
206 $batches = $cache->partition( 'templatelinks', $wgUpdateRowsPerJob );
208 wfProfileOut( __METHOD__
);
212 foreach ( $batches as $batch ) {
213 list( $start, $end ) = $batch;
218 $jobs[] = new RefreshLinksJob2( $this->mTitle
, $params );
220 Job
::batchInsert( $jobs );
222 wfProfileOut( __METHOD__
);
226 * Invalidate the cache of a list of pages from a single namespace
228 * @param $namespace Integer
229 * @param $dbkeys Array
231 function invalidatePages( $namespace, $dbkeys ) {
232 if ( !count( $dbkeys ) ) {
237 * Determine which pages need to be updated
238 * This is necessary to prevent the job queue from smashing the DB with
239 * large numbers of concurrent invalidations of the same page
241 $now = $this->mDb
->timestamp();
243 $res = $this->mDb
->select( 'page', array( 'page_id' ),
245 'page_namespace' => $namespace,
246 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
247 'page_touched < ' . $this->mDb
->addQuotes( $now )
250 foreach ( $res as $row ) {
251 $ids[] = $row->page_id
;
253 if ( !count( $ids ) ) {
259 * We still need the page_touched condition, in case the row has changed since
260 * the non-locking select above.
262 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
264 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
265 'page_touched < ' . $this->mDb
->addQuotes( $now )
270 function invalidateCategories( $cats ) {
271 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
275 * Update all the appropriate counts in the category table.
276 * @param $added associative array of category name => sort key
277 * @param $deleted associative array of category name => sort key
279 function updateCategoryCounts( $added, $deleted ) {
280 $a = new Article($this->mTitle
);
281 $a->updateCategoryCounts(
282 array_keys( $added ), array_keys( $deleted )
286 function invalidateImageDescriptions( $images ) {
287 $this->invalidatePages( NS_FILE
, array_keys( $images ) );
290 function dumbTableUpdate( $table, $insertions, $fromField ) {
291 $this->mDb
->delete( $table, array( $fromField => $this->mId
), __METHOD__
);
292 if ( count( $insertions ) ) {
293 # The link array was constructed without FOR UPDATE, so there may
294 # be collisions. This may cause minor link table inconsistencies,
295 # which is better than crippling the site with lock contention.
296 $this->mDb
->insert( $table, $insertions, __METHOD__
, array( 'IGNORE' ) );
301 * Update a table by doing a delete query then an insert query
304 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
305 if ( $table == 'page_props' ) {
306 $fromField = 'pp_page';
308 $fromField = "{$prefix}_from";
310 $where = array( $fromField => $this->mId
);
311 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ||
$table == 'iwlinks' ) {
312 if ( $table == 'iwlinks' ) {
313 $baseKey = 'iwl_prefix';
315 $baseKey = "{$prefix}_namespace";
317 $clause = $this->mDb
->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
324 if ( $table == 'langlinks' ) {
325 $toField = 'll_lang';
326 } elseif ( $table == 'page_props' ) {
327 $toField = 'pp_propname';
329 $toField = $prefix . '_to';
331 if ( count( $deletions ) ) {
332 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
338 $this->mDb
->delete( $table, $where, __METHOD__
);
340 if ( count( $insertions ) ) {
341 $this->mDb
->insert( $table, $insertions, __METHOD__
, 'IGNORE' );
347 * Get an array of pagelinks insertions for passing to the DB
348 * Skips the titles specified by the 2-D array $existing
351 function getLinkInsertions( $existing = array() ) {
353 foreach( $this->mLinks
as $ns => $dbkeys ) {
354 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
355 foreach ( $diffs as $dbk => $id ) {
357 'pl_from' => $this->mId
,
358 'pl_namespace' => $ns,
367 * Get an array of template insertions. Like getLinkInsertions()
370 function getTemplateInsertions( $existing = array() ) {
372 foreach( $this->mTemplates
as $ns => $dbkeys ) {
373 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
374 foreach ( $diffs as $dbk => $id ) {
376 'tl_from' => $this->mId
,
377 'tl_namespace' => $ns,
386 * Get an array of image insertions
387 * Skips the names specified in $existing
390 function getImageInsertions( $existing = array() ) {
392 $diffs = array_diff_key( $this->mImages
, $existing );
393 foreach( $diffs as $iname => $dummy ) {
395 'il_from' => $this->mId
,
403 * Get an array of externallinks insertions. Skips the names specified in $existing
406 function getExternalInsertions( $existing = array() ) {
408 $diffs = array_diff_key( $this->mExternals
, $existing );
409 foreach( $diffs as $url => $dummy ) {
411 'el_from' => $this->mId
,
413 'el_index' => wfMakeUrlIndex( $url ),
420 * Get an array of category insertions
422 * @param $existing Array mapping existing category names to sort keys. If both
423 * match a link in $this, the link will be omitted from the output
426 function getCategoryInsertions( $existing = array() ) {
427 global $wgContLang, $wgCategoryCollation;
428 $diffs = array_diff_assoc( $this->mCategories
, $existing );
430 foreach ( $diffs as $name => $sortkey ) {
431 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
432 $wgContLang->findVariantLink( $name, $nt, true );
434 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
436 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
442 # TODO: This is kind of wrong, because someone might set a sort
443 # key prefix that's the same as the default sortkey for the
444 # title. This should be fixed by refactoring code to replace
445 # $sortkey in this array by a prefix, but it's basically harmless
446 # (Title::moveTo() has had the same issue for a long time).
447 if ( $this->mTitle
->getCategorySortkey() == $sortkey ) {
449 $sortkey = $wgContLang->convertToSortkey( $sortkey );
451 # Treat custom sortkeys as a prefix, so that if multiple
452 # things are forced to sort as '*' or something, they'll
453 # sort properly in the category rather than in page_id
456 $sortkey = $wgContLang->convertToSortkey(
457 $this->mTitle
->getCategorySortkey( $prefix ) );
461 'cl_from' => $this->mId
,
463 'cl_sortkey' => $sortkey,
464 'cl_timestamp' => $this->mDb
->timestamp(),
465 'cl_sortkey_prefix' => $prefix,
466 'cl_collation' => $wgCategoryCollation,
474 * Get an array of interlanguage link insertions
476 * @param $existing Array mapping existing language codes to titles
479 function getInterlangInsertions( $existing = array() ) {
480 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
482 foreach( $diffs as $lang => $title ) {
484 'll_from' => $this->mId
,
493 * Get an array of page property insertions
495 function getPropertyInsertions( $existing = array() ) {
496 $diffs = array_diff_assoc( $this->mProperties
, $existing );
498 foreach ( $diffs as $name => $value ) {
500 'pp_page' => $this->mId
,
501 'pp_propname' => $name,
502 'pp_value' => $value,
509 * Get an array of interwiki insertions for passing to the DB
510 * Skips the titles specified by the 2-D array $existing
513 function getInterwikiInsertions( $existing = array() ) {
515 foreach( $this->mInterwikis
as $prefix => $dbkeys ) {
516 $diffs = isset( $existing[$prefix] ) ?
array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
517 foreach ( $diffs as $dbk => $id ) {
519 'iwl_from' => $this->mId
,
520 'iwl_prefix' => $prefix,
529 * Given an array of existing links, returns those links which are not in $this
530 * and thus should be deleted.
533 function getLinkDeletions( $existing ) {
535 foreach ( $existing as $ns => $dbkeys ) {
536 if ( isset( $this->mLinks
[$ns] ) ) {
537 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
539 $del[$ns] = $existing[$ns];
546 * Given an array of existing templates, returns those templates which are not in $this
547 * and thus should be deleted.
550 function getTemplateDeletions( $existing ) {
552 foreach ( $existing as $ns => $dbkeys ) {
553 if ( isset( $this->mTemplates
[$ns] ) ) {
554 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
556 $del[$ns] = $existing[$ns];
563 * Given an array of existing images, returns those images which are not in $this
564 * and thus should be deleted.
567 function getImageDeletions( $existing ) {
568 return array_diff_key( $existing, $this->mImages
);
572 * Given an array of existing external links, returns those links which are not
573 * in $this and thus should be deleted.
576 function getExternalDeletions( $existing ) {
577 return array_diff_key( $existing, $this->mExternals
);
581 * Given an array of existing categories, returns those categories which are not in $this
582 * and thus should be deleted.
585 function getCategoryDeletions( $existing ) {
586 return array_diff_assoc( $existing, $this->mCategories
);
590 * Given an array of existing interlanguage links, returns those links which are not
591 * in $this and thus should be deleted.
594 function getInterlangDeletions( $existing ) {
595 return array_diff_assoc( $existing, $this->mInterlangs
);
599 * Get array of properties which should be deleted.
602 function getPropertyDeletions( $existing ) {
603 return array_diff_assoc( $existing, $this->mProperties
);
607 * Given an array of existing interwiki links, returns those links which are not in $this
608 * and thus should be deleted.
611 function getInterwikiDeletions( $existing ) {
613 foreach ( $existing as $prefix => $dbkeys ) {
614 if ( isset( $this->mInterwikis
[$prefix] ) ) {
615 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
617 $del[$prefix] = $existing[$prefix];
624 * Get an array of existing links, as a 2-D array
627 function getExistingLinks() {
628 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
629 array( 'pl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
631 foreach ( $res as $row ) {
632 if ( !isset( $arr[$row->pl_namespace
] ) ) {
633 $arr[$row->pl_namespace
] = array();
635 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
641 * Get an array of existing templates, as a 2-D array
644 function getExistingTemplates() {
645 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
646 array( 'tl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
648 foreach ( $res as $row ) {
649 if ( !isset( $arr[$row->tl_namespace
] ) ) {
650 $arr[$row->tl_namespace
] = array();
652 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
658 * Get an array of existing images, image names in the keys
661 function getExistingImages() {
662 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
663 array( 'il_from' => $this->mId
), __METHOD__
, $this->mOptions
);
665 foreach ( $res as $row ) {
666 $arr[$row->il_to
] = 1;
672 * Get an array of existing external links, URLs in the keys
675 function getExistingExternals() {
676 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
677 array( 'el_from' => $this->mId
), __METHOD__
, $this->mOptions
);
679 foreach ( $res as $row ) {
680 $arr[$row->el_to
] = 1;
686 * Get an array of existing categories, with the name in the key and sort key in the value.
689 function getExistingCategories() {
690 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
691 array( 'cl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
693 foreach ( $res as $row ) {
694 $arr[$row->cl_to
] = $row->cl_sortkey
;
700 * Get an array of existing interlanguage links, with the language code in the key and the
701 * title in the value.
704 function getExistingInterlangs() {
705 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
706 array( 'll_from' => $this->mId
), __METHOD__
, $this->mOptions
);
708 foreach ( $res as $row ) {
709 $arr[$row->ll_lang
] = $row->ll_title
;
715 * Get an array of existing inline interwiki links, as a 2-D array
716 * @return array (prefix => array(dbkey => 1))
718 protected function getExistingInterwikis() {
719 $res = $this->mDb
->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
720 array( 'iwl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
722 foreach ( $res as $row ) {
723 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
724 $arr[$row->iwl_prefix
] = array();
726 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
732 * Get an array of existing categories, with the name in the key and sort key in the value.
735 function getExistingProperties() {
736 $res = $this->mDb
->select( 'page_props', array( 'pp_propname', 'pp_value' ),
737 array( 'pp_page' => $this->mId
), __METHOD__
, $this->mOptions
);
739 foreach ( $res as $row ) {
740 $arr[$row->pp_propname
] = $row->pp_value
;
747 * Return the title object of the page being updated
749 function getTitle() {
750 return $this->mTitle
;
754 * Return the list of images used as generated by the parser
756 public function getImages() {
757 return $this->mImages
;
761 * Invalidate any necessary link lists related to page property changes
763 function invalidateProperties( $changed ) {
764 global $wgPagePropLinkInvalidations;
766 foreach ( $changed as $name => $value ) {
767 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
768 $inv = $wgPagePropLinkInvalidations[$name];
769 if ( !is_array( $inv ) ) {
770 $inv = array( $inv );
772 foreach ( $inv as $table ) {
773 $update = new HTMLCacheUpdate( $this->mTitle
, $table );