Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / maintenance / protect.php
blobf6bb2532024ea2d2d79ec9502c805b76387aaad5
1 <?php
2 /**
3 * Protect or unprotect a page.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script that protects or unprotects a page.
29 * @ingroup Maintenance
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 );
43 public function execute() {
44 $userName = $this->getOption( 'user', false );
45 $reason = $this->getOption( 'reason', '' );
47 $cascade = $this->hasOption( 'cascade' );
49 $protection = "sysop";
50 if ( $this->hasOption( 'semiprotect' ) ) {
51 $protection = "autoconfirmed";
52 } elseif ( $this->hasOption( 'unprotect' ) ) {
53 $protection = "";
56 if ( $userName === false ) {
57 $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
58 } else {
59 $user = User::newFromName( $userName );
61 if ( !$user ) {
62 $this->error( "Invalid username", true );
65 // @todo FIXME: This is reset 7 lines down.
66 $restrictions = [ 'edit' => $protection, 'move' => $protection ];
68 $t = Title::newFromText( $this->getArg() );
69 if ( !$t ) {
70 $this->error( "Invalid title", true );
73 $restrictions = [];
74 foreach ( $t->getRestrictionTypes() as $type ) {
75 $restrictions[$type] = $protection;
78 # un/protect the article
79 $this->output( "Updating protection status... " );
81 $page = WikiPage::factory( $t );
82 $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
84 if ( $status->isOK() ) {
85 $this->output( "done\n" );
86 } else {
87 $this->output( "failed\n" );
92 $maintClass = "Protect";
93 require_once RUN_MAINTENANCE_IF_MAIN;