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