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', 'deleterevision' );
96 public function execute( $par ) {
97 global $wgOut, $wgUser, $wgRequest;
98 if( !$wgUser->isAllowed( 'deleterevision' ) ) {
99 $wgOut->permissionRequired( 'deleterevision' );
101 } else if( wfReadOnly() ) {
102 $wgOut->readOnlyPage();
105 $this->skin
= $wgUser->getSkin();
107 $this->outputHeader();
108 $this->submitClicked
= $wgRequest->wasPosted() && $wgRequest->getBool( 'wpSubmit' );
109 # Handle our many different possible input types.
110 $ids = $wgRequest->getVal( 'ids' );
111 if ( !is_null( $ids ) ) {
112 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
113 $this->ids
= explode( ',', $ids );
116 $this->ids
= array_keys( $wgRequest->getArray('ids',array()) );
118 // $this->ids = array_map( 'intval', $this->ids );
119 $this->ids
= array_unique( array_filter( $this->ids
) );
121 if ( $wgRequest->getVal( 'action' ) == 'revisiondelete' ) {
122 # For show/hide form submission from history page
123 $this->targetObj
= $GLOBALS['wgTitle'];
124 $this->typeName
= 'revision';
126 $this->typeName
= $wgRequest->getVal( 'type' );
127 $this->targetObj
= Title
::newFromText( $wgRequest->getText( 'target' ) );
130 # For reviewing deleted files...
131 $this->archiveName
= $wgRequest->getVal( 'file' );
132 $this->token
= $wgRequest->getVal( 'token' );
133 if ( $this->archiveName
&& $this->targetObj
) {
134 $this->tryShowFile( $this->archiveName
);
138 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
139 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
143 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
144 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
147 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
149 # If we have revisions, get the title from the first one
150 # since they should all be from the same page. This allows
151 # for more flexibility with page moves...
152 if( $this->typeName
== 'revision' ) {
153 $rev = Revision
::newFromId( $this->ids
[0] );
154 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
157 $this->otherReason
= $wgRequest->getVal( 'wpReason' );
158 # We need a target page!
159 if( is_null($this->targetObj
) ) {
160 $wgOut->addWikiMsg( 'undelete-header' );
163 # Give a link to the logs/hist for this page
164 $this->showConvenienceLinks();
166 # Initialise checkboxes
167 $this->checks
= array(
168 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
169 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
170 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
172 if( $wgUser->isAllowed('suppressrevision') ) {
173 $this->checks
[] = array( 'revdelete-hide-restricted',
174 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
177 # Either submit or create our form
178 if( $this->submitClicked
) {
179 $this->submit( $wgRequest );
183 $qc = $this->getLogQueryCond();
184 # Show relevant lines from the deletion log
185 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
186 LogEventsList
::showLogExtract( $wgOut, 'delete',
187 $this->targetObj
->getPrefixedText(), '', 25, $qc );
188 # Show relevant lines from the suppression log
189 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
190 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
191 LogEventsList
::showLogExtract( $wgOut, 'suppress',
192 $this->targetObj
->getPrefixedText(), '', 25, $qc );
197 * Show some useful links in the subtitle
199 protected function showConvenienceLinks() {
200 global $wgOut, $wgUser, $wgLang;
201 # Give a link to the logs/hist for this page
202 if( $this->targetObj
) {
204 $logtitle = SpecialPage
::getTitleFor( 'Log' );
205 $links[] = $this->skin
->linkKnown(
207 wfMsgHtml( 'viewpagelogs' ),
209 array( 'page' => $this->targetObj
->getPrefixedText() )
211 if ( $this->targetObj
->getNamespace() != NS_SPECIAL
) {
212 # Give a link to the page history
213 $links[] = $this->skin
->linkKnown(
215 wfMsgHtml( 'pagehist' ),
217 array( 'action' => 'history' )
219 # Link to deleted edits
220 if( $wgUser->isAllowed('undelete') ) {
221 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
222 $links[] = $this->skin
->linkKnown(
224 wfMsgHtml( 'deletedhist' ),
226 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
230 # Logs themselves don't have histories or archived revisions
231 $wgOut->setSubtitle( '<p>' . $wgLang->pipeList( $links ) . '</p>' );
236 * Get the condition used for fetching log snippets
238 protected function getLogQueryCond() {
240 // Revision delete logs for these item
241 $conds['log_type'] = array('delete','suppress');
242 $conds['log_action'] = $this->getList()->getLogAction();
243 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
244 $conds['ls_value'] = $this->ids
;
249 * Show a deleted file version requested by the visitor.
250 * TODO Mostly copied from Special:Undelete. Refactor.
252 protected function tryShowFile( $archiveName ) {
253 global $wgOut, $wgRequest, $wgUser, $wgLang;
255 $repo = RepoGroup
::singleton()->getLocalRepo();
256 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
258 // Check if user is allowed to see this file
259 if ( !$oimage->exists() ) {
260 $wgOut->addWikiMsg( 'revdelete-no-file' );
263 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
264 $wgOut->permissionRequired( 'suppressrevision' );
267 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
268 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
269 $this->targetObj
->getText(),
270 $wgLang->date( $oimage->getTimestamp() ),
271 $wgLang->time( $oimage->getTimestamp() ) );
273 Xml
::openElement( 'form', array(
275 'action' => $this->getTitle()->getLocalUrl(
276 'target=' . urlencode( $oimage->getName() ) .
277 '&file=' . urlencode( $archiveName ) .
278 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
281 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
287 # We mustn't allow the output to be Squid cached, otherwise
288 # if an admin previews a deleted image, and it's cached, then
289 # a user without appropriate permissions can toddle off and
290 # nab the image, and Squid will serve it
291 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
292 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
293 $wgRequest->response()->header( 'Pragma: no-cache' );
295 # Stream the file to the client
297 require_once( "$IP/includes/StreamFile.php" );
298 $key = $oimage->getStorageKey();
299 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
300 wfStreamFile( $path );
304 * Get the list object for this request
306 protected function getList() {
307 if ( is_null( $this->list ) ) {
308 $class = $this->typeInfo
['list-class'];
309 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
315 * Show a list of items that we will operate on, and show a form with checkboxes
316 * which will allow the user to choose new visibility settings.
318 protected function showForm() {
319 global $wgOut, $wgUser, $wgLang;
322 if ( $this->typeName
== 'logging' ) {
323 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
325 $wgOut->addWikiMsg( 'revdelete-selected',
326 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
330 $wgOut->addHTML( "<ul>" );
332 $where = $revObjs = array();
336 $list = $this->getList();
337 for ( $list->reset(); $list->current(); $list->next() ) {
338 $item = $list->current();
339 if ( !$item->canView() ) {
340 if( !$this->submitClicked
) {
341 $wgOut->permissionRequired( 'suppressrevision' );
344 $UserAllowed = false;
347 $bitfields |
= $item->getBits();
348 $wgOut->addHTML( $item->getHTML() );
351 if( !$numRevisions ) {
352 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
356 $wgOut->addHTML( "</ul>" );
358 $this->addUsageText();
360 // Normal sysops can always see what they did, but can't always change it
361 if( !$UserAllowed ) return;
363 $out = Xml
::openElement( 'form', array( 'method' => 'post',
364 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
365 'id' => 'mw-revdel-form-revisions' ) ) .
366 Xml
::fieldset( wfMsg( 'revdelete-legend' ) ) .
367 Xml
::openElement( 'table' ) .
368 $this->buildCheckBoxes( $bitfields ) .
370 '<td class="mw-label">' .
371 Xml
::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
373 '<td class="mw-input">' .
374 Xml
::listDropDown( 'wpRevDeleteReasonList',
375 wfMsgForContent( 'revdelete-reason-dropdown' ),
376 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
380 '<td class="mw-label">' .
381 Xml
::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
383 '<td class="mw-input">' .
384 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason' ) ) .
388 '<td class="mw-submit">' .
389 Xml
::submitButton( wfMsg( 'revdelete-submit' ), array( 'name' => 'wpSubmit' ) ) .
392 Xml
::closeElement( 'table' ) .
393 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
394 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
395 Xml
::hidden( 'type', $this->typeName
) .
396 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
397 Xml
::closeElement( 'fieldset' ) . "\n";
399 if( $wgUser->isAllowed( 'editinterface' ) ) {
400 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'revdelete-reason-dropdown' );
401 $link = $wgUser->getSkin()->link(
403 wfMsgHtml( 'revdelete-edit-reasonlist' ),
405 array( 'action' => 'edit' )
407 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
409 $out .= Xml
::closeElement( 'form' ) . "\n";
411 $wgOut->addHTML( $out );
415 * Show some introductory text
416 * FIXME Wikimedia-specific policy text
418 protected function addUsageText() {
419 global $wgOut, $wgUser;
420 $wgOut->addWikiMsg( 'revdelete-text' );
421 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
422 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
427 * @param $bitfields Interger: aggregate bitfield of all the bitfields
428 * @return String: HTML
430 protected function buildCheckBoxes( $bitfields ) {
432 // FIXME: all items checked for just one rev are checked, even if not set for the others
433 foreach( $this->checks
as $item ) {
434 list( $message, $name, $field ) = $item;
435 $innerHTML = Xml
::checkLabel( wfMsg($message), $name, $name, $bitfields & $field );
436 if( $field == Revision
::DELETED_RESTRICTED
)
437 $innerHTML = "<b>$innerHTML</b>";
438 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
439 $html .= '<tr><td></td>' . $line . "</tr>\n";
445 * UI entry point for form submission.
446 * @param $request WebRequest
448 protected function submit( $request ) {
449 global $wgUser, $wgOut;
450 # Check edit token on submission
451 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
452 $wgOut->addWikiMsg( 'sessionfailure' );
455 $bitfield = $this->extractBitfield( $request );
456 $listReason = $request->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
457 $comment = $listReason;
458 if( $comment != 'other' && $this->otherReason
!= '' ) {
459 // Entry from drop down menu + additional comment
460 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason
;
461 } elseif( $comment == 'other' ) {
462 $comment = $this->otherReason
;
464 # Can the user set this field?
465 if( $bitfield & Revision
::DELETED_RESTRICTED
&& !$wgUser->isAllowed('suppressrevision') ) {
466 $wgOut->permissionRequired( 'suppressrevision' );
469 # If the save went through, go to success message...
470 $status = $this->save( $bitfield, $comment, $this->targetObj
);
471 if ( $status->isGood() ) {
474 # ...otherwise, bounce back to form...
476 $this->failure( $status );
482 * Report that the submit operation succeeded
484 protected function success() {
486 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
487 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo
['success'] );
488 $this->list->reloadFromMaster();
493 * Report that the submit operation failed
495 protected function failure( $status ) {
497 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
498 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
503 * Put together a rev_deleted bitfield from the submitted checkboxes
504 * @param $request WebRequest
507 protected function extractBitfield( $request ) {
509 foreach( $this->checks
as $item ) {
510 list( /* message */ , $name, $field ) = $item;
511 if( $request->getCheck( $name ) ) {
519 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
521 protected function save( $bitfield, $reason, $title ) {
522 // Don't allow simply locking the interface for no reason
523 if( $bitfield == Revision
::DELETED_RESTRICTED
) {
524 return Status
::newFatal( 'revdelete-only-restricted' );
526 return $this->getList()->setVisibility( array(
527 'value' => $bitfield,
528 'comment' => $reason ) );
533 * Temporary b/c interface, collection of static functions.
534 * @ingroup SpecialPage
536 class RevisionDeleter
{
538 * Checks for a change in the bitfield for a certain option and updates the
539 * provided array accordingly.
541 * @param $desc String: description to add to the array if the option was
542 * enabled / disabled.
543 * @param $field Integer: the bitmask describing the single option.
544 * @param $diff Integer: the xor of the old and new bitfields.
545 * @param $new Integer: the new bitfield
546 * @param $arr Array: the array to update.
548 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
549 if( $diff & $field ) {
550 $arr[ ( $new & $field ) ?
0 : 1 ][] = $desc;
555 * Gets an array describing the changes made to the visibilit of the revision.
556 * If the resulting array is $arr, then $arr[0] will contain an array of strings
557 * describing the items that were hidden, $arr[2] will contain an array of strings
558 * describing the items that were unhidden, and $arr[3] will contain an array with
559 * a single string, which can be one of "applied restrictions to sysops",
560 * "removed restrictions from sysops", or null.
562 * @param $n Integer: the new bitfield.
563 * @param $o Integer: the old bitfield.
564 * @return An array as described above.
566 protected static function getChanges( $n, $o ) {
568 $ret = array( 0 => array(), 1 => array(), 2 => array() );
569 // Build bitfield changes in language
570 self
::checkItem( wfMsgForContent( 'revdelete-content' ),
571 Revision
::DELETED_TEXT
, $diff, $n, $ret );
572 self
::checkItem( wfMsgForContent( 'revdelete-summary' ),
573 Revision
::DELETED_COMMENT
, $diff, $n, $ret );
574 self
::checkItem( wfMsgForContent( 'revdelete-uname' ),
575 Revision
::DELETED_USER
, $diff, $n, $ret );
576 // Restriction application to sysops
577 if( $diff & Revision
::DELETED_RESTRICTED
) {
578 if( $n & Revision
::DELETED_RESTRICTED
)
579 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
581 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
587 * Gets a log message to describe the given revision visibility change. This
588 * message will be of the form "[hid {content, edit summary, username}];
589 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
591 * @param $count Integer: The number of effected revisions.
592 * @param $nbitfield Integer: The new bitfield for the revision.
593 * @param $obitfield Integer: The old bitfield for the revision.
594 * @param $isForLog Boolean
596 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
599 $changes = self
::getChanges( $nbitfield, $obitfield );
600 if( count( $changes[0] ) ) {
601 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
603 if( count( $changes[1] ) ) {
605 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
607 if( count( $changes[2] ) ) {
608 $s .= $s ?
' (' . $changes[2][0] . ')' : $changes[2][0];
610 $msg = $isForLog ?
'logdelete-log-message' : 'revdelete-log-message';
611 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
615 // Get DB field name for URL param...
616 // Future code for other things may also track
617 // other types of revision-specific changes.
618 public static function getRelationType( $typeName ) {
619 if ( isset( SpecialRevisionDelete
::$deprecatedTypeMap[$typeName] ) ) {
620 $typeName = SpecialRevisionDelete
::$deprecatedTypeMap[$typeName];
622 if ( isset( SpecialRevisionDelete
::$allowedTypes[$typeName] ) ) {
623 $class = SpecialRevisionDelete
::$allowedTypes[$typeName]['list-class'];
624 $list = new $class( null, null, null );
625 return $list->getIdField();
633 * Abstract base class for a list of deletable items
635 abstract class RevDel_List
{
636 var $special, $title, $ids, $res, $current;
637 var $type = null; // override this
638 var $idField = null; // override this
639 var $dateField = false; // override this
642 * @param $special The parent SpecialPage
643 * @param $title The target title
644 * @param $ids Array of IDs
646 public function __construct( $special, $title, $ids ) {
647 $this->special
= $special;
648 $this->title
= $title;
653 * Get the internal type name of this list. Equal to the table name.
655 public function getType() {
660 * Get the DB field name associated with the ID list/
662 public function getIdField() {
663 return $this->idField
;
667 * Get the DB field name storing timestamps
669 public function getTimestampField() {
670 return $this->dateField
;
674 * Set the visibility for the revisions in this list. Logging and
675 * transactions are done here.
677 * @param $params Associative array of parameters. Members are:
678 * value: The integer value to set the visibility to
679 * comment: The log comment.
682 public function setVisibility( $params ) {
683 $newBits = $params['value'];
684 $comment = $params['comment'];
687 $dbw = wfGetDB( DB_MASTER
);
688 $this->doQuery( $dbw );
690 $status = Status
::newGood();
691 $missing = array_flip( $this->ids
);
692 $this->clearFileOps();
693 $idsForLog = array();
695 for ( $this->reset(); $this->current(); $this->next() ) {
696 $item = $this->current();
697 unset( $missing[ $item->getId() ] );
699 // Make error messages less vague
700 $oldBits = $item->getBits();
701 if ( $oldBits == $newBits ) {
702 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
703 $status->failCount++
;
705 } elseif ( $oldBits == 0 && $newBits != 0 ) {
707 } elseif ( $oldBits != 0 && $newBits == 0 ) {
713 if ( $item->isHideCurrentOp( $newBits ) ) {
714 // Cannot hide current version text
715 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
716 $status->failCount++
;
719 if ( !$item->canView() ) {
720 // Cannot access this revision
721 $msg = $opType == 'show' ?
'revdelete-show-no-access' : 'revdelete-modify-no-access';
722 $status->error( $msg, $item->formatDate(), $item->formatTime() );
723 $status->failCount++
;
727 // Update the revision
728 $ok = $item->setBits( $newBits );
731 $idsForLog[] = $item->getId();
732 $status->successCount++
;
734 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
735 $status->failCount++
;
739 // Handle missing revisions
740 foreach ( $missing as $id => $unused ) {
741 $status->error( 'revdelete-modify-missing', $id );
742 $status->failCount++
;
745 if ( $status->successCount
== 0 ) {
751 // Save success count
752 $successCount = $status->successCount
;
754 // Move files, if there are any
755 $status->merge( $this->doPreCommitUpdates() );
756 if ( !$status->isOK() ) {
757 // Fatal error, such as no configured archive directory
763 $this->updateLog( array(
764 'title' => $this->title
,
765 'count' => $successCount,
766 'newBits' => $newBits,
767 'oldBits' => $oldBits,
768 'comment' => $comment,
774 $status->merge( $this->doPostCommitUpdates() );
779 * Reload the list data from the master DB. This can be done after setVisibility()
780 * to allow $item->getHTML() to show the new data.
782 function reloadFromMaster() {
783 $dbw = wfGetDB( DB_MASTER
);
784 $this->res
= $this->doQuery( $dbw );
788 * Record a log entry on the action
789 * @param $params Associative array of parameters:
790 * newBits: The new value of the *_deleted bitfield
791 * oldBits: The old value of the *_deleted bitfield.
792 * title: The target title
794 * comment: The log comment
796 protected function updateLog( $params ) {
797 // Get the URL param's corresponding DB field
798 $field = RevisionDeleter
::getRelationType( $this->getType() );
800 throw new MWException( "Bad log URL param type!" );
802 // Put things hidden from sysops in the oversight log
803 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
804 $logType = 'suppress';
808 // Add params for effected page and ids
809 $logParams = $this->getLogParams( $params );
810 // Actually add the deletion log entry
811 $log = new LogPage( $logType );
812 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
813 $params['comment'], $logParams );
814 // Allow for easy searching of deletion log items for revision/log items
815 $log->addRelations( $field, $params['ids'], $logid );
819 * Get the log action for this list type
821 public function getLogAction() {
826 * Get log parameter array.
827 * @param $params Associative array of log parameters, same as updateLog()
830 public function getLogParams( $params ) {
833 implode( ',', $params['ids'] ),
834 "ofield={$params['oldBits']}",
835 "nfield={$params['newBits']}"
840 * Initialise the current iteration pointer
842 protected function initCurrent() {
843 $row = $this->res
->current();
845 $this->current
= $this->newItem( $row );
847 $this->current
= false;
852 * Start iteration. This must be called before current() or next().
853 * @return First list item
855 public function reset() {
857 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
859 $this->res
->rewind();
861 $this->initCurrent();
862 return $this->current
;
866 * Get the current list item, or false if we are at the end
868 public function current() {
869 return $this->current
;
873 * Move the iteration pointer to the next list item, and return it.
875 public function next() {
877 $this->initCurrent();
878 return $this->current
;
882 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
885 public function clearFileOps() {
889 * A hook for setVisibility(): do batch updates pre-commit.
893 public function doPreCommitUpdates() {
894 return Status
::newGood();
898 * A hook for setVisibility(): do any necessary updates post-commit.
902 public function doPostCommitUpdates() {
903 return Status
::newGood();
907 * Create an item object from a DB result row
908 * @param $row stdclass
910 abstract public function newItem( $row );
913 * Do the DB query to iterate through the objects.
914 * @param $db Database object to use for the query
916 abstract public function doQuery( $db );
919 * Get the integer value of the flag used for suppression
921 abstract public function getSuppressBit();
925 * Abstract base class for deletable items
927 abstract class RevDel_Item
{
928 /** The parent SpecialPage */
931 /** The parent RevDel_List */
934 /** The DB result row */
938 * @param $list RevDel_List
939 * @param $row DB result row
941 public function __construct( $list, $row ) {
942 $this->special
= $list->special
;
948 * Get the ID, as it would appear in the ids URL parameter
950 public function getId() {
951 $field = $this->list->getIdField();
952 return $this->row
->$field;
956 * Get the date, formatted with $wgLang
958 public function formatDate() {
960 return $wgLang->date( $this->getTimestamp() );
964 * Get the time, formatted with $wgLang
966 public function formatTime() {
968 return $wgLang->time( $this->getTimestamp() );
972 * Get the timestamp in MW 14-char form
974 public function getTimestamp() {
975 $field = $this->list->getTimestampField();
976 return wfTimestamp( TS_MW
, $this->row
->$field );
980 * Returns true if the item is "current", and the operation to set the given
981 * bits can't be executed for that reason
984 public function isHideCurrentOp( $newBits ) {
989 * Returns true if the current user can view the item
991 abstract public function canView();
994 * Get the current deletion bitfield value
996 abstract public function getBits();
999 * Get the HTML of the list item. Should be include <li></li> tags.
1000 * This is used to show the list in HTML form, by the special page.
1002 abstract public function getHTML();
1005 * Set the visibility of the item. This should do any necessary DB queries.
1007 * The DB update query should have a condition which forces it to only update
1008 * if the value in the DB matches the value fetched earlier with the SELECT.
1009 * If the update fails because it did not match, the function should return
1010 * false. This prevents concurrency problems.
1012 * @return boolean success
1014 abstract public function setBits( $newBits );
1018 * List for revision table items
1020 class RevDel_RevisionList
extends RevDel_List
{
1022 var $type = 'revision';
1023 var $idField = 'rev_id';
1024 var $dateField = 'rev_timestamp';
1026 public function doQuery( $db ) {
1027 $ids = array_map( 'intval', $this->ids
);
1028 return $db->select( array('revision','page'), '*',
1030 'rev_page' => $this->title
->getArticleID(),
1032 'rev_page = page_id'
1038 public function newItem( $row ) {
1039 return new RevDel_RevisionItem( $this, $row );
1042 public function getCurrent() {
1043 if ( is_null( $this->currentRevId
) ) {
1044 $dbw = wfGetDB( DB_MASTER
);
1045 $this->currentRevId
= $dbw->selectField(
1046 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
1048 return $this->currentRevId
;
1051 public function getSuppressBit() {
1052 return Revision
::DELETED_RESTRICTED
;
1055 public function doPreCommitUpdates() {
1056 $this->title
->invalidateCache();
1057 return Status
::newGood();
1060 public function doPostCommitUpdates() {
1061 $this->title
->purgeSquid();
1062 // Extensions that require referencing previous revisions may need this
1063 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$this->title
) );
1064 return Status
::newGood();
1069 * Item class for a revision table row
1071 class RevDel_RevisionItem
extends RevDel_Item
{
1074 public function __construct( $list, $row ) {
1075 parent
::__construct( $list, $row );
1076 $this->revision
= new Revision( $row );
1079 public function canView() {
1080 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
);
1083 public function getBits() {
1084 return $this->revision
->mDeleted
;
1087 public function setBits( $bits ) {
1088 $dbw = wfGetDB( DB_MASTER
);
1089 // Update revision table
1090 $dbw->update( 'revision',
1091 array( 'rev_deleted' => $bits ),
1093 'rev_id' => $this->revision
->getId(),
1094 'rev_page' => $this->revision
->getPage(),
1095 'rev_deleted' => $this->getBits()
1099 if ( !$dbw->affectedRows() ) {
1103 // Update recentchanges table
1104 $dbw->update( 'recentchanges',
1106 'rc_deleted' => $bits,
1110 'rc_this_oldid' => $this->revision
->getId(), // condition
1111 // non-unique timestamp index
1112 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ),
1119 public function isDeleted() {
1120 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
1123 public function isHideCurrentOp( $newBits ) {
1124 return ( $newBits & Revision
::DELETED_TEXT
)
1125 && $this->list->getCurrent() == $this->getId();
1129 * Get the HTML link to the revision text.
1130 * Overridden by RevDel_ArchiveItem.
1132 protected function getRevisionLink() {
1134 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1135 if ( $this->isDeleted() && !$this->canView() ) {
1138 return $this->special
->skin
->link(
1143 'oldid' => $this->revision
->getId(),
1150 * Get the HTML link to the diff.
1151 * Overridden by RevDel_ArchiveItem
1153 protected function getDiffLink() {
1154 if ( $this->isDeleted() && !$this->canView() ) {
1155 return wfMsgHtml('diff');
1158 $this->special
->skin
->link(
1163 'diff' => $this->revision
->getId(),
1175 public function getHTML() {
1176 $difflink = $this->getDiffLink();
1177 $revlink = $this->getRevisionLink();
1178 $userlink = $this->special
->skin
->revUserLink( $this->revision
);
1179 $comment = $this->special
->skin
->revComment( $this->revision
);
1180 if ( $this->isDeleted() ) {
1181 $revlink = "<span class=\"history-deleted\">$revlink</span>";
1183 return "<li>($difflink) $revlink $userlink $comment</li>";
1188 * List for archive table items, i.e. revisions deleted via action=delete
1190 class RevDel_ArchiveList
extends RevDel_RevisionList
{
1191 var $type = 'archive';
1192 var $idField = 'ar_timestamp';
1193 var $dateField = 'ar_timestamp';
1195 public function doQuery( $db ) {
1196 $timestamps = array();
1197 foreach ( $this->ids
as $id ) {
1198 $timestamps[] = $db->timestamp( $id );
1200 return $db->select( 'archive', '*',
1202 'ar_namespace' => $this->title
->getNamespace(),
1203 'ar_title' => $this->title
->getDBkey(),
1204 'ar_timestamp' => $timestamps
1210 public function newItem( $row ) {
1211 return new RevDel_ArchiveItem( $this, $row );
1214 public function doPreCommitUpdates() {
1215 return Status
::newGood();
1218 public function doPostCommitUpdates() {
1219 return Status
::newGood();
1224 * Item class for a archive table row
1226 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
1227 public function __construct( $list, $row ) {
1228 RevDel_Item
::__construct( $list, $row );
1229 $this->revision
= Revision
::newFromArchiveRow( $row,
1230 array( 'page' => $this->list->title
->getArticleId() ) );
1233 public function getId() {
1234 # Convert DB timestamp to MW timestamp
1235 return $this->revision
->getTimestamp();
1238 public function setBits( $bits ) {
1239 $dbw = wfGetDB( DB_MASTER
);
1240 $dbw->update( 'archive',
1241 array( 'ar_deleted' => $bits ),
1242 array( 'ar_namespace' => $this->list->title
->getNamespace(),
1243 'ar_title' => $this->list->title
->getDBkey(),
1244 // use timestamp for index
1245 'ar_timestamp' => $this->row
->ar_timestamp
,
1246 'ar_rev_id' => $this->row
->ar_rev_id
,
1247 'ar_deleted' => $this->getBits()
1250 return (bool)$dbw->affectedRows();
1253 protected function getRevisionLink() {
1255 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1256 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1257 if ( $this->isDeleted() && !$this->canView() ) {
1260 return $this->special
->skin
->link( $undelete, $date, array(),
1262 'target' => $this->list->title
->getPrefixedText(),
1263 'timestamp' => $this->revision
->getTimestamp()
1267 protected function getDiffLink() {
1268 if ( $this->isDeleted() && !$this->canView() ) {
1269 return wfMsgHtml( 'diff' );
1271 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1272 return $this->special
->skin
->link( $undelete, wfMsgHtml('diff'), array(),
1274 'target' => $this->list->title
->getPrefixedText(),
1276 'timestamp' => $this->revision
->getTimestamp()
1282 * List for oldimage table items
1284 class RevDel_FileList
extends RevDel_List
{
1285 var $type = 'oldimage';
1286 var $idField = 'oi_archive_name';
1287 var $dateField = 'oi_timestamp';
1288 var $storeBatch, $deleteBatch, $cleanupBatch;
1290 public function doQuery( $db ) {
1291 $archiveName = array();
1292 foreach( $this->ids
as $timestamp ) {
1293 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
1295 return $db->select( 'oldimage', '*',
1297 'oi_name' => $this->title
->getDBkey(),
1298 'oi_archive_name' => $archiveNames
1304 public function newItem( $row ) {
1305 return new RevDel_FileItem( $this, $row );
1308 public function clearFileOps() {
1309 $this->deleteBatch
= array();
1310 $this->storeBatch
= array();
1311 $this->cleanupBatch
= array();
1314 public function doPreCommitUpdates() {
1315 $status = Status
::newGood();
1316 $repo = RepoGroup
::singleton()->getLocalRepo();
1317 if ( $this->storeBatch
) {
1318 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
1320 if ( !$status->isOK() ) {
1323 if ( $this->deleteBatch
) {
1324 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
1326 if ( !$status->isOK() ) {
1327 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1328 // modified (but destined for rollback) causes data loss
1331 if ( $this->cleanupBatch
) {
1332 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
1337 public function doPostCommitUpdates() {
1338 $file = wfLocalFile( $this->title
);
1339 $file->purgeCache();
1340 $file->purgeDescription();
1341 return Status
::newGood();
1344 public function getSuppressBit() {
1345 return File
::DELETED_RESTRICTED
;
1350 * Item class for an oldimage table row
1352 class RevDel_FileItem
extends RevDel_Item
{
1355 public function __construct( $list, $row ) {
1356 parent
::__construct( $list, $row );
1357 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
1360 public function getId() {
1361 $parts = explode( '!', $this->row
->oi_archive_name
);
1365 public function canView() {
1366 return $this->file
->userCan( File
::DELETED_RESTRICTED
);
1369 public function getBits() {
1370 return $this->file
->getVisibility();
1373 public function setBits( $bits ) {
1375 # FIXME: move to LocalFile.php
1376 if ( $this->isDeleted() ) {
1377 if ( $bits & File
::DELETED_FILE
) {
1381 $key = $this->file
->getStorageKey();
1382 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1383 $this->list->storeBatch
[] = array(
1384 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1386 $this->file
->getRel()
1388 $this->list->cleanupBatch
[] = $key;
1390 } elseif ( $bits & File
::DELETED_FILE
) {
1392 $key = $this->file
->getStorageKey();
1393 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1394 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
1397 # Do the database operations
1398 $dbw = wfGetDB( DB_MASTER
);
1399 $dbw->update( 'oldimage',
1400 array( 'oi_deleted' => $bits ),
1402 'oi_name' => $this->row
->oi_name
,
1403 'oi_timestamp' => $this->row
->oi_timestamp
,
1404 'oi_deleted' => $this->getBits()
1408 return (bool)$dbw->affectedRows();
1411 public function isDeleted() {
1412 return $this->file
->isDeleted( File
::DELETED_FILE
);
1416 * Get the link to the file.
1417 * Overridden by RevDel_ArchivedFileItem.
1419 protected function getLink() {
1421 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1422 if ( $this->isDeleted() ) {
1424 if ( !$this->canView() ) {
1427 $link = $this->special
->skin
->link(
1428 $this->special
->getTitle(),
1431 'target' => $this->list->title
->getPrefixedText(),
1432 'file' => $this->file
->sha1
. '.' . $this->file
->getExtension()
1436 return '<span class="history-deleted">' . $link . '</span>';
1439 $url = $this->file
->getUrl();
1440 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
1444 * Generate a user tool link cluster if the current user is allowed to view it
1445 * @return string HTML
1447 protected function getUserTools() {
1448 if( $this->file
->userCan( Revision
::DELETED_USER
) ) {
1449 $link = $this->special
->skin
->userLink( $this->file
->user
, $this->file
->user_text
) .
1450 $this->special
->skin
->userToolLinks( $this->file
->user
, $this->file
->user_text
);
1452 $link = wfMsgHtml( 'rev-deleted-user' );
1454 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
1455 return '<span class="history-deleted">' . $link . '</span>';
1461 * Wrap and format the file's comment block, if the current
1462 * user is allowed to view it.
1464 * @return string HTML
1466 protected function getComment() {
1467 if( $this->file
->userCan( File
::DELETED_COMMENT
) ) {
1468 $block = $this->special
->skin
->commentBlock( $this->file
->description
);
1470 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1472 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
1473 return "<span class=\"history-deleted\">$block</span>";
1478 public function getHTML() {
1483 $wgLang->formatNum( $this->file
->getWidth() ),
1484 $wgLang->formatNum( $this->file
->getHeight() )
1487 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file
->getSize() ) ) .
1489 $pageLink = $this->getLink();
1491 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1492 $data . ' ' . $this->getComment(). '</li>';
1497 * List for filearchive table items
1499 class RevDel_ArchivedFileList
extends RevDel_FileList
{
1500 var $type = 'filearchive';
1501 var $idField = 'fa_id';
1502 var $dateField = 'fa_timestamp';
1504 public function doQuery( $db ) {
1505 $ids = array_map( 'intval', $this->ids
);
1506 return $db->select( 'filearchive', '*',
1508 'fa_name' => $this->title
->getDBkey(),
1515 public function newItem( $row ) {
1516 return new RevDel_ArchivedFileItem( $this, $row );
1521 * Item class for a filearchive table row
1523 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
1524 public function __construct( $list, $row ) {
1525 RevDel_Item
::__construct( $list, $row );
1526 $this->file
= ArchivedFile
::newFromRow( $row );
1529 public function getId() {
1530 return $this->row
->fa_id
;
1533 public function setBits( $bits ) {
1534 $dbw = wfGetDB( DB_MASTER
);
1535 $dbw->update( 'filearchive',
1536 array( 'fa_deleted' => $bits ),
1538 'fa_id' => $this->row
->fa_id
,
1539 'fa_deleted' => $this->getBits(),
1543 return (bool)$dbw->affectedRows();
1546 protected function getLink() {
1547 global $wgLang, $wgUser;
1548 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1549 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1550 $key = $this->file
->getKey();
1551 return $this->special
->skin
->link( $undelete, $date, array(),
1553 'target' => $this->list->title
->getPrefixedText(),
1555 'token' => $wgUser->editToken( $key )
1562 * List for logging table items
1564 class RevDel_LogList
extends RevDel_List
{
1565 var $type = 'logging';
1566 var $idField = 'log_id';
1567 var $dateField = 'log_timestamp';
1569 public function doQuery( $db ) {
1570 global $wgMessageCache;
1571 $wgMessageCache->loadAllMessages();
1572 $ids = array_map( 'intval', $this->ids
);
1573 return $db->select( 'logging', '*',
1574 array( 'log_id' => $ids ),
1579 public function newItem( $row ) {
1580 return new RevDel_LogItem( $this, $row );
1583 public function getSuppressBit() {
1584 return Revision
::DELETED_RESTRICTED
;
1587 public function getLogAction() {
1591 public function getLogParams( $params ) {
1593 implode( ',', $params['ids'] ),
1594 "ofield={$params['oldBits']}",
1595 "nfield={$params['newBits']}"
1601 * Item class for a logging table row
1603 class RevDel_LogItem
extends RevDel_Item
{
1604 public function canView() {
1605 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
);
1608 public function getBits() {
1609 return $this->row
->log_deleted
;
1612 public function setBits( $bits ) {
1613 $dbw = wfGetDB( DB_MASTER
);
1614 $dbw->update( 'recentchanges',
1616 'rc_deleted' => $bits,
1620 'rc_logid' => $this->row
->log_id
,
1621 'rc_timestamp' => $this->row
->log_timestamp
1625 $dbw->update( 'logging',
1626 array( 'log_deleted' => $bits ),
1628 'log_id' => $this->row
->log_id
,
1629 'log_deleted' => $this->getBits()
1633 return (bool)$dbw->affectedRows();
1636 public function getHTML() {
1639 $date = htmlspecialchars( $wgLang->timeanddate( $this->row
->log_timestamp
) );
1640 $paramArray = LogPage
::extractParams( $this->row
->log_params
);
1641 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
1643 $logtitle = SpecialPage
::getTitleFor( 'Log' );
1644 $loglink = $this->special
->skin
->link(
1648 array( 'page' => $title->getPrefixedText() )
1651 if( !$this->canView() ) {
1652 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1654 $action = LogPage
::actionText( $this->row
->log_type
, $this->row
->log_action
, $title,
1655 $this->special
->skin
, $paramArray, true, true );
1656 if( $this->row
->log_deleted
& LogPage
::DELETED_ACTION
)
1657 $action = '<span class="history-deleted">' . $action . '</span>';
1660 $userLink = $this->special
->skin
->userLink( $this->row
->log_user
,
1661 User
::WhoIs( $this->row
->log_user
) );
1662 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_USER
) ) {
1663 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1666 $comment = $wgLang->getDirMark() . $this->special
->skin
->commentBlock( $this->row
->log_comment
);
1667 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
1668 $comment = '<span class="history-deleted">' . $comment . '</span>';
1670 return "<li>($loglink) $date $userLink $action $comment</li>";