3 * See docs/deferred.txt
5 * @todo document (e.g. one-sentence top-level class description).
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 $mDb, //!< Database connection reference
21 $mOptions, //!< SELECT options to be used (array)
22 $mRecursive; //!< Whether to queue jobs for recursive updates
28 * @param Title $title Title of the page we're updating
29 * @param ParserOutput $parserOutput Output from a full parse of this page
30 * @param bool $recursive Queue jobs for recursive updates?
32 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
33 global $wgAntiLockFlags;
35 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
36 $this->mOptions
= array();
38 $this->mOptions
= array( 'FOR UPDATE' );
40 $this->mDb
= wfGetDB( DB_MASTER
);
42 if ( !is_object( $title ) ) {
43 throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
44 "Please see Article::editUpdates() for an invocation example.\n" );
46 $this->mTitle
= $title;
47 $this->mId
= $title->getArticleID();
49 $this->mLinks
= $parserOutput->getLinks();
50 $this->mImages
= $parserOutput->getImages();
51 $this->mTemplates
= $parserOutput->getTemplates();
52 $this->mExternals
= $parserOutput->getExternalLinks();
53 $this->mCategories
= $parserOutput->getCategories();
55 # Convert the format of the interlanguage links
56 # I didn't want to change it in the ParserOutput, because that array is passed all
57 # the way back to the skin, so either a skin API break would be required, or an
58 # inefficient back-conversion.
59 $ill = $parserOutput->getLanguageLinks();
60 $this->mInterlangs
= array();
61 foreach ( $ill as $link ) {
62 list( $key, $title ) = explode( ':', $link, 2 );
63 $this->mInterlangs
[$key] = $title;
66 $this->mRecursive
= $recursive;
68 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
72 * Update link tables with outgoing links from an updated article
75 global $wgUseDumbLinkUpdate;
77 wfRunHooks( 'LinksUpdate', array( &$this ) );
78 if ( $wgUseDumbLinkUpdate ) {
79 $this->doDumbUpdate();
81 $this->doIncrementalUpdate();
83 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
87 function doIncrementalUpdate() {
88 $fname = 'LinksUpdate::doIncrementalUpdate';
89 wfProfileIn( $fname );
92 $existing = $this->getExistingLinks();
93 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
94 $this->getLinkInsertions( $existing ) );
97 $existing = $this->getExistingImages();
98 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
99 $this->getImageInsertions( $existing ) );
101 # Invalidate all image description pages which had links added or removed
102 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
103 $this->invalidateImageDescriptions( $imageUpdates );
106 $existing = $this->getExistingExternals();
107 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
108 $this->getExternalInsertions( $existing ) );
111 $existing = $this->getExistingInterlangs();
112 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
113 $this->getInterlangInsertions( $existing ) );
116 $existing = $this->getExistingTemplates();
117 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
118 $this->getTemplateInsertions( $existing ) );
121 $existing = $this->getExistingCategories();
122 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
123 $this->getCategoryInsertions( $existing ) );
125 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
126 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
127 $this->invalidateCategories( $categoryUpdates );
129 # Refresh links of all pages including this page
130 # This will be in a separate transaction
131 if ( $this->mRecursive
) {
132 $this->queueRecursiveJobs();
135 wfProfileOut( $fname );
139 * Link update which clears the previous entries and inserts new ones
140 * May be slower or faster depending on level of lock contention and write speed of DB
141 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
143 function doDumbUpdate() {
144 $fname = 'LinksUpdate::doDumbUpdate';
145 wfProfileIn( $fname );
147 # Refresh category pages and image description pages
148 $existing = $this->getExistingCategories();
149 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
150 $existing = $this->getExistingImages();
151 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
153 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
154 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
155 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
156 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
157 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
158 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
160 # Update the cache of all the category pages and image description pages which were changed
161 $this->invalidateCategories( $categoryUpdates );
162 $this->invalidateImageDescriptions( $imageUpdates );
164 # Refresh links of all pages including this page
165 # This will be in a separate transaction
166 if ( $this->mRecursive
) {
167 $this->queueRecursiveJobs();
170 wfProfileOut( $fname );
173 function queueRecursiveJobs() {
174 wfProfileIn( __METHOD__
);
177 $dbr = wfGetDB( DB_SLAVE
);
178 $res = $dbr->select( array( 'templatelinks', 'page' ),
179 array( 'page_namespace', 'page_title' ),
182 'tl_namespace' => $this->mTitle
->getNamespace(),
183 'tl_title' => $this->mTitle
->getDBkey()
190 for ( $i = 0; $i < $batchSize; $i++
) {
191 $row = $dbr->fetchObject( $res );
196 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
197 $jobs[] = new RefreshLinksJob( $title, '' );
199 Job
::batchInsert( $jobs );
201 $dbr->freeResult( $res );
202 wfProfileOut( __METHOD__
);
206 * Invalidate the cache of a list of pages from a single namespace
208 * @param integer $namespace
209 * @param array $dbkeys
211 function invalidatePages( $namespace, $dbkeys ) {
212 $fname = 'LinksUpdate::invalidatePages';
214 if ( !count( $dbkeys ) ) {
219 * Determine which pages need to be updated
220 * This is necessary to prevent the job queue from smashing the DB with
221 * large numbers of concurrent invalidations of the same page
223 $now = $this->mDb
->timestamp();
225 $res = $this->mDb
->select( 'page', array( 'page_id' ),
227 'page_namespace' => $namespace,
228 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
229 'page_touched < ' . $this->mDb
->addQuotes( $now )
232 while ( $row = $this->mDb
->fetchObject( $res ) ) {
233 $ids[] = $row->page_id
;
235 if ( !count( $ids ) ) {
241 * We still need the page_touched condition, in case the row has changed since
242 * the non-locking select above.
244 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
246 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
247 'page_touched < ' . $this->mDb
->addQuotes( $now )
252 function invalidateCategories( $cats ) {
253 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
256 function invalidateImageDescriptions( $images ) {
257 $this->invalidatePages( NS_IMAGE
, array_keys( $images ) );
260 function dumbTableUpdate( $table, $insertions, $fromField ) {
261 $fname = 'LinksUpdate::dumbTableUpdate';
262 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
263 if ( count( $insertions ) ) {
264 # The link array was constructed without FOR UPDATE, so there may be collisions
265 # This may cause minor link table inconsistencies, which is better than
266 # crippling the site with lock contention.
267 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
272 * Make a WHERE clause from a 2-d NS/dbkey array
274 * @param array $arr 2-d array indexed by namespace and DB key
275 * @param string $prefix Field name prefix, without the underscore
277 function makeWhereFrom2d( &$arr, $prefix ) {
279 $lb->setArray( $arr );
280 return $lb->constructSet( $prefix, $this->mDb
);
284 * Update a table by doing a delete query then an insert query
287 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
288 $fname = 'LinksUpdate::incrTableUpdate';
289 $where = array( "{$prefix}_from" => $this->mId
);
290 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
291 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
298 if ( $table == 'langlinks' ) {
299 $toField = 'll_lang';
301 $toField = $prefix . '_to';
303 if ( count( $deletions ) ) {
304 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
310 $this->mDb
->delete( $table, $where, $fname );
312 if ( count( $insertions ) ) {
313 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
319 * Get an array of pagelinks insertions for passing to the DB
320 * Skips the titles specified by the 2-D array $existing
323 function getLinkInsertions( $existing = array() ) {
325 foreach( $this->mLinks
as $ns => $dbkeys ) {
326 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
327 # in GlobalFunctions.php
328 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
329 foreach ( $diffs as $dbk => $id ) {
331 'pl_from' => $this->mId
,
332 'pl_namespace' => $ns,
341 * Get an array of template insertions. Like getLinkInsertions()
344 function getTemplateInsertions( $existing = array() ) {
346 foreach( $this->mTemplates
as $ns => $dbkeys ) {
347 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
348 foreach ( $diffs as $dbk => $id ) {
350 'tl_from' => $this->mId
,
351 'tl_namespace' => $ns,
360 * Get an array of image insertions
361 * Skips the names specified in $existing
364 function getImageInsertions( $existing = array() ) {
366 $diffs = array_diff_key( $this->mImages
, $existing );
367 foreach( $diffs as $iname => $dummy ) {
369 'il_from' => $this->mId
,
377 * Get an array of externallinks insertions. Skips the names specified in $existing
380 function getExternalInsertions( $existing = array() ) {
382 $diffs = array_diff_key( $this->mExternals
, $existing );
383 foreach( $diffs as $url => $dummy ) {
385 'el_from' => $this->mId
,
387 'el_index' => wfMakeUrlIndex( $url ),
394 * Get an array of category insertions
395 * @param array $existing Array mapping existing category names to sort keys. If both
396 * match a link in $this, the link will be omitted from the output
399 function getCategoryInsertions( $existing = array() ) {
400 $diffs = array_diff_assoc( $this->mCategories
, $existing );
402 foreach ( $diffs as $name => $sortkey ) {
404 'cl_from' => $this->mId
,
406 'cl_sortkey' => $sortkey,
407 'cl_timestamp' => $this->mDb
->timestamp()
414 * Get an array of interlanguage link insertions
415 * @param array $existing Array mapping existing language codes to titles
418 function getInterlangInsertions( $existing = array() ) {
419 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
421 foreach( $diffs as $lang => $title ) {
423 'll_from' => $this->mId
,
432 * Given an array of existing links, returns those links which are not in $this
433 * and thus should be deleted.
436 function getLinkDeletions( $existing ) {
438 foreach ( $existing as $ns => $dbkeys ) {
439 if ( isset( $this->mLinks
[$ns] ) ) {
440 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
442 $del[$ns] = $existing[$ns];
449 * Given an array of existing templates, returns those templates which are not in $this
450 * and thus should be deleted.
453 function getTemplateDeletions( $existing ) {
455 foreach ( $existing as $ns => $dbkeys ) {
456 if ( isset( $this->mTemplates
[$ns] ) ) {
457 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
459 $del[$ns] = $existing[$ns];
466 * Given an array of existing images, returns those images which are not in $this
467 * and thus should be deleted.
470 function getImageDeletions( $existing ) {
471 return array_diff_key( $existing, $this->mImages
);
475 * Given an array of existing external links, returns those links which are not
476 * in $this and thus should be deleted.
479 function getExternalDeletions( $existing ) {
480 return array_diff_key( $existing, $this->mExternals
);
484 * Given an array of existing categories, returns those categories which are not in $this
485 * and thus should be deleted.
488 function getCategoryDeletions( $existing ) {
489 return array_diff_assoc( $existing, $this->mCategories
);
493 * Given an array of existing interlanguage links, returns those links which are not
494 * in $this and thus should be deleted.
497 function getInterlangDeletions( $existing ) {
498 return array_diff_assoc( $existing, $this->mInterlangs
);
502 * Get an array of existing links, as a 2-D array
505 function getExistingLinks() {
506 $fname = 'LinksUpdate::getExistingLinks';
507 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
508 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
510 while ( $row = $this->mDb
->fetchObject( $res ) ) {
511 if ( !isset( $arr[$row->pl_namespace
] ) ) {
512 $arr[$row->pl_namespace
] = array();
514 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
516 $this->mDb
->freeResult( $res );
521 * Get an array of existing templates, as a 2-D array
524 function getExistingTemplates() {
525 $fname = 'LinksUpdate::getExistingTemplates';
526 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
527 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
529 while ( $row = $this->mDb
->fetchObject( $res ) ) {
530 if ( !isset( $arr[$row->tl_namespace
] ) ) {
531 $arr[$row->tl_namespace
] = array();
533 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
535 $this->mDb
->freeResult( $res );
540 * Get an array of existing images, image names in the keys
543 function getExistingImages() {
544 $fname = 'LinksUpdate::getExistingImages';
545 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
546 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
548 while ( $row = $this->mDb
->fetchObject( $res ) ) {
549 $arr[$row->il_to
] = 1;
551 $this->mDb
->freeResult( $res );
556 * Get an array of existing external links, URLs in the keys
559 function getExistingExternals() {
560 $fname = 'LinksUpdate::getExistingExternals';
561 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
562 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
564 while ( $row = $this->mDb
->fetchObject( $res ) ) {
565 $arr[$row->el_to
] = 1;
567 $this->mDb
->freeResult( $res );
572 * Get an array of existing categories, with the name in the key and sort key in the value.
575 function getExistingCategories() {
576 $fname = 'LinksUpdate::getExistingCategories';
577 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
578 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
580 while ( $row = $this->mDb
->fetchObject( $res ) ) {
581 $arr[$row->cl_to
] = $row->cl_sortkey
;
583 $this->mDb
->freeResult( $res );
588 * Get an array of existing interlanguage links, with the language code in the key and the
589 * title in the value.
592 function getExistingInterlangs() {
593 $fname = 'LinksUpdate::getExistingInterlangs';
594 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
595 array( 'll_from' => $this->mId
), $fname, $this->mOptions
);
597 while ( $row = $this->mDb
->fetchObject( $res ) ) {
598 $arr[$row->ll_lang
] = $row->ll_title
;
604 * Return the title object of the page being updated
606 function getTitle() {
607 return $this->mTitle
;