(bug 9033) Use a more specific error message when users are not able/allowed to edit...
[lhc/web/wiklou.git] / includes / ProtectionForm.php
1 <?php
2 /**
3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @addtogroup SpecialPage
22 */
23
24 class ProtectionForm {
25 var $mRestrictions = array();
26 var $mReason = '';
27 var $mCascade = false;
28 var $mExpiry = null;
29
30 function __construct( &$article ) {
31 global $wgRequest, $wgUser;
32 global $wgRestrictionTypes, $wgRestrictionLevels;
33 $this->mArticle =& $article;
34 $this->mTitle =& $article->mTitle;
35
36 if( $this->mTitle ) {
37 $this->mTitle->loadRestrictions();
38
39 foreach( $wgRestrictionTypes as $action ) {
40 // Fixme: this form currently requires individual selections,
41 // but the db allows multiples separated by commas.
42 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
43 }
44
45 $this->mCascade = $this->mTitle->areRestrictionsCascading();
46
47 if ( $this->mTitle->mRestrictionsExpiry == 'infinity' ) {
48 $this->mExpiry = 'infinite';
49 } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
50 $this->mExpiry = '';
51 } else {
52 $this->mExpiry = wfTimestamp( TS_RFC2822, $this->mTitle->mRestrictionsExpiry );
53 }
54 }
55
56 // The form will be available in read-only to show levels.
57 $this->disabled = !$wgUser->isAllowed( 'protect' ) || wfReadOnly() || $wgUser->isBlocked();
58 $this->disabledAttrib = $this->disabled
59 ? array( 'disabled' => 'disabled' )
60 : array();
61
62 if( $wgRequest->wasPosted() ) {
63 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
64 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
65 $this->mExpiry = $wgRequest->getText( 'mwProtect-expiry' );
66
67 foreach( $wgRestrictionTypes as $action ) {
68 $val = $wgRequest->getVal( "mwProtect-level-$action" );
69 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
70 $this->mRestrictions[$action] = $val;
71 }
72 }
73 }
74 }
75
76 function execute() {
77 global $wgRequest;
78 if( $wgRequest->wasPosted() ) {
79 if( $this->save() ) {
80 global $wgOut;
81 $wgOut->redirect( $this->mTitle->getFullUrl() );
82 }
83 } else {
84 $this->show();
85 }
86 }
87
88 function show( $err = null ) {
89 global $wgOut, $wgUser;
90
91 $wgOut->setRobotpolicy( 'noindex,nofollow' );
92
93 if( is_null( $this->mTitle ) ||
94 !$this->mTitle->exists() ||
95 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
96 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
97 return;
98 }
99
100 list( $cascadeSources, $restrictions ) = $this->mTitle->getCascadeProtectionSources();
101
102 if ( "" != $err ) {
103 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
104 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
105 }
106
107 if ( $cascadeSources && count($cascadeSources) > 0 ) {
108 $titles = '';
109
110 foreach ( $cascadeSources as $title ) {
111 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
112 }
113
114 $notice = wfMsg( 'protect-cascadeon' ) . "\r\n$titles";
115
116 $wgOut->addWikiText( $notice );
117 }
118
119 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
120 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
121
122 # Show an appropriate message if the user isn't allowed or able to change
123 # the protection settings at this time
124 if( $this->disabled ) {
125 if( $wgUser->isAllowed( 'protect' ) ) {
126 if( $wgUser->isBlocked() ) {
127 # Blocked
128 $message = 'protect-locked-blocked';
129 } else {
130 # Database lock
131 $message = 'protect-locked-dblock';
132 }
133 } else {
134 # Permission error
135 $message = 'protect-locked-access';
136 }
137 } else {
138 $message = 'protect-text';
139 }
140 $wgOut->addWikiText( wfMsg( $message, wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
141
142 $wgOut->addHTML( $this->buildForm() );
143
144 $this->showLogExtract( $wgOut );
145 }
146
147 function save() {
148 global $wgRequest, $wgUser, $wgOut;
149
150 if( $this->disabled ) {
151 $this->show();
152 return false;
153 }
154
155 $token = $wgRequest->getVal( 'wpEditToken' );
156 if( !$wgUser->matchEditToken( $token ) ) {
157 $this->show( wfMsg( 'sessionfailure' ) );
158 return false;
159 }
160
161 if ( strlen( $this->mExpiry ) == 0 ) {
162 $this->mExpiry = 'infinite';
163 }
164
165 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
166 $expiry = Block::infinity();
167 } else {
168 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
169 $expiry = strtotime( $this->mExpiry );
170
171 if ( $expiry < 0 || $expiry === false ) {
172 $this->show( wfMsg( 'protect_expiry_invalid' ) );
173 return false;
174 }
175
176 $expiry = wfTimestamp( TS_MW, $expiry );
177
178 if ( $expiry < wfTimestampNow() ) {
179 $this->show( wfMsg( 'protect_expiry_old' ) );
180 return false;
181 }
182
183 }
184
185 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
186 if( !$ok ) {
187 throw new FatalError( "Unknown error at restriction save time." );
188 }
189 return $ok;
190 }
191
192 function buildForm() {
193 global $wgUser;
194
195 $out = '';
196 if( !$this->disabled ) {
197 $out .= $this->buildScript();
198 // The submission needs to reenable the move permission selector
199 // if it's in locked mode, or some browsers won't submit the data.
200 $out .= wfOpenElement( 'form', array(
201 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
202 'method' => 'post',
203 'onsubmit' => 'protectEnable(true)' ) );
204
205 $out .= wfElement( 'input', array(
206 'type' => 'hidden',
207 'name' => 'wpEditToken',
208 'value' => $wgUser->editToken() ) );
209 }
210
211 $out .= "<table id='mwProtectSet'>";
212 $out .= "<tbody>";
213 $out .= "<tr>\n";
214 foreach( $this->mRestrictions as $action => $required ) {
215 /* Not all languages have V_x <-> N_x relation */
216 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
217 }
218 $out .= "</tr>\n";
219 $out .= "<tr>\n";
220 foreach( $this->mRestrictions as $action => $selected ) {
221 $out .= "<td>\n";
222 $out .= $this->buildSelector( $action, $selected );
223 $out .= "</td>\n";
224 }
225 $out .= "</tr>\n";
226
227 // JavaScript will add another row with a value-chaining checkbox
228
229 $out .= "</tbody>\n";
230 $out .= "</table>\n";
231
232 global $wgEnableCascadingProtection;
233
234 if ($wgEnableCascadingProtection)
235 $out .= $this->buildCascadeInput();
236
237 $out .= "<table>\n";
238 $out .= "<tbody>\n";
239
240 $out .= $this->buildExpiryInput();
241
242 if( !$this->disabled ) {
243 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
244 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
245 }
246
247 $out .= "</tbody>\n";
248 $out .= "</table>\n";
249
250 if ( !$this->disabled ) {
251 $out .= "</form>\n";
252 $out .= $this->buildCleanupScript();
253 }
254
255 return $out;
256 }
257
258 function buildSelector( $action, $selected ) {
259 global $wgRestrictionLevels;
260 $id = 'mwProtect-level-' . $action;
261 $attribs = array(
262 'id' => $id,
263 'name' => $id,
264 'size' => count( $wgRestrictionLevels ),
265 'onchange' => 'protectLevelsUpdate(this)',
266 ) + $this->disabledAttrib;
267
268 $out = wfOpenElement( 'select', $attribs );
269 foreach( $wgRestrictionLevels as $key ) {
270 $out .= $this->buildOption( $key, $selected );
271 }
272 $out .= "</select>\n";
273 return $out;
274 }
275
276 function buildOption( $key, $selected ) {
277 $text = ( $key == '' )
278 ? wfMsg( 'protect-default' )
279 : wfMsg( "protect-level-$key" );
280 $selectedAttrib = ($selected == $key)
281 ? array( 'selected' => 'selected' )
282 : array();
283 return wfElement( 'option',
284 array( 'value' => $key ) + $selectedAttrib,
285 $text );
286 }
287
288 function buildReasonInput() {
289 $id = 'mwProtect-reason';
290 return wfElement( 'label', array(
291 'id' => "$id-label",
292 'for' => $id ),
293 wfMsg( 'protectcomment' ) ) .
294 '</td><td>' .
295 wfElement( 'input', array(
296 'size' => 60,
297 'name' => $id,
298 'id' => $id,
299 'value' => $this->mReason ) );
300 }
301
302 function buildCascadeInput() {
303 $id = 'mwProtect-cascade';
304 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
305 return $ci;
306 }
307
308 function buildExpiryInput() {
309 $id = 'mwProtect-expiry';
310
311 $ci = "<tr> <td align=\"right\">";
312 $ci .= wfElement( 'label', array (
313 'id' => "$id-label",
314 'for' => $id ),
315 wfMsg( 'protectexpiry' ) );
316 $ci .= "</td> <td align=\"left\">";
317 $ci .= wfElement( 'input', array(
318 'size' => 60,
319 'name' => $id,
320 'id' => $id,
321 'value' => $this->mExpiry ) + $this->disabledAttrib );
322 $ci .= "</td></tr>";
323
324 return $ci;
325 }
326
327 function buildSubmit() {
328 return wfElement( 'input', array(
329 'type' => 'submit',
330 'value' => wfMsg( 'confirm' ) ) );
331 }
332
333 function buildScript() {
334 global $wgStylePath, $wgStyleVersion;
335 return '<script type="text/javascript" src="' .
336 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
337 '"></script>';
338 }
339
340 function buildCleanupScript() {
341 global $wgRestrictionLevels, $wgGroupPermissions;
342 $script = 'var wgCascadeableLevels=';
343 $CascadeableLevels = array();
344 foreach( $wgRestrictionLevels as $key ) {
345 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
346 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
347 }
348 }
349 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
350 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
351 return '<script type="text/javascript">' . $script . '</script>';
352 }
353
354 /**
355 * @param OutputPage $out
356 * @access private
357 */
358 function showLogExtract( &$out ) {
359 # Show relevant lines from the deletion log:
360 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
361 $logViewer = new LogViewer(
362 new LogReader(
363 new FauxRequest(
364 array( 'page' => $this->mTitle->getPrefixedText(),
365 'type' => 'protect' ) ) ) );
366 $logViewer->showList( $out );
367 }
368 }
369
370 ?>