Removed most exit() calls from the MediaWiki core, by replacing them with either...
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
2 /**
3 * See deferred.txt
4 * @package MediaWiki
5 */
6
7 /**
8 * @todo document
9 * @package MediaWiki
10 */
11 class LinksUpdate {
12
13 /**@{{
14 * @private
15 */
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 /**@}}*/
28
29 /**
30 * Constructor
31 * Initialize private variables
32 * @param $title Integer: FIXME
33 * @param $parserOutput FIXME
34 * @param $recursive Boolean: FIXME, default 'true'.
35 */
36 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
37 global $wgAntiLockFlags;
38
39 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
40 $this->mOptions = array();
41 } else {
42 $this->mOptions = array( 'FOR UPDATE' );
43 }
44 $this->mDb =& wfGetDB( DB_MASTER );
45
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" );
49 }
50 $this->mTitle = $title;
51 $this->mId = $title->getArticleID();
52
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();
58
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;
68 }
69
70 $this->mRecursive = $recursive;
71 }
72
73 /**
74 * Update link tables with outgoing links from an updated article
75 */
76 function doUpdate() {
77 global $wgUseDumbLinkUpdate;
78 if ( $wgUseDumbLinkUpdate ) {
79 $this->doDumbUpdate();
80 } else {
81 $this->doIncrementalUpdate();
82 }
83 }
84
85 function doIncrementalUpdate() {
86 $fname = 'LinksUpdate::doIncrementalUpdate';
87 wfProfileIn( $fname );
88
89 # Page links
90 $existing = $this->getExistingLinks();
91 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
92 $this->getLinkInsertions( $existing ) );
93
94 # Image links
95 $existing = $this->getExistingImages();
96 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
97 $this->getImageInsertions( $existing ) );
98
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 );
102
103 # External links
104 $existing = $this->getExistingExternals();
105 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
106 $this->getExternalInsertions( $existing ) );
107
108 # Language links
109 $existing = $this->getExistingInterlangs();
110 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
111 $this->getInterlangInsertions( $existing ) );
112
113 # Template links
114 $existing = $this->getExistingTemplates();
115 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
116 $this->getTemplateInsertions( $existing ) );
117
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 );
123 }
124 }
125
126 # Category links
127 $existing = $this->getExistingCategories();
128 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
129 $this->getCategoryInsertions( $existing ) );
130
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 );
134
135 wfProfileOut( $fname );
136 }
137
138 /**
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
142 */
143 function doDumbUpdate() {
144 $fname = 'LinksUpdate::doDumbUpdate';
145 wfProfileIn( $fname );
146
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 );
152
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 );
158 }
159 }
160
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' );
167
168 # Update the cache of all the category pages and image description pages which were changed
169 $this->invalidateCategories( $categoryUpdates );
170 $this->invalidateImageDescriptions( $imageUpdates );
171
172 wfProfileOut( $fname );
173 }
174
175 /**
176 * Invalidate the cache of a list of pages from a single namespace
177 *
178 * @param integer $namespace
179 * @param array $dbkeys
180 */
181 function invalidatePages( $namespace, $dbkeys ) {
182 $fname = 'LinksUpdate::invalidatePages';
183
184 if ( !count( $dbkeys ) ) {
185 return;
186 }
187
188 /**
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
192 */
193 $now = $this->mDb->timestamp();
194 $ids = array();
195 $res = $this->mDb->select( 'page', array( 'page_id' ),
196 array(
197 'page_namespace' => $namespace,
198 'page_title IN (' . $this->mDb->makeList( $dbkeys ) . ')',
199 'page_touched < ' . $this->mDb->addQuotes( $now )
200 ), $fname
201 );
202 while ( $row = $this->mDb->fetchObject( $res ) ) {
203 $ids[] = $row->page_id;
204 }
205 if ( !count( $ids ) ) {
206 return;
207 }
208
209 /**
210 * Do the update
211 * We still need the page_touched condition, in case the row has changed since
212 * the non-locking select above.
213 */
214 $this->mDb->update( 'page', array( 'page_touched' => $now ),
215 array(
216 'page_id IN (' . $this->mDb->makeList( $ids ) . ')',
217 'page_touched < ' . $this->mDb->addQuotes( $now )
218 ), $fname
219 );
220 }
221
222 function invalidateCategories( $cats ) {
223 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
224 }
225
226 function invalidateImageDescriptions( $images ) {
227 $this->invalidatePages( NS_IMAGE, array_keys( $images ) );
228 }
229
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' ) );
238 }
239 }
240
241 /**
242 * Make a WHERE clause from a 2-d NS/dbkey array
243 *
244 * @param array $arr 2-d array indexed by namespace and DB key
245 * @param string $prefix Field name prefix, without the underscore
246 */
247 function makeWhereFrom2d( &$arr, $prefix ) {
248 $lb = new LinkBatch;
249 $lb->setArray( $arr );
250 return $lb->constructSet( $prefix, $this->mDb );
251 }
252
253 /**
254 * Update a table by doing a delete query then an insert query
255 * @private
256 */
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 );
262 if ( $clause ) {
263 $where[] = $clause;
264 } else {
265 $where = false;
266 }
267 } else {
268 if ( $table == 'langlinks' ) {
269 $toField = 'll_lang';
270 } else {
271 $toField = $prefix . '_to';
272 }
273 if ( count( $deletions ) ) {
274 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
275 } else {
276 $where = false;
277 }
278 }
279 if ( $where ) {
280 $this->mDb->delete( $table, $where, $fname );
281 }
282 if ( count( $insertions ) ) {
283 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
284 }
285 }
286
287
288 /**
289 * Get an array of pagelinks insertions for passing to the DB
290 * Skips the titles specified by the 2-D array $existing
291 * @private
292 */
293 function getLinkInsertions( $existing = array() ) {
294 $arr = 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 ) {
300 $arr[] = array(
301 'pl_from' => $this->mId,
302 'pl_namespace' => $ns,
303 'pl_title' => $dbk
304 );
305 }
306 }
307 return $arr;
308 }
309
310 /**
311 * Get an array of template insertions. Like getLinkInsertions()
312 * @private
313 */
314 function getTemplateInsertions( $existing = array() ) {
315 $arr = 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 ) {
319 $arr[] = array(
320 'tl_from' => $this->mId,
321 'tl_namespace' => $ns,
322 'tl_title' => $dbk
323 );
324 }
325 }
326 return $arr;
327 }
328
329 /**
330 * Get an array of image insertions
331 * Skips the names specified in $existing
332 * @private
333 */
334 function getImageInsertions( $existing = array() ) {
335 $arr = array();
336 $diffs = array_diff_key( $this->mImages, $existing );
337 foreach( $diffs as $iname => $dummy ) {
338 $arr[] = array(
339 'il_from' => $this->mId,
340 'il_to' => $iname
341 );
342 }
343 return $arr;
344 }
345
346 /**
347 * Get an array of externallinks insertions. Skips the names specified in $existing
348 * @private
349 */
350 function getExternalInsertions( $existing = array() ) {
351 $arr = array();
352 $diffs = array_diff_key( $this->mExternals, $existing );
353 foreach( $diffs as $url => $dummy ) {
354 $arr[] = array(
355 'el_from' => $this->mId,
356 'el_to' => $url,
357 'el_index' => wfMakeUrlIndex( $url ),
358 );
359 }
360 return $arr;
361 }
362
363 /**
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
367 * @private
368 */
369 function getCategoryInsertions( $existing = array() ) {
370 $diffs = array_diff_assoc( $this->mCategories, $existing );
371 $arr = array();
372 foreach ( $diffs as $name => $sortkey ) {
373 $arr[] = array(
374 'cl_from' => $this->mId,
375 'cl_to' => $name,
376 'cl_sortkey' => $sortkey,
377 'cl_timestamp' => $this->mDb->timestamp()
378 );
379 }
380 return $arr;
381 }
382
383 /**
384 * Get an array of interlanguage link insertions
385 * @param array $existing Array mapping existing language codes to titles
386 * @private
387 */
388 function getInterlangInsertions( $existing = array() ) {
389 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
390 $arr = array();
391 foreach( $diffs as $lang => $title ) {
392 $arr[] = array(
393 'll_from' => $this->mId,
394 'll_lang' => $lang,
395 'll_title' => $title
396 );
397 }
398 return $arr;
399 }
400
401 /**
402 * Given an array of existing links, returns those links which are not in $this
403 * and thus should be deleted.
404 * @private
405 */
406 function getLinkDeletions( $existing ) {
407 $del = array();
408 foreach ( $existing as $ns => $dbkeys ) {
409 if ( isset( $this->mLinks[$ns] ) ) {
410 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
411 } else {
412 $del[$ns] = $existing[$ns];
413 }
414 }
415 return $del;
416 }
417
418 /**
419 * Given an array of existing templates, returns those templates which are not in $this
420 * and thus should be deleted.
421 * @private
422 */
423 function getTemplateDeletions( $existing ) {
424 $del = array();
425 foreach ( $existing as $ns => $dbkeys ) {
426 if ( isset( $this->mTemplates[$ns] ) ) {
427 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
428 } else {
429 $del[$ns] = $existing[$ns];
430 }
431 }
432 return $del;
433 }
434
435 /**
436 * Given an array of existing images, returns those images which are not in $this
437 * and thus should be deleted.
438 * @private
439 */
440 function getImageDeletions( $existing ) {
441 return array_diff_key( $existing, $this->mImages );
442 }
443
444 /**
445 * Given an array of existing external links, returns those links which are not
446 * in $this and thus should be deleted.
447 * @private
448 */
449 function getExternalDeletions( $existing ) {
450 return array_diff_key( $existing, $this->mExternals );
451 }
452
453 /**
454 * Given an array of existing categories, returns those categories which are not in $this
455 * and thus should be deleted.
456 * @private
457 */
458 function getCategoryDeletions( $existing ) {
459 return array_diff_assoc( $existing, $this->mCategories );
460 }
461
462 /**
463 * Given an array of existing interlanguage links, returns those links which are not
464 * in $this and thus should be deleted.
465 * @private
466 */
467 function getInterlangDeletions( $existing ) {
468 return array_diff_assoc( $existing, $this->mInterlangs );
469 }
470
471 /**
472 * Get an array of existing links, as a 2-D array
473 * @private
474 */
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 );
479 $arr = array();
480 while ( $row = $this->mDb->fetchObject( $res ) ) {
481 if ( !isset( $arr[$row->pl_namespace] ) ) {
482 $arr[$row->pl_namespace] = array();
483 }
484 $arr[$row->pl_namespace][$row->pl_title] = 1;
485 }
486 $this->mDb->freeResult( $res );
487 return $arr;
488 }
489
490 /**
491 * Get an array of existing templates, as a 2-D array
492 * @private
493 */
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 );
498 $arr = array();
499 while ( $row = $this->mDb->fetchObject( $res ) ) {
500 if ( !isset( $arr[$row->tl_namespace] ) ) {
501 $arr[$row->tl_namespace] = array();
502 }
503 $arr[$row->tl_namespace][$row->tl_title] = 1;
504 }
505 $this->mDb->freeResult( $res );
506 return $arr;
507 }
508
509 /**
510 * Get an array of existing images, image names in the keys
511 * @private
512 */
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 );
517 $arr = array();
518 while ( $row = $this->mDb->fetchObject( $res ) ) {
519 $arr[$row->il_to] = 1;
520 }
521 $this->mDb->freeResult( $res );
522 return $arr;
523 }
524
525 /**
526 * Get an array of existing external links, URLs in the keys
527 * @private
528 */
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 );
533 $arr = array();
534 while ( $row = $this->mDb->fetchObject( $res ) ) {
535 $arr[$row->el_to] = 1;
536 }
537 $this->mDb->freeResult( $res );
538 return $arr;
539 }
540
541 /**
542 * Get an array of existing categories, with the name in the key and sort key in the value.
543 * @private
544 */
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 );
549 $arr = array();
550 while ( $row = $this->mDb->fetchObject( $res ) ) {
551 $arr[$row->cl_to] = $row->cl_sortkey;
552 }
553 $this->mDb->freeResult( $res );
554 return $arr;
555 }
556
557 /**
558 * Get an array of existing interlanguage links, with the language code in the key and the
559 * title in the value.
560 * @private
561 */
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 );
566 $arr = array();
567 while ( $row = $this->mDb->fetchObject( $res ) ) {
568 $arr[$row->ll_lang] = $row->ll_title;
569 }
570 return $arr;
571 }
572 }
573 ?>