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() {
45 * @param $db DatabaseBase
48 public function doQuery( $db ) {
49 $ids = array_map( 'intval', $this->ids
);
51 array( 'revision', 'page', 'user' ),
52 array_merge( Revision
::selectFields(), Revision
::selectUserFields() ),
54 'rev_page' => $this->title
->getArticleID(),
58 array( 'ORDER BY' => 'rev_id DESC' ),
60 'page' => Revision
::pageJoinCond(),
61 'user' => Revision
::userJoinCond() )
64 if ( $live->numRows() >= count( $ids ) ) {
65 // All requested revisions are live, keeps things simple!
69 // Check if any requested revisions are available fully deleted.
70 $archived = $db->select( array( 'archive' ), '*',
75 array( 'ORDER BY' => 'ar_rev_id DESC' )
78 if ( $archived->numRows() == 0 ) {
80 } elseif ( $live->numRows() == 0 ) {
83 // Combine the two! Whee
85 foreach ( $live as $row ) {
86 $rows[$row->rev_id
] = $row;
88 foreach ( $archived as $row ) {
89 $rows[$row->ar_rev_id
] = $row;
92 return new FakeResultWrapper( array_values( $rows ) );
96 public function newItem( $row ) {
97 if ( isset( $row->rev_id
) ) {
98 return new RevDel_RevisionItem( $this, $row );
99 } elseif ( isset( $row->ar_rev_id
) ) {
100 return new RevDel_ArchivedRevisionItem( $this, $row );
102 // This shouldn't happen. :)
103 throw new MWException( 'Invalid row type in RevDel_RevisionList' );
107 public function getCurrent() {
108 if ( is_null( $this->currentRevId
) ) {
109 $dbw = wfGetDB( DB_MASTER
);
110 $this->currentRevId
= $dbw->selectField(
111 'page', 'page_latest', $this->title
->pageCond(), __METHOD__
);
113 return $this->currentRevId
;
116 public function getSuppressBit() {
117 return Revision
::DELETED_RESTRICTED
;
120 public function doPreCommitUpdates() {
121 $this->title
->invalidateCache();
122 return Status
::newGood();
125 public function doPostCommitUpdates() {
126 $this->title
->purgeSquid();
127 // Extensions that require referencing previous revisions may need this
128 wfRunHooks( 'ArticleRevisionVisibilitySet', array( &$this->title
) );
129 return Status
::newGood();
134 * Item class for a live revision table row
136 class RevDel_RevisionItem
extends RevDel_Item
{
139 public function __construct( $list, $row ) {
140 parent
::__construct( $list, $row );
141 $this->revision
= new Revision( $row );
144 public function getIdField() {
148 public function getTimestampField() {
149 return 'rev_timestamp';
152 public function getAuthorIdField() {
156 public function getAuthorNameField() {
157 return 'user_name'; // see Revision::selectUserFields()
160 public function canView() {
161 return $this->revision
->userCan( Revision
::DELETED_RESTRICTED
, $this->list->getUser() );
164 public function canViewContent() {
165 return $this->revision
->userCan( Revision
::DELETED_TEXT
, $this->list->getUser() );
168 public function getBits() {
169 return $this->revision
->getVisibility();
172 public function setBits( $bits ) {
173 $dbw = wfGetDB( DB_MASTER
);
174 // Update revision table
175 $dbw->update( 'revision',
176 array( 'rev_deleted' => $bits ),
178 'rev_id' => $this->revision
->getId(),
179 'rev_page' => $this->revision
->getPage(),
180 'rev_deleted' => $this->getBits()
184 if ( !$dbw->affectedRows() ) {
188 // Update recentchanges table
189 $dbw->update( 'recentchanges',
191 'rc_deleted' => $bits,
195 'rc_this_oldid' => $this->revision
->getId(), // condition
196 // non-unique timestamp index
197 'rc_timestamp' => $dbw->timestamp( $this->revision
->getTimestamp() ),
204 public function isDeleted() {
205 return $this->revision
->isDeleted( Revision
::DELETED_TEXT
);
208 public function isHideCurrentOp( $newBits ) {
209 return ( $newBits & Revision
::DELETED_TEXT
)
210 && $this->list->getCurrent() == $this->getId();
214 * Get the HTML link to the revision text.
215 * Overridden by RevDel_ArchiveItem.
218 protected function getRevisionLink() {
219 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
220 $this->revision
->getTimestamp(), $this->list->getUser() ) );
222 if ( $this->isDeleted() && !$this->canViewContent() ) {
225 return Linker
::linkKnown(
230 'oldid' => $this->revision
->getId(),
237 * Get the HTML link to the diff.
238 * Overridden by RevDel_ArchiveItem
241 protected function getDiffLink() {
242 if ( $this->isDeleted() && !$this->canViewContent() ) {
243 return $this->list->msg( 'diff' )->escaped();
245 return Linker
::linkKnown(
247 $this->list->msg( 'diff' )->escaped(),
250 'diff' => $this->revision
->getId(),
258 public function getHTML() {
259 $difflink = $this->list->msg( 'parentheses' )
260 ->rawParams( $this->getDiffLink() )->escaped();
261 $revlink = $this->getRevisionLink();
262 $userlink = Linker
::revUserLink( $this->revision
);
263 $comment = Linker
::revComment( $this->revision
);
264 if ( $this->isDeleted() ) {
265 $revlink = "<span class=\"history-deleted\">$revlink</span>";
267 return "<li>$difflink $revlink $userlink $comment</li>";
272 * List for archive table items, i.e. revisions deleted via action=delete
274 class RevDel_ArchiveList
extends RevDel_RevisionList
{
275 public function getType() {
279 public static function getRelationType() {
280 return 'ar_timestamp';
284 * @param $db DatabaseBase
287 public function doQuery( $db ) {
288 $timestamps = array();
289 foreach ( $this->ids
as $id ) {
290 $timestamps[] = $db->timestamp( $id );
292 return $db->select( 'archive', '*',
294 'ar_namespace' => $this->title
->getNamespace(),
295 'ar_title' => $this->title
->getDBkey(),
296 'ar_timestamp' => $timestamps
299 array( 'ORDER BY' => 'ar_timestamp DESC' )
303 public function newItem( $row ) {
304 return new RevDel_ArchiveItem( $this, $row );
307 public function doPreCommitUpdates() {
308 return Status
::newGood();
311 public function doPostCommitUpdates() {
312 return Status
::newGood();
317 * Item class for a archive table row
319 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
320 public function __construct( $list, $row ) {
321 RevDel_Item
::__construct( $list, $row );
322 $this->revision
= Revision
::newFromArchiveRow( $row,
323 array( 'page' => $this->list->title
->getArticleID() ) );
326 public function getIdField() {
327 return 'ar_timestamp';
330 public function getTimestampField() {
331 return 'ar_timestamp';
334 public function getAuthorIdField() {
338 public function getAuthorNameField() {
339 return 'ar_user_text';
342 public function getId() {
343 # Convert DB timestamp to MW timestamp
344 return $this->revision
->getTimestamp();
347 public function setBits( $bits ) {
348 $dbw = wfGetDB( DB_MASTER
);
349 $dbw->update( 'archive',
350 array( 'ar_deleted' => $bits ),
352 'ar_namespace' => $this->list->title
->getNamespace(),
353 'ar_title' => $this->list->title
->getDBkey(),
354 // use timestamp for index
355 'ar_timestamp' => $this->row
->ar_timestamp
,
356 'ar_rev_id' => $this->row
->ar_rev_id
,
357 'ar_deleted' => $this->getBits()
360 return (bool)$dbw->affectedRows();
363 protected function getRevisionLink() {
364 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
365 $this->revision
->getTimestamp(), $this->list->getUser() ) );
367 if ( $this->isDeleted() && !$this->canViewContent() ) {
372 SpecialPage
::getTitleFor( 'Undelete' ),
376 'target' => $this->list->title
->getPrefixedText(),
377 'timestamp' => $this->revision
->getTimestamp()
382 protected function getDiffLink() {
383 if ( $this->isDeleted() && !$this->canViewContent() ) {
384 return $this->list->msg( 'diff' )->escaped();
388 SpecialPage
::getTitleFor( 'Undelete' ),
389 $this->list->msg( 'diff' )->escaped(),
392 'target' => $this->list->title
->getPrefixedText(),
394 'timestamp' => $this->revision
->getTimestamp()
401 * Item class for a archive table row by ar_rev_id -- actually
402 * used via RevDel_RevisionList.
404 class RevDel_ArchivedRevisionItem
extends RevDel_ArchiveItem
{
405 public function __construct( $list, $row ) {
406 RevDel_Item
::__construct( $list, $row );
408 $this->revision
= Revision
::newFromArchiveRow( $row,
409 array( 'page' => $this->list->title
->getArticleID() ) );
412 public function getIdField() {
416 public function getId() {
417 return $this->revision
->getId();
420 public function setBits( $bits ) {
421 $dbw = wfGetDB( DB_MASTER
);
422 $dbw->update( 'archive',
423 array( 'ar_deleted' => $bits ),
424 array( 'ar_rev_id' => $this->row
->ar_rev_id
,
425 'ar_deleted' => $this->getBits()
428 return (bool)$dbw->affectedRows();
433 * List for oldimage table items
435 class RevDel_FileList
extends RevDel_List
{
436 public function getType() {
440 public static function getRelationType() {
441 return 'oi_archive_name';
444 var $storeBatch, $deleteBatch, $cleanupBatch;
447 * @param $db DatabaseBase
450 public function doQuery( $db ) {
451 $archiveNames = array();
452 foreach ( $this->ids
as $timestamp ) {
453 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
457 OldLocalFile
::selectFields(),
459 'oi_name' => $this->title
->getDBkey(),
460 'oi_archive_name' => $archiveNames
463 array( 'ORDER BY' => 'oi_timestamp DESC' )
467 public function newItem( $row ) {
468 return new RevDel_FileItem( $this, $row );
471 public function clearFileOps() {
472 $this->deleteBatch
= array();
473 $this->storeBatch
= array();
474 $this->cleanupBatch
= array();
477 public function doPreCommitUpdates() {
478 $status = Status
::newGood();
479 $repo = RepoGroup
::singleton()->getLocalRepo();
480 if ( $this->storeBatch
) {
481 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
483 if ( !$status->isOK() ) {
486 if ( $this->deleteBatch
) {
487 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
489 if ( !$status->isOK() ) {
490 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
491 // modified (but destined for rollback) causes data loss
494 if ( $this->cleanupBatch
) {
495 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
500 public function doPostCommitUpdates() {
501 $file = wfLocalFile( $this->title
);
503 $file->purgeDescription();
504 return Status
::newGood();
507 public function getSuppressBit() {
508 return File
::DELETED_RESTRICTED
;
513 * Item class for an oldimage table row
515 class RevDel_FileItem
extends RevDel_Item
{
522 public function __construct( $list, $row ) {
523 parent
::__construct( $list, $row );
524 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
527 public function getIdField() {
528 return 'oi_archive_name';
531 public function getTimestampField() {
532 return 'oi_timestamp';
535 public function getAuthorIdField() {
539 public function getAuthorNameField() {
540 return 'oi_user_text';
543 public function getId() {
544 $parts = explode( '!', $this->row
->oi_archive_name
);
548 public function canView() {
549 return $this->file
->userCan( File
::DELETED_RESTRICTED
, $this->list->getUser() );
552 public function canViewContent() {
553 return $this->file
->userCan( File
::DELETED_FILE
, $this->list->getUser() );
556 public function getBits() {
557 return $this->file
->getVisibility();
560 public function setBits( $bits ) {
562 # @todo FIXME: Move to LocalFile.php
563 if ( $this->isDeleted() ) {
564 if ( $bits & File
::DELETED_FILE
) {
568 $key = $this->file
->getStorageKey();
569 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
570 $this->list->storeBatch
[] = array(
571 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
573 $this->file
->getRel()
575 $this->list->cleanupBatch
[] = $key;
577 } elseif ( $bits & File
::DELETED_FILE
) {
579 $key = $this->file
->getStorageKey();
580 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
581 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
584 # Do the database operations
585 $dbw = wfGetDB( DB_MASTER
);
586 $dbw->update( 'oldimage',
587 array( 'oi_deleted' => $bits ),
589 'oi_name' => $this->row
->oi_name
,
590 'oi_timestamp' => $this->row
->oi_timestamp
,
591 'oi_deleted' => $this->getBits()
595 return (bool)$dbw->affectedRows();
598 public function isDeleted() {
599 return $this->file
->isDeleted( File
::DELETED_FILE
);
603 * Get the link to the file.
604 * Overridden by RevDel_ArchivedFileItem.
607 protected function getLink() {
608 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
609 $this->file
->getTimestamp(), $this->list->getUser() ) );
611 if ( !$this->isDeleted() ) {
613 return Html
::rawElement( 'a', array( 'href' => $this->file
->getUrl() ), $date );
617 if ( !$this->canViewContent() ) {
620 $link = Linker
::link(
621 SpecialPage
::getTitleFor( 'Revisiondelete' ),
625 'target' => $this->list->title
->getPrefixedText(),
626 'file' => $this->file
->getArchiveName(),
627 'token' => $this->list->getUser()->getEditToken(
628 $this->file
->getArchiveName() )
632 return '<span class="history-deleted">' . $link . '</span>';
635 * Generate a user tool link cluster if the current user is allowed to view it
636 * @return string HTML
638 protected function getUserTools() {
639 if ( $this->file
->userCan( Revision
::DELETED_USER
, $this->list->getUser() ) ) {
640 $link = Linker
::userLink( $this->file
->user
, $this->file
->user_text
) .
641 Linker
::userToolLinks( $this->file
->user
, $this->file
->user_text
);
643 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
645 if ( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
646 return '<span class="history-deleted">' . $link . '</span>';
652 * Wrap and format the file's comment block, if the current
653 * user is allowed to view it.
655 * @return string HTML
657 protected function getComment() {
658 if ( $this->file
->userCan( File
::DELETED_COMMENT
, $this->list->getUser() ) ) {
659 $block = Linker
::commentBlock( $this->file
->description
);
661 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
663 if ( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
664 return "<span class=\"history-deleted\">$block</span>";
669 public function getHTML() {
671 $this->list->msg( 'widthheight' )->numParams(
672 $this->file
->getWidth(), $this->file
->getHeight() )->text() .
673 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file
->getSize() )->text() . ')';
675 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
676 $data . ' ' . $this->getComment() . '</li>';
681 * List for filearchive table items
683 class RevDel_ArchivedFileList
extends RevDel_FileList
{
684 public function getType() {
685 return 'filearchive';
688 public static function getRelationType() {
693 * @param $db DatabaseBase
696 public function doQuery( $db ) {
697 $ids = array_map( 'intval', $this->ids
);
700 ArchivedFile
::selectFields(),
702 'fa_name' => $this->title
->getDBkey(),
706 array( 'ORDER BY' => 'fa_id DESC' )
710 public function newItem( $row ) {
711 return new RevDel_ArchivedFileItem( $this, $row );
716 * Item class for a filearchive table row
718 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
719 public function __construct( $list, $row ) {
720 RevDel_Item
::__construct( $list, $row );
721 $this->file
= ArchivedFile
::newFromRow( $row );
724 public function getIdField() {
728 public function getTimestampField() {
729 return 'fa_timestamp';
732 public function getAuthorIdField() {
736 public function getAuthorNameField() {
737 return 'fa_user_text';
740 public function getId() {
741 return $this->row
->fa_id
;
744 public function setBits( $bits ) {
745 $dbw = wfGetDB( DB_MASTER
);
746 $dbw->update( 'filearchive',
747 array( 'fa_deleted' => $bits ),
749 'fa_id' => $this->row
->fa_id
,
750 'fa_deleted' => $this->getBits(),
754 return (bool)$dbw->affectedRows();
757 protected function getLink() {
758 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
759 $this->file
->getTimestamp(), $this->list->getUser() ) );
762 if ( !$this->canViewContent() ) {
765 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
766 $key = $this->file
->getKey();
767 $link = Linker
::link( $undelete, $date, array(),
769 'target' => $this->list->title
->getPrefixedText(),
771 'token' => $this->list->getUser()->getEditToken( $key )
775 if ( $this->isDeleted() ) {
776 $link = '<span class="history-deleted">' . $link . '</span>';
783 * List for logging table items
785 class RevDel_LogList
extends RevDel_List
{
786 public function getType() {
790 public static function getRelationType() {
795 * @param $db DatabaseBase
798 public function doQuery( $db ) {
799 $ids = array_map( 'intval', $this->ids
);
800 return $db->select( 'logging', '*',
801 array( 'log_id' => $ids ),
803 array( 'ORDER BY' => 'log_id DESC' )
807 public function newItem( $row ) {
808 return new RevDel_LogItem( $this, $row );
811 public function getSuppressBit() {
812 return Revision
::DELETED_RESTRICTED
;
815 public function getLogAction() {
819 public function getLogParams( $params ) {
821 implode( ',', $params['ids'] ),
822 "ofield={$params['oldBits']}",
823 "nfield={$params['newBits']}"
829 * Item class for a logging table row
831 class RevDel_LogItem
extends RevDel_Item
{
832 public function getIdField() {
836 public function getTimestampField() {
837 return 'log_timestamp';
840 public function getAuthorIdField() {
844 public function getAuthorNameField() {
845 return 'log_user_text';
848 public function canView() {
849 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
, $this->list->getUser() );
852 public function canViewContent() {
856 public function getBits() {
857 return $this->row
->log_deleted
;
860 public function setBits( $bits ) {
861 $dbw = wfGetDB( DB_MASTER
);
862 $dbw->update( 'recentchanges',
864 'rc_deleted' => $bits,
868 'rc_logid' => $this->row
->log_id
,
869 'rc_timestamp' => $this->row
->log_timestamp
// index
873 $dbw->update( 'logging',
874 array( 'log_deleted' => $bits ),
876 'log_id' => $this->row
->log_id
,
877 'log_deleted' => $this->getBits()
881 return (bool)$dbw->affectedRows();
884 public function getHTML() {
885 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
886 $this->row
->log_timestamp
, $this->list->getUser() ) );
887 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
888 $formatter = LogFormatter
::newFromRow( $this->row
);
889 $formatter->setContext( $this->list->getContext() );
890 $formatter->setAudience( LogFormatter
::FOR_THIS_USER
);
892 // Log link for this page
893 $loglink = Linker
::link(
894 SpecialPage
::getTitleFor( 'Log' ),
895 $this->list->msg( 'log' )->escaped(),
897 array( 'page' => $title->getPrefixedText() )
899 $loglink = $this->list->msg( 'parentheses' )->rawParams( $loglink )->escaped();
900 // User links and action text
901 $action = $formatter->getActionText();
903 $comment = $this->list->getLanguage()->getDirMark() . Linker
::commentBlock( $this->row
->log_comment
);
904 if ( LogEventsList
::isDeleted( $this->row
, LogPage
::DELETED_COMMENT
) ) {
905 $comment = '<span class="history-deleted">' . $comment . '</span>';
908 return "<li>$loglink $date $action $comment</li>";