Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / api / ApiProtect.php
blobd28906069fc0b066053c7c488227d9be3d2f1055
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 {
31 public function execute() {
32 global $wgContLang;
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 $user = $this->getUser();
46 $tags = $params['tags'];
48 // Check if user can add tags
49 if ( !is_null( $tags ) ) {
50 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $user );
51 if ( !$ableToTag->isOK() ) {
52 $this->dieStatus( $ableToTag );
56 $expiry = (array)$params['expiry'];
57 if ( count( $expiry ) != count( $params['protections'] ) ) {
58 if ( count( $expiry ) == 1 ) {
59 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
60 } else {
61 $this->dieUsageMsg( [
62 'toofewexpiries',
63 count( $expiry ),
64 count( $params['protections'] )
65 ] );
69 $restrictionTypes = $titleObj->getRestrictionTypes();
71 $protections = [];
72 $expiryarray = [];
73 $resultProtections = [];
74 foreach ( $params['protections'] as $i => $prot ) {
75 $p = explode( '=', $prot );
76 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
78 if ( $titleObj->exists() && $p[0] == 'create' ) {
79 $this->dieUsageMsg( 'create-titleexists' );
81 if ( !$titleObj->exists() && $p[0] != 'create' ) {
82 $this->dieUsageMsg( 'missingtitle-createonly' );
85 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
86 $this->dieUsageMsg( [ 'protect-invalidaction', $p[0] ] );
88 if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
89 $this->dieUsageMsg( [ 'protect-invalidlevel', $p[1] ] );
92 if ( wfIsInfinity( $expiry[$i] ) ) {
93 $expiryarray[$p[0]] = 'infinity';
94 } else {
95 $exp = strtotime( $expiry[$i] );
96 if ( $exp < 0 || !$exp ) {
97 $this->dieUsageMsg( [ 'invalidexpiry', $expiry[$i] ] );
100 $exp = wfTimestamp( TS_MW, $exp );
101 if ( $exp < wfTimestampNow() ) {
102 $this->dieUsageMsg( [ 'pastexpiry', $expiry[$i] ] );
104 $expiryarray[$p[0]] = $exp;
106 $resultProtections[] = [
107 $p[0] => $protections[$p[0]],
108 'expiry' => $wgContLang->formatExpiry( $expiryarray[$p[0]], TS_ISO_8601, 'infinite' ),
112 $cascade = $params['cascade'];
114 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
115 $this->setWatch( $watch, $titleObj, 'watchdefault' );
117 $status = $pageObj->doUpdateRestrictions(
118 $protections,
119 $expiryarray,
120 $cascade,
121 $params['reason'],
122 $user,
123 $tags
126 if ( !$status->isOK() ) {
127 $this->dieStatus( $status );
129 $res = [
130 'title' => $titleObj->getPrefixedText(),
131 'reason' => $params['reason']
133 if ( $cascade ) {
134 $res['cascade'] = true;
136 $res['protections'] = $resultProtections;
137 $result = $this->getResult();
138 ApiResult::setIndexedTagName( $res['protections'], 'protection' );
139 $result->addValue( null, $this->getModuleName(), $res );
142 public function mustBePosted() {
143 return true;
146 public function isWriteMode() {
147 return true;
150 public function getAllowedParams() {
151 return [
152 'title' => [
153 ApiBase::PARAM_TYPE => 'string',
155 'pageid' => [
156 ApiBase::PARAM_TYPE => 'integer',
158 'protections' => [
159 ApiBase::PARAM_ISMULTI => true,
160 ApiBase::PARAM_REQUIRED => true,
162 'expiry' => [
163 ApiBase::PARAM_ISMULTI => true,
164 ApiBase::PARAM_ALLOW_DUPLICATES => true,
165 ApiBase::PARAM_DFLT => 'infinite',
167 'reason' => '',
168 'tags' => [
169 ApiBase::PARAM_TYPE => 'tags',
170 ApiBase::PARAM_ISMULTI => true,
172 'cascade' => false,
173 'watch' => [
174 ApiBase::PARAM_DFLT => false,
175 ApiBase::PARAM_DEPRECATED => true,
177 'watchlist' => [
178 ApiBase::PARAM_DFLT => 'preferences',
179 ApiBase::PARAM_TYPE => [
180 'watch',
181 'unwatch',
182 'preferences',
183 'nochange'
189 public function needsToken() {
190 return 'csrf';
193 protected function getExamplesMessages() {
194 return [
195 'action=protect&title=Main%20Page&token=123ABC&' .
196 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
197 => 'apihelp-protect-example-protect',
198 'action=protect&title=Main%20Page&token=123ABC&' .
199 'protections=edit=all|move=all&reason=Lifting%20restrictions'
200 => 'apihelp-protect-example-unprotect',
201 'action=protect&title=Main%20Page&token=123ABC&' .
202 'protections=&reason=Lifting%20restrictions'
203 => 'apihelp-protect-example-unprotect2',
207 public function getHelpUrls() {
208 return 'https://www.mediawiki.org/wiki/API:Protect';