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;
32 function __construct( &$article ) {
33 global $wgRequest, $wgUser;
34 global $wgRestrictionTypes, $wgRestrictionLevels;
35 $this->mArticle
=& $article;
36 $this->mTitle
=& $article->mTitle
;
39 $this->mTitle
->loadRestrictions();
41 foreach( $wgRestrictionTypes as $action ) {
42 // Fixme: this form currently requires individual selections,
43 // but the db allows multiples separated by commas.
44 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
47 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
49 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
50 $this->mExpiry
= 'infinite';
51 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
54 $this->mExpiry
= wfTimestamp( TS_RFC2822
, $this->mTitle
->mRestrictionsExpiry
);
58 // The form will be available in read-only to show levels.
59 $this->disabled
= !$wgUser->isAllowed( 'protect' ) ||
wfReadOnly() ||
$wgUser->isBlocked();
60 $this->disabledAttrib
= $this->disabled
61 ?
array( 'disabled' => 'disabled' )
64 if( $wgRequest->wasPosted() ) {
65 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
66 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
67 $this->mExpiry
= $wgRequest->getText( 'mwProtect-expiry' );
69 foreach( $wgRestrictionTypes as $action ) {
70 $val = $wgRequest->getVal( "mwProtect-level-$action" );
71 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
72 $this->mRestrictions
[$action] = $val;
79 global $wgRequest, $wgOut;
80 if( $wgRequest->wasPosted() ) {
82 $article = new Article( $this->mTitle
);
83 $q = $article->isRedirect() ?
'redirect=no' : '';
84 $wgOut->redirect( $this->mTitle
->getFullUrl( $q ) );
91 function show( $err = null ) {
92 global $wgOut, $wgUser;
94 $wgOut->setRobotpolicy( 'noindex,nofollow' );
96 if( is_null( $this->mTitle
) ||
97 !$this->mTitle
->exists() ||
98 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
99 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
103 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
106 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
107 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
110 if ( $cascadeSources && count($cascadeSources) > 0 ) {
113 foreach ( $cascadeSources as $title ) {
114 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
117 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
119 $wgOut->addWikiText( $notice );
122 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
123 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
125 # Show an appropriate message if the user isn't allowed or able to change
126 # the protection settings at this time
127 if( $this->disabled
) {
128 if( $wgUser->isAllowed( 'protect' ) ) {
129 if( $wgUser->isBlocked() ) {
131 $message = 'protect-locked-blocked';
134 $message = 'protect-locked-dblock';
138 $message = 'protect-locked-access';
141 $message = 'protect-text';
143 $wgOut->addWikiText( wfMsg( $message, wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
145 $wgOut->addHTML( $this->buildForm() );
147 $this->showLogExtract( $wgOut );
151 global $wgRequest, $wgUser, $wgOut;
153 if( $this->disabled
) {
158 $token = $wgRequest->getVal( 'wpEditToken' );
159 if( !$wgUser->matchEditToken( $token ) ) {
160 $this->show( wfMsg( 'sessionfailure' ) );
164 if ( strlen( $this->mExpiry
) == 0 ) {
165 $this->mExpiry
= 'infinite';
168 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
169 $expiry = Block
::infinity();
171 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
172 $expiry = strtotime( $this->mExpiry
);
174 if ( $expiry < 0 ||
$expiry === false ) {
175 $this->show( wfMsg( 'protect_expiry_invalid' ) );
179 $expiry = wfTimestamp( TS_MW
, $expiry );
181 if ( $expiry < wfTimestampNow() ) {
182 $this->show( wfMsg( 'protect_expiry_old' ) );
188 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
189 # to a semi-protected page.
190 global $wgGroupPermissions;
192 $edit_restriction = $this->mRestrictions
['edit'];
194 if ($this->mCascade
&& ($edit_restriction != 'protect') &&
195 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
196 $this->mCascade
= false;
198 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
200 throw new FatalError( "Unknown error at restriction save time." );
203 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
204 $this->mArticle
->doWatch();
205 } elseif( $this->mTitle
->userIsWatching() ) {
206 $this->mArticle
->doUnwatch();
212 function buildForm() {
216 if( !$this->disabled
) {
217 $out .= $this->buildScript();
218 // The submission needs to reenable the move permission selector
219 // if it's in locked mode, or some browsers won't submit the data.
220 $out .= wfOpenElement( 'form', array(
221 'id' => 'mw-Protect-Form',
222 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
224 'onsubmit' => 'protectEnable(true)' ) );
226 $out .= wfElement( 'input', array(
228 'name' => 'wpEditToken',
229 'value' => $wgUser->editToken() ) );
232 $out .= "<table id='mwProtectSet'>";
235 foreach( $this->mRestrictions
as $action => $required ) {
236 /* Not all languages have V_x <-> N_x relation */
237 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
241 foreach( $this->mRestrictions
as $action => $selected ) {
243 $out .= $this->buildSelector( $action, $selected );
248 // JavaScript will add another row with a value-chaining checkbox
250 $out .= "</tbody>\n";
251 $out .= "</table>\n";
256 global $wgEnableCascadingProtection;
257 if( $wgEnableCascadingProtection )
258 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
260 $out .= $this->buildExpiryInput();
262 if( !$this->disabled
) {
263 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
264 $out .= "<tr><td></td><td>" . $this->buildWatchInput() . "</td></tr>\n";
265 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
268 $out .= "</tbody>\n";
269 $out .= "</table>\n";
271 if ( !$this->disabled
) {
273 $out .= $this->buildCleanupScript();
279 function buildSelector( $action, $selected ) {
280 global $wgRestrictionLevels;
281 $id = 'mwProtect-level-' . $action;
285 'size' => count( $wgRestrictionLevels ),
286 'onchange' => 'protectLevelsUpdate(this)',
287 ) +
$this->disabledAttrib
;
289 $out = wfOpenElement( 'select', $attribs );
290 foreach( $wgRestrictionLevels as $key ) {
291 $out .= Xml
::option( $this->getOptionLabel( $key ), $key, $key == $selected );
293 $out .= "</select>\n";
298 * Prepare the label for a protection selector option
300 * @param string $permission Permission required
303 private function getOptionLabel( $permission ) {
304 if( $permission == '' ) {
305 return wfMsg( 'protect-default' );
307 $key = "protect-level-{$permission}";
308 $msg = wfMsg( $key );
309 if( wfEmptyMsg( $key, $msg ) )
310 $msg = wfMsg( 'protect-fallback', $permission );
315 function buildReasonInput() {
316 $id = 'mwProtect-reason';
317 return wfElement( 'label', array(
320 wfMsg( 'protectcomment' ) ) .
322 wfElement( 'input', array(
326 'value' => $this->mReason
) );
329 function buildCascadeInput() {
330 $id = 'mwProtect-cascade';
331 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
335 function buildExpiryInput() {
336 $attribs = array( 'id' => 'expires' ) +
$this->disabledAttrib
;
338 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
339 . '<td>' . Xml
::input( 'mwProtect-expiry', 60, $this->mExpiry
, $attribs ) . '</td>'
343 function buildWatchInput() {
345 return Xml
::checkLabel(
346 wfMsg( 'watchthis' ),
349 $this->mTitle
->userIsWatching() ||
$wgUser->getOption( 'watchdefault' )
353 function buildSubmit() {
354 return wfElement( 'input', array(
355 'id' => 'mw-Protect-submit',
357 'value' => wfMsg( 'confirm' ) ) );
360 function buildScript() {
361 global $wgStylePath, $wgStyleVersion;
362 return '<script type="text/javascript" src="' .
363 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
367 function buildCleanupScript() {
368 global $wgRestrictionLevels, $wgGroupPermissions;
369 $script = 'var wgCascadeableLevels=';
370 $CascadeableLevels = array();
371 foreach( $wgRestrictionLevels as $key ) {
372 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) ||
$key == 'protect' ) {
373 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
376 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
377 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
378 return '<script type="text/javascript">' . $script . '</script>';
382 * @param OutputPage $out
385 function showLogExtract( &$out ) {
386 # Show relevant lines from the protection log:
387 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
388 $logViewer = new LogViewer(
391 array( 'page' => $this->mTitle
->getPrefixedText(),
392 'type' => 'protect' ) ) ) );
393 $logViewer->showList( $out );