Two new parser tests related to bug 6200
[lhc/web/wiklou.git] / maintenance / refreshCategoryCounts.php
1 <?php
2 /**
3 * This script will refresh the cat_pages, cat_subcats and cat_files fields of
4 * the category table, which may be incorrect if the wiki ran the corrupted
5 * version of Article::doDeleteArticle (r40912 --> r47326); see explanation at
6 * [https://bugzilla.wikimedia.org/show_bug.cgi?id=17155]. It will print out
7 * progress indicators every 1000 categories it updates. You may want to use the
8 * throttling options if it's causing too much load; they will not affect
9 * correctness.
10 *
11 * If the script is stopped and later resumed, you can use the --start option
12 * with the last printed progress indicator to pick up where you left off.
13 * This is safe, because any newly-added categories will be added at the end of
14 * the table.
15 *
16 * @file
17 * @ingroup Maintenance
18 * @author Happy-melon, Max Semenik
19 * Based on /maintenance/populateCategory.php by Simetrical.
20 */
21
22 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
23
24 class RefreshCategoryCounts extends Maintenance {
25 const REPORTING_INTERVAL = 1000;
26
27 public function __construct() {
28 $this->mDescription = 'Refreshes category counts';
29 $this->addOption( 'start', 'Start from this category ID', false, true );
30 $this->addOption( 'maxlag', 'Maximum database slave lag in seconds (5 by default)', false, true );
31 $this->addOption( 'throttle', 'Optional delay after every processed category in milliseconds',
32 false, true );
33 }
34
35 public function execute() {
36 $start = intval( $this->getOption( 'start', 0 ) );
37 $maxlag = intval( $this->getOption( 'maxlag', 5 ) );
38 $throttle = intval( $this->getOption( 'throttle', 0 ) );
39
40 $this->doRefresh( $start, $maxlag, $throttle );
41 }
42
43 protected function doRefresh( $start, $maxlag, $throttle ) {
44 $dbw = wfGetDB( DB_MASTER );
45
46 $maxlag = intval( $maxlag );
47 $throttle = intval( $throttle );
48 $id = $start;
49
50 $i = 0;
51 while ( true ) {
52 # Find which category to update
53 $row = $dbw->selectRow(
54 'category',
55 array( 'cat_id', 'cat_title' ),
56 'cat_id > ' . $dbw->addQuotes( $id ),
57 __METHOD__,
58 array( 'ORDER BY' => 'cat_id' )
59 );
60 if ( !$row ) {
61 # Done, hopefully.
62 break;
63 }
64 $id = $row->cat_id;
65 $name = $row->cat_title;
66
67 # Use the row to update the category count
68 $cat = Category::newFromName( $name );
69 if ( !is_object( $cat ) ) {
70 $this->output( "Invalid category name '$name'\n" );
71 } else {
72 $cat->refreshCounts();
73 }
74
75 $i++;
76 if ( !( $i % self::REPORTING_INTERVAL ) ) {
77 $this->output( "$id\n" );
78 wfWaitForSlaves( $maxlag );
79 }
80 usleep( $throttle * 1000 );
81 }
82
83 /*if ( $dbw->insert(
84 'updatelog',
85 array( 'ul_key' => 'refresh catgory counts' ),
86 __METHOD__,
87 'IGNORE'
88 )
89 ) {
90 $this->output( "Category count refresh complete.\n" );
91 return true;
92 } else {
93 $this->output( "Could not insert category population row.\n" );
94 return false;
95 }*/
96 $this->output( "Category count refresh complete.\n" );
97 }
98 }
99
100 $maintClass = "RefreshCategoryCounts";
101 require_once( DO_MAINTENANCE );
102