Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Protect
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Protect or unprotect a page.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\Title\Title;
25use MediaWiki\User\User;
26
27require_once __DIR__ . '/Maintenance.php';
28
29/**
30 * Maintenance script that protects or unprotects a page.
31 *
32 * @ingroup Maintenance
33 */
34class Protect extends Maintenance {
35    public function __construct() {
36        parent::__construct();
37        $this->addDescription( 'Protect or unprotect a page from the command line.' );
38        $this->addOption( 'unprotect', 'Removes protection' );
39        $this->addOption( 'semiprotect', 'Adds semi-protection' );
40        $this->addOption( 'cascade', 'Add cascading protection' );
41        $this->addOption( 'user', 'Username to protect with', false, true, 'u' );
42        $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
43        $this->addArg( 'title', 'Title to protect', true );
44    }
45
46    public function execute() {
47        $userName = $this->getOption( 'user', false );
48        $reason = $this->getOption( 'reason', '' );
49
50        $cascade = $this->hasOption( 'cascade' );
51
52        $protection = "sysop";
53        if ( $this->hasOption( 'semiprotect' ) ) {
54            $protection = "autoconfirmed";
55        } elseif ( $this->hasOption( 'unprotect' ) ) {
56            $protection = "";
57        }
58
59        if ( $userName === false ) {
60            $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
61        } else {
62            $user = User::newFromName( $userName );
63        }
64        if ( !$user ) {
65            $this->fatalError( "Invalid username" );
66        }
67
68        $t = Title::newFromText( $this->getArg( 0 ) );
69        if ( !$t ) {
70            $this->fatalError( "Invalid title" );
71        }
72
73        $services = $this->getServiceContainer();
74        $restrictions = [];
75        foreach ( $services->getRestrictionStore()->listApplicableRestrictionTypes( $t ) as $type ) {
76            $restrictions[$type] = $protection;
77        }
78
79        # un/protect the article
80        $this->output( "Updating protection status..." );
81
82        $page = $services->getWikiPageFactory()->newFromTitle( $t );
83        $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
84
85        if ( $status->isOK() ) {
86            $this->output( "done\n" );
87        } else {
88            $this->output( "failed\n" );
89        }
90    }
91}
92
93$maintClass = Protect::class;
94require_once RUN_MAINTENANCE_IF_MAIN;