Merge "ActiveUsersPager: Fix ordering and return 0-action users"
[lhc/web/wiklou.git] / includes / deferred / LinksUpdate.php
1 <?php
2 /**
3 * Updater for link tracking tables after a page edit.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use Wikimedia\Rdbms\IDatabase;
24 use MediaWiki\Logger\LoggerFactory;
25 use MediaWiki\MediaWikiServices;
26 use Wikimedia\ScopedCallback;
27
28 /**
29 * Class the manages updates of *_link tables as well as similar extension-managed tables
30 *
31 * @note: LinksUpdate is managed by DeferredUpdates::execute(). Do not run this in a transaction.
32 *
33 * See docs/deferred.txt
34 */
35 class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate {
36 // @todo make members protected, but make sure extensions don't break
37
38 /** @var int Page ID of the article linked from */
39 public $mId;
40
41 /** @var Title Title object of the article linked from */
42 public $mTitle;
43
44 /** @var ParserOutput */
45 public $mParserOutput;
46
47 /** @var array Map of title strings to IDs for the links in the document */
48 public $mLinks;
49
50 /** @var array DB keys of the images used, in the array key only */
51 public $mImages;
52
53 /** @var array Map of title strings to IDs for the template references, including broken ones */
54 public $mTemplates;
55
56 /** @var array URLs of external links, array key only */
57 public $mExternals;
58
59 /** @var array Map of category names to sort keys */
60 public $mCategories;
61
62 /** @var array Map of language codes to titles */
63 public $mInterlangs;
64
65 /** @var array 2-D map of (prefix => DBK => 1) */
66 public $mInterwikis;
67
68 /** @var array Map of arbitrary name to value */
69 public $mProperties;
70
71 /** @var bool Whether to queue jobs for recursive updates */
72 public $mRecursive;
73
74 /** @var Revision Revision for which this update has been triggered */
75 private $mRevision;
76
77 /**
78 * @var null|array Added links if calculated.
79 */
80 private $linkInsertions = null;
81
82 /**
83 * @var null|array Deleted links if calculated.
84 */
85 private $linkDeletions = null;
86
87 /**
88 * @var null|array Added external links if calculated.
89 */
90 private $externalLinkInsertions = null;
91
92 /**
93 * @var null|array Deleted external links if calculated.
94 */
95 private $externalLinkDeletions = null;
96
97 /**
98 * @var null|array Added properties if calculated.
99 */
100 private $propertyInsertions = null;
101
102 /**
103 * @var null|array Deleted properties if calculated.
104 */
105 private $propertyDeletions = null;
106
107 /**
108 * @var User|null
109 */
110 private $user;
111
112 /** @var IDatabase */
113 private $db;
114
115 /**
116 * @param Title $title Title of the page we're updating
117 * @param ParserOutput $parserOutput Output from a full parse of this page
118 * @param bool $recursive Queue jobs for recursive updates?
119 * @throws MWException
120 */
121 function __construct( Title $title, ParserOutput $parserOutput, $recursive = true ) {
122 parent::__construct();
123
124 $this->mTitle = $title;
125 $this->mId = $title->getArticleID( Title::GAID_FOR_UPDATE );
126
127 if ( !$this->mId ) {
128 throw new InvalidArgumentException(
129 "The Title object yields no ID. Perhaps the page doesn't exist?"
130 );
131 }
132
133 $this->mParserOutput = $parserOutput;
134
135 $this->mLinks = $parserOutput->getLinks();
136 $this->mImages = $parserOutput->getImages();
137 $this->mTemplates = $parserOutput->getTemplates();
138 $this->mExternals = $parserOutput->getExternalLinks();
139 $this->mCategories = $parserOutput->getCategories();
140 $this->mProperties = $parserOutput->getProperties();
141 $this->mInterwikis = $parserOutput->getInterwikiLinks();
142
143 # Convert the format of the interlanguage links
144 # I didn't want to change it in the ParserOutput, because that array is passed all
145 # the way back to the skin, so either a skin API break would be required, or an
146 # inefficient back-conversion.
147 $ill = $parserOutput->getLanguageLinks();
148 $this->mInterlangs = [];
149 foreach ( $ill as $link ) {
150 list( $key, $title ) = explode( ':', $link, 2 );
151 $this->mInterlangs[$key] = $title;
152 }
153
154 foreach ( $this->mCategories as &$sortkey ) {
155 # If the sortkey is longer then 255 bytes, it is truncated by DB, and then doesn't match
156 # when comparing existing vs current categories, causing T27254.
157 $sortkey = mb_strcut( $sortkey, 0, 255 );
158 }
159
160 $this->mRecursive = $recursive;
161
162 // Avoid PHP 7.1 warning from passing $this by reference
163 $linksUpdate = $this;
164 Hooks::run( 'LinksUpdateConstructed', [ &$linksUpdate ] );
165 }
166
167 /**
168 * Update link tables with outgoing links from an updated article
169 *
170 * @note this is managed by DeferredUpdates::execute(). Do not run this in a transaction.
171 */
172 public function doUpdate() {
173 if ( $this->ticket ) {
174 // Make sure all links update threads see the changes of each other.
175 // This handles the case when updates have to batched into several COMMITs.
176 $scopedLock = self::acquirePageLock( $this->getDB(), $this->mId );
177 if ( !$scopedLock ) {
178 throw new RuntimeException( "Could not acquire lock for page ID '{$this->mId}'." );
179 }
180 }
181
182 // Avoid PHP 7.1 warning from passing $this by reference
183 $linksUpdate = $this;
184 Hooks::run( 'LinksUpdate', [ &$linksUpdate ] );
185 $this->doIncrementalUpdate();
186
187 // Commit and release the lock (if set)
188 ScopedCallback::consume( $scopedLock );
189 // Run post-commit hook handlers without DBO_TRX
190 DeferredUpdates::addUpdate( new AutoCommitUpdate(
191 $this->getDB(),
192 __METHOD__,
193 function () {
194 // Avoid PHP 7.1 warning from passing $this by reference
195 $linksUpdate = $this;
196 Hooks::run( 'LinksUpdateComplete', [ &$linksUpdate, $this->ticket ] );
197 }
198 ) );
199 }
200
201 /**
202 * Acquire a lock for performing link table updates for a page on a DB
203 *
204 * @param IDatabase $dbw
205 * @param int $pageId
206 * @param string $why One of (job, atomicity)
207 * @return ScopedCallback|null
208 * @since 1.27
209 */
210 public static function acquirePageLock( IDatabase $dbw, $pageId, $why = 'atomicity' ) {
211 $key = "LinksUpdate:$why:pageid:$pageId";
212 $scopedLock = $dbw->getScopedLockAndFlush( $key, __METHOD__, 15 );
213 if ( !$scopedLock ) {
214 $logger = LoggerFactory::getInstance( 'SecondaryDataUpdate' );
215 $logger->info( "Could not acquire lock '{key}' for page ID '{page_id}'.", [
216 'key' => $key,
217 'page_id' => $pageId,
218 ] );
219 return null;
220 }
221
222 return $scopedLock;
223 }
224
225 protected function doIncrementalUpdate() {
226 # Page links
227 $existingPL = $this->getExistingLinks();
228 $this->linkDeletions = $this->getLinkDeletions( $existingPL );
229 $this->linkInsertions = $this->getLinkInsertions( $existingPL );
230 $this->incrTableUpdate( 'pagelinks', 'pl', $this->linkDeletions, $this->linkInsertions );
231
232 # Image links
233 $existingIL = $this->getExistingImages();
234 $imageDeletes = $this->getImageDeletions( $existingIL );
235 $this->incrTableUpdate(
236 'imagelinks',
237 'il',
238 $imageDeletes,
239 $this->getImageInsertions( $existingIL ) );
240
241 # Invalidate all image description pages which had links added or removed
242 $imageUpdates = $imageDeletes + array_diff_key( $this->mImages, $existingIL );
243 $this->invalidateImageDescriptions( $imageUpdates );
244
245 # External links
246 $existingEL = $this->getExistingExternals();
247 $this->externalLinkDeletions = $this->getExternalDeletions( $existingEL );
248 $this->externalLinkInsertions = $this->getExternalInsertions(
249 $existingEL );
250 $this->incrTableUpdate(
251 'externallinks',
252 'el',
253 $this->externalLinkDeletions,
254 $this->externalLinkInsertions );
255
256 # Language links
257 $existingLL = $this->getExistingInterlangs();
258 $this->incrTableUpdate(
259 'langlinks',
260 'll',
261 $this->getInterlangDeletions( $existingLL ),
262 $this->getInterlangInsertions( $existingLL ) );
263
264 # Inline interwiki links
265 $existingIW = $this->getExistingInterwikis();
266 $this->incrTableUpdate(
267 'iwlinks',
268 'iwl',
269 $this->getInterwikiDeletions( $existingIW ),
270 $this->getInterwikiInsertions( $existingIW ) );
271
272 # Template links
273 $existingTL = $this->getExistingTemplates();
274 $this->incrTableUpdate(
275 'templatelinks',
276 'tl',
277 $this->getTemplateDeletions( $existingTL ),
278 $this->getTemplateInsertions( $existingTL ) );
279
280 # Category links
281 $existingCL = $this->getExistingCategories();
282 $categoryDeletes = $this->getCategoryDeletions( $existingCL );
283 $this->incrTableUpdate(
284 'categorylinks',
285 'cl',
286 $categoryDeletes,
287 $this->getCategoryInsertions( $existingCL ) );
288 $categoryInserts = array_diff_assoc( $this->mCategories, $existingCL );
289 $categoryUpdates = $categoryInserts + $categoryDeletes;
290
291 # Page properties
292 $existingPP = $this->getExistingProperties();
293 $this->propertyDeletions = $this->getPropertyDeletions( $existingPP );
294 $this->incrTableUpdate(
295 'page_props',
296 'pp',
297 $this->propertyDeletions,
298 $this->getPropertyInsertions( $existingPP ) );
299
300 # Invalidate the necessary pages
301 $this->propertyInsertions = array_diff_assoc( $this->mProperties, $existingPP );
302 $changed = $this->propertyDeletions + $this->propertyInsertions;
303 $this->invalidateProperties( $changed );
304
305 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
306 $this->invalidateCategories( $categoryUpdates );
307 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
308
309 # Refresh links of all pages including this page
310 # This will be in a separate transaction
311 if ( $this->mRecursive ) {
312 $this->queueRecursiveJobs();
313 }
314
315 # Update the links table freshness for this title
316 $this->updateLinksTimestamp();
317 }
318
319 /**
320 * Queue recursive jobs for this page
321 *
322 * Which means do LinksUpdate on all pages that include the current page,
323 * using the job queue.
324 */
325 protected function queueRecursiveJobs() {
326 $action = $this->getCauseAction();
327 $agent = $this->getCauseAgent();
328
329 self::queueRecursiveJobsForTable( $this->mTitle, 'templatelinks', $action, $agent );
330 if ( $this->mTitle->getNamespace() == NS_FILE ) {
331 // Process imagelinks in case the title is or was a redirect
332 self::queueRecursiveJobsForTable( $this->mTitle, 'imagelinks', $action, $agent );
333 }
334
335 $bc = $this->mTitle->getBacklinkCache();
336 // Get jobs for cascade-protected backlinks for a high priority queue.
337 // If meta-templates change to using a new template, the new template
338 // should be implicitly protected as soon as possible, if applicable.
339 // These jobs duplicate a subset of the above ones, but can run sooner.
340 // Which ever runs first generally no-ops the other one.
341 $jobs = [];
342 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
343 $jobs[] = RefreshLinksJob::newPrioritized(
344 $title,
345 [
346 'causeAction' => $action,
347 'causeAgent' => $agent
348 ]
349 );
350 }
351 JobQueueGroup::singleton()->push( $jobs );
352 }
353
354 /**
355 * Queue a RefreshLinks job for any table.
356 *
357 * @param Title $title Title to do job for
358 * @param string $table Table to use (e.g. 'templatelinks')
359 * @param string $action Triggering action
360 * @param string $userName Triggering user name
361 */
362 public static function queueRecursiveJobsForTable(
363 Title $title, $table, $action = 'unknown', $userName = 'unknown'
364 ) {
365 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
366 $job = new RefreshLinksJob(
367 $title,
368 [
369 'table' => $table,
370 'recursive' => true,
371 ] + Job::newRootJobParams( // "overall" refresh links job info
372 "refreshlinks:{$table}:{$title->getPrefixedText()}"
373 ) + [ 'causeAction' => $action, 'causeAgent' => $userName ]
374 );
375
376 JobQueueGroup::singleton()->push( $job );
377 }
378 }
379
380 /**
381 * @param array $cats
382 */
383 private function invalidateCategories( $cats ) {
384 PurgeJobUtils::invalidatePages( $this->getDB(), NS_CATEGORY, array_keys( $cats ) );
385 }
386
387 /**
388 * Update all the appropriate counts in the category table.
389 * @param array $added Associative array of category name => sort key
390 * @param array $deleted Associative array of category name => sort key
391 */
392 private function updateCategoryCounts( array $added, array $deleted ) {
393 global $wgUpdateRowsPerQuery;
394
395 if ( !$added && !$deleted ) {
396 return;
397 }
398
399 $domainId = $this->getDB()->getDomainID();
400 $wp = WikiPage::factory( $this->mTitle );
401 $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
402 // T163801: try to release any row locks to reduce contention
403 $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [ 'domain' => $domainId ] );
404
405 foreach ( array_chunk( array_keys( $added ), $wgUpdateRowsPerQuery ) as $addBatch ) {
406 $wp->updateCategoryCounts( $addBatch, [], $this->mId );
407 $lbf->commitAndWaitForReplication(
408 __METHOD__, $this->ticket, [ 'domain' => $domainId ] );
409 }
410
411 foreach ( array_chunk( array_keys( $deleted ), $wgUpdateRowsPerQuery ) as $deleteBatch ) {
412 $wp->updateCategoryCounts( [], $deleteBatch, $this->mId );
413 $lbf->commitAndWaitForReplication(
414 __METHOD__, $this->ticket, [ 'domain' => $domainId ] );
415 }
416 }
417
418 /**
419 * @param array $images
420 */
421 private function invalidateImageDescriptions( array $images ) {
422 PurgeJobUtils::invalidatePages( $this->getDB(), NS_FILE, array_keys( $images ) );
423 }
424
425 /**
426 * Update a table by doing a delete query then an insert query
427 * @param string $table Table name
428 * @param string $prefix Field name prefix
429 * @param array $deletions
430 * @param array $insertions Rows to insert
431 */
432 private function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
433 $services = MediaWikiServices::getInstance();
434 $bSize = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
435 $lbf = $services->getDBLoadBalancerFactory();
436
437 if ( $table === 'page_props' ) {
438 $fromField = 'pp_page';
439 } else {
440 $fromField = "{$prefix}_from";
441 }
442
443 $deleteWheres = []; // list of WHERE clause arrays for each DB delete() call
444 if ( $table === 'pagelinks' || $table === 'templatelinks' || $table === 'iwlinks' ) {
445 $baseKey = ( $table === 'iwlinks' ) ? 'iwl_prefix' : "{$prefix}_namespace";
446
447 $curBatchSize = 0;
448 $curDeletionBatch = [];
449 $deletionBatches = [];
450 foreach ( $deletions as $ns => $dbKeys ) {
451 foreach ( $dbKeys as $dbKey => $unused ) {
452 $curDeletionBatch[$ns][$dbKey] = 1;
453 if ( ++$curBatchSize >= $bSize ) {
454 $deletionBatches[] = $curDeletionBatch;
455 $curDeletionBatch = [];
456 $curBatchSize = 0;
457 }
458 }
459 }
460 if ( $curDeletionBatch ) {
461 $deletionBatches[] = $curDeletionBatch;
462 }
463
464 foreach ( $deletionBatches as $deletionBatch ) {
465 $deleteWheres[] = [
466 $fromField => $this->mId,
467 $this->getDB()->makeWhereFrom2d( $deletionBatch, $baseKey, "{$prefix}_title" )
468 ];
469 }
470 } else {
471 if ( $table === 'langlinks' ) {
472 $toField = 'll_lang';
473 } elseif ( $table === 'page_props' ) {
474 $toField = 'pp_propname';
475 } else {
476 $toField = $prefix . '_to';
477 }
478
479 $deletionBatches = array_chunk( array_keys( $deletions ), $bSize );
480 foreach ( $deletionBatches as $deletionBatch ) {
481 $deleteWheres[] = [ $fromField => $this->mId, $toField => $deletionBatch ];
482 }
483 }
484
485 $domainId = $this->getDB()->getDomainID();
486
487 foreach ( $deleteWheres as $deleteWhere ) {
488 $this->getDB()->delete( $table, $deleteWhere, __METHOD__ );
489 $lbf->commitAndWaitForReplication(
490 __METHOD__, $this->ticket, [ 'domain' => $domainId ]
491 );
492 }
493
494 $insertBatches = array_chunk( $insertions, $bSize );
495 foreach ( $insertBatches as $insertBatch ) {
496 $this->getDB()->insert( $table, $insertBatch, __METHOD__, 'IGNORE' );
497 $lbf->commitAndWaitForReplication(
498 __METHOD__, $this->ticket, [ 'domain' => $domainId ]
499 );
500 }
501
502 if ( count( $insertions ) ) {
503 Hooks::run( 'LinksUpdateAfterInsert', [ $this, $table, $insertions ] );
504 }
505 }
506
507 /**
508 * Get an array of pagelinks insertions for passing to the DB
509 * Skips the titles specified by the 2-D array $existing
510 * @param array $existing
511 * @return array
512 */
513 private function getLinkInsertions( $existing = [] ) {
514 $arr = [];
515 foreach ( $this->mLinks as $ns => $dbkeys ) {
516 $diffs = isset( $existing[$ns] )
517 ? array_diff_key( $dbkeys, $existing[$ns] )
518 : $dbkeys;
519 foreach ( $diffs as $dbk => $id ) {
520 $arr[] = [
521 'pl_from' => $this->mId,
522 'pl_from_namespace' => $this->mTitle->getNamespace(),
523 'pl_namespace' => $ns,
524 'pl_title' => $dbk
525 ];
526 }
527 }
528
529 return $arr;
530 }
531
532 /**
533 * Get an array of template insertions. Like getLinkInsertions()
534 * @param array $existing
535 * @return array
536 */
537 private function getTemplateInsertions( $existing = [] ) {
538 $arr = [];
539 foreach ( $this->mTemplates as $ns => $dbkeys ) {
540 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
541 foreach ( $diffs as $dbk => $id ) {
542 $arr[] = [
543 'tl_from' => $this->mId,
544 'tl_from_namespace' => $this->mTitle->getNamespace(),
545 'tl_namespace' => $ns,
546 'tl_title' => $dbk
547 ];
548 }
549 }
550
551 return $arr;
552 }
553
554 /**
555 * Get an array of image insertions
556 * Skips the names specified in $existing
557 * @param array $existing
558 * @return array
559 */
560 private function getImageInsertions( $existing = [] ) {
561 $arr = [];
562 $diffs = array_diff_key( $this->mImages, $existing );
563 foreach ( $diffs as $iname => $dummy ) {
564 $arr[] = [
565 'il_from' => $this->mId,
566 'il_from_namespace' => $this->mTitle->getNamespace(),
567 'il_to' => $iname
568 ];
569 }
570
571 return $arr;
572 }
573
574 /**
575 * Get an array of externallinks insertions. Skips the names specified in $existing
576 * @param array $existing
577 * @return array
578 */
579 private function getExternalInsertions( $existing = [] ) {
580 $arr = [];
581 $diffs = array_diff_key( $this->mExternals, $existing );
582 foreach ( $diffs as $url => $dummy ) {
583 foreach ( LinkFilter::makeIndexes( $url ) as $index ) {
584 $arr[] = [
585 'el_from' => $this->mId,
586 'el_to' => $url,
587 'el_index' => $index,
588 'el_index_60' => substr( $index, 0, 60 ),
589 ];
590 }
591 }
592
593 return $arr;
594 }
595
596 /**
597 * Get an array of category insertions
598 *
599 * @param array $existing Mapping existing category names to sort keys. If both
600 * match a link in $this, the link will be omitted from the output
601 *
602 * @return array
603 */
604 private function getCategoryInsertions( $existing = [] ) {
605 global $wgCategoryCollation;
606 $diffs = array_diff_assoc( $this->mCategories, $existing );
607 $arr = [];
608 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
609 $collation = Collation::singleton();
610 foreach ( $diffs as $name => $prefix ) {
611 $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
612 $contLang->findVariantLink( $name, $nt, true );
613
614 $type = MWNamespace::getCategoryLinkType( $this->mTitle->getNamespace() );
615
616 # Treat custom sortkeys as a prefix, so that if multiple
617 # things are forced to sort as '*' or something, they'll
618 # sort properly in the category rather than in page_id
619 # order or such.
620 $sortkey = $collation->getSortKey( $this->mTitle->getCategorySortkey( $prefix ) );
621
622 $arr[] = [
623 'cl_from' => $this->mId,
624 'cl_to' => $name,
625 'cl_sortkey' => $sortkey,
626 'cl_timestamp' => $this->getDB()->timestamp(),
627 'cl_sortkey_prefix' => $prefix,
628 'cl_collation' => $wgCategoryCollation,
629 'cl_type' => $type,
630 ];
631 }
632
633 return $arr;
634 }
635
636 /**
637 * Get an array of interlanguage link insertions
638 *
639 * @param array $existing Mapping existing language codes to titles
640 *
641 * @return array
642 */
643 private function getInterlangInsertions( $existing = [] ) {
644 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
645 $arr = [];
646 foreach ( $diffs as $lang => $title ) {
647 $arr[] = [
648 'll_from' => $this->mId,
649 'll_lang' => $lang,
650 'll_title' => $title
651 ];
652 }
653
654 return $arr;
655 }
656
657 /**
658 * Get an array of page property insertions
659 * @param array $existing
660 * @return array
661 */
662 function getPropertyInsertions( $existing = [] ) {
663 $diffs = array_diff_assoc( $this->mProperties, $existing );
664
665 $arr = [];
666 foreach ( array_keys( $diffs ) as $name ) {
667 $arr[] = $this->getPagePropRowData( $name );
668 }
669
670 return $arr;
671 }
672
673 /**
674 * Returns an associative array to be used for inserting a row into
675 * the page_props table. Besides the given property name, this will
676 * include the page id from $this->mId and any property value from
677 * $this->mProperties.
678 *
679 * The array returned will include the pp_sortkey field if this
680 * is present in the database (as indicated by $wgPagePropsHaveSortkey).
681 * The sortkey value is currently determined by getPropertySortKeyValue().
682 *
683 * @note this assumes that $this->mProperties[$prop] is defined.
684 *
685 * @param string $prop The name of the property.
686 *
687 * @return array
688 */
689 private function getPagePropRowData( $prop ) {
690 global $wgPagePropsHaveSortkey;
691
692 $value = $this->mProperties[$prop];
693
694 $row = [
695 'pp_page' => $this->mId,
696 'pp_propname' => $prop,
697 'pp_value' => $value,
698 ];
699
700 if ( $wgPagePropsHaveSortkey ) {
701 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
702 }
703
704 return $row;
705 }
706
707 /**
708 * Determines the sort key for the given property value.
709 * This will return $value if it is a float or int,
710 * 1 or resp. 0 if it is a bool, and null otherwise.
711 *
712 * @note In the future, we may allow the sortkey to be specified explicitly
713 * in ParserOutput::setProperty.
714 *
715 * @param mixed $value
716 *
717 * @return float|null
718 */
719 private function getPropertySortKeyValue( $value ) {
720 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
721 return floatval( $value );
722 }
723
724 return null;
725 }
726
727 /**
728 * Get an array of interwiki insertions for passing to the DB
729 * Skips the titles specified by the 2-D array $existing
730 * @param array $existing
731 * @return array
732 */
733 private function getInterwikiInsertions( $existing = [] ) {
734 $arr = [];
735 foreach ( $this->mInterwikis as $prefix => $dbkeys ) {
736 $diffs = isset( $existing[$prefix] )
737 ? array_diff_key( $dbkeys, $existing[$prefix] )
738 : $dbkeys;
739
740 foreach ( $diffs as $dbk => $id ) {
741 $arr[] = [
742 'iwl_from' => $this->mId,
743 'iwl_prefix' => $prefix,
744 'iwl_title' => $dbk
745 ];
746 }
747 }
748
749 return $arr;
750 }
751
752 /**
753 * Given an array of existing links, returns those links which are not in $this
754 * and thus should be deleted.
755 * @param array $existing
756 * @return array
757 */
758 private function getLinkDeletions( $existing ) {
759 $del = [];
760 foreach ( $existing as $ns => $dbkeys ) {
761 if ( isset( $this->mLinks[$ns] ) ) {
762 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
763 } else {
764 $del[$ns] = $existing[$ns];
765 }
766 }
767
768 return $del;
769 }
770
771 /**
772 * Given an array of existing templates, returns those templates which are not in $this
773 * and thus should be deleted.
774 * @param array $existing
775 * @return array
776 */
777 private function getTemplateDeletions( $existing ) {
778 $del = [];
779 foreach ( $existing as $ns => $dbkeys ) {
780 if ( isset( $this->mTemplates[$ns] ) ) {
781 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
782 } else {
783 $del[$ns] = $existing[$ns];
784 }
785 }
786
787 return $del;
788 }
789
790 /**
791 * Given an array of existing images, returns those images which are not in $this
792 * and thus should be deleted.
793 * @param array $existing
794 * @return array
795 */
796 private function getImageDeletions( $existing ) {
797 return array_diff_key( $existing, $this->mImages );
798 }
799
800 /**
801 * Given an array of existing external links, returns those links which are not
802 * in $this and thus should be deleted.
803 * @param array $existing
804 * @return array
805 */
806 private function getExternalDeletions( $existing ) {
807 return array_diff_key( $existing, $this->mExternals );
808 }
809
810 /**
811 * Given an array of existing categories, returns those categories which are not in $this
812 * and thus should be deleted.
813 * @param array $existing
814 * @return array
815 */
816 private function getCategoryDeletions( $existing ) {
817 return array_diff_assoc( $existing, $this->mCategories );
818 }
819
820 /**
821 * Given an array of existing interlanguage links, returns those links which are not
822 * in $this and thus should be deleted.
823 * @param array $existing
824 * @return array
825 */
826 private function getInterlangDeletions( $existing ) {
827 return array_diff_assoc( $existing, $this->mInterlangs );
828 }
829
830 /**
831 * Get array of properties which should be deleted.
832 * @param array $existing
833 * @return array
834 */
835 private function getPropertyDeletions( $existing ) {
836 return array_diff_assoc( $existing, $this->mProperties );
837 }
838
839 /**
840 * Given an array of existing interwiki links, returns those links which are not in $this
841 * and thus should be deleted.
842 * @param array $existing
843 * @return array
844 */
845 private function getInterwikiDeletions( $existing ) {
846 $del = [];
847 foreach ( $existing as $prefix => $dbkeys ) {
848 if ( isset( $this->mInterwikis[$prefix] ) ) {
849 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis[$prefix] );
850 } else {
851 $del[$prefix] = $existing[$prefix];
852 }
853 }
854
855 return $del;
856 }
857
858 /**
859 * Get an array of existing links, as a 2-D array
860 *
861 * @return array
862 */
863 private function getExistingLinks() {
864 $res = $this->getDB()->select( 'pagelinks', [ 'pl_namespace', 'pl_title' ],
865 [ 'pl_from' => $this->mId ], __METHOD__ );
866 $arr = [];
867 foreach ( $res as $row ) {
868 if ( !isset( $arr[$row->pl_namespace] ) ) {
869 $arr[$row->pl_namespace] = [];
870 }
871 $arr[$row->pl_namespace][$row->pl_title] = 1;
872 }
873
874 return $arr;
875 }
876
877 /**
878 * Get an array of existing templates, as a 2-D array
879 *
880 * @return array
881 */
882 private function getExistingTemplates() {
883 $res = $this->getDB()->select( 'templatelinks', [ 'tl_namespace', 'tl_title' ],
884 [ 'tl_from' => $this->mId ], __METHOD__ );
885 $arr = [];
886 foreach ( $res as $row ) {
887 if ( !isset( $arr[$row->tl_namespace] ) ) {
888 $arr[$row->tl_namespace] = [];
889 }
890 $arr[$row->tl_namespace][$row->tl_title] = 1;
891 }
892
893 return $arr;
894 }
895
896 /**
897 * Get an array of existing images, image names in the keys
898 *
899 * @return array
900 */
901 private function getExistingImages() {
902 $res = $this->getDB()->select( 'imagelinks', [ 'il_to' ],
903 [ 'il_from' => $this->mId ], __METHOD__ );
904 $arr = [];
905 foreach ( $res as $row ) {
906 $arr[$row->il_to] = 1;
907 }
908
909 return $arr;
910 }
911
912 /**
913 * Get an array of existing external links, URLs in the keys
914 *
915 * @return array
916 */
917 private function getExistingExternals() {
918 $res = $this->getDB()->select( 'externallinks', [ 'el_to' ],
919 [ 'el_from' => $this->mId ], __METHOD__ );
920 $arr = [];
921 foreach ( $res as $row ) {
922 $arr[$row->el_to] = 1;
923 }
924
925 return $arr;
926 }
927
928 /**
929 * Get an array of existing categories, with the name in the key and sort key in the value.
930 *
931 * @return array
932 */
933 private function getExistingCategories() {
934 $res = $this->getDB()->select( 'categorylinks', [ 'cl_to', 'cl_sortkey_prefix' ],
935 [ 'cl_from' => $this->mId ], __METHOD__ );
936 $arr = [];
937 foreach ( $res as $row ) {
938 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
939 }
940
941 return $arr;
942 }
943
944 /**
945 * Get an array of existing interlanguage links, with the language code in the key and the
946 * title in the value.
947 *
948 * @return array
949 */
950 private function getExistingInterlangs() {
951 $res = $this->getDB()->select( 'langlinks', [ 'll_lang', 'll_title' ],
952 [ 'll_from' => $this->mId ], __METHOD__ );
953 $arr = [];
954 foreach ( $res as $row ) {
955 $arr[$row->ll_lang] = $row->ll_title;
956 }
957
958 return $arr;
959 }
960
961 /**
962 * Get an array of existing inline interwiki links, as a 2-D array
963 * @return array (prefix => array(dbkey => 1))
964 */
965 private function getExistingInterwikis() {
966 $res = $this->getDB()->select( 'iwlinks', [ 'iwl_prefix', 'iwl_title' ],
967 [ 'iwl_from' => $this->mId ], __METHOD__ );
968 $arr = [];
969 foreach ( $res as $row ) {
970 if ( !isset( $arr[$row->iwl_prefix] ) ) {
971 $arr[$row->iwl_prefix] = [];
972 }
973 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
974 }
975
976 return $arr;
977 }
978
979 /**
980 * Get an array of existing categories, with the name in the key and sort key in the value.
981 *
982 * @return array Array of property names and values
983 */
984 private function getExistingProperties() {
985 $res = $this->getDB()->select( 'page_props', [ 'pp_propname', 'pp_value' ],
986 [ 'pp_page' => $this->mId ], __METHOD__ );
987 $arr = [];
988 foreach ( $res as $row ) {
989 $arr[$row->pp_propname] = $row->pp_value;
990 }
991
992 return $arr;
993 }
994
995 /**
996 * Return the title object of the page being updated
997 * @return Title
998 */
999 public function getTitle() {
1000 return $this->mTitle;
1001 }
1002
1003 /**
1004 * Returns parser output
1005 * @since 1.19
1006 * @return ParserOutput
1007 */
1008 public function getParserOutput() {
1009 return $this->mParserOutput;
1010 }
1011
1012 /**
1013 * Return the list of images used as generated by the parser
1014 * @return array
1015 */
1016 public function getImages() {
1017 return $this->mImages;
1018 }
1019
1020 /**
1021 * Set the revision corresponding to this LinksUpdate
1022 *
1023 * @since 1.27
1024 *
1025 * @param Revision $revision
1026 */
1027 public function setRevision( Revision $revision ) {
1028 $this->mRevision = $revision;
1029 }
1030
1031 /**
1032 * @since 1.28
1033 * @return null|Revision
1034 */
1035 public function getRevision() {
1036 return $this->mRevision;
1037 }
1038
1039 /**
1040 * Set the User who triggered this LinksUpdate
1041 *
1042 * @since 1.27
1043 * @param User $user
1044 */
1045 public function setTriggeringUser( User $user ) {
1046 $this->user = $user;
1047 }
1048
1049 /**
1050 * @since 1.27
1051 * @return null|User
1052 */
1053 public function getTriggeringUser() {
1054 return $this->user;
1055 }
1056
1057 /**
1058 * Invalidate any necessary link lists related to page property changes
1059 * @param array $changed
1060 */
1061 private function invalidateProperties( $changed ) {
1062 global $wgPagePropLinkInvalidations;
1063
1064 foreach ( $changed as $name => $value ) {
1065 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
1066 $inv = $wgPagePropLinkInvalidations[$name];
1067 if ( !is_array( $inv ) ) {
1068 $inv = [ $inv ];
1069 }
1070 foreach ( $inv as $table ) {
1071 DeferredUpdates::addUpdate(
1072 new HTMLCacheUpdate( $this->mTitle, $table, 'page-props' )
1073 );
1074 }
1075 }
1076 }
1077 }
1078
1079 /**
1080 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
1081 * @since 1.22
1082 * @return null|array Array of Titles
1083 */
1084 public function getAddedLinks() {
1085 if ( $this->linkInsertions === null ) {
1086 return null;
1087 }
1088 $result = [];
1089 foreach ( $this->linkInsertions as $insertion ) {
1090 $result[] = Title::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
1091 }
1092
1093 return $result;
1094 }
1095
1096 /**
1097 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
1098 * @since 1.22
1099 * @return null|array Array of Titles
1100 */
1101 public function getRemovedLinks() {
1102 if ( $this->linkDeletions === null ) {
1103 return null;
1104 }
1105 $result = [];
1106 foreach ( $this->linkDeletions as $ns => $titles ) {
1107 foreach ( $titles as $title => $unused ) {
1108 $result[] = Title::makeTitle( $ns, $title );
1109 }
1110 }
1111
1112 return $result;
1113 }
1114
1115 /**
1116 * Fetch external links added by this LinksUpdate. Only available after
1117 * the update is complete.
1118 * @since 1.33
1119 * @return null|array Array of Strings
1120 */
1121 public function getAddedExternalLinks() {
1122 if ( $this->externalLinkInsertions === null ) {
1123 return null;
1124 }
1125 $result = [];
1126 foreach ( $this->externalLinkInsertions as $key => $value ) {
1127 $result[] = $value['el_to'];
1128 }
1129 return $result;
1130 }
1131
1132 /**
1133 * Fetch external links removed by this LinksUpdate. Only available after
1134 * the update is complete.
1135 * @since 1.33
1136 * @return null|array Array of Strings
1137 */
1138 public function getRemovedExternalLinks() {
1139 if ( $this->externalLinkDeletions === null ) {
1140 return null;
1141 }
1142 return array_keys( $this->externalLinkDeletions );
1143 }
1144
1145 /**
1146 * Fetch page properties added by this LinksUpdate.
1147 * Only available after the update is complete.
1148 * @since 1.28
1149 * @return null|array
1150 */
1151 public function getAddedProperties() {
1152 return $this->propertyInsertions;
1153 }
1154
1155 /**
1156 * Fetch page properties removed by this LinksUpdate.
1157 * Only available after the update is complete.
1158 * @since 1.28
1159 * @return null|array
1160 */
1161 public function getRemovedProperties() {
1162 return $this->propertyDeletions;
1163 }
1164
1165 /**
1166 * Update links table freshness
1167 */
1168 private function updateLinksTimestamp() {
1169 if ( $this->mId ) {
1170 // The link updates made here only reflect the freshness of the parser output
1171 $timestamp = $this->mParserOutput->getCacheTime();
1172 $this->getDB()->update( 'page',
1173 [ 'page_links_updated' => $this->getDB()->timestamp( $timestamp ) ],
1174 [ 'page_id' => $this->mId ],
1175 __METHOD__
1176 );
1177 }
1178 }
1179
1180 /**
1181 * @return IDatabase
1182 */
1183 private function getDB() {
1184 if ( !$this->db ) {
1185 $this->db = wfGetDB( DB_MASTER );
1186 }
1187
1188 return $this->db;
1189 }
1190
1191 public function getAsJobSpecification() {
1192 if ( $this->user ) {
1193 $userInfo = [
1194 'userId' => $this->user->getId(),
1195 'userName' => $this->user->getName(),
1196 ];
1197 } else {
1198 $userInfo = false;
1199 }
1200
1201 if ( $this->mRevision ) {
1202 $triggeringRevisionId = $this->mRevision->getId();
1203 } else {
1204 $triggeringRevisionId = false;
1205 }
1206
1207 return [
1208 'wiki' => WikiMap::getWikiIdFromDbDomain( $this->getDB()->getDomainID() ),
1209 'job' => new JobSpecification(
1210 'refreshLinksPrioritized',
1211 [
1212 // Reuse the parser cache if it was saved
1213 'rootJobTimestamp' => $this->mParserOutput->getCacheTime(),
1214 'useRecursiveLinksUpdate' => $this->mRecursive,
1215 'triggeringUser' => $userInfo,
1216 'triggeringRevisionId' => $triggeringRevisionId,
1217 'causeAction' => $this->getCauseAction(),
1218 'causeAgent' => $this->getCauseAgent()
1219 ],
1220 [ 'removeDuplicates' => true ],
1221 $this->getTitle()
1222 )
1223 ];
1224 }
1225 }