MediaWiki REL1_37
ApiEmailUser.php
Go to the documentation of this file.
1<?php
27class 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' => null,
99 'text' => [
100 ApiBase::PARAM_TYPE => 'text',
102 ],
103 'ccme' => false,
104 ];
105 }
106
107 public function needsToken() {
108 return 'csrf';
109 }
110
111 protected function getExamplesMessages() {
112 return [
113 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
114 => 'apihelp-emailuser-example-email',
115 ];
116 }
117
118 public function getHelpUrls() {
119 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
120 }
121}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
const PARAM_REQUIRED
Definition ApiBase.php:105
const PARAM_TYPE
Definition ApiBase.php:81
getErrorFormatter()
Definition ApiBase.php:639
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
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.
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:69
return true
Definition router.php:92