MediaWiki  1.23.8
ApiBlock.php
Go to the documentation of this file.
1 <?php
33 class ApiBlock extends ApiBase {
34 
41  public function execute() {
42  $user = $this->getUser();
43  $params = $this->extractRequestParams();
44 
45  if ( !$user->isAllowed( 'block' ) ) {
46  $this->dieUsageMsg( 'cantblock' );
47  }
48 
49  # bug 15810: blocked admins should have limited access here
50  if ( $user->isBlocked() ) {
51  $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
52  if ( $status !== true ) {
53  $this->dieUsageMsg( array( $status ) );
54  }
55  }
56 
57  $target = User::newFromName( $params['user'] );
58  // Bug 38633 - if the target is a user (not an IP address), but it
59  // doesn't exist or is unusable, error.
60  if ( $target instanceof User &&
61  ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
62  ) {
63  $this->dieUsageMsg( array( 'nosuchuser', $params['user'] ) );
64  }
65 
66  if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
67  $this->dieUsageMsg( 'canthide' );
68  }
69  if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
70  $this->dieUsageMsg( 'cantblock-email' );
71  }
72 
73  $data = array(
74  'PreviousTarget' => $params['user'],
75  'Target' => $params['user'],
76  'Reason' => array(
77  $params['reason'],
78  'other',
79  $params['reason']
80  ),
81  'Expiry' => $params['expiry'] == 'never' ? 'infinite' : $params['expiry'],
82  'HardBlock' => !$params['anononly'],
83  'CreateAccount' => $params['nocreate'],
84  'AutoBlock' => $params['autoblock'],
85  'DisableEmail' => $params['noemail'],
86  'HideUser' => $params['hidename'],
87  'DisableUTEdit' => !$params['allowusertalk'],
88  'Reblock' => $params['reblock'],
89  'Watch' => $params['watchuser'],
90  'Confirm' => true,
91  );
92 
93  $retval = SpecialBlock::processForm( $data, $this->getContext() );
94  if ( $retval !== true ) {
95  // We don't care about multiple errors, just report one of them
96  $this->dieUsageMsg( $retval );
97  }
98 
99  list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
100  $res['user'] = $params['user'];
101  $res['userID'] = $target instanceof User ? $target->getId() : 0;
102 
103  $block = Block::newFromTarget( $target );
104  if ( $block instanceof Block ) {
105  $res['expiry'] = $block->mExpiry == $this->getDB()->getInfinity()
106  ? 'infinite'
107  : wfTimestamp( TS_ISO_8601, $block->mExpiry );
108  $res['id'] = $block->getId();
109  } else {
110  # should be unreachable
111  $res['expiry'] = '';
112  $res['id'] = '';
113  }
114 
115  $res['reason'] = $params['reason'];
116  if ( $params['anononly'] ) {
117  $res['anononly'] = '';
118  }
119  if ( $params['nocreate'] ) {
120  $res['nocreate'] = '';
121  }
122  if ( $params['autoblock'] ) {
123  $res['autoblock'] = '';
124  }
125  if ( $params['noemail'] ) {
126  $res['noemail'] = '';
127  }
128  if ( $params['hidename'] ) {
129  $res['hidename'] = '';
130  }
131  if ( $params['allowusertalk'] ) {
132  $res['allowusertalk'] = '';
133  }
134  if ( $params['watchuser'] ) {
135  $res['watchuser'] = '';
136  }
137 
138  $this->getResult()->addValue( null, $this->getModuleName(), $res );
139  }
140 
141  public function mustBePosted() {
142  return true;
143  }
144 
145  public function isWriteMode() {
146  return true;
147  }
148 
149  public function getAllowedParams() {
150  return array(
151  'user' => array(
152  ApiBase::PARAM_TYPE => 'string',
154  ),
155  'token' => null,
156  'expiry' => 'never',
157  'reason' => '',
158  'anononly' => false,
159  'nocreate' => false,
160  'autoblock' => false,
161  'noemail' => false,
162  'hidename' => false,
163  'allowusertalk' => false,
164  'reblock' => false,
165  'watchuser' => false,
166  );
167  }
168 
169  public function getParamDescription() {
170  return array(
171  'user' => 'Username, IP address or IP range you want to block',
172  'token' => 'A block token previously obtained through prop=info',
173  'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. ' .
174  'If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
175  'reason' => 'Reason for block',
176  'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
177  'nocreate' => 'Prevent account creation',
178  'autoblock' => 'Automatically block the last used IP address, and ' .
179  'any subsequent IP addresses they try to login from',
180  'noemail'
181  => 'Prevent user from sending email through the wiki. (Requires the "blockemail" right.)',
182  'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
183  'allowusertalk'
184  => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
185  'reblock' => 'If the user is already blocked, overwrite the existing block',
186  'watchuser' => 'Watch the user/IP\'s user and talk pages',
187  );
188  }
189 
190  public function getResultProperties() {
191  return array(
192  '' => array(
193  'user' => array(
194  ApiBase::PROP_TYPE => 'string',
195  ApiBase::PROP_NULLABLE => true
196  ),
197  'userID' => array(
198  ApiBase::PROP_TYPE => 'integer',
199  ApiBase::PROP_NULLABLE => true
200  ),
201  'expiry' => array(
202  ApiBase::PROP_TYPE => 'string',
203  ApiBase::PROP_NULLABLE => true
204  ),
205  'id' => array(
206  ApiBase::PROP_TYPE => 'integer',
207  ApiBase::PROP_NULLABLE => true
208  ),
209  'reason' => array(
210  ApiBase::PROP_TYPE => 'string',
211  ApiBase::PROP_NULLABLE => true
212  ),
213  'anononly' => 'boolean',
214  'nocreate' => 'boolean',
215  'autoblock' => 'boolean',
216  'noemail' => 'boolean',
217  'hidename' => 'boolean',
218  'allowusertalk' => 'boolean',
219  'watchuser' => 'boolean'
220  )
221  );
222  }
223 
224  public function getDescription() {
225  return 'Block a user.';
226  }
227 
228  public function getPossibleErrors() {
229  return array_merge( parent::getPossibleErrors(), array(
230  array( 'cantblock' ),
231  array( 'canthide' ),
232  array( 'cantblock-email' ),
233  array( 'ipbblocked' ),
234  array( 'ipbnounblockself' ),
235  ) );
236  }
237 
238  public function needsToken() {
239  return true;
240  }
241 
242  public function getTokenSalt() {
243  return '';
244  }
245 
246  public function getExamples() {
247  return array(
248  'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
249  'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
250  );
251  }
252 
253  public function getHelpUrls() {
254  return 'https://www.mediawiki.org/wiki/API:Block';
255  }
256 }
ContextSource\getContext
getContext()
Get the RequestContext object.
Definition: ContextSource.php:40
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
User\getId
getId()
Get the user's ID.
Definition: User.php:1853
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:62
SpecialBlock\processForm
static processForm(array $data, IContextSource $context)
Given the form data, actually implement a block.
Definition: SpecialBlock.php:595
ApiBase\dieUsageMsg
dieUsageMsg( $error)
Output the error message related to a certain array.
Definition: ApiBase.php:1933
SpecialBlock\canBlockEmail
static canBlockEmail( $user)
Can we do an email block?
Definition: SpecialBlock.php:855
ApiBlock\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiBlock.php:141
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:50
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:205
ApiBlock\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiBlock.php:145
$params
$params
Definition: styleTest.css.php:40
Block\newFromTarget
static newFromTarget( $specificTarget, $vagueTarget=null, $fromMaster=false)
Given a target and the target's type, get an existing Block object if possible.
Definition: Block.php:970
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:389
ApiBase\getDB
getDB()
Gets a default slave database connection object.
Definition: ApiBase.php:2316
ContextSource\getUser
getUser()
Get the User object.
Definition: ContextSource.php:132
ApiBlock\getTokenSalt
getTokenSalt()
Returns the token salt if there is one, '' if the module doesn't require a salt, else false if the mo...
Definition: ApiBlock.php:242
ApiBlock\needsToken
needsToken()
Returns whether this module requires a token to execute It is used to show possible errors in action=...
Definition: ApiBlock.php:238
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiBlock\getExamples
getExamples()
Returns usage examples for this module.
Definition: ApiBlock.php:246
ApiBlock
API module that facilitates the blocking of users.
Definition: ApiBlock.php:33
ApiBlock\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiBlock.php:224
TS_ISO_8601
const TS_ISO_8601
ISO 8601 format with no timezone: 1986-02-09T20:00:00Z.
Definition: GlobalFunctions.php:2448
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiBase\PROP_TYPE
const PROP_TYPE
Definition: ApiBase.php:74
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:687
ApiBlock\getResultProperties
getResultProperties()
Returns possible properties in the result, grouped by the value of the prop parameter that shows them...
Definition: ApiBlock.php:190
ApiBlock\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiBlock.php:169
ApiBase\PROP_NULLABLE
const PROP_NULLABLE
Definition: ApiBase.php:76
ApiBlock\getHelpUrls
getHelpUrls()
Definition: ApiBlock.php:253
ApiBlock\getPossibleErrors
getPossibleErrors()
Returns a list of all possible errors returned by the module.
Definition: ApiBlock.php:228
$user
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 account $user
Definition: hooks.txt:237
ApiBlock\execute
execute()
Blocks the user specified in the parameters for the given expiry, with the given reason,...
Definition: ApiBlock.php:41
Block
Definition: Block.php:22
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:148
SpecialBlock\checkUnblockSelf
static checkUnblockSelf( $user, User $performer)
bug 15810: blocked admins should not be able to block/unblock others, and probably shouldn't be able ...
Definition: SpecialBlock.php:869
SpecialBlock\getTargetAndType
static getTargetAndType( $par, WebRequest $request=null)
Determine the target of the block, and the type of target TODO: should be in Block....
Definition: SpecialBlock.php:455
User\isUsableName
static isUsableName( $name)
Usernames which fail to pass this function will be blocked from user login and new account registrati...
Definition: User.php:624
ApiBlock\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiBlock.php:149
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:59
$res
$res
Definition: database.txt:21
$retval
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 account incomplete not yet checked for validity & $retval
Definition: hooks.txt:237