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
24 class ProtectionForm
{
25 var $mRestrictions = array();
27 var $mCascade = false;
30 function __construct( &$article ) {
31 global $wgRequest, $wgUser;
32 global $wgRestrictionTypes, $wgRestrictionLevels;
33 $this->mArticle
=& $article;
34 $this->mTitle
=& $article->mTitle
;
37 $this->mTitle
->loadRestrictions();
39 foreach( $wgRestrictionTypes as $action ) {
40 // Fixme: this form currently requires individual selections,
41 // but the db allows multiples separated by commas.
42 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
45 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
47 if ( $this->mTitle
->mRestrictionsExpiry
== 'infinity' ) {
48 $this->mExpiry
= 'infinite';
49 } else if ( strlen($this->mTitle
->mRestrictionsExpiry
) == 0 ) {
52 $this->mExpiry
= wfTimestamp( TS_RFC2822
, $this->mTitle
->mRestrictionsExpiry
);
56 // The form will be available in read-only to show levels.
57 $this->disabled
= !$wgUser->isAllowed( 'protect' ) ||
wfReadOnly() ||
$wgUser->isBlocked();
58 $this->disabledAttrib
= $this->disabled
59 ?
array( 'disabled' => 'disabled' )
62 if( $wgRequest->wasPosted() ) {
63 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
64 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
65 $this->mExpiry
= $wgRequest->getText( 'mwProtect-expiry' );
67 foreach( $wgRestrictionTypes as $action ) {
68 $val = $wgRequest->getVal( "mwProtect-level-$action" );
69 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
70 $this->mRestrictions
[$action] = $val;
78 if( $wgRequest->wasPosted() ) {
81 $wgOut->redirect( $this->mTitle
->getFullUrl() );
88 function show( $err = null ) {
91 $wgOut->setRobotpolicy( 'noindex,nofollow' );
93 if( is_null( $this->mTitle
) ||
94 !$this->mTitle
->exists() ||
95 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
96 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
100 $cascadeSources = $this->mTitle
->getCascadeProtectionSources();
103 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
104 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
107 if ( $cascadeSources && count($cascadeSources) > 0 ) {
110 foreach ( $cascadeSources as $title ) {
111 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
114 $notice = wfMsg( 'protect-cascadeon' ) . "\r\n$titles";
116 $wgOut->addWikiText( $notice );
119 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
120 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
123 wfMsg( $this->disabled ?
"protect-viewtext" : "protect-text",
124 wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
126 $wgOut->addHTML( $this->buildForm() );
128 $this->showLogExtract( $wgOut );
132 global $wgRequest, $wgUser, $wgOut;
134 if( $this->disabled
) {
139 $token = $wgRequest->getVal( 'wpEditToken' );
140 if( !$wgUser->matchEditToken( $token ) ) {
141 $this->show( wfMsg( 'sessionfailure' ) );
145 if ( strlen( $this->mExpiry
) == 0 ) {
146 $this->mExpiry
= 'infinite';
149 if ( $this->mExpiry
== 'infinite' ||
$this->mExpiry
== 'indefinite' ) {
150 $expiry = Block
::infinity();
152 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
153 $expiry = strtotime( $this->mExpiry
);
155 if ( $expiry < 0 ||
$expiry === false ) {
156 $this->show( wfMsg( 'protect_expiry_invalid' ) );
160 $expiry = wfTimestamp( TS_MW
, $expiry );
163 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
, $expiry );
165 throw new FatalError( "Unknown error at restriction save time." );
170 function buildForm() {
174 if( !$this->disabled
) {
175 $out .= $this->buildScript();
176 // The submission needs to reenable the move permission selector
177 // if it's in locked mode, or some browsers won't submit the data.
178 $out .= wfOpenElement( 'form', array(
179 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
181 'onsubmit' => 'protectEnable(true)' ) );
183 $out .= wfElement( 'input', array(
185 'name' => 'wpEditToken',
186 'value' => $wgUser->editToken() ) );
189 $out .= "<table id='mwProtectSet'>";
192 foreach( $this->mRestrictions
as $action => $required ) {
193 /* Not all languages have V_x <-> N_x relation */
194 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
198 foreach( $this->mRestrictions
as $action => $selected ) {
200 $out .= $this->buildSelector( $action, $selected );
205 // JavaScript will add another row with a value-chaining checkbox
207 $out .= "</tbody>\n";
208 $out .= "</table>\n";
213 global $wgEnableCascadingProtection;
215 if ($wgEnableCascadingProtection)
216 $out .= $this->buildCascadeInput();
218 $out .= $this->buildExpiryInput();
220 if( !$this->disabled
) {
221 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
222 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
225 $out .= "</tbody>\n";
226 $out .= "</table>\n";
228 if ( !$this->disabled
) {
230 $out .= $this->buildCleanupScript();
236 function buildSelector( $action, $selected ) {
237 global $wgRestrictionLevels;
238 $id = 'mwProtect-level-' . $action;
242 'size' => count( $wgRestrictionLevels ),
243 'onchange' => 'protectLevelsUpdate(this)',
244 ) +
$this->disabledAttrib
;
246 $out = wfOpenElement( 'select', $attribs );
247 foreach( $wgRestrictionLevels as $key ) {
248 $out .= $this->buildOption( $key, $selected );
250 $out .= "</select>\n";
254 function buildOption( $key, $selected ) {
255 $text = ( $key == '' )
256 ?
wfMsg( 'protect-default' )
257 : wfMsg( "protect-level-$key" );
258 $selectedAttrib = ($selected == $key)
259 ?
array( 'selected' => 'selected' )
261 return wfElement( 'option',
262 array( 'value' => $key ) +
$selectedAttrib,
266 function buildReasonInput() {
267 $id = 'mwProtect-reason';
268 return wfElement( 'label', array(
271 wfMsg( 'protectcomment' ) ) .
273 wfElement( 'input', array(
279 function buildCascadeInput() {
280 $id = 'mwProtect-cascade';
281 $ci = "<tr>" . wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
) . "</tr>";
286 function buildExpiryInput() {
287 $id = 'mwProtect-expiry';
289 $ci = "<tr> <td align=\"right\">";
290 $ci .= wfElement( 'label', array (
293 wfMsg( 'protectexpiry' ) );
294 $ci .= "</td> <td aligh=\"left\">";
295 $ci .= wfElement( 'input', array(
299 'value' => $this->mExpiry
) +
$this->disabledAttrib
);
305 function buildSubmit() {
306 return wfElement( 'input', array(
308 'value' => wfMsg( 'confirm' ) ) );
311 function buildScript() {
312 global $wgStylePath, $wgStyleVersion;
313 return '<script type="text/javascript" src="' .
314 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
318 function buildCleanupScript() {
319 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
320 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
324 * @param OutputPage $out
327 function showLogExtract( &$out ) {
328 # Show relevant lines from the deletion log:
329 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
330 $logViewer = new LogViewer(
333 array( 'page' => $this->mTitle
->getPrefixedText(),
334 'type' => 'protect' ) ) ) );
335 $logViewer->showList( $out );