MediaWiki  REL1_19
protect.php
Go to the documentation of this file.
00001 <?php
00023 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00024 
00025 class Protect extends Maintenance {
00026         public function __construct() {
00027                 parent::__construct();
00028                 $this->mDescription = "Protect or unprotect an article from the command line.";
00029                 $this->addOption( 'unprotect', 'Removes protection' );
00030                 $this->addOption( 'semiprotect', 'Adds semi-protection' );
00031                 $this->addOption( 'cascade', 'Add cascading protection' );
00032                 $this->addOption( 'user', 'Username to protect with', false, true, 'u' );
00033                 $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
00034                 $this->addArg( 'title', 'Title to protect', true );
00035         }
00036 
00037         public function execute() {
00038                 global $wgUser;
00039 
00040                 $userName = $this->getOption( 'u', 'Maintenance script' );
00041                 $reason = $this->getOption( 'r', '' );
00042 
00043                 $cascade = $this->hasOption( 'cascade' );
00044 
00045                 $protection = "sysop";
00046                 if ( $this->hasOption( 'semiprotect' ) ) {
00047                         $protection = "autoconfirmed";
00048                 } elseif ( $this->hasOption( 'unprotect' ) ) {
00049                         $protection = "";
00050                 }
00051 
00052                 $user = User::newFromName( $userName );
00053                 if ( !$user ) {
00054                         $this->error( "Invalid username", true );
00055                 }
00056 
00057                 $restrictions = array( 'edit' => $protection, 'move' => $protection );
00058 
00059                 $t = Title::newFromText( $this->getArg() );
00060                 if ( !$t ) {
00061                         $this->error( "Invalid title", true );
00062                 }
00063 
00064                 $restrictions = array();
00065                 foreach( $t->getRestrictionTypes() as $type ) {
00066                         $restrictions[$type] = $protection;
00067                 }
00068 
00069                 # un/protect the article
00070                 $this->output( "Updating protection status... " );
00071 
00072                 $page = WikiPage::factory( $t );
00073                 $status = $page->doUpdateRestrictions( $restrictions, array(), $cascade, $reason, $user );
00074 
00075                 if ( $status->isOK() ) {
00076                         $this->output( "done\n" );
00077                 } else {
00078                         $this->output( "failed\n" );
00079                 }
00080         }
00081 }
00082 
00083 $maintClass = "Protect";
00084 require_once( RUN_MAINTENANCE_IF_MAIN );