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 ) {
58 // Set instance variables.
59 $this->mArticle
= $article;
60 $this->mTitle
= $article->mTitle
;
61 $this->mApplicableTypes
= $this->mTitle
->getRestrictionTypes();
63 // Check if the form should be disabled.
64 // If it is, the form will be available in read-only to show levels.
65 $this->mPermErrors
= $this->mTitle
->getUserPermissionsErrors('protect',$wgUser);
66 $this->disabled
= wfReadOnly() ||
$this->mPermErrors
!= array();
67 $this->disabledAttrib
= $this->disabled
68 ?
array( 'disabled' => 'disabled' )
74 // Loads the current state of protection into the object.
76 global $wgRequest, $wgUser;
77 global $wgRestrictionLevels;
79 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
81 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
82 $this->mReasonSelection
= $wgRequest->getText( 'wpProtectReasonSelection' );
83 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade
);
85 foreach( $this->mApplicableTypes
as $action ) {
86 // Fixme: this form currently requires individual selections,
87 // but the db allows multiples separated by commas.
89 // Pull the actual restriction from the DB
90 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
92 if ( !$this->mRestrictions
[$action] ) {
96 $existingExpiry = $this->mTitle
->getRestrictionExpiry( $action );
98 $this->mExistingExpiry
[$action] = $existingExpiry;
100 $requestExpiry = $wgRequest->getText( "mwProtect-expiry-$action" );
101 $requestExpirySelection = $wgRequest->getVal( "wpProtectExpirySelection-$action" );
103 if ( $requestExpiry ) {
104 // Custom expiry takes precedence
105 $this->mExpiry
[$action] = $requestExpiry;
106 $this->mExpirySelection
[$action] = 'othertime';
107 } elseif ( $requestExpirySelection ) {
108 // Expiry selected from list
109 $this->mExpiry
[$action] = '';
110 $this->mExpirySelection
[$action] = $requestExpirySelection;
111 } elseif ( $existingExpiry == 'infinity' ) {
112 // Existing expiry is infinite, use "infinite" in drop-down
113 $this->mExpiry
[$action] = '';
114 $this->mExpirySelection
[$action] = 'infinite';
115 } elseif ( $existingExpiry ) {
116 // Use existing expiry in its own list item
117 $this->mExpiry
[$action] = '';
118 $this->mExpirySelection
[$action] = $existingExpiry;
120 // Final default: infinite
121 $this->mExpiry
[$action] = '';
122 $this->mExpirySelection
[$action] = 'infinite';
125 $val = $wgRequest->getVal( "mwProtect-level-$action" );
126 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
127 // Prevent users from setting levels that they cannot later unset
128 if( $val == 'sysop' ) {
129 // Special case, rewrite sysop to either protect and editprotected
130 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
133 if( !$wgUser->isAllowed($val) )
136 $this->mRestrictions
[$action] = $val;
142 * Get the expiry time for a given action, by combining the relevant inputs.
143 * Returns a 14-char timestamp or "infinity", or false if the input was invalid
145 function getExpiry( $action ) {
146 if ( $this->mExpirySelection
[$action] == 'existing' ) {
147 return $this->mExistingExpiry
[$action];
148 } elseif ( $this->mExpirySelection
[$action] == 'othertime' ) {
149 $value = $this->mExpiry
[$action];
151 $value = $this->mExpirySelection
[$action];
153 if ( $value == 'infinite' ||
$value == 'indefinite' ||
$value == 'infinity' ) {
154 $time = Block
::infinity();
156 $unix = strtotime( $value );
158 if ( !$unix ||
$unix === -1 ) {
162 // Fixme: non-qualified absolute times are not in users specified timezone
163 // and there isn't notice about it in the ui
164 $time = wfTimestamp( TS_MW
, $unix );
170 global $wgRequest, $wgOut;
171 if( $wgRequest->wasPosted() ) {
172 if( $this->save() ) {
173 $q = $this->mArticle
->isRedirect() ?
'redirect=no' : '';
174 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
181 function show( $err = null ) {
182 global $wgOut, $wgUser;
184 $wgOut->setRobotPolicy( 'noindex,nofollow' );
186 if( is_null( $this->mTitle
) ||
187 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
188 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
192 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
195 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
196 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
199 if ( $cascadeSources && count($cascadeSources) > 0 ) {
202 foreach ( $cascadeSources as $title ) {
203 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
206 $wgOut->wrapWikiMsg( "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles . "</div>", array( 'protect-cascadeon', count($cascadeSources) ) );
209 $sk = $wgUser->getSkin();
210 $titleLink = $sk->link( $this->mTitle
);
211 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle
->getPrefixedText() ) );
212 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
214 # Show an appropriate message if the user isn't allowed or able to change
215 # the protection settings at this time
216 if( $this->disabled
) {
218 $wgOut->readOnlyPage();
219 } elseif( $this->mPermErrors
) {
220 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
) );
223 $wgOut->addWikiMsg( 'protect-text', $this->mTitle
->getPrefixedText() );
226 $wgOut->addHTML( $this->buildForm() );
228 $this->showLogExtract( $wgOut );
232 global $wgRequest, $wgUser;
234 if ( $this->disabled
) {
239 $token = $wgRequest->getVal( 'wpEditToken' );
240 if ( !$wgUser->matchEditToken( $token ) ) {
241 $this->show( wfMsg( 'sessionfailure' ) );
245 # Create reason string. Use list and/or custom string.
246 $reasonstr = $this->mReasonSelection
;
247 if ( $reasonstr != 'other' && $this->mReason
!= '' ) {
248 // Entry from drop down menu + additional comment
249 $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->mReason
;
250 } elseif ( $reasonstr == 'other' ) {
251 $reasonstr = $this->mReason
;
254 foreach( $this->mApplicableTypes
as $action ) {
255 $expiry[$action] = $this->getExpiry( $action );
256 if( empty($this->mRestrictions
[$action]) )
257 continue; // unprotected
258 if ( !$expiry[$action] ) {
259 $this->show( wfMsg( 'protect_expiry_invalid' ) );
262 if ( $expiry[$action] < wfTimestampNow() ) {
263 $this->show( wfMsg( 'protect_expiry_old' ) );
268 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
269 # to a semi-protected page.
270 global $wgGroupPermissions;
272 $edit_restriction = isset( $this->mRestrictions
['edit'] ) ?
$this->mRestrictions
['edit'] : '';
273 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
274 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
275 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
276 $this->mCascade
= false;
278 if ($this->mTitle
->exists()) {
279 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $reasonstr, $this->mCascade
, $expiry );
281 $ok = $this->mTitle
->updateTitleProtection( $this->mRestrictions
['create'], $reasonstr, $expiry['create'] );
285 throw new FatalError( "Unknown error at restriction save time." );
289 # Give extensions a change to handle added form items
290 if( !wfRunHooks( 'ProtectionForm::save', array($this->mArticle
,&$errorMsg) ) ) {
291 throw new FatalError( "Unknown hook error at restriction save time." );
293 if( $errorMsg != '' ) {
294 $this->show( $errorMsg );
298 if( $wgRequest->getCheck( 'mwProtectWatch' ) && $wgUser->isLoggedIn() ) {
299 $this->mArticle
->doWatch();
300 } elseif( $this->mTitle
->userIsWatching() ) {
301 $this->mArticle
->doUnwatch();
307 * Build the input form
309 * @return $out string HTML form
311 function buildForm() {
312 global $wgUser, $wgLang;
314 $mProtectreasonother = Xml
::label( wfMsg( 'protectcomment' ), 'wpProtectReasonSelection' );
315 $mProtectreason = Xml
::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
318 if( !$this->disabled
) {
319 $out .= $this->buildScript();
320 $out .= Xml
::openElement( 'form', array( 'method' => 'post',
321 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
322 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) );
323 $out .= Xml
::hidden( 'wpEditToken',$wgUser->editToken() );
326 $out .= Xml
::openElement( 'fieldset' ) .
327 Xml
::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
328 Xml
::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
329 Xml
::openElement( 'tbody' );
331 foreach( $this->mRestrictions
as $action => $selected ) {
332 /* Not all languages have V_x <-> N_x relation */
333 $msg = wfMsg( 'restriction-' . $action );
334 if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
338 Xml
::openElement( 'fieldset' ) .
339 Xml
::element( 'legend', null, $msg ) .
340 Xml
::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) .
341 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>";
343 $reasonDropDown = Xml
::listDropDown( 'wpProtectReasonSelection',
344 wfMsgForContent( 'protect-dropdown' ),
345 wfMsgForContent( 'protect-otherreason-op' ),
346 $this->mReasonSelection
,
347 'mwProtect-reason', 4 );
348 $scExpiryOptions = wfMsgForContent( 'protect-expiry-options' );
350 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled
);
352 $mProtectexpiry = Xml
::label( wfMsg( 'protectexpiry' ), "mwProtectExpirySelection-$action" );
353 $mProtectother = Xml
::label( wfMsg( 'protect-othertime' ), "mwProtect-$action-expires" );
355 $expiryFormOptions = '';
356 if ( $this->mExistingExpiry
[$action] && $this->mExistingExpiry
[$action] != 'infinity' ) {
357 $timestamp = $wgLang->timeanddate( $this->mExistingExpiry
[$action] );
358 $d = $wgLang->date( $this->mExistingExpiry
[$action] );
359 $t = $wgLang->time( $this->mExistingExpiry
[$action] );
360 $expiryFormOptions .=
362 wfMsg( 'protect-existing-expiry', $timestamp, $d, $t ),
364 $this->mExpirySelection
[$action] == 'existing'
368 $expiryFormOptions .= Xml
::option( wfMsg( 'protect-othertime-op' ), "othertime" ) . "\n";
369 foreach( explode(',', $scExpiryOptions) as $option ) {
370 if ( strpos($option, ":") === false ) {
371 $show = $value = $option;
373 list($show, $value) = explode(":", $option);
375 $show = htmlspecialchars($show);
376 $value = htmlspecialchars($value);
377 $expiryFormOptions .= Xml
::option( $show, $value, $this->mExpirySelection
[$action] === $value ) . "\n";
379 # Add expiry dropdown
380 if( $showProtectOptions && !$this->disabled
) {
383 <td class='mw-label'>
386 <td class='mw-input'>" .
389 'id' => "mwProtectExpirySelection-$action",
390 'name' => "wpProtectExpirySelection-$action",
391 'onchange' => "ProtectionForm.updateExpiryList(this)",
392 'tabindex' => '2' ) +
$this->disabledAttrib
,
393 $expiryFormOptions ) .
397 # Add custom expiry field
398 $attribs = array( 'id' => "mwProtect-$action-expires",
399 'onkeyup' => 'ProtectionForm.updateExpiry(this)',
400 'onchange' => 'ProtectionForm.updateExpiry(this)' ) +
$this->disabledAttrib
;
402 <td class='mw-label'>" .
405 <td class="mw-input">' .
406 Xml
::input( "mwProtect-expiry-$action", 50, $this->mExpiry
[$action], $attribs ) .
409 $out .= "</td></tr>" .
410 Xml
::closeElement( 'table' ) .
411 Xml
::closeElement( 'fieldset' ) .
414 # Give extensions a chance to add items to the form
415 wfRunHooks( 'ProtectionForm::buildForm', array($this->mArticle
,&$out) );
417 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
419 // JavaScript will add another row with a value-chaining checkbox
420 if( $this->mTitle
->exists() ) {
421 $out .= Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
422 Xml
::openElement( 'tbody' );
425 <td class="mw-input">' .
426 Xml
::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
427 $this->mCascade
, $this->disabledAttrib
) .
430 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
433 # Add manual and custom reason field/selects as well as submit
434 if( !$this->disabled
) {
435 $out .= Xml
::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
436 Xml
::openElement( 'tbody' );
439 <td class='mw-label'>
440 {$mProtectreasonother}
442 <td class='mw-input'>
447 <td class='mw-label'>
450 <td class='mw-input'>" .
451 Xml
::input( 'mwProtect-reason', 60, $this->mReason
, array( 'type' => 'text',
452 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
455 # Disallow watching is user is not logged in
456 if( $wgUser->isLoggedIn() ) {
460 <td class='mw-input'>" .
461 Xml
::checkLabel( wfMsg( 'watchthis' ),
462 'mwProtectWatch', 'mwProtectWatch',
463 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' ) ) .
470 <td class='mw-submit'>" .
471 Xml
::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
474 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
476 $out .= Xml
::closeElement( 'fieldset' );
478 if ( $wgUser->isAllowed( 'editinterface' ) ) {
479 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Protect-dropdown' );
480 $link = $wgUser->getSkin()->link(
482 wfMsgHtml( 'protect-edit-reasonlist' ),
484 array( 'action' => 'edit' )
486 $out .= '<p class="mw-protect-editreasons">' . $link . '</p>';
489 if ( !$this->disabled
) {
490 $out .= Xml
::closeElement( 'form' ) .
491 $this->buildCleanupScript();
497 function buildSelector( $action, $selected ) {
498 global $wgRestrictionLevels, $wgUser;
501 foreach( $wgRestrictionLevels as $key ) {
502 //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
503 if( $key == 'sysop' ) {
504 //special case, rewrite sysop to protect and editprotected
505 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled
)
508 if( !$wgUser->isAllowed($key) && !$this->disabled
)
514 $id = 'mwProtect-level-' . $action;
518 'size' => count( $levels ),
519 'onchange' => 'ProtectionForm.updateLevels(this)',
520 ) +
$this->disabledAttrib
;
522 $out = Xml
::openElement( 'select', $attribs );
523 foreach( $levels as $key ) {
524 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
526 $out .= Xml
::closeElement( 'select' );
531 * Prepare the label for a protection selector option
533 * @param string $permission Permission required
536 private function getOptionLabel( $permission ) {
537 if( $permission == '' ) {
538 return wfMsg( 'protect-default' );
540 $key = "protect-level-{$permission}";
541 $msg = wfMsg( $key );
542 if( wfEmptyMsg( $key, $msg ) )
543 $msg = wfMsg( 'protect-fallback', $permission );
548 function buildScript() {
549 global $wgStylePath, $wgStyleVersion;
550 return Xml
::tags( 'script', array(
551 'type' => 'text/javascript',
552 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion.1" ), '' );
555 function buildCleanupScript() {
556 global $wgRestrictionLevels, $wgGroupPermissions;
557 $script = 'var wgCascadeableLevels=';
558 $CascadeableLevels = array();
559 foreach( $wgRestrictionLevels as $key ) {
560 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
561 $CascadeableLevels[] = "'" . Xml
::escapeJsString( $key ) . "'";
564 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
565 $options = (object)array(
566 'tableId' => 'mwProtectSet',
567 'labelText' => wfMsg( 'protect-unchain-permissions' ),
568 'numTypes' => count($this->mApplicableTypes
),
569 'existingMatch' => 1 == count( array_unique( $this->mExistingExpiry
) ),
571 $encOptions = Xml
::encodeJsVar( $options );
573 $script .= "ProtectionForm.init($encOptions)";
574 return Xml
::tags( 'script', array( 'type' => 'text/javascript' ), $script );
578 * @param OutputPage $out
581 function showLogExtract( &$out ) {
582 # Show relevant lines from the protection log:
583 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'protect' ) ) );
584 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
->getPrefixedText() );
585 # Let extensions add other relevant log extracts
586 wfRunHooks( 'ProtectionForm::showLogExtract', array($this->mArticle
,$out) );