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