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