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 $wgContentHandlerNoDB;
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 ( !$wgContentHandlerNoDB ) {
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',
179 array( 'fa_name' => $this->title
->getDBkey() ),
181 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
182 $ret = $dbr->resultObject( $res );
189 * Return a Revision object containing data for the deleted revision.
190 * Note that the result *may* or *may not* have a null page ID.
192 * @param $timestamp String
195 function getRevision( $timestamp ) {
196 global $wgContentHandlerNoDB;
198 $dbr = wfGetDB( DB_SLAVE
);
215 if ( !$wgContentHandlerNoDB ) {
216 $fields[] = 'ar_content_format';
217 $fields[] = 'ar_content_model';
220 $row = $dbr->selectRow( 'archive',
222 array( 'ar_namespace' => $this->title
->getNamespace(),
223 'ar_title' => $this->title
->getDBkey(),
224 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
227 return Revision
::newFromArchiveRow( $row, array( 'title' => $this->title
) );
234 * Return the most-previous revision, either live or deleted, against
235 * the deleted revision given by timestamp.
237 * May produce unexpected results in case of history merges or other
238 * unusual time issues.
240 * @param $timestamp String
241 * @return Revision or null
243 function getPreviousRevision( $timestamp ) {
244 $dbr = wfGetDB( DB_SLAVE
);
246 // Check the previous deleted revision...
247 $row = $dbr->selectRow( 'archive',
249 array( 'ar_namespace' => $this->title
->getNamespace(),
250 'ar_title' => $this->title
->getDBkey(),
252 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
255 'ORDER BY' => 'ar_timestamp DESC',
257 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
259 $row = $dbr->selectRow( array( 'page', 'revision' ),
260 array( 'rev_id', 'rev_timestamp' ),
262 'page_namespace' => $this->title
->getNamespace(),
263 'page_title' => $this->title
->getDBkey(),
264 'page_id = rev_page',
266 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
269 'ORDER BY' => 'rev_timestamp DESC',
271 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
272 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
274 if( $prevLive && $prevLive > $prevDeleted ) {
275 // Most prior revision was live
276 return Revision
::newFromId( $prevLiveId );
277 } elseif( $prevDeleted ) {
278 // Most prior revision was deleted
279 return $this->getRevision( $prevDeleted );
281 // No prior revision on this page.
287 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
289 * @param $row Object: database row
292 function getTextFromRow( $row ) {
293 if( is_null( $row->ar_text_id
) ) {
294 // An old row from MediaWiki 1.4 or previous.
295 // Text is embedded in this row in classic compression format.
296 return Revision
::getRevisionText( $row, 'ar_' );
298 // New-style: keyed to the text storage backend.
299 $dbr = wfGetDB( DB_SLAVE
);
300 $text = $dbr->selectRow( 'text',
301 array( 'old_text', 'old_flags' ),
302 array( 'old_id' => $row->ar_text_id
),
304 return Revision
::getRevisionText( $text );
309 * Fetch (and decompress if necessary) the stored text of the most
310 * recently edited deleted revision of the page.
312 * If there are no archived revisions for the page, returns NULL.
316 function getLastRevisionText() {
317 $dbr = wfGetDB( DB_SLAVE
);
318 $row = $dbr->selectRow( 'archive',
319 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
320 array( 'ar_namespace' => $this->title
->getNamespace(),
321 'ar_title' => $this->title
->getDBkey() ),
323 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
325 return $this->getTextFromRow( $row );
332 * Quick check if any archived revisions are present for the page.
336 function isDeleted() {
337 $dbr = wfGetDB( DB_SLAVE
);
338 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
339 array( 'ar_namespace' => $this->title
->getNamespace(),
340 'ar_title' => $this->title
->getDBkey() ),
347 * Restore the given (or all) text and file revisions for the page.
348 * Once restored, the items will be removed from the archive tables.
349 * The deletion log will be updated with an undeletion notice.
351 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
352 * @param $comment String
353 * @param $fileVersions Array
354 * @param $unsuppress Boolean
355 * @param $user User doing the action, or null to use $wgUser
357 * @return array(number of file revisions restored, number of image revisions restored, log message)
358 * on success, false on failure
360 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
361 // If both the set of text revisions and file revisions are empty,
362 // restore everything. Otherwise, just restore the requested items.
363 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
365 $restoreText = $restoreAll ||
!empty( $timestamps );
366 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
368 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
369 $img = wfLocalFile( $this->title
);
370 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
371 if ( !$this->fileStatus
->isOK() ) {
374 $filesRestored = $this->fileStatus
->successCount
;
380 $this->revisionStatus
= $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
381 if( !$this->revisionStatus
->isOK() ) {
385 $textRestored = $this->revisionStatus
->getValue();
392 if( $textRestored && $filesRestored ) {
393 $reason = wfMessage( 'undeletedrevisions-files' )
394 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
395 } elseif( $textRestored ) {
396 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
397 ->inContentLanguage()->text();
398 } elseif( $filesRestored ) {
399 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
400 ->inContentLanguage()->text();
402 wfDebug( "Undelete: nothing undeleted...\n" );
406 if( trim( $comment ) != '' ) {
407 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
410 if ( $user === null ) {
415 $logEntry = new ManualLogEntry( 'delete', 'restore' );
416 $logEntry->setPerformer( $user );
417 $logEntry->setTarget( $this->title
);
418 $logEntry->setComment( $reason );
419 $logid = $logEntry->insert();
420 $logEntry->publish( $logid );
422 return array( $textRestored, $filesRestored, $reason );
426 * This is the meaty bit -- restores archived revisions of the given page
427 * to the cur/old tables. If the page currently exists, all revisions will
428 * be stuffed into old, otherwise the most recent will go into cur.
430 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
431 * @param $comment String
432 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
434 * @return Status, containing the number of revisions restored on success
436 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
437 global $wgContentHandlerNoDB;
439 if ( wfReadOnly() ) {
440 throw new ReadOnlyError();
442 $restoreAll = empty( $timestamps );
444 $dbw = wfGetDB( DB_MASTER
);
446 # Does this page already exist? We'll have to update it...
447 $article = WikiPage
::factory( $this->title
);
448 # Load latest data for the current page (bug 31179)
449 $article->loadPageData( 'fromdbmaster' );
450 $oldcountable = $article->isCountable();
452 $page = $dbw->selectRow( 'page',
453 array( 'page_id', 'page_latest' ),
454 array( 'page_namespace' => $this->title
->getNamespace(),
455 'page_title' => $this->title
->getDBkey() ),
457 array( 'FOR UPDATE' ) // lock page
461 # Page already exists. Import the history, and if necessary
462 # we'll update the latest revision field in the record.
464 $pageId = $page->page_id
;
465 $previousRevId = $page->page_latest
;
466 # Get the time span of this page
467 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
468 array( 'rev_id' => $previousRevId ),
471 if( $previousTimestamp === false ) {
472 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
474 $status = Status
::newGood( 0 );
475 $status->warning( 'undeleterevision-missing' );
480 # Have to create a new article...
483 $previousTimestamp = 0;
487 $oldones = '1 = 1'; # All revisions...
489 $oldts = implode( ',',
490 array_map( array( &$dbw, 'addQuotes' ),
491 array_map( array( &$dbw, 'timestamp' ),
494 $oldones = "ar_timestamp IN ( {$oldts} )";
512 if ( !$wgContentHandlerNoDB ) {
513 $fields[] = 'ar_content_format';
514 $fields[] = 'ar_content_model';
518 * Select each archived revision...
520 $result = $dbw->select( 'archive',
523 'ar_namespace' => $this->title
->getNamespace(),
524 'ar_title' => $this->title
->getDBkey(),
527 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
529 $ret = $dbw->resultObject( $result );
530 $rev_count = $dbw->numRows( $result );
532 wfDebug( __METHOD__
. ": no revisions to restore\n" );
534 $status = Status
::newGood( 0 );
535 $status->warning( "undelete-no-results" );
539 $ret->seek( $rev_count - 1 ); // move to last
540 $row = $ret->fetchObject(); // get newest archived rev
541 $ret->seek( 0 ); // move back
543 // grab the content to check consistency with global state before restoring the page.
544 $revision = Revision
::newFromArchiveRow( $row,
546 'title' => $article->getTitle(), // used to derive default content model
549 $m = $revision->getContentModel();
551 $user = User
::newFromName( $revision->getRawUserText(), false );
552 $content = $revision->getContent( Revision
::RAW
);
554 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
555 $status = $content->prepareSave( $article, 0, -1, $user );
557 if ( !$status->isOK() ) {
562 // Check the state of the newest to-be version...
563 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
564 return Status
::newFatal( "undeleterevdel" );
566 // Safe to insert now...
567 $newid = $article->insertOn( $dbw );
570 // Check if a deleted revision will become the current revision...
571 if( $row->ar_timestamp
> $previousTimestamp ) {
572 // Check the state of the newest to-be version...
573 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
574 return Status
::newFatal( "undeleterevdel" );
582 foreach ( $ret as $row ) {
583 // Check for key dupes due to shitty archive integrity.
584 if( $row->ar_rev_id
) {
585 $exists = $dbw->selectField( 'revision', '1',
586 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
588 continue; // don't throw DB errors
591 // Insert one revision at a time...maintaining deletion status
592 // unless we are specifically removing all restrictions...
593 $revision = Revision
::newFromArchiveRow( $row,
595 'title' => $this->title
,
596 'deleted' => $unsuppress ?
0 : $row->ar_deleted
599 $revision->insertOn( $dbw );
602 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
604 # Now that it's safely stored, take it out of the archive
605 $dbw->delete( 'archive',
607 'ar_namespace' => $this->title
->getNamespace(),
608 'ar_title' => $this->title
->getDBkey(),
612 // Was anything restored at all?
613 if ( $restored == 0 ) {
614 return Status
::newGood( 0 );
617 $created = (bool)$newid;
619 // Attach the latest revision to the page...
620 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
621 if ( $created ||
$wasnew ) {
622 // Update site stats, link tables, etc
623 $user = User
::newFromName( $revision->getRawUserText(), false );
624 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
627 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
629 if( $this->title
->getNamespace() == NS_FILE
) {
630 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
634 return Status
::newGood( $restored );
640 function getFileStatus() { return $this->fileStatus
; }
645 function getRevisionStatus() { return $this->revisionStatus
; }
649 * Special page allowing users with the appropriate permissions to view
650 * and restore deleted content.
652 * @ingroup SpecialPage
654 class SpecialUndelete
extends SpecialPage
{
655 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
656 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
663 function __construct() {
664 parent
::__construct( 'Undelete', 'deletedhistory' );
667 function loadRequest( $par ) {
668 $request = $this->getRequest();
669 $user = $this->getUser();
671 $this->mAction
= $request->getVal( 'action' );
672 if ( $par !== null && $par !== '' ) {
673 $this->mTarget
= $par;
675 $this->mTarget
= $request->getVal( 'target' );
677 $this->mTargetObj
= null;
678 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
679 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
681 $this->mSearchPrefix
= $request->getText( 'prefix' );
682 $time = $request->getVal( 'timestamp' );
683 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
684 $this->mFilename
= $request->getVal( 'file' );
686 $posted = $request->wasPosted() &&
687 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
688 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
689 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
690 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
691 $this->mDiff
= $request->getCheck( 'diff' );
692 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
693 $this->mComment
= $request->getText( 'wpComment' );
694 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
695 $this->mToken
= $request->getVal( 'token' );
697 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
698 $this->mAllowed
= true; // user can restore
699 $this->mCanView
= true; // user can view content
700 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
701 $this->mAllowed
= false; // user cannot restore
702 $this->mCanView
= true; // user can view content
703 $this->mRestore
= false;
704 } else { // user can only view the list of revisions
705 $this->mAllowed
= false;
706 $this->mCanView
= false;
707 $this->mTimestamp
= '';
708 $this->mRestore
= false;
711 if( $this->mRestore ||
$this->mInvert
) {
712 $timestamps = array();
713 $this->mFileVersions
= array();
714 foreach( $request->getValues() as $key => $val ) {
716 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
717 array_push( $timestamps, $matches[1] );
720 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
721 $this->mFileVersions
[] = intval( $matches[1] );
724 rsort( $timestamps );
725 $this->mTargetTimestamp
= $timestamps;
729 function execute( $par ) {
730 $this->checkPermissions();
731 $user = $this->getUser();
734 $this->outputHeader();
736 $this->loadRequest( $par );
738 $out = $this->getOutput();
740 if ( is_null( $this->mTargetObj
) ) {
741 $out->addWikiMsg( 'undelete-header' );
743 # Not all users can just browse every deleted page from the list
744 if ( $user->isAllowed( 'browsearchive' ) ) {
745 $this->showSearchForm();
750 if ( $this->mAllowed
) {
751 $out->setPageTitle( $this->msg( 'undeletepage' ) );
753 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
756 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
758 if ( $this->mTimestamp
!== '' ) {
759 $this->showRevision( $this->mTimestamp
);
760 } elseif ( $this->mFilename
!== null ) {
761 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
762 // Check if user is allowed to see this file
763 if ( !$file->exists() ) {
764 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
765 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
766 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
767 throw new PermissionsError( 'suppressrevision' );
769 throw new PermissionsError( 'deletedtext' );
771 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
772 $this->showFileConfirmationForm( $this->mFilename
);
774 $this->showFile( $this->mFilename
);
776 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
779 $this->showHistory();
783 function showSearchForm() {
786 $out = $this->getOutput();
787 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
789 Xml
::openElement( 'form', array(
791 'action' => $wgScript ) ) .
792 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
793 Html
::hidden( 'title',
794 $this->getTitle()->getPrefixedDbKey() ) .
795 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
796 'prefix', 'prefix', 20,
797 $this->mSearchPrefix
) . ' ' .
798 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
799 Xml
::closeElement( 'fieldset' ) .
800 Xml
::closeElement( 'form' )
803 # List undeletable articles
804 if( $this->mSearchPrefix
) {
805 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
806 $this->showList( $result );
811 * Generic list of deleted pages
813 * @param $result ResultWrapper
816 private function showList( $result ) {
817 $out = $this->getOutput();
819 if( $result->numRows() == 0 ) {
820 $out->addWikiMsg( 'undelete-no-results' );
824 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
826 $undelete = $this->getTitle();
827 $out->addHTML( "<ul>\n" );
828 foreach ( $result as $row ) {
829 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
830 if ( $title !== null ) {
831 $item = Linker
::linkKnown(
833 htmlspecialchars( $title->getPrefixedText() ),
835 array( 'target' => $title->getPrefixedText() )
838 // The title is no longer valid, show as text
839 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
840 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
842 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
843 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
846 $out->addHTML( "</ul>\n" );
851 private function showRevision( $timestamp ) {
852 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
856 $archive = new PageArchive( $this->mTargetObj
);
857 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) ) ) {
860 $rev = $archive->getRevision( $timestamp );
862 $out = $this->getOutput();
863 $user = $this->getUser();
866 $out->addWikiMsg( 'undeleterevision-missing' );
870 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
871 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
872 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
875 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
876 $out->addHTML( '<br />' );
877 // and we are allowed to see...
882 $previousRev = $archive->getPreviousRevision( $timestamp );
884 $this->showDiff( $previousRev, $rev );
885 if( $this->mDiffOnly
) {
888 $out->addHTML( '<hr />' );
891 $out->addWikiMsg( 'undelete-nodiff' );
895 $link = Linker
::linkKnown(
896 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
897 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
900 $lang = $this->getLanguage();
902 // date and time are separate parameters to facilitate localisation.
903 // $time is kept for backward compat reasons.
904 $time = $lang->userTimeAndDate( $timestamp, $user );
905 $d = $lang->userDate( $timestamp, $user );
906 $t = $lang->userTime( $timestamp, $user );
907 $userLink = Linker
::revUserTools( $rev );
909 $content = $rev->getContent( Revision
::FOR_THIS_USER
, $user );
911 $isText = ( $content instanceof TextContent
);
913 if( $this->mPreview ||
$isText ) {
914 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
916 $openDiv = '<div id="mw-undelete-revision">';
918 $out->addHTML( $openDiv );
920 // Revision delete links
921 if ( !$this->mDiff
) {
922 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
924 $out->addHTML( "$revdel " );
928 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
929 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
931 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) ) ) {
935 if( $this->mPreview ||
!$isText ) {
936 // NOTE: non-text content has no source view, so always use rendered preview
939 $popts = $out->parserOptions();
940 $popts->setEditSection( false );
942 $pout = $content->getParserOutput( $this->mTargetObj
, $rev->getId(), $popts, true );
943 $out->addParserOutput( $pout );
947 // source view for textual content
948 $sourceView = Xml
::element( 'textarea', array(
949 'readonly' => 'readonly',
950 'cols' => intval( $user->getOption( 'cols' ) ),
951 'rows' => intval( $user->getOption( 'rows' ) ) ),
952 $content->getNativeData() . "\n" );
954 $previewButton = Xml
::element( 'input', array(
957 'value' => $this->msg( 'showpreview' )->text() ) );
963 $diffButton = Xml
::element( 'input', array(
966 'value' => $this->msg( 'showdiff' )->text() ) );
970 Xml
::openElement( 'div', array(
971 'style' => 'clear: both' ) ) .
972 Xml
::openElement( 'form', array(
974 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
975 Xml
::element( 'input', array(
978 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
979 Xml
::element( 'input', array(
981 'name' => 'timestamp',
982 'value' => $timestamp ) ) .
983 Xml
::element( 'input', array(
985 'name' => 'wpEditToken',
986 'value' => $user->getEditToken() ) ) .
989 Xml
::closeElement( 'form' ) .
990 Xml
::closeElement( 'div' ) );
994 * Build a diff display between this and the previous either deleted
995 * or non-deleted edit.
997 * @param $previousRev Revision
998 * @param $currentRev Revision
999 * @return String: HTML
1001 function showDiff( $previousRev, $currentRev ) {
1002 $diffContext = clone $this->getContext();
1003 $diffContext->setTitle( $currentRev->getTitle() );
1004 $diffContext->setWikiPage( WikiPage
::factory( $currentRev->getTitle() ) );
1006 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1007 $diffEngine->showDiffStyle();
1008 $this->getOutput()->addHTML(
1010 "<table style='width: 98%;' cellpadding='0' cellspacing='4' class='diff'>" .
1011 "<col class='diff-marker' />" .
1012 "<col class='diff-content' />" .
1013 "<col class='diff-marker' />" .
1014 "<col class='diff-content' />" .
1016 "<td colspan='2' style='width: 50%; text-align: center' class='diff-otitle'>" .
1017 $this->diffHeader( $previousRev, 'o' ) .
1019 "<td colspan='2' style='width: 50%; text-align: center' class='diff-ntitle'>" .
1020 $this->diffHeader( $currentRev, 'n' ) .
1023 $diffEngine->generateContentDiffBody(
1024 $previousRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ),
1025 $currentRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
1032 * @param $rev Revision
1036 private function diffHeader( $rev, $prefix ) {
1037 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1039 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1040 $targetPage = $this->getTitle();
1041 $targetQuery = array(
1042 'target' => $this->mTargetObj
->getPrefixedText(),
1043 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
1046 /// @todo FIXME: getId() may return non-zero for deleted revs...
1047 $targetPage = $rev->getTitle();
1048 $targetQuery = array( 'oldid' => $rev->getId() );
1050 // Add show/hide deletion links if available
1051 $user = $this->getUser();
1052 $lang = $this->getLanguage();
1053 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1054 if ( $rdel ) $rdel = " $rdel";
1056 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1061 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1062 $lang->userDate( $rev->getTimestamp(), $user ),
1063 $lang->userTime( $rev->getTimestamp(), $user )
1069 '<div id="mw-diff-'.$prefix.'title2">' .
1070 Linker
::revUserTools( $rev ) . '<br />' .
1072 '<div id="mw-diff-'.$prefix.'title3">' .
1073 Linker
::revComment( $rev ) . $rdel . '<br />' .
1078 * Show a form confirming whether a tokenless user really wants to see a file
1080 private function showFileConfirmationForm( $key ) {
1081 $out = $this->getOutput();
1082 $lang = $this->getLanguage();
1083 $user = $this->getUser();
1084 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1085 $out->addWikiMsg( 'undelete-show-file-confirm',
1086 $this->mTargetObj
->getText(),
1087 $lang->userDate( $file->getTimestamp(), $user ),
1088 $lang->userTime( $file->getTimestamp(), $user ) );
1090 Xml
::openElement( 'form', array(
1092 'action' => $this->getTitle()->getLocalURL(
1093 'target=' . urlencode( $this->mTarget
) .
1094 '&file=' . urlencode( $key ) .
1095 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1098 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1104 * Show a deleted file version requested by the visitor.
1106 private function showFile( $key ) {
1107 $this->getOutput()->disable();
1109 # We mustn't allow the output to be Squid cached, otherwise
1110 # if an admin previews a deleted image, and it's cached, then
1111 # a user without appropriate permissions can toddle off and
1112 # nab the image, and Squid will serve it
1113 $response = $this->getRequest()->response();
1114 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1115 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1116 $response->header( 'Pragma: no-cache' );
1118 $repo = RepoGroup
::singleton()->getLocalRepo();
1119 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1120 $repo->streamFile( $path );
1123 private function showHistory() {
1124 $out = $this->getOutput();
1125 if( $this->mAllowed
) {
1126 $out->addModules( 'mediawiki.special.undelete' );
1129 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1130 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1133 $archive = new PageArchive( $this->mTargetObj
);
1134 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1136 $text = $archive->getLastRevisionText();
1137 if( is_null( $text ) ) {
1138 $out->addWikiMsg( 'nohistory' );
1142 $out->addHTML( '<div class="mw-undelete-history">' );
1143 if ( $this->mAllowed
) {
1144 $out->addWikiMsg( 'undeletehistory' );
1145 $out->addWikiMsg( 'undeleterevdel' );
1147 $out->addWikiMsg( 'undeletehistorynoadmin' );
1149 $out->addHTML( '</div>' );
1151 # List all stored revisions
1152 $revisions = $archive->listRevisions();
1153 $files = $archive->listFiles();
1155 $haveRevisions = $revisions && $revisions->numRows() > 0;
1156 $haveFiles = $files && $files->numRows() > 0;
1158 # Batch existence check on user and talk pages
1159 if( $haveRevisions ) {
1160 $batch = new LinkBatch();
1161 foreach ( $revisions as $row ) {
1162 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1163 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1166 $revisions->seek( 0 );
1169 $batch = new LinkBatch();
1170 foreach ( $files as $row ) {
1171 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1172 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1178 if ( $this->mAllowed
) {
1179 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1180 # Start the form here
1181 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1182 $out->addHTML( $top );
1185 # Show relevant lines from the deletion log:
1186 $deleteLogPage = new LogPage( 'delete' );
1187 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1188 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1189 # Show relevant lines from the suppression log:
1190 $suppressLogPage = new LogPage( 'suppress' );
1191 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1192 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1193 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1196 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1197 # Format the user-visible controls (comment field, submission button)
1198 # in a nice little table
1199 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1203 <td class='mw-input'>" .
1204 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1205 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1209 $unsuppressBox = '';
1212 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1213 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1215 <td colspan='2' class='mw-undelete-extrahelp'>" .
1216 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1220 <td class='mw-label'>" .
1221 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1223 <td class='mw-input'>" .
1224 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1229 <td class='mw-submit'>" .
1230 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1231 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1235 Xml
::closeElement( 'table' ) .
1236 Xml
::closeElement( 'fieldset' );
1238 $out->addHTML( $table );
1241 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1243 if( $haveRevisions ) {
1244 # The page's stored (deleted) history:
1245 $out->addHTML( '<ul>' );
1246 $remaining = $revisions->numRows();
1247 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1249 foreach ( $revisions as $row ) {
1251 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1254 $out->addHTML( '</ul>' );
1256 $out->addWikiMsg( 'nohistory' );
1260 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1261 $out->addHTML( '<ul>' );
1262 foreach ( $files as $row ) {
1263 $out->addHTML( $this->formatFileRow( $row ) );
1266 $out->addHTML( '</ul>' );
1269 if ( $this->mAllowed
) {
1270 # Slip in the hidden controls here
1271 $misc = Html
::hidden( 'target', $this->mTarget
);
1272 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1273 $misc .= Xml
::closeElement( 'form' );
1274 $out->addHTML( $misc );
1280 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1281 $rev = Revision
::newFromArchiveRow( $row,
1283 'title' => $this->mTargetObj
1287 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1288 // Build checkboxen...
1289 if( $this->mAllowed
) {
1290 if( $this->mInvert
) {
1291 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1292 $checkBox = Xml
::check( "ts$ts" );
1294 $checkBox = Xml
::check( "ts$ts", true );
1297 $checkBox = Xml
::check( "ts$ts" );
1302 $user = $this->getUser();
1303 // Build page & diff links...
1304 if( $this->mCanView
) {
1305 $titleObj = $this->getTitle();
1307 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1308 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1309 $last = $this->msg( 'diff' )->escaped();
1310 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1311 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1312 $last = Linker
::linkKnown(
1314 $this->msg( 'diff' )->escaped(),
1317 'target' => $this->mTargetObj
->getPrefixedText(),
1323 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1324 $last = $this->msg( 'diff' )->escaped();
1327 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1328 $last = $this->msg( 'diff' )->escaped();
1331 $userLink = Linker
::revUserTools( $rev );
1332 // Revision text size
1333 $size = $row->ar_len
;
1334 if( !is_null( $size ) ) {
1335 $revTextSize = Linker
::formatRevisionSize( $size );
1338 $comment = Linker
::revComment( $rev );
1339 // Revision delete links
1340 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1342 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1343 return "<li>$revisionRow</li>";
1346 private function formatFileRow( $row ) {
1347 $file = ArchivedFile
::newFromRow( $row );
1349 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1350 $user = $this->getUser();
1351 if( $this->mAllowed
&& $row->fa_storage_key
) {
1352 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1353 $key = urlencode( $row->fa_storage_key
);
1354 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1357 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1359 $userLink = $this->getFileUser( $file );
1360 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1361 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1362 $data = htmlspecialchars( $data . ' ' . $bytes );
1363 $comment = $this->getFileComment( $file );
1365 // Add show/hide deletion links if available
1366 $canHide = $user->isAllowed( 'deleterevision' );
1367 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1368 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1369 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1372 'type' => 'filearchive',
1373 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1374 'ids' => $row->fa_id
1376 $revdlink = Linker
::revDeleteLink( $query,
1377 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1383 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1387 * Fetch revision text link if it's available to all users
1389 * @param $rev Revision
1390 * @param $titleObj Title
1391 * @param $ts string Timestamp
1394 function getPageLink( $rev, $titleObj, $ts ) {
1395 $user = $this->getUser();
1396 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1398 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1399 return '<span class="history-deleted">' . $time . '</span>';
1401 $link = Linker
::linkKnown(
1403 htmlspecialchars( $time ),
1406 'target' => $this->mTargetObj
->getPrefixedText(),
1410 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1411 $link = '<span class="history-deleted">' . $link . '</span>';
1418 * Fetch image view link if it's available to all users
1421 * @param $titleObj Title
1422 * @param $ts string A timestamp
1423 * @param $key String: a storage key
1425 * @return String: HTML fragment
1427 function getFileLink( $file, $titleObj, $ts, $key ) {
1428 $user = $this->getUser();
1429 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1431 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1432 return '<span class="history-deleted">' . $time . '</span>';
1434 $link = Linker
::linkKnown(
1436 htmlspecialchars( $time ),
1439 'target' => $this->mTargetObj
->getPrefixedText(),
1441 'token' => $user->getEditToken( $key )
1444 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1445 $link = '<span class="history-deleted">' . $link . '</span>';
1452 * Fetch file's user id if it's available to this user
1455 * @return String: HTML fragment
1457 function getFileUser( $file ) {
1458 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1459 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1461 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1462 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1463 if( $file->isDeleted( File
::DELETED_USER
) ) {
1464 $link = '<span class="history-deleted">' . $link . '</span>';
1471 * Fetch file upload comment if it's available to this user
1474 * @return String: HTML fragment
1476 function getFileComment( $file ) {
1477 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1478 return '<span class="history-deleted"><span class="comment">' .
1479 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1481 $link = Linker
::commentBlock( $file->getRawDescription() );
1482 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1483 $link = '<span class="history-deleted">' . $link . '</span>';
1489 function undelete() {
1490 global $wgUploadMaintenance;
1492 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1493 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1496 if ( wfReadOnly() ) {
1497 throw new ReadOnlyError
;
1500 $out = $this->getOutput();
1501 $archive = new PageArchive( $this->mTargetObj
);
1502 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1503 $ok = $archive->undelete(
1504 $this->mTargetTimestamp
,
1506 $this->mFileVersions
,
1511 if( is_array( $ok ) ) {
1512 if ( $ok[1] ) { // Undeleted file count
1513 wfRunHooks( 'FileUndeleteComplete', array(
1514 $this->mTargetObj
, $this->mFileVersions
,
1515 $this->getUser(), $this->mComment
) );
1518 $link = Linker
::linkKnown( $this->mTargetObj
);
1519 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1521 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1524 // Show revision undeletion warnings and errors
1525 $status = $archive->getRevisionStatus();
1526 if( $status && !$status->isGood() ) {
1527 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'cannotundelete', 'cannotundelete' ) . '</div>' );
1530 // Show file undeletion warnings and errors
1531 $status = $archive->getFileStatus();
1532 if( $status && !$status->isGood() ) {
1533 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );