Revert to r14512; domas introduced massive breakage with incomplete experimental...
[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 wfDebugDieBacktrace( "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 require_once( 'JobQueue.php' );
123 Job::queueLinksJobs( $tlto );
124 }
125 }
126
127 # Category links
128 $existing = $this->getExistingCategories();
129 $this->incrTableUpdate( 'categorylinks', 'cl', $this->getCategoryDeletions( $existing ),
130 $this->getCategoryInsertions( $existing ) );
131
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 );
135
136 wfProfileOut( $fname );
137 }
138
139 /**
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
143 */
144 function doDumbUpdate() {
145 $fname = 'LinksUpdate::doDumbUpdate';
146 wfProfileIn( $fname );
147
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 );
153
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 );
160 }
161 }
162
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' );
169
170 # Update the cache of all the category pages and image description pages which were changed
171 $this->invalidateCategories( $categoryUpdates );
172 $this->invalidateImageDescriptions( $imageUpdates );
173
174 wfProfileOut( $fname );
175 }
176
177 /**
178 * Invalidate the cache of a list of pages from a single namespace
179 *
180 * @param integer $namespace
181 * @param array $dbkeys
182 */
183 function invalidatePages( $namespace, $dbkeys ) {
184 $fname = 'LinksUpdate::invalidatePages';
185
186 if ( !count( $dbkeys ) ) {
187 return;
188 }
189
190 /**
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
194 */
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
203 );
204 while ( $row = $this->mDb->fetchObject( $res ) ) {
205 $ids[] = $row->page_id;
206 }
207 if ( !count( $ids ) ) {
208 return;
209 }
210
211 /**
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.
215 */
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
221 );
222 }
223
224 function invalidateCategories( $cats ) {
225 $this->invalidatePages( NS_CATEGORY, array_keys( $cats ) );
226 }
227
228 function invalidateImageDescriptions( $images ) {
229 $this->invalidatePages( NS_IMAGE, array_keys( $images ) );
230 }
231
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' ) );
240 }
241 }
242
243 /**
244 * Make a WHERE clause from a 2-d NS/dbkey array
245 *
246 * @param array $arr 2-d array indexed by namespace and DB key
247 * @param string $prefix Field name prefix, without the underscore
248 */
249 function makeWhereFrom2d( &$arr, $prefix ) {
250 $lb = new LinkBatch;
251 $lb->setArray( $arr );
252 return $lb->constructSet( $prefix, $this->mDb );
253 }
254
255 /**
256 * Update a table by doing a delete query then an insert query
257 * @private
258 */
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;
268 }
269 } else {
270 if ( $table == 'langlinks' ) {
271 $toField = 'll_lang';
272 } else {
273 $toField = $prefix . '_to';
274 }
275 if ( count( $deletions ) ) {
276 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
277 } else {
278 $where = false;
279 }
280 }
281 if ( $where ) {
282 $this->mDb->delete( $table, $where, $fname );
283 }
284 if ( count( $insertions ) ) {
285 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
286 }
287 }
288
289
290 /**
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
294 */
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
306 );
307 }
308 }
309 return $arr;
310 }
311
312 /**
313 * Get an array of template insertions. Like getLinkInsertions()
314 * @private
315 */
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
325 );
326 }
327 }
328 return $arr;
329 }
330
331 /**
332 * Get an array of image insertions
333 * Skips the names specified in $existing
334 * @private
335 */
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
343 );
344 }
345 return $arr;
346 }
347
348 /**
349 * Get an array of externallinks insertions. Skips the names specified in $existing
350 * @private
351 */
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 ),
360 );
361 }
362 return $arr;
363 }
364
365 /**
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
370 */
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()
380 );
381 }
382 return $arr;
383 }
384
385 /**
386 * Get an array of interlanguage link insertions
387 * @param array $existing Array mapping existing language codes to titles
388 * @private
389 */
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
398 );
399 }
400 return $arr;
401 }
402
403 /**
404 * Given an array of existing links, returns those links which are not in $this
405 * and thus should be deleted.
406 * @private
407 */
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];
415 }
416 }
417 return $del;
418 }
419
420 /**
421 * Given an array of existing templates, returns those templates which are not in $this
422 * and thus should be deleted.
423 * @private
424 */
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];
432 }
433 }
434 return $del;
435 }
436
437 /**
438 * Given an array of existing images, returns those images which are not in $this
439 * and thus should be deleted.
440 * @private
441 */
442 function getImageDeletions( $existing ) {
443 return array_diff_key( $existing, $this->mImages );
444 }
445
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
450 */
451 function getExternalDeletions( $existing ) {
452 return array_diff_key( $existing, $this->mExternals );
453 }
454
455 /**
456 * Given an array of existing categories, returns those categories which are not in $this
457 * and thus should be deleted.
458 * @private
459 */
460 function getCategoryDeletions( $existing ) {
461 return array_diff_assoc( $existing, $this->mCategories );
462 }
463
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
468 */
469 function getInterlangDeletions( $existing ) {
470 return array_diff_assoc( $existing, $this->mInterlangs );
471 }
472
473 /**
474 * Get an array of existing links, as a 2-D array
475 * @private
476 */
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();
485 }
486 $arr[$row->pl_namespace][$row->pl_title] = 1;
487 }
488 $this->mDb->freeResult( $res );
489 return $arr;
490 }
491
492 /**
493 * Get an array of existing templates, as a 2-D array
494 * @private
495 */
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();
504 }
505 $arr[$row->tl_namespace][$row->tl_title] = 1;
506 }
507 $this->mDb->freeResult( $res );
508 return $arr;
509 }
510
511 /**
512 * Get an array of existing images, image names in the keys
513 * @private
514 */
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;
522 }
523 $this->mDb->freeResult( $res );
524 return $arr;
525 }
526
527 /**
528 * Get an array of existing external links, URLs in the keys
529 * @private
530 */
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;
538 }
539 $this->mDb->freeResult( $res );
540 return $arr;
541 }
542
543 /**
544 * Get an array of existing categories, with the name in the key and sort key in the value.
545 * @private
546 */
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;
554 }
555 $this->mDb->freeResult( $res );
556 return $arr;
557 }
558
559 /**
560 * Get an array of existing interlanguage links, with the language code in the key and the
561 * title in the value.
562 * @private
563 */
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;
571 }
572 return $arr;
573 }
574 }
575 ?>