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 */
41 /** New bitfield value, used for form display post-submit */
45 * Assorted information about each type, needed by the special page.
46 * TODO Move some of this to the list class
48 static $allowedTypes = array(
50 'check-label' => 'revdelete-hide-text',
51 'deletion-bits' => Revision
::DELETED_TEXT
,
52 'success' => 'revdelete-success',
53 'failure' => 'revdelete-failure',
54 'list-class' => 'RevDel_RevisionList',
57 'check-label' => 'revdelete-hide-text',
58 'deletion-bits' => Revision
::DELETED_TEXT
,
59 'success' => 'revdelete-success',
60 'failure' => 'revdelete-failure',
61 'list-class' => 'RevDel_ArchiveList',
64 'check-label' => 'revdelete-hide-image',
65 'deletion-bits' => File
::DELETED_FILE
,
66 'success' => 'revdelete-success',
67 'failure' => 'revdelete-failure',
68 'list-class' => 'RevDel_FileList',
70 'filearchive' => array(
71 'check-label' => 'revdelete-hide-image',
72 'deletion-bits' => File
::DELETED_FILE
,
73 'success' => 'revdelete-success',
74 'failure' => 'revdelete-failure',
75 'list-class' => 'RevDel_ArchivedFileList',
78 'check-label' => 'revdelete-hide-name',
79 'deletion-bits' => LogPage
::DELETED_ACTION
,
80 'success' => 'logdelete-success',
81 'failure' => 'logdelete-failure',
82 'list-class' => 'RevDel_LogList',
86 /** Type map to support old log entries */
87 static $deprecatedTypeMap = array(
88 'oldid' => 'revision',
89 'artimestamp' => 'archive',
90 'oldimage' => 'oldimage',
91 'fileid' => 'filearchive',
95 public function __construct() {
96 parent
::__construct( 'Revisiondelete', 'deleterevision' );
99 public function execute( $par ) {
100 global $wgOut, $wgUser, $wgRequest;
101 if( !$wgUser->isAllowed( 'deleterevision' ) ) {
102 $wgOut->permissionRequired( 'deleterevision' );
104 } else if( wfReadOnly() ) {
105 $wgOut->readOnlyPage();
108 $this->skin
= $wgUser->getSkin();
110 $this->outputHeader();
111 $this->submitClicked
= $wgRequest->wasPosted() && $wgRequest->getBool( 'wpSubmit' );
112 # Handle our many different possible input types.
113 # Use CSV, since the cgi handling will break on arrays.
114 $this->ids
= explode( ',', $wgRequest->getVal('ids') );
115 $this->ids
= array_unique( array_filter( $this->ids
) );
116 $this->targetObj
= Title
::newFromText( $wgRequest->getText( 'target' ) );
118 # For reviewing deleted files...
119 $this->archiveName
= $wgRequest->getVal( 'file' );
120 $this->token
= $wgRequest->getVal( 'token' );
121 if ( $this->archiveName
&& $this->targetObj
) {
122 $this->tryShowFile( $this->archiveName
);
126 $this->typeName
= $wgRequest->getVal( 'type' );
127 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
128 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
132 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
133 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
136 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
138 # If we have revisions, get the title from the first one
139 # since they should all be from the same page. This allows
140 # for more flexibility with page moves...
141 if( $this->typeName
== 'revision' ) {
142 $rev = Revision
::newFromId( $this->ids
[0] );
143 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
145 # We need a target page!
146 if( is_null($this->targetObj
) ) {
147 $wgOut->addWikiMsg( 'undelete-header' );
150 # Give a link to the logs/hist for this page
151 $this->showConvenienceLinks();
153 # Initialise checkboxes
154 $this->checks
= array(
155 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
156 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
157 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
159 if( $wgUser->isAllowed('suppressrevision') ) {
160 $this->checks
[] = array( 'revdelete-hide-restricted',
161 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
164 # Either submit or create our form
165 if( $this->submitClicked
) {
166 $this->submit( $wgRequest );
170 $qc = $this->getLogQueryCond();
171 # Show relevant lines from the deletion log
172 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
173 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->targetObj
->getPrefixedText(), '', 25, $qc );
174 # Show relevant lines from the suppression log
175 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
176 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
177 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->targetObj
->getPrefixedText(), '', 25, $qc );
182 * Show some useful links in the subtitle
184 protected function showConvenienceLinks() {
185 global $wgOut, $wgUser;
186 # Give a link to the logs/hist for this page
187 if( $this->targetObj
) {
189 $logtitle = SpecialPage
::getTitleFor( 'Log' );
190 $links[] = $this->skin
->makeKnownLinkObj( $logtitle, wfMsgHtml( 'viewpagelogs' ),
191 wfArrayToCGI( array( 'page' => $this->targetObj
->getPrefixedUrl() ) ) );
192 # Give a link to the page history
193 $links[] = $this->skin
->makeKnownLinkObj( $this->targetObj
, wfMsgHtml( 'pagehist' ),
194 wfArrayToCGI( array( 'action' => 'history' ) ) );
195 # Link to deleted edits
196 if( $wgUser->isAllowed('undelete') ) {
197 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
198 $links[] = $this->skin
->makeKnownLinkObj( $undelete, wfMsgHtml( 'deletedhist' ),
199 wfArrayToCGI( array( 'target' => $this->targetObj
->getPrefixedDBkey() ) ) );
201 # Logs themselves don't have histories or archived revisions
202 $wgOut->setSubtitle( '<p>'.implode($links,' / ').'</p>' );
207 * Get the condition used for fetching log snippets
209 protected function getLogQueryCond() {
211 // Revision delete logs for these item
212 $conds['log_type'] = array('delete','suppress');
213 $conds['log_action'] = $this->getList()->getLogAction();
214 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
215 $conds['ls_value'] = $this->ids
;
220 * Show a deleted file version requested by the visitor.
221 * TODO Mostly copied from Special:Undelete. Refactor.
223 protected function tryShowFile( $archiveName ) {
224 global $wgOut, $wgRequest, $wgUser, $wgLang;
226 $repo = RepoGroup
::singleton()->getLocalRepo();
227 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
229 // Check if user is allowed to see this file
230 if ( !$oimage->exists() ) {
231 $wgOut->addWikiMsg( 'revdelete-no-file' );
234 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
235 $wgOut->permissionRequired( 'suppressrevision' );
238 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
239 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
240 $this->targetObj
->getText(),
241 $wgLang->date( $oimage->getTimestamp() ),
242 $wgLang->time( $oimage->getTimestamp() ) );
244 Xml
::openElement( 'form', array(
246 'action' => $this->getTitle()->getLocalUrl(
247 'target=' . urlencode( $oimage->getName() ) .
248 '&file=' . urlencode( $archiveName ) .
249 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
252 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
258 # We mustn't allow the output to be Squid cached, otherwise
259 # if an admin previews a deleted image, and it's cached, then
260 # a user without appropriate permissions can toddle off and
261 # nab the image, and Squid will serve it
262 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
263 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
264 $wgRequest->response()->header( 'Pragma: no-cache' );
266 # Stream the file to the client
268 require_once( "$IP/includes/StreamFile.php" );
269 $key = $oimage->getStorageKey();
270 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
271 wfStreamFile( $path );
275 * Get the list object for this request
277 protected function getList() {
278 if ( is_null( $this->list ) ) {
279 $class = $this->typeInfo
['list-class'];
280 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
286 * Show a list of items that we will operate on, and show a form with checkboxes
287 * which will allow the user to choose new visibility settings.
289 protected function showForm() {
290 global $wgOut, $wgUser, $wgLang;
293 if ( $this->typeName
== 'logging' ) {
294 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
296 $wgOut->addWikiMsg( 'revdelete-selected', $this->targetObj
->getPrefixedText(), count( $this->ids
) );
300 $wgOut->addHTML( "<ul>" );
302 $where = $revObjs = array();
306 $list = $this->getList();
307 for ( $list->reset(); $list->current(); $list->next() ) {
308 $item = $list->current();
309 if ( !$item->canView() ) {
310 if( !$this->submitClicked
) {
311 $wgOut->permissionRequired( 'suppressrevision' );
314 $UserAllowed = false;
317 $bitfields |
= $item->getBits();
318 $wgOut->addHTML( $item->getHTML() );
321 if( !$numRevisions ) {
322 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
326 if ( !is_null( $this->newBits
) ) {
327 $bitfields = $this->newBits
;
330 $wgOut->addHTML( "</ul>" );
332 $this->addUsageText();
334 // Normal sysops can always see what they did, but can't always change it
335 if( !$UserAllowed ) return;
338 Xml
::openElement( 'form', array( 'method' => 'post',
339 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ),
340 'id' => 'mw-revdel-form-revisions' ) ) .
341 Xml
::openElement( 'fieldset' ) .
342 Xml
::element( 'legend', null, wfMsg( 'revdelete-legend' ) ) .
343 $this->buildCheckBoxes( $bitfields ) .
344 '<p>' . Xml
::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ) . '</p>' .
345 '<p>' . Xml
::submitButton( wfMsg( 'revdelete-submit' ),
346 array( 'name' => 'wpSubmit' ) ) . '</p>' .
347 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
348 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
349 Xml
::hidden( 'type', $this->typeName
) .
350 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
351 Xml
::closeElement( 'fieldset' ) .
352 Xml
::closeElement( 'form' ) . "\n"
357 * Show some introductory text
358 * FIXME Wikimedia-specific policy text
360 protected function addUsageText() {
361 global $wgOut, $wgUser;
362 $wgOut->addWikiMsg( 'revdelete-text' );
363 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
364 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
369 * @param int $bitfields, aggregate bitfield of all the bitfields
370 * @returns string HTML
372 protected function buildCheckBoxes( $bitfields ) {
374 // FIXME: all items checked for just one rev are checked, even if not set for the others
375 foreach( $this->checks
as $item ) {
376 list( $message, $name, $field ) = $item;
377 $line = Xml
::tags( 'div', null, Xml
::checkLabel( wfMsg($message), $name, $name,
378 $bitfields & $field ) );
379 if( $field == Revision
::DELETED_RESTRICTED
) $line = "<b>$line</b>";
386 * UI entry point for form submission.
387 * @param WebRequest $request
389 protected function submit( $request ) {
390 global $wgUser, $wgOut;
391 # Check edit token on submission
392 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
393 $wgOut->addWikiMsg( 'sessionfailure' );
396 $bitfield = $this->extractBitfield( $request );
397 $comment = $request->getText( 'wpReason' );
398 # Can the user set this field?
399 if( $bitfield & Revision
::DELETED_RESTRICTED
&& !$wgUser->isAllowed('suppressrevision') ) {
400 $wgOut->permissionRequired( 'suppressrevision' );
403 # If the save went through, go to success message...
404 $status = $this->save( $bitfield, $comment, $this->targetObj
);
405 if ( $status->isGood() ) {
408 # ...otherwise, bounce back to form...
410 $this->failure( $status );
416 * Report that the submit operation succeeded
418 protected function success() {
420 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
421 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo
['success'] );
426 * Report that the submit operation failed
428 protected function failure( $status ) {
430 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
431 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
436 * Put together a rev_deleted bitfield from the submitted checkboxes
437 * @param WebRequest $request
440 protected function extractBitfield( $request ) {
442 foreach( $this->checks
as $item ) {
443 list( /* message */ , $name, $field ) = $item;
444 if( $request->getCheck( $name ) ) {
452 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
454 protected function save( $bitfield, $reason, $title ) {
455 // Don't allow simply locking the interface for no reason
456 if( $bitfield == Revision
::DELETED_RESTRICTED
) {
457 return Status
::newFatal( 'revdelete-only-restricted' );
459 $this->newBits
= $bitfield;
460 return $this->getList()->setVisibility( array(
461 'value' => $bitfield,
462 'comment' => $reason ) );
467 * Temporary b/c interface, collection of static functions.
468 * @ingroup SpecialPage
470 class RevisionDeleter
{
472 * Checks for a change in the bitfield for a certain option and updates the
473 * provided array accordingly.
475 * @param String $desc Description to add to the array if the option was
476 * enabled / disabled.
477 * @param int $field The bitmask describing the single option.
478 * @param int $diff The xor of the old and new bitfields.
479 * @param array $arr The array to update.
481 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
482 if( $diff & $field ) {
483 $arr[ ( $new & $field ) ?
0 : 1 ][] = $desc;
488 * Gets an array describing the changes made to the visibilit of the revision.
489 * If the resulting array is $arr, then $arr[0] will contain an array of strings
490 * describing the items that were hidden, $arr[2] will contain an array of strings
491 * describing the items that were unhidden, and $arr[3] will contain an array with
492 * a single string, which can be one of "applied restrictions to sysops",
493 * "removed restrictions from sysops", or null.
495 * @param int $n The new bitfield.
496 * @param int $o The old bitfield.
497 * @return An array as described above.
499 protected static function getChanges( $n, $o ) {
501 $ret = array( 0 => array(), 1 => array(), 2 => array() );
502 // Build bitfield changes in language
503 self
::checkItem( wfMsgForContent( 'revdelete-content' ),
504 Revision
::DELETED_TEXT
, $diff, $n, $ret );
505 self
::checkItem( wfMsgForContent( 'revdelete-summary' ),
506 Revision
::DELETED_COMMENT
, $diff, $n, $ret );
507 self
::checkItem( wfMsgForContent( 'revdelete-uname' ),
508 Revision
::DELETED_USER
, $diff, $n, $ret );
509 // Restriction application to sysops
510 if( $diff & Revision
::DELETED_RESTRICTED
) {
511 if( $n & Revision
::DELETED_RESTRICTED
)
512 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
514 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
520 * Gets a log message to describe the given revision visibility change. This
521 * message will be of the form "[hid {content, edit summary, username}];
522 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
524 * @param int $count The number of effected revisions.
525 * @param int $nbitfield The new bitfield for the revision.
526 * @param int $obitfield The old bitfield for the revision.
527 * @param bool $isForLog
529 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
532 $changes = self
::getChanges( $nbitfield, $obitfield );
533 if( count( $changes[0] ) ) {
534 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
536 if( count( $changes[1] ) ) {
538 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
540 if( count( $changes[2] ) ) {
541 $s .= $s ?
' (' . $changes[2][0] . ')' : $changes[2][0];
543 $msg = $isForLog ?
'logdelete-log-message' : 'revdelete-log-message';
544 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
548 // Get DB field name for URL param...
549 // Future code for other things may also track
550 // other types of revision-specific changes.
551 public static function getRelationType( $typeName ) {
552 if ( isset( SpecialRevisionDelete
::$deprecatedTypeMap[$typeName] ) ) {
553 $typeName = SpecialRevisionDelete
::$deprecatedTypeMap[$typeName];
555 if ( isset( SpecialRevisionDelete
::$allowedTypes[$typeName] ) ) {
556 $class = SpecialRevisionDelete
::$allowedTypes[$typeName]['list-class'];
557 $list = new $class( null, null, null );
558 return $list->getIdField();
566 * Abstract base class for a list of deletable items
568 abstract class RevDel_List
{
569 var $special, $title, $ids, $res, $current;
570 var $type = null; // override this
571 var $idField = null; // override this
572 var $dateField = false; // override this
575 * @param $special The parent SpecialPage
576 * @param $title The target title
577 * @param $ids Array of IDs
579 public function __construct( $special, $title, $ids ) {
580 $this->special
= $special;
581 $this->title
= $title;
586 * Get the internal type name of this list. Equal to the table name.
588 public function getType() {
593 * Get the DB field name associated with the ID list/
595 public function getIdField() {
596 return $this->idField
;
600 * Get the DB field name storing timestamps
602 public function getTimestampField() {
603 return $this->dateField
;
607 * Set the visibility for the revisions in this list. Logging and
608 * transactions are done here.
610 * @param $params Associative array of parameters. Members are:
611 * value: The integer value to set the visibility to
612 * comment: The log comment.
615 public function setVisibility( $params ) {
616 $newBits = $params['value'];
617 $comment = $params['comment'];
620 $dbw = wfGetDB( DB_MASTER
);
621 $this->doQuery( $dbw );
623 $status = Status
::newGood();
624 $missing = array_flip( $this->ids
);
625 $this->clearFileOps();
626 $idsForLog = array();
628 for ( $this->reset(); $this->current(); $this->next() ) {
629 $item = $this->current();
630 unset( $missing[ $item->getId() ] );
632 // Make error messages less vague
633 $oldBits = $item->getBits();
634 if ( $oldBits == $newBits ) {
635 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
636 $status->failCount++
;
638 } elseif ( $oldBits == 0 && $newBits != 0 ) {
640 } elseif ( $oldBits != 0 && $newBits == 0 ) {
646 if ( $item->isCurrent() && $opType == 'hide' ) {
647 // Cannot hide current version text
648 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
649 $status->failCount++
;
652 if ( !$item->canView() ) {
653 // Cannot access this revision
654 $msg = $opType == 'show' ?
'revdelete-show-no-access' : 'revdelete-modify-no-access';
655 $status->error( $msg, $item->formatDate(), $item->formatTime() );
656 $status->failCount++
;
660 // Update the revision
661 $ok = $item->setBits( $newBits );
664 $idsForLog[] = $item->getId();
665 $status->successCount++
;
667 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
668 $status->failCount++
;
672 // Handle missing revisions
673 foreach ( $missing as $id => $unused ) {
674 $status->error( 'revdelete-modify-missing', $id );
675 $status->failCount++
;
678 if ( $status->successCount
== 0 ) {
684 // Save success count
685 $successCount = $status->successCount
;
687 // Move files, if there are any
688 $status->merge( $this->doPreCommitUpdates() );
689 if ( !$status->isOK() ) {
690 // Fatal error, such as no configured archive directory
696 $this->updateLog( array(
697 'title' => $this->title
,
698 'count' => $successCount,
699 'newBits' => $newBits,
700 'oldBits' => $oldBits,
701 'comment' => $comment,
707 $status->merge( $this->doPostCommitUpdates() );
712 * Record a log entry on the action
713 * @param $params Associative array of parameters:
714 * newBits: The new value of the *_deleted bitfield
715 * oldBits: The old value of the *_deleted bitfield.
716 * title: The target title
718 * comment: The log comment
720 protected function updateLog( $params ) {
721 // Get the URL param's corresponding DB field
722 $field = RevisionDeleter
::getRelationType( $this->getType() );
724 throw new MWException( "Bad log URL param type!" );
726 // Put things hidden from sysops in the oversight log
727 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
728 $logType = 'suppress';
732 // Add params for effected page and ids
733 $logParams = $this->getLogParams( $params );
734 // Actually add the deletion log entry
735 $log = new LogPage( $logType );
736 $logid = $log->addEntry( $this->getLogAction(), $params['title'], $params['comment'], $logParams );
737 // Allow for easy searching of deletion log items for revision/log items
738 $log->addRelations( $field, $params['ids'], $logid );
742 * Get the log action for this list type
744 public function getLogAction() {
749 * Get log parameter array.
750 * @param $params Associative array of log parameters, same as updateLog()
753 public function getLogParams( $params ) {
756 implode( ',', $params['ids'] ),
757 "ofield={$params['oldBits']}",
758 "nfield={$params['newBits']}"
763 * Initialise the current iteration pointer
765 protected function initCurrent() {
766 $row = $this->res
->current();
768 $this->current
= $this->newItem( $row );
770 $this->current
= false;
775 * Start iteration. This must be called before current() or next().
776 * @return First list item
778 public function reset() {
780 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
782 $this->res
->rewind();
784 $this->initCurrent();
785 return $this->current
;
789 * Get the current list item, or false if we are at the end
791 public function current() {
792 return $this->current
;
796 * Move the iteration pointer to the next list item, and return it.
798 public function next() {
800 $this->initCurrent();
801 return $this->current
;
805 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
808 public function clearFileOps() {
812 * A hook for setVisibility(): do batch updates pre-commit.
816 public function doPreCommitUpdates() {
817 return Status
::newGood();
821 * A hook for setVisibility(): do any necessary updates post-commit.
825 public function doPostCommitUpdates() {
826 return Status
::newGood();
830 * Create an item object from a DB result row
831 * @param $row stdclass
833 abstract public function newItem( $row );
836 * Do the DB query to iterate through the objects.
837 * @param $db Database object to use for the query
839 abstract public function doQuery( $db );
842 * Get the integer value of the flag used for suppression
844 abstract public function getSuppressBit();
848 * Abstract base class for deletable items
850 abstract class RevDel_Item
{
851 /** The parent SpecialPage */
854 /** The parent RevDel_List */
857 /** The DB result row */
861 * @param $list RevDel_List
862 * @param $row DB result row
864 public function __construct( $list, $row ) {
865 $this->special
= $list->special
;
871 * Get the ID, as it would appear in the ids URL parameter
873 public function getId() {
874 $field = $this->list->getIdField();
875 return $this->row
->$field;
879 * Get the date, formatted with $wgLang
881 public function formatDate() {
883 return $wgLang->date( $this->getTimestamp() );
887 * Get the date, formatted with $wgLang
889 public function formatTime() {
891 return $wgLang->time( $this->getTimestamp() );
895 * Get the timestamp in MW 14-char form
897 public function getTimestamp() {
898 $field = $this->list->getTimestampField();
899 return wfTimestamp( TS_MW
, $this->row
->$field );
903 * Returns true if the item is "current" and can't be deleted for that reason
906 public function isCurrent() {
911 * Returns true if the current user can view the item
913 abstract public function canView();
916 * Get the current deletion bitfield value
918 abstract public function getBits();
921 * Get the HTML of the list item. Should be include <li></li> tags.
922 * This is used to show the list in HTML form, by the special page.
924 abstract public function getHTML();
927 * Set the visibility of the item. This should do any necessary DB queries.
929 * The DB update query should have a condition which forces it to only update
930 * if the value in the DB matches the value fetched earlier with the SELECT.
931 * If the update fails because it did not match, the function should return
932 * false. This prevents concurrency problems.
934 * @return boolean success
936 abstract public function setBits( $newBits );
940 * List for revision table items
942 class RevDel_RevisionList
extends RevDel_List
{
944 var $type = 'revision';
945 var $idField = 'rev_id';
946 var $dateField = 'rev_timestamp';
948 public function doQuery( $db ) {
949 $ids = array_map( 'intval', $this->ids
);
950 return $db->select( array('revision','page'), '*',
952 'rev_page' => $this->title
->getArticleID(),
960 public function newItem( $row ) {
961 return new RevDel_RevisionItem( $this, $row );
964 public function getCurrent() {
965 if ( is_null( $this->currentRevId
) ) {
966 $dbw = wfGetDB( DB_MASTER
);
967 $this->currentRevId
= $dbw->selectField(
968 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
970 return $this->currentRevId
;
973 public function getSuppressBit() {
974 return Revision
::DELETED_RESTRICTED
;
977 public function doPreCommitUpdates() {
978 $this->title
->invalidateCache();
979 return Status
::newGood();
982 public function doPostCommitUpdates() {
983 $this->title
->purgeSquid();
984 // Extensions that require referencing previous revisions may need this
985 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$this->title
) );
986 return Status
::newGood();
991 * Item class for a revision table row
993 class RevDel_RevisionItem
extends RevDel_Item
{
996 public function __construct( $list, $row ) {
997 parent
::__construct( $list, $row );
998 $this->revision
= new Revision( $row );
1001 public function canView() {
1002 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
);
1005 public function getBits() {
1006 return $this->revision
->mDeleted
;
1009 public function setBits( $bits ) {
1010 $dbw = wfGetDB( DB_MASTER
);
1011 // Update revision table
1012 $dbw->update( 'revision',
1013 array( 'rev_deleted' => $bits ),
1015 'rev_id' => $this->revision
->getId(),
1016 'rev_page' => $this->revision
->getPage(),
1017 'rev_deleted' => $this->getBits()
1021 if ( !$dbw->affectedRows() ) {
1025 // Update recentchanges table
1026 $dbw->update( 'recentchanges',
1028 'rc_deleted' => $bits,
1032 'rc_this_oldid' => $this->revision
->getId(), // condition
1033 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ), // non-unique index
1040 public function isDeleted() {
1041 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
1044 public function isCurrent() {
1045 return $this->list->getCurrent() == $this->getId();
1049 * Get the HTML link to the revision text.
1050 * Overridden by RevDel_ArchiveItem.
1052 protected function getRevisionLink() {
1054 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1055 if ( $this->isDeleted() && !$this->canView() ) {
1058 return $this->special
->skin
->makeLinkObj( $this->list->title
, $date,
1059 'oldid='.$this->revision
->getId() . '&unhide=1' );
1063 * Get the HTML link to the diff.
1064 * Overridden by RevDel_ArchiveItem
1066 protected function getDiffLink() {
1067 if ( $this->isDeleted() && !$this->canView() ) {
1068 return wfMsgHtml('diff');
1071 $this->special
->skin
->makeKnownLinkObj(
1074 'diff=' . $this->revision
->getId() . '&oldid=prev&unhide=1'
1079 public function getHTML() {
1080 $difflink = $this->getDiffLink();
1081 $revlink = $this->getRevisionLink();
1082 $userlink = $this->special
->skin
->revUserLink( $this->revision
);
1083 $comment = $this->special
->skin
->revComment( $this->revision
);
1084 if ( $this->isDeleted() ) {
1085 $revlink = "<span class=\"history-deleted\">$revlink</span>";
1087 return "<li>($difflink) $revlink $userlink $comment</li>";
1092 * List for archive table items, i.e. revisions deleted via action=delete
1094 class RevDel_ArchiveList
extends RevDel_RevisionList
{
1095 var $type = 'archive';
1096 var $idField = 'ar_timestamp';
1097 var $dateField = 'ar_timestamp';
1099 public function doQuery( $db ) {
1100 $timestamps = array();
1101 foreach ( $this->ids
as $id ) {
1102 $timestamps[] = $db->timestamp( $id );
1104 return $db->select( 'archive', '*',
1106 'ar_namespace' => $this->title
->getNamespace(),
1107 'ar_title' => $this->title
->getDBkey(),
1108 'ar_timestamp' => $timestamps
1114 public function newItem( $row ) {
1115 return new RevDel_ArchiveItem( $this, $row );
1118 public function doPreCommitUpdates() {
1119 return Status
::newGood();
1122 public function doPostCommitUpdates() {
1123 return Status
::newGood();
1128 * Item class for a archive table row
1130 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
1131 public function __construct( $list, $row ) {
1132 RevDel_Item
::__construct( $list, $row );
1133 $this->revision
= Revision
::newFromArchiveRow( $row,
1134 array( 'page' => $this->list->title
->getArticleId() ) );
1137 public function getId() {
1138 # Convert DB timestamp to MW timestamp
1139 return $this->revision
->getTimestamp();
1142 public function setBits( $bits ) {
1143 $dbw = wfGetDB( DB_MASTER
);
1144 $dbw->update( 'archive',
1145 array( 'ar_deleted' => $bits ),
1146 array( 'ar_namespace' => $this->list->title
->getNamespace(),
1147 'ar_title' => $this->list->title
->getDBkey(),
1148 // use timestamp for index
1149 'ar_timestamp' => $this->row
->ar_timestamp
,
1150 'ar_rev_id' => $this->row
->ar_rev_id
,
1151 'ar_deleted' => $this->getBits()
1154 return (bool)$dbw->affectedRows();
1157 protected function getRevisionLink() {
1159 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1160 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1161 if ( $this->isDeleted() && !$this->canView() ) {
1164 return $this->special
->skin
->link( $undelete, $date, array(),
1166 'target' => $this->list->title
->getPrefixedText(),
1167 'timestamp' => $this->revision
->getTimestamp()
1171 protected function getDiffLink() {
1172 if ( $this->isDeleted() && !$this->canView() ) {
1173 return wfMsgHtml( 'diff' );
1175 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1176 return $this->special
->skin
->link( $undelete, wfMsgHtml('diff'), array(),
1178 'target' => $this->list->title
->getPrefixedText(),
1180 'timestamp' => $this->revision
->getTimestamp()
1186 * List for oldimage table items
1188 class RevDel_FileList
extends RevDel_List
{
1189 var $type = 'oldimage';
1190 var $idField = 'oi_archive_name';
1191 var $dateField = 'oi_timestamp';
1192 var $storeBatch, $deleteBatch, $cleanupBatch;
1194 public function doQuery( $db ) {
1195 $archiveName = array();
1196 foreach( $this->ids
as $timestamp ) {
1197 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
1199 return $db->select( 'oldimage', '*',
1201 'oi_name' => $this->title
->getDBkey(),
1202 'oi_archive_name' => $archiveNames
1208 public function newItem( $row ) {
1209 return new RevDel_FileItem( $this, $row );
1212 public function clearFileOps() {
1213 $this->deleteBatch
= array();
1214 $this->storeBatch
= array();
1215 $this->cleanupBatch
= array();
1218 public function doPreCommitUpdates() {
1219 $status = Status
::newGood();
1220 $repo = RepoGroup
::singleton()->getLocalRepo();
1221 if ( $this->storeBatch
) {
1222 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
1224 if ( !$status->isOK() ) {
1227 if ( $this->deleteBatch
) {
1228 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
1230 if ( !$status->isOK() ) {
1231 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1232 // modified (but destined for rollback) causes data loss
1235 if ( $this->cleanupBatch
) {
1236 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
1241 public function doPostCommitUpdates() {
1242 $file = wfLocalFile( $this->title
);
1243 $file->purgeCache();
1244 $file->purgeDescription();
1245 return Status
::newGood();
1248 public function getSuppressBit() {
1249 return File
::DELETED_RESTRICTED
;
1254 * Item class for an oldimage table row
1256 class RevDel_FileItem
extends RevDel_Item
{
1259 public function __construct( $list, $row ) {
1260 parent
::__construct( $list, $row );
1261 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
1264 public function getId() {
1265 $parts = explode( '!', $this->row
->oi_archive_name
);
1269 public function canView() {
1270 return $this->file
->userCan( File
::DELETED_RESTRICTED
);
1273 public function getBits() {
1274 /** FIXME: use accessor */
1275 return $this->file
->deleted
;
1278 public function setBits( $bits ) {
1280 # FIXME: move to LocalFile.php
1281 if ( $this->isDeleted() ) {
1282 if ( $bits & File
::DELETED_FILE
) {
1286 $key = $this->file
->getStorageKey();
1287 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1288 $this->list->storeBatch
[] = array(
1289 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1291 $this->file
->getRel()
1293 $this->list->cleanupBatch
[] = $key;
1295 } elseif ( $bits & File
::DELETED_FILE
) {
1297 $key = $this->file
->getStorageKey();
1298 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1299 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
1302 # Do the database operations
1303 $dbw = wfGetDB( DB_MASTER
);
1304 $dbw->update( 'oldimage',
1305 array( 'oi_deleted' => $bits ),
1307 'oi_name' => $this->row
->oi_name
,
1308 'oi_timestamp' => $this->row
->oi_timestamp
,
1309 'oi_deleted' => $this->getBits()
1313 return (bool)$dbw->affectedRows();
1316 public function isDeleted() {
1317 return $this->file
->isDeleted( File
::DELETED_FILE
);
1321 * Get the link to the file.
1322 * Overridden by RevDel_ArchivedFileItem.
1324 protected function getLink() {
1326 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1327 if ( $this->isDeleted() ) {
1329 if ( !$this->canView() ) {
1332 return $this->special
->skin
->link(
1333 $this->special
->getTitle(),
1336 'target' => $this->list->title
->getPrefixedText(),
1337 'file' => $this->file
->sha1
. '.' . $this->file
->getExtension()
1343 $url = $this->file
->getUrl();
1344 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
1348 * Generate a user tool link cluster if the current user is allowed to view it
1349 * @return string HTML
1351 protected function getUserTools() {
1352 if( $this->file
->userCan( Revision
::DELETED_USER
) ) {
1353 $link = $this->special
->skin
->userLink( $this->file
->user
, $this->file
->user_text
) .
1354 $this->special
->skin
->userToolLinks( $this->file
->user
, $this->file
->user_text
);
1356 $link = wfMsgHtml( 'rev-deleted-user' );
1358 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
1359 return '<span class="history-deleted">' . $link . '</span>';
1365 * Wrap and format the file's comment block, if the current
1366 * user is allowed to view it.
1368 * @return string HTML
1370 protected function getComment() {
1371 if( $this->file
->userCan( File
::DELETED_COMMENT
) ) {
1372 $block = $this->special
->skin
->commentBlock( $this->file
->description
);
1374 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1376 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
1377 return "<span class=\"history-deleted\">$block</span>";
1382 public function getHTML() {
1387 $wgLang->formatNum( $this->file
->getWidth() ),
1388 $wgLang->formatNum( $this->file
->getHeight() )
1391 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file
->getSize() ) ) .
1393 $pageLink = $this->getLink();
1395 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1396 $data . ' ' . $this->getComment(). '</li>';
1401 * List for filearchive table items
1403 class RevDel_ArchivedFileList
extends RevDel_FileList
{
1404 var $type = 'filearchive';
1405 var $idField = 'fa_id';
1406 var $dateField = 'fa_timestamp';
1408 public function doQuery( $db ) {
1409 $ids = array_map( 'intval', $this->ids
);
1410 return $db->select( 'filearchive', '*',
1412 'fa_name' => $this->title
->getDBkey(),
1419 public function newItem( $row ) {
1420 return new RevDel_ArchivedFileItem( $this, $row );
1425 * Item class for a filearchive table row
1427 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
1428 public function __construct( $list, $row ) {
1429 RevDel_Item
::__construct( $list, $row );
1430 $this->file
= ArchivedFile
::newFromRow( $row );
1433 public function getId() {
1434 return $this->row
->fa_id
;
1437 public function setBits( $bits ) {
1438 $dbw = wfGetDB( DB_MASTER
);
1439 $dbw->update( 'filearchive',
1440 array( 'fa_deleted' => $bits ),
1442 'fa_id' => $this->row
->fa_id
,
1443 'fa_deleted' => $this->getBits(),
1447 return (bool)$dbw->affectedRows();
1450 protected function getLink() {
1451 global $wgLang, $wgUser;
1452 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1453 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1454 $key = $this->file
->getKey();
1455 return $this->special
->skin
->link( $undelete, $date, array(),
1457 'target' => $this->list->title
->getPrefixedText(),
1459 'token' => $wgUser->editToken( $key )
1465 * List for logging table items
1467 class RevDel_LogList
extends RevDel_List
{
1468 var $type = 'logging';
1469 var $idField = 'log_id';
1470 var $dateField = 'log_timestamp';
1472 public function doQuery( $db ) {
1473 global $wgMessageCache;
1474 $wgMessageCache->loadAllMessages();
1475 $ids = array_map( 'intval', $this->ids
);
1476 return $db->select( 'logging', '*',
1477 array( 'log_id' => $ids ),
1482 public function newItem( $row ) {
1483 return new RevDel_LogItem( $this, $row );
1486 public function getSuppressBit() {
1487 return Revision
::DELETED_RESTRICTED
;
1490 public function getLogAction() {
1494 public function getLogParams( $params ) {
1496 implode( ',', $params['ids'] ),
1497 "ofield={$params['oldBits']}",
1498 "nfield={$params['newBits']}"
1504 * Item class for a logging table row
1506 class RevDel_LogItem
extends RevDel_Item
{
1507 public function canView() {
1508 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
);
1511 public function getBits() {
1512 return $this->row
->log_deleted
;
1515 public function setBits( $bits ) {
1516 $dbw = wfGetDB( DB_MASTER
);
1517 $dbw->update( 'recentchanges',
1519 'rc_deleted' => $bits,
1523 'rc_logid' => $this->row
->log_id
,
1524 'rc_timestamp' => $this->row
->log_timestamp
1528 $dbw->update( 'logging',
1529 array( 'log_deleted' => $bits ),
1531 'log_id' => $this->row
->log_id
,
1532 'log_deleted' => $this->getBits()
1536 return (bool)$dbw->affectedRows();
1539 public function getHTML() {
1542 $date = htmlspecialchars( $wgLang->timeanddate( $this->row
->log_timestamp
) );
1543 $paramArray = LogPage
::extractParams( $this->row
->log_params
);
1544 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
1546 $logtitle = SpecialPage
::getTitleFor( 'Log' );
1547 $loglink = $this->special
->skin
->link( $logtitle, wfMsgHtml( 'log' ), array(),
1548 array( 'page' => $title->getPrefixedUrl() ) );
1550 if( !$this->canView() ) {
1551 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1553 $action = LogPage
::actionText( $this->row
->log_type
, $this->row
->log_action
, $title,
1554 $this->special
->skin
, $paramArray, true, true );
1555 if( $this->row
->log_deleted
& LogPage
::DELETED_ACTION
)
1556 $action = '<span class="history-deleted">' . $action . '</span>';
1559 $userLink = $this->special
->skin
->userLink(
1560 $this->row
->log_user
, User
::WhoIs( $this->row
->log_user
) );
1561 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_USER
) ) {
1562 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1565 $comment = $wgLang->getDirMark() . $this->special
->skin
->commentBlock( $this->row
->log_comment
);
1566 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
1567 $comment = '<span class="history-deleted">' . $comment . '</span>';
1569 return "<li>($loglink) $date $userLink $action $comment</li>";