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->mCascade
= $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade
);
69 $this->mExpiry
= $wgRequest->getText( 'mwProtect-expiry', $this->mExpiry
);
71 foreach( $this->mApplicableTypes
as $action ) {
72 $val = $wgRequest->getVal( "mwProtect-level-$action" );
73 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
74 // Prevent users from setting levels that they cannot later unset
75 if( $val == 'sysop' ) {
76 // Special case, rewrite sysop to either protect and editprotected
77 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
80 if( !$wgUser->isAllowed($val) )
83 $this->mRestrictions
[$action] = $val;
89 global $wgRequest, $wgOut;
90 if( $wgRequest->wasPosted() ) {
92 $article = new Article( $this->mTitle
);
93 $q = $article->isRedirect() ?
'redirect=no' : '';
94 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
101 function show( $err = null ) {
102 global $wgOut, $wgUser;
104 $wgOut->setRobotPolicy( 'noindex,nofollow' );
106 if( is_null( $this->mTitle
) ||
107 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
108 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
112 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
115 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
116 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
119 if ( $cascadeSources && count($cascadeSources) > 0 ) {
122 foreach ( $cascadeSources as $title ) {
123 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
126 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
129 $sk = $wgUser->getSkin();
130 $titleLink = $sk->makeLinkObj( $this->mTitle
);
131 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle
->getPrefixedText() ) );
132 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
134 # Show an appropriate message if the user isn't allowed or able to change
135 # the protection settings at this time
136 if( $this->disabled
) {
138 $wgOut->readOnlyPage();
139 } elseif( $this->mPermErrors
) {
140 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
) );
143 $wgOut->addWikiMsg( 'protect-text', $this->mTitle
->getPrefixedText() );
146 $wgOut->addHTML( $this->buildForm() );
148 $this->showLogExtract( $wgOut );
152 global $wgRequest, $wgUser, $wgOut;
154 if( $this->disabled
) {
159 $token = $wgRequest->getVal( 'wpEditToken' );
160 if( !$wgUser->matchEditToken( $token ) ) {
161 $this->show( wfMsg( 'sessionfailure' ) );
165 if ( strlen( $this->mExpiry
) == 0 ) {
166 $this->mExpiry
= 'infinite';
169 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
170 $expiry = Block
::infinity();
172 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
173 $expiry = strtotime( $this->mExpiry
);
175 if ( $expiry < 0 ||
$expiry === false ) {
176 $this->show( wfMsg( 'protect_expiry_invalid' ) );
180 // Fixme: non-qualified absolute times are not in users specified timezone
181 // and there isn't notice about it in the ui
182 $expiry = wfTimestamp( TS_MW
, $expiry );
184 if ( $expiry < wfTimestampNow() ) {
185 $this->show( wfMsg( 'protect_expiry_old' ) );
191 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
192 # to a semi-protected page.
193 global $wgGroupPermissions;
195 $edit_restriction = $this->mRestrictions
['edit'];
197 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
198 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
199 $this->mCascade
= false;
201 if ($this->mTitle
->exists()) {
202 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
204 $ok = $this->mTitle
->updateTitleProtection( $this->mRestrictions
['create'], $this->mReason
, $expiry );
208 throw new FatalError( "Unknown error at restriction save time." );
211 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
212 $this->mArticle
->doWatch();
213 } elseif( $this->mTitle
->userIsWatching() ) {
214 $this->mArticle
->doUnwatch();
221 * Build the input form
223 * @return $out string HTML form
225 function buildForm() {
229 if( !$this->disabled
) {
230 $out .= $this->buildScript();
231 // The submission needs to reenable the move permission selector
232 // if it's in locked mode, or some browsers won't submit the data.
233 $out .= Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ), 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) ) .
234 Xml
::hidden( 'wpEditToken',$wgUser->editToken() );
237 $out .= Xml
::openElement( 'fieldset' ) .
238 Xml
::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
239 Xml
::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
240 Xml
::openElement( 'tbody' ) .
243 foreach( $this->mRestrictions
as $action => $required ) {
244 /* Not all languages have V_x <-> N_x relation */
245 $label = Xml
::element( 'label',
246 array( 'for' => "mwProtect-level-$action" ),
247 wfMsg( 'restriction-' . $action ) );
248 $out .= "<th>$label</th>";
252 foreach( $this->mRestrictions
as $action => $selected ) {
254 $this->buildSelector( $action, $selected ) .
259 // JavaScript will add another row with a value-chaining checkbox
261 $out .= Xml
::closeElement( 'tbody' ) .
262 Xml
::closeElement( 'table' ) .
263 Xml
::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
264 Xml
::openElement( 'tbody' );
266 if( $this->mTitle
->exists() ) {
269 <td class="mw-input">' .
270 Xml
::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade', $this->mCascade
, $this->disabledAttrib
) .
275 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
277 <td class='mw-label'>" .
278 Xml
::label( wfMsgExt( 'protectexpiry', array( 'parseinline' ) ), 'expires' ) .
280 <td class="mw-input">' .
281 Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) .
285 if( !$this->disabled
) {
286 $id = 'mwProtect-reason';
288 <td class='mw-label'>" .
289 Xml
::label( wfMsg( 'protectcomment' ), $id ) .
291 <td class="mw-input">' .
292 Xml
::input( $id, 60, $this->mReason
, array( 'type' => 'text', 'id' => $id, 'maxlength' => 255 ) ) .
297 <td class='mw-input'>" .
298 Xml
::checkLabel( wfMsg( 'watchthis' ),
299 'mwProtectWatch', 'mwProtectWatch',
300 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' ) ) .
305 <td class='mw-submit'>" .
306 Xml
::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
311 $out .= Xml
::closeElement( 'tbody' ) .
312 Xml
::closeElement( 'table' ) .
313 Xml
::closeElement( 'fieldset' );
315 if ( !$this->disabled
) {
316 $out .= Xml
::closeElement( 'form' ) .
317 $this->buildCleanupScript();
323 function buildSelector( $action, $selected ) {
324 global $wgRestrictionLevels, $wgUser;
325 $id = 'mwProtect-level-' . $action;
329 'size' => count( $wgRestrictionLevels ),
330 'onchange' => 'protectLevelsUpdate(this)',
331 ) +
$this->disabledAttrib
;
333 $out = Xml
::openElement( 'select', $attribs );
334 foreach( $wgRestrictionLevels as $key ) {
335 //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
336 if( $key == 'sysop' ) {
337 //special case, rewrite sysop to protect and editprotected
338 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled
)
341 if( !$wgUser->isAllowed($key) && !$this->disabled
)
344 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
346 $out .= Xml
::closeElement( 'select' );
351 * Prepare the label for a protection selector option
353 * @param string $permission Permission required
356 private function getOptionLabel( $permission ) {
357 if( $permission == '' ) {
358 return wfMsg( 'protect-default' );
360 $key = "protect-level-{$permission}";
361 $msg = wfMsg( $key );
362 if( wfEmptyMsg( $key, $msg ) )
363 $msg = wfMsg( 'protect-fallback', $permission );
368 function buildScript() {
369 global $wgStylePath, $wgStyleVersion;
370 return Xml
::tags( 'script', array(
371 'type' => 'text/javascript',
372 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
375 function buildCleanupScript() {
376 global $wgRestrictionLevels, $wgGroupPermissions;
377 $script = 'var wgCascadeableLevels=';
378 $CascadeableLevels = array();
379 foreach( $wgRestrictionLevels as $key ) {
380 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
381 $CascadeableLevels[] = "'" . Xml
::escapeJsString( $key ) . "'";
384 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
385 $script .= 'protectInitialize("mwProtectSet","' . Xml
::escapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes
) . '")';
386 return Xml
::tags( 'script', array( 'type' => 'text/javascript' ), $script );
390 * @param OutputPage $out
393 function showLogExtract( &$out ) {
394 # Show relevant lines from the protection log:
395 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'protect' ) ) );
396 LogEventsList
::showLogExtract( $out, 'protect', $this->mTitle
->getPrefixedText() );