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
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
25 use MediaWiki\Revision\RevisionRecord
;
26 use MediaWiki\Permissions\PermissionManager
;
29 * Special page allowing users with the appropriate permissions to view
30 * and hide revisions. Log items can also be hidden.
32 * @ingroup SpecialPage
34 class SpecialRevisionDelete
extends UnlistedSpecialPage
{
35 /** @var bool Was the DB modified in this request */
36 protected $wasSaved = false;
38 /** @var bool True if the submit button was clicked, and the form was posted */
39 private $submitClicked;
41 /** @var array Target ID list */
44 /** @var string Archive name, for reviewing deleted files */
47 /** @var string Edit token for securing image views against XSS */
50 /** @var Title Title object for target parameter */
53 /** @var string Deletion type, may be revision, archive, oldimage, filearchive, logging. */
56 /** @var array Array of checkbox specs (message, name, deletion bits) */
59 /** @var array UI Labels about the current type */
62 /** @var RevDelList RevDelList object, storing the list of items to be deleted/undeleted */
65 /** @var bool Whether user is allowed to perform the action */
71 /** @var PermissionManager */
72 private $permissionManager;
75 * UI labels for each type.
77 private static $UILabels = [
79 'check-label' => 'revdelete-hide-text',
80 'success' => 'revdelete-success',
81 'failure' => 'revdelete-failure',
82 'text' => 'revdelete-text-text',
83 'selected' => 'revdelete-selected-text',
86 'check-label' => 'revdelete-hide-text',
87 'success' => 'revdelete-success',
88 'failure' => 'revdelete-failure',
89 'text' => 'revdelete-text-text',
90 'selected' => 'revdelete-selected-text',
93 'check-label' => 'revdelete-hide-image',
94 'success' => 'revdelete-success',
95 'failure' => 'revdelete-failure',
96 'text' => 'revdelete-text-file',
97 'selected' => 'revdelete-selected-file',
100 'check-label' => 'revdelete-hide-image',
101 'success' => 'revdelete-success',
102 'failure' => 'revdelete-failure',
103 'text' => 'revdelete-text-file',
104 'selected' => 'revdelete-selected-file',
107 'check-label' => 'revdelete-hide-name',
108 'success' => 'logdelete-success',
109 'failure' => 'logdelete-failure',
110 'text' => 'logdelete-text',
111 'selected' => 'logdelete-selected',
118 * @param PermissionManager $permissionManager
120 public function __construct( PermissionManager
$permissionManager ) {
121 parent
::__construct( 'Revisiondelete', 'deleterevision' );
123 $this->permissionManager
= $permissionManager;
126 public function doesWrites() {
130 public function execute( $par ) {
131 $this->useTransactionalTimeLimit();
133 $this->checkPermissions();
134 $this->checkReadOnly();
136 $output = $this->getOutput();
137 $user = $this->getUser();
140 $this->outputHeader();
141 $request = $this->getRequest();
142 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
143 # Handle our many different possible input types.
144 $ids = $request->getVal( 'ids' );
145 if ( !is_null( $ids ) ) {
146 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
147 $this->ids
= explode( ',', $ids );
150 $this->ids
= array_keys( $request->getArray( 'ids', [] ) );
152 // $this->ids = array_map( 'intval', $this->ids );
153 $this->ids
= array_unique( array_filter( $this->ids
) );
155 $this->typeName
= $request->getVal( 'type' );
156 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
158 # For reviewing deleted files...
159 $this->archiveName
= $request->getVal( 'file' );
160 $this->token
= $request->getVal( 'token' );
161 if ( $this->archiveName
&& $this->targetObj
) {
162 $this->tryShowFile( $this->archiveName
);
167 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
170 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
171 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
174 # Allow the list type to adjust the passed target
175 $this->targetObj
= RevisionDeleter
::suggestTarget(
181 # We need a target page!
182 if ( $this->targetObj
=== null ) {
183 $output->addWikiMsg( 'undelete-header' );
189 if ( $this->permissionManager
->isBlockedFrom( $user, $this->targetObj
) ) {
190 throw new UserBlockedError( $user->getBlock() );
193 $this->typeLabels
= self
::$UILabels[$this->typeName
];
194 $list = $this->getList();
196 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
197 $this->mIsAllowed
= $permissionManager->userHasRight( $user,
198 RevisionDeleter
::getRestriction( $this->typeName
) );
199 $canViewSuppressedOnly = $permissionManager->userHasRight( $user, 'viewsuppressed' ) &&
200 !$permissionManager->userHasRight( $user, 'suppressrevision' );
201 $pageIsSuppressed = $list->areAnySuppressed();
202 $this->mIsAllowed
= $this->mIsAllowed
&& !( $canViewSuppressedOnly && $pageIsSuppressed );
204 $this->otherReason
= $request->getVal( 'wpReason' );
205 # Give a link to the logs/hist for this page
206 $this->showConvenienceLinks();
208 # Initialise checkboxes
210 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
211 [ $this->typeLabels
['check-label'], 'wpHidePrimary',
212 RevisionDeleter
::getRevdelConstant( $this->typeName
)
214 [ 'revdelete-hide-comment', 'wpHideComment', RevisionRecord
::DELETED_COMMENT
],
215 [ 'revdelete-hide-user', 'wpHideUser', RevisionRecord
::DELETED_USER
]
217 if ( $permissionManager->userHasRight( $user, 'suppressrevision' ) ) {
218 $this->checks
[] = [ 'revdelete-hide-restricted',
219 'wpHideRestricted', RevisionRecord
::DELETED_RESTRICTED
];
222 # Either submit or create our form
223 if ( $this->mIsAllowed
&& $this->submitClicked
) {
229 if ( $permissionManager->userHasRight( $user, 'deletedhistory' ) ) {
230 $qc = $this->getLogQueryCond();
231 # Show relevant lines from the deletion log
232 $deleteLogPage = new LogPage( 'delete' );
233 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
234 LogEventsList
::showLogExtract(
239 [ 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
]
242 # Show relevant lines from the suppression log
243 if ( $permissionManager->userHasRight( $user, 'suppressionlog' ) ) {
244 $suppressLogPage = new LogPage( 'suppress' );
245 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
246 LogEventsList
::showLogExtract(
251 [ 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
]
257 * Show some useful links in the subtitle
259 protected function showConvenienceLinks() {
260 $linkRenderer = $this->getLinkRenderer();
261 # Give a link to the logs/hist for this page
262 if ( $this->targetObj
) {
263 // Also set header tabs to be for the target.
264 $this->getSkin()->setRelevantTitle( $this->targetObj
);
267 $links[] = $linkRenderer->makeKnownLink(
268 SpecialPage
::getTitleFor( 'Log' ),
269 $this->msg( 'viewpagelogs' )->text(),
271 [ 'page' => $this->targetObj
->getPrefixedText() ]
273 if ( !$this->targetObj
->isSpecialPage() ) {
274 # Give a link to the page history
275 $links[] = $linkRenderer->makeKnownLink(
277 $this->msg( 'pagehist' )->text(),
279 [ 'action' => 'history' ]
281 # Link to deleted edits
282 if ( MediaWikiServices
::getInstance()
283 ->getPermissionManager()
284 ->userHasRight( $this->getUser(), 'undelete' )
286 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
287 $links[] = $linkRenderer->makeKnownLink(
289 $this->msg( 'deletedhist' )->text(),
291 [ 'target' => $this->targetObj
->getPrefixedDBkey() ]
295 # Logs themselves don't have histories or archived revisions
296 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
301 * Get the condition used for fetching log snippets
304 protected function getLogQueryCond() {
306 // Revision delete logs for these item
307 $conds['log_type'] = [ 'delete', 'suppress' ];
308 $conds['log_action'] = $this->getList()->getLogAction();
309 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
310 $conds['ls_value'] = $this->ids
;
316 * Show a deleted file version requested by the visitor.
317 * @todo Mostly copied from Special:Undelete. Refactor.
318 * @param string $archiveName
319 * @throws MWException
320 * @throws PermissionsError
322 protected function tryShowFile( $archiveName ) {
323 $repo = RepoGroup
::singleton()->getLocalRepo();
324 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
326 // Check if user is allowed to see this file
327 if ( !$oimage->exists() ) {
328 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
332 $user = $this->getUser();
333 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
334 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
335 throw new PermissionsError( 'suppressrevision' );
337 throw new PermissionsError( 'deletedtext' );
340 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
341 $lang = $this->getLanguage();
342 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
343 $this->targetObj
->getText(),
344 $lang->userDate( $oimage->getTimestamp(), $user ),
345 $lang->userTime( $oimage->getTimestamp(), $user ) );
346 $this->getOutput()->addHTML(
347 Xml
::openElement( 'form', [
349 'action' => $this->getPageTitle()->getLocalURL( [
350 'target' => $this->targetObj
->getPrefixedDBkey(),
351 'file' => $archiveName,
352 'token' => $user->getEditToken( $archiveName ),
356 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
362 $this->getOutput()->disable();
363 # We mustn't allow the output to be CDN cached, otherwise
364 # if an admin previews a deleted image, and it's cached, then
365 # a user without appropriate permissions can toddle off and
366 # nab the image, and CDN will serve it
367 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
368 $this->getRequest()->response()->header(
369 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate'
371 $this->getRequest()->response()->header( 'Pragma: no-cache' );
373 $key = $oimage->getStorageKey();
374 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
375 $repo->streamFileWithStatus( $path );
379 * Get the list object for this request
382 protected function getList() {
383 if ( is_null( $this->revDelList
) ) {
384 $this->revDelList
= RevisionDeleter
::createList(
385 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
389 return $this->revDelList
;
393 * Show a list of items that we will operate on, and show a form with checkboxes
394 * which will allow the user to choose new visibility settings.
396 protected function showForm() {
399 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
400 $out = $this->getOutput();
401 $out->wrapWikiMsg( "<strong>$1</strong>", [ $this->typeLabels
['selected'],
402 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ] );
404 $this->addHelpLink( 'Help:RevisionDelete' );
405 $out->addHTML( "<ul>" );
409 $list = $this->getList();
410 for ( $list->reset(); $list->current(); $list->next() ) {
411 $item = $list->current();
413 if ( !$item->canView() ) {
414 if ( !$this->submitClicked
) {
415 throw new PermissionsError( 'suppressrevision' );
417 $userAllowed = false;
421 $out->addHTML( $item->getHTML() );
424 if ( !$numRevisions ) {
425 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
428 $out->addHTML( "</ul>" );
430 $this->addUsageText();
432 // Normal sysops can always see what they did, but can't always change it
433 if ( !$userAllowed ) {
437 // Show form if the user can submit
438 if ( $this->mIsAllowed
) {
439 $out->addModules( [ 'mediawiki.special.revisionDelete' ] );
440 $out->addModuleStyles( [ 'mediawiki.special',
441 'mediawiki.interface.helpers.styles' ] );
443 $form = Xml
::openElement( 'form', [ 'method' => 'post',
444 'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ),
445 'id' => 'mw-revdel-form-revisions' ] ) .
446 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
447 $this->buildCheckBoxes() .
448 Xml
::openElement( 'table' ) .
450 '<td class="mw-label">' .
451 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
453 '<td class="mw-input">' .
454 Xml
::listDropDown( 'wpRevDeleteReasonList',
455 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
456 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
457 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown'
461 '<td class="mw-label">' .
462 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
464 '<td class="mw-input">' .
465 Xml
::input( 'wpReason', 60, $this->otherReason
, [
467 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
468 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
469 // Unicode codepoints.
470 // "- 155" is to leave room for the 'wpRevDeleteReasonList' value.
471 'maxlength' => CommentStore
::COMMENT_CHARACTER_LIMIT
- 155,
476 '<td class="mw-submit">' .
477 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
478 [ 'name' => 'wpSubmit' ] ) .
481 Xml
::closeElement( 'table' ) .
482 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
483 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
484 Html
::hidden( 'type', $this->typeName
) .
485 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
486 Xml
::closeElement( 'fieldset' ) . "\n" .
487 Xml
::closeElement( 'form' ) . "\n";
488 // Show link to edit the dropdown reasons
489 if ( MediaWikiServices
::getInstance()
490 ->getPermissionManager()
491 ->userHasRight( $this->getUser(), 'editinterface' )
493 $link = $this->getLinkRenderer()->makeKnownLink(
494 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->getTitle(),
495 $this->msg( 'revdelete-edit-reasonlist' )->text(),
497 [ 'action' => 'edit' ]
499 $form .= Xml
::tags( 'p', [ 'class' => 'mw-revdel-editreasons' ], $link ) . "\n";
504 $out->addHTML( $form );
508 * Show some introductory text
509 * @todo FIXME: Wikimedia-specific policy text
511 protected function addUsageText() {
512 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
513 $this->getOutput()->wrapWikiMsg(
514 "<strong>$1</strong>\n$2", $this->typeLabels
['text'],
515 'revdelete-text-others'
518 if ( MediaWikiServices
::getInstance()
519 ->getPermissionManager()
520 ->userHasRight( $this->getUser(), 'suppressrevision' )
522 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
525 if ( $this->mIsAllowed
) {
526 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
531 * @return string HTML
533 protected function buildCheckBoxes() {
535 // If there is just one item, use checkboxes
536 $list = $this->getList();
537 if ( $list->length() == 1 ) {
539 $bitfield = $list->current()->getBits(); // existing field
541 if ( $this->submitClicked
) {
542 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
545 foreach ( $this->checks
as $item ) {
546 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
547 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
548 list( $message, $name, $field ) = $item;
549 $innerHTML = Xml
::checkLabel(
550 $this->msg( $message )->text(),
556 if ( $field == RevisionRecord
::DELETED_RESTRICTED
) {
557 $innerHTML = "<b>$innerHTML</b>";
560 $line = Xml
::tags( 'td', [ 'class' => 'mw-input' ], $innerHTML );
561 $html .= "<tr>$line</tr>\n";
564 // Otherwise, use tri-state radios
566 $html .= '<th class="mw-revdel-checkbox">'
567 . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
568 $html .= '<th class="mw-revdel-checkbox">'
569 . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
570 $html .= '<th class="mw-revdel-checkbox">'
571 . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
572 $html .= "<th></th></tr>\n";
573 foreach ( $this->checks
as $item ) {
574 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
575 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
576 list( $message, $name, $field ) = $item;
577 // If there are several items, use third state by default...
578 if ( $this->submitClicked
) {
579 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
581 $selected = -1; // use existing field
583 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
584 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
585 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
586 $label = $this->msg( $message )->escaped();
587 if ( $field == RevisionRecord
::DELETED_RESTRICTED
) {
588 $label = "<b>$label</b>";
590 $line .= "<td>$label</td>";
591 $html .= "<tr>$line</tr>\n";
601 * UI entry point for form submission.
602 * @throws PermissionsError
605 protected function submit() {
606 # Check edit token on submission
607 $token = $this->getRequest()->getVal( 'wpEditToken' );
608 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
609 $this->getOutput()->addWikiMsg( 'sessionfailure' );
613 $bitParams = $this->extractBitParams();
615 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' );
616 $comment = $listReason;
617 if ( $comment === 'other' ) {
618 $comment = $this->otherReason
;
619 } elseif ( $this->otherReason
!== '' ) {
620 // Entry from drop down menu + additional comment
621 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
622 . $this->otherReason
;
624 # Can the user set this field?
625 if ( $bitParams[RevisionRecord
::DELETED_RESTRICTED
] == 1
626 && !MediaWikiServices
::getInstance()
627 ->getPermissionManager()
628 ->userHasRight( $this->getUser(), 'suppressrevision' )
630 throw new PermissionsError( 'suppressrevision' );
632 # If the save went through, go to success message...
633 $status = $this->save( $bitParams, $comment );
634 if ( $status->isGood() ) {
639 # ...otherwise, bounce back to form...
640 $this->failure( $status );
647 * Report that the submit operation succeeded
649 protected function success() {
650 // Messages: revdelete-success, logdelete-success
651 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
652 $this->getOutput()->wrapWikiMsg(
653 "<div class=\"successbox\">\n$1\n</div>",
654 $this->typeLabels
['success']
656 $this->wasSaved
= true;
657 $this->revDelList
->reloadFromMaster();
662 * Report that the submit operation failed
663 * @param Status $status
665 protected function failure( $status ) {
666 // Messages: revdelete-failure, logdelete-failure
667 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
668 $this->getOutput()->wrapWikiTextAsInterface(
670 $status->getWikiText( $this->typeLabels
['failure'], false, $this->getLanguage() )
676 * Put together an array that contains -1, 0, or the *_deleted const for each bit
680 protected function extractBitParams() {
682 foreach ( $this->checks
as $item ) {
683 list( /* message */, $name, $field ) = $item;
684 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
685 if ( $val < -1 ||
$val > 1 ) {
686 $val = -1; // -1 for existing value
688 $bitfield[$field] = $val;
690 if ( !isset( $bitfield[RevisionRecord
::DELETED_RESTRICTED
] ) ) {
691 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = 0;
698 * Do the write operations. Simple wrapper for RevDel*List::setVisibility().
699 * @param array $bitPars ExtractBitParams() bitfield array
700 * @param string $reason
703 protected function save( array $bitPars, $reason ) {
704 return $this->getList()->setVisibility(
705 [ 'value' => $bitPars, 'comment' => $reason ]
709 protected function getGroupName() {