wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / specials / SpecialRevisiondelete.php
blob6dcbcb191299834e9acb0b26b334869055e8e1b2
1 <?php
2 /**
3 * Implements Special:Revisiondelete
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Special page allowing users with the appropriate permissions to view
26 * and hide revisions. Log items can also be hidden.
28 * @ingroup SpecialPage
30 class SpecialRevisionDelete extends UnlistedSpecialPage {
31 /** @var bool Was the DB modified in this request */
32 protected $wasSaved = false;
34 /** @var bool True if the submit button was clicked, and the form was posted */
35 private $submitClicked;
37 /** @var array Target ID list */
38 private $ids;
40 /** @var string Archive name, for reviewing deleted files */
41 private $archiveName;
43 /** @var string Edit token for securing image views against XSS */
44 private $token;
46 /** @var Title Title object for target parameter */
47 private $targetObj;
49 /** @var string Deletion type, may be revision, archive, oldimage, filearchive, logging. */
50 private $typeName;
52 /** @var array Array of checkbox specs (message, name, deletion bits) */
53 private $checks;
55 /** @var array UI Labels about the current type */
56 private $typeLabels;
58 /** @var RevDelList RevDelList object, storing the list of items to be deleted/undeleted */
59 private $revDelList;
61 /** @var bool Whether user is allowed to perform the action */
62 private $mIsAllowed;
64 /** @var string */
65 private $otherReason;
67 /**
68 * UI labels for each type.
70 private static $UILabels = array(
71 'revision' => array(
72 'check-label' => 'revdelete-hide-text',
73 'success' => 'revdelete-success',
74 'failure' => 'revdelete-failure',
75 'text' => 'revdelete-text-text',
76 'selected'=> 'revdelete-selected-text',
78 'archive' => array(
79 'check-label' => 'revdelete-hide-text',
80 'success' => 'revdelete-success',
81 'failure' => 'revdelete-failure',
82 'text' => 'revdelete-text-text',
83 'selected'=> 'revdelete-selected-text',
85 'oldimage' => array(
86 'check-label' => 'revdelete-hide-image',
87 'success' => 'revdelete-success',
88 'failure' => 'revdelete-failure',
89 'text' => 'revdelete-text-file',
90 'selected'=> 'revdelete-selected-file',
92 'filearchive' => array(
93 'check-label' => 'revdelete-hide-image',
94 'success' => 'revdelete-success',
95 'failure' => 'revdelete-failure',
96 'text' => 'revdelete-text-file',
97 'selected'=> 'revdelete-selected-file',
99 'logging' => array(
100 'check-label' => 'revdelete-hide-name',
101 'success' => 'logdelete-success',
102 'failure' => 'logdelete-failure',
103 'text' => 'logdelete-text',
104 'selected' => 'logdelete-selected',
108 public function __construct() {
109 parent::__construct( 'Revisiondelete', 'deletedhistory' );
112 public function execute( $par ) {
113 $this->useTransactionalTimeLimit();
115 $this->checkPermissions();
116 $this->checkReadOnly();
118 $output = $this->getOutput();
119 $user = $this->getUser();
121 // Check blocks
122 if ( $user->isBlocked() ) {
123 throw new UserBlockedError( $user->getBlock() );
126 $this->setHeaders();
127 $this->outputHeader();
128 $request = $this->getRequest();
129 $this->submitClicked = $request->wasPosted() && $request->getBool( 'wpSubmit' );
130 # Handle our many different possible input types.
131 $ids = $request->getVal( 'ids' );
132 if ( !is_null( $ids ) ) {
133 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
134 $this->ids = explode( ',', $ids );
135 } else {
136 # Array input
137 $this->ids = array_keys( $request->getArray( 'ids', array() ) );
139 // $this->ids = array_map( 'intval', $this->ids );
140 $this->ids = array_unique( array_filter( $this->ids ) );
142 $this->typeName = $request->getVal( 'type' );
143 $this->targetObj = Title::newFromText( $request->getText( 'target' ) );
145 # For reviewing deleted files...
146 $this->archiveName = $request->getVal( 'file' );
147 $this->token = $request->getVal( 'token' );
148 if ( $this->archiveName && $this->targetObj ) {
149 $this->tryShowFile( $this->archiveName );
151 return;
154 $this->typeName = RevisionDeleter::getCanonicalTypeName( $this->typeName );
156 # No targets?
157 if ( !$this->typeName || count( $this->ids ) == 0 ) {
158 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
161 # Allow the list type to adjust the passed target
162 $this->targetObj = RevisionDeleter::suggestTarget(
163 $this->typeName,
164 $this->targetObj,
165 $this->ids
168 # We need a target page!
169 if ( $this->targetObj === null ) {
170 $output->addWikiMsg( 'undelete-header' );
172 return;
175 $this->typeLabels = self::$UILabels[$this->typeName];
176 $list = $this->getList();
177 $list->reset();
178 $this->mIsAllowed = $user->isAllowed( RevisionDeleter::getRestriction( $this->typeName ) );
179 $canViewSuppressedOnly = $this->getUser()->isAllowed( 'viewsuppressed' ) &&
180 !$this->getUser()->isAllowed( 'suppressrevision' );
181 $pageIsSuppressed = $list->areAnySuppressed();
182 $this->mIsAllowed = $this->mIsAllowed && !( $canViewSuppressedOnly && $pageIsSuppressed );
184 $this->otherReason = $request->getVal( 'wpReason' );
185 # Give a link to the logs/hist for this page
186 $this->showConvenienceLinks();
188 # Initialise checkboxes
189 $this->checks = array(
190 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
191 array( $this->typeLabels['check-label'], 'wpHidePrimary',
192 RevisionDeleter::getRevdelConstant( $this->typeName )
194 array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
195 array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER )
197 if ( $user->isAllowed( 'suppressrevision' ) ) {
198 $this->checks[] = array( 'revdelete-hide-restricted',
199 'wpHideRestricted', Revision::DELETED_RESTRICTED );
202 # Either submit or create our form
203 if ( $this->mIsAllowed && $this->submitClicked ) {
204 $this->submit( $request );
205 } else {
206 $this->showForm();
209 $qc = $this->getLogQueryCond();
210 # Show relevant lines from the deletion log
211 $deleteLogPage = new LogPage( 'delete' );
212 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
213 LogEventsList::showLogExtract(
214 $output,
215 'delete',
216 $this->targetObj,
217 '', /* user */
218 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved )
220 # Show relevant lines from the suppression log
221 if ( $user->isAllowed( 'suppressionlog' ) ) {
222 $suppressLogPage = new LogPage( 'suppress' );
223 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
224 LogEventsList::showLogExtract(
225 $output,
226 'suppress',
227 $this->targetObj,
229 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved )
235 * Show some useful links in the subtitle
237 protected function showConvenienceLinks() {
238 # Give a link to the logs/hist for this page
239 if ( $this->targetObj ) {
240 // Also set header tabs to be for the target.
241 $this->getSkin()->setRelevantTitle( $this->targetObj );
243 $links = array();
244 $links[] = Linker::linkKnown(
245 SpecialPage::getTitleFor( 'Log' ),
246 $this->msg( 'viewpagelogs' )->escaped(),
247 array(),
248 array( 'page' => $this->targetObj->getPrefixedText() )
250 if ( !$this->targetObj->isSpecialPage() ) {
251 # Give a link to the page history
252 $links[] = Linker::linkKnown(
253 $this->targetObj,
254 $this->msg( 'pagehist' )->escaped(),
255 array(),
256 array( 'action' => 'history' )
258 # Link to deleted edits
259 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
260 $undelete = SpecialPage::getTitleFor( 'Undelete' );
261 $links[] = Linker::linkKnown(
262 $undelete,
263 $this->msg( 'deletedhist' )->escaped(),
264 array(),
265 array( 'target' => $this->targetObj->getPrefixedDBkey() )
269 # Logs themselves don't have histories or archived revisions
270 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
275 * Get the condition used for fetching log snippets
276 * @return array
278 protected function getLogQueryCond() {
279 $conds = array();
280 // Revision delete logs for these item
281 $conds['log_type'] = array( 'delete', 'suppress' );
282 $conds['log_action'] = $this->getList()->getLogAction();
283 $conds['ls_field'] = RevisionDeleter::getRelationType( $this->typeName );
284 $conds['ls_value'] = $this->ids;
286 return $conds;
290 * Show a deleted file version requested by the visitor.
291 * @todo Mostly copied from Special:Undelete. Refactor.
292 * @param string $archiveName
293 * @throws MWException
294 * @throws PermissionsError
296 protected function tryShowFile( $archiveName ) {
297 $repo = RepoGroup::singleton()->getLocalRepo();
298 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
299 $oimage->load();
300 // Check if user is allowed to see this file
301 if ( !$oimage->exists() ) {
302 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
304 return;
306 $user = $this->getUser();
307 if ( !$oimage->userCan( File::DELETED_FILE, $user ) ) {
308 if ( $oimage->isDeleted( File::DELETED_RESTRICTED ) ) {
309 throw new PermissionsError( 'suppressrevision' );
310 } else {
311 throw new PermissionsError( 'deletedtext' );
314 if ( !$user->matchEditToken( $this->token, $archiveName ) ) {
315 $lang = $this->getLanguage();
316 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
317 $this->targetObj->getText(),
318 $lang->userDate( $oimage->getTimestamp(), $user ),
319 $lang->userTime( $oimage->getTimestamp(), $user ) );
320 $this->getOutput()->addHTML(
321 Xml::openElement( 'form', array(
322 'method' => 'POST',
323 'action' => $this->getPageTitle()->getLocalURL( array(
324 'target' => $this->targetObj->getPrefixedDBkey(),
325 'file' => $archiveName,
326 'token' => $user->getEditToken( $archiveName ),
330 Xml::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
331 '</form>'
334 return;
336 $this->getOutput()->disable();
337 # We mustn't allow the output to be CDN cached, otherwise
338 # if an admin previews a deleted image, and it's cached, then
339 # a user without appropriate permissions can toddle off and
340 # nab the image, and CDN will serve it
341 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
342 $this->getRequest()->response()->header(
343 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate'
345 $this->getRequest()->response()->header( 'Pragma: no-cache' );
347 $key = $oimage->getStorageKey();
348 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
349 $repo->streamFile( $path );
353 * Get the list object for this request
354 * @return RevDelList
356 protected function getList() {
357 if ( is_null( $this->revDelList ) ) {
358 $this->revDelList = RevisionDeleter::createList(
359 $this->typeName, $this->getContext(), $this->targetObj, $this->ids
363 return $this->revDelList;
367 * Show a list of items that we will operate on, and show a form with checkboxes
368 * which will allow the user to choose new visibility settings.
370 protected function showForm() {
371 $userAllowed = true;
373 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
374 $out = $this->getOutput();
375 $out->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels['selected'],
376 $this->getLanguage()->formatNum( count( $this->ids ) ), $this->targetObj->getPrefixedText() ) );
378 $this->addHelpLink( 'Help:RevisionDelete' );
379 $out->addHTML( "<ul>" );
381 $numRevisions = 0;
382 // Live revisions...
383 $list = $this->getList();
384 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
385 for ( $list->reset(); $list->current(); $list->next() ) {
386 // @codingStandardsIgnoreEnd
387 $item = $list->current();
389 if ( !$item->canView() ) {
390 if ( !$this->submitClicked ) {
391 throw new PermissionsError( 'suppressrevision' );
393 $userAllowed = false;
396 $numRevisions++;
397 $out->addHTML( $item->getHTML() );
400 if ( !$numRevisions ) {
401 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
404 $out->addHTML( "</ul>" );
405 // Explanation text
406 $this->addUsageText();
408 // Normal sysops can always see what they did, but can't always change it
409 if ( !$userAllowed ) {
410 return;
413 // Show form if the user can submit
414 if ( $this->mIsAllowed ) {
415 $out->addModuleStyles( 'mediawiki.special' );
417 $form = Xml::openElement( 'form', array( 'method' => 'post',
418 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
419 'id' => 'mw-revdel-form-revisions' ) ) .
420 Xml::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
421 $this->buildCheckBoxes() .
422 Xml::openElement( 'table' ) .
423 "<tr>\n" .
424 '<td class="mw-label">' .
425 Xml::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
426 '</td>' .
427 '<td class="mw-input">' .
428 Xml::listDropDown( 'wpRevDeleteReasonList',
429 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
430 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
431 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown'
433 '</td>' .
434 "</tr><tr>\n" .
435 '<td class="mw-label">' .
436 Xml::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
437 '</td>' .
438 '<td class="mw-input">' .
439 Xml::input(
440 'wpReason',
442 $this->otherReason,
443 array( 'id' => 'wpReason', 'maxlength' => 100 )
445 '</td>' .
446 "</tr><tr>\n" .
447 '<td></td>' .
448 '<td class="mw-submit">' .
449 Xml::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
450 array( 'name' => 'wpSubmit' ) ) .
451 '</td>' .
452 "</tr>\n" .
453 Xml::closeElement( 'table' ) .
454 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
455 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
456 Html::hidden( 'type', $this->typeName ) .
457 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
458 Xml::closeElement( 'fieldset' ) . "\n" .
459 Xml::closeElement( 'form' ) . "\n";
460 // Show link to edit the dropdown reasons
461 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
462 $link = Linker::linkKnown(
463 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->getTitle(),
464 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
465 array(),
466 array( 'action' => 'edit' )
468 $form .= Xml::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
470 } else {
471 $form = '';
473 $out->addHTML( $form );
477 * Show some introductory text
478 * @todo FIXME: Wikimedia-specific policy text
480 protected function addUsageText() {
481 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
482 $this->getOutput()->wrapWikiMsg(
483 "<strong>$1</strong>\n$2", $this->typeLabels['text'],
484 'revdelete-text-others'
487 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
488 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
491 if ( $this->mIsAllowed ) {
492 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
497 * @return string HTML
499 protected function buildCheckBoxes() {
500 $html = '<table>';
501 // If there is just one item, use checkboxes
502 $list = $this->getList();
503 if ( $list->length() == 1 ) {
504 $list->reset();
505 $bitfield = $list->current()->getBits(); // existing field
507 if ( $this->submitClicked ) {
508 $bitfield = RevisionDeleter::extractBitfield( $this->extractBitParams(), $bitfield );
511 foreach ( $this->checks as $item ) {
512 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
513 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
514 list( $message, $name, $field ) = $item;
515 $innerHTML = Xml::checkLabel(
516 $this->msg( $message )->text(),
517 $name,
518 $name,
519 $bitfield & $field
522 if ( $field == Revision::DELETED_RESTRICTED ) {
523 $innerHTML = "<b>$innerHTML</b>";
526 $line = Xml::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
527 $html .= "<tr>$line</tr>\n";
529 } else {
530 // Otherwise, use tri-state radios
531 $html .= '<tr>';
532 $html .= '<th class="mw-revdel-checkbox">'
533 . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
534 $html .= '<th class="mw-revdel-checkbox">'
535 . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
536 $html .= '<th class="mw-revdel-checkbox">'
537 . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
538 $html .= "<th></th></tr>\n";
539 foreach ( $this->checks as $item ) {
540 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
541 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
542 list( $message, $name, $field ) = $item;
543 // If there are several items, use third state by default...
544 if ( $this->submitClicked ) {
545 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
546 } else {
547 $selected = -1; // use existing field
549 $line = '<td class="mw-revdel-checkbox">' . Xml::radio( $name, -1, $selected == -1 ) . '</td>';
550 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 0, $selected == 0 ) . '</td>';
551 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 1, $selected == 1 ) . '</td>';
552 $label = $this->msg( $message )->escaped();
553 if ( $field == Revision::DELETED_RESTRICTED ) {
554 $label = "<b>$label</b>";
556 $line .= "<td>$label</td>";
557 $html .= "<tr>$line</tr>\n";
561 $html .= '</table>';
563 return $html;
567 * UI entry point for form submission.
568 * @throws PermissionsError
569 * @return bool
571 protected function submit() {
572 # Check edit token on submission
573 $token = $this->getRequest()->getVal( 'wpEditToken' );
574 if ( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
575 $this->getOutput()->addWikiMsg( 'sessionfailure' );
577 return false;
579 $bitParams = $this->extractBitParams();
580 // from dropdown
581 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' );
582 $comment = $listReason;
583 if ( $comment === 'other' ) {
584 $comment = $this->otherReason;
585 } elseif ( $this->otherReason !== '' ) {
586 // Entry from drop down menu + additional comment
587 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
588 . $this->otherReason;
590 # Can the user set this field?
591 if ( $bitParams[Revision::DELETED_RESTRICTED] == 1
592 && !$this->getUser()->isAllowed( 'suppressrevision' )
594 throw new PermissionsError( 'suppressrevision' );
596 # If the save went through, go to success message...
597 $status = $this->save( $bitParams, $comment );
598 if ( $status->isGood() ) {
599 $this->success();
601 return true;
602 } else {
603 # ...otherwise, bounce back to form...
604 $this->failure( $status );
607 return false;
611 * Report that the submit operation succeeded
613 protected function success() {
614 // Messages: revdelete-success, logdelete-success
615 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
616 $this->getOutput()->wrapWikiMsg(
617 "<div class=\"successbox\">\n$1\n</div>",
618 $this->typeLabels['success']
620 $this->wasSaved = true;
621 $this->revDelList->reloadFromMaster();
622 $this->showForm();
626 * Report that the submit operation failed
627 * @param Status $status
629 protected function failure( $status ) {
630 // Messages: revdelete-failure, logdelete-failure
631 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
632 $this->getOutput()->addWikiText( '<div class="errorbox">' .
633 $status->getWikiText( $this->typeLabels['failure'] ) .
634 '</div>'
636 $this->showForm();
640 * Put together an array that contains -1, 0, or the *_deleted const for each bit
642 * @return array
644 protected function extractBitParams() {
645 $bitfield = array();
646 foreach ( $this->checks as $item ) {
647 list( /* message */, $name, $field ) = $item;
648 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
649 if ( $val < -1 || $val > 1 ) {
650 $val = -1; // -1 for existing value
652 $bitfield[$field] = $val;
654 if ( !isset( $bitfield[Revision::DELETED_RESTRICTED] ) ) {
655 $bitfield[Revision::DELETED_RESTRICTED] = 0;
658 return $bitfield;
662 * Do the write operations. Simple wrapper for RevDel*List::setVisibility().
663 * @param array $bitPars ExtractBitParams() bitfield array
664 * @param string $reason
665 * @return Status
667 protected function save( array $bitPars, $reason ) {
668 return $this->getList()->setVisibility(
669 array( 'value' => $bitPars, 'comment' => $reason )
673 protected function getGroupName() {
674 return 'pagetools';