Stop mutually exclusive values in ApiProtect
[lhc/web/wiklou.git] / includes / api / ApiProtect.php
1 <?php
2
3 /**
4 * Created on Sep 1, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * @ingroup API
32 */
33 class ApiProtect extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 public function execute() {
40 global $wgUser, $wgRestrictionTypes, $wgRestrictionLevels;
41 $params = $this->extractRequestParams();
42
43 if ( isset( $params['watch'] ) && isset( $params['unwatch'] ) ) {
44 $this->dieUsageMsg( array( 'show' ) );
45 }
46
47 $titleObj = null;
48 if ( !isset( $params['title'] ) ) {
49 $this->dieUsageMsg( array( 'missingparam', 'title' ) );
50 }
51 if ( empty( $params['protections'] ) ) {
52 $this->dieUsageMsg( array( 'missingparam', 'protections' ) );
53 }
54
55 $titleObj = Title::newFromText( $params['title'] );
56 if ( !$titleObj ) {
57 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
58 }
59
60 $errors = $titleObj->getUserPermissionsErrors( 'protect', $wgUser );
61 if ( $errors ) {
62 // We don't care about multiple errors, just report one of them
63 $this->dieUsageMsg( reset( $errors ) );
64 }
65
66 $expiry = (array)$params['expiry'];
67 if ( count( $expiry ) != count( $params['protections'] ) ) {
68 if ( count( $expiry ) == 1 ) {
69 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
70 } else {
71 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
72 }
73 }
74
75 $restrictionTypes = $titleObj->getRestrictionTypes();
76
77 $protections = array();
78 $expiryarray = array();
79 $resultProtections = array();
80 foreach ( $params['protections'] as $i => $prot ) {
81 $p = explode( '=', $prot );
82 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
83
84 if ( $titleObj->exists() && $p[0] == 'create' ) {
85 $this->dieUsageMsg( array( 'create-titleexists' ) );
86 }
87 if ( !$titleObj->exists() && $p[0] != 'create' ) {
88 $this->dieUsageMsg( array( 'missingtitle-createonly' ) );
89 }
90
91 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
92 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
93 }
94 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
95 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
96 }
97
98 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
99 $expiryarray[$p[0]] = Block::infinity();
100 } else {
101 $exp = strtotime( $expiry[$i] );
102 if ( $exp < 0 || $exp == false ) {
103 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
104 }
105
106 $exp = wfTimestamp( TS_MW, $exp );
107 if ( $exp < wfTimestampNow() ) {
108 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
109 }
110 $expiryarray[$p[0]] = $exp;
111 }
112 $resultProtections[] = array( $p[0] => $protections[$p[0]],
113 'expiry' => ( $expiryarray[$p[0]] == Block::infinity() ?
114 'infinite' :
115 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
116 }
117
118 $cascade = $params['cascade'];
119 $articleObj = new Article( $titleObj );
120 if ( $params['watch'] ) {
121 $articleObj->doWatch();
122 } elseif ( $params['unwatch'] ) {
123 $articleObj->doUnwatch();
124 }
125
126 if ( $titleObj->exists() ) {
127 $ok = $articleObj->updateRestrictions( $protections, $params['reason'], $cascade, $expiryarray );
128 } else {
129 $ok = $titleObj->updateTitleProtection( $protections['create'], $params['reason'], $expiryarray['create'] );
130 }
131 if ( !$ok ) {
132 // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?
133 // Just throw an unknown error in this case, as it's very likely to be a race condition
134 $this->dieUsageMsg( array() );
135 }
136 $res = array(
137 'title' => $titleObj->getPrefixedText(),
138 'reason' => $params['reason']
139 );
140 if ( $cascade ) {
141 $res['cascade'] = '';
142 }
143 $res['protections'] = $resultProtections;
144 $this->getResult()->setIndexedTagName( $res['protections'], 'protection' );
145 $this->getResult()->addValue( null, $this->getModuleName(), $res );
146 }
147
148 public function mustBePosted() {
149 return true;
150 }
151
152 public function isWriteMode() {
153 return true;
154 }
155
156 public function getAllowedParams() {
157 return array(
158 'title' => null,
159 'token' => null,
160 'protections' => array(
161 ApiBase::PARAM_ISMULTI => true
162 ),
163 'expiry' => array(
164 ApiBase::PARAM_ISMULTI => true,
165 ApiBase::PARAM_ALLOW_DUPLICATES => true,
166 ApiBase::PARAM_DFLT => 'infinite',
167 ),
168 'reason' => '',
169 'cascade' => false,
170 'watch' => false,
171 'unwatch' => false,
172 );
173 }
174
175 public function getParamDescription() {
176 return array(
177 'title' => 'Title of the page you want to (un)protect.',
178 'token' => 'A protect token previously retrieved through prop=info',
179 'protections' => 'Pipe-separated 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 (optional)',
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 'unwatch' => 'Remove the page being (un)protected from your watchlist',
187 );
188 }
189
190 public function getDescription() {
191 return array(
192 'Change the protection level of a page.'
193 );
194 }
195
196 public function getPossibleErrors() {
197 return array_merge( parent::getPossibleErrors(), array(
198 array( 'missingparam', 'title' ),
199 array( 'missingparam', 'protections' ),
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 getTokenSalt() {
212 return null;
213 }
214
215 protected function getExamples() {
216 return array(
217 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000|never',
218 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
219 );
220 }
221
222 public function getVersion() {
223 return __CLASS__ . ': $Id$';
224 }
225 }