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\Storage\RevisionRecord
;
25 use MediaWiki\Permissions\PermissionManager
;
28 * Special page allowing users with the appropriate permissions to view
29 * and hide revisions. Log items can also be hidden.
31 * @ingroup SpecialPage
33 class SpecialRevisionDelete
extends UnlistedSpecialPage
{
34 /** @var bool Was the DB modified in this request */
35 protected $wasSaved = false;
37 /** @var bool True if the submit button was clicked, and the form was posted */
38 private $submitClicked;
40 /** @var array Target ID list */
43 /** @var string Archive name, for reviewing deleted files */
46 /** @var string Edit token for securing image views against XSS */
49 /** @var Title Title object for target parameter */
52 /** @var string Deletion type, may be revision, archive, oldimage, filearchive, logging. */
55 /** @var array Array of checkbox specs (message, name, deletion bits) */
58 /** @var array UI Labels about the current type */
61 /** @var RevDelList RevDelList object, storing the list of items to be deleted/undeleted */
64 /** @var bool Whether user is allowed to perform the action */
70 /** @var PermissionManager */
71 private $permissionManager;
74 * UI labels for each type.
76 private static $UILabels = [
78 'check-label' => 'revdelete-hide-text',
79 'success' => 'revdelete-success',
80 'failure' => 'revdelete-failure',
81 'text' => 'revdelete-text-text',
82 'selected' => 'revdelete-selected-text',
85 'check-label' => 'revdelete-hide-text',
86 'success' => 'revdelete-success',
87 'failure' => 'revdelete-failure',
88 'text' => 'revdelete-text-text',
89 'selected' => 'revdelete-selected-text',
92 'check-label' => 'revdelete-hide-image',
93 'success' => 'revdelete-success',
94 'failure' => 'revdelete-failure',
95 'text' => 'revdelete-text-file',
96 'selected' => 'revdelete-selected-file',
99 'check-label' => 'revdelete-hide-image',
100 'success' => 'revdelete-success',
101 'failure' => 'revdelete-failure',
102 'text' => 'revdelete-text-file',
103 'selected' => 'revdelete-selected-file',
106 'check-label' => 'revdelete-hide-name',
107 'success' => 'logdelete-success',
108 'failure' => 'logdelete-failure',
109 'text' => 'logdelete-text',
110 'selected' => 'logdelete-selected',
117 * @param PermissionManager $permissionManager
119 public function __construct( PermissionManager
$permissionManager ) {
120 parent
::__construct( 'Revisiondelete', 'deleterevision' );
122 $this->permissionManager
= $permissionManager;
125 public function doesWrites() {
129 public function execute( $par ) {
130 $this->useTransactionalTimeLimit();
132 $this->checkPermissions();
133 $this->checkReadOnly();
135 $output = $this->getOutput();
136 $user = $this->getUser();
139 $this->outputHeader();
140 $request = $this->getRequest();
141 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
142 # Handle our many different possible input types.
143 $ids = $request->getVal( 'ids' );
144 if ( !is_null( $ids ) ) {
145 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
146 $this->ids
= explode( ',', $ids );
149 $this->ids
= array_keys( $request->getArray( 'ids', [] ) );
151 // $this->ids = array_map( 'intval', $this->ids );
152 $this->ids
= array_unique( array_filter( $this->ids
) );
154 $this->typeName
= $request->getVal( 'type' );
155 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
157 # For reviewing deleted files...
158 $this->archiveName
= $request->getVal( 'file' );
159 $this->token
= $request->getVal( 'token' );
160 if ( $this->archiveName
&& $this->targetObj
) {
161 $this->tryShowFile( $this->archiveName
);
166 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
169 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
170 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
173 # Allow the list type to adjust the passed target
174 $this->targetObj
= RevisionDeleter
::suggestTarget(
180 # We need a target page!
181 if ( $this->targetObj
=== null ) {
182 $output->addWikiMsg( 'undelete-header' );
188 if ( $this->permissionManager
->isBlockedFrom( $user, $this->targetObj
) ) {
189 throw new UserBlockedError( $user->getBlock() );
192 $this->typeLabels
= self
::$UILabels[$this->typeName
];
193 $list = $this->getList();
195 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
196 $canViewSuppressedOnly = $this->getUser()->isAllowed( 'viewsuppressed' ) &&
197 !$this->getUser()->isAllowed( 'suppressrevision' );
198 $pageIsSuppressed = $list->areAnySuppressed();
199 $this->mIsAllowed
= $this->mIsAllowed
&& !( $canViewSuppressedOnly && $pageIsSuppressed );
201 $this->otherReason
= $request->getVal( 'wpReason' );
202 # Give a link to the logs/hist for this page
203 $this->showConvenienceLinks();
205 # Initialise checkboxes
207 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
208 [ $this->typeLabels
['check-label'], 'wpHidePrimary',
209 RevisionDeleter
::getRevdelConstant( $this->typeName
)
211 [ 'revdelete-hide-comment', 'wpHideComment', RevisionRecord
::DELETED_COMMENT
],
212 [ 'revdelete-hide-user', 'wpHideUser', RevisionRecord
::DELETED_USER
]
214 if ( $user->isAllowed( 'suppressrevision' ) ) {
215 $this->checks
[] = [ 'revdelete-hide-restricted',
216 'wpHideRestricted', RevisionRecord
::DELETED_RESTRICTED
];
219 # Either submit or create our form
220 if ( $this->mIsAllowed
&& $this->submitClicked
) {
226 if ( $user->isAllowed( 'deletedhistory' ) ) {
227 $qc = $this->getLogQueryCond();
228 # Show relevant lines from the deletion log
229 $deleteLogPage = new LogPage( 'delete' );
230 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
231 LogEventsList
::showLogExtract(
236 [ 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
]
239 # Show relevant lines from the suppression log
240 if ( $user->isAllowed( 'suppressionlog' ) ) {
241 $suppressLogPage = new LogPage( 'suppress' );
242 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
243 LogEventsList
::showLogExtract(
248 [ 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
]
254 * Show some useful links in the subtitle
256 protected function showConvenienceLinks() {
257 $linkRenderer = $this->getLinkRenderer();
258 # Give a link to the logs/hist for this page
259 if ( $this->targetObj
) {
260 // Also set header tabs to be for the target.
261 $this->getSkin()->setRelevantTitle( $this->targetObj
);
264 $links[] = $linkRenderer->makeKnownLink(
265 SpecialPage
::getTitleFor( 'Log' ),
266 $this->msg( 'viewpagelogs' )->text(),
268 [ 'page' => $this->targetObj
->getPrefixedText() ]
270 if ( !$this->targetObj
->isSpecialPage() ) {
271 # Give a link to the page history
272 $links[] = $linkRenderer->makeKnownLink(
274 $this->msg( 'pagehist' )->text(),
276 [ 'action' => 'history' ]
278 # Link to deleted edits
279 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
280 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
281 $links[] = $linkRenderer->makeKnownLink(
283 $this->msg( 'deletedhist' )->text(),
285 [ 'target' => $this->targetObj
->getPrefixedDBkey() ]
289 # Logs themselves don't have histories or archived revisions
290 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
295 * Get the condition used for fetching log snippets
298 protected function getLogQueryCond() {
300 // Revision delete logs for these item
301 $conds['log_type'] = [ 'delete', 'suppress' ];
302 $conds['log_action'] = $this->getList()->getLogAction();
303 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
304 $conds['ls_value'] = $this->ids
;
310 * Show a deleted file version requested by the visitor.
311 * @todo Mostly copied from Special:Undelete. Refactor.
312 * @param string $archiveName
313 * @throws MWException
314 * @throws PermissionsError
316 protected function tryShowFile( $archiveName ) {
317 $repo = RepoGroup
::singleton()->getLocalRepo();
318 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
320 // Check if user is allowed to see this file
321 if ( !$oimage->exists() ) {
322 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
326 $user = $this->getUser();
327 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
328 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
329 throw new PermissionsError( 'suppressrevision' );
331 throw new PermissionsError( 'deletedtext' );
334 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
335 $lang = $this->getLanguage();
336 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
337 $this->targetObj
->getText(),
338 $lang->userDate( $oimage->getTimestamp(), $user ),
339 $lang->userTime( $oimage->getTimestamp(), $user ) );
340 $this->getOutput()->addHTML(
341 Xml
::openElement( 'form', [
343 'action' => $this->getPageTitle()->getLocalURL( [
344 'target' => $this->targetObj
->getPrefixedDBkey(),
345 'file' => $archiveName,
346 'token' => $user->getEditToken( $archiveName ),
350 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
356 $this->getOutput()->disable();
357 # We mustn't allow the output to be CDN cached, otherwise
358 # if an admin previews a deleted image, and it's cached, then
359 # a user without appropriate permissions can toddle off and
360 # nab the image, and CDN will serve it
361 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
362 $this->getRequest()->response()->header(
363 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate'
365 $this->getRequest()->response()->header( 'Pragma: no-cache' );
367 $key = $oimage->getStorageKey();
368 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
369 $repo->streamFileWithStatus( $path );
373 * Get the list object for this request
376 protected function getList() {
377 if ( is_null( $this->revDelList
) ) {
378 $this->revDelList
= RevisionDeleter
::createList(
379 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
383 return $this->revDelList
;
387 * Show a list of items that we will operate on, and show a form with checkboxes
388 * which will allow the user to choose new visibility settings.
390 protected function showForm() {
393 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
394 $out = $this->getOutput();
395 $out->wrapWikiMsg( "<strong>$1</strong>", [ $this->typeLabels
['selected'],
396 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ] );
398 $this->addHelpLink( 'Help:RevisionDelete' );
399 $out->addHTML( "<ul>" );
403 $list = $this->getList();
404 for ( $list->reset(); $list->current(); $list->next() ) {
405 $item = $list->current();
407 if ( !$item->canView() ) {
408 if ( !$this->submitClicked
) {
409 throw new PermissionsError( 'suppressrevision' );
411 $userAllowed = false;
415 $out->addHTML( $item->getHTML() );
418 if ( !$numRevisions ) {
419 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
422 $out->addHTML( "</ul>" );
424 $this->addUsageText();
426 // Normal sysops can always see what they did, but can't always change it
427 if ( !$userAllowed ) {
431 // Show form if the user can submit
432 if ( $this->mIsAllowed
) {
433 $out->addModules( [ 'mediawiki.special.revisionDelete' ] );
434 $out->addModuleStyles( [ 'mediawiki.special',
435 'mediawiki.interface.helpers.styles' ] );
437 $form = Xml
::openElement( 'form', [ 'method' => 'post',
438 'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ),
439 'id' => 'mw-revdel-form-revisions' ] ) .
440 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
441 $this->buildCheckBoxes() .
442 Xml
::openElement( 'table' ) .
444 '<td class="mw-label">' .
445 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
447 '<td class="mw-input">' .
448 Xml
::listDropDown( 'wpRevDeleteReasonList',
449 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
450 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
451 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown'
455 '<td class="mw-label">' .
456 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
458 '<td class="mw-input">' .
459 Xml
::input( 'wpReason', 60, $this->otherReason
, [
461 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
462 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
463 // Unicode codepoints.
464 // "- 155" is to leave room for the 'wpRevDeleteReasonList' value.
465 'maxlength' => CommentStore
::COMMENT_CHARACTER_LIMIT
- 155,
470 '<td class="mw-submit">' .
471 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
472 [ 'name' => 'wpSubmit' ] ) .
475 Xml
::closeElement( 'table' ) .
476 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
477 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
478 Html
::hidden( 'type', $this->typeName
) .
479 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
480 Xml
::closeElement( 'fieldset' ) . "\n" .
481 Xml
::closeElement( 'form' ) . "\n";
482 // Show link to edit the dropdown reasons
483 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
484 $link = $this->getLinkRenderer()->makeKnownLink(
485 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->getTitle(),
486 $this->msg( 'revdelete-edit-reasonlist' )->text(),
488 [ 'action' => 'edit' ]
490 $form .= Xml
::tags( 'p', [ 'class' => 'mw-revdel-editreasons' ], $link ) . "\n";
495 $out->addHTML( $form );
499 * Show some introductory text
500 * @todo FIXME: Wikimedia-specific policy text
502 protected function addUsageText() {
503 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
504 $this->getOutput()->wrapWikiMsg(
505 "<strong>$1</strong>\n$2", $this->typeLabels
['text'],
506 'revdelete-text-others'
509 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
510 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
513 if ( $this->mIsAllowed
) {
514 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
519 * @return string HTML
521 protected function buildCheckBoxes() {
523 // If there is just one item, use checkboxes
524 $list = $this->getList();
525 if ( $list->length() == 1 ) {
527 $bitfield = $list->current()->getBits(); // existing field
529 if ( $this->submitClicked
) {
530 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
533 foreach ( $this->checks
as $item ) {
534 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
535 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
536 list( $message, $name, $field ) = $item;
537 $innerHTML = Xml
::checkLabel(
538 $this->msg( $message )->text(),
544 if ( $field == RevisionRecord
::DELETED_RESTRICTED
) {
545 $innerHTML = "<b>$innerHTML</b>";
548 $line = Xml
::tags( 'td', [ 'class' => 'mw-input' ], $innerHTML );
549 $html .= "<tr>$line</tr>\n";
552 // Otherwise, use tri-state radios
554 $html .= '<th class="mw-revdel-checkbox">'
555 . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
556 $html .= '<th class="mw-revdel-checkbox">'
557 . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
558 $html .= '<th class="mw-revdel-checkbox">'
559 . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
560 $html .= "<th></th></tr>\n";
561 foreach ( $this->checks
as $item ) {
562 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
563 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
564 list( $message, $name, $field ) = $item;
565 // If there are several items, use third state by default...
566 if ( $this->submitClicked
) {
567 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
569 $selected = -1; // use existing field
571 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
572 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
573 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
574 $label = $this->msg( $message )->escaped();
575 if ( $field == RevisionRecord
::DELETED_RESTRICTED
) {
576 $label = "<b>$label</b>";
578 $line .= "<td>$label</td>";
579 $html .= "<tr>$line</tr>\n";
589 * UI entry point for form submission.
590 * @throws PermissionsError
593 protected function submit() {
594 # Check edit token on submission
595 $token = $this->getRequest()->getVal( 'wpEditToken' );
596 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
597 $this->getOutput()->addWikiMsg( 'sessionfailure' );
601 $bitParams = $this->extractBitParams();
603 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' );
604 $comment = $listReason;
605 if ( $comment === 'other' ) {
606 $comment = $this->otherReason
;
607 } elseif ( $this->otherReason
!== '' ) {
608 // Entry from drop down menu + additional comment
609 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
610 . $this->otherReason
;
612 # Can the user set this field?
613 if ( $bitParams[RevisionRecord
::DELETED_RESTRICTED
] == 1
614 && !$this->getUser()->isAllowed( 'suppressrevision' )
616 throw new PermissionsError( 'suppressrevision' );
618 # If the save went through, go to success message...
619 $status = $this->save( $bitParams, $comment );
620 if ( $status->isGood() ) {
625 # ...otherwise, bounce back to form...
626 $this->failure( $status );
633 * Report that the submit operation succeeded
635 protected function success() {
636 // Messages: revdelete-success, logdelete-success
637 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
638 $this->getOutput()->wrapWikiMsg(
639 "<div class=\"successbox\">\n$1\n</div>",
640 $this->typeLabels
['success']
642 $this->wasSaved
= true;
643 $this->revDelList
->reloadFromMaster();
648 * Report that the submit operation failed
649 * @param Status $status
651 protected function failure( $status ) {
652 // Messages: revdelete-failure, logdelete-failure
653 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
654 $this->getOutput()->wrapWikiTextAsInterface(
656 $status->getWikiText( $this->typeLabels
['failure'] )
662 * Put together an array that contains -1, 0, or the *_deleted const for each bit
666 protected function extractBitParams() {
668 foreach ( $this->checks
as $item ) {
669 list( /* message */, $name, $field ) = $item;
670 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
671 if ( $val < -1 ||
$val > 1 ) {
672 $val = -1; // -1 for existing value
674 $bitfield[$field] = $val;
676 if ( !isset( $bitfield[RevisionRecord
::DELETED_RESTRICTED
] ) ) {
677 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = 0;
684 * Do the write operations. Simple wrapper for RevDel*List::setVisibility().
685 * @param array $bitPars ExtractBitParams() bitfield array
686 * @param string $reason
689 protected function save( array $bitPars, $reason ) {
690 return $this->getList()->setVisibility(
691 [ 'value' => $bitPars, 'comment' => $reason ]
695 protected function getGroupName() {