Merge "Updated result properties in paraminfo API"
[lhc/web/wiklou.git] / includes / api / ApiEmailUser.php
1 <?php
2 /**
3 *
4 *
5 * Created on June 1, 2008
6 *
7 * Copyright © 2008 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * API Module to facilitate sending of emails to users
29 * @ingroup API
30 */
31 class ApiEmailUser extends ApiBase {
32
33 public function __construct( $main, $action ) {
34 parent::__construct( $main, $action );
35 }
36
37 public function execute() {
38 $params = $this->extractRequestParams();
39
40 // Validate target
41 $targetUser = SpecialEmailUser::getTarget( $params['target'] );
42 if ( !( $targetUser instanceof User ) ) {
43 $this->dieUsageMsg( array( $targetUser ) );
44 }
45
46 // Check permissions and errors
47 $error = SpecialEmailUser::getPermissionsError( $this->getUser(), $params['token'] );
48 if ( $error ) {
49 $this->dieUsageMsg( array( $error ) );
50 }
51
52 $data = array(
53 'Target' => $targetUser->getName(),
54 'Text' => $params['text'],
55 'Subject' => $params['subject'],
56 'CCMe' => $params['ccme'],
57 );
58 $retval = SpecialEmailUser::submit( $data, $this->getContext() );
59
60 if ( $retval instanceof Status ) {
61 // SpecialEmailUser sometimes returns a status
62 // sometimes it doesn't.
63 if ( $retval->isGood() ) {
64 $retval = true;
65 } else {
66 $retval = $retval->getErrorsArray();
67 }
68 }
69
70 if ( $retval === true ) {
71 $result = array( 'result' => 'Success' );
72 } else {
73 $result = array(
74 'result' => 'Failure',
75 'message' => $retval
76 );
77 }
78
79 $this->getResult()->addValue( null, $this->getModuleName(), $result );
80 }
81
82 public function mustBePosted() {
83 return true;
84 }
85
86 public function isWriteMode() {
87 return true;
88 }
89
90 public function getAllowedParams() {
91 return array(
92 'target' => array(
93 ApiBase::PARAM_TYPE => 'string',
94 ApiBase::PARAM_REQUIRED => true
95 ),
96 'subject' => null,
97 'text' => array(
98 ApiBase::PARAM_TYPE => 'string',
99 ApiBase::PARAM_REQUIRED => true
100 ),
101 'token' => array(
102 ApiBase::PARAM_TYPE => 'string',
103 ApiBase::PARAM_REQUIRED => true
104 ),
105 'ccme' => false,
106 );
107 }
108
109 public function getParamDescription() {
110 return array(
111 'target' => 'User to send email to',
112 'subject' => 'Subject header',
113 'text' => 'Mail body',
114 'token' => 'A token previously acquired via prop=info',
115 'ccme' => 'Send a copy of this mail to me',
116 );
117 }
118
119 public function getResultProperties() {
120 return array(
121 '' => array(
122 'result' => array(
123 ApiBase::PROP_TYPE => array(
124 'Success',
125 'Failure'
126 ),
127 ),
128 'message' => array(
129 ApiBase::PROP_TYPE => 'string',
130 ApiBase::PROP_NULLABLE => true
131 )
132 )
133 );
134 }
135
136 public function getDescription() {
137 return 'Email a user.';
138 }
139
140 public function getPossibleErrors() {
141 return array_merge( parent::getPossibleErrors(), array(
142 array( 'usermaildisabled' ),
143 ) );
144 }
145
146 public function needsToken() {
147 return true;
148 }
149
150 public function getTokenSalt() {
151 return '';
152 }
153
154 public function getExamples() {
155 return array(
156 'api.php?action=emailuser&target=WikiSysop&text=Content' => 'Send an email to the User "WikiSysop" with the text "Content"',
157 );
158 }
159
160 public function getHelpUrls() {
161 return 'https://www.mediawiki.org/wiki/API:E-mail';
162 }
163
164 public function getVersion() {
165 return __CLASS__ . ': $Id$';
166 }
167 }