Localisation updates for core and extension messages from translatewiki.net
[mediawiki.git] / includes / revisiondelete / RevisionDelete.php
blob072aef1968d5159182326aca71eaa9b2322d933b
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.
194 * @return string
196 protected function getRevisionLink() {
197 $date = $this->list->getLanguage()->timeanddate( $this->revision->getTimestamp(), true );
198 if ( $this->isDeleted() && !$this->canViewContent() ) {
199 return $date;
201 return Linker::link(
202 $this->list->title,
203 $date,
204 array(),
205 array(
206 'oldid' => $this->revision->getId(),
207 'unhide' => 1
213 * Get the HTML link to the diff.
214 * Overridden by RevDel_ArchiveItem
215 * @return string
217 protected function getDiffLink() {
218 if ( $this->isDeleted() && !$this->canViewContent() ) {
219 return wfMsgHtml('diff');
220 } else {
221 return
222 Linker::link(
223 $this->list->title,
224 wfMsgHtml('diff'),
225 array(),
226 array(
227 'diff' => $this->revision->getId(),
228 'oldid' => 'prev',
229 'unhide' => 1
231 array(
232 'known',
233 'noclasses'
239 public function getHTML() {
240 $difflink = $this->getDiffLink();
241 $revlink = $this->getRevisionLink();
242 $userlink = Linker::revUserLink( $this->revision );
243 $comment = Linker::revComment( $this->revision );
244 if ( $this->isDeleted() ) {
245 $revlink = "<span class=\"history-deleted\">$revlink</span>";
247 return "<li>($difflink) $revlink $userlink $comment</li>";
252 * List for archive table items, i.e. revisions deleted via action=delete
254 class RevDel_ArchiveList extends RevDel_RevisionList {
255 public function getType() {
256 return 'archive';
259 public static function getRelationType() {
260 return 'ar_timestamp';
264 * @param $db DatabaseBase
265 * @return mixed
267 public function doQuery( $db ) {
268 $timestamps = array();
269 foreach ( $this->ids as $id ) {
270 $timestamps[] = $db->timestamp( $id );
272 return $db->select( 'archive', '*',
273 array(
274 'ar_namespace' => $this->title->getNamespace(),
275 'ar_title' => $this->title->getDBkey(),
276 'ar_timestamp' => $timestamps
278 __METHOD__,
279 array( 'ORDER BY' => 'ar_timestamp DESC' )
283 public function newItem( $row ) {
284 return new RevDel_ArchiveItem( $this, $row );
287 public function doPreCommitUpdates() {
288 return Status::newGood();
291 public function doPostCommitUpdates() {
292 return Status::newGood();
297 * Item class for a archive table row
299 class RevDel_ArchiveItem extends RevDel_RevisionItem {
300 public function __construct( $list, $row ) {
301 RevDel_Item::__construct( $list, $row );
302 $this->revision = Revision::newFromArchiveRow( $row,
303 array( 'page' => $this->list->title->getArticleId() ) );
306 public function getIdField() {
307 return 'ar_timestamp';
310 public function getTimestampField() {
311 return 'ar_timestamp';
314 public function getAuthorIdField() {
315 return 'ar_user';
318 public function getAuthorNameField() {
319 return 'ar_user_text';
322 public function getId() {
323 # Convert DB timestamp to MW timestamp
324 return $this->revision->getTimestamp();
327 public function setBits( $bits ) {
328 $dbw = wfGetDB( DB_MASTER );
329 $dbw->update( 'archive',
330 array( 'ar_deleted' => $bits ),
331 array(
332 'ar_namespace' => $this->list->title->getNamespace(),
333 'ar_title' => $this->list->title->getDBkey(),
334 // use timestamp for index
335 'ar_timestamp' => $this->row->ar_timestamp,
336 'ar_rev_id' => $this->row->ar_rev_id,
337 'ar_deleted' => $this->getBits()
339 __METHOD__ );
340 return (bool)$dbw->affectedRows();
343 protected function getRevisionLink() {
344 $undelete = SpecialPage::getTitleFor( 'Undelete' );
345 $date = $this->list->getLanguage()->timeanddate( $this->revision->getTimestamp(), true );
346 if ( $this->isDeleted() && !$this->canViewContent() ) {
347 return $date;
349 return Linker::link( $undelete, $date, array(),
350 array(
351 'target' => $this->list->title->getPrefixedText(),
352 'timestamp' => $this->revision->getTimestamp()
353 ) );
356 protected function getDiffLink() {
357 if ( $this->isDeleted() && !$this->canViewContent() ) {
358 return wfMsgHtml( 'diff' );
360 $undelete = SpecialPage::getTitleFor( 'Undelete' );
361 return Linker::link( $undelete, wfMsgHtml('diff'), array(),
362 array(
363 'target' => $this->list->title->getPrefixedText(),
364 'diff' => 'prev',
365 'timestamp' => $this->revision->getTimestamp()
366 ) );
372 * Item class for a archive table row by ar_rev_id -- actually
373 * used via RevDel_RevisionList.
375 class RevDel_ArchivedRevisionItem extends RevDel_ArchiveItem {
376 public function __construct( $list, $row ) {
377 RevDel_Item::__construct( $list, $row );
379 $this->revision = Revision::newFromArchiveRow( $row,
380 array( 'page' => $this->list->title->getArticleId() ) );
383 public function getIdField() {
384 return 'ar_rev_id';
387 public function getId() {
388 return $this->revision->getId();
391 public function setBits( $bits ) {
392 $dbw = wfGetDB( DB_MASTER );
393 $dbw->update( 'archive',
394 array( 'ar_deleted' => $bits ),
395 array( 'ar_rev_id' => $this->row->ar_rev_id,
396 'ar_deleted' => $this->getBits()
398 __METHOD__ );
399 return (bool)$dbw->affectedRows();
404 * List for oldimage table items
406 class RevDel_FileList extends RevDel_List {
407 public function getType() {
408 return 'oldimage';
411 public static function getRelationType() {
412 return 'oi_archive_name';
415 var $storeBatch, $deleteBatch, $cleanupBatch;
418 * @param $db DatabaseBase
419 * @return mixed
421 public function doQuery( $db ) {
422 $archiveNames = array();
423 foreach( $this->ids as $timestamp ) {
424 $archiveNames[] = $timestamp . '!' . $this->title->getDBkey();
426 return $db->select( 'oldimage', '*',
427 array(
428 'oi_name' => $this->title->getDBkey(),
429 'oi_archive_name' => $archiveNames
431 __METHOD__,
432 array( 'ORDER BY' => 'oi_timestamp DESC' )
436 public function newItem( $row ) {
437 return new RevDel_FileItem( $this, $row );
440 public function clearFileOps() {
441 $this->deleteBatch = array();
442 $this->storeBatch = array();
443 $this->cleanupBatch = array();
446 public function doPreCommitUpdates() {
447 $status = Status::newGood();
448 $repo = RepoGroup::singleton()->getLocalRepo();
449 if ( $this->storeBatch ) {
450 $status->merge( $repo->storeBatch( $this->storeBatch, FileRepo::OVERWRITE_SAME ) );
452 if ( !$status->isOK() ) {
453 return $status;
455 if ( $this->deleteBatch ) {
456 $status->merge( $repo->deleteBatch( $this->deleteBatch ) );
458 if ( !$status->isOK() ) {
459 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
460 // modified (but destined for rollback) causes data loss
461 return $status;
463 if ( $this->cleanupBatch ) {
464 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch ) );
466 return $status;
469 public function doPostCommitUpdates() {
470 $file = wfLocalFile( $this->title );
471 $file->purgeCache();
472 $file->purgeDescription();
473 return Status::newGood();
476 public function getSuppressBit() {
477 return File::DELETED_RESTRICTED;
482 * Item class for an oldimage table row
484 class RevDel_FileItem extends RevDel_Item {
487 * @var File
489 var $file;
491 public function __construct( $list, $row ) {
492 parent::__construct( $list, $row );
493 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
496 public function getIdField() {
497 return 'oi_archive_name';
500 public function getTimestampField() {
501 return 'oi_timestamp';
504 public function getAuthorIdField() {
505 return 'oi_user';
508 public function getAuthorNameField() {
509 return 'oi_user_text';
512 public function getId() {
513 $parts = explode( '!', $this->row->oi_archive_name );
514 return $parts[0];
517 public function canView() {
518 return $this->file->userCan( File::DELETED_RESTRICTED, $this->list->getUser() );
521 public function canViewContent() {
522 return $this->file->userCan( File::DELETED_FILE, $this->list->getUser() );
525 public function getBits() {
526 return $this->file->getVisibility();
529 public function setBits( $bits ) {
530 # Queue the file op
531 # @todo FIXME: Move to LocalFile.php
532 if ( $this->isDeleted() ) {
533 if ( $bits & File::DELETED_FILE ) {
534 # Still deleted
535 } else {
536 # Newly undeleted
537 $key = $this->file->getStorageKey();
538 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
539 $this->list->storeBatch[] = array(
540 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
541 'public',
542 $this->file->getRel()
544 $this->list->cleanupBatch[] = $key;
546 } elseif ( $bits & File::DELETED_FILE ) {
547 # Newly deleted
548 $key = $this->file->getStorageKey();
549 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
550 $this->list->deleteBatch[] = array( $this->file->getRel(), $dstRel );
553 # Do the database operations
554 $dbw = wfGetDB( DB_MASTER );
555 $dbw->update( 'oldimage',
556 array( 'oi_deleted' => $bits ),
557 array(
558 'oi_name' => $this->row->oi_name,
559 'oi_timestamp' => $this->row->oi_timestamp,
560 'oi_deleted' => $this->getBits()
562 __METHOD__
564 return (bool)$dbw->affectedRows();
567 public function isDeleted() {
568 return $this->file->isDeleted( File::DELETED_FILE );
572 * Get the link to the file.
573 * Overridden by RevDel_ArchivedFileItem.
574 * @return string
576 protected function getLink() {
577 $date = $this->list->getLanguage()->timeanddate( $this->file->getTimestamp(), true );
578 if ( $this->isDeleted() ) {
579 # Hidden files...
580 if ( !$this->canViewContent() ) {
581 $link = $date;
582 } else {
583 $revdelete = SpecialPage::getTitleFor( 'Revisiondelete' );
584 $link = Linker::link(
585 $revdelete,
586 $date, array(),
587 array(
588 'target' => $this->list->title->getPrefixedText(),
589 'file' => $this->file->getArchiveName(),
590 'token' => $this->list->getUser()->getEditToken(
591 $this->file->getArchiveName() )
595 return '<span class="history-deleted">' . $link . '</span>';
596 } else {
597 # Regular files...
598 return Xml::element( 'a', array( 'href' => $this->file->getUrl() ), $date );
602 * Generate a user tool link cluster if the current user is allowed to view it
603 * @return string HTML
605 protected function getUserTools() {
606 if( $this->file->userCan( Revision::DELETED_USER, $this->list->getUser() ) ) {
607 $link = Linker::userLink( $this->file->user, $this->file->user_text ) .
608 Linker::userToolLinks( $this->file->user, $this->file->user_text );
609 } else {
610 $link = wfMsgHtml( 'rev-deleted-user' );
612 if( $this->file->isDeleted( Revision::DELETED_USER ) ) {
613 return '<span class="history-deleted">' . $link . '</span>';
615 return $link;
619 * Wrap and format the file's comment block, if the current
620 * user is allowed to view it.
622 * @return string HTML
624 protected function getComment() {
625 if( $this->file->userCan( File::DELETED_COMMENT, $this->list->getUser() ) ) {
626 $block = Linker::commentBlock( $this->file->description );
627 } else {
628 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
630 if( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
631 return "<span class=\"history-deleted\">$block</span>";
633 return $block;
636 public function getHTML() {
637 $data =
638 wfMsg(
639 'widthheight',
640 $this->list->getLanguage()->formatNum( $this->file->getWidth() ),
641 $this->list->getLanguage()->formatNum( $this->file->getHeight() )
643 ' (' .
644 wfMsgExt( 'nbytes', 'parsemag', $this->list->getLanguage()->formatNum( $this->file->getSize() ) ) .
645 ')';
647 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
648 $data . ' ' . $this->getComment(). '</li>';
653 * List for filearchive table items
655 class RevDel_ArchivedFileList extends RevDel_FileList {
656 public function getType() {
657 return 'filearchive';
660 public static function getRelationType() {
661 return 'fa_id';
665 * @param $db DatabaseBase
666 * @return mixed
668 public function doQuery( $db ) {
669 $ids = array_map( 'intval', $this->ids );
670 return $db->select( 'filearchive', '*',
671 array(
672 'fa_name' => $this->title->getDBkey(),
673 'fa_id' => $ids
675 __METHOD__,
676 array( 'ORDER BY' => 'fa_id DESC' )
680 public function newItem( $row ) {
681 return new RevDel_ArchivedFileItem( $this, $row );
686 * Item class for a filearchive table row
688 class RevDel_ArchivedFileItem extends RevDel_FileItem {
689 public function __construct( $list, $row ) {
690 RevDel_Item::__construct( $list, $row );
691 $this->file = ArchivedFile::newFromRow( $row );
694 public function getIdField() {
695 return 'fa_id';
698 public function getTimestampField() {
699 return 'fa_timestamp';
702 public function getAuthorIdField() {
703 return 'fa_user';
706 public function getAuthorNameField() {
707 return 'fa_user_text';
710 public function getId() {
711 return $this->row->fa_id;
714 public function setBits( $bits ) {
715 $dbw = wfGetDB( DB_MASTER );
716 $dbw->update( 'filearchive',
717 array( 'fa_deleted' => $bits ),
718 array(
719 'fa_id' => $this->row->fa_id,
720 'fa_deleted' => $this->getBits(),
722 __METHOD__
724 return (bool)$dbw->affectedRows();
727 protected function getLink() {
728 $date = $this->list->getLanguage()->timeanddate( $this->file->getTimestamp(), true );
729 $undelete = SpecialPage::getTitleFor( 'Undelete' );
730 $key = $this->file->getKey();
731 # Hidden files...
732 if( !$this->canViewContent() ) {
733 $link = $date;
734 } else {
735 $link = Linker::link( $undelete, $date, array(),
736 array(
737 'target' => $this->list->title->getPrefixedText(),
738 'file' => $key,
739 'token' => $this->list->getUser()->getEditToken( $key )
743 if( $this->isDeleted() ) {
744 $link = '<span class="history-deleted">' . $link . '</span>';
746 return $link;
751 * List for logging table items
753 class RevDel_LogList extends RevDel_List {
754 public function getType() {
755 return 'logging';
758 public static function getRelationType() {
759 return 'log_id';
763 * @param $db DatabaseBase
764 * @return mixed
766 public function doQuery( $db ) {
767 $ids = array_map( 'intval', $this->ids );
768 return $db->select( 'logging', '*',
769 array( 'log_id' => $ids ),
770 __METHOD__,
771 array( 'ORDER BY' => 'log_id DESC' )
775 public function newItem( $row ) {
776 return new RevDel_LogItem( $this, $row );
779 public function getSuppressBit() {
780 return Revision::DELETED_RESTRICTED;
783 public function getLogAction() {
784 return 'event';
787 public function getLogParams( $params ) {
788 return array(
789 implode( ',', $params['ids'] ),
790 "ofield={$params['oldBits']}",
791 "nfield={$params['newBits']}"
797 * Item class for a logging table row
799 class RevDel_LogItem extends RevDel_Item {
800 public function getIdField() {
801 return 'log_id';
804 public function getTimestampField() {
805 return 'log_timestamp';
808 public function getAuthorIdField() {
809 return 'log_user';
812 public function getAuthorNameField() {
813 return 'log_user_text';
816 public function canView() {
817 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED, $this->list->getUser() );
820 public function canViewContent() {
821 return true; // none
824 public function getBits() {
825 return $this->row->log_deleted;
828 public function setBits( $bits ) {
829 $dbw = wfGetDB( DB_MASTER );
830 $dbw->update( 'recentchanges',
831 array(
832 'rc_deleted' => $bits,
833 'rc_patrolled' => 1
835 array(
836 'rc_logid' => $this->row->log_id,
837 'rc_timestamp' => $this->row->log_timestamp // index
839 __METHOD__
841 $dbw->update( 'logging',
842 array( 'log_deleted' => $bits ),
843 array(
844 'log_id' => $this->row->log_id,
845 'log_deleted' => $this->getBits()
847 __METHOD__
849 return (bool)$dbw->affectedRows();
852 public function getHTML() {
853 $date = htmlspecialchars( $this->list->getLanguage()->timeanddate( $this->row->log_timestamp ) );
854 $title = Title::makeTitle( $this->row->log_namespace, $this->row->log_title );
855 $formatter = LogFormatter::newFromRow( $this->row );
856 $formatter->setAudience( LogFormatter::FOR_THIS_USER );
858 // Log link for this page
859 $loglink = Linker::link(
860 SpecialPage::getTitleFor( 'Log' ),
861 wfMsgHtml( 'log' ),
862 array(),
863 array( 'page' => $title->getPrefixedText() )
865 // User links and action text
866 $action = $formatter->getActionText();
867 // Comment
868 $comment = $this->list->getLanguage()->getDirMark() . Linker::commentBlock( $this->row->log_comment );
869 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_COMMENT) ) {
870 $comment = '<span class="history-deleted">' . $comment . '</span>';
873 return "<li>($loglink) $date $action $comment</li>";