Add user rights 'viewmywatchlist', 'editmywatchlist'
[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 execute() {
34 $params = $this->extractRequestParams();
35
36 // Validate target
37 $targetUser = SpecialEmailUser::getTarget( $params['target'] );
38 if ( !( $targetUser instanceof User ) ) {
39 $this->dieUsageMsg( array( $targetUser ) );
40 }
41
42 // Check permissions and errors
43 $error = SpecialEmailUser::getPermissionsError( $this->getUser(), $params['token'] );
44 if ( $error ) {
45 $this->dieUsageMsg( array( $error ) );
46 }
47
48 $data = array(
49 'Target' => $targetUser->getName(),
50 'Text' => $params['text'],
51 'Subject' => $params['subject'],
52 'CCMe' => $params['ccme'],
53 );
54 $retval = SpecialEmailUser::submit( $data, $this->getContext() );
55
56 if ( $retval instanceof Status ) {
57 // SpecialEmailUser sometimes returns a status
58 // sometimes it doesn't.
59 if ( $retval->isGood() ) {
60 $retval = true;
61 } else {
62 $retval = $retval->getErrorsArray();
63 }
64 }
65
66 if ( $retval === true ) {
67 $result = array( 'result' => 'Success' );
68 } else {
69 $result = array(
70 'result' => 'Failure',
71 'message' => $retval
72 );
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 array(
88 'target' => array(
89 ApiBase::PARAM_TYPE => 'string',
90 ApiBase::PARAM_REQUIRED => true
91 ),
92 'subject' => null,
93 'text' => array(
94 ApiBase::PARAM_TYPE => 'string',
95 ApiBase::PARAM_REQUIRED => true
96 ),
97 'token' => array(
98 ApiBase::PARAM_TYPE => 'string',
99 ApiBase::PARAM_REQUIRED => true
100 ),
101 'ccme' => false,
102 );
103 }
104
105 public function getParamDescription() {
106 return array(
107 'target' => 'User to send email to',
108 'subject' => 'Subject header',
109 'text' => 'Mail body',
110 'token' => 'A token previously acquired via prop=info',
111 'ccme' => 'Send a copy of this mail to me',
112 );
113 }
114
115 public function getResultProperties() {
116 return array(
117 '' => array(
118 'result' => array(
119 ApiBase::PROP_TYPE => array(
120 'Success',
121 'Failure'
122 ),
123 ),
124 'message' => array(
125 ApiBase::PROP_TYPE => 'string',
126 ApiBase::PROP_NULLABLE => true
127 )
128 )
129 );
130 }
131
132 public function getDescription() {
133 return 'Email a user.';
134 }
135
136 public function getPossibleErrors() {
137 return array_merge( parent::getPossibleErrors(), array(
138 array( 'usermaildisabled' ),
139 ) );
140 }
141
142 public function needsToken() {
143 return true;
144 }
145
146 public function getTokenSalt() {
147 return '';
148 }
149
150 public function getExamples() {
151 return array(
152 'api.php?action=emailuser&target=WikiSysop&text=Content' => 'Send an email to the User "WikiSysop" with the text "Content"',
153 );
154 }
155
156 public function getHelpUrls() {
157 return 'https://www.mediawiki.org/wiki/API:Email';
158 }
159 }