Fix spacing between two functions
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDelete.php
1 <?php
2 /**
3 * Base implementations for deletable items.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup RevisionDelete
22 */
23
24 /**
25 * List for revision table items
26 *
27 * This will check both the 'revision' table for live revisions and the
28 * 'archive' table for traditionally-deleted revisions that have an
29 * ar_rev_id saved.
30 *
31 * See RevDel_RevisionItem and RevDel_ArchivedRevisionItem for items.
32 */
33 class RevDel_RevisionList extends RevDel_List {
34 var $currentRevId;
35
36 public function getType() {
37 return 'revision';
38 }
39
40 public static function getRelationType() {
41 return 'rev_id';
42 }
43
44 public static function getRestriction() {
45 return 'deleterevision';
46 }
47
48 public static function getRevdelConstant() {
49 return Revision::DELETED_TEXT;
50 }
51
52 public static function suggestTarget( $target, array $ids ) {
53 $rev = Revision::newFromId( $ids[0] );
54 return $rev ? $rev->getTitle() : $target;
55 }
56
57 /**
58 * @param $db DatabaseBase
59 * @return mixed
60 */
61 public function doQuery( $db ) {
62 $ids = array_map( 'intval', $this->ids );
63 $live = $db->select(
64 array( 'revision', 'page', 'user' ),
65 array_merge( Revision::selectFields(), Revision::selectUserFields() ),
66 array(
67 'rev_page' => $this->title->getArticleID(),
68 'rev_id' => $ids,
69 ),
70 __METHOD__,
71 array( 'ORDER BY' => 'rev_id DESC' ),
72 array(
73 'page' => Revision::pageJoinCond(),
74 'user' => Revision::userJoinCond() )
75 );
76
77 if ( $live->numRows() >= count( $ids ) ) {
78 // All requested revisions are live, keeps things simple!
79 return $live;
80 }
81
82 // Check if any requested revisions are available fully deleted.
83 $archived = $db->select( array( 'archive' ), '*',
84 array(
85 'ar_rev_id' => $ids
86 ),
87 __METHOD__,
88 array( 'ORDER BY' => 'ar_rev_id DESC' )
89 );
90
91 if ( $archived->numRows() == 0 ) {
92 return $live;
93 } elseif ( $live->numRows() == 0 ) {
94 return $archived;
95 } else {
96 // Combine the two! Whee
97 $rows = array();
98 foreach ( $live as $row ) {
99 $rows[$row->rev_id] = $row;
100 }
101 foreach ( $archived as $row ) {
102 $rows[$row->ar_rev_id] = $row;
103 }
104 krsort( $rows );
105 return new FakeResultWrapper( array_values( $rows ) );
106 }
107 }
108
109 public function newItem( $row ) {
110 if ( isset( $row->rev_id ) ) {
111 return new RevDel_RevisionItem( $this, $row );
112 } elseif ( isset( $row->ar_rev_id ) ) {
113 return new RevDel_ArchivedRevisionItem( $this, $row );
114 } else {
115 // This shouldn't happen. :)
116 throw new MWException( 'Invalid row type in RevDel_RevisionList' );
117 }
118 }
119
120 public function getCurrent() {
121 if ( is_null( $this->currentRevId ) ) {
122 $dbw = wfGetDB( DB_MASTER );
123 $this->currentRevId = $dbw->selectField(
124 'page', 'page_latest', $this->title->pageCond(), __METHOD__ );
125 }
126 return $this->currentRevId;
127 }
128
129 public function getSuppressBit() {
130 return Revision::DELETED_RESTRICTED;
131 }
132
133 public function doPreCommitUpdates() {
134 $this->title->invalidateCache();
135 return Status::newGood();
136 }
137
138 public function doPostCommitUpdates() {
139 $this->title->purgeSquid();
140 // Extensions that require referencing previous revisions may need this
141 wfRunHooks( 'ArticleRevisionVisibilitySet', array( &$this->title ) );
142 return Status::newGood();
143 }
144 }
145
146 /**
147 * Item class for a live revision table row
148 */
149 class RevDel_RevisionItem extends RevDel_Item {
150 var $revision;
151
152 public function __construct( $list, $row ) {
153 parent::__construct( $list, $row );
154 $this->revision = new Revision( $row );
155 }
156
157 public function getIdField() {
158 return 'rev_id';
159 }
160
161 public function getTimestampField() {
162 return 'rev_timestamp';
163 }
164
165 public function getAuthorIdField() {
166 return 'rev_user';
167 }
168
169 public function getAuthorNameField() {
170 return 'user_name'; // see Revision::selectUserFields()
171 }
172
173 public function canView() {
174 return $this->revision->userCan( Revision::DELETED_RESTRICTED, $this->list->getUser() );
175 }
176
177 public function canViewContent() {
178 return $this->revision->userCan( Revision::DELETED_TEXT, $this->list->getUser() );
179 }
180
181 public function getBits() {
182 return $this->revision->getVisibility();
183 }
184
185 public function setBits( $bits ) {
186 $dbw = wfGetDB( DB_MASTER );
187 // Update revision table
188 $dbw->update( 'revision',
189 array( 'rev_deleted' => $bits ),
190 array(
191 'rev_id' => $this->revision->getId(),
192 'rev_page' => $this->revision->getPage(),
193 'rev_deleted' => $this->getBits()
194 ),
195 __METHOD__
196 );
197 if ( !$dbw->affectedRows() ) {
198 // Concurrent fail!
199 return false;
200 }
201 // Update recentchanges table
202 $dbw->update( 'recentchanges',
203 array(
204 'rc_deleted' => $bits,
205 'rc_patrolled' => 1
206 ),
207 array(
208 'rc_this_oldid' => $this->revision->getId(), // condition
209 // non-unique timestamp index
210 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
211 ),
212 __METHOD__
213 );
214 return true;
215 }
216
217 public function isDeleted() {
218 return $this->revision->isDeleted( Revision::DELETED_TEXT );
219 }
220
221 public function isHideCurrentOp( $newBits ) {
222 return ( $newBits & Revision::DELETED_TEXT )
223 && $this->list->getCurrent() == $this->getId();
224 }
225
226 /**
227 * Get the HTML link to the revision text.
228 * Overridden by RevDel_ArchiveItem.
229 * @return string
230 */
231 protected function getRevisionLink() {
232 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
233 $this->revision->getTimestamp(), $this->list->getUser() ) );
234
235 if ( $this->isDeleted() && !$this->canViewContent() ) {
236 return $date;
237 }
238 return Linker::linkKnown(
239 $this->list->title,
240 $date,
241 array(),
242 array(
243 'oldid' => $this->revision->getId(),
244 'unhide' => 1
245 )
246 );
247 }
248
249 /**
250 * Get the HTML link to the diff.
251 * Overridden by RevDel_ArchiveItem
252 * @return string
253 */
254 protected function getDiffLink() {
255 if ( $this->isDeleted() && !$this->canViewContent() ) {
256 return $this->list->msg( 'diff' )->escaped();
257 } else {
258 return Linker::linkKnown(
259 $this->list->title,
260 $this->list->msg( 'diff' )->escaped(),
261 array(),
262 array(
263 'diff' => $this->revision->getId(),
264 'oldid' => 'prev',
265 'unhide' => 1
266 )
267 );
268 }
269 }
270
271 public function getHTML() {
272 $difflink = $this->list->msg( 'parentheses' )
273 ->rawParams( $this->getDiffLink() )->escaped();
274 $revlink = $this->getRevisionLink();
275 $userlink = Linker::revUserLink( $this->revision );
276 $comment = Linker::revComment( $this->revision );
277 if ( $this->isDeleted() ) {
278 $revlink = "<span class=\"history-deleted\">$revlink</span>";
279 }
280 return "<li>$difflink $revlink $userlink $comment</li>";
281 }
282
283 public function getApiData( ApiResult $result ) {
284 $rev = $this->revision;
285 $user = $this->list->getUser();
286 $ret = array(
287 'id' => $rev->getId(),
288 'timestamp' => wfTimestamp( TS_ISO_8601, $rev->getTimestamp() ),
289 );
290 $ret += $rev->isDeleted( Revision::DELETED_USER ) ? array( 'userhidden' => '' ) : array();
291 $ret += $rev->isDeleted( Revision::DELETED_COMMENT ) ? array( 'commenthidden' => '' ) : array();
292 $ret += $rev->isDeleted( Revision::DELETED_TEXT ) ? array( 'texthidden' => '' ) : array();
293 if ( $rev->userCan( Revision::DELETED_USER, $user ) ) {
294 $ret += array(
295 'userid' => $rev->getUser( Revision::FOR_THIS_USER ),
296 'user' => $rev->getUserText( Revision::FOR_THIS_USER ),
297 );
298 }
299 if ( $rev->userCan( Revision::DELETED_COMMENT, $user ) ) {
300 $ret += array(
301 'comment' => $rev->getComment( Revision::FOR_THIS_USER ),
302 );
303 }
304 return $ret;
305 }
306 }
307
308 /**
309 * List for archive table items, i.e. revisions deleted via action=delete
310 */
311 class RevDel_ArchiveList extends RevDel_RevisionList {
312 public function getType() {
313 return 'archive';
314 }
315
316 public static function getRelationType() {
317 return 'ar_timestamp';
318 }
319
320 /**
321 * @param $db DatabaseBase
322 * @return mixed
323 */
324 public function doQuery( $db ) {
325 $timestamps = array();
326 foreach ( $this->ids as $id ) {
327 $timestamps[] = $db->timestamp( $id );
328 }
329 return $db->select( 'archive', '*',
330 array(
331 'ar_namespace' => $this->title->getNamespace(),
332 'ar_title' => $this->title->getDBkey(),
333 'ar_timestamp' => $timestamps
334 ),
335 __METHOD__,
336 array( 'ORDER BY' => 'ar_timestamp DESC' )
337 );
338 }
339
340 public function newItem( $row ) {
341 return new RevDel_ArchiveItem( $this, $row );
342 }
343
344 public function doPreCommitUpdates() {
345 return Status::newGood();
346 }
347
348 public function doPostCommitUpdates() {
349 return Status::newGood();
350 }
351 }
352
353 /**
354 * Item class for a archive table row
355 */
356 class RevDel_ArchiveItem extends RevDel_RevisionItem {
357 public function __construct( $list, $row ) {
358 RevDel_Item::__construct( $list, $row );
359 $this->revision = Revision::newFromArchiveRow( $row,
360 array( 'page' => $this->list->title->getArticleID() ) );
361 }
362
363 public function getIdField() {
364 return 'ar_timestamp';
365 }
366
367 public function getTimestampField() {
368 return 'ar_timestamp';
369 }
370
371 public function getAuthorIdField() {
372 return 'ar_user';
373 }
374
375 public function getAuthorNameField() {
376 return 'ar_user_text';
377 }
378
379 public function getId() {
380 # Convert DB timestamp to MW timestamp
381 return $this->revision->getTimestamp();
382 }
383
384 public function setBits( $bits ) {
385 $dbw = wfGetDB( DB_MASTER );
386 $dbw->update( 'archive',
387 array( 'ar_deleted' => $bits ),
388 array(
389 'ar_namespace' => $this->list->title->getNamespace(),
390 'ar_title' => $this->list->title->getDBkey(),
391 // use timestamp for index
392 'ar_timestamp' => $this->row->ar_timestamp,
393 'ar_rev_id' => $this->row->ar_rev_id,
394 'ar_deleted' => $this->getBits()
395 ),
396 __METHOD__ );
397 return (bool)$dbw->affectedRows();
398 }
399
400 protected function getRevisionLink() {
401 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
402 $this->revision->getTimestamp(), $this->list->getUser() ) );
403
404 if ( $this->isDeleted() && !$this->canViewContent() ) {
405 return $date;
406 }
407
408 return Linker::link(
409 SpecialPage::getTitleFor( 'Undelete' ),
410 $date,
411 array(),
412 array(
413 'target' => $this->list->title->getPrefixedText(),
414 'timestamp' => $this->revision->getTimestamp()
415 )
416 );
417 }
418
419 protected function getDiffLink() {
420 if ( $this->isDeleted() && !$this->canViewContent() ) {
421 return $this->list->msg( 'diff' )->escaped();
422 }
423
424 return Linker::link(
425 SpecialPage::getTitleFor( 'Undelete' ),
426 $this->list->msg( 'diff' )->escaped(),
427 array(),
428 array(
429 'target' => $this->list->title->getPrefixedText(),
430 'diff' => 'prev',
431 'timestamp' => $this->revision->getTimestamp()
432 )
433 );
434 }
435 }
436
437 /**
438 * Item class for a archive table row by ar_rev_id -- actually
439 * used via RevDel_RevisionList.
440 */
441 class RevDel_ArchivedRevisionItem extends RevDel_ArchiveItem {
442 public function __construct( $list, $row ) {
443 RevDel_Item::__construct( $list, $row );
444
445 $this->revision = Revision::newFromArchiveRow( $row,
446 array( 'page' => $this->list->title->getArticleID() ) );
447 }
448
449 public function getIdField() {
450 return 'ar_rev_id';
451 }
452
453 public function getId() {
454 return $this->revision->getId();
455 }
456
457 public function setBits( $bits ) {
458 $dbw = wfGetDB( DB_MASTER );
459 $dbw->update( 'archive',
460 array( 'ar_deleted' => $bits ),
461 array( 'ar_rev_id' => $this->row->ar_rev_id,
462 'ar_deleted' => $this->getBits()
463 ),
464 __METHOD__ );
465 return (bool)$dbw->affectedRows();
466 }
467 }
468
469 /**
470 * List for oldimage table items
471 */
472 class RevDel_FileList extends RevDel_List {
473 public function getType() {
474 return 'oldimage';
475 }
476
477 public static function getRelationType() {
478 return 'oi_archive_name';
479 }
480
481 public static function getRestriction() {
482 return 'deleterevision';
483 }
484
485 public static function getRevdelConstant() {
486 return File::DELETED_FILE;
487 }
488
489 var $storeBatch, $deleteBatch, $cleanupBatch;
490
491 /**
492 * @param $db DatabaseBase
493 * @return mixed
494 */
495 public function doQuery( $db ) {
496 $archiveNames = array();
497 foreach ( $this->ids as $timestamp ) {
498 $archiveNames[] = $timestamp . '!' . $this->title->getDBkey();
499 }
500 return $db->select(
501 'oldimage',
502 OldLocalFile::selectFields(),
503 array(
504 'oi_name' => $this->title->getDBkey(),
505 'oi_archive_name' => $archiveNames
506 ),
507 __METHOD__,
508 array( 'ORDER BY' => 'oi_timestamp DESC' )
509 );
510 }
511
512 public function newItem( $row ) {
513 return new RevDel_FileItem( $this, $row );
514 }
515
516 public function clearFileOps() {
517 $this->deleteBatch = array();
518 $this->storeBatch = array();
519 $this->cleanupBatch = array();
520 }
521
522 public function doPreCommitUpdates() {
523 $status = Status::newGood();
524 $repo = RepoGroup::singleton()->getLocalRepo();
525 if ( $this->storeBatch ) {
526 $status->merge( $repo->storeBatch( $this->storeBatch, FileRepo::OVERWRITE_SAME ) );
527 }
528 if ( !$status->isOK() ) {
529 return $status;
530 }
531 if ( $this->deleteBatch ) {
532 $status->merge( $repo->deleteBatch( $this->deleteBatch ) );
533 }
534 if ( !$status->isOK() ) {
535 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
536 // modified (but destined for rollback) causes data loss
537 return $status;
538 }
539 if ( $this->cleanupBatch ) {
540 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch ) );
541 }
542 return $status;
543 }
544
545 public function doPostCommitUpdates() {
546 global $wgUseSquid;
547 $file = wfLocalFile( $this->title );
548 $file->purgeCache();
549 $file->purgeDescription();
550 $purgeUrls = array();
551 foreach ( $this->ids as $timestamp ) {
552 $archiveName = $timestamp . '!' . $this->title->getDBkey();
553 $file->purgeOldThumbnails( $archiveName );
554 $purgeUrls[] = $file->getArchiveUrl( $archiveName );
555 }
556 if ( $wgUseSquid ) {
557 // purge full images from cache
558 SquidUpdate::purge( $purgeUrls );
559 }
560 return Status::newGood();
561 }
562
563 public function getSuppressBit() {
564 return File::DELETED_RESTRICTED;
565 }
566 }
567
568 /**
569 * Item class for an oldimage table row
570 */
571 class RevDel_FileItem extends RevDel_Item {
572
573 /**
574 * @var File
575 */
576 var $file;
577
578 public function __construct( $list, $row ) {
579 parent::__construct( $list, $row );
580 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
581 }
582
583 public function getIdField() {
584 return 'oi_archive_name';
585 }
586
587 public function getTimestampField() {
588 return 'oi_timestamp';
589 }
590
591 public function getAuthorIdField() {
592 return 'oi_user';
593 }
594
595 public function getAuthorNameField() {
596 return 'oi_user_text';
597 }
598
599 public function getId() {
600 $parts = explode( '!', $this->row->oi_archive_name );
601 return $parts[0];
602 }
603
604 public function canView() {
605 return $this->file->userCan( File::DELETED_RESTRICTED, $this->list->getUser() );
606 }
607
608 public function canViewContent() {
609 return $this->file->userCan( File::DELETED_FILE, $this->list->getUser() );
610 }
611
612 public function getBits() {
613 return $this->file->getVisibility();
614 }
615
616 public function setBits( $bits ) {
617 # Queue the file op
618 # @todo FIXME: Move to LocalFile.php
619 if ( $this->isDeleted() ) {
620 if ( $bits & File::DELETED_FILE ) {
621 # Still deleted
622 } else {
623 # Newly undeleted
624 $key = $this->file->getStorageKey();
625 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
626 $this->list->storeBatch[] = array(
627 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
628 'public',
629 $this->file->getRel()
630 );
631 $this->list->cleanupBatch[] = $key;
632 }
633 } elseif ( $bits & File::DELETED_FILE ) {
634 # Newly deleted
635 $key = $this->file->getStorageKey();
636 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
637 $this->list->deleteBatch[] = array( $this->file->getRel(), $dstRel );
638 }
639
640 # Do the database operations
641 $dbw = wfGetDB( DB_MASTER );
642 $dbw->update( 'oldimage',
643 array( 'oi_deleted' => $bits ),
644 array(
645 'oi_name' => $this->row->oi_name,
646 'oi_timestamp' => $this->row->oi_timestamp,
647 'oi_deleted' => $this->getBits()
648 ),
649 __METHOD__
650 );
651 return (bool)$dbw->affectedRows();
652 }
653
654 public function isDeleted() {
655 return $this->file->isDeleted( File::DELETED_FILE );
656 }
657
658 /**
659 * Get the link to the file.
660 * Overridden by RevDel_ArchivedFileItem.
661 * @return string
662 */
663 protected function getLink() {
664 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
665 $this->file->getTimestamp(), $this->list->getUser() ) );
666
667 if ( !$this->isDeleted() ) {
668 # Regular files...
669 return Html::rawElement( 'a', array( 'href' => $this->file->getUrl() ), $date );
670 }
671
672 # Hidden files...
673 if ( !$this->canViewContent() ) {
674 $link = $date;
675 } else {
676 $link = Linker::link(
677 SpecialPage::getTitleFor( 'Revisiondelete' ),
678 $date,
679 array(),
680 array(
681 'target' => $this->list->title->getPrefixedText(),
682 'file' => $this->file->getArchiveName(),
683 'token' => $this->list->getUser()->getEditToken(
684 $this->file->getArchiveName() )
685 )
686 );
687 }
688 return '<span class="history-deleted">' . $link . '</span>';
689 }
690
691 /**
692 * Generate a user tool link cluster if the current user is allowed to view it
693 * @return string HTML
694 */
695 protected function getUserTools() {
696 if ( $this->file->userCan( Revision::DELETED_USER, $this->list->getUser() ) ) {
697 $uid = $this->file->getUser( 'id' );
698 $name = $this->file->getUser( 'text' );
699 $link = Linker::userLink( $uid, $name ) . Linker::userToolLinks( $uid, $name );
700 } else {
701 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
702 }
703 if ( $this->file->isDeleted( Revision::DELETED_USER ) ) {
704 return '<span class="history-deleted">' . $link . '</span>';
705 }
706 return $link;
707 }
708
709 /**
710 * Wrap and format the file's comment block, if the current
711 * user is allowed to view it.
712 *
713 * @return string HTML
714 */
715 protected function getComment() {
716 if ( $this->file->userCan( File::DELETED_COMMENT, $this->list->getUser() ) ) {
717 $block = Linker::commentBlock( $this->file->getDescription() );
718 } else {
719 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
720 }
721 if ( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
722 return "<span class=\"history-deleted\">$block</span>";
723 }
724 return $block;
725 }
726
727 public function getHTML() {
728 $data =
729 $this->list->msg( 'widthheight' )->numParams(
730 $this->file->getWidth(), $this->file->getHeight() )->text() .
731 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file->getSize() )->text() . ')';
732
733 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
734 $data . ' ' . $this->getComment() . '</li>';
735 }
736
737 public function getApiData( ApiResult $result ) {
738 $file = $this->file;
739 $user = $this->list->getUser();
740 $ret = array(
741 'title' => $this->list->title->getPrefixedText(),
742 'archivename' => $file->getArchiveName(),
743 'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
744 'width' => $file->getWidth(),
745 'height' => $file->getHeight(),
746 'size' => $file->getSize(),
747 );
748 $ret += $file->isDeleted( Revision::DELETED_USER ) ? array( 'userhidden' => '' ) : array();
749 $ret += $file->isDeleted( Revision::DELETED_COMMENT ) ? array( 'commenthidden' => '' ) : array();
750 $ret += $this->isDeleted() ? array( 'contenthidden' => '' ) : array();
751 if ( !$this->isDeleted() ) {
752 $ret += array(
753 'url' => $file->getUrl(),
754 );
755 } elseif ( $this->canViewContent() ) {
756 $ret += array(
757 'url' => SpecialPage::getTitleFor( 'Revisiondelete' )->getLinkURL(
758 array(
759 'target' => $this->list->title->getPrefixedText(),
760 'file' => $file->getArchiveName(),
761 'token' => $user->getEditToken( $file->getArchiveName() )
762 ),
763 false, PROTO_RELATIVE
764 ),
765 );
766 }
767 if ( $file->userCan( Revision::DELETED_USER, $user ) ) {
768 $ret += array(
769 'userid' => $file->user,
770 'user' => $file->user_text,
771 );
772 }
773 if ( $file->userCan( Revision::DELETED_COMMENT, $user ) ) {
774 $ret += array(
775 'comment' => $file->description,
776 );
777 }
778 return $ret;
779 }
780 }
781
782 /**
783 * List for filearchive table items
784 */
785 class RevDel_ArchivedFileList extends RevDel_FileList {
786 public function getType() {
787 return 'filearchive';
788 }
789
790 public static function getRelationType() {
791 return 'fa_id';
792 }
793
794 /**
795 * @param $db DatabaseBase
796 * @return mixed
797 */
798 public function doQuery( $db ) {
799 $ids = array_map( 'intval', $this->ids );
800 return $db->select(
801 'filearchive',
802 ArchivedFile::selectFields(),
803 array(
804 'fa_name' => $this->title->getDBkey(),
805 'fa_id' => $ids
806 ),
807 __METHOD__,
808 array( 'ORDER BY' => 'fa_id DESC' )
809 );
810 }
811
812 public function newItem( $row ) {
813 return new RevDel_ArchivedFileItem( $this, $row );
814 }
815 }
816
817 /**
818 * Item class for a filearchive table row
819 */
820 class RevDel_ArchivedFileItem extends RevDel_FileItem {
821 public function __construct( $list, $row ) {
822 RevDel_Item::__construct( $list, $row );
823 $this->file = ArchivedFile::newFromRow( $row );
824 }
825
826 public function getIdField() {
827 return 'fa_id';
828 }
829
830 public function getTimestampField() {
831 return 'fa_timestamp';
832 }
833
834 public function getAuthorIdField() {
835 return 'fa_user';
836 }
837
838 public function getAuthorNameField() {
839 return 'fa_user_text';
840 }
841
842 public function getId() {
843 return $this->row->fa_id;
844 }
845
846 public function setBits( $bits ) {
847 $dbw = wfGetDB( DB_MASTER );
848 $dbw->update( 'filearchive',
849 array( 'fa_deleted' => $bits ),
850 array(
851 'fa_id' => $this->row->fa_id,
852 'fa_deleted' => $this->getBits(),
853 ),
854 __METHOD__
855 );
856 return (bool)$dbw->affectedRows();
857 }
858
859 protected function getLink() {
860 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
861 $this->file->getTimestamp(), $this->list->getUser() ) );
862
863 # Hidden files...
864 if ( !$this->canViewContent() ) {
865 $link = $date;
866 } else {
867 $undelete = SpecialPage::getTitleFor( 'Undelete' );
868 $key = $this->file->getKey();
869 $link = Linker::link( $undelete, $date, array(),
870 array(
871 'target' => $this->list->title->getPrefixedText(),
872 'file' => $key,
873 'token' => $this->list->getUser()->getEditToken( $key )
874 )
875 );
876 }
877 if ( $this->isDeleted() ) {
878 $link = '<span class="history-deleted">' . $link . '</span>';
879 }
880 return $link;
881 }
882 }
883
884 /**
885 * List for logging table items
886 */
887 class RevDel_LogList extends RevDel_List {
888 public function getType() {
889 return 'logging';
890 }
891
892 public static function getRelationType() {
893 return 'log_id';
894 }
895
896 public static function getRestriction() {
897 return 'deletelogentry';
898 }
899
900 public static function getRevdelConstant() {
901 return LogPage::DELETED_ACTION;
902 }
903
904 public static function suggestTarget( $target, array $ids ) {
905 $result = wfGetDB( DB_SLAVE )->select( 'logging',
906 'log_type',
907 array( 'log_id' => $ids ),
908 __METHOD__,
909 array( 'DISTINCT' )
910 );
911 if ( $result->numRows() == 1 ) {
912 // If there's only one type, the target can be set to include it.
913 return SpecialPage::getTitleFor( 'Log', $result->current()->log_type );
914 }
915 return SpecialPage::getTitleFor( 'Log' );
916 }
917
918 /**
919 * @param $db DatabaseBase
920 * @return mixed
921 */
922 public function doQuery( $db ) {
923 $ids = array_map( 'intval', $this->ids );
924 return $db->select( 'logging', '*',
925 array( 'log_id' => $ids ),
926 __METHOD__,
927 array( 'ORDER BY' => 'log_id DESC' )
928 );
929 }
930
931 public function newItem( $row ) {
932 return new RevDel_LogItem( $this, $row );
933 }
934
935 public function getSuppressBit() {
936 return Revision::DELETED_RESTRICTED;
937 }
938
939 public function getLogAction() {
940 return 'event';
941 }
942
943 public function getLogParams( $params ) {
944 return array(
945 implode( ',', $params['ids'] ),
946 "ofield={$params['oldBits']}",
947 "nfield={$params['newBits']}"
948 );
949 }
950 }
951
952 /**
953 * Item class for a logging table row
954 */
955 class RevDel_LogItem extends RevDel_Item {
956 public function getIdField() {
957 return 'log_id';
958 }
959
960 public function getTimestampField() {
961 return 'log_timestamp';
962 }
963
964 public function getAuthorIdField() {
965 return 'log_user';
966 }
967
968 public function getAuthorNameField() {
969 return 'log_user_text';
970 }
971
972 public function canView() {
973 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED, $this->list->getUser() );
974 }
975
976 public function canViewContent() {
977 return true; // none
978 }
979
980 public function getBits() {
981 return $this->row->log_deleted;
982 }
983
984 public function setBits( $bits ) {
985 $dbw = wfGetDB( DB_MASTER );
986 $dbw->update( 'recentchanges',
987 array(
988 'rc_deleted' => $bits,
989 'rc_patrolled' => 1
990 ),
991 array(
992 'rc_logid' => $this->row->log_id,
993 'rc_timestamp' => $this->row->log_timestamp // index
994 ),
995 __METHOD__
996 );
997 $dbw->update( 'logging',
998 array( 'log_deleted' => $bits ),
999 array(
1000 'log_id' => $this->row->log_id,
1001 'log_deleted' => $this->getBits()
1002 ),
1003 __METHOD__
1004 );
1005 return (bool)$dbw->affectedRows();
1006 }
1007
1008 public function getHTML() {
1009 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
1010 $this->row->log_timestamp, $this->list->getUser() ) );
1011 $title = Title::makeTitle( $this->row->log_namespace, $this->row->log_title );
1012 $formatter = LogFormatter::newFromRow( $this->row );
1013 $formatter->setContext( $this->list->getContext() );
1014 $formatter->setAudience( LogFormatter::FOR_THIS_USER );
1015
1016 // Log link for this page
1017 $loglink = Linker::link(
1018 SpecialPage::getTitleFor( 'Log' ),
1019 $this->list->msg( 'log' )->escaped(),
1020 array(),
1021 array( 'page' => $title->getPrefixedText() )
1022 );
1023 $loglink = $this->list->msg( 'parentheses' )->rawParams( $loglink )->escaped();
1024 // User links and action text
1025 $action = $formatter->getActionText();
1026 // Comment
1027 $comment = $this->list->getLanguage()->getDirMark() . Linker::commentBlock( $this->row->log_comment );
1028 if ( LogEventsList::isDeleted( $this->row, LogPage::DELETED_COMMENT ) ) {
1029 $comment = '<span class="history-deleted">' . $comment . '</span>';
1030 }
1031
1032 return "<li>$loglink $date $action $comment</li>";
1033 }
1034
1035 public function getApiData( ApiResult $result ) {
1036 $logEntry = DatabaseLogEntry::newFromRow( $this->row );
1037 $user = $this->list->getUser();
1038 $ret = array(
1039 'id' => $logEntry->getId(),
1040 'type' => $logEntry->getType(),
1041 'action' => $logEntry->getSubtype(),
1042 );
1043 $ret += $logEntry->isDeleted( LogPage::DELETED_USER ) ? array( 'userhidden' => '' ) : array();
1044 $ret += $logEntry->isDeleted( LogPage::DELETED_COMMENT ) ? array( 'commenthidden' => '' ) : array();
1045 $ret += $logEntry->isDeleted( LogPage::DELETED_ACTION ) ? array( 'actionhidden' => '' ) : array();
1046
1047 if ( LogEventsList::userCan( $this->row, LogPage::DELETED_ACTION, $user ) ) {
1048 ApiQueryLogEvents::addLogParams(
1049 $result,
1050 $ret,
1051 $logEntry->getParameters(),
1052 $logEntry->getType(),
1053 $logEntry->getSubtype(),
1054 $logEntry->getTimestamp(),
1055 $logEntry->isLegacy()
1056 );
1057 }
1058 if ( LogEventsList::userCan( $this->row, LogPage::DELETED_USER, $user ) ) {
1059 $ret += array(
1060 'userid' => $this->row->log_user,
1061 'user' => $this->row->log_user_text,
1062 );
1063 }
1064 if ( LogEventsList::userCan( $this->row, LogPage::DELETED_COMMENT, $user ) ) {
1065 $ret += array(
1066 'comment' => $this->row->log_comment,
1067 );
1068 }
1069 return $ret;
1070 }
1071 }