Debian Codegraph @

includes/api/ApiEmailUser.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2008 Bryan Tong Minh <[email protected]>
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Api;
24
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;
30
31 /**
32 * API Module to facilitate sending of emails to users
33 * @ingroup API
34 */
35 class ApiEmailUser extends ApiBase {
36
37 private EmailUserFactory $emailUserFactory;
38 private UserFactory $userFactory;
39
40 public function __construct( ApiMain $mainModule, string $moduleName,
41 EmailUserFactory $emailUserFactory, UserFactory $userFactory ) {
42 parent::__construct( $mainModule, $moduleName );
43
44 $this->emailUserFactory = $emailUserFactory;
45 $this->userFactory = $userFactory;
46 }
47
48 public function execute() {
49 $params = $this->extractRequestParams();
50
51 $emailUser = $this->emailUserFactory->newEmailUser( RequestContext::getMain()->getAuthority() );
52 $targetUser = $this->userFactory->newFromName( $params['target'] );
53
54 if ( $targetUser === null ) {
55 $this->dieWithError(
56 [ 'apierror-baduser', 'target', wfEscapeWikiText( $params['target'] ) ],
57 "baduser_target"
58 );
59 }
60
61 $status = $emailUser->validateTarget( $targetUser );
62
63 if ( !$status->isOK() ) {
64 $this->dieStatus( $status );
65 }
66
67 // Check permissions and errors
68 $error = $emailUser->canSend();
69
70 if ( !$error->isGood() ) {
71 $this->dieStatus( $error );
72 }
73
74 $retval = $emailUser->sendEmailUnsafe(
75 $targetUser,
76 $params['subject'],
77 $params['text'],
78 $params['ccme'],
79 $this->getLanguage()->getCode()
80 );
81
82 if ( !$retval instanceof Status ) {
83 // This is probably the reason
84 $retval = Status::newFatal( 'hookaborted' );
85 }
86
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 ] );
92
93 $this->getResult()->addValue( null, $this->getModuleName(), $result );
94 }
95
96 public function mustBePosted() {
97 return true;
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
103
104 public function getAllowedParams() {
105 return [
106 'target' => [
107 ParamValidator::PARAM_TYPE => 'string',
108 ParamValidator::PARAM_REQUIRED => true
109 ],
110 'subject' => [
111 ParamValidator::PARAM_TYPE => 'string',
112 ParamValidator::PARAM_REQUIRED => true
113 ],
114 'text' => [
115 ParamValidator::PARAM_TYPE => 'text',
116 ParamValidator::PARAM_REQUIRED => true
117 ],
118 'ccme' => false,
119 ];
120 }
121
122 public function needsToken() {
123 return 'csrf';
124 }
125
126 protected function getExamplesMessages() {
127 return [
128 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
129 => 'apihelp-emailuser-example-email',
130 ];
131 }
132
133 public function getHelpUrls() {
134 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
135 }
136 }
137
138 /** @deprecated class alias since 1.43 */
139 class_alias( ApiEmailUser::class, 'ApiEmailUser' );