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 * @todo document, briefly.
25 class ProtectionForm
{
26 var $mRestrictions = array();
28 var $mCascade = false;
30 var $mPermErrors = array();
31 var $mApplicableTypes = array();
33 function __construct( &$article ) {
34 global $wgRequest, $wgUser;
35 global $wgRestrictionTypes, $wgRestrictionLevels;
36 $this->mArticle
=& $article;
37 $this->mTitle
=& $article->mTitle
;
38 $this->mApplicableTypes
= $this->mTitle
->exists() ?
$wgRestrictionTypes : array('create');
41 $this->mTitle
->loadRestrictions();
43 foreach( $this->mApplicableTypes
as $action ) {
44 // Fixme: this form currently requires individual selections,
45 // but the db allows multiples separated by commas.
46 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
49 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
51 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
52 $this->mExpiry
= 'infinite';
53 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
56 // FIXME: this format is not user friendly
57 $this->mExpiry
= wfTimestamp( TS_ISO_8601
, $this->mTitle
->mRestrictionsExpiry
);
61 // The form will be available in read-only to show levels.
62 $this->disabled
= wfReadOnly() ||
($this->mPermErrors
= $this->mTitle
->getUserPermissionsErrors('protect',$wgUser)) != array();
63 $this->disabledAttrib
= $this->disabled
64 ?
array( 'disabled' => 'disabled' )
67 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
68 $this->mReasonList
= $wgRequest->getText( 'wpProtectReasonList' );
69 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade
);
70 // Let dropdown have 'infinite' for unprotected pages
71 if( !($expiry = $wgRequest->getText( 'mwProtect-expiry' )) && $this->mExpiry
!= 'infinite' ) {
72 $expiry = $this->mExpiry
;
74 $this->mExpiry
= $expiry;
75 $this->mExpiryList
= $wgRequest->getText( 'wpProtectExpiryList', $this->mExpiry ?
'' : 'infinite' );
77 foreach( $this->mApplicableTypes
as $action ) {
78 $val = $wgRequest->getVal( "mwProtect-level-$action" );
79 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
80 // Prevent users from setting levels that they cannot later unset
81 if( $val == 'sysop' ) {
82 // Special case, rewrite sysop to either protect and editprotected
83 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
86 if( !$wgUser->isAllowed($val) )
89 $this->mRestrictions
[$action] = $val;
95 global $wgRequest, $wgOut;
96 if( $wgRequest->wasPosted() ) {
98 $article = new Article( $this->mTitle
);
99 $q = $article->isRedirect() ?
'redirect=no' : '';
100 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
107 function show( $err = null ) {
108 global $wgOut, $wgUser;
110 $wgOut->setRobotPolicy( 'noindex,nofollow' );
112 if( is_null( $this->mTitle
) ||
113 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
114 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
118 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
121 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
122 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
125 if ( $cascadeSources && count($cascadeSources) > 0 ) {
128 foreach ( $cascadeSources as $title ) {
129 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
132 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
135 $sk = $wgUser->getSkin();
136 $titleLink = $sk->makeLinkObj( $this->mTitle
);
137 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle
->getPrefixedText() ) );
138 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
140 # Show an appropriate message if the user isn't allowed or able to change
141 # the protection settings at this time
142 if( $this->disabled
) {
144 $wgOut->readOnlyPage();
145 } elseif( $this->mPermErrors
) {
146 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
) );
149 $wgOut->addWikiMsg( 'protect-text', $this->mTitle
->getPrefixedText() );
152 $wgOut->addHTML( $this->buildForm() );
154 $this->showLogExtract( $wgOut );
158 global $wgRequest, $wgUser, $wgOut;
160 if ( $this->disabled
) {
165 $token = $wgRequest->getVal( 'wpEditToken' );
166 if ( !$wgUser->matchEditToken( $token ) ) {
167 $this->show( wfMsg( 'sessionfailure' ) );
171 # Create reason string. Use list and/or custom string.
172 $reasonstr = $this->mReasonList
;
173 if ( $reasonstr != 'other' && $this->mReason
!= '' ) {
174 // Entry from drop down menu + additional comment
175 $reasonstr .= ': ' . $this->mReason
;
176 } elseif ( $reasonstr == 'other' ) {
177 $reasonstr = $this->mReason
;
179 # Custom expiry takes precedence
180 if ( strlen( $this->mExpiry
) == 0 ) {
181 $this->mExpiry
= strlen($this->mExpiryList
) ?
$this->mExpiryList
: 'infinite';
184 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
185 $expiry = Block
::infinity();
187 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
188 $expiry = strtotime( $this->mExpiry
);
190 if ( $expiry < 0 ||
$expiry === false ) {
191 $this->show( wfMsg( 'protect_expiry_invalid' ) );
195 // Fixme: non-qualified absolute times are not in users specified timezone
196 // and there isn't notice about it in the ui
197 $expiry = wfTimestamp( TS_MW
, $expiry );
199 if ( $expiry < wfTimestampNow() ) {
200 $this->show( wfMsg( 'protect_expiry_old' ) );
205 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
206 # to a semi-protected page.
207 global $wgGroupPermissions;
209 $edit_restriction = $this->mRestrictions
['edit'];
211 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
212 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
213 $this->mCascade
= false;
215 if ($this->mTitle
->exists()) {
216 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $reasonstr, $this->mCascade
, $expiry );
218 $ok = $this->mTitle
->updateTitleProtection( $this->mRestrictions
['create'], $reasonstr, $expiry );
222 throw new FatalError( "Unknown error at restriction save time." );
225 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
226 $this->mArticle
->doWatch();
227 } elseif( $this->mTitle
->userIsWatching() ) {
228 $this->mArticle
->doUnwatch();
235 * Build the input form
237 * @return $out string HTML form
239 function buildForm() {
242 $mProtectexpiry = Xml
::label( wfMsg( 'protectexpiry' ), 'mwProtectExpiryList' );
243 $mProtectother = Xml
::label( wfMsg( 'protect-othertime' ), 'expires' );
244 $mProtectreasonother = Xml
::label( wfMsg( 'protectcomment' ), 'wpProtectReasonList' );
245 $mProtectreason = Xml
::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
248 if( !$this->disabled
) {
249 $out .= $this->buildScript();
250 // The submission needs to reenable the move permission selector
251 // if it's in locked mode, or some browsers won't submit the data.
252 $out .= Xml
::openElement( 'form', array( 'method' => 'post',
253 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
254 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) );
255 $out .= Xml
::hidden( 'wpEditToken',$wgUser->editToken() );
258 $out .= Xml
::openElement( 'fieldset' ) .
259 Xml
::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
260 Xml
::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
261 Xml
::openElement( 'tbody' ) .
264 foreach( $this->mRestrictions
as $action => $required ) {
265 /* Not all languages have V_x <-> N_x relation */
266 $msg = wfMsg( 'restriction-' . $action );
267 if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
270 $label = Xml
::element( 'label',
271 array( 'for' => "mwProtect-level-$action" ),
273 $out .= "<th>$label</th>";
277 foreach( $this->mRestrictions
as $action => $selected ) {
279 $this->buildSelector( $action, $selected ) .
284 $scExpiryOptions = wfMsgForContent( 'ipboptions' ); // FIXME: use its own message
286 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled
);
287 if( !$showProtectOptions )
288 $mProtectother = $mProtectexpiry;
290 $expiryFormOptions = Xml
::option( wfMsg( 'protect-othertime-op' ), 'wpProtectExpiryList' );
291 foreach( explode(',', $scExpiryOptions) as $option ) {
292 if ( strpos($option, ":") === false ) $option = "$option:$option";
293 list($show, $value) = explode(":", $option);
294 $show = htmlspecialchars($show);
295 $value = htmlspecialchars($value);
296 $expiryFormOptions .= Xml
::option( $show, $value, $this->mExpiryList
=== $value ?
true : false ) . "\n";
299 $reasonDropDown = Xml
::listDropDown( 'wpProtectReasonList',
300 wfMsgForContent( 'protect-dropdown' ),
301 wfMsgForContent( 'protect-otherreason-op' ), '', 'mwProtect-reason', 4 );
303 // JavaScript will add another row with a value-chaining checkbox
304 $out .= Xml
::closeElement( 'tbody' ) .
305 Xml
::closeElement( 'table' ) .
306 Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
307 Xml
::openElement( 'tbody' );
309 if( $this->mTitle
->exists() ) {
312 <td class="mw-input">' .
313 Xml
::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
314 $this->mCascade
, $this->disabledAttrib
) .
318 # Add expiry dropdown
319 if( $showProtectOptions && !$this->disabled
) {
322 <td class='mw-label'>
325 <td class='mw-input'>" .
328 'id' => 'mwProtectExpiryList',
329 'name' => 'wpProtectExpiryList',
330 'onchange' => "document.getElementById('expires').value='';",
331 'tabindex' => '2' ) +
$this->disabledAttrib
,
332 $expiryFormOptions ) .
336 # Add custom expiry field
337 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
339 <td class='mw-label'>" .
342 <td class="mw-input">' .
343 Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) .
346 # Add manual and custom reason field/selects
347 if( !$this->disabled
) {
350 <td class='mw-label'>
351 {$mProtectreasonother}
353 <td class='mw-input'>
358 <td class='mw-label'>
361 <td class='mw-input'>" .
362 Xml
::input( 'mwProtect-reason', 60, $this->mReason
, array( 'type' => 'text',
363 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
368 <td class='mw-input'>" .
369 Xml
::checkLabel( wfMsg( 'watchthis' ),
370 'mwProtectWatch', 'mwProtectWatch',
371 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' ) ) .
376 <td class='mw-submit'>" .
377 Xml
::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
382 $out .= Xml
::closeElement( 'tbody' ) .
383 Xml
::closeElement( 'table' ) .
384 Xml
::closeElement( 'fieldset' );
386 if ( !$this->disabled
) {
387 $out .= Xml
::closeElement( 'form' ) .
388 $this->buildCleanupScript();
394 function buildSelector( $action, $selected ) {
395 global $wgRestrictionLevels, $wgUser;
398 foreach( $wgRestrictionLevels as $key ) {
399 //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
400 if( $key == 'sysop' ) {
401 //special case, rewrite sysop to protect and editprotected
402 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled
)
405 if( !$wgUser->isAllowed($key) && !$this->disabled
)
411 $id = 'mwProtect-level-' . $action;
415 'size' => count( $levels ),
416 'onchange' => 'protectLevelsUpdate(this)',
417 ) +
$this->disabledAttrib
;
419 $out = Xml
::openElement( 'select', $attribs );
420 foreach( $levels as $key ) {
421 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
423 $out .= Xml
::closeElement( 'select' );
428 * Prepare the label for a protection selector option
430 * @param string $permission Permission required
433 private function getOptionLabel( $permission ) {
434 if( $permission == '' ) {
435 return wfMsg( 'protect-default' );
437 $key = "protect-level-{$permission}";
438 $msg = wfMsg( $key );
439 if( wfEmptyMsg( $key, $msg ) )
440 $msg = wfMsg( 'protect-fallback', $permission );
445 function buildScript() {
446 global $wgStylePath, $wgStyleVersion;
447 return Xml
::tags( 'script', array(
448 'type' => 'text/javascript',
449 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
452 function buildCleanupScript() {
453 global $wgRestrictionLevels, $wgGroupPermissions;
454 $script = 'var wgCascadeableLevels=';
455 $CascadeableLevels = array();
456 foreach( $wgRestrictionLevels as $key ) {
457 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
458 $CascadeableLevels[] = "'" . Xml
::escapeJsString( $key ) . "'";
461 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
462 $script .= 'protectInitialize("mwProtectSet","' . Xml
::escapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes
) . '")';
463 return Xml
::tags( 'script', array( 'type' => 'text/javascript' ), $script );
467 * @param OutputPage $out
470 function showLogExtract( &$out ) {
471 # Show relevant lines from the protection log:
472 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'protect' ) ) );
473 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
->getPrefixedText() );