Only whitelist it if QUnit is in that mode though (bug in QUnit?), caused it to repor...
[mediawiki.git] / includes / revisiondelete / RevisionDelete.php
blob9f79837ec6fd20e95d1a6e91808b933759944251
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;
13 var $type = 'revision';
14 var $idField = 'rev_id';
15 var $dateField = 'rev_timestamp';
16 var $authorIdField = 'rev_user';
17 var $authorNameField = 'rev_user_text';
19 /**
20 * @param $db DatabaseBase
21 * @return mixed
23 public function doQuery( $db ) {
24 $ids = array_map( 'intval', $this->ids );
25 $live = $db->select( array('revision','page'), '*',
26 array(
27 'rev_page' => $this->title->getArticleID(),
28 'rev_id' => $ids,
29 'rev_page = page_id'
31 __METHOD__,
32 array( 'ORDER BY' => 'rev_id DESC' )
35 if ( $live->numRows() >= count( $ids ) ) {
36 // All requested revisions are live, keeps things simple!
37 return $live;
40 // Check if any requested revisions are available fully deleted.
41 $archived = $db->select( array( 'archive' ), '*',
42 array(
43 'ar_rev_id' => $ids
45 __METHOD__,
46 array( 'ORDER BY' => 'ar_rev_id DESC' )
49 if ( $archived->numRows() == 0 ) {
50 return $live;
51 } else if ( $live->numRows() == 0 ) {
52 return $archived;
53 } else {
54 // Combine the two! Whee
55 $rows = array();
56 foreach ( $live as $row ) {
57 $rows[$row->rev_id] = $row;
59 foreach ( $archived as $row ) {
60 $rows[$row->ar_rev_id] = $row;
62 krsort( $rows );
63 return new FakeResultWrapper( array_values( $rows ) );
67 public function newItem( $row ) {
68 if ( isset( $row->rev_id ) ) {
69 return new RevDel_RevisionItem( $this, $row );
70 } elseif ( isset( $row->ar_rev_id ) ) {
71 return new RevDel_ArchivedRevisionItem( $this, $row );
72 } else {
73 // This shouldn't happen. :)
74 throw new MWException( 'Invalid row type in RevDel_RevisionList' );
78 public function getCurrent() {
79 if ( is_null( $this->currentRevId ) ) {
80 $dbw = wfGetDB( DB_MASTER );
81 $this->currentRevId = $dbw->selectField(
82 'page', 'page_latest', $this->title->pageCond(), __METHOD__ );
84 return $this->currentRevId;
87 public function getSuppressBit() {
88 return Revision::DELETED_RESTRICTED;
91 public function doPreCommitUpdates() {
92 $this->title->invalidateCache();
93 return Status::newGood();
96 public function doPostCommitUpdates() {
97 $this->title->purgeSquid();
98 // Extensions that require referencing previous revisions may need this
99 wfRunHooks( 'ArticleRevisionVisibilitySet', array( &$this->title ) );
100 return Status::newGood();
105 * Item class for a live revision table row
107 class RevDel_RevisionItem extends RevDel_Item {
108 var $revision;
110 public function __construct( $list, $row ) {
111 parent::__construct( $list, $row );
112 $this->revision = new Revision( $row );
115 public function canView() {
116 return $this->revision->userCan( Revision::DELETED_RESTRICTED );
119 public function canViewContent() {
120 return $this->revision->userCan( Revision::DELETED_TEXT );
123 public function getBits() {
124 return $this->revision->mDeleted;
127 public function setBits( $bits ) {
128 $dbw = wfGetDB( DB_MASTER );
129 // Update revision table
130 $dbw->update( 'revision',
131 array( 'rev_deleted' => $bits ),
132 array(
133 'rev_id' => $this->revision->getId(),
134 'rev_page' => $this->revision->getPage(),
135 'rev_deleted' => $this->getBits()
137 __METHOD__
139 if ( !$dbw->affectedRows() ) {
140 // Concurrent fail!
141 return false;
143 // Update recentchanges table
144 $dbw->update( 'recentchanges',
145 array(
146 'rc_deleted' => $bits,
147 'rc_patrolled' => 1
149 array(
150 'rc_this_oldid' => $this->revision->getId(), // condition
151 // non-unique timestamp index
152 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
154 __METHOD__
156 return true;
159 public function isDeleted() {
160 return $this->revision->isDeleted( Revision::DELETED_TEXT );
163 public function isHideCurrentOp( $newBits ) {
164 return ( $newBits & Revision::DELETED_TEXT )
165 && $this->list->getCurrent() == $this->getId();
169 * Get the HTML link to the revision text.
170 * Overridden by RevDel_ArchiveItem.
172 protected function getRevisionLink() {
173 global $wgLang;
174 $date = $wgLang->timeanddate( $this->revision->getTimestamp(), true );
175 if ( $this->isDeleted() && !$this->canViewContent() ) {
176 return $date;
178 return $this->special->skin->link(
179 $this->list->title,
180 $date,
181 array(),
182 array(
183 'oldid' => $this->revision->getId(),
184 'unhide' => 1
190 * Get the HTML link to the diff.
191 * Overridden by RevDel_ArchiveItem
193 protected function getDiffLink() {
194 if ( $this->isDeleted() && !$this->canViewContent() ) {
195 return wfMsgHtml('diff');
196 } else {
197 return
198 $this->special->skin->link(
199 $this->list->title,
200 wfMsgHtml('diff'),
201 array(),
202 array(
203 'diff' => $this->revision->getId(),
204 'oldid' => 'prev',
205 'unhide' => 1
207 array(
208 'known',
209 'noclasses'
215 public function getHTML() {
216 $difflink = $this->getDiffLink();
217 $revlink = $this->getRevisionLink();
218 $userlink = $this->special->skin->revUserLink( $this->revision );
219 $comment = $this->special->skin->revComment( $this->revision );
220 if ( $this->isDeleted() ) {
221 $revlink = "<span class=\"history-deleted\">$revlink</span>";
223 return "<li>($difflink) $revlink $userlink $comment</li>";
228 * List for archive table items, i.e. revisions deleted via action=delete
230 class RevDel_ArchiveList extends RevDel_RevisionList {
231 var $type = 'archive';
232 var $idField = 'ar_timestamp';
233 var $dateField = 'ar_timestamp';
234 var $authorIdField = 'ar_user';
235 var $authorNameField = 'ar_user_text';
238 * @param $db DatabaseBase
239 * @return mixed
241 public function doQuery( $db ) {
242 $timestamps = array();
243 foreach ( $this->ids as $id ) {
244 $timestamps[] = $db->timestamp( $id );
246 return $db->select( 'archive', '*',
247 array(
248 'ar_namespace' => $this->title->getNamespace(),
249 'ar_title' => $this->title->getDBkey(),
250 'ar_timestamp' => $timestamps
252 __METHOD__,
253 array( 'ORDER BY' => 'ar_timestamp DESC' )
257 public function newItem( $row ) {
258 return new RevDel_ArchiveItem( $this, $row );
261 public function doPreCommitUpdates() {
262 return Status::newGood();
265 public function doPostCommitUpdates() {
266 return Status::newGood();
271 * Item class for a archive table row
273 class RevDel_ArchiveItem extends RevDel_RevisionItem {
274 public function __construct( $list, $row ) {
275 RevDel_Item::__construct( $list, $row );
276 $this->revision = Revision::newFromArchiveRow( $row,
277 array( 'page' => $this->list->title->getArticleId() ) );
280 public function getId() {
281 # Convert DB timestamp to MW timestamp
282 return $this->revision->getTimestamp();
285 public function setBits( $bits ) {
286 $dbw = wfGetDB( DB_MASTER );
287 $dbw->update( 'archive',
288 array( 'ar_deleted' => $bits ),
289 array( 'ar_namespace' => $this->list->title->getNamespace(),
290 'ar_title' => $this->list->title->getDBkey(),
291 // use timestamp for index
292 'ar_timestamp' => $this->row->ar_timestamp,
293 'ar_rev_id' => $this->row->ar_rev_id,
294 'ar_deleted' => $this->getBits()
296 __METHOD__ );
297 return (bool)$dbw->affectedRows();
300 protected function getRevisionLink() {
301 global $wgLang;
302 $undelete = SpecialPage::getTitleFor( 'Undelete' );
303 $date = $wgLang->timeanddate( $this->revision->getTimestamp(), true );
304 if ( $this->isDeleted() && !$this->canViewContent() ) {
305 return $date;
307 return $this->special->skin->link( $undelete, $date, array(),
308 array(
309 'target' => $this->list->title->getPrefixedText(),
310 'timestamp' => $this->revision->getTimestamp()
311 ) );
314 protected function getDiffLink() {
315 if ( $this->isDeleted() && !$this->canViewContent() ) {
316 return wfMsgHtml( 'diff' );
318 $undelete = SpecialPage::getTitleFor( 'Undelete' );
319 return $this->special->skin->link( $undelete, wfMsgHtml('diff'), array(),
320 array(
321 'target' => $this->list->title->getPrefixedText(),
322 'diff' => 'prev',
323 'timestamp' => $this->revision->getTimestamp()
324 ) );
330 * Item class for a archive table row by ar_rev_id -- actually
331 * used via RevDel_RevisionList.
333 class RevDel_ArchivedRevisionItem extends RevDel_ArchiveItem {
334 public function __construct( $list, $row ) {
335 RevDel_Item::__construct( $list, $row );
337 $this->revision = Revision::newFromArchiveRow( $row,
338 array( 'page' => $this->list->title->getArticleId() ) );
341 public function getId() {
342 return $this->revision->getId();
345 public function setBits( $bits ) {
346 $dbw = wfGetDB( DB_MASTER );
347 $dbw->update( 'archive',
348 array( 'ar_deleted' => $bits ),
349 array( 'ar_rev_id' => $this->row->ar_rev_id,
350 'ar_deleted' => $this->getBits()
352 __METHOD__ );
353 return (bool)$dbw->affectedRows();
358 * List for oldimage table items
360 class RevDel_FileList extends RevDel_List {
361 var $type = 'oldimage';
362 var $idField = 'oi_archive_name';
363 var $dateField = 'oi_timestamp';
364 var $authorIdField = 'oi_user';
365 var $authorNameField = 'oi_user_text';
366 var $storeBatch, $deleteBatch, $cleanupBatch;
369 * @param $db DatabaseBase
370 * @return mixed
372 public function doQuery( $db ) {
373 $archiveNames = array();
374 foreach( $this->ids as $timestamp ) {
375 $archiveNames[] = $timestamp . '!' . $this->title->getDBkey();
377 return $db->select( 'oldimage', '*',
378 array(
379 'oi_name' => $this->title->getDBkey(),
380 'oi_archive_name' => $archiveNames
382 __METHOD__,
383 array( 'ORDER BY' => 'oi_timestamp DESC' )
387 public function newItem( $row ) {
388 return new RevDel_FileItem( $this, $row );
391 public function clearFileOps() {
392 $this->deleteBatch = array();
393 $this->storeBatch = array();
394 $this->cleanupBatch = array();
397 public function doPreCommitUpdates() {
398 $status = Status::newGood();
399 $repo = RepoGroup::singleton()->getLocalRepo();
400 if ( $this->storeBatch ) {
401 $status->merge( $repo->storeBatch( $this->storeBatch, FileRepo::OVERWRITE_SAME ) );
403 if ( !$status->isOK() ) {
404 return $status;
406 if ( $this->deleteBatch ) {
407 $status->merge( $repo->deleteBatch( $this->deleteBatch ) );
409 if ( !$status->isOK() ) {
410 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
411 // modified (but destined for rollback) causes data loss
412 return $status;
414 if ( $this->cleanupBatch ) {
415 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch ) );
417 return $status;
420 public function doPostCommitUpdates() {
421 $file = wfLocalFile( $this->title );
422 $file->purgeCache();
423 $file->purgeDescription();
424 return Status::newGood();
427 public function getSuppressBit() {
428 return File::DELETED_RESTRICTED;
433 * Item class for an oldimage table row
435 class RevDel_FileItem extends RevDel_Item {
438 * @var File
440 var $file;
442 public function __construct( $list, $row ) {
443 parent::__construct( $list, $row );
444 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
447 public function getId() {
448 $parts = explode( '!', $this->row->oi_archive_name );
449 return $parts[0];
452 public function canView() {
453 return $this->file->userCan( File::DELETED_RESTRICTED );
456 public function canViewContent() {
457 return $this->file->userCan( File::DELETED_FILE );
460 public function getBits() {
461 return $this->file->getVisibility();
464 public function setBits( $bits ) {
465 # Queue the file op
466 # @todo FIXME: Move to LocalFile.php
467 if ( $this->isDeleted() ) {
468 if ( $bits & File::DELETED_FILE ) {
469 # Still deleted
470 } else {
471 # Newly undeleted
472 $key = $this->file->getStorageKey();
473 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
474 $this->list->storeBatch[] = array(
475 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
476 'public',
477 $this->file->getRel()
479 $this->list->cleanupBatch[] = $key;
481 } elseif ( $bits & File::DELETED_FILE ) {
482 # Newly deleted
483 $key = $this->file->getStorageKey();
484 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
485 $this->list->deleteBatch[] = array( $this->file->getRel(), $dstRel );
488 # Do the database operations
489 $dbw = wfGetDB( DB_MASTER );
490 $dbw->update( 'oldimage',
491 array( 'oi_deleted' => $bits ),
492 array(
493 'oi_name' => $this->row->oi_name,
494 'oi_timestamp' => $this->row->oi_timestamp,
495 'oi_deleted' => $this->getBits()
497 __METHOD__
499 return (bool)$dbw->affectedRows();
502 public function isDeleted() {
503 return $this->file->isDeleted( File::DELETED_FILE );
507 * Get the link to the file.
508 * Overridden by RevDel_ArchivedFileItem.
510 protected function getLink() {
511 global $wgLang, $wgUser;
512 $date = $wgLang->timeanddate( $this->file->getTimestamp(), true );
513 if ( $this->isDeleted() ) {
514 # Hidden files...
515 if ( !$this->canViewContent() ) {
516 $link = $date;
517 } else {
518 $link = $this->special->skin->link(
519 $this->special->getTitle(),
520 $date, array(),
521 array(
522 'target' => $this->list->title->getPrefixedText(),
523 'file' => $this->file->getArchiveName(),
524 'token' => $wgUser->editToken( $this->file->getArchiveName() )
528 return '<span class="history-deleted">' . $link . '</span>';
529 } else {
530 # Regular files...
531 return Xml::element( 'a', array( 'href' => $this->file->getUrl() ), $date );
535 * Generate a user tool link cluster if the current user is allowed to view it
536 * @return string HTML
538 protected function getUserTools() {
539 if( $this->file->userCan( Revision::DELETED_USER ) ) {
540 $link = $this->special->skin->userLink( $this->file->user, $this->file->user_text ) .
541 $this->special->skin->userToolLinks( $this->file->user, $this->file->user_text );
542 } else {
543 $link = wfMsgHtml( 'rev-deleted-user' );
545 if( $this->file->isDeleted( Revision::DELETED_USER ) ) {
546 return '<span class="history-deleted">' . $link . '</span>';
548 return $link;
552 * Wrap and format the file's comment block, if the current
553 * user is allowed to view it.
555 * @return string HTML
557 protected function getComment() {
558 if( $this->file->userCan( File::DELETED_COMMENT ) ) {
559 $block = $this->special->skin->commentBlock( $this->file->description );
560 } else {
561 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
563 if( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
564 return "<span class=\"history-deleted\">$block</span>";
566 return $block;
569 public function getHTML() {
570 global $wgLang;
571 $data =
572 wfMsg(
573 'widthheight',
574 $wgLang->formatNum( $this->file->getWidth() ),
575 $wgLang->formatNum( $this->file->getHeight() )
577 ' (' .
578 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file->getSize() ) ) .
579 ')';
581 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
582 $data . ' ' . $this->getComment(). '</li>';
587 * List for filearchive table items
589 class RevDel_ArchivedFileList extends RevDel_FileList {
590 var $type = 'filearchive';
591 var $idField = 'fa_id';
592 var $dateField = 'fa_timestamp';
593 var $authorIdField = 'fa_user';
594 var $authorNameField = 'fa_user_text';
597 * @param $db DatabaseBase
598 * @return mixed
600 public function doQuery( $db ) {
601 $ids = array_map( 'intval', $this->ids );
602 return $db->select( 'filearchive', '*',
603 array(
604 'fa_name' => $this->title->getDBkey(),
605 'fa_id' => $ids
607 __METHOD__,
608 array( 'ORDER BY' => 'fa_id DESC' )
612 public function newItem( $row ) {
613 return new RevDel_ArchivedFileItem( $this, $row );
618 * Item class for a filearchive table row
620 class RevDel_ArchivedFileItem extends RevDel_FileItem {
621 public function __construct( $list, $row ) {
622 RevDel_Item::__construct( $list, $row );
623 $this->file = ArchivedFile::newFromRow( $row );
626 public function getId() {
627 return $this->row->fa_id;
630 public function setBits( $bits ) {
631 $dbw = wfGetDB( DB_MASTER );
632 $dbw->update( 'filearchive',
633 array( 'fa_deleted' => $bits ),
634 array(
635 'fa_id' => $this->row->fa_id,
636 'fa_deleted' => $this->getBits(),
638 __METHOD__
640 return (bool)$dbw->affectedRows();
643 protected function getLink() {
644 global $wgLang, $wgUser;
645 $date = $wgLang->timeanddate( $this->file->getTimestamp(), true );
646 $undelete = SpecialPage::getTitleFor( 'Undelete' );
647 $key = $this->file->getKey();
648 # Hidden files...
649 if( !$this->canViewContent() ) {
650 $link = $date;
651 } else {
652 $link = $this->special->skin->link( $undelete, $date, array(),
653 array(
654 'target' => $this->list->title->getPrefixedText(),
655 'file' => $key,
656 'token' => $wgUser->editToken( $key )
660 if( $this->isDeleted() ) {
661 $link = '<span class="history-deleted">' . $link . '</span>';
663 return $link;
668 * List for logging table items
670 class RevDel_LogList extends RevDel_List {
671 var $type = 'logging';
672 var $idField = 'log_id';
673 var $dateField = 'log_timestamp';
674 var $authorIdField = 'log_user';
675 var $authorNameField = 'log_user_text';
678 * @param $db DatabaseBase
679 * @return mixed
681 public function doQuery( $db ) {
682 $ids = array_map( 'intval', $this->ids );
683 return $db->select( 'logging', '*',
684 array( 'log_id' => $ids ),
685 __METHOD__,
686 array( 'ORDER BY' => 'log_id DESC' )
690 public function newItem( $row ) {
691 return new RevDel_LogItem( $this, $row );
694 public function getSuppressBit() {
695 return Revision::DELETED_RESTRICTED;
698 public function getLogAction() {
699 return 'event';
702 public function getLogParams( $params ) {
703 return array(
704 implode( ',', $params['ids'] ),
705 "ofield={$params['oldBits']}",
706 "nfield={$params['newBits']}"
712 * Item class for a logging table row
714 class RevDel_LogItem extends RevDel_Item {
715 public function canView() {
716 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED );
719 public function canViewContent() {
720 return true; // none
723 public function getBits() {
724 return $this->row->log_deleted;
727 public function setBits( $bits ) {
728 $dbw = wfGetDB( DB_MASTER );
729 $dbw->update( 'recentchanges',
730 array(
731 'rc_deleted' => $bits,
732 'rc_patrolled' => 1
734 array(
735 'rc_logid' => $this->row->log_id,
736 'rc_timestamp' => $this->row->log_timestamp // index
738 __METHOD__
740 $dbw->update( 'logging',
741 array( 'log_deleted' => $bits ),
742 array(
743 'log_id' => $this->row->log_id,
744 'log_deleted' => $this->getBits()
746 __METHOD__
748 return (bool)$dbw->affectedRows();
751 public function getHTML() {
752 global $wgLang;
754 $date = htmlspecialchars( $wgLang->timeanddate( $this->row->log_timestamp ) );
755 $paramArray = LogPage::extractParams( $this->row->log_params );
756 $title = Title::makeTitle( $this->row->log_namespace, $this->row->log_title );
758 // Log link for this page
759 $loglink = $this->special->skin->link(
760 SpecialPage::getTitleFor( 'Log' ),
761 wfMsgHtml( 'log' ),
762 array(),
763 array( 'page' => $title->getPrefixedText() )
765 // Action text
766 if( !$this->canView() ) {
767 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
768 } else {
769 $action = LogPage::actionText( $this->row->log_type, $this->row->log_action, $title,
770 $this->special->skin, $paramArray, true, true );
771 if( $this->row->log_deleted & LogPage::DELETED_ACTION )
772 $action = '<span class="history-deleted">' . $action . '</span>';
774 // User links
775 $userLink = $this->special->skin->userLink( $this->row->log_user,
776 User::WhoIs( $this->row->log_user ) );
777 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_USER) ) {
778 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
780 // Comment
781 $comment = $wgLang->getDirMark() . $this->special->skin->commentBlock( $this->row->log_comment );
782 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_COMMENT) ) {
783 $comment = '<span class="history-deleted">' . $comment . '</span>';
785 return "<li>($loglink) $date $userLink $action $comment</li>";