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 Wikimedia\Rdbms\IDatabase
;
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\ScopedCallback
;
28 * Class the manages updates of *_link tables as well as similar extension-managed tables
30 * @note: LinksUpdate is managed by DeferredUpdates::execute(). Do not run this in a transaction.
32 * See docs/deferred.txt
34 class LinksUpdate
extends DataUpdate
implements EnqueueableDataUpdate
{
35 // @todo make members protected, but make sure extensions don't break
37 /** @var int Page ID of the article linked from */
40 /** @var Title Title object of the article linked from */
43 /** @var ParserOutput */
44 public $mParserOutput;
46 /** @var array Map of title strings to IDs for the links in the document */
49 /** @var array DB keys of the images used, in the array key only */
52 /** @var array Map of title strings to IDs for the template references, including broken ones */
55 /** @var array URLs of external links, array key only */
58 /** @var array Map of category names to sort keys */
61 /** @var array Map of language codes to titles */
64 /** @var array 2-D map of (prefix => DBK => 1) */
67 /** @var array Map of arbitrary name to value */
70 /** @var bool Whether to queue jobs for recursive updates */
73 /** @var Revision Revision for which this update has been triggered */
77 * @var null|array Added links if calculated.
79 private $linkInsertions = null;
82 * @var null|array Deleted links if calculated.
84 private $linkDeletions = null;
87 * @var null|array Added properties if calculated.
89 private $propertyInsertions = null;
92 * @var null|array Deleted properties if calculated.
94 private $propertyDeletions = null;
101 /** @var IDatabase */
105 * @param Title $title Title of the page we're updating
106 * @param ParserOutput $parserOutput Output from a full parse of this page
107 * @param bool $recursive Queue jobs for recursive updates?
108 * @throws MWException
110 function __construct( Title
$title, ParserOutput
$parserOutput, $recursive = true ) {
111 parent
::__construct();
113 $this->mTitle
= $title;
114 $this->mId
= $title->getArticleID( Title
::GAID_FOR_UPDATE
);
117 throw new InvalidArgumentException(
118 "The Title object yields no ID. Perhaps the page doesn't exist?"
122 $this->mParserOutput
= $parserOutput;
124 $this->mLinks
= $parserOutput->getLinks();
125 $this->mImages
= $parserOutput->getImages();
126 $this->mTemplates
= $parserOutput->getTemplates();
127 $this->mExternals
= $parserOutput->getExternalLinks();
128 $this->mCategories
= $parserOutput->getCategories();
129 $this->mProperties
= $parserOutput->getProperties();
130 $this->mInterwikis
= $parserOutput->getInterwikiLinks();
132 # Convert the format of the interlanguage links
133 # I didn't want to change it in the ParserOutput, because that array is passed all
134 # the way back to the skin, so either a skin API break would be required, or an
135 # inefficient back-conversion.
136 $ill = $parserOutput->getLanguageLinks();
137 $this->mInterlangs
= [];
138 foreach ( $ill as $link ) {
139 list( $key, $title ) = explode( ':', $link, 2 );
140 $this->mInterlangs
[$key] = $title;
143 foreach ( $this->mCategories
as &$sortkey ) {
144 # If the sortkey is longer then 255 bytes,
145 # it truncated by DB, and then doesn't get
146 # matched when comparing existing vs current
147 # categories, causing T27254.
148 # Also. substr behaves weird when given "".
149 if ( $sortkey !== '' ) {
150 $sortkey = substr( $sortkey, 0, 255 );
154 $this->mRecursive
= $recursive;
156 // Avoid PHP 7.1 warning from passing $this by reference
157 $linksUpdate = $this;
158 Hooks
::run( 'LinksUpdateConstructed', [ &$linksUpdate ] );
162 * Update link tables with outgoing links from an updated article
164 * @note: this is managed by DeferredUpdates::execute(). Do not run this in a transaction.
166 public function doUpdate() {
167 if ( $this->ticket
) {
168 // Make sure all links update threads see the changes of each other.
169 // This handles the case when updates have to batched into several COMMITs.
170 $scopedLock = self
::acquirePageLock( $this->getDB(), $this->mId
);
173 // Avoid PHP 7.1 warning from passing $this by reference
174 $linksUpdate = $this;
175 Hooks
::run( 'LinksUpdate', [ &$linksUpdate ] );
176 $this->doIncrementalUpdate();
178 // Commit and release the lock (if set)
179 ScopedCallback
::consume( $scopedLock );
180 // Run post-commit hook handlers without DBO_TRX
181 DeferredUpdates
::addUpdate( new AutoCommitUpdate(
185 // Avoid PHP 7.1 warning from passing $this by reference
186 $linksUpdate = $this;
187 Hooks
::run( 'LinksUpdateComplete', [ &$linksUpdate, $this->ticket
] );
193 * Acquire a lock for performing link table updates for a page on a DB
195 * @param IDatabase $dbw
197 * @param string $why One of (job, atomicity)
198 * @return ScopedCallback
199 * @throws RuntimeException
202 public static function acquirePageLock( IDatabase
$dbw, $pageId, $why = 'atomicity' ) {
203 $key = "LinksUpdate:$why:pageid:$pageId";
204 $scopedLock = $dbw->getScopedLockAndFlush( $key, __METHOD__
, 15 );
205 if ( !$scopedLock ) {
206 throw new RuntimeException( "Could not acquire lock '$key'." );
212 protected function doIncrementalUpdate() {
214 $existingPL = $this->getExistingLinks();
215 $this->linkDeletions
= $this->getLinkDeletions( $existingPL );
216 $this->linkInsertions
= $this->getLinkInsertions( $existingPL );
217 $this->incrTableUpdate( 'pagelinks', 'pl', $this->linkDeletions
, $this->linkInsertions
);
220 $existingIL = $this->getExistingImages();
221 $imageDeletes = $this->getImageDeletions( $existingIL );
222 $this->incrTableUpdate(
226 $this->getImageInsertions( $existingIL ) );
228 # Invalidate all image description pages which had links added or removed
229 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existingIL );
230 $this->invalidateImageDescriptions( $imageUpdates );
233 $existingEL = $this->getExistingExternals();
234 $this->incrTableUpdate(
237 $this->getExternalDeletions( $existingEL ),
238 $this->getExternalInsertions( $existingEL ) );
241 $existingLL = $this->getExistingInterlangs();
242 $this->incrTableUpdate(
245 $this->getInterlangDeletions( $existingLL ),
246 $this->getInterlangInsertions( $existingLL ) );
248 # Inline interwiki links
249 $existingIW = $this->getExistingInterwikis();
250 $this->incrTableUpdate(
253 $this->getInterwikiDeletions( $existingIW ),
254 $this->getInterwikiInsertions( $existingIW ) );
257 $existingTL = $this->getExistingTemplates();
258 $this->incrTableUpdate(
261 $this->getTemplateDeletions( $existingTL ),
262 $this->getTemplateInsertions( $existingTL ) );
265 $existingCL = $this->getExistingCategories();
266 $categoryDeletes = $this->getCategoryDeletions( $existingCL );
267 $this->incrTableUpdate(
271 $this->getCategoryInsertions( $existingCL ) );
272 $categoryInserts = array_diff_assoc( $this->mCategories
, $existingCL );
273 $categoryUpdates = $categoryInserts +
$categoryDeletes;
276 $existingPP = $this->getExistingProperties();
277 $this->propertyDeletions
= $this->getPropertyDeletions( $existingPP );
278 $this->incrTableUpdate(
281 $this->propertyDeletions
,
282 $this->getPropertyInsertions( $existingPP ) );
284 # Invalidate the necessary pages
285 $this->propertyInsertions
= array_diff_assoc( $this->mProperties
, $existingPP );
286 $changed = $this->propertyDeletions +
$this->propertyInsertions
;
287 $this->invalidateProperties( $changed );
289 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
290 $this->invalidateCategories( $categoryUpdates );
291 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
293 # Refresh links of all pages including this page
294 # This will be in a separate transaction
295 if ( $this->mRecursive
) {
296 $this->queueRecursiveJobs();
299 # Update the links table freshness for this title
300 $this->updateLinksTimestamp();
304 * Queue recursive jobs for this page
306 * Which means do LinksUpdate on all pages that include the current page,
307 * using the job queue.
309 protected function queueRecursiveJobs() {
310 $action = $this->getCauseAction();
311 $agent = $this->getCauseAgent();
313 self
::queueRecursiveJobsForTable( $this->mTitle
, 'templatelinks', $action, $agent );
314 if ( $this->mTitle
->getNamespace() == NS_FILE
) {
315 // Process imagelinks in case the title is or was a redirect
316 self
::queueRecursiveJobsForTable( $this->mTitle
, 'imagelinks', $action, $agent );
319 $bc = $this->mTitle
->getBacklinkCache();
320 // Get jobs for cascade-protected backlinks for a high priority queue.
321 // If meta-templates change to using a new template, the new template
322 // should be implicitly protected as soon as possible, if applicable.
323 // These jobs duplicate a subset of the above ones, but can run sooner.
324 // Which ever runs first generally no-ops the other one.
326 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
327 $jobs[] = RefreshLinksJob
::newPrioritized(
330 'causeAction' => $action,
331 'causeAgent' => $agent
335 JobQueueGroup
::singleton()->push( $jobs );
339 * Queue a RefreshLinks job for any table.
341 * @param Title $title Title to do job for
342 * @param string $table Table to use (e.g. 'templatelinks')
343 * @param string $action Triggering action
344 * @param string $userName Triggering user name
346 public static function queueRecursiveJobsForTable(
347 Title
$title, $table, $action = 'unknown', $userName = 'unknown'
349 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
350 $job = new RefreshLinksJob(
355 ] + Job
::newRootJobParams( // "overall" refresh links job info
356 "refreshlinks:{$table}:{$title->getPrefixedText()}"
357 ) +
[ 'causeAction' => $action, 'causeAgent' => $userName ]
360 JobQueueGroup
::singleton()->push( $job );
367 private function invalidateCategories( $cats ) {
368 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_CATEGORY
, array_keys( $cats ) );
372 * Update all the appropriate counts in the category table.
373 * @param array $added Associative array of category name => sort key
374 * @param array $deleted Associative array of category name => sort key
376 private function updateCategoryCounts( array $added, array $deleted ) {
377 global $wgUpdateRowsPerQuery;
379 if ( !$added && !$deleted ) {
383 $domainId = $this->getDB()->getDomainID();
384 $wp = WikiPage
::factory( $this->mTitle
);
385 $lbf = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
386 // T163801: try to release any row locks to reduce contention
387 $lbf->commitAndWaitForReplication( __METHOD__
, $this->ticket
, [ 'domain' => $domainId ] );
389 foreach ( array_chunk( array_keys( $added ), $wgUpdateRowsPerQuery ) as $addBatch ) {
390 $wp->updateCategoryCounts( $addBatch, [], $this->mId
);
391 $lbf->commitAndWaitForReplication(
392 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ] );
395 foreach ( array_chunk( array_keys( $deleted ), $wgUpdateRowsPerQuery ) as $deleteBatch ) {
396 $wp->updateCategoryCounts( [], $deleteBatch, $this->mId
);
397 $lbf->commitAndWaitForReplication(
398 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ] );
403 * @param array $images
405 private function invalidateImageDescriptions( $images ) {
406 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_FILE
, array_keys( $images ) );
410 * Update a table by doing a delete query then an insert query
411 * @param string $table Table name
412 * @param string $prefix Field name prefix
413 * @param array $deletions
414 * @param array $insertions Rows to insert
416 private function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
417 $services = MediaWikiServices
::getInstance();
418 $bSize = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
419 $lbf = $services->getDBLoadBalancerFactory();
421 if ( $table === 'page_props' ) {
422 $fromField = 'pp_page';
424 $fromField = "{$prefix}_from";
427 $deleteWheres = []; // list of WHERE clause arrays for each DB delete() call
428 if ( $table === 'pagelinks' ||
$table === 'templatelinks' ||
$table === 'iwlinks' ) {
429 $baseKey = ( $table === 'iwlinks' ) ?
'iwl_prefix' : "{$prefix}_namespace";
432 $curDeletionBatch = [];
433 $deletionBatches = [];
434 foreach ( $deletions as $ns => $dbKeys ) {
435 foreach ( $dbKeys as $dbKey => $unused ) {
436 $curDeletionBatch[$ns][$dbKey] = 1;
437 if ( ++
$curBatchSize >= $bSize ) {
438 $deletionBatches[] = $curDeletionBatch;
439 $curDeletionBatch = [];
444 if ( $curDeletionBatch ) {
445 $deletionBatches[] = $curDeletionBatch;
448 foreach ( $deletionBatches as $deletionBatch ) {
450 $fromField => $this->mId
,
451 $this->getDB()->makeWhereFrom2d( $deletionBatch, $baseKey, "{$prefix}_title" )
455 if ( $table === 'langlinks' ) {
456 $toField = 'll_lang';
457 } elseif ( $table === 'page_props' ) {
458 $toField = 'pp_propname';
460 $toField = $prefix . '_to';
463 $deletionBatches = array_chunk( array_keys( $deletions ), $bSize );
464 foreach ( $deletionBatches as $deletionBatch ) {
465 $deleteWheres[] = [ $fromField => $this->mId
, $toField => $deletionBatch ];
469 $domainId = $this->getDB()->getDomainID();
471 foreach ( $deleteWheres as $deleteWhere ) {
472 $this->getDB()->delete( $table, $deleteWhere, __METHOD__
);
473 $lbf->commitAndWaitForReplication(
474 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ]
478 $insertBatches = array_chunk( $insertions, $bSize );
479 foreach ( $insertBatches as $insertBatch ) {
480 $this->getDB()->insert( $table, $insertBatch, __METHOD__
, 'IGNORE' );
481 $lbf->commitAndWaitForReplication(
482 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ]
486 if ( count( $insertions ) ) {
487 Hooks
::run( 'LinksUpdateAfterInsert', [ $this, $table, $insertions ] );
492 * Get an array of pagelinks insertions for passing to the DB
493 * Skips the titles specified by the 2-D array $existing
494 * @param array $existing
497 private function getLinkInsertions( $existing = [] ) {
499 foreach ( $this->mLinks
as $ns => $dbkeys ) {
500 $diffs = isset( $existing[$ns] )
501 ?
array_diff_key( $dbkeys, $existing[$ns] )
503 foreach ( $diffs as $dbk => $id ) {
505 'pl_from' => $this->mId
,
506 'pl_from_namespace' => $this->mTitle
->getNamespace(),
507 'pl_namespace' => $ns,
517 * Get an array of template insertions. Like getLinkInsertions()
518 * @param array $existing
521 private function getTemplateInsertions( $existing = [] ) {
523 foreach ( $this->mTemplates
as $ns => $dbkeys ) {
524 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
525 foreach ( $diffs as $dbk => $id ) {
527 'tl_from' => $this->mId
,
528 'tl_from_namespace' => $this->mTitle
->getNamespace(),
529 'tl_namespace' => $ns,
539 * Get an array of image insertions
540 * Skips the names specified in $existing
541 * @param array $existing
544 private function getImageInsertions( $existing = [] ) {
546 $diffs = array_diff_key( $this->mImages
, $existing );
547 foreach ( $diffs as $iname => $dummy ) {
549 'il_from' => $this->mId
,
550 'il_from_namespace' => $this->mTitle
->getNamespace(),
559 * Get an array of externallinks insertions. Skips the names specified in $existing
560 * @param array $existing
563 private function getExternalInsertions( $existing = [] ) {
565 $diffs = array_diff_key( $this->mExternals
, $existing );
566 foreach ( $diffs as $url => $dummy ) {
567 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
569 'el_from' => $this->mId
,
571 'el_index' => $index,
572 'el_index_60' => substr( $index, 0, 60 ),
581 * Get an array of category insertions
583 * @param array $existing Mapping existing category names to sort keys. If both
584 * match a link in $this, the link will be omitted from the output
588 private function getCategoryInsertions( $existing = [] ) {
589 global $wgContLang, $wgCategoryCollation;
590 $diffs = array_diff_assoc( $this->mCategories
, $existing );
592 foreach ( $diffs as $name => $prefix ) {
593 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
594 $wgContLang->findVariantLink( $name, $nt, true );
596 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
598 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
604 # Treat custom sortkeys as a prefix, so that if multiple
605 # things are forced to sort as '*' or something, they'll
606 # sort properly in the category rather than in page_id
608 $sortkey = Collation
::singleton()->getSortKey(
609 $this->mTitle
->getCategorySortkey( $prefix ) );
612 'cl_from' => $this->mId
,
614 'cl_sortkey' => $sortkey,
615 'cl_timestamp' => $this->getDB()->timestamp(),
616 'cl_sortkey_prefix' => $prefix,
617 'cl_collation' => $wgCategoryCollation,
626 * Get an array of interlanguage link insertions
628 * @param array $existing Mapping existing language codes to titles
632 private function getInterlangInsertions( $existing = [] ) {
633 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
635 foreach ( $diffs as $lang => $title ) {
637 'll_from' => $this->mId
,
647 * Get an array of page property insertions
648 * @param array $existing
651 function getPropertyInsertions( $existing = [] ) {
652 $diffs = array_diff_assoc( $this->mProperties
, $existing );
655 foreach ( array_keys( $diffs ) as $name ) {
656 $arr[] = $this->getPagePropRowData( $name );
663 * Returns an associative array to be used for inserting a row into
664 * the page_props table. Besides the given property name, this will
665 * include the page id from $this->mId and any property value from
666 * $this->mProperties.
668 * The array returned will include the pp_sortkey field if this
669 * is present in the database (as indicated by $wgPagePropsHaveSortkey).
670 * The sortkey value is currently determined by getPropertySortKeyValue().
672 * @note this assumes that $this->mProperties[$prop] is defined.
674 * @param string $prop The name of the property.
678 private function getPagePropRowData( $prop ) {
679 global $wgPagePropsHaveSortkey;
681 $value = $this->mProperties
[$prop];
684 'pp_page' => $this->mId
,
685 'pp_propname' => $prop,
686 'pp_value' => $value,
689 if ( $wgPagePropsHaveSortkey ) {
690 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
697 * Determines the sort key for the given property value.
698 * This will return $value if it is a float or int,
699 * 1 or resp. 0 if it is a bool, and null otherwise.
701 * @note In the future, we may allow the sortkey to be specified explicitly
702 * in ParserOutput::setProperty.
704 * @param mixed $value
708 private function getPropertySortKeyValue( $value ) {
709 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
710 return floatval( $value );
717 * Get an array of interwiki insertions for passing to the DB
718 * Skips the titles specified by the 2-D array $existing
719 * @param array $existing
722 private function getInterwikiInsertions( $existing = [] ) {
724 foreach ( $this->mInterwikis
as $prefix => $dbkeys ) {
725 $diffs = isset( $existing[$prefix] )
726 ?
array_diff_key( $dbkeys, $existing[$prefix] )
729 foreach ( $diffs as $dbk => $id ) {
731 'iwl_from' => $this->mId
,
732 'iwl_prefix' => $prefix,
742 * Given an array of existing links, returns those links which are not in $this
743 * and thus should be deleted.
744 * @param array $existing
747 private function getLinkDeletions( $existing ) {
749 foreach ( $existing as $ns => $dbkeys ) {
750 if ( isset( $this->mLinks
[$ns] ) ) {
751 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
753 $del[$ns] = $existing[$ns];
761 * Given an array of existing templates, returns those templates which are not in $this
762 * and thus should be deleted.
763 * @param array $existing
766 private function getTemplateDeletions( $existing ) {
768 foreach ( $existing as $ns => $dbkeys ) {
769 if ( isset( $this->mTemplates
[$ns] ) ) {
770 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
772 $del[$ns] = $existing[$ns];
780 * Given an array of existing images, returns those images which are not in $this
781 * and thus should be deleted.
782 * @param array $existing
785 private function getImageDeletions( $existing ) {
786 return array_diff_key( $existing, $this->mImages
);
790 * Given an array of existing external links, returns those links which are not
791 * in $this and thus should be deleted.
792 * @param array $existing
795 private function getExternalDeletions( $existing ) {
796 return array_diff_key( $existing, $this->mExternals
);
800 * Given an array of existing categories, returns those categories which are not in $this
801 * and thus should be deleted.
802 * @param array $existing
805 private function getCategoryDeletions( $existing ) {
806 return array_diff_assoc( $existing, $this->mCategories
);
810 * Given an array of existing interlanguage links, returns those links which are not
811 * in $this and thus should be deleted.
812 * @param array $existing
815 private function getInterlangDeletions( $existing ) {
816 return array_diff_assoc( $existing, $this->mInterlangs
);
820 * Get array of properties which should be deleted.
821 * @param array $existing
824 function getPropertyDeletions( $existing ) {
825 return array_diff_assoc( $existing, $this->mProperties
);
829 * Given an array of existing interwiki links, returns those links which are not in $this
830 * and thus should be deleted.
831 * @param array $existing
834 private function getInterwikiDeletions( $existing ) {
836 foreach ( $existing as $prefix => $dbkeys ) {
837 if ( isset( $this->mInterwikis
[$prefix] ) ) {
838 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
840 $del[$prefix] = $existing[$prefix];
848 * Get an array of existing links, as a 2-D array
852 private function getExistingLinks() {
853 $res = $this->getDB()->select( 'pagelinks', [ 'pl_namespace', 'pl_title' ],
854 [ 'pl_from' => $this->mId
], __METHOD__
);
856 foreach ( $res as $row ) {
857 if ( !isset( $arr[$row->pl_namespace
] ) ) {
858 $arr[$row->pl_namespace
] = [];
860 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
867 * Get an array of existing templates, as a 2-D array
871 private function getExistingTemplates() {
872 $res = $this->getDB()->select( 'templatelinks', [ 'tl_namespace', 'tl_title' ],
873 [ 'tl_from' => $this->mId
], __METHOD__
);
875 foreach ( $res as $row ) {
876 if ( !isset( $arr[$row->tl_namespace
] ) ) {
877 $arr[$row->tl_namespace
] = [];
879 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
886 * Get an array of existing images, image names in the keys
890 private function getExistingImages() {
891 $res = $this->getDB()->select( 'imagelinks', [ 'il_to' ],
892 [ 'il_from' => $this->mId
], __METHOD__
);
894 foreach ( $res as $row ) {
895 $arr[$row->il_to
] = 1;
902 * Get an array of existing external links, URLs in the keys
906 private function getExistingExternals() {
907 $res = $this->getDB()->select( 'externallinks', [ 'el_to' ],
908 [ 'el_from' => $this->mId
], __METHOD__
);
910 foreach ( $res as $row ) {
911 $arr[$row->el_to
] = 1;
918 * Get an array of existing categories, with the name in the key and sort key in the value.
922 private function getExistingCategories() {
923 $res = $this->getDB()->select( 'categorylinks', [ 'cl_to', 'cl_sortkey_prefix' ],
924 [ 'cl_from' => $this->mId
], __METHOD__
);
926 foreach ( $res as $row ) {
927 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
934 * Get an array of existing interlanguage links, with the language code in the key and the
935 * title in the value.
939 private function getExistingInterlangs() {
940 $res = $this->getDB()->select( 'langlinks', [ 'll_lang', 'll_title' ],
941 [ 'll_from' => $this->mId
], __METHOD__
);
943 foreach ( $res as $row ) {
944 $arr[$row->ll_lang
] = $row->ll_title
;
951 * Get an array of existing inline interwiki links, as a 2-D array
952 * @return array (prefix => array(dbkey => 1))
954 private function getExistingInterwikis() {
955 $res = $this->getDB()->select( 'iwlinks', [ 'iwl_prefix', 'iwl_title' ],
956 [ 'iwl_from' => $this->mId
], __METHOD__
);
958 foreach ( $res as $row ) {
959 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
960 $arr[$row->iwl_prefix
] = [];
962 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
969 * Get an array of existing categories, with the name in the key and sort key in the value.
971 * @return array Array of property names and values
973 private function getExistingProperties() {
974 $res = $this->getDB()->select( 'page_props', [ 'pp_propname', 'pp_value' ],
975 [ 'pp_page' => $this->mId
], __METHOD__
);
977 foreach ( $res as $row ) {
978 $arr[$row->pp_propname
] = $row->pp_value
;
985 * Return the title object of the page being updated
988 public function getTitle() {
989 return $this->mTitle
;
993 * Returns parser output
995 * @return ParserOutput
997 public function getParserOutput() {
998 return $this->mParserOutput
;
1002 * Return the list of images used as generated by the parser
1005 public function getImages() {
1006 return $this->mImages
;
1010 * Set the revision corresponding to this LinksUpdate
1014 * @param Revision $revision
1016 public function setRevision( Revision
$revision ) {
1017 $this->mRevision
= $revision;
1022 * @return null|Revision
1024 public function getRevision() {
1025 return $this->mRevision
;
1029 * Set the User who triggered this LinksUpdate
1034 public function setTriggeringUser( User
$user ) {
1035 $this->user
= $user;
1042 public function getTriggeringUser() {
1047 * Invalidate any necessary link lists related to page property changes
1048 * @param array $changed
1050 private function invalidateProperties( $changed ) {
1051 global $wgPagePropLinkInvalidations;
1053 foreach ( $changed as $name => $value ) {
1054 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
1055 $inv = $wgPagePropLinkInvalidations[$name];
1056 if ( !is_array( $inv ) ) {
1059 foreach ( $inv as $table ) {
1060 DeferredUpdates
::addUpdate(
1061 new HTMLCacheUpdate( $this->mTitle
, $table, 'page-props' )
1069 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
1071 * @return null|array Array of Titles
1073 public function getAddedLinks() {
1074 if ( $this->linkInsertions
=== null ) {
1078 foreach ( $this->linkInsertions
as $insertion ) {
1079 $result[] = Title
::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
1086 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
1088 * @return null|array Array of Titles
1090 public function getRemovedLinks() {
1091 if ( $this->linkDeletions
=== null ) {
1095 foreach ( $this->linkDeletions
as $ns => $titles ) {
1096 foreach ( $titles as $title => $unused ) {
1097 $result[] = Title
::makeTitle( $ns, $title );
1105 * Fetch page properties added by this LinksUpdate.
1106 * Only available after the update is complete.
1108 * @return null|array
1110 public function getAddedProperties() {
1111 return $this->propertyInsertions
;
1115 * Fetch page properties removed by this LinksUpdate.
1116 * Only available after the update is complete.
1118 * @return null|array
1120 public function getRemovedProperties() {
1121 return $this->propertyDeletions
;
1125 * Update links table freshness
1127 private function updateLinksTimestamp() {
1129 // The link updates made here only reflect the freshness of the parser output
1130 $timestamp = $this->mParserOutput
->getCacheTime();
1131 $this->getDB()->update( 'page',
1132 [ 'page_links_updated' => $this->getDB()->timestamp( $timestamp ) ],
1133 [ 'page_id' => $this->mId
],
1142 private function getDB() {
1144 $this->db
= wfGetDB( DB_MASTER
);
1150 public function getAsJobSpecification() {
1151 if ( $this->user
) {
1153 'userId' => $this->user
->getId(),
1154 'userName' => $this->user
->getName(),
1160 if ( $this->mRevision
) {
1161 $triggeringRevisionId = $this->mRevision
->getId();
1163 $triggeringRevisionId = false;
1167 'wiki' => WikiMap
::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
1168 'job' => new JobSpecification(
1169 'refreshLinksPrioritized',
1171 // Reuse the parser cache if it was saved
1172 'rootJobTimestamp' => $this->mParserOutput
->getCacheTime(),
1173 'useRecursiveLinksUpdate' => $this->mRecursive
,
1174 'triggeringUser' => $userInfo,
1175 'triggeringRevisionId' => $triggeringRevisionId,
1176 'causeAction' => $this->getCauseAction(),
1177 'causeAgent' => $this->getCauseAgent()
1179 [ 'removeDuplicates' => true ],