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
21 * @addtogroup SpecialPage
25 * @todo document, briefly.
27 class ProtectionForm
{
28 var $mRestrictions = array();
30 var $mCascade = false;
33 function __construct( &$article ) {
34 global $wgRequest, $wgUser;
35 global $wgRestrictionTypes, $wgRestrictionLevels;
36 $this->mArticle
=& $article;
37 $this->mTitle
=& $article->mTitle
;
40 $this->mTitle
->loadRestrictions();
42 foreach( $wgRestrictionTypes as $action ) {
43 // Fixme: this form currently requires individual selections,
44 // but the db allows multiples separated by commas.
45 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
48 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
50 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
51 $this->mExpiry
= 'infinite';
52 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
55 $this->mExpiry
= wfTimestamp( TS_RFC2822
, $this->mTitle
->mRestrictionsExpiry
);
59 // The form will be available in read-only to show levels.
60 $this->disabled
= !$wgUser->isAllowed( 'protect' ) ||
wfReadOnly() ||
$wgUser->isBlocked();
61 $this->disabledAttrib
= $this->disabled
62 ?
array( 'disabled' => 'disabled' )
65 if( $wgRequest->wasPosted() ) {
66 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
67 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
68 $this->mExpiry
= $wgRequest->getText( 'mwProtect-expiry' );
70 foreach( $wgRestrictionTypes as $action ) {
71 $val = $wgRequest->getVal( "mwProtect-level-$action" );
72 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
73 $this->mRestrictions
[$action] = $val;
81 if( $wgRequest->wasPosted() ) {
84 $wgOut->redirect( $this->mTitle
->getFullUrl() );
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 = wfMsg( 'protect-cascadeon' ) . "\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 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
190 throw new FatalError( "Unknown error at restriction save time." );
195 function buildForm() {
199 if( !$this->disabled
) {
200 $out .= $this->buildScript();
201 // The submission needs to reenable the move permission selector
202 // if it's in locked mode, or some browsers won't submit the data.
203 $out .= wfOpenElement( 'form', array(
204 'id' => 'mw-Protect-Form',
205 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
207 'onsubmit' => 'protectEnable(true)' ) );
209 $out .= wfElement( 'input', array(
211 'name' => 'wpEditToken',
212 'value' => $wgUser->editToken() ) );
215 $out .= "<table id='mwProtectSet'>";
218 foreach( $this->mRestrictions
as $action => $required ) {
219 /* Not all languages have V_x <-> N_x relation */
220 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
224 foreach( $this->mRestrictions
as $action => $selected ) {
226 $out .= $this->buildSelector( $action, $selected );
231 // JavaScript will add another row with a value-chaining checkbox
233 $out .= "</tbody>\n";
234 $out .= "</table>\n";
236 global $wgEnableCascadingProtection;
238 if ($wgEnableCascadingProtection)
239 $out .= $this->buildCascadeInput();
244 $out .= $this->buildExpiryInput();
246 if( !$this->disabled
) {
247 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
248 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
251 $out .= "</tbody>\n";
252 $out .= "</table>\n";
254 if ( !$this->disabled
) {
256 $out .= $this->buildCleanupScript();
262 function buildSelector( $action, $selected ) {
263 global $wgRestrictionLevels;
264 $id = 'mwProtect-level-' . $action;
268 'size' => count( $wgRestrictionLevels ),
269 'onchange' => 'protectLevelsUpdate(this)',
270 ) +
$this->disabledAttrib
;
272 $out = wfOpenElement( 'select', $attribs );
273 foreach( $wgRestrictionLevels as $key ) {
274 $out .= $this->buildOption( $key, $selected );
276 $out .= "</select>\n";
280 function buildOption( $key, $selected ) {
281 $text = ( $key == '' )
282 ?
wfMsg( 'protect-default' )
283 : wfMsg( "protect-level-$key" );
284 $selectedAttrib = ($selected == $key)
285 ?
array( 'selected' => 'selected' )
287 return wfElement( 'option',
288 array( 'value' => $key ) +
$selectedAttrib,
292 function buildReasonInput() {
293 $id = 'mwProtect-reason';
294 return wfElement( 'label', array(
297 wfMsg( 'protectcomment' ) ) .
299 wfElement( 'input', array(
303 'value' => $this->mReason
) );
306 function buildCascadeInput() {
307 $id = 'mwProtect-cascade';
308 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
312 function buildExpiryInput() {
313 $id = 'mwProtect-expiry';
315 $ci = "<tr> <td align=\"right\">";
316 $ci .= wfElement( 'label', array (
319 wfMsg( 'protectexpiry' ) );
320 $ci .= "</td> <td align=\"left\">";
321 $ci .= wfElement( 'input', array(
325 'value' => $this->mExpiry
) +
$this->disabledAttrib
);
331 function buildSubmit() {
332 return wfElement( 'input', array(
333 'id' => 'mw-Protect-submit',
335 'value' => wfMsg( 'confirm' ) ) );
338 function buildScript() {
339 global $wgStylePath, $wgStyleVersion;
340 return '<script type="text/javascript" src="' .
341 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
345 function buildCleanupScript() {
346 global $wgRestrictionLevels, $wgGroupPermissions;
347 $script = 'var wgCascadeableLevels=';
348 $CascadeableLevels = array();
349 foreach( $wgRestrictionLevels as $key ) {
350 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
351 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
354 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
355 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
356 return '<script type="text/javascript">' . $script . '</script>';
360 * @param OutputPage $out
363 function showLogExtract( &$out ) {
364 # Show relevant lines from the deletion log:
365 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
366 $logViewer = new LogViewer(
369 array( 'page' => $this->mTitle
->getPrefixedText(),
370 'type' => 'protect' ) ) ) );
371 $logViewer->showList( $out );