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).
27 var $mId, //!< Page ID of the article linked from
28 $mTitle, //!< Title object of the article linked from
29 $mParserOutput, //!< Parser output
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 $mDb, //!< Database connection reference
38 $mOptions, //!< SELECT options to be used (array)
39 $mRecursive; //!< Whether to queue jobs for recursive updates
45 * @param $title Title of the page we're updating
46 * @param $parserOutput ParserOutput: output from a full parse of this page
47 * @param $recursive Boolean: queue jobs for recursive updates?
49 function __construct( $title, $parserOutput, $recursive = true ) {
50 global $wgAntiLockFlags;
52 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
53 $this->mOptions
= array();
55 $this->mOptions
= array( 'FOR UPDATE' );
57 $this->mDb
= wfGetDB( DB_MASTER
);
59 if ( !is_object( $title ) ) {
60 throw new MWException( "The calling convention to LinksUpdate::__construct() has changed. " .
61 "Please see WikiPage::doEditUpdates() for an invocation example.\n" );
63 $this->mTitle
= $title;
64 $this->mId
= $title->getArticleID();
66 $this->mParserOutput
= $parserOutput;
67 $this->mLinks
= $parserOutput->getLinks();
68 $this->mImages
= $parserOutput->getImages();
69 $this->mTemplates
= $parserOutput->getTemplates();
70 $this->mExternals
= $parserOutput->getExternalLinks();
71 $this->mCategories
= $parserOutput->getCategories();
72 $this->mProperties
= $parserOutput->getProperties();
73 $this->mInterwikis
= $parserOutput->getInterwikiLinks();
75 # Convert the format of the interlanguage links
76 # I didn't want to change it in the ParserOutput, because that array is passed all
77 # the way back to the skin, so either a skin API break would be required, or an
78 # inefficient back-conversion.
79 $ill = $parserOutput->getLanguageLinks();
80 $this->mInterlangs
= array();
81 foreach ( $ill as $link ) {
82 list( $key, $title ) = explode( ':', $link, 2 );
83 $this->mInterlangs
[$key] = $title;
86 foreach ( $this->mCategories
as &$sortkey ) {
87 # If the sortkey is longer then 255 bytes,
88 # it truncated by DB, and then doesn't get
89 # matched when comparing existing vs current
90 # categories, causing bug 25254.
91 # Also. substr behaves weird when given "".
92 if ( $sortkey !== '' ) {
93 $sortkey = substr( $sortkey, 0, 255 );
97 $this->mRecursive
= $recursive;
99 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
103 * Update link tables with outgoing links from an updated article
105 public function doUpdate() {
106 global $wgUseDumbLinkUpdate;
108 wfRunHooks( 'LinksUpdate', array( &$this ) );
109 if ( $wgUseDumbLinkUpdate ) {
110 $this->doDumbUpdate();
112 $this->doIncrementalUpdate();
114 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
117 protected function doIncrementalUpdate() {
118 wfProfileIn( __METHOD__
);
121 $existing = $this->getExistingLinks();
122 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
123 $this->getLinkInsertions( $existing ) );
126 $existing = $this->getExistingImages();
128 $imageDeletes = $this->getImageDeletions( $existing );
129 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
130 $this->getImageInsertions( $existing ) );
132 # Invalidate all image description pages which had links added or removed
133 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existing );
134 $this->invalidateImageDescriptions( $imageUpdates );
137 $existing = $this->getExistingExternals();
138 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
139 $this->getExternalInsertions( $existing ) );
142 $existing = $this->getExistingInterlangs();
143 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
144 $this->getInterlangInsertions( $existing ) );
146 # Inline interwiki links
147 $existing = $this->getExistingInterwikis();
148 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
149 $this->getInterwikiInsertions( $existing ) );
152 $existing = $this->getExistingTemplates();
153 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
154 $this->getTemplateInsertions( $existing ) );
157 $existing = $this->getExistingCategories();
159 $categoryDeletes = $this->getCategoryDeletions( $existing );
161 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
162 $this->getCategoryInsertions( $existing ) );
164 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
165 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
166 $categoryUpdates = $categoryInserts +
$categoryDeletes;
167 $this->invalidateCategories( $categoryUpdates );
168 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
171 $existing = $this->getExistingProperties();
173 $propertiesDeletes = $this->getPropertyDeletions( $existing );
175 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
176 $this->getPropertyInsertions( $existing ) );
178 # Invalidate the necessary pages
179 $changed = $propertiesDeletes +
array_diff_assoc( $this->mProperties
, $existing );
180 $this->invalidateProperties( $changed );
182 # Refresh links of all pages including this page
183 # This will be in a separate transaction
184 if ( $this->mRecursive
) {
185 $this->queueRecursiveJobs();
188 wfProfileOut( __METHOD__
);
192 * Link update which clears the previous entries and inserts new ones
193 * May be slower or faster depending on level of lock contention and write speed of DB
194 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
196 protected function doDumbUpdate() {
197 wfProfileIn( __METHOD__
);
199 # Refresh category pages and image description pages
200 $existing = $this->getExistingCategories();
201 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
202 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories
);
203 $categoryUpdates = $categoryInserts +
$categoryDeletes;
204 $existing = $this->getExistingImages();
205 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
207 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
208 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
209 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
210 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
211 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
212 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(),'ll_from' );
213 $this->dumbTableUpdate( 'iwlinks', $this->getInterwikiInsertions(),'iwl_from' );
214 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
216 # Update the cache of all the category pages and image description
217 # pages which were changed, and fix the category table count
218 $this->invalidateCategories( $categoryUpdates );
219 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
220 $this->invalidateImageDescriptions( $imageUpdates );
222 # Refresh links of all pages including this page
223 # This will be in a separate transaction
224 if ( $this->mRecursive
) {
225 $this->queueRecursiveJobs();
228 wfProfileOut( __METHOD__
);
231 function queueRecursiveJobs() {
232 global $wgUpdateRowsPerJob;
233 wfProfileIn( __METHOD__
);
235 $cache = $this->mTitle
->getBacklinkCache();
236 $batches = $cache->partition( 'templatelinks', $wgUpdateRowsPerJob );
238 wfProfileOut( __METHOD__
);
242 foreach ( $batches as $batch ) {
243 list( $start, $end ) = $batch;
245 'table' => 'templatelinks',
249 $jobs[] = new RefreshLinksJob2( $this->mTitle
, $params );
251 Job
::batchInsert( $jobs );
253 wfProfileOut( __METHOD__
);
257 * Invalidate the cache of a list of pages from a single namespace
259 * @param $namespace Integer
260 * @param $dbkeys Array
262 function invalidatePages( $namespace, $dbkeys ) {
263 if ( !count( $dbkeys ) ) {
268 * Determine which pages need to be updated
269 * This is necessary to prevent the job queue from smashing the DB with
270 * large numbers of concurrent invalidations of the same page
272 $now = $this->mDb
->timestamp();
274 $res = $this->mDb
->select( 'page', array( 'page_id' ),
276 'page_namespace' => $namespace,
277 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
278 'page_touched < ' . $this->mDb
->addQuotes( $now )
281 foreach ( $res as $row ) {
282 $ids[] = $row->page_id
;
284 if ( !count( $ids ) ) {
290 * We still need the page_touched condition, in case the row has changed since
291 * the non-locking select above.
293 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
295 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
296 'page_touched < ' . $this->mDb
->addQuotes( $now )
304 function invalidateCategories( $cats ) {
305 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
309 * Update all the appropriate counts in the category table.
310 * @param $added array associative array of category name => sort key
311 * @param $deleted array associative array of category name => sort key
313 function updateCategoryCounts( $added, $deleted ) {
314 $a = WikiPage
::factory( $this->mTitle
);
315 $a->updateCategoryCounts(
316 array_keys( $added ), array_keys( $deleted )
323 function invalidateImageDescriptions( $images ) {
324 $this->invalidatePages( NS_FILE
, array_keys( $images ) );
332 private function dumbTableUpdate( $table, $insertions, $fromField ) {
333 $this->mDb
->delete( $table, array( $fromField => $this->mId
), __METHOD__
);
334 if ( count( $insertions ) ) {
335 # The link array was constructed without FOR UPDATE, so there may
336 # be collisions. This may cause minor link table inconsistencies,
337 # which is better than crippling the site with lock contention.
338 $this->mDb
->insert( $table, $insertions, __METHOD__
, array( 'IGNORE' ) );
343 * Update a table by doing a delete query then an insert query
349 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
350 if ( $table == 'page_props' ) {
351 $fromField = 'pp_page';
353 $fromField = "{$prefix}_from";
355 $where = array( $fromField => $this->mId
);
356 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ||
$table == 'iwlinks' ) {
357 if ( $table == 'iwlinks' ) {
358 $baseKey = 'iwl_prefix';
360 $baseKey = "{$prefix}_namespace";
362 $clause = $this->mDb
->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
369 if ( $table == 'langlinks' ) {
370 $toField = 'll_lang';
371 } elseif ( $table == 'page_props' ) {
372 $toField = 'pp_propname';
374 $toField = $prefix . '_to';
376 if ( count( $deletions ) ) {
377 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
383 $this->mDb
->delete( $table, $where, __METHOD__
);
385 if ( count( $insertions ) ) {
386 $this->mDb
->insert( $table, $insertions, __METHOD__
, 'IGNORE' );
391 * Get an array of pagelinks insertions for passing to the DB
392 * Skips the titles specified by the 2-D array $existing
393 * @param $existing array
396 private function getLinkInsertions( $existing = array() ) {
398 foreach( $this->mLinks
as $ns => $dbkeys ) {
399 $diffs = isset( $existing[$ns] )
400 ?
array_diff_key( $dbkeys, $existing[$ns] )
402 foreach ( $diffs as $dbk => $id ) {
404 'pl_from' => $this->mId
,
405 'pl_namespace' => $ns,
414 * Get an array of template insertions. Like getLinkInsertions()
415 * @param $existing array
418 private function getTemplateInsertions( $existing = array() ) {
420 foreach( $this->mTemplates
as $ns => $dbkeys ) {
421 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
422 foreach ( $diffs as $dbk => $id ) {
424 'tl_from' => $this->mId
,
425 'tl_namespace' => $ns,
434 * Get an array of image insertions
435 * Skips the names specified in $existing
436 * @param $existing array
439 private function getImageInsertions( $existing = array() ) {
441 $diffs = array_diff_key( $this->mImages
, $existing );
442 foreach( $diffs as $iname => $dummy ) {
444 'il_from' => $this->mId
,
452 * Get an array of externallinks insertions. Skips the names specified in $existing
453 * @param $existing array
456 private function getExternalInsertions( $existing = array() ) {
458 $diffs = array_diff_key( $this->mExternals
, $existing );
459 foreach( $diffs as $url => $dummy ) {
460 foreach( wfMakeUrlIndexes( $url ) as $index ) {
462 'el_from' => $this->mId
,
464 'el_index' => $index,
472 * Get an array of category insertions
474 * @param $existing array mapping existing category names to sort keys. If both
475 * match a link in $this, the link will be omitted from the output
479 private function getCategoryInsertions( $existing = array() ) {
480 global $wgContLang, $wgCategoryCollation;
481 $diffs = array_diff_assoc( $this->mCategories
, $existing );
483 foreach ( $diffs as $name => $prefix ) {
484 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
485 $wgContLang->findVariantLink( $name, $nt, true );
487 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
489 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
495 # Treat custom sortkeys as a prefix, so that if multiple
496 # things are forced to sort as '*' or something, they'll
497 # sort properly in the category rather than in page_id
499 $sortkey = Collation
::singleton()->getSortKey(
500 $this->mTitle
->getCategorySortkey( $prefix ) );
503 'cl_from' => $this->mId
,
505 'cl_sortkey' => $sortkey,
506 'cl_timestamp' => $this->mDb
->timestamp(),
507 'cl_sortkey_prefix' => $prefix,
508 'cl_collation' => $wgCategoryCollation,
516 * Get an array of interlanguage link insertions
518 * @param $existing Array mapping existing language codes to titles
522 private function getInterlangInsertions( $existing = array() ) {
523 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
525 foreach( $diffs as $lang => $title ) {
527 'll_from' => $this->mId
,
536 * Get an array of page property insertions
537 * @param $existing array
540 function getPropertyInsertions( $existing = array() ) {
541 $diffs = array_diff_assoc( $this->mProperties
, $existing );
543 foreach ( $diffs as $name => $value ) {
545 'pp_page' => $this->mId
,
546 'pp_propname' => $name,
547 'pp_value' => $value,
554 * Get an array of interwiki insertions for passing to the DB
555 * Skips the titles specified by the 2-D array $existing
556 * @param $existing array
559 private function getInterwikiInsertions( $existing = array() ) {
561 foreach( $this->mInterwikis
as $prefix => $dbkeys ) {
562 $diffs = isset( $existing[$prefix] ) ?
array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
563 foreach ( $diffs as $dbk => $id ) {
565 'iwl_from' => $this->mId
,
566 'iwl_prefix' => $prefix,
575 * Given an array of existing links, returns those links which are not in $this
576 * and thus should be deleted.
577 * @param $existing array
580 private function getLinkDeletions( $existing ) {
582 foreach ( $existing as $ns => $dbkeys ) {
583 if ( isset( $this->mLinks
[$ns] ) ) {
584 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
586 $del[$ns] = $existing[$ns];
593 * Given an array of existing templates, returns those templates which are not in $this
594 * and thus should be deleted.
595 * @param $existing array
598 private function getTemplateDeletions( $existing ) {
600 foreach ( $existing as $ns => $dbkeys ) {
601 if ( isset( $this->mTemplates
[$ns] ) ) {
602 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
604 $del[$ns] = $existing[$ns];
611 * Given an array of existing images, returns those images which are not in $this
612 * and thus should be deleted.
613 * @param $existing array
616 private function getImageDeletions( $existing ) {
617 return array_diff_key( $existing, $this->mImages
);
621 * Given an array of existing external links, returns those links which are not
622 * in $this and thus should be deleted.
623 * @param $existing array
626 private function getExternalDeletions( $existing ) {
627 return array_diff_key( $existing, $this->mExternals
);
631 * Given an array of existing categories, returns those categories which are not in $this
632 * and thus should be deleted.
633 * @param $existing array
636 private function getCategoryDeletions( $existing ) {
637 return array_diff_assoc( $existing, $this->mCategories
);
641 * Given an array of existing interlanguage links, returns those links which are not
642 * in $this and thus should be deleted.
643 * @param $existing array
646 private function getInterlangDeletions( $existing ) {
647 return array_diff_assoc( $existing, $this->mInterlangs
);
651 * Get array of properties which should be deleted.
652 * @param $existing array
655 function getPropertyDeletions( $existing ) {
656 return array_diff_assoc( $existing, $this->mProperties
);
660 * Given an array of existing interwiki links, returns those links which are not in $this
661 * and thus should be deleted.
662 * @param $existing array
665 private function getInterwikiDeletions( $existing ) {
667 foreach ( $existing as $prefix => $dbkeys ) {
668 if ( isset( $this->mInterwikis
[$prefix] ) ) {
669 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
671 $del[$prefix] = $existing[$prefix];
678 * Get an array of existing links, as a 2-D array
682 private function getExistingLinks() {
683 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
684 array( 'pl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
686 foreach ( $res as $row ) {
687 if ( !isset( $arr[$row->pl_namespace
] ) ) {
688 $arr[$row->pl_namespace
] = array();
690 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
696 * Get an array of existing templates, as a 2-D array
700 private function getExistingTemplates() {
701 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
702 array( 'tl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
704 foreach ( $res as $row ) {
705 if ( !isset( $arr[$row->tl_namespace
] ) ) {
706 $arr[$row->tl_namespace
] = array();
708 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
714 * Get an array of existing images, image names in the keys
718 private function getExistingImages() {
719 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
720 array( 'il_from' => $this->mId
), __METHOD__
, $this->mOptions
);
722 foreach ( $res as $row ) {
723 $arr[$row->il_to
] = 1;
729 * Get an array of existing external links, URLs in the keys
733 private function getExistingExternals() {
734 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
735 array( 'el_from' => $this->mId
), __METHOD__
, $this->mOptions
);
737 foreach ( $res as $row ) {
738 $arr[$row->el_to
] = 1;
744 * Get an array of existing categories, with the name in the key and sort key in the value.
748 private function getExistingCategories() {
749 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
750 array( 'cl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
752 foreach ( $res as $row ) {
753 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
759 * Get an array of existing interlanguage links, with the language code in the key and the
760 * title in the value.
764 private function getExistingInterlangs() {
765 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
766 array( 'll_from' => $this->mId
), __METHOD__
, $this->mOptions
);
768 foreach ( $res as $row ) {
769 $arr[$row->ll_lang
] = $row->ll_title
;
775 * Get an array of existing inline interwiki links, as a 2-D array
776 * @return array (prefix => array(dbkey => 1))
778 protected function getExistingInterwikis() {
779 $res = $this->mDb
->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
780 array( 'iwl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
782 foreach ( $res as $row ) {
783 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
784 $arr[$row->iwl_prefix
] = array();
786 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
792 * Get an array of existing categories, with the name in the key and sort key in the value.
796 private function getExistingProperties() {
797 $res = $this->mDb
->select( 'page_props', array( 'pp_propname', 'pp_value' ),
798 array( 'pp_page' => $this->mId
), __METHOD__
, $this->mOptions
);
800 foreach ( $res as $row ) {
801 $arr[$row->pp_propname
] = $row->pp_value
;
807 * Return the title object of the page being updated
810 public function getTitle() {
811 return $this->mTitle
;
815 * Returns parser output
817 * @return ParserOutput
819 public function getParserOutput() {
820 return $this->mParserOutput
;
824 * Return the list of images used as generated by the parser
827 public function getImages() {
828 return $this->mImages
;
832 * Invalidate any necessary link lists related to page property changes
835 private function invalidateProperties( $changed ) {
836 global $wgPagePropLinkInvalidations;
838 foreach ( $changed as $name => $value ) {
839 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
840 $inv = $wgPagePropLinkInvalidations[$name];
841 if ( !is_array( $inv ) ) {
842 $inv = array( $inv );
844 foreach ( $inv as $table ) {
845 $update = new HTMLCacheUpdate( $this->mTitle
, $table );