Merge "Add SkinTemplateGetLanguageLink hook"
[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 * Generate a user tool link cluster if the current user is allowed to view it
692 * @return string HTML
693 */
694 protected function getUserTools() {
695 if ( $this->file->userCan( Revision::DELETED_USER, $this->list->getUser() ) ) {
696 $uid = $this->file->getUser( 'id' );
697 $name = $this->file->getUser( 'text' );
698 $link = Linker::userLink( $uid, $name ) . Linker::userToolLinks( $uid, $name );
699 } else {
700 $link = $this->list->msg( 'rev-deleted-user' )->escaped();
701 }
702 if ( $this->file->isDeleted( Revision::DELETED_USER ) ) {
703 return '<span class="history-deleted">' . $link . '</span>';
704 }
705 return $link;
706 }
707
708 /**
709 * Wrap and format the file's comment block, if the current
710 * user is allowed to view it.
711 *
712 * @return string HTML
713 */
714 protected function getComment() {
715 if ( $this->file->userCan( File::DELETED_COMMENT, $this->list->getUser() ) ) {
716 $block = Linker::commentBlock( $this->file->getDescription() );
717 } else {
718 $block = ' ' . $this->list->msg( 'rev-deleted-comment' )->escaped();
719 }
720 if ( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
721 return "<span class=\"history-deleted\">$block</span>";
722 }
723 return $block;
724 }
725
726 public function getHTML() {
727 $data =
728 $this->list->msg( 'widthheight' )->numParams(
729 $this->file->getWidth(), $this->file->getHeight() )->text() .
730 ' (' . $this->list->msg( 'nbytes' )->numParams( $this->file->getSize() )->text() . ')';
731
732 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
733 $data . ' ' . $this->getComment() . '</li>';
734 }
735
736 public function getApiData( ApiResult $result ) {
737 $file = $this->file;
738 $user = $this->list->getUser();
739 $ret = array(
740 'title' => $this->list->title->getPrefixedText(),
741 'archivename' => $file->getArchiveName(),
742 'timestamp' => wfTimestamp( TS_ISO_8601, $file->getTimestamp() ),
743 'width' => $file->getWidth(),
744 'height' => $file->getHeight(),
745 'size' => $file->getSize(),
746 );
747 $ret += $file->isDeleted( Revision::DELETED_USER ) ? array( 'userhidden' => '' ) : array();
748 $ret += $file->isDeleted( Revision::DELETED_COMMENT ) ? array( 'commenthidden' => '' ) : array();
749 $ret += $this->isDeleted() ? array( 'contenthidden' => '' ) : array();
750 if ( !$this->isDeleted() ) {
751 $ret += array(
752 'url' => $file->getUrl(),
753 );
754 } elseif ( $this->canViewContent() ) {
755 $ret += array(
756 'url' => SpecialPage::getTitleFor( 'Revisiondelete' )->getLinkURL(
757 array(
758 'target' => $this->list->title->getPrefixedText(),
759 'file' => $file->getArchiveName(),
760 'token' => $user->getEditToken( $file->getArchiveName() )
761 ),
762 false, PROTO_RELATIVE
763 ),
764 );
765 }
766 if ( $file->userCan( Revision::DELETED_USER, $user ) ) {
767 $ret += array(
768 'userid' => $file->user,
769 'user' => $file->user_text,
770 );
771 }
772 if ( $file->userCan( Revision::DELETED_COMMENT, $user ) ) {
773 $ret += array(
774 'comment' => $file->description,
775 );
776 }
777 return $ret;
778 }
779 }
780
781 /**
782 * List for filearchive table items
783 */
784 class RevDel_ArchivedFileList extends RevDel_FileList {
785 public function getType() {
786 return 'filearchive';
787 }
788
789 public static function getRelationType() {
790 return 'fa_id';
791 }
792
793 /**
794 * @param $db DatabaseBase
795 * @return mixed
796 */
797 public function doQuery( $db ) {
798 $ids = array_map( 'intval', $this->ids );
799 return $db->select(
800 'filearchive',
801 ArchivedFile::selectFields(),
802 array(
803 'fa_name' => $this->title->getDBkey(),
804 'fa_id' => $ids
805 ),
806 __METHOD__,
807 array( 'ORDER BY' => 'fa_id DESC' )
808 );
809 }
810
811 public function newItem( $row ) {
812 return new RevDel_ArchivedFileItem( $this, $row );
813 }
814 }
815
816 /**
817 * Item class for a filearchive table row
818 */
819 class RevDel_ArchivedFileItem extends RevDel_FileItem {
820 public function __construct( $list, $row ) {
821 RevDel_Item::__construct( $list, $row );
822 $this->file = ArchivedFile::newFromRow( $row );
823 }
824
825 public function getIdField() {
826 return 'fa_id';
827 }
828
829 public function getTimestampField() {
830 return 'fa_timestamp';
831 }
832
833 public function getAuthorIdField() {
834 return 'fa_user';
835 }
836
837 public function getAuthorNameField() {
838 return 'fa_user_text';
839 }
840
841 public function getId() {
842 return $this->row->fa_id;
843 }
844
845 public function setBits( $bits ) {
846 $dbw = wfGetDB( DB_MASTER );
847 $dbw->update( 'filearchive',
848 array( 'fa_deleted' => $bits ),
849 array(
850 'fa_id' => $this->row->fa_id,
851 'fa_deleted' => $this->getBits(),
852 ),
853 __METHOD__
854 );
855 return (bool)$dbw->affectedRows();
856 }
857
858 protected function getLink() {
859 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
860 $this->file->getTimestamp(), $this->list->getUser() ) );
861
862 # Hidden files...
863 if ( !$this->canViewContent() ) {
864 $link = $date;
865 } else {
866 $undelete = SpecialPage::getTitleFor( 'Undelete' );
867 $key = $this->file->getKey();
868 $link = Linker::link( $undelete, $date, array(),
869 array(
870 'target' => $this->list->title->getPrefixedText(),
871 'file' => $key,
872 'token' => $this->list->getUser()->getEditToken( $key )
873 )
874 );
875 }
876 if ( $this->isDeleted() ) {
877 $link = '<span class="history-deleted">' . $link . '</span>';
878 }
879 return $link;
880 }
881 }
882
883 /**
884 * List for logging table items
885 */
886 class RevDel_LogList extends RevDel_List {
887 public function getType() {
888 return 'logging';
889 }
890
891 public static function getRelationType() {
892 return 'log_id';
893 }
894
895 public static function getRestriction() {
896 return 'deletelogentry';
897 }
898
899 public static function getRevdelConstant() {
900 return LogPage::DELETED_ACTION;
901 }
902
903 public static function suggestTarget( $target, array $ids ) {
904 $result = wfGetDB( DB_SLAVE )->select( 'logging',
905 'log_type',
906 array( 'log_id' => $ids ),
907 __METHOD__,
908 array( 'DISTINCT' )
909 );
910 if ( $result->numRows() == 1 ) {
911 // If there's only one type, the target can be set to include it.
912 return SpecialPage::getTitleFor( 'Log', $result->current()->log_type );
913 }
914 return SpecialPage::getTitleFor( 'Log' );
915 }
916
917 /**
918 * @param $db DatabaseBase
919 * @return mixed
920 */
921 public function doQuery( $db ) {
922 $ids = array_map( 'intval', $this->ids );
923 return $db->select( 'logging', '*',
924 array( 'log_id' => $ids ),
925 __METHOD__,
926 array( 'ORDER BY' => 'log_id DESC' )
927 );
928 }
929
930 public function newItem( $row ) {
931 return new RevDel_LogItem( $this, $row );
932 }
933
934 public function getSuppressBit() {
935 return Revision::DELETED_RESTRICTED;
936 }
937
938 public function getLogAction() {
939 return 'event';
940 }
941
942 public function getLogParams( $params ) {
943 return array(
944 implode( ',', $params['ids'] ),
945 "ofield={$params['oldBits']}",
946 "nfield={$params['newBits']}"
947 );
948 }
949 }
950
951 /**
952 * Item class for a logging table row
953 */
954 class RevDel_LogItem extends RevDel_Item {
955 public function getIdField() {
956 return 'log_id';
957 }
958
959 public function getTimestampField() {
960 return 'log_timestamp';
961 }
962
963 public function getAuthorIdField() {
964 return 'log_user';
965 }
966
967 public function getAuthorNameField() {
968 return 'log_user_text';
969 }
970
971 public function canView() {
972 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED, $this->list->getUser() );
973 }
974
975 public function canViewContent() {
976 return true; // none
977 }
978
979 public function getBits() {
980 return $this->row->log_deleted;
981 }
982
983 public function setBits( $bits ) {
984 $dbw = wfGetDB( DB_MASTER );
985 $dbw->update( 'recentchanges',
986 array(
987 'rc_deleted' => $bits,
988 'rc_patrolled' => 1
989 ),
990 array(
991 'rc_logid' => $this->row->log_id,
992 'rc_timestamp' => $this->row->log_timestamp // index
993 ),
994 __METHOD__
995 );
996 $dbw->update( 'logging',
997 array( 'log_deleted' => $bits ),
998 array(
999 'log_id' => $this->row->log_id,
1000 'log_deleted' => $this->getBits()
1001 ),
1002 __METHOD__
1003 );
1004 return (bool)$dbw->affectedRows();
1005 }
1006
1007 public function getHTML() {
1008 $date = htmlspecialchars( $this->list->getLanguage()->userTimeAndDate(
1009 $this->row->log_timestamp, $this->list->getUser() ) );
1010 $title = Title::makeTitle( $this->row->log_namespace, $this->row->log_title );
1011 $formatter = LogFormatter::newFromRow( $this->row );
1012 $formatter->setContext( $this->list->getContext() );
1013 $formatter->setAudience( LogFormatter::FOR_THIS_USER );
1014
1015 // Log link for this page
1016 $loglink = Linker::link(
1017 SpecialPage::getTitleFor( 'Log' ),
1018 $this->list->msg( 'log' )->escaped(),
1019 array(),
1020 array( 'page' => $title->getPrefixedText() )
1021 );
1022 $loglink = $this->list->msg( 'parentheses' )->rawParams( $loglink )->escaped();
1023 // User links and action text
1024 $action = $formatter->getActionText();
1025 // Comment
1026 $comment = $this->list->getLanguage()->getDirMark() . Linker::commentBlock( $this->row->log_comment );
1027 if ( LogEventsList::isDeleted( $this->row, LogPage::DELETED_COMMENT ) ) {
1028 $comment = '<span class="history-deleted">' . $comment . '</span>';
1029 }
1030
1031 return "<li>$loglink $date $action $comment</li>";
1032 }
1033
1034 public function getApiData( ApiResult $result ) {
1035 $logEntry = DatabaseLogEntry::newFromRow( $this->row );
1036 $user = $this->list->getUser();
1037 $ret = array(
1038 'id' => $logEntry->getId(),
1039 'type' => $logEntry->getType(),
1040 'action' => $logEntry->getSubtype(),
1041 );
1042 $ret += $logEntry->isDeleted( LogPage::DELETED_USER ) ? array( 'userhidden' => '' ) : array();
1043 $ret += $logEntry->isDeleted( LogPage::DELETED_COMMENT ) ? array( 'commenthidden' => '' ) : array();
1044 $ret += $logEntry->isDeleted( LogPage::DELETED_ACTION ) ? array( 'actionhidden' => '' ) : array();
1045
1046 if ( LogEventsList::userCan( $this->row, LogPage::DELETED_ACTION, $user ) ) {
1047 ApiQueryLogEvents::addLogParams(
1048 $result,
1049 $ret,
1050 $logEntry->getParameters(),
1051 $logEntry->getType(),
1052 $logEntry->getSubtype(),
1053 $logEntry->getTimestamp(),
1054 $logEntry->isLegacy()
1055 );
1056 }
1057 if ( LogEventsList::userCan( $this->row, LogPage::DELETED_USER, $user ) ) {
1058 $ret += array(
1059 'userid' => $this->row->log_user,
1060 'user' => $this->row->log_user_text,
1061 );
1062 }
1063 if ( LogEventsList::userCan( $this->row, LogPage::DELETED_COMMENT, $user ) ) {
1064 $ret += array(
1065 'comment' => $this->row->log_comment,
1066 );
1067 }
1068 return $ret;
1069 }
1070 }