Merge "CSSMin: Clean up $remote trailing slash fix"
[lhc/web/wiklou.git] / maintenance / protect.php
1 <?php
2 /**
3 * Protect or unprotect an article.
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 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
25 class Protect extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Protect or unprotect an article from the command line.";
29 $this->addOption( 'unprotect', 'Removes protection' );
30 $this->addOption( 'semiprotect', 'Adds semi-protection' );
31 $this->addOption( 'cascade', 'Add cascading protection' );
32 $this->addOption( 'user', 'Username to protect with', false, true, 'u' );
33 $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' );
34 $this->addArg( 'title', 'Title to protect', true );
35 }
36
37 public function execute() {
38 $userName = $this->getOption( 'u', 'Maintenance script' );
39 $reason = $this->getOption( 'r', '' );
40
41 $cascade = $this->hasOption( 'cascade' );
42
43 $protection = "sysop";
44 if ( $this->hasOption( 'semiprotect' ) ) {
45 $protection = "autoconfirmed";
46 } elseif ( $this->hasOption( 'unprotect' ) ) {
47 $protection = "";
48 }
49
50 $user = User::newFromName( $userName );
51 if ( !$user ) {
52 $this->error( "Invalid username", true );
53 }
54
55 $restrictions = array( 'edit' => $protection, 'move' => $protection );
56
57 $t = Title::newFromText( $this->getArg() );
58 if ( !$t ) {
59 $this->error( "Invalid title", true );
60 }
61
62 $restrictions = array();
63 foreach( $t->getRestrictionTypes() as $type ) {
64 $restrictions[$type] = $protection;
65 }
66
67 # un/protect the article
68 $this->output( "Updating protection status... " );
69
70 $page = WikiPage::factory( $t );
71 $status = $page->doUpdateRestrictions( $restrictions, array(), $cascade, $reason, $user );
72
73 if ( $status->isOK() ) {
74 $this->output( "done\n" );
75 } else {
76 $this->output( "failed\n" );
77 }
78 }
79 }
80
81 $maintClass = "Protect";
82 require_once( RUN_MAINTENANCE_IF_MAIN );