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