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