Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
36 / 36 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Protect | |
100.00% |
36 / 36 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
8 |
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 | use MediaWiki\Maintenance\Maintenance; |
25 | use MediaWiki\Title\Title; |
26 | use MediaWiki\User\User; |
27 | |
28 | // @codeCoverageIgnoreStart |
29 | require_once __DIR__ . '/Maintenance.php'; |
30 | // @codeCoverageIgnoreEnd |
31 | |
32 | /** |
33 | * Maintenance script that protects or unprotects a page. |
34 | * |
35 | * @ingroup Maintenance |
36 | */ |
37 | class Protect extends Maintenance { |
38 | public function __construct() { |
39 | parent::__construct(); |
40 | $this->addDescription( 'Protect or unprotect a page from the command line.' ); |
41 | $this->addOption( 'unprotect', 'Removes protection' ); |
42 | $this->addOption( 'semiprotect', 'Adds semi-protection' ); |
43 | $this->addOption( 'cascade', 'Add cascading protection' ); |
44 | $this->addOption( 'user', 'Username to protect with', false, true, 'u' ); |
45 | $this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' ); |
46 | $this->addArg( 'title', 'Title to protect', true ); |
47 | } |
48 | |
49 | public function execute() { |
50 | $userName = $this->getOption( 'user', false ); |
51 | $reason = $this->getOption( 'reason', '' ); |
52 | |
53 | $cascade = $this->hasOption( 'cascade' ); |
54 | |
55 | $protection = "sysop"; |
56 | if ( $this->hasOption( 'semiprotect' ) ) { |
57 | $protection = "autoconfirmed"; |
58 | } elseif ( $this->hasOption( 'unprotect' ) ) { |
59 | $protection = ""; |
60 | } |
61 | |
62 | if ( $userName === false ) { |
63 | $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
64 | } else { |
65 | $user = User::newFromName( $userName ); |
66 | } |
67 | if ( !$user ) { |
68 | $this->fatalError( "Invalid username" ); |
69 | } |
70 | |
71 | $t = Title::newFromText( $this->getArg( 0 ) ); |
72 | if ( !$t ) { |
73 | $this->fatalError( "Invalid title" ); |
74 | } |
75 | |
76 | $services = $this->getServiceContainer(); |
77 | $restrictions = []; |
78 | foreach ( $services->getRestrictionStore()->listApplicableRestrictionTypes( $t ) as $type ) { |
79 | $restrictions[$type] = $protection; |
80 | } |
81 | |
82 | # un/protect the article |
83 | $this->output( "Updating protection status..." ); |
84 | |
85 | $this->beginTransactionRound( __METHOD__ ); |
86 | $page = $services->getWikiPageFactory()->newFromTitle( $t ); |
87 | $status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user ); |
88 | $this->commitTransactionRound( __METHOD__ ); |
89 | |
90 | if ( $status->isOK() ) { |
91 | $this->output( "done\n" ); |
92 | } else { |
93 | $this->output( "failed\n" ); |
94 | } |
95 | } |
96 | } |
97 | |
98 | // @codeCoverageIgnoreStart |
99 | $maintClass = Protect::class; |
100 | require_once RUN_MAINTENANCE_IF_MAIN; |
101 | // @codeCoverageIgnoreEnd |