Do not show empty parenthesis on log entry with no block flags
[mediawiki.git] / includes / LinksUpdate.php
blob4b1b5b888cd1d5783e610b551665324952745e47
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__ );
240 function queueRecursiveJobs() {
241 wfProfileIn( __METHOD__ );
243 if ( $this->mTitle->getBacklinkCache()->hasLinks( 'templatelinks' ) ) {
244 $job = new RefreshLinksJob2(
245 $this->mTitle,
246 array(
247 'table' => 'templatelinks',
248 ) + Job::newRootJobParams( // "overall" refresh links job info
249 "refreshlinks:templatelinks:{$this->mTitle->getPrefixedText()}"
252 JobQueueGroup::singleton()->push( $job );
253 JobQueueGroup::singleton()->deduplicateRootJob( $job );
256 wfProfileOut( __METHOD__ );
260 * @param $cats
262 function invalidateCategories( $cats ) {
263 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
267 * Update all the appropriate counts in the category table.
268 * @param array $added associative array of category name => sort key
269 * @param array $deleted associative array of category name => sort key
271 function updateCategoryCounts( $added, $deleted ) {
272 $a = WikiPage::factory( $this->mTitle );
273 $a->updateCategoryCounts(
274 array_keys( $added ), array_keys( $deleted )
279 * @param $images
281 function invalidateImageDescriptions( $images ) {
282 $this->invalidatePages( NS_FILE, array_keys( $images ) );
286 * @param $table
287 * @param $insertions
288 * @param $fromField
290 private function dumbTableUpdate( $table, $insertions, $fromField ) {
291 $this->mDb->delete( $table, array( $fromField => $this->mId ), __METHOD__ );
292 if ( count( $insertions ) ) {
293 # The link array was constructed without FOR UPDATE, so there may
294 # be collisions. This may cause minor link table inconsistencies,
295 # which is better than crippling the site with lock contention.
296 $this->mDb->insert( $table, $insertions, __METHOD__, array( 'IGNORE' ) );
301 * Update a table by doing a delete query then an insert query
302 * @param $table
303 * @param $prefix
304 * @param $deletions
305 * @param $insertions
307 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
308 if ( $table == 'page_props' ) {
309 $fromField = 'pp_page';
310 } else {
311 $fromField = "{$prefix}_from";
313 $where = array( $fromField => $this->mId );
314 if ( $table == 'pagelinks' || $table == 'templatelinks' || $table == 'iwlinks' ) {
315 if ( $table == 'iwlinks' ) {
316 $baseKey = 'iwl_prefix';
317 } else {
318 $baseKey = "{$prefix}_namespace";
320 $clause = $this->mDb->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
321 if ( $clause ) {
322 $where[] = $clause;
323 } else {
324 $where = false;
326 } else {
327 if ( $table == 'langlinks' ) {
328 $toField = 'll_lang';
329 } elseif ( $table == 'page_props' ) {
330 $toField = 'pp_propname';
331 } else {
332 $toField = $prefix . '_to';
334 if ( count( $deletions ) ) {
335 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
336 } else {
337 $where = false;
340 if ( $where ) {
341 $this->mDb->delete( $table, $where, __METHOD__ );
343 if ( count( $insertions ) ) {
344 $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
345 wfRunHooks( 'LinksUpdateAfterInsert', array( $this, $table, $insertions ) );
350 * Get an array of pagelinks insertions for passing to the DB
351 * Skips the titles specified by the 2-D array $existing
352 * @param $existing array
353 * @return array
355 private function getLinkInsertions( $existing = array() ) {
356 $arr = array();
357 foreach ( $this->mLinks as $ns => $dbkeys ) {
358 $diffs = isset( $existing[$ns] )
359 ? array_diff_key( $dbkeys, $existing[$ns] )
360 : $dbkeys;
361 foreach ( $diffs as $dbk => $id ) {
362 $arr[] = array(
363 'pl_from' => $this->mId,
364 'pl_namespace' => $ns,
365 'pl_title' => $dbk
369 return $arr;
373 * Get an array of template insertions. Like getLinkInsertions()
374 * @param $existing array
375 * @return array
377 private function getTemplateInsertions( $existing = array() ) {
378 $arr = array();
379 foreach ( $this->mTemplates as $ns => $dbkeys ) {
380 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
381 foreach ( $diffs as $dbk => $id ) {
382 $arr[] = array(
383 'tl_from' => $this->mId,
384 'tl_namespace' => $ns,
385 'tl_title' => $dbk
389 return $arr;
393 * Get an array of image insertions
394 * Skips the names specified in $existing
395 * @param $existing array
396 * @return array
398 private function getImageInsertions( $existing = array() ) {
399 $arr = array();
400 $diffs = array_diff_key( $this->mImages, $existing );
401 foreach ( $diffs as $iname => $dummy ) {
402 $arr[] = array(
403 'il_from' => $this->mId,
404 'il_to' => $iname
407 return $arr;
411 * Get an array of externallinks insertions. Skips the names specified in $existing
412 * @param $existing array
413 * @return array
415 private function getExternalInsertions( $existing = array() ) {
416 $arr = array();
417 $diffs = array_diff_key( $this->mExternals, $existing );
418 foreach ( $diffs as $url => $dummy ) {
419 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
420 $arr[] = array(
421 'el_from' => $this->mId,
422 'el_to' => $url,
423 'el_index' => $index,
427 return $arr;
431 * Get an array of category insertions
433 * @param array $existing mapping existing category names to sort keys. If both
434 * match a link in $this, the link will be omitted from the output
436 * @return array
438 private function getCategoryInsertions( $existing = array() ) {
439 global $wgContLang, $wgCategoryCollation;
440 $diffs = array_diff_assoc( $this->mCategories, $existing );
441 $arr = array();
442 foreach ( $diffs as $name => $prefix ) {
443 $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
444 $wgContLang->findVariantLink( $name, $nt, true );
446 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
447 $type = 'subcat';
448 } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
449 $type = 'file';
450 } else {
451 $type = 'page';
454 # Treat custom sortkeys as a prefix, so that if multiple
455 # things are forced to sort as '*' or something, they'll
456 # sort properly in the category rather than in page_id
457 # order or such.
458 $sortkey = Collation::singleton()->getSortKey(
459 $this->mTitle->getCategorySortkey( $prefix ) );
461 $arr[] = array(
462 'cl_from' => $this->mId,
463 'cl_to' => $name,
464 'cl_sortkey' => $sortkey,
465 'cl_timestamp' => $this->mDb->timestamp(),
466 'cl_sortkey_prefix' => $prefix,
467 'cl_collation' => $wgCategoryCollation,
468 'cl_type' => $type,
471 return $arr;
475 * Get an array of interlanguage link insertions
477 * @param array $existing mapping existing language codes to titles
479 * @return array
481 private function getInterlangInsertions( $existing = array() ) {
482 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
483 $arr = array();
484 foreach ( $diffs as $lang => $title ) {
485 $arr[] = array(
486 'll_from' => $this->mId,
487 'll_lang' => $lang,
488 'll_title' => $title
491 return $arr;
495 * Get an array of page property insertions
496 * @param $existing array
497 * @return array
499 function getPropertyInsertions( $existing = array() ) {
500 $diffs = array_diff_assoc( $this->mProperties, $existing );
501 $arr = array();
502 foreach ( $diffs as $name => $value ) {
503 $arr[] = array(
504 'pp_page' => $this->mId,
505 'pp_propname' => $name,
506 'pp_value' => $value,
509 return $arr;
513 * Get an array of interwiki insertions for passing to the DB
514 * Skips the titles specified by the 2-D array $existing
515 * @param $existing array
516 * @return array
518 private function getInterwikiInsertions( $existing = array() ) {
519 $arr = array();
520 foreach ( $this->mInterwikis as $prefix => $dbkeys ) {
521 $diffs = isset( $existing[$prefix] ) ? array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
522 foreach ( $diffs as $dbk => $id ) {
523 $arr[] = array(
524 'iwl_from' => $this->mId,
525 'iwl_prefix' => $prefix,
526 'iwl_title' => $dbk
530 return $arr;
534 * Given an array of existing links, returns those links which are not in $this
535 * and thus should be deleted.
536 * @param $existing array
537 * @return array
539 private function getLinkDeletions( $existing ) {
540 $del = array();
541 foreach ( $existing as $ns => $dbkeys ) {
542 if ( isset( $this->mLinks[$ns] ) ) {
543 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
544 } else {
545 $del[$ns] = $existing[$ns];
548 return $del;
552 * Given an array of existing templates, returns those templates which are not in $this
553 * and thus should be deleted.
554 * @param $existing array
555 * @return array
557 private function getTemplateDeletions( $existing ) {
558 $del = array();
559 foreach ( $existing as $ns => $dbkeys ) {
560 if ( isset( $this->mTemplates[$ns] ) ) {
561 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
562 } else {
563 $del[$ns] = $existing[$ns];
566 return $del;
570 * Given an array of existing images, returns those images which are not in $this
571 * and thus should be deleted.
572 * @param $existing array
573 * @return array
575 private function getImageDeletions( $existing ) {
576 return array_diff_key( $existing, $this->mImages );
580 * Given an array of existing external links, returns those links which are not
581 * in $this and thus should be deleted.
582 * @param $existing array
583 * @return array
585 private function getExternalDeletions( $existing ) {
586 return array_diff_key( $existing, $this->mExternals );
590 * Given an array of existing categories, returns those categories which are not in $this
591 * and thus should be deleted.
592 * @param $existing array
593 * @return array
595 private function getCategoryDeletions( $existing ) {
596 return array_diff_assoc( $existing, $this->mCategories );
600 * Given an array of existing interlanguage links, returns those links which are not
601 * in $this and thus should be deleted.
602 * @param $existing array
603 * @return array
605 private function getInterlangDeletions( $existing ) {
606 return array_diff_assoc( $existing, $this->mInterlangs );
610 * Get array of properties which should be deleted.
611 * @param $existing array
612 * @return array
614 function getPropertyDeletions( $existing ) {
615 return array_diff_assoc( $existing, $this->mProperties );
619 * Given an array of existing interwiki links, returns those links which are not in $this
620 * and thus should be deleted.
621 * @param $existing array
622 * @return array
624 private function getInterwikiDeletions( $existing ) {
625 $del = array();
626 foreach ( $existing as $prefix => $dbkeys ) {
627 if ( isset( $this->mInterwikis[$prefix] ) ) {
628 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis[$prefix] );
629 } else {
630 $del[$prefix] = $existing[$prefix];
633 return $del;
637 * Get an array of existing links, as a 2-D array
639 * @return array
641 private function getExistingLinks() {
642 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
643 array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
644 $arr = array();
645 foreach ( $res as $row ) {
646 if ( !isset( $arr[$row->pl_namespace] ) ) {
647 $arr[$row->pl_namespace] = array();
649 $arr[$row->pl_namespace][$row->pl_title] = 1;
651 return $arr;
655 * Get an array of existing templates, as a 2-D array
657 * @return array
659 private function getExistingTemplates() {
660 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
661 array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
662 $arr = array();
663 foreach ( $res as $row ) {
664 if ( !isset( $arr[$row->tl_namespace] ) ) {
665 $arr[$row->tl_namespace] = array();
667 $arr[$row->tl_namespace][$row->tl_title] = 1;
669 return $arr;
673 * Get an array of existing images, image names in the keys
675 * @return array
677 private function getExistingImages() {
678 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
679 array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
680 $arr = array();
681 foreach ( $res as $row ) {
682 $arr[$row->il_to] = 1;
684 return $arr;
688 * Get an array of existing external links, URLs in the keys
690 * @return array
692 private function getExistingExternals() {
693 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
694 array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
695 $arr = array();
696 foreach ( $res as $row ) {
697 $arr[$row->el_to] = 1;
699 return $arr;
703 * Get an array of existing categories, with the name in the key and sort key in the value.
705 * @return array
707 private function getExistingCategories() {
708 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
709 array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
710 $arr = array();
711 foreach ( $res as $row ) {
712 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
714 return $arr;
718 * Get an array of existing interlanguage links, with the language code in the key and the
719 * title in the value.
721 * @return array
723 private function getExistingInterlangs() {
724 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
725 array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
726 $arr = array();
727 foreach ( $res as $row ) {
728 $arr[$row->ll_lang] = $row->ll_title;
730 return $arr;
734 * Get an array of existing inline interwiki links, as a 2-D array
735 * @return array (prefix => array(dbkey => 1))
737 protected function getExistingInterwikis() {
738 $res = $this->mDb->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
739 array( 'iwl_from' => $this->mId ), __METHOD__, $this->mOptions );
740 $arr = array();
741 foreach ( $res as $row ) {
742 if ( !isset( $arr[$row->iwl_prefix] ) ) {
743 $arr[$row->iwl_prefix] = array();
745 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
747 return $arr;
751 * Get an array of existing categories, with the name in the key and sort key in the value.
753 * @return array
755 private function getExistingProperties() {
756 $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
757 array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
758 $arr = array();
759 foreach ( $res as $row ) {
760 $arr[$row->pp_propname] = $row->pp_value;
762 return $arr;
766 * Return the title object of the page being updated
767 * @return Title
769 public function getTitle() {
770 return $this->mTitle;
774 * Returns parser output
775 * @since 1.19
776 * @return ParserOutput
778 public function getParserOutput() {
779 return $this->mParserOutput;
783 * Return the list of images used as generated by the parser
784 * @return array
786 public function getImages() {
787 return $this->mImages;
791 * Invalidate any necessary link lists related to page property changes
792 * @param $changed
794 private function invalidateProperties( $changed ) {
795 global $wgPagePropLinkInvalidations;
797 foreach ( $changed as $name => $value ) {
798 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
799 $inv = $wgPagePropLinkInvalidations[$name];
800 if ( !is_array( $inv ) ) {
801 $inv = array( $inv );
803 foreach ( $inv as $table ) {
804 $update = new HTMLCacheUpdate( $this->mTitle, $table );
805 $update->doUpdate();
813 * Update object handling the cleanup of links tables after a page was deleted.
815 class LinksDeletionUpdate extends SqlDataUpdate {
817 protected $mPage; //!< WikiPage the wikipage that was deleted
820 * Constructor
822 * @param $page WikiPage Page we are updating
823 * @throws MWException
825 function __construct( WikiPage $page ) {
826 parent::__construct( false ); // no implicit transaction
828 $this->mPage = $page;
830 if ( !$page->exists() ) {
831 throw new MWException( "Page ID not known, perhaps the page doesn't exist?" );
836 * Do some database updates after deletion
838 public function doUpdate() {
839 $title = $this->mPage->getTitle();
840 $id = $this->mPage->getId();
842 # Delete restrictions for it
843 $this->mDb->delete( 'page_restrictions', array( 'pr_page' => $id ), __METHOD__ );
845 # Fix category table counts
846 $cats = array();
847 $res = $this->mDb->select( 'categorylinks', 'cl_to', array( 'cl_from' => $id ), __METHOD__ );
849 foreach ( $res as $row ) {
850 $cats[] = $row->cl_to;
853 $this->mPage->updateCategoryCounts( array(), $cats );
855 # If using cascading deletes, we can skip some explicit deletes
856 if ( !$this->mDb->cascadingDeletes() ) {
857 $this->mDb->delete( 'revision', array( 'rev_page' => $id ), __METHOD__ );
859 # Delete outgoing links
860 $this->mDb->delete( 'pagelinks', array( 'pl_from' => $id ), __METHOD__ );
861 $this->mDb->delete( 'imagelinks', array( 'il_from' => $id ), __METHOD__ );
862 $this->mDb->delete( 'categorylinks', array( 'cl_from' => $id ), __METHOD__ );
863 $this->mDb->delete( 'templatelinks', array( 'tl_from' => $id ), __METHOD__ );
864 $this->mDb->delete( 'externallinks', array( 'el_from' => $id ), __METHOD__ );
865 $this->mDb->delete( 'langlinks', array( 'll_from' => $id ), __METHOD__ );
866 $this->mDb->delete( 'iwlinks', array( 'iwl_from' => $id ), __METHOD__ );
867 $this->mDb->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__ );
868 $this->mDb->delete( 'page_props', array( 'pp_page' => $id ), __METHOD__ );
871 # If using cleanup triggers, we can skip some manual deletes
872 if ( !$this->mDb->cleanupTriggers() ) {
873 # Clean up recentchanges entries...
874 $this->mDb->delete( 'recentchanges',
875 array( 'rc_type != ' . RC_LOG,
876 'rc_namespace' => $title->getNamespace(),
877 'rc_title' => $title->getDBkey() ),
878 __METHOD__ );
879 $this->mDb->delete( 'recentchanges',
880 array( 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ),
881 __METHOD__ );
886 * Update all the appropriate counts in the category table.
887 * @param array $added associative array of category name => sort key
888 * @param array $deleted associative array of category name => sort key
890 function updateCategoryCounts( $added, $deleted ) {
891 $a = WikiPage::factory( $this->mTitle );
892 $a->updateCategoryCounts(
893 array_keys( $added ), array_keys( $deleted )