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