Revert r44271 "(bug 12998) Weaken DISPLAYTITLE restictions (patch by 'rememberthedot...
[mediawiki.git] / includes / api / ApiProtect.php
blob0163e5ab6ac5683a4023d0fd7740c903cc9c8d93
1 <?php
3 /*
4 * Created on Sep 1, 2007
5 * API for MediaWiki 1.8+
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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.
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.
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
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
30 /**
31 * @ingroup API
33 class ApiProtect extends ApiBase {
35 public function __construct($main, $action) {
36 parent :: __construct($main, $action);
39 public function execute() {
40 global $wgUser, $wgRestrictionTypes, $wgRestrictionLevels;
41 $this->getMain()->requestWriteMode();
42 $params = $this->extractRequestParams();
44 $titleObj = NULL;
45 if(!isset($params['title']))
46 $this->dieUsageMsg(array('missingparam', 'title'));
47 if(!isset($params['token']))
48 $this->dieUsageMsg(array('missingparam', 'token'));
49 if(empty($params['protections']))
50 $this->dieUsageMsg(array('missingparam', 'protections'));
52 if(!$wgUser->matchEditToken($params['token']))
53 $this->dieUsageMsg(array('sessionfailure'));
55 $titleObj = Title::newFromText($params['title']);
56 if(!$titleObj)
57 $this->dieUsageMsg(array('invalidtitle', $params['title']));
59 $errors = $titleObj->getUserPermissionsErrors('protect', $wgUser);
60 if($errors)
61 // We don't care about multiple errors, just report one of them
62 $this->dieUsageMsg(current($errors));
64 $expiry = (array)$params['expiry'];
65 if(count($expiry) != count($params['protections']))
67 if(count($expiry) == 1)
68 $expiry = array_fill(0, count($params['protections']), $expiry[0]);
69 else
70 $this->dieUsageMsg(array('toofewexpiries', count($expiry), count($params['protections'])));
73 $protections = array();
74 $expiryarray = array();
75 $resultProtections = array();
76 foreach($params['protections'] as $i => $prot)
78 $p = explode('=', $prot);
79 $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);
80 if($titleObj->exists() && $p[0] == 'create')
81 $this->dieUsageMsg(array('create-titleexists'));
82 if(!$titleObj->exists() && $p[0] != 'create')
83 $this->dieUsageMsg(array('missingtitles-createonly'));
84 if(!in_array($p[0], $wgRestrictionTypes) && $p[0] != 'create')
85 $this->dieUsageMsg(array('protect-invalidaction', $p[0]));
86 if(!in_array($p[1], $wgRestrictionLevels) && $p[1] != 'all')
87 $this->dieUsageMsg(array('protect-invalidlevel', $p[1]));
89 if(in_array($expiry[$i], array('infinite', 'indefinite', 'never')))
90 $expiryarray[$p[0]] = Block::infinity();
91 else
93 $exp = strtotime($expiry[$i]);
94 if($exp < 0 || $exp == false)
95 $this->dieUsageMsg(array('invalidexpiry', $expiry[$i]));
97 $exp = wfTimestamp(TS_MW, $exp);
98 if($exp < wfTimestampNow())
99 $this->dieUsageMsg(array('pastexpiry', $expiry[$i]));
100 $expiryarray[$p[0]] = $exp;
102 $resultProtections[] = array($p[0] => $protections[$p[0]],
103 'expiry' => ($expiryarray[$p[0]] == Block::infinity() ?
104 'infinite' :
105 wfTimestamp(TS_ISO_8601, $expiryarray[$p[0]])));
108 $cascade = $params['cascade'];
109 if($titleObj->exists()) {
110 $articleObj = new Article($titleObj);
111 $ok = $articleObj->updateRestrictions($protections, $params['reason'], $cascade, $expiryarray);
112 } else
113 $ok = $titleObj->updateTitleProtection($protections['create'], $params['reason'], $expiryarray['create']);
114 if(!$ok)
115 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
116 // Just throw an unknown error in this case, as it's very likely to be a race condition
117 $this->dieUsageMsg(array());
118 $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason']);
119 if($cascade)
120 $res['cascade'] = '';
121 $res['protections'] = $resultProtections;
122 $this->getResult()->setIndexedTagName($res['protections'], 'protection');
123 $this->getResult()->addValue(null, $this->getModuleName(), $res);
126 public function mustBePosted() { return true; }
128 public function getAllowedParams() {
129 return array (
130 'title' => null,
131 'token' => null,
132 'protections' => array(
133 ApiBase :: PARAM_ISMULTI => true
135 'expiry' => array(
136 ApiBase :: PARAM_ISMULTI => true,
137 ApiBase :: PARAM_ALLOW_DUPLICATES => true,
138 ApiBase :: PARAM_DFLT => 'infinite',
140 'reason' => '',
141 'cascade' => false
145 public function getParamDescription() {
146 return array (
147 'title' => 'Title of the page you want to (un)protect.',
148 'token' => 'A protect token previously retrieved through prop=info',
149 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
150 'expiry' => array('Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
151 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.'),
152 'reason' => 'Reason for (un)protecting (optional)',
153 'cascade' => array('Enable cascading protection (i.e. protect pages included in this page)',
154 'Ignored if not all protection levels are \'sysop\' or \'protect\''),
158 public function getDescription() {
159 return array(
160 'Change the protection level of a page.'
164 protected function getExamples() {
165 return array (
166 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000|never',
167 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
171 public function getVersion() {
172 return __CLASS__ . ': $Id$';