Add some more spacing due to long parameter names
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * @ingroup API
34 */
35 class ApiProtect extends ApiBase {
36
37 public function __construct( $main, $action ) {
38 parent::__construct( $main, $action );
39 }
40
41 public function execute() {
42 global $wgUser, $wgRestrictionLevels;
43 $params = $this->extractRequestParams();
44
45 $titleObj = Title::newFromText( $params['title'] );
46 if ( !$titleObj ) {
47 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
48 }
49
50 $errors = $titleObj->getUserPermissionsErrors( 'protect', $wgUser );
51 if ( $errors ) {
52 // We don't care about multiple errors, just report one of them
53 $this->dieUsageMsg( reset( $errors ) );
54 }
55
56 $expiry = (array)$params['expiry'];
57 if ( count( $expiry ) != count( $params['protections'] ) ) {
58 if ( count( $expiry ) == 1 ) {
59 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
60 } else {
61 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
62 }
63 }
64
65 $restrictionTypes = $titleObj->getRestrictionTypes();
66 $dbr = wfGetDB( DB_SLAVE );
67
68 $protections = array();
69 $expiryarray = array();
70 $resultProtections = array();
71 foreach ( $params['protections'] as $i => $prot ) {
72 $p = explode( '=', $prot );
73 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
74
75 if ( $titleObj->exists() && $p[0] == 'create' ) {
76 $this->dieUsageMsg( array( 'create-titleexists' ) );
77 }
78 if ( !$titleObj->exists() && $p[0] != 'create' ) {
79 $this->dieUsageMsg( array( 'missingtitle-createonly' ) );
80 }
81
82 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
83 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
84 }
85 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
86 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
87 }
88
89 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
90 $expiryarray[$p[0]] = $dbr->getInfinity();
91 } else {
92 $exp = strtotime( $expiry[$i] );
93 if ( $exp < 0 || !$exp ) {
94 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
95 }
96
97 $exp = wfTimestamp( TS_MW, $exp );
98 if ( $exp < wfTimestampNow() ) {
99 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
100 }
101 $expiryarray[$p[0]] = $exp;
102 }
103 $resultProtections[] = array( $p[0] => $protections[$p[0]],
104 'expiry' => ( $expiryarray[$p[0]] == $dbr->getInfinity() ?
105 'infinite' :
106 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
107 }
108
109 $cascade = $params['cascade'];
110 $articleObj = new Article( $titleObj );
111
112 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
113 $this->setWatch( $watch, $titleObj );
114
115 if ( $titleObj->exists() ) {
116 $ok = $articleObj->updateRestrictions( $protections, $params['reason'], $cascade, $expiryarray );
117 } else {
118 $ok = $titleObj->updateTitleProtection( $protections['create'], $params['reason'], $expiryarray['create'] );
119 }
120 if ( !$ok ) {
121 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
122 // Just throw an unknown error in this case, as it's very likely to be a race condition
123 $this->dieUsageMsg( array() );
124 }
125 $res = array(
126 'title' => $titleObj->getPrefixedText(),
127 'reason' => $params['reason']
128 );
129 if ( $cascade ) {
130 $res['cascade'] = '';
131 }
132 $res['protections'] = $resultProtections;
133 $this->getResult()->setIndexedTagName( $res['protections'], 'protection' );
134 $this->getResult()->addValue( null, $this->getModuleName(), $res );
135 }
136
137 public function mustBePosted() {
138 return true;
139 }
140
141 public function isWriteMode() {
142 return true;
143 }
144
145 public function getAllowedParams() {
146 return array(
147 'title' => array(
148 ApiBase::PARAM_TYPE => 'string',
149 ApiBase::PARAM_REQUIRED => true
150 ),
151 'token' => null,
152 'protections' => array(
153 ApiBase::PARAM_ISMULTI => true,
154 ApiBase::PARAM_REQUIRED => true,
155 ),
156 'expiry' => array(
157 ApiBase::PARAM_ISMULTI => true,
158 ApiBase::PARAM_ALLOW_DUPLICATES => true,
159 ApiBase::PARAM_DFLT => 'infinite',
160 ),
161 'reason' => '',
162 'cascade' => false,
163 'watch' => array(
164 ApiBase::PARAM_DFLT => false,
165 ApiBase::PARAM_DEPRECATED => true,
166 ),
167 'watchlist' => array(
168 ApiBase::PARAM_DFLT => 'preferences',
169 ApiBase::PARAM_TYPE => array(
170 'watch',
171 'unwatch',
172 'preferences',
173 'nochange'
174 ),
175 ),
176 );
177 }
178
179 public function getParamDescription() {
180 return array(
181 'title' => 'Title of the page you want to (un)protect',
182 'token' => 'A protect token previously retrieved through prop=info',
183 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
184 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
185 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.' ),
186 'reason' => 'Reason for (un)protecting (optional)',
187 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
188 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
189 'watch' => 'If set, add the page being (un)protected to your watchlist',
190 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
191 );
192 }
193
194 public function getDescription() {
195 return 'Change the protection level of a page';
196 }
197
198 public function getPossibleErrors() {
199 return array_merge( parent::getPossibleErrors(), array(
200 array( 'invalidtitle', 'title' ),
201 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
202 array( 'create-titleexists' ),
203 array( 'missingtitle-createonly' ),
204 array( 'protect-invalidaction', 'action' ),
205 array( 'protect-invalidlevel', 'level' ),
206 array( 'invalidexpiry', 'expiry' ),
207 array( 'pastexpiry', 'expiry' ),
208 ) );
209 }
210
211 public function needsToken() {
212 return true;
213 }
214
215 public function getTokenSalt() {
216 return '';
217 }
218
219 protected function getExamples() {
220 return array(
221 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
222 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
223 );
224 }
225
226 public function getVersion() {
227 return __CLASS__ . ': $Id$';
228 }
229 }