More function documentation
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDelete.php
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';
12
13 /**
14 * @param $db DatabaseBase
15 * @return mixed
16 */
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'
24 ),
25 __METHOD__,
26 array( 'ORDER BY' => 'rev_id DESC' )
27 );
28 }
29
30 public function newItem( $row ) {
31 return new RevDel_RevisionItem( $this, $row );
32 }
33
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__ );
39 }
40 return $this->currentRevId;
41 }
42
43 public function getSuppressBit() {
44 return Revision::DELETED_RESTRICTED;
45 }
46
47 public function doPreCommitUpdates() {
48 $this->title->invalidateCache();
49 return Status::newGood();
50 }
51
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();
57 }
58 }
59
60 /**
61 * Item class for a revision table row
62 */
63 class RevDel_RevisionItem extends RevDel_Item {
64 var $revision;
65
66 public function __construct( $list, $row ) {
67 parent::__construct( $list, $row );
68 $this->revision = new Revision( $row );
69 }
70
71 public function canView() {
72 return $this->revision->userCan( Revision::DELETED_RESTRICTED );
73 }
74
75 public function canViewContent() {
76 return $this->revision->userCan( Revision::DELETED_TEXT );
77 }
78
79 public function getBits() {
80 return $this->revision->mDeleted;
81 }
82
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()
92 ),
93 __METHOD__
94 );
95 if ( !$dbw->affectedRows() ) {
96 // Concurrent fail!
97 return false;
98 }
99 // Update recentchanges table
100 $dbw->update( 'recentchanges',
101 array(
102 'rc_deleted' => $bits,
103 'rc_patrolled' => 1
104 ),
105 array(
106 'rc_this_oldid' => $this->revision->getId(), // condition
107 // non-unique timestamp index
108 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
109 ),
110 __METHOD__
111 );
112 return true;
113 }
114
115 public function isDeleted() {
116 return $this->revision->isDeleted( Revision::DELETED_TEXT );
117 }
118
119 public function isHideCurrentOp( $newBits ) {
120 return ( $newBits & Revision::DELETED_TEXT )
121 && $this->list->getCurrent() == $this->getId();
122 }
123
124 /**
125 * Get the HTML link to the revision text.
126 * Overridden by RevDel_ArchiveItem.
127 */
128 protected function getRevisionLink() {
129 global $wgLang;
130 $date = $wgLang->timeanddate( $this->revision->getTimestamp(), true );
131 if ( $this->isDeleted() && !$this->canViewContent() ) {
132 return $date;
133 }
134 return $this->special->skin->link(
135 $this->list->title,
136 $date,
137 array(),
138 array(
139 'oldid' => $this->revision->getId(),
140 'unhide' => 1
141 )
142 );
143 }
144
145 /**
146 * Get the HTML link to the diff.
147 * Overridden by RevDel_ArchiveItem
148 */
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
162 ),
163 array(
164 'known',
165 'noclasses'
166 )
167 );
168 }
169 }
170
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>";
178 }
179 return "<li>($difflink) $revlink $userlink $comment</li>";
180 }
181 }
182
183 /**
184 * List for archive table items, i.e. revisions deleted via action=delete
185 */
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';
192
193 /**
194 * @param $db DatabaseBase
195 * @return mixed
196 */
197 public function doQuery( $db ) {
198 $timestamps = array();
199 foreach ( $this->ids as $id ) {
200 $timestamps[] = $db->timestamp( $id );
201 }
202 return $db->select( 'archive', '*',
203 array(
204 'ar_namespace' => $this->title->getNamespace(),
205 'ar_title' => $this->title->getDBkey(),
206 'ar_timestamp' => $timestamps
207 ),
208 __METHOD__,
209 array( 'ORDER BY' => 'ar_timestamp DESC' )
210 );
211 }
212
213 public function newItem( $row ) {
214 return new RevDel_ArchiveItem( $this, $row );
215 }
216
217 public function doPreCommitUpdates() {
218 return Status::newGood();
219 }
220
221 public function doPostCommitUpdates() {
222 return Status::newGood();
223 }
224 }
225
226 /**
227 * Item class for a archive table row
228 */
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() ) );
234 }
235
236 public function getId() {
237 # Convert DB timestamp to MW timestamp
238 return $this->revision->getTimestamp();
239 }
240
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()
251 ),
252 __METHOD__ );
253 return (bool)$dbw->affectedRows();
254 }
255
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;
262 }
263 return $this->special->skin->link( $undelete, $date, array(),
264 array(
265 'target' => $this->list->title->getPrefixedText(),
266 'timestamp' => $this->revision->getTimestamp()
267 ) );
268 }
269
270 protected function getDiffLink() {
271 if ( $this->isDeleted() && !$this->canViewContent() ) {
272 return wfMsgHtml( 'diff' );
273 }
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 ) );
281 }
282 }
283
284 /**
285 * List for oldimage table items
286 */
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;
294
295 /**
296 * @param $db DatabaseBase
297 * @return mixed
298 */
299 public function doQuery( $db ) {
300 $archiveNames = array();
301 foreach( $this->ids as $timestamp ) {
302 $archiveNames[] = $timestamp . '!' . $this->title->getDBkey();
303 }
304 return $db->select( 'oldimage', '*',
305 array(
306 'oi_name' => $this->title->getDBkey(),
307 'oi_archive_name' => $archiveNames
308 ),
309 __METHOD__,
310 array( 'ORDER BY' => 'oi_timestamp DESC' )
311 );
312 }
313
314 public function newItem( $row ) {
315 return new RevDel_FileItem( $this, $row );
316 }
317
318 public function clearFileOps() {
319 $this->deleteBatch = array();
320 $this->storeBatch = array();
321 $this->cleanupBatch = array();
322 }
323
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 ) );
329 }
330 if ( !$status->isOK() ) {
331 return $status;
332 }
333 if ( $this->deleteBatch ) {
334 $status->merge( $repo->deleteBatch( $this->deleteBatch ) );
335 }
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;
340 }
341 if ( $this->cleanupBatch ) {
342 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch ) );
343 }
344 return $status;
345 }
346
347 public function doPostCommitUpdates() {
348 $file = wfLocalFile( $this->title );
349 $file->purgeCache();
350 $file->purgeDescription();
351 return Status::newGood();
352 }
353
354 public function getSuppressBit() {
355 return File::DELETED_RESTRICTED;
356 }
357 }
358
359 /**
360 * Item class for an oldimage table row
361 */
362 class RevDel_FileItem extends RevDel_Item {
363
364 /**
365 * @var File
366 */
367 var $file;
368
369 public function __construct( $list, $row ) {
370 parent::__construct( $list, $row );
371 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
372 }
373
374 public function getId() {
375 $parts = explode( '!', $this->row->oi_archive_name );
376 return $parts[0];
377 }
378
379 public function canView() {
380 return $this->file->userCan( File::DELETED_RESTRICTED );
381 }
382
383 public function canViewContent() {
384 return $this->file->userCan( File::DELETED_FILE );
385 }
386
387 public function getBits() {
388 return $this->file->getVisibility();
389 }
390
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()
405 );
406 $this->list->cleanupBatch[] = $key;
407 }
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 );
413 }
414
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()
423 ),
424 __METHOD__
425 );
426 return (bool)$dbw->affectedRows();
427 }
428
429 public function isDeleted() {
430 return $this->file->isDeleted( File::DELETED_FILE );
431 }
432
433 /**
434 * Get the link to the file.
435 * Overridden by RevDel_ArchivedFileItem.
436 */
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() )
452 )
453 );
454 }
455 return '<span class="history-deleted">' . $link . '</span>';
456 } else {
457 # Regular files...
458 return Xml::element( 'a', array( 'href' => $this->file->getUrl() ), $date );
459 }
460 }
461 /**
462 * Generate a user tool link cluster if the current user is allowed to view it
463 * @return string HTML
464 */
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' );
471 }
472 if( $this->file->isDeleted( Revision::DELETED_USER ) ) {
473 return '<span class="history-deleted">' . $link . '</span>';
474 }
475 return $link;
476 }
477
478 /**
479 * Wrap and format the file's comment block, if the current
480 * user is allowed to view it.
481 *
482 * @return string HTML
483 */
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' );
489 }
490 if( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
491 return "<span class=\"history-deleted\">$block</span>";
492 }
493 return $block;
494 }
495
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() )
503 ) .
504 ' (' .
505 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file->getSize() ) ) .
506 ')';
507
508 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
509 $data . ' ' . $this->getComment(). '</li>';
510 }
511 }
512
513 /**
514 * List for filearchive table items
515 */
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';
522
523 /**
524 * @param $db DatabaseBase
525 * @return mixed
526 */
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
533 ),
534 __METHOD__,
535 array( 'ORDER BY' => 'fa_id DESC' )
536 );
537 }
538
539 public function newItem( $row ) {
540 return new RevDel_ArchivedFileItem( $this, $row );
541 }
542 }
543
544 /**
545 * Item class for a filearchive table row
546 */
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 );
551 }
552
553 public function getId() {
554 return $this->row->fa_id;
555 }
556
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(),
564 ),
565 __METHOD__
566 );
567 return (bool)$dbw->affectedRows();
568 }
569
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 )
584 )
585 );
586 }
587 if( $this->isDeleted() ) {
588 $link = '<span class="history-deleted">' . $link . '</span>';
589 }
590 return $link;
591 }
592 }
593
594 /**
595 * List for logging table items
596 */
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';
603
604 /**
605 * @param $db DatabaseBase
606 * @return mixed
607 */
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' )
614 );
615 }
616
617 public function newItem( $row ) {
618 return new RevDel_LogItem( $this, $row );
619 }
620
621 public function getSuppressBit() {
622 return Revision::DELETED_RESTRICTED;
623 }
624
625 public function getLogAction() {
626 return 'event';
627 }
628
629 public function getLogParams( $params ) {
630 return array(
631 implode( ',', $params['ids'] ),
632 "ofield={$params['oldBits']}",
633 "nfield={$params['newBits']}"
634 );
635 }
636 }
637
638 /**
639 * Item class for a logging table row
640 */
641 class RevDel_LogItem extends RevDel_Item {
642 public function canView() {
643 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED );
644 }
645
646 public function canViewContent() {
647 return true; // none
648 }
649
650 public function getBits() {
651 return $this->row->log_deleted;
652 }
653
654 public function setBits( $bits ) {
655 $dbw = wfGetDB( DB_MASTER );
656 $dbw->update( 'recentchanges',
657 array(
658 'rc_deleted' => $bits,
659 'rc_patrolled' => 1
660 ),
661 array(
662 'rc_logid' => $this->row->log_id,
663 'rc_timestamp' => $this->row->log_timestamp // index
664 ),
665 __METHOD__
666 );
667 $dbw->update( 'logging',
668 array( 'log_deleted' => $bits ),
669 array(
670 'log_id' => $this->row->log_id,
671 'log_deleted' => $this->getBits()
672 ),
673 __METHOD__
674 );
675 return (bool)$dbw->affectedRows();
676 }
677
678 public function getHTML() {
679 global $wgLang;
680
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 );
684
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() )
691 );
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>';
700 }
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>';
706 }
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>';
711 }
712 return "<li>($loglink) $date $userLink $action $comment</li>";
713 }
714 }