MediaWiki REL1_37
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 $restrictions = [];
73 foreach ( $t->getRestrictionTypes() as $type ) {
74 $restrictions[$type] = $protection;
75 }
76
77 # un/protect the article
78 $this->output( "Updating protection status..." );
79
80 $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $t );
81 $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
82
83 if ( $status->isOK() ) {
84 $this->output( "done\n" );
85 } else {
86 $this->output( "failed\n" );
87 }
88 }
89}
90
91$maintClass = Protect::class;
92require_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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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:607
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:810
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition User.php:122
$maintClass
Definition protect.php:91