* Add time/reason dropdown for protect form (bug 10799)
[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 */
25 class ProtectionForm {
26 var $mRestrictions = array();
27 var $mReason = '';
28 var $mCascade = false;
29 var $mExpiry = null;
30 var $mPermErrors = array();
31 var $mApplicableTypes = array();
32
33 function __construct( &$article ) {
34 global $wgRequest, $wgUser;
35 global $wgRestrictionTypes, $wgRestrictionLevels;
36 $this->mArticle =& $article;
37 $this->mTitle =& $article->mTitle;
38 $this->mApplicableTypes = $this->mTitle->exists() ? $wgRestrictionTypes : array('create');
39
40 if( $this->mTitle ) {
41 $this->mTitle->loadRestrictions();
42
43 foreach( $this->mApplicableTypes as $action ) {
44 // Fixme: this form currently requires individual selections,
45 // but the db allows multiples separated by commas.
46 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
47 }
48
49 $this->mCascade = $this->mTitle->areRestrictionsCascading();
50
51 if ( $this->mTitle->mRestrictionsExpiry == 'infinity' ) {
52 $this->mExpiry = 'infinite';
53 } else if ( strlen($this->mTitle->mRestrictionsExpiry) == 0 ) {
54 $this->mExpiry = '';
55 } else {
56 // FIXME: this format is not user friendly
57 $this->mExpiry = wfTimestamp( TS_ISO_8601, $this->mTitle->mRestrictionsExpiry );
58 }
59 }
60
61 // The form will be available in read-only to show levels.
62 $this->disabled = wfReadOnly() || ($this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser)) != array();
63 $this->disabledAttrib = $this->disabled
64 ? array( 'disabled' => 'disabled' )
65 : array();
66
67 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
68 $this->mReasonList = $wgRequest->getText( 'wpProtectReasonList' );
69 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade );
70 // Let dropdown have 'infinite' for unprotected pages
71 if( !($expiry = $wgRequest->getText( 'mwProtect-expiry' )) && $this->mExpiry != 'infinite' ) {
72 $expiry = $this->mExpiry;
73 }
74 $this->mExpiry = $expiry;
75 $this->mExpiryList = $wgRequest->getText( 'wpProtectExpiryList', $this->mExpiry ? '' : 'infinite' );
76
77 foreach( $this->mApplicableTypes as $action ) {
78 $val = $wgRequest->getVal( "mwProtect-level-$action" );
79 if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
80 // Prevent users from setting levels that they cannot later unset
81 if( $val == 'sysop' ) {
82 // Special case, rewrite sysop to either protect and editprotected
83 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
84 continue;
85 } else {
86 if( !$wgUser->isAllowed($val) )
87 continue;
88 }
89 $this->mRestrictions[$action] = $val;
90 }
91 }
92 }
93
94 function execute() {
95 global $wgRequest, $wgOut;
96 if( $wgRequest->wasPosted() ) {
97 if( $this->save() ) {
98 $article = new Article( $this->mTitle );
99 $q = $article->isRedirect() ? 'redirect=no' : '';
100 $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
101 }
102 } else {
103 $this->show();
104 }
105 }
106
107 function show( $err = null ) {
108 global $wgOut, $wgUser;
109
110 $wgOut->setRobotPolicy( 'noindex,nofollow' );
111
112 if( is_null( $this->mTitle ) ||
113 $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
114 $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
115 return;
116 }
117
118 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
119
120 if ( "" != $err ) {
121 $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
122 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
123 }
124
125 if ( $cascadeSources && count($cascadeSources) > 0 ) {
126 $titles = '';
127
128 foreach ( $cascadeSources as $title ) {
129 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
130 }
131
132 $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
133 }
134
135 $sk = $wgUser->getSkin();
136 $titleLink = $sk->makeLinkObj( $this->mTitle );
137 $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle->getPrefixedText() ) );
138 $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
139
140 # Show an appropriate message if the user isn't allowed or able to change
141 # the protection settings at this time
142 if( $this->disabled ) {
143 if( wfReadOnly() ) {
144 $wgOut->readOnlyPage();
145 } elseif( $this->mPermErrors ) {
146 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors ) );
147 }
148 } else {
149 $wgOut->addWikiMsg( 'protect-text', $this->mTitle->getPrefixedText() );
150 }
151
152 $wgOut->addHTML( $this->buildForm() );
153
154 $this->showLogExtract( $wgOut );
155 }
156
157 function save() {
158 global $wgRequest, $wgUser, $wgOut;
159 # Permission check!
160 if ( $this->disabled ) {
161 $this->show();
162 return false;
163 }
164
165 $token = $wgRequest->getVal( 'wpEditToken' );
166 if ( !$wgUser->matchEditToken( $token ) ) {
167 $this->show( wfMsg( 'sessionfailure' ) );
168 return false;
169 }
170
171 # Create reason string. Use list and/or custom string.
172 $reasonstr = $this->mReasonList;
173 if ( $reasonstr != 'other' && $this->mReason != '' ) {
174 // Entry from drop down menu + additional comment
175 $reasonstr .= ': ' . $this->mReason;
176 } elseif ( $reasonstr == 'other' ) {
177 $reasonstr = $this->mReason;
178 }
179 # Custom expiry takes precedence
180 if ( strlen( $this->mExpiry ) == 0 ) {
181 $this->mExpiry = strlen($this->mExpiryList) ? $this->mExpiryList : 'infinite';
182 }
183
184 if ( $this->mExpiry == 'infinite' || $this->mExpiry == 'indefinite' ) {
185 $expiry = Block::infinity();
186 } else {
187 # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
188 $expiry = strtotime( $this->mExpiry );
189
190 if ( $expiry < 0 || $expiry === false ) {
191 $this->show( wfMsg( 'protect_expiry_invalid' ) );
192 return false;
193 }
194
195 // Fixme: non-qualified absolute times are not in users specified timezone
196 // and there isn't notice about it in the ui
197 $expiry = wfTimestamp( TS_MW, $expiry );
198
199 if ( $expiry < wfTimestampNow() ) {
200 $this->show( wfMsg( 'protect_expiry_old' ) );
201 return false;
202 }
203 }
204
205 # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
206 # to a semi-protected page.
207 global $wgGroupPermissions;
208
209 $edit_restriction = $this->mRestrictions['edit'];
210
211 if ($this->mCascade && ($edit_restriction != 'protect') &&
212 !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
213 $this->mCascade = false;
214
215 if ($this->mTitle->exists()) {
216 $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $reasonstr, $this->mCascade, $expiry );
217 } else {
218 $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $reasonstr, $expiry );
219 }
220
221 if( !$ok ) {
222 throw new FatalError( "Unknown error at restriction save time." );
223 }
224
225 if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
226 $this->mArticle->doWatch();
227 } elseif( $this->mTitle->userIsWatching() ) {
228 $this->mArticle->doUnwatch();
229 }
230
231 return $ok;
232 }
233
234 /**
235 * Build the input form
236 *
237 * @return $out string HTML form
238 */
239 function buildForm() {
240 global $wgUser;
241
242 $mProtectexpiry = Xml::label( wfMsg( 'protectexpiry' ), 'mwProtectExpiryList' );
243 $mProtectother = Xml::label( wfMsg( 'protect-otheroption' ), 'expires' );
244 $mProtectreasonother = Xml::label( wfMsg( 'protectcomment' ), 'wpProtectReasonList' );
245 $mProtectreason = Xml::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
246
247 $out = '';
248 if( !$this->disabled ) {
249 $out .= $this->buildScript();
250 // The submission needs to reenable the move permission selector
251 // if it's in locked mode, or some browsers won't submit the data.
252 $out .= Xml::openElement( 'form', array( 'method' => 'post',
253 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
254 'id' => 'mw-Protect-Form', 'onsubmit' => 'protectEnable(true)' ) );
255 $out .= Xml::hidden( 'wpEditToken',$wgUser->editToken() );
256 }
257
258 $out .= Xml::openElement( 'fieldset' ) .
259 Xml::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
260 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
261 Xml::openElement( 'tbody' ) .
262 "<tr>\n";
263
264 foreach( $this->mRestrictions as $action => $required ) {
265 /* Not all languages have V_x <-> N_x relation */
266 $label = Xml::element( 'label',
267 array( 'for' => "mwProtect-level-$action" ),
268 wfMsg( 'restriction-' . $action ) );
269 $out .= "<th>$label</th>";
270 }
271 $out .= "</tr>
272 <tr>\n";
273 foreach( $this->mRestrictions as $action => $selected ) {
274 $out .= "<td>" .
275 $this->buildSelector( $action, $selected ) .
276 "</td>";
277 }
278 $out .= "</tr>\n";
279
280 $scExpiryOptions = wfMsgForContent( 'ipboptions' ); // FIXME: use its own message
281
282 $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled);
283 if( !$showProtectOptions )
284 $mProtectother = $mProtectexpiry;
285
286 $expiryFormOptions = Xml::option( wfMsg( 'protect-otheroption' ), 'wpProtectExpiryList' );
287 foreach( explode(',', $scExpiryOptions) as $option ) {
288 if ( strpos($option, ":") === false ) $option = "$option:$option";
289 list($show, $value) = explode(":", $option);
290 $show = htmlspecialchars($show);
291 $value = htmlspecialchars($value);
292 $expiryFormOptions .= Xml::option( $show, $value, $this->mExpiryList === $value ? true : false ) . "\n";
293 }
294
295 $reasonDropDown = Xml::listDropDown( 'wpProtectReasonList',
296 wfMsgForContent( 'protect-dropdown' ),
297 wfMsgForContent( 'protect-otherreason' ), '', 'mwProtect-reason', 4 );
298
299 // JavaScript will add another row with a value-chaining checkbox
300 $out .= Xml::closeElement( 'tbody' ) .
301 Xml::closeElement( 'table' ) .
302 Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
303 Xml::openElement( 'tbody' );
304
305 if( $this->mTitle->exists() ) {
306 $out .= '<tr>
307 <td></td>
308 <td class="mw-input">' .
309 Xml::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
310 $this->mCascade, $this->disabledAttrib ) .
311 "</td>
312 </tr>\n";
313 }
314 # Add expiry dropdown
315 if( $showProtectOptions && !$this->disabled ) {
316 $out .= "
317 <tr>
318 <td class='mw-label'>
319 {$mProtectexpiry}
320 </td>
321 <td class='mw-input'>" .
322 Xml::tags( 'select',
323 array(
324 'id' => 'mwProtectExpiryList',
325 'name' => 'wpProtectExpiryList',
326 'onchange' => "document.getElementById('expires').value='';",
327 'tabindex' => '2' ) + $this->disabledAttrib,
328 $expiryFormOptions ) .
329 "</td>
330 </tr>";
331 }
332 # Add custom expiry field
333 $attribs = array( 'id' => 'expires' ) + $this->disabledAttrib;
334 $out .= "<tr>
335 <td class='mw-label'>" .
336 $mProtectother .
337 '</td>
338 <td class="mw-input">' .
339 Xml::input( 'mwProtect-expiry', 60, $this->mExpiry, $attribs ) .
340 '</td>
341 </tr>';
342 # Add manual and custom reason field/selects
343 if( !$this->disabled ) {
344 $out .= "
345 <tr>
346 <td class='mw-label'>
347 {$mProtectreasonother}
348 </td>
349 <td class='mw-input'>
350 {$reasonDropDown}
351 </td>
352 </tr>
353 <tr>
354 <td class='mw-label'>
355 {$mProtectreason}
356 </td>
357 <td class='mw-input'>" .
358 Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text',
359 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
360 "</td>
361 </tr>
362 <tr>
363 <td></td>
364 <td class='mw-input'>" .
365 Xml::checkLabel( wfMsg( 'watchthis' ),
366 'mwProtectWatch', 'mwProtectWatch',
367 $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) .
368 "</td>
369 </tr>
370 <tr>
371 <td></td>
372 <td class='mw-submit'>" .
373 Xml::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
374 "</td>
375 </tr>\n";
376 }
377
378 $out .= Xml::closeElement( 'tbody' ) .
379 Xml::closeElement( 'table' ) .
380 Xml::closeElement( 'fieldset' );
381
382 if ( !$this->disabled ) {
383 $out .= Xml::closeElement( 'form' ) .
384 $this->buildCleanupScript();
385 }
386
387 return $out;
388 }
389
390 function buildSelector( $action, $selected ) {
391 global $wgRestrictionLevels, $wgUser;
392 $id = 'mwProtect-level-' . $action;
393 $attribs = array(
394 'id' => $id,
395 'name' => $id,
396 'size' => count( $wgRestrictionLevels ),
397 'onchange' => 'protectLevelsUpdate(this)',
398 ) + $this->disabledAttrib;
399
400 $out = Xml::openElement( 'select', $attribs );
401 foreach( $wgRestrictionLevels as $key ) {
402 //don't let them choose levels above their own (aka so they can still unprotect and edit the page). but only when the form isn't disabled
403 if( $key == 'sysop' ) {
404 //special case, rewrite sysop to protect and editprotected
405 if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled )
406 continue;
407 } else {
408 if( !$wgUser->isAllowed($key) && !$this->disabled )
409 continue;
410 }
411 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
412 }
413 $out .= Xml::closeElement( 'select' );
414 return $out;
415 }
416
417 /**
418 * Prepare the label for a protection selector option
419 *
420 * @param string $permission Permission required
421 * @return string
422 */
423 private function getOptionLabel( $permission ) {
424 if( $permission == '' ) {
425 return wfMsg( 'protect-default' );
426 } else {
427 $key = "protect-level-{$permission}";
428 $msg = wfMsg( $key );
429 if( wfEmptyMsg( $key, $msg ) )
430 $msg = wfMsg( 'protect-fallback', $permission );
431 return $msg;
432 }
433 }
434
435 function buildScript() {
436 global $wgStylePath, $wgStyleVersion;
437 return Xml::tags( 'script', array(
438 'type' => 'text/javascript',
439 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion" ), '' );
440 }
441
442 function buildCleanupScript() {
443 global $wgRestrictionLevels, $wgGroupPermissions;
444 $script = 'var wgCascadeableLevels=';
445 $CascadeableLevels = array();
446 foreach( $wgRestrictionLevels as $key ) {
447 if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
448 $CascadeableLevels[] = "'" . Xml::escapeJsString( $key ) . "'";
449 }
450 }
451 $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
452 $script .= 'protectInitialize("mwProtectSet","' . Xml::escapeJsString( wfMsg( 'protect-unchain' ) ) . '","' . count($this->mApplicableTypes) . '")';
453 return Xml::tags( 'script', array( 'type' => 'text/javascript' ), $script );
454 }
455
456 /**
457 * @param OutputPage $out
458 * @access private
459 */
460 function showLogExtract( &$out ) {
461 # Show relevant lines from the protection log:
462 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'protect' ) ) );
463 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle->getPrefixedText() );
464 }
465 }