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 # Use CSV, since the cgi handling will break on arrays.
111 $this->ids
= explode( ',', $wgRequest->getVal('ids') );
112 $this->ids
= array_unique( array_filter( $this->ids
) );
113 $this->targetObj
= Title
::newFromText( $wgRequest->getText( 'target' ) );
115 # For reviewing deleted files...
116 $this->archiveName
= $wgRequest->getVal( 'file' );
117 $this->token
= $wgRequest->getVal( 'token' );
118 if ( $this->archiveName
&& $this->targetObj
) {
119 $this->tryShowFile( $this->archiveName
);
123 $this->typeName
= $wgRequest->getVal( 'type' );
124 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
125 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
129 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
130 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
133 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
135 # If we have revisions, get the title from the first one
136 # since they should all be from the same page. This allows
137 # for more flexibility with page moves...
138 if( $this->typeName
== 'revision' ) {
139 $rev = Revision
::newFromId( $this->ids
[0] );
140 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
142 # We need a target page!
143 if( is_null($this->targetObj
) ) {
144 $wgOut->addWikiMsg( 'undelete-header' );
147 # Give a link to the logs/hist for this page
148 $this->showConvenienceLinks();
150 # Initialise checkboxes
151 $this->checks
= array(
152 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
153 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
154 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
156 if( $wgUser->isAllowed('suppressrevision') ) {
157 $this->checks
[] = array( 'revdelete-hide-restricted',
158 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
161 # Either submit or create our form
162 if( $this->submitClicked
) {
163 $this->submit( $wgRequest );
167 $qc = $this->getLogQueryCond();
168 # Show relevant lines from the deletion log
169 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
170 LogEventsList
::showLogExtract( $wgOut, 'delete',
171 $this->targetObj
->getPrefixedText(), '', 25, $qc );
172 # Show relevant lines from the suppression log
173 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
174 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
175 LogEventsList
::showLogExtract( $wgOut, 'suppress',
176 $this->targetObj
->getPrefixedText(), '', 25, $qc );
181 * Show some useful links in the subtitle
183 protected function showConvenienceLinks() {
184 global $wgOut, $wgUser;
185 # Give a link to the logs/hist for this page
186 if( $this->targetObj
) {
188 $logtitle = SpecialPage
::getTitleFor( 'Log' );
189 $links[] = $this->skin
->linkKnown(
191 wfMsgHtml( 'viewpagelogs' ),
193 array( 'page' => $this->targetObj
->getPrefixedUrl() )
195 # Give a link to the page history
196 $links[] = $this->skin
->linkKnown(
198 wfMsgHtml( 'pagehist' ),
200 array( 'action' => 'history' )
202 # Link to deleted edits
203 if( $wgUser->isAllowed('undelete') ) {
204 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
205 $links[] = $this->skin
->linkKnown(
207 wfMsgHtml( 'deletedhist' ),
209 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
212 # Logs themselves don't have histories or archived revisions
213 $wgOut->setSubtitle( '<p>'.implode($links,' / ').'</p>' );
218 * Get the condition used for fetching log snippets
220 protected function getLogQueryCond() {
222 // Revision delete logs for these item
223 $conds['log_type'] = array('delete','suppress');
224 $conds['log_action'] = $this->getList()->getLogAction();
225 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
226 $conds['ls_value'] = $this->ids
;
231 * Show a deleted file version requested by the visitor.
232 * TODO Mostly copied from Special:Undelete. Refactor.
234 protected function tryShowFile( $archiveName ) {
235 global $wgOut, $wgRequest, $wgUser, $wgLang;
237 $repo = RepoGroup
::singleton()->getLocalRepo();
238 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
240 // Check if user is allowed to see this file
241 if ( !$oimage->exists() ) {
242 $wgOut->addWikiMsg( 'revdelete-no-file' );
245 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
246 $wgOut->permissionRequired( 'suppressrevision' );
249 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
250 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
251 $this->targetObj
->getText(),
252 $wgLang->date( $oimage->getTimestamp() ),
253 $wgLang->time( $oimage->getTimestamp() ) );
255 Xml
::openElement( 'form', array(
257 'action' => $this->getTitle()->getLocalUrl(
258 'target=' . urlencode( $oimage->getName() ) .
259 '&file=' . urlencode( $archiveName ) .
260 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
263 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
269 # We mustn't allow the output to be Squid cached, otherwise
270 # if an admin previews a deleted image, and it's cached, then
271 # a user without appropriate permissions can toddle off and
272 # nab the image, and Squid will serve it
273 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
274 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
275 $wgRequest->response()->header( 'Pragma: no-cache' );
277 # Stream the file to the client
279 require_once( "$IP/includes/StreamFile.php" );
280 $key = $oimage->getStorageKey();
281 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
282 wfStreamFile( $path );
286 * Get the list object for this request
288 protected function getList() {
289 if ( is_null( $this->list ) ) {
290 $class = $this->typeInfo
['list-class'];
291 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
297 * Show a list of items that we will operate on, and show a form with checkboxes
298 * which will allow the user to choose new visibility settings.
300 protected function showForm() {
301 global $wgOut, $wgUser, $wgLang;
304 if ( $this->typeName
== 'logging' ) {
305 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
307 $wgOut->addWikiMsg( 'revdelete-selected',
308 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
312 $wgOut->addHTML( "<ul>" );
314 $where = $revObjs = array();
318 $list = $this->getList();
319 for ( $list->reset(); $list->current(); $list->next() ) {
320 $item = $list->current();
321 if ( !$item->canView() ) {
322 if( !$this->submitClicked
) {
323 $wgOut->permissionRequired( 'suppressrevision' );
326 $UserAllowed = false;
329 $bitfields |
= $item->getBits();
330 $wgOut->addHTML( $item->getHTML() );
333 if( !$numRevisions ) {
334 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
338 $wgOut->addHTML( "</ul>" );
340 $this->addUsageText();
342 // Normal sysops can always see what they did, but can't always change it
343 if( !$UserAllowed ) return;
346 Xml
::openElement( 'form', array( 'method' => 'post',
347 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
348 'id' => 'mw-revdel-form-revisions' ) ) .
349 Xml
::openElement( 'fieldset' ) .
350 Xml
::element( 'legend', null, wfMsg( 'revdelete-legend' ) ) .
351 $this->buildCheckBoxes( $bitfields ) .
352 '<p>' . Xml
::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ) . '</p>' .
353 '<p>' . Xml
::submitButton( wfMsg( 'revdelete-submit' ),
354 array( 'name' => 'wpSubmit' ) ) . '</p>' .
355 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
356 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
357 Xml
::hidden( 'type', $this->typeName
) .
358 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
359 Xml
::closeElement( 'fieldset' ) .
360 Xml
::closeElement( 'form' ) . "\n"
365 * Show some introductory text
366 * FIXME Wikimedia-specific policy text
368 protected function addUsageText() {
369 global $wgOut, $wgUser;
370 $wgOut->addWikiMsg( 'revdelete-text' );
371 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
372 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
377 * @param $bitfields Interger: aggregate bitfield of all the bitfields
378 * @return String: HTML
380 protected function buildCheckBoxes( $bitfields ) {
382 // FIXME: all items checked for just one rev are checked, even if not set for the others
383 foreach( $this->checks
as $item ) {
384 list( $message, $name, $field ) = $item;
385 $line = Xml
::tags( 'div', null, Xml
::checkLabel( wfMsg($message), $name, $name,
386 $bitfields & $field ) );
387 if( $field == Revision
::DELETED_RESTRICTED
) $line = "<b>$line</b>";
394 * UI entry point for form submission.
395 * @param $request WebRequest
397 protected function submit( $request ) {
398 global $wgUser, $wgOut;
399 # Check edit token on submission
400 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
401 $wgOut->addWikiMsg( 'sessionfailure' );
404 $bitfield = $this->extractBitfield( $request );
405 $comment = $request->getText( 'wpReason' );
406 # Can the user set this field?
407 if( $bitfield & Revision
::DELETED_RESTRICTED
&& !$wgUser->isAllowed('suppressrevision') ) {
408 $wgOut->permissionRequired( 'suppressrevision' );
411 # If the save went through, go to success message...
412 $status = $this->save( $bitfield, $comment, $this->targetObj
);
413 if ( $status->isGood() ) {
416 # ...otherwise, bounce back to form...
418 $this->failure( $status );
424 * Report that the submit operation succeeded
426 protected function success() {
428 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
429 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo
['success'] );
430 $this->list->reloadFromMaster();
435 * Report that the submit operation failed
437 protected function failure( $status ) {
439 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
440 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
445 * Put together a rev_deleted bitfield from the submitted checkboxes
446 * @param $request WebRequest
449 protected function extractBitfield( $request ) {
451 foreach( $this->checks
as $item ) {
452 list( /* message */ , $name, $field ) = $item;
453 if( $request->getCheck( $name ) ) {
461 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
463 protected function save( $bitfield, $reason, $title ) {
464 // Don't allow simply locking the interface for no reason
465 if( $bitfield == Revision
::DELETED_RESTRICTED
) {
466 return Status
::newFatal( 'revdelete-only-restricted' );
468 return $this->getList()->setVisibility( array(
469 'value' => $bitfield,
470 'comment' => $reason ) );
475 * Temporary b/c interface, collection of static functions.
476 * @ingroup SpecialPage
478 class RevisionDeleter
{
480 * Checks for a change in the bitfield for a certain option and updates the
481 * provided array accordingly.
483 * @param $desc String: description to add to the array if the option was
484 * enabled / disabled.
485 * @param $field Integer: the bitmask describing the single option.
486 * @param $diff Integer: the xor of the old and new bitfields.
487 * @param $new Integer: the new bitfield
488 * @param $arr Array: the array to update.
490 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
491 if( $diff & $field ) {
492 $arr[ ( $new & $field ) ?
0 : 1 ][] = $desc;
497 * Gets an array describing the changes made to the visibilit of the revision.
498 * If the resulting array is $arr, then $arr[0] will contain an array of strings
499 * describing the items that were hidden, $arr[2] will contain an array of strings
500 * describing the items that were unhidden, and $arr[3] will contain an array with
501 * a single string, which can be one of "applied restrictions to sysops",
502 * "removed restrictions from sysops", or null.
504 * @param $n Integer: the new bitfield.
505 * @param $o Integer: the old bitfield.
506 * @return An array as described above.
508 protected static function getChanges( $n, $o ) {
510 $ret = array( 0 => array(), 1 => array(), 2 => array() );
511 // Build bitfield changes in language
512 self
::checkItem( wfMsgForContent( 'revdelete-content' ),
513 Revision
::DELETED_TEXT
, $diff, $n, $ret );
514 self
::checkItem( wfMsgForContent( 'revdelete-summary' ),
515 Revision
::DELETED_COMMENT
, $diff, $n, $ret );
516 self
::checkItem( wfMsgForContent( 'revdelete-uname' ),
517 Revision
::DELETED_USER
, $diff, $n, $ret );
518 // Restriction application to sysops
519 if( $diff & Revision
::DELETED_RESTRICTED
) {
520 if( $n & Revision
::DELETED_RESTRICTED
)
521 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
523 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
529 * Gets a log message to describe the given revision visibility change. This
530 * message will be of the form "[hid {content, edit summary, username}];
531 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
533 * @param $count Integer: The number of effected revisions.
534 * @param $nbitfield Integer: The new bitfield for the revision.
535 * @param $obitfield Integer: The old bitfield for the revision.
536 * @param $isForLog Boolean
538 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
541 $changes = self
::getChanges( $nbitfield, $obitfield );
542 if( count( $changes[0] ) ) {
543 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
545 if( count( $changes[1] ) ) {
547 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
549 if( count( $changes[2] ) ) {
550 $s .= $s ?
' (' . $changes[2][0] . ')' : $changes[2][0];
552 $msg = $isForLog ?
'logdelete-log-message' : 'revdelete-log-message';
553 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
557 // Get DB field name for URL param...
558 // Future code for other things may also track
559 // other types of revision-specific changes.
560 public static function getRelationType( $typeName ) {
561 if ( isset( SpecialRevisionDelete
::$deprecatedTypeMap[$typeName] ) ) {
562 $typeName = SpecialRevisionDelete
::$deprecatedTypeMap[$typeName];
564 if ( isset( SpecialRevisionDelete
::$allowedTypes[$typeName] ) ) {
565 $class = SpecialRevisionDelete
::$allowedTypes[$typeName]['list-class'];
566 $list = new $class( null, null, null );
567 return $list->getIdField();
575 * Abstract base class for a list of deletable items
577 abstract class RevDel_List
{
578 var $special, $title, $ids, $res, $current;
579 var $type = null; // override this
580 var $idField = null; // override this
581 var $dateField = false; // override this
584 * @param $special The parent SpecialPage
585 * @param $title The target title
586 * @param $ids Array of IDs
588 public function __construct( $special, $title, $ids ) {
589 $this->special
= $special;
590 $this->title
= $title;
595 * Get the internal type name of this list. Equal to the table name.
597 public function getType() {
602 * Get the DB field name associated with the ID list/
604 public function getIdField() {
605 return $this->idField
;
609 * Get the DB field name storing timestamps
611 public function getTimestampField() {
612 return $this->dateField
;
616 * Set the visibility for the revisions in this list. Logging and
617 * transactions are done here.
619 * @param $params Associative array of parameters. Members are:
620 * value: The integer value to set the visibility to
621 * comment: The log comment.
624 public function setVisibility( $params ) {
625 $newBits = $params['value'];
626 $comment = $params['comment'];
629 $dbw = wfGetDB( DB_MASTER
);
630 $this->doQuery( $dbw );
632 $status = Status
::newGood();
633 $missing = array_flip( $this->ids
);
634 $this->clearFileOps();
635 $idsForLog = array();
637 for ( $this->reset(); $this->current(); $this->next() ) {
638 $item = $this->current();
639 unset( $missing[ $item->getId() ] );
641 // Make error messages less vague
642 $oldBits = $item->getBits();
643 if ( $oldBits == $newBits ) {
644 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
645 $status->failCount++
;
647 } elseif ( $oldBits == 0 && $newBits != 0 ) {
649 } elseif ( $oldBits != 0 && $newBits == 0 ) {
655 if ( $item->isHideCurrentOp( $newBits ) ) {
656 // Cannot hide current version text
657 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
658 $status->failCount++
;
661 if ( !$item->canView() ) {
662 // Cannot access this revision
663 $msg = $opType == 'show' ?
'revdelete-show-no-access' : 'revdelete-modify-no-access';
664 $status->error( $msg, $item->formatDate(), $item->formatTime() );
665 $status->failCount++
;
669 // Update the revision
670 $ok = $item->setBits( $newBits );
673 $idsForLog[] = $item->getId();
674 $status->successCount++
;
676 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
677 $status->failCount++
;
681 // Handle missing revisions
682 foreach ( $missing as $id => $unused ) {
683 $status->error( 'revdelete-modify-missing', $id );
684 $status->failCount++
;
687 if ( $status->successCount
== 0 ) {
693 // Save success count
694 $successCount = $status->successCount
;
696 // Move files, if there are any
697 $status->merge( $this->doPreCommitUpdates() );
698 if ( !$status->isOK() ) {
699 // Fatal error, such as no configured archive directory
705 $this->updateLog( array(
706 'title' => $this->title
,
707 'count' => $successCount,
708 'newBits' => $newBits,
709 'oldBits' => $oldBits,
710 'comment' => $comment,
716 $status->merge( $this->doPostCommitUpdates() );
721 * Reload the list data from the master DB. This can be done after setVisibility()
722 * to allow $item->getHTML() to show the new data.
724 function reloadFromMaster() {
725 $dbw = wfGetDB( DB_MASTER
);
726 $this->res
= $this->doQuery( $dbw );
730 * Record a log entry on the action
731 * @param $params Associative array of parameters:
732 * newBits: The new value of the *_deleted bitfield
733 * oldBits: The old value of the *_deleted bitfield.
734 * title: The target title
736 * comment: The log comment
738 protected function updateLog( $params ) {
739 // Get the URL param's corresponding DB field
740 $field = RevisionDeleter
::getRelationType( $this->getType() );
742 throw new MWException( "Bad log URL param type!" );
744 // Put things hidden from sysops in the oversight log
745 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
746 $logType = 'suppress';
750 // Add params for effected page and ids
751 $logParams = $this->getLogParams( $params );
752 // Actually add the deletion log entry
753 $log = new LogPage( $logType );
754 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
755 $params['comment'], $logParams );
756 // Allow for easy searching of deletion log items for revision/log items
757 $log->addRelations( $field, $params['ids'], $logid );
761 * Get the log action for this list type
763 public function getLogAction() {
768 * Get log parameter array.
769 * @param $params Associative array of log parameters, same as updateLog()
772 public function getLogParams( $params ) {
775 implode( ',', $params['ids'] ),
776 "ofield={$params['oldBits']}",
777 "nfield={$params['newBits']}"
782 * Initialise the current iteration pointer
784 protected function initCurrent() {
785 $row = $this->res
->current();
787 $this->current
= $this->newItem( $row );
789 $this->current
= false;
794 * Start iteration. This must be called before current() or next().
795 * @return First list item
797 public function reset() {
799 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
801 $this->res
->rewind();
803 $this->initCurrent();
804 return $this->current
;
808 * Get the current list item, or false if we are at the end
810 public function current() {
811 return $this->current
;
815 * Move the iteration pointer to the next list item, and return it.
817 public function next() {
819 $this->initCurrent();
820 return $this->current
;
824 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
827 public function clearFileOps() {
831 * A hook for setVisibility(): do batch updates pre-commit.
835 public function doPreCommitUpdates() {
836 return Status
::newGood();
840 * A hook for setVisibility(): do any necessary updates post-commit.
844 public function doPostCommitUpdates() {
845 return Status
::newGood();
849 * Create an item object from a DB result row
850 * @param $row stdclass
852 abstract public function newItem( $row );
855 * Do the DB query to iterate through the objects.
856 * @param $db Database object to use for the query
858 abstract public function doQuery( $db );
861 * Get the integer value of the flag used for suppression
863 abstract public function getSuppressBit();
867 * Abstract base class for deletable items
869 abstract class RevDel_Item
{
870 /** The parent SpecialPage */
873 /** The parent RevDel_List */
876 /** The DB result row */
880 * @param $list RevDel_List
881 * @param $row DB result row
883 public function __construct( $list, $row ) {
884 $this->special
= $list->special
;
890 * Get the ID, as it would appear in the ids URL parameter
892 public function getId() {
893 $field = $this->list->getIdField();
894 return $this->row
->$field;
898 * Get the date, formatted with $wgLang
900 public function formatDate() {
902 return $wgLang->date( $this->getTimestamp() );
906 * Get the time, formatted with $wgLang
908 public function formatTime() {
910 return $wgLang->time( $this->getTimestamp() );
914 * Get the timestamp in MW 14-char form
916 public function getTimestamp() {
917 $field = $this->list->getTimestampField();
918 return wfTimestamp( TS_MW
, $this->row
->$field );
922 * Returns true if the item is "current", and the operation to set the given
923 * bits can't be executed for that reason
926 public function isHideCurrentOp( $newBits ) {
931 * Returns true if the current user can view the item
933 abstract public function canView();
936 * Get the current deletion bitfield value
938 abstract public function getBits();
941 * Get the HTML of the list item. Should be include <li></li> tags.
942 * This is used to show the list in HTML form, by the special page.
944 abstract public function getHTML();
947 * Set the visibility of the item. This should do any necessary DB queries.
949 * The DB update query should have a condition which forces it to only update
950 * if the value in the DB matches the value fetched earlier with the SELECT.
951 * If the update fails because it did not match, the function should return
952 * false. This prevents concurrency problems.
954 * @return boolean success
956 abstract public function setBits( $newBits );
960 * List for revision table items
962 class RevDel_RevisionList
extends RevDel_List
{
964 var $type = 'revision';
965 var $idField = 'rev_id';
966 var $dateField = 'rev_timestamp';
968 public function doQuery( $db ) {
969 $ids = array_map( 'intval', $this->ids
);
970 return $db->select( array('revision','page'), '*',
972 'rev_page' => $this->title
->getArticleID(),
980 public function newItem( $row ) {
981 return new RevDel_RevisionItem( $this, $row );
984 public function getCurrent() {
985 if ( is_null( $this->currentRevId
) ) {
986 $dbw = wfGetDB( DB_MASTER
);
987 $this->currentRevId
= $dbw->selectField(
988 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
990 return $this->currentRevId
;
993 public function getSuppressBit() {
994 return Revision
::DELETED_RESTRICTED
;
997 public function doPreCommitUpdates() {
998 $this->title
->invalidateCache();
999 return Status
::newGood();
1002 public function doPostCommitUpdates() {
1003 $this->title
->purgeSquid();
1004 // Extensions that require referencing previous revisions may need this
1005 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$this->title
) );
1006 return Status
::newGood();
1011 * Item class for a revision table row
1013 class RevDel_RevisionItem
extends RevDel_Item
{
1016 public function __construct( $list, $row ) {
1017 parent
::__construct( $list, $row );
1018 $this->revision
= new Revision( $row );
1021 public function canView() {
1022 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
);
1025 public function getBits() {
1026 return $this->revision
->mDeleted
;
1029 public function setBits( $bits ) {
1030 $dbw = wfGetDB( DB_MASTER
);
1031 // Update revision table
1032 $dbw->update( 'revision',
1033 array( 'rev_deleted' => $bits ),
1035 'rev_id' => $this->revision
->getId(),
1036 'rev_page' => $this->revision
->getPage(),
1037 'rev_deleted' => $this->getBits()
1041 if ( !$dbw->affectedRows() ) {
1045 // Update recentchanges table
1046 $dbw->update( 'recentchanges',
1048 'rc_deleted' => $bits,
1052 'rc_this_oldid' => $this->revision
->getId(), // condition
1053 // non-unique timestamp index
1054 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ),
1061 public function isDeleted() {
1062 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
1065 public function isHideCurrentOp( $newBits ) {
1066 return ( $newBits & Revision
::DELETED_TEXT
)
1067 && $this->list->getCurrent() == $this->getId();
1071 * Get the HTML link to the revision text.
1072 * Overridden by RevDel_ArchiveItem.
1074 protected function getRevisionLink() {
1076 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1077 if ( $this->isDeleted() && !$this->canView() ) {
1080 return $this->special
->skin
->link(
1085 'oldid' => $this->revision
->getId(),
1092 * Get the HTML link to the diff.
1093 * Overridden by RevDel_ArchiveItem
1095 protected function getDiffLink() {
1096 if ( $this->isDeleted() && !$this->canView() ) {
1097 return wfMsgHtml('diff');
1100 $this->special
->skin
->link(
1105 'diff' => $this->revision
->getId(),
1117 public function getHTML() {
1118 $difflink = $this->getDiffLink();
1119 $revlink = $this->getRevisionLink();
1120 $userlink = $this->special
->skin
->revUserLink( $this->revision
);
1121 $comment = $this->special
->skin
->revComment( $this->revision
);
1122 if ( $this->isDeleted() ) {
1123 $revlink = "<span class=\"history-deleted\">$revlink</span>";
1125 return "<li>($difflink) $revlink $userlink $comment</li>";
1130 * List for archive table items, i.e. revisions deleted via action=delete
1132 class RevDel_ArchiveList
extends RevDel_RevisionList
{
1133 var $type = 'archive';
1134 var $idField = 'ar_timestamp';
1135 var $dateField = 'ar_timestamp';
1137 public function doQuery( $db ) {
1138 $timestamps = array();
1139 foreach ( $this->ids
as $id ) {
1140 $timestamps[] = $db->timestamp( $id );
1142 return $db->select( 'archive', '*',
1144 'ar_namespace' => $this->title
->getNamespace(),
1145 'ar_title' => $this->title
->getDBkey(),
1146 'ar_timestamp' => $timestamps
1152 public function newItem( $row ) {
1153 return new RevDel_ArchiveItem( $this, $row );
1156 public function doPreCommitUpdates() {
1157 return Status
::newGood();
1160 public function doPostCommitUpdates() {
1161 return Status
::newGood();
1166 * Item class for a archive table row
1168 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
1169 public function __construct( $list, $row ) {
1170 RevDel_Item
::__construct( $list, $row );
1171 $this->revision
= Revision
::newFromArchiveRow( $row,
1172 array( 'page' => $this->list->title
->getArticleId() ) );
1175 public function getId() {
1176 # Convert DB timestamp to MW timestamp
1177 return $this->revision
->getTimestamp();
1180 public function setBits( $bits ) {
1181 $dbw = wfGetDB( DB_MASTER
);
1182 $dbw->update( 'archive',
1183 array( 'ar_deleted' => $bits ),
1184 array( 'ar_namespace' => $this->list->title
->getNamespace(),
1185 'ar_title' => $this->list->title
->getDBkey(),
1186 // use timestamp for index
1187 'ar_timestamp' => $this->row
->ar_timestamp
,
1188 'ar_rev_id' => $this->row
->ar_rev_id
,
1189 'ar_deleted' => $this->getBits()
1192 return (bool)$dbw->affectedRows();
1195 protected function getRevisionLink() {
1197 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1198 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1199 if ( $this->isDeleted() && !$this->canView() ) {
1202 return $this->special
->skin
->link( $undelete, $date, array(),
1204 'target' => $this->list->title
->getPrefixedText(),
1205 'timestamp' => $this->revision
->getTimestamp()
1209 protected function getDiffLink() {
1210 if ( $this->isDeleted() && !$this->canView() ) {
1211 return wfMsgHtml( 'diff' );
1213 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1214 return $this->special
->skin
->link( $undelete, wfMsgHtml('diff'), array(),
1216 'target' => $this->list->title
->getPrefixedText(),
1218 'timestamp' => $this->revision
->getTimestamp()
1224 * List for oldimage table items
1226 class RevDel_FileList
extends RevDel_List
{
1227 var $type = 'oldimage';
1228 var $idField = 'oi_archive_name';
1229 var $dateField = 'oi_timestamp';
1230 var $storeBatch, $deleteBatch, $cleanupBatch;
1232 public function doQuery( $db ) {
1233 $archiveName = array();
1234 foreach( $this->ids
as $timestamp ) {
1235 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
1237 return $db->select( 'oldimage', '*',
1239 'oi_name' => $this->title
->getDBkey(),
1240 'oi_archive_name' => $archiveNames
1246 public function newItem( $row ) {
1247 return new RevDel_FileItem( $this, $row );
1250 public function clearFileOps() {
1251 $this->deleteBatch
= array();
1252 $this->storeBatch
= array();
1253 $this->cleanupBatch
= array();
1256 public function doPreCommitUpdates() {
1257 $status = Status
::newGood();
1258 $repo = RepoGroup
::singleton()->getLocalRepo();
1259 if ( $this->storeBatch
) {
1260 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
1262 if ( !$status->isOK() ) {
1265 if ( $this->deleteBatch
) {
1266 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
1268 if ( !$status->isOK() ) {
1269 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1270 // modified (but destined for rollback) causes data loss
1273 if ( $this->cleanupBatch
) {
1274 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
1279 public function doPostCommitUpdates() {
1280 $file = wfLocalFile( $this->title
);
1281 $file->purgeCache();
1282 $file->purgeDescription();
1283 return Status
::newGood();
1286 public function getSuppressBit() {
1287 return File
::DELETED_RESTRICTED
;
1292 * Item class for an oldimage table row
1294 class RevDel_FileItem
extends RevDel_Item
{
1297 public function __construct( $list, $row ) {
1298 parent
::__construct( $list, $row );
1299 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
1302 public function getId() {
1303 $parts = explode( '!', $this->row
->oi_archive_name
);
1307 public function canView() {
1308 return $this->file
->userCan( File
::DELETED_RESTRICTED
);
1311 public function getBits() {
1312 /** FIXME: use accessor */
1313 return $this->file
->deleted
;
1316 public function setBits( $bits ) {
1318 # FIXME: move to LocalFile.php
1319 if ( $this->isDeleted() ) {
1320 if ( $bits & File
::DELETED_FILE
) {
1324 $key = $this->file
->getStorageKey();
1325 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1326 $this->list->storeBatch
[] = array(
1327 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1329 $this->file
->getRel()
1331 $this->list->cleanupBatch
[] = $key;
1333 } elseif ( $bits & File
::DELETED_FILE
) {
1335 $key = $this->file
->getStorageKey();
1336 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1337 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
1340 # Do the database operations
1341 $dbw = wfGetDB( DB_MASTER
);
1342 $dbw->update( 'oldimage',
1343 array( 'oi_deleted' => $bits ),
1345 'oi_name' => $this->row
->oi_name
,
1346 'oi_timestamp' => $this->row
->oi_timestamp
,
1347 'oi_deleted' => $this->getBits()
1351 return (bool)$dbw->affectedRows();
1354 public function isDeleted() {
1355 return $this->file
->isDeleted( File
::DELETED_FILE
);
1359 * Get the link to the file.
1360 * Overridden by RevDel_ArchivedFileItem.
1362 protected function getLink() {
1364 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1365 if ( $this->isDeleted() ) {
1367 if ( !$this->canView() ) {
1370 return $this->special
->skin
->link(
1371 $this->special
->getTitle(),
1374 'target' => $this->list->title
->getPrefixedText(),
1375 'file' => $this->file
->sha1
. '.' . $this->file
->getExtension()
1381 $url = $this->file
->getUrl();
1382 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
1386 * Generate a user tool link cluster if the current user is allowed to view it
1387 * @return string HTML
1389 protected function getUserTools() {
1390 if( $this->file
->userCan( Revision
::DELETED_USER
) ) {
1391 $link = $this->special
->skin
->userLink( $this->file
->user
, $this->file
->user_text
) .
1392 $this->special
->skin
->userToolLinks( $this->file
->user
, $this->file
->user_text
);
1394 $link = wfMsgHtml( 'rev-deleted-user' );
1396 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
1397 return '<span class="history-deleted">' . $link . '</span>';
1403 * Wrap and format the file's comment block, if the current
1404 * user is allowed to view it.
1406 * @return string HTML
1408 protected function getComment() {
1409 if( $this->file
->userCan( File
::DELETED_COMMENT
) ) {
1410 $block = $this->special
->skin
->commentBlock( $this->file
->description
);
1412 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1414 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
1415 return "<span class=\"history-deleted\">$block</span>";
1420 public function getHTML() {
1425 $wgLang->formatNum( $this->file
->getWidth() ),
1426 $wgLang->formatNum( $this->file
->getHeight() )
1429 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file
->getSize() ) ) .
1431 $pageLink = $this->getLink();
1433 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1434 $data . ' ' . $this->getComment(). '</li>';
1439 * List for filearchive table items
1441 class RevDel_ArchivedFileList
extends RevDel_FileList
{
1442 var $type = 'filearchive';
1443 var $idField = 'fa_id';
1444 var $dateField = 'fa_timestamp';
1446 public function doQuery( $db ) {
1447 $ids = array_map( 'intval', $this->ids
);
1448 return $db->select( 'filearchive', '*',
1450 'fa_name' => $this->title
->getDBkey(),
1457 public function newItem( $row ) {
1458 return new RevDel_ArchivedFileItem( $this, $row );
1463 * Item class for a filearchive table row
1465 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
1466 public function __construct( $list, $row ) {
1467 RevDel_Item
::__construct( $list, $row );
1468 $this->file
= ArchivedFile
::newFromRow( $row );
1471 public function getId() {
1472 return $this->row
->fa_id
;
1475 public function setBits( $bits ) {
1476 $dbw = wfGetDB( DB_MASTER
);
1477 $dbw->update( 'filearchive',
1478 array( 'fa_deleted' => $bits ),
1480 'fa_id' => $this->row
->fa_id
,
1481 'fa_deleted' => $this->getBits(),
1485 return (bool)$dbw->affectedRows();
1488 protected function getLink() {
1489 global $wgLang, $wgUser;
1490 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1491 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1492 $key = $this->file
->getKey();
1493 return $this->special
->skin
->link( $undelete, $date, array(),
1495 'target' => $this->list->title
->getPrefixedText(),
1497 'token' => $wgUser->editToken( $key )
1503 * List for logging table items
1505 class RevDel_LogList
extends RevDel_List
{
1506 var $type = 'logging';
1507 var $idField = 'log_id';
1508 var $dateField = 'log_timestamp';
1510 public function doQuery( $db ) {
1511 global $wgMessageCache;
1512 $wgMessageCache->loadAllMessages();
1513 $ids = array_map( 'intval', $this->ids
);
1514 return $db->select( 'logging', '*',
1515 array( 'log_id' => $ids ),
1520 public function newItem( $row ) {
1521 return new RevDel_LogItem( $this, $row );
1524 public function getSuppressBit() {
1525 return Revision
::DELETED_RESTRICTED
;
1528 public function getLogAction() {
1532 public function getLogParams( $params ) {
1534 implode( ',', $params['ids'] ),
1535 "ofield={$params['oldBits']}",
1536 "nfield={$params['newBits']}"
1542 * Item class for a logging table row
1544 class RevDel_LogItem
extends RevDel_Item
{
1545 public function canView() {
1546 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
);
1549 public function getBits() {
1550 return $this->row
->log_deleted
;
1553 public function setBits( $bits ) {
1554 $dbw = wfGetDB( DB_MASTER
);
1555 $dbw->update( 'recentchanges',
1557 'rc_deleted' => $bits,
1561 'rc_logid' => $this->row
->log_id
,
1562 'rc_timestamp' => $this->row
->log_timestamp
1566 $dbw->update( 'logging',
1567 array( 'log_deleted' => $bits ),
1569 'log_id' => $this->row
->log_id
,
1570 'log_deleted' => $this->getBits()
1574 return (bool)$dbw->affectedRows();
1577 public function getHTML() {
1580 $date = htmlspecialchars( $wgLang->timeanddate( $this->row
->log_timestamp
) );
1581 $paramArray = LogPage
::extractParams( $this->row
->log_params
);
1582 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
1584 $logtitle = SpecialPage
::getTitleFor( 'Log' );
1585 $loglink = $this->special
->skin
->link( $logtitle, wfMsgHtml( 'log' ), array(),
1586 array( 'page' => $title->getPrefixedUrl() ) );
1588 if( !$this->canView() ) {
1589 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1591 $action = LogPage
::actionText( $this->row
->log_type
, $this->row
->log_action
, $title,
1592 $this->special
->skin
, $paramArray, true, true );
1593 if( $this->row
->log_deleted
& LogPage
::DELETED_ACTION
)
1594 $action = '<span class="history-deleted">' . $action . '</span>';
1597 $userLink = $this->special
->skin
->userLink(
1598 $this->row
->log_user
, User
::WhoIs( $this->row
->log_user
) );
1599 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_USER
) ) {
1600 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1603 $comment = $wgLang->getDirMark() . $this->special
->skin
->commentBlock( $this->row
->log_comment
);
1604 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
1605 $comment = '<span class="history-deleted">' . $comment . '</span>';
1607 return "<li>($loglink) $date $userLink $action $comment</li>";