MediaWiki master
ApiEmailUser.php
Go to the documentation of this file.
1<?php
28
33class ApiEmailUser extends ApiBase {
34
35 private EmailUserFactory $emailUserFactory;
36 private UserFactory $userFactory;
37
38 public function __construct( ApiMain $mainModule, $moduleName,
39 EmailUserFactory $emailUserFactory, UserFactory $userFactory ) {
40 parent::__construct( $mainModule, $moduleName );
41
42 $this->emailUserFactory = $emailUserFactory;
43 $this->userFactory = $userFactory;
44 }
45
46 public function execute() {
48
49 $emailUser = $this->emailUserFactory->newEmailUser( RequestContext::getMain()->getAuthority() );
50 $targetUser = $this->userFactory->newFromName( $params['target'] );
51
52 if ( $targetUser === null ) {
53 $this->dieWithError(
54 [ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ],
55 "baduser_target"
56 );
57 }
58
59 $status = $emailUser->validateTarget( $targetUser );
60
61 if ( !$status->isOK() ) {
62 $this->dieStatus( $status );
63 }
64
65 // Check permissions and errors
66 $error = $emailUser->canSend();
67
68 if ( !$error->isGood() ) {
69 $this->dieStatus( $error );
70 }
71
72 $retval = $emailUser->sendEmailUnsafe(
73 $targetUser,
74 $params['subject'],
75 $params['text'],
76 $params['ccme'],
77 $this->getLanguage()->getCode()
78 );
79
80 if ( !$retval instanceof Status ) {
81 // This is probably the reason
82 $retval = Status::newFatal( 'hookaborted' );
83 }
84
85 $result = array_filter( [
86 'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
87 'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
88 'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
89 ] );
90
91 $this->getResult()->addValue( null, $this->getModuleName(), $result );
92 }
93
94 public function mustBePosted() {
95 return true;
96 }
97
98 public function isWriteMode() {
99 return true;
100 }
101
102 public function getAllowedParams() {
103 return [
104 'target' => [
105 ParamValidator::PARAM_TYPE => 'string',
106 ParamValidator::PARAM_REQUIRED => true
107 ],
108 'subject' => [
109 ParamValidator::PARAM_TYPE => 'string',
110 ParamValidator::PARAM_REQUIRED => true
111 ],
112 'text' => [
113 ParamValidator::PARAM_TYPE => 'text',
114 ParamValidator::PARAM_REQUIRED => true
115 ],
116 'ccme' => false,
117 ];
118 }
119
120 public function needsToken() {
121 return 'csrf';
122 }
123
124 protected function getExamplesMessages() {
125 return [
126 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
127 => 'apihelp-emailuser-example-email',
128 ];
129 }
130
131 public function getHelpUrls() {
132 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
133 }
134}
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:65
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1540
getErrorFormatter()
Definition ApiBase.php:692
getResult()
Get the result object.
Definition ApiBase.php:681
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:821
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:542
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1595
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...
__construct(ApiMain $mainModule, $moduleName, EmailUserFactory $emailUserFactory, UserFactory $userFactory)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:67
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
Creates User objects.
Service for formatting and validating API parameters.