Merge "Fix typo erronously -> erroneously"
[mediawiki.git] / includes / LinksUpdate.php
blobfdd0e3c1162cb4eb5d6fb4ebcc0a8c3981fadb69
1 <?php
2 /**
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
20 * @file
23 /**
24 * See docs/deferred.txt
26 * @todo document (e.g. one-sentence top-level class description).
28 class LinksUpdate extends SqlDataUpdate {
30 // @todo make members protected, but make sure extensions don't break
32 public $mId, //!< Page ID of the article linked from
33 $mTitle, //!< Title object of the article linked from
34 $mParserOutput, //!< Parser output
35 $mLinks, //!< Map of title strings to IDs for the links in the document
36 $mImages, //!< DB keys of the images used, in the array key only
37 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
38 $mExternals, //!< URLs of external links, array key only
39 $mCategories, //!< Map of category names to sort keys
40 $mInterlangs, //!< Map of language codes to titles
41 $mProperties, //!< Map of arbitrary name to value
42 $mDb, //!< Database connection reference
43 $mOptions, //!< SELECT options to be used (array)
44 $mRecursive; //!< Whether to queue jobs for recursive updates
46 /**
47 * @var null|array Added links if calculated.
49 private $linkInsertions = null;
51 /**
52 * @var null|array Deleted links if calculated.
54 private $linkDeletions = null;
56 /**
57 * Constructor
59 * @param $title Title of the page we're updating
60 * @param $parserOutput ParserOutput: output from a full parse of this page
61 * @param $recursive Boolean: queue jobs for recursive updates?
62 * @throws MWException
64 function __construct( $title, $parserOutput, $recursive = true ) {
65 parent::__construct( false ); // no implicit transaction
67 if ( !( $title instanceof Title ) ) {
68 throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
69 "Please see Article::editUpdates() for an invocation example.\n" );
72 if ( !( $parserOutput instanceof ParserOutput ) ) {
73 throw new MWException( "The calling convention to LinksUpdate::__construct() has changed. " .
74 "Please see WikiPage::doEditUpdates() for an invocation example.\n" );
77 $this->mTitle = $title;
78 $this->mId = $title->getArticleID();
80 if ( !$this->mId ) {
81 throw new MWException( "The Title object did not provide an article ID. Perhaps the page doesn't exist?" );
84 $this->mParserOutput = $parserOutput;
86 $this->mLinks = $parserOutput->getLinks();
87 $this->mImages = $parserOutput->getImages();
88 $this->mTemplates = $parserOutput->getTemplates();
89 $this->mExternals = $parserOutput->getExternalLinks();
90 $this->mCategories = $parserOutput->getCategories();
91 $this->mProperties = $parserOutput->getProperties();
92 $this->mInterwikis = $parserOutput->getInterwikiLinks();
94 # Convert the format of the interlanguage links
95 # I didn't want to change it in the ParserOutput, because that array is passed all
96 # the way back to the skin, so either a skin API break would be required, or an
97 # inefficient back-conversion.
98 $ill = $parserOutput->getLanguageLinks();
99 $this->mInterlangs = array();
100 foreach ( $ill as $link ) {
101 list( $key, $title ) = explode( ':', $link, 2 );
102 $this->mInterlangs[$key] = $title;
105 foreach ( $this->mCategories as &$sortkey ) {
106 # If the sortkey is longer then 255 bytes,
107 # it truncated by DB, and then doesn't get
108 # matched when comparing existing vs current
109 # categories, causing bug 25254.
110 # Also. substr behaves weird when given "".
111 if ( $sortkey !== '' ) {
112 $sortkey = substr( $sortkey, 0, 255 );
116 $this->mRecursive = $recursive;
118 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
122 * Update link tables with outgoing links from an updated article
124 public function doUpdate() {
125 wfRunHooks( 'LinksUpdate', array( &$this ) );
126 $this->doIncrementalUpdate();
127 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
130 protected function doIncrementalUpdate() {
131 wfProfileIn( __METHOD__ );
133 # Page links
134 $existing = $this->getExistingLinks();
135 $this->linkDeletions = $this->getLinkDeletions( $existing );
136 $this->linkInsertions = $this->getLinkInsertions( $existing );
137 $this->incrTableUpdate( 'pagelinks', 'pl', $this->linkDeletions, $this->linkInsertions );
139 # Image links
140 $existing = $this->getExistingImages();
142 $imageDeletes = $this->getImageDeletions( $existing );
143 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
144 $this->getImageInsertions( $existing ) );
146 # Invalidate all image description pages which had links added or removed
147 $imageUpdates = $imageDeletes + array_diff_key( $this->mImages, $existing );
148 $this->invalidateImageDescriptions( $imageUpdates );
150 # External links
151 $existing = $this->getExistingExternals();
152 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
153 $this->getExternalInsertions( $existing ) );
155 # Language links
156 $existing = $this->getExistingInterlangs();
157 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
158 $this->getInterlangInsertions( $existing ) );
160 # Inline interwiki links
161 $existing = $this->getExistingInterwikis();
162 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
163 $this->getInterwikiInsertions( $existing ) );
165 # Template links
166 $existing = $this->getExistingTemplates();
167 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
168 $this->getTemplateInsertions( $existing ) );
170 # Category links
171 $existing = $this->getExistingCategories();
173 $categoryDeletes = $this->getCategoryDeletions( $existing );
175 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
176 $this->getCategoryInsertions( $existing ) );
178 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
179 $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
180 $categoryUpdates = $categoryInserts + $categoryDeletes;
181 $this->invalidateCategories( $categoryUpdates );
182 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
184 # Page properties
185 $existing = $this->getExistingProperties();
187 $propertiesDeletes = $this->getPropertyDeletions( $existing );
189 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
190 $this->getPropertyInsertions( $existing ) );
192 # Invalidate the necessary pages
193 $changed = $propertiesDeletes + array_diff_assoc( $this->mProperties, $existing );
194 $this->invalidateProperties( $changed );
196 # Refresh links of all pages including this page
197 # This will be in a separate transaction
198 if ( $this->mRecursive ) {
199 $this->queueRecursiveJobs();
202 wfProfileOut( __METHOD__ );
206 * Queue recursive jobs for this page
208 * Which means do LinksUpdate on all templates
209 * that include the current page, using the job queue.
211 function queueRecursiveJobs() {
212 self::queueRecursiveJobsForTable( $this->mTitle, 'templatelinks' );
216 * Queue a RefreshLinks job for any table.
218 * @param Title $title Title to do job for
219 * @param String $table Table to use (e.g. 'templatelinks')
221 public static function queueRecursiveJobsForTable( Title $title, $table ) {
222 wfProfileIn( __METHOD__ );
223 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
224 $job = new RefreshLinksJob2(
225 $title,
226 array(
227 'table' => $table,
228 ) + Job::newRootJobParams( // "overall" refresh links job info
229 "refreshlinks:{$table}:{$title->getPrefixedText()}"
232 JobQueueGroup::singleton()->push( $job );
233 JobQueueGroup::singleton()->deduplicateRootJob( $job );
235 wfProfileOut( __METHOD__ );
239 * @param $cats
241 function invalidateCategories( $cats ) {
242 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
246 * Update all the appropriate counts in the category table.
247 * @param array $added associative array of category name => sort key
248 * @param array $deleted associative array of category name => sort key
250 function updateCategoryCounts( $added, $deleted ) {
251 $a = WikiPage::factory( $this->mTitle );
252 $a->updateCategoryCounts(
253 array_keys( $added ), array_keys( $deleted )
258 * @param $images
260 function invalidateImageDescriptions( $images ) {
261 $this->invalidatePages( NS_FILE, array_keys( $images ) );
265 * Update a table by doing a delete query then an insert query
266 * @param $table
267 * @param $prefix
268 * @param $deletions
269 * @param $insertions
271 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
272 if ( $table == 'page_props' ) {
273 $fromField = 'pp_page';
274 } else {
275 $fromField = "{$prefix}_from";
277 $where = array( $fromField => $this->mId );
278 if ( $table == 'pagelinks' || $table == 'templatelinks' || $table == 'iwlinks' ) {
279 if ( $table == 'iwlinks' ) {
280 $baseKey = 'iwl_prefix';
281 } else {
282 $baseKey = "{$prefix}_namespace";
284 $clause = $this->mDb->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
285 if ( $clause ) {
286 $where[] = $clause;
287 } else {
288 $where = false;
290 } else {
291 if ( $table == 'langlinks' ) {
292 $toField = 'll_lang';
293 } elseif ( $table == 'page_props' ) {
294 $toField = 'pp_propname';
295 } else {
296 $toField = $prefix . '_to';
298 if ( count( $deletions ) ) {
299 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
300 } else {
301 $where = false;
304 if ( $where ) {
305 $this->mDb->delete( $table, $where, __METHOD__ );
307 if ( count( $insertions ) ) {
308 $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
309 wfRunHooks( 'LinksUpdateAfterInsert', array( $this, $table, $insertions ) );
314 * Get an array of pagelinks insertions for passing to the DB
315 * Skips the titles specified by the 2-D array $existing
316 * @param $existing array
317 * @return array
319 private function getLinkInsertions( $existing = array() ) {
320 $arr = array();
321 foreach ( $this->mLinks as $ns => $dbkeys ) {
322 $diffs = isset( $existing[$ns] )
323 ? array_diff_key( $dbkeys, $existing[$ns] )
324 : $dbkeys;
325 foreach ( $diffs as $dbk => $id ) {
326 $arr[] = array(
327 'pl_from' => $this->mId,
328 'pl_namespace' => $ns,
329 'pl_title' => $dbk
333 return $arr;
337 * Get an array of template insertions. Like getLinkInsertions()
338 * @param $existing array
339 * @return array
341 private function getTemplateInsertions( $existing = array() ) {
342 $arr = array();
343 foreach ( $this->mTemplates as $ns => $dbkeys ) {
344 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
345 foreach ( $diffs as $dbk => $id ) {
346 $arr[] = array(
347 'tl_from' => $this->mId,
348 'tl_namespace' => $ns,
349 'tl_title' => $dbk
353 return $arr;
357 * Get an array of image insertions
358 * Skips the names specified in $existing
359 * @param $existing array
360 * @return array
362 private function getImageInsertions( $existing = array() ) {
363 $arr = array();
364 $diffs = array_diff_key( $this->mImages, $existing );
365 foreach ( $diffs as $iname => $dummy ) {
366 $arr[] = array(
367 'il_from' => $this->mId,
368 'il_to' => $iname
371 return $arr;
375 * Get an array of externallinks insertions. Skips the names specified in $existing
376 * @param $existing array
377 * @return array
379 private function getExternalInsertions( $existing = array() ) {
380 $arr = array();
381 $diffs = array_diff_key( $this->mExternals, $existing );
382 foreach ( $diffs as $url => $dummy ) {
383 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
384 $arr[] = array(
385 'el_from' => $this->mId,
386 'el_to' => $url,
387 'el_index' => $index,
391 return $arr;
395 * Get an array of category insertions
397 * @param array $existing mapping existing category names to sort keys. If both
398 * match a link in $this, the link will be omitted from the output
400 * @return array
402 private function getCategoryInsertions( $existing = array() ) {
403 global $wgContLang, $wgCategoryCollation;
404 $diffs = array_diff_assoc( $this->mCategories, $existing );
405 $arr = array();
406 foreach ( $diffs as $name => $prefix ) {
407 $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
408 $wgContLang->findVariantLink( $name, $nt, true );
410 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
411 $type = 'subcat';
412 } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
413 $type = 'file';
414 } else {
415 $type = 'page';
418 # Treat custom sortkeys as a prefix, so that if multiple
419 # things are forced to sort as '*' or something, they'll
420 # sort properly in the category rather than in page_id
421 # order or such.
422 $sortkey = Collation::singleton()->getSortKey(
423 $this->mTitle->getCategorySortkey( $prefix ) );
425 $arr[] = array(
426 'cl_from' => $this->mId,
427 'cl_to' => $name,
428 'cl_sortkey' => $sortkey,
429 'cl_timestamp' => $this->mDb->timestamp(),
430 'cl_sortkey_prefix' => $prefix,
431 'cl_collation' => $wgCategoryCollation,
432 'cl_type' => $type,
435 return $arr;
439 * Get an array of interlanguage link insertions
441 * @param array $existing mapping existing language codes to titles
443 * @return array
445 private function getInterlangInsertions( $existing = array() ) {
446 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
447 $arr = array();
448 foreach ( $diffs as $lang => $title ) {
449 $arr[] = array(
450 'll_from' => $this->mId,
451 'll_lang' => $lang,
452 'll_title' => $title
455 return $arr;
459 * Get an array of page property insertions
460 * @param $existing array
461 * @return array
463 function getPropertyInsertions( $existing = array() ) {
464 $diffs = array_diff_assoc( $this->mProperties, $existing );
465 $arr = array();
466 foreach ( $diffs as $name => $value ) {
467 $arr[] = array(
468 'pp_page' => $this->mId,
469 'pp_propname' => $name,
470 'pp_value' => $value,
473 return $arr;
477 * Get an array of interwiki insertions for passing to the DB
478 * Skips the titles specified by the 2-D array $existing
479 * @param $existing array
480 * @return array
482 private function getInterwikiInsertions( $existing = array() ) {
483 $arr = array();
484 foreach ( $this->mInterwikis as $prefix => $dbkeys ) {
485 $diffs = isset( $existing[$prefix] ) ? array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
486 foreach ( $diffs as $dbk => $id ) {
487 $arr[] = array(
488 'iwl_from' => $this->mId,
489 'iwl_prefix' => $prefix,
490 'iwl_title' => $dbk
494 return $arr;
498 * Given an array of existing links, returns those links which are not in $this
499 * and thus should be deleted.
500 * @param $existing array
501 * @return array
503 private function getLinkDeletions( $existing ) {
504 $del = array();
505 foreach ( $existing as $ns => $dbkeys ) {
506 if ( isset( $this->mLinks[$ns] ) ) {
507 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
508 } else {
509 $del[$ns] = $existing[$ns];
512 return $del;
516 * Given an array of existing templates, returns those templates which are not in $this
517 * and thus should be deleted.
518 * @param $existing array
519 * @return array
521 private function getTemplateDeletions( $existing ) {
522 $del = array();
523 foreach ( $existing as $ns => $dbkeys ) {
524 if ( isset( $this->mTemplates[$ns] ) ) {
525 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
526 } else {
527 $del[$ns] = $existing[$ns];
530 return $del;
534 * Given an array of existing images, returns those images which are not in $this
535 * and thus should be deleted.
536 * @param $existing array
537 * @return array
539 private function getImageDeletions( $existing ) {
540 return array_diff_key( $existing, $this->mImages );
544 * Given an array of existing external links, returns those links which are not
545 * in $this and thus should be deleted.
546 * @param $existing array
547 * @return array
549 private function getExternalDeletions( $existing ) {
550 return array_diff_key( $existing, $this->mExternals );
554 * Given an array of existing categories, returns those categories which are not in $this
555 * and thus should be deleted.
556 * @param $existing array
557 * @return array
559 private function getCategoryDeletions( $existing ) {
560 return array_diff_assoc( $existing, $this->mCategories );
564 * Given an array of existing interlanguage links, returns those links which are not
565 * in $this and thus should be deleted.
566 * @param $existing array
567 * @return array
569 private function getInterlangDeletions( $existing ) {
570 return array_diff_assoc( $existing, $this->mInterlangs );
574 * Get array of properties which should be deleted.
575 * @param $existing array
576 * @return array
578 function getPropertyDeletions( $existing ) {
579 return array_diff_assoc( $existing, $this->mProperties );
583 * Given an array of existing interwiki links, returns those links which are not in $this
584 * and thus should be deleted.
585 * @param $existing array
586 * @return array
588 private function getInterwikiDeletions( $existing ) {
589 $del = array();
590 foreach ( $existing as $prefix => $dbkeys ) {
591 if ( isset( $this->mInterwikis[$prefix] ) ) {
592 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis[$prefix] );
593 } else {
594 $del[$prefix] = $existing[$prefix];
597 return $del;
601 * Get an array of existing links, as a 2-D array
603 * @return array
605 private function getExistingLinks() {
606 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
607 array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
608 $arr = array();
609 foreach ( $res as $row ) {
610 if ( !isset( $arr[$row->pl_namespace] ) ) {
611 $arr[$row->pl_namespace] = array();
613 $arr[$row->pl_namespace][$row->pl_title] = 1;
615 return $arr;
619 * Get an array of existing templates, as a 2-D array
621 * @return array
623 private function getExistingTemplates() {
624 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
625 array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
626 $arr = array();
627 foreach ( $res as $row ) {
628 if ( !isset( $arr[$row->tl_namespace] ) ) {
629 $arr[$row->tl_namespace] = array();
631 $arr[$row->tl_namespace][$row->tl_title] = 1;
633 return $arr;
637 * Get an array of existing images, image names in the keys
639 * @return array
641 private function getExistingImages() {
642 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
643 array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
644 $arr = array();
645 foreach ( $res as $row ) {
646 $arr[$row->il_to] = 1;
648 return $arr;
652 * Get an array of existing external links, URLs in the keys
654 * @return array
656 private function getExistingExternals() {
657 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
658 array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
659 $arr = array();
660 foreach ( $res as $row ) {
661 $arr[$row->el_to] = 1;
663 return $arr;
667 * Get an array of existing categories, with the name in the key and sort key in the value.
669 * @return array
671 private function getExistingCategories() {
672 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
673 array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
674 $arr = array();
675 foreach ( $res as $row ) {
676 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
678 return $arr;
682 * Get an array of existing interlanguage links, with the language code in the key and the
683 * title in the value.
685 * @return array
687 private function getExistingInterlangs() {
688 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
689 array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
690 $arr = array();
691 foreach ( $res as $row ) {
692 $arr[$row->ll_lang] = $row->ll_title;
694 return $arr;
698 * Get an array of existing inline interwiki links, as a 2-D array
699 * @return array (prefix => array(dbkey => 1))
701 protected function getExistingInterwikis() {
702 $res = $this->mDb->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
703 array( 'iwl_from' => $this->mId ), __METHOD__, $this->mOptions );
704 $arr = array();
705 foreach ( $res as $row ) {
706 if ( !isset( $arr[$row->iwl_prefix] ) ) {
707 $arr[$row->iwl_prefix] = array();
709 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
711 return $arr;
715 * Get an array of existing categories, with the name in the key and sort key in the value.
717 * @return array
719 private function getExistingProperties() {
720 $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
721 array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
722 $arr = array();
723 foreach ( $res as $row ) {
724 $arr[$row->pp_propname] = $row->pp_value;
726 return $arr;
730 * Return the title object of the page being updated
731 * @return Title
733 public function getTitle() {
734 return $this->mTitle;
738 * Returns parser output
739 * @since 1.19
740 * @return ParserOutput
742 public function getParserOutput() {
743 return $this->mParserOutput;
747 * Return the list of images used as generated by the parser
748 * @return array
750 public function getImages() {
751 return $this->mImages;
755 * Invalidate any necessary link lists related to page property changes
756 * @param $changed
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 );
769 $update->doUpdate();
776 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
777 * @since 1.22
778 * @return null|array of Titles
780 public function getAddedLinks() {
781 if ( $this->linkInsertions === null ) {
782 return null;
784 $result = array();
785 foreach ( $this->linkInsertions as $insertion ) {
786 $result[] = Title::makeTitle( $insertion[ 'pl_namespace' ], $insertion[ 'pl_title' ] );
788 return $result;
792 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
793 * @since 1.22
794 * @return null|array of Titles
796 public function getRemovedLinks() {
797 if ( $this->linkDeletions === null ) {
798 return null;
800 $result = array();
801 foreach ( $this->linkDeletions as $ns => $titles ) {
802 foreach ( $titles as $title => $unused ) {
803 $result[] = Title::makeTitle( $ns, $title );
806 return $result;
811 * Update object handling the cleanup of links tables after a page was deleted.
813 class LinksDeletionUpdate extends SqlDataUpdate {
815 protected $mPage; //!< WikiPage the wikipage that was deleted
818 * Constructor
820 * @param $page WikiPage Page we are updating
821 * @throws MWException
823 function __construct( WikiPage $page ) {
824 parent::__construct( false ); // no implicit transaction
826 $this->mPage = $page;
828 if ( !$page->exists() ) {
829 throw new MWException( "Page ID not known, perhaps the page doesn't exist?" );
834 * Do some database updates after deletion
836 public function doUpdate() {
837 $title = $this->mPage->getTitle();
838 $id = $this->mPage->getId();
840 # Delete restrictions for it
841 $this->mDb->delete( 'page_restrictions', array( 'pr_page' => $id ), __METHOD__ );
843 # Fix category table counts
844 $cats = array();
845 $res = $this->mDb->select( 'categorylinks', 'cl_to', array( 'cl_from' => $id ), __METHOD__ );
847 foreach ( $res as $row ) {
848 $cats[] = $row->cl_to;
851 $this->mPage->updateCategoryCounts( array(), $cats );
853 # If using cascading deletes, we can skip some explicit deletes
854 if ( !$this->mDb->cascadingDeletes() ) {
855 # Delete outgoing links
856 $this->mDb->delete( 'pagelinks', array( 'pl_from' => $id ), __METHOD__ );
857 $this->mDb->delete( 'imagelinks', array( 'il_from' => $id ), __METHOD__ );
858 $this->mDb->delete( 'categorylinks', array( 'cl_from' => $id ), __METHOD__ );
859 $this->mDb->delete( 'templatelinks', array( 'tl_from' => $id ), __METHOD__ );
860 $this->mDb->delete( 'externallinks', array( 'el_from' => $id ), __METHOD__ );
861 $this->mDb->delete( 'langlinks', array( 'll_from' => $id ), __METHOD__ );
862 $this->mDb->delete( 'iwlinks', array( 'iwl_from' => $id ), __METHOD__ );
863 $this->mDb->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__ );
864 $this->mDb->delete( 'page_props', array( 'pp_page' => $id ), __METHOD__ );
867 # If using cleanup triggers, we can skip some manual deletes
868 if ( !$this->mDb->cleanupTriggers() ) {
869 # Clean up recentchanges entries...
870 $this->mDb->delete( 'recentchanges',
871 array( 'rc_type != ' . RC_LOG,
872 'rc_namespace' => $title->getNamespace(),
873 'rc_title' => $title->getDBkey() ),
874 __METHOD__ );
875 $this->mDb->delete( 'recentchanges',
876 array( 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ),
877 __METHOD__ );
882 * Update all the appropriate counts in the category table.
883 * @param array $added associative array of category name => sort key
884 * @param array $deleted associative array of category name => sort key
886 function updateCategoryCounts( $added, $deleted ) {
887 $a = WikiPage::factory( $this->mTitle );
888 $a->updateCategoryCounts(
889 array_keys( $added ), array_keys( $deleted )