917a16657d0923119ee24d83f01383c8661df4b2
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' ) );
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
;
156 # We need a target page!
157 if( is_null($this->targetObj
) ) {
158 $wgOut->addWikiMsg( 'undelete-header' );
161 # Give a link to the logs/hist for this page
162 $this->showConvenienceLinks();
164 # Initialise checkboxes
165 $this->checks
= array(
166 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
167 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
168 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
170 if( $wgUser->isAllowed('suppressrevision') ) {
171 $this->checks
[] = array( 'revdelete-hide-restricted',
172 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
175 # Either submit or create our form
176 if( $this->submitClicked
) {
177 $this->submit( $wgRequest );
181 $qc = $this->getLogQueryCond();
182 # Show relevant lines from the deletion log
183 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
184 LogEventsList
::showLogExtract( $wgOut, 'delete',
185 $this->targetObj
->getPrefixedText(), '', 25, $qc );
186 # Show relevant lines from the suppression log
187 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
188 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
189 LogEventsList
::showLogExtract( $wgOut, 'suppress',
190 $this->targetObj
->getPrefixedText(), '', 25, $qc );
195 * Show some useful links in the subtitle
197 protected function showConvenienceLinks() {
198 global $wgOut, $wgUser, $wgLang;
199 # Give a link to the logs/hist for this page
200 if( $this->targetObj
) {
202 $logtitle = SpecialPage
::getTitleFor( 'Log' );
203 $links[] = $this->skin
->linkKnown(
205 wfMsgHtml( 'viewpagelogs' ),
207 array( 'page' => $this->targetObj
->getPrefixedText() )
209 if ( $this->targetObj
->getNamespace() != NS_SPECIAL
) {
210 # Give a link to the page history
211 $links[] = $this->skin
->linkKnown(
213 wfMsgHtml( 'pagehist' ),
215 array( 'action' => 'history' )
217 # Link to deleted edits
218 if( $wgUser->isAllowed('undelete') ) {
219 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
220 $links[] = $this->skin
->linkKnown(
222 wfMsgHtml( 'deletedhist' ),
224 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
228 # Logs themselves don't have histories or archived revisions
229 $wgOut->setSubtitle( '<p>' . $wgLang->pipeList( $links ) . '</p>' );
234 * Get the condition used for fetching log snippets
236 protected function getLogQueryCond() {
238 // Revision delete logs for these item
239 $conds['log_type'] = array('delete','suppress');
240 $conds['log_action'] = $this->getList()->getLogAction();
241 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
242 $conds['ls_value'] = $this->ids
;
247 * Show a deleted file version requested by the visitor.
248 * TODO Mostly copied from Special:Undelete. Refactor.
250 protected function tryShowFile( $archiveName ) {
251 global $wgOut, $wgRequest, $wgUser, $wgLang;
253 $repo = RepoGroup
::singleton()->getLocalRepo();
254 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
256 // Check if user is allowed to see this file
257 if ( !$oimage->exists() ) {
258 $wgOut->addWikiMsg( 'revdelete-no-file' );
261 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
262 $wgOut->permissionRequired( 'suppressrevision' );
265 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
266 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
267 $this->targetObj
->getText(),
268 $wgLang->date( $oimage->getTimestamp() ),
269 $wgLang->time( $oimage->getTimestamp() ) );
271 Xml
::openElement( 'form', array(
273 'action' => $this->getTitle()->getLocalUrl(
274 'target=' . urlencode( $oimage->getName() ) .
275 '&file=' . urlencode( $archiveName ) .
276 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
279 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
285 # We mustn't allow the output to be Squid cached, otherwise
286 # if an admin previews a deleted image, and it's cached, then
287 # a user without appropriate permissions can toddle off and
288 # nab the image, and Squid will serve it
289 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
290 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
291 $wgRequest->response()->header( 'Pragma: no-cache' );
293 # Stream the file to the client
295 require_once( "$IP/includes/StreamFile.php" );
296 $key = $oimage->getStorageKey();
297 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
298 wfStreamFile( $path );
302 * Get the list object for this request
304 protected function getList() {
305 if ( is_null( $this->list ) ) {
306 $class = $this->typeInfo
['list-class'];
307 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
313 * Show a list of items that we will operate on, and show a form with checkboxes
314 * which will allow the user to choose new visibility settings.
316 protected function showForm() {
317 global $wgOut, $wgUser, $wgLang;
320 if ( $this->typeName
== 'logging' ) {
321 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
323 $wgOut->addWikiMsg( 'revdelete-selected',
324 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
328 $wgOut->addHTML( "<ul>" );
330 $where = $revObjs = array();
334 $list = $this->getList();
335 for ( $list->reset(); $list->current(); $list->next() ) {
336 $item = $list->current();
337 if ( !$item->canView() ) {
338 if( !$this->submitClicked
) {
339 $wgOut->permissionRequired( 'suppressrevision' );
342 $UserAllowed = false;
345 $bitfields |
= $item->getBits();
346 $wgOut->addHTML( $item->getHTML() );
349 if( !$numRevisions ) {
350 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
354 $wgOut->addHTML( "</ul>" );
356 $this->addUsageText();
358 // Normal sysops can always see what they did, but can't always change it
359 if( !$UserAllowed ) return;
362 Xml
::openElement( 'form', array( 'method' => 'post',
363 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
364 'id' => 'mw-revdel-form-revisions' ) ) .
365 Xml
::openElement( 'fieldset' ) .
366 Xml
::element( 'legend', null, wfMsg( 'revdelete-legend' ) ) .
367 $this->buildCheckBoxes( $bitfields ) .
368 '<p>' . Xml
::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ) . '</p>' .
369 '<p>' . Xml
::submitButton( wfMsg( 'revdelete-submit' ),
370 array( 'name' => 'wpSubmit' ) ) . '</p>' .
371 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
372 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
373 Xml
::hidden( 'type', $this->typeName
) .
374 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
375 Xml
::closeElement( 'fieldset' ) .
376 Xml
::closeElement( 'form' ) . "\n"
381 * Show some introductory text
382 * FIXME Wikimedia-specific policy text
384 protected function addUsageText() {
385 global $wgOut, $wgUser;
386 $wgOut->addWikiMsg( 'revdelete-text' );
387 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
388 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
393 * @param $bitfields Interger: aggregate bitfield of all the bitfields
394 * @return String: HTML
396 protected function buildCheckBoxes( $bitfields ) {
398 // FIXME: all items checked for just one rev are checked, even if not set for the others
399 foreach( $this->checks
as $item ) {
400 list( $message, $name, $field ) = $item;
401 $line = Xml
::tags( 'div', null, Xml
::checkLabel( wfMsg($message), $name, $name,
402 $bitfields & $field ) );
403 if( $field == Revision
::DELETED_RESTRICTED
) $line = "<b>$line</b>";
410 * UI entry point for form submission.
411 * @param $request WebRequest
413 protected function submit( $request ) {
414 global $wgUser, $wgOut;
415 # Check edit token on submission
416 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
417 $wgOut->addWikiMsg( 'sessionfailure' );
420 $bitfield = $this->extractBitfield( $request );
421 $comment = $request->getText( 'wpReason' );
422 # Can the user set this field?
423 if( $bitfield & Revision
::DELETED_RESTRICTED
&& !$wgUser->isAllowed('suppressrevision') ) {
424 $wgOut->permissionRequired( 'suppressrevision' );
427 # If the save went through, go to success message...
428 $status = $this->save( $bitfield, $comment, $this->targetObj
);
429 if ( $status->isGood() ) {
432 # ...otherwise, bounce back to form...
434 $this->failure( $status );
440 * Report that the submit operation succeeded
442 protected function success() {
444 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
445 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo
['success'] );
446 $this->list->reloadFromMaster();
451 * Report that the submit operation failed
453 protected function failure( $status ) {
455 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
456 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
461 * Put together a rev_deleted bitfield from the submitted checkboxes
462 * @param $request WebRequest
465 protected function extractBitfield( $request ) {
467 foreach( $this->checks
as $item ) {
468 list( /* message */ , $name, $field ) = $item;
469 if( $request->getCheck( $name ) ) {
477 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
479 protected function save( $bitfield, $reason, $title ) {
480 // Don't allow simply locking the interface for no reason
481 if( $bitfield == Revision
::DELETED_RESTRICTED
) {
482 return Status
::newFatal( 'revdelete-only-restricted' );
484 return $this->getList()->setVisibility( array(
485 'value' => $bitfield,
486 'comment' => $reason ) );
491 * Temporary b/c interface, collection of static functions.
492 * @ingroup SpecialPage
494 class RevisionDeleter
{
496 * Checks for a change in the bitfield for a certain option and updates the
497 * provided array accordingly.
499 * @param $desc String: description to add to the array if the option was
500 * enabled / disabled.
501 * @param $field Integer: the bitmask describing the single option.
502 * @param $diff Integer: the xor of the old and new bitfields.
503 * @param $new Integer: the new bitfield
504 * @param $arr Array: the array to update.
506 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
507 if( $diff & $field ) {
508 $arr[ ( $new & $field ) ?
0 : 1 ][] = $desc;
513 * Gets an array describing the changes made to the visibilit of the revision.
514 * If the resulting array is $arr, then $arr[0] will contain an array of strings
515 * describing the items that were hidden, $arr[2] will contain an array of strings
516 * describing the items that were unhidden, and $arr[3] will contain an array with
517 * a single string, which can be one of "applied restrictions to sysops",
518 * "removed restrictions from sysops", or null.
520 * @param $n Integer: the new bitfield.
521 * @param $o Integer: the old bitfield.
522 * @return An array as described above.
524 protected static function getChanges( $n, $o ) {
526 $ret = array( 0 => array(), 1 => array(), 2 => array() );
527 // Build bitfield changes in language
528 self
::checkItem( wfMsgForContent( 'revdelete-content' ),
529 Revision
::DELETED_TEXT
, $diff, $n, $ret );
530 self
::checkItem( wfMsgForContent( 'revdelete-summary' ),
531 Revision
::DELETED_COMMENT
, $diff, $n, $ret );
532 self
::checkItem( wfMsgForContent( 'revdelete-uname' ),
533 Revision
::DELETED_USER
, $diff, $n, $ret );
534 // Restriction application to sysops
535 if( $diff & Revision
::DELETED_RESTRICTED
) {
536 if( $n & Revision
::DELETED_RESTRICTED
)
537 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
539 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
545 * Gets a log message to describe the given revision visibility change. This
546 * message will be of the form "[hid {content, edit summary, username}];
547 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
549 * @param $count Integer: The number of effected revisions.
550 * @param $nbitfield Integer: The new bitfield for the revision.
551 * @param $obitfield Integer: The old bitfield for the revision.
552 * @param $isForLog Boolean
554 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
557 $changes = self
::getChanges( $nbitfield, $obitfield );
558 if( count( $changes[0] ) ) {
559 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
561 if( count( $changes[1] ) ) {
563 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
565 if( count( $changes[2] ) ) {
566 $s .= $s ?
' (' . $changes[2][0] . ')' : $changes[2][0];
568 $msg = $isForLog ?
'logdelete-log-message' : 'revdelete-log-message';
569 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
573 // Get DB field name for URL param...
574 // Future code for other things may also track
575 // other types of revision-specific changes.
576 public static function getRelationType( $typeName ) {
577 if ( isset( SpecialRevisionDelete
::$deprecatedTypeMap[$typeName] ) ) {
578 $typeName = SpecialRevisionDelete
::$deprecatedTypeMap[$typeName];
580 if ( isset( SpecialRevisionDelete
::$allowedTypes[$typeName] ) ) {
581 $class = SpecialRevisionDelete
::$allowedTypes[$typeName]['list-class'];
582 $list = new $class( null, null, null );
583 return $list->getIdField();
591 * Abstract base class for a list of deletable items
593 abstract class RevDel_List
{
594 var $special, $title, $ids, $res, $current;
595 var $type = null; // override this
596 var $idField = null; // override this
597 var $dateField = false; // override this
600 * @param $special The parent SpecialPage
601 * @param $title The target title
602 * @param $ids Array of IDs
604 public function __construct( $special, $title, $ids ) {
605 $this->special
= $special;
606 $this->title
= $title;
611 * Get the internal type name of this list. Equal to the table name.
613 public function getType() {
618 * Get the DB field name associated with the ID list/
620 public function getIdField() {
621 return $this->idField
;
625 * Get the DB field name storing timestamps
627 public function getTimestampField() {
628 return $this->dateField
;
632 * Set the visibility for the revisions in this list. Logging and
633 * transactions are done here.
635 * @param $params Associative array of parameters. Members are:
636 * value: The integer value to set the visibility to
637 * comment: The log comment.
640 public function setVisibility( $params ) {
641 $newBits = $params['value'];
642 $comment = $params['comment'];
645 $dbw = wfGetDB( DB_MASTER
);
646 $this->doQuery( $dbw );
648 $status = Status
::newGood();
649 $missing = array_flip( $this->ids
);
650 $this->clearFileOps();
651 $idsForLog = array();
653 for ( $this->reset(); $this->current(); $this->next() ) {
654 $item = $this->current();
655 unset( $missing[ $item->getId() ] );
657 // Make error messages less vague
658 $oldBits = $item->getBits();
659 if ( $oldBits == $newBits ) {
660 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
661 $status->failCount++
;
663 } elseif ( $oldBits == 0 && $newBits != 0 ) {
665 } elseif ( $oldBits != 0 && $newBits == 0 ) {
671 if ( $item->isHideCurrentOp( $newBits ) ) {
672 // Cannot hide current version text
673 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
674 $status->failCount++
;
677 if ( !$item->canView() ) {
678 // Cannot access this revision
679 $msg = $opType == 'show' ?
'revdelete-show-no-access' : 'revdelete-modify-no-access';
680 $status->error( $msg, $item->formatDate(), $item->formatTime() );
681 $status->failCount++
;
685 // Update the revision
686 $ok = $item->setBits( $newBits );
689 $idsForLog[] = $item->getId();
690 $status->successCount++
;
692 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
693 $status->failCount++
;
697 // Handle missing revisions
698 foreach ( $missing as $id => $unused ) {
699 $status->error( 'revdelete-modify-missing', $id );
700 $status->failCount++
;
703 if ( $status->successCount
== 0 ) {
709 // Save success count
710 $successCount = $status->successCount
;
712 // Move files, if there are any
713 $status->merge( $this->doPreCommitUpdates() );
714 if ( !$status->isOK() ) {
715 // Fatal error, such as no configured archive directory
721 $this->updateLog( array(
722 'title' => $this->title
,
723 'count' => $successCount,
724 'newBits' => $newBits,
725 'oldBits' => $oldBits,
726 'comment' => $comment,
732 $status->merge( $this->doPostCommitUpdates() );
737 * Reload the list data from the master DB. This can be done after setVisibility()
738 * to allow $item->getHTML() to show the new data.
740 function reloadFromMaster() {
741 $dbw = wfGetDB( DB_MASTER
);
742 $this->res
= $this->doQuery( $dbw );
746 * Record a log entry on the action
747 * @param $params Associative array of parameters:
748 * newBits: The new value of the *_deleted bitfield
749 * oldBits: The old value of the *_deleted bitfield.
750 * title: The target title
752 * comment: The log comment
754 protected function updateLog( $params ) {
755 // Get the URL param's corresponding DB field
756 $field = RevisionDeleter
::getRelationType( $this->getType() );
758 throw new MWException( "Bad log URL param type!" );
760 // Put things hidden from sysops in the oversight log
761 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
762 $logType = 'suppress';
766 // Add params for effected page and ids
767 $logParams = $this->getLogParams( $params );
768 // Actually add the deletion log entry
769 $log = new LogPage( $logType );
770 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
771 $params['comment'], $logParams );
772 // Allow for easy searching of deletion log items for revision/log items
773 $log->addRelations( $field, $params['ids'], $logid );
777 * Get the log action for this list type
779 public function getLogAction() {
784 * Get log parameter array.
785 * @param $params Associative array of log parameters, same as updateLog()
788 public function getLogParams( $params ) {
791 implode( ',', $params['ids'] ),
792 "ofield={$params['oldBits']}",
793 "nfield={$params['newBits']}"
798 * Initialise the current iteration pointer
800 protected function initCurrent() {
801 $row = $this->res
->current();
803 $this->current
= $this->newItem( $row );
805 $this->current
= false;
810 * Start iteration. This must be called before current() or next().
811 * @return First list item
813 public function reset() {
815 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
817 $this->res
->rewind();
819 $this->initCurrent();
820 return $this->current
;
824 * Get the current list item, or false if we are at the end
826 public function current() {
827 return $this->current
;
831 * Move the iteration pointer to the next list item, and return it.
833 public function next() {
835 $this->initCurrent();
836 return $this->current
;
840 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
843 public function clearFileOps() {
847 * A hook for setVisibility(): do batch updates pre-commit.
851 public function doPreCommitUpdates() {
852 return Status
::newGood();
856 * A hook for setVisibility(): do any necessary updates post-commit.
860 public function doPostCommitUpdates() {
861 return Status
::newGood();
865 * Create an item object from a DB result row
866 * @param $row stdclass
868 abstract public function newItem( $row );
871 * Do the DB query to iterate through the objects.
872 * @param $db Database object to use for the query
874 abstract public function doQuery( $db );
877 * Get the integer value of the flag used for suppression
879 abstract public function getSuppressBit();
883 * Abstract base class for deletable items
885 abstract class RevDel_Item
{
886 /** The parent SpecialPage */
889 /** The parent RevDel_List */
892 /** The DB result row */
896 * @param $list RevDel_List
897 * @param $row DB result row
899 public function __construct( $list, $row ) {
900 $this->special
= $list->special
;
906 * Get the ID, as it would appear in the ids URL parameter
908 public function getId() {
909 $field = $this->list->getIdField();
910 return $this->row
->$field;
914 * Get the date, formatted with $wgLang
916 public function formatDate() {
918 return $wgLang->date( $this->getTimestamp() );
922 * Get the time, formatted with $wgLang
924 public function formatTime() {
926 return $wgLang->time( $this->getTimestamp() );
930 * Get the timestamp in MW 14-char form
932 public function getTimestamp() {
933 $field = $this->list->getTimestampField();
934 return wfTimestamp( TS_MW
, $this->row
->$field );
938 * Returns true if the item is "current", and the operation to set the given
939 * bits can't be executed for that reason
942 public function isHideCurrentOp( $newBits ) {
947 * Returns true if the current user can view the item
949 abstract public function canView();
952 * Get the current deletion bitfield value
954 abstract public function getBits();
957 * Get the HTML of the list item. Should be include <li></li> tags.
958 * This is used to show the list in HTML form, by the special page.
960 abstract public function getHTML();
963 * Set the visibility of the item. This should do any necessary DB queries.
965 * The DB update query should have a condition which forces it to only update
966 * if the value in the DB matches the value fetched earlier with the SELECT.
967 * If the update fails because it did not match, the function should return
968 * false. This prevents concurrency problems.
970 * @return boolean success
972 abstract public function setBits( $newBits );
976 * List for revision table items
978 class RevDel_RevisionList
extends RevDel_List
{
980 var $type = 'revision';
981 var $idField = 'rev_id';
982 var $dateField = 'rev_timestamp';
984 public function doQuery( $db ) {
985 $ids = array_map( 'intval', $this->ids
);
986 return $db->select( array('revision','page'), '*',
988 'rev_page' => $this->title
->getArticleID(),
996 public function newItem( $row ) {
997 return new RevDel_RevisionItem( $this, $row );
1000 public function getCurrent() {
1001 if ( is_null( $this->currentRevId
) ) {
1002 $dbw = wfGetDB( DB_MASTER
);
1003 $this->currentRevId
= $dbw->selectField(
1004 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
1006 return $this->currentRevId
;
1009 public function getSuppressBit() {
1010 return Revision
::DELETED_RESTRICTED
;
1013 public function doPreCommitUpdates() {
1014 $this->title
->invalidateCache();
1015 return Status
::newGood();
1018 public function doPostCommitUpdates() {
1019 $this->title
->purgeSquid();
1020 // Extensions that require referencing previous revisions may need this
1021 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$this->title
) );
1022 return Status
::newGood();
1027 * Item class for a revision table row
1029 class RevDel_RevisionItem
extends RevDel_Item
{
1032 public function __construct( $list, $row ) {
1033 parent
::__construct( $list, $row );
1034 $this->revision
= new Revision( $row );
1037 public function canView() {
1038 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
);
1041 public function getBits() {
1042 return $this->revision
->mDeleted
;
1045 public function setBits( $bits ) {
1046 $dbw = wfGetDB( DB_MASTER
);
1047 // Update revision table
1048 $dbw->update( 'revision',
1049 array( 'rev_deleted' => $bits ),
1051 'rev_id' => $this->revision
->getId(),
1052 'rev_page' => $this->revision
->getPage(),
1053 'rev_deleted' => $this->getBits()
1057 if ( !$dbw->affectedRows() ) {
1061 // Update recentchanges table
1062 $dbw->update( 'recentchanges',
1064 'rc_deleted' => $bits,
1068 'rc_this_oldid' => $this->revision
->getId(), // condition
1069 // non-unique timestamp index
1070 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ),
1077 public function isDeleted() {
1078 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
1081 public function isHideCurrentOp( $newBits ) {
1082 return ( $newBits & Revision
::DELETED_TEXT
)
1083 && $this->list->getCurrent() == $this->getId();
1087 * Get the HTML link to the revision text.
1088 * Overridden by RevDel_ArchiveItem.
1090 protected function getRevisionLink() {
1092 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1093 if ( $this->isDeleted() && !$this->canView() ) {
1096 return $this->special
->skin
->link(
1101 'oldid' => $this->revision
->getId(),
1108 * Get the HTML link to the diff.
1109 * Overridden by RevDel_ArchiveItem
1111 protected function getDiffLink() {
1112 if ( $this->isDeleted() && !$this->canView() ) {
1113 return wfMsgHtml('diff');
1116 $this->special
->skin
->link(
1121 'diff' => $this->revision
->getId(),
1133 public function getHTML() {
1134 $difflink = $this->getDiffLink();
1135 $revlink = $this->getRevisionLink();
1136 $userlink = $this->special
->skin
->revUserLink( $this->revision
);
1137 $comment = $this->special
->skin
->revComment( $this->revision
);
1138 if ( $this->isDeleted() ) {
1139 $revlink = "<span class=\"history-deleted\">$revlink</span>";
1141 return "<li>($difflink) $revlink $userlink $comment</li>";
1146 * List for archive table items, i.e. revisions deleted via action=delete
1148 class RevDel_ArchiveList
extends RevDel_RevisionList
{
1149 var $type = 'archive';
1150 var $idField = 'ar_timestamp';
1151 var $dateField = 'ar_timestamp';
1153 public function doQuery( $db ) {
1154 $timestamps = array();
1155 foreach ( $this->ids
as $id ) {
1156 $timestamps[] = $db->timestamp( $id );
1158 return $db->select( 'archive', '*',
1160 'ar_namespace' => $this->title
->getNamespace(),
1161 'ar_title' => $this->title
->getDBkey(),
1162 'ar_timestamp' => $timestamps
1168 public function newItem( $row ) {
1169 return new RevDel_ArchiveItem( $this, $row );
1172 public function doPreCommitUpdates() {
1173 return Status
::newGood();
1176 public function doPostCommitUpdates() {
1177 return Status
::newGood();
1182 * Item class for a archive table row
1184 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
1185 public function __construct( $list, $row ) {
1186 RevDel_Item
::__construct( $list, $row );
1187 $this->revision
= Revision
::newFromArchiveRow( $row,
1188 array( 'page' => $this->list->title
->getArticleId() ) );
1191 public function getId() {
1192 # Convert DB timestamp to MW timestamp
1193 return $this->revision
->getTimestamp();
1196 public function setBits( $bits ) {
1197 $dbw = wfGetDB( DB_MASTER
);
1198 $dbw->update( 'archive',
1199 array( 'ar_deleted' => $bits ),
1200 array( 'ar_namespace' => $this->list->title
->getNamespace(),
1201 'ar_title' => $this->list->title
->getDBkey(),
1202 // use timestamp for index
1203 'ar_timestamp' => $this->row
->ar_timestamp
,
1204 'ar_rev_id' => $this->row
->ar_rev_id
,
1205 'ar_deleted' => $this->getBits()
1208 return (bool)$dbw->affectedRows();
1211 protected function getRevisionLink() {
1213 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1214 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1215 if ( $this->isDeleted() && !$this->canView() ) {
1218 return $this->special
->skin
->link( $undelete, $date, array(),
1220 'target' => $this->list->title
->getPrefixedText(),
1221 'timestamp' => $this->revision
->getTimestamp()
1225 protected function getDiffLink() {
1226 if ( $this->isDeleted() && !$this->canView() ) {
1227 return wfMsgHtml( 'diff' );
1229 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1230 return $this->special
->skin
->link( $undelete, wfMsgHtml('diff'), array(),
1232 'target' => $this->list->title
->getPrefixedText(),
1234 'timestamp' => $this->revision
->getTimestamp()
1240 * List for oldimage table items
1242 class RevDel_FileList
extends RevDel_List
{
1243 var $type = 'oldimage';
1244 var $idField = 'oi_archive_name';
1245 var $dateField = 'oi_timestamp';
1246 var $storeBatch, $deleteBatch, $cleanupBatch;
1248 public function doQuery( $db ) {
1249 $archiveName = array();
1250 foreach( $this->ids
as $timestamp ) {
1251 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
1253 return $db->select( 'oldimage', '*',
1255 'oi_name' => $this->title
->getDBkey(),
1256 'oi_archive_name' => $archiveNames
1262 public function newItem( $row ) {
1263 return new RevDel_FileItem( $this, $row );
1266 public function clearFileOps() {
1267 $this->deleteBatch
= array();
1268 $this->storeBatch
= array();
1269 $this->cleanupBatch
= array();
1272 public function doPreCommitUpdates() {
1273 $status = Status
::newGood();
1274 $repo = RepoGroup
::singleton()->getLocalRepo();
1275 if ( $this->storeBatch
) {
1276 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
1278 if ( !$status->isOK() ) {
1281 if ( $this->deleteBatch
) {
1282 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
1284 if ( !$status->isOK() ) {
1285 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1286 // modified (but destined for rollback) causes data loss
1289 if ( $this->cleanupBatch
) {
1290 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
1295 public function doPostCommitUpdates() {
1296 $file = wfLocalFile( $this->title
);
1297 $file->purgeCache();
1298 $file->purgeDescription();
1299 return Status
::newGood();
1302 public function getSuppressBit() {
1303 return File
::DELETED_RESTRICTED
;
1308 * Item class for an oldimage table row
1310 class RevDel_FileItem
extends RevDel_Item
{
1313 public function __construct( $list, $row ) {
1314 parent
::__construct( $list, $row );
1315 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
1318 public function getId() {
1319 $parts = explode( '!', $this->row
->oi_archive_name
);
1323 public function canView() {
1324 return $this->file
->userCan( File
::DELETED_RESTRICTED
);
1327 public function getBits() {
1328 /** FIXME: use accessor */
1329 return $this->file
->deleted
;
1332 public function setBits( $bits ) {
1334 # FIXME: move to LocalFile.php
1335 if ( $this->isDeleted() ) {
1336 if ( $bits & File
::DELETED_FILE
) {
1340 $key = $this->file
->getStorageKey();
1341 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1342 $this->list->storeBatch
[] = array(
1343 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1345 $this->file
->getRel()
1347 $this->list->cleanupBatch
[] = $key;
1349 } elseif ( $bits & File
::DELETED_FILE
) {
1351 $key = $this->file
->getStorageKey();
1352 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1353 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
1356 # Do the database operations
1357 $dbw = wfGetDB( DB_MASTER
);
1358 $dbw->update( 'oldimage',
1359 array( 'oi_deleted' => $bits ),
1361 'oi_name' => $this->row
->oi_name
,
1362 'oi_timestamp' => $this->row
->oi_timestamp
,
1363 'oi_deleted' => $this->getBits()
1367 return (bool)$dbw->affectedRows();
1370 public function isDeleted() {
1371 return $this->file
->isDeleted( File
::DELETED_FILE
);
1375 * Get the link to the file.
1376 * Overridden by RevDel_ArchivedFileItem.
1378 protected function getLink() {
1380 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1381 if ( $this->isDeleted() ) {
1383 if ( !$this->canView() ) {
1386 return $this->special
->skin
->link(
1387 $this->special
->getTitle(),
1390 'target' => $this->list->title
->getPrefixedText(),
1391 'file' => $this->file
->sha1
. '.' . $this->file
->getExtension()
1397 $url = $this->file
->getUrl();
1398 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
1402 * Generate a user tool link cluster if the current user is allowed to view it
1403 * @return string HTML
1405 protected function getUserTools() {
1406 if( $this->file
->userCan( Revision
::DELETED_USER
) ) {
1407 $link = $this->special
->skin
->userLink( $this->file
->user
, $this->file
->user_text
) .
1408 $this->special
->skin
->userToolLinks( $this->file
->user
, $this->file
->user_text
);
1410 $link = wfMsgHtml( 'rev-deleted-user' );
1412 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
1413 return '<span class="history-deleted">' . $link . '</span>';
1419 * Wrap and format the file's comment block, if the current
1420 * user is allowed to view it.
1422 * @return string HTML
1424 protected function getComment() {
1425 if( $this->file
->userCan( File
::DELETED_COMMENT
) ) {
1426 $block = $this->special
->skin
->commentBlock( $this->file
->description
);
1428 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1430 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
1431 return "<span class=\"history-deleted\">$block</span>";
1436 public function getHTML() {
1441 $wgLang->formatNum( $this->file
->getWidth() ),
1442 $wgLang->formatNum( $this->file
->getHeight() )
1445 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file
->getSize() ) ) .
1447 $pageLink = $this->getLink();
1449 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1450 $data . ' ' . $this->getComment(). '</li>';
1455 * List for filearchive table items
1457 class RevDel_ArchivedFileList
extends RevDel_FileList
{
1458 var $type = 'filearchive';
1459 var $idField = 'fa_id';
1460 var $dateField = 'fa_timestamp';
1462 public function doQuery( $db ) {
1463 $ids = array_map( 'intval', $this->ids
);
1464 return $db->select( 'filearchive', '*',
1466 'fa_name' => $this->title
->getDBkey(),
1473 public function newItem( $row ) {
1474 return new RevDel_ArchivedFileItem( $this, $row );
1479 * Item class for a filearchive table row
1481 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
1482 public function __construct( $list, $row ) {
1483 RevDel_Item
::__construct( $list, $row );
1484 $this->file
= ArchivedFile
::newFromRow( $row );
1487 public function getId() {
1488 return $this->row
->fa_id
;
1491 public function setBits( $bits ) {
1492 $dbw = wfGetDB( DB_MASTER
);
1493 $dbw->update( 'filearchive',
1494 array( 'fa_deleted' => $bits ),
1496 'fa_id' => $this->row
->fa_id
,
1497 'fa_deleted' => $this->getBits(),
1501 return (bool)$dbw->affectedRows();
1504 protected function getLink() {
1505 global $wgLang, $wgUser;
1506 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1507 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1508 $key = $this->file
->getKey();
1509 return $this->special
->skin
->link( $undelete, $date, array(),
1511 'target' => $this->list->title
->getPrefixedText(),
1513 'token' => $wgUser->editToken( $key )
1519 * List for logging table items
1521 class RevDel_LogList
extends RevDel_List
{
1522 var $type = 'logging';
1523 var $idField = 'log_id';
1524 var $dateField = 'log_timestamp';
1526 public function doQuery( $db ) {
1527 global $wgMessageCache;
1528 $wgMessageCache->loadAllMessages();
1529 $ids = array_map( 'intval', $this->ids
);
1530 return $db->select( 'logging', '*',
1531 array( 'log_id' => $ids ),
1536 public function newItem( $row ) {
1537 return new RevDel_LogItem( $this, $row );
1540 public function getSuppressBit() {
1541 return Revision
::DELETED_RESTRICTED
;
1544 public function getLogAction() {
1548 public function getLogParams( $params ) {
1550 implode( ',', $params['ids'] ),
1551 "ofield={$params['oldBits']}",
1552 "nfield={$params['newBits']}"
1558 * Item class for a logging table row
1560 class RevDel_LogItem
extends RevDel_Item
{
1561 public function canView() {
1562 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
);
1565 public function getBits() {
1566 return $this->row
->log_deleted
;
1569 public function setBits( $bits ) {
1570 $dbw = wfGetDB( DB_MASTER
);
1571 $dbw->update( 'recentchanges',
1573 'rc_deleted' => $bits,
1577 'rc_logid' => $this->row
->log_id
,
1578 'rc_timestamp' => $this->row
->log_timestamp
1582 $dbw->update( 'logging',
1583 array( 'log_deleted' => $bits ),
1585 'log_id' => $this->row
->log_id
,
1586 'log_deleted' => $this->getBits()
1590 return (bool)$dbw->affectedRows();
1593 public function getHTML() {
1596 $date = htmlspecialchars( $wgLang->timeanddate( $this->row
->log_timestamp
) );
1597 $paramArray = LogPage
::extractParams( $this->row
->log_params
);
1598 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
1600 $logtitle = SpecialPage
::getTitleFor( 'Log' );
1601 $loglink = $this->special
->skin
->link(
1605 array( 'page' => $title->getPrefixedText() )
1608 if( !$this->canView() ) {
1609 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1611 $action = LogPage
::actionText( $this->row
->log_type
, $this->row
->log_action
, $title,
1612 $this->special
->skin
, $paramArray, true, true );
1613 if( $this->row
->log_deleted
& LogPage
::DELETED_ACTION
)
1614 $action = '<span class="history-deleted">' . $action . '</span>';
1617 $userLink = $this->special
->skin
->userLink(
1618 $this->row
->log_user
, User
::WhoIs( $this->row
->log_user
) );
1619 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_USER
) ) {
1620 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1623 $comment = $wgLang->getDirMark() . $this->special
->skin
->commentBlock( $this->row
->log_comment
);
1624 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
1625 $comment = '<span class="history-deleted">' . $comment . '</span>';
1627 return "<li>($loglink) $date $userLink $action $comment</li>";