Followup r70461 if PARAM_REQUIRED is set, use for missing param in getPossibleErrors...
[lhc/web/wiklou.git] / includes / api / ApiEmailUser.php
1 <?php
2
3 /**
4 * Created on June 1, 2008
5 * API for MediaWiki 1.8+
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
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * API Module to facilitate sending of emails to users
32 * @ingroup API
33 */
34 class ApiEmailUser extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 public function execute() {
41 global $wgUser;
42
43 $params = $this->extractRequestParams();
44
45 // Validate target
46 $targetUser = SpecialEmailUser::getTarget( $params['target'] );
47 if ( !( $targetUser instanceof User ) ) {
48 $this->dieUsageMsg( array( $targetUser ) );
49 }
50
51 // Check permissions and errors
52 $error = SpecialEmailUser::getPermissionsError( $wgUser, $params['token'] );
53 if ( $error ) {
54 $this->dieUsageMsg( array( $error ) );
55 }
56
57 $data = array(
58 'Target' => $targetUser->getName(),
59 'Text' => $params['text'],
60 'Subject' => $params['subject'],
61 'CCMe' => $params['ccme'],
62 );
63 $retval = SpecialEmailUser::submit( $data );
64 if ( $retval === true ) {
65 $result = array( 'result' => 'Success' );
66 } else {
67 $result = array(
68 'result' => 'Failure',
69 'message' => $retval
70 );
71 }
72
73 $this->getResult()->addValue( null, $this->getModuleName(), $result );
74 }
75
76 public function mustBePosted() {
77 return true;
78 }
79
80 public function isWriteMode() {
81 return true;
82 }
83
84 public function getAllowedParams() {
85 return array(
86 'target' => array(
87 ApiBase::PARAM_TYPE => 'string',
88 ApiBase::PARAM_REQUIRED => true
89 ),
90 'subject' => null,
91 'text' => array(
92 ApiBase::PARAM_TYPE => 'string',
93 ApiBase::PARAM_REQUIRED => true
94 ),
95 'token' => null,
96 'ccme' => false,
97 );
98 }
99
100 public function getParamDescription() {
101 return array(
102 'target' => 'User to send email to',
103 'subject' => 'Subject header',
104 'text' => 'Mail body',
105 'token' => 'A token previously acquired via prop=info',
106 'ccme' => 'Send a copy of this mail to me',
107 );
108 }
109
110 public function getDescription() {
111 return 'Email a user.';
112 }
113
114 public function getPossibleErrors() {
115 return array_merge( parent::getPossibleErrors(), array(
116 array( 'usermaildisabled' ),
117 ) );
118 }
119
120 public function getTokenSalt() {
121 return '';
122 }
123
124 protected function getExamples() {
125 return array(
126 'api.php?action=emailuser&target=WikiSysop&text=Content'
127 );
128 }
129
130 public function getVersion() {
131 return __CLASS__ . ': $Id$';
132 }
133 }