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