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