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
27 * Initialize private variables
28 * @param $title Integer: FIXME
29 * @param $parserOutput FIXME
30 * @param $recursive Boolean: FIXME, default 'true'.
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;
70 * Update link tables with outgoing links from an updated article
73 global $wgUseDumbLinkUpdate;
74 if ( $wgUseDumbLinkUpdate ) {
75 $this->doDumbUpdate();
77 $this->doIncrementalUpdate();
79 wfRunHooks( 'TitleLinkUpdatesAfterCompletion', array( &$this->mTitle
) );
82 function doIncrementalUpdate() {
83 $fname = 'LinksUpdate::doIncrementalUpdate';
84 wfProfileIn( $fname );
87 $existing = $this->getExistingLinks();
88 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
89 $this->getLinkInsertions( $existing ) );
92 $existing = $this->getExistingImages();
93 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
94 $this->getImageInsertions( $existing ) );
96 # Invalidate all image description pages which had links added or removed
97 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
98 $this->invalidateImageDescriptions( $imageUpdates );
101 $existing = $this->getExistingExternals();
102 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
103 $this->getExternalInsertions( $existing ) );
106 $existing = $this->getExistingInterlangs();
107 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
108 $this->getInterlangInsertions( $existing ) );
111 $existing = $this->getExistingTemplates();
112 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
113 $this->getTemplateInsertions( $existing ) );
116 $existing = $this->getExistingCategories();
117 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
118 $this->getCategoryInsertions( $existing ) );
120 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
121 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
122 $this->invalidateCategories( $categoryUpdates );
124 # Refresh links of all pages including this page
125 # This will be in a separate transaction
126 if ( $this->mRecursive
) {
127 $this->queueRecursiveJobs();
130 wfProfileOut( $fname );
134 * Link update which clears the previous entries and inserts new ones
135 * May be slower or faster depending on level of lock contention and write speed of DB
136 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
138 function doDumbUpdate() {
139 $fname = 'LinksUpdate::doDumbUpdate';
140 wfProfileIn( $fname );
142 # Refresh category pages and image description pages
143 $existing = $this->getExistingCategories();
144 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
145 $existing = $this->getExistingImages();
146 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
148 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
149 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
150 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
151 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
152 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
153 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
155 # Update the cache of all the category pages and image description pages which were changed
156 $this->invalidateCategories( $categoryUpdates );
157 $this->invalidateImageDescriptions( $imageUpdates );
159 # Refresh links of all pages including this page
160 # This will be in a separate transaction
161 if ( $this->mRecursive
) {
162 $this->queueRecursiveJobs();
165 wfProfileOut( $fname );
168 function queueRecursiveJobs() {
169 wfProfileIn( __METHOD__
);
172 $dbr = wfGetDB( DB_SLAVE
);
173 $res = $dbr->select( array( 'templatelinks', 'page' ),
174 array( 'page_namespace', 'page_title' ),
177 'tl_namespace' => $this->mTitle
->getNamespace(),
178 'tl_title' => $this->mTitle
->getDBkey()
185 for ( $i = 0; $i < $batchSize; $i++
) {
186 $row = $dbr->fetchObject( $res );
191 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
192 $jobs[] = new RefreshLinksJob( $title, '' );
194 Job
::batchInsert( $jobs );
196 $dbr->freeResult( $res );
197 wfProfileOut( __METHOD__
);
201 * Invalidate the cache of a list of pages from a single namespace
203 * @param integer $namespace
204 * @param array $dbkeys
206 function invalidatePages( $namespace, $dbkeys ) {
207 $fname = 'LinksUpdate::invalidatePages';
209 if ( !count( $dbkeys ) ) {
214 * Determine which pages need to be updated
215 * This is necessary to prevent the job queue from smashing the DB with
216 * large numbers of concurrent invalidations of the same page
218 $now = $this->mDb
->timestamp();
220 $res = $this->mDb
->select( 'page', array( 'page_id' ),
222 'page_namespace' => $namespace,
223 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
224 'page_touched < ' . $this->mDb
->addQuotes( $now )
227 while ( $row = $this->mDb
->fetchObject( $res ) ) {
228 $ids[] = $row->page_id
;
230 if ( !count( $ids ) ) {
236 * We still need the page_touched condition, in case the row has changed since
237 * the non-locking select above.
239 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
241 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
242 'page_touched < ' . $this->mDb
->addQuotes( $now )
247 function invalidateCategories( $cats ) {
248 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
251 function invalidateImageDescriptions( $images ) {
252 $this->invalidatePages( NS_IMAGE
, array_keys( $images ) );
255 function dumbTableUpdate( $table, $insertions, $fromField ) {
256 $fname = 'LinksUpdate::dumbTableUpdate';
257 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
258 if ( count( $insertions ) ) {
259 # The link array was constructed without FOR UPDATE, so there may be collisions
260 # This may cause minor link table inconsistencies, which is better than
261 # crippling the site with lock contention.
262 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
267 * Make a WHERE clause from a 2-d NS/dbkey array
269 * @param array $arr 2-d array indexed by namespace and DB key
270 * @param string $prefix Field name prefix, without the underscore
272 function makeWhereFrom2d( &$arr, $prefix ) {
274 $lb->setArray( $arr );
275 return $lb->constructSet( $prefix, $this->mDb
);
279 * Update a table by doing a delete query then an insert query
282 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
283 $fname = 'LinksUpdate::incrTableUpdate';
284 $where = array( "{$prefix}_from" => $this->mId
);
285 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
286 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
293 if ( $table == 'langlinks' ) {
294 $toField = 'll_lang';
296 $toField = $prefix . '_to';
298 if ( count( $deletions ) ) {
299 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
305 $this->mDb
->delete( $table, $where, $fname );
307 if ( count( $insertions ) ) {
308 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
314 * Get an array of pagelinks insertions for passing to the DB
315 * Skips the titles specified by the 2-D array $existing
318 function getLinkInsertions( $existing = array() ) {
320 foreach( $this->mLinks
as $ns => $dbkeys ) {
321 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
322 # in GlobalFunctions.php
323 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
324 foreach ( $diffs as $dbk => $id ) {
326 'pl_from' => $this->mId
,
327 'pl_namespace' => $ns,
336 * Get an array of template insertions. Like getLinkInsertions()
339 function getTemplateInsertions( $existing = array() ) {
341 foreach( $this->mTemplates
as $ns => $dbkeys ) {
342 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
343 foreach ( $diffs as $dbk => $id ) {
345 'tl_from' => $this->mId
,
346 'tl_namespace' => $ns,
355 * Get an array of image insertions
356 * Skips the names specified in $existing
359 function getImageInsertions( $existing = array() ) {
361 $diffs = array_diff_key( $this->mImages
, $existing );
362 foreach( $diffs as $iname => $dummy ) {
364 'il_from' => $this->mId
,
372 * Get an array of externallinks insertions. Skips the names specified in $existing
375 function getExternalInsertions( $existing = array() ) {
377 $diffs = array_diff_key( $this->mExternals
, $existing );
378 foreach( $diffs as $url => $dummy ) {
380 'el_from' => $this->mId
,
382 'el_index' => wfMakeUrlIndex( $url ),
389 * Get an array of category insertions
390 * @param array $existing Array mapping existing category names to sort keys. If both
391 * match a link in $this, the link will be omitted from the output
394 function getCategoryInsertions( $existing = array() ) {
395 $diffs = array_diff_assoc( $this->mCategories
, $existing );
397 foreach ( $diffs as $name => $sortkey ) {
399 'cl_from' => $this->mId
,
401 'cl_sortkey' => $sortkey,
402 'cl_timestamp' => $this->mDb
->timestamp()
409 * Get an array of interlanguage link insertions
410 * @param array $existing Array mapping existing language codes to titles
413 function getInterlangInsertions( $existing = array() ) {
414 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
416 foreach( $diffs as $lang => $title ) {
418 'll_from' => $this->mId
,
427 * Given an array of existing links, returns those links which are not in $this
428 * and thus should be deleted.
431 function getLinkDeletions( $existing ) {
433 foreach ( $existing as $ns => $dbkeys ) {
434 if ( isset( $this->mLinks
[$ns] ) ) {
435 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
437 $del[$ns] = $existing[$ns];
444 * Given an array of existing templates, returns those templates which are not in $this
445 * and thus should be deleted.
448 function getTemplateDeletions( $existing ) {
450 foreach ( $existing as $ns => $dbkeys ) {
451 if ( isset( $this->mTemplates
[$ns] ) ) {
452 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
454 $del[$ns] = $existing[$ns];
461 * Given an array of existing images, returns those images which are not in $this
462 * and thus should be deleted.
465 function getImageDeletions( $existing ) {
466 return array_diff_key( $existing, $this->mImages
);
470 * Given an array of existing external links, returns those links which are not
471 * in $this and thus should be deleted.
474 function getExternalDeletions( $existing ) {
475 return array_diff_key( $existing, $this->mExternals
);
479 * Given an array of existing categories, returns those categories which are not in $this
480 * and thus should be deleted.
483 function getCategoryDeletions( $existing ) {
484 return array_diff_assoc( $existing, $this->mCategories
);
488 * Given an array of existing interlanguage links, returns those links which are not
489 * in $this and thus should be deleted.
492 function getInterlangDeletions( $existing ) {
493 return array_diff_assoc( $existing, $this->mInterlangs
);
497 * Get an array of existing links, as a 2-D array
500 function getExistingLinks() {
501 $fname = 'LinksUpdate::getExistingLinks';
502 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
503 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
505 while ( $row = $this->mDb
->fetchObject( $res ) ) {
506 if ( !isset( $arr[$row->pl_namespace
] ) ) {
507 $arr[$row->pl_namespace
] = array();
509 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
511 $this->mDb
->freeResult( $res );
516 * Get an array of existing templates, as a 2-D array
519 function getExistingTemplates() {
520 $fname = 'LinksUpdate::getExistingTemplates';
521 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
522 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
524 while ( $row = $this->mDb
->fetchObject( $res ) ) {
525 if ( !isset( $arr[$row->tl_namespace
] ) ) {
526 $arr[$row->tl_namespace
] = array();
528 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
530 $this->mDb
->freeResult( $res );
535 * Get an array of existing images, image names in the keys
538 function getExistingImages() {
539 $fname = 'LinksUpdate::getExistingImages';
540 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
541 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
543 while ( $row = $this->mDb
->fetchObject( $res ) ) {
544 $arr[$row->il_to
] = 1;
546 $this->mDb
->freeResult( $res );
551 * Get an array of existing external links, URLs in the keys
554 function getExistingExternals() {
555 $fname = 'LinksUpdate::getExistingExternals';
556 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
557 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
559 while ( $row = $this->mDb
->fetchObject( $res ) ) {
560 $arr[$row->el_to
] = 1;
562 $this->mDb
->freeResult( $res );
567 * Get an array of existing categories, with the name in the key and sort key in the value.
570 function getExistingCategories() {
571 $fname = 'LinksUpdate::getExistingCategories';
572 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
573 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
575 while ( $row = $this->mDb
->fetchObject( $res ) ) {
576 $arr[$row->cl_to
] = $row->cl_sortkey
;
578 $this->mDb
->freeResult( $res );
583 * Get an array of existing interlanguage links, with the language code in the key and the
584 * title in the value.
587 function getExistingInterlangs() {
588 $fname = 'LinksUpdate::getExistingInterlangs';
589 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
590 array( 'll_from' => $this->mId
), $fname, $this->mOptions
);
592 while ( $row = $this->mDb
->fetchObject( $res ) ) {
593 $arr[$row->ll_lang
] = $row->ll_title
;