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 ) );
43 $this->mCascade
= $this->mTitle
->areRestrictionsCascading();
46 // The form will be available in read-only to show levels.
47 $this->disabled
= !$wgUser->isAllowed( 'protect' ) ||
wfReadOnly() ||
$wgUser->isBlocked();
48 $this->disabledAttrib
= $this->disabled
49 ?
array( 'disabled' => 'disabled' )
52 if( $wgRequest->wasPosted() ) {
53 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
54 $this->mCascade
= $wgRequest->getBool( 'mwProtect-cascade' );
55 foreach( $wgRestrictionTypes as $action ) {
56 $val = $wgRequest->getVal( "mwProtect-level-$action" );
57 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
58 $this->mRestrictions
[$action] = $val;
67 $wgOut->setRobotpolicy( 'noindex,nofollow' );
69 if( is_null( $this->mTitle
) ||
70 !$this->mTitle
->exists() ||
71 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
72 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
76 $cascadeSources = $this->mTitle
->getCascadeProtectionSources();
78 if ( count($cascadeSources) > 0 ) {
81 foreach ( $cascadeSources as $title ) {
82 $titles .= '* ' . $title->getPrefixedText() . "\r\n";
85 $wgOut->addWikiText( wfMsgForContent( 'protect-cascadeon', $titles ) );
89 $wgOut->redirect( $this->mTitle
->getFullUrl() );
93 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
94 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
97 wfMsg( $this->disabled ?
"protect-viewtext" : "protect-text",
98 wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
100 $wgOut->addHTML( $this->buildForm() );
102 $this->showLogExtract( $wgOut );
106 global $wgRequest, $wgUser, $wgOut;
107 if( !$wgRequest->wasPosted() ) {
111 if( $this->disabled
) {
115 $token = $wgRequest->getVal( 'wpEditToken' );
116 if( !$wgUser->matchEditToken( $token ) ) {
117 throw new FatalError( wfMsg( 'sessionfailure' ) );
120 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
);
122 throw new FatalError( "Unknown error at restriction save time." );
127 function buildForm() {
131 if( !$this->disabled
) {
132 $out .= $this->buildScript();
133 // The submission needs to reenable the move permission selector
134 // if it's in locked mode, or some browsers won't submit the data.
135 $out .= wfOpenElement( 'form', array(
136 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
138 'onsubmit' => 'protectEnable(true)' ) );
140 $out .= wfElement( 'input', array(
142 'name' => 'wpEditToken',
143 'value' => $wgUser->editToken() ) );
146 $out .= "<table id='mwProtectSet'>";
149 foreach( $this->mRestrictions
as $action => $required ) {
150 /* Not all languages have V_x <-> N_x relation */
151 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
155 foreach( $this->mRestrictions
as $action => $selected ) {
157 $out .= $this->buildSelector( $action, $selected );
162 // JavaScript will add another row with a value-chaining checkbox
164 $out .= "</tbody>\n";
165 $out .= "</table>\n";
167 global $wgEnableCascadingProtection;
169 if ($wgEnableCascadingProtection)
170 $out .= $this->buildCascadeInput();
172 if( !$this->disabled
) {
175 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
176 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
177 $out .= "</tbody>\n";
178 $out .= "</table>\n";
180 $out .= $this->buildCleanupScript();
186 function buildSelector( $action, $selected ) {
187 global $wgRestrictionLevels;
188 $id = 'mwProtect-level-' . $action;
192 'size' => count( $wgRestrictionLevels ),
193 'onchange' => 'protectLevelsUpdate(this)',
194 ) +
$this->disabledAttrib
;
196 $out = wfOpenElement( 'select', $attribs );
197 foreach( $wgRestrictionLevels as $key ) {
198 $out .= $this->buildOption( $key, $selected );
200 $out .= "</select>\n";
204 function buildOption( $key, $selected ) {
205 $text = ( $key == '' )
206 ?
wfMsg( 'protect-default' )
207 : wfMsg( "protect-level-$key" );
208 $selectedAttrib = ($selected == $key)
209 ?
array( 'selected' => 'selected' )
211 return wfElement( 'option',
212 array( 'value' => $key ) +
$selectedAttrib,
216 function buildReasonInput() {
217 $id = 'mwProtect-reason';
218 return wfElement( 'label', array(
221 wfMsg( 'protectcomment' ) ) .
223 wfElement( 'input', array(
229 function buildCascadeInput() {
230 $id = 'mwProtect-cascade';
231 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
236 function buildSubmit() {
237 return wfElement( 'input', array(
239 'value' => wfMsg( 'confirm' ) ) );
242 function buildScript() {
243 global $wgStylePath, $wgStyleVersion;
244 return '<script type="text/javascript" src="' .
245 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
249 function buildCleanupScript() {
250 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
251 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
255 * @param OutputPage $out
258 function showLogExtract( &$out ) {
259 # Show relevant lines from the deletion log:
260 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
261 $logViewer = new LogViewer(
264 array( 'page' => $this->mTitle
->getPrefixedText(),
265 'type' => 'protect' ) ) ) );
266 $logViewer->showList( $out );