00001 <?php
00012 define( 'BATCH_SIZE', 100 );
00013
00014 require_once 'commandLine.inc';
00015
00016 $db =& wfGetDB( DB_MASTER );
00017 if ( !$db->tableExists( 'page_restrictions' ) ) {
00018 echo "page_restrictions does not exist\n";
00019 exit( 1 );
00020 }
00021
00022 migrate_page_restrictions( $db );
00023
00024 function migrate_page_restrictions( $db ) {
00025 $start = $db->selectField( 'page', 'MIN(page_id)', false, __FUNCTION__ );
00026 if( !$start ) {
00027 die("Nothing to do.\n");
00028 }
00029 $end = $db->selectField( 'page', 'MAX(page_id)', false, __FUNCTION__ );
00030
00031 # Do remaining chunk
00032 $end += BATCH_SIZE - 1;
00033 $blockStart = $start;
00034 $blockEnd = $start + BATCH_SIZE - 1;
00035 $encodedExpiry = 'infinity';
00036 while( $blockEnd <= $end ) {
00037 echo "...doing page_id from $blockStart to $blockEnd\n";
00038 $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !=''";
00039 $res = $db->select( 'page', array('page_id','page_namespace','page_restrictions'), $cond, __FUNCTION__ );
00040 $batch = array();
00041 while( $row = $db->fetchObject( $res ) ) {
00042 $oldRestrictions = array();
00043 foreach( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
00044 $temp = explode( '=', trim( $restrict ) );
00045
00046 if( count($temp) == 1 && $temp[0] ) {
00047
00048 $oldRestrictions["edit"] = trim( $temp[0] );
00049 $oldRestrictions["move"] = trim( $temp[0] );
00050 } else if( $temp[1] ) {
00051 $oldRestrictions[$temp[0]] = trim( $temp[1] );
00052 }
00053 }
00054 # Clear invalid columns
00055 if( $row->page_namespace == NS_MEDIAWIKI ) {
00056 $db->update( 'page', array( 'page_restrictions' => '' ),
00057 array( 'page_id' => $row->page_id ), __FUNCTION__ );
00058 echo "...removed dead page_restrictions column for page {$row->page_id}\n";
00059 }
00060 # Update restrictions table
00061 foreach( $oldRestrictions as $action => $restrictions ) {
00062 $batch[] = array(
00063 'pr_page' => $row->page_id,
00064 'pr_type' => $action,
00065 'pr_level' => $restrictions,
00066 'pr_cascade' => 0,
00067 'pr_expiry' => $encodedExpiry
00068 );
00069 }
00070 }
00071 # We use insert() and not replace() as Article.php replaces
00072 # page_restrictions with '' when protected in the restrictions table
00073 if ( count( $batch ) ) {
00074 $ok = $db->deadlockLoop( array( $db, 'insert' ), 'page_restrictions',
00075 $batch, __FUNCTION__, array( 'IGNORE' ) );
00076 if( !$ok ) {
00077 throw new MWException( "Deadlock loop failed wtf :(" );
00078 }
00079 }
00080 $blockStart += BATCH_SIZE - 1;
00081 $blockEnd += BATCH_SIZE - 1;
00082 wfWaitForSlaves( 5 );
00083 }
00084 echo "...removing dead rows from page_restrictions\n";
00085
00086 $db->delete( 'page_restrictions', array( 'pr_level' => '' ) );
00087
00088 $db->deleteJoin( 'page_restrictions', 'page', 'pr_page', 'page_id', array('page_namespace' => NS_MEDIAWIKI) );
00089 echo "...Done!\n";
00090 }
00091
00092