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->list->msg( 'parentheses' )
264 ->rawParams( $this->getDiffLink() )->escaped();
265 $revlink = $this->getRevisionLink();
266 $userlink = Linker
::revUserLink( $this->revision
);
267 $comment = Linker
::revComment( $this->revision
);
268 if ( $this->isDeleted() ) {
269 $revlink = "<span class=\"history-deleted\">$revlink</span>";
271 return "<li>$difflink $revlink $userlink $comment</li>";
276 * List for archive table items, i.e. revisions deleted via action=delete
278 class RevDel_ArchiveList
extends RevDel_RevisionList
{
279 public function getType() {
283 public static function getRelationType() {
284 return 'ar_timestamp';
288 * @param $db DatabaseBase
291 public function doQuery( $db ) {
292 $timestamps = array();
293 foreach ( $this->ids
as $id ) {
294 $timestamps[] = $db->timestamp( $id );
296 return $db->select( 'archive', '*',
298 'ar_namespace' => $this->title
->getNamespace(),
299 'ar_title' => $this->title
->getDBkey(),
300 'ar_timestamp' => $timestamps
303 array( 'ORDER BY' => 'ar_timestamp DESC' )
307 public function newItem( $row ) {
308 return new RevDel_ArchiveItem( $this, $row );
311 public function doPreCommitUpdates() {
312 return Status
::newGood();
315 public function doPostCommitUpdates() {
316 return Status
::newGood();
321 * Item class for a archive table row
323 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
324 public function __construct( $list, $row ) {
325 RevDel_Item
::__construct( $list, $row );
326 $this->revision
= Revision
::newFromArchiveRow( $row,
327 array( 'page' => $this->list->title
->getArticleID() ) );
330 public function getIdField() {
331 return 'ar_timestamp';
334 public function getTimestampField() {
335 return 'ar_timestamp';
338 public function getAuthorIdField() {
342 public function getAuthorNameField() {
343 return 'ar_user_text';
346 public function getId() {
347 # Convert DB timestamp to MW timestamp
348 return $this->revision
->getTimestamp();
351 public function setBits( $bits ) {
352 $dbw = wfGetDB( DB_MASTER
);
353 $dbw->update( 'archive',
354 array( 'ar_deleted' => $bits ),
356 'ar_namespace' => $this->list->title
->getNamespace(),
357 'ar_title' => $this->list->title
->getDBkey(),
358 // use timestamp for index
359 'ar_timestamp' => $this->row
->ar_timestamp
,
360 'ar_rev_id' => $this->row
->ar_rev_id
,
361 'ar_deleted' => $this->getBits()
364 return (bool)$dbw->affectedRows();
367 protected function getRevisionLink() {
368 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
369 $date = $this->list->getLanguage()->userTimeAndDate(
370 $this->revision
->getTimestamp(), $this->list->getUser() );
371 if ( $this->isDeleted() && !$this->canViewContent() ) {
374 return Linker
::link( $undelete, $date, array(),
376 'target' => $this->list->title
->getPrefixedText(),
377 'timestamp' => $this->revision
->getTimestamp()
381 protected function getDiffLink() {
382 if ( $this->isDeleted() && !$this->canViewContent() ) {
383 return $this->list->msg( 'diff' )->escaped();
385 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
386 return Linker
::link( $undelete, $this->list->msg( 'diff' )->escaped(), array(),
388 'target' => $this->list->title
->getPrefixedText(),
390 'timestamp' => $this->revision
->getTimestamp()
397 * Item class for a archive table row by ar_rev_id -- actually
398 * used via RevDel_RevisionList.
400 class RevDel_ArchivedRevisionItem
extends RevDel_ArchiveItem
{
401 public function __construct( $list, $row ) {
402 RevDel_Item
::__construct( $list, $row );
404 $this->revision
= Revision
::newFromArchiveRow( $row,
405 array( 'page' => $this->list->title
->getArticleID() ) );
408 public function getIdField() {
412 public function getId() {
413 return $this->revision
->getId();
416 public function setBits( $bits ) {
417 $dbw = wfGetDB( DB_MASTER
);
418 $dbw->update( 'archive',
419 array( 'ar_deleted' => $bits ),
420 array( 'ar_rev_id' => $this->row
->ar_rev_id
,
421 'ar_deleted' => $this->getBits()
424 return (bool)$dbw->affectedRows();
429 * List for oldimage table items
431 class RevDel_FileList
extends RevDel_List
{
432 public function getType() {
436 public static function getRelationType() {
437 return 'oi_archive_name';
440 var $storeBatch, $deleteBatch, $cleanupBatch;
443 * @param $db DatabaseBase
446 public function doQuery( $db ) {
447 $archiveNames = array();
448 foreach( $this->ids
as $timestamp ) {
449 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
451 return $db->select( 'oldimage', '*',
453 'oi_name' => $this->title
->getDBkey(),
454 'oi_archive_name' => $archiveNames
457 array( 'ORDER BY' => 'oi_timestamp DESC' )
461 public function newItem( $row ) {
462 return new RevDel_FileItem( $this, $row );
465 public function clearFileOps() {
466 $this->deleteBatch
= array();
467 $this->storeBatch
= array();
468 $this->cleanupBatch
= array();
471 public function doPreCommitUpdates() {
472 $status = Status
::newGood();
473 $repo = RepoGroup
::singleton()->getLocalRepo();
474 if ( $this->storeBatch
) {
475 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
477 if ( !$status->isOK() ) {
480 if ( $this->deleteBatch
) {
481 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
483 if ( !$status->isOK() ) {
484 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
485 // modified (but destined for rollback) causes data loss
488 if ( $this->cleanupBatch
) {
489 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
494 public function doPostCommitUpdates() {
495 $file = wfLocalFile( $this->title
);
497 $file->purgeDescription();
498 return Status
::newGood();
501 public function getSuppressBit() {
502 return File
::DELETED_RESTRICTED
;
507 * Item class for an oldimage table row
509 class RevDel_FileItem
extends RevDel_Item
{
516 public function __construct( $list, $row ) {
517 parent
::__construct( $list, $row );
518 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
521 public function getIdField() {
522 return 'oi_archive_name';
525 public function getTimestampField() {
526 return 'oi_timestamp';
529 public function getAuthorIdField() {
533 public function getAuthorNameField() {
534 return 'oi_user_text';
537 public function getId() {
538 $parts = explode( '!', $this->row
->oi_archive_name
);
542 public function canView() {
543 return $this->file
->userCan( File
::DELETED_RESTRICTED
, $this->list->getUser() );
546 public function canViewContent() {
547 return $this->file
->userCan( File
::DELETED_FILE
, $this->list->getUser() );
550 public function getBits() {
551 return $this->file
->getVisibility();
554 public function setBits( $bits ) {
556 # @todo FIXME: Move to LocalFile.php
557 if ( $this->isDeleted() ) {
558 if ( $bits & File
::DELETED_FILE
) {
562 $key = $this->file
->getStorageKey();
563 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
564 $this->list->storeBatch
[] = array(
565 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
567 $this->file
->getRel()
569 $this->list->cleanupBatch
[] = $key;
571 } elseif ( $bits & File
::DELETED_FILE
) {
573 $key = $this->file
->getStorageKey();
574 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
575 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
578 # Do the database operations
579 $dbw = wfGetDB( DB_MASTER
);
580 $dbw->update( 'oldimage',
581 array( 'oi_deleted' => $bits ),
583 'oi_name' => $this->row
->oi_name
,
584 'oi_timestamp' => $this->row
->oi_timestamp
,
585 'oi_deleted' => $this->getBits()
589 return (bool)$dbw->affectedRows();
592 public function isDeleted() {
593 return $this->file
->isDeleted( File
::DELETED_FILE
);
597 * Get the link to the file.
598 * Overridden by RevDel_ArchivedFileItem.
601 protected function getLink() {
602 $date = $this->list->getLanguage()->userTimeAndDate(
603 $this->file
->getTimestamp(), $this->list->getUser() );
604 if ( $this->isDeleted() ) {
606 if ( !$this->canViewContent() ) {
609 $revdelete = SpecialPage
::getTitleFor( 'Revisiondelete' );
610 $link = Linker
::link(
614 'target' => $this->list->title
->getPrefixedText(),
615 'file' => $this->file
->getArchiveName(),
616 'token' => $this->list->getUser()->getEditToken(
617 $this->file
->getArchiveName() )
621 return '<span class="history-deleted">' . $link . '</span>';
624 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
628 * Generate a user tool link cluster if the current user is allowed to view it
629 * @return string HTML
631 protected function getUserTools() {
632 if( $this->file
->userCan( Revision
::DELETED_USER
, $this->list->getUser() ) ) {
633 $link = Linker
::userLink( $this->file
->user
, $this->file
->user_text
) .
634 Linker
::userToolLinks( $this->file
->user
, $this->file
->user_text
);
636 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
638 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
639 return '<span class="history-deleted">' . $link . '</span>';
645 * Wrap and format the file's comment block, if the current
646 * user is allowed to view it.
648 * @return string HTML
650 protected function getComment() {
651 if( $this->file
->userCan( File
::DELETED_COMMENT
, $this->list->getUser() ) ) {
652 $block = Linker
::commentBlock( $this->file
->description
);
654 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
656 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
657 return "<span class=\"history-deleted\">$block</span>";
662 public function getHTML() {
664 $this->list->msg( 'widthheight' )->numParams(
665 $this->file
->getWidth(), $this->file
->getHeight() )->text() .
666 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file
->getSize() )->text() . ')';
668 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
669 $data . ' ' . $this->getComment(). '</li>';
674 * List for filearchive table items
676 class RevDel_ArchivedFileList
extends RevDel_FileList
{
677 public function getType() {
678 return 'filearchive';
681 public static function getRelationType() {
686 * @param $db DatabaseBase
689 public function doQuery( $db ) {
690 $ids = array_map( 'intval', $this->ids
);
691 return $db->select( 'filearchive', '*',
693 'fa_name' => $this->title
->getDBkey(),
697 array( 'ORDER BY' => 'fa_id DESC' )
701 public function newItem( $row ) {
702 return new RevDel_ArchivedFileItem( $this, $row );
707 * Item class for a filearchive table row
709 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
710 public function __construct( $list, $row ) {
711 RevDel_Item
::__construct( $list, $row );
712 $this->file
= ArchivedFile
::newFromRow( $row );
715 public function getIdField() {
719 public function getTimestampField() {
720 return 'fa_timestamp';
723 public function getAuthorIdField() {
727 public function getAuthorNameField() {
728 return 'fa_user_text';
731 public function getId() {
732 return $this->row
->fa_id
;
735 public function setBits( $bits ) {
736 $dbw = wfGetDB( DB_MASTER
);
737 $dbw->update( 'filearchive',
738 array( 'fa_deleted' => $bits ),
740 'fa_id' => $this->row
->fa_id
,
741 'fa_deleted' => $this->getBits(),
745 return (bool)$dbw->affectedRows();
748 protected function getLink() {
749 $date = $this->list->getLanguage()->userTimeAndDate(
750 $this->file
->getTimestamp(), $this->list->getUser() );
751 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
752 $key = $this->file
->getKey();
754 if( !$this->canViewContent() ) {
757 $link = Linker
::link( $undelete, $date, array(),
759 'target' => $this->list->title
->getPrefixedText(),
761 'token' => $this->list->getUser()->getEditToken( $key )
765 if( $this->isDeleted() ) {
766 $link = '<span class="history-deleted">' . $link . '</span>';
773 * List for logging table items
775 class RevDel_LogList
extends RevDel_List
{
776 public function getType() {
780 public static function getRelationType() {
785 * @param $db DatabaseBase
788 public function doQuery( $db ) {
789 $ids = array_map( 'intval', $this->ids
);
790 return $db->select( 'logging', '*',
791 array( 'log_id' => $ids ),
793 array( 'ORDER BY' => 'log_id DESC' )
797 public function newItem( $row ) {
798 return new RevDel_LogItem( $this, $row );
801 public function getSuppressBit() {
802 return Revision
::DELETED_RESTRICTED
;
805 public function getLogAction() {
809 public function getLogParams( $params ) {
811 implode( ',', $params['ids'] ),
812 "ofield={$params['oldBits']}",
813 "nfield={$params['newBits']}"
819 * Item class for a logging table row
821 class RevDel_LogItem
extends RevDel_Item
{
822 public function getIdField() {
826 public function getTimestampField() {
827 return 'log_timestamp';
830 public function getAuthorIdField() {
834 public function getAuthorNameField() {
835 return 'log_user_text';
838 public function canView() {
839 return LogEventsList
::userCan( $this->row
, Revision
::DELETED_RESTRICTED
, $this->list->getUser() );
842 public function canViewContent() {
846 public function getBits() {
847 return $this->row
->log_deleted
;
850 public function setBits( $bits ) {
851 $dbw = wfGetDB( DB_MASTER
);
852 $dbw->update( 'recentchanges',
854 'rc_deleted' => $bits,
858 'rc_logid' => $this->row
->log_id
,
859 'rc_timestamp' => $this->row
->log_timestamp
// index
863 $dbw->update( 'logging',
864 array( 'log_deleted' => $bits ),
866 'log_id' => $this->row
->log_id
,
867 'log_deleted' => $this->getBits()
871 return (bool)$dbw->affectedRows();
874 public function getHTML() {
875 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
876 $this->row
->log_timestamp
, $this->list->getUser() ) );
877 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
878 $formatter = LogFormatter
::newFromRow( $this->row
);
879 $formatter->setContext( $this->list->getContext() );
880 $formatter->setAudience( LogFormatter
::FOR_THIS_USER
);
882 // Log link for this page
883 $loglink = Linker
::link(
884 SpecialPage
::getTitleFor( 'Log' ),
885 $this->list->msg( 'log' )->escaped(),
887 array( 'page' => $title->getPrefixedText() )
889 $loglink = $this->list->msg( 'parentheses' )->rawParams( $loglink )->escaped();
890 // User links and action text
891 $action = $formatter->getActionText();
893 $comment = $this->list->getLanguage()->getDirMark() . Linker
::commentBlock( $this->row
->log_comment
);
894 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
895 $comment = '<span class="history-deleted">' . $comment . '</span>';
898 return "<li>$loglink $date $action $comment</li>";