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.
24 * @addtogroup SpecialPage
26 class ProtectionForm
{
27 var $mRestrictions = array();
29 var $mCascade = false;
31 var $mPermErrors = array();
32 var $mApplicableTypes = array();
34 function __construct( &$article ) {
35 global $wgRequest, $wgUser;
36 global $wgRestrictionTypes, $wgRestrictionLevels;
37 $this->mArticle
=& $article;
38 $this->mTitle
=& $article->mTitle
;
39 $this->mApplicableTypes
= $this->mTitle
->exists() ?
$wgRestrictionTypes : array('create');
42 $this->mTitle
->loadRestrictions();
44 foreach( $this->mApplicableTypes
as $action ) {
45 // Fixme: this form currently requires individual selections,
46 // but the db allows multiples separated by commas.
47 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
50 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
52 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
53 $this->mExpiry
= 'infinite';
54 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
57 $this->mExpiry
= wfTimestamp( TS_RFC2822
, $this->mTitle
->mRestrictionsExpiry
);
61 // The form will be available in read-only to show levels.
62 $this->disabled
= ($this->mPermErrors
= $this->mTitle
->getUserPermissionsErrors('protect',$wgUser)) != array();
63 $this->disabledAttrib
= $this->disabled
64 ?
array( 'disabled' => 'disabled' )
67 if( $wgRequest->wasPosted() ) {
68 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
69 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
70 $this->mExpiry
= $wgRequest->getText( 'mwProtect-expiry' );
72 foreach( $this->mApplicableTypes
as $action ) {
73 $val = $wgRequest->getVal( "mwProtect-level-$action" );
74 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
75 $this->mRestrictions
[$action] = $val;
82 global $wgRequest, $wgOut;
83 if( $wgRequest->wasPosted() ) {
85 $article = new Article( $this->mTitle
);
86 $q = $article->isRedirect() ?
'redirect=no' : '';
87 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
94 function show( $err = null ) {
95 global $wgOut, $wgUser;
97 $wgOut->setRobotpolicy( 'noindex,nofollow' );
99 if( is_null( $this->mTitle
) ||
100 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
101 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
105 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
108 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
109 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
112 if ( $cascadeSources && count($cascadeSources) > 0 ) {
115 foreach ( $cascadeSources as $title ) {
116 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
119 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
121 $wgOut->addWikiText( $notice );
124 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
125 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
127 # Show an appropriate message if the user isn't allowed or able to change
128 # the protection settings at this time
129 if( $this->disabled
) {
130 $message = $wgOut->formatPermissionsErrorMessage( $this->mPermErrors
);
132 $message = wfMsg( 'protect-text', wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) );
134 $wgOut->addWikiText( $message );
136 $wgOut->addHTML( $this->buildForm() );
138 $this->showLogExtract( $wgOut );
142 global $wgRequest, $wgUser, $wgOut;
144 if( $this->disabled
) {
149 $token = $wgRequest->getVal( 'wpEditToken' );
150 if( !$wgUser->matchEditToken( $token ) ) {
151 $this->show( wfMsg( 'sessionfailure' ) );
155 if ( strlen( $this->mExpiry
) == 0 ) {
156 $this->mExpiry
= 'infinite';
159 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
160 $expiry = Block
::infinity();
162 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
163 $expiry = strtotime( $this->mExpiry
);
165 if ( $expiry < 0 ||
$expiry === false ) {
166 $this->show( wfMsg( 'protect_expiry_invalid' ) );
170 $expiry = wfTimestamp( TS_MW
, $expiry );
172 if ( $expiry < wfTimestampNow() ) {
173 $this->show( wfMsg( 'protect_expiry_old' ) );
179 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
180 # to a semi-protected page.
181 global $wgGroupPermissions;
183 $edit_restriction = $this->mRestrictions
['edit'];
185 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
186 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
187 $this->mCascade
= false;
189 if ($this->mTitle
->exists()) {
190 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
192 $ok = $this->mTitle
->updateTitleProtection( $this->mRestrictions
['create'], $this->mReason
, $expiry );
196 throw new FatalError( "Unknown error at restriction save time." );
199 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
200 $this->mArticle
->doWatch();
201 } elseif( $this->mTitle
->userIsWatching() ) {
202 $this->mArticle
->doUnwatch();
208 function buildForm() {
212 if( !$this->disabled
) {
213 $out .= $this->buildScript();
214 // The submission needs to reenable the move permission selector
215 // if it's in locked mode, or some browsers won't submit the data.
216 $out .= wfOpenElement( 'form', array(
217 'id' => 'mw-Protect-Form',
218 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
220 'onsubmit' => 'protectEnable(true)' ) );
222 $out .= wfElement( 'input', array(
224 'name' => 'wpEditToken',
225 'value' => $wgUser->editToken() ) );
228 $out .= "<table id='mwProtectSet'>";
232 foreach( $this->mRestrictions
as $action => $required ) {
233 /* Not all languages have V_x <-> N_x relation */
234 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
238 foreach( $this->mRestrictions
as $action => $selected ) {
240 $out .= $this->buildSelector( $action, $selected );
245 // JavaScript will add another row with a value-chaining checkbox
247 $out .= "</tbody>\n";
248 $out .= "</table>\n";
253 global $wgEnableCascadingProtection;
254 if( $wgEnableCascadingProtection && $this->mTitle
->exists() )
255 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
257 $out .= $this->buildExpiryInput();
259 if( !$this->disabled
) {
260 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
261 $out .= "<tr><td></td><td>" . $this->buildWatchInput() . "</td></tr>\n";
262 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
265 $out .= "</tbody>\n";
266 $out .= "</table>\n";
268 if ( !$this->disabled
) {
270 $out .= $this->buildCleanupScript();
276 function buildSelector( $action, $selected ) {
277 global $wgRestrictionLevels;
278 $id = 'mwProtect-level-' . $action;
282 'size' => count( $wgRestrictionLevels ),
283 'onchange' => 'protectLevelsUpdate(this)',
284 ) +
$this->disabledAttrib
;
286 $out = wfOpenElement( 'select', $attribs );
287 foreach( $wgRestrictionLevels as $key ) {
288 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
290 $out .= "</select>\n";
295 * Prepare the label for a protection selector option
297 * @param string $permission Permission required
300 private function getOptionLabel( $permission ) {
301 if( $permission == '' ) {
302 return wfMsg( 'protect-default' );
304 $key = "protect-level-{$permission}";
305 $msg = wfMsg( $key );
306 if( wfEmptyMsg( $key, $msg ) )
307 $msg = wfMsg( 'protect-fallback', $permission );
312 function buildReasonInput() {
313 $id = 'mwProtect-reason';
314 return wfElement( 'label', array(
317 wfMsg( 'protectcomment' ) ) .
319 wfElement( 'input', array(
324 'value' => $this->mReason
) );
327 function buildCascadeInput() {
328 $id = 'mwProtect-cascade';
329 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
333 function buildExpiryInput() {
334 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
336 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
337 . '<td>' . Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) . '</td>'
341 function buildWatchInput() {
343 return Xml
::checkLabel(
344 wfMsg( 'watchthis' ),
347 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' )
351 function buildSubmit() {
352 return wfElement( 'input', array(
353 'id' => 'mw-Protect-submit',
355 'value' => wfMsg( 'confirm' ) ) );
358 function buildScript() {
359 global $wgStylePath, $wgStyleVersion;
360 return '<script type="text/javascript" src="' .
361 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
365 function buildCleanupScript() {
366 global $wgRestrictionLevels, $wgGroupPermissions;
367 $script = 'var wgCascadeableLevels=';
368 $CascadeableLevels = array();
369 foreach( $wgRestrictionLevels as $key ) {
370 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
371 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
374 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
375 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes
) . '")';
376 return '<script type="text/javascript">' . $script . '</script>';
380 * @param OutputPage $out
383 function showLogExtract( &$out ) {
384 # Show relevant lines from the protection log:
385 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
386 $logViewer = new LogViewer(
389 array( 'page' => $this->mTitle
->getPrefixedText(),
390 'type' => 'protect' ) ) ) );
391 $logViewer->showList( $out );