Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / includes / api / ApiEmailUser.php
1 <?php
2 /**
3 * Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
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 /**
24 * API Module to facilitate sending of emails to users
25 * @ingroup API
26 */
27 class ApiEmailUser extends ApiBase {
28
29 public function execute() {
30 $params = $this->extractRequestParams();
31
32 // Validate target
33 $targetUser = SpecialEmailUser::getTarget( $params['target'], $this->getUser() );
34 if ( !( $targetUser instanceof User ) ) {
35 switch ( $targetUser ) {
36 case 'notarget':
37 $this->dieWithError( 'apierror-notarget' );
38 case 'noemail':
39 $this->dieWithError( [ 'noemail', $params['target'] ] );
40 case 'nowikiemail':
41 $this->dieWithError( 'nowikiemailtext', 'nowikiemail' );
42 default:
43 $this->dieWithError( [ 'apierror-unknownerror', $targetUser ] );
44 }
45 }
46
47 // Check permissions and errors
48 $error = SpecialEmailUser::getPermissionsError(
49 $this->getUser(),
50 $params['token'],
51 $this->getConfig()
52 );
53 if ( $error ) {
54 $this->dieWithError( $error );
55 }
56
57 $data = [
58 'Target' => $targetUser->getName(),
59 'Text' => $params['text'],
60 'Subject' => $params['subject'],
61 'CCMe' => $params['ccme'],
62 ];
63 $retval = SpecialEmailUser::submit( $data, $this->getContext() );
64 if ( !$retval instanceof Status ) {
65 // This is probably the reason
66 $retval = Status::newFatal( 'hookaborted' );
67 }
68
69 $result = array_filter( [
70 'result' => $retval->isGood() ? 'Success' : ( $retval->isOK() ? 'Warnings' : 'Failure' ),
71 'warnings' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'warning' ),
72 'errors' => $this->getErrorFormatter()->arrayFromStatus( $retval, 'error' ),
73 ] );
74
75 $this->getResult()->addValue( null, $this->getModuleName(), $result );
76 }
77
78 public function mustBePosted() {
79 return true;
80 }
81
82 public function isWriteMode() {
83 return true;
84 }
85
86 public function getAllowedParams() {
87 return [
88 'target' => [
89 ApiBase::PARAM_TYPE => 'string',
90 ApiBase::PARAM_REQUIRED => true
91 ],
92 'subject' => null,
93 'text' => [
94 ApiBase::PARAM_TYPE => 'text',
95 ApiBase::PARAM_REQUIRED => true
96 ],
97 'ccme' => false,
98 ];
99 }
100
101 public function needsToken() {
102 return 'csrf';
103 }
104
105 protected function getExamplesMessages() {
106 return [
107 'action=emailuser&target=WikiSysop&text=Content&token=123ABC'
108 => 'apihelp-emailuser-example-email',
109 ];
110 }
111
112 public function getHelpUrls() {
113 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Email';
114 }
115 }