Fix #218: Redirects do not support named anchors
[mediawiki.git] / includes / LinksUpdate.php
blobbd09502e797430bb787de3557d3c0a5ca66d49d7
1 <?php
2 /**
3 * See deferred.txt
4 * @package MediaWiki
5 */
7 /**
8 * @todo document
9 * @package MediaWiki
11 class LinksUpdate {
13 /**@{{
14 * @private
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
27 /**@}}*/
29 /**
30 * Constructor
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();
41 } else {
42 $this->mOptions = array( 'FOR UPDATE' );
44 $this->mDb =& wfGetDB( DB_MASTER );
46 if ( !is_object( $title ) ) {
47 wfDebugDieBacktrace( "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;
73 /**
74 * Update link tables with outgoing links from an updated article
76 function doUpdate() {
77 global $wgUseDumbLinkUpdate;
78 if ( $wgUseDumbLinkUpdate ) {
79 $this->doDumbUpdate();
80 } else {
81 $this->doIncrementalUpdate();
85 function doIncrementalUpdate() {
86 $fname = 'LinksUpdate::doIncrementalUpdate';
87 wfProfileIn( $fname );
89 # Page links
90 $existing = $this->getExistingLinks();
91 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
92 $this->getLinkInsertions( $existing ) );
94 # Image links
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 );
103 # External links
104 $existing = $this->getExistingExternals();
105 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
106 $this->getExternalInsertions( $existing ) );
108 # Language links
109 $existing = $this->getExistingInterlangs();
110 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
111 $this->getInterlangInsertions( $existing ) );
113 # Template links
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 require_once( 'JobQueue.php' );
123 Job::queueLinksJobs( $tlto );
127 # Category links
128 $existing = $this->getExistingCategories();
129 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
130 $this->getCategoryInsertions( $existing ) );
132 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
133 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
134 $this->invalidateCategories( $categoryUpdates );
136 wfProfileOut( $fname );
140 * Link update which clears the previous entries and inserts new ones
141 * May be slower or faster depending on level of lock contention and write speed of DB
142 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
144 function doDumbUpdate() {
145 $fname = 'LinksUpdate::doDumbUpdate';
146 wfProfileIn( $fname );
148 # Refresh category pages and image description pages
149 $existing = $this->getExistingCategories();
150 $categoryUpdates = array_diff_assoc( $existing, $this->mCategories ) + array_diff_assoc( $this->mCategories, $existing );
151 $existing = $this->getExistingImages();
152 $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
154 # Refresh links of all pages including this page
155 if ( $this->mRecursive ) {
156 $tlto = $this->mTitle->getTemplateLinksTo();
157 if ( count( $tlto ) ) {
158 require_once( 'JobQueue.php' );
159 Job::queueLinksJobs( $tlto );
163 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
164 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
165 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
166 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
167 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
168 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
170 # Update the cache of all the category pages and image description pages which were changed
171 $this->invalidateCategories( $categoryUpdates );
172 $this->invalidateImageDescriptions( $imageUpdates );
174 wfProfileOut( $fname );
178 * Invalidate the cache of a list of pages from a single namespace
180 * @param integer $namespace
181 * @param array $dbkeys
183 function invalidatePages( $namespace, $dbkeys ) {
184 $fname = 'LinksUpdate::invalidatePages';
186 if ( !count( $dbkeys ) ) {
187 return;
191 * Determine which pages need to be updated
192 * This is necessary to prevent the job queue from smashing the DB with
193 * large numbers of concurrent invalidations of the same page
195 $now = $this->mDb->timestamp();
196 $ids = array();
197 $res = $this->mDb->select( 'page', array( 'page_id' ),
198 array(
199 'page_namespace' => $namespace,
200 'page_title IN (' . $this->mDb->makeList( $dbkeys ) . ')',
201 'page_touched < ' . $this->mDb->addQuotes( $now )
202 ), $fname
204 while ( $row = $this->mDb->fetchObject( $res ) ) {
205 $ids[] = $row->page_id;
207 if ( !count( $ids ) ) {
208 return;
212 * Do the update
213 * We still need the page_touched condition, in case the row has changed since
214 * the non-locking select above.
216 $this->mDb->update( 'page', array( 'page_touched' => $now ),
217 array(
218 'page_id IN (' . $this->mDb->makeList( $ids ) . ')',
219 'page_touched < ' . $this->mDb->addQuotes( $now )
220 ), $fname
224 function invalidateCategories( $cats ) {
225 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
228 function invalidateImageDescriptions( $images ) {
229 $this->invalidatePages( NS_IMAGE, array_keys( $images ) );
232 function dumbTableUpdate( $table, $insertions, $fromField ) {
233 $fname = 'LinksUpdate::dumbTableUpdate';
234 $this->mDb->delete( $table, array( $fromField => $this->mId ), $fname );
235 if ( count( $insertions ) ) {
236 # The link array was constructed without FOR UPDATE, so there may be collisions
237 # This may cause minor link table inconsistencies, which is better than
238 # crippling the site with lock contention.
239 $this->mDb->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
244 * Make a WHERE clause from a 2-d NS/dbkey array
246 * @param array $arr 2-d array indexed by namespace and DB key
247 * @param string $prefix Field name prefix, without the underscore
249 function makeWhereFrom2d( &$arr, $prefix ) {
250 $lb = new LinkBatch;
251 $lb->setArray( $arr );
252 return $lb->constructSet( $prefix, $this->mDb );
256 * Update a table by doing a delete query then an insert query
257 * @private
259 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
260 $fname = 'LinksUpdate::incrTableUpdate';
261 $where = array( "{$prefix}_from" => $this->mId );
262 if ( $table == 'pagelinks' || $table == 'templatelinks' ) {
263 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
264 if ( $clause ) {
265 $where[] = $clause;
266 } else {
267 $where = false;
269 } else {
270 if ( $table == 'langlinks' ) {
271 $toField = 'll_lang';
272 } else {
273 $toField = $prefix . '_to';
275 if ( count( $deletions ) ) {
276 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
277 } else {
278 $where = false;
281 if ( $where ) {
282 $this->mDb->delete( $table, $where, $fname );
284 if ( count( $insertions ) ) {
285 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
291 * Get an array of pagelinks insertions for passing to the DB
292 * Skips the titles specified by the 2-D array $existing
293 * @private
295 function getLinkInsertions( $existing = array() ) {
296 $arr = array();
297 foreach( $this->mLinks as $ns => $dbkeys ) {
298 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
299 # in GlobalFunctions.php
300 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
301 foreach ( $diffs as $dbk => $id ) {
302 $arr[] = array(
303 'pl_from' => $this->mId,
304 'pl_namespace' => $ns,
305 'pl_title' => $dbk
309 return $arr;
313 * Get an array of template insertions. Like getLinkInsertions()
314 * @private
316 function getTemplateInsertions( $existing = array() ) {
317 $arr = array();
318 foreach( $this->mTemplates as $ns => $dbkeys ) {
319 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
320 foreach ( $diffs as $dbk => $id ) {
321 $arr[] = array(
322 'tl_from' => $this->mId,
323 'tl_namespace' => $ns,
324 'tl_title' => $dbk
328 return $arr;
332 * Get an array of image insertions
333 * Skips the names specified in $existing
334 * @private
336 function getImageInsertions( $existing = array() ) {
337 $arr = array();
338 $diffs = array_diff_key( $this->mImages, $existing );
339 foreach( $diffs as $iname => $dummy ) {
340 $arr[] = array(
341 'il_from' => $this->mId,
342 'il_to' => $iname
345 return $arr;
349 * Get an array of externallinks insertions. Skips the names specified in $existing
350 * @private
352 function getExternalInsertions( $existing = array() ) {
353 $arr = array();
354 $diffs = array_diff_key( $this->mExternals, $existing );
355 foreach( $diffs as $url => $dummy ) {
356 $arr[] = array(
357 'el_from' => $this->mId,
358 'el_to' => $url,
359 'el_index' => wfMakeUrlIndex( $url ),
362 return $arr;
366 * Get an array of category insertions
367 * @param array $existing Array mapping existing category names to sort keys. If both
368 * match a link in $this, the link will be omitted from the output
369 * @private
371 function getCategoryInsertions( $existing = array() ) {
372 $diffs = array_diff_assoc( $this->mCategories, $existing );
373 $arr = array();
374 foreach ( $diffs as $name => $sortkey ) {
375 $arr[] = array(
376 'cl_from' => $this->mId,
377 'cl_to' => $name,
378 'cl_sortkey' => $sortkey,
379 'cl_timestamp' => $this->mDb->timestamp()
382 return $arr;
386 * Get an array of interlanguage link insertions
387 * @param array $existing Array mapping existing language codes to titles
388 * @private
390 function getInterlangInsertions( $existing = array() ) {
391 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
392 $arr = array();
393 foreach( $diffs as $lang => $title ) {
394 $arr[] = array(
395 'll_from' => $this->mId,
396 'll_lang' => $lang,
397 'll_title' => $title
400 return $arr;
404 * Given an array of existing links, returns those links which are not in $this
405 * and thus should be deleted.
406 * @private
408 function getLinkDeletions( $existing ) {
409 $del = array();
410 foreach ( $existing as $ns => $dbkeys ) {
411 if ( isset( $this->mLinks[$ns] ) ) {
412 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
413 } else {
414 $del[$ns] = $existing[$ns];
417 return $del;
421 * Given an array of existing templates, returns those templates which are not in $this
422 * and thus should be deleted.
423 * @private
425 function getTemplateDeletions( $existing ) {
426 $del = array();
427 foreach ( $existing as $ns => $dbkeys ) {
428 if ( isset( $this->mTemplates[$ns] ) ) {
429 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
430 } else {
431 $del[$ns] = $existing[$ns];
434 return $del;
438 * Given an array of existing images, returns those images which are not in $this
439 * and thus should be deleted.
440 * @private
442 function getImageDeletions( $existing ) {
443 return array_diff_key( $existing, $this->mImages );
446 /**
447 * Given an array of existing external links, returns those links which are not
448 * in $this and thus should be deleted.
449 * @private
451 function getExternalDeletions( $existing ) {
452 return array_diff_key( $existing, $this->mExternals );
456 * Given an array of existing categories, returns those categories which are not in $this
457 * and thus should be deleted.
458 * @private
460 function getCategoryDeletions( $existing ) {
461 return array_diff_assoc( $existing, $this->mCategories );
464 /**
465 * Given an array of existing interlanguage links, returns those links which are not
466 * in $this and thus should be deleted.
467 * @private
469 function getInterlangDeletions( $existing ) {
470 return array_diff_assoc( $existing, $this->mInterlangs );
474 * Get an array of existing links, as a 2-D array
475 * @private
477 function getExistingLinks() {
478 $fname = 'LinksUpdate::getExistingLinks';
479 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
480 array( 'pl_from' => $this->mId ), $fname, $this->mOptions );
481 $arr = array();
482 while ( $row = $this->mDb->fetchObject( $res ) ) {
483 if ( !isset( $arr[$row->pl_namespace] ) ) {
484 $arr[$row->pl_namespace] = array();
486 $arr[$row->pl_namespace][$row->pl_title] = 1;
488 $this->mDb->freeResult( $res );
489 return $arr;
493 * Get an array of existing templates, as a 2-D array
494 * @private
496 function getExistingTemplates() {
497 $fname = 'LinksUpdate::getExistingTemplates';
498 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
499 array( 'tl_from' => $this->mId ), $fname, $this->mOptions );
500 $arr = array();
501 while ( $row = $this->mDb->fetchObject( $res ) ) {
502 if ( !isset( $arr[$row->tl_namespace] ) ) {
503 $arr[$row->tl_namespace] = array();
505 $arr[$row->tl_namespace][$row->tl_title] = 1;
507 $this->mDb->freeResult( $res );
508 return $arr;
512 * Get an array of existing images, image names in the keys
513 * @private
515 function getExistingImages() {
516 $fname = 'LinksUpdate::getExistingImages';
517 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
518 array( 'il_from' => $this->mId ), $fname, $this->mOptions );
519 $arr = array();
520 while ( $row = $this->mDb->fetchObject( $res ) ) {
521 $arr[$row->il_to] = 1;
523 $this->mDb->freeResult( $res );
524 return $arr;
528 * Get an array of existing external links, URLs in the keys
529 * @private
531 function getExistingExternals() {
532 $fname = 'LinksUpdate::getExistingExternals';
533 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
534 array( 'el_from' => $this->mId ), $fname, $this->mOptions );
535 $arr = array();
536 while ( $row = $this->mDb->fetchObject( $res ) ) {
537 $arr[$row->el_to] = 1;
539 $this->mDb->freeResult( $res );
540 return $arr;
544 * Get an array of existing categories, with the name in the key and sort key in the value.
545 * @private
547 function getExistingCategories() {
548 $fname = 'LinksUpdate::getExistingCategories';
549 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
550 array( 'cl_from' => $this->mId ), $fname, $this->mOptions );
551 $arr = array();
552 while ( $row = $this->mDb->fetchObject( $res ) ) {
553 $arr[$row->cl_to] = $row->cl_sortkey;
555 $this->mDb->freeResult( $res );
556 return $arr;
560 * Get an array of existing interlanguage links, with the language code in the key and the
561 * title in the value.
562 * @private
564 function getExistingInterlangs() {
565 $fname = 'LinksUpdate::getExistingInterlangs';
566 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
567 array( 'll_from' => $this->mId ), $fname, $this->mOptions );
568 $arr = array();
569 while ( $row = $this->mDb->fetchObject( $res ) ) {
570 $arr[$row->ll_lang] = $row->ll_title;
572 return $arr;