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 $label = Xml
::element( 'label',
267 array( 'for' => "mwProtect-level-$action" ),
268 wfMsg( 'restriction-' . $action ) );
269 $out .= "<th>$label</th>";
273 foreach( $this->mRestrictions
as $action => $selected ) {
275 $this->buildSelector( $action, $selected ) .
280 $scExpiryOptions = wfMsgForContent( 'ipboptions' ); // FIXME: use its own message
282 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled
);
283 if( !$showProtectOptions )
284 $mProtectother = $mProtectexpiry;
286 $expiryFormOptions = Xml
::option( wfMsg( 'protect-othertime-op' ), 'wpProtectExpiryList' );
287 foreach( explode(',', $scExpiryOptions) as $option ) {
288 if ( strpos($option, ":") === false ) $option = "$option:$option";
289 list($show, $value) = explode(":", $option);
290 $show = htmlspecialchars($show);
291 $value = htmlspecialchars($value);
292 $expiryFormOptions .= Xml
::option( $show, $value, $this->mExpiryList
=== $value ?
true : false ) . "\n";
295 $reasonDropDown = Xml
::listDropDown( 'wpProtectReasonList',
296 wfMsgForContent( 'protect-dropdown' ),
297 wfMsgForContent( 'protect-otherreason-op' ), '', 'mwProtect-reason', 4 );
299 // JavaScript will add another row with a value-chaining checkbox
300 $out .= Xml
::closeElement( 'tbody' ) .
301 Xml
::closeElement( 'table' ) .
302 Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
303 Xml
::openElement( 'tbody' );
305 if( $this->mTitle
->exists() ) {
308 <td class="mw-input">' .
309 Xml
::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
310 $this->mCascade
, $this->disabledAttrib
) .
314 # Add expiry dropdown
315 if( $showProtectOptions && !$this->disabled
) {
318 <td class='mw-label'>
321 <td class='mw-input'>" .
324 'id' => 'mwProtectExpiryList',
325 'name' => 'wpProtectExpiryList',
326 'onchange' => "document.getElementById('expires').value='';",
327 'tabindex' => '2' ) +
$this->disabledAttrib
,
328 $expiryFormOptions ) .
332 # Add custom expiry field
333 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
335 <td class='mw-label'>" .
338 <td class="mw-input">' .
339 Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) .
342 # Add manual and custom reason field/selects
343 if( !$this->disabled
) {
346 <td class='mw-label'>
347 {$mProtectreasonother}
349 <td class='mw-input'>
354 <td class='mw-label'>
357 <td class='mw-input'>" .
358 Xml
::input( 'mwProtect-reason', 60, $this->mReason
, array( 'type' => 'text',
359 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
364 <td class='mw-input'>" .
365 Xml
::checkLabel( wfMsg( 'watchthis' ),
366 'mwProtectWatch', 'mwProtectWatch',
367 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' ) ) .
372 <td class='mw-submit'>" .
373 Xml
::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
378 $out .= Xml
::closeElement( 'tbody' ) .
379 Xml
::closeElement( 'table' ) .
380 Xml
::closeElement( 'fieldset' );
382 if ( !$this->disabled
) {
383 $out .= Xml
::closeElement( 'form' ) .
384 $this->buildCleanupScript();
390 function buildSelector( $action, $selected ) {
391 global $wgRestrictionLevels, $wgUser;
394 foreach( $wgRestrictionLevels as $key ) {
395 //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
396 if( $key == 'sysop' ) {
397 //special case, rewrite sysop to protect and editprotected
398 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled
)
401 if( !$wgUser->isAllowed($key) && !$this->disabled
)
407 $id = 'mwProtect-level-' . $action;
411 'size' => count( $levels ),
412 'onchange' => 'protectLevelsUpdate(this)',
413 ) +
$this->disabledAttrib
;
415 $out = Xml
::openElement( 'select', $attribs );
416 foreach( $levels as $key ) {
417 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
419 $out .= Xml
::closeElement( 'select' );
424 * Prepare the label for a protection selector option
426 * @param string $permission Permission required
429 private function getOptionLabel( $permission ) {
430 if( $permission == '' ) {
431 return wfMsg( 'protect-default' );
433 $key = "protect-level-{$permission}";
434 $msg = wfMsg( $key );
435 if( wfEmptyMsg( $key, $msg ) )
436 $msg = wfMsg( 'protect-fallback', $permission );
441 function buildScript() {
442 global $wgStylePath, $wgStyleVersion;
443 return Xml
::tags( 'script', array(
444 'type' => 'text/javascript',
445 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
448 function buildCleanupScript() {
449 global $wgRestrictionLevels, $wgGroupPermissions;
450 $script = 'var wgCascadeableLevels=';
451 $CascadeableLevels = array();
452 foreach( $wgRestrictionLevels as $key ) {
453 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
454 $CascadeableLevels[] = "'" . Xml
::escapeJsString( $key ) . "'";
457 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
458 $script .= 'protectInitialize("mwProtectSet","' . Xml
::escapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes
) . '")';
459 return Xml
::tags( 'script', array( 'type' => 'text/javascript' ), $script );
463 * @param OutputPage $out
466 function showLogExtract( &$out ) {
467 # Show relevant lines from the protection log:
468 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'protect' ) ) );
469 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
->getPrefixedText() );