Revert r29671, it was based on a misunderstanding of the purpose of the LoggedOut...
[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
22 /**
23 * @todo document, briefly.
24 * @addtogroup SpecialPage
25 */
26 class ProtectionForm {
27 var $mRestrictions = array();
28 var $mReason = '';
29 var $mCascade = false;
30 var $mExpiry = null;
31 var $mPermErrors = array();
32 var $mApplicableTypes = array();
33
34 function __construct( &$article ) {
35 global $wgRequest, $wgUser;
36 global $wgRestrictionTypes, $wgRestrictionLevels;
37 $this->mArticle =& $article;
38 $this->mTitle =& $article->mTitle;
39 $this->mApplicableTypes = $this->mTitle->exists() ? $wgRestrictionTypes : array('create');
40
41 if( $this->mTitle ) {
42 $this->mTitle->loadRestrictions();
43
44 foreach( $this->mApplicableTypes as $action ) {
45 // Fixme: this form currently requires individual selections,
46 // but the db allows multiples separated by commas.
47 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
48 }
49
50 $this->mCascade = $this->mTitle->areRestrictionsCascading();
51
52 if ( $this->mTitle->mRestrictionsExpiry == 'infinity' ) {
53 $this->mExpiry = 'infinite';
54 } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
55 $this->mExpiry = '';
56 } else {
57 $this->mExpiry = wfTimestamp( TS_RFC2822, $this->mTitle->mRestrictionsExpiry );
58 }
59 }
60
61 // The form will be available in read-only to show levels.
62 $this->disabled = ($this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser)) != array();
63 $this->disabledAttrib = $this->disabled
64 ? array( 'disabled' => 'disabled' )
65 : array();
66
67 if( $wgRequest->wasPosted() ) {
68 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
69 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
70 $this->mExpiry = $wgRequest->getText( 'mwProtect-expiry' );
71
72 foreach( $this->mApplicableTypes as $action ) {
73 $val = $wgRequest->getVal( "mwProtect-level-$action" );
74 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
75 $this->mRestrictions[$action] = $val;
76 }
77 }
78 }
79 }
80
81 function execute() {
82 global $wgRequest, $wgOut;
83 if( $wgRequest->wasPosted() ) {
84 if( $this->save() ) {
85 $article = new Article( $this->mTitle );
86 $q = $article->isRedirect() ? 'redirect=no' : '';
87 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
88 }
89 } else {
90 $this->show();
91 }
92 }
93
94 function show( $err = null ) {
95 global $wgOut, $wgUser;
96
97 $wgOut->setRobotpolicy( 'noindex,nofollow' );
98
99 if( is_null( $this->mTitle ) ||
100 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
101 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
102 return;
103 }
104
105 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
106
107 if ( "" != $err ) {
108 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
109 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
110 }
111
112 if ( $cascadeSources && count($cascadeSources) > 0 ) {
113 $titles = '';
114
115 foreach ( $cascadeSources as $title ) {
116 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
117 }
118
119 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
120
121 $wgOut->addWikiText( $notice );
122 }
123
124 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
125 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
126
127 # Show an appropriate message if the user isn't allowed or able to change
128 # the protection settings at this time
129 if( $this->disabled ) {
130 $message = $wgOut->formatPermissionsErrorMessage( $this->mPermErrors );
131 } else {
132 $message = wfMsg( 'protect-text', wfEscapeWikiText( $this->mTitle->getPrefixedText() ) );
133 }
134 $wgOut->addWikiText( $message );
135
136 $wgOut->addHTML( $this->buildForm() );
137
138 $this->showLogExtract( $wgOut );
139 }
140
141 function save() {
142 global $wgRequest, $wgUser, $wgOut;
143
144 if( $this->disabled ) {
145 $this->show();
146 return false;
147 }
148
149 $token = $wgRequest->getVal( 'wpEditToken' );
150 if( !$wgUser->matchEditToken( $token ) ) {
151 $this->show( wfMsg( 'sessionfailure' ) );
152 return false;
153 }
154
155 if ( strlen( $this->mExpiry ) == 0 ) {
156 $this->mExpiry = 'infinite';
157 }
158
159 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
160 $expiry = Block::infinity();
161 } else {
162 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
163 $expiry = strtotime( $this->mExpiry );
164
165 if ( $expiry < 0 || $expiry === false ) {
166 $this->show( wfMsg( 'protect_expiry_invalid' ) );
167 return false;
168 }
169
170 $expiry = wfTimestamp( TS_MW, $expiry );
171
172 if ( $expiry < wfTimestampNow() ) {
173 $this->show( wfMsg( 'protect_expiry_old' ) );
174 return false;
175 }
176
177 }
178
179 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
180 # to a semi-protected page.
181 global $wgGroupPermissions;
182
183 $edit_restriction = $this->mRestrictions['edit'];
184
185 if ($this->mCascade && ($edit_restriction != 'protect') &&
186 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
187 $this->mCascade = false;
188
189 if ($this->mTitle->exists()) {
190 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
191 } else {
192 $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $this->mReason, $expiry );
193 }
194
195 if( !$ok ) {
196 throw new FatalError( "Unknown error at restriction save time." );
197 }
198
199 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
200 $this->mArticle->doWatch();
201 } elseif( $this->mTitle->userIsWatching() ) {
202 $this->mArticle->doUnwatch();
203 }
204
205 return $ok;
206 }
207
208 function buildForm() {
209 global $wgUser;
210
211 $out = '';
212 if( !$this->disabled ) {
213 $out .= $this->buildScript();
214 // The submission needs to reenable the move permission selector
215 // if it's in locked mode, or some browsers won't submit the data.
216 $out .= wfOpenElement( 'form', array(
217 'id' => 'mw-Protect-Form',
218 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
219 'method' => 'post',
220 'onsubmit' => 'protectEnable(true)' ) );
221
222 $out .= wfElement( 'input', array(
223 'type' => 'hidden',
224 'name' => 'wpEditToken',
225 'value' => $wgUser->editToken() ) );
226 }
227
228 $out .= "<table id='mwProtectSet'>";
229 $out .= "<tbody>";
230 $out .= "<tr>\n";
231
232 foreach( $this->mRestrictions as $action => $required ) {
233 /* Not all languages have V_x <-> N_x relation */
234 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
235 }
236 $out .= "</tr>\n";
237 $out .= "<tr>\n";
238 foreach( $this->mRestrictions as $action => $selected ) {
239 $out .= "<td>\n";
240 $out .= $this->buildSelector( $action, $selected );
241 $out .= "</td>\n";
242 }
243 $out .= "</tr>\n";
244
245 // JavaScript will add another row with a value-chaining checkbox
246
247 $out .= "</tbody>\n";
248 $out .= "</table>\n";
249
250 $out .= "<table>\n";
251 $out .= "<tbody>\n";
252
253 global $wgEnableCascadingProtection;
254 if( $wgEnableCascadingProtection && $this->mTitle->exists() )
255 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
256
257 $out .= $this->buildExpiryInput();
258
259 if( !$this->disabled ) {
260 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
261 $out .= "<tr><td></td><td>" . $this->buildWatchInput() . "</td></tr>\n";
262 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
263 }
264
265 $out .= "</tbody>\n";
266 $out .= "</table>\n";
267
268 if ( !$this->disabled ) {
269 $out .= "</form>\n";
270 $out .= $this->buildCleanupScript();
271 }
272
273 return $out;
274 }
275
276 function buildSelector( $action, $selected ) {
277 global $wgRestrictionLevels;
278 $id = 'mwProtect-level-' . $action;
279 $attribs = array(
280 'id' => $id,
281 'name' => $id,
282 'size' => count( $wgRestrictionLevels ),
283 'onchange' => 'protectLevelsUpdate(this)',
284 ) + $this->disabledAttrib;
285
286 $out = wfOpenElement( 'select', $attribs );
287 foreach( $wgRestrictionLevels as $key ) {
288 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
289 }
290 $out .= "</select>\n";
291 return $out;
292 }
293
294 /**
295 * Prepare the label for a protection selector option
296 *
297 * @param string $permission Permission required
298 * @return string
299 */
300 private function getOptionLabel( $permission ) {
301 if( $permission == '' ) {
302 return wfMsg( 'protect-default' );
303 } else {
304 $key = "protect-level-{$permission}";
305 $msg = wfMsg( $key );
306 if( wfEmptyMsg( $key, $msg ) )
307 $msg = wfMsg( 'protect-fallback', $permission );
308 return $msg;
309 }
310 }
311
312 function buildReasonInput() {
313 $id = 'mwProtect-reason';
314 return wfElement( 'label', array(
315 'id' => "$id-label",
316 'for' => $id ),
317 wfMsg( 'protectcomment' ) ) .
318 '</td><td>' .
319 wfElement( 'input', array(
320 'size' => 60,
321 'maxlength' => 255,
322 'name' => $id,
323 'id' => $id,
324 'value' => $this->mReason ) );
325 }
326
327 function buildCascadeInput() {
328 $id = 'mwProtect-cascade';
329 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
330 return $ci;
331 }
332
333 function buildExpiryInput() {
334 $attribs = array( 'id' => 'expires' ) + $this->disabledAttrib;
335 return '<tr>'
336 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
337 . '<td>' . Xml::input( 'mwProtect-expiry', 60, $this->mExpiry, $attribs ) . '</td>'
338 . '</tr>';
339 }
340
341 function buildWatchInput() {
342 global $wgUser;
343 return Xml::checkLabel(
344 wfMsg( 'watchthis' ),
345 'mwProtectWatch',
346 'mwProtectWatch',
347 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' )
348 );
349 }
350
351 function buildSubmit() {
352 return wfElement( 'input', array(
353 'id' => 'mw-Protect-submit',
354 'type' => 'submit',
355 'value' => wfMsg( 'confirm' ) ) );
356 }
357
358 function buildScript() {
359 global $wgStylePath, $wgStyleVersion;
360 return '<script type="text/javascript" src="' .
361 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
362 '"></script>';
363 }
364
365 function buildCleanupScript() {
366 global $wgRestrictionLevels, $wgGroupPermissions;
367 $script = 'var wgCascadeableLevels=';
368 $CascadeableLevels = array();
369 foreach( $wgRestrictionLevels as $key ) {
370 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
371 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
372 }
373 }
374 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
375 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes) . '")';
376 return '<script type="text/javascript">' . $script . '</script>';
377 }
378
379 /**
380 * @param OutputPage $out
381 * @access private
382 */
383 function showLogExtract( &$out ) {
384 # Show relevant lines from the protection log:
385 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
386 $logViewer = new LogViewer(
387 new LogReader(
388 new FauxRequest(
389 array( 'page' => $this->mTitle->getPrefixedText(),
390 'type' => 'protect' ) ) ) );
391 $logViewer->showList( $out );
392 }
393
394 }