Swap else if for elseif
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDelete.php
1 <?php
2 /**
3 * List for revision table items
4 *
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.
8 *
9 * See RevDel_RevisionItem and RevDel_ArchivedRevisionItem for items.
10 */
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';
18
19 /**
20 * @param $db DatabaseBase
21 * @return mixed
22 */
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'
30 ),
31 __METHOD__,
32 array( 'ORDER BY' => 'rev_id DESC' )
33 );
34
35 if ( $live->numRows() >= count( $ids ) ) {
36 // All requested revisions are live, keeps things simple!
37 return $live;
38 }
39
40 // Check if any requested revisions are available fully deleted.
41 $archived = $db->select( array( 'archive' ), '*',
42 array(
43 'ar_rev_id' => $ids
44 ),
45 __METHOD__,
46 array( 'ORDER BY' => 'ar_rev_id DESC' )
47 );
48
49 if ( $archived->numRows() == 0 ) {
50 return $live;
51 } elseif ( $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;
58 }
59 foreach ( $archived as $row ) {
60 $rows[$row->ar_rev_id] = $row;
61 }
62 krsort( $rows );
63 return new FakeResultWrapper( array_values( $rows ) );
64 }
65 }
66
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' );
75 }
76 }
77
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__ );
83 }
84 return $this->currentRevId;
85 }
86
87 public function getSuppressBit() {
88 return Revision::DELETED_RESTRICTED;
89 }
90
91 public function doPreCommitUpdates() {
92 $this->title->invalidateCache();
93 return Status::newGood();
94 }
95
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();
101 }
102 }
103
104 /**
105 * Item class for a live revision table row
106 */
107 class RevDel_RevisionItem extends RevDel_Item {
108 var $revision;
109
110 public function __construct( $list, $row ) {
111 parent::__construct( $list, $row );
112 $this->revision = new Revision( $row );
113 }
114
115 public function canView() {
116 return $this->revision->userCan( Revision::DELETED_RESTRICTED );
117 }
118
119 public function canViewContent() {
120 return $this->revision->userCan( Revision::DELETED_TEXT );
121 }
122
123 public function getBits() {
124 return $this->revision->mDeleted;
125 }
126
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()
136 ),
137 __METHOD__
138 );
139 if ( !$dbw->affectedRows() ) {
140 // Concurrent fail!
141 return false;
142 }
143 // Update recentchanges table
144 $dbw->update( 'recentchanges',
145 array(
146 'rc_deleted' => $bits,
147 'rc_patrolled' => 1
148 ),
149 array(
150 'rc_this_oldid' => $this->revision->getId(), // condition
151 // non-unique timestamp index
152 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
153 ),
154 __METHOD__
155 );
156 return true;
157 }
158
159 public function isDeleted() {
160 return $this->revision->isDeleted( Revision::DELETED_TEXT );
161 }
162
163 public function isHideCurrentOp( $newBits ) {
164 return ( $newBits & Revision::DELETED_TEXT )
165 && $this->list->getCurrent() == $this->getId();
166 }
167
168 /**
169 * Get the HTML link to the revision text.
170 * Overridden by RevDel_ArchiveItem.
171 */
172 protected function getRevisionLink() {
173 $date = $this->getLang()->timeanddate( $this->revision->getTimestamp(), true );
174 if ( $this->isDeleted() && !$this->canViewContent() ) {
175 return $date;
176 }
177 return Linker::link(
178 $this->list->title,
179 $date,
180 array(),
181 array(
182 'oldid' => $this->revision->getId(),
183 'unhide' => 1
184 )
185 );
186 }
187
188 /**
189 * Get the HTML link to the diff.
190 * Overridden by RevDel_ArchiveItem
191 */
192 protected function getDiffLink() {
193 if ( $this->isDeleted() && !$this->canViewContent() ) {
194 return wfMsgHtml('diff');
195 } else {
196 return
197 Linker::link(
198 $this->list->title,
199 wfMsgHtml('diff'),
200 array(),
201 array(
202 'diff' => $this->revision->getId(),
203 'oldid' => 'prev',
204 'unhide' => 1
205 ),
206 array(
207 'known',
208 'noclasses'
209 )
210 );
211 }
212 }
213
214 public function getHTML() {
215 $difflink = $this->getDiffLink();
216 $revlink = $this->getRevisionLink();
217 $userlink = Linker::revUserLink( $this->revision );
218 $comment = Linker::revComment( $this->revision );
219 if ( $this->isDeleted() ) {
220 $revlink = "<span class=\"history-deleted\">$revlink</span>";
221 }
222 return "<li>($difflink) $revlink $userlink $comment</li>";
223 }
224 }
225
226 /**
227 * List for archive table items, i.e. revisions deleted via action=delete
228 */
229 class RevDel_ArchiveList extends RevDel_RevisionList {
230 var $type = 'archive';
231 var $idField = 'ar_timestamp';
232 var $dateField = 'ar_timestamp';
233 var $authorIdField = 'ar_user';
234 var $authorNameField = 'ar_user_text';
235
236 /**
237 * @param $db DatabaseBase
238 * @return mixed
239 */
240 public function doQuery( $db ) {
241 $timestamps = array();
242 foreach ( $this->ids as $id ) {
243 $timestamps[] = $db->timestamp( $id );
244 }
245 return $db->select( 'archive', '*',
246 array(
247 'ar_namespace' => $this->title->getNamespace(),
248 'ar_title' => $this->title->getDBkey(),
249 'ar_timestamp' => $timestamps
250 ),
251 __METHOD__,
252 array( 'ORDER BY' => 'ar_timestamp DESC' )
253 );
254 }
255
256 public function newItem( $row ) {
257 return new RevDel_ArchiveItem( $this, $row );
258 }
259
260 public function doPreCommitUpdates() {
261 return Status::newGood();
262 }
263
264 public function doPostCommitUpdates() {
265 return Status::newGood();
266 }
267 }
268
269 /**
270 * Item class for a archive table row
271 */
272 class RevDel_ArchiveItem extends RevDel_RevisionItem {
273 public function __construct( $list, $row ) {
274 RevDel_Item::__construct( $list, $row );
275 $this->revision = Revision::newFromArchiveRow( $row,
276 array( 'page' => $this->list->title->getArticleId() ) );
277 }
278
279 public function getId() {
280 # Convert DB timestamp to MW timestamp
281 return $this->revision->getTimestamp();
282 }
283
284 public function setBits( $bits ) {
285 $dbw = wfGetDB( DB_MASTER );
286 $dbw->update( 'archive',
287 array( 'ar_deleted' => $bits ),
288 array( 'ar_namespace' => $this->list->title->getNamespace(),
289 'ar_title' => $this->list->title->getDBkey(),
290 // use timestamp for index
291 'ar_timestamp' => $this->row->ar_timestamp,
292 'ar_rev_id' => $this->row->ar_rev_id,
293 'ar_deleted' => $this->getBits()
294 ),
295 __METHOD__ );
296 return (bool)$dbw->affectedRows();
297 }
298
299 protected function getRevisionLink() {
300 $undelete = SpecialPage::getTitleFor( 'Undelete' );
301 $date = $this->getLang()->timeanddate( $this->revision->getTimestamp(), true );
302 if ( $this->isDeleted() && !$this->canViewContent() ) {
303 return $date;
304 }
305 return Linker::link( $undelete, $date, array(),
306 array(
307 'target' => $this->list->title->getPrefixedText(),
308 'timestamp' => $this->revision->getTimestamp()
309 ) );
310 }
311
312 protected function getDiffLink() {
313 if ( $this->isDeleted() && !$this->canViewContent() ) {
314 return wfMsgHtml( 'diff' );
315 }
316 $undelete = SpecialPage::getTitleFor( 'Undelete' );
317 return Linker::link( $undelete, wfMsgHtml('diff'), array(),
318 array(
319 'target' => $this->list->title->getPrefixedText(),
320 'diff' => 'prev',
321 'timestamp' => $this->revision->getTimestamp()
322 ) );
323 }
324 }
325
326
327 /**
328 * Item class for a archive table row by ar_rev_id -- actually
329 * used via RevDel_RevisionList.
330 */
331 class RevDel_ArchivedRevisionItem extends RevDel_ArchiveItem {
332 public function __construct( $list, $row ) {
333 RevDel_Item::__construct( $list, $row );
334
335 $this->revision = Revision::newFromArchiveRow( $row,
336 array( 'page' => $this->list->title->getArticleId() ) );
337 }
338
339 public function getId() {
340 return $this->revision->getId();
341 }
342
343 public function setBits( $bits ) {
344 $dbw = wfGetDB( DB_MASTER );
345 $dbw->update( 'archive',
346 array( 'ar_deleted' => $bits ),
347 array( 'ar_rev_id' => $this->row->ar_rev_id,
348 'ar_deleted' => $this->getBits()
349 ),
350 __METHOD__ );
351 return (bool)$dbw->affectedRows();
352 }
353 }
354
355 /**
356 * List for oldimage table items
357 */
358 class RevDel_FileList extends RevDel_List {
359 var $type = 'oldimage';
360 var $idField = 'oi_archive_name';
361 var $dateField = 'oi_timestamp';
362 var $authorIdField = 'oi_user';
363 var $authorNameField = 'oi_user_text';
364 var $storeBatch, $deleteBatch, $cleanupBatch;
365
366 /**
367 * @param $db DatabaseBase
368 * @return mixed
369 */
370 public function doQuery( $db ) {
371 $archiveNames = array();
372 foreach( $this->ids as $timestamp ) {
373 $archiveNames[] = $timestamp . '!' . $this->title->getDBkey();
374 }
375 return $db->select( 'oldimage', '*',
376 array(
377 'oi_name' => $this->title->getDBkey(),
378 'oi_archive_name' => $archiveNames
379 ),
380 __METHOD__,
381 array( 'ORDER BY' => 'oi_timestamp DESC' )
382 );
383 }
384
385 public function newItem( $row ) {
386 return new RevDel_FileItem( $this, $row );
387 }
388
389 public function clearFileOps() {
390 $this->deleteBatch = array();
391 $this->storeBatch = array();
392 $this->cleanupBatch = array();
393 }
394
395 public function doPreCommitUpdates() {
396 $status = Status::newGood();
397 $repo = RepoGroup::singleton()->getLocalRepo();
398 if ( $this->storeBatch ) {
399 $status->merge( $repo->storeBatch( $this->storeBatch, FileRepo::OVERWRITE_SAME ) );
400 }
401 if ( !$status->isOK() ) {
402 return $status;
403 }
404 if ( $this->deleteBatch ) {
405 $status->merge( $repo->deleteBatch( $this->deleteBatch ) );
406 }
407 if ( !$status->isOK() ) {
408 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
409 // modified (but destined for rollback) causes data loss
410 return $status;
411 }
412 if ( $this->cleanupBatch ) {
413 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch ) );
414 }
415 return $status;
416 }
417
418 public function doPostCommitUpdates() {
419 $file = wfLocalFile( $this->title );
420 $file->purgeCache();
421 $file->purgeDescription();
422 return Status::newGood();
423 }
424
425 public function getSuppressBit() {
426 return File::DELETED_RESTRICTED;
427 }
428 }
429
430 /**
431 * Item class for an oldimage table row
432 */
433 class RevDel_FileItem extends RevDel_Item {
434
435 /**
436 * @var File
437 */
438 var $file;
439
440 public function __construct( $list, $row ) {
441 parent::__construct( $list, $row );
442 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
443 }
444
445 public function getId() {
446 $parts = explode( '!', $this->row->oi_archive_name );
447 return $parts[0];
448 }
449
450 public function canView() {
451 return $this->file->userCan( File::DELETED_RESTRICTED );
452 }
453
454 public function canViewContent() {
455 return $this->file->userCan( File::DELETED_FILE );
456 }
457
458 public function getBits() {
459 return $this->file->getVisibility();
460 }
461
462 public function setBits( $bits ) {
463 # Queue the file op
464 # @todo FIXME: Move to LocalFile.php
465 if ( $this->isDeleted() ) {
466 if ( $bits & File::DELETED_FILE ) {
467 # Still deleted
468 } else {
469 # Newly undeleted
470 $key = $this->file->getStorageKey();
471 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
472 $this->list->storeBatch[] = array(
473 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
474 'public',
475 $this->file->getRel()
476 );
477 $this->list->cleanupBatch[] = $key;
478 }
479 } elseif ( $bits & File::DELETED_FILE ) {
480 # Newly deleted
481 $key = $this->file->getStorageKey();
482 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
483 $this->list->deleteBatch[] = array( $this->file->getRel(), $dstRel );
484 }
485
486 # Do the database operations
487 $dbw = wfGetDB( DB_MASTER );
488 $dbw->update( 'oldimage',
489 array( 'oi_deleted' => $bits ),
490 array(
491 'oi_name' => $this->row->oi_name,
492 'oi_timestamp' => $this->row->oi_timestamp,
493 'oi_deleted' => $this->getBits()
494 ),
495 __METHOD__
496 );
497 return (bool)$dbw->affectedRows();
498 }
499
500 public function isDeleted() {
501 return $this->file->isDeleted( File::DELETED_FILE );
502 }
503
504 /**
505 * Get the link to the file.
506 * Overridden by RevDel_ArchivedFileItem.
507 */
508 protected function getLink() {
509 $date = $this->getLang()->timeanddate( $this->file->getTimestamp(), true );
510 if ( $this->isDeleted() ) {
511 # Hidden files...
512 if ( !$this->canViewContent() ) {
513 $link = $date;
514 } else {
515 $link = Linker::link(
516 $this->special->getTitle(),
517 $date, array(),
518 array(
519 'target' => $this->list->title->getPrefixedText(),
520 'file' => $this->file->getArchiveName(),
521 'token' => $this->getUser()->editToken( $this->file->getArchiveName() )
522 )
523 );
524 }
525 return '<span class="history-deleted">' . $link . '</span>';
526 } else {
527 # Regular files...
528 return Xml::element( 'a', array( 'href' => $this->file->getUrl() ), $date );
529 }
530 }
531 /**
532 * Generate a user tool link cluster if the current user is allowed to view it
533 * @return string HTML
534 */
535 protected function getUserTools() {
536 if( $this->file->userCan( Revision::DELETED_USER ) ) {
537 $link = Linker::userLink( $this->file->user, $this->file->user_text ) .
538 Linker::userToolLinks( $this->file->user, $this->file->user_text );
539 } else {
540 $link = wfMsgHtml( 'rev-deleted-user' );
541 }
542 if( $this->file->isDeleted( Revision::DELETED_USER ) ) {
543 return '<span class="history-deleted">' . $link . '</span>';
544 }
545 return $link;
546 }
547
548 /**
549 * Wrap and format the file's comment block, if the current
550 * user is allowed to view it.
551 *
552 * @return string HTML
553 */
554 protected function getComment() {
555 if( $this->file->userCan( File::DELETED_COMMENT ) ) {
556 $block = Linker::commentBlock( $this->file->description );
557 } else {
558 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
559 }
560 if( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
561 return "<span class=\"history-deleted\">$block</span>";
562 }
563 return $block;
564 }
565
566 public function getHTML() {
567 $data =
568 wfMsg(
569 'widthheight',
570 $this->getLang()->formatNum( $this->file->getWidth() ),
571 $this->getLang()->formatNum( $this->file->getHeight() )
572 ) .
573 ' (' .
574 wfMsgExt( 'nbytes', 'parsemag', $this->getLang()->formatNum( $this->file->getSize() ) ) .
575 ')';
576
577 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
578 $data . ' ' . $this->getComment(). '</li>';
579 }
580 }
581
582 /**
583 * List for filearchive table items
584 */
585 class RevDel_ArchivedFileList extends RevDel_FileList {
586 var $type = 'filearchive';
587 var $idField = 'fa_id';
588 var $dateField = 'fa_timestamp';
589 var $authorIdField = 'fa_user';
590 var $authorNameField = 'fa_user_text';
591
592 /**
593 * @param $db DatabaseBase
594 * @return mixed
595 */
596 public function doQuery( $db ) {
597 $ids = array_map( 'intval', $this->ids );
598 return $db->select( 'filearchive', '*',
599 array(
600 'fa_name' => $this->title->getDBkey(),
601 'fa_id' => $ids
602 ),
603 __METHOD__,
604 array( 'ORDER BY' => 'fa_id DESC' )
605 );
606 }
607
608 public function newItem( $row ) {
609 return new RevDel_ArchivedFileItem( $this, $row );
610 }
611 }
612
613 /**
614 * Item class for a filearchive table row
615 */
616 class RevDel_ArchivedFileItem extends RevDel_FileItem {
617 public function __construct( $list, $row ) {
618 RevDel_Item::__construct( $list, $row );
619 $this->file = ArchivedFile::newFromRow( $row );
620 }
621
622 public function getId() {
623 return $this->row->fa_id;
624 }
625
626 public function setBits( $bits ) {
627 $dbw = wfGetDB( DB_MASTER );
628 $dbw->update( 'filearchive',
629 array( 'fa_deleted' => $bits ),
630 array(
631 'fa_id' => $this->row->fa_id,
632 'fa_deleted' => $this->getBits(),
633 ),
634 __METHOD__
635 );
636 return (bool)$dbw->affectedRows();
637 }
638
639 protected function getLink() {
640 $date = $this->getLang()->timeanddate( $this->file->getTimestamp(), true );
641 $undelete = SpecialPage::getTitleFor( 'Undelete' );
642 $key = $this->file->getKey();
643 # Hidden files...
644 if( !$this->canViewContent() ) {
645 $link = $date;
646 } else {
647 $link = Linker::link( $undelete, $date, array(),
648 array(
649 'target' => $this->list->title->getPrefixedText(),
650 'file' => $key,
651 'token' => $this->getUser()->editToken( $key )
652 )
653 );
654 }
655 if( $this->isDeleted() ) {
656 $link = '<span class="history-deleted">' . $link . '</span>';
657 }
658 return $link;
659 }
660 }
661
662 /**
663 * List for logging table items
664 */
665 class RevDel_LogList extends RevDel_List {
666 var $type = 'logging';
667 var $idField = 'log_id';
668 var $dateField = 'log_timestamp';
669 var $authorIdField = 'log_user';
670 var $authorNameField = 'log_user_text';
671
672 /**
673 * @param $db DatabaseBase
674 * @return mixed
675 */
676 public function doQuery( $db ) {
677 $ids = array_map( 'intval', $this->ids );
678 return $db->select( 'logging', '*',
679 array( 'log_id' => $ids ),
680 __METHOD__,
681 array( 'ORDER BY' => 'log_id DESC' )
682 );
683 }
684
685 public function newItem( $row ) {
686 return new RevDel_LogItem( $this, $row );
687 }
688
689 public function getSuppressBit() {
690 return Revision::DELETED_RESTRICTED;
691 }
692
693 public function getLogAction() {
694 return 'event';
695 }
696
697 public function getLogParams( $params ) {
698 return array(
699 implode( ',', $params['ids'] ),
700 "ofield={$params['oldBits']}",
701 "nfield={$params['newBits']}"
702 );
703 }
704 }
705
706 /**
707 * Item class for a logging table row
708 */
709 class RevDel_LogItem extends RevDel_Item {
710 public function canView() {
711 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED );
712 }
713
714 public function canViewContent() {
715 return true; // none
716 }
717
718 public function getBits() {
719 return $this->row->log_deleted;
720 }
721
722 public function setBits( $bits ) {
723 $dbw = wfGetDB( DB_MASTER );
724 $dbw->update( 'recentchanges',
725 array(
726 'rc_deleted' => $bits,
727 'rc_patrolled' => 1
728 ),
729 array(
730 'rc_logid' => $this->row->log_id,
731 'rc_timestamp' => $this->row->log_timestamp // index
732 ),
733 __METHOD__
734 );
735 $dbw->update( 'logging',
736 array( 'log_deleted' => $bits ),
737 array(
738 'log_id' => $this->row->log_id,
739 'log_deleted' => $this->getBits()
740 ),
741 __METHOD__
742 );
743 return (bool)$dbw->affectedRows();
744 }
745
746 public function getHTML() {
747 $date = htmlspecialchars( $this->getLang()->timeanddate( $this->row->log_timestamp ) );
748 $paramArray = LogPage::extractParams( $this->row->log_params );
749 $title = Title::makeTitle( $this->row->log_namespace, $this->row->log_title );
750
751 // Log link for this page
752 $loglink = Linker::link(
753 SpecialPage::getTitleFor( 'Log' ),
754 wfMsgHtml( 'log' ),
755 array(),
756 array( 'page' => $title->getPrefixedText() )
757 );
758 // Action text
759 if( !$this->canView() ) {
760 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
761 } else {
762 $action = LogPage::actionText( $this->row->log_type, $this->row->log_action, $title,
763 $this->special->getSkin(), $paramArray, true, true );
764 if( $this->row->log_deleted & LogPage::DELETED_ACTION )
765 $action = '<span class="history-deleted">' . $action . '</span>';
766 }
767 // User links
768 $userLink = Linker::userLink( $this->row->log_user,
769 User::WhoIs( $this->row->log_user ) );
770 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_USER) ) {
771 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
772 }
773 // Comment
774 $comment = $this->getLang()->getDirMark() . Linker::commentBlock( $this->row->log_comment );
775 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_COMMENT) ) {
776 $comment = '<span class="history-deleted">' . $comment . '</span>';
777 }
778 return "<li>($loglink) $date $userLink $action $comment</li>";
779 }
780 }