]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/populateCategory.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.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                 $this->addOption( 'begin', 'Only do categories whose names are alphabetically after the provided name', false, true );
35                 $this->addOption( 'max-slave-lag', 'If slave lag exceeds this many seconds, wait until it drops before continuing.  Default: 10', false, true );
36                 $this->addOption( 'throttle', 'Wait this many milliseconds after each category.  Default: 0', false, true );
37                 $this->addOption( 'force', 'Run regardless of whether the database says it\'s been run already' );
38         }
39         
40         public function execute() {
41                 $begin = $this->getOption( 'begin', '' );
42                 $maxSlaveLag = $this->getOption( 'max-slave-lag', 10 );
43                 $throttle = $this->getOption( 'throttle', 0 );
44                 $force = $this->getOption( 'force', false );
45                 $this->doPopulateCategory( $begin, $maxSlaveLag, $throttle, $force );
46         }
47
48         private function doPopulateCategory( $begin, $maxlag, $throttle, $force ) {
49                 $dbw = wfGetDB( DB_MASTER );
50         
51                 if( !$force ) {
52                         $row = $dbw->selectRow(
53                                 'updatelog',
54                                 '1',
55                                 array( 'ul_key' => 'populate category' ),
56                                 __FUNCTION__
57                         );
58                         if( $row ) {
59                                 $this->output( "Category table already populated.  Use php ".
60                                 "maintenance/populateCategory.php\n--force from the command line ".
61                                 "to override.\n" );
62                                 return true;
63                         }
64                 }
65         
66                 $maxlag = intval( $maxlag );
67                 $throttle = intval( $throttle );
68                 $force = (bool)$force;
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                                 __FUNCTION__,
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                                 __FUNCTION__,
114                                 'IGNORE'
115                         )
116                 ) {
117                         wfOut( "Category population complete.\n" );
118                         return true;
119                 } else {
120                         wfOut( "Could not insert category population row.\n" );
121                         return false;
122                 }
123         }
124 }
125
126 $maintClass = "PopulateCategory";
127 require_once( DO_MAINTENANCE );