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