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
;
27 * Special page allowing users with the appropriate permissions to view
28 * and hide revisions. Log items can also be hidden.
30 * @ingroup SpecialPage
32 class SpecialRevisionDelete
extends UnlistedSpecialPage
{
33 /** @var bool Was the DB modified in this request */
34 protected $wasSaved = false;
36 /** @var bool True if the submit button was clicked, and the form was posted */
37 private $submitClicked;
39 /** @var array Target ID list */
42 /** @var string Archive name, for reviewing deleted files */
45 /** @var string Edit token for securing image views against XSS */
48 /** @var Title Title object for target parameter */
51 /** @var string Deletion type, may be revision, archive, oldimage, filearchive, logging. */
54 /** @var array Array of checkbox specs (message, name, deletion bits) */
57 /** @var array UI Labels about the current type */
60 /** @var RevDelList RevDelList object, storing the list of items to be deleted/undeleted */
63 /** @var bool Whether user is allowed to perform the action */
70 * UI labels for each type.
72 private static $UILabels = [
74 'check-label' => 'revdelete-hide-text',
75 'success' => 'revdelete-success',
76 'failure' => 'revdelete-failure',
77 'text' => 'revdelete-text-text',
78 'selected' => 'revdelete-selected-text',
81 'check-label' => 'revdelete-hide-text',
82 'success' => 'revdelete-success',
83 'failure' => 'revdelete-failure',
84 'text' => 'revdelete-text-text',
85 'selected' => 'revdelete-selected-text',
88 'check-label' => 'revdelete-hide-image',
89 'success' => 'revdelete-success',
90 'failure' => 'revdelete-failure',
91 'text' => 'revdelete-text-file',
92 'selected' => 'revdelete-selected-file',
95 'check-label' => 'revdelete-hide-image',
96 'success' => 'revdelete-success',
97 'failure' => 'revdelete-failure',
98 'text' => 'revdelete-text-file',
99 'selected' => 'revdelete-selected-file',
102 'check-label' => 'revdelete-hide-name',
103 'success' => 'logdelete-success',
104 'failure' => 'logdelete-failure',
105 'text' => 'logdelete-text',
106 'selected' => 'logdelete-selected',
110 public function __construct() {
111 parent
::__construct( 'Revisiondelete', 'deleterevision' );
114 public function doesWrites() {
118 public function execute( $par ) {
119 $this->useTransactionalTimeLimit();
121 $this->checkPermissions();
122 $this->checkReadOnly();
124 $output = $this->getOutput();
125 $user = $this->getUser();
128 // @TODO Use PermissionManager::isBlockedFrom() instead.
129 $block = $user->getBlock();
131 throw new UserBlockedError( $block );
135 $this->outputHeader();
136 $request = $this->getRequest();
137 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
138 # Handle our many different possible input types.
139 $ids = $request->getVal( 'ids' );
140 if ( !is_null( $ids ) ) {
141 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
142 $this->ids
= explode( ',', $ids );
145 $this->ids
= array_keys( $request->getArray( 'ids', [] ) );
147 // $this->ids = array_map( 'intval', $this->ids );
148 $this->ids
= array_unique( array_filter( $this->ids
) );
150 $this->typeName
= $request->getVal( 'type' );
151 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
153 # For reviewing deleted files...
154 $this->archiveName
= $request->getVal( 'file' );
155 $this->token
= $request->getVal( 'token' );
156 if ( $this->archiveName
&& $this->targetObj
) {
157 $this->tryShowFile( $this->archiveName
);
162 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
165 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
166 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
169 # Allow the list type to adjust the passed target
170 $this->targetObj
= RevisionDeleter
::suggestTarget(
176 # We need a target page!
177 if ( $this->targetObj
=== null ) {
178 $output->addWikiMsg( 'undelete-header' );
183 $this->typeLabels
= self
::$UILabels[$this->typeName
];
184 $list = $this->getList();
186 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
187 $canViewSuppressedOnly = $this->getUser()->isAllowed( 'viewsuppressed' ) &&
188 !$this->getUser()->isAllowed( 'suppressrevision' );
189 $pageIsSuppressed = $list->areAnySuppressed();
190 $this->mIsAllowed
= $this->mIsAllowed
&& !( $canViewSuppressedOnly && $pageIsSuppressed );
192 $this->otherReason
= $request->getVal( 'wpReason' );
193 # Give a link to the logs/hist for this page
194 $this->showConvenienceLinks();
196 # Initialise checkboxes
198 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
199 [ $this->typeLabels
['check-label'], 'wpHidePrimary',
200 RevisionDeleter
::getRevdelConstant( $this->typeName
)
202 [ 'revdelete-hide-comment', 'wpHideComment', RevisionRecord
::DELETED_COMMENT
],
203 [ 'revdelete-hide-user', 'wpHideUser', RevisionRecord
::DELETED_USER
]
205 if ( $user->isAllowed( 'suppressrevision' ) ) {
206 $this->checks
[] = [ 'revdelete-hide-restricted',
207 'wpHideRestricted', RevisionRecord
::DELETED_RESTRICTED
];
210 # Either submit or create our form
211 if ( $this->mIsAllowed
&& $this->submitClicked
) {
217 if ( $user->isAllowed( 'deletedhistory' ) ) {
218 $qc = $this->getLogQueryCond();
219 # Show relevant lines from the deletion log
220 $deleteLogPage = new LogPage( 'delete' );
221 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
222 LogEventsList
::showLogExtract(
227 [ 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
]
230 # Show relevant lines from the suppression log
231 if ( $user->isAllowed( 'suppressionlog' ) ) {
232 $suppressLogPage = new LogPage( 'suppress' );
233 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
234 LogEventsList
::showLogExtract(
239 [ 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
]
245 * Show some useful links in the subtitle
247 protected function showConvenienceLinks() {
248 $linkRenderer = $this->getLinkRenderer();
249 # Give a link to the logs/hist for this page
250 if ( $this->targetObj
) {
251 // Also set header tabs to be for the target.
252 $this->getSkin()->setRelevantTitle( $this->targetObj
);
255 $links[] = $linkRenderer->makeKnownLink(
256 SpecialPage
::getTitleFor( 'Log' ),
257 $this->msg( 'viewpagelogs' )->text(),
259 [ 'page' => $this->targetObj
->getPrefixedText() ]
261 if ( !$this->targetObj
->isSpecialPage() ) {
262 # Give a link to the page history
263 $links[] = $linkRenderer->makeKnownLink(
265 $this->msg( 'pagehist' )->text(),
267 [ 'action' => 'history' ]
269 # Link to deleted edits
270 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
271 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
272 $links[] = $linkRenderer->makeKnownLink(
274 $this->msg( 'deletedhist' )->text(),
276 [ 'target' => $this->targetObj
->getPrefixedDBkey() ]
280 # Logs themselves don't have histories or archived revisions
281 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
286 * Get the condition used for fetching log snippets
289 protected function getLogQueryCond() {
291 // Revision delete logs for these item
292 $conds['log_type'] = [ 'delete', 'suppress' ];
293 $conds['log_action'] = $this->getList()->getLogAction();
294 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
295 $conds['ls_value'] = $this->ids
;
301 * Show a deleted file version requested by the visitor.
302 * @todo Mostly copied from Special:Undelete. Refactor.
303 * @param string $archiveName
304 * @throws MWException
305 * @throws PermissionsError
307 protected function tryShowFile( $archiveName ) {
308 $repo = RepoGroup
::singleton()->getLocalRepo();
309 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
311 // Check if user is allowed to see this file
312 if ( !$oimage->exists() ) {
313 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
317 $user = $this->getUser();
318 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
319 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
320 throw new PermissionsError( 'suppressrevision' );
322 throw new PermissionsError( 'deletedtext' );
325 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
326 $lang = $this->getLanguage();
327 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
328 $this->targetObj
->getText(),
329 $lang->userDate( $oimage->getTimestamp(), $user ),
330 $lang->userTime( $oimage->getTimestamp(), $user ) );
331 $this->getOutput()->addHTML(
332 Xml
::openElement( 'form', [
334 'action' => $this->getPageTitle()->getLocalURL( [
335 'target' => $this->targetObj
->getPrefixedDBkey(),
336 'file' => $archiveName,
337 'token' => $user->getEditToken( $archiveName ),
341 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
347 $this->getOutput()->disable();
348 # We mustn't allow the output to be CDN cached, otherwise
349 # if an admin previews a deleted image, and it's cached, then
350 # a user without appropriate permissions can toddle off and
351 # nab the image, and CDN will serve it
352 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
353 $this->getRequest()->response()->header(
354 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate'
356 $this->getRequest()->response()->header( 'Pragma: no-cache' );
358 $key = $oimage->getStorageKey();
359 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
360 $repo->streamFileWithStatus( $path );
364 * Get the list object for this request
367 protected function getList() {
368 if ( is_null( $this->revDelList
) ) {
369 $this->revDelList
= RevisionDeleter
::createList(
370 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
374 return $this->revDelList
;
378 * Show a list of items that we will operate on, and show a form with checkboxes
379 * which will allow the user to choose new visibility settings.
381 protected function showForm() {
384 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
385 $out = $this->getOutput();
386 $out->wrapWikiMsg( "<strong>$1</strong>", [ $this->typeLabels
['selected'],
387 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ] );
389 $this->addHelpLink( 'Help:RevisionDelete' );
390 $out->addHTML( "<ul>" );
394 $list = $this->getList();
395 for ( $list->reset(); $list->current(); $list->next() ) {
396 $item = $list->current();
398 if ( !$item->canView() ) {
399 if ( !$this->submitClicked
) {
400 throw new PermissionsError( 'suppressrevision' );
402 $userAllowed = false;
406 $out->addHTML( $item->getHTML() );
409 if ( !$numRevisions ) {
410 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
413 $out->addHTML( "</ul>" );
415 $this->addUsageText();
417 // Normal sysops can always see what they did, but can't always change it
418 if ( !$userAllowed ) {
422 // Show form if the user can submit
423 if ( $this->mIsAllowed
) {
424 $out->addModules( [ 'mediawiki.special.revisionDelete' ] );
425 $out->addModuleStyles( [ 'mediawiki.special',
426 'mediawiki.interface.helpers.styles' ] );
428 $form = Xml
::openElement( 'form', [ 'method' => 'post',
429 'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ),
430 'id' => 'mw-revdel-form-revisions' ] ) .
431 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
432 $this->buildCheckBoxes() .
433 Xml
::openElement( 'table' ) .
435 '<td class="mw-label">' .
436 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
438 '<td class="mw-input">' .
439 Xml
::listDropDown( 'wpRevDeleteReasonList',
440 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
441 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
442 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown'
446 '<td class="mw-label">' .
447 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
449 '<td class="mw-input">' .
450 Xml
::input( 'wpReason', 60, $this->otherReason
, [
452 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
453 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
454 // Unicode codepoints.
455 // "- 155" is to leave room for the 'wpRevDeleteReasonList' value.
456 'maxlength' => CommentStore
::COMMENT_CHARACTER_LIMIT
- 155,
461 '<td class="mw-submit">' .
462 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
463 [ 'name' => 'wpSubmit' ] ) .
466 Xml
::closeElement( 'table' ) .
467 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
468 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
469 Html
::hidden( 'type', $this->typeName
) .
470 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
471 Xml
::closeElement( 'fieldset' ) . "\n" .
472 Xml
::closeElement( 'form' ) . "\n";
473 // Show link to edit the dropdown reasons
474 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
475 $link = $this->getLinkRenderer()->makeKnownLink(
476 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->getTitle(),
477 $this->msg( 'revdelete-edit-reasonlist' )->text(),
479 [ 'action' => 'edit' ]
481 $form .= Xml
::tags( 'p', [ 'class' => 'mw-revdel-editreasons' ], $link ) . "\n";
486 $out->addHTML( $form );
490 * Show some introductory text
491 * @todo FIXME: Wikimedia-specific policy text
493 protected function addUsageText() {
494 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
495 $this->getOutput()->wrapWikiMsg(
496 "<strong>$1</strong>\n$2", $this->typeLabels
['text'],
497 'revdelete-text-others'
500 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
501 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
504 if ( $this->mIsAllowed
) {
505 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
510 * @return string HTML
512 protected function buildCheckBoxes() {
514 // If there is just one item, use checkboxes
515 $list = $this->getList();
516 if ( $list->length() == 1 ) {
518 $bitfield = $list->current()->getBits(); // existing field
520 if ( $this->submitClicked
) {
521 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
524 foreach ( $this->checks
as $item ) {
525 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
526 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
527 list( $message, $name, $field ) = $item;
528 $innerHTML = Xml
::checkLabel(
529 $this->msg( $message )->text(),
535 if ( $field == RevisionRecord
::DELETED_RESTRICTED
) {
536 $innerHTML = "<b>$innerHTML</b>";
539 $line = Xml
::tags( 'td', [ 'class' => 'mw-input' ], $innerHTML );
540 $html .= "<tr>$line</tr>\n";
543 // Otherwise, use tri-state radios
545 $html .= '<th class="mw-revdel-checkbox">'
546 . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
547 $html .= '<th class="mw-revdel-checkbox">'
548 . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
549 $html .= '<th class="mw-revdel-checkbox">'
550 . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
551 $html .= "<th></th></tr>\n";
552 foreach ( $this->checks
as $item ) {
553 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
554 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
555 list( $message, $name, $field ) = $item;
556 // If there are several items, use third state by default...
557 if ( $this->submitClicked
) {
558 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
560 $selected = -1; // use existing field
562 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
563 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
564 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
565 $label = $this->msg( $message )->escaped();
566 if ( $field == RevisionRecord
::DELETED_RESTRICTED
) {
567 $label = "<b>$label</b>";
569 $line .= "<td>$label</td>";
570 $html .= "<tr>$line</tr>\n";
580 * UI entry point for form submission.
581 * @throws PermissionsError
584 protected function submit() {
585 # Check edit token on submission
586 $token = $this->getRequest()->getVal( 'wpEditToken' );
587 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
588 $this->getOutput()->addWikiMsg( 'sessionfailure' );
592 $bitParams = $this->extractBitParams();
594 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' );
595 $comment = $listReason;
596 if ( $comment === 'other' ) {
597 $comment = $this->otherReason
;
598 } elseif ( $this->otherReason
!== '' ) {
599 // Entry from drop down menu + additional comment
600 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
601 . $this->otherReason
;
603 # Can the user set this field?
604 if ( $bitParams[RevisionRecord
::DELETED_RESTRICTED
] == 1
605 && !$this->getUser()->isAllowed( 'suppressrevision' )
607 throw new PermissionsError( 'suppressrevision' );
609 # If the save went through, go to success message...
610 $status = $this->save( $bitParams, $comment );
611 if ( $status->isGood() ) {
616 # ...otherwise, bounce back to form...
617 $this->failure( $status );
624 * Report that the submit operation succeeded
626 protected function success() {
627 // Messages: revdelete-success, logdelete-success
628 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
629 $this->getOutput()->wrapWikiMsg(
630 "<div class=\"successbox\">\n$1\n</div>",
631 $this->typeLabels
['success']
633 $this->wasSaved
= true;
634 $this->revDelList
->reloadFromMaster();
639 * Report that the submit operation failed
640 * @param Status $status
642 protected function failure( $status ) {
643 // Messages: revdelete-failure, logdelete-failure
644 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
645 $this->getOutput()->wrapWikiTextAsInterface(
647 $status->getWikiText( $this->typeLabels
['failure'] )
653 * Put together an array that contains -1, 0, or the *_deleted const for each bit
657 protected function extractBitParams() {
659 foreach ( $this->checks
as $item ) {
660 list( /* message */, $name, $field ) = $item;
661 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
662 if ( $val < -1 ||
$val > 1 ) {
663 $val = -1; // -1 for existing value
665 $bitfield[$field] = $val;
667 if ( !isset( $bitfield[RevisionRecord
::DELETED_RESTRICTED
] ) ) {
668 $bitfield[RevisionRecord
::DELETED_RESTRICTED
] = 0;
675 * Do the write operations. Simple wrapper for RevDel*List::setVisibility().
676 * @param array $bitPars ExtractBitParams() bitfield array
677 * @param string $reason
680 protected function save( array $bitPars, $reason ) {
681 return $this->getList()->setVisibility(
682 [ 'value' => $bitPars, 'comment' => $reason ]
686 protected function getGroupName() {