00001 <?php
00026 require_once( dirname(__FILE__) . '/Maintenance.php' );
00027
00028 class UpdateRestrictions extends Maintenance {
00029 public function __construct() {
00030 parent::__construct();
00031 $this->mDescription = "Updates page_restrictions table from old page_restriction column";
00032 $this->setBatchSize( 100 );
00033 }
00034
00035 public function execute() {
00036 $db = wfGetDB( DB_MASTER );
00037 if( !$db->tableExists( 'page_restrictions' ) ) {
00038 $this->error( "page_restrictions table does not exist", true );
00039 }
00040
00041 $start = $db->selectField( 'page', 'MIN(page_id)', false, __METHOD__ );
00042 if( !$start ) {
00043 $this->error( "Nothing to do.", true );
00044 }
00045 $end = $db->selectField( 'page', 'MAX(page_id)', false, __METHOD__ );
00046
00047 # Do remaining chunk
00048 $end += $this->mBatchSize - 1;
00049 $blockStart = $start;
00050 $blockEnd = $start + $this->mBatchSize - 1;
00051 $encodedExpiry = 'infinity';
00052 while( $blockEnd <= $end ) {
00053 $this->output( "...doing page_id from $blockStart to $blockEnd\n" );
00054 $cond = "page_id BETWEEN $blockStart AND $blockEnd AND page_restrictions !=''";
00055 $res = $db->select( 'page', array('page_id','page_namespace','page_restrictions'), $cond, __METHOD__ );
00056 $batch = array();
00057 foreach( $res as $row ) {
00058 $oldRestrictions = array();
00059 foreach( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
00060 $temp = explode( '=', trim( $restrict ) );
00061
00062 if( count($temp) == 1 && $temp[0] ) {
00063
00064 $oldRestrictions["edit"] = trim( $temp[0] );
00065 $oldRestrictions["move"] = trim( $temp[0] );
00066 } else if( $temp[1] ) {
00067 $oldRestrictions[$temp[0]] = trim( $temp[1] );
00068 }
00069 }
00070 # Clear invalid columns
00071 if( $row->page_namespace == NS_MEDIAWIKI ) {
00072 $db->update( 'page', array( 'page_restrictions' => '' ),
00073 array( 'page_id' => $row->page_id ), __FUNCTION__ );
00074 $this->output( "...removed dead page_restrictions column for page {$row->page_id}\n" );
00075 }
00076 # Update restrictions table
00077 foreach( $oldRestrictions as $action => $restrictions ) {
00078 $batch[] = array(
00079 'pr_page' => $row->page_id,
00080 'pr_type' => $action,
00081 'pr_level' => $restrictions,
00082 'pr_cascade' => 0,
00083 'pr_expiry' => $encodedExpiry
00084 );
00085 }
00086 }
00087 # We use insert() and not replace() as Article.php replaces
00088 # page_restrictions with '' when protected in the restrictions table
00089 if ( count( $batch ) ) {
00090 $ok = $db->deadlockLoop( array( $db, 'insert' ), 'page_restrictions',
00091 $batch, __FUNCTION__, array( 'IGNORE' ) );
00092 if( !$ok ) {
00093 throw new MWException( "Deadlock loop failed wtf :(" );
00094 }
00095 }
00096 $blockStart += $this->mBatchSize - 1;
00097 $blockEnd += $this->mBatchSize - 1;
00098 wfWaitForSlaves( 5 );
00099 }
00100 $this->output( "...removing dead rows from page_restrictions\n" );
00101
00102 $db->delete( 'page_restrictions', array( 'pr_level' => '' ) );
00103
00104 $db->deleteJoin( 'page_restrictions', 'page', 'pr_page', 'page_id', array('page_namespace' => NS_MEDIAWIKI) );
00105 $this->output( "...Done!\n" );
00106 }
00107 }
00108
00109 $maintClass = "UpdateRestrictions";
00110 require_once( DO_MAINTENANCE );