23use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
28use Wikimedia\ScopedCallback;
38 use ProtectedHookAccessorTrait;
130 parent::__construct();
136 $this->mId =
$title->getArticleID( Title::READ_LATEST );
140 throw new InvalidArgumentException(
141 "The Title object yields no ID. "
142 .
"Perhaps the page [[{$title->getPrefixedDBkey()}]] doesn't exist?"
146 $this->mParserOutput = $parserOutput;
148 $this->mLinks = $parserOutput->
getLinks();
149 $this->mImages = $parserOutput->
getImages();
156 # Convert the format of the interlanguage links
157 # I didn't want to change it in the ParserOutput, because that array is passed all
158 # the way back to the skin, so either a skin API break would be required, or an
159 # inefficient back-conversion.
161 $this->mInterlangs = [];
162 foreach ( $ill as $link ) {
163 list( $key,
$title ) = explode(
':', $link, 2 );
164 $this->mInterlangs[$key] =
$title;
167 foreach ( $this->mCategories as &$sortkey ) {
168 # If the sortkey is longer then 255 bytes, it is truncated by DB, and then doesn't match
169 # when comparing existing vs current categories, causing T27254.
170 $sortkey = mb_strcut( $sortkey, 0, 255 );
173 $this->mRecursive = $recursive;
175 $this->getHookRunner()->onLinksUpdateConstructed( $this );
184 if ( $this->ticket ) {
187 $scopedLock = self::acquirePageLock( $this->
getDB(), $this->mId );
188 if ( !$scopedLock ) {
189 throw new RuntimeException(
"Could not acquire lock for page ID '{$this->mId}'." );
193 $this->getHookRunner()->onLinksUpdate( $this );
197 ScopedCallback::consume( $scopedLock );
203 $this->getHookRunner()->onLinksUpdateComplete( $this, $this->ticket );
218 $key =
"{$dbw->getDomainID()}:LinksUpdate:$why:pageid:$pageId";
220 if ( !$scopedLock ) {
221 $logger = LoggerFactory::getInstance(
'SecondaryDataUpdate' );
222 $logger->info(
"Could not acquire lock '{key}' for page ID '{page_id}'.", [
224 'page_id' => $pageId,
237 $this->
incrTableUpdate(
'pagelinks',
'pl', $this->linkDeletions, $this->linkInsertions );
248 # Invalidate all image description pages which had links added or removed
249 $imageUpdates = $imageDeletes + array_diff_key( $this->mImages, $existingIL );
260 $this->externalLinkDeletions,
261 $this->externalLinkInsertions );
271 # Inline interwiki links
295 $categoryInserts = array_diff_assoc( $this->mCategories, $existingCL );
296 $categoryUpdates = $categoryInserts + $categoryDeletes;
304 $this->propertyDeletions,
307 # Invalidate the necessary pages
308 $this->propertyInsertions = array_diff_assoc( $this->mProperties, $existingPP );
309 $changed = $this->propertyDeletions + $this->propertyInsertions;
312 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
316 # Refresh links of all pages including this page
317 # This will be in a separate transaction
318 if ( $this->mRecursive ) {
322 # Update the links table freshness for this title
336 self::queueRecursiveJobsForTable( $this->mTitle,
'templatelinks', $action, $agent );
337 if ( $this->mTitle->getNamespace() ==
NS_FILE ) {
339 self::queueRecursiveJobsForTable( $this->mTitle,
'imagelinks', $action, $agent );
342 $bc = $this->mTitle->getBacklinkCache();
349 foreach ( $bc->getCascadeProtectedLinks() as
$title ) {
353 'causeAction' => $action,
354 'causeAgent' => $agent
358 JobQueueGroup::singleton()->push( $jobs );
370 Title $title, $table, $action =
'unknown', $userName =
'unknown'
372 if (
$title->getBacklinkCache()->hasLinks( $table ) ) {
379 "refreshlinks:{$table}:{$title->getPrefixedText()}"
380 ) + [
'causeAction' => $action,
'causeAgent' => $userName ]
383 JobQueueGroup::singleton()->push(
$job );
404 if ( !$added && !$deleted ) {
408 $domainId = $this->
getDB()->getDomainID();
409 $wp = WikiPage::factory( $this->mTitle );
410 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
412 $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [
'domain' => $domainId ] );
415 $wp->updateCategoryCounts( array_map(
'strval', $addBatch ), [], $this->mId );
416 $lbf->commitAndWaitForReplication(
417 __METHOD__, $this->ticket, [
'domain' => $domainId ] );
421 $wp->updateCategoryCounts( [], array_map(
'strval', $deleteBatch ), $this->mId );
422 $lbf->commitAndWaitForReplication(
423 __METHOD__, $this->ticket, [
'domain' => $domainId ] );
432 $this->
getDB(),
NS_FILE, array_map(
'strval', array_keys( $images ) )
444 $services = MediaWikiServices::getInstance();
445 $bSize = $services->getMainConfig()->get(
'UpdateRowsPerQuery' );
446 $lbf = $services->getDBLoadBalancerFactory();
448 if ( $table ===
'page_props' ) {
449 $fromField =
'pp_page';
451 $fromField =
"{$prefix}_from";
455 if ( $table ===
'pagelinks' || $table ===
'templatelinks' || $table ===
'iwlinks' ) {
456 $baseKey = ( $table ===
'iwlinks' ) ?
'iwl_prefix' :
"{$prefix}_namespace";
459 $curDeletionBatch = [];
460 $deletionBatches = [];
461 foreach ( $deletions as $ns => $dbKeys ) {
462 foreach ( $dbKeys as $dbKey => $unused ) {
463 $curDeletionBatch[$ns][$dbKey] = 1;
464 if ( ++$curBatchSize >= $bSize ) {
465 $deletionBatches[] = $curDeletionBatch;
466 $curDeletionBatch = [];
471 if ( $curDeletionBatch ) {
472 $deletionBatches[] = $curDeletionBatch;
475 foreach ( $deletionBatches as $deletionBatch ) {
477 $fromField => $this->mId,
478 $this->
getDB()->makeWhereFrom2d( $deletionBatch, $baseKey,
"{$prefix}_title" )
482 if ( $table ===
'langlinks' ) {
483 $toField =
'll_lang';
484 } elseif ( $table ===
'page_props' ) {
485 $toField =
'pp_propname';
487 $toField = $prefix .
'_to';
490 $deletionBatches = array_chunk( array_keys( $deletions ), $bSize );
491 foreach ( $deletionBatches as $deletionBatch ) {
493 $fromField => $this->mId,
494 $toField => array_map(
'strval', $deletionBatch )
499 $domainId = $this->
getDB()->getDomainID();
501 foreach ( $deleteWheres as $deleteWhere ) {
502 $this->
getDB()->delete( $table, $deleteWhere, __METHOD__ );
503 $lbf->commitAndWaitForReplication(
504 __METHOD__, $this->ticket, [
'domain' => $domainId ]
508 $insertBatches = array_chunk( $insertions, $bSize );
509 foreach ( $insertBatches as $insertBatch ) {
510 $this->
getDB()->insert( $table, $insertBatch, __METHOD__, [
'IGNORE' ] );
511 $lbf->commitAndWaitForReplication(
512 __METHOD__, $this->ticket, [
'domain' => $domainId ]
516 if ( count( $insertions ) ) {
517 $this->getHookRunner()->onLinksUpdateAfterInsert( $this, $table, $insertions );
530 foreach ( $this->mLinks as $ns => $dbkeys ) {
531 $diffs = isset( $existing[$ns] )
532 ? array_diff_key( $dbkeys, $existing[$ns] )
534 foreach ( $diffs as $dbk => $id ) {
536 'pl_from' => $this->mId,
537 'pl_from_namespace' => $this->mTitle->getNamespace(),
538 'pl_namespace' => $ns,
554 foreach ( $this->mTemplates as $ns => $dbkeys ) {
555 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
556 foreach ( $diffs as $dbk => $id ) {
558 'tl_from' => $this->mId,
559 'tl_from_namespace' => $this->mTitle->getNamespace(),
560 'tl_namespace' => $ns,
577 $diffs = array_diff_key( $this->mImages, $existing );
578 foreach ( $diffs as $iname => $dummy ) {
580 'il_from' => $this->mId,
581 'il_from_namespace' => $this->mTitle->getNamespace(),
596 $diffs = array_diff_key( $this->mExternals, $existing );
597 foreach ( $diffs as $url => $dummy ) {
600 'el_from' => $this->mId,
602 'el_index' => $index,
603 'el_index_60' => substr( $index, 0, 60 ),
621 $diffs = array_diff_assoc( $this->mCategories, $existing );
624 $languageConverter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
625 ->getLanguageConverter();
627 $collation = Collation::singleton();
628 foreach ( $diffs as $name => $prefix ) {
630 $languageConverter->findVariantLink( $name, $nt,
true );
632 $type = MediaWikiServices::getInstance()->getNamespaceInfo()->
633 getCategoryLinkType( $this->mTitle->getNamespace() );
635 # Treat custom sortkeys as a prefix, so that if multiple
636 # things are forced to sort as '*' or something, they'll
637 # sort properly in the category rather than in page_id
639 $sortkey = $collation->getSortKey( $this->mTitle->getCategorySortkey( $prefix ) );
642 'cl_from' => $this->mId,
644 'cl_sortkey' => $sortkey,
645 'cl_timestamp' => $this->
getDB()->timestamp(),
646 'cl_sortkey_prefix' => $prefix,
663 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
667 'll_from' => $this->mId,
682 $diffs = array_diff_assoc( $this->mProperties, $existing );
685 foreach ( array_keys( $diffs ) as $name ) {
711 $value = $this->mProperties[$prop];
714 'pp_page' => $this->mId,
715 'pp_propname' => $prop,
716 'pp_value' => $value,
739 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
740 return floatval( $value );
754 foreach ( $this->mInterwikis as $prefix => $dbkeys ) {
755 $diffs = isset( $existing[$prefix] )
756 ? array_diff_key( $dbkeys, $existing[$prefix] )
759 foreach ( $diffs as $dbk => $id ) {
761 'iwl_from' => $this->mId,
762 'iwl_prefix' => $prefix,
779 foreach ( $existing as $ns => $dbkeys ) {
780 if ( isset( $this->mLinks[$ns] ) ) {
781 $del[$ns] = array_diff_key( $dbkeys, $this->mLinks[$ns] );
798 foreach ( $existing as $ns => $dbkeys ) {
799 if ( isset( $this->mTemplates[$ns] ) ) {
800 $del[$ns] = array_diff_key( $dbkeys, $this->mTemplates[$ns] );
816 return array_diff_key( $existing, $this->mImages );
826 return array_diff_key( $existing, $this->mExternals );
836 return array_diff_assoc( $existing, $this->mCategories );
846 return array_diff_assoc( $existing, $this->mInterlangs );
855 return array_diff_assoc( $existing, $this->mProperties );
866 foreach ( $existing as $prefix => $dbkeys ) {
867 if ( isset( $this->mInterwikis[$prefix] ) ) {
868 $del[$prefix] = array_diff_key( $dbkeys, $this->mInterwikis[$prefix] );
870 $del[$prefix] = $dbkeys;
883 $res = $this->
getDB()->select(
'pagelinks', [
'pl_namespace',
'pl_title' ],
884 [
'pl_from' => $this->mId ], __METHOD__ );
886 foreach (
$res as $row ) {
887 if ( !isset( $arr[$row->pl_namespace] ) ) {
888 $arr[$row->pl_namespace] = [];
890 $arr[$row->pl_namespace][$row->pl_title] = 1;
902 $res = $this->
getDB()->select(
'templatelinks', [
'tl_namespace',
'tl_title' ],
903 [
'tl_from' => $this->mId ], __METHOD__ );
905 foreach (
$res as $row ) {
906 if ( !isset( $arr[$row->tl_namespace] ) ) {
907 $arr[$row->tl_namespace] = [];
909 $arr[$row->tl_namespace][$row->tl_title] = 1;
921 $res = $this->
getDB()->select(
'imagelinks', [
'il_to' ],
922 [
'il_from' => $this->mId ], __METHOD__ );
924 foreach (
$res as $row ) {
925 $arr[$row->il_to] = 1;
937 $res = $this->
getDB()->select(
'externallinks', [
'el_to' ],
938 [
'el_from' => $this->mId ], __METHOD__ );
940 foreach (
$res as $row ) {
941 $arr[$row->el_to] = 1;
953 $res = $this->
getDB()->select(
'categorylinks', [
'cl_to',
'cl_sortkey_prefix' ],
954 [
'cl_from' => $this->mId ], __METHOD__ );
956 foreach (
$res as $row ) {
957 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
970 $res = $this->
getDB()->select(
'langlinks', [
'll_lang',
'll_title' ],
971 [
'll_from' => $this->mId ], __METHOD__ );
973 foreach (
$res as $row ) {
974 $arr[$row->ll_lang] = $row->ll_title;
985 $res = $this->
getDB()->select(
'iwlinks', [
'iwl_prefix',
'iwl_title' ],
986 [
'iwl_from' => $this->mId ], __METHOD__ );
988 foreach (
$res as $row ) {
989 if ( !isset( $arr[$row->iwl_prefix] ) ) {
990 $arr[$row->iwl_prefix] = [];
992 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
1004 $res = $this->
getDB()->select(
'page_props', [
'pp_propname',
'pp_value' ],
1005 [
'pp_page' => $this->mId ], __METHOD__ );
1007 foreach (
$res as $row ) {
1008 $arr[$row->pp_propname] = $row->pp_value;
1028 return $this->mParserOutput;
1036 return $this->mImages;
1058 $this->mRevisionRecord = $revisionRecord;
1068 $revRecord = $this->mRevisionRecord;
1069 return $revRecord ?
new Revision( $revRecord ) :
null;
1077 return $this->mRevisionRecord;
1087 $this->user = $user;
1106 foreach ( $changed as $name => $value ) {
1109 if ( !is_array( $inv ) ) {
1112 foreach ( $inv as $table ) {
1116 [
'causeAction' =>
'page-props' ]
1122 JobQueueGroup::singleton()->lazyPush( $jobs );
1131 if ( $this->linkInsertions ===
null ) {
1135 foreach ( $this->linkInsertions as $insertion ) {
1136 $result[] = Title::makeTitle( $insertion[
'pl_namespace'], $insertion[
'pl_title'] );
1148 if ( $this->linkDeletions ===
null ) {
1152 foreach ( $this->linkDeletions as $ns => $titles ) {
1153 foreach ( $titles as
$title => $unused ) {
1154 $result[] = Title::makeTitle( $ns,
$title );
1168 if ( $this->externalLinkInsertions ===
null ) {
1172 foreach ( $this->externalLinkInsertions as $key => $value ) {
1173 $result[] = $value[
'el_to'];
1185 if ( $this->externalLinkDeletions ===
null ) {
1188 return array_keys( $this->externalLinkDeletions );
1198 return $this->propertyInsertions;
1208 return $this->propertyDeletions;
1217 $timestamp = $this->mParserOutput->getCacheTime();
1218 $this->
getDB()->update(
'page',
1219 [
'page_links_updated' => $this->
getDB()->timestamp( $timestamp ) ],
1220 [
'page_id' => $this->mId ],
1244 return $this->mRecursive;
$wgPagePropLinkInvalidations
Page property link table invalidation lists.
$wgCategoryCollation
Specify how category names should be sorted, when listed on a category page.
$wgUpdateRowsPerQuery
Number of rows to update per query.
$wgPagePropsHaveSortkey
Whether the page_props table has a pp_sortkey column.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Deferrable Update for closure/callback updates that should use auto-commit mode.
Abstract base class for update jobs that do something with some secondary data extracted from article...
static newForBacklinks(Title $title, $table, $params=[])
static newRootJobParams( $key)
Get "root job" parameters for a task.
static makeIndexes( $url)
Converts a URL into a format for el_index.
Class the manages updates of *_link tables as well as similar extension-managed tables.
updateCategoryCounts(array $added, array $deleted)
Update all the appropriate counts in the category table.
array $mTemplates
Map of title strings to IDs for the template references, including broken ones.
updateLinksTimestamp()
Update links table freshness.
getTitle()
Return the title object of the page being updated.
getExistingLinks()
Get an array of existing links, as a 2-D array.
getExistingProperties()
Get an array of existing categories, with the name in the key and sort key in the value.
getImageInsertions( $existing=[])
Get an array of image insertions Skips the names specified in $existing.
getInterlangInsertions( $existing=[])
Get an array of interlanguage link insertions.
getExternalInsertions( $existing=[])
Get an array of externallinks insertions.
__construct(Title $title, ParserOutput $parserOutput, $recursive=true)
getPropertyInsertions( $existing=[])
Get an array of page property insertions.
array $mInterwikis
2-D map of (prefix => DBK => 1)
getAddedExternalLinks()
Fetch external links added by this LinksUpdate.
setTriggeringUser(User $user)
Set the User who triggered this LinksUpdate.
getTemplateDeletions( $existing)
Given an array of existing templates, returns those templates which are not in $this and thus should ...
invalidateCategories( $cats)
null array $propertyInsertions
Added properties if calculated.
doUpdate()
Update link tables with outgoing links from an updated article.
Title $mTitle
Title object of the article linked from.
getPropertySortKeyValue( $value)
Determines the sort key for the given property value.
int $mId
Page ID of the article linked from.
getAddedProperties()
Fetch page properties added by this LinksUpdate.
static queueRecursiveJobsForTable(Title $title, $table, $action='unknown', $userName='unknown')
Queue a RefreshLinks job for any table.
getLinkInsertions( $existing=[])
Get an array of pagelinks insertions for passing to the DB Skips the titles specified by the 2-D arra...
getExistingImages()
Get an array of existing images, image names in the keys.
getRemovedLinks()
Fetch page links removed by this LinksUpdate.
setRevision(Revision $revision)
Set the revision corresponding to this LinksUpdate.
getTemplateInsertions( $existing=[])
Get an array of template insertions.
getRemovedExternalLinks()
Fetch external links removed by this LinksUpdate.
invalidateImageDescriptions(array $images)
getCategoryDeletions( $existing)
Given an array of existing categories, returns those categories which are not in $this and thus shoul...
getInterwikiInsertions( $existing=[])
Get an array of interwiki insertions for passing to the DB Skips the titles specified by the 2-D arra...
ParserOutput $mParserOutput
null array[] $externalLinkInsertions
Added external links if calculated.
getImages()
Return the list of images used as generated by the parser.
array $mProperties
Map of arbitrary name to value.
getExistingInterlangs()
Get an array of existing interlanguage links, with the language code in the key and the title in the ...
getExistingExternals()
Get an array of existing external links, URLs in the keys.
getParserOutput()
Returns parser output.
getPropertyDeletions( $existing)
Get array of properties which should be deleted.
null array $externalLinkDeletions
Deleted external links if calculated.
invalidateProperties( $changed)
Invalidate any necessary link lists related to page property changes.
null array $linkDeletions
Deleted links if calculated.
null array $propertyDeletions
Deleted properties if calculated.
getInterlangDeletions( $existing)
Given an array of existing interlanguage links, returns those links which are not in $this and thus s...
getPagePropRowData( $prop)
Returns an associative array to be used for inserting a row into the page_props table.
getExistingCategories()
Get an array of existing categories, with the name in the key and sort key in the value.
RevisionRecord $mRevisionRecord
Revision for which this update has been triggered.
getAddedLinks()
Fetch page links added by this LinksUpdate.
queueRecursiveJobs()
Queue recursive jobs for this page.
array $mImages
DB keys of the images used, in the array key only.
getImageDeletions( $existing)
Given an array of existing images, returns those images which are not in $this and thus should be del...
array $mInterlangs
Map of language codes to titles.
isRecursive()
Whether or not this LinksUpdate will also update pages which transclude the current page or otherwise...
getLinkDeletions( $existing)
Given an array of existing links, returns those links which are not in $this and thus should be delet...
array $mCategories
Map of category names to sort keys.
bool $mRecursive
Whether to queue jobs for recursive updates.
getCategoryInsertions( $existing=[])
Get an array of category insertions.
getRemovedProperties()
Fetch page properties removed by this LinksUpdate.
int[][] $mLinks
Map of title strings to IDs for the links in the document -var array<int,array<string,...
setRevisionRecord(RevisionRecord $revisionRecord)
Set the RevisionRecord corresponding to this LinksUpdate.
getExistingTemplates()
Get an array of existing templates, as a 2-D array.
getExternalDeletions( $existing)
Given an array of existing external links, returns those links which are not in $this and thus should...
static acquirePageLock(IDatabase $dbw, $pageId, $why='atomicity')
Acquire a session-level lock for performing link table updates for a page on a DB.
getInterwikiDeletions( $existing)
Given an array of existing interwiki links, returns those links which are not in $this and thus shoul...
array $mExternals
URLs of external links, array key only.
incrTableUpdate( $table, $prefix, $deletions, $insertions)
Update a table by doing a delete query then an insert query.
getExistingInterwikis()
Get an array of existing inline interwiki links, as a 2-D array.
array[] null $linkInsertions
Added links if calculated.
static invalidatePages(IDatabase $dbw, $namespace, array $dbkeys)
Invalidate the cache of a list of pages from a single namespace.
Job to update link tables for pages.
static newPrioritized(Title $title, array $params)
Represents a title within MediaWiki.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
if(count( $args)< 1) $job
if(!isset( $args[0])) $lang