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
344 * @param $user User doing the action, or null to use $wgUser
346 * @return array(number of file revisions restored, number of image revisions restored, log message)
347 * on success, false on failure
349 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
350 global $wgContLang, $wgUser;
352 // If both the set of text revisions and file revisions are empty,
353 // restore everything. Otherwise, just restore the requested items.
354 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
356 $restoreText = $restoreAll ||
!empty( $timestamps );
357 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
359 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
360 $img = wfLocalFile( $this->title
);
361 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
362 if ( !$this->fileStatus
->isOk() ) {
365 $filesRestored = $this->fileStatus
->successCount
;
371 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
372 if( $textRestored === false ) { // It must be one of UNDELETE_*
381 if( $textRestored && $filesRestored ) {
382 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
383 $wgContLang->formatNum( $textRestored ),
384 $wgContLang->formatNum( $filesRestored ) );
385 } elseif( $textRestored ) {
386 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
387 $wgContLang->formatNum( $textRestored ) );
388 } elseif( $filesRestored ) {
389 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
390 $wgContLang->formatNum( $filesRestored ) );
392 wfDebug( "Undelete: nothing undeleted...\n" );
396 if( trim( $comment ) != '' ) {
397 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
400 if ( $user === null ) {
404 $logEntry = new ManualLogEntry( 'delete', 'restore' );
405 $logEntry->setPerformer( $user );
406 $logEntry->setTarget( $this->title
);
407 $logEntry->setComment( $reason );
408 $logid = $logEntry->insert();
409 $logEntry->publish( $logid );
411 return array( $textRestored, $filesRestored, $reason );
415 * This is the meaty bit -- restores archived revisions of the given page
416 * to the cur/old tables. If the page currently exists, all revisions will
417 * be stuffed into old, otherwise the most recent will go into cur.
419 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
420 * @param $comment String
421 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
423 * @return Mixed: number of revisions restored or false on failure
425 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
426 global $wgContentHandlerNoDB;
428 if ( wfReadOnly() ) {
431 $restoreAll = empty( $timestamps );
433 $dbw = wfGetDB( DB_MASTER
);
435 # Does this page already exist? We'll have to update it...
436 $article = WikiPage
::factory( $this->title
);
437 # Load latest data for the current page (bug 31179)
438 $article->loadPageData( 'fromdbmaster' );
439 $oldcountable = $article->isCountable();
441 $page = $dbw->selectRow( 'page',
442 array( 'page_id', 'page_latest' ),
443 array( 'page_namespace' => $this->title
->getNamespace(),
444 'page_title' => $this->title
->getDBkey() ),
446 array( 'FOR UPDATE' ) // lock page
450 # Page already exists. Import the history, and if necessary
451 # we'll update the latest revision field in the record.
453 $pageId = $page->page_id
;
454 $previousRevId = $page->page_latest
;
455 # Get the time span of this page
456 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
457 array( 'rev_id' => $previousRevId ),
459 if( $previousTimestamp === false ) {
460 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
464 # Have to create a new article...
467 $previousTimestamp = 0;
471 $oldones = '1 = 1'; # All revisions...
473 $oldts = implode( ',',
474 array_map( array( &$dbw, 'addQuotes' ),
475 array_map( array( &$dbw, 'timestamp' ),
478 $oldones = "ar_timestamp IN ( {$oldts} )";
496 if ( !$wgContentHandlerNoDB ) {
497 $fields[] = 'ar_content_format';
498 $fields[] = 'ar_content_model';
502 * Select each archived revision...
504 $result = $dbw->select( 'archive',
507 'ar_namespace' => $this->title
->getNamespace(),
508 'ar_title' => $this->title
->getDBkey(),
511 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
513 $ret = $dbw->resultObject( $result );
514 $rev_count = $dbw->numRows( $result );
516 wfDebug( __METHOD__
. ": no revisions to restore\n" );
520 $ret->seek( $rev_count - 1 ); // move to last
521 $row = $ret->fetchObject(); // get newest archived rev
522 $ret->seek( 0 ); // move back
525 // Check the state of the newest to-be version...
526 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
527 return false; // we can't leave the current revision like this!
529 // Safe to insert now...
530 $newid = $article->insertOn( $dbw );
533 // Check if a deleted revision will become the current revision...
534 if( $row->ar_timestamp
> $previousTimestamp ) {
535 // Check the state of the newest to-be version...
536 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
537 return false; // we can't leave the current revision like this!
545 foreach ( $ret as $row ) {
546 // Check for key dupes due to shitty archive integrity.
547 if( $row->ar_rev_id
) {
548 $exists = $dbw->selectField( 'revision', '1',
549 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
551 continue; // don't throw DB errors
554 // Insert one revision at a time...maintaining deletion status
555 // unless we are specifically removing all restrictions...
556 $revision = Revision
::newFromArchiveRow( $row,
559 'deleted' => $unsuppress ?
0 : $row->ar_deleted
562 $revision->insertOn( $dbw );
565 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
567 # Now that it's safely stored, take it out of the archive
568 $dbw->delete( 'archive',
570 'ar_namespace' => $this->title
->getNamespace(),
571 'ar_title' => $this->title
->getDBkey(),
575 // Was anything restored at all?
576 if ( $restored == 0 ) {
580 $created = (bool)$newid;
582 // Attach the latest revision to the page...
583 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
584 if ( $created ||
$wasnew ) {
585 // Update site stats, link tables, etc
586 $user = User
::newFromName( $revision->getRawUserText(), false );
587 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
590 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
592 if( $this->title
->getNamespace() == NS_FILE
) {
593 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
603 function getFileStatus() { return $this->fileStatus
; }
607 * Special page allowing users with the appropriate permissions to view
608 * and restore deleted content.
610 * @ingroup SpecialPage
612 class SpecialUndelete
extends SpecialPage
{
613 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
614 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
621 function __construct() {
622 parent
::__construct( 'Undelete', 'deletedhistory' );
625 function loadRequest( $par ) {
626 $request = $this->getRequest();
627 $user = $this->getUser();
629 $this->mAction
= $request->getVal( 'action' );
630 if ( $par !== null && $par !== '' ) {
631 $this->mTarget
= $par;
633 $this->mTarget
= $request->getVal( 'target' );
635 $this->mTargetObj
= null;
636 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
637 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
639 $this->mSearchPrefix
= $request->getText( 'prefix' );
640 $time = $request->getVal( 'timestamp' );
641 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
642 $this->mFilename
= $request->getVal( 'file' );
644 $posted = $request->wasPosted() &&
645 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
646 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
647 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
648 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
649 $this->mDiff
= $request->getCheck( 'diff' );
650 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
651 $this->mComment
= $request->getText( 'wpComment' );
652 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
653 $this->mToken
= $request->getVal( 'token' );
655 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
656 $this->mAllowed
= true; // user can restore
657 $this->mCanView
= true; // user can view content
658 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
659 $this->mAllowed
= false; // user cannot restore
660 $this->mCanView
= true; // user can view content
661 $this->mRestore
= false;
662 } else { // user can only view the list of revisions
663 $this->mAllowed
= false;
664 $this->mCanView
= false;
665 $this->mTimestamp
= '';
666 $this->mRestore
= false;
669 if( $this->mRestore ||
$this->mInvert
) {
670 $timestamps = array();
671 $this->mFileVersions
= array();
672 foreach( $request->getValues() as $key => $val ) {
674 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
675 array_push( $timestamps, $matches[1] );
678 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
679 $this->mFileVersions
[] = intval( $matches[1] );
682 rsort( $timestamps );
683 $this->mTargetTimestamp
= $timestamps;
687 function execute( $par ) {
688 $this->checkPermissions();
689 $user = $this->getUser();
692 $this->outputHeader();
694 $this->loadRequest( $par );
696 $out = $this->getOutput();
698 if ( is_null( $this->mTargetObj
) ) {
699 $out->addWikiMsg( 'undelete-header' );
701 # Not all users can just browse every deleted page from the list
702 if ( $user->isAllowed( 'browsearchive' ) ) {
703 $this->showSearchForm();
708 if ( $this->mAllowed
) {
709 $out->setPageTitle( $this->msg( 'undeletepage' ) );
711 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
714 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
716 if ( $this->mTimestamp
!== '' ) {
717 $this->showRevision( $this->mTimestamp
);
718 } elseif ( $this->mFilename
!== null ) {
719 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
720 // Check if user is allowed to see this file
721 if ( !$file->exists() ) {
722 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
723 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
724 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
725 throw new PermissionsError( 'suppressrevision' );
727 throw new PermissionsError( 'deletedtext' );
729 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
730 $this->showFileConfirmationForm( $this->mFilename
);
732 $this->showFile( $this->mFilename
);
734 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
737 $this->showHistory();
741 function showSearchForm() {
744 $out = $this->getOutput();
745 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
747 Xml
::openElement( 'form', array(
749 'action' => $wgScript ) ) .
750 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
751 Html
::hidden( 'title',
752 $this->getTitle()->getPrefixedDbKey() ) .
753 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
754 'prefix', 'prefix', 20,
755 $this->mSearchPrefix
) . ' ' .
756 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
757 Xml
::closeElement( 'fieldset' ) .
758 Xml
::closeElement( 'form' )
761 # List undeletable articles
762 if( $this->mSearchPrefix
) {
763 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
764 $this->showList( $result );
769 * Generic list of deleted pages
771 * @param $result ResultWrapper
774 private function showList( $result ) {
775 $out = $this->getOutput();
777 if( $result->numRows() == 0 ) {
778 $out->addWikiMsg( 'undelete-no-results' );
782 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
784 $undelete = $this->getTitle();
785 $out->addHTML( "<ul>\n" );
786 foreach ( $result as $row ) {
787 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
788 if ( $title !== null ) {
789 $item = Linker
::linkKnown(
791 htmlspecialchars( $title->getPrefixedText() ),
793 array( 'target' => $title->getPrefixedText() )
796 // The title is no longer valid, show as text
797 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
798 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
800 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
801 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
804 $out->addHTML( "</ul>\n" );
809 private function showRevision( $timestamp ) {
810 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
814 $archive = new PageArchive( $this->mTargetObj
);
815 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
816 $rev = $archive->getRevision( $timestamp );
818 $out = $this->getOutput();
819 $user = $this->getUser();
822 $out->addWikiMsg( 'undeleterevision-missing' );
826 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
827 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
828 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
831 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
832 $out->addHTML( '<br />' );
833 // and we are allowed to see...
838 $previousRev = $archive->getPreviousRevision( $timestamp );
840 $this->showDiff( $previousRev, $rev );
841 if( $this->mDiffOnly
) {
844 $out->addHTML( '<hr />' );
847 $out->addWikiMsg( 'undelete-nodiff' );
851 $link = Linker
::linkKnown(
852 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
853 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
856 $lang = $this->getLanguage();
858 // date and time are separate parameters to facilitate localisation.
859 // $time is kept for backward compat reasons.
860 $time = $lang->userTimeAndDate( $timestamp, $user );
861 $d = $lang->userDate( $timestamp, $user );
862 $t = $lang->userTime( $timestamp, $user );
863 $userLink = Linker
::revUserTools( $rev );
865 if( $this->mPreview
) {
866 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
868 $openDiv = '<div id="mw-undelete-revision">';
870 $out->addHTML( $openDiv );
872 // Revision delete links
873 if ( !$this->mDiff
) {
874 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
876 $out->addHTML( "$revdel " );
880 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
881 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
882 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
884 if( $this->mPreview
) {
886 //FIXME: ContentHandler will have to provide some specialized magic to do this
887 $popts = $out->parserOptions();
888 $popts->setEditSection( false );
889 $out->parserOptions( $popts );
890 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
894 Xml
::element( 'textarea', array(
895 'readonly' => 'readonly',
896 'cols' => intval( $user->getOption( 'cols' ) ),
897 'rows' => intval( $user->getOption( 'rows' ) ) ),
898 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) . //FIXME: ContentHandler will have to provide some specialized magic to do this
899 Xml
::openElement( 'div' ) .
900 Xml
::openElement( 'form', array(
902 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
903 Xml
::element( 'input', array(
906 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
907 Xml
::element( 'input', array(
909 'name' => 'timestamp',
910 'value' => $timestamp ) ) .
911 Xml
::element( 'input', array(
913 'name' => 'wpEditToken',
914 'value' => $user->getEditToken() ) ) .
915 Xml
::element( 'input', array(
918 'value' => $this->msg( 'showpreview' )->text() ) ) .
919 Xml
::element( 'input', array(
922 'value' => $this->msg( 'showdiff' )->text() ) ) .
923 Xml
::closeElement( 'form' ) .
924 Xml
::closeElement( 'div' ) );
928 * Build a diff display between this and the previous either deleted
929 * or non-deleted edit.
931 * @param $previousRev Revision
932 * @param $currentRev Revision
933 * @return String: HTML
935 function showDiff( $previousRev, $currentRev ) {
936 $contentHandler = ContentHandler
::getForTitle( $this->getTitle() );
937 $diffEngine = $contentHandler->createDifferenceEngine( $this->getContext() );
938 $diffEngine->showDiffStyle();
939 $this->getOutput()->addHTML(
941 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
942 "<col class='diff-marker' />" .
943 "<col class='diff-content' />" .
944 "<col class='diff-marker' />" .
945 "<col class='diff-content' />" .
947 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
948 $this->diffHeader( $previousRev, 'o' ) .
950 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
951 $this->diffHeader( $currentRev, 'n' ) .
954 $diffEngine->generateContentDiffBody(
955 $previousRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ),
956 $currentRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
963 * @param $rev Revision
967 private function diffHeader( $rev, $prefix ) {
968 $isDeleted = !( $rev->getId() && $rev->getTitle() );
970 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
971 $targetPage = $this->getTitle();
972 $targetQuery = array(
973 'target' => $this->mTargetObj
->getPrefixedText(),
974 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
977 /// @todo FIXME: getId() may return non-zero for deleted revs...
978 $targetPage = $rev->getTitle();
979 $targetQuery = array( 'oldid' => $rev->getId() );
981 // Add show/hide deletion links if available
982 $user = $this->getUser();
983 $lang = $this->getLanguage();
984 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
985 if ( $rdel ) $rdel = " $rdel";
987 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
992 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
993 $lang->userDate( $rev->getTimestamp(), $user ),
994 $lang->userTime( $rev->getTimestamp(), $user )
1000 '<div id="mw-diff-'.$prefix.'title2">' .
1001 Linker
::revUserTools( $rev ) . '<br />' .
1003 '<div id="mw-diff-'.$prefix.'title3">' .
1004 Linker
::revComment( $rev ) . $rdel . '<br />' .
1009 * Show a form confirming whether a tokenless user really wants to see a file
1011 private function showFileConfirmationForm( $key ) {
1012 $out = $this->getOutput();
1013 $lang = $this->getLanguage();
1014 $user = $this->getUser();
1015 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1016 $out->addWikiMsg( 'undelete-show-file-confirm',
1017 $this->mTargetObj
->getText(),
1018 $lang->userDate( $file->getTimestamp(), $user ),
1019 $lang->userTime( $file->getTimestamp(), $user ) );
1021 Xml
::openElement( 'form', array(
1023 'action' => $this->getTitle()->getLocalURL(
1024 'target=' . urlencode( $this->mTarget
) .
1025 '&file=' . urlencode( $key ) .
1026 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1029 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1035 * Show a deleted file version requested by the visitor.
1037 private function showFile( $key ) {
1038 $this->getOutput()->disable();
1040 # We mustn't allow the output to be Squid cached, otherwise
1041 # if an admin previews a deleted image, and it's cached, then
1042 # a user without appropriate permissions can toddle off and
1043 # nab the image, and Squid will serve it
1044 $response = $this->getRequest()->response();
1045 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1046 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1047 $response->header( 'Pragma: no-cache' );
1049 $repo = RepoGroup
::singleton()->getLocalRepo();
1050 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1051 $repo->streamFile( $path );
1054 private function showHistory() {
1055 $out = $this->getOutput();
1056 if( $this->mAllowed
) {
1057 $out->addModules( 'mediawiki.special.undelete' );
1060 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1061 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1064 $archive = new PageArchive( $this->mTargetObj
);
1065 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1067 $text = $archive->getLastRevisionText();
1068 if( is_null( $text ) ) {
1069 $out->addWikiMsg( 'nohistory' );
1073 $out->addHTML( '<div class="mw-undelete-history">' );
1074 if ( $this->mAllowed
) {
1075 $out->addWikiMsg( 'undeletehistory' );
1076 $out->addWikiMsg( 'undeleterevdel' );
1078 $out->addWikiMsg( 'undeletehistorynoadmin' );
1080 $out->addHTML( '</div>' );
1082 # List all stored revisions
1083 $revisions = $archive->listRevisions();
1084 $files = $archive->listFiles();
1086 $haveRevisions = $revisions && $revisions->numRows() > 0;
1087 $haveFiles = $files && $files->numRows() > 0;
1089 # Batch existence check on user and talk pages
1090 if( $haveRevisions ) {
1091 $batch = new LinkBatch();
1092 foreach ( $revisions as $row ) {
1093 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1094 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1097 $revisions->seek( 0 );
1100 $batch = new LinkBatch();
1101 foreach ( $files as $row ) {
1102 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1103 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1109 if ( $this->mAllowed
) {
1110 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1111 # Start the form here
1112 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1113 $out->addHTML( $top );
1116 # Show relevant lines from the deletion log:
1117 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1118 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1119 # Show relevant lines from the suppression log:
1120 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1121 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1122 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1125 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1126 # Format the user-visible controls (comment field, submission button)
1127 # in a nice little table
1128 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1132 <td class='mw-input'>" .
1133 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1134 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1138 $unsuppressBox = '';
1141 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1142 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1144 <td colspan='2' class='mw-undelete-extrahelp'>" .
1145 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1149 <td class='mw-label'>" .
1150 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1152 <td class='mw-input'>" .
1153 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1158 <td class='mw-submit'>" .
1159 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1160 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1164 Xml
::closeElement( 'table' ) .
1165 Xml
::closeElement( 'fieldset' );
1167 $out->addHTML( $table );
1170 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1172 if( $haveRevisions ) {
1173 # The page's stored (deleted) history:
1174 $out->addHTML( '<ul>' );
1175 $remaining = $revisions->numRows();
1176 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1178 foreach ( $revisions as $row ) {
1180 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1183 $out->addHTML( '</ul>' );
1185 $out->addWikiMsg( 'nohistory' );
1189 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1190 $out->addHTML( '<ul>' );
1191 foreach ( $files as $row ) {
1192 $out->addHTML( $this->formatFileRow( $row ) );
1195 $out->addHTML( '</ul>' );
1198 if ( $this->mAllowed
) {
1199 # Slip in the hidden controls here
1200 $misc = Html
::hidden( 'target', $this->mTarget
);
1201 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1202 $misc .= Xml
::closeElement( 'form' );
1203 $out->addHTML( $misc );
1209 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1210 $rev = Revision
::newFromArchiveRow( $row,
1211 array( 'page' => $this->mTargetObj
->getArticleID() ) );
1213 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1214 // Build checkboxen...
1215 if( $this->mAllowed
) {
1216 if( $this->mInvert
) {
1217 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1218 $checkBox = Xml
::check( "ts$ts" );
1220 $checkBox = Xml
::check( "ts$ts", true );
1223 $checkBox = Xml
::check( "ts$ts" );
1228 $user = $this->getUser();
1229 // Build page & diff links...
1230 if( $this->mCanView
) {
1231 $titleObj = $this->getTitle();
1233 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1234 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1235 $last = $this->msg( 'diff' )->escaped();
1236 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1237 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1238 $last = Linker
::linkKnown(
1240 $this->msg( 'diff' )->escaped(),
1243 'target' => $this->mTargetObj
->getPrefixedText(),
1249 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1250 $last = $this->msg( 'diff' )->escaped();
1253 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1254 $last = $this->msg( 'diff' )->escaped();
1257 $userLink = Linker
::revUserTools( $rev );
1258 // Revision text size
1259 $size = $row->ar_len
;
1260 if( !is_null( $size ) ) {
1261 $revTextSize = Linker
::formatRevisionSize( $size );
1264 $comment = Linker
::revComment( $rev );
1265 // Revision delete links
1266 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1268 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1269 return "<li>$revisionRow</li>";
1272 private function formatFileRow( $row ) {
1273 $file = ArchivedFile
::newFromRow( $row );
1275 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1276 $user = $this->getUser();
1277 if( $this->mAllowed
&& $row->fa_storage_key
) {
1278 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1279 $key = urlencode( $row->fa_storage_key
);
1280 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1283 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1285 $userLink = $this->getFileUser( $file );
1286 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1287 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1288 $data = htmlspecialchars( $data . ' ' . $bytes );
1289 $comment = $this->getFileComment( $file );
1291 // Add show/hide deletion links if available
1292 $canHide = $user->isAllowed( 'deleterevision' );
1293 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1294 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1295 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1298 'type' => 'filearchive',
1299 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1300 'ids' => $row->fa_id
1302 $revdlink = Linker
::revDeleteLink( $query,
1303 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1309 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1313 * Fetch revision text link if it's available to all users
1315 * @param $rev Revision
1316 * @param $titleObj Title
1317 * @param $ts string Timestamp
1320 function getPageLink( $rev, $titleObj, $ts ) {
1321 $user = $this->getUser();
1322 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1324 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1325 return '<span class="history-deleted">' . $time . '</span>';
1327 $link = Linker
::linkKnown(
1329 htmlspecialchars( $time ),
1332 'target' => $this->mTargetObj
->getPrefixedText(),
1336 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1337 $link = '<span class="history-deleted">' . $link . '</span>';
1344 * Fetch image view link if it's available to all users
1347 * @param $titleObj Title
1348 * @param $ts string A timestamp
1349 * @param $key String: a storage key
1351 * @return String: HTML fragment
1353 function getFileLink( $file, $titleObj, $ts, $key ) {
1354 $user = $this->getUser();
1355 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1357 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1358 return '<span class="history-deleted">' . $time . '</span>';
1360 $link = Linker
::linkKnown(
1362 htmlspecialchars( $time ),
1365 'target' => $this->mTargetObj
->getPrefixedText(),
1367 'token' => $user->getEditToken( $key )
1370 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1371 $link = '<span class="history-deleted">' . $link . '</span>';
1378 * Fetch file's user id if it's available to this user
1381 * @return String: HTML fragment
1383 function getFileUser( $file ) {
1384 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1385 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1387 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1388 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1389 if( $file->isDeleted( File
::DELETED_USER
) ) {
1390 $link = '<span class="history-deleted">' . $link . '</span>';
1397 * Fetch file upload comment if it's available to this user
1400 * @return String: HTML fragment
1402 function getFileComment( $file ) {
1403 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1404 return '<span class="history-deleted"><span class="comment">' .
1405 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1407 $link = Linker
::commentBlock( $file->getRawDescription() );
1408 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1409 $link = '<span class="history-deleted">' . $link . '</span>';
1415 function undelete() {
1416 global $wgUploadMaintenance;
1418 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1419 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1422 if ( wfReadOnly() ) {
1423 throw new ReadOnlyError
;
1426 $out = $this->getOutput();
1427 $archive = new PageArchive( $this->mTargetObj
);
1428 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1429 $ok = $archive->undelete(
1430 $this->mTargetTimestamp
,
1432 $this->mFileVersions
,
1437 if( is_array( $ok ) ) {
1438 if ( $ok[1] ) { // Undeleted file count
1439 wfRunHooks( 'FileUndeleteComplete', array(
1440 $this->mTargetObj
, $this->mFileVersions
,
1441 $this->getUser(), $this->mComment
) );
1444 $link = Linker
::linkKnown( $this->mTargetObj
);
1445 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1447 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1448 $out->addWikiMsg( 'cannotundelete' );
1449 $out->addWikiMsg( 'undeleterevdel' );
1452 // Show file deletion warnings and errors
1453 $status = $archive->getFileStatus();
1454 if( $status && !$status->isGood() ) {
1455 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );