MediaWiki fundraising/REL1_35
ApiUnblock.php
Go to the documentation of this file.
1<?php
25
32class ApiUnblock extends ApiBase {
33
35
39 public function execute() {
40 $user = $this->getUser();
41 $params = $this->extractRequestParams();
42
43 $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
44
45 if ( !$this->getPermissionManager()->userHasRight( $user, 'block' ) ) {
46 $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
47 }
48 # T17810: blocked admins should have limited access here
49 $block = $user->getBlock();
50 if ( $block ) {
51 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
52 if ( $status !== true ) {
53 $this->dieWithError(
54 $status,
55 null,
56 [ 'blockinfo' => $this->getBlockDetails( $block ) ]
57 );
58 }
59 }
60
61 // Check if user can add tags
62 if ( $params['tags'] !== null ) {
63 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
64 if ( !$ableToTag->isOK() ) {
65 $this->dieStatus( $ableToTag );
66 }
67 }
68
69 if ( $params['userid'] !== null ) {
70 $username = User::whoIs( $params['userid'] );
71
72 if ( $username === false ) {
73 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
74 } else {
75 $params['user'] = $username;
76 }
77 }
78
79 $data = [
80 'Target' => $params['id'] === null ? $params['user'] : "#{$params['id']}",
81 'Reason' => $params['reason'],
82 'Tags' => $params['tags']
83 ];
84 $block = DatabaseBlock::newFromTarget( $data['Target'] );
85 $retval = SpecialUnblock::processUnblock( $data, $this->getContext() );
86 if ( $retval !== true ) {
87 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
88 }
89
90 $target = $block->getType() == DatabaseBlock::TYPE_AUTO ? '' : $block->getTarget();
91 $res = [
92 'id' => $block->getId(),
93 'user' => $target instanceof User ? $target->getName() : $target,
94 'userid' => $target instanceof User ? $target->getId() : 0,
95 'reason' => $params['reason']
96 ];
97 $this->getResult()->addValue( null, $this->getModuleName(), $res );
98 }
99
100 public function mustBePosted() {
101 return true;
102 }
103
104 public function isWriteMode() {
105 return true;
106 }
107
108 public function getAllowedParams() {
109 return [
110 'id' => [
111 ApiBase::PARAM_TYPE => 'integer',
112 ],
113 'user' => [
114 ApiBase::PARAM_TYPE => 'user',
115 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'cidr', 'id' ],
116 ],
117 'userid' => [
118 ApiBase::PARAM_TYPE => 'integer',
120 ],
121 'reason' => '',
122 'tags' => [
123 ApiBase::PARAM_TYPE => 'tags',
125 ],
126 ];
127 }
128
129 public function needsToken() {
130 return 'csrf';
131 }
132
133 protected function getExamplesMessages() {
134 return [
135 'action=unblock&id=105'
136 => 'apihelp-unblock-example-id',
137 'action=unblock&user=Bob&reason=Sorry%20Bob'
138 => 'apihelp-unblock-example-user',
139 ];
140 }
141
142 public function getHelpUrls() {
143 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
144 }
145}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
const PARAM_DEPRECATED
Definition ApiBase.php:98
const PARAM_TYPE
Definition ApiBase.php:78
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition ApiBase.php:692
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition ApiBase.php:1248
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:909
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
const PARAM_ISMULTI
Definition ApiBase.php:74
API module that facilitates the unblocking of users.
isWriteMode()
Indicates whether this module requires write mode.
needsToken()
Returns the token type this module requires in order to execute.
execute()
Unblocks the specified user or provides the reason the unblock failed.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
getExamplesMessages()
Returns usage examples for this module.
static canAddTagsAccompanyingChange(array $tags, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
getUser()
Stable to override.
getContext()
Get the base IContextSource object.
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Type definition for user types.
Definition UserDef.php:23
static processUnblock(array $data, IContextSource $context)
Process the form.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2150
getId()
Get the user's ID.
Definition User.php:2121
trait ApiBlockInfoTrait