Merge "MWException: Don't send headers multiple times"
[lhc/web/wiklou.git] / includes / ProtectionForm.php
1 <?php
2 /**
3 * Page protection
4 *
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 /**
27 * Handles the page protection UI and backend
28 */
29 class ProtectionForm {
30 /** @var array A map of action to restriction level, from request or default */
31 protected $mRestrictions = array();
32
33 /** @var string The custom/additional protection reason */
34 protected $mReason = '';
35
36 /** @var string The reason selected from the list, blank for other/additional */
37 protected $mReasonSelection = '';
38
39 /** @var bool True if the restrictions are cascading, from request or existing protection */
40 protected $mCascade = false;
41
42 /** @var array Map of action to "other" expiry time. Used in preference to mExpirySelection. */
43 protected $mExpiry = array();
44
45 /**
46 * @var array Map of action to value selected in expiry drop-down list.
47 * Will be set to 'othertime' whenever mExpiry is set.
48 */
49 protected $mExpirySelection = array();
50
51 /** @var array Permissions errors for the protect action */
52 protected $mPermErrors = array();
53
54 /** @var array Types (i.e. actions) for which levels can be selected */
55 protected $mApplicableTypes = array();
56
57 /** @var array Map of action to the expiry time of the existing protection */
58 protected $mExistingExpiry = array();
59
60 function __construct( Page $article ) {
61 global $wgUser;
62 // Set instance variables.
63 $this->mArticle = $article;
64 $this->mTitle = $article->getTitle();
65 $this->mApplicableTypes = $this->mTitle->getRestrictionTypes();
66
67 // Check if the form should be disabled.
68 // If it is, the form will be available in read-only to show levels.
69 $this->mPermErrors = $this->mTitle->getUserPermissionsErrors( 'protect', $wgUser );
70 if ( wfReadOnly() ) {
71 $this->mPermErrors[] = array( 'readonlytext', wfReadOnlyReason() );
72 }
73 $this->disabled = $this->mPermErrors != array();
74 $this->disabledAttrib = $this->disabled
75 ? array( 'disabled' => 'disabled' )
76 : array();
77
78 $this->loadData();
79 }
80
81 /**
82 * Loads the current state of protection into the object.
83 */
84 function loadData() {
85 global $wgRequest, $wgUser;
86
87 $levels = MWNamespace::getRestrictionLevels( $this->mTitle->getNamespace(), $wgUser );
88 $this->mCascade = $this->mTitle->areRestrictionsCascading();
89
90 $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
91 $this->mReasonSelection = $wgRequest->getText( 'wpProtectReasonSelection' );
92 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade );
93
94 foreach ( $this->mApplicableTypes as $action ) {
95 // @todo FIXME: This form currently requires individual selections,
96 // but the db allows multiples separated by commas.
97
98 // Pull the actual restriction from the DB
99 $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
100
101 if ( !$this->mRestrictions[$action] ) {
102 // No existing expiry
103 $existingExpiry = '';
104 } else {
105 $existingExpiry = $this->mTitle->getRestrictionExpiry( $action );
106 }
107 $this->mExistingExpiry[$action] = $existingExpiry;
108
109 $requestExpiry = $wgRequest->getText( "mwProtect-expiry-$action" );
110 $requestExpirySelection = $wgRequest->getVal( "wpProtectExpirySelection-$action" );
111
112 if ( $requestExpiry ) {
113 // Custom expiry takes precedence
114 $this->mExpiry[$action] = $requestExpiry;
115 $this->mExpirySelection[$action] = 'othertime';
116 } elseif ( $requestExpirySelection ) {
117 // Expiry selected from list
118 $this->mExpiry[$action] = '';
119 $this->mExpirySelection[$action] = $requestExpirySelection;
120 } elseif ( $existingExpiry == 'infinity' ) {
121 // Existing expiry is infinite, use "infinite" in drop-down
122 $this->mExpiry[$action] = '';
123 $this->mExpirySelection[$action] = 'infinite';
124 } elseif ( $existingExpiry ) {
125 // Use existing expiry in its own list item
126 $this->mExpiry[$action] = '';
127 $this->mExpirySelection[$action] = $existingExpiry;
128 } else {
129 // Final default: infinite
130 $this->mExpiry[$action] = '';
131 $this->mExpirySelection[$action] = 'infinite';
132 }
133
134 $val = $wgRequest->getVal( "mwProtect-level-$action" );
135 if ( isset( $val ) && in_array( $val, $levels ) ) {
136 $this->mRestrictions[$action] = $val;
137 }
138 }
139 }
140
141 /**
142 * Get the expiry time for a given action, by combining the relevant inputs.
143 *
144 * @param string $action
145 *
146 * @return string 14-char timestamp or "infinity", or false if the input was invalid
147 */
148 function getExpiry( $action ) {
149 if ( $this->mExpirySelection[$action] == 'existing' ) {
150 return $this->mExistingExpiry[$action];
151 } elseif ( $this->mExpirySelection[$action] == 'othertime' ) {
152 $value = $this->mExpiry[$action];
153 } else {
154 $value = $this->mExpirySelection[$action];
155 }
156 if ( $value == 'infinite' || $value == 'indefinite' || $value == 'infinity' ) {
157 $time = wfGetDB( DB_SLAVE )->getInfinity();
158 } else {
159 $unix = strtotime( $value );
160
161 if ( !$unix || $unix === -1 ) {
162 return false;
163 }
164
165 // @todo FIXME: Non-qualified absolute times are not in users specified timezone
166 // and there isn't notice about it in the ui
167 $time = wfTimestamp( TS_MW, $unix );
168 }
169 return $time;
170 }
171
172 /**
173 * Main entry point for action=protect and action=unprotect
174 */
175 function execute() {
176 global $wgRequest, $wgOut;
177
178 if ( MWNamespace::getRestrictionLevels( $this->mTitle->getNamespace() ) === array( '' ) ) {
179 throw new ErrorPageError( 'protect-badnamespace-title', 'protect-badnamespace-text' );
180 }
181
182 if ( $wgRequest->wasPosted() ) {
183 if ( $this->save() ) {
184 $q = $this->mArticle->isRedirect() ? 'redirect=no' : '';
185 $wgOut->redirect( $this->mTitle->getFullURL( $q ) );
186 }
187 } else {
188 $this->show();
189 }
190 }
191
192 /**
193 * Show the input form with optional error message
194 *
195 * @param string $err Error message or null if there's no error
196 */
197 function show( $err = null ) {
198 global $wgOut;
199
200 $wgOut->setRobotPolicy( 'noindex,nofollow' );
201 $wgOut->addBacklinkSubtitle( $this->mTitle );
202
203 if ( is_array( $err ) ) {
204 $wgOut->wrapWikiMsg( "<p class='error'>\n$1\n</p>\n", $err );
205 } elseif ( is_string( $err ) ) {
206 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
207 }
208
209 if ( $this->mTitle->getRestrictionTypes() === array() ) {
210 // No restriction types available for the current title
211 // this might happen if an extension alters the available types
212 $wgOut->setPageTitle( wfMessage(
213 'protect-norestrictiontypes-title',
214 $this->mTitle->getPrefixedText()
215 ) );
216 $wgOut->addWikiText( wfMessage( 'protect-norestrictiontypes-text' )->text() );
217
218 // Show the log in case protection was possible once
219 $this->showLogExtract( $wgOut );
220 // return as there isn't anything else we can do
221 return;
222 }
223
224 list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
225 if ( $cascadeSources && count( $cascadeSources ) > 0 ) {
226 $titles = '';
227
228 foreach ( $cascadeSources as $title ) {
229 $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
230 }
231
232 /** @todo FIXME: i18n issue, should use formatted number. */
233 $wgOut->wrapWikiMsg(
234 "<div id=\"mw-protect-cascadeon\">\n$1\n" . $titles . "</div>",
235 array( 'protect-cascadeon', count( $cascadeSources ) )
236 );
237 }
238
239 # Show an appropriate message if the user isn't allowed or able to change
240 # the protection settings at this time
241 if ( $this->disabled ) {
242 $wgOut->setPageTitle(
243 wfMessage( 'protect-title-notallowed',
244 $this->mTitle->getPrefixedText() )
245 );
246 $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors, 'protect' ) );
247 } else {
248 $wgOut->setPageTitle( wfMessage( 'protect-title', $this->mTitle->getPrefixedText() ) );
249 $wgOut->addWikiMsg( 'protect-text',
250 wfEscapeWikiText( $this->mTitle->getPrefixedText() ) );
251 }
252
253 $wgOut->addHTML( $this->buildForm() );
254 $this->showLogExtract( $wgOut );
255 }
256
257 /**
258 * Save submitted protection form
259 *
260 * @return bool Success
261 */
262 function save() {
263 global $wgRequest, $wgUser, $wgOut;
264
265 # Permission check!
266 if ( $this->disabled ) {
267 $this->show();
268 return false;
269 }
270
271 $token = $wgRequest->getVal( 'wpEditToken' );
272 if ( !$wgUser->matchEditToken( $token, array( 'protect', $this->mTitle->getPrefixedDBkey() ) ) ) {
273 $this->show( array( 'sessionfailure' ) );
274 return false;
275 }
276
277 # Create reason string. Use list and/or custom string.
278 $reasonstr = $this->mReasonSelection;
279 if ( $reasonstr != 'other' && $this->mReason != '' ) {
280 // Entry from drop down menu + additional comment
281 $reasonstr .= wfMessage( 'colon-separator' )->text() . $this->mReason;
282 } elseif ( $reasonstr == 'other' ) {
283 $reasonstr = $this->mReason;
284 }
285 $expiry = array();
286 foreach ( $this->mApplicableTypes as $action ) {
287 $expiry[$action] = $this->getExpiry( $action );
288 if ( empty( $this->mRestrictions[$action] ) ) {
289 continue; // unprotected
290 }
291 if ( !$expiry[$action] ) {
292 $this->show( array( 'protect_expiry_invalid' ) );
293 return false;
294 }
295 if ( $expiry[$action] < wfTimestampNow() ) {
296 $this->show( array( 'protect_expiry_old' ) );
297 return false;
298 }
299 }
300
301 $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
302
303 $status = $this->mArticle->doUpdateRestrictions(
304 $this->mRestrictions,
305 $expiry,
306 $this->mCascade,
307 $reasonstr,
308 $wgUser
309 );
310
311 if ( !$status->isOK() ) {
312 $this->show( $wgOut->parseInline( $status->getWikiText() ) );
313 return false;
314 }
315
316 /**
317 * Give extensions a change to handle added form items
318 *
319 * @since 1.19 you can (and you should) return false to abort saving;
320 * you can also return an array of message name and its parameters
321 */
322 $errorMsg = '';
323 if ( !wfRunHooks( 'ProtectionForm::save', array( $this->mArticle, &$errorMsg, $reasonstr ) ) ) {
324 if ( $errorMsg == '' ) {
325 $errorMsg = array( 'hookaborted' );
326 }
327 }
328 if ( $errorMsg != '' ) {
329 $this->show( $errorMsg );
330 return false;
331 }
332
333 WatchAction::doWatchOrUnwatch( $wgRequest->getCheck( 'mwProtectWatch' ), $this->mTitle, $wgUser );
334
335 return true;
336 }
337
338 /**
339 * Build the input form
340 *
341 * @return string HTML form
342 */
343 function buildForm() {
344 global $wgUser, $wgLang, $wgOut;
345
346 $mProtectreasonother = Xml::label(
347 wfMessage( 'protectcomment' )->text(),
348 'wpProtectReasonSelection'
349 );
350 $mProtectreason = Xml::label(
351 wfMessage( 'protect-otherreason' )->text(),
352 'mwProtect-reason'
353 );
354
355 $out = '';
356 if ( !$this->disabled ) {
357 $wgOut->addModules( 'mediawiki.legacy.protect' );
358 $out .= Xml::openElement( 'form', array( 'method' => 'post',
359 'action' => $this->mTitle->getLocalURL( 'action=protect' ),
360 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) );
361 }
362
363 $out .= Xml::openElement( 'fieldset' ) .
364 Xml::element( 'legend', null, wfMessage( 'protect-legend' )->text() ) .
365 Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
366 Xml::openElement( 'tbody' );
367
368 // Not all languages have V_x <-> N_x relation
369 foreach ( $this->mRestrictions as $action => $selected ) {
370 // Messages:
371 // restriction-edit, restriction-move, restriction-create, restriction-upload
372 $msg = wfMessage( 'restriction-' . $action );
373 $out .= "<tr><td>" .
374 Xml::openElement( 'fieldset' ) .
375 Xml::element( 'legend', null, $msg->exists() ? $msg->text() : $action ) .
376 Xml::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) .
377 "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>";
378
379 $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection',
380 wfMessage( 'protect-dropdown' )->inContentLanguage()->text(),
381 wfMessage( 'protect-otherreason-op' )->inContentLanguage()->text(),
382 $this->mReasonSelection,
383 'mwProtect-reason', 4 );
384 $scExpiryOptions = wfMessage( 'protect-expiry-options' )->inContentLanguage()->text();
385
386 $showProtectOptions = $scExpiryOptions !== '-' && !$this->disabled;
387
388 $mProtectexpiry = Xml::label(
389 wfMessage( 'protectexpiry' )->text(),
390 "mwProtectExpirySelection-$action"
391 );
392 $mProtectother = Xml::label(
393 wfMessage( 'protect-othertime' )->text(),
394 "mwProtect-$action-expires"
395 );
396
397 $expiryFormOptions = '';
398 if ( $this->mExistingExpiry[$action] && $this->mExistingExpiry[$action] != 'infinity' ) {
399 $timestamp = $wgLang->timeanddate( $this->mExistingExpiry[$action], true );
400 $d = $wgLang->date( $this->mExistingExpiry[$action], true );
401 $t = $wgLang->time( $this->mExistingExpiry[$action], true );
402 $expiryFormOptions .=
403 Xml::option(
404 wfMessage( 'protect-existing-expiry', $timestamp, $d, $t )->text(),
405 'existing',
406 $this->mExpirySelection[$action] == 'existing'
407 ) . "\n";
408 }
409
410 $expiryFormOptions .= Xml::option(
411 wfMessage( 'protect-othertime-op' )->text(),
412 "othertime"
413 ) . "\n";
414 foreach ( explode( ',', $scExpiryOptions ) as $option ) {
415 if ( strpos( $option, ":" ) === false ) {
416 $show = $value = $option;
417 } else {
418 list( $show, $value ) = explode( ":", $option );
419 }
420 $show = htmlspecialchars( $show );
421 $value = htmlspecialchars( $value );
422 $expiryFormOptions .= Xml::option(
423 $show,
424 $value,
425 $this->mExpirySelection[$action] === $value
426 ) . "\n";
427 }
428 # Add expiry dropdown
429 if ( $showProtectOptions && !$this->disabled ) {
430 $out .= "
431 <table><tr>
432 <td class='mw-label'>
433 {$mProtectexpiry}
434 </td>
435 <td class='mw-input'>" .
436 Xml::tags( 'select',
437 array(
438 'id' => "mwProtectExpirySelection-$action",
439 'name' => "wpProtectExpirySelection-$action",
440 'onchange' => "ProtectionForm.updateExpiryList(this)",
441 'tabindex' => '2' ) + $this->disabledAttrib,
442 $expiryFormOptions ) .
443 "</td>
444 </tr></table>";
445 }
446 # Add custom expiry field
447 $attribs = array( 'id' => "mwProtect-$action-expires",
448 'onkeyup' => 'ProtectionForm.updateExpiry(this)',
449 'onchange' => 'ProtectionForm.updateExpiry(this)' ) + $this->disabledAttrib;
450 $out .= "<table><tr>
451 <td class='mw-label'>" .
452 $mProtectother .
453 '</td>
454 <td class="mw-input">' .
455 Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) .
456 '</td>
457 </tr></table>';
458 $out .= "</td></tr>" .
459 Xml::closeElement( 'table' ) .
460 Xml::closeElement( 'fieldset' ) .
461 "</td></tr>";
462 }
463 # Give extensions a chance to add items to the form
464 wfRunHooks( 'ProtectionForm::buildForm', array( $this->mArticle, &$out ) );
465
466 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
467
468 // JavaScript will add another row with a value-chaining checkbox
469 if ( $this->mTitle->exists() ) {
470 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
471 Xml::openElement( 'tbody' );
472 $out .= '<tr>
473 <td></td>
474 <td class="mw-input">' .
475 Xml::checkLabel(
476 wfMessage( 'protect-cascade' )->text(),
477 'mwProtect-cascade',
478 'mwProtect-cascade',
479 $this->mCascade, $this->disabledAttrib
480 ) .
481 "</td>
482 </tr>\n";
483 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
484 }
485
486 # Add manual and custom reason field/selects as well as submit
487 if ( !$this->disabled ) {
488 $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
489 Xml::openElement( 'tbody' );
490 $out .= "
491 <tr>
492 <td class='mw-label'>
493 {$mProtectreasonother}
494 </td>
495 <td class='mw-input'>
496 {$reasonDropDown}
497 </td>
498 </tr>
499 <tr>
500 <td class='mw-label'>
501 {$mProtectreason}
502 </td>
503 <td class='mw-input'>" .
504 Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text',
505 'id' => 'mwProtect-reason', 'maxlength' => 180 ) ) .
506 // Limited maxlength as the database trims at 255 bytes and other texts
507 // chosen by dropdown menus on this page are also included in this database field.
508 // The byte limit of 180 bytes is enforced in javascript
509 "</td>
510 </tr>";
511 # Disallow watching is user is not logged in
512 if ( $wgUser->isLoggedIn() ) {
513 $out .= "
514 <tr>
515 <td></td>
516 <td class='mw-input'>" .
517 Xml::checkLabel( wfMessage( 'watchthis' )->text(),
518 'mwProtectWatch', 'mwProtectWatch',
519 $wgUser->isWatched( $this->mTitle ) || $wgUser->getOption( 'watchdefault' ) ) .
520 "</td>
521 </tr>";
522 }
523 $out .= "
524 <tr>
525 <td></td>
526 <td class='mw-submit'>" .
527 Xml::submitButton(
528 wfMessage( 'confirm' )->text(),
529 array( 'id' => 'mw-Protect-submit' )
530 ) .
531 "</td>
532 </tr>\n";
533 $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
534 }
535 $out .= Xml::closeElement( 'fieldset' );
536
537 if ( $wgUser->isAllowed( 'editinterface' ) ) {
538 $title = Title::makeTitle( NS_MEDIAWIKI, 'Protect-dropdown' );
539 $link = Linker::link(
540 $title,
541 wfMessage( 'protect-edit-reasonlist' )->escaped(),
542 array(),
543 array( 'action' => 'edit' )
544 );
545 $out .= '<p class="mw-protect-editreasons">' . $link . '</p>';
546 }
547
548 if ( !$this->disabled ) {
549 $out .= Html::hidden(
550 'wpEditToken',
551 $wgUser->getEditToken( array( 'protect', $this->mTitle->getPrefixedDBkey() ) )
552 );
553 $out .= Xml::closeElement( 'form' );
554 $wgOut->addScript( $this->buildCleanupScript() );
555 }
556
557 return $out;
558 }
559
560 /**
561 * Build protection level selector
562 *
563 * @param string $action Action to protect
564 * @param string $selected Current protection level
565 * @return string HTML fragment
566 */
567 function buildSelector( $action, $selected ) {
568 global $wgUser;
569
570 // If the form is disabled, display all relevant levels. Otherwise,
571 // just show the ones this user can use.
572 $levels = MWNamespace::getRestrictionLevels( $this->mTitle->getNamespace(),
573 $this->disabled ? null : $wgUser
574 );
575
576 $id = 'mwProtect-level-' . $action;
577 $attribs = array(
578 'id' => $id,
579 'name' => $id,
580 'size' => count( $levels ),
581 'onchange' => 'ProtectionForm.updateLevels(this)',
582 ) + $this->disabledAttrib;
583
584 $out = Xml::openElement( 'select', $attribs );
585 foreach ( $levels as $key ) {
586 $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
587 }
588 $out .= Xml::closeElement( 'select' );
589 return $out;
590 }
591
592 /**
593 * Prepare the label for a protection selector option
594 *
595 * @param string $permission Permission required
596 * @return string
597 */
598 private function getOptionLabel( $permission ) {
599 if ( $permission == '' ) {
600 return wfMessage( 'protect-default' )->text();
601 } else {
602 // Messages: protect-level-autoconfirmed, protect-level-sysop
603 $msg = wfMessage( "protect-level-{$permission}" );
604 if ( $msg->exists() ) {
605 return $msg->text();
606 }
607 return wfMessage( 'protect-fallback', $permission )->text();
608 }
609 }
610
611 function buildCleanupScript() {
612 global $wgCascadingRestrictionLevels, $wgOut;
613
614 $cascadeableLevels = $wgCascadingRestrictionLevels;
615 $options = array(
616 'tableId' => 'mwProtectSet',
617 'labelText' => wfMessage( 'protect-unchain-permissions' )->plain(),
618 'numTypes' => count( $this->mApplicableTypes ),
619 'existingMatch' => count( array_unique( $this->mExistingExpiry ) ) === 1,
620 );
621
622 $wgOut->addJsConfigVars( 'wgCascadeableLevels', $cascadeableLevels );
623 $script = Xml::encodeJsCall( 'ProtectionForm.init', array( $options ) );
624 return Html::inlineScript( ResourceLoader::makeLoaderConditionalScript( $script ) );
625 }
626
627 /**
628 * Show protection long extracts for this page
629 *
630 * @param OutputPage $out
631 * @access private
632 */
633 function showLogExtract( &$out ) {
634 # Show relevant lines from the protection log:
635 $protectLogPage = new LogPage( 'protect' );
636 $out->addHTML( Xml::element( 'h2', null, $protectLogPage->getName()->text() ) );
637 LogEventsList::showLogExtract( $out, 'protect', $this->mTitle );
638 # Let extensions add other relevant log extracts
639 wfRunHooks( 'ProtectionForm::showLogExtract', array( $this->mArticle, $out ) );
640 }
641 }