Revert r23166, 23178, 23179, and probably some other related bits.
[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
192 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
193 $this->mArticle->doWatch();
194 } elseif( $this->mTitle->userIsWatching() ) {
195 $this->mArticle->doUnwatch();
196 }
197
198 return $ok;
199 }
200
201 function buildForm() {
202 global $wgUser;
203
204 $out = '';
205 if( !$this->disabled ) {
206 $out .= $this->buildScript();
207 // The submission needs to reenable the move permission selector
208 // if it's in locked mode, or some browsers won't submit the data.
209 $out .= wfOpenElement( 'form', array(
210 'id' => 'mw-Protect-Form',
211 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
212 'method' => 'post',
213 'onsubmit' => 'protectEnable(true)' ) );
214
215 $out .= wfElement( 'input', array(
216 'type' => 'hidden',
217 'name' => 'wpEditToken',
218 'value' => $wgUser->editToken() ) );
219 }
220
221 $out .= "<table id='mwProtectSet'>";
222 $out .= "<tbody>";
223 $out .= "<tr>\n";
224 foreach( $this->mRestrictions as $action => $required ) {
225 /* Not all languages have V_x <-> N_x relation */
226 $out .= "<th>" . wfMsgHtml( 'restriction-' . $action ) . "</th>\n";
227 }
228 $out .= "</tr>\n";
229 $out .= "<tr>\n";
230 foreach( $this->mRestrictions as $action => $selected ) {
231 $out .= "<td>\n";
232 $out .= $this->buildSelector( $action, $selected );
233 $out .= "</td>\n";
234 }
235 $out .= "</tr>\n";
236
237 // JavaScript will add another row with a value-chaining checkbox
238
239 $out .= "</tbody>\n";
240 $out .= "</table>\n";
241
242 $out .= "<table>\n";
243 $out .= "<tbody>\n";
244
245 global $wgEnableCascadingProtection;
246 if( $wgEnableCascadingProtection )
247 $out .= '<tr><td></td><td>' . $this->buildCascadeInput() . "</td></tr>\n";
248
249 $out .= $this->buildExpiryInput();
250
251 if( !$this->disabled ) {
252 $out .= "<tr><td>" . $this->buildReasonInput() . "</td></tr>\n";
253 $out .= "<tr><td></td><td>" . $this->buildWatchInput() . "</td></tr>\n";
254 $out .= "<tr><td></td><td>" . $this->buildSubmit() . "</td></tr>\n";
255 }
256
257 $out .= "</tbody>\n";
258 $out .= "</table>\n";
259
260 if ( !$this->disabled ) {
261 $out .= "</form>\n";
262 $out .= $this->buildCleanupScript();
263 }
264
265 return $out;
266 }
267
268 function buildSelector( $action, $selected ) {
269 global $wgRestrictionLevels;
270 $id = 'mwProtect-level-' . $action;
271 $attribs = array(
272 'id' => $id,
273 'name' => $id,
274 'size' => count( $wgRestrictionLevels ),
275 'onchange' => 'protectLevelsUpdate(this)',
276 ) + $this->disabledAttrib;
277
278 $out = wfOpenElement( 'select', $attribs );
279 foreach( $wgRestrictionLevels as $key ) {
280 $out .= $this->buildOption( $key, $selected );
281 }
282 $out .= "</select>\n";
283 return $out;
284 }
285
286 function buildOption( $key, $selected ) {
287 $text = ( $key == '' )
288 ? wfMsg( 'protect-default' )
289 : wfMsg( "protect-level-$key" );
290 $selectedAttrib = ($selected == $key)
291 ? array( 'selected' => 'selected' )
292 : array();
293 return wfElement( 'option',
294 array( 'value' => $key ) + $selectedAttrib,
295 $text );
296 }
297
298 function buildReasonInput() {
299 $id = 'mwProtect-reason';
300 return wfElement( 'label', array(
301 'id' => "$id-label",
302 'for' => $id ),
303 wfMsg( 'protectcomment' ) ) .
304 '</td><td>' .
305 wfElement( 'input', array(
306 'size' => 60,
307 'name' => $id,
308 'id' => $id,
309 'value' => $this->mReason ) );
310 }
311
312 function buildCascadeInput() {
313 $id = 'mwProtect-cascade';
314 $ci = wfCheckLabel( wfMsg( 'protect-cascade' ), $id, $id, $this->mCascade, $this->disabledAttrib);
315 return $ci;
316 }
317
318 function buildExpiryInput() {
319 $attribs = array( 'id' => 'expires' ) + $this->disabledAttrib;
320 return '<tr>'
321 . '<td><label for="expires">' . wfMsgExt( 'protectexpiry', array( 'parseinline' ) ) . '</label></td>'
322 . '<td>' . Xml::input( 'mwProtect-expiry', 60, $this->mExpiry, $attribs ) . '</td>'
323 . '</tr>';
324 }
325
326 function buildWatchInput() {
327 global $wgUser;
328 return Xml::checkLabel(
329 wfMsg( 'watchthis' ),
330 'mwProtectWatch',
331 'mwProtectWatch',
332 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' )
333 );
334 }
335
336 function buildSubmit() {
337 return wfElement( 'input', array(
338 'id' => 'mw-Protect-submit',
339 'type' => 'submit',
340 'value' => wfMsg( 'confirm' ) ) );
341 }
342
343 function buildScript() {
344 global $wgStylePath, $wgStyleVersion;
345 return '<script type="text/javascript" src="' .
346 htmlspecialchars( $wgStylePath . "/common/protect.js?$wgStyleVersion" ) .
347 '"></script>';
348 }
349
350 function buildCleanupScript() {
351 global $wgRestrictionLevels, $wgGroupPermissions;
352 $script = 'var wgCascadeableLevels=';
353 $CascadeableLevels = array();
354 foreach( $wgRestrictionLevels as $key ) {
355 if ( isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect'] ) {
356 $CascadeableLevels[]="'" . wfEscapeJsString($key) . "'";
357 }
358 }
359 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
360 $script .= 'protectInitialize("mwProtectSet","' . wfEscapeJsString( wfMsg( 'protect-unchain' ) ) . '")';
361 return '<script type="text/javascript">' . $script . '</script>';
362 }
363
364 /**
365 * @param OutputPage $out
366 * @access private
367 */
368 function showLogExtract( &$out ) {
369 # Show relevant lines from the protection log:
370 $out->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'protect' ) ) . "</h2>\n" );
371 $logViewer = new LogViewer(
372 new LogReader(
373 new FauxRequest(
374 array( 'page' => $this->mTitle->getPrefixedText(),
375 'type' => 'protect' ) ) ) );
376 $logViewer->showList( $out );
377 }
378 }
379
380 ?>