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 hooks without DBO_TRX
181 $this->getDB()->onTransactionIdle(
183 // Avoid PHP 7.1 warning from passing $this by reference
184 $linksUpdate = $this;
185 Hooks
::run( 'LinksUpdateComplete', [ &$linksUpdate, $this->ticket
] );
192 * Acquire a lock for performing link table updates for a page on a DB
194 * @param IDatabase $dbw
196 * @param string $why One of (job, atomicity)
197 * @return ScopedCallback
198 * @throws RuntimeException
201 public static function acquirePageLock( IDatabase
$dbw, $pageId, $why = 'atomicity' ) {
202 $key = "LinksUpdate:$why:pageid:$pageId";
203 $scopedLock = $dbw->getScopedLockAndFlush( $key, __METHOD__
, 15 );
204 if ( !$scopedLock ) {
205 throw new RuntimeException( "Could not acquire lock '$key'." );
211 protected function doIncrementalUpdate() {
213 $existingPL = $this->getExistingLinks();
214 $this->linkDeletions
= $this->getLinkDeletions( $existingPL );
215 $this->linkInsertions
= $this->getLinkInsertions( $existingPL );
216 $this->incrTableUpdate( 'pagelinks', 'pl', $this->linkDeletions
, $this->linkInsertions
);
219 $existingIL = $this->getExistingImages();
220 $imageDeletes = $this->getImageDeletions( $existingIL );
221 $this->incrTableUpdate(
225 $this->getImageInsertions( $existingIL ) );
227 # Invalidate all image description pages which had links added or removed
228 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existingIL );
229 $this->invalidateImageDescriptions( $imageUpdates );
232 $existingEL = $this->getExistingExternals();
233 $this->incrTableUpdate(
236 $this->getExternalDeletions( $existingEL ),
237 $this->getExternalInsertions( $existingEL ) );
240 $existingLL = $this->getExistingInterlangs();
241 $this->incrTableUpdate(
244 $this->getInterlangDeletions( $existingLL ),
245 $this->getInterlangInsertions( $existingLL ) );
247 # Inline interwiki links
248 $existingIW = $this->getExistingInterwikis();
249 $this->incrTableUpdate(
252 $this->getInterwikiDeletions( $existingIW ),
253 $this->getInterwikiInsertions( $existingIW ) );
256 $existingTL = $this->getExistingTemplates();
257 $this->incrTableUpdate(
260 $this->getTemplateDeletions( $existingTL ),
261 $this->getTemplateInsertions( $existingTL ) );
264 $existingCL = $this->getExistingCategories();
265 $categoryDeletes = $this->getCategoryDeletions( $existingCL );
266 $this->incrTableUpdate(
270 $this->getCategoryInsertions( $existingCL ) );
271 $categoryInserts = array_diff_assoc( $this->mCategories
, $existingCL );
272 $categoryUpdates = $categoryInserts +
$categoryDeletes;
275 $existingPP = $this->getExistingProperties();
276 $this->propertyDeletions
= $this->getPropertyDeletions( $existingPP );
277 $this->incrTableUpdate(
280 $this->propertyDeletions
,
281 $this->getPropertyInsertions( $existingPP ) );
283 # Invalidate the necessary pages
284 $this->propertyInsertions
= array_diff_assoc( $this->mProperties
, $existingPP );
285 $changed = $this->propertyDeletions +
$this->propertyInsertions
;
286 $this->invalidateProperties( $changed );
288 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
289 $this->invalidateCategories( $categoryUpdates );
290 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
292 # Refresh links of all pages including this page
293 # This will be in a separate transaction
294 if ( $this->mRecursive
) {
295 $this->queueRecursiveJobs();
298 # Update the links table freshness for this title
299 $this->updateLinksTimestamp();
303 * Queue recursive jobs for this page
305 * Which means do LinksUpdate on all pages that include the current page,
306 * using the job queue.
308 protected function queueRecursiveJobs() {
309 $action = $this->getCauseAction();
310 $agent = $this->getCauseAgent();
312 self
::queueRecursiveJobsForTable( $this->mTitle
, 'templatelinks', $action, $agent );
313 if ( $this->mTitle
->getNamespace() == NS_FILE
) {
314 // Process imagelinks in case the title is or was a redirect
315 self
::queueRecursiveJobsForTable( $this->mTitle
, 'imagelinks', $action, $agent );
318 $bc = $this->mTitle
->getBacklinkCache();
319 // Get jobs for cascade-protected backlinks for a high priority queue.
320 // If meta-templates change to using a new template, the new template
321 // should be implicitly protected as soon as possible, if applicable.
322 // These jobs duplicate a subset of the above ones, but can run sooner.
323 // Which ever runs first generally no-ops the other one.
325 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
326 $jobs[] = RefreshLinksJob
::newPrioritized(
329 'causeAction' => $action,
330 'causeAgent' => $agent
334 JobQueueGroup
::singleton()->push( $jobs );
338 * Queue a RefreshLinks job for any table.
340 * @param Title $title Title to do job for
341 * @param string $table Table to use (e.g. 'templatelinks')
342 * @param string $action Triggering action
343 * @param string $userName Triggering user name
345 public static function queueRecursiveJobsForTable(
346 Title
$title, $table, $action = 'unknown', $userName = 'unknown'
348 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
349 $job = new RefreshLinksJob(
354 ] + Job
::newRootJobParams( // "overall" refresh links job info
355 "refreshlinks:{$table}:{$title->getPrefixedText()}"
356 ) +
[ 'causeAction' => $action, 'causeAgent' => $userName ]
359 JobQueueGroup
::singleton()->push( $job );
366 private function invalidateCategories( $cats ) {
367 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_CATEGORY
, array_keys( $cats ) );
371 * Update all the appropriate counts in the category table.
372 * @param array $added Associative array of category name => sort key
373 * @param array $deleted Associative array of category name => sort key
375 private function updateCategoryCounts( array $added, array $deleted ) {
376 global $wgUpdateRowsPerQuery;
378 if ( !$added && !$deleted ) {
382 $domainId = $this->getDB()->getDomainID();
383 $wp = WikiPage
::factory( $this->mTitle
);
384 $lbf = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
385 // T163801: try to release any row locks to reduce contention
386 $lbf->commitAndWaitForReplication( __METHOD__
, $this->ticket
, [ 'domain' => $domainId ] );
388 foreach ( array_chunk( array_keys( $added ), $wgUpdateRowsPerQuery ) as $addBatch ) {
389 $wp->updateCategoryCounts( $addBatch, [], $this->mId
);
390 $lbf->commitAndWaitForReplication(
391 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ] );
394 foreach ( array_chunk( array_keys( $deleted ), $wgUpdateRowsPerQuery ) as $deleteBatch ) {
395 $wp->updateCategoryCounts( [], $deleteBatch, $this->mId
);
396 $lbf->commitAndWaitForReplication(
397 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ] );
402 * @param array $images
404 private function invalidateImageDescriptions( $images ) {
405 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_FILE
, array_keys( $images ) );
409 * Update a table by doing a delete query then an insert query
410 * @param string $table Table name
411 * @param string $prefix Field name prefix
412 * @param array $deletions
413 * @param array $insertions Rows to insert
415 private function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
416 $services = MediaWikiServices
::getInstance();
417 $bSize = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
418 $lbf = $services->getDBLoadBalancerFactory();
420 if ( $table === 'page_props' ) {
421 $fromField = 'pp_page';
423 $fromField = "{$prefix}_from";
426 $deleteWheres = []; // list of WHERE clause arrays for each DB delete() call
427 if ( $table === 'pagelinks' ||
$table === 'templatelinks' ||
$table === 'iwlinks' ) {
428 $baseKey = ( $table === 'iwlinks' ) ?
'iwl_prefix' : "{$prefix}_namespace";
431 $curDeletionBatch = [];
432 $deletionBatches = [];
433 foreach ( $deletions as $ns => $dbKeys ) {
434 foreach ( $dbKeys as $dbKey => $unused ) {
435 $curDeletionBatch[$ns][$dbKey] = 1;
436 if ( ++
$curBatchSize >= $bSize ) {
437 $deletionBatches[] = $curDeletionBatch;
438 $curDeletionBatch = [];
443 if ( $curDeletionBatch ) {
444 $deletionBatches[] = $curDeletionBatch;
447 foreach ( $deletionBatches as $deletionBatch ) {
449 $fromField => $this->mId
,
450 $this->getDB()->makeWhereFrom2d( $deletionBatch, $baseKey, "{$prefix}_title" )
454 if ( $table === 'langlinks' ) {
455 $toField = 'll_lang';
456 } elseif ( $table === 'page_props' ) {
457 $toField = 'pp_propname';
459 $toField = $prefix . '_to';
462 $deletionBatches = array_chunk( array_keys( $deletions ), $bSize );
463 foreach ( $deletionBatches as $deletionBatch ) {
464 $deleteWheres[] = [ $fromField => $this->mId
, $toField => $deletionBatch ];
468 $domainId = $this->getDB()->getDomainID();
470 foreach ( $deleteWheres as $deleteWhere ) {
471 $this->getDB()->delete( $table, $deleteWhere, __METHOD__
);
472 $lbf->commitAndWaitForReplication(
473 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ]
477 $insertBatches = array_chunk( $insertions, $bSize );
478 foreach ( $insertBatches as $insertBatch ) {
479 $this->getDB()->insert( $table, $insertBatch, __METHOD__
, 'IGNORE' );
480 $lbf->commitAndWaitForReplication(
481 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ]
485 if ( count( $insertions ) ) {
486 Hooks
::run( 'LinksUpdateAfterInsert', [ $this, $table, $insertions ] );
491 * Get an array of pagelinks insertions for passing to the DB
492 * Skips the titles specified by the 2-D array $existing
493 * @param array $existing
496 private function getLinkInsertions( $existing = [] ) {
498 foreach ( $this->mLinks
as $ns => $dbkeys ) {
499 $diffs = isset( $existing[$ns] )
500 ?
array_diff_key( $dbkeys, $existing[$ns] )
502 foreach ( $diffs as $dbk => $id ) {
504 'pl_from' => $this->mId
,
505 'pl_from_namespace' => $this->mTitle
->getNamespace(),
506 'pl_namespace' => $ns,
516 * Get an array of template insertions. Like getLinkInsertions()
517 * @param array $existing
520 private function getTemplateInsertions( $existing = [] ) {
522 foreach ( $this->mTemplates
as $ns => $dbkeys ) {
523 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
524 foreach ( $diffs as $dbk => $id ) {
526 'tl_from' => $this->mId
,
527 'tl_from_namespace' => $this->mTitle
->getNamespace(),
528 'tl_namespace' => $ns,
538 * Get an array of image insertions
539 * Skips the names specified in $existing
540 * @param array $existing
543 private function getImageInsertions( $existing = [] ) {
545 $diffs = array_diff_key( $this->mImages
, $existing );
546 foreach ( $diffs as $iname => $dummy ) {
548 'il_from' => $this->mId
,
549 'il_from_namespace' => $this->mTitle
->getNamespace(),
558 * Get an array of externallinks insertions. Skips the names specified in $existing
559 * @param array $existing
562 private function getExternalInsertions( $existing = [] ) {
564 $diffs = array_diff_key( $this->mExternals
, $existing );
565 foreach ( $diffs as $url => $dummy ) {
566 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
568 'el_from' => $this->mId
,
570 'el_index' => $index,
579 * Get an array of category insertions
581 * @param array $existing Mapping existing category names to sort keys. If both
582 * match a link in $this, the link will be omitted from the output
586 private function getCategoryInsertions( $existing = [] ) {
587 global $wgContLang, $wgCategoryCollation;
588 $diffs = array_diff_assoc( $this->mCategories
, $existing );
590 foreach ( $diffs as $name => $prefix ) {
591 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
592 $wgContLang->findVariantLink( $name, $nt, true );
594 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
596 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
602 # Treat custom sortkeys as a prefix, so that if multiple
603 # things are forced to sort as '*' or something, they'll
604 # sort properly in the category rather than in page_id
606 $sortkey = Collation
::singleton()->getSortKey(
607 $this->mTitle
->getCategorySortkey( $prefix ) );
610 'cl_from' => $this->mId
,
612 'cl_sortkey' => $sortkey,
613 'cl_timestamp' => $this->getDB()->timestamp(),
614 'cl_sortkey_prefix' => $prefix,
615 'cl_collation' => $wgCategoryCollation,
624 * Get an array of interlanguage link insertions
626 * @param array $existing Mapping existing language codes to titles
630 private function getInterlangInsertions( $existing = [] ) {
631 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
633 foreach ( $diffs as $lang => $title ) {
635 'll_from' => $this->mId
,
645 * Get an array of page property insertions
646 * @param array $existing
649 function getPropertyInsertions( $existing = [] ) {
650 $diffs = array_diff_assoc( $this->mProperties
, $existing );
653 foreach ( array_keys( $diffs ) as $name ) {
654 $arr[] = $this->getPagePropRowData( $name );
661 * Returns an associative array to be used for inserting a row into
662 * the page_props table. Besides the given property name, this will
663 * include the page id from $this->mId and any property value from
664 * $this->mProperties.
666 * The array returned will include the pp_sortkey field if this
667 * is present in the database (as indicated by $wgPagePropsHaveSortkey).
668 * The sortkey value is currently determined by getPropertySortKeyValue().
670 * @note this assumes that $this->mProperties[$prop] is defined.
672 * @param string $prop The name of the property.
676 private function getPagePropRowData( $prop ) {
677 global $wgPagePropsHaveSortkey;
679 $value = $this->mProperties
[$prop];
682 'pp_page' => $this->mId
,
683 'pp_propname' => $prop,
684 'pp_value' => $value,
687 if ( $wgPagePropsHaveSortkey ) {
688 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
695 * Determines the sort key for the given property value.
696 * This will return $value if it is a float or int,
697 * 1 or resp. 0 if it is a bool, and null otherwise.
699 * @note In the future, we may allow the sortkey to be specified explicitly
700 * in ParserOutput::setProperty.
702 * @param mixed $value
706 private function getPropertySortKeyValue( $value ) {
707 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
708 return floatval( $value );
715 * Get an array of interwiki insertions for passing to the DB
716 * Skips the titles specified by the 2-D array $existing
717 * @param array $existing
720 private function getInterwikiInsertions( $existing = [] ) {
722 foreach ( $this->mInterwikis
as $prefix => $dbkeys ) {
723 $diffs = isset( $existing[$prefix] )
724 ?
array_diff_key( $dbkeys, $existing[$prefix] )
727 foreach ( $diffs as $dbk => $id ) {
729 'iwl_from' => $this->mId
,
730 'iwl_prefix' => $prefix,
740 * Given an array of existing links, returns those links which are not in $this
741 * and thus should be deleted.
742 * @param array $existing
745 private function getLinkDeletions( $existing ) {
747 foreach ( $existing as $ns => $dbkeys ) {
748 if ( isset( $this->mLinks
[$ns] ) ) {
749 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
751 $del[$ns] = $existing[$ns];
759 * Given an array of existing templates, returns those templates which are not in $this
760 * and thus should be deleted.
761 * @param array $existing
764 private function getTemplateDeletions( $existing ) {
766 foreach ( $existing as $ns => $dbkeys ) {
767 if ( isset( $this->mTemplates
[$ns] ) ) {
768 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
770 $del[$ns] = $existing[$ns];
778 * Given an array of existing images, returns those images which are not in $this
779 * and thus should be deleted.
780 * @param array $existing
783 private function getImageDeletions( $existing ) {
784 return array_diff_key( $existing, $this->mImages
);
788 * Given an array of existing external links, returns those links which are not
789 * in $this and thus should be deleted.
790 * @param array $existing
793 private function getExternalDeletions( $existing ) {
794 return array_diff_key( $existing, $this->mExternals
);
798 * Given an array of existing categories, returns those categories which are not in $this
799 * and thus should be deleted.
800 * @param array $existing
803 private function getCategoryDeletions( $existing ) {
804 return array_diff_assoc( $existing, $this->mCategories
);
808 * Given an array of existing interlanguage links, returns those links which are not
809 * in $this and thus should be deleted.
810 * @param array $existing
813 private function getInterlangDeletions( $existing ) {
814 return array_diff_assoc( $existing, $this->mInterlangs
);
818 * Get array of properties which should be deleted.
819 * @param array $existing
822 function getPropertyDeletions( $existing ) {
823 return array_diff_assoc( $existing, $this->mProperties
);
827 * Given an array of existing interwiki links, returns those links which are not in $this
828 * and thus should be deleted.
829 * @param array $existing
832 private function getInterwikiDeletions( $existing ) {
834 foreach ( $existing as $prefix => $dbkeys ) {
835 if ( isset( $this->mInterwikis
[$prefix] ) ) {
836 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
838 $del[$prefix] = $existing[$prefix];
846 * Get an array of existing links, as a 2-D array
850 private function getExistingLinks() {
851 $res = $this->getDB()->select( 'pagelinks', [ 'pl_namespace', 'pl_title' ],
852 [ 'pl_from' => $this->mId
], __METHOD__
);
854 foreach ( $res as $row ) {
855 if ( !isset( $arr[$row->pl_namespace
] ) ) {
856 $arr[$row->pl_namespace
] = [];
858 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
865 * Get an array of existing templates, as a 2-D array
869 private function getExistingTemplates() {
870 $res = $this->getDB()->select( 'templatelinks', [ 'tl_namespace', 'tl_title' ],
871 [ 'tl_from' => $this->mId
], __METHOD__
);
873 foreach ( $res as $row ) {
874 if ( !isset( $arr[$row->tl_namespace
] ) ) {
875 $arr[$row->tl_namespace
] = [];
877 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
884 * Get an array of existing images, image names in the keys
888 private function getExistingImages() {
889 $res = $this->getDB()->select( 'imagelinks', [ 'il_to' ],
890 [ 'il_from' => $this->mId
], __METHOD__
);
892 foreach ( $res as $row ) {
893 $arr[$row->il_to
] = 1;
900 * Get an array of existing external links, URLs in the keys
904 private function getExistingExternals() {
905 $res = $this->getDB()->select( 'externallinks', [ 'el_to' ],
906 [ 'el_from' => $this->mId
], __METHOD__
);
908 foreach ( $res as $row ) {
909 $arr[$row->el_to
] = 1;
916 * Get an array of existing categories, with the name in the key and sort key in the value.
920 private function getExistingCategories() {
921 $res = $this->getDB()->select( 'categorylinks', [ 'cl_to', 'cl_sortkey_prefix' ],
922 [ 'cl_from' => $this->mId
], __METHOD__
);
924 foreach ( $res as $row ) {
925 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
932 * Get an array of existing interlanguage links, with the language code in the key and the
933 * title in the value.
937 private function getExistingInterlangs() {
938 $res = $this->getDB()->select( 'langlinks', [ 'll_lang', 'll_title' ],
939 [ 'll_from' => $this->mId
], __METHOD__
);
941 foreach ( $res as $row ) {
942 $arr[$row->ll_lang
] = $row->ll_title
;
949 * Get an array of existing inline interwiki links, as a 2-D array
950 * @return array (prefix => array(dbkey => 1))
952 private function getExistingInterwikis() {
953 $res = $this->getDB()->select( 'iwlinks', [ 'iwl_prefix', 'iwl_title' ],
954 [ 'iwl_from' => $this->mId
], __METHOD__
);
956 foreach ( $res as $row ) {
957 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
958 $arr[$row->iwl_prefix
] = [];
960 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
967 * Get an array of existing categories, with the name in the key and sort key in the value.
969 * @return array Array of property names and values
971 private function getExistingProperties() {
972 $res = $this->getDB()->select( 'page_props', [ 'pp_propname', 'pp_value' ],
973 [ 'pp_page' => $this->mId
], __METHOD__
);
975 foreach ( $res as $row ) {
976 $arr[$row->pp_propname
] = $row->pp_value
;
983 * Return the title object of the page being updated
986 public function getTitle() {
987 return $this->mTitle
;
991 * Returns parser output
993 * @return ParserOutput
995 public function getParserOutput() {
996 return $this->mParserOutput
;
1000 * Return the list of images used as generated by the parser
1003 public function getImages() {
1004 return $this->mImages
;
1008 * Set the revision corresponding to this LinksUpdate
1012 * @param Revision $revision
1014 public function setRevision( Revision
$revision ) {
1015 $this->mRevision
= $revision;
1020 * @return null|Revision
1022 public function getRevision() {
1023 return $this->mRevision
;
1027 * Set the User who triggered this LinksUpdate
1032 public function setTriggeringUser( User
$user ) {
1033 $this->user
= $user;
1040 public function getTriggeringUser() {
1045 * Invalidate any necessary link lists related to page property changes
1046 * @param array $changed
1048 private function invalidateProperties( $changed ) {
1049 global $wgPagePropLinkInvalidations;
1051 foreach ( $changed as $name => $value ) {
1052 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
1053 $inv = $wgPagePropLinkInvalidations[$name];
1054 if ( !is_array( $inv ) ) {
1057 foreach ( $inv as $table ) {
1058 DeferredUpdates
::addUpdate(
1059 new HTMLCacheUpdate( $this->mTitle
, $table, 'page-props' )
1067 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
1069 * @return null|array Array of Titles
1071 public function getAddedLinks() {
1072 if ( $this->linkInsertions
=== null ) {
1076 foreach ( $this->linkInsertions
as $insertion ) {
1077 $result[] = Title
::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
1084 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
1086 * @return null|array Array of Titles
1088 public function getRemovedLinks() {
1089 if ( $this->linkDeletions
=== null ) {
1093 foreach ( $this->linkDeletions
as $ns => $titles ) {
1094 foreach ( $titles as $title => $unused ) {
1095 $result[] = Title
::makeTitle( $ns, $title );
1103 * Fetch page properties added by this LinksUpdate.
1104 * Only available after the update is complete.
1106 * @return null|array
1108 public function getAddedProperties() {
1109 return $this->propertyInsertions
;
1113 * Fetch page properties removed by this LinksUpdate.
1114 * Only available after the update is complete.
1116 * @return null|array
1118 public function getRemovedProperties() {
1119 return $this->propertyDeletions
;
1123 * Update links table freshness
1125 private function updateLinksTimestamp() {
1127 // The link updates made here only reflect the freshness of the parser output
1128 $timestamp = $this->mParserOutput
->getCacheTime();
1129 $this->getDB()->update( 'page',
1130 [ 'page_links_updated' => $this->getDB()->timestamp( $timestamp ) ],
1131 [ 'page_id' => $this->mId
],
1140 private function getDB() {
1142 $this->db
= wfGetDB( DB_MASTER
);
1148 public function getAsJobSpecification() {
1149 if ( $this->user
) {
1151 'userId' => $this->user
->getId(),
1152 'userName' => $this->user
->getName(),
1158 if ( $this->mRevision
) {
1159 $triggeringRevisionId = $this->mRevision
->getId();
1161 $triggeringRevisionId = false;
1165 'wiki' => WikiMap
::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
1166 'job' => new JobSpecification(
1167 'refreshLinksPrioritized',
1169 // Reuse the parser cache if it was saved
1170 'rootJobTimestamp' => $this->mParserOutput
->getCacheTime(),
1171 'useRecursiveLinksUpdate' => $this->mRecursive
,
1172 'triggeringUser' => $userInfo,
1173 'triggeringRevisionId' => $triggeringRevisionId,
1174 'causeAction' => $this->getCauseAction(),
1175 'causeAgent' => $this->getCauseAgent()
1177 [ 'removeDuplicates' => true ],