UI stuff for the cascading protection feature - warnings for sysops editing cascading...
[lhc/web/wiklou.git] / includes / ProtectionForm.php
1 <?php
2 /**
3 * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
4 * http://www.mediawiki.org/
5 *
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.
10 *
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.
15 *
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
20 *
21 * @package MediaWiki
22 * @subpackage SpecialPage
23 */
24
25 class ProtectionForm {
26 var $mRestrictions = array();
27 var $mReason = '';
28 var $mCascade = false;
29
30 function ProtectionForm( &$article ) {
31 global $wgRequest, $wgUser;
32 global $wgRestrictionTypes, $wgRestrictionLevels;
33 $this->mArticle =& $article;
34 $this->mTitle =& $article->mTitle;
35
36 if( $this->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 ) );
41 }
42
43 $this->mCascade = $this->mTitle->areRestrictionsCascading();
44 }
45
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' )
50 : array();
51
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;
59 }
60 }
61 }
62 }
63
64 function show() {
65 global $wgOut;
66
67 $wgOut->setRobotpolicy( 'noindex,nofollow' );
68
69 if( is_null( $this->mTitle ) ||
70 !$this->mTitle->exists() ||
71 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
72 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
73 return;
74 }
75
76 $cascadeSource = $this->mTitle->getCascadeProtectionSource();
77
78 if ( $cascadeSource ) {
79 $cascadeSourceTitle = Title::newFromId( $cascadeSource );
80 $cascadeSourceText = $cascadeSourceTitle->getPrefixedText();
81
82 $wgOut->addWikiText( wfMsgForContent( 'protect-cascadeon', $cascadeSourceText ) );
83 }
84
85 if( $this->save() ) {
86 $wgOut->redirect( $this->mTitle->getFullUrl() );
87 return;
88 }
89
90 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
91 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
92
93 $wgOut->addWikiText(
94 wfMsg( $this->disabled ? "protect-viewtext" : "protect-text",
95 wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
96
97 $wgOut->addHTML( $this->buildForm() );
98
99 $this->showLogExtract( $wgOut );
100 }
101
102 function save() {
103 global $wgRequest, $wgUser, $wgOut;
104 if( !$wgRequest->wasPosted() ) {
105 return false;
106 }
107
108 if( $this->disabled ) {
109 return false;
110 }
111
112 $token = $wgRequest->getVal( 'wpEditToken' );
113 if( !$wgUser->matchEditToken( $token ) ) {
114 throw new FatalError( wfMsg( 'sessionfailure' ) );
115 }
116
117 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade );
118 if( !$ok ) {
119 throw new FatalError( "Unknown error at restriction save time." );
120 }
121 return $ok;
122 }
123
124 function buildForm() {
125 global $wgUser;
126
127 $out = '';
128 if( !$this->disabled ) {
129 $out .= $this->buildScript();
130 // The submission needs to reenable the move permission selector
131 // if it's in locked mode, or some browsers won't submit the data.
132 $out .= wfOpenElement( 'form', array(
133 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
134 'method' => 'post',
135 'onsubmit' => 'protectEnable(true)' ) );
136
137 $out .= wfElement( 'input', array(
138 'type' => 'hidden',
139 'name' => 'wpEditToken',
140 'value' => $wgUser->editToken() ) );
141 }
142
143 $out .= "<table id='mwProtectSet'>";
144 $out .= "<tbody>";
145 $out .= "<tr>\n";
146 foreach( $this->mRestrictions as $action => $required ) {
147 /* Not all languages have V_x <-> N_x relation */
148 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
149 }
150 $out .= "</tr>\n";
151 $out .= "<tr>\n";
152 foreach( $this->mRestrictions as $action => $selected ) {
153 $out .= "<td>\n";
154 $out .= $this->buildSelector( $action, $selected );
155 $out .= "</td>\n";
156 }
157 $out .= "</tr>\n";
158
159 // JavaScript will add another row with a value-chaining checkbox
160
161 $out .= "</tbody>\n";
162 $out .= "</table>\n";
163
164 global $wgEnableCascadingProtection;
165
166 if ($wgEnableCascadingProtection)
167 $out .= $this->buildCascadeInput();
168
169 if( !$this->disabled ) {
170 $out .= "<table>\n";
171 $out .= "<tbody>\n";
172 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
173 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
174 $out .= "</tbody>\n";
175 $out .= "</table>\n";
176 $out .= "</form>\n";
177 $out .= $this->buildCleanupScript();
178 }
179
180 return $out;
181 }
182
183 function buildSelector( $action, $selected ) {
184 global $wgRestrictionLevels;
185 $id = 'mwProtect-level-' . $action;
186 $attribs = array(
187 'id' => $id,
188 'name' => $id,
189 'size' => count( $wgRestrictionLevels ),
190 'onchange' => 'protectLevelsUpdate(this)',
191 ) + $this->disabledAttrib;
192
193 $out = wfOpenElement( 'select', $attribs );
194 foreach( $wgRestrictionLevels as $key ) {
195 $out .= $this->buildOption( $key, $selected );
196 }
197 $out .= "</select>\n";
198 return $out;
199 }
200
201 function buildOption( $key, $selected ) {
202 $text = ( $key == '' )
203 ? wfMsg( 'protect-default' )
204 : wfMsg( "protect-level-$key" );
205 $selectedAttrib = ($selected == $key)
206 ? array( 'selected' => 'selected' )
207 : array();
208 return wfElement( 'option',
209 array( 'value' => $key ) + $selectedAttrib,
210 $text );
211 }
212
213 function buildReasonInput() {
214 $id = 'mwProtect-reason';
215 return wfElement( 'label', array(
216 'id' => "$id-label",
217 'for' => $id ),
218 wfMsg( 'protectcomment' ) ) .
219 '</td><td>' .
220 wfElement( 'input', array(
221 'size' => 60,
222 'name' => $id,
223 'id' => $id ) );
224 }
225
226 function buildCascadeInput() {
227 $id = 'mwProtect-cascade';
228 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
229
230 return $ci;
231 }
232
233 function buildSubmit() {
234 return wfElement( 'input', array(
235 'type' => 'submit',
236 'value' => wfMsg( 'confirm' ) ) );
237 }
238
239 function buildScript() {
240 global $wgStylePath, $wgStyleVersion;
241 return '<script type="text/javascript" src="' .
242 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
243 '"></script>';
244 }
245
246 function buildCleanupScript() {
247 return '<script type="text/javascript">protectInitialize("mwProtectSet","' .
248 wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")</script>';
249 }
250
251 /**
252 * @param OutputPage $out
253 * @access private
254 */
255 function showLogExtract( &$out ) {
256 # Show relevant lines from the deletion log:
257 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
258 $logViewer = new LogViewer(
259 new LogReader(
260 new FauxRequest(
261 array( 'page' => $this->mTitle->getPrefixedText(),
262 'type' => 'protect' ) ) ) );
263 $logViewer->showList( $out );
264 }
265 }
266
267
268 ?>