MediaWiki  REL1_24
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(
00063                 'page',
00064                 array( 'page_id', 'page_namespace', 'page_restrictions' ),
00065                 $cond,
00066                 __METHOD__
00067             );
00068             $batch = array();
00069             foreach ( $res as $row ) {
00070                 $oldRestrictions = array();
00071                 foreach ( explode( ':', trim( $row->page_restrictions ) ) as $restrict ) {
00072                     $temp = explode( '=', trim( $restrict ) );
00073                     // Make sure we are not settings restrictions to ""
00074                     if ( count( $temp ) == 1 && $temp[0] ) {
00075                         // old old format should be treated as edit/move restriction
00076                         $oldRestrictions["edit"] = trim( $temp[0] );
00077                         $oldRestrictions["move"] = trim( $temp[0] );
00078                     } elseif ( $temp[1] ) {
00079                         $oldRestrictions[$temp[0]] = trim( $temp[1] );
00080                     }
00081                 }
00082                 # Clear invalid columns
00083                 if ( $row->page_namespace == NS_MEDIAWIKI ) {
00084                     $db->update( 'page', array( 'page_restrictions' => '' ),
00085                         array( 'page_id' => $row->page_id ), __FUNCTION__ );
00086                     $this->output( "...removed dead page_restrictions column for page {$row->page_id}\n" );
00087                 }
00088                 # Update restrictions table
00089                 foreach ( $oldRestrictions as $action => $restrictions ) {
00090                     $batch[] = array(
00091                         'pr_page' => $row->page_id,
00092                         'pr_type' => $action,
00093                         'pr_level' => $restrictions,
00094                         'pr_cascade' => 0,
00095                         'pr_expiry' => $encodedExpiry
00096                     );
00097                 }
00098             }
00099             # We use insert() and not replace() as Article.php replaces
00100             # page_restrictions with '' when protected in the restrictions table
00101             if ( count( $batch ) ) {
00102                 $ok = $db->deadlockLoop( array( $db, 'insert' ), 'page_restrictions',
00103                     $batch, __FUNCTION__, array( 'IGNORE' ) );
00104                 if ( !$ok ) {
00105                     throw new MWException( "Deadlock loop failed wtf :(" );
00106                 }
00107             }
00108             $blockStart += $this->mBatchSize - 1;
00109             $blockEnd += $this->mBatchSize - 1;
00110             wfWaitForSlaves();
00111         }
00112         $this->output( "...removing dead rows from page_restrictions\n" );
00113         // Kill any broken rows from previous imports
00114         $db->delete( 'page_restrictions', array( 'pr_level' => '' ) );
00115         // Kill other invalid rows
00116         $db->deleteJoin(
00117             'page_restrictions',
00118             'page',
00119             'pr_page',
00120             'page_id',
00121             array( 'page_namespace' => NS_MEDIAWIKI )
00122         );
00123         $this->output( "...Done!\n" );
00124     }
00125 }
00126 
00127 $maintClass = "UpdateRestrictions";
00128 require_once RUN_MAINTENANCE_IF_MAIN;