MediaWiki  master
ApiEmailUser.php
Go to the documentation of this file.
1 <?php
27 class ApiEmailUser extends ApiBase {
28 
29  public function execute() {
30  $params = $this->extractRequestParams();
31 
32  // Validate target
33  $targetUser = SpecialEmailUser::getTarget( $params['target'], $this->getUser() );
34  if ( !( $targetUser instanceof User ) ) {
35  switch ( $targetUser ) {
36  case 'notarget':
37  $this->dieWithError( 'apierror-notarget' );
38  case 'noemail':
39  $this->dieWithError( [ 'noemail', $params['target'] ] );
40  case 'nowikiemail':
41  $this->dieWithError( 'nowikiemailtext', 'nowikiemail' );
42  default:
43  $this->dieWithError( [ 'apierror-unknownerror', $targetUser ] );
44  }
45  }
46 
47  // Check permissions and errors
49  $this->getUser(),
50  $params['token'],
51  $this->getConfig()
52  );
53  if ( $error ) {
54  $this->dieWithError( $error );
55  }
56 
57  $data = [
58  'Target' => $targetUser->getName(),
59  'Text' => $params['text'],
60  'Subject' => $params['subject'],
61  'CCMe' => $params['ccme'],
62  ];
63  $retval = SpecialEmailUser::submit( $data, $this->getContext() );
64  if ( !$retval instanceof Status ) {
65  // This is probably the reason
66  $retval = Status::newFatal( 'hookaborted' );
67  }
68 
69  $result = array_filter( [
70  'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
71  'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
72  'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
73  ] );
74 
75  $this->getResult()->addValue( null, $this->getModuleName(), $result );
76  }
77 
78  public function mustBePosted() {
79  return true;
80  }
81 
82  public function isWriteMode() {
83  return true;
84  }
85 
86  public function getAllowedParams() {
87  return [
88  'target' => [
89  ApiBase::PARAM_TYPE => 'string',
91  ],
92  'subject' => null,
93  'text' => [
94  ApiBase::PARAM_TYPE => 'text',
96  ],
97  'ccme' => false,
98  ];
99  }
100 
101  public function needsToken() {
102  return 'csrf';
103  }
104 
105  protected function getExamplesMessages() {
106  return [
107  'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
108  => 'apihelp-emailuser-example-email',
109  ];
110  }
111 
112  public function getHelpUrls() {
113  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
114  }
115 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
ApiEmailUser\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiEmailUser.php:105
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:40
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:74
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1415
true
return true
Definition: router.php:90
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:68
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:546
ApiEmailUser\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiEmailUser.php:78
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
SpecialEmailUser\getTarget
static getTarget( $target, User $sender)
Validate target User.
Definition: SpecialEmailUser.php:172
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:50
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:42
ApiEmailUser\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiEmailUser.php:86
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:695
ApiEmailUser
API Module to facilitate sending of emails to users.
Definition: ApiEmailUser.php:27
ApiEmailUser\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiEmailUser.php:29
SpecialEmailUser\getPermissionsError
static getPermissionsError( $user, $editToken, Config $config=null)
Check whether a user is allowed to send email.
Definition: SpecialEmailUser.php:242
SpecialEmailUser\submit
static submit(array $data, IContextSource $context)
Really send a mail.
Definition: SpecialEmailUser.php:362
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:426
ApiEmailUser\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiEmailUser.php:82
ApiEmailUser\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiEmailUser.php:112
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
ApiEmailUser\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiEmailUser.php:101