MediaWiki  master
protect.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class Protect extends Maintenance {
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Protect or unprotect a page from the command line.' );
35  $this->addOption( 'unprotect', 'Removes protection' );
36  $this->addOption( 'semiprotect', 'Adds semi-protection' );
37  $this->addOption( 'cascade', 'Add cascading protection' );
38  $this->addOption( 'user', 'Username to protect with', false, true, 'u' );
39  $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
40  $this->addArg( 'title', 'Title to protect', true );
41  }
42 
43  public function execute() {
44  $userName = $this->getOption( 'user', false );
45  $reason = $this->getOption( 'reason', '' );
46 
47  $cascade = $this->hasOption( 'cascade' );
48 
49  $protection = "sysop";
50  if ( $this->hasOption( 'semiprotect' ) ) {
51  $protection = "autoconfirmed";
52  } elseif ( $this->hasOption( 'unprotect' ) ) {
53  $protection = "";
54  }
55 
56  if ( $userName === false ) {
57  $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
58  } else {
59  $user = User::newFromName( $userName );
60  }
61  if ( !$user ) {
62  $this->fatalError( "Invalid username" );
63  }
64 
65  $t = Title::newFromText( $this->getArg( 0 ) );
66  if ( !$t ) {
67  $this->fatalError( "Invalid title" );
68  }
69 
70  $restrictions = [];
71  foreach ( $t->getRestrictionTypes() as $type ) {
72  $restrictions[$type] = $protection;
73  }
74 
75  # un/protect the article
76  $this->output( "Updating protection status... " );
77 
78  $page = WikiPage::factory( $t );
79  $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
80 
81  if ( $status->isOK() ) {
82  $this->output( "done\n" );
83  } else {
84  $this->output( "failed\n" );
85  }
86  }
87 }
88 
89 $maintClass = Protect::class;
90 require_once RUN_MAINTENANCE_IF_MAIN;
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
getArg( $argId=0, $default=null)
Get an argument.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
Maintenance script that protects or unprotects a page.
Definition: protect.php:31
__construct()
Definition: protect.php:32
hasOption( $name)
Checks to see if a particular option exists.
addDescription( $text)
Set the description text.
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
execute()
Definition: protect.php:43
$maintClass
Definition: protect.php:89
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:737
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319