Revert r37223 "keeping the consistence of the function name string"
[mediawiki.git] / includes / LinksUpdate.php
blobbb192fb945b1affd7571d11d99d06fcc837898b0
1 <?php
2 /**
3 * See docs/deferred.txt
5 * @todo document (e.g. one-sentence top-level class description).
6 */
7 class LinksUpdate {
9 /**@{{
10 * @private
12 var $mId, //!< Page ID of the article linked from
13 $mTitle, //!< Title object of the article linked from
14 $mLinks, //!< Map of title strings to IDs for the links in the document
15 $mImages, //!< DB keys of the images used, in the array key only
16 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
17 $mExternals, //!< URLs of external links, array key only
18 $mCategories, //!< Map of category names to sort keys
19 $mInterlangs, //!< Map of language codes to titles
20 $mProperties, //!< Map of arbitrary name to value
21 $mDb, //!< Database connection reference
22 $mOptions, //!< SELECT options to be used (array)
23 $mRecursive; //!< Whether to queue jobs for recursive updates
24 /**@}}*/
26 /**
27 * Constructor
29 * @param Title $title Title of the page we're updating
30 * @param ParserOutput $parserOutput Output from a full parse of this page
31 * @param bool $recursive Queue jobs for recursive updates?
33 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
34 global $wgAntiLockFlags;
36 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
37 $this->mOptions = array();
38 } else {
39 $this->mOptions = array( 'FOR UPDATE' );
41 $this->mDb = wfGetDB( DB_MASTER );
43 if ( !is_object( $title ) ) {
44 throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
45 "Please see Article::editUpdates() for an invocation example.\n" );
47 $this->mTitle = $title;
48 $this->mId = $title->getArticleID();
50 $this->mParserOutput = $parserOutput;
51 $this->mLinks = $parserOutput->getLinks();
52 $this->mImages = $parserOutput->getImages();
53 $this->mTemplates = $parserOutput->getTemplates();
54 $this->mExternals = $parserOutput->getExternalLinks();
55 $this->mCategories = $parserOutput->getCategories();
56 $this->mProperties = $parserOutput->getProperties();
58 # Convert the format of the interlanguage links
59 # I didn't want to change it in the ParserOutput, because that array is passed all
60 # the way back to the skin, so either a skin API break would be required, or an
61 # inefficient back-conversion.
62 $ill = $parserOutput->getLanguageLinks();
63 $this->mInterlangs = array();
64 foreach ( $ill as $link ) {
65 list( $key, $title ) = explode( ':', $link, 2 );
66 $this->mInterlangs[$key] = $title;
69 $this->mRecursive = $recursive;
71 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
74 /**
75 * Update link tables with outgoing links from an updated article
77 function doUpdate() {
78 global $wgUseDumbLinkUpdate;
80 wfRunHooks( 'LinksUpdate', array( &$this ) );
81 if ( $wgUseDumbLinkUpdate ) {
82 $this->doDumbUpdate();
83 } else {
84 $this->doIncrementalUpdate();
86 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
90 function doIncrementalUpdate() {
91 wfProfileIn( __METHOD__ );
93 # Page links
94 $existing = $this->getExistingLinks();
95 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
96 $this->getLinkInsertions( $existing ) );
98 # Image links
99 $existing = $this->getExistingImages();
101 $imageDeletes = $this->getImageDeletions( $existing );
102 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes, $this->getImageInsertions( $existing ) );
104 # Invalidate all image description pages which had links added or removed
105 $imageUpdates = $imageDeletes + array_diff_key( $this->mImages, $existing );
106 $this->invalidateImageDescriptions( $imageUpdates );
108 # External links
109 $existing = $this->getExistingExternals();
110 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
111 $this->getExternalInsertions( $existing ) );
113 # Language links
114 $existing = $this->getExistingInterlangs();
115 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
116 $this->getInterlangInsertions( $existing ) );
118 # Template links
119 $existing = $this->getExistingTemplates();
120 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
121 $this->getTemplateInsertions( $existing ) );
123 # Category links
124 $existing = $this->getExistingCategories();
126 $categoryDeletes = $this->getCategoryDeletions( $existing );
128 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes, $this->getCategoryInsertions( $existing ) );
130 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
131 $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
132 $categoryUpdates = $categoryInserts + $categoryDeletes;
133 $this->invalidateCategories( $categoryUpdates );
134 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
136 # Page properties
137 $existing = $this->getExistingProperties();
139 $propertiesDeletes = $this->getPropertyDeletions( $existing );
141 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes, $this->getPropertyInsertions( $existing ) );
143 # Invalidate the necessary pages
144 $changed = $propertiesDeletes + array_diff_assoc( $this->mProperties, $existing );
145 $this->invalidateProperties( $changed );
147 # Refresh links of all pages including this page
148 # This will be in a separate transaction
149 if ( $this->mRecursive ) {
150 $this->queueRecursiveJobs();
153 wfProfileOut( __METHOD__ );
157 * Link update which clears the previous entries and inserts new ones
158 * May be slower or faster depending on level of lock contention and write speed of DB
159 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
161 function doDumbUpdate() {
162 wfProfileIn( __METHOD__ );
164 # Refresh category pages and image description pages
165 $existing = $this->getExistingCategories();
166 $categoryInserts = array_diff_assoc( $this->mCategories, $existing );
167 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories );
168 $categoryUpdates = $categoryInserts + $categoryDeletes;
169 $existing = $this->getExistingImages();
170 $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
172 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
173 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
174 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
175 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
176 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
177 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(),'ll_from' );
178 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
180 # Update the cache of all the category pages and image description
181 # pages which were changed, and fix the category table count
182 $this->invalidateCategories( $categoryUpdates );
183 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
184 $this->invalidateImageDescriptions( $imageUpdates );
186 # Refresh links of all pages including this page
187 # This will be in a separate transaction
188 if ( $this->mRecursive ) {
189 $this->queueRecursiveJobs();
192 wfProfileOut( __METHOD__ );
195 function queueRecursiveJobs() {
196 wfProfileIn( __METHOD__ );
198 $batchSize = 100;
199 $dbr = wfGetDB( DB_SLAVE );
200 $res = $dbr->select( array( 'templatelinks', 'page' ),
201 array( 'page_namespace', 'page_title' ),
202 array(
203 'page_id=tl_from',
204 'tl_namespace' => $this->mTitle->getNamespace(),
205 'tl_title' => $this->mTitle->getDBkey()
206 ), __METHOD__
209 $done = false;
210 while ( !$done ) {
211 $jobs = array();
212 for ( $i = 0; $i < $batchSize; $i++ ) {
213 $row = $dbr->fetchObject( $res );
214 if ( !$row ) {
215 $done = true;
216 break;
218 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
219 $jobs[] = new RefreshLinksJob( $title, '' );
221 Job::batchInsert( $jobs );
223 $dbr->freeResult( $res );
224 wfProfileOut( __METHOD__ );
228 * Invalidate the cache of a list of pages from a single namespace
230 * @param integer $namespace
231 * @param array $dbkeys
233 function invalidatePages( $namespace, $dbkeys ) {
234 if ( !count( $dbkeys ) ) {
235 return;
239 * Determine which pages need to be updated
240 * This is necessary to prevent the job queue from smashing the DB with
241 * large numbers of concurrent invalidations of the same page
243 $now = $this->mDb->timestamp();
244 $ids = array();
245 $res = $this->mDb->select( 'page', array( 'page_id' ),
246 array(
247 'page_namespace' => $namespace,
248 'page_title IN (' . $this->mDb->makeList( $dbkeys ) . ')',
249 'page_touched < ' . $this->mDb->addQuotes( $now )
250 ), __METHOD__
252 while ( $row = $this->mDb->fetchObject( $res ) ) {
253 $ids[] = $row->page_id;
255 if ( !count( $ids ) ) {
256 return;
260 * Do the update
261 * We still need the page_touched condition, in case the row has changed since
262 * the non-locking select above.
264 $this->mDb->update( 'page', array( 'page_touched' => $now ),
265 array(
266 'page_id IN (' . $this->mDb->makeList( $ids ) . ')',
267 'page_touched < ' . $this->mDb->addQuotes( $now )
268 ), __METHOD__
272 function invalidateCategories( $cats ) {
273 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
277 * Update all the appropriate counts in the category table.
278 * @param $added associative array of category name => sort key
279 * @param $deleted associative array of category name => sort key
281 function updateCategoryCounts( $added, $deleted ) {
282 $a = new Article($this->mTitle);
283 $a->updateCategoryCounts(
284 array_keys( $added ), array_keys( $deleted )
288 function invalidateImageDescriptions( $images ) {
289 $this->invalidatePages( NS_IMAGE, array_keys( $images ) );
292 function dumbTableUpdate( $table, $insertions, $fromField ) {
293 $this->mDb->delete( $table, array( $fromField => $this->mId ), __METHOD__ );
294 if ( count( $insertions ) ) {
295 # The link array was constructed without FOR UPDATE, so there may
296 # be collisions. This may cause minor link table inconsistencies,
297 # which is better than crippling the site with lock contention.
298 $this->mDb->insert( $table, $insertions, __METHOD__, array( 'IGNORE' ) );
303 * Make a WHERE clause from a 2-d NS/dbkey array
305 * @param array $arr 2-d array indexed by namespace and DB key
306 * @param string $prefix Field name prefix, without the underscore
308 function makeWhereFrom2d( &$arr, $prefix ) {
309 $lb = new LinkBatch;
310 $lb->setArray( $arr );
311 return $lb->constructSet( $prefix, $this->mDb );
315 * Update a table by doing a delete query then an insert query
316 * @private
318 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
319 if ( $table == 'page_props' ) {
320 $fromField = 'pp_page';
321 } else {
322 $fromField = "{$prefix}_from";
324 $where = array( $fromField => $this->mId );
325 if ( $table == 'pagelinks' || $table == 'templatelinks' ) {
326 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
327 if ( $clause ) {
328 $where[] = $clause;
329 } else {
330 $where = false;
332 } else {
333 if ( $table == 'langlinks' ) {
334 $toField = 'll_lang';
335 } elseif ( $table == 'page_props' ) {
336 $toField = 'pp_propname';
337 } else {
338 $toField = $prefix . '_to';
340 if ( count( $deletions ) ) {
341 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
342 } else {
343 $where = false;
346 if ( $where ) {
347 $this->mDb->delete( $table, $where, __METHOD__ );
349 if ( count( $insertions ) ) {
350 $this->mDb->insert( $table, $insertions, __METHOD__, 'IGNORE' );
356 * Get an array of pagelinks insertions for passing to the DB
357 * Skips the titles specified by the 2-D array $existing
358 * @private
360 function getLinkInsertions( $existing = array() ) {
361 $arr = array();
362 foreach( $this->mLinks as $ns => $dbkeys ) {
363 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
364 # in GlobalFunctions.php
365 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
366 foreach ( $diffs as $dbk => $id ) {
367 $arr[] = array(
368 'pl_from' => $this->mId,
369 'pl_namespace' => $ns,
370 'pl_title' => $dbk
374 return $arr;
378 * Get an array of template insertions. Like getLinkInsertions()
379 * @private
381 function getTemplateInsertions( $existing = array() ) {
382 $arr = array();
383 foreach( $this->mTemplates as $ns => $dbkeys ) {
384 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
385 foreach ( $diffs as $dbk => $id ) {
386 $arr[] = array(
387 'tl_from' => $this->mId,
388 'tl_namespace' => $ns,
389 'tl_title' => $dbk
393 return $arr;
397 * Get an array of image insertions
398 * Skips the names specified in $existing
399 * @private
401 function getImageInsertions( $existing = array() ) {
402 $arr = array();
403 $diffs = array_diff_key( $this->mImages, $existing );
404 foreach( $diffs as $iname => $dummy ) {
405 $arr[] = array(
406 'il_from' => $this->mId,
407 'il_to' => $iname
410 return $arr;
414 * Get an array of externallinks insertions. Skips the names specified in $existing
415 * @private
417 function getExternalInsertions( $existing = array() ) {
418 $arr = array();
419 $diffs = array_diff_key( $this->mExternals, $existing );
420 foreach( $diffs as $url => $dummy ) {
421 $arr[] = array(
422 'el_from' => $this->mId,
423 'el_to' => $url,
424 'el_index' => wfMakeUrlIndex( $url ),
427 return $arr;
431 * Get an array of category insertions
432 * @param array $existing Array mapping existing category names to sort keys. If both
433 * match a link in $this, the link will be omitted from the output
434 * @private
436 function getCategoryInsertions( $existing = array() ) {
437 $diffs = array_diff_assoc( $this->mCategories, $existing );
438 $arr = array();
439 foreach ( $diffs as $name => $sortkey ) {
440 $arr[] = array(
441 'cl_from' => $this->mId,
442 'cl_to' => $name,
443 'cl_sortkey' => $sortkey,
444 'cl_timestamp' => $this->mDb->timestamp()
447 return $arr;
451 * Get an array of interlanguage link insertions
452 * @param array $existing Array mapping existing language codes to titles
453 * @private
455 function getInterlangInsertions( $existing = array() ) {
456 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
457 $arr = array();
458 foreach( $diffs as $lang => $title ) {
459 $arr[] = array(
460 'll_from' => $this->mId,
461 'll_lang' => $lang,
462 'll_title' => $title
465 return $arr;
469 * Get an array of page property insertions
471 function getPropertyInsertions( $existing = array() ) {
472 $diffs = array_diff_assoc( $this->mProperties, $existing );
473 $arr = array();
474 foreach ( $diffs as $name => $value ) {
475 $arr[] = array(
476 'pp_page' => $this->mId,
477 'pp_propname' => $name,
478 'pp_value' => $value,
481 return $arr;
486 * Given an array of existing links, returns those links which are not in $this
487 * and thus should be deleted.
488 * @private
490 function getLinkDeletions( $existing ) {
491 $del = array();
492 foreach ( $existing as $ns => $dbkeys ) {
493 if ( isset( $this->mLinks[$ns] ) ) {
494 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
495 } else {
496 $del[$ns] = $existing[$ns];
499 return $del;
503 * Given an array of existing templates, returns those templates which are not in $this
504 * and thus should be deleted.
505 * @private
507 function getTemplateDeletions( $existing ) {
508 $del = array();
509 foreach ( $existing as $ns => $dbkeys ) {
510 if ( isset( $this->mTemplates[$ns] ) ) {
511 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
512 } else {
513 $del[$ns] = $existing[$ns];
516 return $del;
520 * Given an array of existing images, returns those images which are not in $this
521 * and thus should be deleted.
522 * @private
524 function getImageDeletions( $existing ) {
525 return array_diff_key( $existing, $this->mImages );
529 * Given an array of existing external links, returns those links which are not
530 * in $this and thus should be deleted.
531 * @private
533 function getExternalDeletions( $existing ) {
534 return array_diff_key( $existing, $this->mExternals );
538 * Given an array of existing categories, returns those categories which are not in $this
539 * and thus should be deleted.
540 * @private
542 function getCategoryDeletions( $existing ) {
543 return array_diff_assoc( $existing, $this->mCategories );
547 * Given an array of existing interlanguage links, returns those links which are not
548 * in $this and thus should be deleted.
549 * @private
551 function getInterlangDeletions( $existing ) {
552 return array_diff_assoc( $existing, $this->mInterlangs );
556 * Get array of properties which should be deleted.
557 * @private
559 function getPropertyDeletions( $existing ) {
560 return array_diff_assoc( $existing, $this->mProperties );
564 * Get an array of existing links, as a 2-D array
565 * @private
567 function getExistingLinks() {
568 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
569 array( 'pl_from' => $this->mId ), __METHOD__, $this->mOptions );
570 $arr = array();
571 while ( $row = $this->mDb->fetchObject( $res ) ) {
572 if ( !isset( $arr[$row->pl_namespace] ) ) {
573 $arr[$row->pl_namespace] = array();
575 $arr[$row->pl_namespace][$row->pl_title] = 1;
577 $this->mDb->freeResult( $res );
578 return $arr;
582 * Get an array of existing templates, as a 2-D array
583 * @private
585 function getExistingTemplates() {
586 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
587 array( 'tl_from' => $this->mId ), __METHOD__, $this->mOptions );
588 $arr = array();
589 while ( $row = $this->mDb->fetchObject( $res ) ) {
590 if ( !isset( $arr[$row->tl_namespace] ) ) {
591 $arr[$row->tl_namespace] = array();
593 $arr[$row->tl_namespace][$row->tl_title] = 1;
595 $this->mDb->freeResult( $res );
596 return $arr;
600 * Get an array of existing images, image names in the keys
601 * @private
603 function getExistingImages() {
604 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
605 array( 'il_from' => $this->mId ), __METHOD__, $this->mOptions );
606 $arr = array();
607 while ( $row = $this->mDb->fetchObject( $res ) ) {
608 $arr[$row->il_to] = 1;
610 $this->mDb->freeResult( $res );
611 return $arr;
615 * Get an array of existing external links, URLs in the keys
616 * @private
618 function getExistingExternals() {
619 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
620 array( 'el_from' => $this->mId ), __METHOD__, $this->mOptions );
621 $arr = array();
622 while ( $row = $this->mDb->fetchObject( $res ) ) {
623 $arr[$row->el_to] = 1;
625 $this->mDb->freeResult( $res );
626 return $arr;
630 * Get an array of existing categories, with the name in the key and sort key in the value.
631 * @private
633 function getExistingCategories() {
634 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
635 array( 'cl_from' => $this->mId ), __METHOD__, $this->mOptions );
636 $arr = array();
637 while ( $row = $this->mDb->fetchObject( $res ) ) {
638 $arr[$row->cl_to] = $row->cl_sortkey;
640 $this->mDb->freeResult( $res );
641 return $arr;
645 * Get an array of existing interlanguage links, with the language code in the key and the
646 * title in the value.
647 * @private
649 function getExistingInterlangs() {
650 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
651 array( 'll_from' => $this->mId ), __METHOD__, $this->mOptions );
652 $arr = array();
653 while ( $row = $this->mDb->fetchObject( $res ) ) {
654 $arr[$row->ll_lang] = $row->ll_title;
656 return $arr;
660 * Get an array of existing categories, with the name in the key and sort key in the value.
661 * @private
663 function getExistingProperties() {
664 $res = $this->mDb->select( 'page_props', array( 'pp_propname', 'pp_value' ),
665 array( 'pp_page' => $this->mId ), __METHOD__, $this->mOptions );
666 $arr = array();
667 while ( $row = $this->mDb->fetchObject( $res ) ) {
668 $arr[$row->pp_propname] = $row->pp_value;
670 $this->mDb->freeResult( $res );
671 return $arr;
676 * Return the title object of the page being updated
678 function getTitle() {
679 return $this->mTitle;
683 * Invalidate any necessary link lists related to page property changes
685 function invalidateProperties( $changed ) {
686 global $wgPagePropLinkInvalidations;
688 foreach ( $changed as $name => $value ) {
689 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
690 $inv = $wgPagePropLinkInvalidations[$name];
691 if ( !is_array( $inv ) ) {
692 $inv = array( $inv );
694 foreach ( $inv as $table ) {
695 $update = new HTMLCacheUpdate( $this->mTitle, $table );
696 $update->doUpdate();