91fa57d55e94f11214a803f9a209819141739cb0
[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->exists())
63 $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle');
64 if(!$titleObj->userCan('protect'))
65 $this->dieUsage('You don\'t have permission to change protection levels', 'permissiondenied');
66 $articleObj = new Article($titleObj);
67
68 if(in_array($params['expiry'], array('infinite', 'indefinite', 'never')))
69 $expiry = Block::infinity();
70 else
71 {
72 $expiry = strtotime($params['expiry']);
73 if($expiry < 0 || $expiry == false)
74 $this->dieUsage('Invalid expiry time', 'invalidexpiry');
75
76 $expiry = wfTimestamp(TS_MW, $expiry);
77 if($expiry < wfTimestampNow())
78 $this->dieUsage('Expiry time is in the past', 'pastexpiry');
79 }
80
81 $protections = array();
82 foreach($params['protections'] as $prot)
83 {
84 $p = explode('=', $prot);
85 $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
86 }
87
88 $dbw = wfGetDb(DB_MASTER);
89 $dbw->begin();
90 $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiry);
91 if(!$ok)
92 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
93 $this->dieUsage('Unknown error', 'unknownerror');
94 $dbw->commit();
95 $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'], 'expiry' => $expiry);
96 if($params['cascade'])
97 $res['cascade'] = '';
98 $res['protections'] = $protections;
99 $this->getResult()->addValue(null, $this->getModuleName(), $res);
100 }
101
102 protected function getAllowedParams() {
103 return array (
104 'title' => null,
105 'token' => null,
106 'protections' => array(
107 ApiBase :: PARAM_ISMULTI => true
108 ),
109 'expiry' => 'infinite',
110 'reason' => '',
111 'cascade' => false
112 );
113 }
114
115 protected function getParamDescription() {
116 return array (
117 'title' => 'Title of the page you want to restore.',
118 'token' => 'A protect token previously retrieved through prop=info',
119 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
120 'expiry' => 'Expiry timestamp. If set to \'infinite\', \'indefinite\' or \'never\', the protection will never expire.',
121 'reason' => 'Reason for (un)protecting (optional)',
122 'cascade' => 'Enable cascading protection (i.e. protect pages included in this page)'
123 );
124 }
125
126 protected function getDescription() {
127 return array(
128 'Change the protection level of a page.'
129 );
130 }
131
132 protected function getExamples() {
133 return array (
134 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000',
135 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
136 );
137 }
138
139 public function getVersion() {
140 return __CLASS__ . ': $Id$';
141 }
142 }