make "other time" chain properly with JS
[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 */
25 class ProtectionForm {
26 var $mRestrictions = array();
27 var $mReason = '';
28 var $mReasonList = '';
29 var $mCascade = false;
30 var $mExpiry =array();
31 var $mExpiryList = array();
32 var $mPermErrors = array();
33 var $mApplicableTypes = array();
34
35 function __construct( Article $article ) {
36 global $wgRequest, $wgUser;
37 global $wgRestrictionTypes, $wgRestrictionLevels;
38 $this->mArticle = $article;
39 $this->mTitle = $article->mTitle;
40 $this->mApplicableTypes = $this->mTitle->exists() ? $wgRestrictionTypes : array('create');
41
42 if( $this->mTitle ) {
43 $this->mTitle->loadRestrictions();
44
45 foreach( $this->mApplicableTypes as $action ) {
46 // Fixme: this form currently requires individual selections,
47 // but the db allows multiples separated by commas.
48 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
49
50 if ( $this->mTitle->mRestrictionsExpiry[$action] == 'infinity' ) {
51 $this->mExpiry[$action] = 'infinite';
52 } else if ( strlen($this->mTitle->mRestrictionsExpiry[$action]) == 0 ) {
53 $this->mExpiry[$action] = '';
54 } else {
55 // FIXME: this format is not user friendly
56 $this->mExpiry[$action] = wfTimestamp( TS_ISO_8601, $this->mTitle->mRestrictionsExpiry[$action] );
57 }
58 }
59 $this->mCascade = $this->mTitle->areRestrictionsCascading();
60 }
61
62 // The form will be available in read-only to show levels.
63 $this->disabled = wfReadOnly() || ($this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser)) != array();
64 $this->disabledAttrib = $this->disabled
65 ? array( 'disabled' => 'disabled' )
66 : array();
67
68 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
69 $this->mReasonList = $wgRequest->getText( 'wpProtectReasonList' );
70 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade );
71
72 foreach( $this->mApplicableTypes as $action ) {
73 // Let dropdown have 'infinite' for unprotected pages
74 if( !($expiry[$action] = $wgRequest->getText( "mwProtect-expiry-$action" )) && $this->mExpiry[$action] != 'infinite' ) {
75 $expiry[$action] = $this->mExpiry[$action];
76 }
77 $this->mExpiry[$action] = $expiry[$action];
78 $this->mExpiryList[$action] = $wgRequest->getText( "wpProtectExpiryList-$action", $this->mExpiry[$action] ? '' : 'infinite' );
79
80 $val = $wgRequest->getVal( "mwProtect-level-$action" );
81 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
82 // Prevent users from setting levels that they cannot later unset
83 if( $val == 'sysop' ) {
84 // Special case, rewrite sysop to either protect and editprotected
85 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
86 continue;
87 } else {
88 if( !$wgUser->isAllowed($val) )
89 continue;
90 }
91 $this->mRestrictions[$action] = $val;
92 }
93 }
94 }
95
96 function execute() {
97 global $wgRequest, $wgOut;
98 if( $wgRequest->wasPosted() ) {
99 if( $this->save() ) {
100 $q = $this->mArticle->isRedirect() ? 'redirect=no' : '';
101 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
102 }
103 } else {
104 $this->show();
105 }
106 }
107
108 function show( $err = null ) {
109 global $wgOut, $wgUser;
110
111 $wgOut->setRobotPolicy( 'noindex,nofollow' );
112
113 if( is_null( $this->mTitle ) ||
114 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
115 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
116 return;
117 }
118
119 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
120
121 if ( "" != $err ) {
122 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
123 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
124 }
125
126 if ( $cascadeSources && count($cascadeSources) > 0 ) {
127 $titles = '';
128
129 foreach ( $cascadeSources as $title ) {
130 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
131 }
132
133 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
134 }
135
136 $sk = $wgUser->getSkin();
137 $titleLink = $sk->makeLinkObj( $this->mTitle );
138 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle->getPrefixedText() ) );
139 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
140
141 # Show an appropriate message if the user isn't allowed or able to change
142 # the protection settings at this time
143 if( $this->disabled ) {
144 if( wfReadOnly() ) {
145 $wgOut->readOnlyPage();
146 } elseif( $this->mPermErrors ) {
147 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors ) );
148 }
149 } else {
150 $wgOut->addWikiMsg( 'protect-text', $this->mTitle->getPrefixedText() );
151 }
152
153 $wgOut->addHTML( $this->buildForm() );
154
155 $this->showLogExtract( $wgOut );
156 }
157
158 function save() {
159 global $wgRequest, $wgUser, $wgOut;
160 # Permission check!
161 if ( $this->disabled ) {
162 $this->show();
163 return false;
164 }
165
166 $token = $wgRequest->getVal( 'wpEditToken' );
167 if ( !$wgUser->matchEditToken( $token ) ) {
168 $this->show( wfMsg( 'sessionfailure' ) );
169 return false;
170 }
171
172 # Create reason string. Use list and/or custom string.
173 $reasonstr = $this->mReasonList;
174 if ( $reasonstr != 'other' && $this->mReason != '' ) {
175 // Entry from drop down menu + additional comment
176 $reasonstr .= ': ' . $this->mReason;
177 } elseif ( $reasonstr == 'other' ) {
178 $reasonstr = $this->mReason;
179 }
180 $expiry = array();
181 foreach( $this->mApplicableTypes as $action ) {
182 # Custom expiry takes precedence
183 if ( strlen( $wgRequest->getText( "mwProtect-expiry-$action" ) ) == 0 ) {
184 $this->mExpiry[$action] = strlen($wgRequest->getText( "wpProtectExpiryList-$action")) ? $wgRequest->getText( "wpProtectExpiryList-$action") : 'infinite';
185 } else {
186 $this->mExpiry[$action] = $wgRequest->getText( "mwProtect-expiry-$action" );
187 }
188 if ( $this->mExpiry[$action] == 'infinite' || $this->mExpiry[$action] == 'indefinite' ) {
189 $expiry[$action] = Block::infinity();
190 } else {
191 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
192 $expiry[$action] = strtotime( $this->mExpiry[$action] );
193
194 if ( $expiry[$action] < 0 || $expiry[$action] === false ) {
195 $this->show( wfMsg( 'protect_expiry_invalid' ) );
196 return false;
197 }
198
199 // Fixme: non-qualified absolute times are not in users specified timezone
200 // and there isn't notice about it in the ui
201 $expiry[$action] = wfTimestamp( TS_MW, $expiry[$action] );
202
203 if ( $expiry[$action] < wfTimestampNow() ) {
204 $this->show( wfMsg( 'protect_expiry_old' ) );
205 return false;
206 }
207 }
208 }
209 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
210 # to a semi-protected page.
211 global $wgGroupPermissions;
212
213 $edit_restriction = $this->mRestrictions['edit'];
214
215 if ($this->mCascade && ($edit_restriction != 'protect') &&
216 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
217 $this->mCascade = false;
218
219 if ($this->mTitle->exists()) {
220 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $reasonstr, $this->mCascade, $expiry );
221 } else {
222 $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $reasonstr, $expiry['create'] );
223 }
224
225 if( !$ok ) {
226 throw new FatalError( "Unknown error at restriction save time." );
227 }
228
229 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
230 $this->mArticle->doWatch();
231 } elseif( $this->mTitle->userIsWatching() ) {
232 $this->mArticle->doUnwatch();
233 }
234
235 return $ok;
236 }
237
238 /**
239 * Build the input form
240 *
241 * @return $out string HTML form
242 */
243 function buildForm() {
244 global $wgUser;
245
246 $mProtectreasonother = Xml::label( wfMsg( 'protectcomment' ), 'wpProtectReasonList' );
247 $mProtectreason = Xml::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
248
249 $out = '';
250 if( !$this->disabled ) {
251 $out .= $this->buildScript();
252 // The submission needs to reenable the move permission selector
253 // if it's in locked mode, or some browsers won't submit the data.
254 $out .= Xml::openElement( 'form', array( 'method' => 'post',
255 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
256 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) );
257 $out .= Xml::hidden( 'wpEditToken',$wgUser->editToken() );
258 }
259
260 $out .= Xml::openElement( 'fieldset' ) .
261 Xml::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
262 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
263 Xml::openElement( 'tbody' );
264
265 foreach( $this->mRestrictions as $action => $selected ) {
266 /* Not all languages have V_x <-> N_x relation */
267 $msg = wfMsg( 'restriction-' . $action );
268 if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
269 $msg = $action;
270 }
271 $label = Xml::element( 'label', array( 'for' => "mwProtect-level-$action" ), $msg );
272 $out .= "<tr><td><table>" .
273 "<tr><th>$label</th><th></th></tr>" .
274 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td><td>";
275
276 $reasonDropDown = Xml::listDropDown( 'wpProtectReasonList',
277 wfMsgForContent( 'protect-dropdown' ),
278 wfMsgForContent( 'protect-otherreason-op' ), '', 'mwProtect-reason', 4 );
279 $scExpiryOptions = wfMsgForContent( 'ipboptions' ); // FIXME: use its own message
280
281 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled);
282
283 $mProtectexpiry = Xml::label( wfMsg( 'protectexpiry' ), "mwProtectExpiryList-$action" );
284 $mProtectother = Xml::label( wfMsg( 'protect-othertime' ), "mwProtect-$action-expires" );
285 $expiryFormOptions = Xml::option( wfMsg( 'protect-othertime-op' ), "othertime" );
286 foreach( explode(',', $scExpiryOptions) as $option ) {
287 if ( strpos($option, ":") === false ) $option = "$option:$option";
288 list($show, $value) = explode(":", $option);
289 $show = htmlspecialchars($show);
290 $value = htmlspecialchars($value);
291 $expiryFormOptions .= Xml::option( $show, $value, $this->mExpiryList[$action] === $value ? true : false ) . "\n";
292 }
293 # Add expiry dropdown
294 if( $showProtectOptions && !$this->disabled ) {
295 $out .= "
296 <table><tr>
297 <td class='mw-label'>
298 {$mProtectexpiry}
299 </td>
300 <td class='mw-input'>" .
301 Xml::tags( 'select',
302 array(
303 'id' => "mwProtectExpiryList-$action",
304 'name' => "wpProtectExpiryList-$action",
305 'onchange' => "protectExpiryListUpdate(this)",
306 'tabindex' => '2' ) + $this->disabledAttrib,
307 $expiryFormOptions ) .
308 "</td>
309 </tr></table>";
310 }
311 # Add custom expiry field
312 $attribs = array( 'id' => "mwProtect-$action-expires", 'onkeyup' => 'protectExpiryUpdate(this)' ) + $this->disabledAttrib;
313 $out .= "<table><tr>
314 <td class='mw-label'>" .
315 $mProtectother .
316 '</td>
317 <td class="mw-input">' .
318 Xml::input( "mwProtect-expiry-$action", 60, $this->mExpiry[$action], $attribs ) .
319 '</td>
320 </tr></table>';
321 $out .= "</td></tr></table></td></tr>";
322 }
323
324 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
325
326 // JavaScript will add another row with a value-chaining checkbox
327 if( $this->mTitle->exists() ) {
328 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
329 Xml::openElement( 'tbody' );
330 $out .= '<tr>
331 <td></td>
332 <td class="mw-input">' .
333 Xml::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
334 $this->mCascade, $this->disabledAttrib ) .
335 "</td>
336 </tr>\n";
337 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
338 }
339
340 # Add manual and custom reason field/selects as well as submit
341 if( !$this->disabled ) {
342 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
343 Xml::openElement( 'tbody' );
344 $out .= "
345 <tr>
346 <td class='mw-label'>
347 {$mProtectreasonother}
348 </td>
349 <td class='mw-input'>
350 {$reasonDropDown}
351 </td>
352 </tr>
353 <tr>
354 <td class='mw-label'>
355 {$mProtectreason}
356 </td>
357 <td class='mw-input'>" .
358 Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text',
359 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
360 "</td>
361 </tr>
362 <tr>
363 <td></td>
364 <td class='mw-input'>" .
365 Xml::checkLabel( wfMsg( 'watchthis' ),
366 'mwProtectWatch', 'mwProtectWatch',
367 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) .
368 "</td>
369 </tr>
370 <tr>
371 <td></td>
372 <td class='mw-submit'>" .
373 Xml::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
374 "</td>
375 </tr>\n";
376 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
377 }
378 $out .= Xml::closeElement( 'fieldset' );
379
380 if ( !$this->disabled ) {
381 $out .= Xml::closeElement( 'form' ) .
382 $this->buildCleanupScript();
383 }
384
385 return $out;
386 }
387
388 function buildSelector( $action, $selected ) {
389 global $wgRestrictionLevels, $wgUser;
390
391 $levels = array();
392 foreach( $wgRestrictionLevels as $key ) {
393 //don't let them choose levels above their own (aka so they can still unprotect and edit the page). but only when the form isn't disabled
394 if( $key == 'sysop' ) {
395 //special case, rewrite sysop to protect and editprotected
396 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled )
397 continue;
398 } else {
399 if( !$wgUser->isAllowed($key) && !$this->disabled )
400 continue;
401 }
402 $levels[] = $key;
403 }
404
405 $id = 'mwProtect-level-' . $action;
406 $attribs = array(
407 'id' => $id,
408 'name' => $id,
409 'size' => count( $levels ),
410 'onchange' => 'protectLevelsUpdate(this)',
411 ) + $this->disabledAttrib;
412
413 $out = Xml::openElement( 'select', $attribs );
414 foreach( $levels as $key ) {
415 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
416 }
417 $out .= Xml::closeElement( 'select' );
418 return $out;
419 }
420
421 /**
422 * Prepare the label for a protection selector option
423 *
424 * @param string $permission Permission required
425 * @return string
426 */
427 private function getOptionLabel( $permission ) {
428 if( $permission == '' ) {
429 return wfMsg( 'protect-default' );
430 } else {
431 $key = "protect-level-{$permission}";
432 $msg = wfMsg( $key );
433 if( wfEmptyMsg( $key, $msg ) )
434 $msg = wfMsg( 'protect-fallback', $permission );
435 return $msg;
436 }
437 }
438
439 function buildScript() {
440 global $wgStylePath, $wgStyleVersion;
441 return Xml::tags( 'script', array(
442 'type' => 'text/javascript',
443 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
444 }
445
446 function buildCleanupScript() {
447 global $wgRestrictionLevels, $wgGroupPermissions;
448 $script = 'var wgCascadeableLevels=';
449 $CascadeableLevels = array();
450 foreach( $wgRestrictionLevels as $key ) {
451 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
452 $CascadeableLevels[] = "'" . Xml::escapeJsString( $key ) . "'";
453 }
454 }
455 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
456 $script .= 'protectInitialize("mw-protect-table2","' . Xml::escapeJsString( wfMsg( 'protect-unchain' ) ) .
457 '","' . count($this->mApplicableTypes) . '")';
458 return Xml::tags( 'script', array( 'type' => 'text/javascript' ), $script );
459 }
460
461 /**
462 * @param OutputPage $out
463 * @access private
464 */
465 function showLogExtract( &$out ) {
466 # Show relevant lines from the protection log:
467 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'protect' ) ) );
468 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle->getPrefixedText() );
469 }
470 }