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