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