Fix #2571: Duplicate anchors and missing form id tag in search form on Cologne Blue...
[mediawiki.git] / includes / ProtectionForm.php
blob3f7a0fdfd0c0ae198178d4e2bd0d4e2443637bf7
1 <?php
2 /**
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
21 * @package MediaWiki
22 * @subpackage SpecialPage
25 class ProtectionForm {
26 var $mRestrictions = array();
27 var $mReason = '';
29 function ProtectionForm( &$article ) {
30 global $wgRequest, $wgUser;
31 global $wgRestrictionTypes, $wgRestrictionLevels;
32 $this->mArticle =& $article;
33 $this->mTitle =& $article->mTitle;
35 if( $this->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' )
47 : array();
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;
60 function show() {
61 global $wgOut;
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' ) );
69 return;
72 if( $this->save() ) {
73 $wgOut->redirect( $this->mTitle->getFullUrl() );
74 return;
77 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
78 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
80 $wgOut->addWikiText(
81 wfMsg( $this->disabled ? "protect-viewtext" : "protect-text",
82 $this->mTitle->getPrefixedText() ) );
84 $wgOut->addHTML( $this->buildForm() );
86 $this->showLogExtract( $wgOut );
89 function save() {
90 global $wgRequest, $wgUser;
91 if( !$wgRequest->wasPosted() ) {
92 return false;
95 if( $this->disabled ) {
96 return false;
99 $token = $wgRequest->getVal( 'wpEditToken' );
100 if( !$wgUser->matchEditToken( $token ) ) {
101 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
102 return false;
105 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason );
106 if( !$ok ) {
107 $wgOut->fatalError( "Unknown error at restriction save time." );
109 return $ok;
112 function buildForm() {
113 global $wgUser;
115 $out = '';
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' ),
122 'method' => 'post',
123 'onsubmit' => 'protectEnable(true)' ) );
125 $out .= wfElement( 'input', array(
126 'type' => 'hidden',
127 'name' => 'wpEditToken',
128 'value' => $wgUser->editToken() ) );
131 $out .= "<table id='mwProtectSet'>";
132 $out .= "<tbody>";
133 $out .= "<tr>\n";
134 foreach( $this->mRestrictions as $action => $required ) {
135 $out .= "<th>" . wfMsgHtml( $action ) . "</th>\n";
137 $out .= "</tr>\n";
138 $out .= "<tr>\n";
139 foreach( $this->mRestrictions as $action => $selected ) {
140 $out .= "<td>\n";
141 $out .= $this->buildSelector( $action, $selected );
142 $out .= "</td>\n";
144 $out .= "</tr>\n";
146 // JavaScript will add another row with a value-chaining checkbox
148 $out .= "</tbody>\n";
149 $out .= "</table>\n";
151 if( !$this->disabled ) {
152 $out .= "<table>\n";
153 $out .= "<tbody>\n";
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";
158 $out .= "</form>\n";
159 $out .= $this->buildCleanupScript();
162 return $out;
165 function buildSelector( $action, $selected ) {
166 global $wgRestrictionLevels;
167 $id = 'mwProtect-level-' . $action;
168 $attribs = array(
169 'id' => $id,
170 'name' => $id,
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";
180 return $out;
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' )
189 : array();
190 return wfElement( 'option',
191 array( 'value' => $key ) + $selectedAttrib,
192 $text );
195 function buildReasonInput() {
196 $id = 'mwProtect-reason';
197 return wfElement( 'label', array(
198 'id' => "$id-label",
199 'for' => $id ),
200 wfMsg( 'protectcomment' ) ) .
201 '</td><td>' .
202 wfElement( 'input', array(
203 'size' => 60,
204 'name' => $id,
205 'id' => $id ) );
208 function buildSubmit() {
209 return wfElement( 'input', array(
210 'type' => 'submit',
211 'value' => wfMsg( 'confirm' ) ) );
214 function buildScript() {
215 global $wgStylePath;
216 return '<script type="text/javascript" src="' .
217 htmlspecialchars( $wgStylePath . "/common/protect.js" ) .
218 '"></script>';
221 function buildCleanupScript() {
222 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
223 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
227 * @param OutputPage $out
228 * @access private
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(
235 new LogReader(
236 new FauxRequest(
237 array( 'page' => $this->mTitle->getPrefixedText(),
238 'type' => 'protect' ) ) ) );
239 $logViewer->showList( $out );