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