Use Agora oojs-ui theme on mobile
[mediawiki.git] / includes / specials / SpecialUndelete.php
blobe8a45a7ae14e9c13a65fe87f677c104aa7fb1c2d
1 <?php
2 /**
3 * Implements Special:Undelete
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Used to show archived pages and eventually restore them.
27 * @ingroup SpecialPage
29 class PageArchive {
30 /** @var Title */
31 protected $title;
33 /** @var Status */
34 protected $fileStatus;
36 /** @var Status */
37 protected $revisionStatus;
39 function __construct( $title ) {
40 if ( is_null( $title ) ) {
41 throw new MWException( __METHOD__ . ' given a null title.' );
43 $this->title = $title;
46 /**
47 * List all deleted pages recorded in the archive table. Returns result
48 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
49 * namespace/title.
51 * @return ResultWrapper
53 public static function listAllPages() {
54 $dbr = wfGetDB( DB_SLAVE );
56 return self::listPages( $dbr, '' );
59 /**
60 * List deleted pages recorded in the archive table matching the
61 * given title prefix.
62 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
64 * @param string $prefix Title prefix
65 * @return ResultWrapper
67 public static function listPagesByPrefix( $prefix ) {
68 $dbr = wfGetDB( DB_SLAVE );
70 $title = Title::newFromText( $prefix );
71 if ( $title ) {
72 $ns = $title->getNamespace();
73 $prefix = $title->getDBkey();
74 } else {
75 // Prolly won't work too good
76 // @todo handle bare namespace names cleanly?
77 $ns = 0;
80 $conds = array(
81 'ar_namespace' => $ns,
82 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
85 return self::listPages( $dbr, $conds );
88 /**
89 * @param DatabaseBase $dbr
90 * @param string|array $condition
91 * @return bool|ResultWrapper
93 protected static function listPages( $dbr, $condition ) {
94 return $dbr->resultObject( $dbr->select(
95 array( 'archive' ),
96 array(
97 'ar_namespace',
98 'ar_title',
99 'count' => 'COUNT(*)'
101 $condition,
102 __METHOD__,
103 array(
104 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
105 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
106 'LIMIT' => 100,
108 ) );
112 * List the revisions of the given page. Returns result wrapper with
113 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
115 * @return ResultWrapper
117 function listRevisions() {
118 global $wgContentHandlerUseDB;
120 $dbr = wfGetDB( DB_SLAVE );
122 $tables = array( 'archive' );
124 $fields = array(
125 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
126 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
129 if ( $wgContentHandlerUseDB ) {
130 $fields[] = 'ar_content_format';
131 $fields[] = 'ar_content_model';
134 $conds = array( 'ar_namespace' => $this->title->getNamespace(),
135 'ar_title' => $this->title->getDBkey() );
137 $options = array( 'ORDER BY' => 'ar_timestamp DESC' );
139 $join_conds = array();
141 ChangeTags::modifyDisplayQuery(
142 $tables,
143 $fields,
144 $conds,
145 $join_conds,
146 $options
149 $res = $dbr->select( $tables,
150 $fields,
151 $conds,
152 __METHOD__,
153 $options,
154 $join_conds
157 return $dbr->resultObject( $res );
161 * List the deleted file revisions for this page, if it's a file page.
162 * Returns a result wrapper with various filearchive fields, or null
163 * if not a file page.
165 * @return ResultWrapper
166 * @todo Does this belong in Image for fuller encapsulation?
168 function listFiles() {
169 if ( $this->title->getNamespace() != NS_FILE ) {
170 return null;
173 $dbr = wfGetDB( DB_SLAVE );
174 $res = $dbr->select(
175 'filearchive',
176 ArchivedFile::selectFields(),
177 array( 'fa_name' => $this->title->getDBkey() ),
178 __METHOD__,
179 array( 'ORDER BY' => 'fa_timestamp DESC' )
182 return $dbr->resultObject( $res );
186 * Return a Revision object containing data for the deleted revision.
187 * Note that the result *may* or *may not* have a null page ID.
189 * @param string $timestamp
190 * @return Revision|null
192 function getRevision( $timestamp ) {
193 global $wgContentHandlerUseDB;
195 $dbr = wfGetDB( DB_SLAVE );
197 $fields = array(
198 'ar_rev_id',
199 'ar_text',
200 'ar_comment',
201 'ar_user',
202 'ar_user_text',
203 'ar_timestamp',
204 'ar_minor_edit',
205 'ar_flags',
206 'ar_text_id',
207 'ar_deleted',
208 'ar_len',
209 'ar_sha1',
212 if ( $wgContentHandlerUseDB ) {
213 $fields[] = 'ar_content_format';
214 $fields[] = 'ar_content_model';
217 $row = $dbr->selectRow( 'archive',
218 $fields,
219 array( 'ar_namespace' => $this->title->getNamespace(),
220 'ar_title' => $this->title->getDBkey(),
221 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
222 __METHOD__ );
224 if ( $row ) {
225 return Revision::newFromArchiveRow( $row, array( 'title' => $this->title ) );
228 return null;
232 * Return the most-previous revision, either live or deleted, against
233 * the deleted revision given by timestamp.
235 * May produce unexpected results in case of history merges or other
236 * unusual time issues.
238 * @param string $timestamp
239 * @return Revision|null Null when there is no previous revision
241 function getPreviousRevision( $timestamp ) {
242 $dbr = wfGetDB( DB_SLAVE );
244 // Check the previous deleted revision...
245 $row = $dbr->selectRow( 'archive',
246 'ar_timestamp',
247 array( 'ar_namespace' => $this->title->getNamespace(),
248 'ar_title' => $this->title->getDBkey(),
249 'ar_timestamp < ' .
250 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
251 __METHOD__,
252 array(
253 'ORDER BY' => 'ar_timestamp DESC',
254 'LIMIT' => 1 ) );
255 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
257 $row = $dbr->selectRow( array( 'page', 'revision' ),
258 array( 'rev_id', 'rev_timestamp' ),
259 array(
260 'page_namespace' => $this->title->getNamespace(),
261 'page_title' => $this->title->getDBkey(),
262 'page_id = rev_page',
263 'rev_timestamp < ' .
264 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
265 __METHOD__,
266 array(
267 'ORDER BY' => 'rev_timestamp DESC',
268 'LIMIT' => 1 ) );
269 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
270 $prevLiveId = $row ? intval( $row->rev_id ) : null;
272 if ( $prevLive && $prevLive > $prevDeleted ) {
273 // Most prior revision was live
274 return Revision::newFromId( $prevLiveId );
275 } elseif ( $prevDeleted ) {
276 // Most prior revision was deleted
277 return $this->getRevision( $prevDeleted );
280 // No prior revision on this page.
281 return null;
285 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
287 * @param object $row Database row
288 * @return string
290 function getTextFromRow( $row ) {
291 if ( is_null( $row->ar_text_id ) ) {
292 // An old row from MediaWiki 1.4 or previous.
293 // Text is embedded in this row in classic compression format.
294 return Revision::getRevisionText( $row, 'ar_' );
297 // New-style: keyed to the text storage backend.
298 $dbr = wfGetDB( DB_SLAVE );
299 $text = $dbr->selectRow( 'text',
300 array( 'old_text', 'old_flags' ),
301 array( 'old_id' => $row->ar_text_id ),
302 __METHOD__ );
304 return Revision::getRevisionText( $text );
308 * Fetch (and decompress if necessary) the stored text of the most
309 * recently edited deleted revision of the page.
311 * If there are no archived revisions for the page, returns NULL.
313 * @return string|null
315 function getLastRevisionText() {
316 $dbr = wfGetDB( DB_SLAVE );
317 $row = $dbr->selectRow( 'archive',
318 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
319 array( 'ar_namespace' => $this->title->getNamespace(),
320 'ar_title' => $this->title->getDBkey() ),
321 __METHOD__,
322 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
324 if ( $row ) {
325 return $this->getTextFromRow( $row );
328 return null;
332 * Quick check if any archived revisions are present for the page.
334 * @return boolean
336 function isDeleted() {
337 $dbr = wfGetDB( DB_SLAVE );
338 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
339 array( 'ar_namespace' => $this->title->getNamespace(),
340 'ar_title' => $this->title->getDBkey() ),
341 __METHOD__
344 return ( $n > 0 );
348 * Restore the given (or all) text and file revisions for the page.
349 * Once restored, the items will be removed from the archive tables.
350 * The deletion log will be updated with an undeletion notice.
352 * @param array $timestamps Pass an empty array to restore all revisions,
353 * otherwise list the ones to undelete.
354 * @param string $comment
355 * @param array $fileVersions
356 * @param bool $unsuppress
357 * @param User $user User performing the action, or null to use $wgUser
358 * @return array(number of file revisions restored, number of image revisions
359 * restored, log message) on success, false on failure.
361 function undelete( $timestamps, $comment = '', $fileVersions = array(),
362 $unsuppress = false, User $user = null
364 // If both the set of text revisions and file revisions are empty,
365 // restore everything. Otherwise, just restore the requested items.
366 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
368 $restoreText = $restoreAll || !empty( $timestamps );
369 $restoreFiles = $restoreAll || !empty( $fileVersions );
371 if ( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
372 $img = wfLocalFile( $this->title );
373 $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
374 if ( !$this->fileStatus->isOK() ) {
375 return false;
377 $filesRestored = $this->fileStatus->successCount;
378 } else {
379 $filesRestored = 0;
382 if ( $restoreText ) {
383 $this->revisionStatus = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
384 if ( !$this->revisionStatus->isOK() ) {
385 return false;
388 $textRestored = $this->revisionStatus->getValue();
389 } else {
390 $textRestored = 0;
393 // Touch the log!
395 if ( $textRestored && $filesRestored ) {
396 $reason = wfMessage( 'undeletedrevisions-files' )
397 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
398 } elseif ( $textRestored ) {
399 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
400 ->inContentLanguage()->text();
401 } elseif ( $filesRestored ) {
402 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
403 ->inContentLanguage()->text();
404 } else {
405 wfDebug( "Undelete: nothing undeleted...\n" );
407 return false;
410 if ( trim( $comment ) != '' ) {
411 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
414 if ( $user === null ) {
415 global $wgUser;
416 $user = $wgUser;
419 $logEntry = new ManualLogEntry( 'delete', 'restore' );
420 $logEntry->setPerformer( $user );
421 $logEntry->setTarget( $this->title );
422 $logEntry->setComment( $reason );
424 wfRunHooks( 'ArticleUndeleteLogEntry', array( $this, &$logEntry, $user ) );
426 $logid = $logEntry->insert();
427 $logEntry->publish( $logid );
429 return array( $textRestored, $filesRestored, $reason );
433 * This is the meaty bit -- restores archived revisions of the given page
434 * to the cur/old tables. If the page currently exists, all revisions will
435 * be stuffed into old, otherwise the most recent will go into cur.
437 * @param array $timestamps Pass an empty array to restore all revisions,
438 * otherwise list the ones to undelete.
439 * @param bool $unsuppress Remove all ar_deleted/fa_deleted restrictions of seletected revs
440 * @param string $comment
441 * @throws ReadOnlyError
442 * @return Status Object containing the number of revisions restored on success
444 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
445 global $wgContentHandlerUseDB;
447 if ( wfReadOnly() ) {
448 throw new ReadOnlyError();
451 $restoreAll = empty( $timestamps );
452 $dbw = wfGetDB( DB_MASTER );
454 # Does this page already exist? We'll have to update it...
455 $article = WikiPage::factory( $this->title );
456 # Load latest data for the current page (bug 31179)
457 $article->loadPageData( 'fromdbmaster' );
458 $oldcountable = $article->isCountable();
460 $page = $dbw->selectRow( 'page',
461 array( 'page_id', 'page_latest' ),
462 array( 'page_namespace' => $this->title->getNamespace(),
463 'page_title' => $this->title->getDBkey() ),
464 __METHOD__,
465 array( 'FOR UPDATE' ) // lock page
468 if ( $page ) {
469 $makepage = false;
470 # Page already exists. Import the history, and if necessary
471 # we'll update the latest revision field in the record.
473 $previousRevId = $page->page_latest;
475 # Get the time span of this page
476 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
477 array( 'rev_id' => $previousRevId ),
478 __METHOD__ );
480 if ( $previousTimestamp === false ) {
481 wfDebug( __METHOD__ . ": existing page refers to a page_latest that does not exist\n" );
483 $status = Status::newGood( 0 );
484 $status->warning( 'undeleterevision-missing' );
486 return $status;
488 } else {
489 # Have to create a new article...
490 $makepage = true;
491 $previousRevId = 0;
492 $previousTimestamp = 0;
495 if ( $restoreAll ) {
496 $oldones = '1 = 1'; # All revisions...
497 } else {
498 $oldts = implode( ',',
499 array_map( array( &$dbw, 'addQuotes' ),
500 array_map( array( &$dbw, 'timestamp' ),
501 $timestamps ) ) );
503 $oldones = "ar_timestamp IN ( {$oldts} )";
506 $fields = array(
507 'ar_rev_id',
508 'ar_text',
509 'ar_comment',
510 'ar_user',
511 'ar_user_text',
512 'ar_timestamp',
513 'ar_minor_edit',
514 'ar_flags',
515 'ar_text_id',
516 'ar_deleted',
517 'ar_page_id',
518 'ar_len',
519 'ar_sha1'
522 if ( $wgContentHandlerUseDB ) {
523 $fields[] = 'ar_content_format';
524 $fields[] = 'ar_content_model';
528 * Select each archived revision...
530 $result = $dbw->select( 'archive',
531 $fields,
532 /* WHERE */ array(
533 'ar_namespace' => $this->title->getNamespace(),
534 'ar_title' => $this->title->getDBkey(),
535 $oldones ),
536 __METHOD__,
537 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
539 $ret = $dbw->resultObject( $result );
540 $rev_count = $dbw->numRows( $result );
542 if ( !$rev_count ) {
543 wfDebug( __METHOD__ . ": no revisions to restore\n" );
545 $status = Status::newGood( 0 );
546 $status->warning( "undelete-no-results" );
548 return $status;
551 $ret->seek( $rev_count - 1 ); // move to last
552 $row = $ret->fetchObject(); // get newest archived rev
553 $ret->seek( 0 ); // move back
555 // grab the content to check consistency with global state before restoring the page.
556 $revision = Revision::newFromArchiveRow( $row,
557 array(
558 'title' => $article->getTitle(), // used to derive default content model
561 $user = User::newFromName( $revision->getRawUserText(), false );
562 $content = $revision->getContent( Revision::RAW );
564 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
565 $status = $content->prepareSave( $article, 0, -1, $user );
567 if ( !$status->isOK() ) {
568 return $status;
571 if ( $makepage ) {
572 // Check the state of the newest to-be version...
573 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
574 return Status::newFatal( "undeleterevdel" );
576 // Safe to insert now...
577 $newid = $article->insertOn( $dbw );
578 $pageId = $newid;
579 } else {
580 // Check if a deleted revision will become the current revision...
581 if ( $row->ar_timestamp > $previousTimestamp ) {
582 // Check the state of the newest to-be version...
583 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
584 return Status::newFatal( "undeleterevdel" );
588 $newid = false;
589 $pageId = $article->getId();
592 $revision = null;
593 $restored = 0;
595 foreach ( $ret as $row ) {
596 // Check for key dupes due to shitty archive integrity.
597 if ( $row->ar_rev_id ) {
598 $exists = $dbw->selectField( 'revision', '1',
599 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
600 if ( $exists ) {
601 continue; // don't throw DB errors
604 // Insert one revision at a time...maintaining deletion status
605 // unless we are specifically removing all restrictions...
606 $revision = Revision::newFromArchiveRow( $row,
607 array(
608 'page' => $pageId,
609 'title' => $this->title,
610 'deleted' => $unsuppress ? 0 : $row->ar_deleted
611 ) );
613 $revision->insertOn( $dbw );
614 $restored++;
616 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
618 # Now that it's safely stored, take it out of the archive
619 $dbw->delete( 'archive',
620 /* WHERE */ array(
621 'ar_namespace' => $this->title->getNamespace(),
622 'ar_title' => $this->title->getDBkey(),
623 $oldones ),
624 __METHOD__ );
626 // Was anything restored at all?
627 if ( $restored == 0 ) {
628 return Status::newGood( 0 );
631 $created = (bool)$newid;
633 // Attach the latest revision to the page...
634 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
635 if ( $created || $wasnew ) {
636 // Update site stats, link tables, etc
637 $user = User::newFromName( $revision->getRawUserText(), false );
638 $article->doEditUpdates(
639 $revision,
640 $user,
641 array( 'created' => $created, 'oldcountable' => $oldcountable )
645 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment ) );
647 if ( $this->title->getNamespace() == NS_FILE ) {
648 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
649 $update->doUpdate();
652 return Status::newGood( $restored );
656 * @return Status
658 function getFileStatus() {
659 return $this->fileStatus;
663 * @return Status
665 function getRevisionStatus() {
666 return $this->revisionStatus;
671 * Special page allowing users with the appropriate permissions to view
672 * and restore deleted content.
674 * @ingroup SpecialPage
676 class SpecialUndelete extends SpecialPage {
677 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
678 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
680 /** @var Title */
681 var $mTargetObj;
683 function __construct() {
684 parent::__construct( 'Undelete', 'deletedhistory' );
687 function loadRequest( $par ) {
688 $request = $this->getRequest();
689 $user = $this->getUser();
691 $this->mAction = $request->getVal( 'action' );
692 if ( $par !== null && $par !== '' ) {
693 $this->mTarget = $par;
694 } else {
695 $this->mTarget = $request->getVal( 'target' );
698 $this->mTargetObj = null;
700 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
701 $this->mTargetObj = Title::newFromURL( $this->mTarget );
704 $this->mSearchPrefix = $request->getText( 'prefix' );
705 $time = $request->getVal( 'timestamp' );
706 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
707 $this->mFilename = $request->getVal( 'file' );
709 $posted = $request->wasPosted() &&
710 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
711 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
712 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
713 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
714 $this->mDiff = $request->getCheck( 'diff' );
715 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
716 $this->mComment = $request->getText( 'wpComment' );
717 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
718 $this->mToken = $request->getVal( 'token' );
720 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
721 $this->mAllowed = true; // user can restore
722 $this->mCanView = true; // user can view content
723 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
724 $this->mAllowed = false; // user cannot restore
725 $this->mCanView = true; // user can view content
726 $this->mRestore = false;
727 } else { // user can only view the list of revisions
728 $this->mAllowed = false;
729 $this->mCanView = false;
730 $this->mTimestamp = '';
731 $this->mRestore = false;
734 if ( $this->mRestore || $this->mInvert ) {
735 $timestamps = array();
736 $this->mFileVersions = array();
737 foreach ( $request->getValues() as $key => $val ) {
738 $matches = array();
739 if ( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
740 array_push( $timestamps, $matches[1] );
743 if ( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
744 $this->mFileVersions[] = intval( $matches[1] );
747 rsort( $timestamps );
748 $this->mTargetTimestamp = $timestamps;
752 function execute( $par ) {
753 $this->checkPermissions();
754 $user = $this->getUser();
756 $this->setHeaders();
757 $this->outputHeader();
759 $this->loadRequest( $par );
761 $out = $this->getOutput();
763 if ( is_null( $this->mTargetObj ) ) {
764 $out->addWikiMsg( 'undelete-header' );
766 # Not all users can just browse every deleted page from the list
767 if ( $user->isAllowed( 'browsearchive' ) ) {
768 $this->showSearchForm();
771 return;
774 if ( $this->mAllowed ) {
775 $out->setPageTitle( $this->msg( 'undeletepage' ) );
776 } else {
777 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
780 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
782 if ( $this->mTimestamp !== '' ) {
783 $this->showRevision( $this->mTimestamp );
784 } elseif ( $this->mFilename !== null && $this->mTargetObj->inNamespace( NS_FILE ) ) {
785 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
786 // Check if user is allowed to see this file
787 if ( !$file->exists() ) {
788 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename );
789 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
790 if ( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
791 throw new PermissionsError( 'suppressrevision' );
792 } else {
793 throw new PermissionsError( 'deletedtext' );
795 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
796 $this->showFileConfirmationForm( $this->mFilename );
797 } else {
798 $this->showFile( $this->mFilename );
800 } elseif ( $this->mRestore && $this->mAction == 'submit' ) {
801 $this->undelete();
802 } else {
803 $this->showHistory();
807 function showSearchForm() {
808 global $wgScript;
810 $out = $this->getOutput();
811 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
812 $out->addHTML(
813 Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
814 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
815 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
816 Html::rawElement(
817 'label',
818 array( 'for' => 'prefix' ),
819 $this->msg( 'undelete-search-prefix' )->parse()
821 Xml::input(
822 'prefix',
824 $this->mSearchPrefix,
825 array( 'id' => 'prefix', 'autofocus' => true )
826 ) . ' ' .
827 Xml::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
828 Xml::closeElement( 'fieldset' ) .
829 Xml::closeElement( 'form' )
832 # List undeletable articles
833 if ( $this->mSearchPrefix ) {
834 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
835 $this->showList( $result );
840 * Generic list of deleted pages
842 * @param ResultWrapper $result
843 * @return bool
845 private function showList( $result ) {
846 $out = $this->getOutput();
848 if ( $result->numRows() == 0 ) {
849 $out->addWikiMsg( 'undelete-no-results' );
851 return false;
854 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
856 $undelete = $this->getPageTitle();
857 $out->addHTML( "<ul>\n" );
858 foreach ( $result as $row ) {
859 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
860 if ( $title !== null ) {
861 $item = Linker::linkKnown(
862 $undelete,
863 htmlspecialchars( $title->getPrefixedText() ),
864 array(),
865 array( 'target' => $title->getPrefixedText() )
867 } else {
868 // The title is no longer valid, show as text
869 $item = Html::element(
870 'span',
871 array( 'class' => 'mw-invalidtitle' ),
872 Linker::getInvalidTitleDescription(
873 $this->getContext(),
874 $row->ar_namespace,
875 $row->ar_title
879 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
880 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
882 $result->free();
883 $out->addHTML( "</ul>\n" );
885 return true;
888 private function showRevision( $timestamp ) {
889 if ( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
890 return;
893 $archive = new PageArchive( $this->mTargetObj );
894 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) ) ) {
895 return;
897 $rev = $archive->getRevision( $timestamp );
899 $out = $this->getOutput();
900 $user = $this->getUser();
902 if ( !$rev ) {
903 $out->addWikiMsg( 'undeleterevision-missing' );
905 return;
908 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
909 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
910 $out->wrapWikiMsg(
911 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
912 'rev-deleted-text-permission'
915 return;
918 $out->wrapWikiMsg(
919 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
920 'rev-deleted-text-view'
922 $out->addHTML( '<br />' );
923 // and we are allowed to see...
926 if ( $this->mDiff ) {
927 $previousRev = $archive->getPreviousRevision( $timestamp );
928 if ( $previousRev ) {
929 $this->showDiff( $previousRev, $rev );
930 if ( $this->mDiffOnly ) {
931 return;
934 $out->addHTML( '<hr />' );
935 } else {
936 $out->addWikiMsg( 'undelete-nodiff' );
940 $link = Linker::linkKnown(
941 $this->getPageTitle( $this->mTargetObj->getPrefixedDBkey() ),
942 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
945 $lang = $this->getLanguage();
947 // date and time are separate parameters to facilitate localisation.
948 // $time is kept for backward compat reasons.
949 $time = $lang->userTimeAndDate( $timestamp, $user );
950 $d = $lang->userDate( $timestamp, $user );
951 $t = $lang->userTime( $timestamp, $user );
952 $userLink = Linker::revUserTools( $rev );
954 $content = $rev->getContent( Revision::FOR_THIS_USER, $user );
956 $isText = ( $content instanceof TextContent );
958 if ( $this->mPreview || $isText ) {
959 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
960 } else {
961 $openDiv = '<div id="mw-undelete-revision">';
963 $out->addHTML( $openDiv );
965 // Revision delete links
966 if ( !$this->mDiff ) {
967 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
968 if ( $revdel ) {
969 $out->addHTML( "$revdel " );
973 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
974 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
976 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) ) ) {
977 return;
980 if ( $this->mPreview || !$isText ) {
981 // NOTE: non-text content has no source view, so always use rendered preview
983 // Hide [edit]s
984 $popts = $out->parserOptions();
985 $popts->setEditSection( false );
987 $pout = $content->getParserOutput( $this->mTargetObj, $rev->getId(), $popts, true );
988 $out->addParserOutput( $pout );
991 if ( $isText ) {
992 // source view for textual content
993 $sourceView = Xml::element(
994 'textarea',
995 array(
996 'readonly' => 'readonly',
997 'cols' => $user->getIntOption( 'cols' ),
998 'rows' => $user->getIntOption( 'rows' )
1000 $content->getNativeData() . "\n"
1003 $previewButton = Xml::element( 'input', array(
1004 'type' => 'submit',
1005 'name' => 'preview',
1006 'value' => $this->msg( 'showpreview' )->text()
1007 ) );
1008 } else {
1009 $sourceView = '';
1010 $previewButton = '';
1013 $diffButton = Xml::element( 'input', array(
1014 'name' => 'diff',
1015 'type' => 'submit',
1016 'value' => $this->msg( 'showdiff' )->text() ) );
1018 $out->addHTML(
1019 $sourceView .
1020 Xml::openElement( 'div', array(
1021 'style' => 'clear: both' ) ) .
1022 Xml::openElement( 'form', array(
1023 'method' => 'post',
1024 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
1025 Xml::element( 'input', array(
1026 'type' => 'hidden',
1027 'name' => 'target',
1028 'value' => $this->mTargetObj->getPrefixedDBkey() ) ) .
1029 Xml::element( 'input', array(
1030 'type' => 'hidden',
1031 'name' => 'timestamp',
1032 'value' => $timestamp ) ) .
1033 Xml::element( 'input', array(
1034 'type' => 'hidden',
1035 'name' => 'wpEditToken',
1036 'value' => $user->getEditToken() ) ) .
1037 $previewButton .
1038 $diffButton .
1039 Xml::closeElement( 'form' ) .
1040 Xml::closeElement( 'div' )
1045 * Build a diff display between this and the previous either deleted
1046 * or non-deleted edit.
1048 * @param Revision $previousRev
1049 * @param Revision $currentRev
1050 * @return string HTML
1052 function showDiff( $previousRev, $currentRev ) {
1053 $diffContext = clone $this->getContext();
1054 $diffContext->setTitle( $currentRev->getTitle() );
1055 $diffContext->setWikiPage( WikiPage::factory( $currentRev->getTitle() ) );
1057 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1058 $diffEngine->showDiffStyle();
1060 $formattedDiff = $diffEngine->generateContentDiffBody(
1061 $previousRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ),
1062 $currentRev->getContent( Revision::FOR_THIS_USER, $this->getUser() )
1065 $formattedDiff = $diffEngine->addHeader(
1066 $formattedDiff,
1067 $this->diffHeader( $previousRev, 'o' ),
1068 $this->diffHeader( $currentRev, 'n' )
1071 $this->getOutput()->addHTML( "<div>$formattedDiff</div>\n" );
1075 * @param Revision $rev
1076 * @param string $prefix
1077 * @return string
1079 private function diffHeader( $rev, $prefix ) {
1080 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1081 if ( $isDeleted ) {
1082 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1083 $targetPage = $this->getPageTitle();
1084 $targetQuery = array(
1085 'target' => $this->mTargetObj->getPrefixedText(),
1086 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
1088 } else {
1089 /// @todo FIXME: getId() may return non-zero for deleted revs...
1090 $targetPage = $rev->getTitle();
1091 $targetQuery = array( 'oldid' => $rev->getId() );
1094 // Add show/hide deletion links if available
1095 $user = $this->getUser();
1096 $lang = $this->getLanguage();
1097 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1099 if ( $rdel ) {
1100 $rdel = " $rdel";
1103 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1105 $tags = wfGetDB( DB_SLAVE )->selectField(
1106 'tag_summary',
1107 'ts_tags',
1108 array( 'ts_rev_id' => $rev->getId() ),
1109 __METHOD__
1111 $tagSummary = ChangeTags::formatSummaryRow( $tags, 'deleteddiff' );
1113 // FIXME This is reimplementing DifferenceEngine#getRevisionHeader
1114 // and partially #showDiffPage, but worse
1115 return '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1116 Linker::link(
1117 $targetPage,
1118 $this->msg(
1119 'revisionasof',
1120 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1121 $lang->userDate( $rev->getTimestamp(), $user ),
1122 $lang->userTime( $rev->getTimestamp(), $user )
1123 )->escaped(),
1124 array(),
1125 $targetQuery
1127 '</strong></div>' .
1128 '<div id="mw-diff-' . $prefix . 'title2">' .
1129 Linker::revUserTools( $rev ) . '<br />' .
1130 '</div>' .
1131 '<div id="mw-diff-' . $prefix . 'title3">' .
1132 $minor . Linker::revComment( $rev ) . $rdel . '<br />' .
1133 '</div>' .
1134 '<div id="mw-diff-' . $prefix . 'title5">' .
1135 $tagSummary[0] . '<br />' .
1136 '</div>';
1140 * Show a form confirming whether a tokenless user really wants to see a file
1142 private function showFileConfirmationForm( $key ) {
1143 $out = $this->getOutput();
1144 $lang = $this->getLanguage();
1145 $user = $this->getUser();
1146 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
1147 $out->addWikiMsg( 'undelete-show-file-confirm',
1148 $this->mTargetObj->getText(),
1149 $lang->userDate( $file->getTimestamp(), $user ),
1150 $lang->userTime( $file->getTimestamp(), $user ) );
1151 $out->addHTML(
1152 Xml::openElement( 'form', array(
1153 'method' => 'POST',
1154 'action' => $this->getPageTitle()->getLocalURL( array(
1155 'target' => $this->mTarget,
1156 'file' => $key,
1157 'token' => $user->getEditToken( $key ),
1158 ) ),
1161 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1162 '</form>'
1167 * Show a deleted file version requested by the visitor.
1169 private function showFile( $key ) {
1170 $this->getOutput()->disable();
1172 # We mustn't allow the output to be Squid cached, otherwise
1173 # if an admin previews a deleted image, and it's cached, then
1174 # a user without appropriate permissions can toddle off and
1175 # nab the image, and Squid will serve it
1176 $response = $this->getRequest()->response();
1177 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1178 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1179 $response->header( 'Pragma: no-cache' );
1181 $repo = RepoGroup::singleton()->getLocalRepo();
1182 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1183 $repo->streamFile( $path );
1186 private function showHistory() {
1187 $out = $this->getOutput();
1188 if ( $this->mAllowed ) {
1189 $out->addModules( 'mediawiki.special.undelete' );
1191 $out->wrapWikiMsg(
1192 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1193 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
1196 $archive = new PageArchive( $this->mTargetObj );
1197 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1199 $text = $archive->getLastRevisionText();
1200 if( is_null( $text ) ) {
1201 $out->addWikiMsg( 'nohistory' );
1202 return;
1205 $out->addHTML( '<div class="mw-undelete-history">' );
1206 if ( $this->mAllowed ) {
1207 $out->addWikiMsg( 'undeletehistory' );
1208 $out->addWikiMsg( 'undeleterevdel' );
1209 } else {
1210 $out->addWikiMsg( 'undeletehistorynoadmin' );
1212 $out->addHTML( '</div>' );
1214 # List all stored revisions
1215 $revisions = $archive->listRevisions();
1216 $files = $archive->listFiles();
1218 $haveRevisions = $revisions && $revisions->numRows() > 0;
1219 $haveFiles = $files && $files->numRows() > 0;
1221 # Batch existence check on user and talk pages
1222 if ( $haveRevisions ) {
1223 $batch = new LinkBatch();
1224 foreach ( $revisions as $row ) {
1225 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1226 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1228 $batch->execute();
1229 $revisions->seek( 0 );
1231 if ( $haveFiles ) {
1232 $batch = new LinkBatch();
1233 foreach ( $files as $row ) {
1234 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1235 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1237 $batch->execute();
1238 $files->seek( 0 );
1241 if ( $this->mAllowed ) {
1242 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
1243 # Start the form here
1244 $top = Xml::openElement(
1245 'form',
1246 array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' )
1248 $out->addHTML( $top );
1251 # Show relevant lines from the deletion log:
1252 $deleteLogPage = new LogPage( 'delete' );
1253 $out->addHTML( Xml::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1254 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1255 # Show relevant lines from the suppression log:
1256 $suppressLogPage = new LogPage( 'suppress' );
1257 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1258 $out->addHTML( Xml::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1259 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1262 if ( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1263 # Format the user-visible controls (comment field, submission button)
1264 # in a nice little table
1265 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1266 $unsuppressBox =
1267 "<tr>
1268 <td>&#160;</td>
1269 <td class='mw-input'>" .
1270 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1271 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ) .
1272 "</td>
1273 </tr>";
1274 } else {
1275 $unsuppressBox = '';
1278 $table = Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1279 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1280 "<tr>
1281 <td colspan='2' class='mw-undelete-extrahelp'>" .
1282 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1283 "</td>
1284 </tr>
1285 <tr>
1286 <td class='mw-label'>" .
1287 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1288 "</td>
1289 <td class='mw-input'>" .
1290 Xml::input(
1291 'wpComment',
1293 $this->mComment,
1294 array( 'id' => 'wpComment', 'autofocus' => true )
1296 "</td>
1297 </tr>
1298 <tr>
1299 <td>&#160;</td>
1300 <td class='mw-submit'>" .
1301 Xml::submitButton(
1302 $this->msg( 'undeletebtn' )->text(),
1303 array( 'name' => 'restore', 'id' => 'mw-undelete-submit' )
1304 ) . ' ' .
1305 Xml::submitButton(
1306 $this->msg( 'undeleteinvert' )->text(),
1307 array( 'name' => 'invert', 'id' => 'mw-undelete-invert' )
1309 "</td>
1310 </tr>" .
1311 $unsuppressBox .
1312 Xml::closeElement( 'table' ) .
1313 Xml::closeElement( 'fieldset' );
1315 $out->addHTML( $table );
1318 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1320 if ( $haveRevisions ) {
1321 # The page's stored (deleted) history:
1322 $out->addHTML( '<ul>' );
1323 $remaining = $revisions->numRows();
1324 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1326 foreach ( $revisions as $row ) {
1327 $remaining--;
1328 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1330 $revisions->free();
1331 $out->addHTML( '</ul>' );
1332 } else {
1333 $out->addWikiMsg( 'nohistory' );
1336 if ( $haveFiles ) {
1337 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1338 $out->addHTML( '<ul>' );
1339 foreach ( $files as $row ) {
1340 $out->addHTML( $this->formatFileRow( $row ) );
1342 $files->free();
1343 $out->addHTML( '</ul>' );
1346 if ( $this->mAllowed ) {
1347 # Slip in the hidden controls here
1348 $misc = Html::hidden( 'target', $this->mTarget );
1349 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1350 $misc .= Xml::closeElement( 'form' );
1351 $out->addHTML( $misc );
1354 return true;
1357 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1358 $rev = Revision::newFromArchiveRow( $row,
1359 array(
1360 'title' => $this->mTargetObj
1361 ) );
1363 $revTextSize = '';
1364 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1365 // Build checkboxen...
1366 if ( $this->mAllowed ) {
1367 if ( $this->mInvert ) {
1368 if ( in_array( $ts, $this->mTargetTimestamp ) ) {
1369 $checkBox = Xml::check( "ts$ts" );
1370 } else {
1371 $checkBox = Xml::check( "ts$ts", true );
1373 } else {
1374 $checkBox = Xml::check( "ts$ts" );
1376 } else {
1377 $checkBox = '';
1380 // Build page & diff links...
1381 $user = $this->getUser();
1382 if ( $this->mCanView ) {
1383 $titleObj = $this->getPageTitle();
1384 # Last link
1385 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1386 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1387 $last = $this->msg( 'diff' )->escaped();
1388 } elseif ( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1389 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1390 $last = Linker::linkKnown(
1391 $titleObj,
1392 $this->msg( 'diff' )->escaped(),
1393 array(),
1394 array(
1395 'target' => $this->mTargetObj->getPrefixedText(),
1396 'timestamp' => $ts,
1397 'diff' => 'prev'
1400 } else {
1401 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1402 $last = $this->msg( 'diff' )->escaped();
1404 } else {
1405 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1406 $last = $this->msg( 'diff' )->escaped();
1409 // User links
1410 $userLink = Linker::revUserTools( $rev );
1412 // Minor edit
1413 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1415 // Revision text size
1416 $size = $row->ar_len;
1417 if ( !is_null( $size ) ) {
1418 $revTextSize = Linker::formatRevisionSize( $size );
1421 // Edit summary
1422 $comment = Linker::revComment( $rev );
1424 // Tags
1425 $attribs = array();
1426 list( $tagSummary, $classes ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'deletedhistory' );
1427 if ( $classes ) {
1428 $attribs['class'] = implode( ' ', $classes );
1431 // Revision delete links
1432 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1434 $revisionRow = $this->msg( 'undelete-revision-row' )
1435 ->rawParams(
1436 $checkBox,
1437 $revdlink,
1438 $last,
1439 $pageLink,
1440 $userLink,
1441 $minor,
1442 $revTextSize,
1443 $comment,
1444 $tagSummary
1446 ->escaped();
1448 return Xml::tags( 'li', $attribs, $revisionRow ) . "\n";
1451 private function formatFileRow( $row ) {
1452 $file = ArchivedFile::newFromRow( $row );
1453 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1454 $user = $this->getUser();
1456 if ( $this->mAllowed && $row->fa_storage_key ) {
1457 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1458 $key = urlencode( $row->fa_storage_key );
1459 $pageLink = $this->getFileLink( $file, $this->getPageTitle(), $ts, $key );
1460 } else {
1461 $checkBox = '';
1462 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1464 $userLink = $this->getFileUser( $file );
1465 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1466 $bytes = $this->msg( 'parentheses' )
1467 ->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )
1468 ->plain();
1469 $data = htmlspecialchars( $data . ' ' . $bytes );
1470 $comment = $this->getFileComment( $file );
1472 // Add show/hide deletion links if available
1473 $canHide = $user->isAllowed( 'deleterevision' );
1474 if ( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1475 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1476 // Revision was hidden from sysops
1477 $revdlink = Linker::revDeleteLinkDisabled( $canHide );
1478 } else {
1479 $query = array(
1480 'type' => 'filearchive',
1481 'target' => $this->mTargetObj->getPrefixedDBkey(),
1482 'ids' => $row->fa_id
1484 $revdlink = Linker::revDeleteLink( $query,
1485 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1487 } else {
1488 $revdlink = '';
1491 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1495 * Fetch revision text link if it's available to all users
1497 * @param Revision $rev
1498 * @param Title $titleObj
1499 * @param string $ts Timestamp
1500 * @return string
1502 function getPageLink( $rev, $titleObj, $ts ) {
1503 $user = $this->getUser();
1504 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1506 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1507 return '<span class="history-deleted">' . $time . '</span>';
1510 $link = Linker::linkKnown(
1511 $titleObj,
1512 htmlspecialchars( $time ),
1513 array(),
1514 array(
1515 'target' => $this->mTargetObj->getPrefixedText(),
1516 'timestamp' => $ts
1520 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1521 $link = '<span class="history-deleted">' . $link . '</span>';
1524 return $link;
1528 * Fetch image view link if it's available to all users
1530 * @param File|ArchivedFile $file
1531 * @param Title $titleObj
1532 * @param string $ts A timestamp
1533 * @param string $key a storage key
1535 * @return string HTML fragment
1537 function getFileLink( $file, $titleObj, $ts, $key ) {
1538 $user = $this->getUser();
1539 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1541 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
1542 return '<span class="history-deleted">' . $time . '</span>';
1545 $link = Linker::linkKnown(
1546 $titleObj,
1547 htmlspecialchars( $time ),
1548 array(),
1549 array(
1550 'target' => $this->mTargetObj->getPrefixedText(),
1551 'file' => $key,
1552 'token' => $user->getEditToken( $key )
1556 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1557 $link = '<span class="history-deleted">' . $link . '</span>';
1560 return $link;
1564 * Fetch file's user id if it's available to this user
1566 * @param File|ArchivedFile $file
1567 * @return string HTML fragment
1569 function getFileUser( $file ) {
1570 if ( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1571 return '<span class="history-deleted">' .
1572 $this->msg( 'rev-deleted-user' )->escaped() .
1573 '</span>';
1576 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1577 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1579 if ( $file->isDeleted( File::DELETED_USER ) ) {
1580 $link = '<span class="history-deleted">' . $link . '</span>';
1583 return $link;
1587 * Fetch file upload comment if it's available to this user
1589 * @param File|ArchivedFile $file
1590 * @return string HTML fragment
1592 function getFileComment( $file ) {
1593 if ( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1594 return '<span class="history-deleted"><span class="comment">' .
1595 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1598 $link = Linker::commentBlock( $file->getRawDescription() );
1600 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1601 $link = '<span class="history-deleted">' . $link . '</span>';
1604 return $link;
1607 function undelete() {
1608 global $wgUploadMaintenance;
1610 if ( $wgUploadMaintenance && $this->mTargetObj->getNamespace() == NS_FILE ) {
1611 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1614 if ( wfReadOnly() ) {
1615 throw new ReadOnlyError;
1618 $out = $this->getOutput();
1619 $archive = new PageArchive( $this->mTargetObj );
1620 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1621 $ok = $archive->undelete(
1622 $this->mTargetTimestamp,
1623 $this->mComment,
1624 $this->mFileVersions,
1625 $this->mUnsuppress,
1626 $this->getUser()
1629 if ( is_array( $ok ) ) {
1630 if ( $ok[1] ) { // Undeleted file count
1631 wfRunHooks( 'FileUndeleteComplete', array(
1632 $this->mTargetObj, $this->mFileVersions,
1633 $this->getUser(), $this->mComment ) );
1636 $link = Linker::linkKnown( $this->mTargetObj );
1637 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1638 } else {
1639 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1642 // Show revision undeletion warnings and errors
1643 $status = $archive->getRevisionStatus();
1644 if ( $status && !$status->isGood() ) {
1645 $out->addWikiText( '<div class="error">' .
1646 $status->getWikiText(
1647 'cannotundelete',
1648 'cannotundelete'
1649 ) . '</div>'
1653 // Show file undeletion warnings and errors
1654 $status = $archive->getFileStatus();
1655 if ( $status && !$status->isGood() ) {
1656 $out->addWikiText( '<div class="error">' .
1657 $status->getWikiText(
1658 'undelete-error-short',
1659 'undelete-error-long'
1660 ) . '</div>'
1665 protected function getGroupName() {
1666 return 'pagetools';