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