Rank aliases in search in order they appear in the messages file.
[lhc/web/wiklou.git] / maintenance / protect.php
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 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script that protects or unprotects a page.
28 *
29 * @ingroup Maintenance
30 */
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 );
41 }
42
43 public function execute() {
44 $userName = $this->getOption( 'u', false );
45 $reason = $this->getOption( 'r', '' );
46
47 $cascade = $this->hasOption( 'cascade' );
48
49 $protection = "sysop";
50 if ( $this->hasOption( 'semiprotect' ) ) {
51 $protection = "autoconfirmed";
52 } elseif ( $this->hasOption( 'unprotect' ) ) {
53 $protection = "";
54 }
55
56 if ( $userName === false ) {
57 $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
58 } else {
59 $user = User::newFromName( $userName );
60 }
61 if ( !$user ) {
62 $this->error( "Invalid username", true );
63 }
64
65 // @todo FIXME: This is reset 7 lines down.
66 $restrictions = [ 'edit' => $protection, 'move' => $protection ];
67
68 $t = Title::newFromText( $this->getArg() );
69 if ( !$t ) {
70 $this->error( "Invalid title", true );
71 }
72
73 $restrictions = [];
74 foreach ( $t->getRestrictionTypes() as $type ) {
75 $restrictions[$type] = $protection;
76 }
77
78 # un/protect the article
79 $this->output( "Updating protection status... " );
80
81 $page = WikiPage::factory( $t );
82 $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user );
83
84 if ( $status->isOK() ) {
85 $this->output( "done\n" );
86 } else {
87 $this->output( "failed\n" );
88 }
89 }
90 }
91
92 $maintClass = "Protect";
93 require_once RUN_MAINTENANCE_IF_MAIN;