]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiEmailUser.php
MediaWiki 1.14.0
[autoinstalls/mediawiki.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                 
43                 // Check whether email is enabled
44                 if ( !EmailUserForm::userEmailEnabled() )
45                         $this->dieUsageMsg( array( 'usermaildisabled' ) );
46                 
47                 $this->getMain()->requestWriteMode();
48                 $params = $this->extractRequestParams();
49                 
50                 // Check required parameters
51                 if ( !isset( $params['target'] ) )
52                         $this->dieUsageMsg( array( 'missingparam', 'target' ) );
53                 if ( !isset( $params['text'] ) )
54                         $this->dieUsageMsg( array( 'missingparam', 'text' ) );
55                 if ( !isset( $params['token'] ) )
56                         $this->dieUsageMsg( array( 'missingparam', 'token' ) ); 
57                 
58                 // Validate target 
59                 $targetUser = EmailUserForm::validateEmailTarget( $params['target'] );
60                 if ( !( $targetUser instanceof User ) )
61                         $this->dieUsageMsg( array( $targetUser ) );
62                 
63                 // Check permissions
64                 $error = EmailUserForm::getPermissionsError( $wgUser, $params['token'] );
65                 if ( $error )
66                         $this->dieUsageMsg( array( $error ) );
67                 
68                         
69                 $form = new EmailUserForm( $targetUser, $params['text'], $params['subject'], $params['ccme'] );
70                 $retval = $form->doSubmit();
71                 if ( is_null( $retval ) )
72                         $result = array( 'result' => 'Success' );
73                 else
74                         $result = array( 'result' => 'Failure',
75                                  'message' => $retval->getMessage() );
76                 
77                 $this->getResult()->addValue( null, $this->getModuleName(), $result );
78         }
79         
80         public function mustBePosted() { return true; }
81
82         public function getAllowedParams() {
83                 return array (
84                         'target' => null,
85                         'subject' => null,
86                         'text' => null,
87                         'token' => null,
88                         'ccme' => false,
89                 );
90         }
91
92         public function getParamDescription() {
93                 return array (
94                         'target' => 'User to send email to',
95                         'subject' => 'Subject header',
96                         'text' => 'Mail body',
97                         'token' => 'A token previously acquired via prop=info',
98                         'ccme' => 'Send a copy of this mail to me',
99                 );
100         }
101
102         public function getDescription() {
103                 return array(
104                         'Email a user.'
105                 );
106         }
107
108         protected function getExamples() {
109                 return array (
110                         'api.php?action=emailuser&target=WikiSysop&text=Content'
111                 );
112         }
113
114         public function getVersion() {
115                 return __CLASS__ . ': $Id: ApiEmailUser.php 41269 2008-09-25 21:39:36Z catrope $';
116         }
117 }       
118