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();
81 function doIncrementalUpdate() {
82 $fname = 'LinksUpdate::doIncrementalUpdate';
83 wfProfileIn( $fname );
86 $existing = $this->getExistingLinks();
87 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
88 $this->getLinkInsertions( $existing ) );
91 $existing = $this->getExistingImages();
92 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
93 $this->getImageInsertions( $existing ) );
95 # Invalidate all image description pages which had links added or removed
96 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
97 $this->invalidateImageDescriptions( $imageUpdates );
100 $existing = $this->getExistingExternals();
101 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
102 $this->getExternalInsertions( $existing ) );
105 $existing = $this->getExistingInterlangs();
106 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
107 $this->getInterlangInsertions( $existing ) );
110 $existing = $this->getExistingTemplates();
111 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
112 $this->getTemplateInsertions( $existing ) );
115 $existing = $this->getExistingCategories();
116 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
117 $this->getCategoryInsertions( $existing ) );
119 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
120 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
121 $this->invalidateCategories( $categoryUpdates );
123 # Refresh links of all pages including this page
124 # This will be in a separate transaction
125 if ( $this->mRecursive
) {
126 $this->queueRecursiveJobs();
129 wfProfileOut( $fname );
133 * Link update which clears the previous entries and inserts new ones
134 * May be slower or faster depending on level of lock contention and write speed of DB
135 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
137 function doDumbUpdate() {
138 $fname = 'LinksUpdate::doDumbUpdate';
139 wfProfileIn( $fname );
141 # Refresh category pages and image description pages
142 $existing = $this->getExistingCategories();
143 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
144 $existing = $this->getExistingImages();
145 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
147 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
148 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
149 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
150 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
151 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
152 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
154 # Update the cache of all the category pages and image description pages which were changed
155 $this->invalidateCategories( $categoryUpdates );
156 $this->invalidateImageDescriptions( $imageUpdates );
158 # Refresh links of all pages including this page
159 # This will be in a separate transaction
160 if ( $this->mRecursive
) {
161 $this->queueRecursiveJobs();
164 wfProfileOut( $fname );
167 function queueRecursiveJobs() {
168 wfProfileIn( __METHOD__
);
171 $dbr = wfGetDB( DB_SLAVE
);
172 $res = $dbr->select( array( 'templatelinks', 'page' ),
173 array( 'page_namespace', 'page_title' ),
176 'tl_namespace' => $this->mTitle
->getNamespace(),
177 'tl_title' => $this->mTitle
->getDBkey()
184 for ( $i = 0; $i < $batchSize; $i++
) {
185 $row = $dbr->fetchObject( $res );
190 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
191 $jobs[] = Job
::factory( 'refreshLinks', $title );
193 Job
::batchInsert( $jobs );
195 $dbr->freeResult( $res );
196 wfProfileOut( __METHOD__
);
200 * Invalidate the cache of a list of pages from a single namespace
202 * @param integer $namespace
203 * @param array $dbkeys
205 function invalidatePages( $namespace, $dbkeys ) {
206 $fname = 'LinksUpdate::invalidatePages';
208 if ( !count( $dbkeys ) ) {
213 * Determine which pages need to be updated
214 * This is necessary to prevent the job queue from smashing the DB with
215 * large numbers of concurrent invalidations of the same page
217 $now = $this->mDb
->timestamp();
219 $res = $this->mDb
->select( 'page', array( 'page_id' ),
221 'page_namespace' => $namespace,
222 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
223 'page_touched < ' . $this->mDb
->addQuotes( $now )
226 while ( $row = $this->mDb
->fetchObject( $res ) ) {
227 $ids[] = $row->page_id
;
229 if ( !count( $ids ) ) {
235 * We still need the page_touched condition, in case the row has changed since
236 * the non-locking select above.
238 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
240 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
241 'page_touched < ' . $this->mDb
->addQuotes( $now )
246 function invalidateCategories( $cats ) {
247 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
250 function invalidateImageDescriptions( $images ) {
251 $this->invalidatePages( NS_IMAGE
, array_keys( $images ) );
254 function dumbTableUpdate( $table, $insertions, $fromField ) {
255 $fname = 'LinksUpdate::dumbTableUpdate';
256 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
257 if ( count( $insertions ) ) {
258 # The link array was constructed without FOR UPDATE, so there may be collisions
259 # This may cause minor link table inconsistencies, which is better than
260 # crippling the site with lock contention.
261 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
266 * Make a WHERE clause from a 2-d NS/dbkey array
268 * @param array $arr 2-d array indexed by namespace and DB key
269 * @param string $prefix Field name prefix, without the underscore
271 function makeWhereFrom2d( &$arr, $prefix ) {
273 $lb->setArray( $arr );
274 return $lb->constructSet( $prefix, $this->mDb
);
278 * Update a table by doing a delete query then an insert query
281 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
282 $fname = 'LinksUpdate::incrTableUpdate';
283 $where = array( "{$prefix}_from" => $this->mId
);
284 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
285 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
292 if ( $table == 'langlinks' ) {
293 $toField = 'll_lang';
295 $toField = $prefix . '_to';
297 if ( count( $deletions ) ) {
298 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
304 $this->mDb
->delete( $table, $where, $fname );
306 if ( count( $insertions ) ) {
307 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
313 * Get an array of pagelinks insertions for passing to the DB
314 * Skips the titles specified by the 2-D array $existing
317 function getLinkInsertions( $existing = array() ) {
319 foreach( $this->mLinks
as $ns => $dbkeys ) {
320 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
321 # in GlobalFunctions.php
322 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
323 foreach ( $diffs as $dbk => $id ) {
325 'pl_from' => $this->mId
,
326 'pl_namespace' => $ns,
335 * Get an array of template insertions. Like getLinkInsertions()
338 function getTemplateInsertions( $existing = array() ) {
340 foreach( $this->mTemplates
as $ns => $dbkeys ) {
341 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
342 foreach ( $diffs as $dbk => $id ) {
344 'tl_from' => $this->mId
,
345 'tl_namespace' => $ns,
354 * Get an array of image insertions
355 * Skips the names specified in $existing
358 function getImageInsertions( $existing = array() ) {
360 $diffs = array_diff_key( $this->mImages
, $existing );
361 foreach( $diffs as $iname => $dummy ) {
363 'il_from' => $this->mId
,
371 * Get an array of externallinks insertions. Skips the names specified in $existing
374 function getExternalInsertions( $existing = array() ) {
376 $diffs = array_diff_key( $this->mExternals
, $existing );
377 foreach( $diffs as $url => $dummy ) {
379 'el_from' => $this->mId
,
381 'el_index' => wfMakeUrlIndex( $url ),
388 * Get an array of category insertions
389 * @param array $existing Array mapping existing category names to sort keys. If both
390 * match a link in $this, the link will be omitted from the output
393 function getCategoryInsertions( $existing = array() ) {
394 $diffs = array_diff_assoc( $this->mCategories
, $existing );
396 foreach ( $diffs as $name => $sortkey ) {
398 'cl_from' => $this->mId
,
400 'cl_sortkey' => $sortkey,
401 'cl_timestamp' => $this->mDb
->timestamp()
408 * Get an array of interlanguage link insertions
409 * @param array $existing Array mapping existing language codes to titles
412 function getInterlangInsertions( $existing = array() ) {
413 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
415 foreach( $diffs as $lang => $title ) {
417 'll_from' => $this->mId
,
426 * Given an array of existing links, returns those links which are not in $this
427 * and thus should be deleted.
430 function getLinkDeletions( $existing ) {
432 foreach ( $existing as $ns => $dbkeys ) {
433 if ( isset( $this->mLinks
[$ns] ) ) {
434 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
436 $del[$ns] = $existing[$ns];
443 * Given an array of existing templates, returns those templates which are not in $this
444 * and thus should be deleted.
447 function getTemplateDeletions( $existing ) {
449 foreach ( $existing as $ns => $dbkeys ) {
450 if ( isset( $this->mTemplates
[$ns] ) ) {
451 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
453 $del[$ns] = $existing[$ns];
460 * Given an array of existing images, returns those images which are not in $this
461 * and thus should be deleted.
464 function getImageDeletions( $existing ) {
465 return array_diff_key( $existing, $this->mImages
);
469 * Given an array of existing external links, returns those links which are not
470 * in $this and thus should be deleted.
473 function getExternalDeletions( $existing ) {
474 return array_diff_key( $existing, $this->mExternals
);
478 * Given an array of existing categories, returns those categories which are not in $this
479 * and thus should be deleted.
482 function getCategoryDeletions( $existing ) {
483 return array_diff_assoc( $existing, $this->mCategories
);
487 * Given an array of existing interlanguage links, returns those links which are not
488 * in $this and thus should be deleted.
491 function getInterlangDeletions( $existing ) {
492 return array_diff_assoc( $existing, $this->mInterlangs
);
496 * Get an array of existing links, as a 2-D array
499 function getExistingLinks() {
500 $fname = 'LinksUpdate::getExistingLinks';
501 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
502 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
504 while ( $row = $this->mDb
->fetchObject( $res ) ) {
505 if ( !isset( $arr[$row->pl_namespace
] ) ) {
506 $arr[$row->pl_namespace
] = array();
508 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
510 $this->mDb
->freeResult( $res );
515 * Get an array of existing templates, as a 2-D array
518 function getExistingTemplates() {
519 $fname = 'LinksUpdate::getExistingTemplates';
520 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
521 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
523 while ( $row = $this->mDb
->fetchObject( $res ) ) {
524 if ( !isset( $arr[$row->tl_namespace
] ) ) {
525 $arr[$row->tl_namespace
] = array();
527 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
529 $this->mDb
->freeResult( $res );
534 * Get an array of existing images, image names in the keys
537 function getExistingImages() {
538 $fname = 'LinksUpdate::getExistingImages';
539 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
540 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
542 while ( $row = $this->mDb
->fetchObject( $res ) ) {
543 $arr[$row->il_to
] = 1;
545 $this->mDb
->freeResult( $res );
550 * Get an array of existing external links, URLs in the keys
553 function getExistingExternals() {
554 $fname = 'LinksUpdate::getExistingExternals';
555 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
556 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
558 while ( $row = $this->mDb
->fetchObject( $res ) ) {
559 $arr[$row->el_to
] = 1;
561 $this->mDb
->freeResult( $res );
566 * Get an array of existing categories, with the name in the key and sort key in the value.
569 function getExistingCategories() {
570 $fname = 'LinksUpdate::getExistingCategories';
571 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
572 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
574 while ( $row = $this->mDb
->fetchObject( $res ) ) {
575 $arr[$row->cl_to
] = $row->cl_sortkey
;
577 $this->mDb
->freeResult( $res );
582 * Get an array of existing interlanguage links, with the language code in the key and the
583 * title in the value.
586 function getExistingInterlangs() {
587 $fname = 'LinksUpdate::getExistingInterlangs';
588 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
589 array( 'll_from' => $this->mId
), $fname, $this->mOptions
);
591 while ( $row = $this->mDb
->fetchObject( $res ) ) {
592 $arr[$row->ll_lang
] = $row->ll_title
;