Clarify comment on $wgRateLimits.
[lhc/web/wiklou.git] / includes / api / ApiProtect.php
1 <?php
2
3 /*
4 * Created on Sep 1, 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 * @addtogroup API
32 */
33 class ApiProtect extends ApiBase {
34
35 public function __construct($main, $action) {
36 parent :: __construct($main, $action);
37 }
38
39 public function execute() {
40 global $wgUser;
41 $this->getMain()->requestWriteMode();
42 $params = $this->extractRequestParams();
43
44 $titleObj = NULL;
45 if(!isset($params['title']))
46 $this->dieUsage('The title parameter must be set', 'notitle');
47 if(!isset($params['token']))
48 $this->dieUsage('The token parameter must be set', 'notoken');
49 if(!isset($params['protections']) || empty($params['protections']))
50 $this->dieUsage('The protections parameter must be set', 'noprotections');
51
52 if($wgUser->isBlocked())
53 $this->dieUsage('You have been blocked from editing', 'blocked');
54 if(wfReadOnly())
55 $this->dieUsage('The wiki is in read-only mode', 'readonly');
56 if(!$wgUser->matchEditToken($params['token']))
57 $this->dieUsage('Invalid token', 'badtoken');
58
59 $titleObj = Title::newFromText($params['title']);
60 if(!$titleObj)
61 $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');
62 if(!$titleObj->userCan('protect'))
63 $this->dieUsage('You don\'t have permission to change protection levels', 'permissiondenied');
64
65 if(in_array($params['expiry'], array('infinite', 'indefinite', 'never')))
66 $expiry = Block::infinity();
67 else
68 {
69 $expiry = strtotime($params['expiry']);
70 if($expiry < 0 || $expiry == false)
71 $this->dieUsage('Invalid expiry time', 'invalidexpiry');
72
73 $expiry = wfTimestamp(TS_MW, $expiry);
74 if($expiry < wfTimestampNow())
75 $this->dieUsage('Expiry time is in the past', 'pastexpiry');
76 }
77
78 $protections = array();
79 foreach($params['protections'] as $prot)
80 {
81 $p = explode('=', $prot);
82 $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
83 if($titleObj->exists() && $p[0] == 'create')
84 $this->dieUsage("Existing titles can't be protected with 'create'", 'create-titleexists');
85 if(!$titleObj->exists() && $p[0] != 'create')
86 $this->dieUsage("Missing titles can only be protected with 'create'", 'missingtitle');
87 }
88
89 $dbw = wfGetDb(DB_MASTER);
90 $dbw->begin();
91 if($titleObj->exists()) {
92 $articleObj = new Article($titleObj);
93 $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiry);
94 } else
95 $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiry);
96 if(!$ok)
97 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
98 $this->dieUsage('Unknown error', 'unknownerror');
99 $dbw->commit();
100 $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'], 'expiry' => wfTimestamp(TS_ISO_8601, $expiry));
101 if($params['cascade'])
102 $res['cascade'] = '';
103 $res['protections'] = $protections;
104 $this->getResult()->addValue(null, $this->getModuleName(), $res);
105 }
106
107 protected function getAllowedParams() {
108 return array (
109 'title' => null,
110 'token' => null,
111 'protections' => array(
112 ApiBase :: PARAM_ISMULTI => true
113 ),
114 'expiry' => 'infinite',
115 'reason' => '',
116 'cascade' => false
117 );
118 }
119
120 protected function getParamDescription() {
121 return array (
122 'title' => 'Title of the page you want to restore.',
123 'token' => 'A protect token previously retrieved through prop=info',
124 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
125 'expiry' => 'Expiry timestamp. If set to \'infinite\', \'indefinite\' or \'never\', the protection will never expire.',
126 'reason' => 'Reason for (un)protecting (optional)',
127 'cascade' => 'Enable cascading protection (i.e. protect pages included in this page)'
128 );
129 }
130
131 protected function getDescription() {
132 return array(
133 'Change the protection level of a page.'
134 );
135 }
136
137 protected function getExamples() {
138 return array (
139 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000',
140 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
141 );
142 }
143
144 public function getVersion() {
145 return __CLASS__ . ': $Id$';
146 }
147 }