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