langlinks table
[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 * @access 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 integer $id
33 * @param string $title
34 */
35 function LinksUpdate( $title, $parserOutput, $recursive = true ) {
36 global $wgAntiLockFlags;
37
38 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK ) {
39 $this->mOptions = array();
40 } else {
41 $this->mOptions = array( 'FOR UPDATE' );
42 }
43 $this->mDb =& wfGetDB( DB_MASTER );
44
45 if ( !is_object( $title ) ) {
46 wfDebugDieBacktrace( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
47 "Please see Article::editUpdates() for an invocation example.\n" );
48 }
49 $this->mTitle = $title;
50 $this->mId = $title->getArticleID();
51
52 $this->mLinks = $parserOutput->getLinks();
53 $this->mImages = $parserOutput->getImages();
54 $this->mTemplates = $parserOutput->getTemplates();
55 $this->mExternals = $parserOutput->getExternalLinks();
56 $this->mCategories = $parserOutput->getCategories();
57
58 # Convert the format of the interlanguage links
59 # I didn't want to change it in the ParserOutput, because that array is passed all
60 # the way back to the skin, so either a skin API break would be required, or an
61 # inefficient back-conversion.
62 $ill = $parserOutput->getLanguageLinks();
63 $this->mInterlangs = array();
64 foreach ( $ill as $link ) {
65 list( $key, $title ) = explode( ':', $link, 2 );
66 $this->mInterlangs[$key] = $title;
67 }
68
69 $this->mRecursive = $recursive;
70 }
71
72 /**
73 * Update link tables with outgoing links from an updated article
74 */
75 function doUpdate() {
76 global $wgUseDumbLinkUpdate;
77 if ( $wgUseDumbLinkUpdate ) {
78 $this->doDumbUpdate();
79 } else {
80 $this->doIncrementalUpdate();
81 }
82 }
83
84 function doIncrementalUpdate() {
85 $fname = 'LinksUpdate::doIncrementalUpdate';
86 wfProfileIn( $fname );
87
88 # Page links
89 $existing = $this->getExistingLinks();
90 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
91 $this->getLinkInsertions( $existing ) );
92
93 # Image links
94 $existing = $this->getExistingImages();
95 $this->incrTableUpdate( 'imagelinks', 'il', $this->getImageDeletions( $existing ),
96 $this->getImageInsertions( $existing ) );
97
98 # Invalidate all image description pages which had links added or removed
99 $imageUpdates = array_diff_key( $existing, $this->mImages ) + array_diff_key( $this->mImages, $existing );
100 $this->invalidateImageDescriptions( $imageUpdates );
101
102 # External links
103 $existing = $this->getExistingExternals();
104 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
105 $this->getExternalInsertions( $existing ) );
106
107 # Language links
108 $existing = $this->getExistingInterlangs();
109 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
110 $this->getInterlangInsertions( $existing ) );
111
112 # Template links
113 $existing = $this->getExistingTemplates();
114 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
115 $this->getTemplateInsertions( $existing ) );
116
117 # Refresh links of all pages including this page
118 if ( $this->mRecursive ) {
119 $tlto = $this->mTitle->getTemplateLinksTo();
120 if ( count( $tlto ) ) {
121 require_once( 'JobQueue.php' );
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 require_once( 'JobQueue.php' );
158 Job::queueLinksJobs( $tlto );
159 }
160 }
161
162 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
163 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
164 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
165 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
166 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
167 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(), 'll_from' );
168
169 # Update the cache of all the category pages and image description pages which were changed
170 $this->invalidateCategories( $categoryUpdates );
171 $this->invalidateImageDescriptions( $imageUpdates );
172
173 wfProfileOut( $fname );
174 }
175
176 function invalidateCategories( $cats ) {
177 $fname = 'LinksUpdate::invalidateCategories';
178 if ( count( $cats ) ) {
179 $this->mDb->update( 'page', array( 'page_touched' => $this->mDb->timestamp() ),
180 array(
181 'page_namespace' => NS_CATEGORY,
182 'page_title IN (' . $this->mDb->makeList( array_keys( $cats ) ) . ')'
183 ), $fname
184 );
185 }
186 }
187
188 function invalidateImageDescriptions( $images ) {
189 $fname = 'LinksUpdate::invalidateImageDescriptions';
190 if ( count( $images ) ) {
191 $this->mDb->update( 'page', array( 'page_touched' => $this->mDb->timestamp() ),
192 array(
193 'page_namespace' => NS_IMAGE,
194 'page_title IN (' . $this->mDb->makeList( array_keys( $images ) ) . ')'
195 ), $fname
196 );
197 }
198 }
199
200 function dumbTableUpdate( $table, $insertions, $fromField ) {
201 $fname = 'LinksUpdate::dumbTableUpdate';
202 $this->mDb->delete( $table, array( $fromField => $this->mId ), $fname );
203 if ( count( $insertions ) ) {
204 # The link array was constructed without FOR UPDATE, so there may be collisions
205 # This may cause minor link table inconsistencies, which is better than
206 # crippling the site with lock contention.
207 $this->mDb->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
208 }
209 }
210
211 /**
212 * Make a WHERE clause from a 2-d NS/dbkey array
213 *
214 * @param array $arr 2-d array indexed by namespace and DB key
215 * @param string $prefix Field name prefix, without the underscore
216 */
217 function makeWhereFrom2d( &$arr, $prefix ) {
218 $lb = new LinkBatch;
219 $lb->setArray( $arr );
220 return $lb->constructSet( $prefix, $this->mDb );
221 }
222
223 /**
224 * Update a table by doing a delete query then an insert query
225 * @access private
226 */
227 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
228 $fname = 'LinksUpdate::incrTableUpdate';
229 $where = array( "{$prefix}_from" => $this->mId );
230 if ( $table == 'pagelinks' || $table == 'templatelinks' ) {
231 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
232 if ( $clause ) {
233 $where[] = $clause;
234 } else {
235 $where = false;
236 }
237 } else {
238 if ( $table == 'langlinks' ) {
239 $toField = 'll_lang';
240 } else {
241 $toField = $prefix . '_to';
242 }
243 if ( count( $deletions ) ) {
244 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
245 } else {
246 $where = false;
247 }
248 }
249 if ( $where ) {
250 $this->mDb->delete( $table, $where, $fname );
251 }
252 if ( count( $insertions ) ) {
253 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
254 }
255 }
256
257
258 /**
259 * Get an array of pagelinks insertions for passing to the DB
260 * Skips the titles specified by the 2-D array $existing
261 * @access private
262 */
263 function getLinkInsertions( $existing = array() ) {
264 $arr = array();
265 foreach( $this->mLinks as $ns => $dbkeys ) {
266 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
267 # in GlobalFunctions.php
268 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
269 foreach ( $diffs as $dbk => $id ) {
270 $arr[] = array(
271 'pl_from' => $this->mId,
272 'pl_namespace' => $ns,
273 'pl_title' => $dbk
274 );
275 }
276 }
277 return $arr;
278 }
279
280 /**
281 * Get an array of template insertions. Like getLinkInsertions()
282 * @access private
283 */
284 function getTemplateInsertions( $existing = array() ) {
285 $arr = array();
286 foreach( $this->mTemplates as $ns => $dbkeys ) {
287 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
288 foreach ( $diffs as $dbk => $id ) {
289 $arr[] = array(
290 'tl_from' => $this->mId,
291 'tl_namespace' => $ns,
292 'tl_title' => $dbk
293 );
294 }
295 }
296 return $arr;
297 }
298
299 /**
300 * Get an array of image insertions
301 * Skips the names specified in $existing
302 * @access private
303 */
304 function getImageInsertions( $existing = array() ) {
305 $arr = array();
306 $diffs = array_diff_key( $this->mImages, $existing );
307 foreach( $diffs as $iname => $dummy ) {
308 $arr[] = array(
309 'il_from' => $this->mId,
310 'il_to' => $iname
311 );
312 }
313 return $arr;
314 }
315
316 /**
317 * Get an array of externallinks insertions. Skips the names specified in $existing
318 * @access private
319 */
320 function getExternalInsertions( $existing = array() ) {
321 $arr = array();
322 $diffs = array_diff_key( $this->mExternals, $existing );
323 foreach( $diffs as $url => $dummy ) {
324 $arr[] = array(
325 'el_from' => $this->mId,
326 'el_to' => $url,
327 'el_index' => wfMakeUrlIndex( $url ),
328 );
329 }
330 return $arr;
331 }
332
333 /**
334 * Get an array of category insertions
335 * @param array $existing Array mapping existing category names to sort keys. If both
336 * match a link in $this, the link will be omitted from the output
337 * @access private
338 */
339 function getCategoryInsertions( $existing = array() ) {
340 $diffs = array_diff_assoc( $this->mCategories, $existing );
341 $arr = array();
342 foreach ( $diffs as $name => $sortkey ) {
343 $arr[] = array(
344 'cl_from' => $this->mId,
345 'cl_to' => $name,
346 'cl_sortkey' => $sortkey,
347 'cl_timestamp' => $this->mDb->timestamp()
348 );
349 }
350 return $arr;
351 }
352
353 /**
354 * Get an array of interlanguage link insertions
355 * @param array $existing Array mapping existing language codes to titles
356 * @access private
357 */
358 function getInterlangInsertions( $existing = array() ) {
359 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
360 $arr = array();
361 foreach( $diffs as $lang => $title ) {
362 $arr[] = array(
363 'll_from' => $this->mId,
364 'll_lang' => $lang,
365 'll_title' => $title
366 );
367 }
368 return $arr;
369 }
370
371 /**
372 * Given an array of existing links, returns those links which are not in $this
373 * and thus should be deleted.
374 * @access private
375 */
376 function getLinkDeletions( $existing ) {
377 $del = array();
378 foreach ( $existing as $ns => $dbkeys ) {
379 if ( isset( $this->mLinks[$ns] ) ) {
380 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
381 } else {
382 $del[$ns] = $existing[$ns];
383 }
384 }
385 return $del;
386 }
387
388 /**
389 * Given an array of existing templates, returns those templates which are not in $this
390 * and thus should be deleted.
391 * @access private
392 */
393 function getTemplateDeletions( $existing ) {
394 $del = array();
395 foreach ( $existing as $ns => $dbkeys ) {
396 if ( isset( $this->mTemplates[$ns] ) ) {
397 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
398 } else {
399 $del[$ns] = $existing[$ns];
400 }
401 }
402 return $del;
403 }
404
405 /**
406 * Given an array of existing images, returns those images which are not in $this
407 * and thus should be deleted.
408 * @access private
409 */
410 function getImageDeletions( $existing ) {
411 return array_diff_key( $existing, $this->mImages );
412 }
413
414 /**
415 * Given an array of existing external links, returns those links which are not
416 * in $this and thus should be deleted.
417 * @access private
418 */
419 function getExternalDeletions( $existing ) {
420 return array_diff_key( $existing, $this->mExternals );
421 }
422
423 /**
424 * Given an array of existing categories, returns those categories which are not in $this
425 * and thus should be deleted.
426 * @access private
427 */
428 function getCategoryDeletions( $existing ) {
429 return array_diff_assoc( $existing, $this->mCategories );
430 }
431
432 /**
433 * Given an array of existing interlanguage links, returns those links which are not
434 * in $this and thus should be deleted.
435 * @access private
436 */
437 function getInterlangDeletions( $existing ) {
438 return array_diff_assoc( $existing, $this->mInterlangs );
439 }
440
441 /**
442 * Get an array of existing links, as a 2-D array
443 * @access private
444 */
445 function getExistingLinks() {
446 $fname = 'LinksUpdate::getExistingLinks';
447 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
448 array( 'pl_from' => $this->mId ), $fname, $this->mOptions );
449 $arr = array();
450 while ( $row = $this->mDb->fetchObject( $res ) ) {
451 if ( !isset( $arr[$row->pl_namespace] ) ) {
452 $arr[$row->pl_namespace] = array();
453 }
454 $arr[$row->pl_namespace][$row->pl_title] = 1;
455 }
456 $this->mDb->freeResult( $res );
457 return $arr;
458 }
459
460 /**
461 * Get an array of existing templates, as a 2-D array
462 * @access private
463 */
464 function getExistingTemplates() {
465 $fname = 'LinksUpdate::getExistingTemplates';
466 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
467 array( 'tl_from' => $this->mId ), $fname, $this->mOptions );
468 $arr = array();
469 while ( $row = $this->mDb->fetchObject( $res ) ) {
470 if ( !isset( $arr[$row->tl_namespace] ) ) {
471 $arr[$row->tl_namespace] = array();
472 }
473 $arr[$row->tl_namespace][$row->tl_title] = 1;
474 }
475 $this->mDb->freeResult( $res );
476 return $arr;
477 }
478
479 /**
480 * Get an array of existing images, image names in the keys
481 * @access private
482 */
483 function getExistingImages() {
484 $fname = 'LinksUpdate::getExistingImages';
485 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
486 array( 'il_from' => $this->mId ), $fname, $this->mOptions );
487 $arr = array();
488 while ( $row = $this->mDb->fetchObject( $res ) ) {
489 $arr[$row->il_to] = 1;
490 }
491 $this->mDb->freeResult( $res );
492 return $arr;
493 }
494
495 /**
496 * Get an array of existing external links, URLs in the keys
497 * @access private
498 */
499 function getExistingExternals() {
500 $fname = 'LinksUpdate::getExistingExternals';
501 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
502 array( 'el_from' => $this->mId ), $fname, $this->mOptions );
503 $arr = array();
504 while ( $row = $this->mDb->fetchObject( $res ) ) {
505 $arr[$row->el_to] = 1;
506 }
507 $this->mDb->freeResult( $res );
508 return $arr;
509 }
510
511 /**
512 * Get an array of existing categories, with the name in the key and sort key in the value.
513 * @access private
514 */
515 function getExistingCategories() {
516 $fname = 'LinksUpdate::getExistingCategories';
517 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
518 array( 'cl_from' => $this->mId ), $fname, $this->mOptions );
519 $arr = array();
520 while ( $row = $this->mDb->fetchObject( $res ) ) {
521 $arr[$row->cl_to] = $row->cl_sortkey;
522 }
523 $this->mDb->freeResult( $res );
524 return $arr;
525 }
526
527 /**
528 * Get an array of existing interlanguage links, with the language code in the key and the
529 * title in the value.
530 * @access private
531 */
532 function getExistingInterlangs() {
533 $fname = 'LinksUpdate::getExistingInterlangs';
534 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
535 array( 'll_from' => $this->mId ), $fname, $this->mOptions );
536 $arr = array();
537 while ( $row = $this->mDb->fetchObject( $res ) ) {
538 $arr[$row->ll_lang] = $row->ll_title;
539 }
540 return $arr;
541 }
542 }
543 ?>