788b565dfba3d3c8b869101f8298d3772bda6434
[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;
80 if( $wgRequest->wasPosted() ) {
81 if( $this->save() ) {
82 global $wgOut;
83 $wgOut->redirect( $this->mTitle->getFullUrl() );
84 }
85 } else {
86 $this->show();
87 }
88 }
89
90 function show( $err = null ) {
91 global $wgOut, $wgUser;
92
93 $wgOut->setRobotpolicy( 'noindex,nofollow' );
94
95 if( is_null( $this->mTitle ) ||
96 !$this->mTitle->exists() ||
97 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
98 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
99 return;
100 }
101
102 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
103
104 if ( "" != $err ) {
105 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
106 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
107 }
108
109 if ( $cascadeSources && count($cascadeSources) > 0 ) {
110 $titles = '';
111
112 foreach ( $cascadeSources as $title ) {
113 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
114 }
115
116 $notice = wfMsgExt( 'protect-cascadeon', array('parsemag'), count($cascadeSources) ) . "\r\n$titles";
117
118 $wgOut->addWikiText( $notice );
119 }
120
121 $wgOut->setPageTitle( wfMsg( 'confirmprotect' ) );
122 $wgOut->setSubtitle( wfMsg( 'protectsub', $this->mTitle->getPrefixedText() ) );
123
124 # Show an appropriate message if the user isn't allowed or able to change
125 # the protection settings at this time
126 if( $this->disabled ) {
127 if( $wgUser->isAllowed( 'protect' ) ) {
128 if( $wgUser->isBlocked() ) {
129 # Blocked
130 $message = 'protect-locked-blocked';
131 } else {
132 # Database lock
133 $message = 'protect-locked-dblock';
134 }
135 } else {
136 # Permission error
137 $message = 'protect-locked-access';
138 }
139 } else {
140 $message = 'protect-text';
141 }
142 $wgOut->addWikiText( wfMsg( $message, wfEscapeWikiText( $this->mTitle->getPrefixedText() ) ) );
143
144 $wgOut->addHTML( $this->buildForm() );
145
146 $this->showLogExtract( $wgOut );
147 }
148
149 function save() {
150 global $wgRequest, $wgUser, $wgOut;
151
152 if( $this->disabled ) {
153 $this->show();
154 return false;
155 }
156
157 $token = $wgRequest->getVal( 'wpEditToken' );
158 if( !$wgUser->matchEditToken( $token ) ) {
159 $this->show( wfMsg( 'sessionfailure' ) );
160 return false;
161 }
162
163 if ( strlen( $this->mExpiry ) == 0 ) {
164 $this->mExpiry = 'infinite';
165 }
166
167 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
168 $expiry = Block::infinity();
169 } else {
170 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
171 $expiry = strtotime( $this->mExpiry );
172
173 if ( $expiry < 0 || $expiry === false ) {
174 $this->show( wfMsg( 'protect_expiry_invalid' ) );
175 return false;
176 }
177
178 $expiry = wfTimestamp( TS_MW, $expiry );
179
180 if ( $expiry < wfTimestampNow() ) {
181 $this->show( wfMsg( 'protect_expiry_old' ) );
182 return false;
183 }
184
185 }
186
187 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
188 if( !$ok ) {
189 throw new FatalError( "Unknown error at restriction save time." );
190 }
191 return $ok;
192 }
193
194 function buildForm() {
195 global $wgUser;
196
197 $out = '';
198 if( !$this->disabled ) {
199 $out .= $this->buildScript();
200 // The submission needs to reenable the move permission selector
201 // if it's in locked mode, or some browsers won't submit the data.
202 $out .= wfOpenElement( 'form', array(
203 'id' => 'mw-Protect-Form',
204 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
205 'method' => 'post',
206 'onsubmit' => 'protectEnable(true)' ) );
207
208 $out .= wfElement( 'input', array(
209 'type' => 'hidden',
210 'name' => 'wpEditToken',
211 'value' => $wgUser->editToken() ) );
212 }
213
214 $out .= "<table id='mwProtectSet'>";
215 $out .= "<tbody>";
216 $out .= "<tr>\n";
217 foreach( $this->mRestrictions as $action => $required ) {
218 /* Not all languages have V_x <-> N_x relation */
219 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
220 }
221 $out .= "</tr>\n";
222 $out .= "<tr>\n";
223 foreach( $this->mRestrictions as $action => $selected ) {
224 $out .= "<td>\n";
225 $out .= $this->buildSelector( $action, $selected );
226 $out .= "</td>\n";
227 }
228 $out .= "</tr>\n";
229
230 // JavaScript will add another row with a value-chaining checkbox
231
232 $out .= "</tbody>\n";
233 $out .= "</table>\n";
234
235 global $wgEnableCascadingProtection;
236
237 if ($wgEnableCascadingProtection)
238 $out .= $this->buildCascadeInput();
239
240 $out .= "<table>\n";
241 $out .= "<tbody>\n";
242
243 $out .= $this->buildExpiryInput();
244
245 if( !$this->disabled ) {
246 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
247 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
248 }
249
250 $out .= "</tbody>\n";
251 $out .= "</table>\n";
252
253 if ( !$this->disabled ) {
254 $out .= "</form>\n";
255 $out .= $this->buildCleanupScript();
256 }
257
258 return $out;
259 }
260
261 function buildSelector( $action, $selected ) {
262 global $wgRestrictionLevels;
263 $id = 'mwProtect-level-' . $action;
264 $attribs = array(
265 'id' => $id,
266 'name' => $id,
267 'size' => count( $wgRestrictionLevels ),
268 'onchange' => 'protectLevelsUpdate(this)',
269 ) + $this->disabledAttrib;
270
271 $out = wfOpenElement( 'select', $attribs );
272 foreach( $wgRestrictionLevels as $key ) {
273 $out .= $this->buildOption( $key, $selected );
274 }
275 $out .= "</select>\n";
276 return $out;
277 }
278
279 function buildOption( $key, $selected ) {
280 $text = ( $key == '' )
281 ? wfMsg( 'protect-default' )
282 : wfMsg( "protect-level-$key" );
283 $selectedAttrib = ($selected == $key)
284 ? array( 'selected' => 'selected' )
285 : array();
286 return wfElement( 'option',
287 array( 'value' => $key ) + $selectedAttrib,
288 $text );
289 }
290
291 function buildReasonInput() {
292 $id = 'mwProtect-reason';
293 return wfElement( 'label', array(
294 'id' => "$id-label",
295 'for' => $id ),
296 wfMsg( 'protectcomment' ) ) .
297 '</td><td>' .
298 wfElement( 'input', array(
299 'size' => 60,
300 'name' => $id,
301 'id' => $id,
302 'value' => $this->mReason ) );
303 }
304
305 function buildCascadeInput() {
306 $id = 'mwProtect-cascade';
307 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
308 return $ci;
309 }
310
311 function buildExpiryInput() {
312 $id = 'mwProtect-expiry';
313
314 $ci = "<tr> <td align=\"right\">";
315 $ci .= wfElement( 'label', array (
316 'id' => "$id-label",
317 'for' => $id ),
318 wfMsg( 'protectexpiry' ) );
319 $ci .= "</td> <td align=\"left\">";
320 $ci .= wfElement( 'input', array(
321 'size' => 60,
322 'name' => $id,
323 'id' => $id,
324 'value' => $this->mExpiry ) + $this->disabledAttrib );
325 $ci .= "</td></tr>";
326
327 return $ci;
328 }
329
330 function buildSubmit() {
331 return wfElement( 'input', array(
332 'id' => 'mw-Protect-submit',
333 'type' => 'submit',
334 'value' => wfMsg( 'confirm' ) ) );
335 }
336
337 function buildScript() {
338 global $wgStylePath, $wgStyleVersion;
339 return '<script type="text/javascript" src="' .
340 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
341 '"></script>';
342 }
343
344 function buildCleanupScript() {
345 global $wgRestrictionLevels, $wgGroupPermissions;
346 $script = 'var wgCascadeableLevels=';
347 $CascadeableLevels = array();
348 foreach( $wgRestrictionLevels as $key ) {
349 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
350 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
351 }
352 }
353 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
354 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
355 return '<script type="text/javascript">' . $script . '</script>';
356 }
357
358 /**
359 * @param OutputPage $out
360 * @access private
361 */
362 function showLogExtract( &$out ) {
363 # Show relevant lines from the protection log:
364 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
365 $logViewer = new LogViewer(
366 new LogReader(
367 new FauxRequest(
368 array( 'page' => $this->mTitle->getPrefixedText(),
369 'type' => 'protect' ) ) ) );
370 $logViewer->showList( $out );
371 }
372 }
373
374 ?>