MediaWiki master
protect.php
Go to the documentation of this file.
1<?php
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
37class Protect extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Protect or unprotect a page from the command line.' );
41 $this->addOption( 'unprotect', 'Removes protection' );
42 $this->addOption( 'semiprotect', 'Adds semi-protection' );
43 $this->addOption( 'cascade', 'Add cascading protection' );
44 $this->addOption( 'user', 'Username to protect with', false, true, 'u' );
45 $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
46 $this->addArg( 'title', 'Title to protect', true );
47 }
48
49 public function execute() {
50 $userName = $this->getOption( 'user', false );
51 $reason = $this->getOption( 'reason', '' );
52
53 $cascade = $this->hasOption( 'cascade' );
54
55 $protection = "sysop";
56 if ( $this->hasOption( 'semiprotect' ) ) {
57 $protection = "autoconfirmed";
58 } elseif ( $this->hasOption( 'unprotect' ) ) {
59 $protection = "";
60 }
61
62 if ( $userName === false ) {
63 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
64 } else {
65 $user = User::newFromName( $userName );
66 }
67 if ( !$user ) {
68 $this->fatalError( "Invalid username" );
69 }
70
71 $t = Title::newFromText( $this->getArg( 0 ) );
72 if ( !$t ) {
73 $this->fatalError( "Invalid title" );
74 }
75
76 $services = $this->getServiceContainer();
77 $restrictions = [];
78 foreach ( $services->getRestrictionStore()->listApplicableRestrictionTypes( $t ) as $type ) {
79 $restrictions[$type] = $protection;
80 }
81
82 # un/protect the article
83 $this->output( "Updating protection status..." );
84
85 $this->beginTransactionRound( __METHOD__ );
86 $page = $services->getWikiPageFactory()->newFromTitle( $t );
87 $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
88 $this->commitTransactionRound( __METHOD__ );
89
90 if ( $status->isOK() ) {
91 $this->output( "done\n" );
92 } else {
93 $this->output( "failed\n" );
94 }
95 }
96}
97
98// @codeCoverageIgnoreStart
99$maintClass = Protect::class;
100require_once RUN_MAINTENANCE_IF_MAIN;
101// @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.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:78
User class for the MediaWiki software.
Definition User.php:119
Maintenance script that protects or unprotects a page.
Definition protect.php:37
__construct()
Default constructor.
Definition protect.php:38
execute()
Do the actual work.
Definition protect.php:49
$maintClass
Definition protect.php:99