Normalise comment usage (# --> //)
[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 (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 * @ingroup API
32 */
33 class ApiEmailUser extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent :: __construct( $main, $action );
37 }
38
39 public function execute() {
40 global $wgUser;
41 // Check whether email is enabled
42 if ( !EmailUserForm::userEmailEnabled() )
43 $this->dieUsageMsg( array( 'usermaildisabled' ) );
44
45 $params = $this->extractRequestParams();
46 // Check required parameters
47 if ( !isset( $params['target'] ) )
48 $this->dieUsageMsg( array( 'missingparam', 'target' ) );
49 if ( !isset( $params['text'] ) )
50 $this->dieUsageMsg( array( 'missingparam', 'text' ) );
51 if ( !isset( $params['token'] ) )
52 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
53
54 // Validate target
55 $targetUser = EmailUserForm::validateEmailTarget( $params['target'] );
56 if ( !( $targetUser instanceof User ) )
57 $this->dieUsageMsg( array( $targetUser ) );
58
59 // Check permissions
60 $error = EmailUserForm::getPermissionsError( $wgUser, $params['token'] );
61 if ( $error )
62 $this->dieUsageMsg( array( $error ) );
63
64 $form = new EmailUserForm( $targetUser, $params['text'], $params['subject'], $params['ccme'] );
65 $retval = $form->doSubmit();
66 if ( is_null( $retval ) )
67 $result = array( 'result' => 'Success' );
68 else
69 $result = array( 'result' => 'Failure',
70 'message' => $retval->getMessage() );
71
72 $this->getResult()->addValue( null, $this->getModuleName(), $result );
73 }
74
75 public function mustBePosted() { return true; }
76
77 public function isWriteMode() {
78 return true;
79 }
80
81 public function getAllowedParams() {
82 return array (
83 'target' => null,
84 'subject' => null,
85 'text' => null,
86 'token' => null,
87 'ccme' => false,
88 );
89 }
90
91 public function getParamDescription() {
92 return array (
93 'target' => 'User to send email to',
94 'subject' => 'Subject header',
95 'text' => 'Mail body',
96 'token' => 'A token previously acquired via prop=info',
97 'ccme' => 'Send a copy of this mail to me',
98 );
99 }
100
101 public function getDescription() {
102 return array(
103 'Email a user.'
104 );
105 }
106
107 protected function getExamples() {
108 return array (
109 'api.php?action=emailuser&target=WikiSysop&text=Content'
110 );
111 }
112
113 public function getVersion() {
114 return __CLASS__ . ': $Id$';
115 }
116 }
117