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