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;
29 function __construct( &$article ) {
30 global $wgRequest, $wgUser;
31 global $wgRestrictionTypes, $wgRestrictionLevels;
32 $this->mArticle
=& $article;
33 $this->mTitle
=& $article->mTitle
;
36 foreach( $wgRestrictionTypes as $action ) {
37 // Fixme: this form currently requires individual selections,
38 // but the db allows multiples separated by commas.
39 $this->mRestrictions
[$action] = implode( '', $this->mTitle
->getRestrictions( $action ) );
42 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
45 // The form will be available in read-only to show levels.
46 $this->disabled
= !$wgUser->isAllowed( 'protect' ) ||
wfReadOnly() ||
$wgUser->isBlocked();
47 $this->disabledAttrib
= $this->disabled
48 ?
array( 'disabled' => 'disabled' )
51 if( $wgRequest->wasPosted() ) {
52 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
53 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
54 foreach( $wgRestrictionTypes as $action ) {
55 $val = $wgRequest->getVal( "mwProtect-level-$action" );
56 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
57 $this->mRestrictions
[$action] = $val;
66 $wgOut->setRobotpolicy( 'noindex,nofollow' );
68 if( is_null( $this->mTitle
) ||
69 !$this->mTitle
->exists() ||
70 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
71 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
75 $cascadeSources = $this->mTitle
->getCascadeProtectionSources();
77 if ( $cascadeSources && count($cascadeSources) > 0 ) {
80 foreach ( $cascadeSources as $title ) {
81 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
84 $notice = wfMsg( 'protect-cascadeon' ) . "\r\n$titles";
86 $wgOut->addWikiText( $notice );
90 $wgOut->redirect( $this->mTitle
->getFullUrl() );
94 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
95 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
98 wfMsg( $this->disabled ?
"protect-viewtext" : "protect-text",
99 wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
101 $wgOut->addHTML( $this->buildForm() );
103 $this->showLogExtract( $wgOut );
107 global $wgRequest, $wgUser, $wgOut;
108 if( !$wgRequest->wasPosted() ) {
112 if( $this->disabled
) {
116 $token = $wgRequest->getVal( 'wpEditToken' );
117 if( !$wgUser->matchEditToken( $token ) ) {
118 throw new FatalError( wfMsg( 'sessionfailure' ) );
121 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
);
123 throw new FatalError( "Unknown error at restriction save time." );
128 function buildForm() {
132 if( !$this->disabled
) {
133 $out .= $this->buildScript();
134 // The submission needs to reenable the move permission selector
135 // if it's in locked mode, or some browsers won't submit the data.
136 $out .= wfOpenElement( 'form', array(
137 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
139 'onsubmit' => 'protectEnable(true)' ) );
141 $out .= wfElement( 'input', array(
143 'name' => 'wpEditToken',
144 'value' => $wgUser->editToken() ) );
147 $out .= "<table id='mwProtectSet'>";
150 foreach( $this->mRestrictions
as $action => $required ) {
151 /* Not all languages have V_x <-> N_x relation */
152 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
156 foreach( $this->mRestrictions
as $action => $selected ) {
158 $out .= $this->buildSelector( $action, $selected );
163 // JavaScript will add another row with a value-chaining checkbox
165 $out .= "</tbody>\n";
166 $out .= "</table>\n";
168 global $wgEnableCascadingProtection;
170 if ($wgEnableCascadingProtection)
171 $out .= $this->buildCascadeInput();
173 if( !$this->disabled
) {
176 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
177 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
178 $out .= "</tbody>\n";
179 $out .= "</table>\n";
181 $out .= $this->buildCleanupScript();
187 function buildSelector( $action, $selected ) {
188 global $wgRestrictionLevels;
189 $id = 'mwProtect-level-' . $action;
193 'size' => count( $wgRestrictionLevels ),
194 'onchange' => 'protectLevelsUpdate(this)',
195 ) +
$this->disabledAttrib
;
197 $out = wfOpenElement( 'select', $attribs );
198 foreach( $wgRestrictionLevels as $key ) {
199 $out .= $this->buildOption( $key, $selected );
201 $out .= "</select>\n";
205 function buildOption( $key, $selected ) {
206 $text = ( $key == '' )
207 ?
wfMsg( 'protect-default' )
208 : wfMsg( "protect-level-$key" );
209 $selectedAttrib = ($selected == $key)
210 ?
array( 'selected' => 'selected' )
212 return wfElement( 'option',
213 array( 'value' => $key ) +
$selectedAttrib,
217 function buildReasonInput() {
218 $id = 'mwProtect-reason';
219 return wfElement( 'label', array(
222 wfMsg( 'protectcomment' ) ) .
224 wfElement( 'input', array(
230 function buildCascadeInput() {
231 $id = 'mwProtect-cascade';
232 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
237 function buildSubmit() {
238 return wfElement( 'input', array(
240 'value' => wfMsg( 'confirm' ) ) );
243 function buildScript() {
244 global $wgStylePath, $wgStyleVersion;
245 return '<script type="text/javascript" src="' .
246 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
250 function buildCleanupScript() {
251 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
252 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
256 * @param OutputPage $out
259 function showLogExtract( &$out ) {
260 # Show relevant lines from the deletion log:
261 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
262 $logViewer = new LogViewer(
265 array( 'page' => $this->mTitle
->getPrefixedText(),
266 'type' => 'protect' ) ) ) );
267 $logViewer->showList( $out );