Merge "GENDER should use the GenderCache"
[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 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
41
42 if ( isset( $params['title'] ) ) {
43 $titleObj = Title::newFromText( $params['title'] );
44 if ( !$titleObj ) {
45 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
46 }
47 } elseif ( isset( $params['pageid'] ) ) {
48 $titleObj = Title::newFromID( $params['pageid'] );
49 if ( !$titleObj ) {
50 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
51 }
52 }
53
54 $errors = $titleObj->getUserPermissionsErrors( 'protect', $this->getUser() );
55 if ( $errors ) {
56 // We don't care about multiple errors, just report one of them
57 $this->dieUsageMsg( reset( $errors ) );
58 }
59
60 $expiry = (array)$params['expiry'];
61 if ( count( $expiry ) != count( $params['protections'] ) ) {
62 if ( count( $expiry ) == 1 ) {
63 $expiry = array_fill( 0, count( $params['protections'] ), $expiry[0] );
64 } else {
65 $this->dieUsageMsg( array( 'toofewexpiries', count( $expiry ), count( $params['protections'] ) ) );
66 }
67 }
68
69 $restrictionTypes = $titleObj->getRestrictionTypes();
70 $dbr = wfGetDB( DB_SLAVE );
71
72 $protections = array();
73 $expiryarray = array();
74 $resultProtections = array();
75 foreach ( $params['protections'] as $i => $prot ) {
76 $p = explode( '=', $prot );
77 $protections[$p[0]] = ( $p[1] == 'all' ? '' : $p[1] );
78
79 if ( $titleObj->exists() && $p[0] == 'create' ) {
80 $this->dieUsageMsg( 'create-titleexists' );
81 }
82 if ( !$titleObj->exists() && $p[0] != 'create' ) {
83 $this->dieUsageMsg( 'missingtitle-createonly' );
84 }
85
86 if ( !in_array( $p[0], $restrictionTypes ) && $p[0] != 'create' ) {
87 $this->dieUsageMsg( array( 'protect-invalidaction', $p[0] ) );
88 }
89 if ( !in_array( $p[1], $wgRestrictionLevels ) && $p[1] != 'all' ) {
90 $this->dieUsageMsg( array( 'protect-invalidlevel', $p[1] ) );
91 }
92
93 if ( in_array( $expiry[$i], array( 'infinite', 'indefinite', 'never' ) ) ) {
94 $expiryarray[$p[0]] = $dbr->getInfinity();
95 } else {
96 $exp = strtotime( $expiry[$i] );
97 if ( $exp < 0 || !$exp ) {
98 $this->dieUsageMsg( array( 'invalidexpiry', $expiry[$i] ) );
99 }
100
101 $exp = wfTimestamp( TS_MW, $exp );
102 if ( $exp < wfTimestampNow() ) {
103 $this->dieUsageMsg( array( 'pastexpiry', $expiry[$i] ) );
104 }
105 $expiryarray[$p[0]] = $exp;
106 }
107 $resultProtections[] = array( $p[0] => $protections[$p[0]],
108 'expiry' => ( $expiryarray[$p[0]] == $dbr->getInfinity() ?
109 'infinite' :
110 wfTimestamp( TS_ISO_8601, $expiryarray[$p[0]] ) ) );
111 }
112
113 $cascade = $params['cascade'];
114
115 $watch = $params['watch'] ? 'watch' : $params['watchlist'];
116 $this->setWatch( $watch, $titleObj );
117
118 $pageObj = WikiPage::factory( $titleObj );
119 $status = $pageObj->doUpdateRestrictions( $protections, $expiryarray, $cascade, $params['reason'], $this->getUser() );
120
121 if ( !$status->isOK() ) {
122 $errors = $status->getErrorsArray();
123 $this->dieUsageMsg( $errors[0] );
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 ),
151 'pageid' => array(
152 ApiBase::PARAM_TYPE => 'integer',
153 ),
154 'token' => null,
155 'protections' => array(
156 ApiBase::PARAM_ISMULTI => true,
157 ApiBase::PARAM_REQUIRED => true,
158 ),
159 'expiry' => array(
160 ApiBase::PARAM_ISMULTI => true,
161 ApiBase::PARAM_ALLOW_DUPLICATES => true,
162 ApiBase::PARAM_DFLT => 'infinite',
163 ),
164 'reason' => '',
165 'cascade' => false,
166 'watch' => array(
167 ApiBase::PARAM_DFLT => false,
168 ApiBase::PARAM_DEPRECATED => true,
169 ),
170 'watchlist' => array(
171 ApiBase::PARAM_DFLT => 'preferences',
172 ApiBase::PARAM_TYPE => array(
173 'watch',
174 'unwatch',
175 'preferences',
176 'nochange'
177 ),
178 ),
179 );
180 }
181
182 public function getParamDescription() {
183 $p = $this->getModulePrefix();
184 return array(
185 'title' => "Title of the page you want to (un)protect. Cannot be used together with {$p}pageid",
186 'pageid' => "ID of the page you want to (un)protect. Cannot be used together with {$p}title",
187 'token' => 'A protect token previously retrieved through prop=info',
188 'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',
189 'expiry' => array( 'Expiry timestamps. If only one timestamp is set, it\'ll be used for all protections.',
190 'Use \'infinite\', \'indefinite\' or \'never\', for a neverexpiring protection.' ),
191 'reason' => 'Reason for (un)protecting (optional)',
192 'cascade' => array( 'Enable cascading protection (i.e. protect pages included in this page)',
193 'Ignored if not all protection levels are \'sysop\' or \'protect\'' ),
194 'watch' => 'If set, add the page being (un)protected to your watchlist',
195 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
196 );
197 }
198
199 public function getDescription() {
200 return 'Change the protection level of a page';
201 }
202
203 public function getPossibleErrors() {
204 return array_merge( parent::getPossibleErrors(),
205 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
206 array(
207 array( 'invalidtitle', 'title' ),
208 array( 'nosuchpageid', 'pageid' ),
209 array( 'toofewexpiries', 'noofexpiries', 'noofprotections' ),
210 array( 'create-titleexists' ),
211 array( 'missingtitle-createonly' ),
212 array( 'protect-invalidaction', 'action' ),
213 array( 'protect-invalidlevel', 'level' ),
214 array( 'invalidexpiry', 'expiry' ),
215 array( 'pastexpiry', 'expiry' ),
216 )
217 );
218 }
219
220 public function needsToken() {
221 return true;
222 }
223
224 public function getTokenSalt() {
225 return '';
226 }
227
228 public function getExamples() {
229 return array(
230 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade=&expiry=20070901163000|never',
231 'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'
232 );
233 }
234
235 public function getHelpUrls() {
236 return 'https://www.mediawiki.org/wiki/API:Protect';
237 }
238
239 public function getVersion() {
240 return __CLASS__ . ': $Id$';
241 }
242 }