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 $mId, //!< Page ID of the article linked from
28 $mTitle, //!< Title object of the article linked from
29 $mParserOutput, //!< Whether to queue jobs for recursive update
30 $mLinks, //!< Map of title strings to IDs for the links in the document
31 $mImages, //!< DB keys of the images used, in the array key only
32 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
33 $mExternals, //!< URLs of external links, array key only
34 $mCategories, //!< Map of category names to sort keys
35 $mInterlangs, //!< Map of language codes to titles
36 $mProperties, //!< Map of arbitrary name to value
37 $mRecursive; //!< Whether to queue jobs for recursive updates
43 * @param $title Title of the page we're updating
44 * @param $parserOutput ParserOutput: output from a full parse of this page
45 * @param $recursive Boolean: queue jobs for recursive updates?
47 function __construct( $title, $parserOutput, $recursive = true ) {
48 parent
::__construct( );
50 if ( !is_object( $title ) ) {
51 throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
52 "Please see Article::editUpdates() for an invocation example.\n" );
55 if ( !is_object( $title ) ) {
56 throw new MWException( "The calling convention to LinksUpdate::__construct() has changed. " .
57 "Please see WikiPage::doEditUpdates() for an invocation example.\n" );
59 $this->mTitle
= $title;
60 $this->mId
= $title->getArticleID();
62 $this->mParserOutput
= $parserOutput;
64 $this->mLinks
= $parserOutput->getLinks();
65 $this->mImages
= $parserOutput->getImages();
66 $this->mTemplates
= $parserOutput->getTemplates();
67 $this->mExternals
= $parserOutput->getExternalLinks();
68 $this->mCategories
= $parserOutput->getCategories();
69 $this->mProperties
= $parserOutput->getProperties();
70 $this->mInterwikis
= $parserOutput->getInterwikiLinks();
72 # Convert the format of the interlanguage links
73 # I didn't want to change it in the ParserOutput, because that array is passed all
74 # the way back to the skin, so either a skin API break would be required, or an
75 # inefficient back-conversion.
76 $ill = $parserOutput->getLanguageLinks();
77 $this->mInterlangs
= array();
78 foreach ( $ill as $link ) {
79 list( $key, $title ) = explode( ':', $link, 2 );
80 $this->mInterlangs
[$key] = $title;
83 foreach ( $this->mCategories
as &$sortkey ) {
84 # If the sortkey is longer then 255 bytes,
85 # it truncated by DB, and then doesn't get
86 # matched when comparing existing vs current
87 # categories, causing bug 25254.
88 # Also. substr behaves weird when given "".
89 if ( $sortkey !== '' ) {
90 $sortkey = substr( $sortkey, 0, 255 );
94 $this->mRecursive
= $recursive;
96 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
100 * Update link tables with outgoing links from an updated article
102 public function doUpdate() {
103 global $wgUseDumbLinkUpdate;
105 wfRunHooks( 'LinksUpdate', array( &$this ) );
106 if ( $wgUseDumbLinkUpdate ) {
107 $this->doDumbUpdate();
109 $this->doIncrementalUpdate();
111 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
114 protected function doIncrementalUpdate() {
115 wfProfileIn( __METHOD__
);
118 $existing = $this->getExistingLinks();
119 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
120 $this->getLinkInsertions( $existing ) );
123 $existing = $this->getExistingImages();
125 $imageDeletes = $this->getImageDeletions( $existing );
126 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
127 $this->getImageInsertions( $existing ) );
129 # Invalidate all image description pages which had links added or removed
130 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existing );
131 $this->invalidateImageDescriptions( $imageUpdates );
134 $existing = $this->getExistingExternals();
135 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
136 $this->getExternalInsertions( $existing ) );
139 $existing = $this->getExistingInterlangs();
140 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
141 $this->getInterlangInsertions( $existing ) );
143 # Inline interwiki links
144 $existing = $this->getExistingInterwikis();
145 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
146 $this->getInterwikiInsertions( $existing ) );
149 $existing = $this->getExistingTemplates();
150 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
151 $this->getTemplateInsertions( $existing ) );
154 $existing = $this->getExistingCategories();
156 $categoryDeletes = $this->getCategoryDeletions( $existing );
158 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
159 $this->getCategoryInsertions( $existing ) );
161 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
162 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
163 $categoryUpdates = $categoryInserts +
$categoryDeletes;
164 $this->invalidateCategories( $categoryUpdates );
165 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
168 $existing = $this->getExistingProperties();
170 $propertiesDeletes = $this->getPropertyDeletions( $existing );
172 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
173 $this->getPropertyInsertions( $existing ) );
175 # Invalidate the necessary pages
176 $changed = $propertiesDeletes +
array_diff_assoc( $this->mProperties
, $existing );
177 $this->invalidateProperties( $changed );
179 # Refresh links of all pages including this page
180 # This will be in a separate transaction
181 if ( $this->mRecursive
) {
182 $this->queueRecursiveJobs();
185 wfProfileOut( __METHOD__
);
189 * Link update which clears the previous entries and inserts new ones
190 * May be slower or faster depending on level of lock contention and write speed of DB
191 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
193 protected function doDumbUpdate() {
194 wfProfileIn( __METHOD__
);
196 # Refresh category pages and image description pages
197 $existing = $this->getExistingCategories();
198 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
199 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories
);
200 $categoryUpdates = $categoryInserts +
$categoryDeletes;
201 $existing = $this->getExistingImages();
202 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
204 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
205 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
206 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
207 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
208 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
209 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(),'ll_from' );
210 $this->dumbTableUpdate( 'iwlinks', $this->getInterwikiInsertions(),'iwl_from' );
211 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
213 # Update the cache of all the category pages and image description
214 # pages which were changed, and fix the category table count
215 $this->invalidateCategories( $categoryUpdates );
216 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
217 $this->invalidateImageDescriptions( $imageUpdates );
219 # Refresh links of all pages including this page
220 # This will be in a separate transaction
221 if ( $this->mRecursive
) {
222 $this->queueRecursiveJobs();
225 wfProfileOut( __METHOD__
);
228 function queueRecursiveJobs() {
229 global $wgUpdateRowsPerJob;
230 wfProfileIn( __METHOD__
);
232 $cache = $this->mTitle
->getBacklinkCache();
233 $batches = $cache->partition( 'templatelinks', $wgUpdateRowsPerJob );
235 wfProfileOut( __METHOD__
);
239 foreach ( $batches as $batch ) {
240 list( $start, $end ) = $batch;
242 'table' => 'templatelinks',
246 $jobs[] = new RefreshLinksJob2( $this->mTitle
, $params );
248 Job
::batchInsert( $jobs );
250 wfProfileOut( __METHOD__
);
256 function invalidateCategories( $cats ) {
257 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
261 * Update all the appropriate counts in the category table.
262 * @param $added array associative array of category name => sort key
263 * @param $deleted array associative array of category name => sort key
265 function updateCategoryCounts( $added, $deleted ) {
266 $a = WikiPage
::factory( $this->mTitle
);
267 $a->updateCategoryCounts(
268 array_keys( $added ), array_keys( $deleted )
275 function invalidateImageDescriptions( $images ) {
276 $this->invalidatePages( NS_FILE
, array_keys( $images ) );
284 private function dumbTableUpdate( $table, $insertions, $fromField ) {
285 $this->mDb
->delete( $table, array( $fromField => $this->mId
), __METHOD__
);
286 if ( count( $insertions ) ) {
287 # The link array was constructed without FOR UPDATE, so there may
288 # be collisions. This may cause minor link table inconsistencies,
289 # which is better than crippling the site with lock contention.
290 $this->mDb
->insert( $table, $insertions, __METHOD__
, array( 'IGNORE' ) );
295 * Update a table by doing a delete query then an insert query
301 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
302 if ( $table == 'page_props' ) {
303 $fromField = 'pp_page';
305 $fromField = "{$prefix}_from";
307 $where = array( $fromField => $this->mId
);
308 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ||
$table == 'iwlinks' ) {
309 if ( $table == 'iwlinks' ) {
310 $baseKey = 'iwl_prefix';
312 $baseKey = "{$prefix}_namespace";
314 $clause = $this->mDb
->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
321 if ( $table == 'langlinks' ) {
322 $toField = 'll_lang';
323 } elseif ( $table == 'page_props' ) {
324 $toField = 'pp_propname';
326 $toField = $prefix . '_to';
328 if ( count( $deletions ) ) {
329 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
335 $this->mDb
->delete( $table, $where, __METHOD__
);
337 if ( count( $insertions ) ) {
338 $this->mDb
->insert( $table, $insertions, __METHOD__
, 'IGNORE' );
343 * Get an array of pagelinks insertions for passing to the DB
344 * Skips the titles specified by the 2-D array $existing
345 * @param $existing array
348 private function getLinkInsertions( $existing = array() ) {
350 foreach( $this->mLinks
as $ns => $dbkeys ) {
351 $diffs = isset( $existing[$ns] )
352 ?
array_diff_key( $dbkeys, $existing[$ns] )
354 foreach ( $diffs as $dbk => $id ) {
356 'pl_from' => $this->mId
,
357 'pl_namespace' => $ns,
366 * Get an array of template insertions. Like getLinkInsertions()
367 * @param $existing array
370 private 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
388 * @param $existing array
391 private function getImageInsertions( $existing = array() ) {
393 $diffs = array_diff_key( $this->mImages
, $existing );
394 foreach( $diffs as $iname => $dummy ) {
396 'il_from' => $this->mId
,
404 * Get an array of externallinks insertions. Skips the names specified in $existing
405 * @param $existing array
408 private function getExternalInsertions( $existing = array() ) {
410 $diffs = array_diff_key( $this->mExternals
, $existing );
411 foreach( $diffs as $url => $dummy ) {
412 foreach( wfMakeUrlIndexes( $url ) as $index ) {
414 'el_from' => $this->mId
,
416 'el_index' => $index,
424 * Get an array of category insertions
426 * @param $existing array mapping existing category names to sort keys. If both
427 * match a link in $this, the link will be omitted from the output
431 private function getCategoryInsertions( $existing = array() ) {
432 global $wgContLang, $wgCategoryCollation;
433 $diffs = array_diff_assoc( $this->mCategories
, $existing );
435 foreach ( $diffs as $name => $prefix ) {
436 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
437 $wgContLang->findVariantLink( $name, $nt, true );
439 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
441 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
447 # Treat custom sortkeys as a prefix, so that if multiple
448 # things are forced to sort as '*' or something, they'll
449 # sort properly in the category rather than in page_id
451 $sortkey = Collation
::singleton()->getSortKey(
452 $this->mTitle
->getCategorySortkey( $prefix ) );
455 'cl_from' => $this->mId
,
457 'cl_sortkey' => $sortkey,
458 'cl_timestamp' => $this->mDb
->timestamp(),
459 'cl_sortkey_prefix' => $prefix,
460 'cl_collation' => $wgCategoryCollation,
468 * Get an array of interlanguage link insertions
470 * @param $existing Array mapping existing language codes to titles
474 private function getInterlangInsertions( $existing = array() ) {
475 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
477 foreach( $diffs as $lang => $title ) {
479 'll_from' => $this->mId
,
488 * Get an array of page property insertions
489 * @param $existing array
492 function getPropertyInsertions( $existing = array() ) {
493 $diffs = array_diff_assoc( $this->mProperties
, $existing );
495 foreach ( $diffs as $name => $value ) {
497 'pp_page' => $this->mId
,
498 'pp_propname' => $name,
499 'pp_value' => $value,
506 * Get an array of interwiki insertions for passing to the DB
507 * Skips the titles specified by the 2-D array $existing
508 * @param $existing array
511 private function getInterwikiInsertions( $existing = array() ) {
513 foreach( $this->mInterwikis
as $prefix => $dbkeys ) {
514 $diffs = isset( $existing[$prefix] ) ?
array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
515 foreach ( $diffs as $dbk => $id ) {
517 'iwl_from' => $this->mId
,
518 'iwl_prefix' => $prefix,
527 * Given an array of existing links, returns those links which are not in $this
528 * and thus should be deleted.
529 * @param $existing array
532 private function getLinkDeletions( $existing ) {
534 foreach ( $existing as $ns => $dbkeys ) {
535 if ( isset( $this->mLinks
[$ns] ) ) {
536 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
538 $del[$ns] = $existing[$ns];
545 * Given an array of existing templates, returns those templates which are not in $this
546 * and thus should be deleted.
547 * @param $existing array
550 private 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.
565 * @param $existing array
568 private function getImageDeletions( $existing ) {
569 return array_diff_key( $existing, $this->mImages
);
573 * Given an array of existing external links, returns those links which are not
574 * in $this and thus should be deleted.
575 * @param $existing array
578 private function getExternalDeletions( $existing ) {
579 return array_diff_key( $existing, $this->mExternals
);
583 * Given an array of existing categories, returns those categories which are not in $this
584 * and thus should be deleted.
585 * @param $existing array
588 private function getCategoryDeletions( $existing ) {
589 return array_diff_assoc( $existing, $this->mCategories
);
593 * Given an array of existing interlanguage links, returns those links which are not
594 * in $this and thus should be deleted.
595 * @param $existing array
598 private function getInterlangDeletions( $existing ) {
599 return array_diff_assoc( $existing, $this->mInterlangs
);
603 * Get array of properties which should be deleted.
604 * @param $existing array
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.
614 * @param $existing array
617 private function getInterwikiDeletions( $existing ) {
619 foreach ( $existing as $prefix => $dbkeys ) {
620 if ( isset( $this->mInterwikis
[$prefix] ) ) {
621 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
623 $del[$prefix] = $existing[$prefix];
630 * Get an array of existing links, as a 2-D array
634 private function getExistingLinks() {
635 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
636 array( 'pl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
638 foreach ( $res as $row ) {
639 if ( !isset( $arr[$row->pl_namespace
] ) ) {
640 $arr[$row->pl_namespace
] = array();
642 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
648 * Get an array of existing templates, as a 2-D array
652 private function getExistingTemplates() {
653 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
654 array( 'tl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
656 foreach ( $res as $row ) {
657 if ( !isset( $arr[$row->tl_namespace
] ) ) {
658 $arr[$row->tl_namespace
] = array();
660 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
666 * Get an array of existing images, image names in the keys
670 private function getExistingImages() {
671 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
672 array( 'il_from' => $this->mId
), __METHOD__
, $this->mOptions
);
674 foreach ( $res as $row ) {
675 $arr[$row->il_to
] = 1;
681 * Get an array of existing external links, URLs in the keys
685 private function getExistingExternals() {
686 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
687 array( 'el_from' => $this->mId
), __METHOD__
, $this->mOptions
);
689 foreach ( $res as $row ) {
690 $arr[$row->el_to
] = 1;
696 * Get an array of existing categories, with the name in the key and sort key in the value.
700 private function getExistingCategories() {
701 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
702 array( 'cl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
704 foreach ( $res as $row ) {
705 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
711 * Get an array of existing interlanguage links, with the language code in the key and the
712 * title in the value.
716 private function getExistingInterlangs() {
717 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
718 array( 'll_from' => $this->mId
), __METHOD__
, $this->mOptions
);
720 foreach ( $res as $row ) {
721 $arr[$row->ll_lang
] = $row->ll_title
;
727 * Get an array of existing inline interwiki links, as a 2-D array
728 * @return array (prefix => array(dbkey => 1))
730 protected function getExistingInterwikis() {
731 $res = $this->mDb
->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
732 array( 'iwl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
734 foreach ( $res as $row ) {
735 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
736 $arr[$row->iwl_prefix
] = array();
738 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
744 * Get an array of existing categories, with the name in the key and sort key in the value.
748 private function getExistingProperties() {
749 $res = $this->mDb
->select( 'page_props', array( 'pp_propname', 'pp_value' ),
750 array( 'pp_page' => $this->mId
), __METHOD__
, $this->mOptions
);
752 foreach ( $res as $row ) {
753 $arr[$row->pp_propname
] = $row->pp_value
;
759 * Return the title object of the page being updated
762 public function getTitle() {
763 return $this->mTitle
;
767 * Returns parser output
769 * @return ParserOutput
771 public function getParserOutput() {
772 return $this->mParserOutput
;
776 * Return the list of images used as generated by the parser
779 public function getImages() {
780 return $this->mImages
;
784 * Invalidate any necessary link lists related to page property changes
787 private function invalidateProperties( $changed ) {
788 global $wgPagePropLinkInvalidations;
790 foreach ( $changed as $name => $value ) {
791 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
792 $inv = $wgPagePropLinkInvalidations[$name];
793 if ( !is_array( $inv ) ) {
794 $inv = array( $inv );
796 foreach ( $inv as $table ) {
797 $update = new HTMLCacheUpdate( $this->mTitle
, $table );