Merge "Add CollationFa"
[lhc/web/wiklou.git] / includes / api / ApiProtect.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 1, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * @ingroup API
29 */
30 class ApiProtect extends ApiBase {
31 public function execute() {
32 global $wgContLang;
33
34 $params = $this->extractRequestParams();
35
36 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
37 $titleObj = $pageObj->getTitle();
38
39 $this->checkTitleUserPermissions( $titleObj, 'protect' );
40
41 $user = $this->getUser();
42 $tags = $params['tags'];
43
44 // Check if user can add tags
45 if ( !is_null( $tags ) ) {
46 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $user );
47 if ( !$ableToTag->isOK() ) {
48 $this->dieStatus( $ableToTag );
49 }
50 }
51
52 $expiry = (array)$params['expiry'];
53 if ( count( $expiry ) != count( $params['protections'] ) ) {
54 if ( count( $expiry ) == 1 ) {
55 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
56 } else {
57 $this->dieWithError( [
58 'apierror-toofewexpiries',
59 count( $expiry ),
60 count( $params['protections'] )
61 ] );
62 }
63 }
64
65 $restrictionTypes = $titleObj->getRestrictionTypes();
66
67 $protections = [];
68 $expiryarray = [];
69 $resultProtections = [];
70 foreach ( $params['protections'] as $i => $prot ) {
71 $p = explode( '=', $prot );
72 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
73
74 if ( $titleObj->exists() && $p[0] == 'create' ) {
75 $this->dieWithError( 'apierror-create-titleexists' );
76 }
77 if ( !$titleObj->exists() && $p[0] != 'create' ) {
78 $this->dieWithError( 'apierror-missingtitle-createonly' );
79 }
80
81 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
82 $this->dieWithError( [ 'apierror-protect-invalidaction', wfEscapeWikiText( $p[0] ) ] );
83 }
84 if ( !in_array( $p[1], $this->getConfig()->get( 'RestrictionLevels' ) ) && $p[1] != 'all' ) {
85 $this->dieWithError( [ 'apierror-protect-invalidlevel', wfEscapeWikiText( $p[1] ) ] );
86 }
87
88 if ( wfIsInfinity( $expiry[$i] ) ) {
89 $expiryarray[$p[0]] = 'infinity';
90 } else {
91 $exp = strtotime( $expiry[$i] );
92 if ( $exp < 0 || !$exp ) {
93 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
94 }
95
96 $exp = wfTimestamp( TS_MW, $exp );
97 if ( $exp < wfTimestampNow() ) {
98 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiry[$i] ) ] );
99 }
100 $expiryarray[$p[0]] = $exp;
101 }
102 $resultProtections[] = [
103 $p[0] => $protections[$p[0]],
104 'expiry' => $wgContLang->formatExpiry( $expiryarray[$p[0]], TS_ISO_8601, 'infinite' ),
105 ];
106 }
107
108 $cascade = $params['cascade'];
109
110 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
111 $this->setWatch( $watch, $titleObj, 'watchdefault' );
112
113 $status = $pageObj->doUpdateRestrictions(
114 $protections,
115 $expiryarray,
116 $cascade,
117 $params['reason'],
118 $user,
119 $tags
120 );
121
122 if ( !$status->isOK() ) {
123 $this->dieStatus( $status );
124 }
125 $res = [
126 'title' => $titleObj->getPrefixedText(),
127 'reason' => $params['reason']
128 ];
129 if ( $cascade ) {
130 $res['cascade'] = true;
131 }
132 $res['protections'] = $resultProtections;
133 $result = $this->getResult();
134 ApiResult::setIndexedTagName( $res['protections'], 'protection' );
135 $result->addValue( null, $this->getModuleName(), $res );
136 }
137
138 public function mustBePosted() {
139 return true;
140 }
141
142 public function isWriteMode() {
143 return true;
144 }
145
146 public function getAllowedParams() {
147 return [
148 'title' => [
149 ApiBase::PARAM_TYPE => 'string',
150 ],
151 'pageid' => [
152 ApiBase::PARAM_TYPE => 'integer',
153 ],
154 'protections' => [
155 ApiBase::PARAM_ISMULTI => true,
156 ApiBase::PARAM_REQUIRED => true,
157 ],
158 'expiry' => [
159 ApiBase::PARAM_ISMULTI => true,
160 ApiBase::PARAM_ALLOW_DUPLICATES => true,
161 ApiBase::PARAM_DFLT => 'infinite',
162 ],
163 'reason' => '',
164 'tags' => [
165 ApiBase::PARAM_TYPE => 'tags',
166 ApiBase::PARAM_ISMULTI => true,
167 ],
168 'cascade' => false,
169 'watch' => [
170 ApiBase::PARAM_DFLT => false,
171 ApiBase::PARAM_DEPRECATED => true,
172 ],
173 'watchlist' => [
174 ApiBase::PARAM_DFLT => 'preferences',
175 ApiBase::PARAM_TYPE => [
176 'watch',
177 'unwatch',
178 'preferences',
179 'nochange'
180 ],
181 ],
182 ];
183 }
184
185 public function needsToken() {
186 return 'csrf';
187 }
188
189 protected function getExamplesMessages() {
190 return [
191 'action=protect&title=Main%20Page&token=123ABC&' .
192 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never'
193 => 'apihelp-protect-example-protect',
194 'action=protect&title=Main%20Page&token=123ABC&' .
195 'protections=edit=all|move=all&reason=Lifting%20restrictions'
196 => 'apihelp-protect-example-unprotect',
197 'action=protect&title=Main%20Page&token=123ABC&' .
198 'protections=&reason=Lifting%20restrictions'
199 => 'apihelp-protect-example-unprotect2',
200 ];
201 }
202
203 public function getHelpUrls() {
204 return 'https://www.mediawiki.org/wiki/API:Protect';
205 }
206 }