*stab stab stab*
[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 * @addtogroup SpecialPage
22 */
23
24 /**
25 * @todo document, briefly.
26 */
27 class ProtectionForm {
28 var $mRestrictions = array();
29 var $mReason = '';
30 var $mCascade = false;
31 var $mExpiry = null;
32
33 function __construct( &$article ) {
34 global $wgRequest, $wgUser;
35 global $wgRestrictionTypes, $wgRestrictionLevels;
36 $this->mArticle =& $article;
37 $this->mTitle =& $article->mTitle;
38
39 if( $this->mTitle ) {
40 $this->mTitle->loadRestrictions();
41
42 foreach( $wgRestrictionTypes as $action ) {
43 // Fixme: this form currently requires individual selections,
44 // but the db allows multiples separated by commas.
45 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
46 }
47
48 $this->mCascade = $this->mTitle->areRestrictionsCascading();
49
50 if ( $this->mTitle->mRestrictionsExpiry == 'infinity' ) {
51 $this->mExpiry = 'infinite';
52 } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
53 $this->mExpiry = '';
54 } else {
55 $this->mExpiry = wfTimestamp( TS_RFC2822, $this->mTitle->mRestrictionsExpiry );
56 }
57 }
58
59 // The form will be available in read-only to show levels.
60 $this->disabled = !$wgUser->isAllowed( 'protect' ) || wfReadOnly() || $wgUser->isBlocked();
61 $this->disabledAttrib = $this->disabled
62 ? array( 'disabled' => 'disabled' )
63 : array();
64
65 if( $wgRequest->wasPosted() ) {
66 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
67 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
68 $this->mExpiry = $wgRequest->getText( 'mwProtect-expiry' );
69
70 foreach( $wgRestrictionTypes as $action ) {
71 $val = $wgRequest->getVal( "mwProtect-level-$action" );
72 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
73 $this->mRestrictions[$action] = $val;
74 }
75 }
76 }
77 }
78
79 function execute() {
80 global $wgRequest;
81 if( $wgRequest->wasPosted() ) {
82 if( $this->save() ) {
83 global $wgOut;
84 $wgOut->redirect( $this->mTitle->getFullUrl() );
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 = wfMsg( 'protect-cascadeon' ) . "\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 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $this->mReason, $this->mCascade, $expiry );
189 if( !$ok ) {
190 throw new FatalError( "Unknown error at restriction save time." );
191 }
192 return $ok;
193 }
194
195 function buildForm() {
196 global $wgUser;
197
198 $out = '';
199 if( !$this->disabled ) {
200 $out .= $this->buildScript();
201 // The submission needs to reenable the move permission selector
202 // if it's in locked mode, or some browsers won't submit the data.
203 $out .= wfOpenElement( 'form', array(
204 'id' => 'mw-Protect-Form',
205 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
206 'method' => 'post',
207 'onsubmit' => 'protectEnable(true)' ) );
208
209 $out .= wfElement( 'input', array(
210 'type' => 'hidden',
211 'name' => 'wpEditToken',
212 'value' => $wgUser->editToken() ) );
213 }
214
215 $out .= "<table id='mwProtectSet'>";
216 $out .= "<tbody>";
217 $out .= "<tr>\n";
218 foreach( $this->mRestrictions as $action => $required ) {
219 /* Not all languages have V_x <-> N_x relation */
220 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
221 }
222 $out .= "</tr>\n";
223 $out .= "<tr>\n";
224 foreach( $this->mRestrictions as $action => $selected ) {
225 $out .= "<td>\n";
226 $out .= $this->buildSelector( $action, $selected );
227 $out .= "</td>\n";
228 }
229 $out .= "</tr>\n";
230
231 // JavaScript will add another row with a value-chaining checkbox
232
233 $out .= "</tbody>\n";
234 $out .= "</table>\n";
235
236 global $wgEnableCascadingProtection;
237
238 if ($wgEnableCascadingProtection)
239 $out .= $this->buildCascadeInput();
240
241 $out .= "<table>\n";
242 $out .= "<tbody>\n";
243
244 $out .= $this->buildExpiryInput();
245
246 if( !$this->disabled ) {
247 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
248 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
249 }
250
251 $out .= "</tbody>\n";
252 $out .= "</table>\n";
253
254 if ( !$this->disabled ) {
255 $out .= "</form>\n";
256 $out .= $this->buildCleanupScript();
257 }
258
259 return $out;
260 }
261
262 function buildSelector( $action, $selected ) {
263 global $wgRestrictionLevels;
264 $id = 'mwProtect-level-' . $action;
265 $attribs = array(
266 'id' => $id,
267 'name' => $id,
268 'size' => count( $wgRestrictionLevels ),
269 'onchange' => 'protectLevelsUpdate(this)',
270 ) + $this->disabledAttrib;
271
272 $out = wfOpenElement( 'select', $attribs );
273 foreach( $wgRestrictionLevels as $key ) {
274 $out .= $this->buildOption( $key, $selected );
275 }
276 $out .= "</select>\n";
277 return $out;
278 }
279
280 function buildOption( $key, $selected ) {
281 $text = ( $key == '' )
282 ? wfMsg( 'protect-default' )
283 : wfMsg( "protect-level-$key" );
284 $selectedAttrib = ($selected == $key)
285 ? array( 'selected' => 'selected' )
286 : array();
287 return wfElement( 'option',
288 array( 'value' => $key ) + $selectedAttrib,
289 $text );
290 }
291
292 function buildReasonInput() {
293 $id = 'mwProtect-reason';
294 return wfElement( 'label', array(
295 'id' => "$id-label",
296 'for' => $id ),
297 wfMsg( 'protectcomment' ) ) .
298 '</td><td>' .
299 wfElement( 'input', array(
300 'size' => 60,
301 'name' => $id,
302 'id' => $id,
303 'value' => $this->mReason ) );
304 }
305
306 function buildCascadeInput() {
307 $id = 'mwProtect-cascade';
308 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
309 return $ci;
310 }
311
312 function buildExpiryInput() {
313 $id = 'mwProtect-expiry';
314
315 $ci = "<tr> <td align=\"right\">";
316 $ci .= wfElement( 'label', array (
317 'id' => "$id-label",
318 'for' => $id ),
319 wfMsg( 'protectexpiry' ) );
320 $ci .= "</td> <td align=\"left\">";
321 $ci .= wfElement( 'input', array(
322 'size' => 60,
323 'name' => $id,
324 'id' => $id,
325 'value' => $this->mExpiry ) + $this->disabledAttrib );
326 $ci .= "</td></tr>";
327
328 return $ci;
329 }
330
331 function buildSubmit() {
332 return wfElement( 'input', array(
333 'id' => 'mw-Protect-submit',
334 'type' => 'submit',
335 'value' => wfMsg( 'confirm' ) ) );
336 }
337
338 function buildScript() {
339 global $wgStylePath, $wgStyleVersion;
340 return '<script type="text/javascript" src="' .
341 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
342 '"></script>';
343 }
344
345 function buildCleanupScript() {
346 global $wgRestrictionLevels, $wgGroupPermissions;
347 $script = 'var wgCascadeableLevels=';
348 $CascadeableLevels = array();
349 foreach( $wgRestrictionLevels as $key ) {
350 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
351 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
352 }
353 }
354 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
355 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
356 return '<script type="text/javascript">' . $script . '</script>';
357 }
358
359 /**
360 * @param OutputPage $out
361 * @access private
362 */
363 function showLogExtract( &$out ) {
364 # Show relevant lines from the deletion log:
365 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
366 $logViewer = new LogViewer(
367 new LogReader(
368 new FauxRequest(
369 array( 'page' => $this->mTitle->getPrefixedText(),
370 'type' => 'protect' ) ) ) );
371 $logViewer->showList( $out );
372 }
373 }
374
375 ?>