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 $list = $this->getList();
166 $bitfield = $list->current()->getBits();
167 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
168 $canViewSuppressedOnly = $this->getUser()->isAllowed( 'viewsuppressed' ) &&
169 !$this->getUser()->isAllowed( 'suppressrevision' );
170 $pageIsSuppressed = $bitfield & Revision
::DELETED_RESTRICTED
;
171 $this->mIsAllowed
= $this->mIsAllowed
&& !( $canViewSuppressedOnly && $pageIsSuppressed );
173 # Allow the list type to adjust the passed target
174 $this->targetObj
= RevisionDeleter
::suggestTarget(
180 $this->otherReason
= $request->getVal( 'wpReason' );
181 # We need a target page!
182 if ( is_null( $this->targetObj
) ) {
183 $output->addWikiMsg( 'undelete-header' );
187 # Give a link to the logs/hist for this page
188 $this->showConvenienceLinks();
190 # Initialise checkboxes
191 $this->checks
= array(
192 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
193 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
194 RevisionDeleter
::getRevdelConstant( $this->typeName
)
196 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
197 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
199 if ( $user->isAllowed( 'suppressrevision' ) ) {
200 $this->checks
[] = array( 'revdelete-hide-restricted',
201 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
204 # Either submit or create our form
205 if ( $this->mIsAllowed
&& $this->submitClicked
) {
206 $this->submit( $request );
211 $qc = $this->getLogQueryCond();
212 # Show relevant lines from the deletion log
213 $deleteLogPage = new LogPage( 'delete' );
214 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
215 LogEventsList
::showLogExtract(
220 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
222 # Show relevant lines from the suppression log
223 if ( $user->isAllowed( 'suppressionlog' ) ) {
224 $suppressLogPage = new LogPage( 'suppress' );
225 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
226 LogEventsList
::showLogExtract(
231 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
237 * Show some useful links in the subtitle
239 protected function showConvenienceLinks() {
240 # Give a link to the logs/hist for this page
241 if ( $this->targetObj
) {
242 // Also set header tabs to be for the target.
243 $this->getSkin()->setRelevantTitle( $this->targetObj
);
246 $links[] = Linker
::linkKnown(
247 SpecialPage
::getTitleFor( 'Log' ),
248 $this->msg( 'viewpagelogs' )->escaped(),
250 array( 'page' => $this->targetObj
->getPrefixedText() )
252 if ( !$this->targetObj
->isSpecialPage() ) {
253 # Give a link to the page history
254 $links[] = Linker
::linkKnown(
256 $this->msg( 'pagehist' )->escaped(),
258 array( 'action' => 'history' )
260 # Link to deleted edits
261 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
262 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
263 $links[] = Linker
::linkKnown(
265 $this->msg( 'deletedhist' )->escaped(),
267 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
271 # Logs themselves don't have histories or archived revisions
272 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
277 * Get the condition used for fetching log snippets
280 protected function getLogQueryCond() {
282 // Revision delete logs for these item
283 $conds['log_type'] = array( 'delete', 'suppress' );
284 $conds['log_action'] = $this->getList()->getLogAction();
285 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
286 $conds['ls_value'] = $this->ids
;
292 * Show a deleted file version requested by the visitor.
293 * @todo Mostly copied from Special:Undelete. Refactor.
294 * @param string $archiveName
296 protected function tryShowFile( $archiveName ) {
297 $repo = RepoGroup
::singleton()->getLocalRepo();
298 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
300 // Check if user is allowed to see this file
301 if ( !$oimage->exists() ) {
302 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
306 $user = $this->getUser();
307 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
308 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
309 throw new PermissionsError( 'suppressrevision' );
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(
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() ) .
336 $this->getOutput()->disable();
337 # We mustn't allow the output to be Squid 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 Squid 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
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() {
373 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
374 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels
['selected'],
375 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ) );
377 $this->getOutput()->addHTML( "<ul>" );
381 $list = $this->getList();
382 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
383 for ( $list->reset(); $list->current(); $list->next() ) {
384 // @codingStandardsIgnoreEnd
385 $item = $list->current();
387 if ( !$item->canView() ) {
388 if ( !$this->submitClicked
) {
389 throw new PermissionsError( 'suppressrevision' );
391 $userAllowed = false;
395 $this->getOutput()->addHTML( $item->getHTML() );
398 if ( !$numRevisions ) {
399 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
402 $this->getOutput()->addHTML( "</ul>" );
404 $this->addUsageText();
406 // Normal sysops can always see what they did, but can't always change it
407 if ( !$userAllowed ) {
411 // Show form if the user can submit
412 if ( $this->mIsAllowed
) {
413 $out = Xml
::openElement( 'form', array( 'method' => 'post',
414 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
415 'id' => 'mw-revdel-form-revisions' ) ) .
416 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
417 $this->buildCheckBoxes() .
418 Xml
::openElement( 'table' ) .
420 '<td class="mw-label">' .
421 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
423 '<td class="mw-input">' .
424 Xml
::listDropDown( 'wpRevDeleteReasonList',
425 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
426 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
427 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown'
431 '<td class="mw-label">' .
432 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
434 '<td class="mw-input">' .
439 array( 'id' => 'wpReason', 'maxlength' => 100 )
444 '<td class="mw-submit">' .
445 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
446 array( 'name' => 'wpSubmit' ) ) .
449 Xml
::closeElement( 'table' ) .
450 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
451 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
452 Html
::hidden( 'type', $this->typeName
) .
453 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
454 Xml
::closeElement( 'fieldset' ) . "\n" .
455 Xml
::closeElement( 'form' ) . "\n";
456 // Show link to edit the dropdown reasons
457 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
458 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
459 $link = Linker
::link(
461 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
463 array( 'action' => 'edit' )
465 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
470 $this->getOutput()->addHTML( $out );
474 * Show some introductory text
475 * @todo FIXME: Wikimedia-specific policy text
477 protected function addUsageText() {
478 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
479 $this->getOutput()->wrapWikiMsg(
480 "<strong>$1</strong>\n$2", $this->typeLabels
['text'],
481 'revdelete-text-others'
484 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
485 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
488 if ( $this->mIsAllowed
) {
489 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
494 * @return string HTML
496 protected function buildCheckBoxes() {
498 // If there is just one item, use checkboxes
499 $list = $this->getList();
500 if ( $list->length() == 1 ) {
502 $bitfield = $list->current()->getBits(); // existing field
504 if ( $this->submitClicked
) {
505 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
508 foreach ( $this->checks
as $item ) {
509 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
510 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
511 list( $message, $name, $field ) = $item;
512 $innerHTML = Xml
::checkLabel(
513 $this->msg( $message )->text(),
519 if ( $field == Revision
::DELETED_RESTRICTED
) {
520 $innerHTML = "<b>$innerHTML</b>";
523 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
524 $html .= "<tr>$line</tr>\n";
527 // Otherwise, use tri-state radios
529 $html .= '<th class="mw-revdel-checkbox">'
530 . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
531 $html .= '<th class="mw-revdel-checkbox">'
532 . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
533 $html .= '<th class="mw-revdel-checkbox">'
534 . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
535 $html .= "<th></th></tr>\n";
536 foreach ( $this->checks
as $item ) {
537 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
538 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
539 list( $message, $name, $field ) = $item;
540 // If there are several items, use third state by default...
541 if ( $this->submitClicked
) {
542 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
544 $selected = -1; // use existing field
546 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
547 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
548 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
549 $label = $this->msg( $message )->escaped();
550 if ( $field == Revision
::DELETED_RESTRICTED
) {
551 $label = "<b>$label</b>";
553 $line .= "<td>$label</td>";
554 $html .= "<tr>$line</tr>\n";
564 * UI entry point for form submission.
565 * @throws PermissionsError
568 protected function submit() {
569 # Check edit token on submission
570 $token = $this->getRequest()->getVal( 'wpEditToken' );
571 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
572 $this->getOutput()->addWikiMsg( 'sessionfailure' );
576 $bitParams = $this->extractBitParams();
578 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' );
579 $comment = $listReason;
580 if ( $comment === 'other' ) {
581 $comment = $this->otherReason
;
582 } elseif ( $this->otherReason
!== '' ) {
583 // Entry from drop down menu + additional comment
584 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
585 . $this->otherReason
;
587 # Can the user set this field?
588 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1
589 && !$this->getUser()->isAllowed( 'suppressrevision' )
591 throw new PermissionsError( 'suppressrevision' );
593 # If the save went through, go to success message...
594 $status = $this->save( $bitParams, $comment, $this->targetObj
);
595 if ( $status->isGood() ) {
600 # ...otherwise, bounce back to form...
601 $this->failure( $status );
608 * Report that the submit operation succeeded
610 protected function success() {
611 // Messages: revdelete-success, logdelete-success
612 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
613 $this->getOutput()->wrapWikiMsg(
614 "<span class=\"success\">\n$1\n</span>",
615 $this->typeLabels
['success']
617 $this->wasSaved
= true;
618 $this->revDelList
->reloadFromMaster();
623 * Report that the submit operation failed
625 protected function failure( $status ) {
626 // Messages: revdelete-failure, logdelete-failure
627 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
628 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['failure'] ) );
633 * Put together an array that contains -1, 0, or the *_deleted const for each bit
637 protected function extractBitParams() {
639 foreach ( $this->checks
as $item ) {
640 list( /* message */, $name, $field ) = $item;
641 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
642 if ( $val < -1 ||
$val > 1 ) {
643 $val = -1; // -1 for existing value
645 $bitfield[$field] = $val;
647 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
648 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
655 * Do the write operations. Simple wrapper for RevDel*List::setVisibility().
656 * @param int $bitfield
657 * @param string $reason
658 * @param Title $title
661 protected function save( $bitfield, $reason, $title ) {
662 return $this->getList()->setVisibility(
663 array( 'value' => $bitfield, 'comment' => $reason )
667 protected function getGroupName() {