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
);
161 ArchivedFile
::selectFields(),
162 array( 'fa_name' => $this->title
->getDBkey() ),
164 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
165 $ret = $dbr->resultObject( $res );
172 * Return a Revision object containing data for the deleted revision.
173 * Note that the result *may* or *may not* have a null page ID.
175 * @param $timestamp String
178 function getRevision( $timestamp ) {
179 global $wgContentHandlerUseDB;
181 $dbr = wfGetDB( DB_SLAVE
);
198 if ( $wgContentHandlerUseDB ) {
199 $fields[] = 'ar_content_format';
200 $fields[] = 'ar_content_model';
203 $row = $dbr->selectRow( 'archive',
205 array( 'ar_namespace' => $this->title
->getNamespace(),
206 'ar_title' => $this->title
->getDBkey(),
207 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
210 return Revision
::newFromArchiveRow( $row, array( 'title' => $this->title
) );
217 * Return the most-previous revision, either live or deleted, against
218 * the deleted revision given by timestamp.
220 * May produce unexpected results in case of history merges or other
221 * unusual time issues.
223 * @param $timestamp String
224 * @return Revision or null
226 function getPreviousRevision( $timestamp ) {
227 $dbr = wfGetDB( DB_SLAVE
);
229 // Check the previous deleted revision...
230 $row = $dbr->selectRow( 'archive',
232 array( 'ar_namespace' => $this->title
->getNamespace(),
233 'ar_title' => $this->title
->getDBkey(),
235 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
238 'ORDER BY' => 'ar_timestamp DESC',
240 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
242 $row = $dbr->selectRow( array( 'page', 'revision' ),
243 array( 'rev_id', 'rev_timestamp' ),
245 'page_namespace' => $this->title
->getNamespace(),
246 'page_title' => $this->title
->getDBkey(),
247 'page_id = rev_page',
249 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
252 'ORDER BY' => 'rev_timestamp DESC',
254 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
255 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
257 if( $prevLive && $prevLive > $prevDeleted ) {
258 // Most prior revision was live
259 return Revision
::newFromId( $prevLiveId );
260 } elseif( $prevDeleted ) {
261 // Most prior revision was deleted
262 return $this->getRevision( $prevDeleted );
264 // No prior revision on this page.
270 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
272 * @param $row Object: database row
275 function getTextFromRow( $row ) {
276 if( is_null( $row->ar_text_id
) ) {
277 // An old row from MediaWiki 1.4 or previous.
278 // Text is embedded in this row in classic compression format.
279 return Revision
::getRevisionText( $row, 'ar_' );
281 // New-style: keyed to the text storage backend.
282 $dbr = wfGetDB( DB_SLAVE
);
283 $text = $dbr->selectRow( 'text',
284 array( 'old_text', 'old_flags' ),
285 array( 'old_id' => $row->ar_text_id
),
287 return Revision
::getRevisionText( $text );
292 * Fetch (and decompress if necessary) the stored text of the most
293 * recently edited deleted revision of the page.
295 * If there are no archived revisions for the page, returns NULL.
299 function getLastRevisionText() {
300 $dbr = wfGetDB( DB_SLAVE
);
301 $row = $dbr->selectRow( 'archive',
302 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
303 array( 'ar_namespace' => $this->title
->getNamespace(),
304 'ar_title' => $this->title
->getDBkey() ),
306 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
308 return $this->getTextFromRow( $row );
315 * Quick check if any archived revisions are present for the page.
319 function isDeleted() {
320 $dbr = wfGetDB( DB_SLAVE
);
321 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
322 array( 'ar_namespace' => $this->title
->getNamespace(),
323 'ar_title' => $this->title
->getDBkey() ),
330 * Restore the given (or all) text and file revisions for the page.
331 * Once restored, the items will be removed from the archive tables.
332 * The deletion log will be updated with an undeletion notice.
334 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
335 * @param $comment String
336 * @param $fileVersions Array
337 * @param $unsuppress Boolean
338 * @param $user User doing the action, or null to use $wgUser
340 * @return array(number of file revisions restored, number of image revisions restored, log message)
341 * on success, false on failure
343 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
344 // If both the set of text revisions and file revisions are empty,
345 // restore everything. Otherwise, just restore the requested items.
346 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
348 $restoreText = $restoreAll ||
!empty( $timestamps );
349 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
351 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
352 $img = wfLocalFile( $this->title
);
353 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
354 if ( !$this->fileStatus
->isOK() ) {
357 $filesRestored = $this->fileStatus
->successCount
;
363 $this->revisionStatus
= $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
364 if( !$this->revisionStatus
->isOK() ) {
368 $textRestored = $this->revisionStatus
->getValue();
375 if( $textRestored && $filesRestored ) {
376 $reason = wfMessage( 'undeletedrevisions-files' )
377 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
378 } elseif( $textRestored ) {
379 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
380 ->inContentLanguage()->text();
381 } elseif( $filesRestored ) {
382 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
383 ->inContentLanguage()->text();
385 wfDebug( "Undelete: nothing undeleted...\n" );
389 if( trim( $comment ) != '' ) {
390 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
393 if ( $user === null ) {
398 $logEntry = new ManualLogEntry( 'delete', 'restore' );
399 $logEntry->setPerformer( $user );
400 $logEntry->setTarget( $this->title
);
401 $logEntry->setComment( $reason );
403 wfRunHooks( 'ArticleUndeleteLogEntry', array( $this, &$logEntry, $user ) );
405 $logid = $logEntry->insert();
406 $logEntry->publish( $logid );
408 return array( $textRestored, $filesRestored, $reason );
412 * This is the meaty bit -- restores archived revisions of the given page
413 * to the cur/old tables. If the page currently exists, all revisions will
414 * be stuffed into old, otherwise the most recent will go into cur.
416 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
417 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
419 * @param $comment String
420 * @throws ReadOnlyError
421 * @return Status, containing the number of revisions restored on success
423 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
424 global $wgContentHandlerUseDB;
426 if ( wfReadOnly() ) {
427 throw new ReadOnlyError();
429 $restoreAll = empty( $timestamps );
431 $dbw = wfGetDB( DB_MASTER
);
433 # Does this page already exist? We'll have to update it...
434 $article = WikiPage
::factory( $this->title
);
435 # Load latest data for the current page (bug 31179)
436 $article->loadPageData( 'fromdbmaster' );
437 $oldcountable = $article->isCountable();
439 $page = $dbw->selectRow( 'page',
440 array( 'page_id', 'page_latest' ),
441 array( 'page_namespace' => $this->title
->getNamespace(),
442 'page_title' => $this->title
->getDBkey() ),
444 array( 'FOR UPDATE' ) // lock page
448 # Page already exists. Import the history, and if necessary
449 # we'll update the latest revision field in the record.
451 $pageId = $page->page_id
;
452 $previousRevId = $page->page_latest
;
453 # Get the time span of this page
454 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
455 array( 'rev_id' => $previousRevId ),
458 if( $previousTimestamp === false ) {
459 wfDebug( __METHOD__
. ": existing page refers to a page_latest that does not exist\n" );
461 $status = Status
::newGood( 0 );
462 $status->warning( 'undeleterevision-missing' );
467 # Have to create a new article...
470 $previousTimestamp = 0;
474 $oldones = '1 = 1'; # All revisions...
476 $oldts = implode( ',',
477 array_map( array( &$dbw, 'addQuotes' ),
478 array_map( array( &$dbw, 'timestamp' ),
481 $oldones = "ar_timestamp IN ( {$oldts} )";
500 if ( $wgContentHandlerUseDB ) {
501 $fields[] = 'ar_content_format';
502 $fields[] = 'ar_content_model';
506 * Select each archived revision...
508 $result = $dbw->select( 'archive',
511 'ar_namespace' => $this->title
->getNamespace(),
512 'ar_title' => $this->title
->getDBkey(),
515 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
517 $ret = $dbw->resultObject( $result );
518 $rev_count = $dbw->numRows( $result );
520 wfDebug( __METHOD__
. ": no revisions to restore\n" );
522 $status = Status
::newGood( 0 );
523 $status->warning( "undelete-no-results" );
527 $ret->seek( $rev_count - 1 ); // move to last
528 $row = $ret->fetchObject(); // get newest archived rev
529 $ret->seek( 0 ); // move back
531 // grab the content to check consistency with global state before restoring the page.
532 $revision = Revision
::newFromArchiveRow( $row,
534 'title' => $article->getTitle(), // used to derive default content model
537 $m = $revision->getContentModel();
539 $user = User
::newFromName( $revision->getRawUserText(), false );
540 $content = $revision->getContent( Revision
::RAW
);
542 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
543 $status = $content->prepareSave( $article, 0, -1, $user );
545 if ( !$status->isOK() ) {
550 // Check the state of the newest to-be version...
551 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
552 return Status
::newFatal( "undeleterevdel" );
554 // Safe to insert now...
555 $newid = $article->insertOn( $dbw );
558 // Check if a deleted revision will become the current revision...
559 if( $row->ar_timestamp
> $previousTimestamp ) {
560 // Check the state of the newest to-be version...
561 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
562 return Status
::newFatal( "undeleterevdel" );
567 $pageId = $article->getId();
573 foreach ( $ret as $row ) {
574 // Check for key dupes due to shitty archive integrity.
575 if( $row->ar_rev_id
) {
576 $exists = $dbw->selectField( 'revision', '1',
577 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
579 continue; // don't throw DB errors
582 // Insert one revision at a time...maintaining deletion status
583 // unless we are specifically removing all restrictions...
584 $revision = Revision
::newFromArchiveRow( $row,
587 'title' => $this->title
,
588 'deleted' => $unsuppress ?
0 : $row->ar_deleted
591 $revision->insertOn( $dbw );
594 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
596 # Now that it's safely stored, take it out of the archive
597 $dbw->delete( 'archive',
599 'ar_namespace' => $this->title
->getNamespace(),
600 'ar_title' => $this->title
->getDBkey(),
604 // Was anything restored at all?
605 if ( $restored == 0 ) {
606 return Status
::newGood( 0 );
609 $created = (bool)$newid;
611 // Attach the latest revision to the page...
612 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
613 if ( $created ||
$wasnew ) {
614 // Update site stats, link tables, etc
615 $user = User
::newFromName( $revision->getRawUserText(), false );
616 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
619 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
621 if( $this->title
->getNamespace() == NS_FILE
) {
622 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
626 return Status
::newGood( $restored );
632 function getFileStatus() { return $this->fileStatus
; }
637 function getRevisionStatus() { return $this->revisionStatus
; }
641 * Special page allowing users with the appropriate permissions to view
642 * and restore deleted content.
644 * @ingroup SpecialPage
646 class SpecialUndelete
extends SpecialPage
{
647 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
648 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
655 function __construct() {
656 parent
::__construct( 'Undelete', 'deletedhistory' );
659 function loadRequest( $par ) {
660 $request = $this->getRequest();
661 $user = $this->getUser();
663 $this->mAction
= $request->getVal( 'action' );
664 if ( $par !== null && $par !== '' ) {
665 $this->mTarget
= $par;
667 $this->mTarget
= $request->getVal( 'target' );
669 $this->mTargetObj
= null;
670 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
671 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
673 $this->mSearchPrefix
= $request->getText( 'prefix' );
674 $time = $request->getVal( 'timestamp' );
675 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
676 $this->mFilename
= $request->getVal( 'file' );
678 $posted = $request->wasPosted() &&
679 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
680 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
681 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
682 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
683 $this->mDiff
= $request->getCheck( 'diff' );
684 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
685 $this->mComment
= $request->getText( 'wpComment' );
686 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
687 $this->mToken
= $request->getVal( 'token' );
689 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
690 $this->mAllowed
= true; // user can restore
691 $this->mCanView
= true; // user can view content
692 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
693 $this->mAllowed
= false; // user cannot restore
694 $this->mCanView
= true; // user can view content
695 $this->mRestore
= false;
696 } else { // user can only view the list of revisions
697 $this->mAllowed
= false;
698 $this->mCanView
= false;
699 $this->mTimestamp
= '';
700 $this->mRestore
= false;
703 if( $this->mRestore ||
$this->mInvert
) {
704 $timestamps = array();
705 $this->mFileVersions
= array();
706 foreach( $request->getValues() as $key => $val ) {
708 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
709 array_push( $timestamps, $matches[1] );
712 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
713 $this->mFileVersions
[] = intval( $matches[1] );
716 rsort( $timestamps );
717 $this->mTargetTimestamp
= $timestamps;
721 function execute( $par ) {
722 $this->checkPermissions();
723 $user = $this->getUser();
726 $this->outputHeader();
728 $this->loadRequest( $par );
730 $out = $this->getOutput();
732 if ( is_null( $this->mTargetObj
) ) {
733 $out->addWikiMsg( 'undelete-header' );
735 # Not all users can just browse every deleted page from the list
736 if ( $user->isAllowed( 'browsearchive' ) ) {
737 $this->showSearchForm();
742 if ( $this->mAllowed
) {
743 $out->setPageTitle( $this->msg( 'undeletepage' ) );
745 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
748 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
750 if ( $this->mTimestamp
!== '' ) {
751 $this->showRevision( $this->mTimestamp
);
752 } elseif ( $this->mFilename
!== null ) {
753 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
754 // Check if user is allowed to see this file
755 if ( !$file->exists() ) {
756 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
757 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
758 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
759 throw new PermissionsError( 'suppressrevision' );
761 throw new PermissionsError( 'deletedtext' );
763 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
764 $this->showFileConfirmationForm( $this->mFilename
);
766 $this->showFile( $this->mFilename
);
768 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
771 $this->showHistory();
775 function showSearchForm() {
778 $out = $this->getOutput();
779 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
781 Xml
::openElement( 'form', array(
783 'action' => $wgScript ) ) .
784 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
785 Html
::hidden( 'title',
786 $this->getTitle()->getPrefixedDbKey() ) .
787 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
788 'prefix', 'prefix', 20,
789 $this->mSearchPrefix
) . ' ' .
790 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
791 Xml
::closeElement( 'fieldset' ) .
792 Xml
::closeElement( 'form' )
795 # List undeletable articles
796 if( $this->mSearchPrefix
) {
797 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
798 $this->showList( $result );
803 * Generic list of deleted pages
805 * @param $result ResultWrapper
808 private function showList( $result ) {
809 $out = $this->getOutput();
811 if( $result->numRows() == 0 ) {
812 $out->addWikiMsg( 'undelete-no-results' );
816 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
818 $undelete = $this->getTitle();
819 $out->addHTML( "<ul>\n" );
820 foreach ( $result as $row ) {
821 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
822 if ( $title !== null ) {
823 $item = Linker
::linkKnown(
825 htmlspecialchars( $title->getPrefixedText() ),
827 array( 'target' => $title->getPrefixedText() )
830 // The title is no longer valid, show as text
831 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
832 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
834 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
835 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
838 $out->addHTML( "</ul>\n" );
843 private function showRevision( $timestamp ) {
844 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
848 $archive = new PageArchive( $this->mTargetObj
);
849 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) ) ) {
852 $rev = $archive->getRevision( $timestamp );
854 $out = $this->getOutput();
855 $user = $this->getUser();
858 $out->addWikiMsg( 'undeleterevision-missing' );
862 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
863 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
864 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
867 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
868 $out->addHTML( '<br />' );
869 // and we are allowed to see...
874 $previousRev = $archive->getPreviousRevision( $timestamp );
876 $this->showDiff( $previousRev, $rev );
877 if( $this->mDiffOnly
) {
880 $out->addHTML( '<hr />' );
883 $out->addWikiMsg( 'undelete-nodiff' );
887 $link = Linker
::linkKnown(
888 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
889 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
892 $lang = $this->getLanguage();
894 // date and time are separate parameters to facilitate localisation.
895 // $time is kept for backward compat reasons.
896 $time = $lang->userTimeAndDate( $timestamp, $user );
897 $d = $lang->userDate( $timestamp, $user );
898 $t = $lang->userTime( $timestamp, $user );
899 $userLink = Linker
::revUserTools( $rev );
901 $content = $rev->getContent( Revision
::FOR_THIS_USER
, $user );
903 $isText = ( $content instanceof TextContent
);
905 if( $this->mPreview ||
$isText ) {
906 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
908 $openDiv = '<div id="mw-undelete-revision">';
910 $out->addHTML( $openDiv );
912 // Revision delete links
913 if ( !$this->mDiff
) {
914 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
916 $out->addHTML( "$revdel " );
920 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
921 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
923 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) ) ) {
927 if( $this->mPreview ||
!$isText ) {
928 // NOTE: non-text content has no source view, so always use rendered preview
931 $popts = $out->parserOptions();
932 $popts->setEditSection( false );
934 $pout = $content->getParserOutput( $this->mTargetObj
, $rev->getId(), $popts, true );
935 $out->addParserOutput( $pout );
939 // source view for textual content
940 $sourceView = Xml
::element( 'textarea', array(
941 'readonly' => 'readonly',
942 'cols' => $user->getIntOption( 'cols' ),
943 'rows' => $user->getIntOption( 'rows' ) ),
944 $content->getNativeData() . "\n" );
946 $previewButton = Xml
::element( 'input', array(
949 'value' => $this->msg( 'showpreview' )->text() ) );
955 $diffButton = Xml
::element( 'input', array(
958 'value' => $this->msg( 'showdiff' )->text() ) );
962 Xml
::openElement( 'div', array(
963 'style' => 'clear: both' ) ) .
964 Xml
::openElement( 'form', array(
966 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
967 Xml
::element( 'input', array(
970 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
971 Xml
::element( 'input', array(
973 'name' => 'timestamp',
974 'value' => $timestamp ) ) .
975 Xml
::element( 'input', array(
977 'name' => 'wpEditToken',
978 'value' => $user->getEditToken() ) ) .
981 Xml
::closeElement( 'form' ) .
982 Xml
::closeElement( 'div' ) );
986 * Build a diff display between this and the previous either deleted
987 * or non-deleted edit.
989 * @param $previousRev Revision
990 * @param $currentRev Revision
991 * @return String: HTML
993 function showDiff( $previousRev, $currentRev ) {
994 $diffContext = clone $this->getContext();
995 $diffContext->setTitle( $currentRev->getTitle() );
996 $diffContext->setWikiPage( WikiPage
::factory( $currentRev->getTitle() ) );
998 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
999 $diffEngine->showDiffStyle();
1000 $this->getOutput()->addHTML(
1002 "<table style='width: 98%;' cellpadding='0' cellspacing='4' class='diff'>" .
1003 "<col class='diff-marker' />" .
1004 "<col class='diff-content' />" .
1005 "<col class='diff-marker' />" .
1006 "<col class='diff-content' />" .
1008 "<td colspan='2' style='width: 50%; text-align: center' class='diff-otitle'>" .
1009 $this->diffHeader( $previousRev, 'o' ) .
1011 "<td colspan='2' style='width: 50%; text-align: center' class='diff-ntitle'>" .
1012 $this->diffHeader( $currentRev, 'n' ) .
1015 $diffEngine->generateContentDiffBody(
1016 $previousRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ),
1017 $currentRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
1024 * @param $rev Revision
1028 private function diffHeader( $rev, $prefix ) {
1029 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1031 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1032 $targetPage = $this->getTitle();
1033 $targetQuery = array(
1034 'target' => $this->mTargetObj
->getPrefixedText(),
1035 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
1038 /// @todo FIXME: getId() may return non-zero for deleted revs...
1039 $targetPage = $rev->getTitle();
1040 $targetQuery = array( 'oldid' => $rev->getId() );
1042 // Add show/hide deletion links if available
1043 $user = $this->getUser();
1044 $lang = $this->getLanguage();
1045 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1046 if ( $rdel ) $rdel = " $rdel";
1048 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1053 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1054 $lang->userDate( $rev->getTimestamp(), $user ),
1055 $lang->userTime( $rev->getTimestamp(), $user )
1061 '<div id="mw-diff-' . $prefix . 'title2">' .
1062 Linker
::revUserTools( $rev ) . '<br />' .
1064 '<div id="mw-diff-' . $prefix . 'title3">' .
1065 Linker
::revComment( $rev ) . $rdel . '<br />' .
1070 * Show a form confirming whether a tokenless user really wants to see a file
1072 private function showFileConfirmationForm( $key ) {
1073 $out = $this->getOutput();
1074 $lang = $this->getLanguage();
1075 $user = $this->getUser();
1076 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1077 $out->addWikiMsg( 'undelete-show-file-confirm',
1078 $this->mTargetObj
->getText(),
1079 $lang->userDate( $file->getTimestamp(), $user ),
1080 $lang->userTime( $file->getTimestamp(), $user ) );
1082 Xml
::openElement( 'form', array(
1084 'action' => $this->getTitle()->getLocalURL(
1085 'target=' . urlencode( $this->mTarget
) .
1086 '&file=' . urlencode( $key ) .
1087 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1090 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1096 * Show a deleted file version requested by the visitor.
1098 private function showFile( $key ) {
1099 $this->getOutput()->disable();
1101 # We mustn't allow the output to be Squid cached, otherwise
1102 # if an admin previews a deleted image, and it's cached, then
1103 # a user without appropriate permissions can toddle off and
1104 # nab the image, and Squid will serve it
1105 $response = $this->getRequest()->response();
1106 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1107 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1108 $response->header( 'Pragma: no-cache' );
1110 $repo = RepoGroup
::singleton()->getLocalRepo();
1111 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1112 $repo->streamFile( $path );
1115 private function showHistory() {
1116 $out = $this->getOutput();
1117 if( $this->mAllowed
) {
1118 $out->addModules( 'mediawiki.special.undelete' );
1121 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1122 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1125 $archive = new PageArchive( $this->mTargetObj
);
1126 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1128 $text = $archive->getLastRevisionText();
1129 if( is_null( $text ) ) {
1130 $out->addWikiMsg( 'nohistory' );
1134 $out->addHTML( '<div class="mw-undelete-history">' );
1135 if ( $this->mAllowed
) {
1136 $out->addWikiMsg( 'undeletehistory' );
1137 $out->addWikiMsg( 'undeleterevdel' );
1139 $out->addWikiMsg( 'undeletehistorynoadmin' );
1141 $out->addHTML( '</div>' );
1143 # List all stored revisions
1144 $revisions = $archive->listRevisions();
1145 $files = $archive->listFiles();
1147 $haveRevisions = $revisions && $revisions->numRows() > 0;
1148 $haveFiles = $files && $files->numRows() > 0;
1150 # Batch existence check on user and talk pages
1151 if( $haveRevisions ) {
1152 $batch = new LinkBatch();
1153 foreach ( $revisions as $row ) {
1154 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1155 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1158 $revisions->seek( 0 );
1161 $batch = new LinkBatch();
1162 foreach ( $files as $row ) {
1163 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1164 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1170 if ( $this->mAllowed
) {
1171 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1172 # Start the form here
1173 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1174 $out->addHTML( $top );
1177 # Show relevant lines from the deletion log:
1178 $deleteLogPage = new LogPage( 'delete' );
1179 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1180 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1181 # Show relevant lines from the suppression log:
1182 $suppressLogPage = new LogPage( 'suppress' );
1183 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1184 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1185 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1188 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1189 # Format the user-visible controls (comment field, submission button)
1190 # in a nice little table
1191 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1195 <td class='mw-input'>" .
1196 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1197 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1201 $unsuppressBox = '';
1204 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1205 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1207 <td colspan='2' class='mw-undelete-extrahelp'>" .
1208 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1212 <td class='mw-label'>" .
1213 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1215 <td class='mw-input'>" .
1216 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1221 <td class='mw-submit'>" .
1222 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1223 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1227 Xml
::closeElement( 'table' ) .
1228 Xml
::closeElement( 'fieldset' );
1230 $out->addHTML( $table );
1233 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1235 if( $haveRevisions ) {
1236 # The page's stored (deleted) history:
1237 $out->addHTML( '<ul>' );
1238 $remaining = $revisions->numRows();
1239 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1241 foreach ( $revisions as $row ) {
1243 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1246 $out->addHTML( '</ul>' );
1248 $out->addWikiMsg( 'nohistory' );
1252 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1253 $out->addHTML( '<ul>' );
1254 foreach ( $files as $row ) {
1255 $out->addHTML( $this->formatFileRow( $row ) );
1258 $out->addHTML( '</ul>' );
1261 if ( $this->mAllowed
) {
1262 # Slip in the hidden controls here
1263 $misc = Html
::hidden( 'target', $this->mTarget
);
1264 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1265 $misc .= Xml
::closeElement( 'form' );
1266 $out->addHTML( $misc );
1272 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1273 $rev = Revision
::newFromArchiveRow( $row,
1275 'title' => $this->mTargetObj
1279 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1280 // Build checkboxen...
1281 if( $this->mAllowed
) {
1282 if( $this->mInvert
) {
1283 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1284 $checkBox = Xml
::check( "ts$ts" );
1286 $checkBox = Xml
::check( "ts$ts", true );
1289 $checkBox = Xml
::check( "ts$ts" );
1294 $user = $this->getUser();
1295 // Build page & diff links...
1296 if( $this->mCanView
) {
1297 $titleObj = $this->getTitle();
1299 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1300 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1301 $last = $this->msg( 'diff' )->escaped();
1302 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1303 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1304 $last = Linker
::linkKnown(
1306 $this->msg( 'diff' )->escaped(),
1309 'target' => $this->mTargetObj
->getPrefixedText(),
1315 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1316 $last = $this->msg( 'diff' )->escaped();
1319 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1320 $last = $this->msg( 'diff' )->escaped();
1323 $userLink = Linker
::revUserTools( $rev );
1324 // Revision text size
1325 $size = $row->ar_len
;
1326 if( !is_null( $size ) ) {
1327 $revTextSize = Linker
::formatRevisionSize( $size );
1330 $comment = Linker
::revComment( $rev );
1331 // Revision delete links
1332 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1334 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink, $userLink, $revTextSize, $comment )->escaped();
1335 return "<li>$revisionRow</li>";
1338 private function formatFileRow( $row ) {
1339 $file = ArchivedFile
::newFromRow( $row );
1341 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1342 $user = $this->getUser();
1343 if( $this->mAllowed
&& $row->fa_storage_key
) {
1344 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1345 $key = urlencode( $row->fa_storage_key
);
1346 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1349 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1351 $userLink = $this->getFileUser( $file );
1352 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1353 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1354 $data = htmlspecialchars( $data . ' ' . $bytes );
1355 $comment = $this->getFileComment( $file );
1357 // Add show/hide deletion links if available
1358 $canHide = $user->isAllowed( 'deleterevision' );
1359 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1360 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1361 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1364 'type' => 'filearchive',
1365 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1366 'ids' => $row->fa_id
1368 $revdlink = Linker
::revDeleteLink( $query,
1369 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1375 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1379 * Fetch revision text link if it's available to all users
1381 * @param $rev Revision
1382 * @param $titleObj Title
1383 * @param $ts string Timestamp
1386 function getPageLink( $rev, $titleObj, $ts ) {
1387 $user = $this->getUser();
1388 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1390 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1391 return '<span class="history-deleted">' . $time . '</span>';
1393 $link = Linker
::linkKnown(
1395 htmlspecialchars( $time ),
1398 'target' => $this->mTargetObj
->getPrefixedText(),
1402 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1403 $link = '<span class="history-deleted">' . $link . '</span>';
1410 * Fetch image view link if it's available to all users
1413 * @param $titleObj Title
1414 * @param $ts string A timestamp
1415 * @param $key String: a storage key
1417 * @return String: HTML fragment
1419 function getFileLink( $file, $titleObj, $ts, $key ) {
1420 $user = $this->getUser();
1421 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1423 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1424 return '<span class="history-deleted">' . $time . '</span>';
1426 $link = Linker
::linkKnown(
1428 htmlspecialchars( $time ),
1431 'target' => $this->mTargetObj
->getPrefixedText(),
1433 'token' => $user->getEditToken( $key )
1436 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1437 $link = '<span class="history-deleted">' . $link . '</span>';
1444 * Fetch file's user id if it's available to this user
1447 * @return String: HTML fragment
1449 function getFileUser( $file ) {
1450 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1451 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1453 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1454 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1455 if( $file->isDeleted( File
::DELETED_USER
) ) {
1456 $link = '<span class="history-deleted">' . $link . '</span>';
1463 * Fetch file upload comment if it's available to this user
1466 * @return String: HTML fragment
1468 function getFileComment( $file ) {
1469 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1470 return '<span class="history-deleted"><span class="comment">' .
1471 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1473 $link = Linker
::commentBlock( $file->getRawDescription() );
1474 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1475 $link = '<span class="history-deleted">' . $link . '</span>';
1481 function undelete() {
1482 global $wgUploadMaintenance;
1484 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1485 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1488 if ( wfReadOnly() ) {
1489 throw new ReadOnlyError
;
1492 $out = $this->getOutput();
1493 $archive = new PageArchive( $this->mTargetObj
);
1494 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1495 $ok = $archive->undelete(
1496 $this->mTargetTimestamp
,
1498 $this->mFileVersions
,
1503 if( is_array( $ok ) ) {
1504 if ( $ok[1] ) { // Undeleted file count
1505 wfRunHooks( 'FileUndeleteComplete', array(
1506 $this->mTargetObj
, $this->mFileVersions
,
1507 $this->getUser(), $this->mComment
) );
1510 $link = Linker
::linkKnown( $this->mTargetObj
);
1511 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1513 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1516 // Show revision undeletion warnings and errors
1517 $status = $archive->getRevisionStatus();
1518 if( $status && !$status->isGood() ) {
1519 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'cannotundelete', 'cannotundelete' ) . '</div>' );
1522 // Show file undeletion warnings and errors
1523 $status = $archive->getFileStatus();
1524 if( $status && !$status->isGood() ) {
1525 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );