phpcs: More require/include is not a function
[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->mDescription = "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', 'Maintenance script' );
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 $user = User::newFromName( $userName );
57 if ( !$user ) {
58 $this->error( "Invalid username", true );
59 }
60
61 // @todo FIXME: This is reset 7 lines down.
62 $restrictions = array( 'edit' => $protection, 'move' => $protection );
63
64 $t = Title::newFromText( $this->getArg() );
65 if ( !$t ) {
66 $this->error( "Invalid title", true );
67 }
68
69 $restrictions = array();
70 foreach ( $t->getRestrictionTypes() as $type ) {
71 $restrictions[$type] = $protection;
72 }
73
74 # un/protect the article
75 $this->output( "Updating protection status... " );
76
77 $page = WikiPage::factory( $t );
78 $status = $page->doUpdateRestrictions( $restrictions, array(), $cascade, $reason, $user );
79
80 if ( $status->isOK() ) {
81 $this->output( "done\n" );
82 } else {
83 $this->output( "failed\n" );
84 }
85 }
86 }
87
88 $maintClass = "Protect";
89 require_once RUN_MAINTENANCE_IF_MAIN;