MediaWiki master
ApiEmailUser.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
16
21class ApiEmailUser extends ApiBase {
22
23 private EmailUserFactory $emailUserFactory;
24 private UserFactory $userFactory;
25
26 public function __construct( ApiMain $mainModule, string $moduleName,
27 EmailUserFactory $emailUserFactory, UserFactory $userFactory ) {
28 parent::__construct( $mainModule, $moduleName );
29
30 $this->emailUserFactory = $emailUserFactory;
31 $this->userFactory = $userFactory;
32 }
33
34 public function execute() {
35 $params = $this->extractRequestParams();
36
37 $emailUser = $this->emailUserFactory->newEmailUser( RequestContext::getMain()->getAuthority() );
38 $targetUser = $this->userFactory->newFromName( $params['target'] );
39
40 if ( $targetUser === null ) {
41 $this->dieWithError(
42 [ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ],
43 "baduser_target"
44 );
45 }
46
47 $status = $emailUser->validateTarget( $targetUser );
48
49 if ( !$status->isOK() ) {
50 $this->dieStatus( $status );
51 }
52
53 // Check permissions and errors
54 $error = $emailUser->canSend();
55
56 if ( !$error->isGood() ) {
57 $this->dieStatus( $error );
58 }
59
60 $retval = $emailUser->sendEmailUnsafe(
61 $targetUser,
62 $params['subject'],
63 $params['text'],
64 $params['ccme'],
65 $this->getLanguage()->getCode()
66 );
67
68 if ( !$retval instanceof Status ) {
69 // This is probably the reason
70 $retval = Status::newFatal( 'hookaborted' );
71 }
72
73 $result = array_filter( [
74 'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
75 'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
76 'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
77 ] );
78
79 $this->getResult()->addValue( null, $this->getModuleName(), $result );
80 }
81
83 public function mustBePosted() {
84 return true;
85 }
86
88 public function isWriteMode() {
89 return true;
90 }
91
93 public function getAllowedParams() {
94 return [
95 'target' => [
96 ParamValidator::PARAM_TYPE => 'string',
97 ParamValidator::PARAM_REQUIRED => true
98 ],
99 'subject' => [
100 ParamValidator::PARAM_TYPE => 'string',
101 ParamValidator::PARAM_REQUIRED => true
102 ],
103 'text' => [
104 ParamValidator::PARAM_TYPE => 'text',
105 ParamValidator::PARAM_REQUIRED => true
106 ],
107 'ccme' => false,
108 ];
109 }
110
112 public function needsToken() {
113 return 'csrf';
114 }
115
117 protected function getExamplesMessages() {
118 return [
119 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
120 => 'apihelp-emailuser-example-email',
121 ];
122 }
123
125 public function getHelpUrls() {
126 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
127 }
128}
129
131class_alias( ApiEmailUser::class, 'ApiEmailUser' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
getResult()
Get the result object.
Definition ApiBase.php:682
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1562
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
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.Implementations of this method must ...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
needsToken()
Returns the token type this module requires in order to execute.Modules are strongly encouraged to us...
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.API modules must override this method...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
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:66
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:44
Create User objects.
Service for formatting and validating API parameters.