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