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