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 MediaWiki\MediaWikiServices
;
26 * Class the manages updates of *_link tables as well as similar extension-managed tables
28 * @note: LinksUpdate is managed by DeferredUpdates::execute(). Do not run this in a transaction.
30 * See docs/deferred.txt
32 class LinksUpdate
extends DataUpdate
implements EnqueueableDataUpdate
{
33 // @todo make members protected, but make sure extensions don't break
35 /** @var int Page ID of the article linked from */
38 /** @var Title Title object of the article linked from */
41 /** @var ParserOutput */
42 public $mParserOutput;
44 /** @var array Map of title strings to IDs for the links in the document */
47 /** @var array DB keys of the images used, in the array key only */
50 /** @var array Map of title strings to IDs for the template references, including broken ones */
53 /** @var array URLs of external links, array key only */
56 /** @var array Map of category names to sort keys */
59 /** @var array Map of language codes to titles */
62 /** @var array 2-D map of (prefix => DBK => 1) */
65 /** @var array Map of arbitrary name to value */
68 /** @var bool Whether to queue jobs for recursive updates */
71 /** @var Revision Revision for which this update has been triggered */
75 * @var null|array Added links if calculated.
77 private $linkInsertions = null;
80 * @var null|array Deleted links if calculated.
82 private $linkDeletions = null;
85 * @var null|array Added properties if calculated.
87 private $propertyInsertions = null;
90 * @var null|array Deleted properties if calculated.
92 private $propertyDeletions = null;
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 bug 25254.
148 # Also. substr behaves weird when given "".
149 if ( $sortkey !== '' ) {
150 $sortkey = substr( $sortkey, 0, 255 );
154 $this->mRecursive
= $recursive;
156 Hooks
::run( 'LinksUpdateConstructed', [ &$this ] );
160 * Update link tables with outgoing links from an updated article
162 * @note: this is managed by DeferredUpdates::execute(). Do not run this in a transaction.
164 public function doUpdate() {
165 // Make sure all links update threads see the changes of each other.
166 // This handles the case when updates have to batched into several COMMITs.
167 $scopedLock = self
::acquirePageLock( $this->getDB(), $this->mId
);
169 Hooks
::run( 'LinksUpdate', [ &$this ] );
170 $this->doIncrementalUpdate();
172 // Commit and release the lock
173 ScopedCallback
::consume( $scopedLock );
174 // Run post-commit hooks without DBO_TRX
175 $this->getDB()->onTransactionIdle( function() {
176 Hooks
::run( 'LinksUpdateComplete', [ &$this ] );
181 * Acquire a lock for performing link table updates for a page on a DB
183 * @param IDatabase $dbw
184 * @param integer $pageId
185 * @param string $why One of (job, atomicity)
186 * @return ScopedCallback
187 * @throws RuntimeException
190 public static function acquirePageLock( IDatabase
$dbw, $pageId, $why = 'atomicity' ) {
191 $key = "LinksUpdate:$why:pageid:$pageId";
192 $scopedLock = $dbw->getScopedLockAndFlush( $key, __METHOD__
, 15 );
193 if ( !$scopedLock ) {
194 throw new RuntimeException( "Could not acquire lock '$key'." );
200 protected function doIncrementalUpdate() {
202 $existing = $this->getExistingLinks();
203 $this->linkDeletions
= $this->getLinkDeletions( $existing );
204 $this->linkInsertions
= $this->getLinkInsertions( $existing );
205 $this->incrTableUpdate( 'pagelinks', 'pl', $this->linkDeletions
, $this->linkInsertions
);
208 $existing = $this->getExistingImages();
209 $imageDeletes = $this->getImageDeletions( $existing );
210 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
211 $this->getImageInsertions( $existing ) );
213 # Invalidate all image description pages which had links added or removed
214 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existing );
215 $this->invalidateImageDescriptions( $imageUpdates );
218 $existing = $this->getExistingExternals();
219 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
220 $this->getExternalInsertions( $existing ) );
223 $existing = $this->getExistingInterlangs();
224 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
225 $this->getInterlangInsertions( $existing ) );
227 # Inline interwiki links
228 $existing = $this->getExistingInterwikis();
229 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
230 $this->getInterwikiInsertions( $existing ) );
233 $existing = $this->getExistingTemplates();
234 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
235 $this->getTemplateInsertions( $existing ) );
238 $existing = $this->getExistingCategories();
239 $categoryDeletes = $this->getCategoryDeletions( $existing );
240 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
241 $this->getCategoryInsertions( $existing ) );
243 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
244 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
245 $categoryUpdates = $categoryInserts +
$categoryDeletes;
246 $this->invalidateCategories( $categoryUpdates );
247 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
250 $existing = $this->getExistingProperties();
251 $this->propertyDeletions
= $this->getPropertyDeletions( $existing );
252 $this->incrTableUpdate( 'page_props', 'pp', $this->propertyDeletions
,
253 $this->getPropertyInsertions( $existing ) );
255 # Invalidate the necessary pages
256 $this->propertyInsertions
= array_diff_assoc( $this->mProperties
, $existing );
257 $changed = $this->propertyDeletions +
$this->propertyInsertions
;
258 $this->invalidateProperties( $changed );
260 # Refresh links of all pages including this page
261 # This will be in a separate transaction
262 if ( $this->mRecursive
) {
263 $this->queueRecursiveJobs();
266 # Update the links table freshness for this title
267 $this->updateLinksTimestamp();
271 * Queue recursive jobs for this page
273 * Which means do LinksUpdate on all pages that include the current page,
274 * using the job queue.
276 protected function queueRecursiveJobs() {
277 self
::queueRecursiveJobsForTable( $this->mTitle
, 'templatelinks' );
278 if ( $this->mTitle
->getNamespace() == NS_FILE
) {
279 // Process imagelinks in case the title is or was a redirect
280 self
::queueRecursiveJobsForTable( $this->mTitle
, 'imagelinks' );
283 $bc = $this->mTitle
->getBacklinkCache();
284 // Get jobs for cascade-protected backlinks for a high priority queue.
285 // If meta-templates change to using a new template, the new template
286 // should be implicitly protected as soon as possible, if applicable.
287 // These jobs duplicate a subset of the above ones, but can run sooner.
288 // Which ever runs first generally no-ops the other one.
290 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
291 $jobs[] = RefreshLinksJob
::newPrioritized( $title, [] );
293 JobQueueGroup
::singleton()->push( $jobs );
297 * Queue a RefreshLinks job for any table.
299 * @param Title $title Title to do job for
300 * @param string $table Table to use (e.g. 'templatelinks')
302 public static function queueRecursiveJobsForTable( Title
$title, $table ) {
303 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
304 $job = new RefreshLinksJob(
309 ] + Job
::newRootJobParams( // "overall" refresh links job info
310 "refreshlinks:{$table}:{$title->getPrefixedText()}"
314 JobQueueGroup
::singleton()->push( $job );
321 function invalidateCategories( $cats ) {
322 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_CATEGORY
, array_keys( $cats ) );
326 * Update all the appropriate counts in the category table.
327 * @param array $added Associative array of category name => sort key
328 * @param array $deleted Associative array of category name => sort key
330 function updateCategoryCounts( $added, $deleted ) {
331 $a = WikiPage
::factory( $this->mTitle
);
332 $a->updateCategoryCounts(
333 array_keys( $added ), array_keys( $deleted )
338 * @param array $images
340 function invalidateImageDescriptions( $images ) {
341 PurgeJobUtils
::invalidatePages( $this->getDB(), NS_FILE
, array_keys( $images ) );
345 * Update a table by doing a delete query then an insert query
346 * @param string $table Table name
347 * @param string $prefix Field name prefix
348 * @param array $deletions
349 * @param array $insertions Rows to insert
351 private function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
352 $services = MediaWikiServices
::getInstance();
353 $bSize = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
354 $factory = $services->getDBLoadBalancerFactory();
356 if ( $table === 'page_props' ) {
357 $fromField = 'pp_page';
359 $fromField = "{$prefix}_from";
362 $deleteWheres = []; // list of WHERE clause arrays for each DB delete() call
363 if ( $table === 'pagelinks' ||
$table === 'templatelinks' ||
$table === 'iwlinks' ) {
364 $baseKey = ( $table === 'iwlinks' ) ?
'iwl_prefix' : "{$prefix}_namespace";
367 $curDeletionBatch = [];
368 $deletionBatches = [];
369 foreach ( $deletions as $ns => $dbKeys ) {
370 foreach ( $dbKeys as $dbKey => $unused ) {
371 $curDeletionBatch[$ns][$dbKey] = 1;
372 if ( ++
$curBatchSize >= $bSize ) {
373 $deletionBatches[] = $curDeletionBatch;
374 $curDeletionBatch = [];
379 if ( $curDeletionBatch ) {
380 $deletionBatches[] = $curDeletionBatch;
383 foreach ( $deletionBatches as $deletionBatch ) {
385 $fromField => $this->mId
,
386 $this->getDB()->makeWhereFrom2d( $deletionBatch, $baseKey, "{$prefix}_title" )
390 if ( $table === 'langlinks' ) {
391 $toField = 'll_lang';
392 } elseif ( $table === 'page_props' ) {
393 $toField = 'pp_propname';
395 $toField = $prefix . '_to';
398 $deletionBatches = array_chunk( array_keys( $deletions ), $bSize );
399 foreach ( $deletionBatches as $deletionBatch ) {
400 $deleteWheres[] = [ $fromField => $this->mId
, $toField => $deletionBatch ];
404 foreach ( $deleteWheres as $deleteWhere ) {
405 $this->getDB()->delete( $table, $deleteWhere, __METHOD__
);
406 $factory->commitAndWaitForReplication(
407 __METHOD__
, $this->ticket
, [ 'wiki' => $this->getDB()->getWikiID() ]
411 $insertBatches = array_chunk( $insertions, $bSize );
412 foreach ( $insertBatches as $insertBatch ) {
413 $this->getDB()->insert( $table, $insertBatch, __METHOD__
, 'IGNORE' );
414 $factory->commitAndWaitForReplication(
415 __METHOD__
, $this->ticket
, [ 'wiki' => $this->getDB()->getWikiID() ]
419 if ( count( $insertions ) ) {
420 Hooks
::run( 'LinksUpdateAfterInsert', [ $this, $table, $insertions ] );
425 * Get an array of pagelinks insertions for passing to the DB
426 * Skips the titles specified by the 2-D array $existing
427 * @param array $existing
430 private function getLinkInsertions( $existing = [] ) {
432 foreach ( $this->mLinks
as $ns => $dbkeys ) {
433 $diffs = isset( $existing[$ns] )
434 ?
array_diff_key( $dbkeys, $existing[$ns] )
436 foreach ( $diffs as $dbk => $id ) {
438 'pl_from' => $this->mId
,
439 'pl_from_namespace' => $this->mTitle
->getNamespace(),
440 'pl_namespace' => $ns,
450 * Get an array of template insertions. Like getLinkInsertions()
451 * @param array $existing
454 private function getTemplateInsertions( $existing = [] ) {
456 foreach ( $this->mTemplates
as $ns => $dbkeys ) {
457 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
458 foreach ( $diffs as $dbk => $id ) {
460 'tl_from' => $this->mId
,
461 'tl_from_namespace' => $this->mTitle
->getNamespace(),
462 'tl_namespace' => $ns,
472 * Get an array of image insertions
473 * Skips the names specified in $existing
474 * @param array $existing
477 private function getImageInsertions( $existing = [] ) {
479 $diffs = array_diff_key( $this->mImages
, $existing );
480 foreach ( $diffs as $iname => $dummy ) {
482 'il_from' => $this->mId
,
483 'il_from_namespace' => $this->mTitle
->getNamespace(),
492 * Get an array of externallinks insertions. Skips the names specified in $existing
493 * @param array $existing
496 private function getExternalInsertions( $existing = [] ) {
498 $diffs = array_diff_key( $this->mExternals
, $existing );
499 foreach ( $diffs as $url => $dummy ) {
500 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
502 'el_id' => $this->getDB()->nextSequenceValue( 'externallinks_el_id_seq' ),
503 'el_from' => $this->mId
,
505 'el_index' => $index,
514 * Get an array of category insertions
516 * @param array $existing Mapping existing category names to sort keys. If both
517 * match a link in $this, the link will be omitted from the output
521 private function getCategoryInsertions( $existing = [] ) {
522 global $wgContLang, $wgCategoryCollation;
523 $diffs = array_diff_assoc( $this->mCategories
, $existing );
525 foreach ( $diffs as $name => $prefix ) {
526 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
527 $wgContLang->findVariantLink( $name, $nt, true );
529 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
531 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
537 # Treat custom sortkeys as a prefix, so that if multiple
538 # things are forced to sort as '*' or something, they'll
539 # sort properly in the category rather than in page_id
541 $sortkey = Collation
::singleton()->getSortKey(
542 $this->mTitle
->getCategorySortkey( $prefix ) );
545 'cl_from' => $this->mId
,
547 'cl_sortkey' => $sortkey,
548 'cl_timestamp' => $this->getDB()->timestamp(),
549 'cl_sortkey_prefix' => $prefix,
550 'cl_collation' => $wgCategoryCollation,
559 * Get an array of interlanguage link insertions
561 * @param array $existing Mapping existing language codes to titles
565 private function getInterlangInsertions( $existing = [] ) {
566 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
568 foreach ( $diffs as $lang => $title ) {
570 'll_from' => $this->mId
,
580 * Get an array of page property insertions
581 * @param array $existing
584 function getPropertyInsertions( $existing = [] ) {
585 $diffs = array_diff_assoc( $this->mProperties
, $existing );
588 foreach ( array_keys( $diffs ) as $name ) {
589 $arr[] = $this->getPagePropRowData( $name );
596 * Returns an associative array to be used for inserting a row into
597 * the page_props table. Besides the given property name, this will
598 * include the page id from $this->mId and any property value from
599 * $this->mProperties.
601 * The array returned will include the pp_sortkey field if this
602 * is present in the database (as indicated by $wgPagePropsHaveSortkey).
603 * The sortkey value is currently determined by getPropertySortKeyValue().
605 * @note this assumes that $this->mProperties[$prop] is defined.
607 * @param string $prop The name of the property.
611 private function getPagePropRowData( $prop ) {
612 global $wgPagePropsHaveSortkey;
614 $value = $this->mProperties
[$prop];
617 'pp_page' => $this->mId
,
618 'pp_propname' => $prop,
619 'pp_value' => $value,
622 if ( $wgPagePropsHaveSortkey ) {
623 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
630 * Determines the sort key for the given property value.
631 * This will return $value if it is a float or int,
632 * 1 or resp. 0 if it is a bool, and null otherwise.
634 * @note In the future, we may allow the sortkey to be specified explicitly
635 * in ParserOutput::setProperty.
637 * @param mixed $value
641 private function getPropertySortKeyValue( $value ) {
642 if ( is_int( $value ) ||
is_float( $value ) ||
is_bool( $value ) ) {
643 return floatval( $value );
650 * Get an array of interwiki insertions for passing to the DB
651 * Skips the titles specified by the 2-D array $existing
652 * @param array $existing
655 private function getInterwikiInsertions( $existing = [] ) {
657 foreach ( $this->mInterwikis
as $prefix => $dbkeys ) {
658 $diffs = isset( $existing[$prefix] )
659 ?
array_diff_key( $dbkeys, $existing[$prefix] )
662 foreach ( $diffs as $dbk => $id ) {
664 'iwl_from' => $this->mId
,
665 'iwl_prefix' => $prefix,
675 * Given an array of existing links, returns those links which are not in $this
676 * and thus should be deleted.
677 * @param array $existing
680 private function getLinkDeletions( $existing ) {
682 foreach ( $existing as $ns => $dbkeys ) {
683 if ( isset( $this->mLinks
[$ns] ) ) {
684 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
686 $del[$ns] = $existing[$ns];
694 * Given an array of existing templates, returns those templates which are not in $this
695 * and thus should be deleted.
696 * @param array $existing
699 private function getTemplateDeletions( $existing ) {
701 foreach ( $existing as $ns => $dbkeys ) {
702 if ( isset( $this->mTemplates
[$ns] ) ) {
703 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
705 $del[$ns] = $existing[$ns];
713 * Given an array of existing images, returns those images which are not in $this
714 * and thus should be deleted.
715 * @param array $existing
718 private function getImageDeletions( $existing ) {
719 return array_diff_key( $existing, $this->mImages
);
723 * Given an array of existing external links, returns those links which are not
724 * in $this and thus should be deleted.
725 * @param array $existing
728 private function getExternalDeletions( $existing ) {
729 return array_diff_key( $existing, $this->mExternals
);
733 * Given an array of existing categories, returns those categories which are not in $this
734 * and thus should be deleted.
735 * @param array $existing
738 private function getCategoryDeletions( $existing ) {
739 return array_diff_assoc( $existing, $this->mCategories
);
743 * Given an array of existing interlanguage links, returns those links which are not
744 * in $this and thus should be deleted.
745 * @param array $existing
748 private function getInterlangDeletions( $existing ) {
749 return array_diff_assoc( $existing, $this->mInterlangs
);
753 * Get array of properties which should be deleted.
754 * @param array $existing
757 function getPropertyDeletions( $existing ) {
758 return array_diff_assoc( $existing, $this->mProperties
);
762 * Given an array of existing interwiki links, returns those links which are not in $this
763 * and thus should be deleted.
764 * @param array $existing
767 private function getInterwikiDeletions( $existing ) {
769 foreach ( $existing as $prefix => $dbkeys ) {
770 if ( isset( $this->mInterwikis
[$prefix] ) ) {
771 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
773 $del[$prefix] = $existing[$prefix];
781 * Get an array of existing links, as a 2-D array
785 private function getExistingLinks() {
786 $res = $this->getDB()->select( 'pagelinks', [ 'pl_namespace', 'pl_title' ],
787 [ 'pl_from' => $this->mId
], __METHOD__
);
789 foreach ( $res as $row ) {
790 if ( !isset( $arr[$row->pl_namespace
] ) ) {
791 $arr[$row->pl_namespace
] = [];
793 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
800 * Get an array of existing templates, as a 2-D array
804 private function getExistingTemplates() {
805 $res = $this->getDB()->select( 'templatelinks', [ 'tl_namespace', 'tl_title' ],
806 [ 'tl_from' => $this->mId
], __METHOD__
);
808 foreach ( $res as $row ) {
809 if ( !isset( $arr[$row->tl_namespace
] ) ) {
810 $arr[$row->tl_namespace
] = [];
812 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
819 * Get an array of existing images, image names in the keys
823 private function getExistingImages() {
824 $res = $this->getDB()->select( 'imagelinks', [ 'il_to' ],
825 [ 'il_from' => $this->mId
], __METHOD__
);
827 foreach ( $res as $row ) {
828 $arr[$row->il_to
] = 1;
835 * Get an array of existing external links, URLs in the keys
839 private function getExistingExternals() {
840 $res = $this->getDB()->select( 'externallinks', [ 'el_to' ],
841 [ 'el_from' => $this->mId
], __METHOD__
);
843 foreach ( $res as $row ) {
844 $arr[$row->el_to
] = 1;
851 * Get an array of existing categories, with the name in the key and sort key in the value.
855 private function getExistingCategories() {
856 $res = $this->getDB()->select( 'categorylinks', [ 'cl_to', 'cl_sortkey_prefix' ],
857 [ 'cl_from' => $this->mId
], __METHOD__
);
859 foreach ( $res as $row ) {
860 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
867 * Get an array of existing interlanguage links, with the language code in the key and the
868 * title in the value.
872 private function getExistingInterlangs() {
873 $res = $this->getDB()->select( 'langlinks', [ 'll_lang', 'll_title' ],
874 [ 'll_from' => $this->mId
], __METHOD__
);
876 foreach ( $res as $row ) {
877 $arr[$row->ll_lang
] = $row->ll_title
;
884 * Get an array of existing inline interwiki links, as a 2-D array
885 * @return array (prefix => array(dbkey => 1))
887 private function getExistingInterwikis() {
888 $res = $this->getDB()->select( 'iwlinks', [ 'iwl_prefix', 'iwl_title' ],
889 [ 'iwl_from' => $this->mId
], __METHOD__
);
891 foreach ( $res as $row ) {
892 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
893 $arr[$row->iwl_prefix
] = [];
895 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
902 * Get an array of existing categories, with the name in the key and sort key in the value.
904 * @return array Array of property names and values
906 private function getExistingProperties() {
907 $res = $this->getDB()->select( 'page_props', [ 'pp_propname', 'pp_value' ],
908 [ 'pp_page' => $this->mId
], __METHOD__
);
910 foreach ( $res as $row ) {
911 $arr[$row->pp_propname
] = $row->pp_value
;
918 * Return the title object of the page being updated
921 public function getTitle() {
922 return $this->mTitle
;
926 * Returns parser output
928 * @return ParserOutput
930 public function getParserOutput() {
931 return $this->mParserOutput
;
935 * Return the list of images used as generated by the parser
938 public function getImages() {
939 return $this->mImages
;
943 * Set the revision corresponding to this LinksUpdate
947 * @param Revision $revision
949 public function setRevision( Revision
$revision ) {
950 $this->mRevision
= $revision;
955 * @return null|Revision
957 public function getRevision() {
958 return $this->mRevision
;
962 * Set the User who triggered this LinksUpdate
967 public function setTriggeringUser( User
$user ) {
975 public function getTriggeringUser() {
980 * Invalidate any necessary link lists related to page property changes
981 * @param array $changed
983 private function invalidateProperties( $changed ) {
984 global $wgPagePropLinkInvalidations;
986 foreach ( $changed as $name => $value ) {
987 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
988 $inv = $wgPagePropLinkInvalidations[$name];
989 if ( !is_array( $inv ) ) {
992 foreach ( $inv as $table ) {
993 DeferredUpdates
::addUpdate( new HTMLCacheUpdate( $this->mTitle
, $table ) );
1000 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
1002 * @return null|array Array of Titles
1004 public function getAddedLinks() {
1005 if ( $this->linkInsertions
=== null ) {
1009 foreach ( $this->linkInsertions
as $insertion ) {
1010 $result[] = Title
::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
1017 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
1019 * @return null|array Array of Titles
1021 public function getRemovedLinks() {
1022 if ( $this->linkDeletions
=== null ) {
1026 foreach ( $this->linkDeletions
as $ns => $titles ) {
1027 foreach ( $titles as $title => $unused ) {
1028 $result[] = Title
::makeTitle( $ns, $title );
1036 * Fetch page properties added by this LinksUpdate.
1037 * Only available after the update is complete.
1039 * @return null|array
1041 public function getAddedProperties() {
1042 return $this->propertyInsertions
;
1046 * Fetch page properties removed by this LinksUpdate.
1047 * Only available after the update is complete.
1049 * @return null|array
1051 public function getRemovedProperties() {
1052 return $this->propertyDeletions
;
1056 * Update links table freshness
1058 private function updateLinksTimestamp() {
1060 // The link updates made here only reflect the freshness of the parser output
1061 $timestamp = $this->mParserOutput
->getCacheTime();
1062 $this->getDB()->update( 'page',
1063 [ 'page_links_updated' => $this->getDB()->timestamp( $timestamp ) ],
1064 [ 'page_id' => $this->mId
],
1073 private function getDB() {
1075 $this->db
= wfGetDB( DB_MASTER
);
1081 public function getAsJobSpecification() {
1082 if ( $this->user
) {
1084 'userId' => $this->user
->getId(),
1085 'userName' => $this->user
->getName(),
1091 if ( $this->mRevision
) {
1092 $triggeringRevisionId = $this->mRevision
->getId();
1094 $triggeringRevisionId = false;
1098 'wiki' => $this->getDB()->getWikiID(),
1099 'job' => new JobSpecification(
1100 'refreshLinksPrioritized',
1102 // Reuse the parser cache if it was saved
1103 'rootJobTimestamp' => $this->mParserOutput
->getCacheTime(),
1104 'useRecursiveLinksUpdate' => $this->mRecursive
,
1105 'triggeringUser' => $userInfo,
1106 'triggeringRevisionId' => $triggeringRevisionId,
1108 [ 'removeDuplicates' => true ],