Merge "Added result properties to action=paraminfo"
[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' => null,
143 'protections' => array(
144 ApiBase::PARAM_ISMULTI => true,
145 ApiBase::PARAM_REQUIRED => true,
146 ),
147 'expiry' => array(
148 ApiBase::PARAM_ISMULTI => true,
149 ApiBase::PARAM_ALLOW_DUPLICATES => true,
150 ApiBase::PARAM_DFLT => 'infinite',
151 ),
152 'reason' => '',
153 'cascade' => false,
154 'watch' => array(
155 ApiBase::PARAM_DFLT => false,
156 ApiBase::PARAM_DEPRECATED => true,
157 ),
158 'watchlist' => array(
159 ApiBase::PARAM_DFLT => 'preferences',
160 ApiBase::PARAM_TYPE => array(
161 'watch',
162 'unwatch',
163 'preferences',
164 'nochange'
165 ),
166 ),
167 );
168 }
169
170 public function getParamDescription() {
171 $p = $this->getModulePrefix();
172 return array(
173 'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
174 'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
175 'token' => 'A protect token previously retrieved through prop=info',
176 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
177 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
178 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.' ),
179 'reason' => 'Reason for (un)protecting (optional)',
180 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
181 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
182 'watch' => 'If set, add the page being (un)protected to your watchlist',
183 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
184 );
185 }
186
187 public function getResultProperties() {
188 return array(
189 '' => array(
190 'title' => 'string',
191 'reason' => 'string',
192 'cascade' => 'boolean'
193 )
194 );
195 }
196
197 public function getDescription() {
198 return 'Change the protection level of a page';
199 }
200
201 public function getPossibleErrors() {
202 return array_merge( parent::getPossibleErrors(),
203 $this->getTitleOrPageIdErrorMessage(),
204 array(
205 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
206 array( 'create-titleexists' ),
207 array( 'missingtitle-createonly' ),
208 array( 'protect-invalidaction', 'action' ),
209 array( 'protect-invalidlevel', 'level' ),
210 array( 'invalidexpiry', 'expiry' ),
211 array( 'pastexpiry', 'expiry' ),
212 )
213 );
214 }
215
216 public function needsToken() {
217 return true;
218 }
219
220 public function getTokenSalt() {
221 return '';
222 }
223
224 public function getExamples() {
225 return array(
226 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
227 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
228 );
229 }
230
231 public function getHelpUrls() {
232 return 'https://www.mediawiki.org/wiki/API:Protect';
233 }
234
235 public function getVersion() {
236 return __CLASS__ . ': $Id$';
237 }
238 }