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()->timeanddate( $this->revision
->getTimestamp(), true );
220 if ( $this->isDeleted() && !$this->canViewContent() ) {
228 'oldid' => $this->revision
->getId(),
235 * Get the HTML link to the diff.
236 * Overridden by RevDel_ArchiveItem
239 protected function getDiffLink() {
240 if ( $this->isDeleted() && !$this->canViewContent() ) {
241 return wfMsgHtml('diff');
249 'diff' => $this->revision
->getId(),
261 public function getHTML() {
262 $difflink = $this->getDiffLink();
263 $revlink = $this->getRevisionLink();
264 $userlink = Linker
::revUserLink( $this->revision
);
265 $comment = Linker
::revComment( $this->revision
);
266 if ( $this->isDeleted() ) {
267 $revlink = "<span class=\"history-deleted\">$revlink</span>";
269 return "<li>($difflink) $revlink $userlink $comment</li>";
274 * List for archive table items, i.e. revisions deleted via action=delete
276 class RevDel_ArchiveList
extends RevDel_RevisionList
{
277 public function getType() {
281 public static function getRelationType() {
282 return 'ar_timestamp';
286 * @param $db DatabaseBase
289 public function doQuery( $db ) {
290 $timestamps = array();
291 foreach ( $this->ids
as $id ) {
292 $timestamps[] = $db->timestamp( $id );
294 return $db->select( 'archive', '*',
296 'ar_namespace' => $this->title
->getNamespace(),
297 'ar_title' => $this->title
->getDBkey(),
298 'ar_timestamp' => $timestamps
301 array( 'ORDER BY' => 'ar_timestamp DESC' )
305 public function newItem( $row ) {
306 return new RevDel_ArchiveItem( $this, $row );
309 public function doPreCommitUpdates() {
310 return Status
::newGood();
313 public function doPostCommitUpdates() {
314 return Status
::newGood();
319 * Item class for a archive table row
321 class RevDel_ArchiveItem
extends RevDel_RevisionItem
{
322 public function __construct( $list, $row ) {
323 RevDel_Item
::__construct( $list, $row );
324 $this->revision
= Revision
::newFromArchiveRow( $row,
325 array( 'page' => $this->list->title
->getArticleID() ) );
328 public function getIdField() {
329 return 'ar_timestamp';
332 public function getTimestampField() {
333 return 'ar_timestamp';
336 public function getAuthorIdField() {
340 public function getAuthorNameField() {
341 return 'ar_user_text';
344 public function getId() {
345 # Convert DB timestamp to MW timestamp
346 return $this->revision
->getTimestamp();
349 public function setBits( $bits ) {
350 $dbw = wfGetDB( DB_MASTER
);
351 $dbw->update( 'archive',
352 array( 'ar_deleted' => $bits ),
354 'ar_namespace' => $this->list->title
->getNamespace(),
355 'ar_title' => $this->list->title
->getDBkey(),
356 // use timestamp for index
357 'ar_timestamp' => $this->row
->ar_timestamp
,
358 'ar_rev_id' => $this->row
->ar_rev_id
,
359 'ar_deleted' => $this->getBits()
362 return (bool)$dbw->affectedRows();
365 protected function getRevisionLink() {
366 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
367 $date = $this->list->getLanguage()->timeanddate( $this->revision
->getTimestamp(), true );
368 if ( $this->isDeleted() && !$this->canViewContent() ) {
371 return Linker
::link( $undelete, $date, array(),
373 'target' => $this->list->title
->getPrefixedText(),
374 'timestamp' => $this->revision
->getTimestamp()
378 protected function getDiffLink() {
379 if ( $this->isDeleted() && !$this->canViewContent() ) {
380 return wfMsgHtml( 'diff' );
382 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
383 return Linker
::link( $undelete, wfMsgHtml('diff'), array(),
385 'target' => $this->list->title
->getPrefixedText(),
387 'timestamp' => $this->revision
->getTimestamp()
394 * Item class for a archive table row by ar_rev_id -- actually
395 * used via RevDel_RevisionList.
397 class RevDel_ArchivedRevisionItem
extends RevDel_ArchiveItem
{
398 public function __construct( $list, $row ) {
399 RevDel_Item
::__construct( $list, $row );
401 $this->revision
= Revision
::newFromArchiveRow( $row,
402 array( 'page' => $this->list->title
->getArticleID() ) );
405 public function getIdField() {
409 public function getId() {
410 return $this->revision
->getId();
413 public function setBits( $bits ) {
414 $dbw = wfGetDB( DB_MASTER
);
415 $dbw->update( 'archive',
416 array( 'ar_deleted' => $bits ),
417 array( 'ar_rev_id' => $this->row
->ar_rev_id
,
418 'ar_deleted' => $this->getBits()
421 return (bool)$dbw->affectedRows();
426 * List for oldimage table items
428 class RevDel_FileList
extends RevDel_List
{
429 public function getType() {
433 public static function getRelationType() {
434 return 'oi_archive_name';
437 var $storeBatch, $deleteBatch, $cleanupBatch;
440 * @param $db DatabaseBase
443 public function doQuery( $db ) {
444 $archiveNames = array();
445 foreach( $this->ids
as $timestamp ) {
446 $archiveNames[] = $timestamp . '!' . $this->title
->getDBkey();
448 return $db->select( 'oldimage', '*',
450 'oi_name' => $this->title
->getDBkey(),
451 'oi_archive_name' => $archiveNames
454 array( 'ORDER BY' => 'oi_timestamp DESC' )
458 public function newItem( $row ) {
459 return new RevDel_FileItem( $this, $row );
462 public function clearFileOps() {
463 $this->deleteBatch
= array();
464 $this->storeBatch
= array();
465 $this->cleanupBatch
= array();
468 public function doPreCommitUpdates() {
469 $status = Status
::newGood();
470 $repo = RepoGroup
::singleton()->getLocalRepo();
471 if ( $this->storeBatch
) {
472 $status->merge( $repo->storeBatch( $this->storeBatch
, FileRepo
::OVERWRITE_SAME
) );
474 if ( !$status->isOK() ) {
477 if ( $this->deleteBatch
) {
478 $status->merge( $repo->deleteBatch( $this->deleteBatch
) );
480 if ( !$status->isOK() ) {
481 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
482 // modified (but destined for rollback) causes data loss
485 if ( $this->cleanupBatch
) {
486 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch
) );
491 public function doPostCommitUpdates() {
492 $file = wfLocalFile( $this->title
);
494 $file->purgeDescription();
495 return Status
::newGood();
498 public function getSuppressBit() {
499 return File
::DELETED_RESTRICTED
;
504 * Item class for an oldimage table row
506 class RevDel_FileItem
extends RevDel_Item
{
513 public function __construct( $list, $row ) {
514 parent
::__construct( $list, $row );
515 $this->file
= RepoGroup
::singleton()->getLocalRepo()->newFileFromRow( $row );
518 public function getIdField() {
519 return 'oi_archive_name';
522 public function getTimestampField() {
523 return 'oi_timestamp';
526 public function getAuthorIdField() {
530 public function getAuthorNameField() {
531 return 'oi_user_text';
534 public function getId() {
535 $parts = explode( '!', $this->row
->oi_archive_name
);
539 public function canView() {
540 return $this->file
->userCan( File
::DELETED_RESTRICTED
, $this->list->getUser() );
543 public function canViewContent() {
544 return $this->file
->userCan( File
::DELETED_FILE
, $this->list->getUser() );
547 public function getBits() {
548 return $this->file
->getVisibility();
551 public function setBits( $bits ) {
553 # @todo FIXME: Move to LocalFile.php
554 if ( $this->isDeleted() ) {
555 if ( $bits & File
::DELETED_FILE
) {
559 $key = $this->file
->getStorageKey();
560 $srcRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
561 $this->list->storeBatch
[] = array(
562 $this->file
->repo
->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
564 $this->file
->getRel()
566 $this->list->cleanupBatch
[] = $key;
568 } elseif ( $bits & File
::DELETED_FILE
) {
570 $key = $this->file
->getStorageKey();
571 $dstRel = $this->file
->repo
->getDeletedHashPath( $key ) . $key;
572 $this->list->deleteBatch
[] = array( $this->file
->getRel(), $dstRel );
575 # Do the database operations
576 $dbw = wfGetDB( DB_MASTER
);
577 $dbw->update( 'oldimage',
578 array( 'oi_deleted' => $bits ),
580 'oi_name' => $this->row
->oi_name
,
581 'oi_timestamp' => $this->row
->oi_timestamp
,
582 'oi_deleted' => $this->getBits()
586 return (bool)$dbw->affectedRows();
589 public function isDeleted() {
590 return $this->file
->isDeleted( File
::DELETED_FILE
);
594 * Get the link to the file.
595 * Overridden by RevDel_ArchivedFileItem.
598 protected function getLink() {
599 $date = $this->list->getLanguage()->timeanddate( $this->file
->getTimestamp(), true );
600 if ( $this->isDeleted() ) {
602 if ( !$this->canViewContent() ) {
605 $revdelete = SpecialPage
::getTitleFor( 'Revisiondelete' );
606 $link = Linker
::link(
610 'target' => $this->list->title
->getPrefixedText(),
611 'file' => $this->file
->getArchiveName(),
612 'token' => $this->list->getUser()->getEditToken(
613 $this->file
->getArchiveName() )
617 return '<span class="history-deleted">' . $link . '</span>';
620 return Xml
::element( 'a', array( 'href' => $this->file
->getUrl() ), $date );
624 * Generate a user tool link cluster if the current user is allowed to view it
625 * @return string HTML
627 protected function getUserTools() {
628 if( $this->file
->userCan( Revision
::DELETED_USER
, $this->list->getUser() ) ) {
629 $link = Linker
::userLink( $this->file
->user
, $this->file
->user_text
) .
630 Linker
::userToolLinks( $this->file
->user
, $this->file
->user_text
);
632 $link = wfMsgHtml( 'rev-deleted-user' );
634 if( $this->file
->isDeleted( Revision
::DELETED_USER
) ) {
635 return '<span class="history-deleted">' . $link . '</span>';
641 * Wrap and format the file's comment block, if the current
642 * user is allowed to view it.
644 * @return string HTML
646 protected function getComment() {
647 if( $this->file
->userCan( File
::DELETED_COMMENT
, $this->list->getUser() ) ) {
648 $block = Linker
::commentBlock( $this->file
->description
);
650 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
652 if( $this->file
->isDeleted( File
::DELETED_COMMENT
) ) {
653 return "<span class=\"history-deleted\">$block</span>";
658 public function getHTML() {
662 $this->list->getLanguage()->formatNum( $this->file
->getWidth() ),
663 $this->list->getLanguage()->formatNum( $this->file
->getHeight() )
666 wfMsgExt( 'nbytes', 'parsemag', $this->list->getLanguage()->formatNum( $this->file
->getSize() ) ) .
669 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
670 $data . ' ' . $this->getComment(). '</li>';
675 * List for filearchive table items
677 class RevDel_ArchivedFileList
extends RevDel_FileList
{
678 public function getType() {
679 return 'filearchive';
682 public static function getRelationType() {
687 * @param $db DatabaseBase
690 public function doQuery( $db ) {
691 $ids = array_map( 'intval', $this->ids
);
692 return $db->select( 'filearchive', '*',
694 'fa_name' => $this->title
->getDBkey(),
698 array( 'ORDER BY' => 'fa_id DESC' )
702 public function newItem( $row ) {
703 return new RevDel_ArchivedFileItem( $this, $row );
708 * Item class for a filearchive table row
710 class RevDel_ArchivedFileItem
extends RevDel_FileItem
{
711 public function __construct( $list, $row ) {
712 RevDel_Item
::__construct( $list, $row );
713 $this->file
= ArchivedFile
::newFromRow( $row );
716 public function getIdField() {
720 public function getTimestampField() {
721 return 'fa_timestamp';
724 public function getAuthorIdField() {
728 public function getAuthorNameField() {
729 return 'fa_user_text';
732 public function getId() {
733 return $this->row
->fa_id
;
736 public function setBits( $bits ) {
737 $dbw = wfGetDB( DB_MASTER
);
738 $dbw->update( 'filearchive',
739 array( 'fa_deleted' => $bits ),
741 'fa_id' => $this->row
->fa_id
,
742 'fa_deleted' => $this->getBits(),
746 return (bool)$dbw->affectedRows();
749 protected function getLink() {
750 $date = $this->list->getLanguage()->timeanddate( $this->file
->getTimestamp(), true );
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()->timeanddate( $this->row
->log_timestamp
) );
876 $title = Title
::makeTitle( $this->row
->log_namespace
, $this->row
->log_title
);
877 $formatter = LogFormatter
::newFromRow( $this->row
);
878 $formatter->setAudience( LogFormatter
::FOR_THIS_USER
);
880 // Log link for this page
881 $loglink = Linker
::link(
882 SpecialPage
::getTitleFor( 'Log' ),
885 array( 'page' => $title->getPrefixedText() )
887 // User links and action text
888 $action = $formatter->getActionText();
890 $comment = $this->list->getLanguage()->getDirMark() . Linker
::commentBlock( $this->row
->log_comment
);
891 if( LogEventsList
::isDeleted($this->row
,LogPage
::DELETED_COMMENT
) ) {
892 $comment = '<span class="history-deleted">' . $comment . '</span>';
895 return "<li>($loglink) $date $action $comment</li>";