Merge "Http::getProxy() method to get proxy configuration"
[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 /**
24 * See docs/deferred.txt
25 *
26 * @todo document (e.g. one-sentence top-level class description).
27 */
28 class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
29 // @todo make members protected, but make sure extensions don't break
30
31 /** @var int Page ID of the article linked from */
32 public $mId;
33
34 /** @var Title Title object of the article linked from */
35 public $mTitle;
36
37 /** @var ParserOutput */
38 public $mParserOutput;
39
40 /** @var array Map of title strings to IDs for the links in the document */
41 public $mLinks;
42
43 /** @var array DB keys of the images used, in the array key only */
44 public $mImages;
45
46 /** @var array Map of title strings to IDs for the template references, including broken ones */
47 public $mTemplates;
48
49 /** @var array URLs of external links, array key only */
50 public $mExternals;
51
52 /** @var array Map of category names to sort keys */
53 public $mCategories;
54
55 /** @var array Map of language codes to titles */
56 public $mInterlangs;
57
58 /** @var array Map of arbitrary name to value */
59 public $mProperties;
60
61 /** @var bool Whether to queue jobs for recursive updates */
62 public $mRecursive;
63
64 /** @var Revision Revision for which this update has been triggered */
65 private $mRevision;
66
67 /**
68 * @var null|array Added links if calculated.
69 */
70 private $linkInsertions = null;
71
72 /**
73 * @var null|array Deleted links if calculated.
74 */
75 private $linkDeletions = null;
76
77 /**
78 * @var User|null
79 */
80 private $user;
81
82 /**
83 * Constructor
84 *
85 * @param Title $title Title of the page we're updating
86 * @param ParserOutput $parserOutput Output from a full parse of this page
87 * @param bool $recursive Queue jobs for recursive updates?
88 * @throws MWException
89 */
90 function __construct( Title $title, ParserOutput $parserOutput, $recursive = true ) {
91 parent::__construct( false ); // no implicit transaction
92
93 $this->mTitle = $title;
94 $this->mId = $title->getArticleID( Title::GAID_FOR_UPDATE );
95
96 if ( !$this->mId ) {
97 throw new InvalidArgumentException(
98 "The Title object yields no ID. Perhaps the page doesn't exist?"
99 );
100 }
101
102 $this->mParserOutput = $parserOutput;
103
104 $this->mLinks = $parserOutput->getLinks();
105 $this->mImages = $parserOutput->getImages();
106 $this->mTemplates = $parserOutput->getTemplates();
107 $this->mExternals = $parserOutput->getExternalLinks();
108 $this->mCategories = $parserOutput->getCategories();
109 $this->mProperties = $parserOutput->getProperties();
110 $this->mInterwikis = $parserOutput->getInterwikiLinks();
111
112 # Convert the format of the interlanguage links
113 # I didn't want to change it in the ParserOutput, because that array is passed all
114 # the way back to the skin, so either a skin API break would be required, or an
115 # inefficient back-conversion.
116 $ill = $parserOutput->getLanguageLinks();
117 $this->mInterlangs = [];
118 foreach ( $ill as $link ) {
119 list( $key, $title ) = explode( ':', $link, 2 );
120 $this->mInterlangs[$key] = $title;
121 }
122
123 foreach ( $this->mCategories as &$sortkey ) {
124 # If the sortkey is longer then 255 bytes,
125 # it truncated by DB, and then doesn't get
126 # matched when comparing existing vs current
127 # categories, causing bug 25254.
128 # Also. substr behaves weird when given "".
129 if ( $sortkey !== '' ) {
130 $sortkey = substr( $sortkey, 0, 255 );
131 }
132 }
133
134 $this->mRecursive = $recursive;
135
136 Hooks::run( 'LinksUpdateConstructed', [ &$this ] );
137 }
138
139 /**
140 * Update link tables with outgoing links from an updated article
141 */
142 public function doUpdate() {
143 Hooks::run( 'LinksUpdate', [ &$this ] );
144 $this->doIncrementalUpdate();
145
146 $this->mDb->onTransactionIdle( function() {
147 Hooks::run( 'LinksUpdateComplete', [ &$this ] );
148 } );
149 }
150
151 protected function doIncrementalUpdate() {
152 # Page links
153 $existing = $this->getExistingLinks();
154 $this->linkDeletions = $this->getLinkDeletions( $existing );
155 $this->linkInsertions = $this->getLinkInsertions( $existing );
156 $this->incrTableUpdate( 'pagelinks', 'pl', $this->linkDeletions, $this->linkInsertions );
157
158 # Image links
159 $existing = $this->getExistingImages();
160
161 $imageDeletes = $this->getImageDeletions( $existing );
162 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
163 $this->getImageInsertions( $existing ) );
164
165 # Invalidate all image description pages which had links added or removed
166 $imageUpdates = $imageDeletes + array_diff_key( $this->mImages, $existing );
167 $this->invalidateImageDescriptions( $imageUpdates );
168
169 # External links
170 $existing = $this->getExistingExternals();
171 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
172 $this->getExternalInsertions( $existing ) );
173
174 # Language links
175 $existing = $this->getExistingInterlangs();
176 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
177 $this->getInterlangInsertions( $existing ) );
178
179 # Inline interwiki links
180 $existing = $this->getExistingInterwikis();
181 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
182 $this->getInterwikiInsertions( $existing ) );
183
184 # Template links
185 $existing = $this->getExistingTemplates();
186 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
187 $this->getTemplateInsertions( $existing ) );
188
189 # Category links
190 $existing = $this->getExistingCategories();
191
192 $categoryDeletes = $this->getCategoryDeletions( $existing );
193
194 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
195 $this->getCategoryInsertions( $existing ) );
196
197 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
198 $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
199 $categoryUpdates = $categoryInserts + $categoryDeletes;
200 $this->invalidateCategories( $categoryUpdates );
201 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
202
203 # Page properties
204 $existing = $this->getExistingProperties();
205
206 $propertiesDeletes = $this->getPropertyDeletions( $existing );
207
208 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
209 $this->getPropertyInsertions( $existing ) );
210
211 # Invalidate the necessary pages
212 $changed = $propertiesDeletes + array_diff_assoc( $this->mProperties, $existing );
213 $this->invalidateProperties( $changed );
214
215 # Update the links table freshness for this title
216 $this->updateLinksTimestamp();
217
218 # Refresh links of all pages including this page
219 # This will be in a separate transaction
220 if ( $this->mRecursive ) {
221 $this->queueRecursiveJobs();
222 }
223
224 }
225
226 /**
227 * Queue recursive jobs for this page
228 *
229 * Which means do LinksUpdate on all pages that include the current page,
230 * using the job queue.
231 */
232 protected function queueRecursiveJobs() {
233 self::queueRecursiveJobsForTable( $this->mTitle, 'templatelinks' );
234 if ( $this->mTitle->getNamespace() == NS_FILE ) {
235 // Process imagelinks in case the title is or was a redirect
236 self::queueRecursiveJobsForTable( $this->mTitle, 'imagelinks' );
237 }
238
239 $bc = $this->mTitle->getBacklinkCache();
240 // Get jobs for cascade-protected backlinks for a high priority queue.
241 // If meta-templates change to using a new template, the new template
242 // should be implicitly protected as soon as possible, if applicable.
243 // These jobs duplicate a subset of the above ones, but can run sooner.
244 // Which ever runs first generally no-ops the other one.
245 $jobs = [];
246 foreach ( $bc->getCascadeProtectedLinks() as $title ) {
247 $jobs[] = RefreshLinksJob::newPrioritized( $title, [] );
248 }
249 JobQueueGroup::singleton()->push( $jobs );
250 }
251
252 /**
253 * Queue a RefreshLinks job for any table.
254 *
255 * @param Title $title Title to do job for
256 * @param string $table Table to use (e.g. 'templatelinks')
257 */
258 public static function queueRecursiveJobsForTable( Title $title, $table ) {
259 if ( $title->getBacklinkCache()->hasLinks( $table ) ) {
260 $job = new RefreshLinksJob(
261 $title,
262 [
263 'table' => $table,
264 'recursive' => true,
265 ] + Job::newRootJobParams( // "overall" refresh links job info
266 "refreshlinks:{$table}:{$title->getPrefixedText()}"
267 )
268 );
269
270 JobQueueGroup::singleton()->push( $job );
271 }
272 }
273
274 /**
275 * @param array $cats
276 */
277 function invalidateCategories( $cats ) {
278 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
279 }
280
281 /**
282 * Update all the appropriate counts in the category table.
283 * @param array $added Associative array of category name => sort key
284 * @param array $deleted Associative array of category name => sort key
285 */
286 function updateCategoryCounts( $added, $deleted ) {
287 $a = WikiPage::factory( $this->mTitle );
288 $a->updateCategoryCounts(
289 array_keys( $added ), array_keys( $deleted )
290 );
291 }
292
293 /**
294 * @param array $images
295 */
296 function invalidateImageDescriptions( $images ) {
297 $this->invalidatePages( NS_FILE, array_keys( $images ) );
298 }
299
300 /**
301 * Update a table by doing a delete query then an insert query
302 * @param string $table Table name
303 * @param string $prefix Field name prefix
304 * @param array $deletions
305 * @param array $insertions Rows to insert
306 */
307 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
308 if ( $table == 'page_props' ) {
309 $fromField = 'pp_page';
310 } else {
311 $fromField = "{$prefix}_from";
312 }
313 $where = [ $fromField => $this->mId ];
314 if ( $table == 'pagelinks' || $table == 'templatelinks' || $table == 'iwlinks' ) {
315 if ( $table == 'iwlinks' ) {
316 $baseKey = 'iwl_prefix';
317 } else {
318 $baseKey = "{$prefix}_namespace";
319 }
320 $clause = $this->mDb->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
321 if ( $clause ) {
322 $where[] = $clause;
323 } else {
324 $where = false;
325 }
326 } else {
327 if ( $table == 'langlinks' ) {
328 $toField = 'll_lang';
329 } elseif ( $table == 'page_props' ) {
330 $toField = 'pp_propname';
331 } else {
332 $toField = $prefix . '_to';
333 }
334 if ( count( $deletions ) ) {
335 $where[$toField] = array_keys( $deletions );
336 } else {
337 $where = false;
338 }
339 }
340 if ( $where ) {
341 $this->mDb->delete( $table, $where, __METHOD__ );
342 }
343 if ( count( $insertions ) ) {
344 $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
345 Hooks::run( 'LinksUpdateAfterInsert', [ $this, $table, $insertions ] );
346 }
347 }
348
349 /**
350 * Get an array of pagelinks insertions for passing to the DB
351 * Skips the titles specified by the 2-D array $existing
352 * @param array $existing
353 * @return array
354 */
355 private function getLinkInsertions( $existing = [] ) {
356 $arr = [];
357 foreach ( $this->mLinks as $ns => $dbkeys ) {
358 $diffs = isset( $existing[$ns] )
359 ? array_diff_key( $dbkeys, $existing[$ns] )
360 : $dbkeys;
361 foreach ( $diffs as $dbk => $id ) {
362 $arr[] = [
363 'pl_from' => $this->mId,
364 'pl_from_namespace' => $this->mTitle->getNamespace(),
365 'pl_namespace' => $ns,
366 'pl_title' => $dbk
367 ];
368 }
369 }
370
371 return $arr;
372 }
373
374 /**
375 * Get an array of template insertions. Like getLinkInsertions()
376 * @param array $existing
377 * @return array
378 */
379 private function getTemplateInsertions( $existing = [] ) {
380 $arr = [];
381 foreach ( $this->mTemplates as $ns => $dbkeys ) {
382 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
383 foreach ( $diffs as $dbk => $id ) {
384 $arr[] = [
385 'tl_from' => $this->mId,
386 'tl_from_namespace' => $this->mTitle->getNamespace(),
387 'tl_namespace' => $ns,
388 'tl_title' => $dbk
389 ];
390 }
391 }
392
393 return $arr;
394 }
395
396 /**
397 * Get an array of image insertions
398 * Skips the names specified in $existing
399 * @param array $existing
400 * @return array
401 */
402 private function getImageInsertions( $existing = [] ) {
403 $arr = [];
404 $diffs = array_diff_key( $this->mImages, $existing );
405 foreach ( $diffs as $iname => $dummy ) {
406 $arr[] = [
407 'il_from' => $this->mId,
408 'il_from_namespace' => $this->mTitle->getNamespace(),
409 'il_to' => $iname
410 ];
411 }
412
413 return $arr;
414 }
415
416 /**
417 * Get an array of externallinks insertions. Skips the names specified in $existing
418 * @param array $existing
419 * @return array
420 */
421 private function getExternalInsertions( $existing = [] ) {
422 $arr = [];
423 $diffs = array_diff_key( $this->mExternals, $existing );
424 foreach ( $diffs as $url => $dummy ) {
425 foreach ( wfMakeUrlIndexes( $url ) as $index ) {
426 $arr[] = [
427 'el_id' => $this->mDb->nextSequenceValue( 'externallinks_el_id_seq' ),
428 'el_from' => $this->mId,
429 'el_to' => $url,
430 'el_index' => $index,
431 ];
432 }
433 }
434
435 return $arr;
436 }
437
438 /**
439 * Get an array of category insertions
440 *
441 * @param array $existing Mapping existing category names to sort keys. If both
442 * match a link in $this, the link will be omitted from the output
443 *
444 * @return array
445 */
446 private function getCategoryInsertions( $existing = [] ) {
447 global $wgContLang, $wgCategoryCollation;
448 $diffs = array_diff_assoc( $this->mCategories, $existing );
449 $arr = [];
450 foreach ( $diffs as $name => $prefix ) {
451 $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
452 $wgContLang->findVariantLink( $name, $nt, true );
453
454 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
455 $type = 'subcat';
456 } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
457 $type = 'file';
458 } else {
459 $type = 'page';
460 }
461
462 # Treat custom sortkeys as a prefix, so that if multiple
463 # things are forced to sort as '*' or something, they'll
464 # sort properly in the category rather than in page_id
465 # order or such.
466 $sortkey = Collation::singleton()->getSortKey(
467 $this->mTitle->getCategorySortkey( $prefix ) );
468
469 $arr[] = [
470 'cl_from' => $this->mId,
471 'cl_to' => $name,
472 'cl_sortkey' => $sortkey,
473 'cl_timestamp' => $this->mDb->timestamp(),
474 'cl_sortkey_prefix' => $prefix,
475 'cl_collation' => $wgCategoryCollation,
476 'cl_type' => $type,
477 ];
478 }
479
480 return $arr;
481 }
482
483 /**
484 * Get an array of interlanguage link insertions
485 *
486 * @param array $existing Mapping existing language codes to titles
487 *
488 * @return array
489 */
490 private function getInterlangInsertions( $existing = [] ) {
491 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
492 $arr = [];
493 foreach ( $diffs as $lang => $title ) {
494 $arr[] = [
495 'll_from' => $this->mId,
496 'll_lang' => $lang,
497 'll_title' => $title
498 ];
499 }
500
501 return $arr;
502 }
503
504 /**
505 * Get an array of page property insertions
506 * @param array $existing
507 * @return array
508 */
509 function getPropertyInsertions( $existing = [] ) {
510 $diffs = array_diff_assoc( $this->mProperties, $existing );
511
512 $arr = [];
513 foreach ( array_keys( $diffs ) as $name ) {
514 $arr[] = $this->getPagePropRowData( $name );
515 }
516
517 return $arr;
518 }
519
520 /**
521 * Returns an associative array to be used for inserting a row into
522 * the page_props table. Besides the given property name, this will
523 * include the page id from $this->mId and any property value from
524 * $this->mProperties.
525 *
526 * The array returned will include the pp_sortkey field if this
527 * is present in the database (as indicated by $wgPagePropsHaveSortkey).
528 * The sortkey value is currently determined by getPropertySortKeyValue().
529 *
530 * @note this assumes that $this->mProperties[$prop] is defined.
531 *
532 * @param string $prop The name of the property.
533 *
534 * @return array
535 */
536 private function getPagePropRowData( $prop ) {
537 global $wgPagePropsHaveSortkey;
538
539 $value = $this->mProperties[$prop];
540
541 $row = [
542 'pp_page' => $this->mId,
543 'pp_propname' => $prop,
544 'pp_value' => $value,
545 ];
546
547 if ( $wgPagePropsHaveSortkey ) {
548 $row['pp_sortkey'] = $this->getPropertySortKeyValue( $value );
549 }
550
551 return $row;
552 }
553
554 /**
555 * Determines the sort key for the given property value.
556 * This will return $value if it is a float or int,
557 * 1 or resp. 0 if it is a bool, and null otherwise.
558 *
559 * @note In the future, we may allow the sortkey to be specified explicitly
560 * in ParserOutput::setProperty.
561 *
562 * @param mixed $value
563 *
564 * @return float|null
565 */
566 private function getPropertySortKeyValue( $value ) {
567 if ( is_int( $value ) || is_float( $value ) || is_bool( $value ) ) {
568 return floatval( $value );
569 }
570
571 return null;
572 }
573
574 /**
575 * Get an array of interwiki insertions for passing to the DB
576 * Skips the titles specified by the 2-D array $existing
577 * @param array $existing
578 * @return array
579 */
580 private function getInterwikiInsertions( $existing = [] ) {
581 $arr = [];
582 foreach ( $this->mInterwikis as $prefix => $dbkeys ) {
583 $diffs = isset( $existing[$prefix] )
584 ? array_diff_key( $dbkeys, $existing[$prefix] )
585 : $dbkeys;
586
587 foreach ( $diffs as $dbk => $id ) {
588 $arr[] = [
589 'iwl_from' => $this->mId,
590 'iwl_prefix' => $prefix,
591 'iwl_title' => $dbk
592 ];
593 }
594 }
595
596 return $arr;
597 }
598
599 /**
600 * Given an array of existing links, returns those links which are not in $this
601 * and thus should be deleted.
602 * @param array $existing
603 * @return array
604 */
605 private function getLinkDeletions( $existing ) {
606 $del = [];
607 foreach ( $existing as $ns => $dbkeys ) {
608 if ( isset( $this->mLinks[$ns] ) ) {
609 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
610 } else {
611 $del[$ns] = $existing[$ns];
612 }
613 }
614
615 return $del;
616 }
617
618 /**
619 * Given an array of existing templates, returns those templates which are not in $this
620 * and thus should be deleted.
621 * @param array $existing
622 * @return array
623 */
624 private function getTemplateDeletions( $existing ) {
625 $del = [];
626 foreach ( $existing as $ns => $dbkeys ) {
627 if ( isset( $this->mTemplates[$ns] ) ) {
628 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
629 } else {
630 $del[$ns] = $existing[$ns];
631 }
632 }
633
634 return $del;
635 }
636
637 /**
638 * Given an array of existing images, returns those images which are not in $this
639 * and thus should be deleted.
640 * @param array $existing
641 * @return array
642 */
643 private function getImageDeletions( $existing ) {
644 return array_diff_key( $existing, $this->mImages );
645 }
646
647 /**
648 * Given an array of existing external links, returns those links which are not
649 * in $this and thus should be deleted.
650 * @param array $existing
651 * @return array
652 */
653 private function getExternalDeletions( $existing ) {
654 return array_diff_key( $existing, $this->mExternals );
655 }
656
657 /**
658 * Given an array of existing categories, returns those categories which are not in $this
659 * and thus should be deleted.
660 * @param array $existing
661 * @return array
662 */
663 private function getCategoryDeletions( $existing ) {
664 return array_diff_assoc( $existing, $this->mCategories );
665 }
666
667 /**
668 * Given an array of existing interlanguage links, returns those links which are not
669 * in $this and thus should be deleted.
670 * @param array $existing
671 * @return array
672 */
673 private function getInterlangDeletions( $existing ) {
674 return array_diff_assoc( $existing, $this->mInterlangs );
675 }
676
677 /**
678 * Get array of properties which should be deleted.
679 * @param array $existing
680 * @return array
681 */
682 function getPropertyDeletions( $existing ) {
683 return array_diff_assoc( $existing, $this->mProperties );
684 }
685
686 /**
687 * Given an array of existing interwiki links, returns those links which are not in $this
688 * and thus should be deleted.
689 * @param array $existing
690 * @return array
691 */
692 private function getInterwikiDeletions( $existing ) {
693 $del = [];
694 foreach ( $existing as $prefix => $dbkeys ) {
695 if ( isset( $this->mInterwikis[$prefix] ) ) {
696 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis[$prefix] );
697 } else {
698 $del[$prefix] = $existing[$prefix];
699 }
700 }
701
702 return $del;
703 }
704
705 /**
706 * Get an array of existing links, as a 2-D array
707 *
708 * @return array
709 */
710 private function getExistingLinks() {
711 $res = $this->mDb->select( 'pagelinks', [ 'pl_namespace', 'pl_title' ],
712 [ 'pl_from' => $this->mId ], __METHOD__, $this->mOptions );
713 $arr = [];
714 foreach ( $res as $row ) {
715 if ( !isset( $arr[$row->pl_namespace] ) ) {
716 $arr[$row->pl_namespace] = [];
717 }
718 $arr[$row->pl_namespace][$row->pl_title] = 1;
719 }
720
721 return $arr;
722 }
723
724 /**
725 * Get an array of existing templates, as a 2-D array
726 *
727 * @return array
728 */
729 private function getExistingTemplates() {
730 $res = $this->mDb->select( 'templatelinks', [ 'tl_namespace', 'tl_title' ],
731 [ 'tl_from' => $this->mId ], __METHOD__, $this->mOptions );
732 $arr = [];
733 foreach ( $res as $row ) {
734 if ( !isset( $arr[$row->tl_namespace] ) ) {
735 $arr[$row->tl_namespace] = [];
736 }
737 $arr[$row->tl_namespace][$row->tl_title] = 1;
738 }
739
740 return $arr;
741 }
742
743 /**
744 * Get an array of existing images, image names in the keys
745 *
746 * @return array
747 */
748 private function getExistingImages() {
749 $res = $this->mDb->select( 'imagelinks', [ 'il_to' ],
750 [ 'il_from' => $this->mId ], __METHOD__, $this->mOptions );
751 $arr = [];
752 foreach ( $res as $row ) {
753 $arr[$row->il_to] = 1;
754 }
755
756 return $arr;
757 }
758
759 /**
760 * Get an array of existing external links, URLs in the keys
761 *
762 * @return array
763 */
764 private function getExistingExternals() {
765 $res = $this->mDb->select( 'externallinks', [ 'el_to' ],
766 [ 'el_from' => $this->mId ], __METHOD__, $this->mOptions );
767 $arr = [];
768 foreach ( $res as $row ) {
769 $arr[$row->el_to] = 1;
770 }
771
772 return $arr;
773 }
774
775 /**
776 * Get an array of existing categories, with the name in the key and sort key in the value.
777 *
778 * @return array
779 */
780 private function getExistingCategories() {
781 $res = $this->mDb->select( 'categorylinks', [ 'cl_to', 'cl_sortkey_prefix' ],
782 [ 'cl_from' => $this->mId ], __METHOD__, $this->mOptions );
783 $arr = [];
784 foreach ( $res as $row ) {
785 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
786 }
787
788 return $arr;
789 }
790
791 /**
792 * Get an array of existing interlanguage links, with the language code in the key and the
793 * title in the value.
794 *
795 * @return array
796 */
797 private function getExistingInterlangs() {
798 $res = $this->mDb->select( 'langlinks', [ 'll_lang', 'll_title' ],
799 [ 'll_from' => $this->mId ], __METHOD__, $this->mOptions );
800 $arr = [];
801 foreach ( $res as $row ) {
802 $arr[$row->ll_lang] = $row->ll_title;
803 }
804
805 return $arr;
806 }
807
808 /**
809 * Get an array of existing inline interwiki links, as a 2-D array
810 * @return array (prefix => array(dbkey => 1))
811 */
812 protected function getExistingInterwikis() {
813 $res = $this->mDb->select( 'iwlinks', [ 'iwl_prefix', 'iwl_title' ],
814 [ 'iwl_from' => $this->mId ], __METHOD__, $this->mOptions );
815 $arr = [];
816 foreach ( $res as $row ) {
817 if ( !isset( $arr[$row->iwl_prefix] ) ) {
818 $arr[$row->iwl_prefix] = [];
819 }
820 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
821 }
822
823 return $arr;
824 }
825
826 /**
827 * Get an array of existing categories, with the name in the key and sort key in the value.
828 *
829 * @return array Array of property names and values
830 */
831 private function getExistingProperties() {
832 $res = $this->mDb->select( 'page_props', [ 'pp_propname', 'pp_value' ],
833 [ 'pp_page' => $this->mId ], __METHOD__, $this->mOptions );
834 $arr = [];
835 foreach ( $res as $row ) {
836 $arr[$row->pp_propname] = $row->pp_value;
837 }
838
839 return $arr;
840 }
841
842 /**
843 * Return the title object of the page being updated
844 * @return Title
845 */
846 public function getTitle() {
847 return $this->mTitle;
848 }
849
850 /**
851 * Returns parser output
852 * @since 1.19
853 * @return ParserOutput
854 */
855 public function getParserOutput() {
856 return $this->mParserOutput;
857 }
858
859 /**
860 * Return the list of images used as generated by the parser
861 * @return array
862 */
863 public function getImages() {
864 return $this->mImages;
865 }
866
867 /**
868 * Set the revision corresponding to this LinksUpdate
869 *
870 * @since 1.27
871 *
872 * @param Revision $revision
873 */
874 public function setRevision( Revision $revision ) {
875 $this->mRevision = $revision;
876 }
877
878 /**
879 * Set the User who triggered this LinksUpdate
880 *
881 * @since 1.27
882 * @param User $user
883 */
884 public function setTriggeringUser( User $user ) {
885 $this->user = $user;
886 }
887
888 /**
889 * @since 1.27
890 * @return null|User
891 */
892 public function getTriggeringUser() {
893 return $this->user;
894 }
895
896 /**
897 * Invalidate any necessary link lists related to page property changes
898 * @param array $changed
899 */
900 private function invalidateProperties( $changed ) {
901 global $wgPagePropLinkInvalidations;
902
903 foreach ( $changed as $name => $value ) {
904 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
905 $inv = $wgPagePropLinkInvalidations[$name];
906 if ( !is_array( $inv ) ) {
907 $inv = [ $inv ];
908 }
909 foreach ( $inv as $table ) {
910 DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->mTitle, $table ) );
911 }
912 }
913 }
914 }
915
916 /**
917 * Fetch page links added by this LinksUpdate. Only available after the update is complete.
918 * @since 1.22
919 * @return null|array Array of Titles
920 */
921 public function getAddedLinks() {
922 if ( $this->linkInsertions === null ) {
923 return null;
924 }
925 $result = [];
926 foreach ( $this->linkInsertions as $insertion ) {
927 $result[] = Title::makeTitle( $insertion['pl_namespace'], $insertion['pl_title'] );
928 }
929
930 return $result;
931 }
932
933 /**
934 * Fetch page links removed by this LinksUpdate. Only available after the update is complete.
935 * @since 1.22
936 * @return null|array Array of Titles
937 */
938 public function getRemovedLinks() {
939 if ( $this->linkDeletions === null ) {
940 return null;
941 }
942 $result = [];
943 foreach ( $this->linkDeletions as $ns => $titles ) {
944 foreach ( $titles as $title => $unused ) {
945 $result[] = Title::makeTitle( $ns, $title );
946 }
947 }
948
949 return $result;
950 }
951
952 /**
953 * Update links table freshness
954 */
955 protected function updateLinksTimestamp() {
956 if ( $this->mId ) {
957 // The link updates made here only reflect the freshness of the parser output
958 $timestamp = $this->mParserOutput->getCacheTime();
959 $this->mDb->update( 'page',
960 [ 'page_links_updated' => $this->mDb->timestamp( $timestamp ) ],
961 [ 'page_id' => $this->mId ],
962 __METHOD__
963 );
964 }
965 }
966
967 public function getAsJobSpecification() {
968 if ( $this->user ) {
969 $userInfo = [
970 'userId' => $this->user->getId(),
971 'userName' => $this->user->getName(),
972 ];
973 } else {
974 $userInfo = false;
975 }
976
977 if ( $this->mRevision ) {
978 $triggeringRevisionId = $this->mRevision->getId();
979 } else {
980 $triggeringRevisionId = false;
981 }
982
983 return [
984 'wiki' => $this->mDb->getWikiID(),
985 'job' => new JobSpecification(
986 'refreshLinksPrioritized',
987 [
988 // Reuse the parser cache if it was saved
989 'rootJobTimestamp' => $this->mParserOutput->getCacheTime(),
990 'useRecursiveLinksUpdate' => $this->mRecursive,
991 'triggeringUser' => $userInfo,
992 'triggeringRevisionId' => $triggeringRevisionId,
993 ],
994 [ 'removeDuplicates' => true ],
995 $this->getTitle()
996 )
997 ];
998 }
999 }