5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * http://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
27 * Handles the page protection UI and backend
29 class ProtectionForm
{
30 /** A map of action to restriction level, from request or default */
31 var $mRestrictions = array();
33 /** The custom/additional protection reason */
36 /** The reason selected from the list, blank for other/additional */
37 var $mReasonSelection = '';
39 /** True if the restrictions are cascading, from request or existing protection */
40 var $mCascade = false;
42 /** Map of action to "other" expiry time. Used in preference to mExpirySelection. */
43 var $mExpiry = array();
46 * Map of action to value selected in expiry drop-down list.
47 * Will be set to 'othertime' whenever mExpiry is set.
49 var $mExpirySelection = array();
51 /** Permissions errors for the protect action */
52 var $mPermErrors = array();
54 /** Types (i.e. actions) for which levels can be selected */
55 var $mApplicableTypes = array();
57 /** Map of action to the expiry time of the existing protection */
58 var $mExistingExpiry = array();
60 function __construct( Page
$article ) {
62 // Set instance variables.
63 $this->mArticle
= $article;
64 $this->mTitle
= $article->getTitle();
65 $this->mApplicableTypes
= $this->mTitle
->getRestrictionTypes();
67 // Check if the form should be disabled.
68 // If it is, the form will be available in read-only to show levels.
69 $this->mPermErrors
= $this->mTitle
->getUserPermissionsErrors( 'protect', $wgUser );
71 $this->mPermErrors
[] = array( 'readonlytext', wfReadOnlyReason() );
73 $this->disabled
= $this->mPermErrors
!= array();
74 $this->disabledAttrib
= $this->disabled
75 ?
array( 'disabled' => 'disabled' )
82 * Loads the current state of protection into the object.
85 global $wgRequest, $wgUser;
86 global $wgRestrictionLevels;
88 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
90 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
91 $this->mReasonSelection
= $wgRequest->getText( 'wpProtectReasonSelection' );
92 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade
);
94 foreach( $this->mApplicableTypes
as $action ) {
95 // @todo FIXME: This form currently requires individual selections,
96 // but the db allows multiples separated by commas.
98 // Pull the actual restriction from the DB
99 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
101 if ( !$this->mRestrictions
[$action] ) {
102 // No existing expiry
103 $existingExpiry = '';
105 $existingExpiry = $this->mTitle
->getRestrictionExpiry( $action );
107 $this->mExistingExpiry
[$action] = $existingExpiry;
109 $requestExpiry = $wgRequest->getText( "mwProtect-expiry-$action" );
110 $requestExpirySelection = $wgRequest->getVal( "wpProtectExpirySelection-$action" );
112 if ( $requestExpiry ) {
113 // Custom expiry takes precedence
114 $this->mExpiry
[$action] = $requestExpiry;
115 $this->mExpirySelection
[$action] = 'othertime';
116 } elseif ( $requestExpirySelection ) {
117 // Expiry selected from list
118 $this->mExpiry
[$action] = '';
119 $this->mExpirySelection
[$action] = $requestExpirySelection;
120 } elseif ( $existingExpiry == 'infinity' ) {
121 // Existing expiry is infinite, use "infinite" in drop-down
122 $this->mExpiry
[$action] = '';
123 $this->mExpirySelection
[$action] = 'infinite';
124 } elseif ( $existingExpiry ) {
125 // Use existing expiry in its own list item
126 $this->mExpiry
[$action] = '';
127 $this->mExpirySelection
[$action] = $existingExpiry;
129 // Final default: infinite
130 $this->mExpiry
[$action] = '';
131 $this->mExpirySelection
[$action] = 'infinite';
134 $val = $wgRequest->getVal( "mwProtect-level-$action" );
135 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
136 // Prevent users from setting levels that they cannot later unset
137 if( $val == 'sysop' ) {
138 // Special case, rewrite sysop to either protect and editprotected
139 if( !$wgUser->isAllowedAny( 'protect', 'editprotected' ) )
142 if( !$wgUser->isAllowed($val) )
145 $this->mRestrictions
[$action] = $val;
151 * Get the expiry time for a given action, by combining the relevant inputs.
153 * @param $action string
155 * @return string 14-char timestamp or "infinity", or false if the input was invalid
157 function getExpiry( $action ) {
158 if ( $this->mExpirySelection
[$action] == 'existing' ) {
159 return $this->mExistingExpiry
[$action];
160 } elseif ( $this->mExpirySelection
[$action] == 'othertime' ) {
161 $value = $this->mExpiry
[$action];
163 $value = $this->mExpirySelection
[$action];
165 if ( $value == 'infinite' ||
$value == 'indefinite' ||
$value == 'infinity' ) {
166 $time = wfGetDB( DB_SLAVE
)->getInfinity();
168 $unix = strtotime( $value );
170 if ( !$unix ||
$unix === -1 ) {
174 // @todo FIXME: Non-qualified absolute times are not in users specified timezone
175 // and there isn't notice about it in the ui
176 $time = wfTimestamp( TS_MW
, $unix );
182 * Main entry point for action=protect and action=unprotect
185 global $wgRequest, $wgOut;
187 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
188 throw new ErrorPageError( 'protect-badnamespace-title', 'protect-badnamespace-text' );
191 if( $wgRequest->wasPosted() ) {
192 if( $this->save() ) {
193 $q = $this->mArticle
->isRedirect() ?
'redirect=no' : '';
194 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
202 * Show the input form with optional error message
204 * @param $err String: error message or null if there's no error
206 function show( $err = null ) {
209 $wgOut->setRobotPolicy( 'noindex,nofollow' );
211 if ( is_array( $err ) ) {
212 $wgOut->wrapWikiMsg( "<p class='error'>\n$1\n</p>\n", $err );
213 } elseif ( is_string( $err ) ) {
214 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
217 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
218 if ( $cascadeSources && count($cascadeSources) > 0 ) {
221 foreach ( $cascadeSources as $title ) {
222 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
225 $wgOut->wrapWikiMsg( "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles . "</div>", array( 'protect-cascadeon', count($cascadeSources) ) );
228 # Show an appropriate message if the user isn't allowed or able to change
229 # the protection settings at this time
230 if ( $this->disabled
) {
231 $wgOut->setPageTitle( wfMessage( 'protect-title-notallowed', $this->mTitle
->getPrefixedText() ) );
232 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
, 'protect' ) );
234 $wgOut->setPageTitle( wfMessage( 'protect-title', $this->mTitle
->getPrefixedText() ) );
235 $wgOut->addWikiMsg( 'protect-text',
236 wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) );
239 $wgOut->addBacklinkSubtitle( $this->mTitle
);
240 $wgOut->addHTML( $this->buildForm() );
241 $this->showLogExtract( $wgOut );
245 * Save submitted protection form
247 * @return Boolean: success
250 global $wgRequest, $wgUser, $wgOut;
253 if ( $this->disabled
) {
258 $token = $wgRequest->getVal( 'wpEditToken' );
259 if ( !$wgUser->matchEditToken( $token, array( 'protect', $this->mTitle
->getPrefixedDBkey() ) ) ) {
260 $this->show( array( 'sessionfailure' ) );
264 # Create reason string. Use list and/or custom string.
265 $reasonstr = $this->mReasonSelection
;
266 if ( $reasonstr != 'other' && $this->mReason
!= '' ) {
267 // Entry from drop down menu + additional comment
268 $reasonstr .= wfMessage( 'colon-separator' )->text() . $this->mReason
;
269 } elseif ( $reasonstr == 'other' ) {
270 $reasonstr = $this->mReason
;
273 foreach( $this->mApplicableTypes
as $action ) {
274 $expiry[$action] = $this->getExpiry( $action );
275 if( empty($this->mRestrictions
[$action]) )
276 continue; // unprotected
277 if ( !$expiry[$action] ) {
278 $this->show( array( 'protect_expiry_invalid' ) );
281 if ( $expiry[$action] < wfTimestampNow() ) {
282 $this->show( array( 'protect_expiry_old' ) );
287 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
288 # to a semi-protected page.
289 $edit_restriction = isset( $this->mRestrictions
['edit'] ) ?
$this->mRestrictions
['edit'] : '';
290 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
291 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
292 !User
::groupHasPermission( $edit_restriction, 'protect' ) )
293 $this->mCascade
= false;
295 $status = $this->mArticle
->doUpdateRestrictions( $this->mRestrictions
, $expiry, $this->mCascade
, $reasonstr, $wgUser );
297 if ( !$status->isOK() ) {
298 $this->show( $wgOut->parseInline( $status->getWikiText() ) );
303 * Give extensions a change to handle added form items
305 * @since 1.19 you can (and you should) return false to abort saving;
306 * you can also return an array of message name and its parameters
309 if( !wfRunHooks( 'ProtectionForm::save', array( $this->mArticle
, &$errorMsg ) ) ) {
310 if ( $errorMsg == '' ) {
311 $errorMsg = array( 'hookaborted' );
314 if( $errorMsg != '' ) {
315 $this->show( $errorMsg );
319 if ( $wgUser->isLoggedIn() && $wgRequest->getCheck( 'mwProtectWatch' ) != $wgUser->isWatched( $this->mTitle
) ) {
320 if ( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
321 WatchAction
::doWatch( $this->mTitle
, $wgUser );
323 WatchAction
::doUnwatch( $this->mTitle
, $wgUser );
330 * Build the input form
332 * @return String: HTML form
334 function buildForm() {
335 global $wgUser, $wgLang, $wgOut;
337 $mProtectreasonother = Xml
::label(
338 wfMessage( 'protectcomment' )->text(),
339 'wpProtectReasonSelection'
341 $mProtectreason = Xml
::label(
342 wfMessage( 'protect-otherreason' )->text(),
347 if( !$this->disabled
) {
348 $wgOut->addModules( 'mediawiki.legacy.protect' );
349 $out .= Xml
::openElement( 'form', array( 'method' => 'post',
350 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
351 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) );
354 $out .= Xml
::openElement( 'fieldset' ) .
355 Xml
::element( 'legend', null, wfMessage( 'protect-legend' )->text() ) .
356 Xml
::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
357 Xml
::openElement( 'tbody' );
359 foreach( $this->mRestrictions
as $action => $selected ) {
360 /* Not all languages have V_x <-> N_x relation */
361 $msg = wfMessage( 'restriction-' . $action );
363 Xml
::openElement( 'fieldset' ) .
364 Xml
::element( 'legend', null, $msg->exists() ?
$msg->text() : $action ) .
365 Xml
::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) .
366 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>";
368 $reasonDropDown = Xml
::listDropDown( 'wpProtectReasonSelection',
369 wfMessage( 'protect-dropdown' )->inContentLanguage()->text(),
370 wfMessage( 'protect-otherreason-op' )->inContentLanguage()->text(),
371 $this->mReasonSelection
,
372 'mwProtect-reason', 4 );
373 $scExpiryOptions = wfMessage( 'protect-expiry-options' )->inContentLanguage()->text();
375 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled
);
377 $mProtectexpiry = Xml
::label(
378 wfMessage( 'protectexpiry' )->text(),
379 "mwProtectExpirySelection-$action"
381 $mProtectother = Xml
::label(
382 wfMessage( 'protect-othertime' )->text(),
383 "mwProtect-$action-expires"
386 $expiryFormOptions = '';
387 if ( $this->mExistingExpiry
[$action] && $this->mExistingExpiry
[$action] != 'infinity' ) {
388 $timestamp = $wgLang->timeanddate( $this->mExistingExpiry
[$action], true );
389 $d = $wgLang->date( $this->mExistingExpiry
[$action], true );
390 $t = $wgLang->time( $this->mExistingExpiry
[$action], true );
391 $expiryFormOptions .=
393 wfMessage( 'protect-existing-expiry', $timestamp, $d, $t )->text(),
395 $this->mExpirySelection
[$action] == 'existing'
399 $expiryFormOptions .= Xml
::option(
400 wfMessage( 'protect-othertime-op' )->text(),
403 foreach( explode(',', $scExpiryOptions) as $option ) {
404 if ( strpos($option, ":") === false ) {
405 $show = $value = $option;
407 list($show, $value) = explode(":", $option);
409 $show = htmlspecialchars($show);
410 $value = htmlspecialchars($value);
411 $expiryFormOptions .= Xml
::option( $show, $value, $this->mExpirySelection
[$action] === $value ) . "\n";
413 # Add expiry dropdown
414 if( $showProtectOptions && !$this->disabled
) {
417 <td class='mw-label'>
420 <td class='mw-input'>" .
423 'id' => "mwProtectExpirySelection-$action",
424 'name' => "wpProtectExpirySelection-$action",
425 'onchange' => "ProtectionForm.updateExpiryList(this)",
426 'tabindex' => '2' ) +
$this->disabledAttrib
,
427 $expiryFormOptions ) .
431 # Add custom expiry field
432 $attribs = array( 'id' => "mwProtect-$action-expires",
433 'onkeyup' => 'ProtectionForm.updateExpiry(this)',
434 'onchange' => 'ProtectionForm.updateExpiry(this)' ) +
$this->disabledAttrib
;
436 <td class='mw-label'>" .
439 <td class="mw-input">' .
440 Xml
::input( "mwProtect-expiry-$action", 50, $this->mExpiry
[$action], $attribs ) .
443 $out .= "</td></tr>" .
444 Xml
::closeElement( 'table' ) .
445 Xml
::closeElement( 'fieldset' ) .
448 # Give extensions a chance to add items to the form
449 wfRunHooks( 'ProtectionForm::buildForm', array($this->mArticle
,&$out) );
451 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
453 // JavaScript will add another row with a value-chaining checkbox
454 if( $this->mTitle
->exists() ) {
455 $out .= Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
456 Xml
::openElement( 'tbody' );
459 <td class="mw-input">' .
461 wfMessage( 'protect-cascade' )->text(),
464 $this->mCascade
, $this->disabledAttrib
468 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
471 # Add manual and custom reason field/selects as well as submit
472 if( !$this->disabled
) {
473 $out .= Xml
::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
474 Xml
::openElement( 'tbody' );
477 <td class='mw-label'>
478 {$mProtectreasonother}
480 <td class='mw-input'>
485 <td class='mw-label'>
488 <td class='mw-input'>" .
489 Xml
::input( 'mwProtect-reason', 60, $this->mReason
, array( 'type' => 'text',
490 'id' => 'mwProtect-reason', 'maxlength' => 180 ) ) .
491 // Limited maxlength as the database trims at 255 bytes and other texts
492 // chosen by dropdown menus on this page are also included in this database field.
493 // The byte limit of 180 bytes is enforced in javascript
496 # Disallow watching is user is not logged in
497 if( $wgUser->isLoggedIn() ) {
501 <td class='mw-input'>" .
502 Xml
::checkLabel( wfMessage( 'watchthis' )->text(),
503 'mwProtectWatch', 'mwProtectWatch',
504 $wgUser->isWatched( $this->mTitle
) ||
$wgUser->getOption( 'watchdefault' ) ) .
511 <td class='mw-submit'>" .
513 wfMessage( 'confirm' )->text(),
514 array( 'id' => 'mw-Protect-submit' )
518 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
520 $out .= Xml
::closeElement( 'fieldset' );
522 if ( $wgUser->isAllowed( 'editinterface' ) ) {
523 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Protect-dropdown' );
524 $link = Linker
::link(
526 wfMessage( 'protect-edit-reasonlist' )->escaped(),
528 array( 'action' => 'edit' )
530 $out .= '<p class="mw-protect-editreasons">' . $link . '</p>';
533 if ( !$this->disabled
) {
534 $out .= Html
::hidden( 'wpEditToken', $wgUser->getEditToken( array( 'protect', $this->mTitle
->getPrefixedDBkey() ) ) );
535 $out .= Xml
::closeElement( 'form' );
536 $wgOut->addScript( $this->buildCleanupScript() );
543 * Build protection level selector
545 * @param $action String: action to protect
546 * @param $selected String: current protection level
547 * @return String: HTML fragment
549 function buildSelector( $action, $selected ) {
550 global $wgRestrictionLevels, $wgUser;
553 foreach( $wgRestrictionLevels as $key ) {
554 //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
555 if( $key == 'sysop' ) {
556 //special case, rewrite sysop to protect and editprotected
557 if( !$wgUser->isAllowedAny( 'protect', 'editprotected' ) && !$this->disabled
)
560 if( !$wgUser->isAllowed($key) && !$this->disabled
)
566 $id = 'mwProtect-level-' . $action;
570 'size' => count( $levels ),
571 'onchange' => 'ProtectionForm.updateLevels(this)',
572 ) +
$this->disabledAttrib
;
574 $out = Xml
::openElement( 'select', $attribs );
575 foreach( $levels as $key ) {
576 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
578 $out .= Xml
::closeElement( 'select' );
583 * Prepare the label for a protection selector option
585 * @param $permission String: permission required
588 private function getOptionLabel( $permission ) {
589 if( $permission == '' ) {
590 return wfMessage( 'protect-default' )->text();
592 $msg = wfMessage( "protect-level-{$permission}" );
593 if( $msg->exists() ) {
596 return wfMessage( 'protect-fallback', $permission )->text();
600 function buildCleanupScript() {
601 global $wgRestrictionLevels, $wgOut;
603 $cascadeableLevels = array();
604 foreach( $wgRestrictionLevels as $key ) {
605 if ( User
::groupHasPermission( $key, 'protect' )
608 $cascadeableLevels[] = $key;
612 'tableId' => 'mwProtectSet',
613 'labelText' => wfMessage( 'protect-unchain-permissions' )->plain(),
614 'numTypes' => count( $this->mApplicableTypes
),
615 'existingMatch' => count( array_unique( $this->mExistingExpiry
) ) === 1,
618 $wgOut->addJsConfigVars( 'wgCascadeableLevels', $cascadeableLevels );
619 $script = Xml
::encodeJsCall( 'ProtectionForm.init', array( $options ) );
620 return Html
::inlineScript( ResourceLoader
::makeLoaderConditionalScript( $script ) );
624 * Show protection long extracts for this page
626 * @param $out OutputPage
629 function showLogExtract( &$out ) {
630 # Show relevant lines from the protection log:
631 $protectLogPage = new LogPage( 'protect' );
632 $out->addHTML( Xml
::element( 'h2', null, $protectLogPage->getName()->text() ) );
633 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
);
634 # Let extensions add other relevant log extracts
635 wfRunHooks( 'ProtectionForm::showLogExtract', array($this->mArticle
,$out) );