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