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
23 use MediaWiki\MediaWikiServices
;
24 use Wikimedia\ScopedCallback
;
27 * Class the manages updates of *_link tables as well as similar extension-managed tables
29 * @note: LinksUpdate is managed by DeferredUpdates::execute(). Do not run this in a transaction.
31 * See docs/deferred.txt
33 class LinksUpdate
extends DataUpdate
implements EnqueueableDataUpdate
{
34 // @todo make members protected, but make sure extensions don't break
36 /** @var int Page ID of the article linked from */
39 /** @var Title Title object of the article linked from */
42 /** @var ParserOutput */
43 public $mParserOutput;
45 /** @var array Map of title strings to IDs for the links in the document */
48 /** @var array DB keys of the images used, in the array key only */
51 /** @var array Map of title strings to IDs for the template references, including broken ones */
54 /** @var array URLs of external links, array key only */
57 /** @var array Map of category names to sort keys */
60 /** @var array Map of language codes to titles */
63 /** @var array 2-D map of (prefix => DBK => 1) */
66 /** @var array Map of arbitrary name to value */
69 /** @var bool Whether to queue jobs for recursive updates */
72 /** @var Revision Revision for which this update has been triggered */
76 * @var null|array Added links if calculated.
78 private $linkInsertions = null;
81 * @var null|array Deleted links if calculated.
83 private $linkDeletions = null;
86 * @var null|array Added properties if calculated.
88 private $propertyInsertions = null;
91 * @var null|array Deleted properties if calculated.
93 private $propertyDeletions = null;
100 /** @var IDatabase */
106 * @param Title $title Title of the page we're updating
107 * @param ParserOutput $parserOutput Output from a full parse of this page
108 * @param bool $recursive Queue jobs for recursive updates?
109 * @throws MWException
111 function __construct( Title
$title, ParserOutput
$parserOutput, $recursive = true ) {
112 parent
::__construct();
114 $this->mTitle
= $title;
115 $this->mId
= $title->getArticleID( Title
::GAID_FOR_UPDATE
);
118 throw new InvalidArgumentException(
119 "The Title object yields no ID. Perhaps the page doesn't exist?"
123 $this->mParserOutput
= $parserOutput;
125 $this->mLinks
= $parserOutput->getLinks();
126 $this->mImages
= $parserOutput->getImages();
127 $this->mTemplates
= $parserOutput->getTemplates();
128 $this->mExternals
= $parserOutput->getExternalLinks();
129 $this->mCategories
= $parserOutput->getCategories();
130 $this->mProperties
= $parserOutput->getProperties();
131 $this->mInterwikis
= $parserOutput->getInterwikiLinks();
133 # Convert the format of the interlanguage links
134 # I didn't want to change it in the ParserOutput, because that array is passed all
135 # the way back to the skin, so either a skin API break would be required, or an
136 # inefficient back-conversion.
137 $ill = $parserOutput->getLanguageLinks();
138 $this->mInterlangs
= [];
139 foreach ( $ill as $link ) {
140 list( $key, $title ) = explode( ':', $link, 2 );
141 $this->mInterlangs
[$key] = $title;
144 foreach ( $this->mCategories
as &$sortkey ) {
145 # If the sortkey is longer then 255 bytes,
146 # it truncated by DB, and then doesn't get
147 # matched when comparing existing vs current
148 # categories, causing bug 25254.
149 # Also. substr behaves weird when given "".
150 if ( $sortkey !== '' ) {
151 $sortkey = substr( $sortkey, 0, 255 );
155 $this->mRecursive
= $recursive;
157 Hooks
::run( 'LinksUpdateConstructed', [ &$this ] );
161 * Update link tables with outgoing links from an updated article
163 * @note: this is managed by DeferredUpdates::execute(). Do not run this in a transaction.
165 public function doUpdate() {
166 if ( $this->ticket
) {
167 // Make sure all links update threads see the changes of each other.
168 // This handles the case when updates have to batched into several COMMITs.
169 $scopedLock = self
::acquirePageLock( $this->getDB(), $this->mId
);
172 Hooks
::run( 'LinksUpdate', [ &$this ] );
173 $this->doIncrementalUpdate();
175 // Commit and release the lock (if set)
176 ScopedCallback
::consume( $scopedLock );
177 // Run post-commit hooks without DBO_TRX
178 $this->getDB()->onTransactionIdle(
180 Hooks
::run( 'LinksUpdateComplete', [ &$this, $this->ticket
] );
187 * Acquire a lock for performing link table updates for a page on a DB
189 * @param IDatabase $dbw
190 * @param integer $pageId
191 * @param string $why One of (job, atomicity)
192 * @return ScopedCallback
193 * @throws RuntimeException
196 public static function acquirePageLock( IDatabase
$dbw, $pageId, $why = 'atomicity' ) {
197 $key = "LinksUpdate:$why:pageid:$pageId";
198 $scopedLock = $dbw->getScopedLockAndFlush( $key, __METHOD__
, 15 );
199 if ( !$scopedLock ) {
200 throw new RuntimeException( "Could not acquire lock '$key'." );
206 protected function doIncrementalUpdate() {
208 $existing = $this->getExistingLinks();
209 $this->linkDeletions
= $this->getLinkDeletions( $existing );
210 $this->linkInsertions
= $this->getLinkInsertions( $existing );
211 $this->incrTableUpdate( 'pagelinks', 'pl', $this->linkDeletions
, $this->linkInsertions
);
214 $existing = $this->getExistingImages();
215 $imageDeletes = $this->getImageDeletions( $existing );
216 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
217 $this->getImageInsertions( $existing ) );
219 # Invalidate all image description pages which had links added or removed
220 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existing );
221 $this->invalidateImageDescriptions( $imageUpdates );
224 $existing = $this->getExistingExternals();
225 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
226 $this->getExternalInsertions( $existing ) );
229 $existing = $this->getExistingInterlangs();
230 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
231 $this->getInterlangInsertions( $existing ) );
233 # Inline interwiki links
234 $existing = $this->getExistingInterwikis();
235 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
236 $this->getInterwikiInsertions( $existing ) );
239 $existing = $this->getExistingTemplates();
240 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
241 $this->getTemplateInsertions( $existing ) );
244 $existing = $this->getExistingCategories();
245 $categoryDeletes = $this->getCategoryDeletions( $existing );
246 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
247 $this->getCategoryInsertions( $existing ) );
249 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
250 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
251 $categoryUpdates = $categoryInserts +
$categoryDeletes;
252 $this->invalidateCategories( $categoryUpdates );
253 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
256 $existing = $this->getExistingProperties();
257 $this->propertyDeletions
= $this->getPropertyDeletions( $existing );
258 $this->incrTableUpdate( 'page_props', 'pp', $this->propertyDeletions
,
259 $this->getPropertyInsertions( $existing ) );
261 # Invalidate the necessary pages
262 $this->propertyInsertions
= array_diff_assoc( $this->mProperties
, $existing );
263 $changed = $this->propertyDeletions +
$this->propertyInsertions
;
264 $this->invalidateProperties( $changed );
266 # Refresh links of all pages including this page
267 # This will be in a separate transaction
268 if ( $this->mRecursive
) {
269 $this->queueRecursiveJobs();
272 # Update the links table freshness for this title
273 $this->updateLinksTimestamp();
277 * Queue recursive jobs for this page
279 * Which means do LinksUpdate on all pages that include the current page,
280 * using the job queue.
282 protected function queueRecursiveJobs() {
283 self
::queueRecursiveJobsForTable( $this->mTitle
, 'templatelinks' );
284 if ( $this->mTitle
->getNamespace() == NS_FILE
) {
285 // Process imagelinks in case the title is or was a redirect
286 self
::queueRecursiveJobsForTable( $this->mTitle
, 'imagelinks' );
289 $bc = $this->mTitle
->getBacklinkCache();
290 // Get jobs for cascade-protected backlinks for a high priority queue.
291 // If meta-templates change to using a new template, the new template
292 // should be implicitly protected as soon as possible, if applicable.
293 // These jobs duplicate a subset of the above ones, but can run sooner.
294 // Which ever runs first generally no-ops the other one.
296 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
297 $jobs[] = RefreshLinksJob
::newPrioritized( $title, [] );
299 JobQueueGroup
::singleton()->push( $jobs );
303 * Queue a RefreshLinks job for any table.
305 * @param Title $title Title to do job for
306 * @param string $table Table to use (e.g. 'templatelinks')
308 public static function queueRecursiveJobsForTable( Title
$title, $table ) {
309 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
310 $job = new RefreshLinksJob(
315 ] + Job
::newRootJobParams( // "overall" refresh links job info
316 "refreshlinks:{$table}:{$title->getPrefixedText()}"
320 JobQueueGroup
::singleton()->push( $job );
327 function invalidateCategories( $cats ) {
328 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_CATEGORY
, array_keys( $cats ) );
332 * Update all the appropriate counts in the category table.
333 * @param array $added Associative array of category name => sort key
334 * @param array $deleted Associative array of category name => sort key
336 function updateCategoryCounts( $added, $deleted ) {
337 $a = WikiPage
::factory( $this->mTitle
);
338 $a->updateCategoryCounts(
339 array_keys( $added ), array_keys( $deleted )
344 * @param array $images
346 function invalidateImageDescriptions( $images ) {
347 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_FILE
, array_keys( $images ) );
351 * Update a table by doing a delete query then an insert query
352 * @param string $table Table name
353 * @param string $prefix Field name prefix
354 * @param array $deletions
355 * @param array $insertions Rows to insert
357 private function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
358 $services = MediaWikiServices
::getInstance();
359 $bSize = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
360 $factory = $services->getDBLoadBalancerFactory();
362 if ( $table === 'page_props' ) {
363 $fromField = 'pp_page';
365 $fromField = "{$prefix}_from";
368 $deleteWheres = []; // list of WHERE clause arrays for each DB delete() call
369 if ( $table === 'pagelinks' ||
$table === 'templatelinks' ||
$table === 'iwlinks' ) {
370 $baseKey = ( $table === 'iwlinks' ) ?
'iwl_prefix' : "{$prefix}_namespace";
373 $curDeletionBatch = [];
374 $deletionBatches = [];
375 foreach ( $deletions as $ns => $dbKeys ) {
376 foreach ( $dbKeys as $dbKey => $unused ) {
377 $curDeletionBatch[$ns][$dbKey] = 1;
378 if ( ++
$curBatchSize >= $bSize ) {
379 $deletionBatches[] = $curDeletionBatch;
380 $curDeletionBatch = [];
385 if ( $curDeletionBatch ) {
386 $deletionBatches[] = $curDeletionBatch;
389 foreach ( $deletionBatches as $deletionBatch ) {
391 $fromField => $this->mId
,
392 $this->getDB()->makeWhereFrom2d( $deletionBatch, $baseKey, "{$prefix}_title" )
396 if ( $table === 'langlinks' ) {
397 $toField = 'll_lang';
398 } elseif ( $table === 'page_props' ) {
399 $toField = 'pp_propname';
401 $toField = $prefix . '_to';
404 $deletionBatches = array_chunk( array_keys( $deletions ), $bSize );
405 foreach ( $deletionBatches as $deletionBatch ) {
406 $deleteWheres[] = [ $fromField => $this->mId
, $toField => $deletionBatch ];
410 foreach ( $deleteWheres as $deleteWhere ) {
411 $this->getDB()->delete( $table, $deleteWhere, __METHOD__
);
412 $factory->commitAndWaitForReplication(
413 __METHOD__
, $this->ticket
, [ 'wiki' => $this->getDB()->getWikiID() ]
417 $insertBatches = array_chunk( $insertions, $bSize );
418 foreach ( $insertBatches as $insertBatch ) {
419 $this->getDB()->insert( $table, $insertBatch, __METHOD__
, 'IGNORE' );
420 $factory->commitAndWaitForReplication(
421 __METHOD__
, $this->ticket
, [ 'wiki' => $this->getDB()->getWikiID() ]
425 if ( count( $insertions ) ) {
426 Hooks
::run( 'LinksUpdateAfterInsert', [ $this, $table, $insertions ] );
431 * Get an array of pagelinks insertions for passing to the DB
432 * Skips the titles specified by the 2-D array $existing
433 * @param array $existing
436 private function getLinkInsertions( $existing = [] ) {
438 foreach ( $this->mLinks
as $ns => $dbkeys ) {
439 $diffs = isset( $existing[$ns] )
440 ?
array_diff_key( $dbkeys, $existing[$ns] )
442 foreach ( $diffs as $dbk => $id ) {
444 'pl_from' => $this->mId
,
445 'pl_from_namespace' => $this->mTitle
->getNamespace(),
446 'pl_namespace' => $ns,
456 * Get an array of template insertions. Like getLinkInsertions()
457 * @param array $existing
460 private function getTemplateInsertions( $existing = [] ) {
462 foreach ( $this->mTemplates
as $ns => $dbkeys ) {
463 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
464 foreach ( $diffs as $dbk => $id ) {
466 'tl_from' => $this->mId
,
467 'tl_from_namespace' => $this->mTitle
->getNamespace(),
468 'tl_namespace' => $ns,
478 * Get an array of image insertions
479 * Skips the names specified in $existing
480 * @param array $existing
483 private function getImageInsertions( $existing = [] ) {
485 $diffs = array_diff_key( $this->mImages
, $existing );
486 foreach ( $diffs as $iname => $dummy ) {
488 'il_from' => $this->mId
,
489 'il_from_namespace' => $this->mTitle
->getNamespace(),
498 * Get an array of externallinks insertions. Skips the names specified in $existing
499 * @param array $existing
502 private function getExternalInsertions( $existing = [] ) {
504 $diffs = array_diff_key( $this->mExternals
, $existing );
505 foreach ( $diffs as $url => $dummy ) {
506 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
508 'el_id' => $this->getDB()->nextSequenceValue( 'externallinks_el_id_seq' ),
509 'el_from' => $this->mId
,
511 'el_index' => $index,
520 * Get an array of category insertions
522 * @param array $existing Mapping existing category names to sort keys. If both
523 * match a link in $this, the link will be omitted from the output
527 private function getCategoryInsertions( $existing = [] ) {
528 global $wgContLang, $wgCategoryCollation;
529 $diffs = array_diff_assoc( $this->mCategories
, $existing );
531 foreach ( $diffs as $name => $prefix ) {
532 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
533 $wgContLang->findVariantLink( $name, $nt, true );
535 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
537 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
543 # Treat custom sortkeys as a prefix, so that if multiple
544 # things are forced to sort as '*' or something, they'll
545 # sort properly in the category rather than in page_id
547 $sortkey = Collation
::singleton()->getSortKey(
548 $this->mTitle
->getCategorySortkey( $prefix ) );
551 'cl_from' => $this->mId
,
553 'cl_sortkey' => $sortkey,
554 'cl_timestamp' => $this->getDB()->timestamp(),
555 'cl_sortkey_prefix' => $prefix,
556 'cl_collation' => $wgCategoryCollation,
565 * Get an array of interlanguage link insertions
567 * @param array $existing Mapping existing language codes to titles
571 private function getInterlangInsertions( $existing = [] ) {
572 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
574 foreach ( $diffs as $lang => $title ) {
576 'll_from' => $this->mId
,
586 * Get an array of page property insertions
587 * @param array $existing
590 function getPropertyInsertions( $existing = [] ) {
591 $diffs = array_diff_assoc( $this->mProperties
, $existing );
594 foreach ( array_keys( $diffs ) as $name ) {
595 $arr[] = $this->getPagePropRowData( $name );
602 * Returns an associative array to be used for inserting a row into
603 * the page_props table. Besides the given property name, this will
604 * include the page id from $this->mId and any property value from
605 * $this->mProperties.
607 * The array returned will include the pp_sortkey field if this
608 * is present in the database (as indicated by $wgPagePropsHaveSortkey).
609 * The sortkey value is currently determined by getPropertySortKeyValue().
611 * @note this assumes that $this->mProperties[$prop] is defined.
613 * @param string $prop The name of the property.
617 private function getPagePropRowData( $prop ) {
618 global $wgPagePropsHaveSortkey;
620 $value = $this->mProperties
[$prop];
623 'pp_page' => $this->mId
,
624 'pp_propname' => $prop,
625 'pp_value' => $value,
628 if ( $wgPagePropsHaveSortkey ) {
629 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
636 * Determines the sort key for the given property value.
637 * This will return $value if it is a float or int,
638 * 1 or resp. 0 if it is a bool, and null otherwise.
640 * @note In the future, we may allow the sortkey to be specified explicitly
641 * in ParserOutput::setProperty.
643 * @param mixed $value
647 private function getPropertySortKeyValue( $value ) {
648 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
649 return floatval( $value );
656 * Get an array of interwiki insertions for passing to the DB
657 * Skips the titles specified by the 2-D array $existing
658 * @param array $existing
661 private function getInterwikiInsertions( $existing = [] ) {
663 foreach ( $this->mInterwikis
as $prefix => $dbkeys ) {
664 $diffs = isset( $existing[$prefix] )
665 ?
array_diff_key( $dbkeys, $existing[$prefix] )
668 foreach ( $diffs as $dbk => $id ) {
670 'iwl_from' => $this->mId
,
671 'iwl_prefix' => $prefix,
681 * Given an array of existing links, returns those links which are not in $this
682 * and thus should be deleted.
683 * @param array $existing
686 private function getLinkDeletions( $existing ) {
688 foreach ( $existing as $ns => $dbkeys ) {
689 if ( isset( $this->mLinks
[$ns] ) ) {
690 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
692 $del[$ns] = $existing[$ns];
700 * Given an array of existing templates, returns those templates which are not in $this
701 * and thus should be deleted.
702 * @param array $existing
705 private function getTemplateDeletions( $existing ) {
707 foreach ( $existing as $ns => $dbkeys ) {
708 if ( isset( $this->mTemplates
[$ns] ) ) {
709 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
711 $del[$ns] = $existing[$ns];
719 * Given an array of existing images, returns those images which are not in $this
720 * and thus should be deleted.
721 * @param array $existing
724 private function getImageDeletions( $existing ) {
725 return array_diff_key( $existing, $this->mImages
);
729 * Given an array of existing external links, returns those links which are not
730 * in $this and thus should be deleted.
731 * @param array $existing
734 private function getExternalDeletions( $existing ) {
735 return array_diff_key( $existing, $this->mExternals
);
739 * Given an array of existing categories, returns those categories which are not in $this
740 * and thus should be deleted.
741 * @param array $existing
744 private function getCategoryDeletions( $existing ) {
745 return array_diff_assoc( $existing, $this->mCategories
);
749 * Given an array of existing interlanguage links, returns those links which are not
750 * in $this and thus should be deleted.
751 * @param array $existing
754 private function getInterlangDeletions( $existing ) {
755 return array_diff_assoc( $existing, $this->mInterlangs
);
759 * Get array of properties which should be deleted.
760 * @param array $existing
763 function getPropertyDeletions( $existing ) {
764 return array_diff_assoc( $existing, $this->mProperties
);
768 * Given an array of existing interwiki links, returns those links which are not in $this
769 * and thus should be deleted.
770 * @param array $existing
773 private function getInterwikiDeletions( $existing ) {
775 foreach ( $existing as $prefix => $dbkeys ) {
776 if ( isset( $this->mInterwikis
[$prefix] ) ) {
777 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
779 $del[$prefix] = $existing[$prefix];
787 * Get an array of existing links, as a 2-D array
791 private function getExistingLinks() {
792 $res = $this->getDB()->select( 'pagelinks', [ 'pl_namespace', 'pl_title' ],
793 [ 'pl_from' => $this->mId
], __METHOD__
);
795 foreach ( $res as $row ) {
796 if ( !isset( $arr[$row->pl_namespace
] ) ) {
797 $arr[$row->pl_namespace
] = [];
799 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
806 * Get an array of existing templates, as a 2-D array
810 private function getExistingTemplates() {
811 $res = $this->getDB()->select( 'templatelinks', [ 'tl_namespace', 'tl_title' ],
812 [ 'tl_from' => $this->mId
], __METHOD__
);
814 foreach ( $res as $row ) {
815 if ( !isset( $arr[$row->tl_namespace
] ) ) {
816 $arr[$row->tl_namespace
] = [];
818 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
825 * Get an array of existing images, image names in the keys
829 private function getExistingImages() {
830 $res = $this->getDB()->select( 'imagelinks', [ 'il_to' ],
831 [ 'il_from' => $this->mId
], __METHOD__
);
833 foreach ( $res as $row ) {
834 $arr[$row->il_to
] = 1;
841 * Get an array of existing external links, URLs in the keys
845 private function getExistingExternals() {
846 $res = $this->getDB()->select( 'externallinks', [ 'el_to' ],
847 [ 'el_from' => $this->mId
], __METHOD__
);
849 foreach ( $res as $row ) {
850 $arr[$row->el_to
] = 1;
857 * Get an array of existing categories, with the name in the key and sort key in the value.
861 private function getExistingCategories() {
862 $res = $this->getDB()->select( 'categorylinks', [ 'cl_to', 'cl_sortkey_prefix' ],
863 [ 'cl_from' => $this->mId
], __METHOD__
);
865 foreach ( $res as $row ) {
866 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
873 * Get an array of existing interlanguage links, with the language code in the key and the
874 * title in the value.
878 private function getExistingInterlangs() {
879 $res = $this->getDB()->select( 'langlinks', [ 'll_lang', 'll_title' ],
880 [ 'll_from' => $this->mId
], __METHOD__
);
882 foreach ( $res as $row ) {
883 $arr[$row->ll_lang
] = $row->ll_title
;
890 * Get an array of existing inline interwiki links, as a 2-D array
891 * @return array (prefix => array(dbkey => 1))
893 private function getExistingInterwikis() {
894 $res = $this->getDB()->select( 'iwlinks', [ 'iwl_prefix', 'iwl_title' ],
895 [ 'iwl_from' => $this->mId
], __METHOD__
);
897 foreach ( $res as $row ) {
898 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
899 $arr[$row->iwl_prefix
] = [];
901 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
908 * Get an array of existing categories, with the name in the key and sort key in the value.
910 * @return array Array of property names and values
912 private function getExistingProperties() {
913 $res = $this->getDB()->select( 'page_props', [ 'pp_propname', 'pp_value' ],
914 [ 'pp_page' => $this->mId
], __METHOD__
);
916 foreach ( $res as $row ) {
917 $arr[$row->pp_propname
] = $row->pp_value
;
924 * Return the title object of the page being updated
927 public function getTitle() {
928 return $this->mTitle
;
932 * Returns parser output
934 * @return ParserOutput
936 public function getParserOutput() {
937 return $this->mParserOutput
;
941 * Return the list of images used as generated by the parser
944 public function getImages() {
945 return $this->mImages
;
949 * Set the revision corresponding to this LinksUpdate
953 * @param Revision $revision
955 public function setRevision( Revision
$revision ) {
956 $this->mRevision
= $revision;
961 * @return null|Revision
963 public function getRevision() {
964 return $this->mRevision
;
968 * Set the User who triggered this LinksUpdate
973 public function setTriggeringUser( User
$user ) {
981 public function getTriggeringUser() {
986 * Invalidate any necessary link lists related to page property changes
987 * @param array $changed
989 private function invalidateProperties( $changed ) {
990 global $wgPagePropLinkInvalidations;
992 foreach ( $changed as $name => $value ) {
993 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
994 $inv = $wgPagePropLinkInvalidations[$name];
995 if ( !is_array( $inv ) ) {
998 foreach ( $inv as $table ) {
999 DeferredUpdates
::addUpdate( new HTMLCacheUpdate( $this->mTitle
, $table ) );
1006 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
1008 * @return null|array Array of Titles
1010 public function getAddedLinks() {
1011 if ( $this->linkInsertions
=== null ) {
1015 foreach ( $this->linkInsertions
as $insertion ) {
1016 $result[] = Title
::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
1023 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
1025 * @return null|array Array of Titles
1027 public function getRemovedLinks() {
1028 if ( $this->linkDeletions
=== null ) {
1032 foreach ( $this->linkDeletions
as $ns => $titles ) {
1033 foreach ( $titles as $title => $unused ) {
1034 $result[] = Title
::makeTitle( $ns, $title );
1042 * Fetch page properties added by this LinksUpdate.
1043 * Only available after the update is complete.
1045 * @return null|array
1047 public function getAddedProperties() {
1048 return $this->propertyInsertions
;
1052 * Fetch page properties removed by this LinksUpdate.
1053 * Only available after the update is complete.
1055 * @return null|array
1057 public function getRemovedProperties() {
1058 return $this->propertyDeletions
;
1062 * Update links table freshness
1064 private function updateLinksTimestamp() {
1066 // The link updates made here only reflect the freshness of the parser output
1067 $timestamp = $this->mParserOutput
->getCacheTime();
1068 $this->getDB()->update( 'page',
1069 [ 'page_links_updated' => $this->getDB()->timestamp( $timestamp ) ],
1070 [ 'page_id' => $this->mId
],
1079 private function getDB() {
1081 $this->db
= wfGetDB( DB_MASTER
);
1087 public function getAsJobSpecification() {
1088 if ( $this->user
) {
1090 'userId' => $this->user
->getId(),
1091 'userName' => $this->user
->getName(),
1097 if ( $this->mRevision
) {
1098 $triggeringRevisionId = $this->mRevision
->getId();
1100 $triggeringRevisionId = false;
1104 'wiki' => $this->getDB()->getWikiID(),
1105 'job' => new JobSpecification(
1106 'refreshLinksPrioritized',
1108 // Reuse the parser cache if it was saved
1109 'rootJobTimestamp' => $this->mParserOutput
->getCacheTime(),
1110 'useRecursiveLinksUpdate' => $this->mRecursive
,
1111 'triggeringUser' => $userInfo,
1112 'triggeringRevisionId' => $triggeringRevisionId,
1114 [ 'removeDuplicates' => true ],