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