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()
402 * Item class for a archive table row by ar_rev_id -- actually
403 * used via RevDel_RevisionList.
405 class RevDel_ArchivedRevisionItem
extends RevDel_ArchiveItem
{
406 public function __construct( $list, $row ) {
407 RevDel_Item
::__construct( $list, $row );
409 $this->revision
= Revision
::newFromArchiveRow( $row,
410 array( 'page' => $this->list->title
->getArticleID() ) );
413 public function getIdField() {
417 public function getId() {
418 return $this->revision
->getId();
421 public function setBits( $bits ) {
422 $dbw = wfGetDB( DB_MASTER
);
423 $dbw->update( 'archive',
424 array( 'ar_deleted' => $bits ),
425 array( 'ar_rev_id' => $this->row
->ar_rev_id
,
426 'ar_deleted' => $this->getBits()
429 return (bool)$dbw->affectedRows();
434 * List for oldimage table items
436 class RevDel_FileList
extends RevDel_List
{
437 public function getType() {
441 public static function getRelationType() {
442 return 'oi_archive_name';
445 var $storeBatch, $deleteBatch, $cleanupBatch;
448 * @param $db DatabaseBase
451 public function doQuery( $db ) {
452 $archiveNames = array();
453 foreach( $this->ids
as $timestamp ) {
454 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
458 OldLocalFile
::selectFields(),
460 'oi_name' => $this->title
->getDBkey(),
461 'oi_archive_name' => $archiveNames
464 array( 'ORDER BY' => 'oi_timestamp DESC' )
468 public function newItem( $row ) {
469 return new RevDel_FileItem( $this, $row );
472 public function clearFileOps() {
473 $this->deleteBatch
= array();
474 $this->storeBatch
= array();
475 $this->cleanupBatch
= array();
478 public function doPreCommitUpdates() {
479 $status = Status
::newGood();
480 $repo = RepoGroup
::singleton()->getLocalRepo();
481 if ( $this->storeBatch
) {
482 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
484 if ( !$status->isOK() ) {
487 if ( $this->deleteBatch
) {
488 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
490 if ( !$status->isOK() ) {
491 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
492 // modified (but destined for rollback) causes data loss
495 if ( $this->cleanupBatch
) {
496 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
501 public function doPostCommitUpdates() {
502 $file = wfLocalFile( $this->title
);
504 $file->purgeDescription();
505 return Status
::newGood();
508 public function getSuppressBit() {
509 return File
::DELETED_RESTRICTED
;
514 * Item class for an oldimage table row
516 class RevDel_FileItem
extends RevDel_Item
{
523 public function __construct( $list, $row ) {
524 parent
::__construct( $list, $row );
525 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
528 public function getIdField() {
529 return 'oi_archive_name';
532 public function getTimestampField() {
533 return 'oi_timestamp';
536 public function getAuthorIdField() {
540 public function getAuthorNameField() {
541 return 'oi_user_text';
544 public function getId() {
545 $parts = explode( '!', $this->row
->oi_archive_name
);
549 public function canView() {
550 return $this->file
->userCan( File
::DELETED_RESTRICTED
, $this->list->getUser() );
553 public function canViewContent() {
554 return $this->file
->userCan( File
::DELETED_FILE
, $this->list->getUser() );
557 public function getBits() {
558 return $this->file
->getVisibility();
561 public function setBits( $bits ) {
563 # @todo FIXME: Move to LocalFile.php
564 if ( $this->isDeleted() ) {
565 if ( $bits & File
::DELETED_FILE
) {
569 $key = $this->file
->getStorageKey();
570 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
571 $this->list->storeBatch
[] = array(
572 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
574 $this->file
->getRel()
576 $this->list->cleanupBatch
[] = $key;
578 } elseif ( $bits & File
::DELETED_FILE
) {
580 $key = $this->file
->getStorageKey();
581 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
582 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
585 # Do the database operations
586 $dbw = wfGetDB( DB_MASTER
);
587 $dbw->update( 'oldimage',
588 array( 'oi_deleted' => $bits ),
590 'oi_name' => $this->row
->oi_name
,
591 'oi_timestamp' => $this->row
->oi_timestamp
,
592 'oi_deleted' => $this->getBits()
596 return (bool)$dbw->affectedRows();
599 public function isDeleted() {
600 return $this->file
->isDeleted( File
::DELETED_FILE
);
604 * Get the link to the file.
605 * Overridden by RevDel_ArchivedFileItem.
608 protected function getLink() {
609 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
610 $this->file
->getTimestamp(), $this->list->getUser() ) );
612 if ( !$this->isDeleted() ) {
614 return Html
::rawElement( 'a', array( 'href' => $this->file
->getUrl() ), $date );
618 if ( !$this->canViewContent() ) {
621 $link = Linker
::link(
622 SpecialPage
::getTitleFor( 'Revisiondelete' ),
626 'target' => $this->list->title
->getPrefixedText(),
627 'file' => $this->file
->getArchiveName(),
628 'token' => $this->list->getUser()->getEditToken(
629 $this->file
->getArchiveName() )
633 return '<span class="history-deleted">' . $link . '</span>';
636 * Generate a user tool link cluster if the current user is allowed to view it
637 * @return string HTML
639 protected function getUserTools() {
640 if( $this->file
->userCan( Revision
::DELETED_USER
, $this->list->getUser() ) ) {
641 $link = Linker
::userLink( $this->file
->user
, $this->file
->user_text
) .
642 Linker
::userToolLinks( $this->file
->user
, $this->file
->user_text
);
644 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
646 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
647 return '<span class="history-deleted">' . $link . '</span>';
653 * Wrap and format the file's comment block, if the current
654 * user is allowed to view it.
656 * @return string HTML
658 protected function getComment() {
659 if( $this->file
->userCan( File
::DELETED_COMMENT
, $this->list->getUser() ) ) {
660 $block = Linker
::commentBlock( $this->file
->description
);
662 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
664 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
665 return "<span class=\"history-deleted\">$block</span>";
670 public function getHTML() {
672 $this->list->msg( 'widthheight' )->numParams(
673 $this->file
->getWidth(), $this->file
->getHeight() )->text() .
674 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file
->getSize() )->text() . ')';
676 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
677 $data . ' ' . $this->getComment(). '</li>';
682 * List for filearchive table items
684 class RevDel_ArchivedFileList
extends RevDel_FileList
{
685 public function getType() {
686 return 'filearchive';
689 public static function getRelationType() {
694 * @param $db DatabaseBase
697 public function doQuery( $db ) {
698 $ids = array_map( 'intval', $this->ids
);
701 ArchivedFile
::selectFields(),
703 'fa_name' => $this->title
->getDBkey(),
707 array( 'ORDER BY' => 'fa_id DESC' )
711 public function newItem( $row ) {
712 return new RevDel_ArchivedFileItem( $this, $row );
717 * Item class for a filearchive table row
719 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
720 public function __construct( $list, $row ) {
721 RevDel_Item
::__construct( $list, $row );
722 $this->file
= ArchivedFile
::newFromRow( $row );
725 public function getIdField() {
729 public function getTimestampField() {
730 return 'fa_timestamp';
733 public function getAuthorIdField() {
737 public function getAuthorNameField() {
738 return 'fa_user_text';
741 public function getId() {
742 return $this->row
->fa_id
;
745 public function setBits( $bits ) {
746 $dbw = wfGetDB( DB_MASTER
);
747 $dbw->update( 'filearchive',
748 array( 'fa_deleted' => $bits ),
750 'fa_id' => $this->row
->fa_id
,
751 'fa_deleted' => $this->getBits(),
755 return (bool)$dbw->affectedRows();
758 protected function getLink() {
759 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
760 $this->file
->getTimestamp(), $this->list->getUser() ) );
763 if( !$this->canViewContent() ) {
766 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
767 $key = $this->file
->getKey();
768 $link = Linker
::link( $undelete, $date, array(),
770 'target' => $this->list->title
->getPrefixedText(),
772 'token' => $this->list->getUser()->getEditToken( $key )
776 if( $this->isDeleted() ) {
777 $link = '<span class="history-deleted">' . $link . '</span>';
784 * List for logging table items
786 class RevDel_LogList
extends RevDel_List
{
787 public function getType() {
791 public static function getRelationType() {
796 * @param $db DatabaseBase
799 public function doQuery( $db ) {
800 $ids = array_map( 'intval', $this->ids
);
801 return $db->select( 'logging', '*',
802 array( 'log_id' => $ids ),
804 array( 'ORDER BY' => 'log_id DESC' )
808 public function newItem( $row ) {
809 return new RevDel_LogItem( $this, $row );
812 public function getSuppressBit() {
813 return Revision
::DELETED_RESTRICTED
;
816 public function getLogAction() {
820 public function getLogParams( $params ) {
822 implode( ',', $params['ids'] ),
823 "ofield={$params['oldBits']}",
824 "nfield={$params['newBits']}"
830 * Item class for a logging table row
832 class RevDel_LogItem
extends RevDel_Item
{
833 public function getIdField() {
837 public function getTimestampField() {
838 return 'log_timestamp';
841 public function getAuthorIdField() {
845 public function getAuthorNameField() {
846 return 'log_user_text';
849 public function canView() {
850 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
, $this->list->getUser() );
853 public function canViewContent() {
857 public function getBits() {
858 return $this->row
->log_deleted
;
861 public function setBits( $bits ) {
862 $dbw = wfGetDB( DB_MASTER
);
863 $dbw->update( 'recentchanges',
865 'rc_deleted' => $bits,
869 'rc_logid' => $this->row
->log_id
,
870 'rc_timestamp' => $this->row
->log_timestamp
// index
874 $dbw->update( 'logging',
875 array( 'log_deleted' => $bits ),
877 'log_id' => $this->row
->log_id
,
878 'log_deleted' => $this->getBits()
882 return (bool)$dbw->affectedRows();
885 public function getHTML() {
886 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
887 $this->row
->log_timestamp
, $this->list->getUser() ) );
888 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
889 $formatter = LogFormatter
::newFromRow( $this->row
);
890 $formatter->setContext( $this->list->getContext() );
891 $formatter->setAudience( LogFormatter
::FOR_THIS_USER
);
893 // Log link for this page
894 $loglink = Linker
::link(
895 SpecialPage
::getTitleFor( 'Log' ),
896 $this->list->msg( 'log' )->escaped(),
898 array( 'page' => $title->getPrefixedText() )
900 $loglink = $this->list->msg( 'parentheses' )->rawParams( $loglink )->escaped();
901 // User links and action text
902 $action = $formatter->getActionText();
904 $comment = $this->list->getLanguage()->getDirMark() . Linker
::commentBlock( $this->row
->log_comment
);
905 if( LogEventsList
::isDeleted( $this->row
, LogPage
::DELETED_COMMENT
) ) {
906 $comment = '<span class="history-deleted">' . $comment . '</span>';
909 return "<li>$loglink $date $action $comment</li>";