3 * Updater for link tracking tables after a page edit.
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
24 * See docs/deferred.txt
26 * @todo document (e.g. one-sentence top-level class description).
33 var $mId, //!< Page ID of the article linked from
34 $mTitle, //!< Title object of the article linked from
35 $mParserOutput, //!< Parser output
36 $mLinks, //!< Map of title strings to IDs for the links in the document
37 $mImages, //!< DB keys of the images used, in the array key only
38 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
39 $mExternals, //!< URLs of external links, array key only
40 $mCategories, //!< Map of category names to sort keys
41 $mInterlangs, //!< Map of language codes to titles
42 $mProperties, //!< Map of arbitrary name to value
43 $mDb, //!< Database connection reference
44 $mOptions, //!< SELECT options to be used (array)
45 $mRecursive; //!< Whether to queue jobs for recursive updates
51 * @param $title Title of the page we're updating
52 * @param $parserOutput ParserOutput: output from a full parse of this page
53 * @param $recursive Boolean: queue jobs for recursive updates?
55 function __construct( $title, $parserOutput, $recursive = true ) {
56 global $wgAntiLockFlags;
58 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
59 $this->mOptions
= array();
61 $this->mOptions
= array( 'FOR UPDATE' );
63 $this->mDb
= wfGetDB( DB_MASTER
);
65 if ( !is_object( $title ) ) {
66 throw new MWException( "The calling convention to LinksUpdate::__construct() has changed. " .
67 "Please see WikiPage::doEditUpdates() for an invocation example.\n" );
69 $this->mTitle
= $title;
70 $this->mId
= $title->getArticleID();
72 $this->mParserOutput
= $parserOutput;
73 $this->mLinks
= $parserOutput->getLinks();
74 $this->mImages
= $parserOutput->getImages();
75 $this->mTemplates
= $parserOutput->getTemplates();
76 $this->mExternals
= $parserOutput->getExternalLinks();
77 $this->mCategories
= $parserOutput->getCategories();
78 $this->mProperties
= $parserOutput->getProperties();
79 $this->mInterwikis
= $parserOutput->getInterwikiLinks();
81 # Convert the format of the interlanguage links
82 # I didn't want to change it in the ParserOutput, because that array is passed all
83 # the way back to the skin, so either a skin API break would be required, or an
84 # inefficient back-conversion.
85 $ill = $parserOutput->getLanguageLinks();
86 $this->mInterlangs
= array();
87 foreach ( $ill as $link ) {
88 list( $key, $title ) = explode( ':', $link, 2 );
89 $this->mInterlangs
[$key] = $title;
92 foreach ( $this->mCategories
as &$sortkey ) {
93 # If the sortkey is longer then 255 bytes,
94 # it truncated by DB, and then doesn't get
95 # matched when comparing existing vs current
96 # categories, causing bug 25254.
97 # Also. substr behaves weird when given "".
98 if ( $sortkey !== '' ) {
99 $sortkey = substr( $sortkey, 0, 255 );
103 $this->mRecursive
= $recursive;
105 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
109 * Update link tables with outgoing links from an updated article
111 public function doUpdate() {
112 global $wgUseDumbLinkUpdate;
114 wfRunHooks( 'LinksUpdate', array( &$this ) );
115 if ( $wgUseDumbLinkUpdate ) {
116 $this->doDumbUpdate();
118 $this->doIncrementalUpdate();
120 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
123 protected function doIncrementalUpdate() {
124 wfProfileIn( __METHOD__
);
127 $existing = $this->getExistingLinks();
128 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
129 $this->getLinkInsertions( $existing ) );
132 $existing = $this->getExistingImages();
134 $imageDeletes = $this->getImageDeletions( $existing );
135 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
136 $this->getImageInsertions( $existing ) );
138 # Invalidate all image description pages which had links added or removed
139 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existing );
140 $this->invalidateImageDescriptions( $imageUpdates );
143 $existing = $this->getExistingExternals();
144 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
145 $this->getExternalInsertions( $existing ) );
148 $existing = $this->getExistingInterlangs();
149 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
150 $this->getInterlangInsertions( $existing ) );
152 # Inline interwiki links
153 $existing = $this->getExistingInterwikis();
154 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
155 $this->getInterwikiInsertions( $existing ) );
158 $existing = $this->getExistingTemplates();
159 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
160 $this->getTemplateInsertions( $existing ) );
163 $existing = $this->getExistingCategories();
165 $categoryDeletes = $this->getCategoryDeletions( $existing );
167 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
168 $this->getCategoryInsertions( $existing ) );
170 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
171 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
172 $categoryUpdates = $categoryInserts +
$categoryDeletes;
173 $this->invalidateCategories( $categoryUpdates );
174 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
177 $existing = $this->getExistingProperties();
179 $propertiesDeletes = $this->getPropertyDeletions( $existing );
181 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
182 $this->getPropertyInsertions( $existing ) );
184 # Invalidate the necessary pages
185 $changed = $propertiesDeletes +
array_diff_assoc( $this->mProperties
, $existing );
186 $this->invalidateProperties( $changed );
188 # Refresh links of all pages including this page
189 # This will be in a separate transaction
190 if ( $this->mRecursive
) {
191 $this->queueRecursiveJobs();
194 wfProfileOut( __METHOD__
);
198 * Link update which clears the previous entries and inserts new ones
199 * May be slower or faster depending on level of lock contention and write speed of DB
200 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
202 protected function doDumbUpdate() {
203 wfProfileIn( __METHOD__
);
205 # Refresh category pages and image description pages
206 $existing = $this->getExistingCategories();
207 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
208 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories
);
209 $categoryUpdates = $categoryInserts +
$categoryDeletes;
210 $existing = $this->getExistingImages();
211 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
213 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
214 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
215 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
216 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
217 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
218 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(),'ll_from' );
219 $this->dumbTableUpdate( 'iwlinks', $this->getInterwikiInsertions(),'iwl_from' );
220 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
222 # Update the cache of all the category pages and image description
223 # pages which were changed, and fix the category table count
224 $this->invalidateCategories( $categoryUpdates );
225 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
226 $this->invalidateImageDescriptions( $imageUpdates );
228 # Refresh links of all pages including this page
229 # This will be in a separate transaction
230 if ( $this->mRecursive
) {
231 $this->queueRecursiveJobs();
234 wfProfileOut( __METHOD__
);
237 function queueRecursiveJobs() {
238 global $wgUpdateRowsPerJob;
239 wfProfileIn( __METHOD__
);
241 $cache = $this->mTitle
->getBacklinkCache();
242 $batches = $cache->partition( 'templatelinks', $wgUpdateRowsPerJob );
244 wfProfileOut( __METHOD__
);
248 foreach ( $batches as $batch ) {
249 list( $start, $end ) = $batch;
251 'table' => 'templatelinks',
255 $jobs[] = new RefreshLinksJob2( $this->mTitle
, $params );
257 Job
::batchInsert( $jobs );
259 wfProfileOut( __METHOD__
);
263 * Invalidate the cache of a list of pages from a single namespace
265 * @param $namespace Integer
266 * @param $dbkeys Array
268 function invalidatePages( $namespace, $dbkeys ) {
269 if ( !count( $dbkeys ) ) {
274 * Determine which pages need to be updated
275 * This is necessary to prevent the job queue from smashing the DB with
276 * large numbers of concurrent invalidations of the same page
278 $now = $this->mDb
->timestamp();
280 $res = $this->mDb
->select( 'page', array( 'page_id' ),
282 'page_namespace' => $namespace,
283 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
284 'page_touched < ' . $this->mDb
->addQuotes( $now )
287 foreach ( $res as $row ) {
288 $ids[] = $row->page_id
;
290 if ( !count( $ids ) ) {
296 * We still need the page_touched condition, in case the row has changed since
297 * the non-locking select above.
299 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
301 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
302 'page_touched < ' . $this->mDb
->addQuotes( $now )
310 function invalidateCategories( $cats ) {
311 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
315 * Update all the appropriate counts in the category table.
316 * @param $added array associative array of category name => sort key
317 * @param $deleted array associative array of category name => sort key
319 function updateCategoryCounts( $added, $deleted ) {
320 $a = WikiPage
::factory( $this->mTitle
);
321 $a->updateCategoryCounts(
322 array_keys( $added ), array_keys( $deleted )
329 function invalidateImageDescriptions( $images ) {
330 $this->invalidatePages( NS_FILE
, array_keys( $images ) );
338 private function dumbTableUpdate( $table, $insertions, $fromField ) {
339 $this->mDb
->delete( $table, array( $fromField => $this->mId
), __METHOD__
);
340 if ( count( $insertions ) ) {
341 # The link array was constructed without FOR UPDATE, so there may
342 # be collisions. This may cause minor link table inconsistencies,
343 # which is better than crippling the site with lock contention.
344 $this->mDb
->insert( $table, $insertions, __METHOD__
, array( 'IGNORE' ) );
349 * Update a table by doing a delete query then an insert query
355 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
356 if ( $table == 'page_props' ) {
357 $fromField = 'pp_page';
359 $fromField = "{$prefix}_from";
361 $where = array( $fromField => $this->mId
);
362 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ||
$table == 'iwlinks' ) {
363 if ( $table == 'iwlinks' ) {
364 $baseKey = 'iwl_prefix';
366 $baseKey = "{$prefix}_namespace";
368 $clause = $this->mDb
->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
375 if ( $table == 'langlinks' ) {
376 $toField = 'll_lang';
377 } elseif ( $table == 'page_props' ) {
378 $toField = 'pp_propname';
380 $toField = $prefix . '_to';
382 if ( count( $deletions ) ) {
383 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
389 $this->mDb
->delete( $table, $where, __METHOD__
);
391 if ( count( $insertions ) ) {
392 $this->mDb
->insert( $table, $insertions, __METHOD__
, 'IGNORE' );
397 * Get an array of pagelinks insertions for passing to the DB
398 * Skips the titles specified by the 2-D array $existing
399 * @param $existing array
402 private function getLinkInsertions( $existing = array() ) {
404 foreach( $this->mLinks
as $ns => $dbkeys ) {
405 $diffs = isset( $existing[$ns] )
406 ?
array_diff_key( $dbkeys, $existing[$ns] )
408 foreach ( $diffs as $dbk => $id ) {
410 'pl_from' => $this->mId
,
411 'pl_namespace' => $ns,
420 * Get an array of template insertions. Like getLinkInsertions()
421 * @param $existing array
424 private function getTemplateInsertions( $existing = array() ) {
426 foreach( $this->mTemplates
as $ns => $dbkeys ) {
427 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
428 foreach ( $diffs as $dbk => $id ) {
430 'tl_from' => $this->mId
,
431 'tl_namespace' => $ns,
440 * Get an array of image insertions
441 * Skips the names specified in $existing
442 * @param $existing array
445 private function getImageInsertions( $existing = array() ) {
447 $diffs = array_diff_key( $this->mImages
, $existing );
448 foreach( $diffs as $iname => $dummy ) {
450 'il_from' => $this->mId
,
458 * Get an array of externallinks insertions. Skips the names specified in $existing
459 * @param $existing array
462 private function getExternalInsertions( $existing = array() ) {
464 $diffs = array_diff_key( $this->mExternals
, $existing );
465 foreach( $diffs as $url => $dummy ) {
466 foreach( wfMakeUrlIndexes( $url ) as $index ) {
468 'el_from' => $this->mId
,
470 'el_index' => $index,
478 * Get an array of category insertions
480 * @param $existing array mapping existing category names to sort keys. If both
481 * match a link in $this, the link will be omitted from the output
485 private function getCategoryInsertions( $existing = array() ) {
486 global $wgContLang, $wgCategoryCollation;
487 $diffs = array_diff_assoc( $this->mCategories
, $existing );
489 foreach ( $diffs as $name => $prefix ) {
490 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
491 $wgContLang->findVariantLink( $name, $nt, true );
493 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
495 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
501 # Treat custom sortkeys as a prefix, so that if multiple
502 # things are forced to sort as '*' or something, they'll
503 # sort properly in the category rather than in page_id
505 $sortkey = Collation
::singleton()->getSortKey(
506 $this->mTitle
->getCategorySortkey( $prefix ) );
509 'cl_from' => $this->mId
,
511 'cl_sortkey' => $sortkey,
512 'cl_timestamp' => $this->mDb
->timestamp(),
513 'cl_sortkey_prefix' => $prefix,
514 'cl_collation' => $wgCategoryCollation,
522 * Get an array of interlanguage link insertions
524 * @param $existing Array mapping existing language codes to titles
528 private function getInterlangInsertions( $existing = array() ) {
529 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
531 foreach( $diffs as $lang => $title ) {
533 'll_from' => $this->mId
,
542 * Get an array of page property insertions
543 * @param $existing array
546 function getPropertyInsertions( $existing = array() ) {
547 $diffs = array_diff_assoc( $this->mProperties
, $existing );
549 foreach ( $diffs as $name => $value ) {
551 'pp_page' => $this->mId
,
552 'pp_propname' => $name,
553 'pp_value' => $value,
560 * Get an array of interwiki insertions for passing to the DB
561 * Skips the titles specified by the 2-D array $existing
562 * @param $existing array
565 private function getInterwikiInsertions( $existing = array() ) {
567 foreach( $this->mInterwikis
as $prefix => $dbkeys ) {
568 $diffs = isset( $existing[$prefix] ) ?
array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
569 foreach ( $diffs as $dbk => $id ) {
571 'iwl_from' => $this->mId
,
572 'iwl_prefix' => $prefix,
581 * Given an array of existing links, returns those links which are not in $this
582 * and thus should be deleted.
583 * @param $existing array
586 private function getLinkDeletions( $existing ) {
588 foreach ( $existing as $ns => $dbkeys ) {
589 if ( isset( $this->mLinks
[$ns] ) ) {
590 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
592 $del[$ns] = $existing[$ns];
599 * Given an array of existing templates, returns those templates which are not in $this
600 * and thus should be deleted.
601 * @param $existing array
604 private function getTemplateDeletions( $existing ) {
606 foreach ( $existing as $ns => $dbkeys ) {
607 if ( isset( $this->mTemplates
[$ns] ) ) {
608 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
610 $del[$ns] = $existing[$ns];
617 * Given an array of existing images, returns those images which are not in $this
618 * and thus should be deleted.
619 * @param $existing array
622 private function getImageDeletions( $existing ) {
623 return array_diff_key( $existing, $this->mImages
);
627 * Given an array of existing external links, returns those links which are not
628 * in $this and thus should be deleted.
629 * @param $existing array
632 private function getExternalDeletions( $existing ) {
633 return array_diff_key( $existing, $this->mExternals
);
637 * Given an array of existing categories, returns those categories which are not in $this
638 * and thus should be deleted.
639 * @param $existing array
642 private function getCategoryDeletions( $existing ) {
643 return array_diff_assoc( $existing, $this->mCategories
);
647 * Given an array of existing interlanguage links, returns those links which are not
648 * in $this and thus should be deleted.
649 * @param $existing array
652 private function getInterlangDeletions( $existing ) {
653 return array_diff_assoc( $existing, $this->mInterlangs
);
657 * Get array of properties which should be deleted.
658 * @param $existing array
661 function getPropertyDeletions( $existing ) {
662 return array_diff_assoc( $existing, $this->mProperties
);
666 * Given an array of existing interwiki links, returns those links which are not in $this
667 * and thus should be deleted.
668 * @param $existing array
671 private function getInterwikiDeletions( $existing ) {
673 foreach ( $existing as $prefix => $dbkeys ) {
674 if ( isset( $this->mInterwikis
[$prefix] ) ) {
675 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
677 $del[$prefix] = $existing[$prefix];
684 * Get an array of existing links, as a 2-D array
688 private function getExistingLinks() {
689 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
690 array( 'pl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
692 foreach ( $res as $row ) {
693 if ( !isset( $arr[$row->pl_namespace
] ) ) {
694 $arr[$row->pl_namespace
] = array();
696 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
702 * Get an array of existing templates, as a 2-D array
706 private function getExistingTemplates() {
707 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
708 array( 'tl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
710 foreach ( $res as $row ) {
711 if ( !isset( $arr[$row->tl_namespace
] ) ) {
712 $arr[$row->tl_namespace
] = array();
714 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
720 * Get an array of existing images, image names in the keys
724 private function getExistingImages() {
725 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
726 array( 'il_from' => $this->mId
), __METHOD__
, $this->mOptions
);
728 foreach ( $res as $row ) {
729 $arr[$row->il_to
] = 1;
735 * Get an array of existing external links, URLs in the keys
739 private function getExistingExternals() {
740 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
741 array( 'el_from' => $this->mId
), __METHOD__
, $this->mOptions
);
743 foreach ( $res as $row ) {
744 $arr[$row->el_to
] = 1;
750 * Get an array of existing categories, with the name in the key and sort key in the value.
754 private function getExistingCategories() {
755 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
756 array( 'cl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
758 foreach ( $res as $row ) {
759 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
765 * Get an array of existing interlanguage links, with the language code in the key and the
766 * title in the value.
770 private function getExistingInterlangs() {
771 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
772 array( 'll_from' => $this->mId
), __METHOD__
, $this->mOptions
);
774 foreach ( $res as $row ) {
775 $arr[$row->ll_lang
] = $row->ll_title
;
781 * Get an array of existing inline interwiki links, as a 2-D array
782 * @return array (prefix => array(dbkey => 1))
784 protected function getExistingInterwikis() {
785 $res = $this->mDb
->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
786 array( 'iwl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
788 foreach ( $res as $row ) {
789 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
790 $arr[$row->iwl_prefix
] = array();
792 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
798 * Get an array of existing categories, with the name in the key and sort key in the value.
802 private function getExistingProperties() {
803 $res = $this->mDb
->select( 'page_props', array( 'pp_propname', 'pp_value' ),
804 array( 'pp_page' => $this->mId
), __METHOD__
, $this->mOptions
);
806 foreach ( $res as $row ) {
807 $arr[$row->pp_propname
] = $row->pp_value
;
813 * Return the title object of the page being updated
816 public function getTitle() {
817 return $this->mTitle
;
821 * Returns parser output
823 * @return ParserOutput
825 public function getParserOutput() {
826 return $this->mParserOutput
;
830 * Return the list of images used as generated by the parser
833 public function getImages() {
834 return $this->mImages
;
838 * Invalidate any necessary link lists related to page property changes
841 private function invalidateProperties( $changed ) {
842 global $wgPagePropLinkInvalidations;
844 foreach ( $changed as $name => $value ) {
845 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
846 $inv = $wgPagePropLinkInvalidations[$name];
847 if ( !is_array( $inv ) ) {
848 $inv = array( $inv );
850 foreach ( $inv as $table ) {
851 $update = new HTMLCacheUpdate( $this->mTitle
, $table );