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