3 * Special page allowing users with the appropriate permissions to view
4 * and hide revisions. Log items can also be hidden.
10 class SpecialRevisionDelete
extends UnlistedSpecialPage
{
14 /** True if the submit button was clicked, and the form was posted */
20 /** Archive name, for reviewing deleted files */
23 /** Edit token for securing image views against XSS */
26 /** Title object for target parameter */
29 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
32 /** Array of checkbox specs (message, name, deletion bits) */
35 /** Information about the current type */
38 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
42 * Assorted information about each type, needed by the special page.
43 * TODO Move some of this to the list class
45 static $allowedTypes = array(
47 'check-label' => 'revdelete-hide-text',
48 'deletion-bits' => Revision
::DELETED_TEXT
,
49 'success' => 'revdelete-success',
50 'failure' => 'revdelete-failure',
51 'list-class' => 'RevDel_RevisionList',
54 'check-label' => 'revdelete-hide-text',
55 'deletion-bits' => Revision
::DELETED_TEXT
,
56 'success' => 'revdelete-success',
57 'failure' => 'revdelete-failure',
58 'list-class' => 'RevDel_ArchiveList',
61 'check-label' => 'revdelete-hide-image',
62 'deletion-bits' => File
::DELETED_FILE
,
63 'success' => 'revdelete-success',
64 'failure' => 'revdelete-failure',
65 'list-class' => 'RevDel_FileList',
67 'filearchive' => array(
68 'check-label' => 'revdelete-hide-image',
69 'deletion-bits' => File
::DELETED_FILE
,
70 'success' => 'revdelete-success',
71 'failure' => 'revdelete-failure',
72 'list-class' => 'RevDel_ArchivedFileList',
75 'check-label' => 'revdelete-hide-name',
76 'deletion-bits' => LogPage
::DELETED_ACTION
,
77 'success' => 'logdelete-success',
78 'failure' => 'logdelete-failure',
79 'list-class' => 'RevDel_LogList',
83 /** Type map to support old log entries */
84 static $deprecatedTypeMap = array(
85 'oldid' => 'revision',
86 'artimestamp' => 'archive',
87 'oldimage' => 'oldimage',
88 'fileid' => 'filearchive',
92 public function __construct() {
93 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
96 public function execute( $par ) {
97 global $wgOut, $wgUser, $wgRequest;
98 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
99 $wgOut->permissionRequired( 'deletedhistory' );
101 } else if( wfReadOnly() ) {
102 $wgOut->readOnlyPage();
105 $this->mIsAllowed
= $wgUser->isAllowed('deleterevision'); // for changes
106 $this->skin
= $wgUser->getSkin();
108 $this->outputHeader();
109 $this->submitClicked
= $wgRequest->wasPosted() && $wgRequest->getBool( 'wpSubmit' );
110 # Handle our many different possible input types.
111 $ids = $wgRequest->getVal( 'ids' );
112 if ( !is_null( $ids ) ) {
113 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
114 $this->ids
= explode( ',', $ids );
117 $this->ids
= array_keys( $wgRequest->getArray('ids',array()) );
119 // $this->ids = array_map( 'intval', $this->ids );
120 $this->ids
= array_unique( array_filter( $this->ids
) );
122 if ( $wgRequest->getVal( 'action' ) == 'historysubmit' ) {
123 # For show/hide form submission from history page
124 $this->targetObj
= $GLOBALS['wgTitle'];
125 $this->typeName
= 'revision';
127 $this->typeName
= $wgRequest->getVal( 'type' );
128 $this->targetObj
= Title
::newFromText( $wgRequest->getText( 'target' ) );
131 # For reviewing deleted files...
132 $this->archiveName
= $wgRequest->getVal( 'file' );
133 $this->token
= $wgRequest->getVal( 'token' );
134 if ( $this->archiveName
&& $this->targetObj
) {
135 $this->tryShowFile( $this->archiveName
);
139 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
140 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
144 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
145 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
148 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
150 # If we have revisions, get the title from the first one
151 # since they should all be from the same page. This allows
152 # for more flexibility with page moves...
153 if( $this->typeName
== 'revision' ) {
154 $rev = Revision
::newFromId( $this->ids
[0] );
155 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
158 $this->otherReason
= $wgRequest->getVal( 'wpReason' );
159 # We need a target page!
160 if( is_null($this->targetObj
) ) {
161 $wgOut->addWikiMsg( 'undelete-header' );
164 # Give a link to the logs/hist for this page
165 $this->showConvenienceLinks();
167 # Initialise checkboxes
168 $this->checks
= array(
169 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
170 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
171 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
173 if( $wgUser->isAllowed('suppressrevision') ) {
174 $this->checks
[] = array( 'revdelete-hide-restricted',
175 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
178 # Either submit or create our form
179 if( $this->mIsAllowed
&& $this->submitClicked
) {
180 $this->submit( $wgRequest );
185 $qc = $this->getLogQueryCond();
186 # Show relevant lines from the deletion log
187 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
188 LogEventsList
::showLogExtract( $wgOut, 'delete',
189 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
190 # Show relevant lines from the suppression log
191 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
192 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
193 LogEventsList
::showLogExtract( $wgOut, 'suppress',
194 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
199 * Show some useful links in the subtitle
201 protected function showConvenienceLinks() {
202 global $wgOut, $wgUser, $wgLang;
203 # Give a link to the logs/hist for this page
204 if( $this->targetObj
) {
206 $links[] = $this->skin
->linkKnown(
207 SpecialPage
::getTitleFor( 'Log' ),
208 wfMsgHtml( 'viewpagelogs' ),
210 array( 'page' => $this->targetObj
->getPrefixedText() )
212 if ( $this->targetObj
->getNamespace() != NS_SPECIAL
) {
213 # Give a link to the page history
214 $links[] = $this->skin
->linkKnown(
216 wfMsgHtml( 'pagehist' ),
218 array( 'action' => 'history' )
220 # Link to deleted edits
221 if( $wgUser->isAllowed('undelete') ) {
222 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
223 $links[] = $this->skin
->linkKnown(
225 wfMsgHtml( 'deletedhist' ),
227 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
231 # Logs themselves don't have histories or archived revisions
232 $wgOut->setSubtitle( '<p>' . $wgLang->pipeList( $links ) . '</p>' );
237 * Get the condition used for fetching log snippets
239 protected function getLogQueryCond() {
241 // Revision delete logs for these item
242 $conds['log_type'] = array('delete','suppress');
243 $conds['log_action'] = $this->getList()->getLogAction();
244 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
245 $conds['ls_value'] = $this->ids
;
250 * Show a deleted file version requested by the visitor.
251 * TODO Mostly copied from Special:Undelete. Refactor.
253 protected function tryShowFile( $archiveName ) {
254 global $wgOut, $wgRequest, $wgUser, $wgLang;
256 $repo = RepoGroup
::singleton()->getLocalRepo();
257 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
259 // Check if user is allowed to see this file
260 if ( !$oimage->exists() ) {
261 $wgOut->addWikiMsg( 'revdelete-no-file' );
264 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
265 if( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
266 $wgOut->permissionRequired( 'suppressrevision' );
268 $wgOut->permissionRequired( 'deletedtext' );
272 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
273 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
274 $this->targetObj
->getText(),
275 $wgLang->date( $oimage->getTimestamp() ),
276 $wgLang->time( $oimage->getTimestamp() ) );
278 Xml
::openElement( 'form', array(
280 'action' => $this->getTitle()->getLocalUrl(
281 'target=' . urlencode( $oimage->getName() ) .
282 '&file=' . urlencode( $archiveName ) .
283 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
286 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
292 # We mustn't allow the output to be Squid cached, otherwise
293 # if an admin previews a deleted image, and it's cached, then
294 # a user without appropriate permissions can toddle off and
295 # nab the image, and Squid will serve it
296 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
297 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
298 $wgRequest->response()->header( 'Pragma: no-cache' );
300 # Stream the file to the client
302 require_once( "$IP/includes/StreamFile.php" );
303 $key = $oimage->getStorageKey();
304 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
305 wfStreamFile( $path );
309 * Get the list object for this request
311 protected function getList() {
312 if ( is_null( $this->list ) ) {
313 $class = $this->typeInfo
['list-class'];
314 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
320 * Show a list of items that we will operate on, and show a form with checkboxes
321 * which will allow the user to choose new visibility settings.
323 protected function showForm() {
324 global $wgOut, $wgUser, $wgLang;
327 if ( $this->typeName
== 'logging' ) {
328 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
330 $wgOut->addWikiMsg( 'revdelete-selected',
331 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
334 $wgOut->addHTML( "<ul>" );
336 $where = $revObjs = array();
340 $list = $this->getList();
341 for ( $list->reset(); $list->current(); $list->next() ) {
342 $item = $list->current();
343 if ( !$item->canView() ) {
344 if( !$this->submitClicked
) {
345 $wgOut->permissionRequired( 'suppressrevision' );
348 $UserAllowed = false;
351 $wgOut->addHTML( $item->getHTML() );
354 if( !$numRevisions ) {
355 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
359 $wgOut->addHTML( "</ul>" );
361 $this->addUsageText();
363 // Normal sysops can always see what they did, but can't always change it
364 if( !$UserAllowed ) return;
366 // Show form if the user can submit
367 if( $this->mIsAllowed
) {
368 $out = Xml
::openElement( 'form', array( 'method' => 'post',
369 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
370 'id' => 'mw-revdel-form-revisions' ) ) .
371 Xml
::fieldset( wfMsg( 'revdelete-legend' ) ) .
372 $this->buildCheckBoxes() .
373 Xml
::openElement( 'table' ) .
375 '<td class="mw-label">' .
376 Xml
::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
378 '<td class="mw-input">' .
379 Xml
::listDropDown( 'wpRevDeleteReasonList',
380 wfMsgForContent( 'revdelete-reason-dropdown' ),
381 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
385 '<td class="mw-label">' .
386 Xml
::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
388 '<td class="mw-input">' .
389 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason' ) ) .
393 '<td class="mw-submit">' .
394 Xml
::submitButton( wfMsgExt('revdelete-submit','parsemag',$numRevisions),
395 array( 'name' => 'wpSubmit' ) ) .
398 Xml
::closeElement( 'table' ) .
399 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
400 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
401 Xml
::hidden( 'type', $this->typeName
) .
402 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
403 Xml
::closeElement( 'fieldset' ) . "\n";
407 if( $this->mIsAllowed
) {
408 $out .= Xml
::closeElement( 'form' ) . "\n";
409 // Show link to edit the dropdown reasons
410 if( $wgUser->isAllowed( 'editinterface' ) ) {
411 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'revdelete-reason-dropdown' );
412 $link = $wgUser->getSkin()->link(
414 wfMsgHtml( 'revdelete-edit-reasonlist' ),
416 array( 'action' => 'edit' )
418 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
421 $wgOut->addHTML( $out );
425 * Show some introductory text
426 * FIXME Wikimedia-specific policy text
428 protected function addUsageText() {
429 global $wgOut, $wgUser;
430 $wgOut->addWikiMsg( 'revdelete-text' );
431 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
432 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
434 if( $this->mIsAllowed
) {
435 $wgOut->addWikiMsg( 'revdelete-confirm' );
440 * @return String: HTML
442 protected function buildCheckBoxes() {
446 // If there is just one item, use checkboxes
447 $list = $this->getList();
448 if( $list->length() == 1 ) {
450 $bitfield = $list->current()->getBits(); // existing field
451 if( $this->submitClicked
) {
452 $bitfield = $this->extractBitfield( $this->extractBitParams($wgRequest), $bitfield );
454 foreach( $this->checks
as $item ) {
455 list( $message, $name, $field ) = $item;
456 $innerHTML = Xml
::checkLabel( wfMsg($message), $name, $name, $bitfield & $field );
457 if( $field == Revision
::DELETED_RESTRICTED
)
458 $innerHTML = "<b>$innerHTML</b>";
459 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
460 $html .= "<tr>$line</tr>\n";
462 // Otherwise, use tri-state radios
465 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-same').'</th>';
466 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
467 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</th>';
468 $html .= "<th></th></tr>\n";
469 foreach( $this->checks
as $item ) {
470 list( $message, $name, $field ) = $item;
471 // If there are several items, use third state by default...
472 if( $this->submitClicked
) {
473 $selected = $wgRequest->getInt( $name, 0 /* unchecked */ );
475 $selected = -1; // use existing field
477 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
478 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
479 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
480 $label = wfMsgHtml($message);
481 if( $field == Revision
::DELETED_RESTRICTED
) {
482 $label = "<b>$label</b>";
484 $line .= "<td>$label</td>";
485 $html .= "<tr>$line</tr>\n";
494 * UI entry point for form submission.
495 * @param $request WebRequest
497 protected function submit( $request ) {
498 global $wgUser, $wgOut;
499 # Check edit token on submission
500 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
501 $wgOut->addWikiMsg( 'sessionfailure' );
504 $bitParams = $this->extractBitParams( $request );
505 $listReason = $request->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
506 $comment = $listReason;
507 if( $comment != 'other' && $this->otherReason
!= '' ) {
508 // Entry from drop down menu + additional comment
509 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason
;
510 } elseif( $comment == 'other' ) {
511 $comment = $this->otherReason
;
513 # Can the user set this field?
514 if( $bitParams[Revision
::DELETED_RESTRICTED
]==1 && !$wgUser->isAllowed('suppressrevision') ) {
515 $wgOut->permissionRequired( 'suppressrevision' );
518 # If the save went through, go to success message...
519 $status = $this->save( $bitParams, $comment, $this->targetObj
);
520 if ( $status->isGood() ) {
523 # ...otherwise, bounce back to form...
525 $this->failure( $status );
531 * Report that the submit operation succeeded
533 protected function success() {
535 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
536 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo
['success'] );
537 $this->list->reloadFromMaster();
542 * Report that the submit operation failed
544 protected function failure( $status ) {
546 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
547 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
552 * Put together an array that contains -1, 0, or the *_deleted const for each bit
553 * @param $request WebRequest
556 protected function extractBitParams( $request ) {
558 foreach( $this->checks
as $item ) {
559 list( /* message */ , $name, $field ) = $item;
560 $val = $request->getInt( $name, 0 /* unchecked */ );
561 if( $val < -1 ||
$val > 1) {
562 $val = -1; // -1 for existing value
564 $bitfield[$field] = $val;
566 if( !isset($bitfield[Revision
::DELETED_RESTRICTED
]) ) {
567 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
573 * Put together a rev_deleted bitfield
574 * @param $bitPars array extractBitParams() params
575 * @param $oldfield int current bitfield
578 public static function extractBitfield( $bitPars, $oldfield ) {
579 // Build the actual new rev_deleted bitfield
581 foreach( $bitPars as $const => $val ) {
583 $newBits |
= $const; // $const is the *_deleted const
584 } else if( $val == -1 ) {
585 $newBits |
= ($oldfield & $const); // use existing
592 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
594 protected function save( $bitfield, $reason, $title ) {
595 return $this->getList()->setVisibility(
596 array( 'value' => $bitfield, 'comment' => $reason )