MediaWiki REL1_40
protect.php
Go to the documentation of this file.
1<?php
26
27require_once __DIR__ . '/Maintenance.php';
28
34class Protect extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Protect or unprotect a page from the command line.' );
38 $this->addOption( 'unprotect', 'Removes protection' );
39 $this->addOption( 'semiprotect', 'Adds semi-protection' );
40 $this->addOption( 'cascade', 'Add cascading protection' );
41 $this->addOption( 'user', 'Username to protect with', false, true, 'u' );
42 $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
43 $this->addArg( 'title', 'Title to protect', true );
44 }
45
46 public function execute() {
47 $userName = $this->getOption( 'user', false );
48 $reason = $this->getOption( 'reason', '' );
49
50 $cascade = $this->hasOption( 'cascade' );
51
52 $protection = "sysop";
53 if ( $this->hasOption( 'semiprotect' ) ) {
54 $protection = "autoconfirmed";
55 } elseif ( $this->hasOption( 'unprotect' ) ) {
56 $protection = "";
57 }
58
59 if ( $userName === false ) {
60 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
61 } else {
62 $user = User::newFromName( $userName );
63 }
64 if ( !$user ) {
65 $this->fatalError( "Invalid username" );
66 }
67
68 $t = Title::newFromText( $this->getArg( 0 ) );
69 if ( !$t ) {
70 $this->fatalError( "Invalid title" );
71 }
72
73 $services = MediaWikiServices::getInstance();
74 $restrictions = [];
75 foreach ( $services->getRestrictionStore()->listApplicableRestrictionTypes( $t ) as $type ) {
76 $restrictions[$type] = $protection;
77 }
78
79 # un/protect the article
80 $this->output( "Updating protection status..." );
81
82 $page = $services->getWikiPageFactory()->newFromTitle( $t );
83 $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
84
85 if ( $status->isOK() ) {
86 $this->output( "done\n" );
87 } else {
88 $this->output( "failed\n" );
89 }
90 }
91}
92
93$maintClass = Protect::class;
94require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
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.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
Maintenance script that protects or unprotects a page.
Definition protect.php:34
__construct()
Default constructor.
Definition protect.php:35
execute()
Do the actual work.
Definition protect.php:46
static newFromName( $name, $validate='valid')
Definition User.php:592
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:793
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition User.php:117
$maintClass
Definition protect.php:93