API: Make action=block return an ISO 8601 timestamp rather than a UNIX timestamp
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
1 <?php
2
3 /*
4 * Created on Sep 4, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * API module that facilitates the blocking of users. Requires API write mode
32 * to be enabled.
33 *
34 * @ingroup API
35 */
36 class ApiBlock extends ApiBase {
37
38 /**
39 * Std ctor.
40 */
41 public function __construct($main, $action) {
42 parent :: __construct($main, $action);
43 }
44
45 /**
46 * Blocks the user specified in the parameters for the given expiry, with the
47 * given reason, and with all other settings provided in the params. If the block
48 * succeeds, produces a result containing the details of the block and notice
49 * of success. If it fails, the result will specify the nature of the error.
50 */
51 public function execute() {
52 global $wgUser;
53 $this->getMain()->requestWriteMode();
54 $params = $this->extractRequestParams();
55
56 if($params['gettoken'])
57 {
58 $res['blocktoken'] = $wgUser->editToken();
59 $this->getResult()->addValue(null, $this->getModuleName(), $res);
60 return;
61 }
62
63 if(is_null($params['user']))
64 $this->dieUsageMsg(array('missingparam', 'user'));
65 if(is_null($params['token']))
66 $this->dieUsageMsg(array('missingparam', 'token'));
67 if(!$wgUser->matchEditToken($params['token']))
68 $this->dieUsageMsg(array('sessionfailure'));
69 if(!$wgUser->isAllowed('block'))
70 $this->dieUsageMsg(array('cantblock'));
71 if($params['hidename'] && !$wgUser->isAllowed('hideuser'))
72 $this->dieUsageMsg(array('canthide'));
73 if($params['noemail'] && !$wgUser->isAllowed('blockemail'))
74 $this->dieUsageMsg(array('cantblock-email'));
75 if(wfReadOnly())
76 $this->dieUsageMsg(array('readonlytext'));
77
78 $form = new IPBlockForm('');
79 $form->BlockAddress = $params['user'];
80 $form->BlockReason = (is_null($params['reason']) ? '' : $params['reason']);
81 $form->BlockReasonList = 'other';
82 $form->BlockExpiry = ($params['expiry'] == 'never' ? 'infinite' : $params['expiry']);
83 $form->BlockOther = '';
84 $form->BlockAnonOnly = $params['anononly'];
85 $form->BlockCreateAccount = $params['nocreate'];
86 $form->BlockEnableAutoBlock = $params['autoblock'];
87 $form->BlockEmail = $params['noemail'];
88 $form->BlockHideName = $params['hidename'];
89
90 $userID = $expiry = null;
91 $retval = $form->doBlock($userID, $expiry);
92 if(!empty($retval))
93 // We don't care about multiple errors, just report one of them
94 $this->dieUsageMsg($retval);
95
96 $res['user'] = $params['user'];
97 $res['userID'] = $userID;
98 $res['expiry'] = ($expiry == Block::infinity() ? 'infinite' : wfTimestamp(TS_ISO_8601, $expiry));
99 $res['reason'] = $params['reason'];
100 if($params['anononly'])
101 $res['anononly'] = '';
102 if($params['nocreate'])
103 $res['nocreate'] = '';
104 if($params['autoblock'])
105 $res['autoblock'] = '';
106 if($params['noemail'])
107 $res['noemail'] = '';
108 if($params['hidename'])
109 $res['hidename'] = '';
110
111 $this->getResult()->addValue(null, $this->getModuleName(), $res);
112 }
113
114 public function mustBePosted() { return true; }
115
116 public function getAllowedParams() {
117 return array (
118 'user' => null,
119 'token' => null,
120 'gettoken' => false,
121 'expiry' => 'never',
122 'reason' => null,
123 'anononly' => false,
124 'nocreate' => false,
125 'autoblock' => false,
126 'noemail' => false,
127 'hidename' => false,
128 );
129 }
130
131 public function getParamDescription() {
132 return array (
133 'user' => 'Username, IP address or IP range you want to block',
134 'token' => 'A block token previously obtained through the gettoken parameter',
135 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
136 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
137 'reason' => 'Reason for block (optional)',
138 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
139 'nocreate' => 'Prevent account creation',
140 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
141 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
142 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)'
143 );
144 }
145
146 public function getDescription() {
147 return array(
148 'Block a user.'
149 );
150 }
151
152 protected function getExamples() {
153 return array (
154 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
155 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'
156 );
157 }
158
159 public function getVersion() {
160 return __CLASS__ . ': $Id$';
161 }
162 }