MediaWiki  1.34.0
protect.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class Protect extends Maintenance {
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Protect or unprotect a page from the command line.' );
35  $this->addOption( 'unprotect', 'Removes protection' );
36  $this->addOption( 'semiprotect', 'Adds semi-protection' );
37  $this->addOption( 'cascade', 'Add cascading protection' );
38  $this->addOption( 'user', 'Username to protect with', false, true, 'u' );
39  $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
40  $this->addArg( 'title', 'Title to protect', true );
41  }
42 
43  public function execute() {
44  $userName = $this->getOption( 'user', false );
45  $reason = $this->getOption( 'reason', '' );
46 
47  $cascade = $this->hasOption( 'cascade' );
48 
49  $protection = "sysop";
50  if ( $this->hasOption( 'semiprotect' ) ) {
51  $protection = "autoconfirmed";
52  } elseif ( $this->hasOption( 'unprotect' ) ) {
53  $protection = "";
54  }
55 
56  if ( $userName === false ) {
57  $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
58  } else {
59  $user = User::newFromName( $userName );
60  }
61  if ( !$user ) {
62  $this->fatalError( "Invalid username" );
63  }
64 
65  $t = Title::newFromText( $this->getArg( 0 ) );
66  if ( !$t ) {
67  $this->fatalError( "Invalid title" );
68  }
69 
70  $restrictions = [];
71  foreach ( $t->getRestrictionTypes() as $type ) {
72  $restrictions[$type] = $protection;
73  }
74 
75  # un/protect the article
76  $this->output( "Updating protection status... " );
77 
78  $page = WikiPage::factory( $t );
79  $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
80 
81  if ( $status->isOK() ) {
82  $this->output( "done\n" );
83  } else {
84  $this->output( "failed\n" );
85  }
86  }
87 }
88 
89 $maintClass = Protect::class;
90 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
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:316
Protect
Maintenance script that protects or unprotects a page.
Definition: protect.php:31
Protect\execute
execute()
Do the actual work.
Definition: protect.php:43
$maintClass
$maintClass
Definition: protect.php:89
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:737
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$t
$t
Definition: make-normalization-table.php:143
$status
return $status
Definition: SyntaxHighlight.php:347
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:319
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Protect\__construct
__construct()
Default constructor.
Definition: protect.php:32
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:371
$type
$type
Definition: testCompression.php:48