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 /** Information about the current type */
55 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
59 * Assorted information about each type, needed by the special page.
60 * TODO Move some of this to the list class
62 static $allowedTypes = array(
64 'check-label' => 'revdelete-hide-text',
65 'deletion-bits' => Revision
::DELETED_TEXT
,
66 'success' => 'revdelete-success',
67 'failure' => 'revdelete-failure',
68 'list-class' => 'RevDel_RevisionList',
69 'permission' => 'deleterevision',
72 'check-label' => 'revdelete-hide-text',
73 'deletion-bits' => Revision
::DELETED_TEXT
,
74 'success' => 'revdelete-success',
75 'failure' => 'revdelete-failure',
76 'list-class' => 'RevDel_ArchiveList',
77 'permission' => 'deleterevision',
80 'check-label' => 'revdelete-hide-image',
81 'deletion-bits' => File
::DELETED_FILE
,
82 'success' => 'revdelete-success',
83 'failure' => 'revdelete-failure',
84 'list-class' => 'RevDel_FileList',
85 'permission' => 'deleterevision',
87 'filearchive' => array(
88 'check-label' => 'revdelete-hide-image',
89 'deletion-bits' => File
::DELETED_FILE
,
90 'success' => 'revdelete-success',
91 'failure' => 'revdelete-failure',
92 'list-class' => 'RevDel_ArchivedFileList',
93 'permission' => 'deleterevision',
96 'check-label' => 'revdelete-hide-name',
97 'deletion-bits' => LogPage
::DELETED_ACTION
,
98 'success' => 'logdelete-success',
99 'failure' => 'logdelete-failure',
100 'list-class' => 'RevDel_LogList',
101 'permission' => 'deletelogentry',
105 /** Type map to support old log entries */
106 static $deprecatedTypeMap = array(
107 'oldid' => 'revision',
108 'artimestamp' => 'archive',
109 'oldimage' => 'oldimage',
110 'fileid' => 'filearchive',
111 'logid' => 'logging',
114 public function __construct() {
115 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
118 public function execute( $par ) {
119 $this->checkPermissions();
120 $this->checkReadOnly();
122 $output = $this->getOutput();
123 $user = $this->getUser();
126 $this->outputHeader();
127 $request = $this->getRequest();
128 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
129 # Handle our many different possible input types.
130 $ids = $request->getVal( 'ids' );
131 if ( !is_null( $ids ) ) {
132 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
133 $this->ids
= explode( ',', $ids );
136 $this->ids
= array_keys( $request->getArray('ids',array()) );
138 // $this->ids = array_map( 'intval', $this->ids );
139 $this->ids
= array_unique( array_filter( $this->ids
) );
141 if ( $request->getVal( 'action' ) == 'historysubmit' ||
$request->getVal( 'action' ) == 'revisiondelete' ) {
142 // For show/hide form submission from history page
143 // Since we are access through index.php?title=XXX&action=historysubmit
144 // getFullTitle() will contain the target title and not our title
145 $this->targetObj
= $this->getFullTitle();
146 $this->typeName
= 'revision';
148 $this->typeName
= $request->getVal( 'type' );
149 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
152 # For reviewing deleted files...
153 $this->archiveName
= $request->getVal( 'file' );
154 $this->token
= $request->getVal( 'token' );
155 if ( $this->archiveName
&& $this->targetObj
) {
156 $this->tryShowFile( $this->archiveName
);
160 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
161 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
165 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
166 $output->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
169 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
170 $this->mIsAllowed
= $user->isAllowed( $this->typeInfo
['permission'] );
172 # If we have revisions, get the title from the first one
173 # since they should all be from the same page. This allows
174 # for more flexibility with page moves...
175 if( $this->typeName
== 'revision' ) {
176 $rev = Revision
::newFromId( $this->ids
[0] );
177 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
180 $this->otherReason
= $request->getVal( 'wpReason' );
181 # We need a target page!
182 if( is_null($this->targetObj
) ) {
183 $output->addWikiMsg( 'undelete-header' );
186 # Give a link to the logs/hist for this page
187 $this->showConvenienceLinks();
189 # Initialise checkboxes
190 $this->checks
= array(
191 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
192 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
193 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
195 if( $user->isAllowed('suppressrevision') ) {
196 $this->checks
[] = array( 'revdelete-hide-restricted',
197 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
200 # Either submit or create our form
201 if( $this->mIsAllowed
&& $this->submitClicked
) {
202 $this->submit( $request );
207 $qc = $this->getLogQueryCond();
208 # Show relevant lines from the deletion log
209 $output->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
210 LogEventsList
::showLogExtract( $output, 'delete',
211 $this->targetObj
, '', array( 'lim' => 25, 'conds' => $qc ) );
212 # Show relevant lines from the suppression log
213 if( $user->isAllowed( 'suppressionlog' ) ) {
214 $output->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
215 LogEventsList
::showLogExtract( $output, 'suppress',
216 $this->targetObj
, '', array( 'lim' => 25, 'conds' => $qc ) );
221 * Show some useful links in the subtitle
223 protected function showConvenienceLinks() {
224 # Give a link to the logs/hist for this page
225 if( $this->targetObj
) {
227 $links[] = Linker
::linkKnown(
228 SpecialPage
::getTitleFor( 'Log' ),
229 $this->msg( 'viewpagelogs' )->escaped(),
231 array( 'page' => $this->targetObj
->getPrefixedText() )
233 if ( !$this->targetObj
->isSpecialPage() ) {
234 # Give a link to the page history
235 $links[] = Linker
::linkKnown(
237 $this->msg( 'pagehist' )->escaped(),
239 array( 'action' => 'history' )
241 # Link to deleted edits
242 if( $this->getUser()->isAllowed('undelete') ) {
243 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
244 $links[] = Linker
::linkKnown(
246 $this->msg( 'deletedhist' )->escaped(),
248 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
252 # Logs themselves don't have histories or archived revisions
253 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
258 * Get the condition used for fetching log snippets
261 protected function getLogQueryCond() {
263 // Revision delete logs for these item
264 $conds['log_type'] = array( 'delete', 'suppress' );
265 $conds['log_action'] = $this->getList()->getLogAction();
266 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
267 $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' );
284 $user = $this->getUser();
285 if( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
286 if( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
287 $this->getOutput()->permissionRequired( 'suppressrevision' );
289 $this->getOutput()->permissionRequired( '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->getTitle()->getLocalUrl(
303 'target=' . urlencode( $this->targetObj
->getPrefixedDBkey() ) .
304 '&file=' . urlencode( $archiveName ) .
305 '&token=' . urlencode( $user->getEditToken( $archiveName ) ) )
308 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
313 $this->getOutput()->disable();
314 # We mustn't allow the output to be Squid cached, otherwise
315 # if an admin previews a deleted image, and it's cached, then
316 # a user without appropriate permissions can toddle off and
317 # nab the image, and Squid will serve it
318 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
319 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
320 $this->getRequest()->response()->header( 'Pragma: no-cache' );
322 $key = $oimage->getStorageKey();
323 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
324 $repo->streamFile( $path );
328 * Get the list object for this request
330 protected function getList() {
331 if ( is_null( $this->list ) ) {
332 $class = $this->typeInfo
['list-class'];
333 $this->list = new $class( $this->getContext(), $this->targetObj
, $this->ids
);
339 * Show a list of items that we will operate on, and show a form with checkboxes
340 * which will allow the user to choose new visibility settings.
342 protected function showForm() {
345 if ( $this->typeName
== 'logging' ) {
346 $this->getOutput()->addWikiMsg( 'logdelete-selected', $this->getLanguage()->formatNum( count($this->ids
) ) );
348 $this->getOutput()->addWikiMsg( 'revdelete-selected',
349 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
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 $this->getOutput()->permissionRequired( 'suppressrevision' );
364 $UserAllowed = false;
367 $this->getOutput()->addHTML( $item->getHTML() );
370 if( !$numRevisions ) {
371 $this->getOutput()->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
375 $this->getOutput()->addHTML( "</ul>" );
377 $this->addUsageText();
379 // Normal sysops can always see what they did, but can't always change it
380 if( !$UserAllowed ) return;
382 // Show form if the user can submit
383 if( $this->mIsAllowed
) {
384 $out = Xml
::openElement( 'form', array( 'method' => 'post',
385 'action' => $this->getTitle()->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 '', '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()->addWikiMsg( 'revdelete-text' );
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 = $this->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>";
473 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
474 $html .= "<tr>$line</tr>\n";
476 // Otherwise, use tri-state radios
479 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
480 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
481 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
482 $html .= "<th></th></tr>\n";
483 foreach( $this->checks
as $item ) {
484 list( $message, $name, $field ) = $item;
485 // If there are several items, use third state by default...
486 if( $this->submitClicked
) {
487 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
489 $selected = -1; // use existing field
491 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
492 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
493 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
494 $label = $this->msg( $message )->escaped();
495 if( $field == Revision
::DELETED_RESTRICTED
) {
496 $label = "<b>$label</b>";
498 $line .= "<td>$label</td>";
499 $html .= "<tr>$line</tr>\n";
508 * UI entry point for form submission.
511 protected function submit() {
512 # Check edit token on submission
513 $token = $this->getRequest()->getVal('wpEditToken');
514 if( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
515 $this->getOutput()->addWikiMsg( 'sessionfailure' );
518 $bitParams = $this->extractBitParams();
519 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
520 $comment = $listReason;
521 if( $comment != 'other' && $this->otherReason
!= '' ) {
522 // Entry from drop down menu + additional comment
523 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason
;
524 } elseif( $comment == 'other' ) {
525 $comment = $this->otherReason
;
527 # Can the user set this field?
528 if( $bitParams[Revision
::DELETED_RESTRICTED
]==1 && !$this->getUser()->isAllowed('suppressrevision') ) {
529 $this->getOutput()->permissionRequired( 'suppressrevision' );
532 # If the save went through, go to success message...
533 $status = $this->save( $bitParams, $comment, $this->targetObj
);
534 if ( $status->isGood() ) {
537 # ...otherwise, bounce back to form...
539 $this->failure( $status );
545 * Report that the submit operation succeeded
547 protected function success() {
548 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
549 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo
['success'] );
550 $this->list->reloadFromMaster();
555 * Report that the submit operation failed
557 protected function failure( $status ) {
558 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
559 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
564 * Put together an array that contains -1, 0, or the *_deleted const for each bit
568 protected function extractBitParams() {
570 foreach( $this->checks
as $item ) {
571 list( /* message */ , $name, $field ) = $item;
572 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
573 if( $val < -1 ||
$val > 1) {
574 $val = -1; // -1 for existing value
576 $bitfield[$field] = $val;
578 if( !isset($bitfield[Revision
::DELETED_RESTRICTED
]) ) {
579 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
585 * Put together a rev_deleted bitfield
586 * @param $bitPars array extractBitParams() params
587 * @param $oldfield int current bitfield
590 public static function extractBitfield( $bitPars, $oldfield ) {
591 // Build the actual new rev_deleted bitfield
593 foreach( $bitPars as $const => $val ) {
595 $newBits |
= $const; // $const is the *_deleted const
596 } elseif( $val == -1 ) {
597 $newBits |
= ($oldfield & $const); // use existing
604 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
610 protected function save( $bitfield, $reason, $title ) {
611 return $this->getList()->setVisibility(
612 array( 'value' => $bitfield, 'comment' => $reason )