Remove unused custom transaction logic from DataUpdate
[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 MediaWiki\MediaWikiServices;
24
25 /**
26 * Class the manages updates of *_link tables as well as similar extension-managed tables
27 *
28 * @note: LinksUpdate is managed by DeferredUpdates::execute(). Do not run this in a transaction.
29 *
30 * See docs/deferred.txt
31 */
32 class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate {
33 // @todo make members protected, but make sure extensions don't break
34
35 /** @var int Page ID of the article linked from */
36 public $mId;
37
38 /** @var Title Title object of the article linked from */
39 public $mTitle;
40
41 /** @var ParserOutput */
42 public $mParserOutput;
43
44 /** @var array Map of title strings to IDs for the links in the document */
45 public $mLinks;
46
47 /** @var array DB keys of the images used, in the array key only */
48 public $mImages;
49
50 /** @var array Map of title strings to IDs for the template references, including broken ones */
51 public $mTemplates;
52
53 /** @var array URLs of external links, array key only */
54 public $mExternals;
55
56 /** @var array Map of category names to sort keys */
57 public $mCategories;
58
59 /** @var array Map of language codes to titles */
60 public $mInterlangs;
61
62 /** @var array 2-D map of (prefix => DBK => 1) */
63 public $mInterwikis;
64
65 /** @var array Map of arbitrary name to value */
66 public $mProperties;
67
68 /** @var bool Whether to queue jobs for recursive updates */
69 public $mRecursive;
70
71 /** @var Revision Revision for which this update has been triggered */
72 private $mRevision;
73
74 /**
75 * @var null|array Added links if calculated.
76 */
77 private $linkInsertions = null;
78
79 /**
80 * @var null|array Deleted links if calculated.
81 */
82 private $linkDeletions = null;
83
84 /**
85 * @var null|array Added properties if calculated.
86 */
87 private $propertyInsertions = null;
88
89 /**
90 * @var null|array Deleted properties if calculated.
91 */
92 private $propertyDeletions = null;
93
94 /**
95 * @var User|null
96 */
97 private $user;
98
99 /** @var IDatabase */
100 private $db;
101
102 /**
103 * Constructor
104 *
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
109 */
110 function __construct( Title $title, ParserOutput $parserOutput, $recursive = true ) {
111 parent::__construct();
112
113 $this->mTitle = $title;
114 $this->mId = $title->getArticleID( Title::GAID_FOR_UPDATE );
115
116 if ( !$this->mId ) {
117 throw new InvalidArgumentException(
118 "The Title object yields no ID. Perhaps the page doesn't exist?"
119 );
120 }
121
122 $this->mParserOutput = $parserOutput;
123
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();
131
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;
141 }
142
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 );
151 }
152 }
153
154 $this->mRecursive = $recursive;
155
156 Hooks::run( 'LinksUpdateConstructed', [ &$this ] );
157 }
158
159 /**
160 * Update link tables with outgoing links from an updated article
161 *
162 * @note: this is managed by DeferredUpdates::execute(). Do not run this in a transaction.
163 */
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 );
168
169 Hooks::run( 'LinksUpdate', [ &$this ] );
170 $this->doIncrementalUpdate();
171
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 ] );
177 } );
178 }
179
180 /**
181 * Acquire a lock for performing link table updates for a page on a DB
182 *
183 * @param IDatabase $dbw
184 * @param integer $pageId
185 * @param string $why One of (job, atomicity)
186 * @return ScopedCallback
187 * @throws RuntimeException
188 * @since 1.27
189 */
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'." );
195 }
196
197 return $scopedLock;
198 }
199
200 protected function doIncrementalUpdate() {
201 # Page links
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 );
206
207 # Image links
208 $existing = $this->getExistingImages();
209 $imageDeletes = $this->getImageDeletions( $existing );
210 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
211 $this->getImageInsertions( $existing ) );
212
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 );
216
217 # External links
218 $existing = $this->getExistingExternals();
219 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
220 $this->getExternalInsertions( $existing ) );
221
222 # Language links
223 $existing = $this->getExistingInterlangs();
224 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
225 $this->getInterlangInsertions( $existing ) );
226
227 # Inline interwiki links
228 $existing = $this->getExistingInterwikis();
229 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
230 $this->getInterwikiInsertions( $existing ) );
231
232 # Template links
233 $existing = $this->getExistingTemplates();
234 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
235 $this->getTemplateInsertions( $existing ) );
236
237 # Category links
238 $existing = $this->getExistingCategories();
239 $categoryDeletes = $this->getCategoryDeletions( $existing );
240 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
241 $this->getCategoryInsertions( $existing ) );
242
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 );
248
249 # Page properties
250 $existing = $this->getExistingProperties();
251 $this->propertyDeletions = $this->getPropertyDeletions( $existing );
252 $this->incrTableUpdate( 'page_props', 'pp', $this->propertyDeletions,
253 $this->getPropertyInsertions( $existing ) );
254
255 # Invalidate the necessary pages
256 $this->propertyInsertions = array_diff_assoc( $this->mProperties, $existing );
257 $changed = $this->propertyDeletions + $this->propertyInsertions;
258 $this->invalidateProperties( $changed );
259
260 # Refresh links of all pages including this page
261 # This will be in a separate transaction
262 if ( $this->mRecursive ) {
263 $this->queueRecursiveJobs();
264 }
265
266 # Update the links table freshness for this title
267 $this->updateLinksTimestamp();
268 }
269
270 /**
271 * Queue recursive jobs for this page
272 *
273 * Which means do LinksUpdate on all pages that include the current page,
274 * using the job queue.
275 */
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' );
281 }
282
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.
289 $jobs = [];
290 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
291 $jobs[] = RefreshLinksJob::newPrioritized( $title, [] );
292 }
293 JobQueueGroup::singleton()->push( $jobs );
294 }
295
296 /**
297 * Queue a RefreshLinks job for any table.
298 *
299 * @param Title $title Title to do job for
300 * @param string $table Table to use (e.g. 'templatelinks')
301 */
302 public static function queueRecursiveJobsForTable( Title $title, $table ) {
303 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
304 $job = new RefreshLinksJob(
305 $title,
306 [
307 'table' => $table,
308 'recursive' => true,
309 ] + Job::newRootJobParams( // "overall" refresh links job info
310 "refreshlinks:{$table}:{$title->getPrefixedText()}"
311 )
312 );
313
314 JobQueueGroup::singleton()->push( $job );
315 }
316 }
317
318 /**
319 * @param array $cats
320 */
321 function invalidateCategories( $cats ) {
322 PurgeJobUtils::invalidatePages( $this->getDB(), NS_CATEGORY, array_keys( $cats ) );
323 }
324
325 /**
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
329 */
330 function updateCategoryCounts( $added, $deleted ) {
331 $a = WikiPage::factory( $this->mTitle );
332 $a->updateCategoryCounts(
333 array_keys( $added ), array_keys( $deleted )
334 );
335 }
336
337 /**
338 * @param array $images
339 */
340 function invalidateImageDescriptions( $images ) {
341 PurgeJobUtils::invalidatePages( $this->getDB(), NS_FILE, array_keys( $images ) );
342 }
343
344 /**
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
350 */
351 private function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
352 $services = MediaWikiServices::getInstance();
353 $bSize = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
354 $factory = $services->getDBLoadBalancerFactory();
355
356 if ( $table === 'page_props' ) {
357 $fromField = 'pp_page';
358 } else {
359 $fromField = "{$prefix}_from";
360 }
361
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";
365
366 $curBatchSize = 0;
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 = [];
375 $curBatchSize = 0;
376 }
377 }
378 }
379 if ( $curDeletionBatch ) {
380 $deletionBatches[] = $curDeletionBatch;
381 }
382
383 foreach ( $deletionBatches as $deletionBatch ) {
384 $deleteWheres[] = [
385 $fromField => $this->mId,
386 $this->getDB()->makeWhereFrom2d( $deletionBatch, $baseKey, "{$prefix}_title" )
387 ];
388 }
389 } else {
390 if ( $table === 'langlinks' ) {
391 $toField = 'll_lang';
392 } elseif ( $table === 'page_props' ) {
393 $toField = 'pp_propname';
394 } else {
395 $toField = $prefix . '_to';
396 }
397
398 $deletionBatches = array_chunk( array_keys( $deletions ), $bSize );
399 foreach ( $deletionBatches as $deletionBatch ) {
400 $deleteWheres[] = [ $fromField => $this->mId, $toField => $deletionBatch ];
401 }
402 }
403
404 foreach ( $deleteWheres as $deleteWhere ) {
405 $this->getDB()->delete( $table, $deleteWhere, __METHOD__ );
406 $factory->commitAndWaitForReplication(
407 __METHOD__, $this->ticket, [ 'wiki' => $this->getDB()->getWikiID() ]
408 );
409 }
410
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() ]
416 );
417 }
418
419 if ( count( $insertions ) ) {
420 Hooks::run( 'LinksUpdateAfterInsert', [ $this, $table, $insertions ] );
421 }
422 }
423
424 /**
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
428 * @return array
429 */
430 private function getLinkInsertions( $existing = [] ) {
431 $arr = [];
432 foreach ( $this->mLinks as $ns => $dbkeys ) {
433 $diffs = isset( $existing[$ns] )
434 ? array_diff_key( $dbkeys, $existing[$ns] )
435 : $dbkeys;
436 foreach ( $diffs as $dbk => $id ) {
437 $arr[] = [
438 'pl_from' => $this->mId,
439 'pl_from_namespace' => $this->mTitle->getNamespace(),
440 'pl_namespace' => $ns,
441 'pl_title' => $dbk
442 ];
443 }
444 }
445
446 return $arr;
447 }
448
449 /**
450 * Get an array of template insertions. Like getLinkInsertions()
451 * @param array $existing
452 * @return array
453 */
454 private function getTemplateInsertions( $existing = [] ) {
455 $arr = [];
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 ) {
459 $arr[] = [
460 'tl_from' => $this->mId,
461 'tl_from_namespace' => $this->mTitle->getNamespace(),
462 'tl_namespace' => $ns,
463 'tl_title' => $dbk
464 ];
465 }
466 }
467
468 return $arr;
469 }
470
471 /**
472 * Get an array of image insertions
473 * Skips the names specified in $existing
474 * @param array $existing
475 * @return array
476 */
477 private function getImageInsertions( $existing = [] ) {
478 $arr = [];
479 $diffs = array_diff_key( $this->mImages, $existing );
480 foreach ( $diffs as $iname => $dummy ) {
481 $arr[] = [
482 'il_from' => $this->mId,
483 'il_from_namespace' => $this->mTitle->getNamespace(),
484 'il_to' => $iname
485 ];
486 }
487
488 return $arr;
489 }
490
491 /**
492 * Get an array of externallinks insertions. Skips the names specified in $existing
493 * @param array $existing
494 * @return array
495 */
496 private function getExternalInsertions( $existing = [] ) {
497 $arr = [];
498 $diffs = array_diff_key( $this->mExternals, $existing );
499 foreach ( $diffs as $url => $dummy ) {
500 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
501 $arr[] = [
502 'el_id' => $this->getDB()->nextSequenceValue( 'externallinks_el_id_seq' ),
503 'el_from' => $this->mId,
504 'el_to' => $url,
505 'el_index' => $index,
506 ];
507 }
508 }
509
510 return $arr;
511 }
512
513 /**
514 * Get an array of category insertions
515 *
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
518 *
519 * @return array
520 */
521 private function getCategoryInsertions( $existing = [] ) {
522 global $wgContLang, $wgCategoryCollation;
523 $diffs = array_diff_assoc( $this->mCategories, $existing );
524 $arr = [];
525 foreach ( $diffs as $name => $prefix ) {
526 $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
527 $wgContLang->findVariantLink( $name, $nt, true );
528
529 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
530 $type = 'subcat';
531 } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
532 $type = 'file';
533 } else {
534 $type = 'page';
535 }
536
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
540 # order or such.
541 $sortkey = Collation::singleton()->getSortKey(
542 $this->mTitle->getCategorySortkey( $prefix ) );
543
544 $arr[] = [
545 'cl_from' => $this->mId,
546 'cl_to' => $name,
547 'cl_sortkey' => $sortkey,
548 'cl_timestamp' => $this->getDB()->timestamp(),
549 'cl_sortkey_prefix' => $prefix,
550 'cl_collation' => $wgCategoryCollation,
551 'cl_type' => $type,
552 ];
553 }
554
555 return $arr;
556 }
557
558 /**
559 * Get an array of interlanguage link insertions
560 *
561 * @param array $existing Mapping existing language codes to titles
562 *
563 * @return array
564 */
565 private function getInterlangInsertions( $existing = [] ) {
566 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
567 $arr = [];
568 foreach ( $diffs as $lang => $title ) {
569 $arr[] = [
570 'll_from' => $this->mId,
571 'll_lang' => $lang,
572 'll_title' => $title
573 ];
574 }
575
576 return $arr;
577 }
578
579 /**
580 * Get an array of page property insertions
581 * @param array $existing
582 * @return array
583 */
584 function getPropertyInsertions( $existing = [] ) {
585 $diffs = array_diff_assoc( $this->mProperties, $existing );
586
587 $arr = [];
588 foreach ( array_keys( $diffs ) as $name ) {
589 $arr[] = $this->getPagePropRowData( $name );
590 }
591
592 return $arr;
593 }
594
595 /**
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.
600 *
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().
604 *
605 * @note this assumes that $this->mProperties[$prop] is defined.
606 *
607 * @param string $prop The name of the property.
608 *
609 * @return array
610 */
611 private function getPagePropRowData( $prop ) {
612 global $wgPagePropsHaveSortkey;
613
614 $value = $this->mProperties[$prop];
615
616 $row = [
617 'pp_page' => $this->mId,
618 'pp_propname' => $prop,
619 'pp_value' => $value,
620 ];
621
622 if ( $wgPagePropsHaveSortkey ) {
623 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
624 }
625
626 return $row;
627 }
628
629 /**
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.
633 *
634 * @note In the future, we may allow the sortkey to be specified explicitly
635 * in ParserOutput::setProperty.
636 *
637 * @param mixed $value
638 *
639 * @return float|null
640 */
641 private function getPropertySortKeyValue( $value ) {
642 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
643 return floatval( $value );
644 }
645
646 return null;
647 }
648
649 /**
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
653 * @return array
654 */
655 private function getInterwikiInsertions( $existing = [] ) {
656 $arr = [];
657 foreach ( $this->mInterwikis as $prefix => $dbkeys ) {
658 $diffs = isset( $existing[$prefix] )
659 ? array_diff_key( $dbkeys, $existing[$prefix] )
660 : $dbkeys;
661
662 foreach ( $diffs as $dbk => $id ) {
663 $arr[] = [
664 'iwl_from' => $this->mId,
665 'iwl_prefix' => $prefix,
666 'iwl_title' => $dbk
667 ];
668 }
669 }
670
671 return $arr;
672 }
673
674 /**
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
678 * @return array
679 */
680 private function getLinkDeletions( $existing ) {
681 $del = [];
682 foreach ( $existing as $ns => $dbkeys ) {
683 if ( isset( $this->mLinks[$ns] ) ) {
684 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
685 } else {
686 $del[$ns] = $existing[$ns];
687 }
688 }
689
690 return $del;
691 }
692
693 /**
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
697 * @return array
698 */
699 private function getTemplateDeletions( $existing ) {
700 $del = [];
701 foreach ( $existing as $ns => $dbkeys ) {
702 if ( isset( $this->mTemplates[$ns] ) ) {
703 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
704 } else {
705 $del[$ns] = $existing[$ns];
706 }
707 }
708
709 return $del;
710 }
711
712 /**
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
716 * @return array
717 */
718 private function getImageDeletions( $existing ) {
719 return array_diff_key( $existing, $this->mImages );
720 }
721
722 /**
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
726 * @return array
727 */
728 private function getExternalDeletions( $existing ) {
729 return array_diff_key( $existing, $this->mExternals );
730 }
731
732 /**
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
736 * @return array
737 */
738 private function getCategoryDeletions( $existing ) {
739 return array_diff_assoc( $existing, $this->mCategories );
740 }
741
742 /**
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
746 * @return array
747 */
748 private function getInterlangDeletions( $existing ) {
749 return array_diff_assoc( $existing, $this->mInterlangs );
750 }
751
752 /**
753 * Get array of properties which should be deleted.
754 * @param array $existing
755 * @return array
756 */
757 function getPropertyDeletions( $existing ) {
758 return array_diff_assoc( $existing, $this->mProperties );
759 }
760
761 /**
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
765 * @return array
766 */
767 private function getInterwikiDeletions( $existing ) {
768 $del = [];
769 foreach ( $existing as $prefix => $dbkeys ) {
770 if ( isset( $this->mInterwikis[$prefix] ) ) {
771 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis[$prefix] );
772 } else {
773 $del[$prefix] = $existing[$prefix];
774 }
775 }
776
777 return $del;
778 }
779
780 /**
781 * Get an array of existing links, as a 2-D array
782 *
783 * @return array
784 */
785 private function getExistingLinks() {
786 $res = $this->getDB()->select( 'pagelinks', [ 'pl_namespace', 'pl_title' ],
787 [ 'pl_from' => $this->mId ], __METHOD__ );
788 $arr = [];
789 foreach ( $res as $row ) {
790 if ( !isset( $arr[$row->pl_namespace] ) ) {
791 $arr[$row->pl_namespace] = [];
792 }
793 $arr[$row->pl_namespace][$row->pl_title] = 1;
794 }
795
796 return $arr;
797 }
798
799 /**
800 * Get an array of existing templates, as a 2-D array
801 *
802 * @return array
803 */
804 private function getExistingTemplates() {
805 $res = $this->getDB()->select( 'templatelinks', [ 'tl_namespace', 'tl_title' ],
806 [ 'tl_from' => $this->mId ], __METHOD__ );
807 $arr = [];
808 foreach ( $res as $row ) {
809 if ( !isset( $arr[$row->tl_namespace] ) ) {
810 $arr[$row->tl_namespace] = [];
811 }
812 $arr[$row->tl_namespace][$row->tl_title] = 1;
813 }
814
815 return $arr;
816 }
817
818 /**
819 * Get an array of existing images, image names in the keys
820 *
821 * @return array
822 */
823 private function getExistingImages() {
824 $res = $this->getDB()->select( 'imagelinks', [ 'il_to' ],
825 [ 'il_from' => $this->mId ], __METHOD__ );
826 $arr = [];
827 foreach ( $res as $row ) {
828 $arr[$row->il_to] = 1;
829 }
830
831 return $arr;
832 }
833
834 /**
835 * Get an array of existing external links, URLs in the keys
836 *
837 * @return array
838 */
839 private function getExistingExternals() {
840 $res = $this->getDB()->select( 'externallinks', [ 'el_to' ],
841 [ 'el_from' => $this->mId ], __METHOD__ );
842 $arr = [];
843 foreach ( $res as $row ) {
844 $arr[$row->el_to] = 1;
845 }
846
847 return $arr;
848 }
849
850 /**
851 * Get an array of existing categories, with the name in the key and sort key in the value.
852 *
853 * @return array
854 */
855 private function getExistingCategories() {
856 $res = $this->getDB()->select( 'categorylinks', [ 'cl_to', 'cl_sortkey_prefix' ],
857 [ 'cl_from' => $this->mId ], __METHOD__ );
858 $arr = [];
859 foreach ( $res as $row ) {
860 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
861 }
862
863 return $arr;
864 }
865
866 /**
867 * Get an array of existing interlanguage links, with the language code in the key and the
868 * title in the value.
869 *
870 * @return array
871 */
872 private function getExistingInterlangs() {
873 $res = $this->getDB()->select( 'langlinks', [ 'll_lang', 'll_title' ],
874 [ 'll_from' => $this->mId ], __METHOD__ );
875 $arr = [];
876 foreach ( $res as $row ) {
877 $arr[$row->ll_lang] = $row->ll_title;
878 }
879
880 return $arr;
881 }
882
883 /**
884 * Get an array of existing inline interwiki links, as a 2-D array
885 * @return array (prefix => array(dbkey => 1))
886 */
887 private function getExistingInterwikis() {
888 $res = $this->getDB()->select( 'iwlinks', [ 'iwl_prefix', 'iwl_title' ],
889 [ 'iwl_from' => $this->mId ], __METHOD__ );
890 $arr = [];
891 foreach ( $res as $row ) {
892 if ( !isset( $arr[$row->iwl_prefix] ) ) {
893 $arr[$row->iwl_prefix] = [];
894 }
895 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
896 }
897
898 return $arr;
899 }
900
901 /**
902 * Get an array of existing categories, with the name in the key and sort key in the value.
903 *
904 * @return array Array of property names and values
905 */
906 private function getExistingProperties() {
907 $res = $this->getDB()->select( 'page_props', [ 'pp_propname', 'pp_value' ],
908 [ 'pp_page' => $this->mId ], __METHOD__ );
909 $arr = [];
910 foreach ( $res as $row ) {
911 $arr[$row->pp_propname] = $row->pp_value;
912 }
913
914 return $arr;
915 }
916
917 /**
918 * Return the title object of the page being updated
919 * @return Title
920 */
921 public function getTitle() {
922 return $this->mTitle;
923 }
924
925 /**
926 * Returns parser output
927 * @since 1.19
928 * @return ParserOutput
929 */
930 public function getParserOutput() {
931 return $this->mParserOutput;
932 }
933
934 /**
935 * Return the list of images used as generated by the parser
936 * @return array
937 */
938 public function getImages() {
939 return $this->mImages;
940 }
941
942 /**
943 * Set the revision corresponding to this LinksUpdate
944 *
945 * @since 1.27
946 *
947 * @param Revision $revision
948 */
949 public function setRevision( Revision $revision ) {
950 $this->mRevision = $revision;
951 }
952
953 /**
954 * @since 1.28
955 * @return null|Revision
956 */
957 public function getRevision() {
958 return $this->mRevision;
959 }
960
961 /**
962 * Set the User who triggered this LinksUpdate
963 *
964 * @since 1.27
965 * @param User $user
966 */
967 public function setTriggeringUser( User $user ) {
968 $this->user = $user;
969 }
970
971 /**
972 * @since 1.27
973 * @return null|User
974 */
975 public function getTriggeringUser() {
976 return $this->user;
977 }
978
979 /**
980 * Invalidate any necessary link lists related to page property changes
981 * @param array $changed
982 */
983 private function invalidateProperties( $changed ) {
984 global $wgPagePropLinkInvalidations;
985
986 foreach ( $changed as $name => $value ) {
987 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
988 $inv = $wgPagePropLinkInvalidations[$name];
989 if ( !is_array( $inv ) ) {
990 $inv = [ $inv ];
991 }
992 foreach ( $inv as $table ) {
993 DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->mTitle, $table ) );
994 }
995 }
996 }
997 }
998
999 /**
1000 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
1001 * @since 1.22
1002 * @return null|array Array of Titles
1003 */
1004 public function getAddedLinks() {
1005 if ( $this->linkInsertions === null ) {
1006 return null;
1007 }
1008 $result = [];
1009 foreach ( $this->linkInsertions as $insertion ) {
1010 $result[] = Title::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
1011 }
1012
1013 return $result;
1014 }
1015
1016 /**
1017 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
1018 * @since 1.22
1019 * @return null|array Array of Titles
1020 */
1021 public function getRemovedLinks() {
1022 if ( $this->linkDeletions === null ) {
1023 return null;
1024 }
1025 $result = [];
1026 foreach ( $this->linkDeletions as $ns => $titles ) {
1027 foreach ( $titles as $title => $unused ) {
1028 $result[] = Title::makeTitle( $ns, $title );
1029 }
1030 }
1031
1032 return $result;
1033 }
1034
1035 /**
1036 * Fetch page properties added by this LinksUpdate.
1037 * Only available after the update is complete.
1038 * @since 1.28
1039 * @return null|array
1040 */
1041 public function getAddedProperties() {
1042 return $this->propertyInsertions;
1043 }
1044
1045 /**
1046 * Fetch page properties removed by this LinksUpdate.
1047 * Only available after the update is complete.
1048 * @since 1.28
1049 * @return null|array
1050 */
1051 public function getRemovedProperties() {
1052 return $this->propertyDeletions;
1053 }
1054
1055 /**
1056 * Update links table freshness
1057 */
1058 private function updateLinksTimestamp() {
1059 if ( $this->mId ) {
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 ],
1065 __METHOD__
1066 );
1067 }
1068 }
1069
1070 /**
1071 * @return IDatabase
1072 */
1073 private function getDB() {
1074 if ( !$this->db ) {
1075 $this->db = wfGetDB( DB_MASTER );
1076 }
1077
1078 return $this->db;
1079 }
1080
1081 public function getAsJobSpecification() {
1082 if ( $this->user ) {
1083 $userInfo = [
1084 'userId' => $this->user->getId(),
1085 'userName' => $this->user->getName(),
1086 ];
1087 } else {
1088 $userInfo = false;
1089 }
1090
1091 if ( $this->mRevision ) {
1092 $triggeringRevisionId = $this->mRevision->getId();
1093 } else {
1094 $triggeringRevisionId = false;
1095 }
1096
1097 return [
1098 'wiki' => $this->getDB()->getWikiID(),
1099 'job' => new JobSpecification(
1100 'refreshLinksPrioritized',
1101 [
1102 // Reuse the parser cache if it was saved
1103 'rootJobTimestamp' => $this->mParserOutput->getCacheTime(),
1104 'useRecursiveLinksUpdate' => $this->mRecursive,
1105 'triggeringUser' => $userInfo,
1106 'triggeringRevisionId' => $triggeringRevisionId,
1107 ],
1108 [ 'removeDuplicates' => true ],
1109 $this->getTitle()
1110 )
1111 ];
1112 }
1113 }