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