Some UI cleanup
[mediawiki.git] / maintenance / populateCategory.inc
blob3b006fef5f1c47a17cb7aef02ab3efff7d19b4c3
1 <?php
2 /**
3  * @addtogroup Maintenance
4  * @author Simetrical
5  */
7 define( 'REPORTING_INTERVAL', 1000 );
9 function populateCategory( $begin, $maxlag, $throttle, $force ) {
10         $dbw = wfGetDB( DB_MASTER );
12         if( !$force ) {
13                 $row = $dbw->selectRow(
14                         'updatelog',
15                         '1',
16                         array( 'ul_key' => 'populate category' ),
17                         __FUNCTION__
18                 );
19                 if( $row ) {
20                         echo "Category table already populated.  Use php ".
21                         "maintenance/populateCategory.php\n--force from the command line ".
22                         "to override.\n";
23                         return true;
24                 }
25         }
27         $maxlag = intval( $maxlag );
28         $throttle = intval( $throttle );
29         $force = (bool)$force;
30         if( $begin !== '' ) {
31                 $where = 'cl_to > '.$dbw->addQuotes( $begin );
32         } else {
33                 $where = null;
34         }
35         $i = 0;
37         while( true ) {
38                 # Find which category to update
39                 $row = $dbw->selectRow(
40                         'categorylinks',
41                         'cl_to',
42                         $where,
43                         __FUNCTION__,
44                         array(
45                                 'ORDER BY' => 'cl_to'
46                         )
47                 );
48                 if( !$row ) {
49                         # Done, hopefully.
50                         break;
51                 }
52                 $name = $row->cl_to;
53                 $where = 'cl_to > '.$dbw->addQuotes( $name );
55                 # Use the row to update the category count
56                 $cat = Category::newFromName( $name );
57                 if( !is_object( $cat ) ) {
58                         echo "The category named $name is not valid?!\n";
59                 } else {
60                         $cat->refreshCounts();
61                 }
63                 ++$i;
64                 if( !($i % REPORTING_INTERVAL) ) {
65                         echo "$name\n";
66                         wfWaitForSlaves( $maxlag );
67                 }
68                 usleep( $throttle*1000 );
69         }
71         if( $dbw->insert(
72                         'updatelog',
73                         array( 'ul_key' => 'populate category' ),
74                         __FUNCTION__,
75                         'IGNORE'
76                 )
77         ) {
78                 echo "Category population complete.\n";
79                 return true;
80         } else {
81                 echo "Could not insert category population row.\n";
82                 return false;
83         }