00001 <?php
00008 define( 'REPORTING_INTERVAL', 1000 );
00009
00010 function populateCategory( $begin, $maxlag, $throttle, $force ) {
00011 $dbw = wfGetDB( DB_MASTER );
00012
00013 if( !$force ) {
00014 $row = $dbw->selectRow(
00015 'updatelog',
00016 '1',
00017 array( 'ul_key' => 'populate category' ),
00018 __FUNCTION__
00019 );
00020 if( $row ) {
00021 wfOut( "Category table already populated. Use php ".
00022 "maintenance/populateCategory.php\n--force from the command line ".
00023 "to override.\n" );
00024 return true;
00025 }
00026 }
00027
00028 $maxlag = intval( $maxlag );
00029 $throttle = intval( $throttle );
00030 $force = (bool)$force;
00031 if( $begin !== '' ) {
00032 $where = 'cl_to > '.$dbw->addQuotes( $begin );
00033 } else {
00034 $where = null;
00035 }
00036 $i = 0;
00037
00038 while( true ) {
00039 # Find which category to update
00040 $row = $dbw->selectRow(
00041 'categorylinks',
00042 'cl_to',
00043 $where,
00044 __FUNCTION__,
00045 array(
00046 'ORDER BY' => 'cl_to'
00047 )
00048 );
00049 if( !$row ) {
00050 # Done, hopefully.
00051 break;
00052 }
00053 $name = $row->cl_to;
00054 $where = 'cl_to > '.$dbw->addQuotes( $name );
00055
00056 # Use the row to update the category count
00057 $cat = Category::newFromName( $name );
00058 if( !is_object( $cat ) ) {
00059 wfOut( "The category named $name is not valid?!\n" );
00060 } else {
00061 $cat->refreshCounts();
00062 }
00063
00064 ++$i;
00065 if( !($i % REPORTING_INTERVAL) ) {
00066 wfOut( "$name\n" );
00067 wfWaitForSlaves( $maxlag );
00068 }
00069 usleep( $throttle*1000 );
00070 }
00071
00072 if( $dbw->insert(
00073 'updatelog',
00074 array( 'ul_key' => 'populate category' ),
00075 __FUNCTION__,
00076 'IGNORE'
00077 )
00078 ) {
00079 wfOut( "Category population complete.\n" );
00080 return true;
00081 } else {
00082 wfOut( "Could not insert category population row.\n" );
00083 return false;
00084 }
00085 }