* $wgDebugTidy feature
[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
22 /**
23 * @todo document, briefly.
24 * @addtogroup SpecialPage
25 */
26 class ProtectionForm {
27 var $mRestrictions = array();
28 var $mReason = '';
29 var $mCascade = false;
30 var $mExpiry = null;
31
32 function __construct( &$article ) {
33 global $wgRequest, $wgUser;
34 global $wgRestrictionTypes, $wgRestrictionLevels;
35 $this->mArticle =& $article;
36 $this->mTitle =& $article->mTitle;
37
38 if( $this->mTitle ) {
39 $this->mTitle->loadRestrictions();
40
41 foreach( $wgRestrictionTypes as $action ) {
42 // Fixme: this form currently requires individual selections,
43 // but the db allows multiples separated by commas.
44 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
45 }
46
47 $this->mCascade = $this->mTitle->areRestrictionsCascading();
48
49 if ( $this->mTitle->mRestrictionsExpiry == 'infinity' ) {
50 $this->mExpiry = 'infinite';
51 } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
52 $this->mExpiry = '';
53 } else {
54 $this->mExpiry = wfTimestamp( TS_RFC2822, $this->mTitle->mRestrictionsExpiry );
55 }
56 }
57
58 // The form will be available in read-only to show levels.
59 $this->disabled = !$wgUser->isAllowed( 'protect' ) || wfReadOnly() || $wgUser->isBlocked();
60 $this->disabledAttrib = $this->disabled
61 ? array( 'disabled' => 'disabled' )
62 : array();
63
64 if( $wgRequest->wasPosted() ) {
65 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
66 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
67 $this->mExpiry = $wgRequest->getText( 'mwProtect-expiry' );
68
69 foreach( $wgRestrictionTypes as $action ) {
70 $val = $wgRequest->getVal( "mwProtect-level-$action" );
71 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
72 $this->mRestrictions[$action] = $val;
73 }
74 }
75 }
76 }
77
78 function execute() {
79 global $wgRequest, $wgOut;
80 if( $wgRequest->wasPosted() ) {
81 if( $this->save() ) {
82 $article = new Article( $this->mTitle );
83 $q = $article->isRedirect() ? 'redirect=no' : '';
84 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
85 }
86 } else {
87 $this->show();
88 }
89 }
90
91 function show( $err = null ) {
92 global $wgOut, $wgUser;
93
94 $wgOut->setRobotpolicy( 'noindex,nofollow' );
95
96 if( is_null( $this->mTitle ) ||
97 !$this->mTitle->exists() ||
98 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
99 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
100 return;
101 }
102
103 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
104
105 if ( "" != $err ) {
106 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
107 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
108 }
109
110 if ( $cascadeSources && count($cascadeSources) > 0 ) {
111 $titles = '';
112
113 foreach ( $cascadeSources as $title ) {
114 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
115 }
116
117 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
118
119 $wgOut->addWikiText( $notice );
120 }
121
122 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
123 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
124
125 # Show an appropriate message if the user isn't allowed or able to change
126 # the protection settings at this time
127 if( $this->disabled ) {
128 if( $wgUser->isAllowed( 'protect' ) ) {
129 if( $wgUser->isBlocked() ) {
130 # Blocked
131 $message = 'protect-locked-blocked';
132 } else {
133 # Database lock
134 $message = 'protect-locked-dblock';
135 }
136 } else {
137 # Permission error
138 $message = 'protect-locked-access';
139 }
140 } else {
141 $message = 'protect-text';
142 }
143 $wgOut->addWikiText( wfMsg( $message, wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
144
145 $wgOut->addHTML( $this->buildForm() );
146
147 $this->showLogExtract( $wgOut );
148 }
149
150 function save() {
151 global $wgRequest, $wgUser, $wgOut;
152
153 if( $this->disabled ) {
154 $this->show();
155 return false;
156 }
157
158 $token = $wgRequest->getVal( 'wpEditToken' );
159 if( !$wgUser->matchEditToken( $token ) ) {
160 $this->show( wfMsg( 'sessionfailure' ) );
161 return false;
162 }
163
164 if ( strlen( $this->mExpiry ) == 0 ) {
165 $this->mExpiry = 'infinite';
166 }
167
168 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
169 $expiry = Block::infinity();
170 } else {
171 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
172 $expiry = strtotime( $this->mExpiry );
173
174 if ( $expiry < 0 || $expiry === false ) {
175 $this->show( wfMsg( 'protect_expiry_invalid' ) );
176 return false;
177 }
178
179 $expiry = wfTimestamp( TS_MW, $expiry );
180
181 if ( $expiry < wfTimestampNow() ) {
182 $this->show( wfMsg( 'protect_expiry_old' ) );
183 return false;
184 }
185
186 }
187
188 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
189 # to a semi-protected page.
190 global $wgGroupPermissions;
191
192 $edit_restriction = $this->mRestrictions['edit'];
193
194 if ($this->mCascade && ($edit_restriction != 'protect') &&
195 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
196 $this->mCascade = false;
197
198 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
199 if( !$ok ) {
200 throw new FatalError( "Unknown error at restriction save time." );
201 }
202
203 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
204 $this->mArticle->doWatch();
205 } elseif( $this->mTitle->userIsWatching() ) {
206 $this->mArticle->doUnwatch();
207 }
208
209 return $ok;
210 }
211
212 function buildForm() {
213 global $wgUser;
214
215 $out = '';
216 if( !$this->disabled ) {
217 $out .= $this->buildScript();
218 // The submission needs to reenable the move permission selector
219 // if it's in locked mode, or some browsers won't submit the data.
220 $out .= wfOpenElement( 'form', array(
221 'id' => 'mw-Protect-Form',
222 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
223 'method' => 'post',
224 'onsubmit' => 'protectEnable(true)' ) );
225
226 $out .= wfElement( 'input', array(
227 'type' => 'hidden',
228 'name' => 'wpEditToken',
229 'value' => $wgUser->editToken() ) );
230 }
231
232 $out .= "<table id='mwProtectSet'>";
233 $out .= "<tbody>";
234 $out .= "<tr>\n";
235 foreach( $this->mRestrictions as $action => $required ) {
236 /* Not all languages have V_x <-> N_x relation */
237 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
238 }
239 $out .= "</tr>\n";
240 $out .= "<tr>\n";
241 foreach( $this->mRestrictions as $action => $selected ) {
242 $out .= "<td>\n";
243 $out .= $this->buildSelector( $action, $selected );
244 $out .= "</td>\n";
245 }
246 $out .= "</tr>\n";
247
248 // JavaScript will add another row with a value-chaining checkbox
249
250 $out .= "</tbody>\n";
251 $out .= "</table>\n";
252
253 $out .= "<table>\n";
254 $out .= "<tbody>\n";
255
256 global $wgEnableCascadingProtection;
257 if( $wgEnableCascadingProtection )
258 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
259
260 $out .= $this->buildExpiryInput();
261
262 if( !$this->disabled ) {
263 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
264 $out .= "<tr><td></td><td>" . $this->buildWatchInput() . "</td></tr>\n";
265 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
266 }
267
268 $out .= "</tbody>\n";
269 $out .= "</table>\n";
270
271 if ( !$this->disabled ) {
272 $out .= "</form>\n";
273 $out .= $this->buildCleanupScript();
274 }
275
276 return $out;
277 }
278
279 function buildSelector( $action, $selected ) {
280 global $wgRestrictionLevels;
281 $id = 'mwProtect-level-' . $action;
282 $attribs = array(
283 'id' => $id,
284 'name' => $id,
285 'size' => count( $wgRestrictionLevels ),
286 'onchange' => 'protectLevelsUpdate(this)',
287 ) + $this->disabledAttrib;
288
289 $out = wfOpenElement( 'select', $attribs );
290 foreach( $wgRestrictionLevels as $key ) {
291 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
292 }
293 $out .= "</select>\n";
294 return $out;
295 }
296
297 /**
298 * Prepare the label for a protection selector option
299 *
300 * @param string $permission Permission required
301 * @return string
302 */
303 private function getOptionLabel( $permission ) {
304 if( $permission == '' ) {
305 return wfMsg( 'protect-default' );
306 } else {
307 $key = "protect-level-{$permission}";
308 $msg = wfMsg( $key );
309 if( wfEmptyMsg( $key, $msg ) )
310 $msg = wfMsg( 'protect-fallback', $permission );
311 return $msg;
312 }
313 }
314
315 function buildReasonInput() {
316 $id = 'mwProtect-reason';
317 return wfElement( 'label', array(
318 'id' => "$id-label",
319 'for' => $id ),
320 wfMsg( 'protectcomment' ) ) .
321 '</td><td>' .
322 wfElement( 'input', array(
323 'size' => 60,
324 'maxlength' => 255,
325 'name' => $id,
326 'id' => $id,
327 'value' => $this->mReason ) );
328 }
329
330 function buildCascadeInput() {
331 $id = 'mwProtect-cascade';
332 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
333 return $ci;
334 }
335
336 function buildExpiryInput() {
337 $attribs = array( 'id' => 'expires' ) + $this->disabledAttrib;
338 return '<tr>'
339 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
340 . '<td>' . Xml::input( 'mwProtect-expiry', 60, $this->mExpiry, $attribs ) . '</td>'
341 . '</tr>';
342 }
343
344 function buildWatchInput() {
345 global $wgUser;
346 return Xml::checkLabel(
347 wfMsg( 'watchthis' ),
348 'mwProtectWatch',
349 'mwProtectWatch',
350 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' )
351 );
352 }
353
354 function buildSubmit() {
355 return wfElement( 'input', array(
356 'id' => 'mw-Protect-submit',
357 'type' => 'submit',
358 'value' => wfMsg( 'confirm' ) ) );
359 }
360
361 function buildScript() {
362 global $wgStylePath, $wgStyleVersion;
363 return '<script type="text/javascript" src="' .
364 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
365 '"></script>';
366 }
367
368 function buildCleanupScript() {
369 global $wgRestrictionLevels, $wgGroupPermissions;
370 $script = 'var wgCascadeableLevels=';
371 $CascadeableLevels = array();
372 foreach( $wgRestrictionLevels as $key ) {
373 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
374 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
375 }
376 }
377 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
378 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
379 return '<script type="text/javascript">' . $script . '</script>';
380 }
381
382 /**
383 * @param OutputPage $out
384 * @access private
385 */
386 function showLogExtract( &$out ) {
387 # Show relevant lines from the protection log:
388 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
389 $logViewer = new LogViewer(
390 new LogReader(
391 new FauxRequest(
392 array( 'page' => $this->mTitle->getPrefixedText(),
393 'type' => 'protect' ) ) ) );
394 $logViewer->showList( $out );
395 }
396
397 }