| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Title\Title; |
| 25 |
use MediaWiki\User\User; |
| 26 |
|
| 27 |
|
| 28 |
require_once __DIR__ . '/Maintenance.php'; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class Protect extends Maintenance { |
| 37 |
public function __construct() { |
| 38 |
parent::__construct(); |
| 39 |
$this->addDescription( 'Protect or unprotect a page from the command line.' ); |
| 40 |
$this->addOption( 'unprotect', 'Removes protection' ); |
| 41 |
$this->addOption( 'semiprotect', 'Adds semi-protection' ); |
| 42 |
$this->addOption( 'cascade', 'Add cascading protection' ); |
| 43 |
$this->addOption( 'user', 'Username to protect with', false, true, 'u' ); |
| 44 |
$this->addOption( 'reason', 'Reason for un/protection', false, true, 'r' ); |
| 45 |
$this->addArg( 'title', 'Title to protect', true ); |
| 46 |
} |
| 47 |
|
| 48 |
public function execute() { |
| 49 |
$userName = $this->getOption( 'user', false ); |
| 50 |
$reason = $this->getOption( 'reason', '' ); |
| 51 |
|
| 52 |
$cascade = $this->hasOption( 'cascade' ); |
| 53 |
|
| 54 |
$protection = "sysop"; |
| 55 |
if ( $this->hasOption( 'semiprotect' ) ) { |
| 56 |
$protection = "autoconfirmed"; |
| 57 |
} elseif ( $this->hasOption( 'unprotect' ) ) { |
| 58 |
$protection = ""; |
| 59 |
} |
| 60 |
|
| 61 |
if ( $userName === false ) { |
| 62 |
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
| 63 |
} else { |
| 64 |
$user = User::newFromName( $userName ); |
| 65 |
} |
| 66 |
if ( !$user ) { |
| 67 |
$this->fatalError( "Invalid username" ); |
| 68 |
} |
| 69 |
|
| 70 |
$t = Title::newFromText( $this->getArg( 0 ) ); |
| 71 |
if ( !$t ) { |
| 72 |
$this->fatalError( "Invalid title" ); |
| 73 |
} |
| 74 |
|
| 75 |
$services = $this->getServiceContainer(); |
| 76 |
$restrictions = []; |
| 77 |
foreach ( $services->getRestrictionStore()->listApplicableRestrictionTypes( $t ) as $type ) { |
| 78 |
$restrictions[$type] = $protection; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
$this->output( "Updating protection status..." ); |
| 83 |
|
| 84 |
$page = $services->getWikiPageFactory()->newFromTitle( $t ); |
| 85 |
$status = $page->doUpdateRestrictions( $restrictions, [], $cascade, $reason, $user ); |
| 86 |
|
| 87 |
if ( $status->isOK() ) { |
| 88 |
$this->output( "done\n" ); |
| 89 |
} else { |
| 90 |
$this->output( "failed\n" ); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
$maintClass = Protect::class; |
| 97 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 98 |
|