Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / includes / api / ApiProtect.php
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * @ingroup API
25 */
26 class ApiProtect extends ApiBase {
27 public function execute() {
28 $params = $this->extractRequestParams();
29
30 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
31 $titleObj = $pageObj->getTitle();
32
33 $this->checkTitleUserPermissions( $titleObj, 'protect' );
34
35 $user = $this->getUser();
36 $tags = $params['tags'];
37
38 // Check if user can add tags
39 if ( !is_null( $tags ) ) {
40 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $user );
41 if ( !$ableToTag->isOK() ) {
42 $this->dieStatus( $ableToTag );
43 }
44 }
45
46 $expiry = (array)$params['expiry'];
47 if ( count( $expiry ) != count( $params['protections'] ) ) {
48 if ( count( $expiry ) == 1 ) {
49 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
50 } else {
51 $this->dieWithError( [
52 'apierror-toofewexpiries',
53 count( $expiry ),
54 count( $params['protections'] )
55 ] );
56 }
57 }
58
59 $restrictionTypes = $titleObj->getRestrictionTypes();
60
61 $protections = [];
62 $expiryarray = [];
63 $resultProtections = [];
64 foreach ( $params['protections'] as $i => $prot ) {
65 $p = explode( '=', $prot );
66 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
67
68 if ( $titleObj->exists() && $p[0] == 'create' ) {
69 $this->dieWithError( 'apierror-create-titleexists' );
70 }
71 if ( !$titleObj->exists() && $p[0] != 'create' ) {
72 $this->dieWithError( 'apierror-missingtitle-createonly' );
73 }
74
75 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
76 $this->dieWithError( [ 'apierror-protect-invalidaction', wfEscapeWikiText( $p[0] ) ] );
77 }
78 if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
79 $this->dieWithError( [ 'apierror-protect-invalidlevel', wfEscapeWikiText( $p[1] ) ] );
80 }
81
82 if ( wfIsInfinity( $expiry[$i] ) ) {
83 $expiryarray[$p[0]] = 'infinity';
84 } else {
85 $exp = strtotime( $expiry[$i] );
86 if ( $exp < 0 || !$exp ) {
87 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
88 }
89
90 $exp = wfTimestamp( TS_MW, $exp );
91 if ( $exp < wfTimestampNow() ) {
92 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
93 }
94 $expiryarray[$p[0]] = $exp;
95 }
96 $resultProtections[] = [
97 $p[0] => $protections[$p[0]],
98 'expiry' => ApiResult::formatExpiry( $expiryarray[$p[0]], 'infinite' ),
99 ];
100 }
101
102 $cascade = $params['cascade'];
103
104 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
105 $this->setWatch( $watch, $titleObj, 'watchdefault' );
106
107 $status = $pageObj->doUpdateRestrictions(
108 $protections,
109 $expiryarray,
110 $cascade,
111 $params['reason'],
112 $user,
113 $tags
114 );
115
116 if ( !$status->isOK() ) {
117 $this->dieStatus( $status );
118 }
119 $res = [
120 'title' => $titleObj->getPrefixedText(),
121 'reason' => $params['reason']
122 ];
123 if ( $cascade ) {
124 $res['cascade'] = true;
125 }
126 $res['protections'] = $resultProtections;
127 $result = $this->getResult();
128 ApiResult::setIndexedTagName( $res['protections'], 'protection' );
129 $result->addValue( null, $this->getModuleName(), $res );
130 }
131
132 public function mustBePosted() {
133 return true;
134 }
135
136 public function isWriteMode() {
137 return true;
138 }
139
140 public function getAllowedParams() {
141 return [
142 'title' => [
143 ApiBase::PARAM_TYPE => 'string',
144 ],
145 'pageid' => [
146 ApiBase::PARAM_TYPE => 'integer',
147 ],
148 'protections' => [
149 ApiBase::PARAM_ISMULTI => true,
150 ApiBase::PARAM_REQUIRED => true,
151 ],
152 'expiry' => [
153 ApiBase::PARAM_ISMULTI => true,
154 ApiBase::PARAM_ALLOW_DUPLICATES => true,
155 ApiBase::PARAM_DFLT => 'infinite',
156 ],
157 'reason' => '',
158 'tags' => [
159 ApiBase::PARAM_TYPE => 'tags',
160 ApiBase::PARAM_ISMULTI => true,
161 ],
162 'cascade' => false,
163 'watch' => [
164 ApiBase::PARAM_DFLT => false,
165 ApiBase::PARAM_DEPRECATED => true,
166 ],
167 'watchlist' => [
168 ApiBase::PARAM_DFLT => 'preferences',
169 ApiBase::PARAM_TYPE => [
170 'watch',
171 'unwatch',
172 'preferences',
173 'nochange'
174 ],
175 ],
176 ];
177 }
178
179 public function needsToken() {
180 return 'csrf';
181 }
182
183 protected function getExamplesMessages() {
184 return [
185 'action=protect&title=Main%20Page&token=123ABC&' .
186 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
187 => 'apihelp-protect-example-protect',
188 'action=protect&title=Main%20Page&token=123ABC&' .
189 'protections=edit=all|move=all&reason=Lifting%20restrictions'
190 => 'apihelp-protect-example-unprotect',
191 'action=protect&title=Main%20Page&token=123ABC&' .
192 'protections=&reason=Lifting%20restrictions'
193 => 'apihelp-protect-example-unprotect2',
194 ];
195 }
196
197 public function getHelpUrls() {
198 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protect';
199 }
200 }