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