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