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
37 function __construct( $title ) {
38 if( is_null( $title ) ) {
39 throw new MWException( __METHOD__
. ' given a null title.' );
41 $this->title
= $title;
45 * List all deleted pages recorded in the archive table. Returns result
46 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
49 * @return ResultWrapper
51 public static function listAllPages() {
52 $dbr = wfGetDB( DB_SLAVE
);
53 return self
::listPages( $dbr, '' );
57 * List deleted pages recorded in the archive table matching the
59 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
61 * @param $prefix String: title prefix
62 * @return ResultWrapper
64 public static function listPagesByPrefix( $prefix ) {
65 $dbr = wfGetDB( DB_SLAVE
);
67 $title = Title
::newFromText( $prefix );
69 $ns = $title->getNamespace();
70 $prefix = $title->getDBkey();
72 // Prolly won't work too good
73 // @todo handle bare namespace names cleanly?
77 'ar_namespace' => $ns,
78 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
80 return self
::listPages( $dbr, $conds );
84 * @param $dbr DatabaseBase
86 * @return bool|ResultWrapper
88 protected static function listPages( $dbr, $condition ) {
89 return $dbr->resultObject(
100 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
101 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
109 * List the revisions of the given page. Returns result wrapper with
110 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
112 * @return ResultWrapper
114 function listRevisions() {
115 global $wgContentHandlerNoDB;
117 $dbr = wfGetDB( DB_SLAVE
);
120 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
121 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
124 if ( !$wgContentHandlerNoDB ) {
125 $fields[] = 'ar_content_format';
126 $fields[] = 'ar_content_model';
129 $res = $dbr->select( 'archive',
131 array( 'ar_namespace' => $this->title
->getNamespace(),
132 'ar_title' => $this->title
->getDBkey() ),
133 'PageArchive::listRevisions',
134 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
135 $ret = $dbr->resultObject( $res );
140 * List the deleted file revisions for this page, if it's a file page.
141 * Returns a result wrapper with various filearchive fields, or null
142 * if not a file page.
144 * @return ResultWrapper
145 * @todo Does this belong in Image for fuller encapsulation?
147 function listFiles() {
148 if( $this->title
->getNamespace() == NS_FILE
) {
149 $dbr = wfGetDB( DB_SLAVE
);
150 $res = $dbr->select( 'filearchive',
170 array( 'fa_name' => $this->title
->getDBkey() ),
172 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
173 $ret = $dbr->resultObject( $res );
180 * Return a Revision object containing data for the deleted revision.
181 * Note that the result *may* or *may not* have a null page ID.
183 * @param $timestamp String
186 function getRevision( $timestamp ) {
187 global $wgContentHandlerNoDB;
189 $dbr = wfGetDB( DB_SLAVE
);
206 if ( !$wgContentHandlerNoDB ) {
207 $fields[] = 'ar_content_format';
208 $fields[] = 'ar_content_model';
211 $row = $dbr->selectRow( 'archive',
213 array( 'ar_namespace' => $this->title
->getNamespace(),
214 'ar_title' => $this->title
->getDBkey(),
215 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
218 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleID() ) );
225 * Return the most-previous revision, either live or deleted, against
226 * the deleted revision given by timestamp.
228 * May produce unexpected results in case of history merges or other
229 * unusual time issues.
231 * @param $timestamp String
232 * @return Revision or null
234 function getPreviousRevision( $timestamp ) {
235 $dbr = wfGetDB( DB_SLAVE
);
237 // Check the previous deleted revision...
238 $row = $dbr->selectRow( 'archive',
240 array( 'ar_namespace' => $this->title
->getNamespace(),
241 'ar_title' => $this->title
->getDBkey(),
243 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
246 'ORDER BY' => 'ar_timestamp DESC',
248 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
250 $row = $dbr->selectRow( array( 'page', 'revision' ),
251 array( 'rev_id', 'rev_timestamp' ),
253 'page_namespace' => $this->title
->getNamespace(),
254 'page_title' => $this->title
->getDBkey(),
255 'page_id = rev_page',
257 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
260 'ORDER BY' => 'rev_timestamp DESC',
262 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
263 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
265 if( $prevLive && $prevLive > $prevDeleted ) {
266 // Most prior revision was live
267 return Revision
::newFromId( $prevLiveId );
268 } elseif( $prevDeleted ) {
269 // Most prior revision was deleted
270 return $this->getRevision( $prevDeleted );
272 // No prior revision on this page.
278 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
280 * @param $row Object: database row
283 function getTextFromRow( $row ) {
284 if( is_null( $row->ar_text_id
) ) {
285 // An old row from MediaWiki 1.4 or previous.
286 // Text is embedded in this row in classic compression format.
287 return Revision
::getRevisionText( $row, 'ar_' );
289 // New-style: keyed to the text storage backend.
290 $dbr = wfGetDB( DB_SLAVE
);
291 $text = $dbr->selectRow( 'text',
292 array( 'old_text', 'old_flags' ),
293 array( 'old_id' => $row->ar_text_id
),
295 return Revision
::getRevisionText( $text );
300 * Fetch (and decompress if necessary) the stored text of the most
301 * recently edited deleted revision of the page.
303 * If there are no archived revisions for the page, returns NULL.
307 function getLastRevisionText() {
308 $dbr = wfGetDB( DB_SLAVE
);
309 $row = $dbr->selectRow( 'archive',
310 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
311 array( 'ar_namespace' => $this->title
->getNamespace(),
312 'ar_title' => $this->title
->getDBkey() ),
314 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
316 return $this->getTextFromRow( $row );
323 * Quick check if any archived revisions are present for the page.
327 function isDeleted() {
328 $dbr = wfGetDB( DB_SLAVE
);
329 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
330 array( 'ar_namespace' => $this->title
->getNamespace(),
331 'ar_title' => $this->title
->getDBkey() ) );
336 * Restore the given (or all) text and file revisions for the page.
337 * Once restored, the items will be removed from the archive tables.
338 * The deletion log will be updated with an undeletion notice.
340 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
341 * @param $comment String
342 * @param $fileVersions Array
343 * @param $unsuppress Boolean
345 * @return array(number of file revisions restored, number of image revisions restored, log message)
346 * on success, false on failure
348 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
349 // If both the set of text revisions and file revisions are empty,
350 // restore everything. Otherwise, just restore the requested items.
351 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
353 $restoreText = $restoreAll ||
!empty( $timestamps );
354 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
356 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
357 $img = wfLocalFile( $this->title
);
358 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
359 if ( !$this->fileStatus
->isOk() ) {
362 $filesRestored = $this->fileStatus
->successCount
;
368 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
369 if( $textRestored === false ) { // It must be one of UNDELETE_*
378 $log = new LogPage( 'delete' );
380 if( $textRestored && $filesRestored ) {
381 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
382 $wgContLang->formatNum( $textRestored ),
383 $wgContLang->formatNum( $filesRestored ) );
384 } elseif( $textRestored ) {
385 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
386 $wgContLang->formatNum( $textRestored ) );
387 } elseif( $filesRestored ) {
388 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
389 $wgContLang->formatNum( $filesRestored ) );
391 wfDebug( "Undelete: nothing undeleted...\n" );
395 if( trim( $comment ) != '' ) {
396 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
398 $log->addEntry( 'restore', $this->title
, $reason );
400 return array( $textRestored, $filesRestored, $reason );
404 * This is the meaty bit -- restores archived revisions of the given page
405 * to the cur/old tables. If the page currently exists, all revisions will
406 * be stuffed into old, otherwise the most recent will go into cur.
408 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
409 * @param $comment String
410 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
412 * @return Mixed: number of revisions restored or false on failure
414 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
415 global $wgContentHandlerNoDB;
417 if ( wfReadOnly() ) {
420 $restoreAll = empty( $timestamps );
422 $dbw = wfGetDB( DB_MASTER
);
424 # Does this page already exist? We'll have to update it...
425 $article = WikiPage
::factory( $this->title
);
426 # Load latest data for the current page (bug 31179)
427 $article->loadPageData( 'fromdbmaster' );
428 $oldcountable = $article->isCountable();
430 $page = $dbw->selectRow( 'page',
431 array( 'page_id', 'page_latest' ),
432 array( 'page_namespace' => $this->title
->getNamespace(),
433 'page_title' => $this->title
->getDBkey() ),
435 array( 'FOR UPDATE' ) // lock page
439 # Page already exists. Import the history, and if necessary
440 # we'll update the latest revision field in the record.
442 $pageId = $page->page_id
;
443 $previousRevId = $page->page_latest
;
444 # Get the time span of this page
445 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
446 array( 'rev_id' => $previousRevId ),
448 if( $previousTimestamp === false ) {
449 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
453 # Have to create a new article...
456 $previousTimestamp = 0;
460 $oldones = '1 = 1'; # All revisions...
462 $oldts = implode( ',',
463 array_map( array( &$dbw, 'addQuotes' ),
464 array_map( array( &$dbw, 'timestamp' ),
467 $oldones = "ar_timestamp IN ( {$oldts} )";
485 if ( !$wgContentHandlerNoDB ) {
486 $fields[] = 'ar_content_format';
487 $fields[] = 'ar_content_model';
491 * Select each archived revision...
493 $result = $dbw->select( 'archive',
496 'ar_namespace' => $this->title
->getNamespace(),
497 'ar_title' => $this->title
->getDBkey(),
500 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
502 $ret = $dbw->resultObject( $result );
503 $rev_count = $dbw->numRows( $result );
505 wfDebug( __METHOD__
. ": no revisions to restore\n" );
509 $ret->seek( $rev_count - 1 ); // move to last
510 $row = $ret->fetchObject(); // get newest archived rev
511 $ret->seek( 0 ); // move back
514 // Check the state of the newest to-be version...
515 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
516 return false; // we can't leave the current revision like this!
518 // Safe to insert now...
519 $newid = $article->insertOn( $dbw );
522 // Check if a deleted revision will become the current revision...
523 if( $row->ar_timestamp
> $previousTimestamp ) {
524 // Check the state of the newest to-be version...
525 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
526 return false; // we can't leave the current revision like this!
534 foreach ( $ret as $row ) {
535 // Check for key dupes due to shitty archive integrity.
536 if( $row->ar_rev_id
) {
537 $exists = $dbw->selectField( 'revision', '1',
538 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
540 continue; // don't throw DB errors
543 // Insert one revision at a time...maintaining deletion status
544 // unless we are specifically removing all restrictions...
545 $revision = Revision
::newFromArchiveRow( $row,
548 'deleted' => $unsuppress ?
0 : $row->ar_deleted
551 $revision->insertOn( $dbw );
554 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
556 # Now that it's safely stored, take it out of the archive
557 $dbw->delete( 'archive',
559 'ar_namespace' => $this->title
->getNamespace(),
560 'ar_title' => $this->title
->getDBkey(),
564 // Was anything restored at all?
565 if ( $restored == 0 ) {
569 $created = (bool)$newid;
571 // Attach the latest revision to the page...
572 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
573 if ( $created ||
$wasnew ) {
574 // Update site stats, link tables, etc
575 $user = User
::newFromName( $revision->getRawUserText(), false );
576 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
579 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
581 if( $this->title
->getNamespace() == NS_FILE
) {
582 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
592 function getFileStatus() { return $this->fileStatus
; }
596 * Special page allowing users with the appropriate permissions to view
597 * and restore deleted content.
599 * @ingroup SpecialPage
601 class SpecialUndelete
extends SpecialPage
{
602 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
603 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
610 function __construct() {
611 parent
::__construct( 'Undelete', 'deletedhistory' );
614 function loadRequest( $par ) {
615 $request = $this->getRequest();
616 $user = $this->getUser();
618 $this->mAction
= $request->getVal( 'action' );
619 if ( $par !== null && $par !== '' ) {
620 $this->mTarget
= $par;
622 $this->mTarget
= $request->getVal( 'target' );
624 $this->mTargetObj
= null;
625 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
626 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
628 $this->mSearchPrefix
= $request->getText( 'prefix' );
629 $time = $request->getVal( 'timestamp' );
630 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
631 $this->mFilename
= $request->getVal( 'file' );
633 $posted = $request->wasPosted() &&
634 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
635 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
636 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
637 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
638 $this->mDiff
= $request->getCheck( 'diff' );
639 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
640 $this->mComment
= $request->getText( 'wpComment' );
641 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
642 $this->mToken
= $request->getVal( 'token' );
644 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
645 $this->mAllowed
= true; // user can restore
646 $this->mCanView
= true; // user can view content
647 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
648 $this->mAllowed
= false; // user cannot restore
649 $this->mCanView
= true; // user can view content
650 $this->mRestore
= false;
651 } else { // user can only view the list of revisions
652 $this->mAllowed
= false;
653 $this->mCanView
= false;
654 $this->mTimestamp
= '';
655 $this->mRestore
= false;
658 if( $this->mRestore ||
$this->mInvert
) {
659 $timestamps = array();
660 $this->mFileVersions
= array();
661 foreach( $request->getValues() as $key => $val ) {
663 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
664 array_push( $timestamps, $matches[1] );
667 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
668 $this->mFileVersions
[] = intval( $matches[1] );
671 rsort( $timestamps );
672 $this->mTargetTimestamp
= $timestamps;
676 function execute( $par ) {
677 $this->checkPermissions();
678 $user = $this->getUser();
681 $this->outputHeader();
683 $this->loadRequest( $par );
685 $out = $this->getOutput();
687 if ( is_null( $this->mTargetObj
) ) {
688 $out->addWikiMsg( 'undelete-header' );
690 # Not all users can just browse every deleted page from the list
691 if ( $user->isAllowed( 'browsearchive' ) ) {
692 $this->showSearchForm();
697 if ( $this->mAllowed
) {
698 $out->setPageTitle( $this->msg( 'undeletepage' ) );
700 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
703 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
705 if ( $this->mTimestamp
!== '' ) {
706 $this->showRevision( $this->mTimestamp
);
707 } elseif ( $this->mFilename
!== null ) {
708 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
709 // Check if user is allowed to see this file
710 if ( !$file->exists() ) {
711 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
712 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
713 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
714 throw new PermissionsError( 'suppressrevision' );
716 throw new PermissionsError( 'deletedtext' );
718 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
719 $this->showFileConfirmationForm( $this->mFilename
);
721 $this->showFile( $this->mFilename
);
723 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
726 $this->showHistory();
730 function showSearchForm() {
733 $out = $this->getOutput();
734 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
736 Xml
::openElement( 'form', array(
738 'action' => $wgScript ) ) .
739 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
740 Html
::hidden( 'title',
741 $this->getTitle()->getPrefixedDbKey() ) .
742 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
743 'prefix', 'prefix', 20,
744 $this->mSearchPrefix
) . ' ' .
745 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
746 Xml
::closeElement( 'fieldset' ) .
747 Xml
::closeElement( 'form' )
750 # List undeletable articles
751 if( $this->mSearchPrefix
) {
752 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
753 $this->showList( $result );
758 * Generic list of deleted pages
760 * @param $result ResultWrapper
763 private function showList( $result ) {
764 $out = $this->getOutput();
766 if( $result->numRows() == 0 ) {
767 $out->addWikiMsg( 'undelete-no-results' );
771 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
773 $undelete = $this->getTitle();
774 $out->addHTML( "<ul>\n" );
775 foreach ( $result as $row ) {
776 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
777 if ( $title !== null ) {
778 $item = Linker
::linkKnown(
780 htmlspecialchars( $title->getPrefixedText() ),
782 array( 'target' => $title->getPrefixedText() )
785 // The title is no longer valid, show as text
786 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
787 $item = htmlspecialchars( $title->getPrefixedText() );
789 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
790 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
793 $out->addHTML( "</ul>\n" );
798 private function showRevision( $timestamp ) {
799 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
803 $archive = new PageArchive( $this->mTargetObj
);
804 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
805 $rev = $archive->getRevision( $timestamp );
807 $out = $this->getOutput();
808 $user = $this->getUser();
811 $out->addWikiMsg( 'undeleterevision-missing' );
815 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
816 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
817 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
820 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
821 $out->addHTML( '<br />' );
822 // and we are allowed to see...
827 $previousRev = $archive->getPreviousRevision( $timestamp );
829 $this->showDiff( $previousRev, $rev );
830 if( $this->mDiffOnly
) {
833 $out->addHTML( '<hr />' );
836 $out->addWikiMsg( 'undelete-nodiff' );
840 $link = Linker
::linkKnown(
841 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
842 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
845 $lang = $this->getLanguage();
847 // date and time are separate parameters to facilitate localisation.
848 // $time is kept for backward compat reasons.
849 $time = $lang->userTimeAndDate( $timestamp, $user );
850 $d = $lang->userDate( $timestamp, $user );
851 $t = $lang->userTime( $timestamp, $user );
852 $userLink = Linker
::revUserTools( $rev );
854 if( $this->mPreview
) {
855 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
857 $openDiv = '<div id="mw-undelete-revision">';
859 $out->addHTML( $openDiv );
861 // Revision delete links
862 if ( !$this->mDiff
) {
863 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
865 $out->addHTML( "$revdel " );
869 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
870 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
871 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
873 if( $this->mPreview
) {
875 //FIXME: ContentHandler will have to provide some specialized magic to do this
876 $popts = $out->parserOptions();
877 $popts->setEditSection( false );
878 $out->parserOptions( $popts );
879 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
883 Xml
::element( 'textarea', array(
884 'readonly' => 'readonly',
885 'cols' => intval( $user->getOption( 'cols' ) ),
886 'rows' => intval( $user->getOption( 'rows' ) ) ),
887 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) . //FIXME: ContentHandler will have to provide some specialized magic to do this
888 Xml
::openElement( 'div' ) .
889 Xml
::openElement( 'form', array(
891 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
892 Xml
::element( 'input', array(
895 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
896 Xml
::element( 'input', array(
898 'name' => 'timestamp',
899 'value' => $timestamp ) ) .
900 Xml
::element( 'input', array(
902 'name' => 'wpEditToken',
903 'value' => $user->getEditToken() ) ) .
904 Xml
::element( 'input', array(
907 'value' => $this->msg( 'showpreview' )->text() ) ) .
908 Xml
::element( 'input', array(
911 'value' => $this->msg( 'showdiff' )->text() ) ) .
912 Xml
::closeElement( 'form' ) .
913 Xml
::closeElement( 'div' ) );
917 * Build a diff display between this and the previous either deleted
918 * or non-deleted edit.
920 * @param $previousRev Revision
921 * @param $currentRev Revision
922 * @return String: HTML
924 function showDiff( $previousRev, $currentRev ) {
925 $contentHandler = ContentHandler
::getForTitle( $this->getTitle() );
926 $diffEngine = $contentHandler->createDifferenceEngine( $this->getContext() );
927 $diffEngine->showDiffStyle();
928 $this->getOutput()->addHTML(
930 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
931 "<col class='diff-marker' />" .
932 "<col class='diff-content' />" .
933 "<col class='diff-marker' />" .
934 "<col class='diff-content' />" .
936 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
937 $this->diffHeader( $previousRev, 'o' ) .
939 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
940 $this->diffHeader( $currentRev, 'n' ) .
943 $diffEngine->generateContentDiffBody(
944 $previousRev->getContent(), $currentRev->getContent() ) .
951 * @param $rev Revision
955 private function diffHeader( $rev, $prefix ) {
956 $isDeleted = !( $rev->getId() && $rev->getTitle() );
958 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
959 $targetPage = $this->getTitle();
960 $targetQuery = array(
961 'target' => $this->mTargetObj
->getPrefixedText(),
962 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
965 /// @todo FIXME: getId() may return non-zero for deleted revs...
966 $targetPage = $rev->getTitle();
967 $targetQuery = array( 'oldid' => $rev->getId() );
969 // Add show/hide deletion links if available
970 $user = $this->getUser();
971 $lang = $this->getLanguage();
972 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
973 if ( $rdel ) $rdel = " $rdel";
975 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
980 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
981 $lang->userDate( $rev->getTimestamp(), $user ),
982 $lang->userTime( $rev->getTimestamp(), $user )
988 '<div id="mw-diff-'.$prefix.'title2">' .
989 Linker
::revUserTools( $rev ) . '<br />' .
991 '<div id="mw-diff-'.$prefix.'title3">' .
992 Linker
::revComment( $rev ) . $rdel . '<br />' .
997 * Show a form confirming whether a tokenless user really wants to see a file
999 private function showFileConfirmationForm( $key ) {
1000 $out = $this->getOutput();
1001 $lang = $this->getLanguage();
1002 $user = $this->getUser();
1003 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1004 $out->addWikiMsg( 'undelete-show-file-confirm',
1005 $this->mTargetObj
->getText(),
1006 $lang->userDate( $file->getTimestamp(), $user ),
1007 $lang->userTime( $file->getTimestamp(), $user ) );
1009 Xml
::openElement( 'form', array(
1011 'action' => $this->getTitle()->getLocalURL(
1012 'target=' . urlencode( $this->mTarget
) .
1013 '&file=' . urlencode( $key ) .
1014 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1017 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1023 * Show a deleted file version requested by the visitor.
1025 private function showFile( $key ) {
1026 $this->getOutput()->disable();
1028 # We mustn't allow the output to be Squid cached, otherwise
1029 # if an admin previews a deleted image, and it's cached, then
1030 # a user without appropriate permissions can toddle off and
1031 # nab the image, and Squid will serve it
1032 $response = $this->getRequest()->response();
1033 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1034 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1035 $response->header( 'Pragma: no-cache' );
1037 $repo = RepoGroup
::singleton()->getLocalRepo();
1038 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1039 $repo->streamFile( $path );
1042 private function showHistory() {
1043 $out = $this->getOutput();
1044 if( $this->mAllowed
) {
1045 $out->addModules( 'mediawiki.special.undelete' );
1048 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1049 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1052 $archive = new PageArchive( $this->mTargetObj
);
1053 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1055 $text = $archive->getLastRevisionText();
1056 if( is_null( $text ) ) {
1057 $out->addWikiMsg( 'nohistory' );
1061 $out->addHTML( '<div class="mw-undelete-history">' );
1062 if ( $this->mAllowed
) {
1063 $out->addWikiMsg( 'undeletehistory' );
1064 $out->addWikiMsg( 'undeleterevdel' );
1066 $out->addWikiMsg( 'undeletehistorynoadmin' );
1068 $out->addHTML( '</div>' );
1070 # List all stored revisions
1071 $revisions = $archive->listRevisions();
1072 $files = $archive->listFiles();
1074 $haveRevisions = $revisions && $revisions->numRows() > 0;
1075 $haveFiles = $files && $files->numRows() > 0;
1077 # Batch existence check on user and talk pages
1078 if( $haveRevisions ) {
1079 $batch = new LinkBatch();
1080 foreach ( $revisions as $row ) {
1081 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1082 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1085 $revisions->seek( 0 );
1088 $batch = new LinkBatch();
1089 foreach ( $files as $row ) {
1090 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1091 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1097 if ( $this->mAllowed
) {
1098 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1099 # Start the form here
1100 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1101 $out->addHTML( $top );
1104 # Show relevant lines from the deletion log:
1105 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1106 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1107 # Show relevant lines from the suppression log:
1108 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1109 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1110 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1113 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1114 # Format the user-visible controls (comment field, submission button)
1115 # in a nice little table
1116 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1120 <td class='mw-input'>" .
1121 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1122 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1126 $unsuppressBox = '';
1129 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1130 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1132 <td colspan='2' class='mw-undelete-extrahelp'>" .
1133 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1137 <td class='mw-label'>" .
1138 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1140 <td class='mw-input'>" .
1141 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1146 <td class='mw-submit'>" .
1147 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1148 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1152 Xml
::closeElement( 'table' ) .
1153 Xml
::closeElement( 'fieldset' );
1155 $out->addHTML( $table );
1158 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1160 if( $haveRevisions ) {
1161 # The page's stored (deleted) history:
1162 $out->addHTML( '<ul>' );
1163 $remaining = $revisions->numRows();
1164 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1166 foreach ( $revisions as $row ) {
1168 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1171 $out->addHTML( '</ul>' );
1173 $out->addWikiMsg( 'nohistory' );
1177 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1178 $out->addHTML( '<ul>' );
1179 foreach ( $files as $row ) {
1180 $out->addHTML( $this->formatFileRow( $row ) );
1183 $out->addHTML( '</ul>' );
1186 if ( $this->mAllowed
) {
1187 # Slip in the hidden controls here
1188 $misc = Html
::hidden( 'target', $this->mTarget
);
1189 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1190 $misc .= Xml
::closeElement( 'form' );
1191 $out->addHTML( $misc );
1197 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1198 $rev = Revision
::newFromArchiveRow( $row,
1199 array( 'page' => $this->mTargetObj
->getArticleID() ) );
1201 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1202 // Build checkboxen...
1203 if( $this->mAllowed
) {
1204 if( $this->mInvert
) {
1205 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1206 $checkBox = Xml
::check( "ts$ts" );
1208 $checkBox = Xml
::check( "ts$ts", true );
1211 $checkBox = Xml
::check( "ts$ts" );
1216 $user = $this->getUser();
1217 // Build page & diff links...
1218 if( $this->mCanView
) {
1219 $titleObj = $this->getTitle();
1221 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1222 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1223 $last = $this->msg( 'diff' )->escaped();
1224 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1225 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1226 $last = Linker
::linkKnown(
1228 $this->msg( 'diff' )->escaped(),
1231 'target' => $this->mTargetObj
->getPrefixedText(),
1237 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1238 $last = $this->msg( 'diff' )->escaped();
1241 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1242 $last = $this->msg( 'diff' )->escaped();
1245 $userLink = Linker
::revUserTools( $rev );
1246 // Revision text size
1247 $size = $row->ar_len
;
1248 if( !is_null( $size ) ) {
1249 $revTextSize = Linker
::formatRevisionSize( $size );
1252 $comment = Linker
::revComment( $rev );
1253 // Revision delete links
1254 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1256 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1257 return "<li>$revisionRow</li>";
1260 private function formatFileRow( $row ) {
1261 $file = ArchivedFile
::newFromRow( $row );
1263 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1264 $user = $this->getUser();
1265 if( $this->mAllowed
&& $row->fa_storage_key
) {
1266 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1267 $key = urlencode( $row->fa_storage_key
);
1268 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1271 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1273 $userLink = $this->getFileUser( $file );
1274 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1275 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1276 $data = htmlspecialchars( $data . ' ' . $bytes );
1277 $comment = $this->getFileComment( $file );
1279 // Add show/hide deletion links if available
1280 $canHide = $user->isAllowed( 'deleterevision' );
1281 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1282 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1283 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1286 'type' => 'filearchive',
1287 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1288 'ids' => $row->fa_id
1290 $revdlink = Linker
::revDeleteLink( $query,
1291 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1297 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1301 * Fetch revision text link if it's available to all users
1303 * @param $rev Revision
1304 * @param $titleObj Title
1305 * @param $ts string Timestamp
1308 function getPageLink( $rev, $titleObj, $ts ) {
1309 $user = $this->getUser();
1310 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1312 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1313 return '<span class="history-deleted">' . $time . '</span>';
1315 $link = Linker
::linkKnown(
1317 htmlspecialchars( $time ),
1320 'target' => $this->mTargetObj
->getPrefixedText(),
1324 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1325 $link = '<span class="history-deleted">' . $link . '</span>';
1332 * Fetch image view link if it's available to all users
1335 * @param $titleObj Title
1336 * @param $ts string A timestamp
1337 * @param $key String: a storage key
1339 * @return String: HTML fragment
1341 function getFileLink( $file, $titleObj, $ts, $key ) {
1342 $user = $this->getUser();
1343 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1345 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1346 return '<span class="history-deleted">' . $time . '</span>';
1348 $link = Linker
::linkKnown(
1350 htmlspecialchars( $time ),
1353 'target' => $this->mTargetObj
->getPrefixedText(),
1355 'token' => $user->getEditToken( $key )
1358 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1359 $link = '<span class="history-deleted">' . $link . '</span>';
1366 * Fetch file's user id if it's available to this user
1369 * @return String: HTML fragment
1371 function getFileUser( $file ) {
1372 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1373 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1375 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1376 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1377 if( $file->isDeleted( File
::DELETED_USER
) ) {
1378 $link = '<span class="history-deleted">' . $link . '</span>';
1385 * Fetch file upload comment if it's available to this user
1388 * @return String: HTML fragment
1390 function getFileComment( $file ) {
1391 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1392 return '<span class="history-deleted"><span class="comment">' .
1393 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1395 $link = Linker
::commentBlock( $file->getRawDescription() );
1396 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1397 $link = '<span class="history-deleted">' . $link . '</span>';
1403 function undelete() {
1404 global $wgUploadMaintenance;
1406 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1407 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1410 if ( wfReadOnly() ) {
1411 throw new ReadOnlyError
;
1414 $out = $this->getOutput();
1415 $archive = new PageArchive( $this->mTargetObj
);
1416 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1417 $ok = $archive->undelete(
1418 $this->mTargetTimestamp
,
1420 $this->mFileVersions
,
1421 $this->mUnsuppress
);
1423 if( is_array( $ok ) ) {
1424 if ( $ok[1] ) { // Undeleted file count
1425 wfRunHooks( 'FileUndeleteComplete', array(
1426 $this->mTargetObj
, $this->mFileVersions
,
1427 $this->getUser(), $this->mComment
) );
1430 $link = Linker
::linkKnown( $this->mTargetObj
);
1431 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1433 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1434 $out->addWikiMsg( 'cannotundelete' );
1435 $out->addWikiMsg( 'undeleterevdel' );
1438 // Show file deletion warnings and errors
1439 $status = $archive->getFileStatus();
1440 if( $status && !$status->isGood() ) {
1441 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );