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 $mReasonList = '';
29 var $mCascade = false;
30 var $mExpiry =array();
31 var $mExpiryList = array();
32 var $mPermErrors = array();
33 var $mApplicableTypes = array();
35 function __construct( Article
$article ) {
36 global $wgRequest, $wgUser;
37 global $wgRestrictionTypes, $wgRestrictionLevels;
38 $this->mArticle
= $article;
39 $this->mTitle
= $article->mTitle
;
40 $this->mApplicableTypes
= $this->mTitle
->exists() ?
$wgRestrictionTypes : array('create');
43 $this->mTitle
->loadRestrictions();
45 foreach( $this->mApplicableTypes
as $action ) {
46 // Fixme: this form currently requires individual selections,
47 // but the db allows multiples separated by commas.
48 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
50 if ( $this->mTitle
->mRestrictionsExpiry
[$action] == 'infinity' ) {
51 $this->mExpiry
[$action] = 'infinite';
52 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
[$action]) == 0 ) {
53 $this->mExpiry
[$action] = '';
55 // FIXME: this format is not user friendly
56 $this->mExpiry
[$action] = wfTimestamp( TS_ISO_8601
, $this->mTitle
->mRestrictionsExpiry
[$action] );
59 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
62 // The form will be available in read-only to show levels.
63 $this->disabled
= wfReadOnly() ||
($this->mPermErrors
= $this->mTitle
->getUserPermissionsErrors('protect',$wgUser)) != array();
64 $this->disabledAttrib
= $this->disabled
65 ?
array( 'disabled' => 'disabled' )
68 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
69 $this->mReasonList
= $wgRequest->getText( 'wpProtectReasonList' );
70 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade
);
72 foreach( $this->mApplicableTypes
as $action ) {
73 // Let dropdown have 'infinite' for unprotected pages
74 if( !($expiry[$action] = $wgRequest->getText( "mwProtect-expiry-$action" )) && $this->mExpiry
[$action] != 'infinite' ) {
75 $expiry[$action] = $this->mExpiry
[$action];
77 $this->mExpiry
[$action] = $expiry[$action];
78 $this->mExpiryList
[$action] = $wgRequest->getText( "wpProtectExpiryList-$action", $this->mExpiry
[$action] ?
'' : 'infinite' );
80 $val = $wgRequest->getVal( "mwProtect-level-$action" );
81 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
82 // Prevent users from setting levels that they cannot later unset
83 if( $val == 'sysop' ) {
84 // Special case, rewrite sysop to either protect and editprotected
85 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
88 if( !$wgUser->isAllowed($val) )
91 $this->mRestrictions
[$action] = $val;
97 global $wgRequest, $wgOut;
98 if( $wgRequest->wasPosted() ) {
100 $q = $this->mArticle
->isRedirect() ?
'redirect=no' : '';
101 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
108 function show( $err = null ) {
109 global $wgOut, $wgUser;
111 $wgOut->setRobotPolicy( 'noindex,nofollow' );
113 if( is_null( $this->mTitle
) ||
114 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
115 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
119 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
122 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
123 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
126 if ( $cascadeSources && count($cascadeSources) > 0 ) {
129 foreach ( $cascadeSources as $title ) {
130 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
133 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
136 $sk = $wgUser->getSkin();
137 $titleLink = $sk->makeLinkObj( $this->mTitle
);
138 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle
->getPrefixedText() ) );
139 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
141 # Show an appropriate message if the user isn't allowed or able to change
142 # the protection settings at this time
143 if( $this->disabled
) {
145 $wgOut->readOnlyPage();
146 } elseif( $this->mPermErrors
) {
147 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
) );
150 $wgOut->addWikiMsg( 'protect-text', $this->mTitle
->getPrefixedText() );
153 $wgOut->addHTML( $this->buildForm() );
155 $this->showLogExtract( $wgOut );
159 global $wgRequest, $wgUser, $wgOut;
161 if ( $this->disabled
) {
166 $token = $wgRequest->getVal( 'wpEditToken' );
167 if ( !$wgUser->matchEditToken( $token ) ) {
168 $this->show( wfMsg( 'sessionfailure' ) );
172 # Create reason string. Use list and/or custom string.
173 $reasonstr = $this->mReasonList
;
174 if ( $reasonstr != 'other' && $this->mReason
!= '' ) {
175 // Entry from drop down menu + additional comment
176 $reasonstr .= ': ' . $this->mReason
;
177 } elseif ( $reasonstr == 'other' ) {
178 $reasonstr = $this->mReason
;
181 foreach( $this->mApplicableTypes
as $action ) {
182 # Custom expiry takes precedence
183 if ( strlen( $wgRequest->getText( "mwProtect-expiry-$action" ) ) == 0 ) {
184 $this->mExpiry
[$action] = strlen($wgRequest->getText( "wpProtectExpiryList-$action")) ?
$wgRequest->getText( "wpProtectExpiryList-$action") : 'infinite';
186 $this->mExpiry
[$action] = $wgRequest->getText( "mwProtect-expiry-$action" );
188 if ( $this->mExpiry
[$action] == 'infinite' ||
$this->mExpiry
[$action] == 'indefinite' ) {
189 $expiry[$action] = Block
::infinity();
191 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
192 $expiry[$action] = strtotime( $this->mExpiry
[$action] );
194 if ( $expiry[$action] < 0 ||
$expiry[$action] === false ) {
195 $this->show( wfMsg( 'protect_expiry_invalid' ) );
199 // Fixme: non-qualified absolute times are not in users specified timezone
200 // and there isn't notice about it in the ui
201 $expiry[$action] = wfTimestamp( TS_MW
, $expiry[$action] );
203 if ( $expiry[$action] < wfTimestampNow() ) {
204 $this->show( wfMsg( 'protect_expiry_old' ) );
209 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
210 # to a semi-protected page.
211 global $wgGroupPermissions;
213 $edit_restriction = $this->mRestrictions
['edit'];
214 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
215 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
216 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
217 $this->mCascade
= false;
219 if ($this->mTitle
->exists()) {
220 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $reasonstr, $this->mCascade
, $expiry );
222 $ok = $this->mTitle
->updateTitleProtection( $this->mRestrictions
['create'], $reasonstr, $expiry['create'] );
226 throw new FatalError( "Unknown error at restriction save time." );
229 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
230 $this->mArticle
->doWatch();
231 } elseif( $this->mTitle
->userIsWatching() ) {
232 $this->mArticle
->doUnwatch();
239 * Build the input form
241 * @return $out string HTML form
243 function buildForm() {
246 $mProtectreasonother = Xml
::label( wfMsg( 'protectcomment' ), 'wpProtectReasonList' );
247 $mProtectreason = Xml
::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
250 if( !$this->disabled
) {
251 $out .= $this->buildScript();
252 // The submission needs to reenable the move permission selector
253 // if it's in locked mode, or some browsers won't submit the data.
254 $out .= Xml
::openElement( 'form', array( 'method' => 'post',
255 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
256 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) );
257 $out .= Xml
::hidden( 'wpEditToken',$wgUser->editToken() );
260 $out .= Xml
::openElement( 'fieldset' ) .
261 Xml
::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
262 Xml
::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
263 Xml
::openElement( 'tbody' );
265 foreach( $this->mRestrictions
as $action => $selected ) {
266 /* Not all languages have V_x <-> N_x relation */
267 $msg = wfMsg( 'restriction-' . $action );
268 if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
271 $label = Xml
::element( 'label', array( 'for' => "mwProtect-level-$action" ), $msg );
272 $out .= "<tr><td><table>" .
273 "<tr><th>$label</th><th></th></tr>" .
274 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td><td>";
276 $reasonDropDown = Xml
::listDropDown( 'wpProtectReasonList',
277 wfMsgForContent( 'protect-dropdown' ),
278 wfMsgForContent( 'protect-otherreason-op' ), '', 'mwProtect-reason', 4 );
279 $scExpiryOptions = wfMsgForContent( 'ipboptions' ); // FIXME: use its own message
281 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled
);
283 $mProtectexpiry = Xml
::label( wfMsg( 'protectexpiry' ), "mwProtectExpiryList-$action" );
284 $mProtectother = Xml
::label( wfMsg( 'protect-othertime' ), "mwProtect-$action-expires" );
285 $expiryFormOptions = Xml
::option( wfMsg( 'protect-othertime-op' ), "othertime" );
286 foreach( explode(',', $scExpiryOptions) as $option ) {
287 if ( strpos($option, ":") === false ) $option = "$option:$option";
288 list($show, $value) = explode(":", $option);
289 $show = htmlspecialchars($show);
290 $value = htmlspecialchars($value);
291 $expiryFormOptions .= Xml
::option( $show, $value, $this->mExpiryList
[$action] === $value ?
true : false ) . "\n";
293 # Add expiry dropdown
294 if( $showProtectOptions && !$this->disabled
) {
297 <td class='mw-label'>
300 <td class='mw-input'>" .
303 'id' => "mwProtectExpiryList-$action",
304 'name' => "wpProtectExpiryList-$action",
305 'onchange' => "protectExpiryListUpdate(this)",
306 'tabindex' => '2' ) +
$this->disabledAttrib
,
307 $expiryFormOptions ) .
311 # Add custom expiry field
312 $attribs = array( 'id' => "mwProtect-$action-expires", 'onkeyup' => 'protectExpiryUpdate(this)' ) +
$this->disabledAttrib
;
314 <td class='mw-label'>" .
317 <td class="mw-input">' .
318 Xml
::input( "mwProtect-expiry-$action", 60, $this->mExpiry
[$action], $attribs ) .
321 $out .= "</td></tr></table></td></tr>";
324 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
326 // JavaScript will add another row with a value-chaining checkbox
327 if( $this->mTitle
->exists() ) {
328 $out .= Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
329 Xml
::openElement( 'tbody' );
332 <td class="mw-input">' .
333 Xml
::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
334 $this->mCascade
, $this->disabledAttrib
) .
337 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
340 # Add manual and custom reason field/selects as well as submit
341 if( !$this->disabled
) {
342 $out .= Xml
::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
343 Xml
::openElement( 'tbody' );
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' ) ) .
376 $out .= Xml
::closeElement( 'tbody' ) . Xml
::closeElement( 'table' );
378 $out .= Xml
::closeElement( 'fieldset' );
380 if ( !$this->disabled
) {
381 $out .= Xml
::closeElement( 'form' ) .
382 $this->buildCleanupScript();
388 function buildSelector( $action, $selected ) {
389 global $wgRestrictionLevels, $wgUser;
392 foreach( $wgRestrictionLevels as $key ) {
393 //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
394 if( $key == 'sysop' ) {
395 //special case, rewrite sysop to protect and editprotected
396 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled
)
399 if( !$wgUser->isAllowed($key) && !$this->disabled
)
405 $id = 'mwProtect-level-' . $action;
409 'size' => count( $levels ),
410 'onchange' => 'protectLevelsUpdate(this)',
411 ) +
$this->disabledAttrib
;
413 $out = Xml
::openElement( 'select', $attribs );
414 foreach( $levels as $key ) {
415 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
417 $out .= Xml
::closeElement( 'select' );
422 * Prepare the label for a protection selector option
424 * @param string $permission Permission required
427 private function getOptionLabel( $permission ) {
428 if( $permission == '' ) {
429 return wfMsg( 'protect-default' );
431 $key = "protect-level-{$permission}";
432 $msg = wfMsg( $key );
433 if( wfEmptyMsg( $key, $msg ) )
434 $msg = wfMsg( 'protect-fallback', $permission );
439 function buildScript() {
440 global $wgStylePath, $wgStyleVersion;
441 return Xml
::tags( 'script', array(
442 'type' => 'text/javascript',
443 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
446 function buildCleanupScript() {
447 global $wgRestrictionLevels, $wgGroupPermissions;
448 $script = 'var wgCascadeableLevels=';
449 $CascadeableLevels = array();
450 foreach( $wgRestrictionLevels as $key ) {
451 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
452 $CascadeableLevels[] = "'" . Xml
::escapeJsString( $key ) . "'";
455 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
456 $script .= 'protectInitialize("mw-protect-table2","' . Xml
::escapeJsString( wfMsg( 'protect-unchain' ) ) .
457 '","' . count($this->mApplicableTypes
) . '")';
458 return Xml
::tags( 'script', array( 'type' => 'text/javascript' ), $script );
462 * @param OutputPage $out
465 function showLogExtract( &$out ) {
466 # Show relevant lines from the protection log:
467 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'protect' ) ) );
468 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
->getPrefixedText() );