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