API: Add action=emailuser
[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 /**
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 $this->getMain()->requestWriteMode();
43 $params = $this->extractRequestParams();
44
45 // Check required parameters
46 if ( !isset( $params['target'] ) )
47 $this->dieUsageMsg( array( 'missingparam', 'target' ) );
48 if ( !isset( $params['text'] ) )
49 $this->dieUsageMsg( array( 'missingparam', 'text' ) );
50 if ( !isset( $params['token'] ) )
51 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
52
53 // Match edit token
54 if( !$wgUser->matchEditToken( $params['token'] ) )
55 $this->dieUsageMsg( array( 'sessionfailure' ) );
56
57 // Check permissions
58 $errors = EmailUserForm::getPermissionsError( $params['target'] );
59 if ( $errors )
60 $this->dieUsageMsg( $errors[0] );
61
62 // Rate limiter
63 if( $wgUser->pingLimiter( 'emailuser' ) )
64 $this->dieUsageMsg( 'actionthrottledtext' );
65
66 $form = EmailUserForm::newFromURL( $params['target'],
67 $params['text'], $params['subject'], $params['ccme'] );
68 $retval = $form->doSubmit();
69 if ( is_null( $retval ) )
70 $result = array();
71 else
72 $result = array( 'result' => 'Failure',
73 'message' => $retval->getMessage() );
74
75 $this->getResult()->addValue( null, $this->getModuleName(), $result );
76 }
77
78 public function mustBePosted() { return true; }
79
80 public function getAllowedParams() {
81 return array (
82 'target' => null,
83 'subject' => null,
84 'text' => null,
85 'token' => null,
86 'ccme' => false,
87 );
88 }
89
90 public function getParamDescription() {
91 return array (
92 'target' => 'User to send email to',
93 'subject' => 'Subject header',
94 'text' => 'Mail body',
95 // FIXME: How to properly get a token?
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 'Emails 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