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