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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @subpackage SpecialPage
25 class ProtectionForm
{
26 var $mRestrictions = array();
29 function ProtectionForm( &$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 ) );
43 // The form will be available in read-only to show levels.
44 $this->disabled
= !$wgUser->isAllowed( 'protect' ) ||
wfReadOnly() ||
$wgUser->isBlocked();
45 $this->disabledAttrib
= $this->disabled
46 ?
array( 'disabled' => 'disabled' )
49 if( $wgRequest->wasPosted() ) {
50 $this->mReason
= $wgRequest->getText( 'mwProtect-reason' );
51 foreach( $wgRestrictionTypes as $action ) {
52 $val = $wgRequest->getVal( "mwProtect-level-$action" );
53 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
54 $this->mRestrictions
[$action] = $val;
63 $wgOut->setRobotpolicy( 'noindex,nofollow' );
65 if( is_null( $this->mTitle
) ||
66 !$this->mTitle
->exists() ||
67 $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
68 $wgOut->fatalError( wfMsg( 'badarticleerror' ) );
73 $wgOut->redirect( $this->mTitle
->getFullUrl() );
77 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
78 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle
->getPrefixedText() ) );
81 wfMsg( $this->disabled ?
"protect-viewtext" : "protect-text",
82 $this->mTitle
->getPrefixedText() ) );
84 $wgOut->addHTML( $this->buildForm() );
86 $this->showLogExtract( $wgOut );
90 global $wgRequest, $wgUser, $wgOut;
91 if( !$wgRequest->wasPosted() ) {
95 if( $this->disabled
) {
99 $token = $wgRequest->getVal( 'wpEditToken' );
100 if( !$wgUser->matchEditToken( $token ) ) {
101 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
105 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
);
107 $wgOut->fatalError( "Unknown error at restriction save time." );
112 function buildForm() {
116 if( !$this->disabled
) {
117 $out .= $this->buildScript();
118 // The submission needs to reenable the move permission selector
119 // if it's in locked mode, or some browsers won't submit the data.
120 $out .= wfOpenElement( 'form', array(
121 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
123 'onsubmit' => 'protectEnable(true)' ) );
125 $out .= wfElement( 'input', array(
127 'name' => 'wpEditToken',
128 'value' => $wgUser->editToken() ) );
131 $out .= "<table id='mwProtectSet'>";
134 foreach( $this->mRestrictions
as $action => $required ) {
135 /* Not all languages have V_x <-> N_x relation */
136 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
140 foreach( $this->mRestrictions
as $action => $selected ) {
142 $out .= $this->buildSelector( $action, $selected );
147 // JavaScript will add another row with a value-chaining checkbox
149 $out .= "</tbody>\n";
150 $out .= "</table>\n";
152 if( !$this->disabled
) {
155 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
156 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
157 $out .= "</tbody>\n";
158 $out .= "</table>\n";
160 $out .= $this->buildCleanupScript();
166 function buildSelector( $action, $selected ) {
167 global $wgRestrictionLevels;
168 $id = 'mwProtect-level-' . $action;
172 'size' => count( $wgRestrictionLevels ),
173 'onchange' => 'protectLevelsUpdate(this)',
174 ) +
$this->disabledAttrib
;
176 $out = wfOpenElement( 'select', $attribs );
177 foreach( $wgRestrictionLevels as $key ) {
178 $out .= $this->buildOption( $key, $selected );
180 $out .= "</select>\n";
184 function buildOption( $key, $selected ) {
185 $text = ( $key == '' )
186 ?
wfMsg( 'protect-default' )
187 : wfMsg( "protect-level-$key" );
188 $selectedAttrib = ($selected == $key)
189 ?
array( 'selected' => 'selected' )
191 return wfElement( 'option',
192 array( 'value' => $key ) +
$selectedAttrib,
196 function buildReasonInput() {
197 $id = 'mwProtect-reason';
198 return wfElement( 'label', array(
201 wfMsg( 'protectcomment' ) ) .
203 wfElement( 'input', array(
209 function buildSubmit() {
210 return wfElement( 'input', array(
212 'value' => wfMsg( 'confirm' ) ) );
215 function buildScript() {
217 return '<script type="text/javascript" src="' .
218 htmlspecialchars( $wgStylePath . "/common/protect.js" ) .
222 function buildCleanupScript() {
223 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
224 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
228 * @param OutputPage $out
231 function showLogExtract( &$out ) {
232 # Show relevant lines from the deletion log:
233 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
234 require_once( 'SpecialLog.php' );
235 $logViewer = new LogViewer(
238 array( 'page' => $this->mTitle
->getPrefixedText(),
239 'type' => 'protect' ) ) ) );
240 $logViewer->showList( $out );