MediaWiki master
protect.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
36class Protect extends Maintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Protect or unprotect a page from the command line.' );
40 $this->addOption( 'unprotect', 'Removes protection' );
41 $this->addOption( 'semiprotect', 'Adds semi-protection' );
42 $this->addOption( 'cascade', 'Add cascading protection' );
43 $this->addOption( 'user', 'Username to protect with', false, true, 'u' );
44 $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
45 $this->addArg( 'title', 'Title to protect', true );
46 }
47
48 public function execute() {
49 $userName = $this->getOption( 'user', false );
50 $reason = $this->getOption( 'reason', '' );
51
52 $cascade = $this->hasOption( 'cascade' );
53
54 $protection = "sysop";
55 if ( $this->hasOption( 'semiprotect' ) ) {
56 $protection = "autoconfirmed";
57 } elseif ( $this->hasOption( 'unprotect' ) ) {
58 $protection = "";
59 }
60
61 if ( $userName === false ) {
62 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
63 } else {
64 $user = User::newFromName( $userName );
65 }
66 if ( !$user ) {
67 $this->fatalError( "Invalid username" );
68 }
69
70 $t = Title::newFromText( $this->getArg( 0 ) );
71 if ( !$t ) {
72 $this->fatalError( "Invalid title" );
73 }
74
75 $services = $this->getServiceContainer();
76 $restrictions = [];
77 foreach ( $services->getRestrictionStore()->listApplicableRestrictionTypes( $t ) as $type ) {
78 $restrictions[$type] = $protection;
79 }
80
81 # un/protect the article
82 $this->output( "Updating protection status..." );
83
84 $page = $services->getWikiPageFactory()->newFromTitle( $t );
85 $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
86
87 if ( $status->isOK() ) {
88 $this->output( "done\n" );
89 } else {
90 $this->output( "failed\n" );
91 }
92 }
93}
94
95// @codeCoverageIgnoreStart
96$maintClass = Protect::class;
97require_once RUN_MAINTENANCE_IF_MAIN;
98// @codeCoverageIgnoreEnd
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.
getServiceContainer()
Returns the main service container.
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.
Represents a title within MediaWiki.
Definition Title.php:78
internal since 1.36
Definition User.php:93
Maintenance script that protects or unprotects a page.
Definition protect.php:36
__construct()
Default constructor.
Definition protect.php:37
execute()
Do the actual work.
Definition protect.php:48
$maintClass
Definition protect.php:96