Use TOCData methods to process new headings
[mediawiki.git] / maintenance / protect.php
blob852c2d9d99ffb46b055ba288e73ea592062069ed
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 use MediaWiki\MediaWikiServices;
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script that protects or unprotects a page.
31 * @ingroup Maintenance
33 class Protect extends Maintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( 'Protect or unprotect a page from the command line.' );
37 $this->addOption( 'unprotect', 'Removes protection' );
38 $this->addOption( 'semiprotect', 'Adds semi-protection' );
39 $this->addOption( 'cascade', 'Add cascading protection' );
40 $this->addOption( 'user', 'Username to protect with', false, true, 'u' );
41 $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
42 $this->addArg( 'title', 'Title to protect', true );
45 public function execute() {
46 $userName = $this->getOption( 'user', false );
47 $reason = $this->getOption( 'reason', '' );
49 $cascade = $this->hasOption( 'cascade' );
51 $protection = "sysop";
52 if ( $this->hasOption( 'semiprotect' ) ) {
53 $protection = "autoconfirmed";
54 } elseif ( $this->hasOption( 'unprotect' ) ) {
55 $protection = "";
58 if ( $userName === false ) {
59 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
60 } else {
61 $user = User::newFromName( $userName );
63 if ( !$user ) {
64 $this->fatalError( "Invalid username" );
67 $t = Title::newFromText( $this->getArg( 0 ) );
68 if ( !$t ) {
69 $this->fatalError( "Invalid title" );
72 $services = MediaWikiServices::getInstance();
73 $restrictions = [];
74 foreach ( $services->getRestrictionStore()->listApplicableRestrictionTypes( $t ) as $type ) {
75 $restrictions[$type] = $protection;
78 # un/protect the article
79 $this->output( "Updating protection status..." );
81 $page = $services->getWikiPageFactory()->newFromTitle( $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::class;
93 require_once RUN_MAINTENANCE_IF_MAIN;