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