Bump trunk version since we've branched
[mediawiki.git] / includes / revisiondelete / RevisionDelete.php
blobc331ec61627443972826fd2e16710078ed761789
1 <?php
2 /**
3 * List for revision table items
4 */
5 class RevDel_RevisionList extends RevDel_List {
6 var $currentRevId;
7 var $type = 'revision';
8 var $idField = 'rev_id';
9 var $dateField = 'rev_timestamp';
10 var $authorIdField = 'rev_user';
11 var $authorNameField = 'rev_user_text';
13 /**
14 * @param $db DatabaseBase
15 * @return mixed
17 public function doQuery( $db ) {
18 $ids = array_map( 'intval', $this->ids );
19 return $db->select( array('revision','page'), '*',
20 array(
21 'rev_page' => $this->title->getArticleID(),
22 'rev_id' => $ids,
23 'rev_page = page_id'
25 __METHOD__,
26 array( 'ORDER BY' => 'rev_id DESC' )
30 public function newItem( $row ) {
31 return new RevDel_RevisionItem( $this, $row );
34 public function getCurrent() {
35 if ( is_null( $this->currentRevId ) ) {
36 $dbw = wfGetDB( DB_MASTER );
37 $this->currentRevId = $dbw->selectField(
38 'page', 'page_latest', $this->title->pageCond(), __METHOD__ );
40 return $this->currentRevId;
43 public function getSuppressBit() {
44 return Revision::DELETED_RESTRICTED;
47 public function doPreCommitUpdates() {
48 $this->title->invalidateCache();
49 return Status::newGood();
52 public function doPostCommitUpdates() {
53 $this->title->purgeSquid();
54 // Extensions that require referencing previous revisions may need this
55 wfRunHooks( 'ArticleRevisionVisibilitySet', array( &$this->title ) );
56 return Status::newGood();
60 /**
61 * Item class for a revision table row
63 class RevDel_RevisionItem extends RevDel_Item {
64 var $revision;
66 public function __construct( $list, $row ) {
67 parent::__construct( $list, $row );
68 $this->revision = new Revision( $row );
71 public function canView() {
72 return $this->revision->userCan( Revision::DELETED_RESTRICTED );
75 public function canViewContent() {
76 return $this->revision->userCan( Revision::DELETED_TEXT );
79 public function getBits() {
80 return $this->revision->mDeleted;
83 public function setBits( $bits ) {
84 $dbw = wfGetDB( DB_MASTER );
85 // Update revision table
86 $dbw->update( 'revision',
87 array( 'rev_deleted' => $bits ),
88 array(
89 'rev_id' => $this->revision->getId(),
90 'rev_page' => $this->revision->getPage(),
91 'rev_deleted' => $this->getBits()
93 __METHOD__
95 if ( !$dbw->affectedRows() ) {
96 // Concurrent fail!
97 return false;
99 // Update recentchanges table
100 $dbw->update( 'recentchanges',
101 array(
102 'rc_deleted' => $bits,
103 'rc_patrolled' => 1
105 array(
106 'rc_this_oldid' => $this->revision->getId(), // condition
107 // non-unique timestamp index
108 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
110 __METHOD__
112 return true;
115 public function isDeleted() {
116 return $this->revision->isDeleted( Revision::DELETED_TEXT );
119 public function isHideCurrentOp( $newBits ) {
120 return ( $newBits & Revision::DELETED_TEXT )
121 && $this->list->getCurrent() == $this->getId();
125 * Get the HTML link to the revision text.
126 * Overridden by RevDel_ArchiveItem.
128 protected function getRevisionLink() {
129 global $wgLang;
130 $date = $wgLang->timeanddate( $this->revision->getTimestamp(), true );
131 if ( $this->isDeleted() && !$this->canViewContent() ) {
132 return $date;
134 return $this->special->skin->link(
135 $this->list->title,
136 $date,
137 array(),
138 array(
139 'oldid' => $this->revision->getId(),
140 'unhide' => 1
146 * Get the HTML link to the diff.
147 * Overridden by RevDel_ArchiveItem
149 protected function getDiffLink() {
150 if ( $this->isDeleted() && !$this->canViewContent() ) {
151 return wfMsgHtml('diff');
152 } else {
153 return
154 $this->special->skin->link(
155 $this->list->title,
156 wfMsgHtml('diff'),
157 array(),
158 array(
159 'diff' => $this->revision->getId(),
160 'oldid' => 'prev',
161 'unhide' => 1
163 array(
164 'known',
165 'noclasses'
171 public function getHTML() {
172 $difflink = $this->getDiffLink();
173 $revlink = $this->getRevisionLink();
174 $userlink = $this->special->skin->revUserLink( $this->revision );
175 $comment = $this->special->skin->revComment( $this->revision );
176 if ( $this->isDeleted() ) {
177 $revlink = "<span class=\"history-deleted\">$revlink</span>";
179 return "<li>($difflink) $revlink $userlink $comment</li>";
184 * List for archive table items, i.e. revisions deleted via action=delete
186 class RevDel_ArchiveList extends RevDel_RevisionList {
187 var $type = 'archive';
188 var $idField = 'ar_timestamp';
189 var $dateField = 'ar_timestamp';
190 var $authorIdField = 'ar_user';
191 var $authorNameField = 'ar_user_text';
194 * @param $db DatabaseBase
195 * @return mixed
197 public function doQuery( $db ) {
198 $timestamps = array();
199 foreach ( $this->ids as $id ) {
200 $timestamps[] = $db->timestamp( $id );
202 return $db->select( 'archive', '*',
203 array(
204 'ar_namespace' => $this->title->getNamespace(),
205 'ar_title' => $this->title->getDBkey(),
206 'ar_timestamp' => $timestamps
208 __METHOD__,
209 array( 'ORDER BY' => 'ar_timestamp DESC' )
213 public function newItem( $row ) {
214 return new RevDel_ArchiveItem( $this, $row );
217 public function doPreCommitUpdates() {
218 return Status::newGood();
221 public function doPostCommitUpdates() {
222 return Status::newGood();
227 * Item class for a archive table row
229 class RevDel_ArchiveItem extends RevDel_RevisionItem {
230 public function __construct( $list, $row ) {
231 RevDel_Item::__construct( $list, $row );
232 $this->revision = Revision::newFromArchiveRow( $row,
233 array( 'page' => $this->list->title->getArticleId() ) );
236 public function getId() {
237 # Convert DB timestamp to MW timestamp
238 return $this->revision->getTimestamp();
241 public function setBits( $bits ) {
242 $dbw = wfGetDB( DB_MASTER );
243 $dbw->update( 'archive',
244 array( 'ar_deleted' => $bits ),
245 array( 'ar_namespace' => $this->list->title->getNamespace(),
246 'ar_title' => $this->list->title->getDBkey(),
247 // use timestamp for index
248 'ar_timestamp' => $this->row->ar_timestamp,
249 'ar_rev_id' => $this->row->ar_rev_id,
250 'ar_deleted' => $this->getBits()
252 __METHOD__ );
253 return (bool)$dbw->affectedRows();
256 protected function getRevisionLink() {
257 global $wgLang;
258 $undelete = SpecialPage::getTitleFor( 'Undelete' );
259 $date = $wgLang->timeanddate( $this->revision->getTimestamp(), true );
260 if ( $this->isDeleted() && !$this->canViewContent() ) {
261 return $date;
263 return $this->special->skin->link( $undelete, $date, array(),
264 array(
265 'target' => $this->list->title->getPrefixedText(),
266 'timestamp' => $this->revision->getTimestamp()
267 ) );
270 protected function getDiffLink() {
271 if ( $this->isDeleted() && !$this->canViewContent() ) {
272 return wfMsgHtml( 'diff' );
274 $undelete = SpecialPage::getTitleFor( 'Undelete' );
275 return $this->special->skin->link( $undelete, wfMsgHtml('diff'), array(),
276 array(
277 'target' => $this->list->title->getPrefixedText(),
278 'diff' => 'prev',
279 'timestamp' => $this->revision->getTimestamp()
280 ) );
285 * List for oldimage table items
287 class RevDel_FileList extends RevDel_List {
288 var $type = 'oldimage';
289 var $idField = 'oi_archive_name';
290 var $dateField = 'oi_timestamp';
291 var $authorIdField = 'oi_user';
292 var $authorNameField = 'oi_user_text';
293 var $storeBatch, $deleteBatch, $cleanupBatch;
296 * @param $db DatabaseBase
297 * @return mixed
299 public function doQuery( $db ) {
300 $archiveNames = array();
301 foreach( $this->ids as $timestamp ) {
302 $archiveNames[] = $timestamp . '!' . $this->title->getDBkey();
304 return $db->select( 'oldimage', '*',
305 array(
306 'oi_name' => $this->title->getDBkey(),
307 'oi_archive_name' => $archiveNames
309 __METHOD__,
310 array( 'ORDER BY' => 'oi_timestamp DESC' )
314 public function newItem( $row ) {
315 return new RevDel_FileItem( $this, $row );
318 public function clearFileOps() {
319 $this->deleteBatch = array();
320 $this->storeBatch = array();
321 $this->cleanupBatch = array();
324 public function doPreCommitUpdates() {
325 $status = Status::newGood();
326 $repo = RepoGroup::singleton()->getLocalRepo();
327 if ( $this->storeBatch ) {
328 $status->merge( $repo->storeBatch( $this->storeBatch, FileRepo::OVERWRITE_SAME ) );
330 if ( !$status->isOK() ) {
331 return $status;
333 if ( $this->deleteBatch ) {
334 $status->merge( $repo->deleteBatch( $this->deleteBatch ) );
336 if ( !$status->isOK() ) {
337 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
338 // modified (but destined for rollback) causes data loss
339 return $status;
341 if ( $this->cleanupBatch ) {
342 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch ) );
344 return $status;
347 public function doPostCommitUpdates() {
348 $file = wfLocalFile( $this->title );
349 $file->purgeCache();
350 $file->purgeDescription();
351 return Status::newGood();
354 public function getSuppressBit() {
355 return File::DELETED_RESTRICTED;
360 * Item class for an oldimage table row
362 class RevDel_FileItem extends RevDel_Item {
365 * @var File
367 var $file;
369 public function __construct( $list, $row ) {
370 parent::__construct( $list, $row );
371 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
374 public function getId() {
375 $parts = explode( '!', $this->row->oi_archive_name );
376 return $parts[0];
379 public function canView() {
380 return $this->file->userCan( File::DELETED_RESTRICTED );
383 public function canViewContent() {
384 return $this->file->userCan( File::DELETED_FILE );
387 public function getBits() {
388 return $this->file->getVisibility();
391 public function setBits( $bits ) {
392 # Queue the file op
393 # FIXME: move to LocalFile.php
394 if ( $this->isDeleted() ) {
395 if ( $bits & File::DELETED_FILE ) {
396 # Still deleted
397 } else {
398 # Newly undeleted
399 $key = $this->file->getStorageKey();
400 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
401 $this->list->storeBatch[] = array(
402 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
403 'public',
404 $this->file->getRel()
406 $this->list->cleanupBatch[] = $key;
408 } elseif ( $bits & File::DELETED_FILE ) {
409 # Newly deleted
410 $key = $this->file->getStorageKey();
411 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
412 $this->list->deleteBatch[] = array( $this->file->getRel(), $dstRel );
415 # Do the database operations
416 $dbw = wfGetDB( DB_MASTER );
417 $dbw->update( 'oldimage',
418 array( 'oi_deleted' => $bits ),
419 array(
420 'oi_name' => $this->row->oi_name,
421 'oi_timestamp' => $this->row->oi_timestamp,
422 'oi_deleted' => $this->getBits()
424 __METHOD__
426 return (bool)$dbw->affectedRows();
429 public function isDeleted() {
430 return $this->file->isDeleted( File::DELETED_FILE );
434 * Get the link to the file.
435 * Overridden by RevDel_ArchivedFileItem.
437 protected function getLink() {
438 global $wgLang, $wgUser;
439 $date = $wgLang->timeanddate( $this->file->getTimestamp(), true );
440 if ( $this->isDeleted() ) {
441 # Hidden files...
442 if ( !$this->canViewContent() ) {
443 $link = $date;
444 } else {
445 $link = $this->special->skin->link(
446 $this->special->getTitle(),
447 $date, array(),
448 array(
449 'target' => $this->list->title->getPrefixedText(),
450 'file' => $this->file->getArchiveName(),
451 'token' => $wgUser->editToken( $this->file->getArchiveName() )
455 return '<span class="history-deleted">' . $link . '</span>';
456 } else {
457 # Regular files...
458 return Xml::element( 'a', array( 'href' => $this->file->getUrl() ), $date );
462 * Generate a user tool link cluster if the current user is allowed to view it
463 * @return string HTML
465 protected function getUserTools() {
466 if( $this->file->userCan( Revision::DELETED_USER ) ) {
467 $link = $this->special->skin->userLink( $this->file->user, $this->file->user_text ) .
468 $this->special->skin->userToolLinks( $this->file->user, $this->file->user_text );
469 } else {
470 $link = wfMsgHtml( 'rev-deleted-user' );
472 if( $this->file->isDeleted( Revision::DELETED_USER ) ) {
473 return '<span class="history-deleted">' . $link . '</span>';
475 return $link;
479 * Wrap and format the file's comment block, if the current
480 * user is allowed to view it.
482 * @return string HTML
484 protected function getComment() {
485 if( $this->file->userCan( File::DELETED_COMMENT ) ) {
486 $block = $this->special->skin->commentBlock( $this->file->description );
487 } else {
488 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
490 if( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
491 return "<span class=\"history-deleted\">$block</span>";
493 return $block;
496 public function getHTML() {
497 global $wgLang;
498 $data =
499 wfMsg(
500 'widthheight',
501 $wgLang->formatNum( $this->file->getWidth() ),
502 $wgLang->formatNum( $this->file->getHeight() )
504 ' (' .
505 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file->getSize() ) ) .
506 ')';
508 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
509 $data . ' ' . $this->getComment(). '</li>';
514 * List for filearchive table items
516 class RevDel_ArchivedFileList extends RevDel_FileList {
517 var $type = 'filearchive';
518 var $idField = 'fa_id';
519 var $dateField = 'fa_timestamp';
520 var $authorIdField = 'fa_user';
521 var $authorNameField = 'fa_user_text';
524 * @param $db DatabaseBase
525 * @return mixed
527 public function doQuery( $db ) {
528 $ids = array_map( 'intval', $this->ids );
529 return $db->select( 'filearchive', '*',
530 array(
531 'fa_name' => $this->title->getDBkey(),
532 'fa_id' => $ids
534 __METHOD__,
535 array( 'ORDER BY' => 'fa_id DESC' )
539 public function newItem( $row ) {
540 return new RevDel_ArchivedFileItem( $this, $row );
545 * Item class for a filearchive table row
547 class RevDel_ArchivedFileItem extends RevDel_FileItem {
548 public function __construct( $list, $row ) {
549 RevDel_Item::__construct( $list, $row );
550 $this->file = ArchivedFile::newFromRow( $row );
553 public function getId() {
554 return $this->row->fa_id;
557 public function setBits( $bits ) {
558 $dbw = wfGetDB( DB_MASTER );
559 $dbw->update( 'filearchive',
560 array( 'fa_deleted' => $bits ),
561 array(
562 'fa_id' => $this->row->fa_id,
563 'fa_deleted' => $this->getBits(),
565 __METHOD__
567 return (bool)$dbw->affectedRows();
570 protected function getLink() {
571 global $wgLang, $wgUser;
572 $date = $wgLang->timeanddate( $this->file->getTimestamp(), true );
573 $undelete = SpecialPage::getTitleFor( 'Undelete' );
574 $key = $this->file->getKey();
575 # Hidden files...
576 if( !$this->canViewContent() ) {
577 $link = $date;
578 } else {
579 $link = $this->special->skin->link( $undelete, $date, array(),
580 array(
581 'target' => $this->list->title->getPrefixedText(),
582 'file' => $key,
583 'token' => $wgUser->editToken( $key )
587 if( $this->isDeleted() ) {
588 $link = '<span class="history-deleted">' . $link . '</span>';
590 return $link;
595 * List for logging table items
597 class RevDel_LogList extends RevDel_List {
598 var $type = 'logging';
599 var $idField = 'log_id';
600 var $dateField = 'log_timestamp';
601 var $authorIdField = 'log_user';
602 var $authorNameField = 'log_user_text';
605 * @param $db DatabaseBase
606 * @return mixed
608 public function doQuery( $db ) {
609 $ids = array_map( 'intval', $this->ids );
610 return $db->select( 'logging', '*',
611 array( 'log_id' => $ids ),
612 __METHOD__,
613 array( 'ORDER BY' => 'log_id DESC' )
617 public function newItem( $row ) {
618 return new RevDel_LogItem( $this, $row );
621 public function getSuppressBit() {
622 return Revision::DELETED_RESTRICTED;
625 public function getLogAction() {
626 return 'event';
629 public function getLogParams( $params ) {
630 return array(
631 implode( ',', $params['ids'] ),
632 "ofield={$params['oldBits']}",
633 "nfield={$params['newBits']}"
639 * Item class for a logging table row
641 class RevDel_LogItem extends RevDel_Item {
642 public function canView() {
643 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED );
646 public function canViewContent() {
647 return true; // none
650 public function getBits() {
651 return $this->row->log_deleted;
654 public function setBits( $bits ) {
655 $dbw = wfGetDB( DB_MASTER );
656 $dbw->update( 'recentchanges',
657 array(
658 'rc_deleted' => $bits,
659 'rc_patrolled' => 1
661 array(
662 'rc_logid' => $this->row->log_id,
663 'rc_timestamp' => $this->row->log_timestamp // index
665 __METHOD__
667 $dbw->update( 'logging',
668 array( 'log_deleted' => $bits ),
669 array(
670 'log_id' => $this->row->log_id,
671 'log_deleted' => $this->getBits()
673 __METHOD__
675 return (bool)$dbw->affectedRows();
678 public function getHTML() {
679 global $wgLang;
681 $date = htmlspecialchars( $wgLang->timeanddate( $this->row->log_timestamp ) );
682 $paramArray = LogPage::extractParams( $this->row->log_params );
683 $title = Title::makeTitle( $this->row->log_namespace, $this->row->log_title );
685 // Log link for this page
686 $loglink = $this->special->skin->link(
687 SpecialPage::getTitleFor( 'Log' ),
688 wfMsgHtml( 'log' ),
689 array(),
690 array( 'page' => $title->getPrefixedText() )
692 // Action text
693 if( !$this->canView() ) {
694 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
695 } else {
696 $action = LogPage::actionText( $this->row->log_type, $this->row->log_action, $title,
697 $this->special->skin, $paramArray, true, true );
698 if( $this->row->log_deleted & LogPage::DELETED_ACTION )
699 $action = '<span class="history-deleted">' . $action . '</span>';
701 // User links
702 $userLink = $this->special->skin->userLink( $this->row->log_user,
703 User::WhoIs( $this->row->log_user ) );
704 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_USER) ) {
705 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
707 // Comment
708 $comment = $wgLang->getDirMark() . $this->special->skin->commentBlock( $this->row->log_comment );
709 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_COMMENT) ) {
710 $comment = '<span class="history-deleted">' . $comment . '</span>';
712 return "<li>($loglink) $date $userLink $action $comment</li>";