Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / api / ApiProtect.php
blob503c692060f6c1d699478123d781c3e7e8905c01
1 <?php
2 /**
5 * Created on Sep 1, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * @ingroup API
30 class ApiProtect extends ApiBase {
32 public function execute() {
33 global $wgRestrictionLevels;
34 $params = $this->extractRequestParams();
36 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
37 $titleObj = $pageObj->getTitle();
39 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
40 if ( $errors ) {
41 // We don't care about multiple errors, just report one of them
42 $this->dieUsageMsg( reset( $errors ) );
45 $expiry = (array)$params['expiry'];
46 if ( count( $expiry ) != count( $params['protections'] ) ) {
47 if ( count( $expiry ) == 1 ) {
48 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
49 } else {
50 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
54 $restrictionTypes = $titleObj->getRestrictionTypes();
55 $db = $this->getDB();
57 $protections = array();
58 $expiryarray = array();
59 $resultProtections = array();
60 foreach ( $params['protections'] as $i => $prot ) {
61 $p = explode( '=', $prot );
62 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
64 if ( $titleObj->exists() && $p[0] == 'create' ) {
65 $this->dieUsageMsg( 'create-titleexists' );
67 if ( !$titleObj->exists() && $p[0] != 'create' ) {
68 $this->dieUsageMsg( 'missingtitle-createonly' );
71 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
72 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
74 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
75 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
78 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
79 $expiryarray[$p[0]] = $db->getInfinity();
80 } else {
81 $exp = strtotime( $expiry[$i] );
82 if ( $exp < 0 || !$exp ) {
83 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
86 $exp = wfTimestamp( TS_MW, $exp );
87 if ( $exp < wfTimestampNow() ) {
88 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
90 $expiryarray[$p[0]] = $exp;
92 $resultProtections[] = array( $p[0] => $protections[$p[0]],
93 'expiry' => ( $expiryarray[$p[0]] == $db->getInfinity() ?
94 'infinite' :
95 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
98 $cascade = $params['cascade'];
100 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
101 $this->setWatch( $watch, $titleObj );
103 $status = $pageObj->doUpdateRestrictions( $protections, $expiryarray, $cascade, $params['reason'], $this->getUser() );
105 if ( !$status->isOK() ) {
106 $errors = $status->getErrorsArray();
107 $this->dieUsageMsg( $errors[0] );
109 $res = array(
110 'title' => $titleObj->getPrefixedText(),
111 'reason' => $params['reason']
113 if ( $cascade ) {
114 $res['cascade'] = '';
116 $res['protections'] = $resultProtections;
117 $result = $this->getResult();
118 $result->setIndexedTagName( $res['protections'], 'protection' );
119 $result->addValue( null, $this->getModuleName(), $res );
122 public function mustBePosted() {
123 return true;
126 public function isWriteMode() {
127 return true;
130 public function getAllowedParams() {
131 return array(
132 'title' => array(
133 ApiBase::PARAM_TYPE => 'string',
135 'pageid' => array(
136 ApiBase::PARAM_TYPE => 'integer',
138 'token' => array(
139 ApiBase::PARAM_TYPE => 'string',
140 ApiBase::PARAM_REQUIRED => true
142 'protections' => array(
143 ApiBase::PARAM_ISMULTI => true,
144 ApiBase::PARAM_REQUIRED => true,
146 'expiry' => array(
147 ApiBase::PARAM_ISMULTI => true,
148 ApiBase::PARAM_ALLOW_DUPLICATES => true,
149 ApiBase::PARAM_DFLT => 'infinite',
151 'reason' => '',
152 'cascade' => false,
153 'watch' => array(
154 ApiBase::PARAM_DFLT => false,
155 ApiBase::PARAM_DEPRECATED => true,
157 'watchlist' => array(
158 ApiBase::PARAM_DFLT => 'preferences',
159 ApiBase::PARAM_TYPE => array(
160 'watch',
161 'unwatch',
162 'preferences',
163 'nochange'
169 public function getParamDescription() {
170 $p = $this->getModulePrefix();
171 return array(
172 'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
173 'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
174 'token' => 'A protect token previously retrieved through prop=info',
175 'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
176 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
177 'Use \'infinite\', \'indefinite\' or \'never\', for a never-expiring protection.' ),
178 'reason' => 'Reason for (un)protecting',
179 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
180 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
181 'watch' => 'If set, add the page being (un)protected to your watchlist',
182 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
186 public function getResultProperties() {
187 return array(
188 '' => array(
189 'title' => 'string',
190 'reason' => 'string',
191 'cascade' => 'boolean'
196 public function getDescription() {
197 return 'Change the protection level of a page';
200 public function getPossibleErrors() {
201 return array_merge( parent::getPossibleErrors(),
202 $this->getTitleOrPageIdErrorMessage(),
203 array(
204 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
205 array( 'create-titleexists' ),
206 array( 'missingtitle-createonly' ),
207 array( 'protect-invalidaction', 'action' ),
208 array( 'protect-invalidlevel', 'level' ),
209 array( 'invalidexpiry', 'expiry' ),
210 array( 'pastexpiry', 'expiry' ),
215 public function needsToken() {
216 return true;
219 public function getTokenSalt() {
220 return '';
223 public function getExamples() {
224 return array(
225 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
226 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/API:Protect';