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;
364 Xml
::openElement( 'form', array( 'method' => 'post',
365 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
366 'id' => 'mw-revdel-form-revisions' ) ) .
367 Xml
::openElement( 'fieldset' ) .
368 Xml
::element( 'legend', null, wfMsg( 'revdelete-legend' ) ) .
369 $this->buildCheckBoxes( $bitfields ) .
371 '<td>' . Xml
::label( wfMsg('revdelete-log'), 'wpRevDeleteReasonList' ) . '</td>' .
372 '<td>' . Xml
::listDropDown( 'wpRevDeleteReasonList',
373 wfMsgForContent( 'revdelete-reason-dropdown' ),
374 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
377 '<td>' . Xml
::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) . '</td>' .
378 '<td>' . Xml
::input( 'wpReason', 60, $this->otherReason
, array('id'=>'wpReason') ) . '</td>' .
379 "\n</tr></table>\n" .
380 Xml
::submitButton( wfMsg( 'revdelete-submit' ), array( 'name' => 'wpSubmit' ) ) .
381 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
382 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
383 Xml
::hidden( 'type', $this->typeName
) .
384 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
385 Xml
::closeElement( 'fieldset' ) .
386 Xml
::closeElement( 'form' ) . "\n"
391 * Show some introductory text
392 * FIXME Wikimedia-specific policy text
394 protected function addUsageText() {
395 global $wgOut, $wgUser;
396 $wgOut->addWikiMsg( 'revdelete-text' );
397 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
398 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
403 * @param $bitfields Interger: aggregate bitfield of all the bitfields
404 * @return String: HTML
406 protected function buildCheckBoxes( $bitfields ) {
408 // FIXME: all items checked for just one rev are checked, even if not set for the others
409 foreach( $this->checks
as $item ) {
410 list( $message, $name, $field ) = $item;
411 $innerHTML = Xml
::checkLabel( wfMsg($message), $name, $name, $bitfields & $field );
412 if( $field == Revision
::DELETED_RESTRICTED
)
413 $innerHTML = "<b>$innerHTML</b>";
414 $line = Xml
::tags( 'div', null, $innerHTML );
421 * UI entry point for form submission.
422 * @param $request WebRequest
424 protected function submit( $request ) {
425 global $wgUser, $wgOut;
426 # Check edit token on submission
427 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
428 $wgOut->addWikiMsg( 'sessionfailure' );
431 $bitfield = $this->extractBitfield( $request );
432 $listReason = $request->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
433 $comment = $listReason;
434 if( $comment != 'other' && $this->otherReason
!= '' ) {
435 // Entry from drop down menu + additional comment
436 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason
;
437 } elseif( $comment == 'other' ) {
438 $comment = $this->otherReason
;
440 # Can the user set this field?
441 if( $bitfield & Revision
::DELETED_RESTRICTED
&& !$wgUser->isAllowed('suppressrevision') ) {
442 $wgOut->permissionRequired( 'suppressrevision' );
445 # If the save went through, go to success message...
446 $status = $this->save( $bitfield, $comment, $this->targetObj
);
447 if ( $status->isGood() ) {
450 # ...otherwise, bounce back to form...
452 $this->failure( $status );
458 * Report that the submit operation succeeded
460 protected function success() {
462 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
463 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo
['success'] );
464 $this->list->reloadFromMaster();
469 * Report that the submit operation failed
471 protected function failure( $status ) {
473 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
474 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
479 * Put together a rev_deleted bitfield from the submitted checkboxes
480 * @param $request WebRequest
483 protected function extractBitfield( $request ) {
485 foreach( $this->checks
as $item ) {
486 list( /* message */ , $name, $field ) = $item;
487 if( $request->getCheck( $name ) ) {
495 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
497 protected function save( $bitfield, $reason, $title ) {
498 // Don't allow simply locking the interface for no reason
499 if( $bitfield == Revision
::DELETED_RESTRICTED
) {
500 return Status
::newFatal( 'revdelete-only-restricted' );
502 return $this->getList()->setVisibility( array(
503 'value' => $bitfield,
504 'comment' => $reason ) );
509 * Temporary b/c interface, collection of static functions.
510 * @ingroup SpecialPage
512 class RevisionDeleter
{
514 * Checks for a change in the bitfield for a certain option and updates the
515 * provided array accordingly.
517 * @param $desc String: description to add to the array if the option was
518 * enabled / disabled.
519 * @param $field Integer: the bitmask describing the single option.
520 * @param $diff Integer: the xor of the old and new bitfields.
521 * @param $new Integer: the new bitfield
522 * @param $arr Array: the array to update.
524 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
525 if( $diff & $field ) {
526 $arr[ ( $new & $field ) ?
0 : 1 ][] = $desc;
531 * Gets an array describing the changes made to the visibilit of the revision.
532 * If the resulting array is $arr, then $arr[0] will contain an array of strings
533 * describing the items that were hidden, $arr[2] will contain an array of strings
534 * describing the items that were unhidden, and $arr[3] will contain an array with
535 * a single string, which can be one of "applied restrictions to sysops",
536 * "removed restrictions from sysops", or null.
538 * @param $n Integer: the new bitfield.
539 * @param $o Integer: the old bitfield.
540 * @return An array as described above.
542 protected static function getChanges( $n, $o ) {
544 $ret = array( 0 => array(), 1 => array(), 2 => array() );
545 // Build bitfield changes in language
546 self
::checkItem( wfMsgForContent( 'revdelete-content' ),
547 Revision
::DELETED_TEXT
, $diff, $n, $ret );
548 self
::checkItem( wfMsgForContent( 'revdelete-summary' ),
549 Revision
::DELETED_COMMENT
, $diff, $n, $ret );
550 self
::checkItem( wfMsgForContent( 'revdelete-uname' ),
551 Revision
::DELETED_USER
, $diff, $n, $ret );
552 // Restriction application to sysops
553 if( $diff & Revision
::DELETED_RESTRICTED
) {
554 if( $n & Revision
::DELETED_RESTRICTED
)
555 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
557 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
563 * Gets a log message to describe the given revision visibility change. This
564 * message will be of the form "[hid {content, edit summary, username}];
565 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
567 * @param $count Integer: The number of effected revisions.
568 * @param $nbitfield Integer: The new bitfield for the revision.
569 * @param $obitfield Integer: The old bitfield for the revision.
570 * @param $isForLog Boolean
572 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
575 $changes = self
::getChanges( $nbitfield, $obitfield );
576 if( count( $changes[0] ) ) {
577 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
579 if( count( $changes[1] ) ) {
581 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
583 if( count( $changes[2] ) ) {
584 $s .= $s ?
' (' . $changes[2][0] . ')' : $changes[2][0];
586 $msg = $isForLog ?
'logdelete-log-message' : 'revdelete-log-message';
587 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
591 // Get DB field name for URL param...
592 // Future code for other things may also track
593 // other types of revision-specific changes.
594 public static function getRelationType( $typeName ) {
595 if ( isset( SpecialRevisionDelete
::$deprecatedTypeMap[$typeName] ) ) {
596 $typeName = SpecialRevisionDelete
::$deprecatedTypeMap[$typeName];
598 if ( isset( SpecialRevisionDelete
::$allowedTypes[$typeName] ) ) {
599 $class = SpecialRevisionDelete
::$allowedTypes[$typeName]['list-class'];
600 $list = new $class( null, null, null );
601 return $list->getIdField();
609 * Abstract base class for a list of deletable items
611 abstract class RevDel_List
{
612 var $special, $title, $ids, $res, $current;
613 var $type = null; // override this
614 var $idField = null; // override this
615 var $dateField = false; // override this
618 * @param $special The parent SpecialPage
619 * @param $title The target title
620 * @param $ids Array of IDs
622 public function __construct( $special, $title, $ids ) {
623 $this->special
= $special;
624 $this->title
= $title;
629 * Get the internal type name of this list. Equal to the table name.
631 public function getType() {
636 * Get the DB field name associated with the ID list/
638 public function getIdField() {
639 return $this->idField
;
643 * Get the DB field name storing timestamps
645 public function getTimestampField() {
646 return $this->dateField
;
650 * Set the visibility for the revisions in this list. Logging and
651 * transactions are done here.
653 * @param $params Associative array of parameters. Members are:
654 * value: The integer value to set the visibility to
655 * comment: The log comment.
658 public function setVisibility( $params ) {
659 $newBits = $params['value'];
660 $comment = $params['comment'];
663 $dbw = wfGetDB( DB_MASTER
);
664 $this->doQuery( $dbw );
666 $status = Status
::newGood();
667 $missing = array_flip( $this->ids
);
668 $this->clearFileOps();
669 $idsForLog = array();
671 for ( $this->reset(); $this->current(); $this->next() ) {
672 $item = $this->current();
673 unset( $missing[ $item->getId() ] );
675 // Make error messages less vague
676 $oldBits = $item->getBits();
677 if ( $oldBits == $newBits ) {
678 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
679 $status->failCount++
;
681 } elseif ( $oldBits == 0 && $newBits != 0 ) {
683 } elseif ( $oldBits != 0 && $newBits == 0 ) {
689 if ( $item->isHideCurrentOp( $newBits ) ) {
690 // Cannot hide current version text
691 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
692 $status->failCount++
;
695 if ( !$item->canView() ) {
696 // Cannot access this revision
697 $msg = $opType == 'show' ?
'revdelete-show-no-access' : 'revdelete-modify-no-access';
698 $status->error( $msg, $item->formatDate(), $item->formatTime() );
699 $status->failCount++
;
703 // Update the revision
704 $ok = $item->setBits( $newBits );
707 $idsForLog[] = $item->getId();
708 $status->successCount++
;
710 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
711 $status->failCount++
;
715 // Handle missing revisions
716 foreach ( $missing as $id => $unused ) {
717 $status->error( 'revdelete-modify-missing', $id );
718 $status->failCount++
;
721 if ( $status->successCount
== 0 ) {
727 // Save success count
728 $successCount = $status->successCount
;
730 // Move files, if there are any
731 $status->merge( $this->doPreCommitUpdates() );
732 if ( !$status->isOK() ) {
733 // Fatal error, such as no configured archive directory
739 $this->updateLog( array(
740 'title' => $this->title
,
741 'count' => $successCount,
742 'newBits' => $newBits,
743 'oldBits' => $oldBits,
744 'comment' => $comment,
750 $status->merge( $this->doPostCommitUpdates() );
755 * Reload the list data from the master DB. This can be done after setVisibility()
756 * to allow $item->getHTML() to show the new data.
758 function reloadFromMaster() {
759 $dbw = wfGetDB( DB_MASTER
);
760 $this->res
= $this->doQuery( $dbw );
764 * Record a log entry on the action
765 * @param $params Associative array of parameters:
766 * newBits: The new value of the *_deleted bitfield
767 * oldBits: The old value of the *_deleted bitfield.
768 * title: The target title
770 * comment: The log comment
772 protected function updateLog( $params ) {
773 // Get the URL param's corresponding DB field
774 $field = RevisionDeleter
::getRelationType( $this->getType() );
776 throw new MWException( "Bad log URL param type!" );
778 // Put things hidden from sysops in the oversight log
779 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
780 $logType = 'suppress';
784 // Add params for effected page and ids
785 $logParams = $this->getLogParams( $params );
786 // Actually add the deletion log entry
787 $log = new LogPage( $logType );
788 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
789 $params['comment'], $logParams );
790 // Allow for easy searching of deletion log items for revision/log items
791 $log->addRelations( $field, $params['ids'], $logid );
795 * Get the log action for this list type
797 public function getLogAction() {
802 * Get log parameter array.
803 * @param $params Associative array of log parameters, same as updateLog()
806 public function getLogParams( $params ) {
809 implode( ',', $params['ids'] ),
810 "ofield={$params['oldBits']}",
811 "nfield={$params['newBits']}"
816 * Initialise the current iteration pointer
818 protected function initCurrent() {
819 $row = $this->res
->current();
821 $this->current
= $this->newItem( $row );
823 $this->current
= false;
828 * Start iteration. This must be called before current() or next().
829 * @return First list item
831 public function reset() {
833 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
835 $this->res
->rewind();
837 $this->initCurrent();
838 return $this->current
;
842 * Get the current list item, or false if we are at the end
844 public function current() {
845 return $this->current
;
849 * Move the iteration pointer to the next list item, and return it.
851 public function next() {
853 $this->initCurrent();
854 return $this->current
;
858 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
861 public function clearFileOps() {
865 * A hook for setVisibility(): do batch updates pre-commit.
869 public function doPreCommitUpdates() {
870 return Status
::newGood();
874 * A hook for setVisibility(): do any necessary updates post-commit.
878 public function doPostCommitUpdates() {
879 return Status
::newGood();
883 * Create an item object from a DB result row
884 * @param $row stdclass
886 abstract public function newItem( $row );
889 * Do the DB query to iterate through the objects.
890 * @param $db Database object to use for the query
892 abstract public function doQuery( $db );
895 * Get the integer value of the flag used for suppression
897 abstract public function getSuppressBit();
901 * Abstract base class for deletable items
903 abstract class RevDel_Item
{
904 /** The parent SpecialPage */
907 /** The parent RevDel_List */
910 /** The DB result row */
914 * @param $list RevDel_List
915 * @param $row DB result row
917 public function __construct( $list, $row ) {
918 $this->special
= $list->special
;
924 * Get the ID, as it would appear in the ids URL parameter
926 public function getId() {
927 $field = $this->list->getIdField();
928 return $this->row
->$field;
932 * Get the date, formatted with $wgLang
934 public function formatDate() {
936 return $wgLang->date( $this->getTimestamp() );
940 * Get the time, formatted with $wgLang
942 public function formatTime() {
944 return $wgLang->time( $this->getTimestamp() );
948 * Get the timestamp in MW 14-char form
950 public function getTimestamp() {
951 $field = $this->list->getTimestampField();
952 return wfTimestamp( TS_MW
, $this->row
->$field );
956 * Returns true if the item is "current", and the operation to set the given
957 * bits can't be executed for that reason
960 public function isHideCurrentOp( $newBits ) {
965 * Returns true if the current user can view the item
967 abstract public function canView();
970 * Get the current deletion bitfield value
972 abstract public function getBits();
975 * Get the HTML of the list item. Should be include <li></li> tags.
976 * This is used to show the list in HTML form, by the special page.
978 abstract public function getHTML();
981 * Set the visibility of the item. This should do any necessary DB queries.
983 * The DB update query should have a condition which forces it to only update
984 * if the value in the DB matches the value fetched earlier with the SELECT.
985 * If the update fails because it did not match, the function should return
986 * false. This prevents concurrency problems.
988 * @return boolean success
990 abstract public function setBits( $newBits );
994 * List for revision table items
996 class RevDel_RevisionList
extends RevDel_List
{
998 var $type = 'revision';
999 var $idField = 'rev_id';
1000 var $dateField = 'rev_timestamp';
1002 public function doQuery( $db ) {
1003 $ids = array_map( 'intval', $this->ids
);
1004 return $db->select( array('revision','page'), '*',
1006 'rev_page' => $this->title
->getArticleID(),
1008 'rev_page = page_id'
1014 public function newItem( $row ) {
1015 return new RevDel_RevisionItem( $this, $row );
1018 public function getCurrent() {
1019 if ( is_null( $this->currentRevId
) ) {
1020 $dbw = wfGetDB( DB_MASTER
);
1021 $this->currentRevId
= $dbw->selectField(
1022 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
1024 return $this->currentRevId
;
1027 public function getSuppressBit() {
1028 return Revision
::DELETED_RESTRICTED
;
1031 public function doPreCommitUpdates() {
1032 $this->title
->invalidateCache();
1033 return Status
::newGood();
1036 public function doPostCommitUpdates() {
1037 $this->title
->purgeSquid();
1038 // Extensions that require referencing previous revisions may need this
1039 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$this->title
) );
1040 return Status
::newGood();
1045 * Item class for a revision table row
1047 class RevDel_RevisionItem
extends RevDel_Item
{
1050 public function __construct( $list, $row ) {
1051 parent
::__construct( $list, $row );
1052 $this->revision
= new Revision( $row );
1055 public function canView() {
1056 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
);
1059 public function getBits() {
1060 return $this->revision
->mDeleted
;
1063 public function setBits( $bits ) {
1064 $dbw = wfGetDB( DB_MASTER
);
1065 // Update revision table
1066 $dbw->update( 'revision',
1067 array( 'rev_deleted' => $bits ),
1069 'rev_id' => $this->revision
->getId(),
1070 'rev_page' => $this->revision
->getPage(),
1071 'rev_deleted' => $this->getBits()
1075 if ( !$dbw->affectedRows() ) {
1079 // Update recentchanges table
1080 $dbw->update( 'recentchanges',
1082 'rc_deleted' => $bits,
1086 'rc_this_oldid' => $this->revision
->getId(), // condition
1087 // non-unique timestamp index
1088 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ),
1095 public function isDeleted() {
1096 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
1099 public function isHideCurrentOp( $newBits ) {
1100 return ( $newBits & Revision
::DELETED_TEXT
)
1101 && $this->list->getCurrent() == $this->getId();
1105 * Get the HTML link to the revision text.
1106 * Overridden by RevDel_ArchiveItem.
1108 protected function getRevisionLink() {
1110 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1111 if ( $this->isDeleted() && !$this->canView() ) {
1114 return $this->special
->skin
->link(
1119 'oldid' => $this->revision
->getId(),
1126 * Get the HTML link to the diff.
1127 * Overridden by RevDel_ArchiveItem
1129 protected function getDiffLink() {
1130 if ( $this->isDeleted() && !$this->canView() ) {
1131 return wfMsgHtml('diff');
1134 $this->special
->skin
->link(
1139 'diff' => $this->revision
->getId(),
1151 public function getHTML() {
1152 $difflink = $this->getDiffLink();
1153 $revlink = $this->getRevisionLink();
1154 $userlink = $this->special
->skin
->revUserLink( $this->revision
);
1155 $comment = $this->special
->skin
->revComment( $this->revision
);
1156 if ( $this->isDeleted() ) {
1157 $revlink = "<span class=\"history-deleted\">$revlink</span>";
1159 return "<li>($difflink) $revlink $userlink $comment</li>";
1164 * List for archive table items, i.e. revisions deleted via action=delete
1166 class RevDel_ArchiveList
extends RevDel_RevisionList
{
1167 var $type = 'archive';
1168 var $idField = 'ar_timestamp';
1169 var $dateField = 'ar_timestamp';
1171 public function doQuery( $db ) {
1172 $timestamps = array();
1173 foreach ( $this->ids
as $id ) {
1174 $timestamps[] = $db->timestamp( $id );
1176 return $db->select( 'archive', '*',
1178 'ar_namespace' => $this->title
->getNamespace(),
1179 'ar_title' => $this->title
->getDBkey(),
1180 'ar_timestamp' => $timestamps
1186 public function newItem( $row ) {
1187 return new RevDel_ArchiveItem( $this, $row );
1190 public function doPreCommitUpdates() {
1191 return Status
::newGood();
1194 public function doPostCommitUpdates() {
1195 return Status
::newGood();
1200 * Item class for a archive table row
1202 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
1203 public function __construct( $list, $row ) {
1204 RevDel_Item
::__construct( $list, $row );
1205 $this->revision
= Revision
::newFromArchiveRow( $row,
1206 array( 'page' => $this->list->title
->getArticleId() ) );
1209 public function getId() {
1210 # Convert DB timestamp to MW timestamp
1211 return $this->revision
->getTimestamp();
1214 public function setBits( $bits ) {
1215 $dbw = wfGetDB( DB_MASTER
);
1216 $dbw->update( 'archive',
1217 array( 'ar_deleted' => $bits ),
1218 array( 'ar_namespace' => $this->list->title
->getNamespace(),
1219 'ar_title' => $this->list->title
->getDBkey(),
1220 // use timestamp for index
1221 'ar_timestamp' => $this->row
->ar_timestamp
,
1222 'ar_rev_id' => $this->row
->ar_rev_id
,
1223 'ar_deleted' => $this->getBits()
1226 return (bool)$dbw->affectedRows();
1229 protected function getRevisionLink() {
1231 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1232 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1233 if ( $this->isDeleted() && !$this->canView() ) {
1236 return $this->special
->skin
->link( $undelete, $date, array(),
1238 'target' => $this->list->title
->getPrefixedText(),
1239 'timestamp' => $this->revision
->getTimestamp()
1243 protected function getDiffLink() {
1244 if ( $this->isDeleted() && !$this->canView() ) {
1245 return wfMsgHtml( 'diff' );
1247 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1248 return $this->special
->skin
->link( $undelete, wfMsgHtml('diff'), array(),
1250 'target' => $this->list->title
->getPrefixedText(),
1252 'timestamp' => $this->revision
->getTimestamp()
1258 * List for oldimage table items
1260 class RevDel_FileList
extends RevDel_List
{
1261 var $type = 'oldimage';
1262 var $idField = 'oi_archive_name';
1263 var $dateField = 'oi_timestamp';
1264 var $storeBatch, $deleteBatch, $cleanupBatch;
1266 public function doQuery( $db ) {
1267 $archiveName = array();
1268 foreach( $this->ids
as $timestamp ) {
1269 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
1271 return $db->select( 'oldimage', '*',
1273 'oi_name' => $this->title
->getDBkey(),
1274 'oi_archive_name' => $archiveNames
1280 public function newItem( $row ) {
1281 return new RevDel_FileItem( $this, $row );
1284 public function clearFileOps() {
1285 $this->deleteBatch
= array();
1286 $this->storeBatch
= array();
1287 $this->cleanupBatch
= array();
1290 public function doPreCommitUpdates() {
1291 $status = Status
::newGood();
1292 $repo = RepoGroup
::singleton()->getLocalRepo();
1293 if ( $this->storeBatch
) {
1294 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
1296 if ( !$status->isOK() ) {
1299 if ( $this->deleteBatch
) {
1300 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
1302 if ( !$status->isOK() ) {
1303 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1304 // modified (but destined for rollback) causes data loss
1307 if ( $this->cleanupBatch
) {
1308 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
1313 public function doPostCommitUpdates() {
1314 $file = wfLocalFile( $this->title
);
1315 $file->purgeCache();
1316 $file->purgeDescription();
1317 return Status
::newGood();
1320 public function getSuppressBit() {
1321 return File
::DELETED_RESTRICTED
;
1326 * Item class for an oldimage table row
1328 class RevDel_FileItem
extends RevDel_Item
{
1331 public function __construct( $list, $row ) {
1332 parent
::__construct( $list, $row );
1333 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
1336 public function getId() {
1337 $parts = explode( '!', $this->row
->oi_archive_name
);
1341 public function canView() {
1342 return $this->file
->userCan( File
::DELETED_RESTRICTED
);
1345 public function getBits() {
1346 return $this->file
->getVisibility();
1349 public function setBits( $bits ) {
1351 # FIXME: move to LocalFile.php
1352 if ( $this->isDeleted() ) {
1353 if ( $bits & File
::DELETED_FILE
) {
1357 $key = $this->file
->getStorageKey();
1358 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1359 $this->list->storeBatch
[] = array(
1360 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1362 $this->file
->getRel()
1364 $this->list->cleanupBatch
[] = $key;
1366 } elseif ( $bits & File
::DELETED_FILE
) {
1368 $key = $this->file
->getStorageKey();
1369 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1370 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
1373 # Do the database operations
1374 $dbw = wfGetDB( DB_MASTER
);
1375 $dbw->update( 'oldimage',
1376 array( 'oi_deleted' => $bits ),
1378 'oi_name' => $this->row
->oi_name
,
1379 'oi_timestamp' => $this->row
->oi_timestamp
,
1380 'oi_deleted' => $this->getBits()
1384 return (bool)$dbw->affectedRows();
1387 public function isDeleted() {
1388 return $this->file
->isDeleted( File
::DELETED_FILE
);
1392 * Get the link to the file.
1393 * Overridden by RevDel_ArchivedFileItem.
1395 protected function getLink() {
1397 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1398 if ( $this->isDeleted() ) {
1400 if ( !$this->canView() ) {
1403 return $this->special
->skin
->link(
1404 $this->special
->getTitle(),
1407 'target' => $this->list->title
->getPrefixedText(),
1408 'file' => $this->file
->sha1
. '.' . $this->file
->getExtension()
1414 $url = $this->file
->getUrl();
1415 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
1419 * Generate a user tool link cluster if the current user is allowed to view it
1420 * @return string HTML
1422 protected function getUserTools() {
1423 if( $this->file
->userCan( Revision
::DELETED_USER
) ) {
1424 $link = $this->special
->skin
->userLink( $this->file
->user
, $this->file
->user_text
) .
1425 $this->special
->skin
->userToolLinks( $this->file
->user
, $this->file
->user_text
);
1427 $link = wfMsgHtml( 'rev-deleted-user' );
1429 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
1430 return '<span class="history-deleted">' . $link . '</span>';
1436 * Wrap and format the file's comment block, if the current
1437 * user is allowed to view it.
1439 * @return string HTML
1441 protected function getComment() {
1442 if( $this->file
->userCan( File
::DELETED_COMMENT
) ) {
1443 $block = $this->special
->skin
->commentBlock( $this->file
->description
);
1445 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1447 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
1448 return "<span class=\"history-deleted\">$block</span>";
1453 public function getHTML() {
1458 $wgLang->formatNum( $this->file
->getWidth() ),
1459 $wgLang->formatNum( $this->file
->getHeight() )
1462 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file
->getSize() ) ) .
1464 $pageLink = $this->getLink();
1466 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1467 $data . ' ' . $this->getComment(). '</li>';
1472 * List for filearchive table items
1474 class RevDel_ArchivedFileList
extends RevDel_FileList
{
1475 var $type = 'filearchive';
1476 var $idField = 'fa_id';
1477 var $dateField = 'fa_timestamp';
1479 public function doQuery( $db ) {
1480 $ids = array_map( 'intval', $this->ids
);
1481 return $db->select( 'filearchive', '*',
1483 'fa_name' => $this->title
->getDBkey(),
1490 public function newItem( $row ) {
1491 return new RevDel_ArchivedFileItem( $this, $row );
1496 * Item class for a filearchive table row
1498 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
1499 public function __construct( $list, $row ) {
1500 RevDel_Item
::__construct( $list, $row );
1501 $this->file
= ArchivedFile
::newFromRow( $row );
1504 public function getId() {
1505 return $this->row
->fa_id
;
1508 public function setBits( $bits ) {
1509 $dbw = wfGetDB( DB_MASTER
);
1510 $dbw->update( 'filearchive',
1511 array( 'fa_deleted' => $bits ),
1513 'fa_id' => $this->row
->fa_id
,
1514 'fa_deleted' => $this->getBits(),
1518 return (bool)$dbw->affectedRows();
1521 protected function getLink() {
1522 global $wgLang, $wgUser;
1523 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1524 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1525 $key = $this->file
->getKey();
1526 return $this->special
->skin
->link( $undelete, $date, array(),
1528 'target' => $this->list->title
->getPrefixedText(),
1530 'token' => $wgUser->editToken( $key )
1537 * List for logging table items
1539 class RevDel_LogList
extends RevDel_List
{
1540 var $type = 'logging';
1541 var $idField = 'log_id';
1542 var $dateField = 'log_timestamp';
1544 public function doQuery( $db ) {
1545 global $wgMessageCache;
1546 $wgMessageCache->loadAllMessages();
1547 $ids = array_map( 'intval', $this->ids
);
1548 return $db->select( 'logging', '*',
1549 array( 'log_id' => $ids ),
1554 public function newItem( $row ) {
1555 return new RevDel_LogItem( $this, $row );
1558 public function getSuppressBit() {
1559 return Revision
::DELETED_RESTRICTED
;
1562 public function getLogAction() {
1566 public function getLogParams( $params ) {
1568 implode( ',', $params['ids'] ),
1569 "ofield={$params['oldBits']}",
1570 "nfield={$params['newBits']}"
1576 * Item class for a logging table row
1578 class RevDel_LogItem
extends RevDel_Item
{
1579 public function canView() {
1580 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
);
1583 public function getBits() {
1584 return $this->row
->log_deleted
;
1587 public function setBits( $bits ) {
1588 $dbw = wfGetDB( DB_MASTER
);
1589 $dbw->update( 'recentchanges',
1591 'rc_deleted' => $bits,
1595 'rc_logid' => $this->row
->log_id
,
1596 'rc_timestamp' => $this->row
->log_timestamp
1600 $dbw->update( 'logging',
1601 array( 'log_deleted' => $bits ),
1603 'log_id' => $this->row
->log_id
,
1604 'log_deleted' => $this->getBits()
1608 return (bool)$dbw->affectedRows();
1611 public function getHTML() {
1614 $date = htmlspecialchars( $wgLang->timeanddate( $this->row
->log_timestamp
) );
1615 $paramArray = LogPage
::extractParams( $this->row
->log_params
);
1616 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
1618 $logtitle = SpecialPage
::getTitleFor( 'Log' );
1619 $loglink = $this->special
->skin
->link(
1623 array( 'page' => $title->getPrefixedText() )
1626 if( !$this->canView() ) {
1627 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1629 $action = LogPage
::actionText( $this->row
->log_type
, $this->row
->log_action
, $title,
1630 $this->special
->skin
, $paramArray, true, true );
1631 if( $this->row
->log_deleted
& LogPage
::DELETED_ACTION
)
1632 $action = '<span class="history-deleted">' . $action . '</span>';
1635 $userLink = $this->special
->skin
->userLink( $this->row
->log_user
,
1636 User
::WhoIs( $this->row
->log_user
) );
1637 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_USER
) ) {
1638 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1641 $comment = $wgLang->getDirMark() . $this->special
->skin
->commentBlock( $this->row
->log_comment
);
1642 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
1643 $comment = '<span class="history-deleted">' . $comment . '</span>';
1645 return "<li>($loglink) $date $userLink $action $comment</li>";