5b9448db16ea5a104789533f35e78bc7685c3a82
3 * Special page allowing users with the appropriate permissions to view
4 * and hide revisions. Log items can also be hidden.
10 class SpecialRevisionDelete
extends UnlistedSpecialPage
{
14 /** True if the submit button was clicked, and the form was posted */
20 /** Archive name, for reviewing deleted files */
23 /** Edit token for securing image views against XSS */
26 /** Title object for target parameter */
29 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
32 /** Array of checkbox specs (message, name, deletion bits) */
35 /** Information about the current type */
38 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
41 /** New bitfield value, used for form display post-submit */
45 * Assorted information about each type, needed by the special page.
46 * TODO Move some of this to the list class
48 static $allowedTypes = array(
50 'check-label' => 'revdelete-hide-text',
51 'deletion-bits' => Revision
::DELETED_TEXT
,
52 'success' => 'revdelete-success',
53 'failure' => 'revdelete-failure',
54 'list-class' => 'RevDel_RevisionList',
57 'check-label' => 'revdelete-hide-text',
58 'deletion-bits' => Revision
::DELETED_TEXT
,
59 'success' => 'revdelete-success',
60 'failure' => 'revdelete-failure',
61 'list-class' => 'RevDel_ArchiveList',
64 'check-label' => 'revdelete-hide-image',
65 'deletion-bits' => File
::DELETED_FILE
,
66 'success' => 'revdelete-success',
67 'failure' => 'revdelete-failure',
68 'list-class' => 'RevDel_FileList',
70 'filearchive' => array(
71 'check-label' => 'revdelete-hide-image',
72 'deletion-bits' => File
::DELETED_FILE
,
73 'success' => 'revdelete-success',
74 'failure' => 'revdelete-failure',
75 'list-class' => 'RevDel_ArchivedFileList',
78 'check-label' => 'revdelete-hide-name',
79 'deletion-bits' => LogPage
::DELETED_ACTION
,
80 'success' => 'logdelete-success',
81 'failure' => 'logdelete-failure',
82 'list-class' => 'RevDel_LogList',
86 /** Type map to support old log entries */
87 static $deprecatedTypeMap = array(
88 'oldid' => 'revision',
89 'artimestamp' => 'archive',
90 'oldimage' => 'oldimage',
91 'fileid' => 'filearchive',
95 public function __construct() {
96 parent
::__construct( 'Revisiondelete', 'deleterevision' );
99 public function execute( $par ) {
100 global $wgOut, $wgUser, $wgRequest;
101 if( !$wgUser->isAllowed( 'deleterevision' ) ) {
102 $wgOut->permissionRequired( 'deleterevision' );
104 } else if( wfReadOnly() ) {
105 $wgOut->readOnlyPage();
108 $this->skin
= $wgUser->getSkin();
110 $this->outputHeader();
111 $this->submitClicked
= $wgRequest->wasPosted() && $wgRequest->getBool( 'wpSubmit' );
112 # Handle our many different possible input types.
113 # Use CSV, since the cgi handling will break on arrays.
114 $this->ids
= explode( ',', $wgRequest->getVal('ids') );
115 $this->ids
= array_unique( array_filter( $this->ids
) );
116 $this->targetObj
= Title
::newFromText( $wgRequest->getText( 'target' ) );
118 # For reviewing deleted files...
119 $this->archiveName
= $wgRequest->getVal( 'file' );
120 $this->token
= $wgRequest->getVal( 'token' );
121 if ( $this->archiveName
&& $this->targetObj
) {
122 $this->tryShowFile( $this->archiveName
);
126 $this->typeName
= $wgRequest->getVal( 'type' );
127 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
128 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
132 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
133 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
136 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
138 # If we have revisions, get the title from the first one
139 # since they should all be from the same page. This allows
140 # for more flexibility with page moves...
141 if( $this->typeName
== 'revision' ) {
142 $rev = Revision
::newFromId( $this->ids
[0] );
143 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
145 # We need a target page!
146 if( is_null($this->targetObj
) ) {
147 $wgOut->addWikiMsg( 'undelete-header' );
150 # Give a link to the logs/hist for this page
151 $this->showConvenienceLinks();
153 # Initialise checkboxes
154 $this->checks
= array(
155 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
156 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
157 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
159 if( $wgUser->isAllowed('suppressrevision') ) {
160 $this->checks
[] = array( 'revdelete-hide-restricted',
161 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
164 # Either submit or create our form
165 if( $this->submitClicked
) {
166 $this->submit( $wgRequest );
170 $qc = $this->getLogQueryCond();
171 # Show relevant lines from the deletion log
172 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
173 LogEventsList
::showLogExtract( $wgOut, 'delete',
174 $this->targetObj
->getPrefixedText(), '', 25, $qc );
175 # Show relevant lines from the suppression log
176 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
177 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
178 LogEventsList
::showLogExtract( $wgOut, 'suppress',
179 $this->targetObj
->getPrefixedText(), '', 25, $qc );
184 * Show some useful links in the subtitle
186 protected function showConvenienceLinks() {
187 global $wgOut, $wgUser;
188 # Give a link to the logs/hist for this page
189 if( $this->targetObj
) {
191 $logtitle = SpecialPage
::getTitleFor( 'Log' );
192 $links[] = $this->skin
->makeKnownLinkObj( $logtitle, wfMsgHtml( 'viewpagelogs' ),
193 wfArrayToCGI( array( 'page' => $this->targetObj
->getPrefixedUrl() ) ) );
194 # Give a link to the page history
195 $links[] = $this->skin
->makeKnownLinkObj( $this->targetObj
, wfMsgHtml( 'pagehist' ),
196 wfArrayToCGI( array( 'action' => 'history' ) ) );
197 # Link to deleted edits
198 if( $wgUser->isAllowed('undelete') ) {
199 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
200 $links[] = $this->skin
->makeKnownLinkObj( $undelete, wfMsgHtml( 'deletedhist' ),
201 wfArrayToCGI( array( 'target' => $this->targetObj
->getPrefixedDBkey() ) ) );
203 # Logs themselves don't have histories or archived revisions
204 $wgOut->setSubtitle( '<p>'.implode($links,' / ').'</p>' );
209 * Get the condition used for fetching log snippets
211 protected function getLogQueryCond() {
213 // Revision delete logs for these item
214 $conds['log_type'] = array('delete','suppress');
215 $conds['log_action'] = $this->getList()->getLogAction();
216 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
217 $conds['ls_value'] = $this->ids
;
222 * Show a deleted file version requested by the visitor.
223 * TODO Mostly copied from Special:Undelete. Refactor.
225 protected function tryShowFile( $archiveName ) {
226 global $wgOut, $wgRequest, $wgUser, $wgLang;
228 $repo = RepoGroup
::singleton()->getLocalRepo();
229 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
231 // Check if user is allowed to see this file
232 if ( !$oimage->exists() ) {
233 $wgOut->addWikiMsg( 'revdelete-no-file' );
236 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
237 $wgOut->permissionRequired( 'suppressrevision' );
240 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
241 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
242 $this->targetObj
->getText(),
243 $wgLang->date( $oimage->getTimestamp() ),
244 $wgLang->time( $oimage->getTimestamp() ) );
246 Xml
::openElement( 'form', array(
248 'action' => $this->getTitle()->getLocalUrl(
249 'target=' . urlencode( $oimage->getName() ) .
250 '&file=' . urlencode( $archiveName ) .
251 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
254 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
260 # We mustn't allow the output to be Squid cached, otherwise
261 # if an admin previews a deleted image, and it's cached, then
262 # a user without appropriate permissions can toddle off and
263 # nab the image, and Squid will serve it
264 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
265 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
266 $wgRequest->response()->header( 'Pragma: no-cache' );
268 # Stream the file to the client
270 require_once( "$IP/includes/StreamFile.php" );
271 $key = $oimage->getStorageKey();
272 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
273 wfStreamFile( $path );
277 * Get the list object for this request
279 protected function getList() {
280 if ( is_null( $this->list ) ) {
281 $class = $this->typeInfo
['list-class'];
282 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
288 * Show a list of items that we will operate on, and show a form with checkboxes
289 * which will allow the user to choose new visibility settings.
291 protected function showForm() {
292 global $wgOut, $wgUser, $wgLang;
295 if ( $this->typeName
== 'logging' ) {
296 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
298 $wgOut->addWikiMsg( 'revdelete-selected',
299 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
303 $wgOut->addHTML( "<ul>" );
305 $where = $revObjs = array();
309 $list = $this->getList();
310 for ( $list->reset(); $list->current(); $list->next() ) {
311 $item = $list->current();
312 if ( !$item->canView() ) {
313 if( !$this->submitClicked
) {
314 $wgOut->permissionRequired( 'suppressrevision' );
317 $UserAllowed = false;
320 $bitfields |
= $item->getBits();
321 $wgOut->addHTML( $item->getHTML() );
324 if( !$numRevisions ) {
325 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
329 if ( !is_null( $this->newBits
) ) {
330 $bitfields = $this->newBits
;
333 $wgOut->addHTML( "</ul>" );
335 $this->addUsageText();
337 // Normal sysops can always see what they did, but can't always change it
338 if( !$UserAllowed ) return;
341 Xml
::openElement( 'form', array( 'method' => 'post',
342 'action' => $this->getTitle()->getLocalUrl( 'action=submit' ),
343 'id' => 'mw-revdel-form-revisions' ) ) .
344 Xml
::openElement( 'fieldset' ) .
345 Xml
::element( 'legend', null, wfMsg( 'revdelete-legend' ) ) .
346 $this->buildCheckBoxes( $bitfields ) .
347 '<p>' . Xml
::inputLabel( wfMsg( 'revdelete-log' ), 'wpReason', 'wpReason', 60 ) . '</p>' .
348 '<p>' . Xml
::submitButton( wfMsg( 'revdelete-submit' ),
349 array( 'name' => 'wpSubmit' ) ) . '</p>' .
350 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
351 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
352 Xml
::hidden( 'type', $this->typeName
) .
353 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
354 Xml
::closeElement( 'fieldset' ) .
355 Xml
::closeElement( 'form' ) . "\n"
360 * Show some introductory text
361 * FIXME Wikimedia-specific policy text
363 protected function addUsageText() {
364 global $wgOut, $wgUser;
365 $wgOut->addWikiMsg( 'revdelete-text' );
366 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
367 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
372 * @param $bitfields Interger: aggregate bitfield of all the bitfields
373 * @return String: HTML
375 protected function buildCheckBoxes( $bitfields ) {
377 // FIXME: all items checked for just one rev are checked, even if not set for the others
378 foreach( $this->checks
as $item ) {
379 list( $message, $name, $field ) = $item;
380 $line = Xml
::tags( 'div', null, Xml
::checkLabel( wfMsg($message), $name, $name,
381 $bitfields & $field ) );
382 if( $field == Revision
::DELETED_RESTRICTED
) $line = "<b>$line</b>";
389 * UI entry point for form submission.
390 * @param $request WebRequest
392 protected function submit( $request ) {
393 global $wgUser, $wgOut;
394 # Check edit token on submission
395 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
396 $wgOut->addWikiMsg( 'sessionfailure' );
399 $bitfield = $this->extractBitfield( $request );
400 $comment = $request->getText( 'wpReason' );
401 # Can the user set this field?
402 if( $bitfield & Revision
::DELETED_RESTRICTED
&& !$wgUser->isAllowed('suppressrevision') ) {
403 $wgOut->permissionRequired( 'suppressrevision' );
406 # If the save went through, go to success message...
407 $status = $this->save( $bitfield, $comment, $this->targetObj
);
408 if ( $status->isGood() ) {
411 # ...otherwise, bounce back to form...
413 $this->failure( $status );
419 * Report that the submit operation succeeded
421 protected function success() {
423 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
424 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo
['success'] );
429 * Report that the submit operation failed
431 protected function failure( $status ) {
433 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
434 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
439 * Put together a rev_deleted bitfield from the submitted checkboxes
440 * @param $request WebRequest
443 protected function extractBitfield( $request ) {
445 foreach( $this->checks
as $item ) {
446 list( /* message */ , $name, $field ) = $item;
447 if( $request->getCheck( $name ) ) {
455 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
457 protected function save( $bitfield, $reason, $title ) {
458 // Don't allow simply locking the interface for no reason
459 if( $bitfield == Revision
::DELETED_RESTRICTED
) {
460 return Status
::newFatal( 'revdelete-only-restricted' );
462 $this->newBits
= $bitfield;
463 return $this->getList()->setVisibility( array(
464 'value' => $bitfield,
465 'comment' => $reason ) );
470 * Temporary b/c interface, collection of static functions.
471 * @ingroup SpecialPage
473 class RevisionDeleter
{
475 * Checks for a change in the bitfield for a certain option and updates the
476 * provided array accordingly.
478 * @param $desc String: description to add to the array if the option was
479 * enabled / disabled.
480 * @param $field Integer: the bitmask describing the single option.
481 * @param $diff Integer: the xor of the old and new bitfields.
482 * @param $new Integer: the new bitfield
483 * @param $arr Array: the array to update.
485 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
486 if( $diff & $field ) {
487 $arr[ ( $new & $field ) ?
0 : 1 ][] = $desc;
492 * Gets an array describing the changes made to the visibilit of the revision.
493 * If the resulting array is $arr, then $arr[0] will contain an array of strings
494 * describing the items that were hidden, $arr[2] will contain an array of strings
495 * describing the items that were unhidden, and $arr[3] will contain an array with
496 * a single string, which can be one of "applied restrictions to sysops",
497 * "removed restrictions from sysops", or null.
499 * @param $n Integer: the new bitfield.
500 * @param $o Integer: the old bitfield.
501 * @return An array as described above.
503 protected static function getChanges( $n, $o ) {
505 $ret = array( 0 => array(), 1 => array(), 2 => array() );
506 // Build bitfield changes in language
507 self
::checkItem( wfMsgForContent( 'revdelete-content' ),
508 Revision
::DELETED_TEXT
, $diff, $n, $ret );
509 self
::checkItem( wfMsgForContent( 'revdelete-summary' ),
510 Revision
::DELETED_COMMENT
, $diff, $n, $ret );
511 self
::checkItem( wfMsgForContent( 'revdelete-uname' ),
512 Revision
::DELETED_USER
, $diff, $n, $ret );
513 // Restriction application to sysops
514 if( $diff & Revision
::DELETED_RESTRICTED
) {
515 if( $n & Revision
::DELETED_RESTRICTED
)
516 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
518 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
524 * Gets a log message to describe the given revision visibility change. This
525 * message will be of the form "[hid {content, edit summary, username}];
526 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
528 * @param $count Integer: The number of effected revisions.
529 * @param $nbitfield Integer: The new bitfield for the revision.
530 * @param $obitfield Integer: The old bitfield for the revision.
531 * @param $isForLog Boolean
533 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
536 $changes = self
::getChanges( $nbitfield, $obitfield );
537 if( count( $changes[0] ) ) {
538 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
540 if( count( $changes[1] ) ) {
542 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
544 if( count( $changes[2] ) ) {
545 $s .= $s ?
' (' . $changes[2][0] . ')' : $changes[2][0];
547 $msg = $isForLog ?
'logdelete-log-message' : 'revdelete-log-message';
548 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
552 // Get DB field name for URL param...
553 // Future code for other things may also track
554 // other types of revision-specific changes.
555 public static function getRelationType( $typeName ) {
556 if ( isset( SpecialRevisionDelete
::$deprecatedTypeMap[$typeName] ) ) {
557 $typeName = SpecialRevisionDelete
::$deprecatedTypeMap[$typeName];
559 if ( isset( SpecialRevisionDelete
::$allowedTypes[$typeName] ) ) {
560 $class = SpecialRevisionDelete
::$allowedTypes[$typeName]['list-class'];
561 $list = new $class( null, null, null );
562 return $list->getIdField();
570 * Abstract base class for a list of deletable items
572 abstract class RevDel_List
{
573 var $special, $title, $ids, $res, $current;
574 var $type = null; // override this
575 var $idField = null; // override this
576 var $dateField = false; // override this
579 * @param $special The parent SpecialPage
580 * @param $title The target title
581 * @param $ids Array of IDs
583 public function __construct( $special, $title, $ids ) {
584 $this->special
= $special;
585 $this->title
= $title;
590 * Get the internal type name of this list. Equal to the table name.
592 public function getType() {
597 * Get the DB field name associated with the ID list/
599 public function getIdField() {
600 return $this->idField
;
604 * Get the DB field name storing timestamps
606 public function getTimestampField() {
607 return $this->dateField
;
611 * Set the visibility for the revisions in this list. Logging and
612 * transactions are done here.
614 * @param $params Associative array of parameters. Members are:
615 * value: The integer value to set the visibility to
616 * comment: The log comment.
619 public function setVisibility( $params ) {
620 $newBits = $params['value'];
621 $comment = $params['comment'];
624 $dbw = wfGetDB( DB_MASTER
);
625 $this->doQuery( $dbw );
627 $status = Status
::newGood();
628 $missing = array_flip( $this->ids
);
629 $this->clearFileOps();
630 $idsForLog = array();
632 for ( $this->reset(); $this->current(); $this->next() ) {
633 $item = $this->current();
634 unset( $missing[ $item->getId() ] );
636 // Make error messages less vague
637 $oldBits = $item->getBits();
638 if ( $oldBits == $newBits ) {
639 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
640 $status->failCount++
;
642 } elseif ( $oldBits == 0 && $newBits != 0 ) {
644 } elseif ( $oldBits != 0 && $newBits == 0 ) {
650 // TODO: use FILE::DELETED_FILE, though value is the same
651 if ( $item->isCurrent() && ($newBits & Revision
::DELETED_TEXT
) ) {
652 // Cannot hide current version text
653 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
654 $status->failCount++
;
657 if ( !$item->canView() ) {
658 // Cannot access this revision
659 $msg = $opType == 'show' ?
'revdelete-show-no-access' : 'revdelete-modify-no-access';
660 $status->error( $msg, $item->formatDate(), $item->formatTime() );
661 $status->failCount++
;
665 // Update the revision
666 $ok = $item->setBits( $newBits );
669 $idsForLog[] = $item->getId();
670 $status->successCount++
;
672 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
673 $status->failCount++
;
677 // Handle missing revisions
678 foreach ( $missing as $id => $unused ) {
679 $status->error( 'revdelete-modify-missing', $id );
680 $status->failCount++
;
683 if ( $status->successCount
== 0 ) {
689 // Save success count
690 $successCount = $status->successCount
;
692 // Move files, if there are any
693 $status->merge( $this->doPreCommitUpdates() );
694 if ( !$status->isOK() ) {
695 // Fatal error, such as no configured archive directory
701 $this->updateLog( array(
702 'title' => $this->title
,
703 'count' => $successCount,
704 'newBits' => $newBits,
705 'oldBits' => $oldBits,
706 'comment' => $comment,
712 $status->merge( $this->doPostCommitUpdates() );
717 * Record a log entry on the action
718 * @param $params Associative array of parameters:
719 * newBits: The new value of the *_deleted bitfield
720 * oldBits: The old value of the *_deleted bitfield.
721 * title: The target title
723 * comment: The log comment
725 protected function updateLog( $params ) {
726 // Get the URL param's corresponding DB field
727 $field = RevisionDeleter
::getRelationType( $this->getType() );
729 throw new MWException( "Bad log URL param type!" );
731 // Put things hidden from sysops in the oversight log
732 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
733 $logType = 'suppress';
737 // Add params for effected page and ids
738 $logParams = $this->getLogParams( $params );
739 // Actually add the deletion log entry
740 $log = new LogPage( $logType );
741 $logid = $log->addEntry( $this->getLogAction(), $params['title'], $params['comment'], $logParams );
742 // Allow for easy searching of deletion log items for revision/log items
743 $log->addRelations( $field, $params['ids'], $logid );
747 * Get the log action for this list type
749 public function getLogAction() {
754 * Get log parameter array.
755 * @param $params Associative array of log parameters, same as updateLog()
758 public function getLogParams( $params ) {
761 implode( ',', $params['ids'] ),
762 "ofield={$params['oldBits']}",
763 "nfield={$params['newBits']}"
768 * Initialise the current iteration pointer
770 protected function initCurrent() {
771 $row = $this->res
->current();
773 $this->current
= $this->newItem( $row );
775 $this->current
= false;
780 * Start iteration. This must be called before current() or next().
781 * @return First list item
783 public function reset() {
785 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
787 $this->res
->rewind();
789 $this->initCurrent();
790 return $this->current
;
794 * Get the current list item, or false if we are at the end
796 public function current() {
797 return $this->current
;
801 * Move the iteration pointer to the next list item, and return it.
803 public function next() {
805 $this->initCurrent();
806 return $this->current
;
810 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
813 public function clearFileOps() {
817 * A hook for setVisibility(): do batch updates pre-commit.
821 public function doPreCommitUpdates() {
822 return Status
::newGood();
826 * A hook for setVisibility(): do any necessary updates post-commit.
830 public function doPostCommitUpdates() {
831 return Status
::newGood();
835 * Create an item object from a DB result row
836 * @param $row stdclass
838 abstract public function newItem( $row );
841 * Do the DB query to iterate through the objects.
842 * @param $db Database object to use for the query
844 abstract public function doQuery( $db );
847 * Get the integer value of the flag used for suppression
849 abstract public function getSuppressBit();
853 * Abstract base class for deletable items
855 abstract class RevDel_Item
{
856 /** The parent SpecialPage */
859 /** The parent RevDel_List */
862 /** The DB result row */
866 * @param $list RevDel_List
867 * @param $row DB result row
869 public function __construct( $list, $row ) {
870 $this->special
= $list->special
;
876 * Get the ID, as it would appear in the ids URL parameter
878 public function getId() {
879 $field = $this->list->getIdField();
880 return $this->row
->$field;
884 * Get the date, formatted with $wgLang
886 public function formatDate() {
888 return $wgLang->date( $this->getTimestamp() );
892 * Get the time, formatted with $wgLang
894 public function formatTime() {
896 return $wgLang->time( $this->getTimestamp() );
900 * Get the timestamp in MW 14-char form
902 public function getTimestamp() {
903 $field = $this->list->getTimestampField();
904 return wfTimestamp( TS_MW
, $this->row
->$field );
908 * Returns true if the item is "current" and can't be deleted for that reason
911 public function isCurrent() {
916 * Returns true if the current user can view the item
918 abstract public function canView();
921 * Get the current deletion bitfield value
923 abstract public function getBits();
926 * Get the HTML of the list item. Should be include <li></li> tags.
927 * This is used to show the list in HTML form, by the special page.
929 abstract public function getHTML();
932 * Set the visibility of the item. This should do any necessary DB queries.
934 * The DB update query should have a condition which forces it to only update
935 * if the value in the DB matches the value fetched earlier with the SELECT.
936 * If the update fails because it did not match, the function should return
937 * false. This prevents concurrency problems.
939 * @return boolean success
941 abstract public function setBits( $newBits );
945 * List for revision table items
947 class RevDel_RevisionList
extends RevDel_List
{
949 var $type = 'revision';
950 var $idField = 'rev_id';
951 var $dateField = 'rev_timestamp';
953 public function doQuery( $db ) {
954 $ids = array_map( 'intval', $this->ids
);
955 return $db->select( array('revision','page'), '*',
957 'rev_page' => $this->title
->getArticleID(),
965 public function newItem( $row ) {
966 return new RevDel_RevisionItem( $this, $row );
969 public function getCurrent() {
970 if ( is_null( $this->currentRevId
) ) {
971 $dbw = wfGetDB( DB_MASTER
);
972 $this->currentRevId
= $dbw->selectField(
973 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
975 return $this->currentRevId
;
978 public function getSuppressBit() {
979 return Revision
::DELETED_RESTRICTED
;
982 public function doPreCommitUpdates() {
983 $this->title
->invalidateCache();
984 return Status
::newGood();
987 public function doPostCommitUpdates() {
988 $this->title
->purgeSquid();
989 // Extensions that require referencing previous revisions may need this
990 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$this->title
) );
991 return Status
::newGood();
996 * Item class for a revision table row
998 class RevDel_RevisionItem
extends RevDel_Item
{
1001 public function __construct( $list, $row ) {
1002 parent
::__construct( $list, $row );
1003 $this->revision
= new Revision( $row );
1006 public function canView() {
1007 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
);
1010 public function getBits() {
1011 return $this->revision
->mDeleted
;
1014 public function setBits( $bits ) {
1015 $dbw = wfGetDB( DB_MASTER
);
1016 // Update revision table
1017 $dbw->update( 'revision',
1018 array( 'rev_deleted' => $bits ),
1020 'rev_id' => $this->revision
->getId(),
1021 'rev_page' => $this->revision
->getPage(),
1022 'rev_deleted' => $this->getBits()
1026 if ( !$dbw->affectedRows() ) {
1030 // Update recentchanges table
1031 $dbw->update( 'recentchanges',
1033 'rc_deleted' => $bits,
1037 'rc_this_oldid' => $this->revision
->getId(), // condition
1038 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ), // non-unique index
1045 public function isDeleted() {
1046 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
1049 public function isCurrent() {
1050 return $this->list->getCurrent() == $this->getId();
1054 * Get the HTML link to the revision text.
1055 * Overridden by RevDel_ArchiveItem.
1057 protected function getRevisionLink() {
1059 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1060 if ( $this->isDeleted() && !$this->canView() ) {
1063 return $this->special
->skin
->makeLinkObj( $this->list->title
, $date,
1064 'oldid='.$this->revision
->getId() . '&unhide=1' );
1068 * Get the HTML link to the diff.
1069 * Overridden by RevDel_ArchiveItem
1071 protected function getDiffLink() {
1072 if ( $this->isDeleted() && !$this->canView() ) {
1073 return wfMsgHtml('diff');
1076 $this->special
->skin
->makeKnownLinkObj(
1079 'diff=' . $this->revision
->getId() . '&oldid=prev&unhide=1'
1084 public function getHTML() {
1085 $difflink = $this->getDiffLink();
1086 $revlink = $this->getRevisionLink();
1087 $userlink = $this->special
->skin
->revUserLink( $this->revision
);
1088 $comment = $this->special
->skin
->revComment( $this->revision
);
1089 if ( $this->isDeleted() ) {
1090 $revlink = "<span class=\"history-deleted\">$revlink</span>";
1092 return "<li>($difflink) $revlink $userlink $comment</li>";
1097 * List for archive table items, i.e. revisions deleted via action=delete
1099 class RevDel_ArchiveList
extends RevDel_RevisionList
{
1100 var $type = 'archive';
1101 var $idField = 'ar_timestamp';
1102 var $dateField = 'ar_timestamp';
1104 public function doQuery( $db ) {
1105 $timestamps = array();
1106 foreach ( $this->ids
as $id ) {
1107 $timestamps[] = $db->timestamp( $id );
1109 return $db->select( 'archive', '*',
1111 'ar_namespace' => $this->title
->getNamespace(),
1112 'ar_title' => $this->title
->getDBkey(),
1113 'ar_timestamp' => $timestamps
1119 public function newItem( $row ) {
1120 return new RevDel_ArchiveItem( $this, $row );
1123 public function doPreCommitUpdates() {
1124 return Status
::newGood();
1127 public function doPostCommitUpdates() {
1128 return Status
::newGood();
1133 * Item class for a archive table row
1135 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
1136 public function __construct( $list, $row ) {
1137 RevDel_Item
::__construct( $list, $row );
1138 $this->revision
= Revision
::newFromArchiveRow( $row,
1139 array( 'page' => $this->list->title
->getArticleId() ) );
1142 public function getId() {
1143 # Convert DB timestamp to MW timestamp
1144 return $this->revision
->getTimestamp();
1147 public function setBits( $bits ) {
1148 $dbw = wfGetDB( DB_MASTER
);
1149 $dbw->update( 'archive',
1150 array( 'ar_deleted' => $bits ),
1151 array( 'ar_namespace' => $this->list->title
->getNamespace(),
1152 'ar_title' => $this->list->title
->getDBkey(),
1153 // use timestamp for index
1154 'ar_timestamp' => $this->row
->ar_timestamp
,
1155 'ar_rev_id' => $this->row
->ar_rev_id
,
1156 'ar_deleted' => $this->getBits()
1159 return (bool)$dbw->affectedRows();
1162 protected function getRevisionLink() {
1164 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1165 $date = $wgLang->timeanddate( $this->revision
->getTimestamp() );
1166 if ( $this->isDeleted() && !$this->canView() ) {
1169 return $this->special
->skin
->link( $undelete, $date, array(),
1171 'target' => $this->list->title
->getPrefixedText(),
1172 'timestamp' => $this->revision
->getTimestamp()
1176 protected function getDiffLink() {
1177 if ( $this->isDeleted() && !$this->canView() ) {
1178 return wfMsgHtml( 'diff' );
1180 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1181 return $this->special
->skin
->link( $undelete, wfMsgHtml('diff'), array(),
1183 'target' => $this->list->title
->getPrefixedText(),
1185 'timestamp' => $this->revision
->getTimestamp()
1191 * List for oldimage table items
1193 class RevDel_FileList
extends RevDel_List
{
1194 var $type = 'oldimage';
1195 var $idField = 'oi_archive_name';
1196 var $dateField = 'oi_timestamp';
1197 var $storeBatch, $deleteBatch, $cleanupBatch;
1199 public function doQuery( $db ) {
1200 $archiveName = array();
1201 foreach( $this->ids
as $timestamp ) {
1202 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
1204 return $db->select( 'oldimage', '*',
1206 'oi_name' => $this->title
->getDBkey(),
1207 'oi_archive_name' => $archiveNames
1213 public function newItem( $row ) {
1214 return new RevDel_FileItem( $this, $row );
1217 public function clearFileOps() {
1218 $this->deleteBatch
= array();
1219 $this->storeBatch
= array();
1220 $this->cleanupBatch
= array();
1223 public function doPreCommitUpdates() {
1224 $status = Status
::newGood();
1225 $repo = RepoGroup
::singleton()->getLocalRepo();
1226 if ( $this->storeBatch
) {
1227 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
1229 if ( !$status->isOK() ) {
1232 if ( $this->deleteBatch
) {
1233 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
1235 if ( !$status->isOK() ) {
1236 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1237 // modified (but destined for rollback) causes data loss
1240 if ( $this->cleanupBatch
) {
1241 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
1246 public function doPostCommitUpdates() {
1247 $file = wfLocalFile( $this->title
);
1248 $file->purgeCache();
1249 $file->purgeDescription();
1250 return Status
::newGood();
1253 public function getSuppressBit() {
1254 return File
::DELETED_RESTRICTED
;
1259 * Item class for an oldimage table row
1261 class RevDel_FileItem
extends RevDel_Item
{
1264 public function __construct( $list, $row ) {
1265 parent
::__construct( $list, $row );
1266 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
1269 public function getId() {
1270 $parts = explode( '!', $this->row
->oi_archive_name
);
1274 public function canView() {
1275 return $this->file
->userCan( File
::DELETED_RESTRICTED
);
1278 public function getBits() {
1279 /** FIXME: use accessor */
1280 return $this->file
->deleted
;
1283 public function setBits( $bits ) {
1285 # FIXME: move to LocalFile.php
1286 if ( $this->isDeleted() ) {
1287 if ( $bits & File
::DELETED_FILE
) {
1291 $key = $this->file
->getStorageKey();
1292 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1293 $this->list->storeBatch
[] = array(
1294 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1296 $this->file
->getRel()
1298 $this->list->cleanupBatch
[] = $key;
1300 } elseif ( $bits & File
::DELETED_FILE
) {
1302 $key = $this->file
->getStorageKey();
1303 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
1304 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
1307 # Do the database operations
1308 $dbw = wfGetDB( DB_MASTER
);
1309 $dbw->update( 'oldimage',
1310 array( 'oi_deleted' => $bits ),
1312 'oi_name' => $this->row
->oi_name
,
1313 'oi_timestamp' => $this->row
->oi_timestamp
,
1314 'oi_deleted' => $this->getBits()
1318 return (bool)$dbw->affectedRows();
1321 public function isDeleted() {
1322 return $this->file
->isDeleted( File
::DELETED_FILE
);
1326 * Get the link to the file.
1327 * Overridden by RevDel_ArchivedFileItem.
1329 protected function getLink() {
1331 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1332 if ( $this->isDeleted() ) {
1334 if ( !$this->canView() ) {
1337 return $this->special
->skin
->link(
1338 $this->special
->getTitle(),
1341 'target' => $this->list->title
->getPrefixedText(),
1342 'file' => $this->file
->sha1
. '.' . $this->file
->getExtension()
1348 $url = $this->file
->getUrl();
1349 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
1353 * Generate a user tool link cluster if the current user is allowed to view it
1354 * @return string HTML
1356 protected function getUserTools() {
1357 if( $this->file
->userCan( Revision
::DELETED_USER
) ) {
1358 $link = $this->special
->skin
->userLink( $this->file
->user
, $this->file
->user_text
) .
1359 $this->special
->skin
->userToolLinks( $this->file
->user
, $this->file
->user_text
);
1361 $link = wfMsgHtml( 'rev-deleted-user' );
1363 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
1364 return '<span class="history-deleted">' . $link . '</span>';
1370 * Wrap and format the file's comment block, if the current
1371 * user is allowed to view it.
1373 * @return string HTML
1375 protected function getComment() {
1376 if( $this->file
->userCan( File
::DELETED_COMMENT
) ) {
1377 $block = $this->special
->skin
->commentBlock( $this->file
->description
);
1379 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1381 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
1382 return "<span class=\"history-deleted\">$block</span>";
1387 public function getHTML() {
1392 $wgLang->formatNum( $this->file
->getWidth() ),
1393 $wgLang->formatNum( $this->file
->getHeight() )
1396 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file
->getSize() ) ) .
1398 $pageLink = $this->getLink();
1400 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1401 $data . ' ' . $this->getComment(). '</li>';
1406 * List for filearchive table items
1408 class RevDel_ArchivedFileList
extends RevDel_FileList
{
1409 var $type = 'filearchive';
1410 var $idField = 'fa_id';
1411 var $dateField = 'fa_timestamp';
1413 public function doQuery( $db ) {
1414 $ids = array_map( 'intval', $this->ids
);
1415 return $db->select( 'filearchive', '*',
1417 'fa_name' => $this->title
->getDBkey(),
1424 public function newItem( $row ) {
1425 return new RevDel_ArchivedFileItem( $this, $row );
1430 * Item class for a filearchive table row
1432 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
1433 public function __construct( $list, $row ) {
1434 RevDel_Item
::__construct( $list, $row );
1435 $this->file
= ArchivedFile
::newFromRow( $row );
1438 public function getId() {
1439 return $this->row
->fa_id
;
1442 public function setBits( $bits ) {
1443 $dbw = wfGetDB( DB_MASTER
);
1444 $dbw->update( 'filearchive',
1445 array( 'fa_deleted' => $bits ),
1447 'fa_id' => $this->row
->fa_id
,
1448 'fa_deleted' => $this->getBits(),
1452 return (bool)$dbw->affectedRows();
1455 protected function getLink() {
1456 global $wgLang, $wgUser;
1457 $date = $wgLang->timeanddate( $this->file
->getTimestamp(), true );
1458 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
1459 $key = $this->file
->getKey();
1460 return $this->special
->skin
->link( $undelete, $date, array(),
1462 'target' => $this->list->title
->getPrefixedText(),
1464 'token' => $wgUser->editToken( $key )
1470 * List for logging table items
1472 class RevDel_LogList
extends RevDel_List
{
1473 var $type = 'logging';
1474 var $idField = 'log_id';
1475 var $dateField = 'log_timestamp';
1477 public function doQuery( $db ) {
1478 global $wgMessageCache;
1479 $wgMessageCache->loadAllMessages();
1480 $ids = array_map( 'intval', $this->ids
);
1481 return $db->select( 'logging', '*',
1482 array( 'log_id' => $ids ),
1487 public function newItem( $row ) {
1488 return new RevDel_LogItem( $this, $row );
1491 public function getSuppressBit() {
1492 return Revision
::DELETED_RESTRICTED
;
1495 public function getLogAction() {
1499 public function getLogParams( $params ) {
1501 implode( ',', $params['ids'] ),
1502 "ofield={$params['oldBits']}",
1503 "nfield={$params['newBits']}"
1509 * Item class for a logging table row
1511 class RevDel_LogItem
extends RevDel_Item
{
1512 public function canView() {
1513 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
);
1516 public function getBits() {
1517 return $this->row
->log_deleted
;
1520 public function setBits( $bits ) {
1521 $dbw = wfGetDB( DB_MASTER
);
1522 $dbw->update( 'recentchanges',
1524 'rc_deleted' => $bits,
1528 'rc_logid' => $this->row
->log_id
,
1529 'rc_timestamp' => $this->row
->log_timestamp
1533 $dbw->update( 'logging',
1534 array( 'log_deleted' => $bits ),
1536 'log_id' => $this->row
->log_id
,
1537 'log_deleted' => $this->getBits()
1541 return (bool)$dbw->affectedRows();
1544 public function getHTML() {
1547 $date = htmlspecialchars( $wgLang->timeanddate( $this->row
->log_timestamp
) );
1548 $paramArray = LogPage
::extractParams( $this->row
->log_params
);
1549 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
1551 $logtitle = SpecialPage
::getTitleFor( 'Log' );
1552 $loglink = $this->special
->skin
->link( $logtitle, wfMsgHtml( 'log' ), array(),
1553 array( 'page' => $title->getPrefixedUrl() ) );
1555 if( !$this->canView() ) {
1556 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1558 $action = LogPage
::actionText( $this->row
->log_type
, $this->row
->log_action
, $title,
1559 $this->special
->skin
, $paramArray, true, true );
1560 if( $this->row
->log_deleted
& LogPage
::DELETED_ACTION
)
1561 $action = '<span class="history-deleted">' . $action . '</span>';
1564 $userLink = $this->special
->skin
->userLink(
1565 $this->row
->log_user
, User
::WhoIs( $this->row
->log_user
) );
1566 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_USER
) ) {
1567 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1570 $comment = $wgLang->getDirMark() . $this->special
->skin
->commentBlock( $this->row
->log_comment
);
1571 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
1572 $comment = '<span class="history-deleted">' . $comment . '</span>';
1574 return "<li>($loglink) $date $userLink $action $comment</li>";