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' );
164 # Allow the list type to adjust the passed target
165 $this->targetObj
= RevisionDeleter
::suggestTarget(
171 $this->typeLabels
= self
::$UILabels[$this->typeName
];
172 $list = $this->getList();
174 $bitfield = $list->current()->getBits();
175 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
176 $canViewSuppressedOnly = $this->getUser()->isAllowed( 'viewsuppressed' ) &&
177 !$this->getUser()->isAllowed( 'suppressrevision' );
178 $pageIsSuppressed = $bitfield & Revision
::DELETED_RESTRICTED
;
179 $this->mIsAllowed
= $this->mIsAllowed
&& !( $canViewSuppressedOnly && $pageIsSuppressed );
181 $this->otherReason
= $request->getVal( 'wpReason' );
182 # We need a target page!
183 if ( is_null( $this->targetObj
) ) {
184 $output->addWikiMsg( 'undelete-header' );
188 # Give a link to the logs/hist for this page
189 $this->showConvenienceLinks();
191 # Initialise checkboxes
192 $this->checks
= array(
193 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
194 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
195 RevisionDeleter
::getRevdelConstant( $this->typeName
)
197 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
198 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
200 if ( $user->isAllowed( 'suppressrevision' ) ) {
201 $this->checks
[] = array( 'revdelete-hide-restricted',
202 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
205 # Either submit or create our form
206 if ( $this->mIsAllowed
&& $this->submitClicked
) {
207 $this->submit( $request );
212 $qc = $this->getLogQueryCond();
213 # Show relevant lines from the deletion log
214 $deleteLogPage = new LogPage( 'delete' );
215 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
216 LogEventsList
::showLogExtract(
221 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
223 # Show relevant lines from the suppression log
224 if ( $user->isAllowed( 'suppressionlog' ) ) {
225 $suppressLogPage = new LogPage( 'suppress' );
226 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
227 LogEventsList
::showLogExtract(
232 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
238 * Show some useful links in the subtitle
240 protected function showConvenienceLinks() {
241 # Give a link to the logs/hist for this page
242 if ( $this->targetObj
) {
243 // Also set header tabs to be for the target.
244 $this->getSkin()->setRelevantTitle( $this->targetObj
);
247 $links[] = Linker
::linkKnown(
248 SpecialPage
::getTitleFor( 'Log' ),
249 $this->msg( 'viewpagelogs' )->escaped(),
251 array( 'page' => $this->targetObj
->getPrefixedText() )
253 if ( !$this->targetObj
->isSpecialPage() ) {
254 # Give a link to the page history
255 $links[] = Linker
::linkKnown(
257 $this->msg( 'pagehist' )->escaped(),
259 array( 'action' => 'history' )
261 # Link to deleted edits
262 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
263 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
264 $links[] = Linker
::linkKnown(
266 $this->msg( 'deletedhist' )->escaped(),
268 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
272 # Logs themselves don't have histories or archived revisions
273 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
278 * Get the condition used for fetching log snippets
281 protected function getLogQueryCond() {
283 // Revision delete logs for these item
284 $conds['log_type'] = array( 'delete', 'suppress' );
285 $conds['log_action'] = $this->getList()->getLogAction();
286 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
287 $conds['ls_value'] = $this->ids
;
293 * Show a deleted file version requested by the visitor.
294 * @todo Mostly copied from Special:Undelete. Refactor.
295 * @param string $archiveName
296 * @throws MWException
297 * @throws PermissionsError
299 protected function tryShowFile( $archiveName ) {
300 $repo = RepoGroup
::singleton()->getLocalRepo();
301 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
303 // Check if user is allowed to see this file
304 if ( !$oimage->exists() ) {
305 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
309 $user = $this->getUser();
310 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
311 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
312 throw new PermissionsError( 'suppressrevision' );
314 throw new PermissionsError( 'deletedtext' );
317 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
318 $lang = $this->getLanguage();
319 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
320 $this->targetObj
->getText(),
321 $lang->userDate( $oimage->getTimestamp(), $user ),
322 $lang->userTime( $oimage->getTimestamp(), $user ) );
323 $this->getOutput()->addHTML(
324 Xml
::openElement( 'form', array(
326 'action' => $this->getPageTitle()->getLocalURL( array(
327 'target' => $this->targetObj
->getPrefixedDBkey(),
328 'file' => $archiveName,
329 'token' => $user->getEditToken( $archiveName ),
333 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
339 $this->getOutput()->disable();
340 # We mustn't allow the output to be Squid cached, otherwise
341 # if an admin previews a deleted image, and it's cached, then
342 # a user without appropriate permissions can toddle off and
343 # nab the image, and Squid will serve it
344 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
345 $this->getRequest()->response()->header(
346 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate'
348 $this->getRequest()->response()->header( 'Pragma: no-cache' );
350 $key = $oimage->getStorageKey();
351 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
352 $repo->streamFile( $path );
356 * Get the list object for this request
359 protected function getList() {
360 if ( is_null( $this->revDelList
) ) {
361 $this->revDelList
= RevisionDeleter
::createList(
362 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
366 return $this->revDelList
;
370 * Show a list of items that we will operate on, and show a form with checkboxes
371 * which will allow the user to choose new visibility settings.
373 protected function showForm() {
376 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
377 $out = $this->getOutput();
378 $out->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels
['selected'],
379 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ) );
381 $out->addHelpLink( 'Help:RevisionDelete' );
382 $out->addHTML( "<ul>" );
386 $list = $this->getList();
387 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
388 for ( $list->reset(); $list->current(); $list->next() ) {
389 // @codingStandardsIgnoreEnd
390 $item = $list->current();
392 if ( !$item->canView() ) {
393 if ( !$this->submitClicked
) {
394 throw new PermissionsError( 'suppressrevision' );
396 $userAllowed = false;
400 $out->addHTML( $item->getHTML() );
403 if ( !$numRevisions ) {
404 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
407 $out->addHTML( "</ul>" );
409 $this->addUsageText();
411 // Normal sysops can always see what they did, but can't always change it
412 if ( !$userAllowed ) {
416 // Show form if the user can submit
417 if ( $this->mIsAllowed
) {
418 $form = Xml
::openElement( 'form', array( 'method' => 'post',
419 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
420 'id' => 'mw-revdel-form-revisions' ) ) .
421 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
422 $this->buildCheckBoxes() .
423 Xml
::openElement( 'table' ) .
425 '<td class="mw-label">' .
426 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
428 '<td class="mw-input">' .
429 Xml
::listDropDown( 'wpRevDeleteReasonList',
430 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
431 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
432 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown'
436 '<td class="mw-label">' .
437 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
439 '<td class="mw-input">' .
444 array( 'id' => 'wpReason', 'maxlength' => 100 )
449 '<td class="mw-submit">' .
450 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
451 array( 'name' => 'wpSubmit' ) ) .
454 Xml
::closeElement( 'table' ) .
455 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
456 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
457 Html
::hidden( 'type', $this->typeName
) .
458 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
459 Xml
::closeElement( 'fieldset' ) . "\n" .
460 Xml
::closeElement( 'form' ) . "\n";
461 // Show link to edit the dropdown reasons
462 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
463 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
464 $link = Linker
::link(
466 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
468 array( 'action' => 'edit' )
470 $form .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
475 $out->addHTML( $form );
479 * Show some introductory text
480 * @todo FIXME: Wikimedia-specific policy text
482 protected function addUsageText() {
483 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
484 $this->getOutput()->wrapWikiMsg(
485 "<strong>$1</strong>\n$2", $this->typeLabels
['text'],
486 'revdelete-text-others'
489 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
490 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
493 if ( $this->mIsAllowed
) {
494 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
499 * @return string HTML
501 protected function buildCheckBoxes() {
503 // If there is just one item, use checkboxes
504 $list = $this->getList();
505 if ( $list->length() == 1 ) {
507 $bitfield = $list->current()->getBits(); // existing field
509 if ( $this->submitClicked
) {
510 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
513 foreach ( $this->checks
as $item ) {
514 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
515 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
516 list( $message, $name, $field ) = $item;
517 $innerHTML = Xml
::checkLabel(
518 $this->msg( $message )->text(),
524 if ( $field == Revision
::DELETED_RESTRICTED
) {
525 $innerHTML = "<b>$innerHTML</b>";
528 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
529 $html .= "<tr>$line</tr>\n";
532 // Otherwise, use tri-state radios
534 $html .= '<th class="mw-revdel-checkbox">'
535 . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
536 $html .= '<th class="mw-revdel-checkbox">'
537 . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
538 $html .= '<th class="mw-revdel-checkbox">'
539 . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
540 $html .= "<th></th></tr>\n";
541 foreach ( $this->checks
as $item ) {
542 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
543 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
544 list( $message, $name, $field ) = $item;
545 // If there are several items, use third state by default...
546 if ( $this->submitClicked
) {
547 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
549 $selected = -1; // use existing field
551 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
552 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
553 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
554 $label = $this->msg( $message )->escaped();
555 if ( $field == Revision
::DELETED_RESTRICTED
) {
556 $label = "<b>$label</b>";
558 $line .= "<td>$label</td>";
559 $html .= "<tr>$line</tr>\n";
569 * UI entry point for form submission.
570 * @throws PermissionsError
573 protected function submit() {
574 # Check edit token on submission
575 $token = $this->getRequest()->getVal( 'wpEditToken' );
576 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
577 $this->getOutput()->addWikiMsg( 'sessionfailure' );
581 $bitParams = $this->extractBitParams();
583 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' );
584 $comment = $listReason;
585 if ( $comment === 'other' ) {
586 $comment = $this->otherReason
;
587 } elseif ( $this->otherReason
!== '' ) {
588 // Entry from drop down menu + additional comment
589 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
590 . $this->otherReason
;
592 # Can the user set this field?
593 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1
594 && !$this->getUser()->isAllowed( 'suppressrevision' )
596 throw new PermissionsError( 'suppressrevision' );
598 # If the save went through, go to success message...
599 $status = $this->save( $bitParams, $comment, $this->targetObj
);
600 if ( $status->isGood() ) {
605 # ...otherwise, bounce back to form...
606 $this->failure( $status );
613 * Report that the submit operation succeeded
615 protected function success() {
616 // Messages: revdelete-success, logdelete-success
617 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
618 $this->getOutput()->wrapWikiMsg(
619 "<span class=\"success\">\n$1\n</span>",
620 $this->typeLabels
['success']
622 $this->wasSaved
= true;
623 $this->revDelList
->reloadFromMaster();
628 * Report that the submit operation failed
629 * @param Status $status
631 protected function failure( $status ) {
632 // Messages: revdelete-failure, logdelete-failure
633 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
634 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['failure'] ) );
639 * Put together an array that contains -1, 0, or the *_deleted const for each bit
643 protected function extractBitParams() {
645 foreach ( $this->checks
as $item ) {
646 list( /* message */, $name, $field ) = $item;
647 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
648 if ( $val < -1 ||
$val > 1 ) {
649 $val = -1; // -1 for existing value
651 $bitfield[$field] = $val;
653 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
654 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
661 * Do the write operations. Simple wrapper for RevDel*List::setVisibility().
662 * @param int $bitfield
663 * @param string $reason
664 * @param Title $title
667 protected function save( $bitfield, $reason, $title ) {
668 return $this->getList()->setVisibility(
669 array( 'value' => $bitfield, 'comment' => $reason )
673 protected function getGroupName() {