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