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