Merge "Typo fix"
[mediawiki.git] / includes / api / ApiProtect.php
blob7830c8b4dc40b02fef9fc241123cb864190044ca
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 $this->dieStatus( $status );
108 $res = array(
109 'title' => $titleObj->getPrefixedText(),
110 'reason' => $params['reason']
112 if ( $cascade ) {
113 $res['cascade'] = '';
115 $res['protections'] = $resultProtections;
116 $result = $this->getResult();
117 $result->setIndexedTagName( $res['protections'], 'protection' );
118 $result->addValue( null, $this->getModuleName(), $res );
121 public function mustBePosted() {
122 return true;
125 public function isWriteMode() {
126 return true;
129 public function getAllowedParams() {
130 return array(
131 'title' => array(
132 ApiBase::PARAM_TYPE => 'string',
134 'pageid' => array(
135 ApiBase::PARAM_TYPE => 'integer',
137 'token' => array(
138 ApiBase::PARAM_TYPE => 'string',
139 ApiBase::PARAM_REQUIRED => true
141 'protections' => array(
142 ApiBase::PARAM_ISMULTI => true,
143 ApiBase::PARAM_REQUIRED => true,
145 'expiry' => array(
146 ApiBase::PARAM_ISMULTI => true,
147 ApiBase::PARAM_ALLOW_DUPLICATES => true,
148 ApiBase::PARAM_DFLT => 'infinite',
150 'reason' => '',
151 'cascade' => false,
152 'watch' => array(
153 ApiBase::PARAM_DFLT => false,
154 ApiBase::PARAM_DEPRECATED => true,
156 'watchlist' => array(
157 ApiBase::PARAM_DFLT => 'preferences',
158 ApiBase::PARAM_TYPE => array(
159 'watch',
160 'unwatch',
161 'preferences',
162 'nochange'
168 public function getParamDescription() {
169 $p = $this->getModulePrefix();
170 return array(
171 'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
172 'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
173 'token' => 'A protect token previously retrieved through prop=info',
174 'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
175 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
176 'Use \'infinite\', \'indefinite\' or \'never\', for a never-expiring protection.' ),
177 'reason' => 'Reason for (un)protecting',
178 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
179 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
180 'watch' => 'If set, add the page being (un)protected to your watchlist',
181 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
185 public function getResultProperties() {
186 return array(
187 '' => array(
188 'title' => 'string',
189 'reason' => 'string',
190 'cascade' => 'boolean'
195 public function getDescription() {
196 return 'Change the protection level of a page';
199 public function getPossibleErrors() {
200 return array_merge( parent::getPossibleErrors(),
201 $this->getTitleOrPageIdErrorMessage(),
202 array(
203 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
204 array( 'create-titleexists' ),
205 array( 'missingtitle-createonly' ),
206 array( 'protect-invalidaction', 'action' ),
207 array( 'protect-invalidlevel', 'level' ),
208 array( 'invalidexpiry', 'expiry' ),
209 array( 'pastexpiry', 'expiry' ),
214 public function needsToken() {
215 return true;
218 public function getTokenSalt() {
219 return '';
222 public function getExamples() {
223 return array(
224 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
225 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
229 public function getHelpUrls() {
230 return 'https://www.mediawiki.org/wiki/API:Protect';