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;
80 if( $wgRequest->wasPosted() ) {
83 $wgOut->redirect( $this->mTitle
->getFullUrl() );
90 function show( $err = null ) {
91 global $wgOut, $wgUser;
93 $wgOut->setRobotpolicy( 'noindex,nofollow' );
95 if( is_null( $this->mTitle
) ||
96 !$this->mTitle
->exists() ||
97 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
98 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
102 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle
->getCascadeProtectionSources();
105 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
106 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
109 if ( $cascadeSources && count($cascadeSources) > 0 ) {
112 foreach ( $cascadeSources as $title ) {
113 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
116 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
118 $wgOut->addWikiText( $notice );
121 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
122 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
124 # Show an appropriate message if the user isn't allowed or able to change
125 # the protection settings at this time
126 if( $this->disabled
) {
127 if( $wgUser->isAllowed( 'protect' ) ) {
128 if( $wgUser->isBlocked() ) {
130 $message = 'protect-locked-blocked';
133 $message = 'protect-locked-dblock';
137 $message = 'protect-locked-access';
140 $message = 'protect-text';
142 $wgOut->addWikiText( wfMsg( $message, wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
144 $wgOut->addHTML( $this->buildForm() );
146 $this->showLogExtract( $wgOut );
150 global $wgRequest, $wgUser, $wgOut;
152 if( $this->disabled
) {
157 $token = $wgRequest->getVal( 'wpEditToken' );
158 if( !$wgUser->matchEditToken( $token ) ) {
159 $this->show( wfMsg( 'sessionfailure' ) );
163 if ( strlen( $this->mExpiry
) == 0 ) {
164 $this->mExpiry
= 'infinite';
167 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
168 $expiry = Block
::infinity();
170 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
171 $expiry = strtotime( $this->mExpiry
);
173 if ( $expiry < 0 ||
$expiry === false ) {
174 $this->show( wfMsg( 'protect_expiry_invalid' ) );
178 $expiry = wfTimestamp( TS_MW
, $expiry );
180 if ( $expiry < wfTimestampNow() ) {
181 $this->show( wfMsg( 'protect_expiry_old' ) );
187 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
189 throw new FatalError( "Unknown error at restriction save time." );
194 function buildForm() {
198 if( !$this->disabled
) {
199 $out .= $this->buildScript();
200 // The submission needs to reenable the move permission selector
201 // if it's in locked mode, or some browsers won't submit the data.
202 $out .= wfOpenElement( 'form', array(
203 'id' => 'mw-Protect-Form',
204 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
206 'onsubmit' => 'protectEnable(true)' ) );
208 $out .= wfElement( 'input', array(
210 'name' => 'wpEditToken',
211 'value' => $wgUser->editToken() ) );
214 $out .= "<table id='mwProtectSet'>";
217 foreach( $this->mRestrictions
as $action => $required ) {
218 /* Not all languages have V_x <-> N_x relation */
219 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
223 foreach( $this->mRestrictions
as $action => $selected ) {
225 $out .= $this->buildSelector( $action, $selected );
230 // JavaScript will add another row with a value-chaining checkbox
232 $out .= "</tbody>\n";
233 $out .= "</table>\n";
235 global $wgEnableCascadingProtection;
237 if ($wgEnableCascadingProtection)
238 $out .= $this->buildCascadeInput();
243 $out .= $this->buildExpiryInput();
245 if( !$this->disabled
) {
246 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
247 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
250 $out .= "</tbody>\n";
251 $out .= "</table>\n";
253 if ( !$this->disabled
) {
255 $out .= $this->buildCleanupScript();
261 function buildSelector( $action, $selected ) {
262 global $wgRestrictionLevels;
263 $id = 'mwProtect-level-' . $action;
267 'size' => count( $wgRestrictionLevels ),
268 'onchange' => 'protectLevelsUpdate(this)',
269 ) +
$this->disabledAttrib
;
271 $out = wfOpenElement( 'select', $attribs );
272 foreach( $wgRestrictionLevels as $key ) {
273 $out .= $this->buildOption( $key, $selected );
275 $out .= "</select>\n";
279 function buildOption( $key, $selected ) {
280 $text = ( $key == '' )
281 ?
wfMsg( 'protect-default' )
282 : wfMsg( "protect-level-$key" );
283 $selectedAttrib = ($selected == $key)
284 ?
array( 'selected' => 'selected' )
286 return wfElement( 'option',
287 array( 'value' => $key ) +
$selectedAttrib,
291 function buildReasonInput() {
292 $id = 'mwProtect-reason';
293 return wfElement( 'label', array(
296 wfMsg( 'protectcomment' ) ) .
298 wfElement( 'input', array(
302 'value' => $this->mReason
) );
305 function buildCascadeInput() {
306 $id = 'mwProtect-cascade';
307 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
311 function buildExpiryInput() {
312 $id = 'mwProtect-expiry';
314 $ci = "<tr> <td align=\"right\">";
315 $ci .= wfElement( 'label', array (
318 wfMsg( 'protectexpiry' ) );
319 $ci .= "</td> <td align=\"left\">";
320 $ci .= wfElement( 'input', array(
324 'value' => $this->mExpiry
) +
$this->disabledAttrib
);
330 function buildSubmit() {
331 return wfElement( 'input', array(
332 'id' => 'mw-Protect-submit',
334 'value' => wfMsg( 'confirm' ) ) );
337 function buildScript() {
338 global $wgStylePath, $wgStyleVersion;
339 return '<script type="text/javascript" src="' .
340 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
344 function buildCleanupScript() {
345 global $wgRestrictionLevels, $wgGroupPermissions;
346 $script = 'var wgCascadeableLevels=';
347 $CascadeableLevels = array();
348 foreach( $wgRestrictionLevels as $key ) {
349 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
350 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
353 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
354 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
355 return '<script type="text/javascript">' . $script . '</script>';
359 * @param OutputPage $out
362 function showLogExtract( &$out ) {
363 # Show relevant lines from the deletion log:
364 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
365 $logViewer = new LogViewer(
368 array( 'page' => $this->mTitle
->getPrefixedText(),
369 'type' => 'protect' ) ) ) );
370 $logViewer->showList( $out );