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
22 * @subpackage SpecialPage
25 class ProtectionForm
{
26 var $mRestrictions = array();
28 var $mCascade = false;
30 function ProtectionForm( &$article ) {
31 global $wgRequest, $wgUser;
32 global $wgRestrictionTypes, $wgRestrictionLevels;
33 $this->mArticle
=& $article;
34 $this->mTitle
=& $article->mTitle
;
37 foreach( $wgRestrictionTypes as $action ) {
38 // Fixme: this form currently requires individual selections,
39 // but the db allows multiples separated by commas.
40 $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' ) );
76 $wgOut->redirect( $this->mTitle
->getFullUrl() );
80 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
81 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
84 wfMsg( $this->disabled ?
"protect-viewtext" : "protect-text",
85 wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
87 $wgOut->addHTML( $this->buildForm() );
89 $this->showLogExtract( $wgOut );
93 global $wgRequest, $wgUser, $wgOut;
94 if( !$wgRequest->wasPosted() ) {
98 if( $this->disabled
) {
102 $token = $wgRequest->getVal( 'wpEditToken' );
103 if( !$wgUser->matchEditToken( $token ) ) {
104 throw new FatalError( wfMsg( 'sessionfailure' ) );
107 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
);
109 throw new FatalError( "Unknown error at restriction save time." );
114 function buildForm() {
118 if( !$this->disabled
) {
119 $out .= $this->buildScript();
120 // The submission needs to reenable the move permission selector
121 // if it's in locked mode, or some browsers won't submit the data.
122 $out .= wfOpenElement( 'form', array(
123 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
125 'onsubmit' => 'protectEnable(true)' ) );
127 $out .= wfElement( 'input', array(
129 'name' => 'wpEditToken',
130 'value' => $wgUser->editToken() ) );
133 $out .= "<table id='mwProtectSet'>";
136 foreach( $this->mRestrictions
as $action => $required ) {
137 /* Not all languages have V_x <-> N_x relation */
138 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
142 foreach( $this->mRestrictions
as $action => $selected ) {
144 $out .= $this->buildSelector( $action, $selected );
149 // JavaScript will add another row with a value-chaining checkbox
151 $out .= "</tbody>\n";
152 $out .= "</table>\n";
154 global $wgEnableCascadingProtection;
156 if ($wgEnableCascadingProtection)
157 $out .= $this->buildCascadeInput();
159 if( !$this->disabled
) {
162 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
163 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
164 $out .= "</tbody>\n";
165 $out .= "</table>\n";
167 $out .= $this->buildCleanupScript();
173 function buildSelector( $action, $selected ) {
174 global $wgRestrictionLevels;
175 $id = 'mwProtect-level-' . $action;
179 'size' => count( $wgRestrictionLevels ),
180 'onchange' => 'protectLevelsUpdate(this)',
181 ) +
$this->disabledAttrib
;
183 $out = wfOpenElement( 'select', $attribs );
184 foreach( $wgRestrictionLevels as $key ) {
185 $out .= $this->buildOption( $key, $selected );
187 $out .= "</select>\n";
191 function buildOption( $key, $selected ) {
192 $text = ( $key == '' )
193 ?
wfMsg( 'protect-default' )
194 : wfMsg( "protect-level-$key" );
195 $selectedAttrib = ($selected == $key)
196 ?
array( 'selected' => 'selected' )
198 return wfElement( 'option',
199 array( 'value' => $key ) +
$selectedAttrib,
203 function buildReasonInput() {
204 $id = 'mwProtect-reason';
205 return wfElement( 'label', array(
208 wfMsg( 'protectcomment' ) ) .
210 wfElement( 'input', array(
216 function buildCascadeInput() {
217 $id = 'mwProtect-cascade';
218 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
223 function buildSubmit() {
224 return wfElement( 'input', array(
226 'value' => wfMsg( 'confirm' ) ) );
229 function buildScript() {
230 global $wgStylePath, $wgStyleVersion;
231 return '<script type="text/javascript" src="' .
232 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
236 function buildCleanupScript() {
237 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
238 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
242 * @param OutputPage $out
245 function showLogExtract( &$out ) {
246 # Show relevant lines from the deletion log:
247 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
248 $logViewer = new LogViewer(
251 array( 'page' => $this->mTitle
->getPrefixedText(),
252 'type' => 'protect' ) ) ) );
253 $logViewer->showList( $out );