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  // dieWithError prevents continuation
39 
40  case 'noemail':
41  $this->dieWithError( [ 'noemail', $params['target'] ] );
42  // dieWithError prevents continuation
43 
44  case 'nowikiemail':
45  $this->dieWithError( 'nowikiemailtext', 'nowikiemail' );
46  // dieWithError prevents continuation
47 
48  default:
49  $this->dieWithError( [ 'apierror-unknownerror', $targetUser ] );
50  }
51  }
52 
53  // Check permissions and errors
55  $this->getUser(),
56  $params['token'],
57  $this->getConfig()
58  );
59  if ( $error ) {
60  $this->dieWithError( $error );
61  }
62 
63  $data = [
64  'Target' => $targetUser->getName(),
65  'Text' => $params['text'],
66  'Subject' => $params['subject'],
67  'CCMe' => $params['ccme'],
68  ];
69  $retval = SpecialEmailUser::submit( $data, $this->getContext() );
70  if ( !$retval instanceof Status ) {
71  // This is probably the reason
72  $retval = Status::newFatal( 'hookaborted' );
73  }
74 
75  $result = array_filter( [
76  'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
77  'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
78  'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
79  ] );
80 
81  $this->getResult()->addValue( null, $this->getModuleName(), $result );
82  }
83 
84  public function mustBePosted() {
85  return true;
86  }
87 
88  public function isWriteMode() {
89  return true;
90  }
91 
92  public function getAllowedParams() {
93  return [
94  'target' => [
95  ApiBase::PARAM_TYPE => 'string',
97  ],
98  'subject' => [
99  ApiBase::PARAM_TYPE => 'string',
101  ],
102  'text' => [
103  ApiBase::PARAM_TYPE => 'text',
105  ],
106  'ccme' => false,
107  ];
108  }
109 
110  public function needsToken() {
111  return 'csrf';
112  }
113 
114  protected function getExamplesMessages() {
115  return [
116  'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
117  => 'apihelp-emailuser-example-email',
118  ];
119  }
120 
121  public function getHelpUrls() {
122  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
123  }
124 }
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:56
const PARAM_REQUIRED
Definition: ApiBase.php:106
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1446
const PARAM_TYPE
Definition: ApiBase.php:82
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:498
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:70
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
return true
Definition: router.php:90