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();
248 $this->list->msg( 'diff' )->escaped(),
251 'diff' => $this->revision
->getId(),
259 public function getHTML() {
260 $difflink = $this->list->msg( 'parentheses' )
261 ->rawParams( $this->getDiffLink() )->escaped();
262 $revlink = $this->getRevisionLink();
263 $userlink = Linker
::revUserLink( $this->revision
);
264 $comment = Linker
::revComment( $this->revision
);
265 if ( $this->isDeleted() ) {
266 $revlink = "<span class=\"history-deleted\">$revlink</span>";
268 return "<li>$difflink $revlink $userlink $comment</li>";
273 * List for archive table items, i.e. revisions deleted via action=delete
275 class RevDel_ArchiveList
extends RevDel_RevisionList
{
276 public function getType() {
280 public static function getRelationType() {
281 return 'ar_timestamp';
285 * @param $db DatabaseBase
288 public function doQuery( $db ) {
289 $timestamps = array();
290 foreach ( $this->ids
as $id ) {
291 $timestamps[] = $db->timestamp( $id );
293 return $db->select( 'archive', '*',
295 'ar_namespace' => $this->title
->getNamespace(),
296 'ar_title' => $this->title
->getDBkey(),
297 'ar_timestamp' => $timestamps
300 array( 'ORDER BY' => 'ar_timestamp DESC' )
304 public function newItem( $row ) {
305 return new RevDel_ArchiveItem( $this, $row );
308 public function doPreCommitUpdates() {
309 return Status
::newGood();
312 public function doPostCommitUpdates() {
313 return Status
::newGood();
318 * Item class for a archive table row
320 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
321 public function __construct( $list, $row ) {
322 RevDel_Item
::__construct( $list, $row );
323 $this->revision
= Revision
::newFromArchiveRow( $row,
324 array( 'page' => $this->list->title
->getArticleID() ) );
327 public function getIdField() {
328 return 'ar_timestamp';
331 public function getTimestampField() {
332 return 'ar_timestamp';
335 public function getAuthorIdField() {
339 public function getAuthorNameField() {
340 return 'ar_user_text';
343 public function getId() {
344 # Convert DB timestamp to MW timestamp
345 return $this->revision
->getTimestamp();
348 public function setBits( $bits ) {
349 $dbw = wfGetDB( DB_MASTER
);
350 $dbw->update( 'archive',
351 array( 'ar_deleted' => $bits ),
353 'ar_namespace' => $this->list->title
->getNamespace(),
354 'ar_title' => $this->list->title
->getDBkey(),
355 // use timestamp for index
356 'ar_timestamp' => $this->row
->ar_timestamp
,
357 'ar_rev_id' => $this->row
->ar_rev_id
,
358 'ar_deleted' => $this->getBits()
361 return (bool)$dbw->affectedRows();
364 protected function getRevisionLink() {
365 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
366 $this->revision
->getTimestamp(), $this->list->getUser() ) );
368 if ( $this->isDeleted() && !$this->canViewContent() ) {
373 SpecialPage
::getTitleFor( 'Undelete' ),
377 'target' => $this->list->title
->getPrefixedText(),
378 'timestamp' => $this->revision
->getTimestamp()
383 protected function getDiffLink() {
384 if ( $this->isDeleted() && !$this->canViewContent() ) {
385 return $this->list->msg( 'diff' )->escaped();
389 SpecialPage
::getTitleFor( 'Undelete' ),
390 $this->list->msg( 'diff' )->escaped(),
393 'target' => $this->list->title
->getPrefixedText(),
395 'timestamp' => $this->revision
->getTimestamp()
403 * Item class for a archive table row by ar_rev_id -- actually
404 * used via RevDel_RevisionList.
406 class RevDel_ArchivedRevisionItem
extends RevDel_ArchiveItem
{
407 public function __construct( $list, $row ) {
408 RevDel_Item
::__construct( $list, $row );
410 $this->revision
= Revision
::newFromArchiveRow( $row,
411 array( 'page' => $this->list->title
->getArticleID() ) );
414 public function getIdField() {
418 public function getId() {
419 return $this->revision
->getId();
422 public function setBits( $bits ) {
423 $dbw = wfGetDB( DB_MASTER
);
424 $dbw->update( 'archive',
425 array( 'ar_deleted' => $bits ),
426 array( 'ar_rev_id' => $this->row
->ar_rev_id
,
427 'ar_deleted' => $this->getBits()
430 return (bool)$dbw->affectedRows();
435 * List for oldimage table items
437 class RevDel_FileList
extends RevDel_List
{
438 public function getType() {
442 public static function getRelationType() {
443 return 'oi_archive_name';
446 var $storeBatch, $deleteBatch, $cleanupBatch;
449 * @param $db DatabaseBase
452 public function doQuery( $db ) {
453 $archiveNames = array();
454 foreach( $this->ids
as $timestamp ) {
455 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
459 OldLocalFile
::selectFields(),
461 'oi_name' => $this->title
->getDBkey(),
462 'oi_archive_name' => $archiveNames
465 array( 'ORDER BY' => 'oi_timestamp DESC' )
469 public function newItem( $row ) {
470 return new RevDel_FileItem( $this, $row );
473 public function clearFileOps() {
474 $this->deleteBatch
= array();
475 $this->storeBatch
= array();
476 $this->cleanupBatch
= array();
479 public function doPreCommitUpdates() {
480 $status = Status
::newGood();
481 $repo = RepoGroup
::singleton()->getLocalRepo();
482 if ( $this->storeBatch
) {
483 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
485 if ( !$status->isOK() ) {
488 if ( $this->deleteBatch
) {
489 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
491 if ( !$status->isOK() ) {
492 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
493 // modified (but destined for rollback) causes data loss
496 if ( $this->cleanupBatch
) {
497 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
502 public function doPostCommitUpdates() {
503 $file = wfLocalFile( $this->title
);
505 $file->purgeDescription();
506 return Status
::newGood();
509 public function getSuppressBit() {
510 return File
::DELETED_RESTRICTED
;
515 * Item class for an oldimage table row
517 class RevDel_FileItem
extends RevDel_Item
{
524 public function __construct( $list, $row ) {
525 parent
::__construct( $list, $row );
526 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
529 public function getIdField() {
530 return 'oi_archive_name';
533 public function getTimestampField() {
534 return 'oi_timestamp';
537 public function getAuthorIdField() {
541 public function getAuthorNameField() {
542 return 'oi_user_text';
545 public function getId() {
546 $parts = explode( '!', $this->row
->oi_archive_name
);
550 public function canView() {
551 return $this->file
->userCan( File
::DELETED_RESTRICTED
, $this->list->getUser() );
554 public function canViewContent() {
555 return $this->file
->userCan( File
::DELETED_FILE
, $this->list->getUser() );
558 public function getBits() {
559 return $this->file
->getVisibility();
562 public function setBits( $bits ) {
564 # @todo FIXME: Move to LocalFile.php
565 if ( $this->isDeleted() ) {
566 if ( $bits & File
::DELETED_FILE
) {
570 $key = $this->file
->getStorageKey();
571 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
572 $this->list->storeBatch
[] = array(
573 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
575 $this->file
->getRel()
577 $this->list->cleanupBatch
[] = $key;
579 } elseif ( $bits & File
::DELETED_FILE
) {
581 $key = $this->file
->getStorageKey();
582 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
583 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
586 # Do the database operations
587 $dbw = wfGetDB( DB_MASTER
);
588 $dbw->update( 'oldimage',
589 array( 'oi_deleted' => $bits ),
591 'oi_name' => $this->row
->oi_name
,
592 'oi_timestamp' => $this->row
->oi_timestamp
,
593 'oi_deleted' => $this->getBits()
597 return (bool)$dbw->affectedRows();
600 public function isDeleted() {
601 return $this->file
->isDeleted( File
::DELETED_FILE
);
605 * Get the link to the file.
606 * Overridden by RevDel_ArchivedFileItem.
609 protected function getLink() {
610 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
611 $this->file
->getTimestamp(), $this->list->getUser() ) );
613 if ( !$this->isDeleted() ) {
615 return Html
::rawElement( 'a', array( 'href' => $this->file
->getUrl() ), $date );
619 if ( !$this->canViewContent() ) {
622 $link = Linker
::link(
623 SpecialPage
::getTitleFor( 'Revisiondelete' ),
627 'target' => $this->list->title
->getPrefixedText(),
628 'file' => $this->file
->getArchiveName(),
629 'token' => $this->list->getUser()->getEditToken(
630 $this->file
->getArchiveName() )
634 return '<span class="history-deleted">' . $link . '</span>';
637 * Generate a user tool link cluster if the current user is allowed to view it
638 * @return string HTML
640 protected function getUserTools() {
641 if( $this->file
->userCan( Revision
::DELETED_USER
, $this->list->getUser() ) ) {
642 $link = Linker
::userLink( $this->file
->user
, $this->file
->user_text
) .
643 Linker
::userToolLinks( $this->file
->user
, $this->file
->user_text
);
645 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
647 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
648 return '<span class="history-deleted">' . $link . '</span>';
654 * Wrap and format the file's comment block, if the current
655 * user is allowed to view it.
657 * @return string HTML
659 protected function getComment() {
660 if( $this->file
->userCan( File
::DELETED_COMMENT
, $this->list->getUser() ) ) {
661 $block = Linker
::commentBlock( $this->file
->description
);
663 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
665 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
666 return "<span class=\"history-deleted\">$block</span>";
671 public function getHTML() {
673 $this->list->msg( 'widthheight' )->numParams(
674 $this->file
->getWidth(), $this->file
->getHeight() )->text() .
675 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file
->getSize() )->text() . ')';
677 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
678 $data . ' ' . $this->getComment(). '</li>';
683 * List for filearchive table items
685 class RevDel_ArchivedFileList
extends RevDel_FileList
{
686 public function getType() {
687 return 'filearchive';
690 public static function getRelationType() {
695 * @param $db DatabaseBase
698 public function doQuery( $db ) {
699 $ids = array_map( 'intval', $this->ids
);
702 ArchivedFile
::selectFields(),
704 'fa_name' => $this->title
->getDBkey(),
708 array( 'ORDER BY' => 'fa_id DESC' )
712 public function newItem( $row ) {
713 return new RevDel_ArchivedFileItem( $this, $row );
718 * Item class for a filearchive table row
720 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
721 public function __construct( $list, $row ) {
722 RevDel_Item
::__construct( $list, $row );
723 $this->file
= ArchivedFile
::newFromRow( $row );
726 public function getIdField() {
730 public function getTimestampField() {
731 return 'fa_timestamp';
734 public function getAuthorIdField() {
738 public function getAuthorNameField() {
739 return 'fa_user_text';
742 public function getId() {
743 return $this->row
->fa_id
;
746 public function setBits( $bits ) {
747 $dbw = wfGetDB( DB_MASTER
);
748 $dbw->update( 'filearchive',
749 array( 'fa_deleted' => $bits ),
751 'fa_id' => $this->row
->fa_id
,
752 'fa_deleted' => $this->getBits(),
756 return (bool)$dbw->affectedRows();
759 protected function getLink() {
760 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
761 $this->file
->getTimestamp(), $this->list->getUser() ) );
764 if( !$this->canViewContent() ) {
767 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
768 $key = $this->file
->getKey();
769 $link = Linker
::link( $undelete, $date, array(),
771 'target' => $this->list->title
->getPrefixedText(),
773 'token' => $this->list->getUser()->getEditToken( $key )
777 if( $this->isDeleted() ) {
778 $link = '<span class="history-deleted">' . $link . '</span>';
785 * List for logging table items
787 class RevDel_LogList
extends RevDel_List
{
788 public function getType() {
792 public static function getRelationType() {
797 * @param $db DatabaseBase
800 public function doQuery( $db ) {
801 $ids = array_map( 'intval', $this->ids
);
802 return $db->select( 'logging', '*',
803 array( 'log_id' => $ids ),
805 array( 'ORDER BY' => 'log_id DESC' )
809 public function newItem( $row ) {
810 return new RevDel_LogItem( $this, $row );
813 public function getSuppressBit() {
814 return Revision
::DELETED_RESTRICTED
;
817 public function getLogAction() {
821 public function getLogParams( $params ) {
823 implode( ',', $params['ids'] ),
824 "ofield={$params['oldBits']}",
825 "nfield={$params['newBits']}"
831 * Item class for a logging table row
833 class RevDel_LogItem
extends RevDel_Item
{
834 public function getIdField() {
838 public function getTimestampField() {
839 return 'log_timestamp';
842 public function getAuthorIdField() {
846 public function getAuthorNameField() {
847 return 'log_user_text';
850 public function canView() {
851 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
, $this->list->getUser() );
854 public function canViewContent() {
858 public function getBits() {
859 return $this->row
->log_deleted
;
862 public function setBits( $bits ) {
863 $dbw = wfGetDB( DB_MASTER
);
864 $dbw->update( 'recentchanges',
866 'rc_deleted' => $bits,
870 'rc_logid' => $this->row
->log_id
,
871 'rc_timestamp' => $this->row
->log_timestamp
// index
875 $dbw->update( 'logging',
876 array( 'log_deleted' => $bits ),
878 'log_id' => $this->row
->log_id
,
879 'log_deleted' => $this->getBits()
883 return (bool)$dbw->affectedRows();
886 public function getHTML() {
887 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
888 $this->row
->log_timestamp
, $this->list->getUser() ) );
889 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
890 $formatter = LogFormatter
::newFromRow( $this->row
);
891 $formatter->setContext( $this->list->getContext() );
892 $formatter->setAudience( LogFormatter
::FOR_THIS_USER
);
894 // Log link for this page
895 $loglink = Linker
::link(
896 SpecialPage
::getTitleFor( 'Log' ),
897 $this->list->msg( 'log' )->escaped(),
899 array( 'page' => $title->getPrefixedText() )
901 $loglink = $this->list->msg( 'parentheses' )->rawParams( $loglink )->escaped();
902 // User links and action text
903 $action = $formatter->getActionText();
905 $comment = $this->list->getLanguage()->getDirMark() . Linker
::commentBlock( $this->row
->log_comment
);
906 if( LogEventsList
::isDeleted( $this->row
, LogPage
::DELETED_COMMENT
) ) {
907 $comment = '<span class="history-deleted">' . $comment . '</span>';
910 return "<li>$loglink $date $action $comment</li>";