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