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