MediaWiki master
ApiEmailUser.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
30
35class ApiEmailUser extends ApiBase {
36
37 private EmailUserFactory $emailUserFactory;
38 private UserFactory $userFactory;
39
40 public function __construct( ApiMain $mainModule, string $moduleName,
41 EmailUserFactory $emailUserFactory, UserFactory $userFactory ) {
42 parent::__construct( $mainModule, $moduleName );
43
44 $this->emailUserFactory = $emailUserFactory;
45 $this->userFactory = $userFactory;
46 }
47
48 public function execute() {
50
51 $emailUser = $this->emailUserFactory->newEmailUser( RequestContext::getMain()->getAuthority() );
52 $targetUser = $this->userFactory->newFromName( $params['target'] );
53
54 if ( $targetUser === null ) {
55 $this->dieWithError(
56 [ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ],
57 "baduser_target"
58 );
59 }
60
61 $status = $emailUser->validateTarget( $targetUser );
62
63 if ( !$status->isOK() ) {
64 $this->dieStatus( $status );
65 }
66
67 // Check permissions and errors
68 $error = $emailUser->canSend();
69
70 if ( !$error->isGood() ) {
71 $this->dieStatus( $error );
72 }
73
74 $retval = $emailUser->sendEmailUnsafe(
75 $targetUser,
76 $params['subject'],
77 $params['text'],
78 $params['ccme'],
79 $this->getLanguage()->getCode()
80 );
81
82 if ( !$retval instanceof Status ) {
83 // This is probably the reason
84 $retval = Status::newFatal( 'hookaborted' );
85 }
86
87 $result = array_filter( [
88 'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
89 'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
90 'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
91 ] );
92
93 $this->getResult()->addValue( null, $this->getModuleName(), $result );
94 }
95
96 public function mustBePosted() {
97 return true;
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
103
104 public function getAllowedParams() {
105 return [
106 'target' => [
107 ParamValidator::PARAM_TYPE => 'string',
108 ParamValidator::PARAM_REQUIRED => true
109 ],
110 'subject' => [
111 ParamValidator::PARAM_TYPE => 'string',
112 ParamValidator::PARAM_REQUIRED => true
113 ],
114 'text' => [
115 ParamValidator::PARAM_TYPE => 'text',
116 ParamValidator::PARAM_REQUIRED => true
117 ],
118 'ccme' => false,
119 ];
120 }
121
122 public function needsToken() {
123 return 'csrf';
124 }
125
126 protected function getExamplesMessages() {
127 return [
128 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
129 => 'apihelp-emailuser-example-email',
130 ];
131 }
132
133 public function getHelpUrls() {
134 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
135 }
136}
137
139class_alias( ApiEmailUser::class, 'ApiEmailUser' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1565
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
getResult()
Get the result object.
Definition ApiBase.php:710
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
API Module to facilitate sending of emails to users.
__construct(ApiMain $mainModule, string $moduleName, EmailUserFactory $emailUserFactory, UserFactory $userFactory)
mustBePosted()
Indicates whether this module must be called with a POST request.
getExamplesMessages()
Returns usage examples for this module.
needsToken()
Returns the token type this module requires in order to execute.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
isWriteMode()
Indicates whether this module requires write access to the wiki.
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
Group all the pieces relevant to the context of a request into one instance.
Factory for EmailUser objects.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Create User objects.
Service for formatting and validating API parameters.