Merge "Make line breaks in <blockquote> behave like <div> (bug 6200)."
[mediawiki.git] / includes / LinksUpdate.php
blob0b7393addbbb9a0dde8bb91a0818e65f099e3318
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 * Constructor
49 * @param $title Title of the page we're updating
50 * @param $parserOutput ParserOutput: output from a full parse of this page
51 * @param $recursive Boolean: queue jobs for recursive updates?
52 * @throws MWException
54 function __construct( $title, $parserOutput, $recursive = true ) {
55 parent::__construct( false ); // no implicit transaction
57 if ( !( $title instanceof Title ) ) {
58 throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
59 "Please see Article::editUpdates() for an invocation example.\n" );
62 if ( !( $parserOutput instanceof ParserOutput ) ) {
63 throw new MWException( "The calling convention to LinksUpdate::__construct() has changed. " .
64 "Please see WikiPage::doEditUpdates() for an invocation example.\n" );
67 $this->mTitle = $title;
68 $this->mId = $title->getArticleID();
70 if ( !$this->mId ) {
71 throw new MWException( "The Title object did not provide an article ID. Perhaps the page doesn't exist?" );
74 $this->mParserOutput = $parserOutput;
76 $this->mLinks = $parserOutput->getLinks();
77 $this->mImages = $parserOutput->getImages();
78 $this->mTemplates = $parserOutput->getTemplates();
79 $this->mExternals = $parserOutput->getExternalLinks();
80 $this->mCategories = $parserOutput->getCategories();
81 $this->mProperties = $parserOutput->getProperties();
82 $this->mInterwikis = $parserOutput->getInterwikiLinks();
84 # Convert the format of the interlanguage links
85 # I didn't want to change it in the ParserOutput, because that array is passed all
86 # the way back to the skin, so either a skin API break would be required, or an
87 # inefficient back-conversion.
88 $ill = $parserOutput->getLanguageLinks();
89 $this->mInterlangs = array();
90 foreach ( $ill as $link ) {
91 list( $key, $title ) = explode( ':', $link, 2 );
92 $this->mInterlangs[$key] = $title;
95 foreach ( $this->mCategories as &$sortkey ) {
96 # If the sortkey is longer then 255 bytes,
97 # it truncated by DB, and then doesn't get
98 # matched when comparing existing vs current
99 # categories, causing bug 25254.
100 # Also. substr behaves weird when given "".
101 if ( $sortkey !== '' ) {
102 $sortkey = substr( $sortkey, 0, 255 );
106 $this->mRecursive = $recursive;
108 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
112 * Update link tables with outgoing links from an updated article
114 public function doUpdate() {
115 global $wgUseDumbLinkUpdate;
117 wfRunHooks( 'LinksUpdate', array( &$this ) );
118 if ( $wgUseDumbLinkUpdate ) {
119 $this->doDumbUpdate();
120 } else {
121 $this->doIncrementalUpdate();
123 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
126 protected function doIncrementalUpdate() {
127 wfProfileIn( __METHOD__ );
129 # Page links
130 $existing = $this->getExistingLinks();
131 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
132 $this->getLinkInsertions( $existing ) );
134 # Image links
135 $existing = $this->getExistingImages();
137 $imageDeletes = $this->getImageDeletions( $existing );
138 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
139 $this->getImageInsertions( $existing ) );
141 # Invalidate all image description pages which had links added or removed
142 $imageUpdates = $imageDeletes + array_diff_key( $this->mImages, $existing );
143 $this->invalidateImageDescriptions( $imageUpdates );
145 # External links
146 $existing = $this->getExistingExternals();
147 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
148 $this->getExternalInsertions( $existing ) );
150 # Language links
151 $existing = $this->getExistingInterlangs();
152 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
153 $this->getInterlangInsertions( $existing ) );
155 # Inline interwiki links
156 $existing = $this->getExistingInterwikis();
157 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
158 $this->getInterwikiInsertions( $existing ) );
160 # Template links
161 $existing = $this->getExistingTemplates();
162 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
163 $this->getTemplateInsertions( $existing ) );
165 # Category links
166 $existing = $this->getExistingCategories();
168 $categoryDeletes = $this->getCategoryDeletions( $existing );
170 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
171 $this->getCategoryInsertions( $existing ) );
173 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
174 $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
175 $categoryUpdates = $categoryInserts + $categoryDeletes;
176 $this->invalidateCategories( $categoryUpdates );
177 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
179 # Page properties
180 $existing = $this->getExistingProperties();
182 $propertiesDeletes = $this->getPropertyDeletions( $existing );
184 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
185 $this->getPropertyInsertions( $existing ) );
187 # Invalidate the necessary pages
188 $changed = $propertiesDeletes + array_diff_assoc( $this->mProperties, $existing );
189 $this->invalidateProperties( $changed );
191 # Refresh links of all pages including this page
192 # This will be in a separate transaction
193 if ( $this->mRecursive ) {
194 $this->queueRecursiveJobs();
197 wfProfileOut( __METHOD__ );
201 * Link update which clears the previous entries and inserts new ones
202 * May be slower or faster depending on level of lock contention and write speed of DB
203 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
205 protected function doDumbUpdate() {
206 wfProfileIn( __METHOD__ );
208 # Refresh category pages and image description pages
209 $existing = $this->getExistingCategories();
210 $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
211 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories );
212 $categoryUpdates = $categoryInserts + $categoryDeletes;
213 $existing = $this->getExistingImages();
214 $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
216 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
217 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
218 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
219 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
220 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
221 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
222 $this->dumbTableUpdate( 'iwlinks', $this->getInterwikiInsertions(), 'iwl_from' );
223 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
225 # Update the cache of all the category pages and image description
226 # pages which were changed, and fix the category table count
227 $this->invalidateCategories( $categoryUpdates );
228 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
229 $this->invalidateImageDescriptions( $imageUpdates );
231 # Refresh links of all pages including this page
232 # This will be in a separate transaction
233 if ( $this->mRecursive ) {
234 $this->queueRecursiveJobs();
237 wfProfileOut( __METHOD__ );
241 * Queue recursive jobs for this page
243 * Which means do LinksUpdate on all templates
244 * that include the current page, using the job queue.
246 function queueRecursiveJobs() {
247 self::queueRecursiveJobsForTable( $this->mTitle, 'templatelinks' );
251 * Queue a RefreshLinks job for any table.
253 * @param Title $title Title to do job for
254 * @param String $table Table to use (e.g. 'templatelinks')
256 public static function queueRecursiveJobsForTable( Title $title, $table ) {
257 wfProfileIn( __METHOD__ );
258 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
259 $job = new RefreshLinksJob2(
260 $title,
261 array(
262 'table' => $table,
263 ) + Job::newRootJobParams( // "overall" refresh links job info
264 "refreshlinks:{$table}:{$title->getPrefixedText()}"
267 JobQueueGroup::singleton()->push( $job );
268 JobQueueGroup::singleton()->deduplicateRootJob( $job );
270 wfProfileOut( __METHOD__ );
274 * @param $cats
276 function invalidateCategories( $cats ) {
277 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
281 * Update all the appropriate counts in the category table.
282 * @param array $added associative array of category name => sort key
283 * @param array $deleted associative array of category name => sort key
285 function updateCategoryCounts( $added, $deleted ) {
286 $a = WikiPage::factory( $this->mTitle );
287 $a->updateCategoryCounts(
288 array_keys( $added ), array_keys( $deleted )
293 * @param $images
295 function invalidateImageDescriptions( $images ) {
296 $this->invalidatePages( NS_FILE, array_keys( $images ) );
300 * @param $table
301 * @param $insertions
302 * @param $fromField
304 private function dumbTableUpdate( $table, $insertions, $fromField ) {
305 $this->mDb->delete( $table, array( $fromField => $this->mId ), __METHOD__ );
306 if ( count( $insertions ) ) {
307 # The link array was constructed without FOR UPDATE, so there may
308 # be collisions. This may cause minor link table inconsistencies,
309 # which is better than crippling the site with lock contention.
310 $this->mDb->insert( $table, $insertions, __METHOD__, array( 'IGNORE' ) );
315 * Update a table by doing a delete query then an insert query
316 * @param $table
317 * @param $prefix
318 * @param $deletions
319 * @param $insertions
321 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
322 if ( $table == 'page_props' ) {
323 $fromField = 'pp_page';
324 } else {
325 $fromField = "{$prefix}_from";
327 $where = array( $fromField => $this->mId );
328 if ( $table == 'pagelinks' || $table == 'templatelinks' || $table == 'iwlinks' ) {
329 if ( $table == 'iwlinks' ) {
330 $baseKey = 'iwl_prefix';
331 } else {
332 $baseKey = "{$prefix}_namespace";
334 $clause = $this->mDb->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
335 if ( $clause ) {
336 $where[] = $clause;
337 } else {
338 $where = false;
340 } else {
341 if ( $table == 'langlinks' ) {
342 $toField = 'll_lang';
343 } elseif ( $table == 'page_props' ) {
344 $toField = 'pp_propname';
345 } else {
346 $toField = $prefix . '_to';
348 if ( count( $deletions ) ) {
349 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
350 } else {
351 $where = false;
354 if ( $where ) {
355 $this->mDb->delete( $table, $where, __METHOD__ );
357 if ( count( $insertions ) ) {
358 $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
359 wfRunHooks( 'LinksUpdateAfterInsert', array( $this, $table, $insertions ) );
364 * Get an array of pagelinks insertions for passing to the DB
365 * Skips the titles specified by the 2-D array $existing
366 * @param $existing array
367 * @return array
369 private function getLinkInsertions( $existing = array() ) {
370 $arr = array();
371 foreach ( $this->mLinks as $ns => $dbkeys ) {
372 $diffs = isset( $existing[$ns] )
373 ? array_diff_key( $dbkeys, $existing[$ns] )
374 : $dbkeys;
375 foreach ( $diffs as $dbk => $id ) {
376 $arr[] = array(
377 'pl_from' => $this->mId,
378 'pl_namespace' => $ns,
379 'pl_title' => $dbk
383 return $arr;
387 * Get an array of template insertions. Like getLinkInsertions()
388 * @param $existing array
389 * @return array
391 private function getTemplateInsertions( $existing = array() ) {
392 $arr = array();
393 foreach ( $this->mTemplates as $ns => $dbkeys ) {
394 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
395 foreach ( $diffs as $dbk => $id ) {
396 $arr[] = array(
397 'tl_from' => $this->mId,
398 'tl_namespace' => $ns,
399 'tl_title' => $dbk
403 return $arr;
407 * Get an array of image insertions
408 * Skips the names specified in $existing
409 * @param $existing array
410 * @return array
412 private function getImageInsertions( $existing = array() ) {
413 $arr = array();
414 $diffs = array_diff_key( $this->mImages, $existing );
415 foreach ( $diffs as $iname => $dummy ) {
416 $arr[] = array(
417 'il_from' => $this->mId,
418 'il_to' => $iname
421 return $arr;
425 * Get an array of externallinks insertions. Skips the names specified in $existing
426 * @param $existing array
427 * @return array
429 private function getExternalInsertions( $existing = array() ) {
430 $arr = array();
431 $diffs = array_diff_key( $this->mExternals, $existing );
432 foreach ( $diffs as $url => $dummy ) {
433 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
434 $arr[] = array(
435 'el_from' => $this->mId,
436 'el_to' => $url,
437 'el_index' => $index,
441 return $arr;
445 * Get an array of category insertions
447 * @param array $existing mapping existing category names to sort keys. If both
448 * match a link in $this, the link will be omitted from the output
450 * @return array
452 private function getCategoryInsertions( $existing = array() ) {
453 global $wgContLang, $wgCategoryCollation;
454 $diffs = array_diff_assoc( $this->mCategories, $existing );
455 $arr = array();
456 foreach ( $diffs as $name => $prefix ) {
457 $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
458 $wgContLang->findVariantLink( $name, $nt, true );
460 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
461 $type = 'subcat';
462 } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
463 $type = 'file';
464 } else {
465 $type = 'page';
468 # Treat custom sortkeys as a prefix, so that if multiple
469 # things are forced to sort as '*' or something, they'll
470 # sort properly in the category rather than in page_id
471 # order or such.
472 $sortkey = Collation::singleton()->getSortKey(
473 $this->mTitle->getCategorySortkey( $prefix ) );
475 $arr[] = array(
476 'cl_from' => $this->mId,
477 'cl_to' => $name,
478 'cl_sortkey' => $sortkey,
479 'cl_timestamp' => $this->mDb->timestamp(),
480 'cl_sortkey_prefix' => $prefix,
481 'cl_collation' => $wgCategoryCollation,
482 'cl_type' => $type,
485 return $arr;
489 * Get an array of interlanguage link insertions
491 * @param array $existing mapping existing language codes to titles
493 * @return array
495 private function getInterlangInsertions( $existing = array() ) {
496 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
497 $arr = array();
498 foreach ( $diffs as $lang => $title ) {
499 $arr[] = array(
500 'll_from' => $this->mId,
501 'll_lang' => $lang,
502 'll_title' => $title
505 return $arr;
509 * Get an array of page property insertions
510 * @param $existing array
511 * @return array
513 function getPropertyInsertions( $existing = array() ) {
514 $diffs = array_diff_assoc( $this->mProperties, $existing );
515 $arr = array();
516 foreach ( $diffs as $name => $value ) {
517 $arr[] = array(
518 'pp_page' => $this->mId,
519 'pp_propname' => $name,
520 'pp_value' => $value,
523 return $arr;
527 * Get an array of interwiki insertions for passing to the DB
528 * Skips the titles specified by the 2-D array $existing
529 * @param $existing array
530 * @return array
532 private function getInterwikiInsertions( $existing = array() ) {
533 $arr = array();
534 foreach ( $this->mInterwikis as $prefix => $dbkeys ) {
535 $diffs = isset( $existing[$prefix] ) ? array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
536 foreach ( $diffs as $dbk => $id ) {
537 $arr[] = array(
538 'iwl_from' => $this->mId,
539 'iwl_prefix' => $prefix,
540 'iwl_title' => $dbk
544 return $arr;
548 * Given an array of existing links, returns those links which are not in $this
549 * and thus should be deleted.
550 * @param $existing array
551 * @return array
553 private function getLinkDeletions( $existing ) {
554 $del = array();
555 foreach ( $existing as $ns => $dbkeys ) {
556 if ( isset( $this->mLinks[$ns] ) ) {
557 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
558 } else {
559 $del[$ns] = $existing[$ns];
562 return $del;
566 * Given an array of existing templates, returns those templates which are not in $this
567 * and thus should be deleted.
568 * @param $existing array
569 * @return array
571 private function getTemplateDeletions( $existing ) {
572 $del = array();
573 foreach ( $existing as $ns => $dbkeys ) {
574 if ( isset( $this->mTemplates[$ns] ) ) {
575 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
576 } else {
577 $del[$ns] = $existing[$ns];
580 return $del;
584 * Given an array of existing images, returns those images which are not in $this
585 * and thus should be deleted.
586 * @param $existing array
587 * @return array
589 private function getImageDeletions( $existing ) {
590 return array_diff_key( $existing, $this->mImages );
594 * Given an array of existing external links, returns those links which are not
595 * in $this and thus should be deleted.
596 * @param $existing array
597 * @return array
599 private function getExternalDeletions( $existing ) {
600 return array_diff_key( $existing, $this->mExternals );
604 * Given an array of existing categories, returns those categories which are not in $this
605 * and thus should be deleted.
606 * @param $existing array
607 * @return array
609 private function getCategoryDeletions( $existing ) {
610 return array_diff_assoc( $existing, $this->mCategories );
614 * Given an array of existing interlanguage links, returns those links which are not
615 * in $this and thus should be deleted.
616 * @param $existing array
617 * @return array
619 private function getInterlangDeletions( $existing ) {
620 return array_diff_assoc( $existing, $this->mInterlangs );
624 * Get array of properties which should be deleted.
625 * @param $existing array
626 * @return array
628 function getPropertyDeletions( $existing ) {
629 return array_diff_assoc( $existing, $this->mProperties );
633 * Given an array of existing interwiki links, returns those links which are not in $this
634 * and thus should be deleted.
635 * @param $existing array
636 * @return array
638 private function getInterwikiDeletions( $existing ) {
639 $del = array();
640 foreach ( $existing as $prefix => $dbkeys ) {
641 if ( isset( $this->mInterwikis[$prefix] ) ) {
642 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis[$prefix] );
643 } else {
644 $del[$prefix] = $existing[$prefix];
647 return $del;
651 * Get an array of existing links, as a 2-D array
653 * @return array
655 private function getExistingLinks() {
656 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
657 array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
658 $arr = array();
659 foreach ( $res as $row ) {
660 if ( !isset( $arr[$row->pl_namespace] ) ) {
661 $arr[$row->pl_namespace] = array();
663 $arr[$row->pl_namespace][$row->pl_title] = 1;
665 return $arr;
669 * Get an array of existing templates, as a 2-D array
671 * @return array
673 private function getExistingTemplates() {
674 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
675 array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
676 $arr = array();
677 foreach ( $res as $row ) {
678 if ( !isset( $arr[$row->tl_namespace] ) ) {
679 $arr[$row->tl_namespace] = array();
681 $arr[$row->tl_namespace][$row->tl_title] = 1;
683 return $arr;
687 * Get an array of existing images, image names in the keys
689 * @return array
691 private function getExistingImages() {
692 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
693 array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
694 $arr = array();
695 foreach ( $res as $row ) {
696 $arr[$row->il_to] = 1;
698 return $arr;
702 * Get an array of existing external links, URLs in the keys
704 * @return array
706 private function getExistingExternals() {
707 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
708 array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
709 $arr = array();
710 foreach ( $res as $row ) {
711 $arr[$row->el_to] = 1;
713 return $arr;
717 * Get an array of existing categories, with the name in the key and sort key in the value.
719 * @return array
721 private function getExistingCategories() {
722 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
723 array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
724 $arr = array();
725 foreach ( $res as $row ) {
726 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
728 return $arr;
732 * Get an array of existing interlanguage links, with the language code in the key and the
733 * title in the value.
735 * @return array
737 private function getExistingInterlangs() {
738 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
739 array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
740 $arr = array();
741 foreach ( $res as $row ) {
742 $arr[$row->ll_lang] = $row->ll_title;
744 return $arr;
748 * Get an array of existing inline interwiki links, as a 2-D array
749 * @return array (prefix => array(dbkey => 1))
751 protected function getExistingInterwikis() {
752 $res = $this->mDb->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
753 array( 'iwl_from' => $this->mId ), __METHOD__, $this->mOptions );
754 $arr = array();
755 foreach ( $res as $row ) {
756 if ( !isset( $arr[$row->iwl_prefix] ) ) {
757 $arr[$row->iwl_prefix] = array();
759 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
761 return $arr;
765 * Get an array of existing categories, with the name in the key and sort key in the value.
767 * @return array
769 private function getExistingProperties() {
770 $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
771 array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
772 $arr = array();
773 foreach ( $res as $row ) {
774 $arr[$row->pp_propname] = $row->pp_value;
776 return $arr;
780 * Return the title object of the page being updated
781 * @return Title
783 public function getTitle() {
784 return $this->mTitle;
788 * Returns parser output
789 * @since 1.19
790 * @return ParserOutput
792 public function getParserOutput() {
793 return $this->mParserOutput;
797 * Return the list of images used as generated by the parser
798 * @return array
800 public function getImages() {
801 return $this->mImages;
805 * Invalidate any necessary link lists related to page property changes
806 * @param $changed
808 private function invalidateProperties( $changed ) {
809 global $wgPagePropLinkInvalidations;
811 foreach ( $changed as $name => $value ) {
812 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
813 $inv = $wgPagePropLinkInvalidations[$name];
814 if ( !is_array( $inv ) ) {
815 $inv = array( $inv );
817 foreach ( $inv as $table ) {
818 $update = new HTMLCacheUpdate( $this->mTitle, $table );
819 $update->doUpdate();
827 * Update object handling the cleanup of links tables after a page was deleted.
829 class LinksDeletionUpdate extends SqlDataUpdate {
831 protected $mPage; //!< WikiPage the wikipage that was deleted
834 * Constructor
836 * @param $page WikiPage Page we are updating
837 * @throws MWException
839 function __construct( WikiPage $page ) {
840 parent::__construct( false ); // no implicit transaction
842 $this->mPage = $page;
844 if ( !$page->exists() ) {
845 throw new MWException( "Page ID not known, perhaps the page doesn't exist?" );
850 * Do some database updates after deletion
852 public function doUpdate() {
853 $title = $this->mPage->getTitle();
854 $id = $this->mPage->getId();
856 # Delete restrictions for it
857 $this->mDb->delete( 'page_restrictions', array( 'pr_page' => $id ), __METHOD__ );
859 # Fix category table counts
860 $cats = array();
861 $res = $this->mDb->select( 'categorylinks', 'cl_to', array( 'cl_from' => $id ), __METHOD__ );
863 foreach ( $res as $row ) {
864 $cats[] = $row->cl_to;
867 $this->mPage->updateCategoryCounts( array(), $cats );
869 # If using cascading deletes, we can skip some explicit deletes
870 if ( !$this->mDb->cascadingDeletes() ) {
871 $this->mDb->delete( 'revision', array( 'rev_page' => $id ), __METHOD__ );
873 # Delete outgoing links
874 $this->mDb->delete( 'pagelinks', array( 'pl_from' => $id ), __METHOD__ );
875 $this->mDb->delete( 'imagelinks', array( 'il_from' => $id ), __METHOD__ );
876 $this->mDb->delete( 'categorylinks', array( 'cl_from' => $id ), __METHOD__ );
877 $this->mDb->delete( 'templatelinks', array( 'tl_from' => $id ), __METHOD__ );
878 $this->mDb->delete( 'externallinks', array( 'el_from' => $id ), __METHOD__ );
879 $this->mDb->delete( 'langlinks', array( 'll_from' => $id ), __METHOD__ );
880 $this->mDb->delete( 'iwlinks', array( 'iwl_from' => $id ), __METHOD__ );
881 $this->mDb->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__ );
882 $this->mDb->delete( 'page_props', array( 'pp_page' => $id ), __METHOD__ );
885 # If using cleanup triggers, we can skip some manual deletes
886 if ( !$this->mDb->cleanupTriggers() ) {
887 # Clean up recentchanges entries...
888 $this->mDb->delete( 'recentchanges',
889 array( 'rc_type != ' . RC_LOG,
890 'rc_namespace' => $title->getNamespace(),
891 'rc_title' => $title->getDBkey() ),
892 __METHOD__ );
893 $this->mDb->delete( 'recentchanges',
894 array( 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ),
895 __METHOD__ );
900 * Update all the appropriate counts in the category table.
901 * @param array $added associative array of category name => sort key
902 * @param array $deleted associative array of category name => sort key
904 function updateCategoryCounts( $added, $deleted ) {
905 $a = WikiPage::factory( $this->mTitle );
906 $a->updateCategoryCounts(
907 array_keys( $added ), array_keys( $deleted )