Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiEmailUser.php
blobb581758ea0474769f75eeb8833af757892df4af0
1 <?php
2 /**
3 * Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\Context\RequestContext;
26 use MediaWiki\Mail\EmailUserFactory;
27 use MediaWiki\Status\Status;
28 use MediaWiki\User\UserFactory;
29 use Wikimedia\ParamValidator\ParamValidator;
31 /**
32 * API Module to facilitate sending of emails to users
33 * @ingroup API
35 class ApiEmailUser extends ApiBase {
37 private EmailUserFactory $emailUserFactory;
38 private UserFactory $userFactory;
40 public function __construct( ApiMain $mainModule, string $moduleName,
41 EmailUserFactory $emailUserFactory, UserFactory $userFactory ) {
42 parent::__construct( $mainModule, $moduleName );
44 $this->emailUserFactory = $emailUserFactory;
45 $this->userFactory = $userFactory;
48 public function execute() {
49 $params = $this->extractRequestParams();
51 $emailUser = $this->emailUserFactory->newEmailUser( RequestContext::getMain()->getAuthority() );
52 $targetUser = $this->userFactory->newFromName( $params['target'] );
54 if ( $targetUser === null ) {
55 $this->dieWithError(
56 [ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ],
57 "baduser_target"
61 $status = $emailUser->validateTarget( $targetUser );
63 if ( !$status->isOK() ) {
64 $this->dieStatus( $status );
67 // Check permissions and errors
68 $error = $emailUser->canSend();
70 if ( !$error->isGood() ) {
71 $this->dieStatus( $error );
74 $retval = $emailUser->sendEmailUnsafe(
75 $targetUser,
76 $params['subject'],
77 $params['text'],
78 $params['ccme'],
79 $this->getLanguage()->getCode()
82 if ( !$retval instanceof Status ) {
83 // This is probably the reason
84 $retval = Status::newFatal( 'hookaborted' );
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 ] );
93 $this->getResult()->addValue( null, $this->getModuleName(), $result );
96 public function mustBePosted() {
97 return true;
100 public function isWriteMode() {
101 return true;
104 public function getAllowedParams() {
105 return [
106 'target' => [
107 ParamValidator::PARAM_TYPE => 'string',
108 ParamValidator::PARAM_REQUIRED => true
110 'subject' => [
111 ParamValidator::PARAM_TYPE => 'string',
112 ParamValidator::PARAM_REQUIRED => true
114 'text' => [
115 ParamValidator::PARAM_TYPE => 'text',
116 ParamValidator::PARAM_REQUIRED => true
118 'ccme' => false,
122 public function needsToken() {
123 return 'csrf';
126 protected function getExamplesMessages() {
127 return [
128 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
129 => 'apihelp-emailuser-example-email',
133 public function getHelpUrls() {
134 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
138 /** @deprecated class alias since 1.43 */
139 class_alias( ApiEmailUser::class, 'ApiEmailUser' );