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