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