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 = $this->list->getLanguage()->userTimeAndDate(
220 $this->revision
->getTimestamp(), $this->list->getUser() );
221 if ( $this->isDeleted() && !$this->canViewContent() ) {
229 'oldid' => $this->revision
->getId(),
236 * Get the HTML link to the diff.
237 * Overridden by RevDel_ArchiveItem
240 protected function getDiffLink() {
241 if ( $this->isDeleted() && !$this->canViewContent() ) {
242 return $this->list->msg( 'diff' )->escaped();
247 $this->list->msg( 'diff' )->escaped(),
250 'diff' => $this->revision
->getId(),
262 public function getHTML() {
263 $difflink = $this->getDiffLink();
264 $revlink = $this->getRevisionLink();
265 $userlink = Linker
::revUserLink( $this->revision
);
266 $comment = Linker
::revComment( $this->revision
);
267 if ( $this->isDeleted() ) {
268 $revlink = "<span class=\"history-deleted\">$revlink</span>";
270 return "<li>($difflink) $revlink $userlink $comment</li>";
275 * List for archive table items, i.e. revisions deleted via action=delete
277 class RevDel_ArchiveList
extends RevDel_RevisionList
{
278 public function getType() {
282 public static function getRelationType() {
283 return 'ar_timestamp';
287 * @param $db DatabaseBase
290 public function doQuery( $db ) {
291 $timestamps = array();
292 foreach ( $this->ids
as $id ) {
293 $timestamps[] = $db->timestamp( $id );
295 return $db->select( 'archive', '*',
297 'ar_namespace' => $this->title
->getNamespace(),
298 'ar_title' => $this->title
->getDBkey(),
299 'ar_timestamp' => $timestamps
302 array( 'ORDER BY' => 'ar_timestamp DESC' )
306 public function newItem( $row ) {
307 return new RevDel_ArchiveItem( $this, $row );
310 public function doPreCommitUpdates() {
311 return Status
::newGood();
314 public function doPostCommitUpdates() {
315 return Status
::newGood();
320 * Item class for a archive table row
322 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
323 public function __construct( $list, $row ) {
324 RevDel_Item
::__construct( $list, $row );
325 $this->revision
= Revision
::newFromArchiveRow( $row,
326 array( 'page' => $this->list->title
->getArticleID() ) );
329 public function getIdField() {
330 return 'ar_timestamp';
333 public function getTimestampField() {
334 return 'ar_timestamp';
337 public function getAuthorIdField() {
341 public function getAuthorNameField() {
342 return 'ar_user_text';
345 public function getId() {
346 # Convert DB timestamp to MW timestamp
347 return $this->revision
->getTimestamp();
350 public function setBits( $bits ) {
351 $dbw = wfGetDB( DB_MASTER
);
352 $dbw->update( 'archive',
353 array( 'ar_deleted' => $bits ),
355 'ar_namespace' => $this->list->title
->getNamespace(),
356 'ar_title' => $this->list->title
->getDBkey(),
357 // use timestamp for index
358 'ar_timestamp' => $this->row
->ar_timestamp
,
359 'ar_rev_id' => $this->row
->ar_rev_id
,
360 'ar_deleted' => $this->getBits()
363 return (bool)$dbw->affectedRows();
366 protected function getRevisionLink() {
367 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
368 $date = $this->list->getLanguage()->userTimeAndDate(
369 $this->revision
->getTimestamp(), $this->list->getUser() );
370 if ( $this->isDeleted() && !$this->canViewContent() ) {
373 return Linker
::link( $undelete, $date, array(),
375 'target' => $this->list->title
->getPrefixedText(),
376 'timestamp' => $this->revision
->getTimestamp()
380 protected function getDiffLink() {
381 if ( $this->isDeleted() && !$this->canViewContent() ) {
382 return $this->list->msg( 'diff' )->escaped();
384 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
385 return Linker
::link( $undelete, $this->list->msg( 'diff' )->escaped(), array(),
387 'target' => $this->list->title
->getPrefixedText(),
389 'timestamp' => $this->revision
->getTimestamp()
396 * Item class for a archive table row by ar_rev_id -- actually
397 * used via RevDel_RevisionList.
399 class RevDel_ArchivedRevisionItem
extends RevDel_ArchiveItem
{
400 public function __construct( $list, $row ) {
401 RevDel_Item
::__construct( $list, $row );
403 $this->revision
= Revision
::newFromArchiveRow( $row,
404 array( 'page' => $this->list->title
->getArticleID() ) );
407 public function getIdField() {
411 public function getId() {
412 return $this->revision
->getId();
415 public function setBits( $bits ) {
416 $dbw = wfGetDB( DB_MASTER
);
417 $dbw->update( 'archive',
418 array( 'ar_deleted' => $bits ),
419 array( 'ar_rev_id' => $this->row
->ar_rev_id
,
420 'ar_deleted' => $this->getBits()
423 return (bool)$dbw->affectedRows();
428 * List for oldimage table items
430 class RevDel_FileList
extends RevDel_List
{
431 public function getType() {
435 public static function getRelationType() {
436 return 'oi_archive_name';
439 var $storeBatch, $deleteBatch, $cleanupBatch;
442 * @param $db DatabaseBase
445 public function doQuery( $db ) {
446 $archiveNames = array();
447 foreach( $this->ids
as $timestamp ) {
448 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
450 return $db->select( 'oldimage', '*',
452 'oi_name' => $this->title
->getDBkey(),
453 'oi_archive_name' => $archiveNames
456 array( 'ORDER BY' => 'oi_timestamp DESC' )
460 public function newItem( $row ) {
461 return new RevDel_FileItem( $this, $row );
464 public function clearFileOps() {
465 $this->deleteBatch
= array();
466 $this->storeBatch
= array();
467 $this->cleanupBatch
= array();
470 public function doPreCommitUpdates() {
471 $status = Status
::newGood();
472 $repo = RepoGroup
::singleton()->getLocalRepo();
473 if ( $this->storeBatch
) {
474 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
476 if ( !$status->isOK() ) {
479 if ( $this->deleteBatch
) {
480 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
482 if ( !$status->isOK() ) {
483 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
484 // modified (but destined for rollback) causes data loss
487 if ( $this->cleanupBatch
) {
488 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
493 public function doPostCommitUpdates() {
494 $file = wfLocalFile( $this->title
);
496 $file->purgeDescription();
497 return Status
::newGood();
500 public function getSuppressBit() {
501 return File
::DELETED_RESTRICTED
;
506 * Item class for an oldimage table row
508 class RevDel_FileItem
extends RevDel_Item
{
515 public function __construct( $list, $row ) {
516 parent
::__construct( $list, $row );
517 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
520 public function getIdField() {
521 return 'oi_archive_name';
524 public function getTimestampField() {
525 return 'oi_timestamp';
528 public function getAuthorIdField() {
532 public function getAuthorNameField() {
533 return 'oi_user_text';
536 public function getId() {
537 $parts = explode( '!', $this->row
->oi_archive_name
);
541 public function canView() {
542 return $this->file
->userCan( File
::DELETED_RESTRICTED
, $this->list->getUser() );
545 public function canViewContent() {
546 return $this->file
->userCan( File
::DELETED_FILE
, $this->list->getUser() );
549 public function getBits() {
550 return $this->file
->getVisibility();
553 public function setBits( $bits ) {
555 # @todo FIXME: Move to LocalFile.php
556 if ( $this->isDeleted() ) {
557 if ( $bits & File
::DELETED_FILE
) {
561 $key = $this->file
->getStorageKey();
562 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
563 $this->list->storeBatch
[] = array(
564 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
566 $this->file
->getRel()
568 $this->list->cleanupBatch
[] = $key;
570 } elseif ( $bits & File
::DELETED_FILE
) {
572 $key = $this->file
->getStorageKey();
573 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
574 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
577 # Do the database operations
578 $dbw = wfGetDB( DB_MASTER
);
579 $dbw->update( 'oldimage',
580 array( 'oi_deleted' => $bits ),
582 'oi_name' => $this->row
->oi_name
,
583 'oi_timestamp' => $this->row
->oi_timestamp
,
584 'oi_deleted' => $this->getBits()
588 return (bool)$dbw->affectedRows();
591 public function isDeleted() {
592 return $this->file
->isDeleted( File
::DELETED_FILE
);
596 * Get the link to the file.
597 * Overridden by RevDel_ArchivedFileItem.
600 protected function getLink() {
601 $date = $this->list->getLanguage()->userTimeAndDate(
602 $this->file
->getTimestamp(), $this->list->getUser() );
603 if ( $this->isDeleted() ) {
605 if ( !$this->canViewContent() ) {
608 $revdelete = SpecialPage
::getTitleFor( 'Revisiondelete' );
609 $link = Linker
::link(
613 'target' => $this->list->title
->getPrefixedText(),
614 'file' => $this->file
->getArchiveName(),
615 'token' => $this->list->getUser()->getEditToken(
616 $this->file
->getArchiveName() )
620 return '<span class="history-deleted">' . $link . '</span>';
623 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
627 * Generate a user tool link cluster if the current user is allowed to view it
628 * @return string HTML
630 protected function getUserTools() {
631 if( $this->file
->userCan( Revision
::DELETED_USER
, $this->list->getUser() ) ) {
632 $link = Linker
::userLink( $this->file
->user
, $this->file
->user_text
) .
633 Linker
::userToolLinks( $this->file
->user
, $this->file
->user_text
);
635 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
637 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
638 return '<span class="history-deleted">' . $link . '</span>';
644 * Wrap and format the file's comment block, if the current
645 * user is allowed to view it.
647 * @return string HTML
649 protected function getComment() {
650 if( $this->file
->userCan( File
::DELETED_COMMENT
, $this->list->getUser() ) ) {
651 $block = Linker
::commentBlock( $this->file
->description
);
653 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
655 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
656 return "<span class=\"history-deleted\">$block</span>";
661 public function getHTML() {
663 $this->list->msg( 'widthheight' )->numParams(
664 $this->file
->getWidth(), $this->file
->getHeight() )->text() .
665 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file
->getSize() )->text() . ')';
667 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
668 $data . ' ' . $this->getComment(). '</li>';
673 * List for filearchive table items
675 class RevDel_ArchivedFileList
extends RevDel_FileList
{
676 public function getType() {
677 return 'filearchive';
680 public static function getRelationType() {
685 * @param $db DatabaseBase
688 public function doQuery( $db ) {
689 $ids = array_map( 'intval', $this->ids
);
690 return $db->select( 'filearchive', '*',
692 'fa_name' => $this->title
->getDBkey(),
696 array( 'ORDER BY' => 'fa_id DESC' )
700 public function newItem( $row ) {
701 return new RevDel_ArchivedFileItem( $this, $row );
706 * Item class for a filearchive table row
708 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
709 public function __construct( $list, $row ) {
710 RevDel_Item
::__construct( $list, $row );
711 $this->file
= ArchivedFile
::newFromRow( $row );
714 public function getIdField() {
718 public function getTimestampField() {
719 return 'fa_timestamp';
722 public function getAuthorIdField() {
726 public function getAuthorNameField() {
727 return 'fa_user_text';
730 public function getId() {
731 return $this->row
->fa_id
;
734 public function setBits( $bits ) {
735 $dbw = wfGetDB( DB_MASTER
);
736 $dbw->update( 'filearchive',
737 array( 'fa_deleted' => $bits ),
739 'fa_id' => $this->row
->fa_id
,
740 'fa_deleted' => $this->getBits(),
744 return (bool)$dbw->affectedRows();
747 protected function getLink() {
748 $date = $this->list->getLanguage()->userTimeAndDate(
749 $this->file
->getTimestamp(), $this->list->getUser() );
750 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
751 $key = $this->file
->getKey();
753 if( !$this->canViewContent() ) {
756 $link = Linker
::link( $undelete, $date, array(),
758 'target' => $this->list->title
->getPrefixedText(),
760 'token' => $this->list->getUser()->getEditToken( $key )
764 if( $this->isDeleted() ) {
765 $link = '<span class="history-deleted">' . $link . '</span>';
772 * List for logging table items
774 class RevDel_LogList
extends RevDel_List
{
775 public function getType() {
779 public static function getRelationType() {
784 * @param $db DatabaseBase
787 public function doQuery( $db ) {
788 $ids = array_map( 'intval', $this->ids
);
789 return $db->select( 'logging', '*',
790 array( 'log_id' => $ids ),
792 array( 'ORDER BY' => 'log_id DESC' )
796 public function newItem( $row ) {
797 return new RevDel_LogItem( $this, $row );
800 public function getSuppressBit() {
801 return Revision
::DELETED_RESTRICTED
;
804 public function getLogAction() {
808 public function getLogParams( $params ) {
810 implode( ',', $params['ids'] ),
811 "ofield={$params['oldBits']}",
812 "nfield={$params['newBits']}"
818 * Item class for a logging table row
820 class RevDel_LogItem
extends RevDel_Item
{
821 public function getIdField() {
825 public function getTimestampField() {
826 return 'log_timestamp';
829 public function getAuthorIdField() {
833 public function getAuthorNameField() {
834 return 'log_user_text';
837 public function canView() {
838 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
, $this->list->getUser() );
841 public function canViewContent() {
845 public function getBits() {
846 return $this->row
->log_deleted
;
849 public function setBits( $bits ) {
850 $dbw = wfGetDB( DB_MASTER
);
851 $dbw->update( 'recentchanges',
853 'rc_deleted' => $bits,
857 'rc_logid' => $this->row
->log_id
,
858 'rc_timestamp' => $this->row
->log_timestamp
// index
862 $dbw->update( 'logging',
863 array( 'log_deleted' => $bits ),
865 'log_id' => $this->row
->log_id
,
866 'log_deleted' => $this->getBits()
870 return (bool)$dbw->affectedRows();
873 public function getHTML() {
874 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
875 $this->row
->log_timestamp
, $this->list->getUser() ) );
876 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
877 $formatter = LogFormatter
::newFromRow( $this->row
);
878 $formatter->setContext( $this->list->getContext() );
879 $formatter->setAudience( LogFormatter
::FOR_THIS_USER
);
881 // Log link for this page
882 $loglink = Linker
::link(
883 SpecialPage
::getTitleFor( 'Log' ),
884 $this->list->msg( 'log' )->escaped(),
886 array( 'page' => $title->getPrefixedText() )
888 // User links and action text
889 $action = $formatter->getActionText();
891 $comment = $this->list->getLanguage()->getDirMark() . Linker
::commentBlock( $this->row
->log_comment
);
892 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
893 $comment = '<span class="history-deleted">' . $comment . '</span>';
896 return "<li>($loglink) $date $action $comment</li>";