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 self
::queueRecursiveJobsForTable( $this->mTitle
, 'templatelinks' );
310 if ( $this->mTitle
->getNamespace() == NS_FILE
) {
311 // Process imagelinks in case the title is or was a redirect
312 self
::queueRecursiveJobsForTable( $this->mTitle
, 'imagelinks' );
315 $bc = $this->mTitle
->getBacklinkCache();
316 // Get jobs for cascade-protected backlinks for a high priority queue.
317 // If meta-templates change to using a new template, the new template
318 // should be implicitly protected as soon as possible, if applicable.
319 // These jobs duplicate a subset of the above ones, but can run sooner.
320 // Which ever runs first generally no-ops the other one.
322 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
323 $jobs[] = RefreshLinksJob
::newPrioritized( $title, [] );
325 JobQueueGroup
::singleton()->push( $jobs );
329 * Queue a RefreshLinks job for any table.
331 * @param Title $title Title to do job for
332 * @param string $table Table to use (e.g. 'templatelinks')
334 public static function queueRecursiveJobsForTable( Title
$title, $table ) {
335 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
336 $job = new RefreshLinksJob(
341 ] + Job
::newRootJobParams( // "overall" refresh links job info
342 "refreshlinks:{$table}:{$title->getPrefixedText()}"
346 JobQueueGroup
::singleton()->push( $job );
353 private function invalidateCategories( $cats ) {
354 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_CATEGORY
, array_keys( $cats ) );
358 * Update all the appropriate counts in the category table.
359 * @param array $added Associative array of category name => sort key
360 * @param array $deleted Associative array of category name => sort key
362 private function updateCategoryCounts( array $added, array $deleted ) {
363 global $wgUpdateRowsPerQuery;
365 if ( !$added && !$deleted ) {
369 $domainId = $this->getDB()->getDomainID();
370 $wp = WikiPage
::factory( $this->mTitle
);
371 $lbf = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
372 // T163801: try to release any row locks to reduce contention
373 $lbf->commitAndWaitForReplication( __METHOD__
, $this->ticket
, [ 'domain' => $domainId ] );
375 foreach ( array_chunk( array_keys( $added ), $wgUpdateRowsPerQuery ) as $addBatch ) {
376 $wp->updateCategoryCounts( $addBatch, [], $this->mId
);
377 $lbf->commitAndWaitForReplication(
378 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ] );
381 foreach ( array_chunk( array_keys( $deleted ), $wgUpdateRowsPerQuery ) as $deleteBatch ) {
382 $wp->updateCategoryCounts( [], $deleteBatch, $this->mId
);
383 $lbf->commitAndWaitForReplication(
384 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ] );
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 $domainId = $this->getDB()->getDomainID();
457 foreach ( $deleteWheres as $deleteWhere ) {
458 $this->getDB()->delete( $table, $deleteWhere, __METHOD__
);
459 $lbf->commitAndWaitForReplication(
460 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ]
464 $insertBatches = array_chunk( $insertions, $bSize );
465 foreach ( $insertBatches as $insertBatch ) {
466 $this->getDB()->insert( $table, $insertBatch, __METHOD__
, 'IGNORE' );
467 $lbf->commitAndWaitForReplication(
468 __METHOD__
, $this->ticket
, [ 'domain' => $domainId ]
472 if ( count( $insertions ) ) {
473 Hooks
::run( 'LinksUpdateAfterInsert', [ $this, $table, $insertions ] );
478 * Get an array of pagelinks insertions for passing to the DB
479 * Skips the titles specified by the 2-D array $existing
480 * @param array $existing
483 private function getLinkInsertions( $existing = [] ) {
485 foreach ( $this->mLinks
as $ns => $dbkeys ) {
486 $diffs = isset( $existing[$ns] )
487 ?
array_diff_key( $dbkeys, $existing[$ns] )
489 foreach ( $diffs as $dbk => $id ) {
491 'pl_from' => $this->mId
,
492 'pl_from_namespace' => $this->mTitle
->getNamespace(),
493 'pl_namespace' => $ns,
503 * Get an array of template insertions. Like getLinkInsertions()
504 * @param array $existing
507 private function getTemplateInsertions( $existing = [] ) {
509 foreach ( $this->mTemplates
as $ns => $dbkeys ) {
510 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
511 foreach ( $diffs as $dbk => $id ) {
513 'tl_from' => $this->mId
,
514 'tl_from_namespace' => $this->mTitle
->getNamespace(),
515 'tl_namespace' => $ns,
525 * Get an array of image insertions
526 * Skips the names specified in $existing
527 * @param array $existing
530 private function getImageInsertions( $existing = [] ) {
532 $diffs = array_diff_key( $this->mImages
, $existing );
533 foreach ( $diffs as $iname => $dummy ) {
535 'il_from' => $this->mId
,
536 'il_from_namespace' => $this->mTitle
->getNamespace(),
545 * Get an array of externallinks insertions. Skips the names specified in $existing
546 * @param array $existing
549 private function getExternalInsertions( $existing = [] ) {
551 $diffs = array_diff_key( $this->mExternals
, $existing );
552 foreach ( $diffs as $url => $dummy ) {
553 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
555 'el_from' => $this->mId
,
557 'el_index' => $index,
566 * Get an array of category insertions
568 * @param array $existing Mapping existing category names to sort keys. If both
569 * match a link in $this, the link will be omitted from the output
573 private function getCategoryInsertions( $existing = [] ) {
574 global $wgContLang, $wgCategoryCollation;
575 $diffs = array_diff_assoc( $this->mCategories
, $existing );
577 foreach ( $diffs as $name => $prefix ) {
578 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
579 $wgContLang->findVariantLink( $name, $nt, true );
581 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
583 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
589 # Treat custom sortkeys as a prefix, so that if multiple
590 # things are forced to sort as '*' or something, they'll
591 # sort properly in the category rather than in page_id
593 $sortkey = Collation
::singleton()->getSortKey(
594 $this->mTitle
->getCategorySortkey( $prefix ) );
597 'cl_from' => $this->mId
,
599 'cl_sortkey' => $sortkey,
600 'cl_timestamp' => $this->getDB()->timestamp(),
601 'cl_sortkey_prefix' => $prefix,
602 'cl_collation' => $wgCategoryCollation,
611 * Get an array of interlanguage link insertions
613 * @param array $existing Mapping existing language codes to titles
617 private function getInterlangInsertions( $existing = [] ) {
618 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
620 foreach ( $diffs as $lang => $title ) {
622 'll_from' => $this->mId
,
632 * Get an array of page property insertions
633 * @param array $existing
636 function getPropertyInsertions( $existing = [] ) {
637 $diffs = array_diff_assoc( $this->mProperties
, $existing );
640 foreach ( array_keys( $diffs ) as $name ) {
641 $arr[] = $this->getPagePropRowData( $name );
648 * Returns an associative array to be used for inserting a row into
649 * the page_props table. Besides the given property name, this will
650 * include the page id from $this->mId and any property value from
651 * $this->mProperties.
653 * The array returned will include the pp_sortkey field if this
654 * is present in the database (as indicated by $wgPagePropsHaveSortkey).
655 * The sortkey value is currently determined by getPropertySortKeyValue().
657 * @note this assumes that $this->mProperties[$prop] is defined.
659 * @param string $prop The name of the property.
663 private function getPagePropRowData( $prop ) {
664 global $wgPagePropsHaveSortkey;
666 $value = $this->mProperties
[$prop];
669 'pp_page' => $this->mId
,
670 'pp_propname' => $prop,
671 'pp_value' => $value,
674 if ( $wgPagePropsHaveSortkey ) {
675 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
682 * Determines the sort key for the given property value.
683 * This will return $value if it is a float or int,
684 * 1 or resp. 0 if it is a bool, and null otherwise.
686 * @note In the future, we may allow the sortkey to be specified explicitly
687 * in ParserOutput::setProperty.
689 * @param mixed $value
693 private function getPropertySortKeyValue( $value ) {
694 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
695 return floatval( $value );
702 * Get an array of interwiki insertions for passing to the DB
703 * Skips the titles specified by the 2-D array $existing
704 * @param array $existing
707 private function getInterwikiInsertions( $existing = [] ) {
709 foreach ( $this->mInterwikis
as $prefix => $dbkeys ) {
710 $diffs = isset( $existing[$prefix] )
711 ?
array_diff_key( $dbkeys, $existing[$prefix] )
714 foreach ( $diffs as $dbk => $id ) {
716 'iwl_from' => $this->mId
,
717 'iwl_prefix' => $prefix,
727 * Given an array of existing links, returns those links which are not in $this
728 * and thus should be deleted.
729 * @param array $existing
732 private function getLinkDeletions( $existing ) {
734 foreach ( $existing as $ns => $dbkeys ) {
735 if ( isset( $this->mLinks
[$ns] ) ) {
736 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
738 $del[$ns] = $existing[$ns];
746 * Given an array of existing templates, returns those templates which are not in $this
747 * and thus should be deleted.
748 * @param array $existing
751 private function getTemplateDeletions( $existing ) {
753 foreach ( $existing as $ns => $dbkeys ) {
754 if ( isset( $this->mTemplates
[$ns] ) ) {
755 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
757 $del[$ns] = $existing[$ns];
765 * Given an array of existing images, returns those images which are not in $this
766 * and thus should be deleted.
767 * @param array $existing
770 private function getImageDeletions( $existing ) {
771 return array_diff_key( $existing, $this->mImages
);
775 * Given an array of existing external links, returns those links which are not
776 * in $this and thus should be deleted.
777 * @param array $existing
780 private function getExternalDeletions( $existing ) {
781 return array_diff_key( $existing, $this->mExternals
);
785 * Given an array of existing categories, returns those categories which are not in $this
786 * and thus should be deleted.
787 * @param array $existing
790 private function getCategoryDeletions( $existing ) {
791 return array_diff_assoc( $existing, $this->mCategories
);
795 * Given an array of existing interlanguage links, returns those links which are not
796 * in $this and thus should be deleted.
797 * @param array $existing
800 private function getInterlangDeletions( $existing ) {
801 return array_diff_assoc( $existing, $this->mInterlangs
);
805 * Get array of properties which should be deleted.
806 * @param array $existing
809 function getPropertyDeletions( $existing ) {
810 return array_diff_assoc( $existing, $this->mProperties
);
814 * Given an array of existing interwiki links, returns those links which are not in $this
815 * and thus should be deleted.
816 * @param array $existing
819 private function getInterwikiDeletions( $existing ) {
821 foreach ( $existing as $prefix => $dbkeys ) {
822 if ( isset( $this->mInterwikis
[$prefix] ) ) {
823 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
825 $del[$prefix] = $existing[$prefix];
833 * Get an array of existing links, as a 2-D array
837 private function getExistingLinks() {
838 $res = $this->getDB()->select( 'pagelinks', [ 'pl_namespace', 'pl_title' ],
839 [ 'pl_from' => $this->mId
], __METHOD__
);
841 foreach ( $res as $row ) {
842 if ( !isset( $arr[$row->pl_namespace
] ) ) {
843 $arr[$row->pl_namespace
] = [];
845 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
852 * Get an array of existing templates, as a 2-D array
856 private function getExistingTemplates() {
857 $res = $this->getDB()->select( 'templatelinks', [ 'tl_namespace', 'tl_title' ],
858 [ 'tl_from' => $this->mId
], __METHOD__
);
860 foreach ( $res as $row ) {
861 if ( !isset( $arr[$row->tl_namespace
] ) ) {
862 $arr[$row->tl_namespace
] = [];
864 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
871 * Get an array of existing images, image names in the keys
875 private function getExistingImages() {
876 $res = $this->getDB()->select( 'imagelinks', [ 'il_to' ],
877 [ 'il_from' => $this->mId
], __METHOD__
);
879 foreach ( $res as $row ) {
880 $arr[$row->il_to
] = 1;
887 * Get an array of existing external links, URLs in the keys
891 private function getExistingExternals() {
892 $res = $this->getDB()->select( 'externallinks', [ 'el_to' ],
893 [ 'el_from' => $this->mId
], __METHOD__
);
895 foreach ( $res as $row ) {
896 $arr[$row->el_to
] = 1;
903 * Get an array of existing categories, with the name in the key and sort key in the value.
907 private function getExistingCategories() {
908 $res = $this->getDB()->select( 'categorylinks', [ 'cl_to', 'cl_sortkey_prefix' ],
909 [ 'cl_from' => $this->mId
], __METHOD__
);
911 foreach ( $res as $row ) {
912 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
919 * Get an array of existing interlanguage links, with the language code in the key and the
920 * title in the value.
924 private function getExistingInterlangs() {
925 $res = $this->getDB()->select( 'langlinks', [ 'll_lang', 'll_title' ],
926 [ 'll_from' => $this->mId
], __METHOD__
);
928 foreach ( $res as $row ) {
929 $arr[$row->ll_lang
] = $row->ll_title
;
936 * Get an array of existing inline interwiki links, as a 2-D array
937 * @return array (prefix => array(dbkey => 1))
939 private function getExistingInterwikis() {
940 $res = $this->getDB()->select( 'iwlinks', [ 'iwl_prefix', 'iwl_title' ],
941 [ 'iwl_from' => $this->mId
], __METHOD__
);
943 foreach ( $res as $row ) {
944 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
945 $arr[$row->iwl_prefix
] = [];
947 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
954 * Get an array of existing categories, with the name in the key and sort key in the value.
956 * @return array Array of property names and values
958 private function getExistingProperties() {
959 $res = $this->getDB()->select( 'page_props', [ 'pp_propname', 'pp_value' ],
960 [ 'pp_page' => $this->mId
], __METHOD__
);
962 foreach ( $res as $row ) {
963 $arr[$row->pp_propname
] = $row->pp_value
;
970 * Return the title object of the page being updated
973 public function getTitle() {
974 return $this->mTitle
;
978 * Returns parser output
980 * @return ParserOutput
982 public function getParserOutput() {
983 return $this->mParserOutput
;
987 * Return the list of images used as generated by the parser
990 public function getImages() {
991 return $this->mImages
;
995 * Set the revision corresponding to this LinksUpdate
999 * @param Revision $revision
1001 public function setRevision( Revision
$revision ) {
1002 $this->mRevision
= $revision;
1007 * @return null|Revision
1009 public function getRevision() {
1010 return $this->mRevision
;
1014 * Set the User who triggered this LinksUpdate
1019 public function setTriggeringUser( User
$user ) {
1020 $this->user
= $user;
1027 public function getTriggeringUser() {
1032 * Invalidate any necessary link lists related to page property changes
1033 * @param array $changed
1035 private function invalidateProperties( $changed ) {
1036 global $wgPagePropLinkInvalidations;
1038 foreach ( $changed as $name => $value ) {
1039 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
1040 $inv = $wgPagePropLinkInvalidations[$name];
1041 if ( !is_array( $inv ) ) {
1044 foreach ( $inv as $table ) {
1045 DeferredUpdates
::addUpdate( new HTMLCacheUpdate( $this->mTitle
, $table ) );
1052 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
1054 * @return null|array Array of Titles
1056 public function getAddedLinks() {
1057 if ( $this->linkInsertions
=== null ) {
1061 foreach ( $this->linkInsertions
as $insertion ) {
1062 $result[] = Title
::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
1069 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
1071 * @return null|array Array of Titles
1073 public function getRemovedLinks() {
1074 if ( $this->linkDeletions
=== null ) {
1078 foreach ( $this->linkDeletions
as $ns => $titles ) {
1079 foreach ( $titles as $title => $unused ) {
1080 $result[] = Title
::makeTitle( $ns, $title );
1088 * Fetch page properties added by this LinksUpdate.
1089 * Only available after the update is complete.
1091 * @return null|array
1093 public function getAddedProperties() {
1094 return $this->propertyInsertions
;
1098 * Fetch page properties removed by this LinksUpdate.
1099 * Only available after the update is complete.
1101 * @return null|array
1103 public function getRemovedProperties() {
1104 return $this->propertyDeletions
;
1108 * Update links table freshness
1110 private function updateLinksTimestamp() {
1112 // The link updates made here only reflect the freshness of the parser output
1113 $timestamp = $this->mParserOutput
->getCacheTime();
1114 $this->getDB()->update( 'page',
1115 [ 'page_links_updated' => $this->getDB()->timestamp( $timestamp ) ],
1116 [ 'page_id' => $this->mId
],
1125 private function getDB() {
1127 $this->db
= wfGetDB( DB_MASTER
);
1133 public function getAsJobSpecification() {
1134 if ( $this->user
) {
1136 'userId' => $this->user
->getId(),
1137 'userName' => $this->user
->getName(),
1143 if ( $this->mRevision
) {
1144 $triggeringRevisionId = $this->mRevision
->getId();
1146 $triggeringRevisionId = false;
1150 'wiki' => WikiMap
::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
1151 'job' => new JobSpecification(
1152 'refreshLinksPrioritized',
1154 // Reuse the parser cache if it was saved
1155 'rootJobTimestamp' => $this->mParserOutput
->getCacheTime(),
1156 'useRecursiveLinksUpdate' => $this->mRecursive
,
1157 'triggeringUser' => $userInfo,
1158 'triggeringRevisionId' => $triggeringRevisionId,
1160 [ 'removeDuplicates' => true ],