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 /** True if the submit button was clicked, and the form was posted */
37 /** Archive name, for reviewing deleted files */
40 /** Edit token for securing image views against XSS */
43 /** Title object for target parameter */
46 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
49 /** Array of checkbox specs (message, name, deletion bits) */
52 /** UI Labels about the current type */
55 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
58 /** Was the DB modified in this request */
59 protected $wasSaved = false;
62 * UI labels for each type.
64 static $UILabels = array(
66 'check-label' => 'revdelete-hide-text',
67 'success' => 'revdelete-success',
68 'failure' => 'revdelete-failure',
69 'text' => 'revdelete-text-text',
70 'selected'=> 'revdelete-selected-text',
73 'check-label' => 'revdelete-hide-text',
74 'success' => 'revdelete-success',
75 'failure' => 'revdelete-failure',
76 'text' => 'revdelete-text-text',
77 'selected'=> 'revdelete-selected-text',
80 'check-label' => 'revdelete-hide-image',
81 'success' => 'revdelete-success',
82 'failure' => 'revdelete-failure',
83 'text' => 'revdelete-text-file',
84 'selected'=> 'revdelete-selected-file',
86 'filearchive' => array(
87 'check-label' => 'revdelete-hide-image',
88 'success' => 'revdelete-success',
89 'failure' => 'revdelete-failure',
90 'text' => 'revdelete-text-file',
91 'selected'=> 'revdelete-selected-file',
94 'check-label' => 'revdelete-hide-name',
95 'success' => 'logdelete-success',
96 'failure' => 'logdelete-failure',
97 'text' => 'logdelete-text',
98 'selected' => 'logdelete-selected',
102 public function __construct() {
103 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
106 public function execute( $par ) {
107 $this->checkPermissions();
108 $this->checkReadOnly();
110 $output = $this->getOutput();
111 $user = $this->getUser();
114 $this->outputHeader();
115 $request = $this->getRequest();
116 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
117 # Handle our many different possible input types.
118 $ids = $request->getVal( 'ids' );
119 if ( !is_null( $ids ) ) {
120 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
121 $this->ids
= explode( ',', $ids );
124 $this->ids
= array_keys( $request->getArray( 'ids', array() ) );
126 // $this->ids = array_map( 'intval', $this->ids );
127 $this->ids
= array_unique( array_filter( $this->ids
) );
129 if ( $request->getVal( 'action' ) == 'historysubmit' ||
$request->getVal( 'action' ) == 'revisiondelete' ) {
130 // For show/hide form submission from history page
131 // Since we are access through index.php?title=XXX&action=historysubmit
132 // getFullTitle() will contain the target title and not our title
133 $this->targetObj
= $this->getFullTitle();
134 $this->typeName
= 'revision';
136 $this->typeName
= $request->getVal( 'type' );
137 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
140 # For reviewing deleted files...
141 $this->archiveName
= $request->getVal( 'file' );
142 $this->token
= $request->getVal( 'token' );
143 if ( $this->archiveName
&& $this->targetObj
) {
144 $this->tryShowFile( $this->archiveName
);
149 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
152 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
153 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
155 $this->typeLabels
= self
::$UILabels[$this->typeName
];
156 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
158 # Allow the list type to adjust the passed target
159 $this->targetObj
= RevisionDeleter
::suggestTarget( $this->typeName
, $this->targetObj
, $this->ids
);
161 $this->otherReason
= $request->getVal( 'wpReason' );
162 # We need a target page!
163 if ( is_null( $this->targetObj
) ) {
164 $output->addWikiMsg( 'undelete-header' );
168 # Give a link to the logs/hist for this page
169 $this->showConvenienceLinks();
171 # Initialise checkboxes
172 $this->checks
= array(
173 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
174 RevisionDeleter
::getRevdelConstant( $this->typeName
)
176 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
177 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
179 if ( $user->isAllowed( 'suppressrevision' ) ) {
180 $this->checks
[] = array( 'revdelete-hide-restricted',
181 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
184 # Either submit or create our form
185 if ( $this->mIsAllowed
&& $this->submitClicked
) {
186 $this->submit( $request );
191 $qc = $this->getLogQueryCond();
192 # Show relevant lines from the deletion log
193 $deleteLogPage = new LogPage( 'delete' );
194 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
195 LogEventsList
::showLogExtract(
200 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
202 # Show relevant lines from the suppression log
203 if ( $user->isAllowed( 'suppressionlog' ) ) {
204 $suppressLogPage = new LogPage( 'suppress' );
205 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
206 LogEventsList
::showLogExtract(
211 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
217 * Show some useful links in the subtitle
219 protected function showConvenienceLinks() {
220 # Give a link to the logs/hist for this page
221 if ( $this->targetObj
) {
222 // Also set header tabs to be for the target.
223 $this->getSkin()->setRelevantTitle( $this->targetObj
);
226 $links[] = Linker
::linkKnown(
227 SpecialPage
::getTitleFor( 'Log' ),
228 $this->msg( 'viewpagelogs' )->escaped(),
230 array( 'page' => $this->targetObj
->getPrefixedText() )
232 if ( !$this->targetObj
->isSpecialPage() ) {
233 # Give a link to the page history
234 $links[] = Linker
::linkKnown(
236 $this->msg( 'pagehist' )->escaped(),
238 array( 'action' => 'history' )
240 # Link to deleted edits
241 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
242 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
243 $links[] = Linker
::linkKnown(
245 $this->msg( 'deletedhist' )->escaped(),
247 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
251 # Logs themselves don't have histories or archived revisions
252 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
257 * Get the condition used for fetching log snippets
260 protected function getLogQueryCond() {
262 // Revision delete logs for these item
263 $conds['log_type'] = array( 'delete', 'suppress' );
264 $conds['log_action'] = $this->getList()->getLogAction();
265 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
266 $conds['ls_value'] = $this->ids
;
272 * Show a deleted file version requested by the visitor.
273 * TODO Mostly copied from Special:Undelete. Refactor.
275 protected function tryShowFile( $archiveName ) {
276 $repo = RepoGroup
::singleton()->getLocalRepo();
277 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
279 // Check if user is allowed to see this file
280 if ( !$oimage->exists() ) {
281 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
285 $user = $this->getUser();
286 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
287 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
288 throw new PermissionsError( 'suppressrevision' );
290 throw new PermissionsError( 'deletedtext' );
293 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
294 $lang = $this->getLanguage();
295 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
296 $this->targetObj
->getText(),
297 $lang->userDate( $oimage->getTimestamp(), $user ),
298 $lang->userTime( $oimage->getTimestamp(), $user ) );
299 $this->getOutput()->addHTML(
300 Xml
::openElement( 'form', array(
302 'action' => $this->getPageTitle()->getLocalURL( array(
303 'target' => $this->targetObj
->getPrefixedDBkey(),
304 'file' => $archiveName,
305 'token' => $user->getEditToken( $archiveName ),
309 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
315 $this->getOutput()->disable();
316 # We mustn't allow the output to be Squid cached, otherwise
317 # if an admin previews a deleted image, and it's cached, then
318 # a user without appropriate permissions can toddle off and
319 # nab the image, and Squid will serve it
320 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
321 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
322 $this->getRequest()->response()->header( 'Pragma: no-cache' );
324 $key = $oimage->getStorageKey();
325 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
326 $repo->streamFile( $path );
330 * Get the list object for this request
332 protected function getList() {
333 if ( is_null( $this->list ) ) {
334 $this->list = RevisionDeleter
::createList(
335 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
343 * Show a list of items that we will operate on, and show a form with checkboxes
344 * which will allow the user to choose new visibility settings.
346 protected function showForm() {
349 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels
['selected'],
350 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ) );
352 $this->getOutput()->addHTML( "<ul>" );
356 $list = $this->getList();
357 for ( $list->reset(); $list->current(); $list->next() ) {
358 $item = $list->current();
359 if ( !$item->canView() ) {
360 if ( !$this->submitClicked
) {
361 throw new PermissionsError( 'suppressrevision' );
363 $userAllowed = false;
366 $this->getOutput()->addHTML( $item->getHTML() );
369 if ( !$numRevisions ) {
370 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
373 $this->getOutput()->addHTML( "</ul>" );
375 $this->addUsageText();
377 // Normal sysops can always see what they did, but can't always change it
378 if ( !$userAllowed ) {
382 // Show form if the user can submit
383 if ( $this->mIsAllowed
) {
384 $out = Xml
::openElement( 'form', array( 'method' => 'post',
385 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
386 'id' => 'mw-revdel-form-revisions' ) ) .
387 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
388 $this->buildCheckBoxes() .
389 Xml
::openElement( 'table' ) .
391 '<td class="mw-label">' .
392 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
394 '<td class="mw-input">' .
395 Xml
::listDropDown( 'wpRevDeleteReasonList',
396 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
397 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
398 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
402 '<td class="mw-label">' .
403 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
405 '<td class="mw-input">' .
406 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
410 '<td class="mw-submit">' .
411 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
412 array( 'name' => 'wpSubmit' ) ) .
415 Xml
::closeElement( 'table' ) .
416 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
417 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
418 Html
::hidden( 'type', $this->typeName
) .
419 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
420 Xml
::closeElement( 'fieldset' ) . "\n";
424 if ( $this->mIsAllowed
) {
425 $out .= Xml
::closeElement( 'form' ) . "\n";
426 // Show link to edit the dropdown reasons
427 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
428 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
429 $link = Linker
::link(
431 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
433 array( 'action' => 'edit' )
435 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
438 $this->getOutput()->addHTML( $out );
442 * Show some introductory text
443 * @todo FIXME: Wikimedia-specific policy text
445 protected function addUsageText() {
446 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>\n$2", $this->typeLabels
['text'], 'revdelete-text-others' );
447 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
448 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
450 if ( $this->mIsAllowed
) {
451 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
456 * @return String: HTML
458 protected function buildCheckBoxes() {
460 // If there is just one item, use checkboxes
461 $list = $this->getList();
462 if ( $list->length() == 1 ) {
464 $bitfield = $list->current()->getBits(); // existing field
465 if ( $this->submitClicked
) {
466 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
468 foreach ( $this->checks
as $item ) {
469 list( $message, $name, $field ) = $item;
470 $innerHTML = Xml
::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
471 if ( $field == Revision
::DELETED_RESTRICTED
) {
472 $innerHTML = "<b>$innerHTML</b>";
474 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
475 $html .= "<tr>$line</tr>\n";
478 // Otherwise, use tri-state radios
480 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
481 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
482 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
483 $html .= "<th></th></tr>\n";
484 foreach ( $this->checks
as $item ) {
485 list( $message, $name, $field ) = $item;
486 // If there are several items, use third state by default...
487 if ( $this->submitClicked
) {
488 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
490 $selected = -1; // use existing field
492 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
493 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
494 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
495 $label = $this->msg( $message )->escaped();
496 if ( $field == Revision
::DELETED_RESTRICTED
) {
497 $label = "<b>$label</b>";
499 $line .= "<td>$label</td>";
500 $html .= "<tr>$line</tr>\n";
510 * UI entry point for form submission.
511 * @throws PermissionsError
514 protected function submit() {
515 # Check edit token on submission
516 $token = $this->getRequest()->getVal( 'wpEditToken' );
517 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
518 $this->getOutput()->addWikiMsg( 'sessionfailure' );
522 $bitParams = $this->extractBitParams();
523 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
524 $comment = $listReason;
525 if ( $comment != 'other' && $this->otherReason
!= '' ) {
526 // Entry from drop down menu + additional comment
527 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason
;
528 } elseif ( $comment == 'other' ) {
529 $comment = $this->otherReason
;
531 # Can the user set this field?
532 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1 && !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
533 throw new PermissionsError( 'suppressrevision' );
535 # If the save went through, go to success message...
536 $status = $this->save( $bitParams, $comment, $this->targetObj
);
537 if ( $status->isGood() ) {
542 # ...otherwise, bounce back to form...
543 $this->failure( $status );
550 * Report that the submit operation succeeded
552 protected function success() {
553 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
554 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeLabels
['success'] );
555 $this->wasSaved
= true;
556 $this->list->reloadFromMaster();
561 * Report that the submit operation failed
563 protected function failure( $status ) {
564 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
565 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['failure'] ) );
570 * Put together an array that contains -1, 0, or the *_deleted const for each bit
574 protected function extractBitParams() {
576 foreach ( $this->checks
as $item ) {
577 list( /* message */, $name, $field ) = $item;
578 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
579 if ( $val < -1 ||
$val > 1 ) {
580 $val = -1; // -1 for existing value
582 $bitfield[$field] = $val;
584 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
585 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
592 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
598 protected function save( $bitfield, $reason, $title ) {
599 return $this->getList()->setVisibility(
600 array( 'value' => $bitfield, 'comment' => $reason )
604 protected function getGroupName() {