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
$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 $q = $this->mArticle
->isRedirect() ?
'redirect=no' : '';
99 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
106 function show( $err = null ) {
107 global $wgOut, $wgUser;
109 $wgOut->setRobotPolicy( 'noindex,nofollow' );
111 if( is_null( $this->mTitle
) ||
112 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
113 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
117 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
120 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
121 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
124 if ( $cascadeSources && count($cascadeSources) > 0 ) {
127 foreach ( $cascadeSources as $title ) {
128 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
131 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
134 $sk = $wgUser->getSkin();
135 $titleLink = $sk->makeLinkObj( $this->mTitle
);
136 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle
->getPrefixedText() ) );
137 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
139 # Show an appropriate message if the user isn't allowed or able to change
140 # the protection settings at this time
141 if( $this->disabled
) {
143 $wgOut->readOnlyPage();
144 } elseif( $this->mPermErrors
) {
145 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
) );
148 $wgOut->addWikiMsg( 'protect-text', $this->mTitle
->getPrefixedText() );
151 $wgOut->addHTML( $this->buildForm() );
153 $this->showLogExtract( $wgOut );
157 global $wgRequest, $wgUser, $wgOut;
159 if ( $this->disabled
) {
164 $token = $wgRequest->getVal( 'wpEditToken' );
165 if ( !$wgUser->matchEditToken( $token ) ) {
166 $this->show( wfMsg( 'sessionfailure' ) );
170 # Create reason string. Use list and/or custom string.
171 $reasonstr = $this->mReasonList
;
172 if ( $reasonstr != 'other' && $this->mReason
!= '' ) {
173 // Entry from drop down menu + additional comment
174 $reasonstr .= ': ' . $this->mReason
;
175 } elseif ( $reasonstr == 'other' ) {
176 $reasonstr = $this->mReason
;
178 # Custom expiry takes precedence
179 if ( strlen( $this->mExpiry
) == 0 ) {
180 $this->mExpiry
= strlen($this->mExpiryList
) ?
$this->mExpiryList
: 'infinite';
183 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
184 $expiry = Block
::infinity();
186 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
187 $expiry = strtotime( $this->mExpiry
);
189 if ( $expiry < 0 ||
$expiry === false ) {
190 $this->show( wfMsg( 'protect_expiry_invalid' ) );
194 // Fixme: non-qualified absolute times are not in users specified timezone
195 // and there isn't notice about it in the ui
196 $expiry = wfTimestamp( TS_MW
, $expiry );
198 if ( $expiry < wfTimestampNow() ) {
199 $this->show( wfMsg( 'protect_expiry_old' ) );
204 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
205 # to a semi-protected page.
206 global $wgGroupPermissions;
208 $edit_restriction = $this->mRestrictions
['edit'];
210 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
211 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
212 $this->mCascade
= false;
214 if ($this->mTitle
->exists()) {
215 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $reasonstr, $this->mCascade
, $expiry );
217 $ok = $this->mTitle
->updateTitleProtection( $this->mRestrictions
['create'], $reasonstr, $expiry );
221 throw new FatalError( "Unknown error at restriction save time." );
224 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
225 $this->mArticle
->doWatch();
226 } elseif( $this->mTitle
->userIsWatching() ) {
227 $this->mArticle
->doUnwatch();
234 * Build the input form
236 * @return $out string HTML form
238 function buildForm() {
241 $mProtectexpiry = Xml
::label( wfMsg( 'protectexpiry' ), 'mwProtectExpiryList' );
242 $mProtectother = Xml
::label( wfMsg( 'protect-othertime' ), 'expires' );
243 $mProtectreasonother = Xml
::label( wfMsg( 'protectcomment' ), 'wpProtectReasonList' );
244 $mProtectreason = Xml
::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
247 if( !$this->disabled
) {
248 $out .= $this->buildScript();
249 // The submission needs to reenable the move permission selector
250 // if it's in locked mode, or some browsers won't submit the data.
251 $out .= Xml
::openElement( 'form', array( 'method' => 'post',
252 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
253 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) );
254 $out .= Xml
::hidden( 'wpEditToken',$wgUser->editToken() );
257 $out .= Xml
::openElement( 'fieldset' ) .
258 Xml
::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
259 Xml
::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
260 Xml
::openElement( 'tbody' ) .
263 foreach( $this->mRestrictions
as $action => $required ) {
264 /* Not all languages have V_x <-> N_x relation */
265 $msg = wfMsg( 'restriction-' . $action );
266 if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
269 $label = Xml
::element( 'label',
270 array( 'for' => "mwProtect-level-$action" ),
272 $out .= "<th>$label</th>";
276 foreach( $this->mRestrictions
as $action => $selected ) {
278 $this->buildSelector( $action, $selected ) .
283 $scExpiryOptions = wfMsgForContent( 'ipboptions' ); // FIXME: use its own message
285 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled
);
286 if( !$showProtectOptions )
287 $mProtectother = $mProtectexpiry;
289 $expiryFormOptions = Xml
::option( wfMsg( 'protect-othertime-op' ), 'wpProtectExpiryList' );
290 foreach( explode(',', $scExpiryOptions) as $option ) {
291 if ( strpos($option, ":") === false ) $option = "$option:$option";
292 list($show, $value) = explode(":", $option);
293 $show = htmlspecialchars($show);
294 $value = htmlspecialchars($value);
295 $expiryFormOptions .= Xml
::option( $show, $value, $this->mExpiryList
=== $value ?
true : false ) . "\n";
298 $reasonDropDown = Xml
::listDropDown( 'wpProtectReasonList',
299 wfMsgForContent( 'protect-dropdown' ),
300 wfMsgForContent( 'protect-otherreason-op' ), '', 'mwProtect-reason', 4 );
302 // JavaScript will add another row with a value-chaining checkbox
303 $out .= Xml
::closeElement( 'tbody' ) .
304 Xml
::closeElement( 'table' ) .
305 Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
306 Xml
::openElement( 'tbody' );
308 if( $this->mTitle
->exists() ) {
311 <td class="mw-input">' .
312 Xml
::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
313 $this->mCascade
, $this->disabledAttrib
) .
317 # Add expiry dropdown
318 if( $showProtectOptions && !$this->disabled
) {
321 <td class='mw-label'>
324 <td class='mw-input'>" .
327 'id' => 'mwProtectExpiryList',
328 'name' => 'wpProtectExpiryList',
329 'onchange' => "document.getElementById('expires').value='';",
330 'tabindex' => '2' ) +
$this->disabledAttrib
,
331 $expiryFormOptions ) .
335 # Add custom expiry field
336 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
338 <td class='mw-label'>" .
341 <td class="mw-input">' .
342 Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) .
345 # Add manual and custom reason field/selects
346 if( !$this->disabled
) {
349 <td class='mw-label'>
350 {$mProtectreasonother}
352 <td class='mw-input'>
357 <td class='mw-label'>
360 <td class='mw-input'>" .
361 Xml
::input( 'mwProtect-reason', 60, $this->mReason
, array( 'type' => 'text',
362 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
367 <td class='mw-input'>" .
368 Xml
::checkLabel( wfMsg( 'watchthis' ),
369 'mwProtectWatch', 'mwProtectWatch',
370 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' ) ) .
375 <td class='mw-submit'>" .
376 Xml
::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
381 $out .= Xml
::closeElement( 'tbody' ) .
382 Xml
::closeElement( 'table' ) .
383 Xml
::closeElement( 'fieldset' );
385 if ( !$this->disabled
) {
386 $out .= Xml
::closeElement( 'form' ) .
387 $this->buildCleanupScript();
393 function buildSelector( $action, $selected ) {
394 global $wgRestrictionLevels, $wgUser;
397 foreach( $wgRestrictionLevels as $key ) {
398 //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
399 if( $key == 'sysop' ) {
400 //special case, rewrite sysop to protect and editprotected
401 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled
)
404 if( !$wgUser->isAllowed($key) && !$this->disabled
)
410 $id = 'mwProtect-level-' . $action;
414 'size' => count( $levels ),
415 'onchange' => 'protectLevelsUpdate(this)',
416 ) +
$this->disabledAttrib
;
418 $out = Xml
::openElement( 'select', $attribs );
419 foreach( $levels as $key ) {
420 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
422 $out .= Xml
::closeElement( 'select' );
427 * Prepare the label for a protection selector option
429 * @param string $permission Permission required
432 private function getOptionLabel( $permission ) {
433 if( $permission == '' ) {
434 return wfMsg( 'protect-default' );
436 $key = "protect-level-{$permission}";
437 $msg = wfMsg( $key );
438 if( wfEmptyMsg( $key, $msg ) )
439 $msg = wfMsg( 'protect-fallback', $permission );
444 function buildScript() {
445 global $wgStylePath, $wgStyleVersion;
446 return Xml
::tags( 'script', array(
447 'type' => 'text/javascript',
448 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
451 function buildCleanupScript() {
452 global $wgRestrictionLevels, $wgGroupPermissions;
453 $script = 'var wgCascadeableLevels=';
454 $CascadeableLevels = array();
455 foreach( $wgRestrictionLevels as $key ) {
456 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
457 $CascadeableLevels[] = "'" . Xml
::escapeJsString( $key ) . "'";
460 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
461 $script .= 'protectInitialize("mwProtectSet","' . Xml
::escapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes
) . '")';
462 return Xml
::tags( 'script', array( 'type' => 'text/javascript' ), $script );
466 * @param OutputPage $out
469 function showLogExtract( &$out ) {
470 # Show relevant lines from the protection log:
471 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'protect' ) ) );
472 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
->getPrefixedText() );