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
->makeKnownLinkObj( $logtitle, wfMsgHtml( 'viewpagelogs' ),
190 wfArrayToCGI( array( 'page' => $this->targetObj
->getPrefixedUrl() ) ) );
191 # Give a link to the page history
192 $links[] = $this->skin
->makeKnownLinkObj( $this->targetObj
, wfMsgHtml( 'pagehist' ),
193 wfArrayToCGI( array( 'action' => 'history' ) ) );
194 # Link to deleted edits
195 if( $wgUser->isAllowed('undelete') ) {
196 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
197 $links[] = $this->skin
->makeKnownLinkObj( $undelete, wfMsgHtml( 'deletedhist' ),
198 wfArrayToCGI( array( 'target' => $this->targetObj
->getPrefixedDBkey() ) ) );
200 # Logs themselves don't have histories or archived revisions
201 $wgOut->setSubtitle( '<p>'.implode($links,' / ').'</p>' );
206 * Get the condition used for fetching log snippets
208 protected function getLogQueryCond() {
210 // Revision delete logs for these item
211 $conds['log_type'] = array('delete','suppress');
212 $conds['log_action'] = $this->getList()->getLogAction();
213 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
214 $conds['ls_value'] = $this->ids
;
219 * Show a deleted file version requested by the visitor.
220 * TODO Mostly copied from Special:Undelete. Refactor.
222 protected function tryShowFile( $archiveName ) {
223 global $wgOut, $wgRequest, $wgUser, $wgLang;
225 $repo = RepoGroup
::singleton()->getLocalRepo();
226 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
228 // Check if user is allowed to see this file
229 if ( !$oimage->exists() ) {
230 $wgOut->addWikiMsg( 'revdelete-no-file' );
233 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
234 $wgOut->permissionRequired( 'suppressrevision' );
237 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
238 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
239 $this->targetObj
->getText(),
240 $wgLang->date( $oimage->getTimestamp() ),
241 $wgLang->time( $oimage->getTimestamp() ) );
243 Xml
::openElement( 'form', array(
245 'action' => $this->getTitle()->getLocalUrl(
246 'target=' . urlencode( $oimage->getName() ) .
247 '&file=' . urlencode( $archiveName ) .
248 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
251 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
257 # We mustn't allow the output to be Squid cached, otherwise
258 # if an admin previews a deleted image, and it's cached, then
259 # a user without appropriate permissions can toddle off and
260 # nab the image, and Squid will serve it
261 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
262 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
263 $wgRequest->response()->header( 'Pragma: no-cache' );
265 # Stream the file to the client
267 require_once( "$IP/includes/StreamFile.php" );
268 $key = $oimage->getStorageKey();
269 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
270 wfStreamFile( $path );
274 * Get the list object for this request
276 protected function getList() {
277 if ( is_null( $this->list ) ) {
278 $class = $this->typeInfo
['list-class'];
279 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
285 * Show a list of items that we will operate on, and show a form with checkboxes
286 * which will allow the user to choose new visibility settings.
288 protected function showForm() {
289 global $wgOut, $wgUser, $wgLang;
292 if ( $this->typeName
== 'logging' ) {
293 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
295 $wgOut->addWikiMsg( 'revdelete-selected',
296 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
300 $wgOut->addHTML( "<ul>" );
302 $where = $revObjs = array();
306 $list = $this->getList();
307 for ( $list->reset(); $list->current(); $list->next() ) {
308 $item = $list->current();
309 if ( !$item->canView() ) {
310 if( !$this->submitClicked
) {
311 $wgOut->permissionRequired( 'suppressrevision' );
314 $UserAllowed = false;
317 $bitfields |
= $item->getBits();
318 $wgOut->addHTML( $item->getHTML() );
321 if( !$numRevisions ) {
322 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
326 $wgOut->addHTML( "</ul>" );
328 $this->addUsageText();
330 // Normal sysops can always see what they did, but can't always change it
331 if( !$UserAllowed ) return;
334 Xml
::openElement( 'form', array( 'method' => 'post',
335 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
336 'id' => 'mw-revdel-form-revisions' ) ) .
337 Xml
::openElement( 'fieldset' ) .
338 Xml
::element( 'legend', null, wfMsg( 'revdelete-legend' ) ) .
339 $this->buildCheckBoxes( $bitfields ) .
340 '<p>' . Xml
::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ) . '</p>' .
341 '<p>' . Xml
::submitButton( wfMsg( 'revdelete-submit' ),
342 array( 'name' => 'wpSubmit' ) ) . '</p>' .
343 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
344 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
345 Xml
::hidden( 'type', $this->typeName
) .
346 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
347 Xml
::closeElement( 'fieldset' ) .
348 Xml
::closeElement( 'form' ) . "\n"
353 * Show some introductory text
354 * FIXME Wikimedia-specific policy text
356 protected function addUsageText() {
357 global $wgOut, $wgUser;
358 $wgOut->addWikiMsg( 'revdelete-text' );
359 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
360 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
365 * @param $bitfields Interger: aggregate bitfield of all the bitfields
366 * @return String: HTML
368 protected function buildCheckBoxes( $bitfields ) {
370 // FIXME: all items checked for just one rev are checked, even if not set for the others
371 foreach( $this->checks
as $item ) {
372 list( $message, $name, $field ) = $item;
373 $line = Xml
::tags( 'div', null, Xml
::checkLabel( wfMsg($message), $name, $name,
374 $bitfields & $field ) );
375 if( $field == Revision
::DELETED_RESTRICTED
) $line = "<b>$line</b>";
382 * UI entry point for form submission.
383 * @param $request WebRequest
385 protected function submit( $request ) {
386 global $wgUser, $wgOut;
387 # Check edit token on submission
388 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
389 $wgOut->addWikiMsg( 'sessionfailure' );
392 $bitfield = $this->extractBitfield( $request );
393 $comment = $request->getText( 'wpReason' );
394 # Can the user set this field?
395 if( $bitfield & Revision
::DELETED_RESTRICTED
&& !$wgUser->isAllowed('suppressrevision') ) {
396 $wgOut->permissionRequired( 'suppressrevision' );
399 # If the save went through, go to success message...
400 $status = $this->save( $bitfield, $comment, $this->targetObj
);
401 if ( $status->isGood() ) {
404 # ...otherwise, bounce back to form...
406 $this->failure( $status );
412 * Report that the submit operation succeeded
414 protected function success() {
416 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
417 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo
['success'] );
418 $this->list->reloadFromMaster();
423 * Report that the submit operation failed
425 protected function failure( $status ) {
427 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
428 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
433 * Put together a rev_deleted bitfield from the submitted checkboxes
434 * @param $request WebRequest
437 protected function extractBitfield( $request ) {
439 foreach( $this->checks
as $item ) {
440 list( /* message */ , $name, $field ) = $item;
441 if( $request->getCheck( $name ) ) {
449 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
451 protected function save( $bitfield, $reason, $title ) {
452 // Don't allow simply locking the interface for no reason
453 if( $bitfield == Revision
::DELETED_RESTRICTED
) {
454 return Status
::newFatal( 'revdelete-only-restricted' );
456 return $this->getList()->setVisibility( array(
457 'value' => $bitfield,
458 'comment' => $reason ) );
463 * Temporary b/c interface, collection of static functions.
464 * @ingroup SpecialPage
466 class RevisionDeleter
{
468 * Checks for a change in the bitfield for a certain option and updates the
469 * provided array accordingly.
471 * @param $desc String: description to add to the array if the option was
472 * enabled / disabled.
473 * @param $field Integer: the bitmask describing the single option.
474 * @param $diff Integer: the xor of the old and new bitfields.
475 * @param $new Integer: the new bitfield
476 * @param $arr Array: the array to update.
478 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
479 if( $diff & $field ) {
480 $arr[ ( $new & $field ) ?
0 : 1 ][] = $desc;
485 * Gets an array describing the changes made to the visibilit of the revision.
486 * If the resulting array is $arr, then $arr[0] will contain an array of strings
487 * describing the items that were hidden, $arr[2] will contain an array of strings
488 * describing the items that were unhidden, and $arr[3] will contain an array with
489 * a single string, which can be one of "applied restrictions to sysops",
490 * "removed restrictions from sysops", or null.
492 * @param $n Integer: the new bitfield.
493 * @param $o Integer: the old bitfield.
494 * @return An array as described above.
496 protected static function getChanges( $n, $o ) {
498 $ret = array( 0 => array(), 1 => array(), 2 => array() );
499 // Build bitfield changes in language
500 self
::checkItem( wfMsgForContent( 'revdelete-content' ),
501 Revision
::DELETED_TEXT
, $diff, $n, $ret );
502 self
::checkItem( wfMsgForContent( 'revdelete-summary' ),
503 Revision
::DELETED_COMMENT
, $diff, $n, $ret );
504 self
::checkItem( wfMsgForContent( 'revdelete-uname' ),
505 Revision
::DELETED_USER
, $diff, $n, $ret );
506 // Restriction application to sysops
507 if( $diff & Revision
::DELETED_RESTRICTED
) {
508 if( $n & Revision
::DELETED_RESTRICTED
)
509 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
511 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
517 * Gets a log message to describe the given revision visibility change. This
518 * message will be of the form "[hid {content, edit summary, username}];
519 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
521 * @param $count Integer: The number of effected revisions.
522 * @param $nbitfield Integer: The new bitfield for the revision.
523 * @param $obitfield Integer: The old bitfield for the revision.
524 * @param $isForLog Boolean
526 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
529 $changes = self
::getChanges( $nbitfield, $obitfield );
530 if( count( $changes[0] ) ) {
531 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
533 if( count( $changes[1] ) ) {
535 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
537 if( count( $changes[2] ) ) {
538 $s .= $s ?
' (' . $changes[2][0] . ')' : $changes[2][0];
540 $msg = $isForLog ?
'logdelete-log-message' : 'revdelete-log-message';
541 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
545 // Get DB field name for URL param...
546 // Future code for other things may also track
547 // other types of revision-specific changes.
548 public static function getRelationType( $typeName ) {
549 if ( isset( SpecialRevisionDelete
::$deprecatedTypeMap[$typeName] ) ) {
550 $typeName = SpecialRevisionDelete
::$deprecatedTypeMap[$typeName];
552 if ( isset( SpecialRevisionDelete
::$allowedTypes[$typeName] ) ) {
553 $class = SpecialRevisionDelete
::$allowedTypes[$typeName]['list-class'];
554 $list = new $class( null, null, null );
555 return $list->getIdField();
563 * Abstract base class for a list of deletable items
565 abstract class RevDel_List
{
566 var $special, $title, $ids, $res, $current;
567 var $type = null; // override this
568 var $idField = null; // override this
569 var $dateField = false; // override this
572 * @param $special The parent SpecialPage
573 * @param $title The target title
574 * @param $ids Array of IDs
576 public function __construct( $special, $title, $ids ) {
577 $this->special
= $special;
578 $this->title
= $title;
583 * Get the internal type name of this list. Equal to the table name.
585 public function getType() {
590 * Get the DB field name associated with the ID list/
592 public function getIdField() {
593 return $this->idField
;
597 * Get the DB field name storing timestamps
599 public function getTimestampField() {
600 return $this->dateField
;
604 * Set the visibility for the revisions in this list. Logging and
605 * transactions are done here.
607 * @param $params Associative array of parameters. Members are:
608 * value: The integer value to set the visibility to
609 * comment: The log comment.
612 public function setVisibility( $params ) {
613 $newBits = $params['value'];
614 $comment = $params['comment'];
617 $dbw = wfGetDB( DB_MASTER
);
618 $this->doQuery( $dbw );
620 $status = Status
::newGood();
621 $missing = array_flip( $this->ids
);
622 $this->clearFileOps();
623 $idsForLog = array();
625 for ( $this->reset(); $this->current(); $this->next() ) {
626 $item = $this->current();
627 unset( $missing[ $item->getId() ] );
629 // Make error messages less vague
630 $oldBits = $item->getBits();
631 if ( $oldBits == $newBits ) {
632 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
633 $status->failCount++
;
635 } elseif ( $oldBits == 0 && $newBits != 0 ) {
637 } elseif ( $oldBits != 0 && $newBits == 0 ) {
643 if ( $item->isHideCurrentOp( $newBits ) ) {
644 // Cannot hide current version text
645 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
646 $status->failCount++
;
649 if ( !$item->canView() ) {
650 // Cannot access this revision
651 $msg = $opType == 'show' ?
'revdelete-show-no-access' : 'revdelete-modify-no-access';
652 $status->error( $msg, $item->formatDate(), $item->formatTime() );
653 $status->failCount++
;
657 // Update the revision
658 $ok = $item->setBits( $newBits );
661 $idsForLog[] = $item->getId();
662 $status->successCount++
;
664 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
665 $status->failCount++
;
669 // Handle missing revisions
670 foreach ( $missing as $id => $unused ) {
671 $status->error( 'revdelete-modify-missing', $id );
672 $status->failCount++
;
675 if ( $status->successCount
== 0 ) {
681 // Save success count
682 $successCount = $status->successCount
;
684 // Move files, if there are any
685 $status->merge( $this->doPreCommitUpdates() );
686 if ( !$status->isOK() ) {
687 // Fatal error, such as no configured archive directory
693 $this->updateLog( array(
694 'title' => $this->title
,
695 'count' => $successCount,
696 'newBits' => $newBits,
697 'oldBits' => $oldBits,
698 'comment' => $comment,
704 $status->merge( $this->doPostCommitUpdates() );
709 * Reload the list data from the master DB. This can be done after setVisibility()
710 * to allow $item->getHTML() to show the new data.
712 function reloadFromMaster() {
713 $dbw = wfGetDB( DB_MASTER
);
714 $this->res
= $this->doQuery( $dbw );
718 * Record a log entry on the action
719 * @param $params Associative array of parameters:
720 * newBits: The new value of the *_deleted bitfield
721 * oldBits: The old value of the *_deleted bitfield.
722 * title: The target title
724 * comment: The log comment
726 protected function updateLog( $params ) {
727 // Get the URL param's corresponding DB field
728 $field = RevisionDeleter
::getRelationType( $this->getType() );
730 throw new MWException( "Bad log URL param type!" );
732 // Put things hidden from sysops in the oversight log
733 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
734 $logType = 'suppress';
738 // Add params for effected page and ids
739 $logParams = $this->getLogParams( $params );
740 // Actually add the deletion log entry
741 $log = new LogPage( $logType );
742 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
743 $params['comment'], $logParams );
744 // Allow for easy searching of deletion log items for revision/log items
745 $log->addRelations( $field, $params['ids'], $logid );
749 * Get the log action for this list type
751 public function getLogAction() {
756 * Get log parameter array.
757 * @param $params Associative array of log parameters, same as updateLog()
760 public function getLogParams( $params ) {
763 implode( ',', $params['ids'] ),
764 "ofield={$params['oldBits']}",
765 "nfield={$params['newBits']}"
770 * Initialise the current iteration pointer
772 protected function initCurrent() {
773 $row = $this->res
->current();
775 $this->current
= $this->newItem( $row );
777 $this->current
= false;
782 * Start iteration. This must be called before current() or next().
783 * @return First list item
785 public function reset() {
787 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
789 $this->res
->rewind();
791 $this->initCurrent();
792 return $this->current
;
796 * Get the current list item, or false if we are at the end
798 public function current() {
799 return $this->current
;
803 * Move the iteration pointer to the next list item, and return it.
805 public function next() {
807 $this->initCurrent();
808 return $this->current
;
812 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
815 public function clearFileOps() {
819 * A hook for setVisibility(): do batch updates pre-commit.
823 public function doPreCommitUpdates() {
824 return Status
::newGood();
828 * A hook for setVisibility(): do any necessary updates post-commit.
832 public function doPostCommitUpdates() {
833 return Status
::newGood();
837 * Create an item object from a DB result row
838 * @param $row stdclass
840 abstract public function newItem( $row );
843 * Do the DB query to iterate through the objects.
844 * @param $db Database object to use for the query
846 abstract public function doQuery( $db );
849 * Get the integer value of the flag used for suppression
851 abstract public function getSuppressBit();
855 * Abstract base class for deletable items
857 abstract class RevDel_Item
{
858 /** The parent SpecialPage */
861 /** The parent RevDel_List */
864 /** The DB result row */
868 * @param $list RevDel_List
869 * @param $row DB result row
871 public function __construct( $list, $row ) {
872 $this->special
= $list->special
;
878 * Get the ID, as it would appear in the ids URL parameter
880 public function getId() {
881 $field = $this->list->getIdField();
882 return $this->row
->$field;
886 * Get the date, formatted with $wgLang
888 public function formatDate() {
890 return $wgLang->date( $this->getTimestamp() );
894 * Get the time, formatted with $wgLang
896 public function formatTime() {
898 return $wgLang->time( $this->getTimestamp() );
902 * Get the timestamp in MW 14-char form
904 public function getTimestamp() {
905 $field = $this->list->getTimestampField();
906 return wfTimestamp( TS_MW
, $this->row
->$field );
910 * Returns true if the item is "current", and the operation to set the given
911 * bits can't be executed for that reason
914 public function isHideCurrentOp( $newBits ) {
919 * Returns true if the current user can view the item
921 abstract public function canView();
924 * Get the current deletion bitfield value
926 abstract public function getBits();
929 * Get the HTML of the list item. Should be include <li></li> tags.
930 * This is used to show the list in HTML form, by the special page.
932 abstract public function getHTML();
935 * Set the visibility of the item. This should do any necessary DB queries.
937 * The DB update query should have a condition which forces it to only update
938 * if the value in the DB matches the value fetched earlier with the SELECT.
939 * If the update fails because it did not match, the function should return
940 * false. This prevents concurrency problems.
942 * @return boolean success
944 abstract public function setBits( $newBits );
948 * List for revision table items
950 class RevDel_RevisionList
extends RevDel_List
{
952 var $type = 'revision';
953 var $idField = 'rev_id';
954 var $dateField = 'rev_timestamp';
956 public function doQuery( $db ) {
957 $ids = array_map( 'intval', $this->ids
);
958 return $db->select( array('revision','page'), '*',
960 'rev_page' => $this->title
->getArticleID(),
968 public function newItem( $row ) {
969 return new RevDel_RevisionItem( $this, $row );
972 public function getCurrent() {
973 if ( is_null( $this->currentRevId
) ) {
974 $dbw = wfGetDB( DB_MASTER
);
975 $this->currentRevId
= $dbw->selectField(
976 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
978 return $this->currentRevId
;
981 public function getSuppressBit() {
982 return Revision
::DELETED_RESTRICTED
;
985 public function doPreCommitUpdates() {
986 $this->title
->invalidateCache();
987 return Status
::newGood();
990 public function doPostCommitUpdates() {
991 $this->title
->purgeSquid();
992 // Extensions that require referencing previous revisions may need this
993 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$this->title
) );
994 return Status
::newGood();
999 * Item class for a revision table row
1001 class RevDel_RevisionItem
extends RevDel_Item
{
1004 public function __construct( $list, $row ) {
1005 parent
::__construct( $list, $row );
1006 $this->revision
= new Revision( $row );
1009 public function canView() {
1010 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
);
1013 public function getBits() {
1014 return $this->revision
->mDeleted
;
1017 public function setBits( $bits ) {
1018 $dbw = wfGetDB( DB_MASTER
);
1019 // Update revision table
1020 $dbw->update( 'revision',
1021 array( 'rev_deleted' => $bits ),
1023 'rev_id' => $this->revision
->getId(),
1024 'rev_page' => $this->revision
->getPage(),
1025 'rev_deleted' => $this->getBits()
1029 if ( !$dbw->affectedRows() ) {
1033 // Update recentchanges table
1034 $dbw->update( 'recentchanges',
1036 'rc_deleted' => $bits,
1040 'rc_this_oldid' => $this->revision
->getId(), // condition
1041 // non-unique timestamp index
1042 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ),
1049 public function isDeleted() {
1050 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
1053 public function isHideCurrentOp( $newBits ) {
1054 return ( $newBits & Revision
::DELETED_TEXT
)
1055 && $this->list->getCurrent() == $this->getId();
1059 * Get the HTML link to the revision text.
1060 * Overridden by RevDel_ArchiveItem.
1062 protected function getRevisionLink() {
1064 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1065 if ( $this->isDeleted() && !$this->canView() ) {
1068 return $this->special
->skin
->link(
1073 'oldid' => $this->revision
->getId(),
1080 * Get the HTML link to the diff.
1081 * Overridden by RevDel_ArchiveItem
1083 protected function getDiffLink() {
1084 if ( $this->isDeleted() && !$this->canView() ) {
1085 return wfMsgHtml('diff');
1088 $this->special
->skin
->link(
1093 'diff' => $this->revision
->getId(),
1105 public function getHTML() {
1106 $difflink = $this->getDiffLink();
1107 $revlink = $this->getRevisionLink();
1108 $userlink = $this->special
->skin
->revUserLink( $this->revision
);
1109 $comment = $this->special
->skin
->revComment( $this->revision
);
1110 if ( $this->isDeleted() ) {
1111 $revlink = "<span class=\"history-deleted\">$revlink</span>";
1113 return "<li>($difflink) $revlink $userlink $comment</li>";
1118 * List for archive table items, i.e. revisions deleted via action=delete
1120 class RevDel_ArchiveList
extends RevDel_RevisionList
{
1121 var $type = 'archive';
1122 var $idField = 'ar_timestamp';
1123 var $dateField = 'ar_timestamp';
1125 public function doQuery( $db ) {
1126 $timestamps = array();
1127 foreach ( $this->ids
as $id ) {
1128 $timestamps[] = $db->timestamp( $id );
1130 return $db->select( 'archive', '*',
1132 'ar_namespace' => $this->title
->getNamespace(),
1133 'ar_title' => $this->title
->getDBkey(),
1134 'ar_timestamp' => $timestamps
1140 public function newItem( $row ) {
1141 return new RevDel_ArchiveItem( $this, $row );
1144 public function doPreCommitUpdates() {
1145 return Status
::newGood();
1148 public function doPostCommitUpdates() {
1149 return Status
::newGood();
1154 * Item class for a archive table row
1156 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
1157 public function __construct( $list, $row ) {
1158 RevDel_Item
::__construct( $list, $row );
1159 $this->revision
= Revision
::newFromArchiveRow( $row,
1160 array( 'page' => $this->list->title
->getArticleId() ) );
1163 public function getId() {
1164 # Convert DB timestamp to MW timestamp
1165 return $this->revision
->getTimestamp();
1168 public function setBits( $bits ) {
1169 $dbw = wfGetDB( DB_MASTER
);
1170 $dbw->update( 'archive',
1171 array( 'ar_deleted' => $bits ),
1172 array( 'ar_namespace' => $this->list->title
->getNamespace(),
1173 'ar_title' => $this->list->title
->getDBkey(),
1174 // use timestamp for index
1175 'ar_timestamp' => $this->row
->ar_timestamp
,
1176 'ar_rev_id' => $this->row
->ar_rev_id
,
1177 'ar_deleted' => $this->getBits()
1180 return (bool)$dbw->affectedRows();
1183 protected function getRevisionLink() {
1185 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1186 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1187 if ( $this->isDeleted() && !$this->canView() ) {
1190 return $this->special
->skin
->link( $undelete, $date, array(),
1192 'target' => $this->list->title
->getPrefixedText(),
1193 'timestamp' => $this->revision
->getTimestamp()
1197 protected function getDiffLink() {
1198 if ( $this->isDeleted() && !$this->canView() ) {
1199 return wfMsgHtml( 'diff' );
1201 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1202 return $this->special
->skin
->link( $undelete, wfMsgHtml('diff'), array(),
1204 'target' => $this->list->title
->getPrefixedText(),
1206 'timestamp' => $this->revision
->getTimestamp()
1212 * List for oldimage table items
1214 class RevDel_FileList
extends RevDel_List
{
1215 var $type = 'oldimage';
1216 var $idField = 'oi_archive_name';
1217 var $dateField = 'oi_timestamp';
1218 var $storeBatch, $deleteBatch, $cleanupBatch;
1220 public function doQuery( $db ) {
1221 $archiveName = array();
1222 foreach( $this->ids
as $timestamp ) {
1223 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
1225 return $db->select( 'oldimage', '*',
1227 'oi_name' => $this->title
->getDBkey(),
1228 'oi_archive_name' => $archiveNames
1234 public function newItem( $row ) {
1235 return new RevDel_FileItem( $this, $row );
1238 public function clearFileOps() {
1239 $this->deleteBatch
= array();
1240 $this->storeBatch
= array();
1241 $this->cleanupBatch
= array();
1244 public function doPreCommitUpdates() {
1245 $status = Status
::newGood();
1246 $repo = RepoGroup
::singleton()->getLocalRepo();
1247 if ( $this->storeBatch
) {
1248 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
1250 if ( !$status->isOK() ) {
1253 if ( $this->deleteBatch
) {
1254 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
1256 if ( !$status->isOK() ) {
1257 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1258 // modified (but destined for rollback) causes data loss
1261 if ( $this->cleanupBatch
) {
1262 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
1267 public function doPostCommitUpdates() {
1268 $file = wfLocalFile( $this->title
);
1269 $file->purgeCache();
1270 $file->purgeDescription();
1271 return Status
::newGood();
1274 public function getSuppressBit() {
1275 return File
::DELETED_RESTRICTED
;
1280 * Item class for an oldimage table row
1282 class RevDel_FileItem
extends RevDel_Item
{
1285 public function __construct( $list, $row ) {
1286 parent
::__construct( $list, $row );
1287 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
1290 public function getId() {
1291 $parts = explode( '!', $this->row
->oi_archive_name
);
1295 public function canView() {
1296 return $this->file
->userCan( File
::DELETED_RESTRICTED
);
1299 public function getBits() {
1300 /** FIXME: use accessor */
1301 return $this->file
->deleted
;
1304 public function setBits( $bits ) {
1306 # FIXME: move to LocalFile.php
1307 if ( $this->isDeleted() ) {
1308 if ( $bits & File
::DELETED_FILE
) {
1312 $key = $this->file
->getStorageKey();
1313 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1314 $this->list->storeBatch
[] = array(
1315 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1317 $this->file
->getRel()
1319 $this->list->cleanupBatch
[] = $key;
1321 } elseif ( $bits & File
::DELETED_FILE
) {
1323 $key = $this->file
->getStorageKey();
1324 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1325 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
1328 # Do the database operations
1329 $dbw = wfGetDB( DB_MASTER
);
1330 $dbw->update( 'oldimage',
1331 array( 'oi_deleted' => $bits ),
1333 'oi_name' => $this->row
->oi_name
,
1334 'oi_timestamp' => $this->row
->oi_timestamp
,
1335 'oi_deleted' => $this->getBits()
1339 return (bool)$dbw->affectedRows();
1342 public function isDeleted() {
1343 return $this->file
->isDeleted( File
::DELETED_FILE
);
1347 * Get the link to the file.
1348 * Overridden by RevDel_ArchivedFileItem.
1350 protected function getLink() {
1352 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1353 if ( $this->isDeleted() ) {
1355 if ( !$this->canView() ) {
1358 return $this->special
->skin
->link(
1359 $this->special
->getTitle(),
1362 'target' => $this->list->title
->getPrefixedText(),
1363 'file' => $this->file
->sha1
. '.' . $this->file
->getExtension()
1369 $url = $this->file
->getUrl();
1370 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
1374 * Generate a user tool link cluster if the current user is allowed to view it
1375 * @return string HTML
1377 protected function getUserTools() {
1378 if( $this->file
->userCan( Revision
::DELETED_USER
) ) {
1379 $link = $this->special
->skin
->userLink( $this->file
->user
, $this->file
->user_text
) .
1380 $this->special
->skin
->userToolLinks( $this->file
->user
, $this->file
->user_text
);
1382 $link = wfMsgHtml( 'rev-deleted-user' );
1384 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
1385 return '<span class="history-deleted">' . $link . '</span>';
1391 * Wrap and format the file's comment block, if the current
1392 * user is allowed to view it.
1394 * @return string HTML
1396 protected function getComment() {
1397 if( $this->file
->userCan( File
::DELETED_COMMENT
) ) {
1398 $block = $this->special
->skin
->commentBlock( $this->file
->description
);
1400 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1402 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
1403 return "<span class=\"history-deleted\">$block</span>";
1408 public function getHTML() {
1413 $wgLang->formatNum( $this->file
->getWidth() ),
1414 $wgLang->formatNum( $this->file
->getHeight() )
1417 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file
->getSize() ) ) .
1419 $pageLink = $this->getLink();
1421 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1422 $data . ' ' . $this->getComment(). '</li>';
1427 * List for filearchive table items
1429 class RevDel_ArchivedFileList
extends RevDel_FileList
{
1430 var $type = 'filearchive';
1431 var $idField = 'fa_id';
1432 var $dateField = 'fa_timestamp';
1434 public function doQuery( $db ) {
1435 $ids = array_map( 'intval', $this->ids
);
1436 return $db->select( 'filearchive', '*',
1438 'fa_name' => $this->title
->getDBkey(),
1445 public function newItem( $row ) {
1446 return new RevDel_ArchivedFileItem( $this, $row );
1451 * Item class for a filearchive table row
1453 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
1454 public function __construct( $list, $row ) {
1455 RevDel_Item
::__construct( $list, $row );
1456 $this->file
= ArchivedFile
::newFromRow( $row );
1459 public function getId() {
1460 return $this->row
->fa_id
;
1463 public function setBits( $bits ) {
1464 $dbw = wfGetDB( DB_MASTER
);
1465 $dbw->update( 'filearchive',
1466 array( 'fa_deleted' => $bits ),
1468 'fa_id' => $this->row
->fa_id
,
1469 'fa_deleted' => $this->getBits(),
1473 return (bool)$dbw->affectedRows();
1476 protected function getLink() {
1477 global $wgLang, $wgUser;
1478 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1479 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1480 $key = $this->file
->getKey();
1481 return $this->special
->skin
->link( $undelete, $date, array(),
1483 'target' => $this->list->title
->getPrefixedText(),
1485 'token' => $wgUser->editToken( $key )
1491 * List for logging table items
1493 class RevDel_LogList
extends RevDel_List
{
1494 var $type = 'logging';
1495 var $idField = 'log_id';
1496 var $dateField = 'log_timestamp';
1498 public function doQuery( $db ) {
1499 global $wgMessageCache;
1500 $wgMessageCache->loadAllMessages();
1501 $ids = array_map( 'intval', $this->ids
);
1502 return $db->select( 'logging', '*',
1503 array( 'log_id' => $ids ),
1508 public function newItem( $row ) {
1509 return new RevDel_LogItem( $this, $row );
1512 public function getSuppressBit() {
1513 return Revision
::DELETED_RESTRICTED
;
1516 public function getLogAction() {
1520 public function getLogParams( $params ) {
1522 implode( ',', $params['ids'] ),
1523 "ofield={$params['oldBits']}",
1524 "nfield={$params['newBits']}"
1530 * Item class for a logging table row
1532 class RevDel_LogItem
extends RevDel_Item
{
1533 public function canView() {
1534 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
);
1537 public function getBits() {
1538 return $this->row
->log_deleted
;
1541 public function setBits( $bits ) {
1542 $dbw = wfGetDB( DB_MASTER
);
1543 $dbw->update( 'recentchanges',
1545 'rc_deleted' => $bits,
1549 'rc_logid' => $this->row
->log_id
,
1550 'rc_timestamp' => $this->row
->log_timestamp
1554 $dbw->update( 'logging',
1555 array( 'log_deleted' => $bits ),
1557 'log_id' => $this->row
->log_id
,
1558 'log_deleted' => $this->getBits()
1562 return (bool)$dbw->affectedRows();
1565 public function getHTML() {
1568 $date = htmlspecialchars( $wgLang->timeanddate( $this->row
->log_timestamp
) );
1569 $paramArray = LogPage
::extractParams( $this->row
->log_params
);
1570 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
1572 $logtitle = SpecialPage
::getTitleFor( 'Log' );
1573 $loglink = $this->special
->skin
->link( $logtitle, wfMsgHtml( 'log' ), array(),
1574 array( 'page' => $title->getPrefixedUrl() ) );
1576 if( !$this->canView() ) {
1577 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1579 $action = LogPage
::actionText( $this->row
->log_type
, $this->row
->log_action
, $title,
1580 $this->special
->skin
, $paramArray, true, true );
1581 if( $this->row
->log_deleted
& LogPage
::DELETED_ACTION
)
1582 $action = '<span class="history-deleted">' . $action . '</span>';
1585 $userLink = $this->special
->skin
->userLink(
1586 $this->row
->log_user
, User
::WhoIs( $this->row
->log_user
) );
1587 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_USER
) ) {
1588 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1591 $comment = $wgLang->getDirMark() . $this->special
->skin
->commentBlock( $this->row
->log_comment
);
1592 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
1593 $comment = '<span class="history-deleted">' . $comment . '</span>';
1595 return "<li>($loglink) $date $userLink $action $comment</li>";