Revert r101492, broken, see CR. Also revert followup r101496.
[mediawiki.git] / includes / revisiondelete / RevisionDelete.php
blob0a6566b0473d1fcb8e4a9169ac30aca29173373d
1 <?php
2 /**
3 * List for revision table items
5 * This will check both the 'revision' table for live revisions and the
6 * 'archive' table for traditionally-deleted revisions that have an
7 * ar_rev_id saved.
9 * See RevDel_RevisionItem and RevDel_ArchivedRevisionItem for items.
11 class RevDel_RevisionList extends RevDel_List {
12 var $currentRevId;
14 public function getType() {
15 return 'revision';
18 public static function getRelationType() {
19 return 'rev_id';
22 /**
23 * @param $db DatabaseBase
24 * @return mixed
26 public function doQuery( $db ) {
27 $ids = array_map( 'intval', $this->ids );
28 $live = $db->select(
29 array( 'revision', 'page', 'user' ),
30 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
31 array(
32 'rev_page' => $this->title->getArticleID(),
33 'rev_id' => $ids,
35 __METHOD__,
36 array( 'ORDER BY' => 'rev_id DESC' ),
37 array(
38 'page' => Revision::pageJoinCond(),
39 'user' => Revision::userJoinCond() )
42 if ( $live->numRows() >= count( $ids ) ) {
43 // All requested revisions are live, keeps things simple!
44 return $live;
47 // Check if any requested revisions are available fully deleted.
48 $archived = $db->select( array( 'archive' ), '*',
49 array(
50 'ar_rev_id' => $ids
52 __METHOD__,
53 array( 'ORDER BY' => 'ar_rev_id DESC' )
56 if ( $archived->numRows() == 0 ) {
57 return $live;
58 } elseif ( $live->numRows() == 0 ) {
59 return $archived;
60 } else {
61 // Combine the two! Whee
62 $rows = array();
63 foreach ( $live as $row ) {
64 $rows[$row->rev_id] = $row;
66 foreach ( $archived as $row ) {
67 $rows[$row->ar_rev_id] = $row;
69 krsort( $rows );
70 return new FakeResultWrapper( array_values( $rows ) );
74 public function newItem( $row ) {
75 if ( isset( $row->rev_id ) ) {
76 return new RevDel_RevisionItem( $this, $row );
77 } elseif ( isset( $row->ar_rev_id ) ) {
78 return new RevDel_ArchivedRevisionItem( $this, $row );
79 } else {
80 // This shouldn't happen. :)
81 throw new MWException( 'Invalid row type in RevDel_RevisionList' );
85 public function getCurrent() {
86 if ( is_null( $this->currentRevId ) ) {
87 $dbw = wfGetDB( DB_MASTER );
88 $this->currentRevId = $dbw->selectField(
89 'page', 'page_latest', $this->title->pageCond(), __METHOD__ );
91 return $this->currentRevId;
94 public function getSuppressBit() {
95 return Revision::DELETED_RESTRICTED;
98 public function doPreCommitUpdates() {
99 $this->title->invalidateCache();
100 return Status::newGood();
103 public function doPostCommitUpdates() {
104 $this->title->purgeSquid();
105 // Extensions that require referencing previous revisions may need this
106 wfRunHooks( 'ArticleRevisionVisibilitySet', array( &$this->title ) );
107 return Status::newGood();
112 * Item class for a live revision table row
114 class RevDel_RevisionItem extends RevDel_Item {
115 var $revision;
117 public function __construct( $list, $row ) {
118 parent::__construct( $list, $row );
119 $this->revision = new Revision( $row );
122 public function getIdField() {
123 return 'rev_id';
126 public function getTimestampField() {
127 return 'rev_timestamp';
130 public function getAuthorIdField() {
131 return 'rev_user';
134 public function getAuthorNameField() {
135 return 'user_name'; // see Revision::selectUserFields()
138 public function canView() {
139 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->list->getUser() );
142 public function canViewContent() {
143 return $this->revision->userCan( Revision::DELETED_TEXT, $this->list->getUser() );
146 public function getBits() {
147 return $this->revision->getVisibility();
150 public function setBits( $bits ) {
151 $dbw = wfGetDB( DB_MASTER );
152 // Update revision table
153 $dbw->update( 'revision',
154 array( 'rev_deleted' => $bits ),
155 array(
156 'rev_id' => $this->revision->getId(),
157 'rev_page' => $this->revision->getPage(),
158 'rev_deleted' => $this->getBits()
160 __METHOD__
162 if ( !$dbw->affectedRows() ) {
163 // Concurrent fail!
164 return false;
166 // Update recentchanges table
167 $dbw->update( 'recentchanges',
168 array(
169 'rc_deleted' => $bits,
170 'rc_patrolled' => 1
172 array(
173 'rc_this_oldid' => $this->revision->getId(), // condition
174 // non-unique timestamp index
175 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
177 __METHOD__
179 return true;
182 public function isDeleted() {
183 return $this->revision->isDeleted( Revision::DELETED_TEXT );
186 public function isHideCurrentOp( $newBits ) {
187 return ( $newBits & Revision::DELETED_TEXT )
188 && $this->list->getCurrent() == $this->getId();
192 * Get the HTML link to the revision text.
193 * Overridden by RevDel_ArchiveItem.
195 protected function getRevisionLink() {
196 $date = $this->list->getLang()->timeanddate( $this->revision->getTimestamp(), true );
197 if ( $this->isDeleted() && !$this->canViewContent() ) {
198 return $date;
200 return Linker::link(
201 $this->list->title,
202 $date,
203 array(),
204 array(
205 'oldid' => $this->revision->getId(),
206 'unhide' => 1
212 * Get the HTML link to the diff.
213 * Overridden by RevDel_ArchiveItem
215 protected function getDiffLink() {
216 if ( $this->isDeleted() && !$this->canViewContent() ) {
217 return wfMsgHtml('diff');
218 } else {
219 return
220 Linker::link(
221 $this->list->title,
222 wfMsgHtml('diff'),
223 array(),
224 array(
225 'diff' => $this->revision->getId(),
226 'oldid' => 'prev',
227 'unhide' => 1
229 array(
230 'known',
231 'noclasses'
237 public function getHTML() {
238 $difflink = $this->getDiffLink();
239 $revlink = $this->getRevisionLink();
240 $userlink = Linker::revUserLink( $this->revision );
241 $comment = Linker::revComment( $this->revision );
242 if ( $this->isDeleted() ) {
243 $revlink = "<span class=\"history-deleted\">$revlink</span>";
245 return "<li>($difflink) $revlink $userlink $comment</li>";
250 * List for archive table items, i.e. revisions deleted via action=delete
252 class RevDel_ArchiveList extends RevDel_RevisionList {
253 public function getType() {
254 return 'archive';
257 public static function getRelationType() {
258 return 'ar_timestamp';
262 * @param $db DatabaseBase
263 * @return mixed
265 public function doQuery( $db ) {
266 $timestamps = array();
267 foreach ( $this->ids as $id ) {
268 $timestamps[] = $db->timestamp( $id );
270 return $db->select( 'archive', '*',
271 array(
272 'ar_namespace' => $this->title->getNamespace(),
273 'ar_title' => $this->title->getDBkey(),
274 'ar_timestamp' => $timestamps
276 __METHOD__,
277 array( 'ORDER BY' => 'ar_timestamp DESC' )
281 public function newItem( $row ) {
282 return new RevDel_ArchiveItem( $this, $row );
285 public function doPreCommitUpdates() {
286 return Status::newGood();
289 public function doPostCommitUpdates() {
290 return Status::newGood();
295 * Item class for a archive table row
297 class RevDel_ArchiveItem extends RevDel_RevisionItem {
298 public function __construct( $list, $row ) {
299 RevDel_Item::__construct( $list, $row );
300 $this->revision = Revision::newFromArchiveRow( $row,
301 array( 'page' => $this->list->title->getArticleId() ) );
304 public function getIdField() {
305 return 'ar_timestamp';
308 public function getTimestampField() {
309 return 'ar_timestamp';
312 public function getAuthorIdField() {
313 return 'ar_user';
316 public function getAuthorNameField() {
317 return 'ar_user_text';
320 public function getId() {
321 # Convert DB timestamp to MW timestamp
322 return $this->revision->getTimestamp();
325 public function setBits( $bits ) {
326 $dbw = wfGetDB( DB_MASTER );
327 $dbw->update( 'archive',
328 array( 'ar_deleted' => $bits ),
329 array(
330 'ar_namespace' => $this->list->title->getNamespace(),
331 'ar_title' => $this->list->title->getDBkey(),
332 // use timestamp for index
333 'ar_timestamp' => $this->row->ar_timestamp,
334 'ar_rev_id' => $this->row->ar_rev_id,
335 'ar_deleted' => $this->getBits()
337 __METHOD__ );
338 return (bool)$dbw->affectedRows();
341 protected function getRevisionLink() {
342 $undelete = SpecialPage::getTitleFor( 'Undelete' );
343 $date = $this->list->getLang()->timeanddate( $this->revision->getTimestamp(), true );
344 if ( $this->isDeleted() && !$this->canViewContent() ) {
345 return $date;
347 return Linker::link( $undelete, $date, array(),
348 array(
349 'target' => $this->list->title->getPrefixedText(),
350 'timestamp' => $this->revision->getTimestamp()
351 ) );
354 protected function getDiffLink() {
355 if ( $this->isDeleted() && !$this->canViewContent() ) {
356 return wfMsgHtml( 'diff' );
358 $undelete = SpecialPage::getTitleFor( 'Undelete' );
359 return Linker::link( $undelete, wfMsgHtml('diff'), array(),
360 array(
361 'target' => $this->list->title->getPrefixedText(),
362 'diff' => 'prev',
363 'timestamp' => $this->revision->getTimestamp()
364 ) );
370 * Item class for a archive table row by ar_rev_id -- actually
371 * used via RevDel_RevisionList.
373 class RevDel_ArchivedRevisionItem extends RevDel_ArchiveItem {
374 public function __construct( $list, $row ) {
375 RevDel_Item::__construct( $list, $row );
377 $this->revision = Revision::newFromArchiveRow( $row,
378 array( 'page' => $this->list->title->getArticleId() ) );
381 public function getIdField() {
382 return 'ar_rev_id';
385 public function getId() {
386 return $this->revision->getId();
389 public function setBits( $bits ) {
390 $dbw = wfGetDB( DB_MASTER );
391 $dbw->update( 'archive',
392 array( 'ar_deleted' => $bits ),
393 array( 'ar_rev_id' => $this->row->ar_rev_id,
394 'ar_deleted' => $this->getBits()
396 __METHOD__ );
397 return (bool)$dbw->affectedRows();
402 * List for oldimage table items
404 class RevDel_FileList extends RevDel_List {
405 public function getType() {
406 return 'oldimage';
409 public static function getRelationType() {
410 return 'oi_archive_name';
413 var $storeBatch, $deleteBatch, $cleanupBatch;
416 * @param $db DatabaseBase
417 * @return mixed
419 public function doQuery( $db ) {
420 $archiveNames = array();
421 foreach( $this->ids as $timestamp ) {
422 $archiveNames[] = $timestamp . '!' . $this->title->getDBkey();
424 return $db->select( 'oldimage', '*',
425 array(
426 'oi_name' => $this->title->getDBkey(),
427 'oi_archive_name' => $archiveNames
429 __METHOD__,
430 array( 'ORDER BY' => 'oi_timestamp DESC' )
434 public function newItem( $row ) {
435 return new RevDel_FileItem( $this, $row );
438 public function clearFileOps() {
439 $this->deleteBatch = array();
440 $this->storeBatch = array();
441 $this->cleanupBatch = array();
444 public function doPreCommitUpdates() {
445 $status = Status::newGood();
446 $repo = RepoGroup::singleton()->getLocalRepo();
447 if ( $this->storeBatch ) {
448 $status->merge( $repo->storeBatch( $this->storeBatch, FileRepo::OVERWRITE_SAME ) );
450 if ( !$status->isOK() ) {
451 return $status;
453 if ( $this->deleteBatch ) {
454 $status->merge( $repo->deleteBatch( $this->deleteBatch ) );
456 if ( !$status->isOK() ) {
457 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
458 // modified (but destined for rollback) causes data loss
459 return $status;
461 if ( $this->cleanupBatch ) {
462 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch ) );
464 return $status;
467 public function doPostCommitUpdates() {
468 $file = wfLocalFile( $this->title );
469 $file->purgeCache();
470 $file->purgeDescription();
471 return Status::newGood();
474 public function getSuppressBit() {
475 return File::DELETED_RESTRICTED;
480 * Item class for an oldimage table row
482 class RevDel_FileItem extends RevDel_Item {
485 * @var File
487 var $file;
489 public function __construct( $list, $row ) {
490 parent::__construct( $list, $row );
491 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
494 public function getIdField() {
495 return 'oi_archive_name';
498 public function getTimestampField() {
499 return 'oi_timestamp';
502 public function getAuthorIdField() {
503 return 'oi_user';
506 public function getAuthorNameField() {
507 return 'oi_user_text';
510 public function getId() {
511 $parts = explode( '!', $this->row->oi_archive_name );
512 return $parts[0];
515 public function canView() {
516 return $this->file->userCan( File::DELETED_RESTRICTED, $this->list->getUser() );
519 public function canViewContent() {
520 return $this->file->userCan( File::DELETED_FILE, $this->list->getUser() );
523 public function getBits() {
524 return $this->file->getVisibility();
527 public function setBits( $bits ) {
528 # Queue the file op
529 # @todo FIXME: Move to LocalFile.php
530 if ( $this->isDeleted() ) {
531 if ( $bits & File::DELETED_FILE ) {
532 # Still deleted
533 } else {
534 # Newly undeleted
535 $key = $this->file->getStorageKey();
536 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
537 $this->list->storeBatch[] = array(
538 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
539 'public',
540 $this->file->getRel()
542 $this->list->cleanupBatch[] = $key;
544 } elseif ( $bits & File::DELETED_FILE ) {
545 # Newly deleted
546 $key = $this->file->getStorageKey();
547 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
548 $this->list->deleteBatch[] = array( $this->file->getRel(), $dstRel );
551 # Do the database operations
552 $dbw = wfGetDB( DB_MASTER );
553 $dbw->update( 'oldimage',
554 array( 'oi_deleted' => $bits ),
555 array(
556 'oi_name' => $this->row->oi_name,
557 'oi_timestamp' => $this->row->oi_timestamp,
558 'oi_deleted' => $this->getBits()
560 __METHOD__
562 return (bool)$dbw->affectedRows();
565 public function isDeleted() {
566 return $this->file->isDeleted( File::DELETED_FILE );
570 * Get the link to the file.
571 * Overridden by RevDel_ArchivedFileItem.
573 protected function getLink() {
574 $date = $this->list->getLang()->timeanddate( $this->file->getTimestamp(), true );
575 if ( $this->isDeleted() ) {
576 # Hidden files...
577 if ( !$this->canViewContent() ) {
578 $link = $date;
579 } else {
580 $revdelete = SpecialPage::getTitleFor( 'Revisiondelete' );
581 $link = Linker::link(
582 $revdelete,
583 $date, array(),
584 array(
585 'target' => $this->list->title->getPrefixedText(),
586 'file' => $this->file->getArchiveName(),
587 'token' => $this->list->getUser()->editToken(
588 $this->file->getArchiveName() )
592 return '<span class="history-deleted">' . $link . '</span>';
593 } else {
594 # Regular files...
595 return Xml::element( 'a', array( 'href' => $this->file->getUrl() ), $date );
599 * Generate a user tool link cluster if the current user is allowed to view it
600 * @return string HTML
602 protected function getUserTools() {
603 if( $this->file->userCan( Revision::DELETED_USER, $this->list->getUser() ) ) {
604 $link = Linker::userLink( $this->file->user, $this->file->user_text ) .
605 Linker::userToolLinks( $this->file->user, $this->file->user_text );
606 } else {
607 $link = wfMsgHtml( 'rev-deleted-user' );
609 if( $this->file->isDeleted( Revision::DELETED_USER ) ) {
610 return '<span class="history-deleted">' . $link . '</span>';
612 return $link;
616 * Wrap and format the file's comment block, if the current
617 * user is allowed to view it.
619 * @return string HTML
621 protected function getComment() {
622 if( $this->file->userCan( File::DELETED_COMMENT, $this->list->getUser() ) ) {
623 $block = Linker::commentBlock( $this->file->description );
624 } else {
625 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
627 if( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
628 return "<span class=\"history-deleted\">$block</span>";
630 return $block;
633 public function getHTML() {
634 $data =
635 wfMsg(
636 'widthheight',
637 $this->list->getLang()->formatNum( $this->file->getWidth() ),
638 $this->list->getLang()->formatNum( $this->file->getHeight() )
640 ' (' .
641 wfMsgExt( 'nbytes', 'parsemag', $this->list->getLang()->formatNum( $this->file->getSize() ) ) .
642 ')';
644 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
645 $data . ' ' . $this->getComment(). '</li>';
650 * List for filearchive table items
652 class RevDel_ArchivedFileList extends RevDel_FileList {
653 public function getType() {
654 return 'filearchive';
657 public static function getRelationType() {
658 return 'fa_id';
662 * @param $db DatabaseBase
663 * @return mixed
665 public function doQuery( $db ) {
666 $ids = array_map( 'intval', $this->ids );
667 return $db->select( 'filearchive', '*',
668 array(
669 'fa_name' => $this->title->getDBkey(),
670 'fa_id' => $ids
672 __METHOD__,
673 array( 'ORDER BY' => 'fa_id DESC' )
677 public function newItem( $row ) {
678 return new RevDel_ArchivedFileItem( $this, $row );
683 * Item class for a filearchive table row
685 class RevDel_ArchivedFileItem extends RevDel_FileItem {
686 public function __construct( $list, $row ) {
687 RevDel_Item::__construct( $list, $row );
688 $this->file = ArchivedFile::newFromRow( $row );
691 public function getIdField() {
692 return 'fa_id';
695 public function getTimestampField() {
696 return 'fa_timestamp';
699 public function getAuthorIdField() {
700 return 'fa_user';
703 public function getAuthorNameField() {
704 return 'fa_user_text';
707 public function getId() {
708 return $this->row->fa_id;
711 public function setBits( $bits ) {
712 $dbw = wfGetDB( DB_MASTER );
713 $dbw->update( 'filearchive',
714 array( 'fa_deleted' => $bits ),
715 array(
716 'fa_id' => $this->row->fa_id,
717 'fa_deleted' => $this->getBits(),
719 __METHOD__
721 return (bool)$dbw->affectedRows();
724 protected function getLink() {
725 $date = $this->list->getLang()->timeanddate( $this->file->getTimestamp(), true );
726 $undelete = SpecialPage::getTitleFor( 'Undelete' );
727 $key = $this->file->getKey();
728 # Hidden files...
729 if( !$this->canViewContent() ) {
730 $link = $date;
731 } else {
732 $link = Linker::link( $undelete, $date, array(),
733 array(
734 'target' => $this->list->title->getPrefixedText(),
735 'file' => $key,
736 'token' => $this->list->getUser()->editToken( $key )
740 if( $this->isDeleted() ) {
741 $link = '<span class="history-deleted">' . $link . '</span>';
743 return $link;
748 * List for logging table items
750 class RevDel_LogList extends RevDel_List {
751 public function getType() {
752 return 'logging';
755 public static function getRelationType() {
756 return 'log_id';
760 * @param $db DatabaseBase
761 * @return mixed
763 public function doQuery( $db ) {
764 $ids = array_map( 'intval', $this->ids );
765 return $db->select( 'logging', '*',
766 array( 'log_id' => $ids ),
767 __METHOD__,
768 array( 'ORDER BY' => 'log_id DESC' )
772 public function newItem( $row ) {
773 return new RevDel_LogItem( $this, $row );
776 public function getSuppressBit() {
777 return Revision::DELETED_RESTRICTED;
780 public function getLogAction() {
781 return 'event';
784 public function getLogParams( $params ) {
785 return array(
786 implode( ',', $params['ids'] ),
787 "ofield={$params['oldBits']}",
788 "nfield={$params['newBits']}"
794 * Item class for a logging table row
796 class RevDel_LogItem extends RevDel_Item {
797 public function getIdField() {
798 return 'log_id';
801 public function getTimestampField() {
802 return 'log_timestamp';
805 public function getAuthorIdField() {
806 return 'log_user';
809 public function getAuthorNameField() {
810 return 'log_user_text';
813 public function canView() {
814 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED, $this->list->getUser() );
817 public function canViewContent() {
818 return true; // none
821 public function getBits() {
822 return $this->row->log_deleted;
825 public function setBits( $bits ) {
826 $dbw = wfGetDB( DB_MASTER );
827 $dbw->update( 'recentchanges',
828 array(
829 'rc_deleted' => $bits,
830 'rc_patrolled' => 1
832 array(
833 'rc_logid' => $this->row->log_id,
834 'rc_timestamp' => $this->row->log_timestamp // index
836 __METHOD__
838 $dbw->update( 'logging',
839 array( 'log_deleted' => $bits ),
840 array(
841 'log_id' => $this->row->log_id,
842 'log_deleted' => $this->getBits()
844 __METHOD__
846 return (bool)$dbw->affectedRows();
849 public function getHTML() {
850 $date = htmlspecialchars( $this->list->getLang()->timeanddate( $this->row->log_timestamp ) );
851 $paramArray = LogPage::extractParams( $this->row->log_params );
852 $title = Title::makeTitle( $this->row->log_namespace, $this->row->log_title );
854 // Log link for this page
855 $loglink = Linker::link(
856 SpecialPage::getTitleFor( 'Log' ),
857 wfMsgHtml( 'log' ),
858 array(),
859 array( 'page' => $title->getPrefixedText() )
861 // Action text
862 if( !$this->canView() ) {
863 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
864 } else {
865 $skin = $this->list->getUser()->getSkin();
866 $action = LogPage::actionText( $this->row->log_type, $this->row->log_action,
867 $title, $skin, $paramArray, true, true );
868 if( $this->row->log_deleted & LogPage::DELETED_ACTION )
869 $action = '<span class="history-deleted">' . $action . '</span>';
871 // User links
872 $userLink = Linker::userLink( $this->row->log_user,
873 User::WhoIs( $this->row->log_user ) );
874 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_USER) ) {
875 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
877 // Comment
878 $comment = $this->list->getLang()->getDirMark() . Linker::commentBlock( $this->row->log_comment );
879 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_COMMENT) ) {
880 $comment = '<span class="history-deleted">' . $comment . '</span>';
882 return "<li>($loglink) $date $userLink $action $comment</li>";