Converted patrol log to the new system
[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 $wgGlobalDatabase;
159 if ( $wgGlobalDatabase ) {
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 'table' => 'templatelinks',
256 'start' => $start,
257 'end' => $end,
258 );
259 $jobs[] = new RefreshLinksJob2( $this->mTitle, $params );
260 }
261 Job::batchInsert( $jobs );
262
263 wfProfileOut( __METHOD__ );
264 }
265
266 /**
267 * Invalidate the cache of a list of pages from a single namespace
268 *
269 * @param $namespace Integer
270 * @param $dbkeys Array
271 */
272 function invalidatePages( $namespace, $dbkeys ) {
273 if ( !count( $dbkeys ) ) {
274 return;
275 }
276
277 /**
278 * Determine which pages need to be updated
279 * This is necessary to prevent the job queue from smashing the DB with
280 * large numbers of concurrent invalidations of the same page
281 */
282 $now = $this->mDb->timestamp();
283 $ids = array();
284 $res = $this->mDb->select( 'page', array( 'page_id' ),
285 array(
286 'page_namespace' => $namespace,
287 'page_title IN (' . $this->mDb->makeList( $dbkeys ) . ')',
288 'page_touched < ' . $this->mDb->addQuotes( $now )
289 ), __METHOD__
290 );
291 foreach ( $res as $row ) {
292 $ids[] = $row->page_id;
293 }
294 if ( !count( $ids ) ) {
295 return;
296 }
297
298 /**
299 * Do the update
300 * We still need the page_touched condition, in case the row has changed since
301 * the non-locking select above.
302 */
303 $this->mDb->update( 'page', array( 'page_touched' => $now ),
304 array(
305 'page_id IN (' . $this->mDb->makeList( $ids ) . ')',
306 'page_touched < ' . $this->mDb->addQuotes( $now )
307 ), __METHOD__
308 );
309 }
310
311 function invalidateCategories( $cats ) {
312 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
313 }
314
315 /**
316 * Update all the appropriate counts in the category table.
317 * @param $added associative array of category name => sort key
318 * @param $deleted associative array of category name => sort key
319 */
320 function updateCategoryCounts( $added, $deleted ) {
321 $a = new Article($this->mTitle);
322 $a->updateCategoryCounts(
323 array_keys( $added ), array_keys( $deleted )
324 );
325 }
326
327 function invalidateImageDescriptions( $images ) {
328 $this->invalidatePages( NS_FILE, array_keys( $images ) );
329 }
330
331 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 * @private
344 */
345 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
346 if ( $table == 'page_props' ) {
347 $fromField = 'pp_page';
348 } else {
349 $fromField = "{$prefix}_from";
350 }
351 $where = array( $fromField => $this->mId );
352 if ( $table == 'pagelinks' || $table == 'templatelinks' || $table == 'iwlinks' ) {
353 if ( $table == 'iwlinks' ) {
354 $baseKey = 'iwl_prefix';
355 } else {
356 $baseKey = "{$prefix}_namespace";
357 }
358 $clause = $this->mDb->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
359 if ( $clause ) {
360 $where[] = $clause;
361 } else {
362 $where = false;
363 }
364 } else {
365 if ( $table == 'langlinks' ) {
366 $toField = 'll_lang';
367 } elseif ( $table == 'page_props' ) {
368 $toField = 'pp_propname';
369 } else {
370 $toField = $prefix . '_to';
371 }
372 if ( count( $deletions ) ) {
373 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
374 } else {
375 $where = false;
376 }
377 }
378 if ( $where ) {
379 $this->mDb->delete( $table, $where, __METHOD__ );
380 }
381 if ( isset( $insertions['globaltemplatelinks'] ) ) {
382 $this->mDb->insert( 'globaltemplatelinks', $insertions['globaltemplatelinks'], __METHOD__, 'IGNORE' );
383 unset( $insertions['globaltemplatelinks'] );
384 }
385 if ( isset( $insertions['globalnamespaces'] ) ) {
386 $this->mDb->insert( 'globalnamespaces', $insertions['globalnamespaces'], __METHOD__, 'IGNORE' );
387 unset( $insertions['globalnamespaces'] );
388 }
389 if ( isset( $insertions['globalinterwiki'] ) ) {
390 $this->mDb->insert( 'globalinterwiki', $insertions['globalinterwiki'], __METHOD__, 'IGNORE' );
391 unset( $insertions['globalinterwiki'] );
392 }
393 if ( count( $insertions ) ) {
394 $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
395 }
396 }
397
398 /**
399 * Update a shared table by doing a delete query then an insert query
400 * @private
401 */
402 function incrSharedTableUpdate( $table, $prefix, $deletions, $insertions ) {
403 global $wgGlobalDatabase;
404
405 if ( $wgGlobalDatabase ) {
406 $dbw = wfGetDB( DB_MASTER, array(), $wgGlobalDatabase );
407 $where = array( "{$prefix}_from_wiki" => wfWikiID(),
408 "{$prefix}_from_page" => $this->mId
409 );
410 $baseKey = "{$prefix}_to_wiki";
411 $middleKey = "{$prefix}_to_namespace";
412
413 $clause = $dbw->makeWhereFrom3d( $deletions, $baseKey, $middleKey, "{$prefix}_to_title" );
414 if ( $clause ) {
415 $where[] = $clause;
416 } else {
417 $where = false;
418 }
419
420 if ( $where ) {
421 $dbw->delete( $table, $where, __METHOD__ );
422 }
423 if ( count( $insertions ) ) {
424 $dbw->insert( $table, $insertions, __METHOD__, 'IGNORE' );
425 }
426 }
427 }
428
429 /**
430 * Get an array of pagelinks insertions for passing to the DB
431 * Skips the titles specified by the 2-D array $existing
432 * @private
433 */
434 function getLinkInsertions( $existing = array() ) {
435 $arr = array();
436 foreach( $this->mLinks as $ns => $dbkeys ) {
437 $diffs = isset( $existing[$ns] )
438 ? array_diff_key( $dbkeys, $existing[$ns] )
439 : $dbkeys;
440 foreach ( $diffs as $dbk => $id ) {
441 $arr[] = array(
442 'pl_from' => $this->mId,
443 'pl_namespace' => $ns,
444 'pl_title' => $dbk
445 );
446 }
447 }
448 return $arr;
449 }
450
451 /**
452 * Get an array of template insertions. Like getLinkInsertions()
453 * @private
454 */
455 function getTemplateInsertions( $existing = array() ) {
456 $arr = array();
457 foreach( $this->mTemplates as $ns => $dbkeys ) {
458 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
459 foreach ( $diffs as $dbk => $id ) {
460 $arr[] = array(
461 'tl_from' => $this->mId,
462 'tl_namespace' => $ns,
463 'tl_title' => $dbk
464 );
465 }
466 }
467 return $arr;
468 }
469
470 /**
471 * Get an array of distant template insertions. Like getLinkInsertions()
472 * @private
473 */
474 function getDistantTemplateInsertions( $existing = array() ) {
475 $arr = array();
476 foreach( $this->mDistantTemplates as $prefix => $templatesToNS ) {
477 foreach( $templatesToNS as $ns => $dbkeys ) {
478 $diffs = isset( $existing[$prefix] ) && isset( $existing[$prefix][$ns] )
479 ? array_diff_key( $dbkeys, $existing[$prefix][$ns] )
480 : $dbkeys;
481 $interwiki = Interwiki::fetch( $prefix );
482 $wikiid = $interwiki->getWikiID();
483 foreach ( $diffs as $dbk => $id ) {
484 $arr['globaltemplatelinks'][] = array(
485 'gtl_from_wiki' => wfWikiID(),
486 'gtl_from_page' => $this->mId,
487 'gtl_from_namespace' => $this->mTitle->getNamespace(),
488 'gtl_from_title' => $this->mTitle->getText(),
489 'gtl_to_wiki' => $wikiid,
490 'gtl_to_namespace' => $ns,
491 'gtl_to_title' => $dbk
492 );
493 $arr['globalinterwiki'][] = array(
494 'giw_wikiid' => $wikiid,
495 'giw_prefix' => $prefix,
496 );
497 $arr['globalnamespaces'][] = array(
498 'gn_wiki' => wfWikiID( ),
499 'gn_namespace' => $this->mTitle->getNamespace(),
500 'gn_namespacetext' => $this->mTitle->getNsText(),
501 );
502 }
503 }
504 }
505 return $arr;
506 }
507
508 /**
509 * Get an array of image insertions
510 * Skips the names specified in $existing
511 * @private
512 */
513 function getImageInsertions( $existing = array() ) {
514 $arr = array();
515 $diffs = array_diff_key( $this->mImages, $existing );
516 foreach( $diffs as $iname => $dummy ) {
517 $arr[] = array(
518 'il_from' => $this->mId,
519 'il_to' => $iname
520 );
521 }
522 return $arr;
523 }
524
525 /**
526 * Get an array of externallinks insertions. Skips the names specified in $existing
527 * @private
528 */
529 function getExternalInsertions( $existing = array() ) {
530 $arr = array();
531 $diffs = array_diff_key( $this->mExternals, $existing );
532 foreach( $diffs as $url => $dummy ) {
533 $arr[] = array(
534 'el_from' => $this->mId,
535 'el_to' => $url,
536 'el_index' => wfMakeUrlIndex( $url ),
537 );
538 }
539 return $arr;
540 }
541
542 /**
543 * Get an array of category insertions
544 *
545 * @param $existing Array mapping existing category names to sort keys. If both
546 * match a link in $this, the link will be omitted from the output
547 * @private
548 */
549 function getCategoryInsertions( $existing = array() ) {
550 global $wgContLang, $wgCategoryCollation;
551 $diffs = array_diff_assoc( $this->mCategories, $existing );
552 $arr = array();
553 foreach ( $diffs as $name => $prefix ) {
554 $nt = Title::makeTitleSafe( NS_CATEGORY, $name );
555 $wgContLang->findVariantLink( $name, $nt, true );
556
557 if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
558 $type = 'subcat';
559 } elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
560 $type = 'file';
561 } else {
562 $type = 'page';
563 }
564
565 # Treat custom sortkeys as a prefix, so that if multiple
566 # things are forced to sort as '*' or something, they'll
567 # sort properly in the category rather than in page_id
568 # order or such.
569 $sortkey = Collation::singleton()->getSortKey(
570 $this->mTitle->getCategorySortkey( $prefix ) );
571
572 $arr[] = array(
573 'cl_from' => $this->mId,
574 'cl_to' => $name,
575 'cl_sortkey' => $sortkey,
576 'cl_timestamp' => $this->mDb->timestamp(),
577 'cl_sortkey_prefix' => $prefix,
578 'cl_collation' => $wgCategoryCollation,
579 'cl_type' => $type,
580 );
581 }
582 return $arr;
583 }
584
585 /**
586 * Get an array of interlanguage link insertions
587 *
588 * @param $existing Array mapping existing language codes to titles
589 * @private
590 */
591 function getInterlangInsertions( $existing = array() ) {
592 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
593 $arr = array();
594 foreach( $diffs as $lang => $title ) {
595 $arr[] = array(
596 'll_from' => $this->mId,
597 'll_lang' => $lang,
598 'll_title' => $title
599 );
600 }
601 return $arr;
602 }
603
604 /**
605 * Get an array of page property insertions
606 */
607 function getPropertyInsertions( $existing = array() ) {
608 $diffs = array_diff_assoc( $this->mProperties, $existing );
609 $arr = array();
610 foreach ( $diffs as $name => $value ) {
611 $arr[] = array(
612 'pp_page' => $this->mId,
613 'pp_propname' => $name,
614 'pp_value' => $value,
615 );
616 }
617 return $arr;
618 }
619
620 /**
621 * Get an array of interwiki insertions for passing to the DB
622 * Skips the titles specified by the 2-D array $existing
623 * @private
624 */
625 function getInterwikiInsertions( $existing = array() ) {
626 $arr = array();
627 foreach( $this->mInterwikis as $prefix => $dbkeys ) {
628 $diffs = isset( $existing[$prefix] ) ? array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
629 foreach ( $diffs as $dbk => $id ) {
630 $arr[] = array(
631 'iwl_from' => $this->mId,
632 'iwl_prefix' => $prefix,
633 'iwl_title' => $dbk
634 );
635 }
636 }
637 return $arr;
638 }
639
640 /**
641 * Given an array of existing links, returns those links which are not in $this
642 * and thus should be deleted.
643 * @private
644 */
645 function getLinkDeletions( $existing ) {
646 $del = array();
647 foreach ( $existing as $ns => $dbkeys ) {
648 if ( isset( $this->mLinks[$ns] ) ) {
649 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
650 } else {
651 $del[$ns] = $existing[$ns];
652 }
653 }
654 return $del;
655 }
656
657 /**
658 * Given an array of existing templates, returns those templates which are not in $this
659 * and thus should be deleted.
660 * @private
661 */
662 function getTemplateDeletions( $existing ) {
663 $del = array();
664 foreach ( $existing as $ns => $dbkeys ) {
665 if ( isset( $this->mTemplates[$ns] ) ) {
666 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
667 } else {
668 $del[$ns] = $existing[$ns];
669 }
670 }
671 return $del;
672 }
673
674 /**
675 * Given an array of existing templates, returns those templates which are not in $this
676 * and thus should be deleted.
677 * @private
678 */
679 function getDistantTemplateDeletions( $existing ) {
680 $del = array();
681 foreach ( $existing as $wikiid => $templatesForNS ) {
682 if ( isset( $this->mDistantTemplates[$wikiid] ) ) {
683 $del[$wikiid] = array_diff_key( $existing[$wikiid], $this->mDistantTemplates[$wikiid] );
684 } else {
685 $del[$wikiid] = $existing[$wikiid];
686 }
687 foreach ( $templatesForNS as $ns => $dbkeys ) {
688 if ( isset( $this->mDistantTemplates[$wikiid][$ns] ) ) {
689 $del[$wikiid][$ns] = array_diff_key( $existing[$wikiid][$ns], $this->mDistantTemplates[$wikiid][$ns] );
690 } else {
691 $del[$wikiid][$ns] = $existing[$wikiid][$ns];
692 }
693 }
694 }
695 return $del;
696 }
697
698 /**
699 * Given an array of existing images, returns those images which are not in $this
700 * and thus should be deleted.
701 * @private
702 */
703 function getImageDeletions( $existing ) {
704 return array_diff_key( $existing, $this->mImages );
705 }
706
707 /**
708 * Given an array of existing external links, returns those links which are not
709 * in $this and thus should be deleted.
710 * @private
711 */
712 function getExternalDeletions( $existing ) {
713 return array_diff_key( $existing, $this->mExternals );
714 }
715
716 /**
717 * Given an array of existing categories, returns those categories which are not in $this
718 * and thus should be deleted.
719 * @private
720 */
721 function getCategoryDeletions( $existing ) {
722 return array_diff_assoc( $existing, $this->mCategories );
723 }
724
725 /**
726 * Given an array of existing interlanguage links, returns those links which are not
727 * in $this and thus should be deleted.
728 * @private
729 */
730 function getInterlangDeletions( $existing ) {
731 return array_diff_assoc( $existing, $this->mInterlangs );
732 }
733
734 /**
735 * Get array of properties which should be deleted.
736 * @private
737 */
738 function getPropertyDeletions( $existing ) {
739 return array_diff_assoc( $existing, $this->mProperties );
740 }
741
742 /**
743 * Given an array of existing interwiki links, returns those links which are not in $this
744 * and thus should be deleted.
745 * @private
746 */
747 function getInterwikiDeletions( $existing ) {
748 $del = array();
749 foreach ( $existing as $prefix => $dbkeys ) {
750 if ( isset( $this->mInterwikis[$prefix] ) ) {
751 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis[$prefix] );
752 } else {
753 $del[$prefix] = $existing[$prefix];
754 }
755 }
756 return $del;
757 }
758
759 /**
760 * Get an array of existing links, as a 2-D array
761 * @private
762 */
763 function getExistingLinks() {
764 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
765 array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
766 $arr = array();
767 foreach ( $res as $row ) {
768 if ( !isset( $arr[$row->pl_namespace] ) ) {
769 $arr[$row->pl_namespace] = array();
770 }
771 $arr[$row->pl_namespace][$row->pl_title] = 1;
772 }
773 return $arr;
774 }
775
776 /**
777 * Get an array of existing templates, as a 2-D array
778 * @private
779 */
780 function getExistingTemplates() {
781 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
782 array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
783 $arr = array();
784 foreach ( $res as $row ) {
785 if ( !isset( $arr[$row->tl_namespace] ) ) {
786 $arr[$row->tl_namespace] = array();
787 }
788 $arr[$row->tl_namespace][$row->tl_title] = 1;
789 }
790 return $arr;
791 }
792
793 /**
794 * Get an array of existing distant templates, as a 3-D array
795 * @private
796 */
797 function getDistantExistingTemplates() {
798 global $wgGlobalDatabase;
799
800 $arr = array();
801 if ( $wgGlobalDatabase ) {
802 $dbr = wfGetDB( DB_SLAVE, array(), $wgGlobalDatabase );
803 $res = $dbr->select(
804 'globaltemplatelinks',
805 array( 'gtl_to_wiki', 'gtl_to_namespace', 'gtl_to_title' ),
806 array( 'gtl_from_wiki' => wfWikiID(), 'gtl_from_page' => $this->mId ),
807 __METHOD__,
808 $this->mOptions
809 );
810 foreach ( $res as $row ) {
811 if ( !isset( $arr[$row->gtl_to_wiki] ) ) {
812 $arr[$row->gtl_to_wiki] = array();
813 }
814 if ( !isset( $arr[$row->gtl_to_wiki][$row->gtl_to_namespace] ) ) {
815 $arr[$row->gtl_to_wiki][$row->gtl_to_namespace] = array();
816 }
817 $arr[$row->gtl_to_wiki][$row->gtl_to_namespace][$row->gtl_to_title] = 1;
818 }
819 }
820 return $arr;
821 }
822
823 /**
824 * Get an array of existing images, image names in the keys
825 * @private
826 */
827 function getExistingImages() {
828 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
829 array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
830 $arr = array();
831 foreach ( $res as $row ) {
832 $arr[$row->il_to] = 1;
833 }
834 return $arr;
835 }
836
837 /**
838 * Get an array of existing external links, URLs in the keys
839 * @private
840 */
841 function getExistingExternals() {
842 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
843 array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
844 $arr = array();
845 foreach ( $res as $row ) {
846 $arr[$row->el_to] = 1;
847 }
848 return $arr;
849 }
850
851 /**
852 * Get an array of existing categories, with the name in the key and sort key in the value.
853 * @private
854 */
855 function getExistingCategories() {
856 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
857 array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
858 $arr = array();
859 foreach ( $res as $row ) {
860 $arr[$row->cl_to] = $row->cl_sortkey_prefix;
861 }
862 return $arr;
863 }
864
865 /**
866 * Get an array of existing interlanguage links, with the language code in the key and the
867 * title in the value.
868 * @private
869 */
870 function getExistingInterlangs() {
871 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
872 array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
873 $arr = array();
874 foreach ( $res as $row ) {
875 $arr[$row->ll_lang] = $row->ll_title;
876 }
877 return $arr;
878 }
879
880 /**
881 * Get an array of existing inline interwiki links, as a 2-D array
882 * @return array (prefix => array(dbkey => 1))
883 */
884 protected function getExistingInterwikis() {
885 $res = $this->mDb->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
886 array( 'iwl_from' => $this->mId ), __METHOD__, $this->mOptions );
887 $arr = array();
888 foreach ( $res as $row ) {
889 if ( !isset( $arr[$row->iwl_prefix] ) ) {
890 $arr[$row->iwl_prefix] = array();
891 }
892 $arr[$row->iwl_prefix][$row->iwl_title] = 1;
893 }
894 return $arr;
895 }
896
897 /**
898 * Get an array of existing categories, with the name in the key and sort key in the value.
899 * @private
900 */
901 function getExistingProperties() {
902 $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
903 array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
904 $arr = array();
905 foreach ( $res as $row ) {
906 $arr[$row->pp_propname] = $row->pp_value;
907 }
908 return $arr;
909 }
910
911
912 /**
913 * Return the title object of the page being updated
914 */
915 function getTitle() {
916 return $this->mTitle;
917 }
918
919 /**
920 * Return the list of images used as generated by the parser
921 */
922 public function getImages() {
923 return $this->mImages;
924 }
925
926 /**
927 * Invalidate any necessary link lists related to page property changes
928 */
929 function invalidateProperties( $changed ) {
930 global $wgPagePropLinkInvalidations;
931
932 foreach ( $changed as $name => $value ) {
933 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
934 $inv = $wgPagePropLinkInvalidations[$name];
935 if ( !is_array( $inv ) ) {
936 $inv = array( $inv );
937 }
938 foreach ( $inv as $table ) {
939 $update = new HTMLCacheUpdate( $this->mTitle, $table );
940 $update->doUpdate();
941 }
942 }
943 }
944 }
945 }