Debian Codegraph @

maintenance/protect.php

raw symbols SCIP
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
24 use MediaWiki\Title\Title;
25 use MediaWiki\User\User;
26
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
30
31 /**
32 * Maintenance script that protects or unprotects a page.
33 *
34 * @ingroup Maintenance
35 */
36 class 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;
97 require_once RUN_MAINTENANCE_IF_MAIN;
98 // @codeCoverageIgnoreEnd