MediaWiki REL1_35
protect.php
Go to the documentation of this file.
1<?php
24require_once __DIR__ . '/Maintenance.php';
25
31class 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;
90require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that protects or unprotects a page.
Definition protect.php:31
__construct()
Default constructor.
Definition protect.php:32
execute()
Do the actual work.
Definition protect.php:43
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:758
$maintClass
Definition protect.php:89