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();
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->showFatalError( 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 throw new FatalError( wfMsg( 'sessionfailure' ) );
104 $ok = $this->mArticle
->updateRestrictions( $this->mRestrictions
, $this->mReason
);
106 throw new FatalError( "Unknown error at restriction save time." );
111 function buildForm() {
115 if( !$this->disabled
) {
116 $out .= $this->buildScript();
117 // The submission needs to reenable the move permission selector
118 // if it's in locked mode, or some browsers won't submit the data.
119 $out .= wfOpenElement( 'form', array(
120 'action' => $this->mTitle
->getLocalUrl( 'action=protect' ),
122 'onsubmit' => 'protectEnable(true)' ) );
124 $out .= wfElement( 'input', array(
126 'name' => 'wpEditToken',
127 'value' => $wgUser->editToken() ) );
130 $out .= "<table id='mwProtectSet'>";
133 foreach( $this->mRestrictions
as $action => $required ) {
134 /* Not all languages have V_x <-> N_x relation */
135 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
139 foreach( $this->mRestrictions
as $action => $selected ) {
141 $out .= $this->buildSelector( $action, $selected );
146 // JavaScript will add another row with a value-chaining checkbox
148 $out .= "</tbody>\n";
149 $out .= "</table>\n";
151 if( !$this->disabled
) {
154 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
155 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
156 $out .= "</tbody>\n";
157 $out .= "</table>\n";
159 $out .= $this->buildCleanupScript();
165 function buildSelector( $action, $selected ) {
166 global $wgRestrictionLevels;
167 $id = 'mwProtect-level-' . $action;
171 'size' => count( $wgRestrictionLevels ),
172 'onchange' => 'protectLevelsUpdate(this)',
173 ) +
$this->disabledAttrib
;
175 $out = wfOpenElement( 'select', $attribs );
176 foreach( $wgRestrictionLevels as $key ) {
177 $out .= $this->buildOption( $key, $selected );
179 $out .= "</select>\n";
183 function buildOption( $key, $selected ) {
184 $text = ( $key == '' )
185 ?
wfMsg( 'protect-default' )
186 : wfMsg( "protect-level-$key" );
187 $selectedAttrib = ($selected == $key)
188 ?
array( 'selected' => 'selected' )
190 return wfElement( 'option',
191 array( 'value' => $key ) +
$selectedAttrib,
195 function buildReasonInput() {
196 $id = 'mwProtect-reason';
197 return wfElement( 'label', array(
200 wfMsg( 'protectcomment' ) ) .
202 wfElement( 'input', array(
208 function buildSubmit() {
209 return wfElement( 'input', array(
211 'value' => wfMsg( 'confirm' ) ) );
214 function buildScript() {
216 return '<script type="text/javascript" src="' .
217 htmlspecialchars( $wgStylePath . "/common/protect.js" ) .
221 function buildCleanupScript() {
222 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
223 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
227 * @param OutputPage $out
230 function showLogExtract( &$out ) {
231 # Show relevant lines from the deletion log:
232 $out->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'protect' ) ) . "</h2>\n" );
233 require_once( 'SpecialLog.php' );
234 $logViewer = new LogViewer(
237 array( 'page' => $this->mTitle
->getPrefixedText(),
238 'type' => 'protect' ) ) ) );
239 $logViewer->showList( $out );