MediaWiki master
ApiEmailUser.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
15
20class ApiEmailUser extends ApiBase {
21
22 public function __construct(
23 ApiMain $mainModule,
24 string $moduleName,
25 private readonly EmailUserFactory $emailUserFactory,
26 private readonly UserFactory $userFactory
27 ) {
28 parent::__construct( $mainModule, $moduleName );
29 }
30
31 public function execute() {
32 $params = $this->extractRequestParams();
33
34 $emailUser = $this->emailUserFactory->newEmailUser( $this->getAuthority() );
35 $targetUser = $this->userFactory->newFromName( $params['target'] );
36
37 if ( $targetUser === null ) {
38 $this->dieWithError(
39 [ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ],
40 "baduser_target"
41 );
42 }
43
44 $status = $emailUser->validateTarget( $targetUser );
45
46 if ( !$status->isOK() ) {
47 $this->dieStatus( $status );
48 }
49
50 // Check permissions and errors
51 $error = $emailUser->canSend();
52
53 if ( !$error->isGood() ) {
54 $this->dieStatus( $error );
55 }
56
57 $retval = $emailUser->sendEmailUnsafe(
58 $targetUser,
59 $params['subject'],
60 $params['text'],
61 $params['ccme'],
62 $this->getLanguage()->getCode()
63 );
64
65 if ( !$retval instanceof Status ) {
66 // This is probably the reason
67 $retval = Status::newFatal( 'hookaborted' );
68 }
69
70 $result = array_filter( [
71 'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
72 'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
73 'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
74 ] );
75
76 $this->getResult()->addValue( null, $this->getModuleName(), $result );
77 }
78
80 public function mustBePosted() {
81 return true;
82 }
83
85 public function isWriteMode() {
86 return true;
87 }
88
90 public function getAllowedParams() {
91 return [
92 'target' => [
93 ParamValidator::PARAM_TYPE => 'string',
94 ParamValidator::PARAM_REQUIRED => true
95 ],
96 'subject' => [
97 ParamValidator::PARAM_TYPE => 'string',
98 ParamValidator::PARAM_REQUIRED => true
99 ],
100 'text' => [
101 ParamValidator::PARAM_TYPE => 'text',
102 ParamValidator::PARAM_REQUIRED => true
103 ],
104 'ccme' => false,
105 ];
106 }
107
109 public function needsToken() {
110 return 'csrf';
111 }
112
114 protected function getExamplesMessages() {
115 return [
116 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
117 => 'apihelp-emailuser-example-email',
118 ];
119 }
120
122 public function getHelpUrls() {
123 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
124 }
125}
126
128class_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:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getResult()
Get the result object.
Definition ApiBase.php:696
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1573
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
API Module to facilitate sending of emails to users.
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...
__construct(ApiMain $mainModule, string $moduleName, private readonly EmailUserFactory $emailUserFactory, private readonly UserFactory $userFactory)
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
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.