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 */
107 * @param Title $title Title of the page we're updating
108 * @param ParserOutput $parserOutput Output from a full parse of this page
109 * @param bool $recursive Queue jobs for recursive updates?
110 * @throws MWException
112 function __construct( Title
$title, ParserOutput
$parserOutput, $recursive = true ) {
113 parent
::__construct();
115 $this->mTitle
= $title;
116 $this->mId
= $title->getArticleID( Title
::GAID_FOR_UPDATE
);
119 throw new InvalidArgumentException(
120 "The Title object yields no ID. Perhaps the page doesn't exist?"
124 $this->mParserOutput
= $parserOutput;
126 $this->mLinks
= $parserOutput->getLinks();
127 $this->mImages
= $parserOutput->getImages();
128 $this->mTemplates
= $parserOutput->getTemplates();
129 $this->mExternals
= $parserOutput->getExternalLinks();
130 $this->mCategories
= $parserOutput->getCategories();
131 $this->mProperties
= $parserOutput->getProperties();
132 $this->mInterwikis
= $parserOutput->getInterwikiLinks();
134 # Convert the format of the interlanguage links
135 # I didn't want to change it in the ParserOutput, because that array is passed all
136 # the way back to the skin, so either a skin API break would be required, or an
137 # inefficient back-conversion.
138 $ill = $parserOutput->getLanguageLinks();
139 $this->mInterlangs
= [];
140 foreach ( $ill as $link ) {
141 list( $key, $title ) = explode( ':', $link, 2 );
142 $this->mInterlangs
[$key] = $title;
145 foreach ( $this->mCategories
as &$sortkey ) {
146 # If the sortkey is longer then 255 bytes,
147 # it truncated by DB, and then doesn't get
148 # matched when comparing existing vs current
149 # categories, causing T27254.
150 # Also. substr behaves weird when given "".
151 if ( $sortkey !== '' ) {
152 $sortkey = substr( $sortkey, 0, 255 );
156 $this->mRecursive
= $recursive;
158 // Avoid PHP 7.1 warning from passing $this by reference
159 $linksUpdate = $this;
160 Hooks
::run( 'LinksUpdateConstructed', [ &$linksUpdate ] );
164 * Update link tables with outgoing links from an updated article
166 * @note: this is managed by DeferredUpdates::execute(). Do not run this in a transaction.
168 public function doUpdate() {
169 if ( $this->ticket
) {
170 // Make sure all links update threads see the changes of each other.
171 // This handles the case when updates have to batched into several COMMITs.
172 $scopedLock = self
::acquirePageLock( $this->getDB(), $this->mId
);
175 // Avoid PHP 7.1 warning from passing $this by reference
176 $linksUpdate = $this;
177 Hooks
::run( 'LinksUpdate', [ &$linksUpdate ] );
178 $this->doIncrementalUpdate();
180 // Commit and release the lock (if set)
181 ScopedCallback
::consume( $scopedLock );
182 // Run post-commit hooks without DBO_TRX
183 $this->getDB()->onTransactionIdle(
185 // Avoid PHP 7.1 warning from passing $this by reference
186 $linksUpdate = $this;
187 Hooks
::run( 'LinksUpdateComplete', [ &$linksUpdate, $this->ticket
] );
194 * Acquire a lock for performing link table updates for a page on a DB
196 * @param IDatabase $dbw
197 * @param integer $pageId
198 * @param string $why One of (job, atomicity)
199 * @return ScopedCallback
200 * @throws RuntimeException
203 public static function acquirePageLock( IDatabase
$dbw, $pageId, $why = 'atomicity' ) {
204 $key = "LinksUpdate:$why:pageid:$pageId";
205 $scopedLock = $dbw->getScopedLockAndFlush( $key, __METHOD__
, 15 );
206 if ( !$scopedLock ) {
207 throw new RuntimeException( "Could not acquire lock '$key'." );
213 protected function doIncrementalUpdate() {
215 $existingPL = $this->getExistingLinks();
216 $this->linkDeletions
= $this->getLinkDeletions( $existingPL );
217 $this->linkInsertions
= $this->getLinkInsertions( $existingPL );
218 $this->incrTableUpdate( 'pagelinks', 'pl', $this->linkDeletions
, $this->linkInsertions
);
221 $existingIL = $this->getExistingImages();
222 $imageDeletes = $this->getImageDeletions( $existingIL );
223 $this->incrTableUpdate(
227 $this->getImageInsertions( $existingIL ) );
229 # Invalidate all image description pages which had links added or removed
230 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existingIL );
231 $this->invalidateImageDescriptions( $imageUpdates );
234 $existingEL = $this->getExistingExternals();
235 $this->incrTableUpdate(
238 $this->getExternalDeletions( $existingEL ),
239 $this->getExternalInsertions( $existingEL ) );
242 $existingLL = $this->getExistingInterlangs();
243 $this->incrTableUpdate(
246 $this->getInterlangDeletions( $existingLL ),
247 $this->getInterlangInsertions( $existingLL ) );
249 # Inline interwiki links
250 $existingIW = $this->getExistingInterwikis();
251 $this->incrTableUpdate(
254 $this->getInterwikiDeletions( $existingIW ),
255 $this->getInterwikiInsertions( $existingIW ) );
258 $existingTL = $this->getExistingTemplates();
259 $this->incrTableUpdate(
262 $this->getTemplateDeletions( $existingTL ),
263 $this->getTemplateInsertions( $existingTL ) );
266 $existingCL = $this->getExistingCategories();
267 $categoryDeletes = $this->getCategoryDeletions( $existingCL );
268 $this->incrTableUpdate(
272 $this->getCategoryInsertions( $existingCL ) );
273 $categoryInserts = array_diff_assoc( $this->mCategories
, $existingCL );
274 $categoryUpdates = $categoryInserts +
$categoryDeletes;
277 $existingPP = $this->getExistingProperties();
278 $this->propertyDeletions
= $this->getPropertyDeletions( $existingPP );
279 $this->incrTableUpdate(
282 $this->propertyDeletions
,
283 $this->getPropertyInsertions( $existingPP ) );
285 # Invalidate the necessary pages
286 $this->propertyInsertions
= array_diff_assoc( $this->mProperties
, $existingPP );
287 $changed = $this->propertyDeletions +
$this->propertyInsertions
;
288 $this->invalidateProperties( $changed );
290 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
291 $this->invalidateCategories( $categoryUpdates );
292 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
294 # Refresh links of all pages including this page
295 # This will be in a separate transaction
296 if ( $this->mRecursive
) {
297 $this->queueRecursiveJobs();
300 # Update the links table freshness for this title
301 $this->updateLinksTimestamp();
305 * Queue recursive jobs for this page
307 * Which means do LinksUpdate on all pages that include the current page,
308 * using the job queue.
310 protected function queueRecursiveJobs() {
311 self
::queueRecursiveJobsForTable( $this->mTitle
, 'templatelinks' );
312 if ( $this->mTitle
->getNamespace() == NS_FILE
) {
313 // Process imagelinks in case the title is or was a redirect
314 self
::queueRecursiveJobsForTable( $this->mTitle
, 'imagelinks' );
317 $bc = $this->mTitle
->getBacklinkCache();
318 // Get jobs for cascade-protected backlinks for a high priority queue.
319 // If meta-templates change to using a new template, the new template
320 // should be implicitly protected as soon as possible, if applicable.
321 // These jobs duplicate a subset of the above ones, but can run sooner.
322 // Which ever runs first generally no-ops the other one.
324 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
325 $jobs[] = RefreshLinksJob
::newPrioritized( $title, [] );
327 JobQueueGroup
::singleton()->push( $jobs );
331 * Queue a RefreshLinks job for any table.
333 * @param Title $title Title to do job for
334 * @param string $table Table to use (e.g. 'templatelinks')
336 public static function queueRecursiveJobsForTable( Title
$title, $table ) {
337 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
338 $job = new RefreshLinksJob(
343 ] + Job
::newRootJobParams( // "overall" refresh links job info
344 "refreshlinks:{$table}:{$title->getPrefixedText()}"
348 JobQueueGroup
::singleton()->push( $job );
355 private function invalidateCategories( $cats ) {
356 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_CATEGORY
, array_keys( $cats ) );
360 * Update all the appropriate counts in the category table.
361 * @param array $added Associative array of category name => sort key
362 * @param array $deleted Associative array of category name => sort key
364 private function updateCategoryCounts( array $added, array $deleted ) {
365 global $wgUpdateRowsPerQuery;
367 if ( !$added && !$deleted ) {
371 $wikiId = $this->getDB()->getWikiID();
372 $wp = WikiPage
::factory( $this->mTitle
);
373 $lbf = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
374 // T163801: try to release any row locks to reduce contention
375 $lbf->commitAndWaitForReplication( __METHOD__
, $this->ticket
, [ 'wiki' => $wikiId ] );
377 foreach ( array_chunk( array_keys( $added ), $wgUpdateRowsPerQuery ) as $addBatch ) {
378 $wp->updateCategoryCounts( $addBatch, [], $this->mId
);
379 $lbf->commitAndWaitForReplication( __METHOD__
, $this->ticket
, [ 'wiki' => $wikiId ] );
382 foreach ( array_chunk( array_keys( $deleted ), $wgUpdateRowsPerQuery ) as $deleteBatch ) {
383 $wp->updateCategoryCounts( [], $deleteBatch, $this->mId
);
384 $lbf->commitAndWaitForReplication( __METHOD__
, $this->ticket
, [ 'wiki' => $wikiId ] );
389 * @param array $images
391 private function invalidateImageDescriptions( $images ) {
392 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_FILE
, array_keys( $images ) );
396 * Update a table by doing a delete query then an insert query
397 * @param string $table Table name
398 * @param string $prefix Field name prefix
399 * @param array $deletions
400 * @param array $insertions Rows to insert
402 private function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
403 $services = MediaWikiServices
::getInstance();
404 $bSize = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
405 $lbf = $services->getDBLoadBalancerFactory();
407 if ( $table === 'page_props' ) {
408 $fromField = 'pp_page';
410 $fromField = "{$prefix}_from";
413 $deleteWheres = []; // list of WHERE clause arrays for each DB delete() call
414 if ( $table === 'pagelinks' ||
$table === 'templatelinks' ||
$table === 'iwlinks' ) {
415 $baseKey = ( $table === 'iwlinks' ) ?
'iwl_prefix' : "{$prefix}_namespace";
418 $curDeletionBatch = [];
419 $deletionBatches = [];
420 foreach ( $deletions as $ns => $dbKeys ) {
421 foreach ( $dbKeys as $dbKey => $unused ) {
422 $curDeletionBatch[$ns][$dbKey] = 1;
423 if ( ++
$curBatchSize >= $bSize ) {
424 $deletionBatches[] = $curDeletionBatch;
425 $curDeletionBatch = [];
430 if ( $curDeletionBatch ) {
431 $deletionBatches[] = $curDeletionBatch;
434 foreach ( $deletionBatches as $deletionBatch ) {
436 $fromField => $this->mId
,
437 $this->getDB()->makeWhereFrom2d( $deletionBatch, $baseKey, "{$prefix}_title" )
441 if ( $table === 'langlinks' ) {
442 $toField = 'll_lang';
443 } elseif ( $table === 'page_props' ) {
444 $toField = 'pp_propname';
446 $toField = $prefix . '_to';
449 $deletionBatches = array_chunk( array_keys( $deletions ), $bSize );
450 foreach ( $deletionBatches as $deletionBatch ) {
451 $deleteWheres[] = [ $fromField => $this->mId
, $toField => $deletionBatch ];
455 foreach ( $deleteWheres as $deleteWhere ) {
456 $this->getDB()->delete( $table, $deleteWhere, __METHOD__
);
457 $lbf->commitAndWaitForReplication(
458 __METHOD__
, $this->ticket
, [ 'wiki' => $this->getDB()->getWikiID() ]
462 $insertBatches = array_chunk( $insertions, $bSize );
463 foreach ( $insertBatches as $insertBatch ) {
464 $this->getDB()->insert( $table, $insertBatch, __METHOD__
, 'IGNORE' );
465 $lbf->commitAndWaitForReplication(
466 __METHOD__
, $this->ticket
, [ 'wiki' => $this->getDB()->getWikiID() ]
470 if ( count( $insertions ) ) {
471 Hooks
::run( 'LinksUpdateAfterInsert', [ $this, $table, $insertions ] );
476 * Get an array of pagelinks insertions for passing to the DB
477 * Skips the titles specified by the 2-D array $existing
478 * @param array $existing
481 private function getLinkInsertions( $existing = [] ) {
483 foreach ( $this->mLinks
as $ns => $dbkeys ) {
484 $diffs = isset( $existing[$ns] )
485 ?
array_diff_key( $dbkeys, $existing[$ns] )
487 foreach ( $diffs as $dbk => $id ) {
489 'pl_from' => $this->mId
,
490 'pl_from_namespace' => $this->mTitle
->getNamespace(),
491 'pl_namespace' => $ns,
501 * Get an array of template insertions. Like getLinkInsertions()
502 * @param array $existing
505 private function getTemplateInsertions( $existing = [] ) {
507 foreach ( $this->mTemplates
as $ns => $dbkeys ) {
508 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
509 foreach ( $diffs as $dbk => $id ) {
511 'tl_from' => $this->mId
,
512 'tl_from_namespace' => $this->mTitle
->getNamespace(),
513 'tl_namespace' => $ns,
523 * Get an array of image insertions
524 * Skips the names specified in $existing
525 * @param array $existing
528 private function getImageInsertions( $existing = [] ) {
530 $diffs = array_diff_key( $this->mImages
, $existing );
531 foreach ( $diffs as $iname => $dummy ) {
533 'il_from' => $this->mId
,
534 'il_from_namespace' => $this->mTitle
->getNamespace(),
543 * Get an array of externallinks insertions. Skips the names specified in $existing
544 * @param array $existing
547 private function getExternalInsertions( $existing = [] ) {
549 $diffs = array_diff_key( $this->mExternals
, $existing );
550 foreach ( $diffs as $url => $dummy ) {
551 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
553 'el_id' => $this->getDB()->nextSequenceValue( 'externallinks_el_id_seq' ),
554 'el_from' => $this->mId
,
556 'el_index' => $index,
565 * Get an array of category insertions
567 * @param array $existing Mapping existing category names to sort keys. If both
568 * match a link in $this, the link will be omitted from the output
572 private function getCategoryInsertions( $existing = [] ) {
573 global $wgContLang, $wgCategoryCollation;
574 $diffs = array_diff_assoc( $this->mCategories
, $existing );
576 foreach ( $diffs as $name => $prefix ) {
577 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
578 $wgContLang->findVariantLink( $name, $nt, true );
580 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
582 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
588 # Treat custom sortkeys as a prefix, so that if multiple
589 # things are forced to sort as '*' or something, they'll
590 # sort properly in the category rather than in page_id
592 $sortkey = Collation
::singleton()->getSortKey(
593 $this->mTitle
->getCategorySortkey( $prefix ) );
596 'cl_from' => $this->mId
,
598 'cl_sortkey' => $sortkey,
599 'cl_timestamp' => $this->getDB()->timestamp(),
600 'cl_sortkey_prefix' => $prefix,
601 'cl_collation' => $wgCategoryCollation,
610 * Get an array of interlanguage link insertions
612 * @param array $existing Mapping existing language codes to titles
616 private function getInterlangInsertions( $existing = [] ) {
617 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
619 foreach ( $diffs as $lang => $title ) {
621 'll_from' => $this->mId
,
631 * Get an array of page property insertions
632 * @param array $existing
635 function getPropertyInsertions( $existing = [] ) {
636 $diffs = array_diff_assoc( $this->mProperties
, $existing );
639 foreach ( array_keys( $diffs ) as $name ) {
640 $arr[] = $this->getPagePropRowData( $name );
647 * Returns an associative array to be used for inserting a row into
648 * the page_props table. Besides the given property name, this will
649 * include the page id from $this->mId and any property value from
650 * $this->mProperties.
652 * The array returned will include the pp_sortkey field if this
653 * is present in the database (as indicated by $wgPagePropsHaveSortkey).
654 * The sortkey value is currently determined by getPropertySortKeyValue().
656 * @note this assumes that $this->mProperties[$prop] is defined.
658 * @param string $prop The name of the property.
662 private function getPagePropRowData( $prop ) {
663 global $wgPagePropsHaveSortkey;
665 $value = $this->mProperties
[$prop];
668 'pp_page' => $this->mId
,
669 'pp_propname' => $prop,
670 'pp_value' => $value,
673 if ( $wgPagePropsHaveSortkey ) {
674 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
681 * Determines the sort key for the given property value.
682 * This will return $value if it is a float or int,
683 * 1 or resp. 0 if it is a bool, and null otherwise.
685 * @note In the future, we may allow the sortkey to be specified explicitly
686 * in ParserOutput::setProperty.
688 * @param mixed $value
692 private function getPropertySortKeyValue( $value ) {
693 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
694 return floatval( $value );
701 * Get an array of interwiki insertions for passing to the DB
702 * Skips the titles specified by the 2-D array $existing
703 * @param array $existing
706 private function getInterwikiInsertions( $existing = [] ) {
708 foreach ( $this->mInterwikis
as $prefix => $dbkeys ) {
709 $diffs = isset( $existing[$prefix] )
710 ?
array_diff_key( $dbkeys, $existing[$prefix] )
713 foreach ( $diffs as $dbk => $id ) {
715 'iwl_from' => $this->mId
,
716 'iwl_prefix' => $prefix,
726 * Given an array of existing links, returns those links which are not in $this
727 * and thus should be deleted.
728 * @param array $existing
731 private function getLinkDeletions( $existing ) {
733 foreach ( $existing as $ns => $dbkeys ) {
734 if ( isset( $this->mLinks
[$ns] ) ) {
735 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
737 $del[$ns] = $existing[$ns];
745 * Given an array of existing templates, returns those templates which are not in $this
746 * and thus should be deleted.
747 * @param array $existing
750 private function getTemplateDeletions( $existing ) {
752 foreach ( $existing as $ns => $dbkeys ) {
753 if ( isset( $this->mTemplates
[$ns] ) ) {
754 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
756 $del[$ns] = $existing[$ns];
764 * Given an array of existing images, returns those images which are not in $this
765 * and thus should be deleted.
766 * @param array $existing
769 private function getImageDeletions( $existing ) {
770 return array_diff_key( $existing, $this->mImages
);
774 * Given an array of existing external links, returns those links which are not
775 * in $this and thus should be deleted.
776 * @param array $existing
779 private function getExternalDeletions( $existing ) {
780 return array_diff_key( $existing, $this->mExternals
);
784 * Given an array of existing categories, returns those categories which are not in $this
785 * and thus should be deleted.
786 * @param array $existing
789 private function getCategoryDeletions( $existing ) {
790 return array_diff_assoc( $existing, $this->mCategories
);
794 * Given an array of existing interlanguage links, returns those links which are not
795 * in $this and thus should be deleted.
796 * @param array $existing
799 private function getInterlangDeletions( $existing ) {
800 return array_diff_assoc( $existing, $this->mInterlangs
);
804 * Get array of properties which should be deleted.
805 * @param array $existing
808 function getPropertyDeletions( $existing ) {
809 return array_diff_assoc( $existing, $this->mProperties
);
813 * Given an array of existing interwiki links, returns those links which are not in $this
814 * and thus should be deleted.
815 * @param array $existing
818 private function getInterwikiDeletions( $existing ) {
820 foreach ( $existing as $prefix => $dbkeys ) {
821 if ( isset( $this->mInterwikis
[$prefix] ) ) {
822 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
824 $del[$prefix] = $existing[$prefix];
832 * Get an array of existing links, as a 2-D array
836 private function getExistingLinks() {
837 $res = $this->getDB()->select( 'pagelinks', [ 'pl_namespace', 'pl_title' ],
838 [ 'pl_from' => $this->mId
], __METHOD__
);
840 foreach ( $res as $row ) {
841 if ( !isset( $arr[$row->pl_namespace
] ) ) {
842 $arr[$row->pl_namespace
] = [];
844 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
851 * Get an array of existing templates, as a 2-D array
855 private function getExistingTemplates() {
856 $res = $this->getDB()->select( 'templatelinks', [ 'tl_namespace', 'tl_title' ],
857 [ 'tl_from' => $this->mId
], __METHOD__
);
859 foreach ( $res as $row ) {
860 if ( !isset( $arr[$row->tl_namespace
] ) ) {
861 $arr[$row->tl_namespace
] = [];
863 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
870 * Get an array of existing images, image names in the keys
874 private function getExistingImages() {
875 $res = $this->getDB()->select( 'imagelinks', [ 'il_to' ],
876 [ 'il_from' => $this->mId
], __METHOD__
);
878 foreach ( $res as $row ) {
879 $arr[$row->il_to
] = 1;
886 * Get an array of existing external links, URLs in the keys
890 private function getExistingExternals() {
891 $res = $this->getDB()->select( 'externallinks', [ 'el_to' ],
892 [ 'el_from' => $this->mId
], __METHOD__
);
894 foreach ( $res as $row ) {
895 $arr[$row->el_to
] = 1;
902 * Get an array of existing categories, with the name in the key and sort key in the value.
906 private function getExistingCategories() {
907 $res = $this->getDB()->select( 'categorylinks', [ 'cl_to', 'cl_sortkey_prefix' ],
908 [ 'cl_from' => $this->mId
], __METHOD__
);
910 foreach ( $res as $row ) {
911 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
918 * Get an array of existing interlanguage links, with the language code in the key and the
919 * title in the value.
923 private function getExistingInterlangs() {
924 $res = $this->getDB()->select( 'langlinks', [ 'll_lang', 'll_title' ],
925 [ 'll_from' => $this->mId
], __METHOD__
);
927 foreach ( $res as $row ) {
928 $arr[$row->ll_lang
] = $row->ll_title
;
935 * Get an array of existing inline interwiki links, as a 2-D array
936 * @return array (prefix => array(dbkey => 1))
938 private function getExistingInterwikis() {
939 $res = $this->getDB()->select( 'iwlinks', [ 'iwl_prefix', 'iwl_title' ],
940 [ 'iwl_from' => $this->mId
], __METHOD__
);
942 foreach ( $res as $row ) {
943 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
944 $arr[$row->iwl_prefix
] = [];
946 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
953 * Get an array of existing categories, with the name in the key and sort key in the value.
955 * @return array Array of property names and values
957 private function getExistingProperties() {
958 $res = $this->getDB()->select( 'page_props', [ 'pp_propname', 'pp_value' ],
959 [ 'pp_page' => $this->mId
], __METHOD__
);
961 foreach ( $res as $row ) {
962 $arr[$row->pp_propname
] = $row->pp_value
;
969 * Return the title object of the page being updated
972 public function getTitle() {
973 return $this->mTitle
;
977 * Returns parser output
979 * @return ParserOutput
981 public function getParserOutput() {
982 return $this->mParserOutput
;
986 * Return the list of images used as generated by the parser
989 public function getImages() {
990 return $this->mImages
;
994 * Set the revision corresponding to this LinksUpdate
998 * @param Revision $revision
1000 public function setRevision( Revision
$revision ) {
1001 $this->mRevision
= $revision;
1006 * @return null|Revision
1008 public function getRevision() {
1009 return $this->mRevision
;
1013 * Set the User who triggered this LinksUpdate
1018 public function setTriggeringUser( User
$user ) {
1019 $this->user
= $user;
1026 public function getTriggeringUser() {
1031 * Invalidate any necessary link lists related to page property changes
1032 * @param array $changed
1034 private function invalidateProperties( $changed ) {
1035 global $wgPagePropLinkInvalidations;
1037 foreach ( $changed as $name => $value ) {
1038 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
1039 $inv = $wgPagePropLinkInvalidations[$name];
1040 if ( !is_array( $inv ) ) {
1043 foreach ( $inv as $table ) {
1044 DeferredUpdates
::addUpdate( new HTMLCacheUpdate( $this->mTitle
, $table ) );
1051 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
1053 * @return null|array Array of Titles
1055 public function getAddedLinks() {
1056 if ( $this->linkInsertions
=== null ) {
1060 foreach ( $this->linkInsertions
as $insertion ) {
1061 $result[] = Title
::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
1068 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
1070 * @return null|array Array of Titles
1072 public function getRemovedLinks() {
1073 if ( $this->linkDeletions
=== null ) {
1077 foreach ( $this->linkDeletions
as $ns => $titles ) {
1078 foreach ( $titles as $title => $unused ) {
1079 $result[] = Title
::makeTitle( $ns, $title );
1087 * Fetch page properties added by this LinksUpdate.
1088 * Only available after the update is complete.
1090 * @return null|array
1092 public function getAddedProperties() {
1093 return $this->propertyInsertions
;
1097 * Fetch page properties removed by this LinksUpdate.
1098 * Only available after the update is complete.
1100 * @return null|array
1102 public function getRemovedProperties() {
1103 return $this->propertyDeletions
;
1107 * Update links table freshness
1109 private function updateLinksTimestamp() {
1111 // The link updates made here only reflect the freshness of the parser output
1112 $timestamp = $this->mParserOutput
->getCacheTime();
1113 $this->getDB()->update( 'page',
1114 [ 'page_links_updated' => $this->getDB()->timestamp( $timestamp ) ],
1115 [ 'page_id' => $this->mId
],
1124 private function getDB() {
1126 $this->db
= wfGetDB( DB_MASTER
);
1132 public function getAsJobSpecification() {
1133 if ( $this->user
) {
1135 'userId' => $this->user
->getId(),
1136 'userName' => $this->user
->getName(),
1142 if ( $this->mRevision
) {
1143 $triggeringRevisionId = $this->mRevision
->getId();
1145 $triggeringRevisionId = false;
1149 'wiki' => $this->getDB()->getWikiID(),
1150 'job' => new JobSpecification(
1151 'refreshLinksPrioritized',
1153 // Reuse the parser cache if it was saved
1154 'rootJobTimestamp' => $this->mParserOutput
->getCacheTime(),
1155 'useRecursiveLinksUpdate' => $this->mRecursive
,
1156 'triggeringUser' => $userInfo,
1157 'triggeringRevisionId' => $triggeringRevisionId,
1159 [ 'removeDuplicates' => true ],