c87cf43e70743bd93b97cf1e567908238f4f5768
[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( 'create-titleexists' );
77 }
78 if ( !$titleObj->exists() && $p[0] != 'create' ) {
79 $this->dieUsageMsg( '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 $result = $this->getResult();
134 $result->setIndexedTagName( $res['protections'], 'protection' );
135 $result->addValue( null, $this->getModuleName(), $res );
136 }
137
138 public function mustBePosted() {
139 return true;
140 }
141
142 public function isWriteMode() {
143 return true;
144 }
145
146 public function getAllowedParams() {
147 return array(
148 'title' => array(
149 ApiBase::PARAM_TYPE => 'string',
150 ApiBase::PARAM_REQUIRED => true
151 ),
152 'token' => null,
153 'protections' => array(
154 ApiBase::PARAM_ISMULTI => true,
155 ApiBase::PARAM_REQUIRED => 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( 'invalidtitle', 'title' ),
202 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
203 array( 'create-titleexists' ),
204 array( 'missingtitle-createonly' ),
205 array( 'protect-invalidaction', 'action' ),
206 array( 'protect-invalidlevel', 'level' ),
207 array( 'invalidexpiry', 'expiry' ),
208 array( 'pastexpiry', 'expiry' ),
209 ) );
210 }
211
212 public function needsToken() {
213 return true;
214 }
215
216 public function getTokenSalt() {
217 return '';
218 }
219
220 protected function getExamples() {
221 return array(
222 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
223 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
224 );
225 }
226
227 public function getHelpUrls() {
228 return 'http://www.mediawiki.org/wiki/API:Protect';
229 }
230
231 public function getVersion() {
232 return __CLASS__ . ': $Id$';
233 }
234 }