MediaWiki  master
protect.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
33 class 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;
92 require_once RUN_MAINTENANCE_IF_MAIN;
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:395
Protect
Maintenance script that protects or unprotects a page.
Definition: protect.php:33
Protect\execute
execute()
Do the actual work.
Definition: protect.php:45
$maintClass
$maintClass
Definition: protect.php:91
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:595
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:803
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:300
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Protect\__construct
__construct()
Default constructor.
Definition: protect.php:34
$t
$t
Definition: testCompression.php:74
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:353
User\MAINTENANCE_SCRIPT_USER
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition: User.php:113
$type
$type
Definition: testCompression.php:52