4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
14 function wfSpecialUndelete( $par ) {
17 $form = new UndeleteForm( $wgRequest, $par );
22 * Used to show archived pages and eventually restore them.
23 * @ingroup SpecialPage
29 function __construct( $title ) {
30 if( is_null( $title ) ) {
31 throw new MWException( 'Archiver() given a null title.');
33 $this->title
= $title;
37 * List all deleted pages recorded in the archive table. Returns result
38 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
41 * @return ResultWrapper
43 public static function listAllPages() {
44 $dbr = wfGetDB( DB_SLAVE
);
45 return self
::listPages( $dbr, '' );
49 * List deleted pages recorded in the archive table matching the
51 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
53 * @return ResultWrapper
55 public static function listPagesByPrefix( $prefix ) {
56 $dbr = wfGetDB( DB_SLAVE
);
58 $title = Title
::newFromText( $prefix );
60 $ns = $title->getNamespace();
61 $encPrefix = $dbr->escapeLike( $title->getDBkey() );
63 // Prolly won't work too good
64 // @todo handle bare namespace names cleanly?
66 $encPrefix = $dbr->escapeLike( $prefix );
69 'ar_namespace' => $ns,
70 "ar_title LIKE '$encPrefix%'",
72 return self
::listPages( $dbr, $conds );
75 protected static function listPages( $dbr, $condition ) {
76 return $dbr->resultObject(
87 'GROUP BY' => 'ar_namespace,ar_title',
88 'ORDER BY' => 'ar_namespace,ar_title',
96 * List the revisions of the given page. Returns result wrapper with
97 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
99 * @return ResultWrapper
101 function listRevisions() {
102 $dbr = wfGetDB( DB_SLAVE
);
103 $res = $dbr->select( 'archive',
104 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
105 array( 'ar_namespace' => $this->title
->getNamespace(),
106 'ar_title' => $this->title
->getDBkey() ),
107 'PageArchive::listRevisions',
108 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
109 $ret = $dbr->resultObject( $res );
114 * List the deleted file revisions for this page, if it's a file page.
115 * Returns a result wrapper with various filearchive fields, or null
116 * if not a file page.
118 * @return ResultWrapper
119 * @todo Does this belong in Image for fuller encapsulation?
121 function listFiles() {
122 if( $this->title
->getNamespace() == NS_FILE
) {
123 $dbr = wfGetDB( DB_SLAVE
);
124 $res = $dbr->select( 'filearchive',
144 array( 'fa_name' => $this->title
->getDBkey() ),
146 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
147 $ret = $dbr->resultObject( $res );
154 * Fetch (and decompress if necessary) the stored text for the deleted
155 * revision of the page with the given timestamp.
158 * @deprecated Use getRevision() for more flexible information
160 function getRevisionText( $timestamp ) {
161 $rev = $this->getRevision( $timestamp );
162 return $rev ?
$rev->getText() : null;
166 * Return a Revision object containing data for the deleted revision.
167 * Note that the result *may* or *may not* have a null page ID.
168 * @param string $timestamp
171 function getRevision( $timestamp ) {
172 $dbr = wfGetDB( DB_SLAVE
);
173 $row = $dbr->selectRow( 'archive',
186 array( 'ar_namespace' => $this->title
->getNamespace(),
187 'ar_title' => $this->title
->getDBkey(),
188 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
191 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
198 * Return the most-previous revision, either live or deleted, against
199 * the deleted revision given by timestamp.
201 * May produce unexpected results in case of history merges or other
202 * unusual time issues.
204 * @param string $timestamp
205 * @return Revision or null
207 function getPreviousRevision( $timestamp ) {
208 $dbr = wfGetDB( DB_SLAVE
);
210 // Check the previous deleted revision...
211 $row = $dbr->selectRow( 'archive',
213 array( 'ar_namespace' => $this->title
->getNamespace(),
214 'ar_title' => $this->title
->getDBkey(),
216 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
219 'ORDER BY' => 'ar_timestamp DESC',
221 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
223 $row = $dbr->selectRow( array( 'page', 'revision' ),
224 array( 'rev_id', 'rev_timestamp' ),
226 'page_namespace' => $this->title
->getNamespace(),
227 'page_title' => $this->title
->getDBkey(),
228 'page_id = rev_page',
230 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
233 'ORDER BY' => 'rev_timestamp DESC',
235 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
236 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
238 if( $prevLive && $prevLive > $prevDeleted ) {
239 // Most prior revision was live
240 return Revision
::newFromId( $prevLiveId );
241 } elseif( $prevDeleted ) {
242 // Most prior revision was deleted
243 return $this->getRevision( $prevDeleted );
245 // No prior revision on this page.
251 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
253 function getTextFromRow( $row ) {
254 if( is_null( $row->ar_text_id
) ) {
255 // An old row from MediaWiki 1.4 or previous.
256 // Text is embedded in this row in classic compression format.
257 return Revision
::getRevisionText( $row, "ar_" );
259 // New-style: keyed to the text storage backend.
260 $dbr = wfGetDB( DB_SLAVE
);
261 $text = $dbr->selectRow( 'text',
262 array( 'old_text', 'old_flags' ),
263 array( 'old_id' => $row->ar_text_id
),
265 return Revision
::getRevisionText( $text );
271 * Fetch (and decompress if necessary) the stored text of the most
272 * recently edited deleted revision of the page.
274 * If there are no archived revisions for the page, returns NULL.
278 function getLastRevisionText() {
279 $dbr = wfGetDB( DB_SLAVE
);
280 $row = $dbr->selectRow( 'archive',
281 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
282 array( 'ar_namespace' => $this->title
->getNamespace(),
283 'ar_title' => $this->title
->getDBkey() ),
284 'PageArchive::getLastRevisionText',
285 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
287 return $this->getTextFromRow( $row );
294 * Quick check if any archived revisions are present for the page.
297 function isDeleted() {
298 $dbr = wfGetDB( DB_SLAVE
);
299 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
300 array( 'ar_namespace' => $this->title
->getNamespace(),
301 'ar_title' => $this->title
->getDBkey() ) );
306 * Restore the given (or all) text and file revisions for the page.
307 * Once restored, the items will be removed from the archive tables.
308 * The deletion log will be updated with an undeletion notice.
310 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
311 * @param string $comment
312 * @param array $fileVersions
313 * @param bool $unsuppress
315 * @return array(number of file revisions restored, number of image revisions restored, log message)
316 * on success, false on failure
318 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
319 // If both the set of text revisions and file revisions are empty,
320 // restore everything. Otherwise, just restore the requested items.
321 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
323 $restoreText = $restoreAll ||
!empty( $timestamps );
324 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
326 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
327 $img = wfLocalFile( $this->title
);
328 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
329 $filesRestored = $this->fileStatus
->successCount
;
335 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
336 if($textRestored === false) // It must be one of UNDELETE_*
344 $log = new LogPage( 'delete' );
346 if( $textRestored && $filesRestored ) {
347 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
348 $wgContLang->formatNum( $textRestored ),
349 $wgContLang->formatNum( $filesRestored ) );
350 } elseif( $textRestored ) {
351 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
352 $wgContLang->formatNum( $textRestored ) );
353 } elseif( $filesRestored ) {
354 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
355 $wgContLang->formatNum( $filesRestored ) );
357 wfDebug( "Undelete: nothing undeleted...\n" );
361 if( trim( $comment ) != '' )
362 $reason .= ": {$comment}";
363 $log->addEntry( 'restore', $this->title
, $reason );
365 return array($textRestored, $filesRestored, $reason);
369 * This is the meaty bit -- restores archived revisions of the given page
370 * to the cur/old tables. If the page currently exists, all revisions will
371 * be stuffed into old, otherwise the most recent will go into cur.
373 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
374 * @param string $comment
375 * @param array $fileVersions
376 * @param bool $unsuppress, remove all ar_deleted/fa_deleted restrictions of seletected revs
378 * @return mixed number of revisions restored or false on failure
380 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
383 $restoreAll = empty( $timestamps );
385 $dbw = wfGetDB( DB_MASTER
);
387 # Does this page already exist? We'll have to update it...
388 $article = new Article( $this->title
);
389 $options = 'FOR UPDATE';
390 $page = $dbw->selectRow( 'page',
391 array( 'page_id', 'page_latest' ),
392 array( 'page_namespace' => $this->title
->getNamespace(),
393 'page_title' => $this->title
->getDBkey() ),
398 # Page already exists. Import the history, and if necessary
399 # we'll update the latest revision field in the record.
401 $pageId = $page->page_id
;
402 $previousRevId = $page->page_latest
;
403 # Get the time span of this page
404 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
405 array( 'rev_id' => $previousRevId ),
407 if( $previousTimestamp === false ) {
408 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
412 # Have to create a new article...
415 $previousTimestamp = 0;
419 $oldones = '1 = 1'; # All revisions...
421 $oldts = implode( ',',
422 array_map( array( &$dbw, 'addQuotes' ),
423 array_map( array( &$dbw, 'timestamp' ),
426 $oldones = "ar_timestamp IN ( {$oldts} )";
430 * Select each archived revision...
432 $result = $dbw->select( 'archive',
447 'ar_namespace' => $this->title
->getNamespace(),
448 'ar_title' => $this->title
->getDBkey(),
451 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
453 $ret = $dbw->resultObject( $result );
454 $rev_count = $dbw->numRows( $result );
457 $newid = $article->insertOn( $dbw );
464 while( $row = $ret->fetchObject() ) {
465 // Check for key dupes due to shitty archive integrity.
466 if( $row->ar_rev_id
) {
467 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
468 if( $exists ) continue; // don't throw DB errors
471 $revision = Revision
::newFromArchiveRow( $row,
474 'deleted' => $unsuppress ?
0 : $row->ar_deleted
477 $revision->insertOn( $dbw );
480 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
482 # Now that it's safely stored, take it out of the archive
483 $dbw->delete( 'archive',
485 'ar_namespace' => $this->title
->getNamespace(),
486 'ar_title' => $this->title
->getDBkey(),
490 // Was anything restored at all?
495 // Attach the latest revision to the page...
496 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
497 if( $newid ||
$wasnew ) {
498 // We don't handle well with top revision deleted
499 // FIXME: any sysop can unsuppress any revision by just undeleting it into a non-existent page!
500 if( $revision->getVisibility() ) {
501 $dbw->update( 'revision',
502 array( 'rev_deleted' => 0 ),
503 array( 'rev_id' => $revision->getId() ),
506 $revision->mDeleted
= 0; // Don't pollute the parser cache
508 // Update site stats, link tables, etc
509 $article->createUpdates( $revision );
513 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
514 Article
::onArticleCreate( $this->title
);
516 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
517 Article
::onArticleEdit( $this->title
);
520 if( $this->title
->getNamespace() == NS_FILE
) {
521 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
525 // Revision couldn't be created. This is very weird
526 return self
::UNDELETE_UNKNOWNERR
;
532 function getFileStatus() { return $this->fileStatus
; }
536 * The HTML form for Special:Undelete, which allows users with the appropriate
537 * permissions to view and restore deleted content.
538 * @ingroup SpecialPage
541 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
542 var $mTargetTimestamp, $mAllowed, $mComment, $mToken;
544 function UndeleteForm( $request, $par = "" ) {
546 $this->mAction
= $request->getVal( 'action' );
547 $this->mTarget
= $request->getVal( 'target' );
548 $this->mSearchPrefix
= $request->getText( 'prefix' );
549 $time = $request->getVal( 'timestamp' );
550 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
551 $this->mFile
= $request->getVal( 'file' );
553 $posted = $request->wasPosted() &&
554 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
555 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
556 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
557 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
558 $this->mDiff
= $request->getCheck( 'diff' );
559 $this->mComment
= $request->getText( 'wpComment' );
560 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
561 $this->mToken
= $request->getVal( 'token' );
564 $this->mTarget
= $par;
566 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
567 $this->mAllowed
= true;
569 $this->mAllowed
= false;
570 $this->mTimestamp
= '';
571 $this->mRestore
= false;
573 if ( $this->mTarget
!== "" ) {
574 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
576 $this->mTargetObj
= NULL;
578 if( $this->mRestore ||
$this->mInvert
) {
579 $timestamps = array();
580 $this->mFileVersions
= array();
581 foreach( $_REQUEST as $key => $val ) {
583 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
584 array_push( $timestamps, $matches[1] );
587 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
588 $this->mFileVersions
[] = intval( $matches[1] );
591 rsort( $timestamps );
592 $this->mTargetTimestamp
= $timestamps;
597 global $wgOut, $wgUser;
598 if ( $this->mAllowed
) {
599 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
601 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
604 if( is_null( $this->mTargetObj
) ) {
605 # Not all users can just browse every deleted page from the list
606 if( $wgUser->isAllowed( 'browsearchive' ) ) {
607 $this->showSearchForm();
609 # List undeletable articles
610 if( $this->mSearchPrefix
) {
611 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
612 $this->showList( $result );
615 $wgOut->addWikiMsg( 'undelete-header' );
619 if( $this->mTimestamp
!== '' ) {
620 return $this->showRevision( $this->mTimestamp
);
622 if( $this->mFile
!== null ) {
623 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
624 // Check if user is allowed to see this file
625 if( !$file->userCan( File
::DELETED_FILE
) ) {
626 $wgOut->permissionRequired( 'suppressrevision' );
628 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
629 $this->showFileConfirmationForm( $this->mFile
);
632 return $this->showFile( $this->mFile
);
635 if( $this->mRestore
&& $this->mAction
== "submit" ) {
636 return $this->undelete();
638 if( $this->mInvert
&& $this->mAction
== "submit" ) {
639 return $this->showHistory( );
641 return $this->showHistory();
644 function showSearchForm() {
645 global $wgOut, $wgScript;
646 $wgOut->addWikiMsg( 'undelete-header' );
649 Xml
::openElement( 'form', array(
651 'action' => $wgScript ) ) .
652 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
653 Xml
::hidden( 'title',
654 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
655 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
656 'prefix', 'prefix', 20,
657 $this->mSearchPrefix
) . ' ' .
658 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
659 Xml
::closeElement( 'fieldset' ) .
660 Xml
::closeElement( 'form' )
664 // Generic list of deleted pages
665 private function showList( $result ) {
666 global $wgLang, $wgContLang, $wgUser, $wgOut;
668 if( $result->numRows() == 0 ) {
669 $wgOut->addWikiMsg( 'undelete-no-results' );
673 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
675 $sk = $wgUser->getSkin();
676 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
677 $wgOut->addHTML( "<ul>\n" );
678 while( $row = $result->fetchObject() ) {
679 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
680 $link = $sk->linkKnown(
682 htmlspecialchars( $title->getPrefixedText() ),
684 array( 'target' => $title->getPrefixedText() )
686 $revs = wfMsgExt( 'undeleterevisions',
687 array( 'parseinline' ),
688 $wgLang->formatNum( $row->count
) );
689 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
692 $wgOut->addHTML( "</ul>\n" );
697 private function showRevision( $timestamp ) {
698 global $wgLang, $wgUser, $wgOut;
699 $self = SpecialPage
::getTitleFor( 'Undelete' );
700 $skin = $wgUser->getSkin();
702 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
704 $archive = new PageArchive( $this->mTargetObj
);
705 $rev = $archive->getRevision( $timestamp );
708 $wgOut->addWikiMsg( 'undeleterevision-missing' );
712 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
713 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
714 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-permission' );
717 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-view' );
718 $wgOut->addHTML( '<br/>' );
719 // and we are allowed to see...
723 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
725 $link = $skin->linkKnown(
726 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
727 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
731 $previousRev = $archive->getPreviousRevision( $timestamp );
733 $this->showDiff( $previousRev, $rev );
734 if( $wgUser->getOption( 'diffonly' ) ) {
737 $wgOut->addHTML( '<hr />' );
740 $wgOut->addWikiMsg( 'undelete-nodiff' );
744 // date and time are separate parameters to facilitate localisation.
745 // $time is kept for backward compat reasons.
746 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
747 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
748 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
749 $user = $skin->revUserTools( $rev );
751 if( $this->mPreview
) {
752 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
754 $openDiv = '<div id="mw-undelete-revision">';
758 if( $wgUser->isAllowed( 'deleterevision' ) ) {
759 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
760 // If revision was hidden from sysops
761 $revdlink = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
762 '('.wfMsgHtml('rev-delundel').')' );
766 'target' => $this->mTargetObj
->getPrefixedDBkey(),
767 'ids' => $rev->getTimestamp()
769 $revdlink = $skin->revDeleteLink( $query, $rev->isDeleted( File
::DELETED_RESTRICTED
) );
773 $wgOut->addHTML( $openDiv . wfMsgWikiHtml( 'undelete-revision', $link, $time, $user, $d, $t ) .
774 $revdlink . '</div>' );
775 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
777 if( $this->mPreview
) {
779 $popts = $wgOut->parserOptions();
780 $popts->setEditSection( false );
781 $wgOut->parserOptions( $popts );
782 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
786 Xml
::element( 'textarea', array(
787 'readonly' => 'readonly',
788 'cols' => intval( $wgUser->getOption( 'cols' ) ),
789 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
790 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
791 Xml
::openElement( 'div' ) .
792 Xml
::openElement( 'form', array(
794 'action' => $self->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
795 Xml
::element( 'input', array(
798 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
799 Xml
::element( 'input', array(
801 'name' => 'timestamp',
802 'value' => $timestamp ) ) .
803 Xml
::element( 'input', array(
805 'name' => 'wpEditToken',
806 'value' => $wgUser->editToken() ) ) .
807 Xml
::element( 'input', array(
810 'value' => wfMsg( 'showpreview' ) ) ) .
811 Xml
::element( 'input', array(
814 'value' => wfMsg( 'showdiff' ) ) ) .
815 Xml
::closeElement( 'form' ) .
816 Xml
::closeElement( 'div' ) );
820 * Build a diff display between this and the previous either deleted
821 * or non-deleted edit.
822 * @param Revision $previousRev
823 * @param Revision $currentRev
824 * @return string HTML
826 function showDiff( $previousRev, $currentRev ) {
829 $diffEngine = new DifferenceEngine();
830 $diffEngine->showDiffStyle();
833 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
834 "<col class='diff-marker' />" .
835 "<col class='diff-content' />" .
836 "<col class='diff-marker' />" .
837 "<col class='diff-content' />" .
839 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
840 $this->diffHeader( $previousRev, 'o' ) .
842 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
843 $this->diffHeader( $currentRev, 'n' ) .
846 $diffEngine->generateDiffBody(
847 $previousRev->getText(), $currentRev->getText() ) .
853 private function diffHeader( $rev, $prefix ) {
854 global $wgUser, $wgLang, $wgLang;
855 $sk = $wgUser->getSkin();
856 $isDeleted = !( $rev->getId() && $rev->getTitle() );
858 /// @fixme $rev->getTitle() is null for deleted revs...?
859 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
860 $targetQuery = array(
861 'target' => $this->mTargetObj
->getPrefixedText(),
862 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
865 /// @fixme getId() may return non-zero for deleted revs...
866 $targetPage = $rev->getTitle();
867 $targetQuery = array( 'oldid' => $rev->getId() );
869 // Add show/hide link if available
870 if( $wgUser->isAllowed( 'deleterevision' ) ) {
871 // If revision was hidden from sysops
872 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
873 $del = ' ' . Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
874 '(' . wfMsgHtml('rev-delundel') . ')' );
875 // Otherwise, show the link...
879 'target' => $this->mTargetObj
->getPrefixedDbkey(),
880 'ids' => $rev->getTimestamp() );
881 $del = ' ' . $sk->revDeleteLink( $query,
882 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
888 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
893 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
894 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
895 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
901 '<div id="mw-diff-'.$prefix.'title2">' .
902 $sk->revUserTools( $rev ) . '<br/>' .
904 '<div id="mw-diff-'.$prefix.'title3">' .
905 $sk->revComment( $rev ) . $del . '<br/>' .
910 * Show a form confirming whether a tokenless user really wants to see a file
912 private function showFileConfirmationForm( $key ) {
913 global $wgOut, $wgUser, $wgLang;
914 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
915 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
916 $this->mTargetObj
->getText(),
917 $wgLang->date( $file->getTimestamp() ),
918 $wgLang->time( $file->getTimestamp() ) );
920 Xml
::openElement( 'form', array(
922 'action' => SpecialPage
::getTitleFor( 'Undelete' )->getLocalUrl(
923 'target=' . urlencode( $this->mTarget
) .
924 '&file=' . urlencode( $key ) .
925 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
928 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
934 * Show a deleted file version requested by the visitor.
936 private function showFile( $key ) {
937 global $wgOut, $wgRequest;
940 # We mustn't allow the output to be Squid cached, otherwise
941 # if an admin previews a deleted image, and it's cached, then
942 # a user without appropriate permissions can toddle off and
943 # nab the image, and Squid will serve it
944 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
945 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
946 $wgRequest->response()->header( 'Pragma: no-cache' );
949 require_once( "$IP/includes/StreamFile.php" );
950 $repo = RepoGroup
::singleton()->getLocalRepo();
951 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
952 wfStreamFile( $path );
955 private function showHistory( ) {
956 global $wgLang, $wgUser, $wgOut;
958 $sk = $wgUser->getSkin();
959 if( $this->mAllowed
) {
960 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
962 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
965 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1</div>\n", array ( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() ) );
967 $archive = new PageArchive( $this->mTargetObj
);
969 $text = $archive->getLastRevisionText();
970 if( is_null( $text ) ) {
971 $wgOut->addWikiMsg( "nohistory" );
975 $wgOut->addHTML( '<div class="mw-undelete-history">' );
976 if ( $this->mAllowed
) {
977 $wgOut->addWikiMsg( "undeletehistory" );
978 $wgOut->addWikiMsg( "undeleterevdel" );
980 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
982 $wgOut->addHTML( '</div>' );
984 # List all stored revisions
985 $revisions = $archive->listRevisions();
986 $files = $archive->listFiles();
988 $haveRevisions = $revisions && $revisions->numRows() > 0;
989 $haveFiles = $files && $files->numRows() > 0;
991 # Batch existence check on user and talk pages
992 if( $haveRevisions ) {
993 $batch = new LinkBatch();
994 while( $row = $revisions->fetchObject() ) {
995 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
996 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
999 $revisions->seek( 0 );
1002 $batch = new LinkBatch();
1003 while( $row = $files->fetchObject() ) {
1004 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1005 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1011 if ( $this->mAllowed
) {
1012 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1013 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
1014 # Start the form here
1015 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1016 $wgOut->addHTML( $top );
1019 # Show relevant lines from the deletion log:
1020 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1021 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1022 # Show relevant lines from the suppression log:
1023 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1024 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1025 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1028 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1029 # Format the user-visible controls (comment field, submission button)
1030 # in a nice little table
1031 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1035 <td class='mw-input'>" .
1036 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1037 'mw-undelete-unsuppress', $this->mUnsuppress
).
1041 $unsuppressBox = "";
1044 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1045 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1047 <td colspan='2' class='mw-undelete-extrahelp'>" .
1048 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1052 <td class='mw-label'>" .
1053 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1055 <td class='mw-input'>" .
1056 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1061 <td class='mw-submit'>" .
1062 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1063 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1064 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1068 Xml
::closeElement( 'table' ) .
1069 Xml
::closeElement( 'fieldset' );
1071 $wgOut->addHTML( $table );
1074 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1076 if( $haveRevisions ) {
1077 # The page's stored (deleted) history:
1078 $wgOut->addHTML("<ul>");
1079 $target = urlencode( $this->mTarget
);
1080 $remaining = $revisions->numRows();
1081 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1083 while( $row = $revisions->fetchObject() ) {
1085 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1088 $wgOut->addHTML("</ul>");
1090 $wgOut->addWikiMsg( "nohistory" );
1094 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1095 $wgOut->addHTML( "<ul>" );
1096 while( $row = $files->fetchObject() ) {
1097 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1100 $wgOut->addHTML( "</ul>" );
1103 if ( $this->mAllowed
) {
1104 # Slip in the hidden controls here
1105 $misc = Xml
::hidden( 'target', $this->mTarget
);
1106 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1107 $misc .= Xml
::closeElement( 'form' );
1108 $wgOut->addHTML( $misc );
1114 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1115 global $wgUser, $wgLang;
1117 $rev = Revision
::newFromArchiveRow( $row,
1118 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1120 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1121 if( $this->mAllowed
) {
1122 if( $this->mInvert
){
1123 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1124 $checkBox = Xml
::check( "ts$ts");
1126 $checkBox = Xml
::check( "ts$ts", true );
1129 $checkBox = Xml
::check( "ts$ts" );
1131 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1132 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1134 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1135 $last = wfMsgHtml('diff');
1136 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1137 $last = $sk->linkKnown(
1142 'target' => $this->mTargetObj
->getPrefixedText(),
1148 $last = wfMsgHtml('diff');
1152 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1153 $last = wfMsgHtml('diff');
1155 $userLink = $sk->revUserTools( $rev );
1157 if(!is_null($size = $row->ar_len
)) {
1158 $stxt = $sk->formatRevisionSize( $size );
1160 $comment = $sk->revComment( $rev );
1162 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1163 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1164 // If revision was hidden from sysops
1165 $revdlink = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
1166 '('.wfMsgHtml('rev-delundel').')' );
1169 'type' => 'archive',
1170 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1173 $revdlink = $sk->revDeleteLink( $query, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1177 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1180 private function formatFileRow( $row, $sk ) {
1181 global $wgUser, $wgLang;
1183 $file = ArchivedFile
::newFromRow( $row );
1185 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1186 if( $this->mAllowed
&& $row->fa_storage_key
) {
1187 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1188 $key = urlencode( $row->fa_storage_key
);
1189 $target = urlencode( $this->mTarget
);
1190 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1191 $pageLink = $this->getFileLink( $file, $titleObj, $ts, $key, $sk );
1194 $pageLink = $wgLang->timeanddate( $ts, true );
1196 $userLink = $this->getFileUser( $file, $sk );
1198 wfMsg( 'widthheight',
1199 $wgLang->formatNum( $row->fa_width
),
1200 $wgLang->formatNum( $row->fa_height
) ) .
1202 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1204 $data = htmlspecialchars( $data );
1205 $comment = $this->getFileComment( $file, $sk );
1207 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1208 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1209 // If revision was hidden from sysops
1210 $revdlink = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.wfMsgHtml('rev-delundel').')' );
1213 'type' => 'filearchive',
1214 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1215 'ids' => $row->fa_id
1217 $revdlink = $sk->revDeleteLink( $query, $file->isDeleted( File
::DELETED_RESTRICTED
) );
1220 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1224 * Fetch revision text link if it's available to all users
1227 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1230 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1232 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1233 return '<span class="history-deleted">' . $time . '</span>';
1235 $link = $sk->linkKnown(
1240 'target' => $this->mTargetObj
->getPrefixedText(),
1244 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1245 $link = '<span class="history-deleted">' . $link . '</span>';
1251 * Fetch image view link if it's available to all users
1254 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1255 global $wgLang, $wgUser;
1257 if( !$file->userCan(File
::DELETED_FILE
) ) {
1258 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1260 $link = $sk->linkKnown(
1262 $wgLang->timeanddate( $ts, true ),
1265 'target' => $this->mTargetObj
->getPrefixedText(),
1267 'token' => $wgUser->editToken( $key )
1270 if( $file->isDeleted(File
::DELETED_FILE
) )
1271 $link = '<span class="history-deleted">' . $link . '</span>';
1277 * Fetch file's user id if it's available to this user
1280 function getFileUser( $file, $sk ) {
1281 if( !$file->userCan(File
::DELETED_USER
) ) {
1282 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1284 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1285 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1286 if( $file->isDeleted(File
::DELETED_USER
) )
1287 $link = '<span class="history-deleted">' . $link . '</span>';
1293 * Fetch file upload comment if it's available to this user
1296 function getFileComment( $file, $sk ) {
1297 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1298 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1300 $link = $sk->commentBlock( $file->getRawDescription() );
1301 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1302 $link = '<span class="history-deleted">' . $link . '</span>';
1307 function undelete() {
1308 global $wgOut, $wgUser;
1309 if ( wfReadOnly() ) {
1310 $wgOut->readOnlyPage();
1313 if( !is_null( $this->mTargetObj
) ) {
1314 $archive = new PageArchive( $this->mTargetObj
);
1315 $ok = $archive->undelete(
1316 $this->mTargetTimestamp
,
1318 $this->mFileVersions
,
1319 $this->mUnsuppress
);
1321 if( is_array($ok) ) {
1322 if ( $ok[1] ) // Undeleted file count
1323 wfRunHooks( 'FileUndeleteComplete', array(
1324 $this->mTargetObj
, $this->mFileVersions
,
1325 $wgUser, $this->mComment
) );
1327 $skin = $wgUser->getSkin();
1328 $link = $skin->linkKnown( $this->mTargetObj
);
1329 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1331 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1332 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1335 // Show file deletion warnings and errors
1336 $status = $archive->getFileStatus();
1337 if( $status && !$status->isGood() ) {
1338 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1341 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );