3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
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.
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.
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
23 * Handles the page protection UI and backend
25 class ProtectionForm
{
26 /** A map of action to restriction level, from request or default */
27 var $mRestrictions = array();
29 /** The custom/additional protection reason */
32 /** The reason selected from the list, blank for other/additional */
33 var $mReasonSelection = '';
35 /** True if the restrictions are cascading, from request or existing protection */
36 var $mCascade = false;
38 /** Map of action to "other" expiry time. Used in preference to mExpirySelection. */
39 var $mExpiry = array();
42 * Map of action to value selected in expiry drop-down list.
43 * Will be set to 'othertime' whenever mExpiry is set.
45 var $mExpirySelection = array();
47 /** Permissions errors for the protect action */
48 var $mPermErrors = array();
50 /** Types (i.e. actions) for which levels can be selected */
51 var $mApplicableTypes = array();
53 /** Map of action to the expiry time of the existing protection */
54 var $mExistingExpiry = array();
56 function __construct( Article
$article ) {
57 global $wgRequest, $wgUser;
58 global $wgRestrictionTypes, $wgRestrictionLevels;
59 $this->mArticle
= $article;
60 $this->mTitle
= $article->mTitle
;
61 $this->mApplicableTypes
= $this->mTitle
->exists() ?
$wgRestrictionTypes : array('create');
63 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
65 // The form will be available in read-only to show levels.
66 $this->mPermErrors
= $this->mTitle
->getUserPermissionsErrors('protect',$wgUser);
67 $this->disabled
= wfReadOnly() ||
$this->mPermErrors
!= array();
68 $this->disabledAttrib
= $this->disabled
69 ?
array( 'disabled' => 'disabled' )
72 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
73 $this->mReasonSelection
= $wgRequest->getText( 'wpProtectReasonSelection' );
74 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade
);
76 foreach( $this->mApplicableTypes
as $action ) {
77 // Fixme: this form currently requires individual selections,
78 // but the db allows multiples separated by commas.
79 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
81 if ( !$this->mRestrictions
[$action] ) {
85 $existingExpiry = $this->mTitle
->getRestrictionExpiry( $action );
87 $this->mExistingExpiry
[$action] = $existingExpiry;
89 $requestExpiry = $wgRequest->getText( "mwProtect-expiry-$action" );
90 $requestExpirySelection = $wgRequest->getVal( "wpProtectExpirySelection-$action" );
92 if ( $requestExpiry ) {
93 // Custom expiry takes precedence
94 $this->mExpiry
[$action] = $requestExpiry;
95 $this->mExpirySelection
[$action] = 'othertime';
96 } elseif ( $requestExpirySelection ) {
97 // Expiry selected from list
98 $this->mExpiry
[$action] = '';
99 $this->mExpirySelection
[$action] = $requestExpirySelection;
100 } elseif ( $existingExpiry == 'infinity' ) {
101 // Existing expiry is infinite, use "infinite" in drop-down
102 $this->mExpiry
[$action] = '';
103 $this->mExpirySelection
[$action] = 'infinite';
104 } elseif ( $existingExpiry ) {
105 // Use existing expiry in its own list item
106 $this->mExpiry
[$action] = '';
107 $this->mExpirySelection
[$action] = $existingExpiry;
109 // Final default: infinite
110 $this->mExpiry
[$action] = '';
111 $this->mExpirySelection
[$action] = 'infinite';
114 $val = $wgRequest->getVal( "mwProtect-level-$action" );
115 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
116 // Prevent users from setting levels that they cannot later unset
117 if( $val == 'sysop' ) {
118 // Special case, rewrite sysop to either protect and editprotected
119 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
122 if( !$wgUser->isAllowed($val) )
125 $this->mRestrictions
[$action] = $val;
131 * Get the expiry time for a given action, by combining the relevant inputs.
132 * Returns a 14-char timestamp or "infinity", or false if the input was invalid
134 function getExpiry( $action ) {
135 if ( $this->mExpirySelection
[$action] == 'existing' ) {
136 return $this->mExistingExpiry
[$action];
137 } elseif ( $this->mExpirySelection
[$action] == 'othertime' ) {
138 $value = $this->mExpiry
[$action];
140 $value = $this->mExpirySelection
[$action];
142 if ( $value == 'infinite' ||
$value == 'indefinite' ||
$value == 'infinity' ) {
143 $time = Block
::infinity();
145 $unix = strtotime( $value );
147 if ( !$unix ||
$unix === -1 ) {
151 // Fixme: non-qualified absolute times are not in users specified timezone
152 // and there isn't notice about it in the ui
153 $time = wfTimestamp( TS_MW
, $unix );
159 global $wgRequest, $wgOut;
160 if( $wgRequest->wasPosted() ) {
161 if( $this->save() ) {
162 $q = $this->mArticle
->isRedirect() ?
'redirect=no' : '';
163 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
170 function show( $err = null ) {
171 global $wgOut, $wgUser;
173 $wgOut->setRobotPolicy( 'noindex,nofollow' );
175 if( is_null( $this->mTitle
) ||
176 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
177 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
181 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
184 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
185 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
188 if ( $cascadeSources && count($cascadeSources) > 0 ) {
191 foreach ( $cascadeSources as $title ) {
192 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
195 $wgOut->wrapWikiMsg( "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles . "</div>", array( 'protect-cascadeon', count($cascadeSources) ) );
198 $sk = $wgUser->getSkin();
199 $titleLink = $sk->link( $this->mTitle
);
200 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle
->getPrefixedText() ) );
201 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
203 # Show an appropriate message if the user isn't allowed or able to change
204 # the protection settings at this time
205 if( $this->disabled
) {
207 $wgOut->readOnlyPage();
208 } elseif( $this->mPermErrors
) {
209 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
) );
212 $wgOut->addWikiMsg( 'protect-text', $this->mTitle
->getPrefixedText() );
215 $wgOut->addHTML( $this->buildForm() );
217 $this->showLogExtract( $wgOut );
221 global $wgRequest, $wgUser;
223 if ( $this->disabled
) {
228 $token = $wgRequest->getVal( 'wpEditToken' );
229 if ( !$wgUser->matchEditToken( $token ) ) {
230 $this->show( wfMsg( 'sessionfailure' ) );
234 # Create reason string. Use list and/or custom string.
235 $reasonstr = $this->mReasonSelection
;
236 if ( $reasonstr != 'other' && $this->mReason
!= '' ) {
237 // Entry from drop down menu + additional comment
238 $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->mReason
;
239 } elseif ( $reasonstr == 'other' ) {
240 $reasonstr = $this->mReason
;
243 foreach( $this->mApplicableTypes
as $action ) {
244 $expiry[$action] = $this->getExpiry( $action );
245 if( empty($this->mRestrictions
[$action]) )
246 continue; // unprotected
247 if ( !$expiry[$action] ) {
248 $this->show( wfMsg( 'protect_expiry_invalid' ) );
251 if ( $expiry[$action] < wfTimestampNow() ) {
252 $this->show( wfMsg( 'protect_expiry_old' ) );
257 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
258 # to a semi-protected page.
259 global $wgGroupPermissions;
261 $edit_restriction = isset( $this->mRestrictions
['edit'] ) ?
$this->mRestrictions
['edit'] : '';
262 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
263 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
264 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
265 $this->mCascade
= false;
267 if ($this->mTitle
->exists()) {
268 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $reasonstr, $this->mCascade
, $expiry );
270 $ok = $this->mTitle
->updateTitleProtection( $this->mRestrictions
['create'], $reasonstr, $expiry['create'] );
274 throw new FatalError( "Unknown error at restriction save time." );
277 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
278 $this->mArticle
->doWatch();
279 } elseif( $this->mTitle
->userIsWatching() ) {
280 $this->mArticle
->doUnwatch();
286 * Build the input form
288 * @return $out string HTML form
290 function buildForm() {
291 global $wgUser, $wgLang;
293 $mProtectreasonother = Xml
::label( wfMsg( 'protectcomment' ), 'wpProtectReasonSelection' );
294 $mProtectreason = Xml
::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
297 if( !$this->disabled
) {
298 $out .= $this->buildScript();
299 $out .= Xml
::openElement( 'form', array( 'method' => 'post',
300 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
301 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) );
302 $out .= Xml
::hidden( 'wpEditToken',$wgUser->editToken() );
305 $out .= Xml
::openElement( 'fieldset' ) .
306 Xml
::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
307 Xml
::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
308 Xml
::openElement( 'tbody' );
310 foreach( $this->mRestrictions
as $action => $selected ) {
311 /* Not all languages have V_x <-> N_x relation */
312 $msg = wfMsg( 'restriction-' . $action );
313 if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
317 Xml
::openElement( 'fieldset' ) .
318 Xml
::element( 'legend', null, $msg ) .
319 Xml
::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) .
320 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>";
322 $reasonDropDown = Xml
::listDropDown( 'wpProtectReasonSelection',
323 wfMsgForContent( 'protect-dropdown' ),
324 wfMsgForContent( 'protect-otherreason-op' ),
325 $this->mReasonSelection
,
326 'mwProtect-reason', 4 );
327 $scExpiryOptions = wfMsgForContent( 'protect-expiry-options' );
329 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled
);
331 $mProtectexpiry = Xml
::label( wfMsg( 'protectexpiry' ), "mwProtectExpirySelection-$action" );
332 $mProtectother = Xml
::label( wfMsg( 'protect-othertime' ), "mwProtect-$action-expires" );
334 $expiryFormOptions = '';
335 if ( $this->mExistingExpiry
[$action] && $this->mExistingExpiry
[$action] != 'infinity' ) {
336 $timestamp = $wgLang->timeanddate( $this->mExistingExpiry
[$action] );
337 $d = $wgLang->date( $this->mExistingExpiry
[$action] );
338 $t = $wgLang->time( $this->mExistingExpiry
[$action] );
339 $expiryFormOptions .=
341 wfMsg( 'protect-existing-expiry', $timestamp, $d, $t ),
343 $this->mExpirySelection
[$action] == 'existing'
347 $expiryFormOptions .= Xml
::option( wfMsg( 'protect-othertime-op' ), "othertime" ) . "\n";
348 foreach( explode(',', $scExpiryOptions) as $option ) {
349 if ( strpos($option, ":") === false ) {
350 $show = $value = $option;
352 list($show, $value) = explode(":", $option);
354 $show = htmlspecialchars($show);
355 $value = htmlspecialchars($value);
356 $expiryFormOptions .= Xml
::option( $show, $value, $this->mExpirySelection
[$action] === $value ) . "\n";
358 # Add expiry dropdown
359 if( $showProtectOptions && !$this->disabled
) {
362 <td class='mw-label'>
365 <td class='mw-input'>" .
368 'id' => "mwProtectExpirySelection-$action",
369 'name' => "wpProtectExpirySelection-$action",
370 'onchange' => "ProtectionForm.updateExpiryList(this)",
371 'tabindex' => '2' ) +
$this->disabledAttrib
,
372 $expiryFormOptions ) .
376 # Add custom expiry field
377 $attribs = array( 'id' => "mwProtect-$action-expires",
378 'onkeyup' => 'ProtectionForm.updateExpiry(this)' ) +
$this->disabledAttrib
;
380 <td class='mw-label'>" .
383 <td class="mw-input">' .
384 Xml
::input( "mwProtect-expiry-$action", 50, $this->mExpiry
[$action], $attribs ) .
387 $out .= "</td></tr>" .
388 Xml
::closeElement( 'table' ) .
389 Xml
::closeElement( 'fieldset' ) .
393 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
395 // JavaScript will add another row with a value-chaining checkbox
396 if( $this->mTitle
->exists() ) {
397 $out .= Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
398 Xml
::openElement( 'tbody' );
401 <td class="mw-input">' .
402 Xml
::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
403 $this->mCascade
, $this->disabledAttrib
) .
406 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
409 # Add manual and custom reason field/selects as well as submit
410 if( !$this->disabled
) {
411 $out .= Xml
::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
412 Xml
::openElement( 'tbody' );
415 <td class='mw-label'>
416 {$mProtectreasonother}
418 <td class='mw-input'>
423 <td class='mw-label'>
426 <td class='mw-input'>" .
427 Xml
::input( 'mwProtect-reason', 60, $this->mReason
, array( 'type' => 'text',
428 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
433 <td class='mw-input'>" .
434 Xml
::checkLabel( wfMsg( 'watchthis' ),
435 'mwProtectWatch', 'mwProtectWatch',
436 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' ) ) .
441 <td class='mw-submit'>" .
442 Xml
::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
445 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
447 $out .= Xml
::closeElement( 'fieldset' );
449 if ( $wgUser->isAllowed( 'editinterface' ) ) {
450 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Protect-dropdown' );
451 $link = $wgUser->getSkin()->link(
453 wfMsgHtml( 'protect-edit-reasonlist' ),
455 array( 'action' => 'edit' )
457 $out .= '<p class="mw-protect-editreasons">' . $link . '</p>';
460 if ( !$this->disabled
) {
461 $out .= Xml
::closeElement( 'form' ) .
462 $this->buildCleanupScript();
468 function buildSelector( $action, $selected ) {
469 global $wgRestrictionLevels, $wgUser;
472 foreach( $wgRestrictionLevels as $key ) {
473 //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
474 if( $key == 'sysop' ) {
475 //special case, rewrite sysop to protect and editprotected
476 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled
)
479 if( !$wgUser->isAllowed($key) && !$this->disabled
)
485 $id = 'mwProtect-level-' . $action;
489 'size' => count( $levels ),
490 'onchange' => 'ProtectionForm.updateLevels(this)',
491 ) +
$this->disabledAttrib
;
493 $out = Xml
::openElement( 'select', $attribs );
494 foreach( $levels as $key ) {
495 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
497 $out .= Xml
::closeElement( 'select' );
502 * Prepare the label for a protection selector option
504 * @param string $permission Permission required
507 private function getOptionLabel( $permission ) {
508 if( $permission == '' ) {
509 return wfMsg( 'protect-default' );
511 $key = "protect-level-{$permission}";
512 $msg = wfMsg( $key );
513 if( wfEmptyMsg( $key, $msg ) )
514 $msg = wfMsg( 'protect-fallback', $permission );
519 function buildScript() {
520 global $wgStylePath, $wgStyleVersion;
521 return Xml
::tags( 'script', array(
522 'type' => 'text/javascript',
523 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion.1" ), '' );
526 function buildCleanupScript() {
527 global $wgRestrictionLevels, $wgGroupPermissions;
528 $script = 'var wgCascadeableLevels=';
529 $CascadeableLevels = array();
530 foreach( $wgRestrictionLevels as $key ) {
531 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
532 $CascadeableLevels[] = "'" . Xml
::escapeJsString( $key ) . "'";
535 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
536 $options = (object)array(
537 'tableId' => 'mw-protect-table-move',
538 'labelText' => wfMsg( 'protect-unchain' ),
539 'numTypes' => count($this->mApplicableTypes
),
540 'existingMatch' => 1 == count( array_unique( $this->mExistingExpiry
) ),
542 $encOptions = Xml
::encodeJsVar( $options );
544 $script .= "ProtectionForm.init($encOptions)";
545 return Xml
::tags( 'script', array( 'type' => 'text/javascript' ), $script );
549 * @param OutputPage $out
552 function showLogExtract( &$out ) {
553 # Show relevant lines from the protection log:
554 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'protect' ) ) );
555 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
->getPrefixedText() );