Remove term from search results info
[mediawiki.git] / includes / specials / SpecialUndelete.php
blob8acf8a2bc9a90cf1afdded0f3273055695bacc1d
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 /** @var Config */
40 protected $config;
42 function __construct( $title, Config $config = null ) {
43 if ( is_null( $title ) ) {
44 throw new MWException( __METHOD__ . ' given a null title.' );
46 $this->title = $title;
47 if ( $config === null ) {
48 wfDebug( __METHOD__ . ' did not have a Config object passed to it' );
49 $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
51 $this->config = $config;
54 /**
55 * List all deleted pages recorded in the archive table. Returns result
56 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
57 * namespace/title.
59 * @return ResultWrapper
61 public static function listAllPages() {
62 $dbr = wfGetDB( DB_SLAVE );
64 return self::listPages( $dbr, '' );
67 /**
68 * List deleted pages recorded in the archive table matching the
69 * given title prefix.
70 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
72 * @param string $prefix Title prefix
73 * @return ResultWrapper
75 public static function listPagesByPrefix( $prefix ) {
76 $dbr = wfGetDB( DB_SLAVE );
78 $title = Title::newFromText( $prefix );
79 if ( $title ) {
80 $ns = $title->getNamespace();
81 $prefix = $title->getDBkey();
82 } else {
83 // Prolly won't work too good
84 // @todo handle bare namespace names cleanly?
85 $ns = 0;
88 $conds = array(
89 'ar_namespace' => $ns,
90 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
93 return self::listPages( $dbr, $conds );
96 /**
97 * @param DatabaseBase $dbr
98 * @param string|array $condition
99 * @return bool|ResultWrapper
101 protected static function listPages( $dbr, $condition ) {
102 return $dbr->resultObject( $dbr->select(
103 array( 'archive' ),
104 array(
105 'ar_namespace',
106 'ar_title',
107 'count' => 'COUNT(*)'
109 $condition,
110 __METHOD__,
111 array(
112 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
113 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
114 'LIMIT' => 100,
116 ) );
120 * List the revisions of the given page. Returns result wrapper with
121 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
123 * @return ResultWrapper
125 function listRevisions() {
126 $dbr = wfGetDB( DB_SLAVE );
128 $tables = array( 'archive' );
130 $fields = array(
131 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
132 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
135 if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
136 $fields[] = 'ar_content_format';
137 $fields[] = 'ar_content_model';
140 $conds = array( 'ar_namespace' => $this->title->getNamespace(),
141 'ar_title' => $this->title->getDBkey() );
143 $options = array( 'ORDER BY' => 'ar_timestamp DESC' );
145 $join_conds = array();
147 ChangeTags::modifyDisplayQuery(
148 $tables,
149 $fields,
150 $conds,
151 $join_conds,
152 $options
155 $res = $dbr->select( $tables,
156 $fields,
157 $conds,
158 __METHOD__,
159 $options,
160 $join_conds
163 return $dbr->resultObject( $res );
167 * List the deleted file revisions for this page, if it's a file page.
168 * Returns a result wrapper with various filearchive fields, or null
169 * if not a file page.
171 * @return ResultWrapper
172 * @todo Does this belong in Image for fuller encapsulation?
174 function listFiles() {
175 if ( $this->title->getNamespace() != NS_FILE ) {
176 return null;
179 $dbr = wfGetDB( DB_SLAVE );
180 $res = $dbr->select(
181 'filearchive',
182 ArchivedFile::selectFields(),
183 array( 'fa_name' => $this->title->getDBkey() ),
184 __METHOD__,
185 array( 'ORDER BY' => 'fa_timestamp DESC' )
188 return $dbr->resultObject( $res );
192 * Return a Revision object containing data for the deleted revision.
193 * Note that the result *may* or *may not* have a null page ID.
195 * @param string $timestamp
196 * @return Revision|null
198 function getRevision( $timestamp ) {
199 $dbr = wfGetDB( DB_SLAVE );
201 $fields = array(
202 'ar_rev_id',
203 'ar_text',
204 'ar_comment',
205 'ar_user',
206 'ar_user_text',
207 'ar_timestamp',
208 'ar_minor_edit',
209 'ar_flags',
210 'ar_text_id',
211 'ar_deleted',
212 'ar_len',
213 'ar_sha1',
216 if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
217 $fields[] = 'ar_content_format';
218 $fields[] = 'ar_content_model';
221 $row = $dbr->selectRow( 'archive',
222 $fields,
223 array( 'ar_namespace' => $this->title->getNamespace(),
224 'ar_title' => $this->title->getDBkey(),
225 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
226 __METHOD__ );
228 if ( $row ) {
229 return Revision::newFromArchiveRow( $row, array( 'title' => $this->title ) );
232 return null;
236 * Return the most-previous revision, either live or deleted, against
237 * the deleted revision given by timestamp.
239 * May produce unexpected results in case of history merges or other
240 * unusual time issues.
242 * @param string $timestamp
243 * @return Revision|null Null when there is no previous revision
245 function getPreviousRevision( $timestamp ) {
246 $dbr = wfGetDB( DB_SLAVE );
248 // Check the previous deleted revision...
249 $row = $dbr->selectRow( 'archive',
250 'ar_timestamp',
251 array( 'ar_namespace' => $this->title->getNamespace(),
252 'ar_title' => $this->title->getDBkey(),
253 'ar_timestamp < ' .
254 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
255 __METHOD__,
256 array(
257 'ORDER BY' => 'ar_timestamp DESC',
258 'LIMIT' => 1 ) );
259 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
261 $row = $dbr->selectRow( array( 'page', 'revision' ),
262 array( 'rev_id', 'rev_timestamp' ),
263 array(
264 'page_namespace' => $this->title->getNamespace(),
265 'page_title' => $this->title->getDBkey(),
266 'page_id = rev_page',
267 'rev_timestamp < ' .
268 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
269 __METHOD__,
270 array(
271 'ORDER BY' => 'rev_timestamp DESC',
272 'LIMIT' => 1 ) );
273 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
274 $prevLiveId = $row ? intval( $row->rev_id ) : null;
276 if ( $prevLive && $prevLive > $prevDeleted ) {
277 // Most prior revision was live
278 return Revision::newFromId( $prevLiveId );
279 } elseif ( $prevDeleted ) {
280 // Most prior revision was deleted
281 return $this->getRevision( $prevDeleted );
284 // No prior revision on this page.
285 return null;
289 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
291 * @param object $row Database row
292 * @return string
294 function getTextFromRow( $row ) {
295 if ( is_null( $row->ar_text_id ) ) {
296 // An old row from MediaWiki 1.4 or previous.
297 // Text is embedded in this row in classic compression format.
298 return Revision::getRevisionText( $row, 'ar_' );
301 // New-style: keyed to the text storage backend.
302 $dbr = wfGetDB( DB_SLAVE );
303 $text = $dbr->selectRow( 'text',
304 array( 'old_text', 'old_flags' ),
305 array( 'old_id' => $row->ar_text_id ),
306 __METHOD__ );
308 return Revision::getRevisionText( $text );
312 * Fetch (and decompress if necessary) the stored text of the most
313 * recently edited deleted revision of the page.
315 * If there are no archived revisions for the page, returns NULL.
317 * @return string|null
319 function getLastRevisionText() {
320 $dbr = wfGetDB( DB_SLAVE );
321 $row = $dbr->selectRow( 'archive',
322 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
323 array( 'ar_namespace' => $this->title->getNamespace(),
324 'ar_title' => $this->title->getDBkey() ),
325 __METHOD__,
326 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
328 if ( $row ) {
329 return $this->getTextFromRow( $row );
332 return null;
336 * Quick check if any archived revisions are present for the page.
338 * @return bool
340 function isDeleted() {
341 $dbr = wfGetDB( DB_SLAVE );
342 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
343 array( 'ar_namespace' => $this->title->getNamespace(),
344 'ar_title' => $this->title->getDBkey() ),
345 __METHOD__
348 return ( $n > 0 );
352 * Restore the given (or all) text and file revisions for the page.
353 * Once restored, the items will be removed from the archive tables.
354 * The deletion log will be updated with an undeletion notice.
356 * @param array $timestamps Pass an empty array to restore all revisions,
357 * otherwise list the ones to undelete.
358 * @param string $comment
359 * @param array $fileVersions
360 * @param bool $unsuppress
361 * @param User $user User performing the action, or null to use $wgUser
362 * @return array(number of file revisions restored, number of image revisions
363 * restored, log message) on success, false on failure.
365 function undelete( $timestamps, $comment = '', $fileVersions = array(),
366 $unsuppress = false, User $user = null
368 // If both the set of text revisions and file revisions are empty,
369 // restore everything. Otherwise, just restore the requested items.
370 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
372 $restoreText = $restoreAll || !empty( $timestamps );
373 $restoreFiles = $restoreAll || !empty( $fileVersions );
375 if ( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
376 $img = wfLocalFile( $this->title );
377 $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
378 if ( !$this->fileStatus->isOK() ) {
379 return false;
381 $filesRestored = $this->fileStatus->successCount;
382 } else {
383 $filesRestored = 0;
386 if ( $restoreText ) {
387 $this->revisionStatus = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
388 if ( !$this->revisionStatus->isOK() ) {
389 return false;
392 $textRestored = $this->revisionStatus->getValue();
393 } else {
394 $textRestored = 0;
397 // Touch the log!
399 if ( $textRestored && $filesRestored ) {
400 $reason = wfMessage( 'undeletedrevisions-files' )
401 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
402 } elseif ( $textRestored ) {
403 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
404 ->inContentLanguage()->text();
405 } elseif ( $filesRestored ) {
406 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
407 ->inContentLanguage()->text();
408 } else {
409 wfDebug( "Undelete: nothing undeleted...\n" );
411 return false;
414 if ( trim( $comment ) != '' ) {
415 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
418 if ( $user === null ) {
419 global $wgUser;
420 $user = $wgUser;
423 $logEntry = new ManualLogEntry( 'delete', 'restore' );
424 $logEntry->setPerformer( $user );
425 $logEntry->setTarget( $this->title );
426 $logEntry->setComment( $reason );
428 wfRunHooks( 'ArticleUndeleteLogEntry', array( $this, &$logEntry, $user ) );
430 $logid = $logEntry->insert();
431 $logEntry->publish( $logid );
433 return array( $textRestored, $filesRestored, $reason );
437 * This is the meaty bit -- restores archived revisions of the given page
438 * to the cur/old tables. If the page currently exists, all revisions will
439 * be stuffed into old, otherwise the most recent will go into cur.
441 * @param array $timestamps Pass an empty array to restore all revisions,
442 * otherwise list the ones to undelete.
443 * @param bool $unsuppress Remove all ar_deleted/fa_deleted restrictions of seletected revs
444 * @param string $comment
445 * @throws ReadOnlyError
446 * @return Status Status object containing the number of revisions restored on success
448 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
449 if ( wfReadOnly() ) {
450 throw new ReadOnlyError();
453 $restoreAll = empty( $timestamps );
454 $dbw = wfGetDB( DB_MASTER );
456 # Does this page already exist? We'll have to update it...
457 $article = WikiPage::factory( $this->title );
458 # Load latest data for the current page (bug 31179)
459 $article->loadPageData( 'fromdbmaster' );
460 $oldcountable = $article->isCountable();
462 $page = $dbw->selectRow( 'page',
463 array( 'page_id', 'page_latest' ),
464 array( 'page_namespace' => $this->title->getNamespace(),
465 'page_title' => $this->title->getDBkey() ),
466 __METHOD__,
467 array( 'FOR UPDATE' ) // lock page
470 if ( $page ) {
471 $makepage = false;
472 # Page already exists. Import the history, and if necessary
473 # we'll update the latest revision field in the record.
475 $previousRevId = $page->page_latest;
477 # Get the time span of this page
478 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
479 array( 'rev_id' => $previousRevId ),
480 __METHOD__ );
482 if ( $previousTimestamp === false ) {
483 wfDebug( __METHOD__ . ": existing page refers to a page_latest that does not exist\n" );
485 $status = Status::newGood( 0 );
486 $status->warning( 'undeleterevision-missing' );
488 return $status;
490 } else {
491 # Have to create a new article...
492 $makepage = true;
493 $previousRevId = 0;
494 $previousTimestamp = 0;
497 $oldWhere = array(
498 'ar_namespace' => $this->title->getNamespace(),
499 'ar_title' => $this->title->getDBkey(),
501 if ( !$restoreAll ) {
502 $oldWhere['ar_timestamp'] = array_map( array( &$dbw, 'timestamp' ), $timestamps );
505 $fields = array(
506 'ar_rev_id',
507 'ar_text',
508 'ar_comment',
509 'ar_user',
510 'ar_user_text',
511 'ar_timestamp',
512 'ar_minor_edit',
513 'ar_flags',
514 'ar_text_id',
515 'ar_deleted',
516 'ar_page_id',
517 'ar_len',
518 'ar_sha1'
521 if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
522 $fields[] = 'ar_content_format';
523 $fields[] = 'ar_content_model';
527 * Select each archived revision...
529 $result = $dbw->select( 'archive',
530 $fields,
531 $oldWhere,
532 __METHOD__,
533 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
535 $ret = $dbw->resultObject( $result );
536 $rev_count = $dbw->numRows( $result );
538 if ( !$rev_count ) {
539 wfDebug( __METHOD__ . ": no revisions to restore\n" );
541 $status = Status::newGood( 0 );
542 $status->warning( "undelete-no-results" );
544 return $status;
547 $ret->seek( $rev_count - 1 ); // move to last
548 $row = $ret->fetchObject(); // get newest archived rev
549 $oldPageId = (int)$row->ar_page_id; // pass this to ArticleUndelete hook
550 $ret->seek( 0 ); // move back
552 // grab the content to check consistency with global state before restoring the page.
553 $revision = Revision::newFromArchiveRow( $row,
554 array(
555 'title' => $article->getTitle(), // used to derive default content model
558 $user = User::newFromName( $revision->getRawUserText(), false );
559 $content = $revision->getContent( Revision::RAW );
561 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
562 $status = $content->prepareSave( $article, 0, -1, $user );
564 if ( !$status->isOK() ) {
565 return $status;
568 if ( $makepage ) {
569 // Check the state of the newest to-be version...
570 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
571 return Status::newFatal( "undeleterevdel" );
573 // Safe to insert now...
574 $newid = $article->insertOn( $dbw );
575 $pageId = $newid;
576 } else {
577 // Check if a deleted revision will become the current revision...
578 if ( $row->ar_timestamp > $previousTimestamp ) {
579 // Check the state of the newest to-be version...
580 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
581 return Status::newFatal( "undeleterevdel" );
585 $newid = false;
586 $pageId = $article->getId();
589 $revision = null;
590 $restored = 0;
592 foreach ( $ret as $row ) {
593 // Check for key dupes due to shitty archive integrity.
594 if ( $row->ar_rev_id ) {
595 $exists = $dbw->selectField( 'revision', '1',
596 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
597 if ( $exists ) {
598 continue; // don't throw DB errors
601 // Insert one revision at a time...maintaining deletion status
602 // unless we are specifically removing all restrictions...
603 $revision = Revision::newFromArchiveRow( $row,
604 array(
605 'page' => $pageId,
606 'title' => $this->title,
607 'deleted' => $unsuppress ? 0 : $row->ar_deleted
608 ) );
610 $revision->insertOn( $dbw );
611 $restored++;
613 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
615 # Now that it's safely stored, take it out of the archive
616 $dbw->delete( 'archive',
617 $oldWhere,
618 __METHOD__ );
620 // Was anything restored at all?
621 if ( $restored == 0 ) {
622 return Status::newGood( 0 );
625 $created = (bool)$newid;
627 // Attach the latest revision to the page...
628 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
629 if ( $created || $wasnew ) {
630 // Update site stats, link tables, etc
631 $user = User::newFromName( $revision->getRawUserText(), false );
632 $article->doEditUpdates(
633 $revision,
634 $user,
635 array( 'created' => $created, 'oldcountable' => $oldcountable )
639 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment, $oldPageId ) );
641 if ( $this->title->getNamespace() == NS_FILE ) {
642 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
643 $update->doUpdate();
646 return Status::newGood( $restored );
650 * @return Status
652 function getFileStatus() {
653 return $this->fileStatus;
657 * @return Status
659 function getRevisionStatus() {
660 return $this->revisionStatus;
665 * Special page allowing users with the appropriate permissions to view
666 * and restore deleted content.
668 * @ingroup SpecialPage
670 class SpecialUndelete extends SpecialPage {
671 private $mAction;
672 private $mTarget;
673 private $mTimestamp;
674 private $mRestore;
675 private $mInvert;
676 private $mFilename;
677 private $mTargetTimestamp;
678 private $mAllowed;
679 private $mCanView;
680 private $mComment;
681 private $mToken;
683 /** @var Title */
684 private $mTargetObj;
686 function __construct() {
687 parent::__construct( 'Undelete', 'deletedhistory' );
690 function loadRequest( $par ) {
691 $request = $this->getRequest();
692 $user = $this->getUser();
694 $this->mAction = $request->getVal( 'action' );
695 if ( $par !== null && $par !== '' ) {
696 $this->mTarget = $par;
697 } else {
698 $this->mTarget = $request->getVal( 'target' );
701 $this->mTargetObj = null;
703 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
704 $this->mTargetObj = Title::newFromURL( $this->mTarget );
707 $this->mSearchPrefix = $request->getText( 'prefix' );
708 $time = $request->getVal( 'timestamp' );
709 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
710 $this->mFilename = $request->getVal( 'file' );
712 $posted = $request->wasPosted() &&
713 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
714 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
715 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
716 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
717 $this->mDiff = $request->getCheck( 'diff' );
718 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
719 $this->mComment = $request->getText( 'wpComment' );
720 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
721 $this->mToken = $request->getVal( 'token' );
723 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
724 $this->mAllowed = true; // user can restore
725 $this->mCanView = true; // user can view content
726 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
727 $this->mAllowed = false; // user cannot restore
728 $this->mCanView = true; // user can view content
729 $this->mRestore = false;
730 } else { // user can only view the list of revisions
731 $this->mAllowed = false;
732 $this->mCanView = false;
733 $this->mTimestamp = '';
734 $this->mRestore = false;
737 if ( $this->mRestore || $this->mInvert ) {
738 $timestamps = array();
739 $this->mFileVersions = array();
740 foreach ( $request->getValues() as $key => $val ) {
741 $matches = array();
742 if ( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
743 array_push( $timestamps, $matches[1] );
746 if ( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
747 $this->mFileVersions[] = intval( $matches[1] );
750 rsort( $timestamps );
751 $this->mTargetTimestamp = $timestamps;
755 function execute( $par ) {
756 $this->checkPermissions();
757 $user = $this->getUser();
759 $this->setHeaders();
760 $this->outputHeader();
762 $this->loadRequest( $par );
764 $out = $this->getOutput();
766 if ( is_null( $this->mTargetObj ) ) {
767 $out->addWikiMsg( 'undelete-header' );
769 # Not all users can just browse every deleted page from the list
770 if ( $user->isAllowed( 'browsearchive' ) ) {
771 $this->showSearchForm();
774 return;
777 if ( $this->mAllowed ) {
778 $out->setPageTitle( $this->msg( 'undeletepage' ) );
779 } else {
780 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
783 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
785 if ( $this->mTimestamp !== '' ) {
786 $this->showRevision( $this->mTimestamp );
787 } elseif ( $this->mFilename !== null && $this->mTargetObj->inNamespace( NS_FILE ) ) {
788 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
789 // Check if user is allowed to see this file
790 if ( !$file->exists() ) {
791 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename );
792 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
793 if ( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
794 throw new PermissionsError( 'suppressrevision' );
795 } else {
796 throw new PermissionsError( 'deletedtext' );
798 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
799 $this->showFileConfirmationForm( $this->mFilename );
800 } else {
801 $this->showFile( $this->mFilename );
803 } elseif ( $this->mRestore && $this->mAction == 'submit' ) {
804 $this->undelete();
805 } else {
806 $this->showHistory();
810 function showSearchForm() {
811 $out = $this->getOutput();
812 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
813 $out->addHTML(
814 Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) ) .
815 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
816 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
817 Html::rawElement(
818 'label',
819 array( 'for' => 'prefix' ),
820 $this->msg( 'undelete-search-prefix' )->parse()
822 Xml::input(
823 'prefix',
825 $this->mSearchPrefix,
826 array( 'id' => 'prefix', 'autofocus' => true )
827 ) . ' ' .
828 Xml::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
829 Xml::closeElement( 'fieldset' ) .
830 Xml::closeElement( 'form' )
833 # List undeletable articles
834 if ( $this->mSearchPrefix ) {
835 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
836 $this->showList( $result );
841 * Generic list of deleted pages
843 * @param ResultWrapper $result
844 * @return bool
846 private function showList( $result ) {
847 $out = $this->getOutput();
849 if ( $result->numRows() == 0 ) {
850 $out->addWikiMsg( 'undelete-no-results' );
852 return false;
855 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
857 $undelete = $this->getPageTitle();
858 $out->addHTML( "<ul>\n" );
859 foreach ( $result as $row ) {
860 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
861 if ( $title !== null ) {
862 $item = Linker::linkKnown(
863 $undelete,
864 htmlspecialchars( $title->getPrefixedText() ),
865 array(),
866 array( 'target' => $title->getPrefixedText() )
868 } else {
869 // The title is no longer valid, show as text
870 $item = Html::element(
871 'span',
872 array( 'class' => 'mw-invalidtitle' ),
873 Linker::getInvalidTitleDescription(
874 $this->getContext(),
875 $row->ar_namespace,
876 $row->ar_title
880 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
881 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
883 $result->free();
884 $out->addHTML( "</ul>\n" );
886 return true;
889 private function showRevision( $timestamp ) {
890 if ( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
891 return;
894 $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
895 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) ) ) {
896 return;
898 $rev = $archive->getRevision( $timestamp );
900 $out = $this->getOutput();
901 $user = $this->getUser();
903 if ( !$rev ) {
904 $out->addWikiMsg( 'undeleterevision-missing' );
906 return;
909 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
910 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
911 $out->wrapWikiMsg(
912 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
913 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ?
914 'rev-suppressed-text-permission' : 'rev-deleted-text-permission'
917 return;
920 $out->wrapWikiMsg(
921 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
922 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ?
923 'rev-suppressed-text-view' : 'rev-deleted-text-view'
925 $out->addHTML( '<br />' );
926 // and we are allowed to see...
929 if ( $this->mDiff ) {
930 $previousRev = $archive->getPreviousRevision( $timestamp );
931 if ( $previousRev ) {
932 $this->showDiff( $previousRev, $rev );
933 if ( $this->mDiffOnly ) {
934 return;
937 $out->addHTML( '<hr />' );
938 } else {
939 $out->addWikiMsg( 'undelete-nodiff' );
943 $link = Linker::linkKnown(
944 $this->getPageTitle( $this->mTargetObj->getPrefixedDBkey() ),
945 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
948 $lang = $this->getLanguage();
950 // date and time are separate parameters to facilitate localisation.
951 // $time is kept for backward compat reasons.
952 $time = $lang->userTimeAndDate( $timestamp, $user );
953 $d = $lang->userDate( $timestamp, $user );
954 $t = $lang->userTime( $timestamp, $user );
955 $userLink = Linker::revUserTools( $rev );
957 $content = $rev->getContent( Revision::FOR_THIS_USER, $user );
959 $isText = ( $content instanceof TextContent );
961 if ( $this->mPreview || $isText ) {
962 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
963 } else {
964 $openDiv = '<div id="mw-undelete-revision">';
966 $out->addHTML( $openDiv );
968 // Revision delete links
969 if ( !$this->mDiff ) {
970 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
971 if ( $revdel ) {
972 $out->addHTML( "$revdel " );
976 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
977 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
979 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) ) ) {
980 return;
983 if ( $this->mPreview || !$isText ) {
984 // NOTE: non-text content has no source view, so always use rendered preview
986 // Hide [edit]s
987 $popts = $out->parserOptions();
988 $popts->setEditSection( false );
990 $pout = $content->getParserOutput( $this->mTargetObj, $rev->getId(), $popts, true );
991 $out->addParserOutput( $pout );
994 if ( $isText ) {
995 // source view for textual content
996 $sourceView = Xml::element(
997 'textarea',
998 array(
999 'readonly' => 'readonly',
1000 'cols' => $user->getIntOption( 'cols' ),
1001 'rows' => $user->getIntOption( 'rows' )
1003 $content->getNativeData() . "\n"
1006 $previewButton = Xml::element( 'input', array(
1007 'type' => 'submit',
1008 'name' => 'preview',
1009 'value' => $this->msg( 'showpreview' )->text()
1010 ) );
1011 } else {
1012 $sourceView = '';
1013 $previewButton = '';
1016 $diffButton = Xml::element( 'input', array(
1017 'name' => 'diff',
1018 'type' => 'submit',
1019 'value' => $this->msg( 'showdiff' )->text() ) );
1021 $out->addHTML(
1022 $sourceView .
1023 Xml::openElement( 'div', array(
1024 'style' => 'clear: both' ) ) .
1025 Xml::openElement( 'form', array(
1026 'method' => 'post',
1027 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
1028 Xml::element( 'input', array(
1029 'type' => 'hidden',
1030 'name' => 'target',
1031 'value' => $this->mTargetObj->getPrefixedDBkey() ) ) .
1032 Xml::element( 'input', array(
1033 'type' => 'hidden',
1034 'name' => 'timestamp',
1035 'value' => $timestamp ) ) .
1036 Xml::element( 'input', array(
1037 'type' => 'hidden',
1038 'name' => 'wpEditToken',
1039 'value' => $user->getEditToken() ) ) .
1040 $previewButton .
1041 $diffButton .
1042 Xml::closeElement( 'form' ) .
1043 Xml::closeElement( 'div' )
1048 * Build a diff display between this and the previous either deleted
1049 * or non-deleted edit.
1051 * @param Revision $previousRev
1052 * @param Revision $currentRev
1053 * @return string HTML
1055 function showDiff( $previousRev, $currentRev ) {
1056 $diffContext = clone $this->getContext();
1057 $diffContext->setTitle( $currentRev->getTitle() );
1058 $diffContext->setWikiPage( WikiPage::factory( $currentRev->getTitle() ) );
1060 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1061 $diffEngine->showDiffStyle();
1063 $formattedDiff = $diffEngine->generateContentDiffBody(
1064 $previousRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ),
1065 $currentRev->getContent( Revision::FOR_THIS_USER, $this->getUser() )
1068 $formattedDiff = $diffEngine->addHeader(
1069 $formattedDiff,
1070 $this->diffHeader( $previousRev, 'o' ),
1071 $this->diffHeader( $currentRev, 'n' )
1074 $this->getOutput()->addHTML( "<div>$formattedDiff</div>\n" );
1078 * @param Revision $rev
1079 * @param string $prefix
1080 * @return string
1082 private function diffHeader( $rev, $prefix ) {
1083 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1084 if ( $isDeleted ) {
1085 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1086 $targetPage = $this->getPageTitle();
1087 $targetQuery = array(
1088 'target' => $this->mTargetObj->getPrefixedText(),
1089 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
1091 } else {
1092 /// @todo FIXME: getId() may return non-zero for deleted revs...
1093 $targetPage = $rev->getTitle();
1094 $targetQuery = array( 'oldid' => $rev->getId() );
1097 // Add show/hide deletion links if available
1098 $user = $this->getUser();
1099 $lang = $this->getLanguage();
1100 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1102 if ( $rdel ) {
1103 $rdel = " $rdel";
1106 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1108 $tags = wfGetDB( DB_SLAVE )->selectField(
1109 'tag_summary',
1110 'ts_tags',
1111 array( 'ts_rev_id' => $rev->getId() ),
1112 __METHOD__
1114 $tagSummary = ChangeTags::formatSummaryRow( $tags, 'deleteddiff' );
1116 // FIXME This is reimplementing DifferenceEngine#getRevisionHeader
1117 // and partially #showDiffPage, but worse
1118 return '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1119 Linker::link(
1120 $targetPage,
1121 $this->msg(
1122 'revisionasof',
1123 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1124 $lang->userDate( $rev->getTimestamp(), $user ),
1125 $lang->userTime( $rev->getTimestamp(), $user )
1126 )->escaped(),
1127 array(),
1128 $targetQuery
1130 '</strong></div>' .
1131 '<div id="mw-diff-' . $prefix . 'title2">' .
1132 Linker::revUserTools( $rev ) . '<br />' .
1133 '</div>' .
1134 '<div id="mw-diff-' . $prefix . 'title3">' .
1135 $minor . Linker::revComment( $rev ) . $rdel . '<br />' .
1136 '</div>' .
1137 '<div id="mw-diff-' . $prefix . 'title5">' .
1138 $tagSummary[0] . '<br />' .
1139 '</div>';
1143 * Show a form confirming whether a tokenless user really wants to see a file
1144 * @param string $key
1146 private function showFileConfirmationForm( $key ) {
1147 $out = $this->getOutput();
1148 $lang = $this->getLanguage();
1149 $user = $this->getUser();
1150 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
1151 $out->addWikiMsg( 'undelete-show-file-confirm',
1152 $this->mTargetObj->getText(),
1153 $lang->userDate( $file->getTimestamp(), $user ),
1154 $lang->userTime( $file->getTimestamp(), $user ) );
1155 $out->addHTML(
1156 Xml::openElement( 'form', array(
1157 'method' => 'POST',
1158 'action' => $this->getPageTitle()->getLocalURL( array(
1159 'target' => $this->mTarget,
1160 'file' => $key,
1161 'token' => $user->getEditToken( $key ),
1162 ) ),
1165 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1166 '</form>'
1171 * Show a deleted file version requested by the visitor.
1172 * @param string $key
1174 private function showFile( $key ) {
1175 $this->getOutput()->disable();
1177 # We mustn't allow the output to be Squid cached, otherwise
1178 # if an admin previews a deleted image, and it's cached, then
1179 # a user without appropriate permissions can toddle off and
1180 # nab the image, and Squid will serve it
1181 $response = $this->getRequest()->response();
1182 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1183 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1184 $response->header( 'Pragma: no-cache' );
1186 $repo = RepoGroup::singleton()->getLocalRepo();
1187 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1188 $repo->streamFile( $path );
1191 private function showHistory() {
1192 $out = $this->getOutput();
1193 if ( $this->mAllowed ) {
1194 $out->addModules( 'mediawiki.special.undelete' );
1196 $out->wrapWikiMsg(
1197 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1198 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
1201 $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
1202 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1204 $text = $archive->getLastRevisionText();
1205 if( is_null( $text ) ) {
1206 $out->addWikiMsg( 'nohistory' );
1207 return;
1210 $out->addHTML( '<div class="mw-undelete-history">' );
1211 if ( $this->mAllowed ) {
1212 $out->addWikiMsg( 'undeletehistory' );
1213 $out->addWikiMsg( 'undeleterevdel' );
1214 } else {
1215 $out->addWikiMsg( 'undeletehistorynoadmin' );
1217 $out->addHTML( '</div>' );
1219 # List all stored revisions
1220 $revisions = $archive->listRevisions();
1221 $files = $archive->listFiles();
1223 $haveRevisions = $revisions && $revisions->numRows() > 0;
1224 $haveFiles = $files && $files->numRows() > 0;
1226 # Batch existence check on user and talk pages
1227 if ( $haveRevisions ) {
1228 $batch = new LinkBatch();
1229 foreach ( $revisions as $row ) {
1230 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1231 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1233 $batch->execute();
1234 $revisions->seek( 0 );
1236 if ( $haveFiles ) {
1237 $batch = new LinkBatch();
1238 foreach ( $files as $row ) {
1239 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1240 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1242 $batch->execute();
1243 $files->seek( 0 );
1246 if ( $this->mAllowed ) {
1247 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
1248 # Start the form here
1249 $top = Xml::openElement(
1250 'form',
1251 array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' )
1253 $out->addHTML( $top );
1256 # Show relevant lines from the deletion log:
1257 $deleteLogPage = new LogPage( 'delete' );
1258 $out->addHTML( Xml::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1259 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1260 # Show relevant lines from the suppression log:
1261 $suppressLogPage = new LogPage( 'suppress' );
1262 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1263 $out->addHTML( Xml::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1264 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1267 if ( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1268 # Format the user-visible controls (comment field, submission button)
1269 # in a nice little table
1270 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1271 $unsuppressBox =
1272 "<tr>
1273 <td>&#160;</td>
1274 <td class='mw-input'>" .
1275 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1276 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ) .
1277 "</td>
1278 </tr>";
1279 } else {
1280 $unsuppressBox = '';
1283 $table = Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1284 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1285 "<tr>
1286 <td colspan='2' class='mw-undelete-extrahelp'>" .
1287 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1288 "</td>
1289 </tr>
1290 <tr>
1291 <td class='mw-label'>" .
1292 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1293 "</td>
1294 <td class='mw-input'>" .
1295 Xml::input(
1296 'wpComment',
1298 $this->mComment,
1299 array( 'id' => 'wpComment', 'autofocus' => true )
1301 "</td>
1302 </tr>
1303 <tr>
1304 <td>&#160;</td>
1305 <td class='mw-submit'>" .
1306 Xml::submitButton(
1307 $this->msg( 'undeletebtn' )->text(),
1308 array( 'name' => 'restore', 'id' => 'mw-undelete-submit' )
1309 ) . ' ' .
1310 Xml::submitButton(
1311 $this->msg( 'undeleteinvert' )->text(),
1312 array( 'name' => 'invert', 'id' => 'mw-undelete-invert' )
1314 "</td>
1315 </tr>" .
1316 $unsuppressBox .
1317 Xml::closeElement( 'table' ) .
1318 Xml::closeElement( 'fieldset' );
1320 $out->addHTML( $table );
1323 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1325 if ( $haveRevisions ) {
1326 # The page's stored (deleted) history:
1327 $out->addHTML( '<ul>' );
1328 $remaining = $revisions->numRows();
1329 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1331 foreach ( $revisions as $row ) {
1332 $remaining--;
1333 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1335 $revisions->free();
1336 $out->addHTML( '</ul>' );
1337 } else {
1338 $out->addWikiMsg( 'nohistory' );
1341 if ( $haveFiles ) {
1342 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1343 $out->addHTML( '<ul>' );
1344 foreach ( $files as $row ) {
1345 $out->addHTML( $this->formatFileRow( $row ) );
1347 $files->free();
1348 $out->addHTML( '</ul>' );
1351 if ( $this->mAllowed ) {
1352 # Slip in the hidden controls here
1353 $misc = Html::hidden( 'target', $this->mTarget );
1354 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1355 $misc .= Xml::closeElement( 'form' );
1356 $out->addHTML( $misc );
1359 return true;
1362 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1363 $rev = Revision::newFromArchiveRow( $row,
1364 array(
1365 'title' => $this->mTargetObj
1366 ) );
1368 $revTextSize = '';
1369 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1370 // Build checkboxen...
1371 if ( $this->mAllowed ) {
1372 if ( $this->mInvert ) {
1373 if ( in_array( $ts, $this->mTargetTimestamp ) ) {
1374 $checkBox = Xml::check( "ts$ts" );
1375 } else {
1376 $checkBox = Xml::check( "ts$ts", true );
1378 } else {
1379 $checkBox = Xml::check( "ts$ts" );
1381 } else {
1382 $checkBox = '';
1385 // Build page & diff links...
1386 $user = $this->getUser();
1387 if ( $this->mCanView ) {
1388 $titleObj = $this->getPageTitle();
1389 # Last link
1390 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1391 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1392 $last = $this->msg( 'diff' )->escaped();
1393 } elseif ( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1394 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1395 $last = Linker::linkKnown(
1396 $titleObj,
1397 $this->msg( 'diff' )->escaped(),
1398 array(),
1399 array(
1400 'target' => $this->mTargetObj->getPrefixedText(),
1401 'timestamp' => $ts,
1402 'diff' => 'prev'
1405 } else {
1406 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1407 $last = $this->msg( 'diff' )->escaped();
1409 } else {
1410 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1411 $last = $this->msg( 'diff' )->escaped();
1414 // User links
1415 $userLink = Linker::revUserTools( $rev );
1417 // Minor edit
1418 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1420 // Revision text size
1421 $size = $row->ar_len;
1422 if ( !is_null( $size ) ) {
1423 $revTextSize = Linker::formatRevisionSize( $size );
1426 // Edit summary
1427 $comment = Linker::revComment( $rev );
1429 // Tags
1430 $attribs = array();
1431 list( $tagSummary, $classes ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'deletedhistory' );
1432 if ( $classes ) {
1433 $attribs['class'] = implode( ' ', $classes );
1436 // Revision delete links
1437 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1439 $revisionRow = $this->msg( 'undelete-revision-row' )
1440 ->rawParams(
1441 $checkBox,
1442 $revdlink,
1443 $last,
1444 $pageLink,
1445 $userLink,
1446 $minor,
1447 $revTextSize,
1448 $comment,
1449 $tagSummary
1451 ->escaped();
1453 return Xml::tags( 'li', $attribs, $revisionRow ) . "\n";
1456 private function formatFileRow( $row ) {
1457 $file = ArchivedFile::newFromRow( $row );
1458 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1459 $user = $this->getUser();
1461 if ( $this->mAllowed && $row->fa_storage_key ) {
1462 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1463 $key = urlencode( $row->fa_storage_key );
1464 $pageLink = $this->getFileLink( $file, $this->getPageTitle(), $ts, $key );
1465 } else {
1466 $checkBox = '';
1467 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1469 $userLink = $this->getFileUser( $file );
1470 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1471 $bytes = $this->msg( 'parentheses' )
1472 ->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )
1473 ->plain();
1474 $data = htmlspecialchars( $data . ' ' . $bytes );
1475 $comment = $this->getFileComment( $file );
1477 // Add show/hide deletion links if available
1478 $canHide = $user->isAllowed( 'deleterevision' );
1479 if ( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1480 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1481 // Revision was hidden from sysops
1482 $revdlink = Linker::revDeleteLinkDisabled( $canHide );
1483 } else {
1484 $query = array(
1485 'type' => 'filearchive',
1486 'target' => $this->mTargetObj->getPrefixedDBkey(),
1487 'ids' => $row->fa_id
1489 $revdlink = Linker::revDeleteLink( $query,
1490 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1492 } else {
1493 $revdlink = '';
1496 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1500 * Fetch revision text link if it's available to all users
1502 * @param Revision $rev
1503 * @param Title $titleObj
1504 * @param string $ts Timestamp
1505 * @return string
1507 function getPageLink( $rev, $titleObj, $ts ) {
1508 $user = $this->getUser();
1509 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1511 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1512 return '<span class="history-deleted">' . $time . '</span>';
1515 $link = Linker::linkKnown(
1516 $titleObj,
1517 htmlspecialchars( $time ),
1518 array(),
1519 array(
1520 'target' => $this->mTargetObj->getPrefixedText(),
1521 'timestamp' => $ts
1525 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1526 $link = '<span class="history-deleted">' . $link . '</span>';
1529 return $link;
1533 * Fetch image view link if it's available to all users
1535 * @param File|ArchivedFile $file
1536 * @param Title $titleObj
1537 * @param string $ts A timestamp
1538 * @param string $key A storage key
1540 * @return string HTML fragment
1542 function getFileLink( $file, $titleObj, $ts, $key ) {
1543 $user = $this->getUser();
1544 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1546 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
1547 return '<span class="history-deleted">' . $time . '</span>';
1550 $link = Linker::linkKnown(
1551 $titleObj,
1552 htmlspecialchars( $time ),
1553 array(),
1554 array(
1555 'target' => $this->mTargetObj->getPrefixedText(),
1556 'file' => $key,
1557 'token' => $user->getEditToken( $key )
1561 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1562 $link = '<span class="history-deleted">' . $link . '</span>';
1565 return $link;
1569 * Fetch file's user id if it's available to this user
1571 * @param File|ArchivedFile $file
1572 * @return string HTML fragment
1574 function getFileUser( $file ) {
1575 if ( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1576 return '<span class="history-deleted">' .
1577 $this->msg( 'rev-deleted-user' )->escaped() .
1578 '</span>';
1581 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1582 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1584 if ( $file->isDeleted( File::DELETED_USER ) ) {
1585 $link = '<span class="history-deleted">' . $link . '</span>';
1588 return $link;
1592 * Fetch file upload comment if it's available to this user
1594 * @param File|ArchivedFile $file
1595 * @return string HTML fragment
1597 function getFileComment( $file ) {
1598 if ( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1599 return '<span class="history-deleted"><span class="comment">' .
1600 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1603 $link = Linker::commentBlock( $file->getRawDescription() );
1605 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1606 $link = '<span class="history-deleted">' . $link . '</span>';
1609 return $link;
1612 function undelete() {
1613 if ( $this->getConfig()->get( 'UploadMaintenance' ) && $this->mTargetObj->getNamespace() == NS_FILE ) {
1614 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1617 if ( wfReadOnly() ) {
1618 throw new ReadOnlyError;
1621 $out = $this->getOutput();
1622 $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
1623 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1624 $ok = $archive->undelete(
1625 $this->mTargetTimestamp,
1626 $this->mComment,
1627 $this->mFileVersions,
1628 $this->mUnsuppress,
1629 $this->getUser()
1632 if ( is_array( $ok ) ) {
1633 if ( $ok[1] ) { // Undeleted file count
1634 wfRunHooks( 'FileUndeleteComplete', array(
1635 $this->mTargetObj, $this->mFileVersions,
1636 $this->getUser(), $this->mComment ) );
1639 $link = Linker::linkKnown( $this->mTargetObj );
1640 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1641 } else {
1642 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1645 // Show revision undeletion warnings and errors
1646 $status = $archive->getRevisionStatus();
1647 if ( $status && !$status->isGood() ) {
1648 $out->addWikiText( '<div class="error">' .
1649 $status->getWikiText(
1650 'cannotundelete',
1651 'cannotundelete'
1652 ) . '</div>'
1656 // Show file undeletion warnings and errors
1657 $status = $archive->getFileStatus();
1658 if ( $status && !$status->isGood() ) {
1659 $out->addWikiText( '<div class="error">' .
1660 $status->getWikiText(
1661 'undelete-error-short',
1662 'undelete-error-long'
1663 ) . '</div>'
1668 protected function getGroupName() {
1669 return 'pagetools';