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