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