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
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 */
40 /** @var string Archive name, for reviewing deleted files */
43 /** @var string Edit token for securing image views against XSS */
46 /** @var Title Title object for target parameter */
49 /** @var string Deletion type, may be revision, archive, oldimage, filearchive, logging. */
52 /** @var array Array of checkbox specs (message, name, deletion bits) */
55 /** @var array UI Labels about the current type */
58 /** @var RevDelList RevDelList object, storing the list of items to be deleted/undeleted */
61 /** @var bool Whether user is allowed to perform the action */
68 * UI labels for each type.
70 private static $UILabels = 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',
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-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',
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->checkPermissions();
114 $this->checkReadOnly();
116 $output = $this->getOutput();
117 $user = $this->getUser();
120 $this->outputHeader();
121 $request = $this->getRequest();
122 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
123 # Handle our many different possible input types.
124 $ids = $request->getVal( 'ids' );
125 if ( !is_null( $ids ) ) {
126 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
127 $this->ids
= explode( ',', $ids );
130 $this->ids
= array_keys( $request->getArray( 'ids', array() ) );
132 // $this->ids = array_map( 'intval', $this->ids );
133 $this->ids
= array_unique( array_filter( $this->ids
) );
135 if ( $request->getVal( 'action' ) == 'historysubmit'
136 ||
$request->getVal( 'action' ) == 'revisiondelete'
138 // For show/hide form submission from history page
139 // Since we are access through index.php?title=XXX&action=historysubmit
140 // getFullTitle() will contain the target title and not our title
141 $this->targetObj
= $this->getFullTitle();
142 $this->typeName
= 'revision';
144 $this->typeName
= $request->getVal( 'type' );
145 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
148 # For reviewing deleted files...
149 $this->archiveName
= $request->getVal( 'file' );
150 $this->token
= $request->getVal( 'token' );
151 if ( $this->archiveName
&& $this->targetObj
) {
152 $this->tryShowFile( $this->archiveName
);
157 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
160 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
161 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
163 $this->typeLabels
= self
::$UILabels[$this->typeName
];
164 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
166 # Allow the list type to adjust the passed target
167 $this->targetObj
= RevisionDeleter
::suggestTarget(
173 $this->otherReason
= $request->getVal( 'wpReason' );
174 # We need a target page!
175 if ( is_null( $this->targetObj
) ) {
176 $output->addWikiMsg( 'undelete-header' );
180 # Give a link to the logs/hist for this page
181 $this->showConvenienceLinks();
183 # Initialise checkboxes
184 $this->checks
= array(
185 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
186 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
187 RevisionDeleter
::getRevdelConstant( $this->typeName
)
189 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
190 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
192 if ( $user->isAllowed( 'suppressrevision' ) ) {
193 $this->checks
[] = array( 'revdelete-hide-restricted',
194 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
197 # Either submit or create our form
198 if ( $this->mIsAllowed
&& $this->submitClicked
) {
199 $this->submit( $request );
204 $qc = $this->getLogQueryCond();
205 # Show relevant lines from the deletion log
206 $deleteLogPage = new LogPage( 'delete' );
207 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
208 LogEventsList
::showLogExtract(
213 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
215 # Show relevant lines from the suppression log
216 if ( $user->isAllowed( 'suppressionlog' ) ) {
217 $suppressLogPage = new LogPage( 'suppress' );
218 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
219 LogEventsList
::showLogExtract(
224 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
230 * Show some useful links in the subtitle
232 protected function showConvenienceLinks() {
233 # Give a link to the logs/hist for this page
234 if ( $this->targetObj
) {
235 // Also set header tabs to be for the target.
236 $this->getSkin()->setRelevantTitle( $this->targetObj
);
239 $links[] = Linker
::linkKnown(
240 SpecialPage
::getTitleFor( 'Log' ),
241 $this->msg( 'viewpagelogs' )->escaped(),
243 array( 'page' => $this->targetObj
->getPrefixedText() )
245 if ( !$this->targetObj
->isSpecialPage() ) {
246 # Give a link to the page history
247 $links[] = Linker
::linkKnown(
249 $this->msg( 'pagehist' )->escaped(),
251 array( 'action' => 'history' )
253 # Link to deleted edits
254 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
255 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
256 $links[] = Linker
::linkKnown(
258 $this->msg( 'deletedhist' )->escaped(),
260 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
264 # Logs themselves don't have histories or archived revisions
265 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
270 * Get the condition used for fetching log snippets
273 protected function getLogQueryCond() {
275 // Revision delete logs for these item
276 $conds['log_type'] = array( 'delete', 'suppress' );
277 $conds['log_action'] = $this->getList()->getLogAction();
278 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
279 $conds['ls_value'] = $this->ids
;
285 * Show a deleted file version requested by the visitor.
286 * @todo Mostly copied from Special:Undelete. Refactor.
287 * @param string $archiveName
289 protected function tryShowFile( $archiveName ) {
290 $repo = RepoGroup
::singleton()->getLocalRepo();
291 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
293 // Check if user is allowed to see this file
294 if ( !$oimage->exists() ) {
295 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
299 $user = $this->getUser();
300 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
301 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
302 throw new PermissionsError( 'suppressrevision' );
304 throw new PermissionsError( 'deletedtext' );
307 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
308 $lang = $this->getLanguage();
309 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
310 $this->targetObj
->getText(),
311 $lang->userDate( $oimage->getTimestamp(), $user ),
312 $lang->userTime( $oimage->getTimestamp(), $user ) );
313 $this->getOutput()->addHTML(
314 Xml
::openElement( 'form', array(
316 'action' => $this->getPageTitle()->getLocalURL( array(
317 'target' => $this->targetObj
->getPrefixedDBkey(),
318 'file' => $archiveName,
319 'token' => $user->getEditToken( $archiveName ),
323 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
329 $this->getOutput()->disable();
330 # We mustn't allow the output to be Squid cached, otherwise
331 # if an admin previews a deleted image, and it's cached, then
332 # a user without appropriate permissions can toddle off and
333 # nab the image, and Squid will serve it
334 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
335 $this->getRequest()->response()->header(
336 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate'
338 $this->getRequest()->response()->header( 'Pragma: no-cache' );
340 $key = $oimage->getStorageKey();
341 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
342 $repo->streamFile( $path );
346 * Get the list object for this request
349 protected function getList() {
350 if ( is_null( $this->revDelList
) ) {
351 $this->revDelList
= RevisionDeleter
::createList(
352 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
356 return $this->revDelList
;
360 * Show a list of items that we will operate on, and show a form with checkboxes
361 * which will allow the user to choose new visibility settings.
363 protected function showForm() {
366 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
367 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels
['selected'],
368 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ) );
370 $this->getOutput()->addHTML( "<ul>" );
374 $list = $this->getList();
375 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
376 for ( $list->reset(); $list->current(); $list->next() ) {
377 // @codingStandardsIgnoreEnd
378 $item = $list->current();
380 if ( !$item->canView() ) {
381 if ( !$this->submitClicked
) {
382 throw new PermissionsError( 'suppressrevision' );
384 $userAllowed = false;
388 $this->getOutput()->addHTML( $item->getHTML() );
391 if ( !$numRevisions ) {
392 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
395 $this->getOutput()->addHTML( "</ul>" );
397 $this->addUsageText();
399 // Normal sysops can always see what they did, but can't always change it
400 if ( !$userAllowed ) {
404 // Show form if the user can submit
405 if ( $this->mIsAllowed
) {
406 $out = Xml
::openElement( 'form', array( 'method' => 'post',
407 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
408 'id' => 'mw-revdel-form-revisions' ) ) .
409 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
410 $this->buildCheckBoxes() .
411 Xml
::openElement( 'table' ) .
413 '<td class="mw-label">' .
414 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
416 '<td class="mw-input">' .
417 Xml
::listDropDown( 'wpRevDeleteReasonList',
418 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
419 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
420 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
424 '<td class="mw-label">' .
425 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
427 '<td class="mw-input">' .
432 array( 'id' => 'wpReason', 'maxlength' => 100 )
437 '<td class="mw-submit">' .
438 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
439 array( 'name' => 'wpSubmit' ) ) .
442 Xml
::closeElement( 'table' ) .
443 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
444 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
445 Html
::hidden( 'type', $this->typeName
) .
446 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
447 Xml
::closeElement( 'fieldset' ) . "\n";
451 if ( $this->mIsAllowed
) {
452 $out .= Xml
::closeElement( 'form' ) . "\n";
453 // Show link to edit the dropdown reasons
454 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
455 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
456 $link = Linker
::link(
458 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
460 array( 'action' => 'edit' )
462 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
465 $this->getOutput()->addHTML( $out );
469 * Show some introductory text
470 * @todo FIXME: Wikimedia-specific policy text
472 protected function addUsageText() {
473 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
474 $this->getOutput()->wrapWikiMsg(
475 "<strong>$1</strong>\n$2", $this->typeLabels
['text'],
476 'revdelete-text-others'
479 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
480 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
483 if ( $this->mIsAllowed
) {
484 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
489 * @return string HTML
491 protected function buildCheckBoxes() {
493 // If there is just one item, use checkboxes
494 $list = $this->getList();
495 if ( $list->length() == 1 ) {
497 $bitfield = $list->current()->getBits(); // existing field
499 if ( $this->submitClicked
) {
500 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
503 foreach ( $this->checks
as $item ) {
504 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
505 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
506 list( $message, $name, $field ) = $item;
507 $innerHTML = Xml
::checkLabel(
508 $this->msg( $message )->text(),
514 if ( $field == Revision
::DELETED_RESTRICTED
) {
515 $innerHTML = "<b>$innerHTML</b>";
518 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
519 $html .= "<tr>$line</tr>\n";
522 // Otherwise, use tri-state radios
524 $html .= '<th class="mw-revdel-checkbox">'
525 . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
526 $html .= '<th class="mw-revdel-checkbox">'
527 . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
528 $html .= '<th class="mw-revdel-checkbox">'
529 . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
530 $html .= "<th></th></tr>\n";
531 foreach ( $this->checks
as $item ) {
532 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
533 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
534 list( $message, $name, $field ) = $item;
535 // If there are several items, use third state by default...
536 if ( $this->submitClicked
) {
537 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
539 $selected = -1; // use existing field
541 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
542 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
543 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
544 $label = $this->msg( $message )->escaped();
545 if ( $field == Revision
::DELETED_RESTRICTED
) {
546 $label = "<b>$label</b>";
548 $line .= "<td>$label</td>";
549 $html .= "<tr>$line</tr>\n";
559 * UI entry point for form submission.
560 * @throws PermissionsError
563 protected function submit() {
564 # Check edit token on submission
565 $token = $this->getRequest()->getVal( 'wpEditToken' );
566 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
567 $this->getOutput()->addWikiMsg( 'sessionfailure' );
571 $bitParams = $this->extractBitParams();
573 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' );
574 $comment = $listReason;
575 if ( $comment === 'other' ) {
576 $comment = $this->otherReason
;
577 } elseif ( $this->otherReason
!== '' ) {
578 // Entry from drop down menu + additional comment
579 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
580 . $this->otherReason
;
582 # Can the user set this field?
583 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1
584 && !$this->getUser()->isAllowed( 'suppressrevision' )
586 throw new PermissionsError( 'suppressrevision' );
588 # If the save went through, go to success message...
589 $status = $this->save( $bitParams, $comment, $this->targetObj
);
590 if ( $status->isGood() ) {
595 # ...otherwise, bounce back to form...
596 $this->failure( $status );
603 * Report that the submit operation succeeded
605 protected function success() {
606 // Messages: revdelete-success, logdelete-success
607 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
608 $this->getOutput()->wrapWikiMsg(
609 "<span class=\"success\">\n$1\n</span>",
610 $this->typeLabels
['success']
612 $this->wasSaved
= true;
613 $this->revDelList
->reloadFromMaster();
618 * Report that the submit operation failed
620 protected function failure( $status ) {
621 // Messages: revdelete-failure, logdelete-failure
622 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
623 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['failure'] ) );
628 * Put together an array that contains -1, 0, or the *_deleted const for each bit
632 protected function extractBitParams() {
634 foreach ( $this->checks
as $item ) {
635 list( /* message */, $name, $field ) = $item;
636 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
637 if ( $val < -1 ||
$val > 1 ) {
638 $val = -1; // -1 for existing value
640 $bitfield[$field] = $val;
642 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
643 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
650 * Do the write operations. Simple wrapper for RevDel*List::setVisibility().
651 * @param int $bitfield
652 * @param string $reason
653 * @param Title $title
656 protected function save( $bitfield, $reason, $title ) {
657 return $this->getList()->setVisibility(
658 array( 'value' => $bitfield, 'comment' => $reason )
662 protected function getGroupName() {