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