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
21 * @ingroup SpecialPage
25 * Used to show archived pages and eventually restore them.
27 * @ingroup SpecialPage
39 protected $fileStatus;
44 protected $revisionStatus;
46 function __construct( $title ) {
47 if( is_null( $title ) ) {
48 throw new MWException( __METHOD__
. ' given a null title.' );
50 $this->title
= $title;
54 * List all deleted pages recorded in the archive table. Returns result
55 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
58 * @return ResultWrapper
60 public static function listAllPages() {
61 $dbr = wfGetDB( DB_SLAVE
);
62 return self
::listPages( $dbr, '' );
66 * List deleted pages recorded in the archive table matching the
68 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
70 * @param $prefix String: title prefix
71 * @return ResultWrapper
73 public static function listPagesByPrefix( $prefix ) {
74 $dbr = wfGetDB( DB_SLAVE
);
76 $title = Title
::newFromText( $prefix );
78 $ns = $title->getNamespace();
79 $prefix = $title->getDBkey();
81 // Prolly won't work too good
82 // @todo handle bare namespace names cleanly?
86 'ar_namespace' => $ns,
87 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
89 return self
::listPages( $dbr, $conds );
93 * @param $dbr DatabaseBase
95 * @return bool|ResultWrapper
97 protected static function listPages( $dbr, $condition ) {
98 return $dbr->resultObject(
104 'count' => 'COUNT(*)'
109 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
110 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
118 * List the revisions of the given page. Returns result wrapper with
119 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
121 * @return ResultWrapper
123 function listRevisions() {
124 global $wgContentHandlerUseDB;
126 $dbr = wfGetDB( DB_SLAVE
);
129 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
130 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
133 if ( $wgContentHandlerUseDB ) {
134 $fields[] = 'ar_content_format';
135 $fields[] = 'ar_content_model';
138 $res = $dbr->select( 'archive',
140 array( 'ar_namespace' => $this->title
->getNamespace(),
141 'ar_title' => $this->title
->getDBkey() ),
143 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
144 $ret = $dbr->resultObject( $res );
149 * List the deleted file revisions for this page, if it's a file page.
150 * Returns a result wrapper with various filearchive fields, or null
151 * if not a file page.
153 * @return ResultWrapper
154 * @todo Does this belong in Image for fuller encapsulation?
156 function listFiles() {
157 if( $this->title
->getNamespace() == NS_FILE
) {
158 $dbr = wfGetDB( DB_SLAVE
);
159 $res = $dbr->select( 'filearchive',
180 array( 'fa_name' => $this->title
->getDBkey() ),
182 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
183 $ret = $dbr->resultObject( $res );
190 * Return a Revision object containing data for the deleted revision.
191 * Note that the result *may* or *may not* have a null page ID.
193 * @param $timestamp String
196 function getRevision( $timestamp ) {
197 global $wgContentHandlerUseDB;
199 $dbr = wfGetDB( DB_SLAVE
);
216 if ( $wgContentHandlerUseDB ) {
217 $fields[] = 'ar_content_format';
218 $fields[] = 'ar_content_model';
221 $row = $dbr->selectRow( 'archive',
223 array( 'ar_namespace' => $this->title
->getNamespace(),
224 'ar_title' => $this->title
->getDBkey(),
225 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
228 return Revision
::newFromArchiveRow( $row, array( 'title' => $this->title
) );
235 * Return the most-previous revision, either live or deleted, against
236 * the deleted revision given by timestamp.
238 * May produce unexpected results in case of history merges or other
239 * unusual time issues.
241 * @param $timestamp String
242 * @return Revision or null
244 function getPreviousRevision( $timestamp ) {
245 $dbr = wfGetDB( DB_SLAVE
);
247 // Check the previous deleted revision...
248 $row = $dbr->selectRow( 'archive',
250 array( 'ar_namespace' => $this->title
->getNamespace(),
251 'ar_title' => $this->title
->getDBkey(),
253 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
256 'ORDER BY' => 'ar_timestamp DESC',
258 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
260 $row = $dbr->selectRow( array( 'page', 'revision' ),
261 array( 'rev_id', 'rev_timestamp' ),
263 'page_namespace' => $this->title
->getNamespace(),
264 'page_title' => $this->title
->getDBkey(),
265 'page_id = rev_page',
267 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
270 'ORDER BY' => 'rev_timestamp DESC',
272 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
273 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
275 if( $prevLive && $prevLive > $prevDeleted ) {
276 // Most prior revision was live
277 return Revision
::newFromId( $prevLiveId );
278 } elseif( $prevDeleted ) {
279 // Most prior revision was deleted
280 return $this->getRevision( $prevDeleted );
282 // No prior revision on this page.
288 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
290 * @param $row Object: database row
293 function getTextFromRow( $row ) {
294 if( is_null( $row->ar_text_id
) ) {
295 // An old row from MediaWiki 1.4 or previous.
296 // Text is embedded in this row in classic compression format.
297 return Revision
::getRevisionText( $row, 'ar_' );
299 // New-style: keyed to the text storage backend.
300 $dbr = wfGetDB( DB_SLAVE
);
301 $text = $dbr->selectRow( 'text',
302 array( 'old_text', 'old_flags' ),
303 array( 'old_id' => $row->ar_text_id
),
305 return Revision
::getRevisionText( $text );
310 * Fetch (and decompress if necessary) the stored text of the most
311 * recently edited deleted revision of the page.
313 * If there are no archived revisions for the page, returns NULL.
317 function getLastRevisionText() {
318 $dbr = wfGetDB( DB_SLAVE
);
319 $row = $dbr->selectRow( 'archive',
320 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
321 array( 'ar_namespace' => $this->title
->getNamespace(),
322 'ar_title' => $this->title
->getDBkey() ),
324 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
326 return $this->getTextFromRow( $row );
333 * Quick check if any archived revisions are present for the page.
337 function isDeleted() {
338 $dbr = wfGetDB( DB_SLAVE
);
339 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
340 array( 'ar_namespace' => $this->title
->getNamespace(),
341 'ar_title' => $this->title
->getDBkey() ),
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 $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
353 * @param $comment String
354 * @param $fileVersions Array
355 * @param $unsuppress Boolean
356 * @param $user User doing the action, or null to use $wgUser
358 * @return array(number of file revisions restored, number of image revisions restored, log message)
359 * on success, false on failure
361 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
362 // If both the set of text revisions and file revisions are empty,
363 // restore everything. Otherwise, just restore the requested items.
364 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
366 $restoreText = $restoreAll ||
!empty( $timestamps );
367 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
369 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
370 $img = wfLocalFile( $this->title
);
371 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
372 if ( !$this->fileStatus
->isOK() ) {
375 $filesRestored = $this->fileStatus
->successCount
;
381 $this->revisionStatus
= $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
382 if( !$this->revisionStatus
->isOK() ) {
386 $textRestored = $this->revisionStatus
->getValue();
393 if( $textRestored && $filesRestored ) {
394 $reason = wfMessage( 'undeletedrevisions-files' )
395 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
396 } elseif( $textRestored ) {
397 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
398 ->inContentLanguage()->text();
399 } elseif( $filesRestored ) {
400 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
401 ->inContentLanguage()->text();
403 wfDebug( "Undelete: nothing undeleted...\n" );
407 if( trim( $comment ) != '' ) {
408 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
411 if ( $user === null ) {
416 $logEntry = new ManualLogEntry( 'delete', 'restore' );
417 $logEntry->setPerformer( $user );
418 $logEntry->setTarget( $this->title
);
419 $logEntry->setComment( $reason );
421 wfRunHooks( 'ArticleUndeleteLogEntry', array( $this, &$logEntry, $user ) );
423 $logid = $logEntry->insert();
424 $logEntry->publish( $logid );
426 return array( $textRestored, $filesRestored, $reason );
430 * This is the meaty bit -- restores archived revisions of the given page
431 * to the cur/old tables. If the page currently exists, all revisions will
432 * be stuffed into old, otherwise the most recent will go into cur.
434 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
435 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
437 * @param $comment String
438 * @throws ReadOnlyError
439 * @return Status, containing the number of revisions restored on success
441 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
442 global $wgContentHandlerUseDB;
444 if ( wfReadOnly() ) {
445 throw new ReadOnlyError();
447 $restoreAll = empty( $timestamps );
449 $dbw = wfGetDB( DB_MASTER
);
451 # Does this page already exist? We'll have to update it...
452 $article = WikiPage
::factory( $this->title
);
453 # Load latest data for the current page (bug 31179)
454 $article->loadPageData( 'fromdbmaster' );
455 $oldcountable = $article->isCountable();
457 $page = $dbw->selectRow( 'page',
458 array( 'page_id', 'page_latest' ),
459 array( 'page_namespace' => $this->title
->getNamespace(),
460 'page_title' => $this->title
->getDBkey() ),
462 array( 'FOR UPDATE' ) // lock page
466 # Page already exists. Import the history, and if necessary
467 # we'll update the latest revision field in the record.
469 $pageId = $page->page_id
;
470 $previousRevId = $page->page_latest
;
471 # Get the time span of this page
472 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
473 array( 'rev_id' => $previousRevId ),
476 if( $previousTimestamp === false ) {
477 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
479 $status = Status
::newGood( 0 );
480 $status->warning( 'undeleterevision-missing' );
485 # Have to create a new article...
488 $previousTimestamp = 0;
492 $oldones = '1 = 1'; # All revisions...
494 $oldts = implode( ',',
495 array_map( array( &$dbw, 'addQuotes' ),
496 array_map( array( &$dbw, 'timestamp' ),
499 $oldones = "ar_timestamp IN ( {$oldts} )";
517 if ( $wgContentHandlerUseDB ) {
518 $fields[] = 'ar_content_format';
519 $fields[] = 'ar_content_model';
523 * Select each archived revision...
525 $result = $dbw->select( 'archive',
528 'ar_namespace' => $this->title
->getNamespace(),
529 'ar_title' => $this->title
->getDBkey(),
532 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
534 $ret = $dbw->resultObject( $result );
535 $rev_count = $dbw->numRows( $result );
537 wfDebug( __METHOD__
. ": no revisions to restore\n" );
539 $status = Status
::newGood( 0 );
540 $status->warning( "undelete-no-results" );
544 $ret->seek( $rev_count - 1 ); // move to last
545 $row = $ret->fetchObject(); // get newest archived rev
546 $ret->seek( 0 ); // move back
548 // grab the content to check consistency with global state before restoring the page.
549 $revision = Revision
::newFromArchiveRow( $row,
551 'title' => $article->getTitle(), // used to derive default content model
554 $m = $revision->getContentModel();
556 $user = User
::newFromName( $revision->getRawUserText(), false );
557 $content = $revision->getContent( Revision
::RAW
);
559 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
560 $status = $content->prepareSave( $article, 0, -1, $user );
562 if ( !$status->isOK() ) {
567 // Check the state of the newest to-be version...
568 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
569 return Status
::newFatal( "undeleterevdel" );
571 // Safe to insert now...
572 $newid = $article->insertOn( $dbw );
575 // Check if a deleted revision will become the current revision...
576 if( $row->ar_timestamp
> $previousTimestamp ) {
577 // Check the state of the newest to-be version...
578 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
579 return Status
::newFatal( "undeleterevdel" );
584 $pageId = $article->getId();
590 foreach ( $ret as $row ) {
591 // Check for key dupes due to shitty archive integrity.
592 if( $row->ar_rev_id
) {
593 $exists = $dbw->selectField( 'revision', '1',
594 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
596 continue; // don't throw DB errors
599 // Insert one revision at a time...maintaining deletion status
600 // unless we are specifically removing all restrictions...
601 $revision = Revision
::newFromArchiveRow( $row,
604 'title' => $this->title
,
605 'deleted' => $unsuppress ?
0 : $row->ar_deleted
608 $revision->insertOn( $dbw );
611 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
613 # Now that it's safely stored, take it out of the archive
614 $dbw->delete( 'archive',
616 'ar_namespace' => $this->title
->getNamespace(),
617 'ar_title' => $this->title
->getDBkey(),
621 // Was anything restored at all?
622 if ( $restored == 0 ) {
623 return Status
::newGood( 0 );
626 $created = (bool)$newid;
628 // Attach the latest revision to the page...
629 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
630 if ( $created ||
$wasnew ) {
631 // Update site stats, link tables, etc
632 $user = User
::newFromName( $revision->getRawUserText(), false );
633 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
636 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
638 if( $this->title
->getNamespace() == NS_FILE
) {
639 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
643 return Status
::newGood( $restored );
649 function getFileStatus() { return $this->fileStatus
; }
654 function getRevisionStatus() { return $this->revisionStatus
; }
658 * Special page allowing users with the appropriate permissions to view
659 * and restore deleted content.
661 * @ingroup SpecialPage
663 class SpecialUndelete
extends SpecialPage
{
664 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
665 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
672 function __construct() {
673 parent
::__construct( 'Undelete', 'deletedhistory' );
676 function loadRequest( $par ) {
677 $request = $this->getRequest();
678 $user = $this->getUser();
680 $this->mAction
= $request->getVal( 'action' );
681 if ( $par !== null && $par !== '' ) {
682 $this->mTarget
= $par;
684 $this->mTarget
= $request->getVal( 'target' );
686 $this->mTargetObj
= null;
687 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
688 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
690 $this->mSearchPrefix
= $request->getText( 'prefix' );
691 $time = $request->getVal( 'timestamp' );
692 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
693 $this->mFilename
= $request->getVal( 'file' );
695 $posted = $request->wasPosted() &&
696 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
697 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
698 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
699 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
700 $this->mDiff
= $request->getCheck( 'diff' );
701 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
702 $this->mComment
= $request->getText( 'wpComment' );
703 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
704 $this->mToken
= $request->getVal( 'token' );
706 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
707 $this->mAllowed
= true; // user can restore
708 $this->mCanView
= true; // user can view content
709 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
710 $this->mAllowed
= false; // user cannot restore
711 $this->mCanView
= true; // user can view content
712 $this->mRestore
= false;
713 } else { // user can only view the list of revisions
714 $this->mAllowed
= false;
715 $this->mCanView
= false;
716 $this->mTimestamp
= '';
717 $this->mRestore
= false;
720 if( $this->mRestore ||
$this->mInvert
) {
721 $timestamps = array();
722 $this->mFileVersions
= array();
723 foreach( $request->getValues() as $key => $val ) {
725 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
726 array_push( $timestamps, $matches[1] );
729 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
730 $this->mFileVersions
[] = intval( $matches[1] );
733 rsort( $timestamps );
734 $this->mTargetTimestamp
= $timestamps;
738 function execute( $par ) {
739 $this->checkPermissions();
740 $user = $this->getUser();
743 $this->outputHeader();
745 $this->loadRequest( $par );
747 $out = $this->getOutput();
749 if ( is_null( $this->mTargetObj
) ) {
750 $out->addWikiMsg( 'undelete-header' );
752 # Not all users can just browse every deleted page from the list
753 if ( $user->isAllowed( 'browsearchive' ) ) {
754 $this->showSearchForm();
759 if ( $this->mAllowed
) {
760 $out->setPageTitle( $this->msg( 'undeletepage' ) );
762 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
765 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
767 if ( $this->mTimestamp
!== '' ) {
768 $this->showRevision( $this->mTimestamp
);
769 } elseif ( $this->mFilename
!== null ) {
770 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
771 // Check if user is allowed to see this file
772 if ( !$file->exists() ) {
773 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
774 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
775 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
776 throw new PermissionsError( 'suppressrevision' );
778 throw new PermissionsError( 'deletedtext' );
780 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
781 $this->showFileConfirmationForm( $this->mFilename
);
783 $this->showFile( $this->mFilename
);
785 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
788 $this->showHistory();
792 function showSearchForm() {
795 $out = $this->getOutput();
796 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
798 Xml
::openElement( 'form', array(
800 'action' => $wgScript ) ) .
801 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
802 Html
::hidden( 'title',
803 $this->getTitle()->getPrefixedDbKey() ) .
804 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
805 'prefix', 'prefix', 20,
806 $this->mSearchPrefix
) . ' ' .
807 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
808 Xml
::closeElement( 'fieldset' ) .
809 Xml
::closeElement( 'form' )
812 # List undeletable articles
813 if( $this->mSearchPrefix
) {
814 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
815 $this->showList( $result );
820 * Generic list of deleted pages
822 * @param $result ResultWrapper
825 private function showList( $result ) {
826 $out = $this->getOutput();
828 if( $result->numRows() == 0 ) {
829 $out->addWikiMsg( 'undelete-no-results' );
833 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
835 $undelete = $this->getTitle();
836 $out->addHTML( "<ul>\n" );
837 foreach ( $result as $row ) {
838 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
839 if ( $title !== null ) {
840 $item = Linker
::linkKnown(
842 htmlspecialchars( $title->getPrefixedText() ),
844 array( 'target' => $title->getPrefixedText() )
847 // The title is no longer valid, show as text
848 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
849 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
851 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
852 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
855 $out->addHTML( "</ul>\n" );
860 private function showRevision( $timestamp ) {
861 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
865 $archive = new PageArchive( $this->mTargetObj
);
866 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) ) ) {
869 $rev = $archive->getRevision( $timestamp );
871 $out = $this->getOutput();
872 $user = $this->getUser();
875 $out->addWikiMsg( 'undeleterevision-missing' );
879 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
880 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
881 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
884 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
885 $out->addHTML( '<br />' );
886 // and we are allowed to see...
891 $previousRev = $archive->getPreviousRevision( $timestamp );
893 $this->showDiff( $previousRev, $rev );
894 if( $this->mDiffOnly
) {
897 $out->addHTML( '<hr />' );
900 $out->addWikiMsg( 'undelete-nodiff' );
904 $link = Linker
::linkKnown(
905 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
906 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
909 $lang = $this->getLanguage();
911 // date and time are separate parameters to facilitate localisation.
912 // $time is kept for backward compat reasons.
913 $time = $lang->userTimeAndDate( $timestamp, $user );
914 $d = $lang->userDate( $timestamp, $user );
915 $t = $lang->userTime( $timestamp, $user );
916 $userLink = Linker
::revUserTools( $rev );
918 $content = $rev->getContent( Revision
::FOR_THIS_USER
, $user );
920 $isText = ( $content instanceof TextContent
);
922 if( $this->mPreview ||
$isText ) {
923 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
925 $openDiv = '<div id="mw-undelete-revision">';
927 $out->addHTML( $openDiv );
929 // Revision delete links
930 if ( !$this->mDiff
) {
931 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
933 $out->addHTML( "$revdel " );
937 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
938 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
940 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) ) ) {
944 if( $this->mPreview ||
!$isText ) {
945 // NOTE: non-text content has no source view, so always use rendered preview
948 $popts = $out->parserOptions();
949 $popts->setEditSection( false );
951 $pout = $content->getParserOutput( $this->mTargetObj
, $rev->getId(), $popts, true );
952 $out->addParserOutput( $pout );
956 // source view for textual content
957 $sourceView = Xml
::element( 'textarea', array(
958 'readonly' => 'readonly',
959 'cols' => intval( $user->getOption( 'cols' ) ),
960 'rows' => intval( $user->getOption( 'rows' ) ) ),
961 $content->getNativeData() . "\n" );
963 $previewButton = Xml
::element( 'input', array(
966 'value' => $this->msg( 'showpreview' )->text() ) );
972 $diffButton = Xml
::element( 'input', array(
975 'value' => $this->msg( 'showdiff' )->text() ) );
979 Xml
::openElement( 'div', array(
980 'style' => 'clear: both' ) ) .
981 Xml
::openElement( 'form', array(
983 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
984 Xml
::element( 'input', array(
987 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
988 Xml
::element( 'input', array(
990 'name' => 'timestamp',
991 'value' => $timestamp ) ) .
992 Xml
::element( 'input', array(
994 'name' => 'wpEditToken',
995 'value' => $user->getEditToken() ) ) .
998 Xml
::closeElement( 'form' ) .
999 Xml
::closeElement( 'div' ) );
1003 * Build a diff display between this and the previous either deleted
1004 * or non-deleted edit.
1006 * @param $previousRev Revision
1007 * @param $currentRev Revision
1008 * @return String: HTML
1010 function showDiff( $previousRev, $currentRev ) {
1011 $diffContext = clone $this->getContext();
1012 $diffContext->setTitle( $currentRev->getTitle() );
1013 $diffContext->setWikiPage( WikiPage
::factory( $currentRev->getTitle() ) );
1015 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1016 $diffEngine->showDiffStyle();
1017 $this->getOutput()->addHTML(
1019 "<table style='width: 98%;' cellpadding='0' cellspacing='4' class='diff'>" .
1020 "<col class='diff-marker' />" .
1021 "<col class='diff-content' />" .
1022 "<col class='diff-marker' />" .
1023 "<col class='diff-content' />" .
1025 "<td colspan='2' style='width: 50%; text-align: center' class='diff-otitle'>" .
1026 $this->diffHeader( $previousRev, 'o' ) .
1028 "<td colspan='2' style='width: 50%; text-align: center' class='diff-ntitle'>" .
1029 $this->diffHeader( $currentRev, 'n' ) .
1032 $diffEngine->generateContentDiffBody(
1033 $previousRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ),
1034 $currentRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
1041 * @param $rev Revision
1045 private function diffHeader( $rev, $prefix ) {
1046 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1048 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1049 $targetPage = $this->getTitle();
1050 $targetQuery = array(
1051 'target' => $this->mTargetObj
->getPrefixedText(),
1052 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
1055 /// @todo FIXME: getId() may return non-zero for deleted revs...
1056 $targetPage = $rev->getTitle();
1057 $targetQuery = array( 'oldid' => $rev->getId() );
1059 // Add show/hide deletion links if available
1060 $user = $this->getUser();
1061 $lang = $this->getLanguage();
1062 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1063 if ( $rdel ) $rdel = " $rdel";
1065 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1070 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1071 $lang->userDate( $rev->getTimestamp(), $user ),
1072 $lang->userTime( $rev->getTimestamp(), $user )
1078 '<div id="mw-diff-'.$prefix.'title2">' .
1079 Linker
::revUserTools( $rev ) . '<br />' .
1081 '<div id="mw-diff-'.$prefix.'title3">' .
1082 Linker
::revComment( $rev ) . $rdel . '<br />' .
1087 * Show a form confirming whether a tokenless user really wants to see a file
1089 private function showFileConfirmationForm( $key ) {
1090 $out = $this->getOutput();
1091 $lang = $this->getLanguage();
1092 $user = $this->getUser();
1093 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1094 $out->addWikiMsg( 'undelete-show-file-confirm',
1095 $this->mTargetObj
->getText(),
1096 $lang->userDate( $file->getTimestamp(), $user ),
1097 $lang->userTime( $file->getTimestamp(), $user ) );
1099 Xml
::openElement( 'form', array(
1101 'action' => $this->getTitle()->getLocalURL(
1102 'target=' . urlencode( $this->mTarget
) .
1103 '&file=' . urlencode( $key ) .
1104 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1107 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1113 * Show a deleted file version requested by the visitor.
1115 private function showFile( $key ) {
1116 $this->getOutput()->disable();
1118 # We mustn't allow the output to be Squid cached, otherwise
1119 # if an admin previews a deleted image, and it's cached, then
1120 # a user without appropriate permissions can toddle off and
1121 # nab the image, and Squid will serve it
1122 $response = $this->getRequest()->response();
1123 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1124 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1125 $response->header( 'Pragma: no-cache' );
1127 $repo = RepoGroup
::singleton()->getLocalRepo();
1128 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1129 $repo->streamFile( $path );
1132 private function showHistory() {
1133 $out = $this->getOutput();
1134 if( $this->mAllowed
) {
1135 $out->addModules( 'mediawiki.special.undelete' );
1138 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1139 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1142 $archive = new PageArchive( $this->mTargetObj
);
1143 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1145 $text = $archive->getLastRevisionText();
1146 if( is_null( $text ) ) {
1147 $out->addWikiMsg( 'nohistory' );
1151 $out->addHTML( '<div class="mw-undelete-history">' );
1152 if ( $this->mAllowed
) {
1153 $out->addWikiMsg( 'undeletehistory' );
1154 $out->addWikiMsg( 'undeleterevdel' );
1156 $out->addWikiMsg( 'undeletehistorynoadmin' );
1158 $out->addHTML( '</div>' );
1160 # List all stored revisions
1161 $revisions = $archive->listRevisions();
1162 $files = $archive->listFiles();
1164 $haveRevisions = $revisions && $revisions->numRows() > 0;
1165 $haveFiles = $files && $files->numRows() > 0;
1167 # Batch existence check on user and talk pages
1168 if( $haveRevisions ) {
1169 $batch = new LinkBatch();
1170 foreach ( $revisions as $row ) {
1171 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1172 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1175 $revisions->seek( 0 );
1178 $batch = new LinkBatch();
1179 foreach ( $files as $row ) {
1180 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1181 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1187 if ( $this->mAllowed
) {
1188 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1189 # Start the form here
1190 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1191 $out->addHTML( $top );
1194 # Show relevant lines from the deletion log:
1195 $deleteLogPage = new LogPage( 'delete' );
1196 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1197 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1198 # Show relevant lines from the suppression log:
1199 $suppressLogPage = new LogPage( 'suppress' );
1200 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1201 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1202 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1205 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1206 # Format the user-visible controls (comment field, submission button)
1207 # in a nice little table
1208 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1212 <td class='mw-input'>" .
1213 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1214 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1218 $unsuppressBox = '';
1221 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1222 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1224 <td colspan='2' class='mw-undelete-extrahelp'>" .
1225 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1229 <td class='mw-label'>" .
1230 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1232 <td class='mw-input'>" .
1233 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1238 <td class='mw-submit'>" .
1239 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1240 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1244 Xml
::closeElement( 'table' ) .
1245 Xml
::closeElement( 'fieldset' );
1247 $out->addHTML( $table );
1250 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1252 if( $haveRevisions ) {
1253 # The page's stored (deleted) history:
1254 $out->addHTML( '<ul>' );
1255 $remaining = $revisions->numRows();
1256 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1258 foreach ( $revisions as $row ) {
1260 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1263 $out->addHTML( '</ul>' );
1265 $out->addWikiMsg( 'nohistory' );
1269 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1270 $out->addHTML( '<ul>' );
1271 foreach ( $files as $row ) {
1272 $out->addHTML( $this->formatFileRow( $row ) );
1275 $out->addHTML( '</ul>' );
1278 if ( $this->mAllowed
) {
1279 # Slip in the hidden controls here
1280 $misc = Html
::hidden( 'target', $this->mTarget
);
1281 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1282 $misc .= Xml
::closeElement( 'form' );
1283 $out->addHTML( $misc );
1289 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1290 $rev = Revision
::newFromArchiveRow( $row,
1292 'title' => $this->mTargetObj
1296 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1297 // Build checkboxen...
1298 if( $this->mAllowed
) {
1299 if( $this->mInvert
) {
1300 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1301 $checkBox = Xml
::check( "ts$ts" );
1303 $checkBox = Xml
::check( "ts$ts", true );
1306 $checkBox = Xml
::check( "ts$ts" );
1311 $user = $this->getUser();
1312 // Build page & diff links...
1313 if( $this->mCanView
) {
1314 $titleObj = $this->getTitle();
1316 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1317 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1318 $last = $this->msg( 'diff' )->escaped();
1319 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1320 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1321 $last = Linker
::linkKnown(
1323 $this->msg( 'diff' )->escaped(),
1326 'target' => $this->mTargetObj
->getPrefixedText(),
1332 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1333 $last = $this->msg( 'diff' )->escaped();
1336 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1337 $last = $this->msg( 'diff' )->escaped();
1340 $userLink = Linker
::revUserTools( $rev );
1341 // Revision text size
1342 $size = $row->ar_len
;
1343 if( !is_null( $size ) ) {
1344 $revTextSize = Linker
::formatRevisionSize( $size );
1347 $comment = Linker
::revComment( $rev );
1348 // Revision delete links
1349 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1351 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1352 return "<li>$revisionRow</li>";
1355 private function formatFileRow( $row ) {
1356 $file = ArchivedFile
::newFromRow( $row );
1358 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1359 $user = $this->getUser();
1360 if( $this->mAllowed
&& $row->fa_storage_key
) {
1361 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1362 $key = urlencode( $row->fa_storage_key
);
1363 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1366 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1368 $userLink = $this->getFileUser( $file );
1369 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1370 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1371 $data = htmlspecialchars( $data . ' ' . $bytes );
1372 $comment = $this->getFileComment( $file );
1374 // Add show/hide deletion links if available
1375 $canHide = $user->isAllowed( 'deleterevision' );
1376 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1377 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1378 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1381 'type' => 'filearchive',
1382 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1383 'ids' => $row->fa_id
1385 $revdlink = Linker
::revDeleteLink( $query,
1386 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1392 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1396 * Fetch revision text link if it's available to all users
1398 * @param $rev Revision
1399 * @param $titleObj Title
1400 * @param $ts string Timestamp
1403 function getPageLink( $rev, $titleObj, $ts ) {
1404 $user = $this->getUser();
1405 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1407 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1408 return '<span class="history-deleted">' . $time . '</span>';
1410 $link = Linker
::linkKnown(
1412 htmlspecialchars( $time ),
1415 'target' => $this->mTargetObj
->getPrefixedText(),
1419 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1420 $link = '<span class="history-deleted">' . $link . '</span>';
1427 * Fetch image view link if it's available to all users
1430 * @param $titleObj Title
1431 * @param $ts string A timestamp
1432 * @param $key String: a storage key
1434 * @return String: HTML fragment
1436 function getFileLink( $file, $titleObj, $ts, $key ) {
1437 $user = $this->getUser();
1438 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1440 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1441 return '<span class="history-deleted">' . $time . '</span>';
1443 $link = Linker
::linkKnown(
1445 htmlspecialchars( $time ),
1448 'target' => $this->mTargetObj
->getPrefixedText(),
1450 'token' => $user->getEditToken( $key )
1453 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1454 $link = '<span class="history-deleted">' . $link . '</span>';
1461 * Fetch file's user id if it's available to this user
1464 * @return String: HTML fragment
1466 function getFileUser( $file ) {
1467 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1468 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1470 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1471 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1472 if( $file->isDeleted( File
::DELETED_USER
) ) {
1473 $link = '<span class="history-deleted">' . $link . '</span>';
1480 * Fetch file upload comment if it's available to this user
1483 * @return String: HTML fragment
1485 function getFileComment( $file ) {
1486 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1487 return '<span class="history-deleted"><span class="comment">' .
1488 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1490 $link = Linker
::commentBlock( $file->getRawDescription() );
1491 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1492 $link = '<span class="history-deleted">' . $link . '</span>';
1498 function undelete() {
1499 global $wgUploadMaintenance;
1501 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1502 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1505 if ( wfReadOnly() ) {
1506 throw new ReadOnlyError
;
1509 $out = $this->getOutput();
1510 $archive = new PageArchive( $this->mTargetObj
);
1511 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1512 $ok = $archive->undelete(
1513 $this->mTargetTimestamp
,
1515 $this->mFileVersions
,
1520 if( is_array( $ok ) ) {
1521 if ( $ok[1] ) { // Undeleted file count
1522 wfRunHooks( 'FileUndeleteComplete', array(
1523 $this->mTargetObj
, $this->mFileVersions
,
1524 $this->getUser(), $this->mComment
) );
1527 $link = Linker
::linkKnown( $this->mTargetObj
);
1528 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1530 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1533 // Show revision undeletion warnings and errors
1534 $status = $archive->getRevisionStatus();
1535 if( $status && !$status->isGood() ) {
1536 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'cannotundelete', 'cannotundelete' ) . '</div>' );
1539 // Show file undeletion warnings and errors
1540 $status = $archive->getFileStatus();
1541 if( $status && !$status->isGood() ) {
1542 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );