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