MediaWiki  1.28.0
protect.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
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 
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;
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:115
addArg($arg, $description, $required=true)
Add some args that are needed.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Maintenance script that protects or unprotects a page.
Definition: protect.php:31
__construct()
Definition: protect.php:32
hasOption($name)
Checks to see if a particular param exists.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
static newSystemUser($name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:653
addDescription($text)
Set the description text.
execute()
Definition: protect.php:43
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$maintClass
Definition: protect.php:92
getOption($name, $default=null)
Get an option, or return the default.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
output($out, $channel=null)
Throw some output to the user.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error($err, $die=0)
Throw an error to the user.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046
getArg($argId=0, $default=null)
Get an argument.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2491