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 ( $cascadeSources && count($cascadeSources) > 0 ) {
81 foreach ( $cascadeSources as $title ) {
82 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
85 $notice = wfMsg( 'protect-cascadeon' ) . "\r\n$titles";
87 $wgOut->addWikiText( $notice );
91 $wgOut->redirect( $this->mTitle
->getFullUrl() );
95 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
96 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
99 wfMsg( $this->disabled ?
"protect-viewtext" : "protect-text",
100 wfEscapeWikiText( $this->mTitle
->getPrefixedText() ) ) );
102 $wgOut->addHTML( $this->buildForm() );
104 $this->showLogExtract( $wgOut );
108 global $wgRequest, $wgUser, $wgOut;
109 if( !$wgRequest->wasPosted() ) {
113 if( $this->disabled
) {
117 $token = $wgRequest->getVal( 'wpEditToken' );
118 if( !$wgUser->matchEditToken( $token ) ) {
119 throw new FatalError( wfMsg( 'sessionfailure' ) );
122 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
, $this->mCascade
);
124 throw new FatalError( "Unknown error at restriction save time." );
129 function buildForm() {
133 if( !$this->disabled
) {
134 $out .= $this->buildScript();
135 // The submission needs to reenable the move permission selector
136 // if it's in locked mode, or some browsers won't submit the data.
137 $out .= wfOpenElement( 'form', array(
138 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
140 'onsubmit' => 'protectEnable(true)' ) );
142 $out .= wfElement( 'input', array(
144 'name' => 'wpEditToken',
145 'value' => $wgUser->editToken() ) );
148 $out .= "<table id='mwProtectSet'>";
151 foreach( $this->mRestrictions
as $action => $required ) {
152 /* Not all languages have V_x <-> N_x relation */
153 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
157 foreach( $this->mRestrictions
as $action => $selected ) {
159 $out .= $this->buildSelector( $action, $selected );
164 // JavaScript will add another row with a value-chaining checkbox
166 $out .= "</tbody>\n";
167 $out .= "</table>\n";
169 global $wgEnableCascadingProtection;
171 if ($wgEnableCascadingProtection)
172 $out .= $this->buildCascadeInput();
174 if( !$this->disabled
) {
177 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
178 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
179 $out .= "</tbody>\n";
180 $out .= "</table>\n";
182 $out .= $this->buildCleanupScript();
188 function buildSelector( $action, $selected ) {
189 global $wgRestrictionLevels;
190 $id = 'mwProtect-level-' . $action;
194 'size' => count( $wgRestrictionLevels ),
195 'onchange' => 'protectLevelsUpdate(this)',
196 ) +
$this->disabledAttrib
;
198 $out = wfOpenElement( 'select', $attribs );
199 foreach( $wgRestrictionLevels as $key ) {
200 $out .= $this->buildOption( $key, $selected );
202 $out .= "</select>\n";
206 function buildOption( $key, $selected ) {
207 $text = ( $key == '' )
208 ?
wfMsg( 'protect-default' )
209 : wfMsg( "protect-level-$key" );
210 $selectedAttrib = ($selected == $key)
211 ?
array( 'selected' => 'selected' )
213 return wfElement( 'option',
214 array( 'value' => $key ) +
$selectedAttrib,
218 function buildReasonInput() {
219 $id = 'mwProtect-reason';
220 return wfElement( 'label', array(
223 wfMsg( 'protectcomment' ) ) .
225 wfElement( 'input', array(
231 function buildCascadeInput() {
232 $id = 'mwProtect-cascade';
233 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade
, $this->disabledAttrib
);
238 function buildSubmit() {
239 return wfElement( 'input', array(
241 'value' => wfMsg( 'confirm' ) ) );
244 function buildScript() {
245 global $wgStylePath, $wgStyleVersion;
246 return '<script type="text/javascript" src="' .
247 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
251 function buildCleanupScript() {
252 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
253 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
257 * @param OutputPage $out
260 function showLogExtract( &$out ) {
261 # Show relevant lines from the deletion log:
262 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
263 $logViewer = new LogViewer(
266 array( 'page' => $this->mTitle
->getPrefixedText(),
267 'type' => 'protect' ) ) ) );
268 $logViewer->showList( $out );