3 * See docs/deferred.txt
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @todo document (e.g. one-sentence top-level class description).
22 class LinksUpdate
extends SecondaryDBDataUpdate
{
27 var $mLinks, //!< Map of title strings to IDs for the links in the document
28 $mImages, //!< DB keys of the images used, in the array key only
29 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
30 $mExternals, //!< URLs of external links, array key only
31 $mCategories, //!< Map of category names to sort keys
32 $mInterlangs, //!< Map of language codes to titles
33 $mProperties, //!< Map of arbitrary name to value
34 $mRecursive; //!< Whether to queue jobs for recursive updates
40 * @param $title Title of the page we're updating
41 * @param $parserOutput ParserOutput: output from a full parse of this page
42 * @param $recursive Boolean: queue jobs for recursive updates?
44 function __construct( $title, $parserOutput, $recursive = true ) {
45 if ( !is_object( $title ) ) {
46 throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
47 "Please see Article::editUpdates() for an invocation example.\n" );
50 parent
::__construct( $title, $parserOutput );
52 $this->mLinks
= $parserOutput->getLinks();
53 $this->mImages
= $parserOutput->getImages();
54 $this->mTemplates
= $parserOutput->getTemplates();
55 $this->mExternals
= $parserOutput->getExternalLinks();
56 $this->mCategories
= $parserOutput->getCategories();
57 $this->mProperties
= $parserOutput->getProperties();
58 $this->mInterwikis
= $parserOutput->getInterwikiLinks();
60 # Convert the format of the interlanguage links
61 # I didn't want to change it in the ParserOutput, because that array is passed all
62 # the way back to the skin, so either a skin API break would be required, or an
63 # inefficient back-conversion.
64 $ill = $parserOutput->getLanguageLinks();
65 $this->mInterlangs
= array();
66 foreach ( $ill as $link ) {
67 list( $key, $title ) = explode( ':', $link, 2 );
68 $this->mInterlangs
[$key] = $title;
71 foreach ( $this->mCategories
as &$sortkey ) {
72 # If the sortkey is longer then 255 bytes,
73 # it truncated by DB, and then doesn't get
74 # matched when comparing existing vs current
75 # categories, causing bug 25254.
76 # Also. substr behaves weird when given "".
77 if ( $sortkey !== '' ) {
78 $sortkey = substr( $sortkey, 0, 255 );
82 $this->mRecursive
= $recursive;
84 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
88 * Update link tables with outgoing links from an updated article
90 public function doUpdate() {
91 global $wgUseDumbLinkUpdate;
93 wfRunHooks( 'LinksUpdate', array( &$this ) );
94 if ( $wgUseDumbLinkUpdate ) {
95 $this->doDumbUpdate();
97 $this->doIncrementalUpdate();
99 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
102 protected function doIncrementalUpdate() {
103 wfProfileIn( __METHOD__
);
106 $existing = $this->getExistingLinks();
107 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
108 $this->getLinkInsertions( $existing ) );
111 $existing = $this->getExistingImages();
113 $imageDeletes = $this->getImageDeletions( $existing );
114 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
115 $this->getImageInsertions( $existing ) );
117 # Invalidate all image description pages which had links added or removed
118 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existing );
119 $this->invalidateImageDescriptions( $imageUpdates );
122 $existing = $this->getExistingExternals();
123 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
124 $this->getExternalInsertions( $existing ) );
127 $existing = $this->getExistingInterlangs();
128 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
129 $this->getInterlangInsertions( $existing ) );
131 # Inline interwiki links
132 $existing = $this->getExistingInterwikis();
133 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
134 $this->getInterwikiInsertions( $existing ) );
137 $existing = $this->getExistingTemplates();
138 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
139 $this->getTemplateInsertions( $existing ) );
142 $existing = $this->getExistingCategories();
144 $categoryDeletes = $this->getCategoryDeletions( $existing );
146 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
147 $this->getCategoryInsertions( $existing ) );
149 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
150 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
151 $categoryUpdates = $categoryInserts +
$categoryDeletes;
152 $this->invalidateCategories( $categoryUpdates );
153 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
156 $existing = $this->getExistingProperties();
158 $propertiesDeletes = $this->getPropertyDeletions( $existing );
160 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
161 $this->getPropertyInsertions( $existing ) );
163 # Invalidate the necessary pages
164 $changed = $propertiesDeletes +
array_diff_assoc( $this->mProperties
, $existing );
165 $this->invalidateProperties( $changed );
167 # Refresh links of all pages including this page
168 # This will be in a separate transaction
169 if ( $this->mRecursive
) {
170 $this->queueRecursiveJobs();
173 wfProfileOut( __METHOD__
);
177 * Link update which clears the previous entries and inserts new ones
178 * May be slower or faster depending on level of lock contention and write speed of DB
179 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
181 protected function doDumbUpdate() {
182 wfProfileIn( __METHOD__
);
184 # Refresh category pages and image description pages
185 $existing = $this->getExistingCategories();
186 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
187 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories
);
188 $categoryUpdates = $categoryInserts +
$categoryDeletes;
189 $existing = $this->getExistingImages();
190 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
192 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
193 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
194 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
195 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
196 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
197 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(),'ll_from' );
198 $this->dumbTableUpdate( 'iwlinks', $this->getInterwikiInsertions(),'iwl_from' );
199 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
201 # Update the cache of all the category pages and image description
202 # pages which were changed, and fix the category table count
203 $this->invalidateCategories( $categoryUpdates );
204 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
205 $this->invalidateImageDescriptions( $imageUpdates );
207 # Refresh links of all pages including this page
208 # This will be in a separate transaction
209 if ( $this->mRecursive
) {
210 $this->queueRecursiveJobs();
213 wfProfileOut( __METHOD__
);
216 function queueRecursiveJobs() {
217 global $wgUpdateRowsPerJob;
218 wfProfileIn( __METHOD__
);
220 $cache = $this->mTitle
->getBacklinkCache();
221 $batches = $cache->partition( 'templatelinks', $wgUpdateRowsPerJob );
223 wfProfileOut( __METHOD__
);
227 foreach ( $batches as $batch ) {
228 list( $start, $end ) = $batch;
230 'table' => 'templatelinks',
234 $jobs[] = new RefreshLinksJob2( $this->mTitle
, $params );
236 Job
::batchInsert( $jobs );
238 wfProfileOut( __METHOD__
);
244 function invalidateCategories( $cats ) {
245 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
249 * Update all the appropriate counts in the category table.
250 * @param $added array associative array of category name => sort key
251 * @param $deleted array associative array of category name => sort key
253 function updateCategoryCounts( $added, $deleted ) {
254 $a = WikiPage
::factory( $this->mTitle
);
255 $a->updateCategoryCounts(
256 array_keys( $added ), array_keys( $deleted )
263 function invalidateImageDescriptions( $images ) {
264 $this->invalidatePages( NS_FILE
, array_keys( $images ) );
272 private function dumbTableUpdate( $table, $insertions, $fromField ) {
273 $this->mDb
->delete( $table, array( $fromField => $this->mId
), __METHOD__
);
274 if ( count( $insertions ) ) {
275 # The link array was constructed without FOR UPDATE, so there may
276 # be collisions. This may cause minor link table inconsistencies,
277 # which is better than crippling the site with lock contention.
278 $this->mDb
->insert( $table, $insertions, __METHOD__
, array( 'IGNORE' ) );
283 * Update a table by doing a delete query then an insert query
289 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
290 if ( $table == 'page_props' ) {
291 $fromField = 'pp_page';
293 $fromField = "{$prefix}_from";
295 $where = array( $fromField => $this->mId
);
296 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ||
$table == 'iwlinks' ) {
297 if ( $table == 'iwlinks' ) {
298 $baseKey = 'iwl_prefix';
300 $baseKey = "{$prefix}_namespace";
302 $clause = $this->mDb
->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
309 if ( $table == 'langlinks' ) {
310 $toField = 'll_lang';
311 } elseif ( $table == 'page_props' ) {
312 $toField = 'pp_propname';
314 $toField = $prefix . '_to';
316 if ( count( $deletions ) ) {
317 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
323 $this->mDb
->delete( $table, $where, __METHOD__
);
325 if ( count( $insertions ) ) {
326 $this->mDb
->insert( $table, $insertions, __METHOD__
, 'IGNORE' );
331 * Get an array of pagelinks insertions for passing to the DB
332 * Skips the titles specified by the 2-D array $existing
333 * @param $existing array
336 private function getLinkInsertions( $existing = array() ) {
338 foreach( $this->mLinks
as $ns => $dbkeys ) {
339 $diffs = isset( $existing[$ns] )
340 ?
array_diff_key( $dbkeys, $existing[$ns] )
342 foreach ( $diffs as $dbk => $id ) {
344 'pl_from' => $this->mId
,
345 'pl_namespace' => $ns,
354 * Get an array of template insertions. Like getLinkInsertions()
355 * @param $existing array
358 private function getTemplateInsertions( $existing = array() ) {
360 foreach( $this->mTemplates
as $ns => $dbkeys ) {
361 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
362 foreach ( $diffs as $dbk => $id ) {
364 'tl_from' => $this->mId
,
365 'tl_namespace' => $ns,
374 * Get an array of image insertions
375 * Skips the names specified in $existing
376 * @param $existing array
379 private function getImageInsertions( $existing = array() ) {
381 $diffs = array_diff_key( $this->mImages
, $existing );
382 foreach( $diffs as $iname => $dummy ) {
384 'il_from' => $this->mId
,
392 * Get an array of externallinks insertions. Skips the names specified in $existing
393 * @param $existing array
396 private function getExternalInsertions( $existing = array() ) {
398 $diffs = array_diff_key( $this->mExternals
, $existing );
399 foreach( $diffs as $url => $dummy ) {
400 foreach( wfMakeUrlIndexes( $url ) as $index ) {
402 'el_from' => $this->mId
,
404 'el_index' => $index,
412 * Get an array of category insertions
414 * @param $existing array mapping existing category names to sort keys. If both
415 * match a link in $this, the link will be omitted from the output
419 private function getCategoryInsertions( $existing = array() ) {
420 global $wgContLang, $wgCategoryCollation;
421 $diffs = array_diff_assoc( $this->mCategories
, $existing );
423 foreach ( $diffs as $name => $prefix ) {
424 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
425 $wgContLang->findVariantLink( $name, $nt, true );
427 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
429 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
435 # Treat custom sortkeys as a prefix, so that if multiple
436 # things are forced to sort as '*' or something, they'll
437 # sort properly in the category rather than in page_id
439 $sortkey = Collation
::singleton()->getSortKey(
440 $this->mTitle
->getCategorySortkey( $prefix ) );
443 'cl_from' => $this->mId
,
445 'cl_sortkey' => $sortkey,
446 'cl_timestamp' => $this->mDb
->timestamp(),
447 'cl_sortkey_prefix' => $prefix,
448 'cl_collation' => $wgCategoryCollation,
456 * Get an array of interlanguage link insertions
458 * @param $existing Array mapping existing language codes to titles
462 private function getInterlangInsertions( $existing = array() ) {
463 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
465 foreach( $diffs as $lang => $title ) {
467 'll_from' => $this->mId
,
476 * Get an array of page property insertions
477 * @param $existing array
480 function getPropertyInsertions( $existing = array() ) {
481 $diffs = array_diff_assoc( $this->mProperties
, $existing );
483 foreach ( $diffs as $name => $value ) {
485 'pp_page' => $this->mId
,
486 'pp_propname' => $name,
487 'pp_value' => $value,
494 * Get an array of interwiki insertions for passing to the DB
495 * Skips the titles specified by the 2-D array $existing
496 * @param $existing array
499 private function getInterwikiInsertions( $existing = array() ) {
501 foreach( $this->mInterwikis
as $prefix => $dbkeys ) {
502 $diffs = isset( $existing[$prefix] ) ?
array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
503 foreach ( $diffs as $dbk => $id ) {
505 'iwl_from' => $this->mId
,
506 'iwl_prefix' => $prefix,
515 * Given an array of existing links, returns those links which are not in $this
516 * and thus should be deleted.
517 * @param $existing array
520 private function getLinkDeletions( $existing ) {
522 foreach ( $existing as $ns => $dbkeys ) {
523 if ( isset( $this->mLinks
[$ns] ) ) {
524 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
526 $del[$ns] = $existing[$ns];
533 * Given an array of existing templates, returns those templates which are not in $this
534 * and thus should be deleted.
535 * @param $existing array
538 private function getTemplateDeletions( $existing ) {
540 foreach ( $existing as $ns => $dbkeys ) {
541 if ( isset( $this->mTemplates
[$ns] ) ) {
542 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
544 $del[$ns] = $existing[$ns];
551 * Given an array of existing images, returns those images which are not in $this
552 * and thus should be deleted.
553 * @param $existing array
556 private function getImageDeletions( $existing ) {
557 return array_diff_key( $existing, $this->mImages
);
561 * Given an array of existing external links, returns those links which are not
562 * in $this and thus should be deleted.
563 * @param $existing array
566 private function getExternalDeletions( $existing ) {
567 return array_diff_key( $existing, $this->mExternals
);
571 * Given an array of existing categories, returns those categories which are not in $this
572 * and thus should be deleted.
573 * @param $existing array
576 private function getCategoryDeletions( $existing ) {
577 return array_diff_assoc( $existing, $this->mCategories
);
581 * Given an array of existing interlanguage links, returns those links which are not
582 * in $this and thus should be deleted.
583 * @param $existing array
586 private function getInterlangDeletions( $existing ) {
587 return array_diff_assoc( $existing, $this->mInterlangs
);
591 * Get array of properties which should be deleted.
592 * @param $existing array
595 function getPropertyDeletions( $existing ) {
596 return array_diff_assoc( $existing, $this->mProperties
);
600 * Given an array of existing interwiki links, returns those links which are not in $this
601 * and thus should be deleted.
602 * @param $existing array
605 private function getInterwikiDeletions( $existing ) {
607 foreach ( $existing as $prefix => $dbkeys ) {
608 if ( isset( $this->mInterwikis
[$prefix] ) ) {
609 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
611 $del[$prefix] = $existing[$prefix];
618 * Get an array of existing links, as a 2-D array
622 private function getExistingLinks() {
623 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
624 array( 'pl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
626 foreach ( $res as $row ) {
627 if ( !isset( $arr[$row->pl_namespace
] ) ) {
628 $arr[$row->pl_namespace
] = array();
630 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
636 * Get an array of existing templates, as a 2-D array
640 private function getExistingTemplates() {
641 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
642 array( 'tl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
644 foreach ( $res as $row ) {
645 if ( !isset( $arr[$row->tl_namespace
] ) ) {
646 $arr[$row->tl_namespace
] = array();
648 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
654 * Get an array of existing images, image names in the keys
658 private function getExistingImages() {
659 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
660 array( 'il_from' => $this->mId
), __METHOD__
, $this->mOptions
);
662 foreach ( $res as $row ) {
663 $arr[$row->il_to
] = 1;
669 * Get an array of existing external links, URLs in the keys
673 private function getExistingExternals() {
674 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
675 array( 'el_from' => $this->mId
), __METHOD__
, $this->mOptions
);
677 foreach ( $res as $row ) {
678 $arr[$row->el_to
] = 1;
684 * Get an array of existing categories, with the name in the key and sort key in the value.
688 private function getExistingCategories() {
689 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
690 array( 'cl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
692 foreach ( $res as $row ) {
693 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
699 * Get an array of existing interlanguage links, with the language code in the key and the
700 * title in the value.
704 private 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.
736 private function getExistingProperties() {
737 $res = $this->mDb
->select( 'page_props', array( 'pp_propname', 'pp_value' ),
738 array( 'pp_page' => $this->mId
), __METHOD__
, $this->mOptions
);
740 foreach ( $res as $row ) {
741 $arr[$row->pp_propname
] = $row->pp_value
;
747 * Return the list of images used as generated by the parser
750 public function getImages() {
751 return $this->mImages
;
755 * Invalidate any necessary link lists related to page property changes
758 private function invalidateProperties( $changed ) {
759 global $wgPagePropLinkInvalidations;
761 foreach ( $changed as $name => $value ) {
762 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
763 $inv = $wgPagePropLinkInvalidations[$name];
764 if ( !is_array( $inv ) ) {
765 $inv = array( $inv );
767 foreach ( $inv as $table ) {
768 $update = new HTMLCacheUpdate( $this->mTitle
, $table );