344589315384757b4411362a79b9e701d9536545
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, $wgLang;
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
->getPrefixedText() )
195 if ( $this->targetObj
->getNamespace() != NS_SPECIAL
) {
196 # Give a link to the page history
197 $links[] = $this->skin
->linkKnown(
199 wfMsgHtml( 'pagehist' ),
201 array( 'action' => 'history' )
203 # Link to deleted edits
204 if( $wgUser->isAllowed('undelete') ) {
205 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
206 $links[] = $this->skin
->linkKnown(
208 wfMsgHtml( 'deletedhist' ),
210 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
214 # Logs themselves don't have histories or archived revisions
215 $wgOut->setSubtitle( '<p>' . $wgLang->pipeList( $links ) . '</p>' );
220 * Get the condition used for fetching log snippets
222 protected function getLogQueryCond() {
224 // Revision delete logs for these item
225 $conds['log_type'] = array('delete','suppress');
226 $conds['log_action'] = $this->getList()->getLogAction();
227 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
228 $conds['ls_value'] = $this->ids
;
233 * Show a deleted file version requested by the visitor.
234 * TODO Mostly copied from Special:Undelete. Refactor.
236 protected function tryShowFile( $archiveName ) {
237 global $wgOut, $wgRequest, $wgUser, $wgLang;
239 $repo = RepoGroup
::singleton()->getLocalRepo();
240 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
242 // Check if user is allowed to see this file
243 if ( !$oimage->exists() ) {
244 $wgOut->addWikiMsg( 'revdelete-no-file' );
247 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
248 $wgOut->permissionRequired( 'suppressrevision' );
251 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
252 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
253 $this->targetObj
->getText(),
254 $wgLang->date( $oimage->getTimestamp() ),
255 $wgLang->time( $oimage->getTimestamp() ) );
257 Xml
::openElement( 'form', array(
259 'action' => $this->getTitle()->getLocalUrl(
260 'target=' . urlencode( $oimage->getName() ) .
261 '&file=' . urlencode( $archiveName ) .
262 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
265 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
271 # We mustn't allow the output to be Squid cached, otherwise
272 # if an admin previews a deleted image, and it's cached, then
273 # a user without appropriate permissions can toddle off and
274 # nab the image, and Squid will serve it
275 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
276 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
277 $wgRequest->response()->header( 'Pragma: no-cache' );
279 # Stream the file to the client
281 require_once( "$IP/includes/StreamFile.php" );
282 $key = $oimage->getStorageKey();
283 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
284 wfStreamFile( $path );
288 * Get the list object for this request
290 protected function getList() {
291 if ( is_null( $this->list ) ) {
292 $class = $this->typeInfo
['list-class'];
293 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
299 * Show a list of items that we will operate on, and show a form with checkboxes
300 * which will allow the user to choose new visibility settings.
302 protected function showForm() {
303 global $wgOut, $wgUser, $wgLang;
306 if ( $this->typeName
== 'logging' ) {
307 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
309 $wgOut->addWikiMsg( 'revdelete-selected',
310 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
314 $wgOut->addHTML( "<ul>" );
316 $where = $revObjs = array();
320 $list = $this->getList();
321 for ( $list->reset(); $list->current(); $list->next() ) {
322 $item = $list->current();
323 if ( !$item->canView() ) {
324 if( !$this->submitClicked
) {
325 $wgOut->permissionRequired( 'suppressrevision' );
328 $UserAllowed = false;
331 $bitfields |
= $item->getBits();
332 $wgOut->addHTML( $item->getHTML() );
335 if( !$numRevisions ) {
336 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
340 $wgOut->addHTML( "</ul>" );
342 $this->addUsageText();
344 // Normal sysops can always see what they did, but can't always change it
345 if( !$UserAllowed ) return;
348 Xml
::openElement( 'form', array( 'method' => 'post',
349 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
350 'id' => 'mw-revdel-form-revisions' ) ) .
351 Xml
::openElement( 'fieldset' ) .
352 Xml
::element( 'legend', null, wfMsg( 'revdelete-legend' ) ) .
353 $this->buildCheckBoxes( $bitfields ) .
354 '<p>' . Xml
::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ) . '</p>' .
355 '<p>' . Xml
::submitButton( wfMsg( 'revdelete-submit' ),
356 array( 'name' => 'wpSubmit' ) ) . '</p>' .
357 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
358 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
359 Xml
::hidden( 'type', $this->typeName
) .
360 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
361 Xml
::closeElement( 'fieldset' ) .
362 Xml
::closeElement( 'form' ) . "\n"
367 * Show some introductory text
368 * FIXME Wikimedia-specific policy text
370 protected function addUsageText() {
371 global $wgOut, $wgUser;
372 $wgOut->addWikiMsg( 'revdelete-text' );
373 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
374 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
379 * @param $bitfields Interger: aggregate bitfield of all the bitfields
380 * @return String: HTML
382 protected function buildCheckBoxes( $bitfields ) {
384 // FIXME: all items checked for just one rev are checked, even if not set for the others
385 foreach( $this->checks
as $item ) {
386 list( $message, $name, $field ) = $item;
387 $line = Xml
::tags( 'div', null, Xml
::checkLabel( wfMsg($message), $name, $name,
388 $bitfields & $field ) );
389 if( $field == Revision
::DELETED_RESTRICTED
) $line = "<b>$line</b>";
396 * UI entry point for form submission.
397 * @param $request WebRequest
399 protected function submit( $request ) {
400 global $wgUser, $wgOut;
401 # Check edit token on submission
402 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
403 $wgOut->addWikiMsg( 'sessionfailure' );
406 $bitfield = $this->extractBitfield( $request );
407 $comment = $request->getText( 'wpReason' );
408 # Can the user set this field?
409 if( $bitfield & Revision
::DELETED_RESTRICTED
&& !$wgUser->isAllowed('suppressrevision') ) {
410 $wgOut->permissionRequired( 'suppressrevision' );
413 # If the save went through, go to success message...
414 $status = $this->save( $bitfield, $comment, $this->targetObj
);
415 if ( $status->isGood() ) {
418 # ...otherwise, bounce back to form...
420 $this->failure( $status );
426 * Report that the submit operation succeeded
428 protected function success() {
430 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
431 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo
['success'] );
432 $this->list->reloadFromMaster();
437 * Report that the submit operation failed
439 protected function failure( $status ) {
441 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
442 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
447 * Put together a rev_deleted bitfield from the submitted checkboxes
448 * @param $request WebRequest
451 protected function extractBitfield( $request ) {
453 foreach( $this->checks
as $item ) {
454 list( /* message */ , $name, $field ) = $item;
455 if( $request->getCheck( $name ) ) {
463 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
465 protected function save( $bitfield, $reason, $title ) {
466 // Don't allow simply locking the interface for no reason
467 if( $bitfield == Revision
::DELETED_RESTRICTED
) {
468 return Status
::newFatal( 'revdelete-only-restricted' );
470 return $this->getList()->setVisibility( array(
471 'value' => $bitfield,
472 'comment' => $reason ) );
477 * Temporary b/c interface, collection of static functions.
478 * @ingroup SpecialPage
480 class RevisionDeleter
{
482 * Checks for a change in the bitfield for a certain option and updates the
483 * provided array accordingly.
485 * @param $desc String: description to add to the array if the option was
486 * enabled / disabled.
487 * @param $field Integer: the bitmask describing the single option.
488 * @param $diff Integer: the xor of the old and new bitfields.
489 * @param $new Integer: the new bitfield
490 * @param $arr Array: the array to update.
492 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
493 if( $diff & $field ) {
494 $arr[ ( $new & $field ) ?
0 : 1 ][] = $desc;
499 * Gets an array describing the changes made to the visibilit of the revision.
500 * If the resulting array is $arr, then $arr[0] will contain an array of strings
501 * describing the items that were hidden, $arr[2] will contain an array of strings
502 * describing the items that were unhidden, and $arr[3] will contain an array with
503 * a single string, which can be one of "applied restrictions to sysops",
504 * "removed restrictions from sysops", or null.
506 * @param $n Integer: the new bitfield.
507 * @param $o Integer: the old bitfield.
508 * @return An array as described above.
510 protected static function getChanges( $n, $o ) {
512 $ret = array( 0 => array(), 1 => array(), 2 => array() );
513 // Build bitfield changes in language
514 self
::checkItem( wfMsgForContent( 'revdelete-content' ),
515 Revision
::DELETED_TEXT
, $diff, $n, $ret );
516 self
::checkItem( wfMsgForContent( 'revdelete-summary' ),
517 Revision
::DELETED_COMMENT
, $diff, $n, $ret );
518 self
::checkItem( wfMsgForContent( 'revdelete-uname' ),
519 Revision
::DELETED_USER
, $diff, $n, $ret );
520 // Restriction application to sysops
521 if( $diff & Revision
::DELETED_RESTRICTED
) {
522 if( $n & Revision
::DELETED_RESTRICTED
)
523 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
525 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
531 * Gets a log message to describe the given revision visibility change. This
532 * message will be of the form "[hid {content, edit summary, username}];
533 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
535 * @param $count Integer: The number of effected revisions.
536 * @param $nbitfield Integer: The new bitfield for the revision.
537 * @param $obitfield Integer: The old bitfield for the revision.
538 * @param $isForLog Boolean
540 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
543 $changes = self
::getChanges( $nbitfield, $obitfield );
544 if( count( $changes[0] ) ) {
545 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
547 if( count( $changes[1] ) ) {
549 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
551 if( count( $changes[2] ) ) {
552 $s .= $s ?
' (' . $changes[2][0] . ')' : $changes[2][0];
554 $msg = $isForLog ?
'logdelete-log-message' : 'revdelete-log-message';
555 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
559 // Get DB field name for URL param...
560 // Future code for other things may also track
561 // other types of revision-specific changes.
562 public static function getRelationType( $typeName ) {
563 if ( isset( SpecialRevisionDelete
::$deprecatedTypeMap[$typeName] ) ) {
564 $typeName = SpecialRevisionDelete
::$deprecatedTypeMap[$typeName];
566 if ( isset( SpecialRevisionDelete
::$allowedTypes[$typeName] ) ) {
567 $class = SpecialRevisionDelete
::$allowedTypes[$typeName]['list-class'];
568 $list = new $class( null, null, null );
569 return $list->getIdField();
577 * Abstract base class for a list of deletable items
579 abstract class RevDel_List
{
580 var $special, $title, $ids, $res, $current;
581 var $type = null; // override this
582 var $idField = null; // override this
583 var $dateField = false; // override this
586 * @param $special The parent SpecialPage
587 * @param $title The target title
588 * @param $ids Array of IDs
590 public function __construct( $special, $title, $ids ) {
591 $this->special
= $special;
592 $this->title
= $title;
597 * Get the internal type name of this list. Equal to the table name.
599 public function getType() {
604 * Get the DB field name associated with the ID list/
606 public function getIdField() {
607 return $this->idField
;
611 * Get the DB field name storing timestamps
613 public function getTimestampField() {
614 return $this->dateField
;
618 * Set the visibility for the revisions in this list. Logging and
619 * transactions are done here.
621 * @param $params Associative array of parameters. Members are:
622 * value: The integer value to set the visibility to
623 * comment: The log comment.
626 public function setVisibility( $params ) {
627 $newBits = $params['value'];
628 $comment = $params['comment'];
631 $dbw = wfGetDB( DB_MASTER
);
632 $this->doQuery( $dbw );
634 $status = Status
::newGood();
635 $missing = array_flip( $this->ids
);
636 $this->clearFileOps();
637 $idsForLog = array();
639 for ( $this->reset(); $this->current(); $this->next() ) {
640 $item = $this->current();
641 unset( $missing[ $item->getId() ] );
643 // Make error messages less vague
644 $oldBits = $item->getBits();
645 if ( $oldBits == $newBits ) {
646 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
647 $status->failCount++
;
649 } elseif ( $oldBits == 0 && $newBits != 0 ) {
651 } elseif ( $oldBits != 0 && $newBits == 0 ) {
657 if ( $item->isHideCurrentOp( $newBits ) ) {
658 // Cannot hide current version text
659 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
660 $status->failCount++
;
663 if ( !$item->canView() ) {
664 // Cannot access this revision
665 $msg = $opType == 'show' ?
'revdelete-show-no-access' : 'revdelete-modify-no-access';
666 $status->error( $msg, $item->formatDate(), $item->formatTime() );
667 $status->failCount++
;
671 // Update the revision
672 $ok = $item->setBits( $newBits );
675 $idsForLog[] = $item->getId();
676 $status->successCount++
;
678 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
679 $status->failCount++
;
683 // Handle missing revisions
684 foreach ( $missing as $id => $unused ) {
685 $status->error( 'revdelete-modify-missing', $id );
686 $status->failCount++
;
689 if ( $status->successCount
== 0 ) {
695 // Save success count
696 $successCount = $status->successCount
;
698 // Move files, if there are any
699 $status->merge( $this->doPreCommitUpdates() );
700 if ( !$status->isOK() ) {
701 // Fatal error, such as no configured archive directory
707 $this->updateLog( array(
708 'title' => $this->title
,
709 'count' => $successCount,
710 'newBits' => $newBits,
711 'oldBits' => $oldBits,
712 'comment' => $comment,
718 $status->merge( $this->doPostCommitUpdates() );
723 * Reload the list data from the master DB. This can be done after setVisibility()
724 * to allow $item->getHTML() to show the new data.
726 function reloadFromMaster() {
727 $dbw = wfGetDB( DB_MASTER
);
728 $this->res
= $this->doQuery( $dbw );
732 * Record a log entry on the action
733 * @param $params Associative array of parameters:
734 * newBits: The new value of the *_deleted bitfield
735 * oldBits: The old value of the *_deleted bitfield.
736 * title: The target title
738 * comment: The log comment
740 protected function updateLog( $params ) {
741 // Get the URL param's corresponding DB field
742 $field = RevisionDeleter
::getRelationType( $this->getType() );
744 throw new MWException( "Bad log URL param type!" );
746 // Put things hidden from sysops in the oversight log
747 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
748 $logType = 'suppress';
752 // Add params for effected page and ids
753 $logParams = $this->getLogParams( $params );
754 // Actually add the deletion log entry
755 $log = new LogPage( $logType );
756 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
757 $params['comment'], $logParams );
758 // Allow for easy searching of deletion log items for revision/log items
759 $log->addRelations( $field, $params['ids'], $logid );
763 * Get the log action for this list type
765 public function getLogAction() {
770 * Get log parameter array.
771 * @param $params Associative array of log parameters, same as updateLog()
774 public function getLogParams( $params ) {
777 implode( ',', $params['ids'] ),
778 "ofield={$params['oldBits']}",
779 "nfield={$params['newBits']}"
784 * Initialise the current iteration pointer
786 protected function initCurrent() {
787 $row = $this->res
->current();
789 $this->current
= $this->newItem( $row );
791 $this->current
= false;
796 * Start iteration. This must be called before current() or next().
797 * @return First list item
799 public function reset() {
801 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
803 $this->res
->rewind();
805 $this->initCurrent();
806 return $this->current
;
810 * Get the current list item, or false if we are at the end
812 public function current() {
813 return $this->current
;
817 * Move the iteration pointer to the next list item, and return it.
819 public function next() {
821 $this->initCurrent();
822 return $this->current
;
826 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
829 public function clearFileOps() {
833 * A hook for setVisibility(): do batch updates pre-commit.
837 public function doPreCommitUpdates() {
838 return Status
::newGood();
842 * A hook for setVisibility(): do any necessary updates post-commit.
846 public function doPostCommitUpdates() {
847 return Status
::newGood();
851 * Create an item object from a DB result row
852 * @param $row stdclass
854 abstract public function newItem( $row );
857 * Do the DB query to iterate through the objects.
858 * @param $db Database object to use for the query
860 abstract public function doQuery( $db );
863 * Get the integer value of the flag used for suppression
865 abstract public function getSuppressBit();
869 * Abstract base class for deletable items
871 abstract class RevDel_Item
{
872 /** The parent SpecialPage */
875 /** The parent RevDel_List */
878 /** The DB result row */
882 * @param $list RevDel_List
883 * @param $row DB result row
885 public function __construct( $list, $row ) {
886 $this->special
= $list->special
;
892 * Get the ID, as it would appear in the ids URL parameter
894 public function getId() {
895 $field = $this->list->getIdField();
896 return $this->row
->$field;
900 * Get the date, formatted with $wgLang
902 public function formatDate() {
904 return $wgLang->date( $this->getTimestamp() );
908 * Get the time, formatted with $wgLang
910 public function formatTime() {
912 return $wgLang->time( $this->getTimestamp() );
916 * Get the timestamp in MW 14-char form
918 public function getTimestamp() {
919 $field = $this->list->getTimestampField();
920 return wfTimestamp( TS_MW
, $this->row
->$field );
924 * Returns true if the item is "current", and the operation to set the given
925 * bits can't be executed for that reason
928 public function isHideCurrentOp( $newBits ) {
933 * Returns true if the current user can view the item
935 abstract public function canView();
938 * Get the current deletion bitfield value
940 abstract public function getBits();
943 * Get the HTML of the list item. Should be include <li></li> tags.
944 * This is used to show the list in HTML form, by the special page.
946 abstract public function getHTML();
949 * Set the visibility of the item. This should do any necessary DB queries.
951 * The DB update query should have a condition which forces it to only update
952 * if the value in the DB matches the value fetched earlier with the SELECT.
953 * If the update fails because it did not match, the function should return
954 * false. This prevents concurrency problems.
956 * @return boolean success
958 abstract public function setBits( $newBits );
962 * List for revision table items
964 class RevDel_RevisionList
extends RevDel_List
{
966 var $type = 'revision';
967 var $idField = 'rev_id';
968 var $dateField = 'rev_timestamp';
970 public function doQuery( $db ) {
971 $ids = array_map( 'intval', $this->ids
);
972 return $db->select( array('revision','page'), '*',
974 'rev_page' => $this->title
->getArticleID(),
982 public function newItem( $row ) {
983 return new RevDel_RevisionItem( $this, $row );
986 public function getCurrent() {
987 if ( is_null( $this->currentRevId
) ) {
988 $dbw = wfGetDB( DB_MASTER
);
989 $this->currentRevId
= $dbw->selectField(
990 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
992 return $this->currentRevId
;
995 public function getSuppressBit() {
996 return Revision
::DELETED_RESTRICTED
;
999 public function doPreCommitUpdates() {
1000 $this->title
->invalidateCache();
1001 return Status
::newGood();
1004 public function doPostCommitUpdates() {
1005 $this->title
->purgeSquid();
1006 // Extensions that require referencing previous revisions may need this
1007 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$this->title
) );
1008 return Status
::newGood();
1013 * Item class for a revision table row
1015 class RevDel_RevisionItem
extends RevDel_Item
{
1018 public function __construct( $list, $row ) {
1019 parent
::__construct( $list, $row );
1020 $this->revision
= new Revision( $row );
1023 public function canView() {
1024 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
);
1027 public function getBits() {
1028 return $this->revision
->mDeleted
;
1031 public function setBits( $bits ) {
1032 $dbw = wfGetDB( DB_MASTER
);
1033 // Update revision table
1034 $dbw->update( 'revision',
1035 array( 'rev_deleted' => $bits ),
1037 'rev_id' => $this->revision
->getId(),
1038 'rev_page' => $this->revision
->getPage(),
1039 'rev_deleted' => $this->getBits()
1043 if ( !$dbw->affectedRows() ) {
1047 // Update recentchanges table
1048 $dbw->update( 'recentchanges',
1050 'rc_deleted' => $bits,
1054 'rc_this_oldid' => $this->revision
->getId(), // condition
1055 // non-unique timestamp index
1056 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ),
1063 public function isDeleted() {
1064 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
1067 public function isHideCurrentOp( $newBits ) {
1068 return ( $newBits & Revision
::DELETED_TEXT
)
1069 && $this->list->getCurrent() == $this->getId();
1073 * Get the HTML link to the revision text.
1074 * Overridden by RevDel_ArchiveItem.
1076 protected function getRevisionLink() {
1078 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1079 if ( $this->isDeleted() && !$this->canView() ) {
1082 return $this->special
->skin
->link(
1087 'oldid' => $this->revision
->getId(),
1094 * Get the HTML link to the diff.
1095 * Overridden by RevDel_ArchiveItem
1097 protected function getDiffLink() {
1098 if ( $this->isDeleted() && !$this->canView() ) {
1099 return wfMsgHtml('diff');
1102 $this->special
->skin
->link(
1107 'diff' => $this->revision
->getId(),
1119 public function getHTML() {
1120 $difflink = $this->getDiffLink();
1121 $revlink = $this->getRevisionLink();
1122 $userlink = $this->special
->skin
->revUserLink( $this->revision
);
1123 $comment = $this->special
->skin
->revComment( $this->revision
);
1124 if ( $this->isDeleted() ) {
1125 $revlink = "<span class=\"history-deleted\">$revlink</span>";
1127 return "<li>($difflink) $revlink $userlink $comment</li>";
1132 * List for archive table items, i.e. revisions deleted via action=delete
1134 class RevDel_ArchiveList
extends RevDel_RevisionList
{
1135 var $type = 'archive';
1136 var $idField = 'ar_timestamp';
1137 var $dateField = 'ar_timestamp';
1139 public function doQuery( $db ) {
1140 $timestamps = array();
1141 foreach ( $this->ids
as $id ) {
1142 $timestamps[] = $db->timestamp( $id );
1144 return $db->select( 'archive', '*',
1146 'ar_namespace' => $this->title
->getNamespace(),
1147 'ar_title' => $this->title
->getDBkey(),
1148 'ar_timestamp' => $timestamps
1154 public function newItem( $row ) {
1155 return new RevDel_ArchiveItem( $this, $row );
1158 public function doPreCommitUpdates() {
1159 return Status
::newGood();
1162 public function doPostCommitUpdates() {
1163 return Status
::newGood();
1168 * Item class for a archive table row
1170 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
1171 public function __construct( $list, $row ) {
1172 RevDel_Item
::__construct( $list, $row );
1173 $this->revision
= Revision
::newFromArchiveRow( $row,
1174 array( 'page' => $this->list->title
->getArticleId() ) );
1177 public function getId() {
1178 # Convert DB timestamp to MW timestamp
1179 return $this->revision
->getTimestamp();
1182 public function setBits( $bits ) {
1183 $dbw = wfGetDB( DB_MASTER
);
1184 $dbw->update( 'archive',
1185 array( 'ar_deleted' => $bits ),
1186 array( 'ar_namespace' => $this->list->title
->getNamespace(),
1187 'ar_title' => $this->list->title
->getDBkey(),
1188 // use timestamp for index
1189 'ar_timestamp' => $this->row
->ar_timestamp
,
1190 'ar_rev_id' => $this->row
->ar_rev_id
,
1191 'ar_deleted' => $this->getBits()
1194 return (bool)$dbw->affectedRows();
1197 protected function getRevisionLink() {
1199 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1200 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1201 if ( $this->isDeleted() && !$this->canView() ) {
1204 return $this->special
->skin
->link( $undelete, $date, array(),
1206 'target' => $this->list->title
->getPrefixedText(),
1207 'timestamp' => $this->revision
->getTimestamp()
1211 protected function getDiffLink() {
1212 if ( $this->isDeleted() && !$this->canView() ) {
1213 return wfMsgHtml( 'diff' );
1215 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1216 return $this->special
->skin
->link( $undelete, wfMsgHtml('diff'), array(),
1218 'target' => $this->list->title
->getPrefixedText(),
1220 'timestamp' => $this->revision
->getTimestamp()
1226 * List for oldimage table items
1228 class RevDel_FileList
extends RevDel_List
{
1229 var $type = 'oldimage';
1230 var $idField = 'oi_archive_name';
1231 var $dateField = 'oi_timestamp';
1232 var $storeBatch, $deleteBatch, $cleanupBatch;
1234 public function doQuery( $db ) {
1235 $archiveName = array();
1236 foreach( $this->ids
as $timestamp ) {
1237 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
1239 return $db->select( 'oldimage', '*',
1241 'oi_name' => $this->title
->getDBkey(),
1242 'oi_archive_name' => $archiveNames
1248 public function newItem( $row ) {
1249 return new RevDel_FileItem( $this, $row );
1252 public function clearFileOps() {
1253 $this->deleteBatch
= array();
1254 $this->storeBatch
= array();
1255 $this->cleanupBatch
= array();
1258 public function doPreCommitUpdates() {
1259 $status = Status
::newGood();
1260 $repo = RepoGroup
::singleton()->getLocalRepo();
1261 if ( $this->storeBatch
) {
1262 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
1264 if ( !$status->isOK() ) {
1267 if ( $this->deleteBatch
) {
1268 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
1270 if ( !$status->isOK() ) {
1271 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1272 // modified (but destined for rollback) causes data loss
1275 if ( $this->cleanupBatch
) {
1276 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
1281 public function doPostCommitUpdates() {
1282 $file = wfLocalFile( $this->title
);
1283 $file->purgeCache();
1284 $file->purgeDescription();
1285 return Status
::newGood();
1288 public function getSuppressBit() {
1289 return File
::DELETED_RESTRICTED
;
1294 * Item class for an oldimage table row
1296 class RevDel_FileItem
extends RevDel_Item
{
1299 public function __construct( $list, $row ) {
1300 parent
::__construct( $list, $row );
1301 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
1304 public function getId() {
1305 $parts = explode( '!', $this->row
->oi_archive_name
);
1309 public function canView() {
1310 return $this->file
->userCan( File
::DELETED_RESTRICTED
);
1313 public function getBits() {
1314 /** FIXME: use accessor */
1315 return $this->file
->deleted
;
1318 public function setBits( $bits ) {
1320 # FIXME: move to LocalFile.php
1321 if ( $this->isDeleted() ) {
1322 if ( $bits & File
::DELETED_FILE
) {
1326 $key = $this->file
->getStorageKey();
1327 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1328 $this->list->storeBatch
[] = array(
1329 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1331 $this->file
->getRel()
1333 $this->list->cleanupBatch
[] = $key;
1335 } elseif ( $bits & File
::DELETED_FILE
) {
1337 $key = $this->file
->getStorageKey();
1338 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1339 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
1342 # Do the database operations
1343 $dbw = wfGetDB( DB_MASTER
);
1344 $dbw->update( 'oldimage',
1345 array( 'oi_deleted' => $bits ),
1347 'oi_name' => $this->row
->oi_name
,
1348 'oi_timestamp' => $this->row
->oi_timestamp
,
1349 'oi_deleted' => $this->getBits()
1353 return (bool)$dbw->affectedRows();
1356 public function isDeleted() {
1357 return $this->file
->isDeleted( File
::DELETED_FILE
);
1361 * Get the link to the file.
1362 * Overridden by RevDel_ArchivedFileItem.
1364 protected function getLink() {
1366 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1367 if ( $this->isDeleted() ) {
1369 if ( !$this->canView() ) {
1372 return $this->special
->skin
->link(
1373 $this->special
->getTitle(),
1376 'target' => $this->list->title
->getPrefixedText(),
1377 'file' => $this->file
->sha1
. '.' . $this->file
->getExtension()
1383 $url = $this->file
->getUrl();
1384 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
1388 * Generate a user tool link cluster if the current user is allowed to view it
1389 * @return string HTML
1391 protected function getUserTools() {
1392 if( $this->file
->userCan( Revision
::DELETED_USER
) ) {
1393 $link = $this->special
->skin
->userLink( $this->file
->user
, $this->file
->user_text
) .
1394 $this->special
->skin
->userToolLinks( $this->file
->user
, $this->file
->user_text
);
1396 $link = wfMsgHtml( 'rev-deleted-user' );
1398 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
1399 return '<span class="history-deleted">' . $link . '</span>';
1405 * Wrap and format the file's comment block, if the current
1406 * user is allowed to view it.
1408 * @return string HTML
1410 protected function getComment() {
1411 if( $this->file
->userCan( File
::DELETED_COMMENT
) ) {
1412 $block = $this->special
->skin
->commentBlock( $this->file
->description
);
1414 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1416 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
1417 return "<span class=\"history-deleted\">$block</span>";
1422 public function getHTML() {
1427 $wgLang->formatNum( $this->file
->getWidth() ),
1428 $wgLang->formatNum( $this->file
->getHeight() )
1431 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file
->getSize() ) ) .
1433 $pageLink = $this->getLink();
1435 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1436 $data . ' ' . $this->getComment(). '</li>';
1441 * List for filearchive table items
1443 class RevDel_ArchivedFileList
extends RevDel_FileList
{
1444 var $type = 'filearchive';
1445 var $idField = 'fa_id';
1446 var $dateField = 'fa_timestamp';
1448 public function doQuery( $db ) {
1449 $ids = array_map( 'intval', $this->ids
);
1450 return $db->select( 'filearchive', '*',
1452 'fa_name' => $this->title
->getDBkey(),
1459 public function newItem( $row ) {
1460 return new RevDel_ArchivedFileItem( $this, $row );
1465 * Item class for a filearchive table row
1467 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
1468 public function __construct( $list, $row ) {
1469 RevDel_Item
::__construct( $list, $row );
1470 $this->file
= ArchivedFile
::newFromRow( $row );
1473 public function getId() {
1474 return $this->row
->fa_id
;
1477 public function setBits( $bits ) {
1478 $dbw = wfGetDB( DB_MASTER
);
1479 $dbw->update( 'filearchive',
1480 array( 'fa_deleted' => $bits ),
1482 'fa_id' => $this->row
->fa_id
,
1483 'fa_deleted' => $this->getBits(),
1487 return (bool)$dbw->affectedRows();
1490 protected function getLink() {
1491 global $wgLang, $wgUser;
1492 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1493 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1494 $key = $this->file
->getKey();
1495 return $this->special
->skin
->link( $undelete, $date, array(),
1497 'target' => $this->list->title
->getPrefixedText(),
1499 'token' => $wgUser->editToken( $key )
1505 * List for logging table items
1507 class RevDel_LogList
extends RevDel_List
{
1508 var $type = 'logging';
1509 var $idField = 'log_id';
1510 var $dateField = 'log_timestamp';
1512 public function doQuery( $db ) {
1513 global $wgMessageCache;
1514 $wgMessageCache->loadAllMessages();
1515 $ids = array_map( 'intval', $this->ids
);
1516 return $db->select( 'logging', '*',
1517 array( 'log_id' => $ids ),
1522 public function newItem( $row ) {
1523 return new RevDel_LogItem( $this, $row );
1526 public function getSuppressBit() {
1527 return Revision
::DELETED_RESTRICTED
;
1530 public function getLogAction() {
1534 public function getLogParams( $params ) {
1536 implode( ',', $params['ids'] ),
1537 "ofield={$params['oldBits']}",
1538 "nfield={$params['newBits']}"
1544 * Item class for a logging table row
1546 class RevDel_LogItem
extends RevDel_Item
{
1547 public function canView() {
1548 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
);
1551 public function getBits() {
1552 return $this->row
->log_deleted
;
1555 public function setBits( $bits ) {
1556 $dbw = wfGetDB( DB_MASTER
);
1557 $dbw->update( 'recentchanges',
1559 'rc_deleted' => $bits,
1563 'rc_logid' => $this->row
->log_id
,
1564 'rc_timestamp' => $this->row
->log_timestamp
1568 $dbw->update( 'logging',
1569 array( 'log_deleted' => $bits ),
1571 'log_id' => $this->row
->log_id
,
1572 'log_deleted' => $this->getBits()
1576 return (bool)$dbw->affectedRows();
1579 public function getHTML() {
1582 $date = htmlspecialchars( $wgLang->timeanddate( $this->row
->log_timestamp
) );
1583 $paramArray = LogPage
::extractParams( $this->row
->log_params
);
1584 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
1586 $logtitle = SpecialPage
::getTitleFor( 'Log' );
1587 $loglink = $this->special
->skin
->link(
1591 array( 'page' => $title->getPrefixedText() )
1594 if( !$this->canView() ) {
1595 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1597 $action = LogPage
::actionText( $this->row
->log_type
, $this->row
->log_action
, $title,
1598 $this->special
->skin
, $paramArray, true, true );
1599 if( $this->row
->log_deleted
& LogPage
::DELETED_ACTION
)
1600 $action = '<span class="history-deleted">' . $action . '</span>';
1603 $userLink = $this->special
->skin
->userLink(
1604 $this->row
->log_user
, User
::WhoIs( $this->row
->log_user
) );
1605 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_USER
) ) {
1606 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1609 $comment = $wgLang->getDirMark() . $this->special
->skin
->commentBlock( $this->row
->log_comment
);
1610 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
1611 $comment = '<span class="history-deleted">' . $comment . '</span>';
1613 return "<li>($loglink) $date $userLink $action $comment</li>";