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