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