Switching from phpdoc to doxygen (use less than 32MB of memory).
[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 function invalidateCategories( $cats ) {
178 $fname = 'LinksUpdate::invalidateCategories';
179 if ( count( $cats ) ) {
180 $this->mDb->update( 'page', array( 'page_touched' => $this->mDb->timestamp() ),
181 array(
182 'page_namespace' => NS_CATEGORY,
183 'page_title IN (' . $this->mDb->makeList( array_keys( $cats ) ) . ')'
184 ), $fname
185 );
186 }
187 }
188
189 function invalidateImageDescriptions( $images ) {
190 $fname = 'LinksUpdate::invalidateImageDescriptions';
191 if ( count( $images ) ) {
192 $this->mDb->update( 'page', array( 'page_touched' => $this->mDb->timestamp() ),
193 array(
194 'page_namespace' => NS_IMAGE,
195 'page_title IN (' . $this->mDb->makeList( array_keys( $images ) ) . ')'
196 ), $fname
197 );
198 }
199 }
200
201 function dumbTableUpdate( $table, $insertions, $fromField ) {
202 $fname = 'LinksUpdate::dumbTableUpdate';
203 $this->mDb->delete( $table, array( $fromField => $this->mId ), $fname );
204 if ( count( $insertions ) ) {
205 # The link array was constructed without FOR UPDATE, so there may be collisions
206 # This may cause minor link table inconsistencies, which is better than
207 # crippling the site with lock contention.
208 $this->mDb->insert( $table, $insertions, $fname, array( 'IGNORE' ) );
209 }
210 }
211
212 /**
213 * Make a WHERE clause from a 2-d NS/dbkey array
214 *
215 * @param array $arr 2-d array indexed by namespace and DB key
216 * @param string $prefix Field name prefix, without the underscore
217 */
218 function makeWhereFrom2d( &$arr, $prefix ) {
219 $lb = new LinkBatch;
220 $lb->setArray( $arr );
221 return $lb->constructSet( $prefix, $this->mDb );
222 }
223
224 /**
225 * Update a table by doing a delete query then an insert query
226 * @private
227 */
228 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
229 $fname = 'LinksUpdate::incrTableUpdate';
230 $where = array( "{$prefix}_from" => $this->mId );
231 if ( $table == 'pagelinks' || $table == 'templatelinks' ) {
232 $clause = $this->makeWhereFrom2d( $deletions, $prefix );
233 if ( $clause ) {
234 $where[] = $clause;
235 } else {
236 $where = false;
237 }
238 } else {
239 if ( $table == 'langlinks' ) {
240 $toField = 'll_lang';
241 } else {
242 $toField = $prefix . '_to';
243 }
244 if ( count( $deletions ) ) {
245 $where[] = "$toField IN (" . $this->mDb->makeList( array_keys( $deletions ) ) . ')';
246 } else {
247 $where = false;
248 }
249 }
250 if ( $where ) {
251 $this->mDb->delete( $table, $where, $fname );
252 }
253 if ( count( $insertions ) ) {
254 $this->mDb->insert( $table, $insertions, $fname, 'IGNORE' );
255 }
256 }
257
258
259 /**
260 * Get an array of pagelinks insertions for passing to the DB
261 * Skips the titles specified by the 2-D array $existing
262 * @private
263 */
264 function getLinkInsertions( $existing = array() ) {
265 $arr = array();
266 foreach( $this->mLinks as $ns => $dbkeys ) {
267 # array_diff_key() was introduced in PHP 5.1, there is a compatibility function
268 # in GlobalFunctions.php
269 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
270 foreach ( $diffs as $dbk => $id ) {
271 $arr[] = array(
272 'pl_from' => $this->mId,
273 'pl_namespace' => $ns,
274 'pl_title' => $dbk
275 );
276 }
277 }
278 return $arr;
279 }
280
281 /**
282 * Get an array of template insertions. Like getLinkInsertions()
283 * @private
284 */
285 function getTemplateInsertions( $existing = array() ) {
286 $arr = array();
287 foreach( $this->mTemplates as $ns => $dbkeys ) {
288 $diffs = isset( $existing[$ns] ) ? array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
289 foreach ( $diffs as $dbk => $id ) {
290 $arr[] = array(
291 'tl_from' => $this->mId,
292 'tl_namespace' => $ns,
293 'tl_title' => $dbk
294 );
295 }
296 }
297 return $arr;
298 }
299
300 /**
301 * Get an array of image insertions
302 * Skips the names specified in $existing
303 * @private
304 */
305 function getImageInsertions( $existing = array() ) {
306 $arr = array();
307 $diffs = array_diff_key( $this->mImages, $existing );
308 foreach( $diffs as $iname => $dummy ) {
309 $arr[] = array(
310 'il_from' => $this->mId,
311 'il_to' => $iname
312 );
313 }
314 return $arr;
315 }
316
317 /**
318 * Get an array of externallinks insertions. Skips the names specified in $existing
319 * @private
320 */
321 function getExternalInsertions( $existing = array() ) {
322 $arr = array();
323 $diffs = array_diff_key( $this->mExternals, $existing );
324 foreach( $diffs as $url => $dummy ) {
325 $arr[] = array(
326 'el_from' => $this->mId,
327 'el_to' => $url,
328 'el_index' => wfMakeUrlIndex( $url ),
329 );
330 }
331 return $arr;
332 }
333
334 /**
335 * Get an array of category insertions
336 * @param array $existing Array mapping existing category names to sort keys. If both
337 * match a link in $this, the link will be omitted from the output
338 * @private
339 */
340 function getCategoryInsertions( $existing = array() ) {
341 $diffs = array_diff_assoc( $this->mCategories, $existing );
342 $arr = array();
343 foreach ( $diffs as $name => $sortkey ) {
344 $arr[] = array(
345 'cl_from' => $this->mId,
346 'cl_to' => $name,
347 'cl_sortkey' => $sortkey,
348 'cl_timestamp' => $this->mDb->timestamp()
349 );
350 }
351 return $arr;
352 }
353
354 /**
355 * Get an array of interlanguage link insertions
356 * @param array $existing Array mapping existing language codes to titles
357 * @private
358 */
359 function getInterlangInsertions( $existing = array() ) {
360 $diffs = array_diff_assoc( $this->mInterlangs, $existing );
361 $arr = array();
362 foreach( $diffs as $lang => $title ) {
363 $arr[] = array(
364 'll_from' => $this->mId,
365 'll_lang' => $lang,
366 'll_title' => $title
367 );
368 }
369 return $arr;
370 }
371
372 /**
373 * Given an array of existing links, returns those links which are not in $this
374 * and thus should be deleted.
375 * @private
376 */
377 function getLinkDeletions( $existing ) {
378 $del = array();
379 foreach ( $existing as $ns => $dbkeys ) {
380 if ( isset( $this->mLinks[$ns] ) ) {
381 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks[$ns] );
382 } else {
383 $del[$ns] = $existing[$ns];
384 }
385 }
386 return $del;
387 }
388
389 /**
390 * Given an array of existing templates, returns those templates which are not in $this
391 * and thus should be deleted.
392 * @private
393 */
394 function getTemplateDeletions( $existing ) {
395 $del = array();
396 foreach ( $existing as $ns => $dbkeys ) {
397 if ( isset( $this->mTemplates[$ns] ) ) {
398 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates[$ns] );
399 } else {
400 $del[$ns] = $existing[$ns];
401 }
402 }
403 return $del;
404 }
405
406 /**
407 * Given an array of existing images, returns those images which are not in $this
408 * and thus should be deleted.
409 * @private
410 */
411 function getImageDeletions( $existing ) {
412 return array_diff_key( $existing, $this->mImages );
413 }
414
415 /**
416 * Given an array of existing external links, returns those links which are not
417 * in $this and thus should be deleted.
418 * @private
419 */
420 function getExternalDeletions( $existing ) {
421 return array_diff_key( $existing, $this->mExternals );
422 }
423
424 /**
425 * Given an array of existing categories, returns those categories which are not in $this
426 * and thus should be deleted.
427 * @private
428 */
429 function getCategoryDeletions( $existing ) {
430 return array_diff_assoc( $existing, $this->mCategories );
431 }
432
433 /**
434 * Given an array of existing interlanguage links, returns those links which are not
435 * in $this and thus should be deleted.
436 * @private
437 */
438 function getInterlangDeletions( $existing ) {
439 return array_diff_assoc( $existing, $this->mInterlangs );
440 }
441
442 /**
443 * Get an array of existing links, as a 2-D array
444 * @private
445 */
446 function getExistingLinks() {
447 $fname = 'LinksUpdate::getExistingLinks';
448 $res = $this->mDb->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
449 array( 'pl_from' => $this->mId ), $fname, $this->mOptions );
450 $arr = array();
451 while ( $row = $this->mDb->fetchObject( $res ) ) {
452 if ( !isset( $arr[$row->pl_namespace] ) ) {
453 $arr[$row->pl_namespace] = array();
454 }
455 $arr[$row->pl_namespace][$row->pl_title] = 1;
456 }
457 $this->mDb->freeResult( $res );
458 return $arr;
459 }
460
461 /**
462 * Get an array of existing templates, as a 2-D array
463 * @private
464 */
465 function getExistingTemplates() {
466 $fname = 'LinksUpdate::getExistingTemplates';
467 $res = $this->mDb->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
468 array( 'tl_from' => $this->mId ), $fname, $this->mOptions );
469 $arr = array();
470 while ( $row = $this->mDb->fetchObject( $res ) ) {
471 if ( !isset( $arr[$row->tl_namespace] ) ) {
472 $arr[$row->tl_namespace] = array();
473 }
474 $arr[$row->tl_namespace][$row->tl_title] = 1;
475 }
476 $this->mDb->freeResult( $res );
477 return $arr;
478 }
479
480 /**
481 * Get an array of existing images, image names in the keys
482 * @private
483 */
484 function getExistingImages() {
485 $fname = 'LinksUpdate::getExistingImages';
486 $res = $this->mDb->select( 'imagelinks', array( 'il_to' ),
487 array( 'il_from' => $this->mId ), $fname, $this->mOptions );
488 $arr = array();
489 while ( $row = $this->mDb->fetchObject( $res ) ) {
490 $arr[$row->il_to] = 1;
491 }
492 $this->mDb->freeResult( $res );
493 return $arr;
494 }
495
496 /**
497 * Get an array of existing external links, URLs in the keys
498 * @private
499 */
500 function getExistingExternals() {
501 $fname = 'LinksUpdate::getExistingExternals';
502 $res = $this->mDb->select( 'externallinks', array( 'el_to' ),
503 array( 'el_from' => $this->mId ), $fname, $this->mOptions );
504 $arr = array();
505 while ( $row = $this->mDb->fetchObject( $res ) ) {
506 $arr[$row->el_to] = 1;
507 }
508 $this->mDb->freeResult( $res );
509 return $arr;
510 }
511
512 /**
513 * Get an array of existing categories, with the name in the key and sort key in the value.
514 * @private
515 */
516 function getExistingCategories() {
517 $fname = 'LinksUpdate::getExistingCategories';
518 $res = $this->mDb->select( 'categorylinks', array( 'cl_to', 'cl_sortkey' ),
519 array( 'cl_from' => $this->mId ), $fname, $this->mOptions );
520 $arr = array();
521 while ( $row = $this->mDb->fetchObject( $res ) ) {
522 $arr[$row->cl_to] = $row->cl_sortkey;
523 }
524 $this->mDb->freeResult( $res );
525 return $arr;
526 }
527
528 /**
529 * Get an array of existing interlanguage links, with the language code in the key and the
530 * title in the value.
531 * @private
532 */
533 function getExistingInterlangs() {
534 $fname = 'LinksUpdate::getExistingInterlangs';
535 $res = $this->mDb->select( 'langlinks', array( 'll_lang', 'll_title' ),
536 array( 'll_from' => $this->mId ), $fname, $this->mOptions );
537 $arr = array();
538 while ( $row = $this->mDb->fetchObject( $res ) ) {
539 $arr[$row->ll_lang] = $row->ll_title;
540 }
541 return $arr;
542 }
543 }
544 ?>