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