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