Merge "Remove m prefixes from private variables"
[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 $wgRestrictionLevels;
33 $params = $this->extractRequestParams();
34
35 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
36 $titleObj = $pageObj->getTitle();
37
38 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
39 if ( $errors ) {
40 // We don't care about multiple errors, just report one of them
41 $this->dieUsageMsg( reset( $errors ) );
42 }
43
44 $expiry = (array)$params['expiry'];
45 if ( count( $expiry ) != count( $params['protections'] ) ) {
46 if ( count( $expiry ) == 1 ) {
47 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
48 } else {
49 $this->dieUsageMsg( array(
50 'toofewexpiries',
51 count( $expiry ),
52 count( $params['protections'] )
53 ) );
54 }
55 }
56
57 $restrictionTypes = $titleObj->getRestrictionTypes();
58 $db = $this->getDB();
59
60 $protections = array();
61 $expiryarray = array();
62 $resultProtections = array();
63 foreach ( $params['protections'] as $i => $prot ) {
64 $p = explode( '=', $prot );
65 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
66
67 if ( $titleObj->exists() && $p[0] == 'create' ) {
68 $this->dieUsageMsg( 'create-titleexists' );
69 }
70 if ( !$titleObj->exists() && $p[0] != 'create' ) {
71 $this->dieUsageMsg( 'missingtitle-createonly' );
72 }
73
74 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
75 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
76 }
77 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
78 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
79 }
80
81 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
82 $expiryarray[$p[0]] = $db->getInfinity();
83 } else {
84 $exp = strtotime( $expiry[$i] );
85 if ( $exp < 0 || !$exp ) {
86 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
87 }
88
89 $exp = wfTimestamp( TS_MW, $exp );
90 if ( $exp < wfTimestampNow() ) {
91 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
92 }
93 $expiryarray[$p[0]] = $exp;
94 }
95 $resultProtections[] = array( $p[0] => $protections[$p[0]],
96 'expiry' => ( $expiryarray[$p[0]] == $db->getInfinity() ?
97 'infinite' :
98 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
99 }
100
101 $cascade = $params['cascade'];
102
103 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
104 $this->setWatch( $watch, $titleObj, 'watchdefault' );
105
106 $status = $pageObj->doUpdateRestrictions(
107 $protections,
108 $expiryarray,
109 $cascade,
110 $params['reason'],
111 $this->getUser()
112 );
113
114 if ( !$status->isOK() ) {
115 $this->dieStatus( $status );
116 }
117 $res = array(
118 'title' => $titleObj->getPrefixedText(),
119 'reason' => $params['reason']
120 );
121 if ( $cascade ) {
122 $res['cascade'] = '';
123 }
124 $res['protections'] = $resultProtections;
125 $result = $this->getResult();
126 $result->setIndexedTagName( $res['protections'], 'protection' );
127 $result->addValue( null, $this->getModuleName(), $res );
128 }
129
130 public function mustBePosted() {
131 return true;
132 }
133
134 public function isWriteMode() {
135 return true;
136 }
137
138 public function getAllowedParams() {
139 return array(
140 'title' => array(
141 ApiBase::PARAM_TYPE => 'string',
142 ),
143 'pageid' => array(
144 ApiBase::PARAM_TYPE => 'integer',
145 ),
146 'token' => array(
147 ApiBase::PARAM_TYPE => 'string',
148 ApiBase::PARAM_REQUIRED => true
149 ),
150 'protections' => array(
151 ApiBase::PARAM_ISMULTI => true,
152 ApiBase::PARAM_REQUIRED => true,
153 ),
154 'expiry' => array(
155 ApiBase::PARAM_ISMULTI => true,
156 ApiBase::PARAM_ALLOW_DUPLICATES => true,
157 ApiBase::PARAM_DFLT => 'infinite',
158 ),
159 'reason' => '',
160 'cascade' => false,
161 'watch' => array(
162 ApiBase::PARAM_DFLT => false,
163 ApiBase::PARAM_DEPRECATED => true,
164 ),
165 'watchlist' => array(
166 ApiBase::PARAM_DFLT => 'preferences',
167 ApiBase::PARAM_TYPE => array(
168 'watch',
169 'unwatch',
170 'preferences',
171 'nochange'
172 ),
173 ),
174 );
175 }
176
177 public function getParamDescription() {
178 $p = $this->getModulePrefix();
179
180 return array(
181 'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
182 'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
183 'token' => 'A protect token previously retrieved through prop=info',
184 'protections' => 'List of protection levels, formatted action=group (e.g. edit=sysop)',
185 'expiry' => array(
186 'Expiry timestamps. If only one timestamp is ' .
187 'set, it\'ll be used for all protections.',
188 'Use \'infinite\', \'indefinite\' or \'never\', for a never-expiring protection.'
189 ),
190 'reason' => 'Reason for (un)protecting',
191 'cascade' => array(
192 'Enable cascading protection (i.e. protect pages included in this page)',
193 'Ignored if not all protection levels are \'sysop\' or \'protect\''
194 ),
195 'watch' => 'If set, add the page being (un)protected to your watchlist',
196 'watchlist' => 'Unconditionally add or remove the page from your ' .
197 'watchlist, use preferences or do not change watch',
198 );
199 }
200
201 public function getResultProperties() {
202 return array(
203 '' => array(
204 'title' => 'string',
205 'reason' => 'string',
206 'cascade' => 'boolean'
207 )
208 );
209 }
210
211 public function getDescription() {
212 return 'Change the protection level of a page';
213 }
214
215 public function getPossibleErrors() {
216 return array_merge( parent::getPossibleErrors(),
217 $this->getTitleOrPageIdErrorMessage(),
218 array(
219 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
220 array( 'create-titleexists' ),
221 array( 'missingtitle-createonly' ),
222 array( 'protect-invalidaction', 'action' ),
223 array( 'protect-invalidlevel', 'level' ),
224 array( 'invalidexpiry', 'expiry' ),
225 array( 'pastexpiry', 'expiry' ),
226 )
227 );
228 }
229
230 public function needsToken() {
231 return true;
232 }
233
234 public function getTokenSalt() {
235 return '';
236 }
237
238 public function getExamples() {
239 return array(
240 'api.php?action=protect&title=Main%20Page&token=123ABC&' .
241 'protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
242 'api.php?action=protect&title=Main%20Page&token=123ABC&' .
243 'protections=edit=all|move=all&reason=Lifting%20restrictions'
244 );
245 }
246
247 public function getHelpUrls() {
248 return 'https://www.mediawiki.org/wiki/API:Protect';
249 }
250 }