Merge "Fix positioning of jQuery.tipsy tooltip arrows"
[mediawiki.git] / includes / deferred / LinksUpdate.php
blob3021af192c0fc7c291d4b2846c3a17907244f7c3
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 implements EnqueueableDataUpdate {
29 // @todo make members protected, but make sure extensions don't break
31 /** @var int Page ID of the article linked from */
32 public $mId;
34 /** @var Title Title object of the article linked from */
35 public $mTitle;
37 /** @var ParserOutput */
38 public $mParserOutput;
40 /** @var array Map of title strings to IDs for the links in the document */
41 public $mLinks;
43 /** @var array DB keys of the images used, in the array key only */
44 public $mImages;
46 /** @var array Map of title strings to IDs for the template references, including broken ones */
47 public $mTemplates;
49 /** @var array URLs of external links, array key only */
50 public $mExternals;
52 /** @var array Map of category names to sort keys */
53 public $mCategories;
55 /** @var array Map of language codes to titles */
56 public $mInterlangs;
58 /** @var array Map of arbitrary name to value */
59 public $mProperties;
61 /** @var bool Whether to queue jobs for recursive updates */
62 public $mRecursive;
64 /** @var Revision Revision for which this update has been triggered */
65 private $mRevision;
67 /**
68 * @var null|array Added links if calculated.
70 private $linkInsertions = null;
72 /**
73 * @var null|array Deleted links if calculated.
75 private $linkDeletions = null;
77 /**
78 * @var User|null
80 private $user;
82 /**
83 * Constructor
85 * @param Title $title Title of the page we're updating
86 * @param ParserOutput $parserOutput Output from a full parse of this page
87 * @param bool $recursive Queue jobs for recursive updates?
88 * @throws MWException
90 function __construct( Title $title, ParserOutput $parserOutput, $recursive = true ) {
91 parent::__construct( false ); // no implicit transaction
93 $this->mTitle = $title;
94 $this->mId = $title->getArticleID( Title::GAID_FOR_UPDATE );
96 if ( !$this->mId ) {
97 throw new InvalidArgumentException(
98 "The Title object yields no ID. Perhaps the page doesn't exist?"
102 $this->mParserOutput = $parserOutput;
104 $this->mLinks = $parserOutput->getLinks();
105 $this->mImages = $parserOutput->getImages();
106 $this->mTemplates = $parserOutput->getTemplates();
107 $this->mExternals = $parserOutput->getExternalLinks();
108 $this->mCategories = $parserOutput->getCategories();
109 $this->mProperties = $parserOutput->getProperties();
110 $this->mInterwikis = $parserOutput->getInterwikiLinks();
112 # Convert the format of the interlanguage links
113 # I didn't want to change it in the ParserOutput, because that array is passed all
114 # the way back to the skin, so either a skin API break would be required, or an
115 # inefficient back-conversion.
116 $ill = $parserOutput->getLanguageLinks();
117 $this->mInterlangs = array();
118 foreach ( $ill as $link ) {
119 list( $key, $title ) = explode( ':', $link, 2 );
120 $this->mInterlangs[$key] = $title;
123 foreach ( $this->mCategories as &$sortkey ) {
124 # If the sortkey is longer then 255 bytes,
125 # it truncated by DB, and then doesn't get
126 # matched when comparing existing vs current
127 # categories, causing bug 25254.
128 # Also. substr behaves weird when given "".
129 if ( $sortkey !== '' ) {
130 $sortkey = substr( $sortkey, 0, 255 );
134 $this->mRecursive = $recursive;
136 Hooks::run( 'LinksUpdateConstructed', array( &$this ) );
140 * Update link tables with outgoing links from an updated article
142 public function doUpdate() {
143 Hooks::run( 'LinksUpdate', array( &$this ) );
144 $this->doIncrementalUpdate();
146 $that = $this;
147 $this->mDb->onTransactionIdle( function() use ( $that ) {
148 Hooks::run( 'LinksUpdateComplete', array( &$that ) );
149 } );
152 protected function doIncrementalUpdate() {
153 # Page links
154 $existing = $this->getExistingLinks();
155 $this->linkDeletions = $this->getLinkDeletions( $existing );
156 $this->linkInsertions = $this->getLinkInsertions( $existing );
157 $this->incrTableUpdate( 'pagelinks', 'pl', $this->linkDeletions, $this->linkInsertions );
159 # Image links
160 $existing = $this->getExistingImages();
162 $imageDeletes = $this->getImageDeletions( $existing );
163 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
164 $this->getImageInsertions( $existing ) );
166 # Invalidate all image description pages which had links added or removed
167 $imageUpdates = $imageDeletes + array_diff_key( $this->mImages, $existing );
168 $this->invalidateImageDescriptions( $imageUpdates );
170 # External links
171 $existing = $this->getExistingExternals();
172 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
173 $this->getExternalInsertions( $existing ) );
175 # Language links
176 $existing = $this->getExistingInterlangs();
177 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
178 $this->getInterlangInsertions( $existing ) );
180 # Inline interwiki links
181 $existing = $this->getExistingInterwikis();
182 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
183 $this->getInterwikiInsertions( $existing ) );
185 # Template links
186 $existing = $this->getExistingTemplates();
187 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
188 $this->getTemplateInsertions( $existing ) );
190 # Category links
191 $existing = $this->getExistingCategories();
193 $categoryDeletes = $this->getCategoryDeletions( $existing );
195 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
196 $this->getCategoryInsertions( $existing ) );
198 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
199 $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
200 $categoryUpdates = $categoryInserts + $categoryDeletes;
201 $this->invalidateCategories( $categoryUpdates );
202 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
204 # Page properties
205 $existing = $this->getExistingProperties();
207 $propertiesDeletes = $this->getPropertyDeletions( $existing );
209 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
210 $this->getPropertyInsertions( $existing ) );
212 # Invalidate the necessary pages
213 $changed = $propertiesDeletes + array_diff_assoc( $this->mProperties, $existing );
214 $this->invalidateProperties( $changed );
216 # Update the links table freshness for this title
217 $this->updateLinksTimestamp();
219 # Refresh links of all pages including this page
220 # This will be in a separate transaction
221 if ( $this->mRecursive ) {
222 $this->queueRecursiveJobs();
228 * Queue recursive jobs for this page
230 * Which means do LinksUpdate on all pages that include the current page,
231 * using the job queue.
233 protected function queueRecursiveJobs() {
234 self::queueRecursiveJobsForTable( $this->mTitle, 'templatelinks' );
235 if ( $this->mTitle->getNamespace() == NS_FILE ) {
236 // Process imagelinks in case the title is or was a redirect
237 self::queueRecursiveJobsForTable( $this->mTitle, 'imagelinks' );
240 $bc = $this->mTitle->getBacklinkCache();
241 // Get jobs for cascade-protected backlinks for a high priority queue.
242 // If meta-templates change to using a new template, the new template
243 // should be implicitly protected as soon as possible, if applicable.
244 // These jobs duplicate a subset of the above ones, but can run sooner.
245 // Which ever runs first generally no-ops the other one.
246 $jobs = array();
247 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
248 $jobs[] = RefreshLinksJob::newPrioritized( $title, array() );
250 JobQueueGroup::singleton()->push( $jobs );
254 * Queue a RefreshLinks job for any table.
256 * @param Title $title Title to do job for
257 * @param string $table Table to use (e.g. 'templatelinks')
259 public static function queueRecursiveJobsForTable( Title $title, $table ) {
260 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
261 $job = new RefreshLinksJob(
262 $title,
263 array(
264 'table' => $table,
265 'recursive' => true,
266 ) + Job::newRootJobParams( // "overall" refresh links job info
267 "refreshlinks:{$table}:{$title->getPrefixedText()}"
271 JobQueueGroup::singleton()->push( $job );
276 * @param array $cats
278 function invalidateCategories( $cats ) {
279 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
283 * Update all the appropriate counts in the category table.
284 * @param array $added Associative array of category name => sort key
285 * @param array $deleted Associative array of category name => sort key
287 function updateCategoryCounts( $added, $deleted ) {
288 $a = WikiPage::factory( $this->mTitle );
289 $a->updateCategoryCounts(
290 array_keys( $added ), array_keys( $deleted )
295 * @param array $images
297 function invalidateImageDescriptions( $images ) {
298 $this->invalidatePages( NS_FILE, array_keys( $images ) );
302 * Update a table by doing a delete query then an insert query
303 * @param string $table Table name
304 * @param string $prefix Field name prefix
305 * @param array $deletions
306 * @param array $insertions Rows to insert
308 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
309 if ( $table == 'page_props' ) {
310 $fromField = 'pp_page';
311 } else {
312 $fromField = "{$prefix}_from";
314 $where = array( $fromField => $this->mId );
315 if ( $table == 'pagelinks' || $table == 'templatelinks' || $table == 'iwlinks' ) {
316 if ( $table == 'iwlinks' ) {
317 $baseKey = 'iwl_prefix';
318 } else {
319 $baseKey = "{$prefix}_namespace";
321 $clause = $this->mDb->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
322 if ( $clause ) {
323 $where[] = $clause;
324 } else {
325 $where = false;
327 } else {
328 if ( $table == 'langlinks' ) {
329 $toField = 'll_lang';
330 } elseif ( $table == 'page_props' ) {
331 $toField = 'pp_propname';
332 } else {
333 $toField = $prefix . '_to';
335 if ( count( $deletions ) ) {
336 $where[$toField] = array_keys( $deletions );
337 } else {
338 $where = false;
341 if ( $where ) {
342 $this->mDb->delete( $table, $where, __METHOD__ );
344 if ( count( $insertions ) ) {
345 $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
346 Hooks::run( 'LinksUpdateAfterInsert', array( $this, $table, $insertions ) );
351 * Get an array of pagelinks insertions for passing to the DB
352 * Skips the titles specified by the 2-D array $existing
353 * @param array $existing
354 * @return array
356 private function getLinkInsertions( $existing = array() ) {
357 $arr = array();
358 foreach ( $this->mLinks as $ns => $dbkeys ) {
359 $diffs = isset( $existing[$ns] )
360 ? array_diff_key( $dbkeys, $existing[$ns] )
361 : $dbkeys;
362 foreach ( $diffs as $dbk => $id ) {
363 $arr[] = array(
364 'pl_from' => $this->mId,
365 'pl_from_namespace' => $this->mTitle->getNamespace(),
366 'pl_namespace' => $ns,
367 'pl_title' => $dbk
372 return $arr;
376 * Get an array of template insertions. Like getLinkInsertions()
377 * @param array $existing
378 * @return array
380 private function getTemplateInsertions( $existing = array() ) {
381 $arr = array();
382 foreach ( $this->mTemplates as $ns => $dbkeys ) {
383 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
384 foreach ( $diffs as $dbk => $id ) {
385 $arr[] = array(
386 'tl_from' => $this->mId,
387 'tl_from_namespace' => $this->mTitle->getNamespace(),
388 'tl_namespace' => $ns,
389 'tl_title' => $dbk
394 return $arr;
398 * Get an array of image insertions
399 * Skips the names specified in $existing
400 * @param array $existing
401 * @return array
403 private function getImageInsertions( $existing = array() ) {
404 $arr = array();
405 $diffs = array_diff_key( $this->mImages, $existing );
406 foreach ( $diffs as $iname => $dummy ) {
407 $arr[] = array(
408 'il_from' => $this->mId,
409 'il_from_namespace' => $this->mTitle->getNamespace(),
410 'il_to' => $iname
414 return $arr;
418 * Get an array of externallinks insertions. Skips the names specified in $existing
419 * @param array $existing
420 * @return array
422 private function getExternalInsertions( $existing = array() ) {
423 $arr = array();
424 $diffs = array_diff_key( $this->mExternals, $existing );
425 foreach ( $diffs as $url => $dummy ) {
426 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
427 $arr[] = array(
428 'el_id' => $this->mDb->nextSequenceValue( 'externallinks_el_id_seq' ),
429 'el_from' => $this->mId,
430 'el_to' => $url,
431 'el_index' => $index,
436 return $arr;
440 * Get an array of category insertions
442 * @param array $existing Mapping existing category names to sort keys. If both
443 * match a link in $this, the link will be omitted from the output
445 * @return array
447 private function getCategoryInsertions( $existing = array() ) {
448 global $wgContLang, $wgCategoryCollation;
449 $diffs = array_diff_assoc( $this->mCategories, $existing );
450 $arr = array();
451 foreach ( $diffs as $name => $prefix ) {
452 $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
453 $wgContLang->findVariantLink( $name, $nt, true );
455 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
456 $type = 'subcat';
457 } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
458 $type = 'file';
459 } else {
460 $type = 'page';
463 # Treat custom sortkeys as a prefix, so that if multiple
464 # things are forced to sort as '*' or something, they'll
465 # sort properly in the category rather than in page_id
466 # order or such.
467 $sortkey = Collation::singleton()->getSortKey(
468 $this->mTitle->getCategorySortkey( $prefix ) );
470 $arr[] = array(
471 'cl_from' => $this->mId,
472 'cl_to' => $name,
473 'cl_sortkey' => $sortkey,
474 'cl_timestamp' => $this->mDb->timestamp(),
475 'cl_sortkey_prefix' => $prefix,
476 'cl_collation' => $wgCategoryCollation,
477 'cl_type' => $type,
481 return $arr;
485 * Get an array of interlanguage link insertions
487 * @param array $existing Mapping existing language codes to titles
489 * @return array
491 private function getInterlangInsertions( $existing = array() ) {
492 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
493 $arr = array();
494 foreach ( $diffs as $lang => $title ) {
495 $arr[] = array(
496 'll_from' => $this->mId,
497 'll_lang' => $lang,
498 'll_title' => $title
502 return $arr;
506 * Get an array of page property insertions
507 * @param array $existing
508 * @return array
510 function getPropertyInsertions( $existing = array() ) {
511 $diffs = array_diff_assoc( $this->mProperties, $existing );
513 $arr = array();
514 foreach ( array_keys( $diffs ) as $name ) {
515 $arr[] = $this->getPagePropRowData( $name );
518 return $arr;
522 * Returns an associative array to be used for inserting a row into
523 * the page_props table. Besides the given property name, this will
524 * include the page id from $this->mId and any property value from
525 * $this->mProperties.
527 * The array returned will include the pp_sortkey field if this
528 * is present in the database (as indicated by $wgPagePropsHaveSortkey).
529 * The sortkey value is currently determined by getPropertySortKeyValue().
531 * @note this assumes that $this->mProperties[$prop] is defined.
533 * @param string $prop The name of the property.
535 * @return array
537 private function getPagePropRowData( $prop ) {
538 global $wgPagePropsHaveSortkey;
540 $value = $this->mProperties[$prop];
542 $row = array(
543 'pp_page' => $this->mId,
544 'pp_propname' => $prop,
545 'pp_value' => $value,
548 if ( $wgPagePropsHaveSortkey ) {
549 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
552 return $row;
556 * Determines the sort key for the given property value.
557 * This will return $value if it is a float or int,
558 * 1 or resp. 0 if it is a bool, and null otherwise.
560 * @note In the future, we may allow the sortkey to be specified explicitly
561 * in ParserOutput::setProperty.
563 * @param mixed $value
565 * @return float|null
567 private function getPropertySortKeyValue( $value ) {
568 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
569 return floatval( $value );
572 return null;
576 * Get an array of interwiki insertions for passing to the DB
577 * Skips the titles specified by the 2-D array $existing
578 * @param array $existing
579 * @return array
581 private function getInterwikiInsertions( $existing = array() ) {
582 $arr = array();
583 foreach ( $this->mInterwikis as $prefix => $dbkeys ) {
584 $diffs = isset( $existing[$prefix] )
585 ? array_diff_key( $dbkeys, $existing[$prefix] )
586 : $dbkeys;
588 foreach ( $diffs as $dbk => $id ) {
589 $arr[] = array(
590 'iwl_from' => $this->mId,
591 'iwl_prefix' => $prefix,
592 'iwl_title' => $dbk
597 return $arr;
601 * Given an array of existing links, returns those links which are not in $this
602 * and thus should be deleted.
603 * @param array $existing
604 * @return array
606 private function getLinkDeletions( $existing ) {
607 $del = array();
608 foreach ( $existing as $ns => $dbkeys ) {
609 if ( isset( $this->mLinks[$ns] ) ) {
610 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
611 } else {
612 $del[$ns] = $existing[$ns];
616 return $del;
620 * Given an array of existing templates, returns those templates which are not in $this
621 * and thus should be deleted.
622 * @param array $existing
623 * @return array
625 private function getTemplateDeletions( $existing ) {
626 $del = array();
627 foreach ( $existing as $ns => $dbkeys ) {
628 if ( isset( $this->mTemplates[$ns] ) ) {
629 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
630 } else {
631 $del[$ns] = $existing[$ns];
635 return $del;
639 * Given an array of existing images, returns those images which are not in $this
640 * and thus should be deleted.
641 * @param array $existing
642 * @return array
644 private function getImageDeletions( $existing ) {
645 return array_diff_key( $existing, $this->mImages );
649 * Given an array of existing external links, returns those links which are not
650 * in $this and thus should be deleted.
651 * @param array $existing
652 * @return array
654 private function getExternalDeletions( $existing ) {
655 return array_diff_key( $existing, $this->mExternals );
659 * Given an array of existing categories, returns those categories which are not in $this
660 * and thus should be deleted.
661 * @param array $existing
662 * @return array
664 private function getCategoryDeletions( $existing ) {
665 return array_diff_assoc( $existing, $this->mCategories );
669 * Given an array of existing interlanguage links, returns those links which are not
670 * in $this and thus should be deleted.
671 * @param array $existing
672 * @return array
674 private function getInterlangDeletions( $existing ) {
675 return array_diff_assoc( $existing, $this->mInterlangs );
679 * Get array of properties which should be deleted.
680 * @param array $existing
681 * @return array
683 function getPropertyDeletions( $existing ) {
684 return array_diff_assoc( $existing, $this->mProperties );
688 * Given an array of existing interwiki links, returns those links which are not in $this
689 * and thus should be deleted.
690 * @param array $existing
691 * @return array
693 private function getInterwikiDeletions( $existing ) {
694 $del = array();
695 foreach ( $existing as $prefix => $dbkeys ) {
696 if ( isset( $this->mInterwikis[$prefix] ) ) {
697 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis[$prefix] );
698 } else {
699 $del[$prefix] = $existing[$prefix];
703 return $del;
707 * Get an array of existing links, as a 2-D array
709 * @return array
711 private function getExistingLinks() {
712 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
713 array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
714 $arr = array();
715 foreach ( $res as $row ) {
716 if ( !isset( $arr[$row->pl_namespace] ) ) {
717 $arr[$row->pl_namespace] = array();
719 $arr[$row->pl_namespace][$row->pl_title] = 1;
722 return $arr;
726 * Get an array of existing templates, as a 2-D array
728 * @return array
730 private function getExistingTemplates() {
731 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
732 array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
733 $arr = array();
734 foreach ( $res as $row ) {
735 if ( !isset( $arr[$row->tl_namespace] ) ) {
736 $arr[$row->tl_namespace] = array();
738 $arr[$row->tl_namespace][$row->tl_title] = 1;
741 return $arr;
745 * Get an array of existing images, image names in the keys
747 * @return array
749 private function getExistingImages() {
750 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
751 array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
752 $arr = array();
753 foreach ( $res as $row ) {
754 $arr[$row->il_to] = 1;
757 return $arr;
761 * Get an array of existing external links, URLs in the keys
763 * @return array
765 private function getExistingExternals() {
766 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
767 array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
768 $arr = array();
769 foreach ( $res as $row ) {
770 $arr[$row->el_to] = 1;
773 return $arr;
777 * Get an array of existing categories, with the name in the key and sort key in the value.
779 * @return array
781 private function getExistingCategories() {
782 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
783 array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
784 $arr = array();
785 foreach ( $res as $row ) {
786 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
789 return $arr;
793 * Get an array of existing interlanguage links, with the language code in the key and the
794 * title in the value.
796 * @return array
798 private function getExistingInterlangs() {
799 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
800 array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
801 $arr = array();
802 foreach ( $res as $row ) {
803 $arr[$row->ll_lang] = $row->ll_title;
806 return $arr;
810 * Get an array of existing inline interwiki links, as a 2-D array
811 * @return array (prefix => array(dbkey => 1))
813 protected function getExistingInterwikis() {
814 $res = $this->mDb->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
815 array( 'iwl_from' => $this->mId ), __METHOD__, $this->mOptions );
816 $arr = array();
817 foreach ( $res as $row ) {
818 if ( !isset( $arr[$row->iwl_prefix] ) ) {
819 $arr[$row->iwl_prefix] = array();
821 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
824 return $arr;
828 * Get an array of existing categories, with the name in the key and sort key in the value.
830 * @return array Array of property names and values
832 private function getExistingProperties() {
833 $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
834 array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
835 $arr = array();
836 foreach ( $res as $row ) {
837 $arr[$row->pp_propname] = $row->pp_value;
840 return $arr;
844 * Return the title object of the page being updated
845 * @return Title
847 public function getTitle() {
848 return $this->mTitle;
852 * Returns parser output
853 * @since 1.19
854 * @return ParserOutput
856 public function getParserOutput() {
857 return $this->mParserOutput;
861 * Return the list of images used as generated by the parser
862 * @return array
864 public function getImages() {
865 return $this->mImages;
869 * Set the revision corresponding to this LinksUpdate
871 * @since 1.27
873 * @param Revision $revision
875 public function setRevision( Revision $revision ) {
876 $this->mRevision = $revision;
880 * Set the User who triggered this LinksUpdate
882 * @since 1.27
883 * @param User $user
885 public function setTriggeringUser( User $user ) {
886 $this->user = $user;
890 * @since 1.27
891 * @return null|User
893 public function getTriggeringUser() {
894 return $this->user;
898 * Invalidate any necessary link lists related to page property changes
899 * @param array $changed
901 private function invalidateProperties( $changed ) {
902 global $wgPagePropLinkInvalidations;
904 foreach ( $changed as $name => $value ) {
905 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
906 $inv = $wgPagePropLinkInvalidations[$name];
907 if ( !is_array( $inv ) ) {
908 $inv = array( $inv );
910 foreach ( $inv as $table ) {
911 DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->mTitle, $table ) );
918 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
919 * @since 1.22
920 * @return null|array Array of Titles
922 public function getAddedLinks() {
923 if ( $this->linkInsertions === null ) {
924 return null;
926 $result = array();
927 foreach ( $this->linkInsertions as $insertion ) {
928 $result[] = Title::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
931 return $result;
935 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
936 * @since 1.22
937 * @return null|array Array of Titles
939 public function getRemovedLinks() {
940 if ( $this->linkDeletions === null ) {
941 return null;
943 $result = array();
944 foreach ( $this->linkDeletions as $ns => $titles ) {
945 foreach ( $titles as $title => $unused ) {
946 $result[] = Title::makeTitle( $ns, $title );
950 return $result;
954 * Update links table freshness
956 protected function updateLinksTimestamp() {
957 if ( $this->mId ) {
958 // The link updates made here only reflect the freshness of the parser output
959 $timestamp = $this->mParserOutput->getCacheTime();
960 $this->mDb->update( 'page',
961 array( 'page_links_updated' => $this->mDb->timestamp( $timestamp ) ),
962 array( 'page_id' => $this->mId ),
963 __METHOD__
968 public function getAsJobSpecification() {
969 if ( $this->user ) {
970 $userInfo = array(
971 'userId' => $this->user->getId(),
972 'userName' => $this->user->getName(),
974 } else {
975 $userInfo = false;
978 if ( $this->mRevision ) {
979 $triggeringRevisionId = $this->mRevision->getId();
980 } else {
981 $triggeringRevisionId = false;
984 return array(
985 'wiki' => $this->mDb->getWikiID(),
986 'job' => new JobSpecification(
987 'refreshLinksPrioritized',
988 array(
989 // Reuse the parser cache if it was saved
990 'rootJobTimestamp' => $this->mParserOutput->getCacheTime(),
991 'useRecursiveLinksUpdate' => $this->mRecursive,
992 'triggeringUser' => $userInfo,
993 'triggeringRevisionId' => $triggeringRevisionId,
995 array( 'removeDuplicates' => true ),
996 $this->getTitle()