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 foreach ( $this->mCategories
as &$sortkey ) {
71 # If the sortkey is longer then 255 bytes,
72 # it truncated by DB, and then doesn't get
73 # matched when comparing existing vs current
74 # categories, causing bug 25254.
75 # Also. substr behaves weird when given "".
76 if ( $sortkey !== '' ) {
77 $sortkey = substr( $sortkey, 0, 255 );
81 $this->mRecursive
= $recursive;
83 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
87 * Update link tables with outgoing links from an updated article
89 public function doUpdate() {
90 global $wgUseDumbLinkUpdate;
92 wfRunHooks( 'LinksUpdate', array( &$this ) );
93 if ( $wgUseDumbLinkUpdate ) {
94 $this->doDumbUpdate();
96 $this->doIncrementalUpdate();
98 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
101 protected function doIncrementalUpdate() {
102 wfProfileIn( __METHOD__
);
105 $existing = $this->getExistingLinks();
106 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
107 $this->getLinkInsertions( $existing ) );
110 $existing = $this->getExistingImages();
112 $imageDeletes = $this->getImageDeletions( $existing );
113 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
114 $this->getImageInsertions( $existing ) );
116 # Invalidate all image description pages which had links added or removed
117 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existing );
118 $this->invalidateImageDescriptions( $imageUpdates );
121 $existing = $this->getExistingExternals();
122 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
123 $this->getExternalInsertions( $existing ) );
126 $existing = $this->getExistingInterlangs();
127 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
128 $this->getInterlangInsertions( $existing ) );
130 # Inline interwiki links
131 $existing = $this->getExistingInterwikis();
132 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
133 $this->getInterwikiInsertions( $existing ) );
136 $existing = $this->getExistingTemplates();
137 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
138 $this->getTemplateInsertions( $existing ) );
141 $existing = $this->getExistingCategories();
143 $categoryDeletes = $this->getCategoryDeletions( $existing );
145 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
146 $this->getCategoryInsertions( $existing ) );
148 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
149 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
150 $categoryUpdates = $categoryInserts +
$categoryDeletes;
151 $this->invalidateCategories( $categoryUpdates );
152 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
155 $existing = $this->getExistingProperties();
157 $propertiesDeletes = $this->getPropertyDeletions( $existing );
159 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
160 $this->getPropertyInsertions( $existing ) );
162 # Invalidate the necessary pages
163 $changed = $propertiesDeletes +
array_diff_assoc( $this->mProperties
, $existing );
164 $this->invalidateProperties( $changed );
166 # Refresh links of all pages including this page
167 # This will be in a separate transaction
168 if ( $this->mRecursive
) {
169 $this->queueRecursiveJobs();
172 wfProfileOut( __METHOD__
);
176 * Link update which clears the previous entries and inserts new ones
177 * May be slower or faster depending on level of lock contention and write speed of DB
178 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
180 protected function doDumbUpdate() {
181 wfProfileIn( __METHOD__
);
183 # Refresh category pages and image description pages
184 $existing = $this->getExistingCategories();
185 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
186 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories
);
187 $categoryUpdates = $categoryInserts +
$categoryDeletes;
188 $existing = $this->getExistingImages();
189 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
191 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
192 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
193 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
194 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
195 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
196 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(),'ll_from' );
197 $this->dumbTableUpdate( 'iwlinks', $this->getInterwikiInsertions(),'iwl_from' );
198 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
200 # Update the cache of all the category pages and image description
201 # pages which were changed, and fix the category table count
202 $this->invalidateCategories( $categoryUpdates );
203 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
204 $this->invalidateImageDescriptions( $imageUpdates );
206 # Refresh links of all pages including this page
207 # This will be in a separate transaction
208 if ( $this->mRecursive
) {
209 $this->queueRecursiveJobs();
212 wfProfileOut( __METHOD__
);
215 function queueRecursiveJobs() {
216 global $wgUpdateRowsPerJob;
217 wfProfileIn( __METHOD__
);
219 $cache = $this->mTitle
->getBacklinkCache();
220 $batches = $cache->partition( 'templatelinks', $wgUpdateRowsPerJob );
222 wfProfileOut( __METHOD__
);
226 foreach ( $batches as $batch ) {
227 list( $start, $end ) = $batch;
232 $jobs[] = new RefreshLinksJob2( $this->mTitle
, $params );
234 Job
::batchInsert( $jobs );
236 wfProfileOut( __METHOD__
);
240 * Invalidate the cache of a list of pages from a single namespace
242 * @param $namespace Integer
243 * @param $dbkeys Array
245 function invalidatePages( $namespace, $dbkeys ) {
246 if ( !count( $dbkeys ) ) {
251 * Determine which pages need to be updated
252 * This is necessary to prevent the job queue from smashing the DB with
253 * large numbers of concurrent invalidations of the same page
255 $now = $this->mDb
->timestamp();
257 $res = $this->mDb
->select( 'page', array( 'page_id' ),
259 'page_namespace' => $namespace,
260 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
261 'page_touched < ' . $this->mDb
->addQuotes( $now )
264 foreach ( $res as $row ) {
265 $ids[] = $row->page_id
;
267 if ( !count( $ids ) ) {
273 * We still need the page_touched condition, in case the row has changed since
274 * the non-locking select above.
276 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
278 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
279 'page_touched < ' . $this->mDb
->addQuotes( $now )
284 function invalidateCategories( $cats ) {
285 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
289 * Update all the appropriate counts in the category table.
290 * @param $added associative array of category name => sort key
291 * @param $deleted associative array of category name => sort key
293 function updateCategoryCounts( $added, $deleted ) {
294 $a = new Article($this->mTitle
);
295 $a->updateCategoryCounts(
296 array_keys( $added ), array_keys( $deleted )
300 function invalidateImageDescriptions( $images ) {
301 $this->invalidatePages( NS_FILE
, array_keys( $images ) );
304 function dumbTableUpdate( $table, $insertions, $fromField ) {
305 $this->mDb
->delete( $table, array( $fromField => $this->mId
), __METHOD__
);
306 if ( count( $insertions ) ) {
307 # The link array was constructed without FOR UPDATE, so there may
308 # be collisions. This may cause minor link table inconsistencies,
309 # which is better than crippling the site with lock contention.
310 $this->mDb
->insert( $table, $insertions, __METHOD__
, array( 'IGNORE' ) );
315 * Update a table by doing a delete query then an insert query
318 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
319 if ( $table == 'page_props' ) {
320 $fromField = 'pp_page';
322 $fromField = "{$prefix}_from";
324 $where = array( $fromField => $this->mId
);
325 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ||
$table == 'iwlinks' ) {
326 if ( $table == 'iwlinks' ) {
327 $baseKey = 'iwl_prefix';
329 $baseKey = "{$prefix}_namespace";
331 $clause = $this->mDb
->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
338 if ( $table == 'langlinks' ) {
339 $toField = 'll_lang';
340 } elseif ( $table == 'page_props' ) {
341 $toField = 'pp_propname';
343 $toField = $prefix . '_to';
345 if ( count( $deletions ) ) {
346 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
352 $this->mDb
->delete( $table, $where, __METHOD__
);
354 if ( count( $insertions ) ) {
355 $this->mDb
->insert( $table, $insertions, __METHOD__
, 'IGNORE' );
361 * Get an array of pagelinks insertions for passing to the DB
362 * Skips the titles specified by the 2-D array $existing
365 function getLinkInsertions( $existing = array() ) {
367 foreach( $this->mLinks
as $ns => $dbkeys ) {
368 $diffs = isset( $existing[$ns] )
369 ?
array_diff_key( $dbkeys, $existing[$ns] )
371 foreach ( $diffs as $dbk => $id ) {
373 'pl_from' => $this->mId
,
374 'pl_namespace' => $ns,
383 * Get an array of template insertions. Like getLinkInsertions()
386 function getTemplateInsertions( $existing = array() ) {
388 foreach( $this->mTemplates
as $ns => $dbkeys ) {
389 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
390 foreach ( $diffs as $dbk => $id ) {
392 'tl_from' => $this->mId
,
393 'tl_namespace' => $ns,
402 * Get an array of image insertions
403 * Skips the names specified in $existing
406 function getImageInsertions( $existing = array() ) {
408 $diffs = array_diff_key( $this->mImages
, $existing );
409 foreach( $diffs as $iname => $dummy ) {
411 'il_from' => $this->mId
,
419 * Get an array of externallinks insertions. Skips the names specified in $existing
422 function getExternalInsertions( $existing = array() ) {
424 $diffs = array_diff_key( $this->mExternals
, $existing );
425 foreach( $diffs as $url => $dummy ) {
427 'el_from' => $this->mId
,
429 'el_index' => wfMakeUrlIndex( $url ),
436 * Get an array of category insertions
438 * @param $existing Array mapping existing category names to sort keys. If both
439 * match a link in $this, the link will be omitted from the output
442 function getCategoryInsertions( $existing = array() ) {
443 global $wgContLang, $wgCategoryCollation;
444 $diffs = array_diff_assoc( $this->mCategories
, $existing );
446 foreach ( $diffs as $name => $prefix ) {
447 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
448 $wgContLang->findVariantLink( $name, $nt, true );
450 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
452 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
458 # Treat custom sortkeys as a prefix, so that if multiple
459 # things are forced to sort as '*' or something, they'll
460 # sort properly in the category rather than in page_id
462 $sortkey = Collation
::singleton()->getSortKey(
463 $this->mTitle
->getCategorySortkey( $prefix ) );
466 'cl_from' => $this->mId
,
468 'cl_sortkey' => $sortkey,
469 'cl_timestamp' => $this->mDb
->timestamp(),
470 'cl_sortkey_prefix' => $prefix,
471 'cl_collation' => $wgCategoryCollation,
479 * Get an array of interlanguage link insertions
481 * @param $existing Array mapping existing language codes to titles
484 function getInterlangInsertions( $existing = array() ) {
485 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
487 foreach( $diffs as $lang => $title ) {
489 'll_from' => $this->mId
,
498 * Get an array of page property insertions
500 function getPropertyInsertions( $existing = array() ) {
501 $diffs = array_diff_assoc( $this->mProperties
, $existing );
503 foreach ( $diffs as $name => $value ) {
505 'pp_page' => $this->mId
,
506 'pp_propname' => $name,
507 'pp_value' => $value,
514 * Get an array of interwiki insertions for passing to the DB
515 * Skips the titles specified by the 2-D array $existing
518 function getInterwikiInsertions( $existing = array() ) {
520 foreach( $this->mInterwikis
as $prefix => $dbkeys ) {
521 $diffs = isset( $existing[$prefix] ) ?
array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
522 foreach ( $diffs as $dbk => $id ) {
524 'iwl_from' => $this->mId
,
525 'iwl_prefix' => $prefix,
534 * Given an array of existing links, returns those links which are not in $this
535 * and thus should be deleted.
538 function getLinkDeletions( $existing ) {
540 foreach ( $existing as $ns => $dbkeys ) {
541 if ( isset( $this->mLinks
[$ns] ) ) {
542 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
544 $del[$ns] = $existing[$ns];
551 * Given an array of existing templates, returns those templates which are not in $this
552 * and thus should be deleted.
555 function getTemplateDeletions( $existing ) {
557 foreach ( $existing as $ns => $dbkeys ) {
558 if ( isset( $this->mTemplates
[$ns] ) ) {
559 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
561 $del[$ns] = $existing[$ns];
568 * Given an array of existing images, returns those images which are not in $this
569 * and thus should be deleted.
572 function getImageDeletions( $existing ) {
573 return array_diff_key( $existing, $this->mImages
);
577 * Given an array of existing external links, returns those links which are not
578 * in $this and thus should be deleted.
581 function getExternalDeletions( $existing ) {
582 return array_diff_key( $existing, $this->mExternals
);
586 * Given an array of existing categories, returns those categories which are not in $this
587 * and thus should be deleted.
590 function getCategoryDeletions( $existing ) {
591 return array_diff_assoc( $existing, $this->mCategories
);
595 * Given an array of existing interlanguage links, returns those links which are not
596 * in $this and thus should be deleted.
599 function getInterlangDeletions( $existing ) {
600 return array_diff_assoc( $existing, $this->mInterlangs
);
604 * Get array of properties which should be deleted.
607 function getPropertyDeletions( $existing ) {
608 return array_diff_assoc( $existing, $this->mProperties
);
612 * Given an array of existing interwiki links, returns those links which are not in $this
613 * and thus should be deleted.
616 function getInterwikiDeletions( $existing ) {
618 foreach ( $existing as $prefix => $dbkeys ) {
619 if ( isset( $this->mInterwikis
[$prefix] ) ) {
620 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
622 $del[$prefix] = $existing[$prefix];
629 * Get an array of existing links, as a 2-D array
632 function getExistingLinks() {
633 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
634 array( 'pl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
636 foreach ( $res as $row ) {
637 if ( !isset( $arr[$row->pl_namespace
] ) ) {
638 $arr[$row->pl_namespace
] = array();
640 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
646 * Get an array of existing templates, as a 2-D array
649 function getExistingTemplates() {
650 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
651 array( 'tl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
653 foreach ( $res as $row ) {
654 if ( !isset( $arr[$row->tl_namespace
] ) ) {
655 $arr[$row->tl_namespace
] = array();
657 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
663 * Get an array of existing images, image names in the keys
666 function getExistingImages() {
667 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
668 array( 'il_from' => $this->mId
), __METHOD__
, $this->mOptions
);
670 foreach ( $res as $row ) {
671 $arr[$row->il_to
] = 1;
677 * Get an array of existing external links, URLs in the keys
680 function getExistingExternals() {
681 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
682 array( 'el_from' => $this->mId
), __METHOD__
, $this->mOptions
);
684 foreach ( $res as $row ) {
685 $arr[$row->el_to
] = 1;
691 * Get an array of existing categories, with the name in the key and sort key in the value.
694 function getExistingCategories() {
695 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
696 array( 'cl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
698 foreach ( $res as $row ) {
699 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
705 * Get an array of existing interlanguage links, with the language code in the key and the
706 * title in the value.
709 function getExistingInterlangs() {
710 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
711 array( 'll_from' => $this->mId
), __METHOD__
, $this->mOptions
);
713 foreach ( $res as $row ) {
714 $arr[$row->ll_lang
] = $row->ll_title
;
720 * Get an array of existing inline interwiki links, as a 2-D array
721 * @return array (prefix => array(dbkey => 1))
723 protected function getExistingInterwikis() {
724 $res = $this->mDb
->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
725 array( 'iwl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
727 foreach ( $res as $row ) {
728 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
729 $arr[$row->iwl_prefix
] = array();
731 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
737 * Get an array of existing categories, with the name in the key and sort key in the value.
740 function getExistingProperties() {
741 $res = $this->mDb
->select( 'page_props', array( 'pp_propname', 'pp_value' ),
742 array( 'pp_page' => $this->mId
), __METHOD__
, $this->mOptions
);
744 foreach ( $res as $row ) {
745 $arr[$row->pp_propname
] = $row->pp_value
;
752 * Return the title object of the page being updated
754 function getTitle() {
755 return $this->mTitle
;
759 * Return the list of images used as generated by the parser
761 public function getImages() {
762 return $this->mImages
;
766 * Invalidate any necessary link lists related to page property changes
768 function invalidateProperties( $changed ) {
769 global $wgPagePropLinkInvalidations;
771 foreach ( $changed as $name => $value ) {
772 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
773 $inv = $wgPagePropLinkInvalidations[$name];
774 if ( !is_array( $inv ) ) {
775 $inv = array( $inv );
777 foreach ( $inv as $table ) {
778 $update = new HTMLCacheUpdate( $this->mTitle
, $table );