3 * Base implementations for deletable items.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup RevisionDelete
25 * List for revision table items
27 * This will check both the 'revision' table for live revisions and the
28 * 'archive' table for traditionally-deleted revisions that have an
31 * See RevDel_RevisionItem and RevDel_ArchivedRevisionItem for items.
33 class RevDel_RevisionList
extends RevDel_List
{
36 public function getType() {
40 public static function getRelationType() {
44 public static function getRestriction() {
45 return 'deleterevision';
48 public static function getRevdelConstant() {
49 return Revision
::DELETED_TEXT
;
52 public static function suggestTarget( $target, array $ids ) {
53 $rev = Revision
::newFromId( $ids[0] );
54 return $rev ?
$rev->getTitle() : $target;
58 * @param $db DatabaseBase
61 public function doQuery( $db ) {
62 $ids = array_map( 'intval', $this->ids
);
64 array( 'revision', 'page', 'user' ),
65 array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
67 'rev_page' => $this->title
->getArticleID(),
71 array( 'ORDER BY' => 'rev_id DESC' ),
73 'page' => Revision
::pageJoinCond(),
74 'user' => Revision
::userJoinCond() )
77 if ( $live->numRows() >= count( $ids ) ) {
78 // All requested revisions are live, keeps things simple!
82 // Check if any requested revisions are available fully deleted.
83 $archived = $db->select( array( 'archive' ), '*',
88 array( 'ORDER BY' => 'ar_rev_id DESC' )
91 if ( $archived->numRows() == 0 ) {
93 } elseif ( $live->numRows() == 0 ) {
96 // Combine the two! Whee
98 foreach ( $live as $row ) {
99 $rows[$row->rev_id
] = $row;
101 foreach ( $archived as $row ) {
102 $rows[$row->ar_rev_id
] = $row;
105 return new FakeResultWrapper( array_values( $rows ) );
109 public function newItem( $row ) {
110 if ( isset( $row->rev_id
) ) {
111 return new RevDel_RevisionItem( $this, $row );
112 } elseif ( isset( $row->ar_rev_id
) ) {
113 return new RevDel_ArchivedRevisionItem( $this, $row );
115 // This shouldn't happen. :)
116 throw new MWException( 'Invalid row type in RevDel_RevisionList' );
120 public function getCurrent() {
121 if ( is_null( $this->currentRevId
) ) {
122 $dbw = wfGetDB( DB_MASTER
);
123 $this->currentRevId
= $dbw->selectField(
124 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
126 return $this->currentRevId
;
129 public function getSuppressBit() {
130 return Revision
::DELETED_RESTRICTED
;
133 public function doPreCommitUpdates() {
134 $this->title
->invalidateCache();
135 return Status
::newGood();
138 public function doPostCommitUpdates() {
139 $this->title
->purgeSquid();
140 // Extensions that require referencing previous revisions may need this
141 wfRunHooks( 'ArticleRevisionVisibilitySet', array( &$this->title
) );
142 return Status
::newGood();
147 * Item class for a live revision table row
149 class RevDel_RevisionItem
extends RevDel_Item
{
152 public function __construct( $list, $row ) {
153 parent
::__construct( $list, $row );
154 $this->revision
= new Revision( $row );
157 public function getIdField() {
161 public function getTimestampField() {
162 return 'rev_timestamp';
165 public function getAuthorIdField() {
169 public function getAuthorNameField() {
170 return 'user_name'; // see Revision::selectUserFields()
173 public function canView() {
174 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
, $this->list->getUser() );
177 public function canViewContent() {
178 return $this->revision
->userCan( Revision
::DELETED_TEXT
, $this->list->getUser() );
181 public function getBits() {
182 return $this->revision
->getVisibility();
185 public function setBits( $bits ) {
186 $dbw = wfGetDB( DB_MASTER
);
187 // Update revision table
188 $dbw->update( 'revision',
189 array( 'rev_deleted' => $bits ),
191 'rev_id' => $this->revision
->getId(),
192 'rev_page' => $this->revision
->getPage(),
193 'rev_deleted' => $this->getBits()
197 if ( !$dbw->affectedRows() ) {
201 // Update recentchanges table
202 $dbw->update( 'recentchanges',
204 'rc_deleted' => $bits,
208 'rc_this_oldid' => $this->revision
->getId(), // condition
209 // non-unique timestamp index
210 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ),
217 public function isDeleted() {
218 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
221 public function isHideCurrentOp( $newBits ) {
222 return ( $newBits & Revision
::DELETED_TEXT
)
223 && $this->list->getCurrent() == $this->getId();
227 * Get the HTML link to the revision text.
228 * Overridden by RevDel_ArchiveItem.
231 protected function getRevisionLink() {
232 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
233 $this->revision
->getTimestamp(), $this->list->getUser() ) );
235 if ( $this->isDeleted() && !$this->canViewContent() ) {
238 return Linker
::linkKnown(
243 'oldid' => $this->revision
->getId(),
250 * Get the HTML link to the diff.
251 * Overridden by RevDel_ArchiveItem
254 protected function getDiffLink() {
255 if ( $this->isDeleted() && !$this->canViewContent() ) {
256 return $this->list->msg( 'diff' )->escaped();
258 return Linker
::linkKnown(
260 $this->list->msg( 'diff' )->escaped(),
263 'diff' => $this->revision
->getId(),
271 public function getHTML() {
272 $difflink = $this->list->msg( 'parentheses' )
273 ->rawParams( $this->getDiffLink() )->escaped();
274 $revlink = $this->getRevisionLink();
275 $userlink = Linker
::revUserLink( $this->revision
);
276 $comment = Linker
::revComment( $this->revision
);
277 if ( $this->isDeleted() ) {
278 $revlink = "<span class=\"history-deleted\">$revlink</span>";
280 return "<li>$difflink $revlink $userlink $comment</li>";
285 * List for archive table items, i.e. revisions deleted via action=delete
287 class RevDel_ArchiveList
extends RevDel_RevisionList
{
288 public function getType() {
292 public static function getRelationType() {
293 return 'ar_timestamp';
297 * @param $db DatabaseBase
300 public function doQuery( $db ) {
301 $timestamps = array();
302 foreach ( $this->ids
as $id ) {
303 $timestamps[] = $db->timestamp( $id );
305 return $db->select( 'archive', '*',
307 'ar_namespace' => $this->title
->getNamespace(),
308 'ar_title' => $this->title
->getDBkey(),
309 'ar_timestamp' => $timestamps
312 array( 'ORDER BY' => 'ar_timestamp DESC' )
316 public function newItem( $row ) {
317 return new RevDel_ArchiveItem( $this, $row );
320 public function doPreCommitUpdates() {
321 return Status
::newGood();
324 public function doPostCommitUpdates() {
325 return Status
::newGood();
330 * Item class for a archive table row
332 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
333 public function __construct( $list, $row ) {
334 RevDel_Item
::__construct( $list, $row );
335 $this->revision
= Revision
::newFromArchiveRow( $row,
336 array( 'page' => $this->list->title
->getArticleID() ) );
339 public function getIdField() {
340 return 'ar_timestamp';
343 public function getTimestampField() {
344 return 'ar_timestamp';
347 public function getAuthorIdField() {
351 public function getAuthorNameField() {
352 return 'ar_user_text';
355 public function getId() {
356 # Convert DB timestamp to MW timestamp
357 return $this->revision
->getTimestamp();
360 public function setBits( $bits ) {
361 $dbw = wfGetDB( DB_MASTER
);
362 $dbw->update( 'archive',
363 array( 'ar_deleted' => $bits ),
365 'ar_namespace' => $this->list->title
->getNamespace(),
366 'ar_title' => $this->list->title
->getDBkey(),
367 // use timestamp for index
368 'ar_timestamp' => $this->row
->ar_timestamp
,
369 'ar_rev_id' => $this->row
->ar_rev_id
,
370 'ar_deleted' => $this->getBits()
373 return (bool)$dbw->affectedRows();
376 protected function getRevisionLink() {
377 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
378 $this->revision
->getTimestamp(), $this->list->getUser() ) );
380 if ( $this->isDeleted() && !$this->canViewContent() ) {
385 SpecialPage
::getTitleFor( 'Undelete' ),
389 'target' => $this->list->title
->getPrefixedText(),
390 'timestamp' => $this->revision
->getTimestamp()
395 protected function getDiffLink() {
396 if ( $this->isDeleted() && !$this->canViewContent() ) {
397 return $this->list->msg( 'diff' )->escaped();
401 SpecialPage
::getTitleFor( 'Undelete' ),
402 $this->list->msg( 'diff' )->escaped(),
405 'target' => $this->list->title
->getPrefixedText(),
407 'timestamp' => $this->revision
->getTimestamp()
414 * Item class for a archive table row by ar_rev_id -- actually
415 * used via RevDel_RevisionList.
417 class RevDel_ArchivedRevisionItem
extends RevDel_ArchiveItem
{
418 public function __construct( $list, $row ) {
419 RevDel_Item
::__construct( $list, $row );
421 $this->revision
= Revision
::newFromArchiveRow( $row,
422 array( 'page' => $this->list->title
->getArticleID() ) );
425 public function getIdField() {
429 public function getId() {
430 return $this->revision
->getId();
433 public function setBits( $bits ) {
434 $dbw = wfGetDB( DB_MASTER
);
435 $dbw->update( 'archive',
436 array( 'ar_deleted' => $bits ),
437 array( 'ar_rev_id' => $this->row
->ar_rev_id
,
438 'ar_deleted' => $this->getBits()
441 return (bool)$dbw->affectedRows();
446 * List for oldimage table items
448 class RevDel_FileList
extends RevDel_List
{
449 public function getType() {
453 public static function getRelationType() {
454 return 'oi_archive_name';
457 public static function getRestriction() {
458 return 'deleterevision';
461 public static function getRevdelConstant() {
462 return File
::DELETED_FILE
;
465 var $storeBatch, $deleteBatch, $cleanupBatch;
468 * @param $db DatabaseBase
471 public function doQuery( $db ) {
472 $archiveNames = array();
473 foreach ( $this->ids
as $timestamp ) {
474 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
478 OldLocalFile
::selectFields(),
480 'oi_name' => $this->title
->getDBkey(),
481 'oi_archive_name' => $archiveNames
484 array( 'ORDER BY' => 'oi_timestamp DESC' )
488 public function newItem( $row ) {
489 return new RevDel_FileItem( $this, $row );
492 public function clearFileOps() {
493 $this->deleteBatch
= array();
494 $this->storeBatch
= array();
495 $this->cleanupBatch
= array();
498 public function doPreCommitUpdates() {
499 $status = Status
::newGood();
500 $repo = RepoGroup
::singleton()->getLocalRepo();
501 if ( $this->storeBatch
) {
502 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
504 if ( !$status->isOK() ) {
507 if ( $this->deleteBatch
) {
508 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
510 if ( !$status->isOK() ) {
511 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
512 // modified (but destined for rollback) causes data loss
515 if ( $this->cleanupBatch
) {
516 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
521 public function doPostCommitUpdates() {
523 $file = wfLocalFile( $this->title
);
525 $file->purgeDescription();
526 $purgeUrls = array();
527 foreach ( $this->ids
as $timestamp ) {
528 $archiveName = $timestamp . '!' . $this->title
->getDBkey();
529 $file->purgeOldThumbnails( $archiveName );
530 $purgeUrls[] = $file->getArchiveUrl( $archiveName );
533 // purge full images from cache
534 SquidUpdate
::purge( $purgeUrls );
536 return Status
::newGood();
539 public function getSuppressBit() {
540 return File
::DELETED_RESTRICTED
;
545 * Item class for an oldimage table row
547 class RevDel_FileItem
extends RevDel_Item
{
554 public function __construct( $list, $row ) {
555 parent
::__construct( $list, $row );
556 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
559 public function getIdField() {
560 return 'oi_archive_name';
563 public function getTimestampField() {
564 return 'oi_timestamp';
567 public function getAuthorIdField() {
571 public function getAuthorNameField() {
572 return 'oi_user_text';
575 public function getId() {
576 $parts = explode( '!', $this->row
->oi_archive_name
);
580 public function canView() {
581 return $this->file
->userCan( File
::DELETED_RESTRICTED
, $this->list->getUser() );
584 public function canViewContent() {
585 return $this->file
->userCan( File
::DELETED_FILE
, $this->list->getUser() );
588 public function getBits() {
589 return $this->file
->getVisibility();
592 public function setBits( $bits ) {
594 # @todo FIXME: Move to LocalFile.php
595 if ( $this->isDeleted() ) {
596 if ( $bits & File
::DELETED_FILE
) {
600 $key = $this->file
->getStorageKey();
601 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
602 $this->list->storeBatch
[] = array(
603 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
605 $this->file
->getRel()
607 $this->list->cleanupBatch
[] = $key;
609 } elseif ( $bits & File
::DELETED_FILE
) {
611 $key = $this->file
->getStorageKey();
612 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
613 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
616 # Do the database operations
617 $dbw = wfGetDB( DB_MASTER
);
618 $dbw->update( 'oldimage',
619 array( 'oi_deleted' => $bits ),
621 'oi_name' => $this->row
->oi_name
,
622 'oi_timestamp' => $this->row
->oi_timestamp
,
623 'oi_deleted' => $this->getBits()
627 return (bool)$dbw->affectedRows();
630 public function isDeleted() {
631 return $this->file
->isDeleted( File
::DELETED_FILE
);
635 * Get the link to the file.
636 * Overridden by RevDel_ArchivedFileItem.
639 protected function getLink() {
640 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
641 $this->file
->getTimestamp(), $this->list->getUser() ) );
643 if ( !$this->isDeleted() ) {
645 return Html
::rawElement( 'a', array( 'href' => $this->file
->getUrl() ), $date );
649 if ( !$this->canViewContent() ) {
652 $link = Linker
::link(
653 SpecialPage
::getTitleFor( 'Revisiondelete' ),
657 'target' => $this->list->title
->getPrefixedText(),
658 'file' => $this->file
->getArchiveName(),
659 'token' => $this->list->getUser()->getEditToken(
660 $this->file
->getArchiveName() )
664 return '<span class="history-deleted">' . $link . '</span>';
667 * Generate a user tool link cluster if the current user is allowed to view it
668 * @return string HTML
670 protected function getUserTools() {
671 if ( $this->file
->userCan( Revision
::DELETED_USER
, $this->list->getUser() ) ) {
672 $link = Linker
::userLink( $this->file
->user
, $this->file
->user_text
) .
673 Linker
::userToolLinks( $this->file
->user
, $this->file
->user_text
);
675 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
677 if ( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
678 return '<span class="history-deleted">' . $link . '</span>';
684 * Wrap and format the file's comment block, if the current
685 * user is allowed to view it.
687 * @return string HTML
689 protected function getComment() {
690 if ( $this->file
->userCan( File
::DELETED_COMMENT
, $this->list->getUser() ) ) {
691 $block = Linker
::commentBlock( $this->file
->description
);
693 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
695 if ( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
696 return "<span class=\"history-deleted\">$block</span>";
701 public function getHTML() {
703 $this->list->msg( 'widthheight' )->numParams(
704 $this->file
->getWidth(), $this->file
->getHeight() )->text() .
705 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file
->getSize() )->text() . ')';
707 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
708 $data . ' ' . $this->getComment() . '</li>';
713 * List for filearchive table items
715 class RevDel_ArchivedFileList
extends RevDel_FileList
{
716 public function getType() {
717 return 'filearchive';
720 public static function getRelationType() {
725 * @param $db DatabaseBase
728 public function doQuery( $db ) {
729 $ids = array_map( 'intval', $this->ids
);
732 ArchivedFile
::selectFields(),
734 'fa_name' => $this->title
->getDBkey(),
738 array( 'ORDER BY' => 'fa_id DESC' )
742 public function newItem( $row ) {
743 return new RevDel_ArchivedFileItem( $this, $row );
748 * Item class for a filearchive table row
750 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
751 public function __construct( $list, $row ) {
752 RevDel_Item
::__construct( $list, $row );
753 $this->file
= ArchivedFile
::newFromRow( $row );
756 public function getIdField() {
760 public function getTimestampField() {
761 return 'fa_timestamp';
764 public function getAuthorIdField() {
768 public function getAuthorNameField() {
769 return 'fa_user_text';
772 public function getId() {
773 return $this->row
->fa_id
;
776 public function setBits( $bits ) {
777 $dbw = wfGetDB( DB_MASTER
);
778 $dbw->update( 'filearchive',
779 array( 'fa_deleted' => $bits ),
781 'fa_id' => $this->row
->fa_id
,
782 'fa_deleted' => $this->getBits(),
786 return (bool)$dbw->affectedRows();
789 protected function getLink() {
790 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
791 $this->file
->getTimestamp(), $this->list->getUser() ) );
794 if ( !$this->canViewContent() ) {
797 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
798 $key = $this->file
->getKey();
799 $link = Linker
::link( $undelete, $date, array(),
801 'target' => $this->list->title
->getPrefixedText(),
803 'token' => $this->list->getUser()->getEditToken( $key )
807 if ( $this->isDeleted() ) {
808 $link = '<span class="history-deleted">' . $link . '</span>';
815 * List for logging table items
817 class RevDel_LogList
extends RevDel_List
{
818 public function getType() {
822 public static function getRelationType() {
826 public static function getRestriction() {
827 return 'deletelogentry';
830 public static function getRevdelConstant() {
831 return LogPage
::DELETED_ACTION
;
834 public static function suggestTarget( $target, array $ids ) {
835 $result = wfGetDB( DB_SLAVE
)->select( 'logging',
837 array( 'log_id' => $ids ),
841 if ( $result->numRows() == 1 ) {
842 // If there's only one type, the target can be set to include it.
843 return SpecialPage
::getTitleFor( 'Log', $result->current()->log_type
);
845 return SpecialPage
::getTitleFor( 'Log' );
849 * @param $db DatabaseBase
852 public function doQuery( $db ) {
853 $ids = array_map( 'intval', $this->ids
);
854 return $db->select( 'logging', '*',
855 array( 'log_id' => $ids ),
857 array( 'ORDER BY' => 'log_id DESC' )
861 public function newItem( $row ) {
862 return new RevDel_LogItem( $this, $row );
865 public function getSuppressBit() {
866 return Revision
::DELETED_RESTRICTED
;
869 public function getLogAction() {
873 public function getLogParams( $params ) {
875 implode( ',', $params['ids'] ),
876 "ofield={$params['oldBits']}",
877 "nfield={$params['newBits']}"
883 * Item class for a logging table row
885 class RevDel_LogItem
extends RevDel_Item
{
886 public function getIdField() {
890 public function getTimestampField() {
891 return 'log_timestamp';
894 public function getAuthorIdField() {
898 public function getAuthorNameField() {
899 return 'log_user_text';
902 public function canView() {
903 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
, $this->list->getUser() );
906 public function canViewContent() {
910 public function getBits() {
911 return $this->row
->log_deleted
;
914 public function setBits( $bits ) {
915 $dbw = wfGetDB( DB_MASTER
);
916 $dbw->update( 'recentchanges',
918 'rc_deleted' => $bits,
922 'rc_logid' => $this->row
->log_id
,
923 'rc_timestamp' => $this->row
->log_timestamp
// index
927 $dbw->update( 'logging',
928 array( 'log_deleted' => $bits ),
930 'log_id' => $this->row
->log_id
,
931 'log_deleted' => $this->getBits()
935 return (bool)$dbw->affectedRows();
938 public function getHTML() {
939 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
940 $this->row
->log_timestamp
, $this->list->getUser() ) );
941 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
942 $formatter = LogFormatter
::newFromRow( $this->row
);
943 $formatter->setContext( $this->list->getContext() );
944 $formatter->setAudience( LogFormatter
::FOR_THIS_USER
);
946 // Log link for this page
947 $loglink = Linker
::link(
948 SpecialPage
::getTitleFor( 'Log' ),
949 $this->list->msg( 'log' )->escaped(),
951 array( 'page' => $title->getPrefixedText() )
953 $loglink = $this->list->msg( 'parentheses' )->rawParams( $loglink )->escaped();
954 // User links and action text
955 $action = $formatter->getActionText();
957 $comment = $this->list->getLanguage()->getDirMark() . Linker
::commentBlock( $this->row
->log_comment
);
958 if ( LogEventsList
::isDeleted( $this->row
, LogPage
::DELETED_COMMENT
) ) {
959 $comment = '<span class="history-deleted">' . $comment . '</span>';
962 return "<li>$loglink $date $action $comment</li>";