16 var $mId, //!< Page ID of the article linked from
17 $mTitle, //!< Title object of the article linked from
18 $mLinks, //!< Map of title strings to IDs for the links in the document
19 $mImages, //!< DB keys of the images used, in the array key only
20 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
21 $mExternals, //!< URLs of external links, array key only
22 $mCategories, //!< Map of category names to sort keys
23 $mInterlangs, //!< Map of language codes to titles
24 $mDb, //!< Database connection reference
25 $mOptions, //!< SELECT options to be used (array)
26 $mRecursive; //!< Whether to queue jobs for recursive updates
31 * Initialize private variables
32 * @param $title Integer: FIXME
33 * @param $parserOutput FIXME
34 * @param $recursive Boolean: FIXME, default 'true'.
36 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
37 global $wgAntiLockFlags;
39 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
40 $this->mOptions
= array();
42 $this->mOptions
= array( 'FOR UPDATE' );
44 $this->mDb
=& wfGetDB( DB_MASTER
);
46 if ( !is_object( $title ) ) {
47 throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
48 "Please see Article::editUpdates() for an invocation example.\n" );
50 $this->mTitle
= $title;
51 $this->mId
= $title->getArticleID();
53 $this->mLinks
= $parserOutput->getLinks();
54 $this->mImages
= $parserOutput->getImages();
55 $this->mTemplates
= $parserOutput->getTemplates();
56 $this->mExternals
= $parserOutput->getExternalLinks();
57 $this->mCategories
= $parserOutput->getCategories();
59 # Convert the format of the interlanguage links
60 # I didn't want to change it in the ParserOutput, because that array is passed all
61 # the way back to the skin, so either a skin API break would be required, or an
62 # inefficient back-conversion.
63 $ill = $parserOutput->getLanguageLinks();
64 $this->mInterlangs
= array();
65 foreach ( $ill as $link ) {
66 list( $key, $title ) = explode( ':', $link, 2 );
67 $this->mInterlangs
[$key] = $title;
70 $this->mRecursive
= $recursive;
74 * Update link tables with outgoing links from an updated article
77 global $wgUseDumbLinkUpdate;
78 if ( $wgUseDumbLinkUpdate ) {
79 $this->doDumbUpdate();
81 $this->doIncrementalUpdate();
85 function doIncrementalUpdate() {
86 $fname = 'LinksUpdate::doIncrementalUpdate';
87 wfProfileIn( $fname );
90 $existing = $this->getExistingLinks();
91 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
92 $this->getLinkInsertions( $existing ) );
95 $existing = $this->getExistingImages();
96 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
97 $this->getImageInsertions( $existing ) );
99 # Invalidate all image description pages which had links added or removed
100 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
101 $this->invalidateImageDescriptions( $imageUpdates );
104 $existing = $this->getExistingExternals();
105 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
106 $this->getExternalInsertions( $existing ) );
109 $existing = $this->getExistingInterlangs();
110 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
111 $this->getInterlangInsertions( $existing ) );
114 $existing = $this->getExistingTemplates();
115 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
116 $this->getTemplateInsertions( $existing ) );
118 # Refresh links of all pages including this page
119 if ( $this->mRecursive
) {
120 $tlto = $this->mTitle
->getTemplateLinksTo();
121 if ( count( $tlto ) ) {
122 Job
::queueLinksJobs( $tlto );
127 $existing = $this->getExistingCategories();
128 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
129 $this->getCategoryInsertions( $existing ) );
131 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
132 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories
) +
array_diff_assoc( $this->mCategories
, $existing );
133 $this->invalidateCategories( $categoryUpdates );
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 # Refresh links of all pages including this page
154 if ( $this->mRecursive
) {
155 $tlto = $this->mTitle
->getTemplateLinksTo();
156 if ( count( $tlto ) ) {
157 Job
::queueLinksJobs( $tlto );
161 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
162 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
163 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
164 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
165 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
166 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
168 # Update the cache of all the category pages and image description pages which were changed
169 $this->invalidateCategories( $categoryUpdates );
170 $this->invalidateImageDescriptions( $imageUpdates );
172 wfProfileOut( $fname );
176 * Invalidate the cache of a list of pages from a single namespace
178 * @param integer $namespace
179 * @param array $dbkeys
181 function invalidatePages( $namespace, $dbkeys ) {
182 $fname = 'LinksUpdate::invalidatePages';
184 if ( !count( $dbkeys ) ) {
189 * Determine which pages need to be updated
190 * This is necessary to prevent the job queue from smashing the DB with
191 * large numbers of concurrent invalidations of the same page
193 $now = $this->mDb
->timestamp();
195 $res = $this->mDb
->select( 'page', array( 'page_id' ),
197 'page_namespace' => $namespace,
198 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
199 'page_touched < ' . $this->mDb
->addQuotes( $now )
202 while ( $row = $this->mDb
->fetchObject( $res ) ) {
203 $ids[] = $row->page_id
;
205 if ( !count( $ids ) ) {
211 * We still need the page_touched condition, in case the row has changed since
212 * the non-locking select above.
214 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
216 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
217 'page_touched < ' . $this->mDb
->addQuotes( $now )
222 function invalidateCategories( $cats ) {
223 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
226 function invalidateImageDescriptions( $images ) {
227 $this->invalidatePages( NS_IMAGE
, array_keys( $images ) );
230 function dumbTableUpdate( $table, $insertions, $fromField ) {
231 $fname = 'LinksUpdate::dumbTableUpdate';
232 $this->mDb
->delete( $table, array( $fromField => $this->mId
), $fname );
233 if ( count( $insertions ) ) {
234 # The link array was constructed without FOR UPDATE, so there may be collisions
235 # This may cause minor link table inconsistencies, which is better than
236 # crippling the site with lock contention.
237 $this->mDb
->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
242 * Make a WHERE clause from a 2-d NS/dbkey array
244 * @param array $arr 2-d array indexed by namespace and DB key
245 * @param string $prefix Field name prefix, without the underscore
247 function makeWhereFrom2d( &$arr, $prefix ) {
249 $lb->setArray( $arr );
250 return $lb->constructSet( $prefix, $this->mDb
);
254 * Update a table by doing a delete query then an insert query
257 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
258 $fname = 'LinksUpdate::incrTableUpdate';
259 $where = array( "{$prefix}_from" => $this->mId
);
260 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ) {
261 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
268 if ( $table == 'langlinks' ) {
269 $toField = 'll_lang';
271 $toField = $prefix . '_to';
273 if ( count( $deletions ) ) {
274 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
280 $this->mDb
->delete( $table, $where, $fname );
282 if ( count( $insertions ) ) {
283 $this->mDb
->insert( $table, $insertions, $fname, 'IGNORE' );
289 * Get an array of pagelinks insertions for passing to the DB
290 * Skips the titles specified by the 2-D array $existing
293 function getLinkInsertions( $existing = array() ) {
295 foreach( $this->mLinks
as $ns => $dbkeys ) {
296 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
297 # in GlobalFunctions.php
298 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
299 foreach ( $diffs as $dbk => $id ) {
301 'pl_from' => $this->mId
,
302 'pl_namespace' => $ns,
311 * Get an array of template insertions. Like getLinkInsertions()
314 function getTemplateInsertions( $existing = array() ) {
316 foreach( $this->mTemplates
as $ns => $dbkeys ) {
317 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
318 foreach ( $diffs as $dbk => $id ) {
320 'tl_from' => $this->mId
,
321 'tl_namespace' => $ns,
330 * Get an array of image insertions
331 * Skips the names specified in $existing
334 function getImageInsertions( $existing = array() ) {
336 $diffs = array_diff_key( $this->mImages
, $existing );
337 foreach( $diffs as $iname => $dummy ) {
339 'il_from' => $this->mId
,
347 * Get an array of externallinks insertions. Skips the names specified in $existing
350 function getExternalInsertions( $existing = array() ) {
352 $diffs = array_diff_key( $this->mExternals
, $existing );
353 foreach( $diffs as $url => $dummy ) {
355 'el_from' => $this->mId
,
357 'el_index' => wfMakeUrlIndex( $url ),
364 * Get an array of category insertions
365 * @param array $existing Array mapping existing category names to sort keys. If both
366 * match a link in $this, the link will be omitted from the output
369 function getCategoryInsertions( $existing = array() ) {
370 $diffs = array_diff_assoc( $this->mCategories
, $existing );
372 foreach ( $diffs as $name => $sortkey ) {
374 'cl_from' => $this->mId
,
376 'cl_sortkey' => $sortkey,
377 'cl_timestamp' => $this->mDb
->timestamp()
384 * Get an array of interlanguage link insertions
385 * @param array $existing Array mapping existing language codes to titles
388 function getInterlangInsertions( $existing = array() ) {
389 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
391 foreach( $diffs as $lang => $title ) {
393 'll_from' => $this->mId
,
402 * Given an array of existing links, returns those links which are not in $this
403 * and thus should be deleted.
406 function getLinkDeletions( $existing ) {
408 foreach ( $existing as $ns => $dbkeys ) {
409 if ( isset( $this->mLinks
[$ns] ) ) {
410 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
412 $del[$ns] = $existing[$ns];
419 * Given an array of existing templates, returns those templates which are not in $this
420 * and thus should be deleted.
423 function getTemplateDeletions( $existing ) {
425 foreach ( $existing as $ns => $dbkeys ) {
426 if ( isset( $this->mTemplates
[$ns] ) ) {
427 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
429 $del[$ns] = $existing[$ns];
436 * Given an array of existing images, returns those images which are not in $this
437 * and thus should be deleted.
440 function getImageDeletions( $existing ) {
441 return array_diff_key( $existing, $this->mImages
);
445 * Given an array of existing external links, returns those links which are not
446 * in $this and thus should be deleted.
449 function getExternalDeletions( $existing ) {
450 return array_diff_key( $existing, $this->mExternals
);
454 * Given an array of existing categories, returns those categories which are not in $this
455 * and thus should be deleted.
458 function getCategoryDeletions( $existing ) {
459 return array_diff_assoc( $existing, $this->mCategories
);
463 * Given an array of existing interlanguage links, returns those links which are not
464 * in $this and thus should be deleted.
467 function getInterlangDeletions( $existing ) {
468 return array_diff_assoc( $existing, $this->mInterlangs
);
472 * Get an array of existing links, as a 2-D array
475 function getExistingLinks() {
476 $fname = 'LinksUpdate::getExistingLinks';
477 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
478 array( 'pl_from' => $this->mId
), $fname, $this->mOptions
);
480 while ( $row = $this->mDb
->fetchObject( $res ) ) {
481 if ( !isset( $arr[$row->pl_namespace
] ) ) {
482 $arr[$row->pl_namespace
] = array();
484 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
486 $this->mDb
->freeResult( $res );
491 * Get an array of existing templates, as a 2-D array
494 function getExistingTemplates() {
495 $fname = 'LinksUpdate::getExistingTemplates';
496 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
497 array( 'tl_from' => $this->mId
), $fname, $this->mOptions
);
499 while ( $row = $this->mDb
->fetchObject( $res ) ) {
500 if ( !isset( $arr[$row->tl_namespace
] ) ) {
501 $arr[$row->tl_namespace
] = array();
503 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
505 $this->mDb
->freeResult( $res );
510 * Get an array of existing images, image names in the keys
513 function getExistingImages() {
514 $fname = 'LinksUpdate::getExistingImages';
515 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
516 array( 'il_from' => $this->mId
), $fname, $this->mOptions
);
518 while ( $row = $this->mDb
->fetchObject( $res ) ) {
519 $arr[$row->il_to
] = 1;
521 $this->mDb
->freeResult( $res );
526 * Get an array of existing external links, URLs in the keys
529 function getExistingExternals() {
530 $fname = 'LinksUpdate::getExistingExternals';
531 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
532 array( 'el_from' => $this->mId
), $fname, $this->mOptions
);
534 while ( $row = $this->mDb
->fetchObject( $res ) ) {
535 $arr[$row->el_to
] = 1;
537 $this->mDb
->freeResult( $res );
542 * Get an array of existing categories, with the name in the key and sort key in the value.
545 function getExistingCategories() {
546 $fname = 'LinksUpdate::getExistingCategories';
547 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
548 array( 'cl_from' => $this->mId
), $fname, $this->mOptions
);
550 while ( $row = $this->mDb
->fetchObject( $res ) ) {
551 $arr[$row->cl_to
] = $row->cl_sortkey
;
553 $this->mDb
->freeResult( $res );
558 * Get an array of existing interlanguage links, with the language code in the key and the
559 * title in the value.
562 function getExistingInterlangs() {
563 $fname = 'LinksUpdate::getExistingInterlangs';
564 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
565 array( 'll_from' => $this->mId
), $fname, $this->mOptions
);
567 while ( $row = $this->mDb
->fetchObject( $res ) ) {
568 $arr[$row->ll_lang
] = $row->ll_title
;