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