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