testPngNativetZtxt requires zlib extension
[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 function queueRecursiveJobs() {
241 wfProfileIn( __METHOD__ );
242
243 if ( $this->mTitle->getBacklinkCache()->hasLinks( 'templatelinks' ) ) {
244 $job = new RefreshLinksJob2(
245 $this->mTitle,
246 array(
247 'table' => 'templatelinks',
248 ) + Job::newRootJobParams( // "overall" refresh links job info
249 "refreshlinks:templatelinks:{$this->mTitle->getPrefixedText()}"
250 )
251 );
252 JobQueueGroup::singleton()->push( $job );
253 JobQueueGroup::singleton()->deduplicateRootJob( $job );
254 }
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 wfRunHooks( 'LinksUpdateAfterInsert', array( $this, $table, $insertions ) );
346 }
347 }
348
349 /**
350 * Get an array of pagelinks insertions for passing to the DB
351 * Skips the titles specified by the 2-D array $existing
352 * @param $existing array
353 * @return array
354 */
355 private function getLinkInsertions( $existing = array() ) {
356 $arr = array();
357 foreach( $this->mLinks as $ns => $dbkeys ) {
358 $diffs = isset( $existing[$ns] )
359 ? array_diff_key( $dbkeys, $existing[$ns] )
360 : $dbkeys;
361 foreach ( $diffs as $dbk => $id ) {
362 $arr[] = array(
363 'pl_from' => $this->mId,
364 'pl_namespace' => $ns,
365 'pl_title' => $dbk
366 );
367 }
368 }
369 return $arr;
370 }
371
372 /**
373 * Get an array of template insertions. Like getLinkInsertions()
374 * @param $existing array
375 * @return array
376 */
377 private function getTemplateInsertions( $existing = array() ) {
378 $arr = array();
379 foreach( $this->mTemplates as $ns => $dbkeys ) {
380 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
381 foreach ( $diffs as $dbk => $id ) {
382 $arr[] = array(
383 'tl_from' => $this->mId,
384 'tl_namespace' => $ns,
385 'tl_title' => $dbk
386 );
387 }
388 }
389 return $arr;
390 }
391
392 /**
393 * Get an array of image insertions
394 * Skips the names specified in $existing
395 * @param $existing array
396 * @return array
397 */
398 private function getImageInsertions( $existing = array() ) {
399 $arr = array();
400 $diffs = array_diff_key( $this->mImages, $existing );
401 foreach( $diffs as $iname => $dummy ) {
402 $arr[] = array(
403 'il_from' => $this->mId,
404 'il_to' => $iname
405 );
406 }
407 return $arr;
408 }
409
410 /**
411 * Get an array of externallinks insertions. Skips the names specified in $existing
412 * @param $existing array
413 * @return array
414 */
415 private function getExternalInsertions( $existing = array() ) {
416 $arr = array();
417 $diffs = array_diff_key( $this->mExternals, $existing );
418 foreach( $diffs as $url => $dummy ) {
419 foreach( wfMakeUrlIndexes( $url ) as $index ) {
420 $arr[] = array(
421 'el_from' => $this->mId,
422 'el_to' => $url,
423 'el_index' => $index,
424 );
425 }
426 }
427 return $arr;
428 }
429
430 /**
431 * Get an array of category insertions
432 *
433 * @param $existing array mapping existing category names to sort keys. If both
434 * match a link in $this, the link will be omitted from the output
435 *
436 * @return array
437 */
438 private function getCategoryInsertions( $existing = array() ) {
439 global $wgContLang, $wgCategoryCollation;
440 $diffs = array_diff_assoc( $this->mCategories, $existing );
441 $arr = array();
442 foreach ( $diffs as $name => $prefix ) {
443 $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
444 $wgContLang->findVariantLink( $name, $nt, true );
445
446 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
447 $type = 'subcat';
448 } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
449 $type = 'file';
450 } else {
451 $type = 'page';
452 }
453
454 # Treat custom sortkeys as a prefix, so that if multiple
455 # things are forced to sort as '*' or something, they'll
456 # sort properly in the category rather than in page_id
457 # order or such.
458 $sortkey = Collation::singleton()->getSortKey(
459 $this->mTitle->getCategorySortkey( $prefix ) );
460
461 $arr[] = array(
462 'cl_from' => $this->mId,
463 'cl_to' => $name,
464 'cl_sortkey' => $sortkey,
465 'cl_timestamp' => $this->mDb->timestamp(),
466 'cl_sortkey_prefix' => $prefix,
467 'cl_collation' => $wgCategoryCollation,
468 'cl_type' => $type,
469 );
470 }
471 return $arr;
472 }
473
474 /**
475 * Get an array of interlanguage link insertions
476 *
477 * @param $existing Array mapping existing language codes to titles
478 *
479 * @return array
480 */
481 private function getInterlangInsertions( $existing = array() ) {
482 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
483 $arr = array();
484 foreach( $diffs as $lang => $title ) {
485 $arr[] = array(
486 'll_from' => $this->mId,
487 'll_lang' => $lang,
488 'll_title' => $title
489 );
490 }
491 return $arr;
492 }
493
494 /**
495 * Get an array of page property insertions
496 * @param $existing array
497 * @return array
498 */
499 function getPropertyInsertions( $existing = array() ) {
500 $diffs = array_diff_assoc( $this->mProperties, $existing );
501 $arr = array();
502 foreach ( $diffs as $name => $value ) {
503 $arr[] = array(
504 'pp_page' => $this->mId,
505 'pp_propname' => $name,
506 'pp_value' => $value,
507 );
508 }
509 return $arr;
510 }
511
512 /**
513 * Get an array of interwiki insertions for passing to the DB
514 * Skips the titles specified by the 2-D array $existing
515 * @param $existing array
516 * @return array
517 */
518 private function getInterwikiInsertions( $existing = array() ) {
519 $arr = array();
520 foreach( $this->mInterwikis as $prefix => $dbkeys ) {
521 $diffs = isset( $existing[$prefix] ) ? array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
522 foreach ( $diffs as $dbk => $id ) {
523 $arr[] = array(
524 'iwl_from' => $this->mId,
525 'iwl_prefix' => $prefix,
526 'iwl_title' => $dbk
527 );
528 }
529 }
530 return $arr;
531 }
532
533 /**
534 * Given an array of existing links, returns those links which are not in $this
535 * and thus should be deleted.
536 * @param $existing array
537 * @return array
538 */
539 private function getLinkDeletions( $existing ) {
540 $del = array();
541 foreach ( $existing as $ns => $dbkeys ) {
542 if ( isset( $this->mLinks[$ns] ) ) {
543 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
544 } else {
545 $del[$ns] = $existing[$ns];
546 }
547 }
548 return $del;
549 }
550
551 /**
552 * Given an array of existing templates, returns those templates which are not in $this
553 * and thus should be deleted.
554 * @param $existing array
555 * @return array
556 */
557 private function getTemplateDeletions( $existing ) {
558 $del = array();
559 foreach ( $existing as $ns => $dbkeys ) {
560 if ( isset( $this->mTemplates[$ns] ) ) {
561 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
562 } else {
563 $del[$ns] = $existing[$ns];
564 }
565 }
566 return $del;
567 }
568
569 /**
570 * Given an array of existing images, returns those images which are not in $this
571 * and thus should be deleted.
572 * @param $existing array
573 * @return array
574 */
575 private function getImageDeletions( $existing ) {
576 return array_diff_key( $existing, $this->mImages );
577 }
578
579 /**
580 * Given an array of existing external links, returns those links which are not
581 * in $this and thus should be deleted.
582 * @param $existing array
583 * @return array
584 */
585 private function getExternalDeletions( $existing ) {
586 return array_diff_key( $existing, $this->mExternals );
587 }
588
589 /**
590 * Given an array of existing categories, returns those categories which are not in $this
591 * and thus should be deleted.
592 * @param $existing array
593 * @return array
594 */
595 private function getCategoryDeletions( $existing ) {
596 return array_diff_assoc( $existing, $this->mCategories );
597 }
598
599 /**
600 * Given an array of existing interlanguage links, returns those links which are not
601 * in $this and thus should be deleted.
602 * @param $existing array
603 * @return array
604 */
605 private function getInterlangDeletions( $existing ) {
606 return array_diff_assoc( $existing, $this->mInterlangs );
607 }
608
609 /**
610 * Get array of properties which should be deleted.
611 * @param $existing array
612 * @return array
613 */
614 function getPropertyDeletions( $existing ) {
615 return array_diff_assoc( $existing, $this->mProperties );
616 }
617
618 /**
619 * Given an array of existing interwiki links, returns those links which are not in $this
620 * and thus should be deleted.
621 * @param $existing array
622 * @return array
623 */
624 private function getInterwikiDeletions( $existing ) {
625 $del = array();
626 foreach ( $existing as $prefix => $dbkeys ) {
627 if ( isset( $this->mInterwikis[$prefix] ) ) {
628 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis[$prefix] );
629 } else {
630 $del[$prefix] = $existing[$prefix];
631 }
632 }
633 return $del;
634 }
635
636 /**
637 * Get an array of existing links, as a 2-D array
638 *
639 * @return array
640 */
641 private function getExistingLinks() {
642 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
643 array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
644 $arr = array();
645 foreach ( $res as $row ) {
646 if ( !isset( $arr[$row->pl_namespace] ) ) {
647 $arr[$row->pl_namespace] = array();
648 }
649 $arr[$row->pl_namespace][$row->pl_title] = 1;
650 }
651 return $arr;
652 }
653
654 /**
655 * Get an array of existing templates, as a 2-D array
656 *
657 * @return array
658 */
659 private function getExistingTemplates() {
660 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
661 array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
662 $arr = array();
663 foreach ( $res as $row ) {
664 if ( !isset( $arr[$row->tl_namespace] ) ) {
665 $arr[$row->tl_namespace] = array();
666 }
667 $arr[$row->tl_namespace][$row->tl_title] = 1;
668 }
669 return $arr;
670 }
671
672 /**
673 * Get an array of existing images, image names in the keys
674 *
675 * @return array
676 */
677 private function getExistingImages() {
678 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
679 array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
680 $arr = array();
681 foreach ( $res as $row ) {
682 $arr[$row->il_to] = 1;
683 }
684 return $arr;
685 }
686
687 /**
688 * Get an array of existing external links, URLs in the keys
689 *
690 * @return array
691 */
692 private function getExistingExternals() {
693 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
694 array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
695 $arr = array();
696 foreach ( $res as $row ) {
697 $arr[$row->el_to] = 1;
698 }
699 return $arr;
700 }
701
702 /**
703 * Get an array of existing categories, with the name in the key and sort key in the value.
704 *
705 * @return array
706 */
707 private function getExistingCategories() {
708 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
709 array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
710 $arr = array();
711 foreach ( $res as $row ) {
712 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
713 }
714 return $arr;
715 }
716
717 /**
718 * Get an array of existing interlanguage links, with the language code in the key and the
719 * title in the value.
720 *
721 * @return array
722 */
723 private function getExistingInterlangs() {
724 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
725 array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
726 $arr = array();
727 foreach ( $res as $row ) {
728 $arr[$row->ll_lang] = $row->ll_title;
729 }
730 return $arr;
731 }
732
733 /**
734 * Get an array of existing inline interwiki links, as a 2-D array
735 * @return array (prefix => array(dbkey => 1))
736 */
737 protected function getExistingInterwikis() {
738 $res = $this->mDb->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
739 array( 'iwl_from' => $this->mId ), __METHOD__, $this->mOptions );
740 $arr = array();
741 foreach ( $res as $row ) {
742 if ( !isset( $arr[$row->iwl_prefix] ) ) {
743 $arr[$row->iwl_prefix] = array();
744 }
745 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
746 }
747 return $arr;
748 }
749
750 /**
751 * Get an array of existing categories, with the name in the key and sort key in the value.
752 *
753 * @return array
754 */
755 private function getExistingProperties() {
756 $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
757 array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
758 $arr = array();
759 foreach ( $res as $row ) {
760 $arr[$row->pp_propname] = $row->pp_value;
761 }
762 return $arr;
763 }
764
765 /**
766 * Return the title object of the page being updated
767 * @return Title
768 */
769 public function getTitle() {
770 return $this->mTitle;
771 }
772
773 /**
774 * Returns parser output
775 * @since 1.19
776 * @return ParserOutput
777 */
778 public function getParserOutput() {
779 return $this->mParserOutput;
780 }
781
782 /**
783 * Return the list of images used as generated by the parser
784 * @return array
785 */
786 public function getImages() {
787 return $this->mImages;
788 }
789
790 /**
791 * Invalidate any necessary link lists related to page property changes
792 * @param $changed
793 */
794 private function invalidateProperties( $changed ) {
795 global $wgPagePropLinkInvalidations;
796
797 foreach ( $changed as $name => $value ) {
798 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
799 $inv = $wgPagePropLinkInvalidations[$name];
800 if ( !is_array( $inv ) ) {
801 $inv = array( $inv );
802 }
803 foreach ( $inv as $table ) {
804 $update = new HTMLCacheUpdate( $this->mTitle, $table );
805 $update->doUpdate();
806 }
807 }
808 }
809 }
810 }
811
812 /**
813 * Update object handling the cleanup of links tables after a page was deleted.
814 **/
815 class LinksDeletionUpdate extends SqlDataUpdate {
816
817 protected $mPage; //!< WikiPage the wikipage that was deleted
818
819 /**
820 * Constructor
821 *
822 * @param $page WikiPage Page we are updating
823 * @throws MWException
824 */
825 function __construct( WikiPage $page ) {
826 parent::__construct( false ); // no implicit transaction
827
828 $this->mPage = $page;
829
830 if ( !$page->exists() ) {
831 throw new MWException( "Page ID not known, perhaps the page doesn't exist?" );
832 }
833 }
834
835 /**
836 * Do some database updates after deletion
837 */
838 public function doUpdate() {
839 $title = $this->mPage->getTitle();
840 $id = $this->mPage->getId();
841
842 # Delete restrictions for it
843 $this->mDb->delete( 'page_restrictions', array ( 'pr_page' => $id ), __METHOD__ );
844
845 # Fix category table counts
846 $cats = array();
847 $res = $this->mDb->select( 'categorylinks', 'cl_to', array( 'cl_from' => $id ), __METHOD__ );
848
849 foreach ( $res as $row ) {
850 $cats [] = $row->cl_to;
851 }
852
853 $this->mPage->updateCategoryCounts( array(), $cats );
854
855 # If using cascading deletes, we can skip some explicit deletes
856 if ( !$this->mDb->cascadingDeletes() ) {
857 $this->mDb->delete( 'revision', array( 'rev_page' => $id ), __METHOD__ );
858
859 # Delete outgoing links
860 $this->mDb->delete( 'pagelinks', array( 'pl_from' => $id ), __METHOD__ );
861 $this->mDb->delete( 'imagelinks', array( 'il_from' => $id ), __METHOD__ );
862 $this->mDb->delete( 'categorylinks', array( 'cl_from' => $id ), __METHOD__ );
863 $this->mDb->delete( 'templatelinks', array( 'tl_from' => $id ), __METHOD__ );
864 $this->mDb->delete( 'externallinks', array( 'el_from' => $id ), __METHOD__ );
865 $this->mDb->delete( 'langlinks', array( 'll_from' => $id ), __METHOD__ );
866 $this->mDb->delete( 'iwlinks', array( 'iwl_from' => $id ), __METHOD__ );
867 $this->mDb->delete( 'redirect', array( 'rd_from' => $id ), __METHOD__ );
868 $this->mDb->delete( 'page_props', array( 'pp_page' => $id ), __METHOD__ );
869 }
870
871 # If using cleanup triggers, we can skip some manual deletes
872 if ( !$this->mDb->cleanupTriggers() ) {
873 # Clean up recentchanges entries...
874 $this->mDb->delete( 'recentchanges',
875 array( 'rc_type != ' . RC_LOG,
876 'rc_namespace' => $title->getNamespace(),
877 'rc_title' => $title->getDBkey() ),
878 __METHOD__ );
879 $this->mDb->delete( 'recentchanges',
880 array( 'rc_type != ' . RC_LOG, 'rc_cur_id' => $id ),
881 __METHOD__ );
882 }
883 }
884
885 /**
886 * Update all the appropriate counts in the category table.
887 * @param $added array associative array of category name => sort key
888 * @param $deleted array associative array of category name => sort key
889 */
890 function updateCategoryCounts( $added, $deleted ) {
891 $a = WikiPage::factory( $this->mTitle );
892 $a->updateCategoryCounts(
893 array_keys( $added ), array_keys( $deleted )
894 );
895 }
896 }