Various unused variables, add some braces
[lhc/web/wiklou.git] / maintenance / populateCategory.php
1 <?php
2 /**
3 * @file
4 * @ingroup Maintenance
5 * @author Simetrical
6 */
7
8 $optionsWithArgs = array( 'begin', 'max-slave-lag', 'throttle' );
9
10 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
11
12
13 class PopulateCategory extends Maintenance {
14
15 const REPORTING_INTERVAL = 1000;
16
17 public function __construct() {
18 parent::__construct();
19 $this->mDescription = <<<TEXT
20 This script will populate the category table, added in MediaWiki 1.13. It will
21 print out progress indicators every 1000 categories it adds to the table. The
22 script is perfectly safe to run on large, live wikis, and running it multiple
23 times is harmless. You may want to use the throttling options if it's causing
24 too much load; they will not affect correctness.
25
26 If the script is stopped and later resumed, you can use the --begin option with
27 the last printed progress indicator to pick up where you left off. This is
28 safe, because any newly-added categories before this cutoff will have been
29 added after the software update and so will be populated anyway.
30
31 When the script has finished, it will make a note of this in the database, and
32 will not run again without the --force option.
33 TEXT;
34 # '
35 $this->addOption( 'begin', 'Only do categories whose names are alphabetically after the provided name', false, true );
36 $this->addOption( 'max-slave-lag', 'If slave lag exceeds this many seconds, wait until it drops before continuing. Default: 10', false, true );
37 $this->addOption( 'throttle', 'Wait this many milliseconds after each category. Default: 0', false, true );
38 $this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' );
39 }
40
41 public function execute() {
42 $begin = $this->getOption( 'begin', '' );
43 $maxSlaveLag = $this->getOption( 'max-slave-lag', 10 );
44 $throttle = $this->getOption( 'throttle', 0 );
45 $force = $this->getOption( 'force', false );
46 $this->doPopulateCategory( $begin, $maxSlaveLag, $throttle, $force );
47 }
48
49 private function doPopulateCategory( $begin, $maxlag, $throttle, $force ) {
50 $dbw = wfGetDB( DB_MASTER );
51
52 if ( !$force ) {
53 $row = $dbw->selectRow(
54 'updatelog',
55 '1',
56 array( 'ul_key' => 'populate category' ),
57 __METHOD__
58 );
59 if ( $row ) {
60 $this->output( "Category table already populated. Use php " .
61 "maintenance/populateCategory.php\n--force from the command line " .
62 "to override.\n" );
63 return true;
64 }
65 }
66
67 $maxlag = intval( $maxlag );
68 $throttle = intval( $throttle );
69 if ( $begin !== '' ) {
70 $where = 'cl_to > ' . $dbw->addQuotes( $begin );
71 } else {
72 $where = null;
73 }
74 $i = 0;
75
76 while ( true ) {
77 # Find which category to update
78 $row = $dbw->selectRow(
79 'categorylinks',
80 'cl_to',
81 $where,
82 __METHOD__,
83 array(
84 'ORDER BY' => 'cl_to'
85 )
86 );
87 if ( !$row ) {
88 # Done, hopefully.
89 break;
90 }
91 $name = $row->cl_to;
92 $where = 'cl_to > ' . $dbw->addQuotes( $name );
93
94 # Use the row to update the category count
95 $cat = Category::newFromName( $name );
96 if ( !is_object( $cat ) ) {
97 $this->output( "The category named $name is not valid?!\n" );
98 } else {
99 $cat->refreshCounts();
100 }
101
102 ++$i;
103 if ( !( $i % self::REPORTING_INTERVAL ) ) {
104 $this->output( "$name\n" );
105 wfWaitForSlaves( $maxlag );
106 }
107 usleep( $throttle * 1000 );
108 }
109
110 if ( $dbw->insert(
111 'updatelog',
112 array( 'ul_key' => 'populate category' ),
113 __METHOD__,
114 'IGNORE'
115 )
116 ) {
117 $this->output( "Category population complete.\n" );
118 return true;
119 } else {
120 $this->output( "Could not insert category population row.\n" );
121 return false;
122 }
123 }
124 }
125
126 $maintClass = "PopulateCategory";
127 require_once( DO_MAINTENANCE );