MediaWiki
master
Toggle main menu visibility
protect.php
Go to the documentation of this file.
1
<?php
9
10
use
MediaWiki\Maintenance\Maintenance
;
11
use MediaWiki\Title\Title;
12
use MediaWiki\User\User;
13
14
// @codeCoverageIgnoreStart
15
require_once __DIR__ .
'/Maintenance.php'
;
16
// @codeCoverageIgnoreEnd
17
23
class
Protect
extends
Maintenance
{
24
public
function
__construct
() {
25
parent::__construct();
26
$this->addDescription(
'Protect or unprotect a page from the command line.'
);
27
$this->addOption(
'unprotect'
,
'Removes protection'
);
28
$this->addOption(
'semiprotect'
,
'Adds semi-protection'
);
29
$this->addOption(
'cascade'
,
'Add cascading protection'
);
30
$this->addOption(
'user'
,
'Username to protect with'
,
false
,
true
,
'u'
);
31
$this->addOption(
'reason'
,
'Reason for un/protection'
,
false
,
true
,
'r'
);
32
$this->addArg(
'title'
,
'Title to protect'
,
true
);
33
}
34
35
public
function
execute
() {
36
$userName = $this->getOption(
'user'
,
false
);
37
$reason = $this->getOption(
'reason'
,
''
);
38
39
$cascade = $this->hasOption(
'cascade'
);
40
41
$protection =
"sysop"
;
42
if
( $this->hasOption(
'semiprotect'
) ) {
43
$protection =
"autoconfirmed"
;
44
} elseif ( $this->hasOption(
'unprotect'
) ) {
45
$protection =
""
;
46
}
47
48
if
( $userName ===
false
) {
49
$user =
User::newSystemUser
(
User::MAINTENANCE_SCRIPT_USER
, [
'steal'
=>
true
] );
50
}
else
{
51
$user =
User::newFromName
( $userName );
52
}
53
if
( !$user ) {
54
$this->fatalError(
"Invalid username"
);
55
}
56
57
$t =
Title::newFromText
( $this->getArg( 0 ) );
58
if
( !$t ) {
59
$this->fatalError(
"Invalid title"
);
60
}
61
62
$services = $this->getServiceContainer();
63
$restrictions = [];
64
foreach
( $services->getRestrictionStore()->listApplicableRestrictionTypes( $t ) as $type ) {
65
$restrictions[$type] = $protection;
66
}
67
68
# un/protect the article
69
$this->output(
"Updating protection status..."
);
70
71
$this->beginTransactionRound( __METHOD__ );
72
$page = $services->getWikiPageFactory()->newFromTitle( $t );
73
$status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
74
$this->commitTransactionRound( __METHOD__ );
75
76
if
( $status->isOK() ) {
77
$this->output(
"done\n"
);
78
}
else
{
79
$this->output(
"failed\n"
);
80
}
81
}
82
}
83
84
// @codeCoverageIgnoreStart
85
$maintClass
= Protect::class;
86
require_once RUN_MAINTENANCE_IF_MAIN;
87
// @codeCoverageIgnoreEnd
$maintClass
$maintClass
Definition
addChangeTag.php:56
MediaWiki\Title\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:388
MediaWiki\User\User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition
User.php:808
MediaWiki\User\User\MAINTENANCE_SCRIPT_USER
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition
User.php:164
MediaWiki\User\User\newFromName
static newFromName( $name, $validate='valid')
Definition
User.php:610
Protect
Maintenance script that protects or unprotects a page.
Definition
protect.php:23
Protect\__construct
__construct()
Definition
protect.php:24
Protect\execute
execute()
Definition
protect.php:35
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
protect.php
Generated on
for MediaWiki by
1.18.0