MediaWiki  master
ApiEmailUser.php
Go to the documentation of this file.
1 <?php
27 
32 class ApiEmailUser extends ApiBase {
33 
34  public function execute() {
35  $params = $this->extractRequestParams();
36 
37  // Validate target
38  $targetUser = SpecialEmailUser::getTarget( $params['target'], $this->getUser() );
39  if ( !( $targetUser instanceof User ) ) {
40  switch ( $targetUser ) {
41  case 'notarget':
42  $this->dieWithError( 'apierror-notarget' );
43  // dieWithError prevents continuation
44 
45  case 'noemail':
46  $this->dieWithError( [ 'noemail', $params['target'] ] );
47  // dieWithError prevents continuation
48 
49  case 'nowikiemail':
50  $this->dieWithError( 'nowikiemailtext', 'nowikiemail' );
51  // dieWithError prevents continuation
52 
53  default:
54  $this->dieWithError( [ 'apierror-unknownerror', $targetUser ] );
55  }
56  }
57 
58  // Check permissions and errors
59  $error = SpecialEmailUser::getPermissionsError(
60  $this->getUser(),
61  $params['token'],
62  $this->getConfig(),
63  true // authorize!
64  );
65  if ( $error ) {
66  $this->dieWithError( $error );
67  }
68 
69  $data = [
70  'Target' => $targetUser->getName(),
71  'Text' => $params['text'],
72  'Subject' => $params['subject'],
73  'CCMe' => $params['ccme'],
74  ];
75  $retval = SpecialEmailUser::submit( $data, $this->getContext() );
76  if ( !$retval instanceof Status ) {
77  // This is probably the reason
78  $retval = Status::newFatal( 'hookaborted' );
79  }
80 
81  $result = array_filter( [
82  'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
83  'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
84  'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
85  ] );
86 
87  $this->getResult()->addValue( null, $this->getModuleName(), $result );
88  }
89 
90  public function mustBePosted() {
91  return true;
92  }
93 
94  public function isWriteMode() {
95  return true;
96  }
97 
98  public function getAllowedParams() {
99  return [
100  'target' => [
101  ParamValidator::PARAM_TYPE => 'string',
102  ParamValidator::PARAM_REQUIRED => true
103  ],
104  'subject' => [
105  ParamValidator::PARAM_TYPE => 'string',
106  ParamValidator::PARAM_REQUIRED => true
107  ],
108  'text' => [
109  ParamValidator::PARAM_TYPE => 'text',
110  ParamValidator::PARAM_REQUIRED => true
111  ],
112  'ccme' => false,
113  ];
114  }
115 
116  public function needsToken() {
117  return 'csrf';
118  }
119 
120  protected function getExamplesMessages() {
121  return [
122  'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
123  => 'apihelp-emailuser-example-email',
124  ];
125  }
126 
127  public function getHelpUrls() {
128  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
129  }
130 }
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:62
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1515
getResult()
Get the result object.
Definition: ApiBase.php:667
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:807
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:528
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.
A special page that allows users to send e-mails to other users.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:58
internal since 1.36
Definition: User.php:98
Service for formatting and validating API parameters.
return true
Definition: router.php:90