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'; // lock page
390 $page = $dbw->selectRow( 'page',
391 array( 'page_id', 'page_latest' ),
392 array( 'page_namespace' => $this->title
->getNamespace(),
393 'page_title' => $this->title
->getDBkey() ),
399 # Page already exists. Import the history, and if necessary
400 # we'll update the latest revision field in the record.
402 $pageId = $page->page_id
;
403 $previousRevId = $page->page_latest
;
404 # Get the time span of this page
405 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
406 array( 'rev_id' => $previousRevId ),
408 if( $previousTimestamp === false ) {
409 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
413 # Have to create a new article...
416 $previousTimestamp = 0;
420 $oldones = '1 = 1'; # All revisions...
422 $oldts = implode( ',',
423 array_map( array( &$dbw, 'addQuotes' ),
424 array_map( array( &$dbw, 'timestamp' ),
427 $oldones = "ar_timestamp IN ( {$oldts} )";
431 * Select each archived revision...
433 $result = $dbw->select( 'archive',
448 'ar_namespace' => $this->title
->getNamespace(),
449 'ar_title' => $this->title
->getDBkey(),
452 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
454 $ret = $dbw->resultObject( $result );
455 $rev_count = $dbw->numRows( $result );
457 wfDebug( __METHOD__
.": no revisions to restore\n" );
461 $ret->seek( $rev_count - 1 ); // move to last
462 $row = $ret->fetchObject(); // get newest archived rev
463 $ret->seek( 0 ); // move back
466 // Check the state of the newest to-be version...
467 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
468 return false; // we can't leave the current revision like this!
470 // Safe to insert now...
471 $newid = $article->insertOn( $dbw );
474 // Check if a deleted revision will become the current revision...
475 if( $row->ar_timestamp
> $previousTimestamp ) {
476 // Check the state of the newest to-be version...
477 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
478 return false; // we can't leave the current revision like this!
486 while( $row = $ret->fetchObject() ) {
487 // Check for key dupes due to shitty archive integrity.
488 if( $row->ar_rev_id
) {
489 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
490 if( $exists ) continue; // don't throw DB errors
492 // Insert one revision at a time...maintaining deletion status
493 // unless we are specifically removing all restrictions...
494 $revision = Revision
::newFromArchiveRow( $row,
497 'deleted' => $unsuppress ?
0 : $row->ar_deleted
500 $revision->insertOn( $dbw );
503 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
505 # Now that it's safely stored, take it out of the archive
506 $dbw->delete( 'archive',
508 'ar_namespace' => $this->title
->getNamespace(),
509 'ar_title' => $this->title
->getDBkey(),
513 // Was anything restored at all?
518 // Attach the latest revision to the page...
519 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
520 if( $newid ||
$wasnew ) {
521 // Update site stats, link tables, etc
522 $article->createUpdates( $revision );
526 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
527 Article
::onArticleCreate( $this->title
);
529 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
530 Article
::onArticleEdit( $this->title
);
533 if( $this->title
->getNamespace() == NS_FILE
) {
534 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
538 // Revision couldn't be created. This is very weird
539 return self
::UNDELETE_UNKNOWNERR
;
545 function getFileStatus() { return $this->fileStatus
; }
549 * The HTML form for Special:Undelete, which allows users with the appropriate
550 * permissions to view and restore deleted content.
551 * @ingroup SpecialPage
554 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
555 var $mTargetTimestamp, $mAllowed, $mComment, $mToken;
557 function UndeleteForm( $request, $par = "" ) {
559 $this->mAction
= $request->getVal( 'action' );
560 $this->mTarget
= $request->getVal( 'target' );
561 $this->mSearchPrefix
= $request->getText( 'prefix' );
562 $time = $request->getVal( 'timestamp' );
563 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
564 $this->mFile
= $request->getVal( 'file' );
566 $posted = $request->wasPosted() &&
567 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
568 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
569 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
570 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
571 $this->mDiff
= $request->getCheck( 'diff' );
572 $this->mComment
= $request->getText( 'wpComment' );
573 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
574 $this->mToken
= $request->getVal( 'token' );
577 $this->mTarget
= $par;
579 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
580 $this->mAllowed
= true;
582 $this->mAllowed
= false;
583 $this->mTimestamp
= '';
584 $this->mRestore
= false;
586 if ( $this->mTarget
!== "" ) {
587 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
589 $this->mTargetObj
= NULL;
591 if( $this->mRestore ||
$this->mInvert
) {
592 $timestamps = array();
593 $this->mFileVersions
= array();
594 foreach( $_REQUEST as $key => $val ) {
596 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
597 array_push( $timestamps, $matches[1] );
600 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
601 $this->mFileVersions
[] = intval( $matches[1] );
604 rsort( $timestamps );
605 $this->mTargetTimestamp
= $timestamps;
610 global $wgOut, $wgUser;
611 if ( $this->mAllowed
) {
612 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
614 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
617 if( is_null( $this->mTargetObj
) ) {
618 # Not all users can just browse every deleted page from the list
619 if( $wgUser->isAllowed( 'browsearchive' ) ) {
620 $this->showSearchForm();
622 # List undeletable articles
623 if( $this->mSearchPrefix
) {
624 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
625 $this->showList( $result );
628 $wgOut->addWikiMsg( 'undelete-header' );
632 if( $this->mTimestamp
!== '' ) {
633 return $this->showRevision( $this->mTimestamp
);
635 if( $this->mFile
!== null ) {
636 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
637 // Check if user is allowed to see this file
638 if( !$file->userCan( File
::DELETED_FILE
) ) {
639 $wgOut->permissionRequired( 'suppressrevision' );
641 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
642 $this->showFileConfirmationForm( $this->mFile
);
645 return $this->showFile( $this->mFile
);
648 if( $this->mRestore
&& $this->mAction
== "submit" ) {
649 return $this->undelete();
651 if( $this->mInvert
&& $this->mAction
== "submit" ) {
652 return $this->showHistory( );
654 return $this->showHistory();
657 function showSearchForm() {
658 global $wgOut, $wgScript;
659 $wgOut->addWikiMsg( 'undelete-header' );
662 Xml
::openElement( 'form', array(
664 'action' => $wgScript ) ) .
665 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
666 Xml
::hidden( 'title',
667 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
668 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
669 'prefix', 'prefix', 20,
670 $this->mSearchPrefix
) . ' ' .
671 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
672 Xml
::closeElement( 'fieldset' ) .
673 Xml
::closeElement( 'form' )
677 // Generic list of deleted pages
678 private function showList( $result ) {
679 global $wgLang, $wgContLang, $wgUser, $wgOut;
681 if( $result->numRows() == 0 ) {
682 $wgOut->addWikiMsg( 'undelete-no-results' );
686 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
688 $sk = $wgUser->getSkin();
689 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
690 $wgOut->addHTML( "<ul>\n" );
691 while( $row = $result->fetchObject() ) {
692 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
693 $link = $sk->linkKnown(
695 htmlspecialchars( $title->getPrefixedText() ),
697 array( 'target' => $title->getPrefixedText() )
699 $revs = wfMsgExt( 'undeleterevisions',
700 array( 'parseinline' ),
701 $wgLang->formatNum( $row->count
) );
702 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
705 $wgOut->addHTML( "</ul>\n" );
710 private function showRevision( $timestamp ) {
711 global $wgLang, $wgUser, $wgOut;
712 $self = SpecialPage
::getTitleFor( 'Undelete' );
713 $skin = $wgUser->getSkin();
715 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
717 $archive = new PageArchive( $this->mTargetObj
);
718 $rev = $archive->getRevision( $timestamp );
721 $wgOut->addWikiMsg( 'undeleterevision-missing' );
725 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
726 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
727 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-permission' );
730 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-view' );
731 $wgOut->addHTML( '<br/>' );
732 // and we are allowed to see...
736 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
738 $link = $skin->linkKnown(
739 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
740 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
744 $previousRev = $archive->getPreviousRevision( $timestamp );
746 $this->showDiff( $previousRev, $rev );
747 if( $wgUser->getOption( 'diffonly' ) ) {
750 $wgOut->addHTML( '<hr />' );
753 $wgOut->addWikiMsg( 'undelete-nodiff' );
757 // date and time are separate parameters to facilitate localisation.
758 // $time is kept for backward compat reasons.
759 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
760 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
761 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
762 $user = $skin->revUserTools( $rev );
764 if( $this->mPreview
) {
765 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
767 $openDiv = '<div id="mw-undelete-revision">';
771 if( $wgUser->isAllowed( 'deleterevision' ) ) {
772 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
773 // If revision was hidden from sysops
774 $revdlink = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
775 '('.wfMsgHtml('rev-delundel').')' );
779 'target' => $this->mTargetObj
->getPrefixedDBkey(),
780 'ids' => $rev->getTimestamp()
782 $revdlink = $skin->revDeleteLink( $query, $rev->isDeleted( File
::DELETED_RESTRICTED
) );
786 $wgOut->addHTML( $openDiv . wfMsgWikiHtml( 'undelete-revision', $link, $time, $user, $d, $t ) .
787 $revdlink . '</div>' );
788 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
790 if( $this->mPreview
) {
792 $popts = $wgOut->parserOptions();
793 $popts->setEditSection( false );
794 $wgOut->parserOptions( $popts );
795 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
799 Xml
::element( 'textarea', array(
800 'readonly' => 'readonly',
801 'cols' => intval( $wgUser->getOption( 'cols' ) ),
802 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
803 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
804 Xml
::openElement( 'div' ) .
805 Xml
::openElement( 'form', array(
807 'action' => $self->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
808 Xml
::element( 'input', array(
811 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
812 Xml
::element( 'input', array(
814 'name' => 'timestamp',
815 'value' => $timestamp ) ) .
816 Xml
::element( 'input', array(
818 'name' => 'wpEditToken',
819 'value' => $wgUser->editToken() ) ) .
820 Xml
::element( 'input', array(
823 'value' => wfMsg( 'showpreview' ) ) ) .
824 Xml
::element( 'input', array(
827 'value' => wfMsg( 'showdiff' ) ) ) .
828 Xml
::closeElement( 'form' ) .
829 Xml
::closeElement( 'div' ) );
833 * Build a diff display between this and the previous either deleted
834 * or non-deleted edit.
835 * @param Revision $previousRev
836 * @param Revision $currentRev
837 * @return string HTML
839 function showDiff( $previousRev, $currentRev ) {
842 $diffEngine = new DifferenceEngine();
843 $diffEngine->showDiffStyle();
846 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
847 "<col class='diff-marker' />" .
848 "<col class='diff-content' />" .
849 "<col class='diff-marker' />" .
850 "<col class='diff-content' />" .
852 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
853 $this->diffHeader( $previousRev, 'o' ) .
855 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
856 $this->diffHeader( $currentRev, 'n' ) .
859 $diffEngine->generateDiffBody(
860 $previousRev->getText(), $currentRev->getText() ) .
866 private function diffHeader( $rev, $prefix ) {
867 global $wgUser, $wgLang, $wgLang;
868 $sk = $wgUser->getSkin();
869 $isDeleted = !( $rev->getId() && $rev->getTitle() );
871 /// @fixme $rev->getTitle() is null for deleted revs...?
872 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
873 $targetQuery = array(
874 'target' => $this->mTargetObj
->getPrefixedText(),
875 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
878 /// @fixme getId() may return non-zero for deleted revs...
879 $targetPage = $rev->getTitle();
880 $targetQuery = array( 'oldid' => $rev->getId() );
882 // Add show/hide link if available
883 if( $wgUser->isAllowed( 'deleterevision' ) ) {
884 // If revision was hidden from sysops
885 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
886 $del = ' ' . Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
887 '(' . wfMsgHtml('rev-delundel') . ')' );
888 // Otherwise, show the link...
892 'target' => $this->mTargetObj
->getPrefixedDbkey(),
893 'ids' => $rev->getTimestamp() );
894 $del = ' ' . $sk->revDeleteLink( $query,
895 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
901 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
906 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
907 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
908 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
914 '<div id="mw-diff-'.$prefix.'title2">' .
915 $sk->revUserTools( $rev ) . '<br/>' .
917 '<div id="mw-diff-'.$prefix.'title3">' .
918 $sk->revComment( $rev ) . $del . '<br/>' .
923 * Show a form confirming whether a tokenless user really wants to see a file
925 private function showFileConfirmationForm( $key ) {
926 global $wgOut, $wgUser, $wgLang;
927 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
928 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
929 $this->mTargetObj
->getText(),
930 $wgLang->date( $file->getTimestamp() ),
931 $wgLang->time( $file->getTimestamp() ) );
933 Xml
::openElement( 'form', array(
935 'action' => SpecialPage
::getTitleFor( 'Undelete' )->getLocalUrl(
936 'target=' . urlencode( $this->mTarget
) .
937 '&file=' . urlencode( $key ) .
938 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
941 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
947 * Show a deleted file version requested by the visitor.
949 private function showFile( $key ) {
950 global $wgOut, $wgRequest;
953 # We mustn't allow the output to be Squid cached, otherwise
954 # if an admin previews a deleted image, and it's cached, then
955 # a user without appropriate permissions can toddle off and
956 # nab the image, and Squid will serve it
957 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
958 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
959 $wgRequest->response()->header( 'Pragma: no-cache' );
962 require_once( "$IP/includes/StreamFile.php" );
963 $repo = RepoGroup
::singleton()->getLocalRepo();
964 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
965 wfStreamFile( $path );
968 private function showHistory( ) {
969 global $wgLang, $wgUser, $wgOut;
971 $sk = $wgUser->getSkin();
972 if( $this->mAllowed
) {
973 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
975 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
978 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1</div>\n", array ( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() ) );
980 $archive = new PageArchive( $this->mTargetObj
);
982 $text = $archive->getLastRevisionText();
983 if( is_null( $text ) ) {
984 $wgOut->addWikiMsg( "nohistory" );
988 $wgOut->addHTML( '<div class="mw-undelete-history">' );
989 if ( $this->mAllowed
) {
990 $wgOut->addWikiMsg( "undeletehistory" );
991 $wgOut->addWikiMsg( "undeleterevdel" );
993 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
995 $wgOut->addHTML( '</div>' );
997 # List all stored revisions
998 $revisions = $archive->listRevisions();
999 $files = $archive->listFiles();
1001 $haveRevisions = $revisions && $revisions->numRows() > 0;
1002 $haveFiles = $files && $files->numRows() > 0;
1004 # Batch existence check on user and talk pages
1005 if( $haveRevisions ) {
1006 $batch = new LinkBatch();
1007 while( $row = $revisions->fetchObject() ) {
1008 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1009 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1012 $revisions->seek( 0 );
1015 $batch = new LinkBatch();
1016 while( $row = $files->fetchObject() ) {
1017 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1018 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1024 if ( $this->mAllowed
) {
1025 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1026 $action = $titleObj->getLocalURL( array( 'action' => 'submit' ) );
1027 # Start the form here
1028 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1029 $wgOut->addHTML( $top );
1032 # Show relevant lines from the deletion log:
1033 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1034 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1035 # Show relevant lines from the suppression log:
1036 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1037 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1038 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1041 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1042 # Format the user-visible controls (comment field, submission button)
1043 # in a nice little table
1044 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1048 <td class='mw-input'>" .
1049 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1050 'mw-undelete-unsuppress', $this->mUnsuppress
).
1054 $unsuppressBox = "";
1057 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1058 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1060 <td colspan='2' class='mw-undelete-extrahelp'>" .
1061 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1065 <td class='mw-label'>" .
1066 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1068 <td class='mw-input'>" .
1069 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1074 <td class='mw-submit'>" .
1075 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1076 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1077 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1081 Xml
::closeElement( 'table' ) .
1082 Xml
::closeElement( 'fieldset' );
1084 $wgOut->addHTML( $table );
1087 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1089 if( $haveRevisions ) {
1090 # The page's stored (deleted) history:
1091 $wgOut->addHTML("<ul>");
1092 $target = urlencode( $this->mTarget
);
1093 $remaining = $revisions->numRows();
1094 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1096 while( $row = $revisions->fetchObject() ) {
1098 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1101 $wgOut->addHTML("</ul>");
1103 $wgOut->addWikiMsg( "nohistory" );
1107 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1108 $wgOut->addHTML( "<ul>" );
1109 while( $row = $files->fetchObject() ) {
1110 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1113 $wgOut->addHTML( "</ul>" );
1116 if ( $this->mAllowed
) {
1117 # Slip in the hidden controls here
1118 $misc = Xml
::hidden( 'target', $this->mTarget
);
1119 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1120 $misc .= Xml
::closeElement( 'form' );
1121 $wgOut->addHTML( $misc );
1127 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1128 global $wgUser, $wgLang;
1130 $rev = Revision
::newFromArchiveRow( $row,
1131 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1133 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1134 if( $this->mAllowed
) {
1135 if( $this->mInvert
){
1136 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1137 $checkBox = Xml
::check( "ts$ts");
1139 $checkBox = Xml
::check( "ts$ts", true );
1142 $checkBox = Xml
::check( "ts$ts" );
1144 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1145 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1147 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1148 $last = wfMsgHtml('diff');
1149 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1150 $last = $sk->linkKnown(
1155 'target' => $this->mTargetObj
->getPrefixedText(),
1161 $last = wfMsgHtml('diff');
1165 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1166 $last = wfMsgHtml('diff');
1168 $userLink = $sk->revUserTools( $rev );
1170 if(!is_null($size = $row->ar_len
)) {
1171 $stxt = $sk->formatRevisionSize( $size );
1173 $comment = $sk->revComment( $rev );
1175 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1176 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1177 // If revision was hidden from sysops
1178 $revdlink = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
1179 '('.wfMsgHtml('rev-delundel').')' );
1182 'type' => 'archive',
1183 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1186 $revdlink = $sk->revDeleteLink( $query, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1190 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1193 private function formatFileRow( $row, $sk ) {
1194 global $wgUser, $wgLang;
1196 $file = ArchivedFile
::newFromRow( $row );
1198 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1199 if( $this->mAllowed
&& $row->fa_storage_key
) {
1200 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1201 $key = urlencode( $row->fa_storage_key
);
1202 $target = urlencode( $this->mTarget
);
1203 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1204 $pageLink = $this->getFileLink( $file, $titleObj, $ts, $key, $sk );
1207 $pageLink = $wgLang->timeanddate( $ts, true );
1209 $userLink = $this->getFileUser( $file, $sk );
1211 wfMsg( 'widthheight',
1212 $wgLang->formatNum( $row->fa_width
),
1213 $wgLang->formatNum( $row->fa_height
) ) .
1215 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1217 $data = htmlspecialchars( $data );
1218 $comment = $this->getFileComment( $file, $sk );
1220 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1221 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1222 // If revision was hidden from sysops
1223 $revdlink = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.wfMsgHtml('rev-delundel').')' );
1226 'type' => 'filearchive',
1227 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1228 'ids' => $row->fa_id
1230 $revdlink = $sk->revDeleteLink( $query, $file->isDeleted( File
::DELETED_RESTRICTED
) );
1233 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1237 * Fetch revision text link if it's available to all users
1240 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1243 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1245 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1246 return '<span class="history-deleted">' . $time . '</span>';
1248 $link = $sk->linkKnown(
1253 'target' => $this->mTargetObj
->getPrefixedText(),
1257 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1258 $link = '<span class="history-deleted">' . $link . '</span>';
1264 * Fetch image view link if it's available to all users
1267 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1268 global $wgLang, $wgUser;
1270 if( !$file->userCan(File
::DELETED_FILE
) ) {
1271 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1273 $link = $sk->linkKnown(
1275 $wgLang->timeanddate( $ts, true ),
1278 'target' => $this->mTargetObj
->getPrefixedText(),
1280 'token' => $wgUser->editToken( $key )
1283 if( $file->isDeleted(File
::DELETED_FILE
) )
1284 $link = '<span class="history-deleted">' . $link . '</span>';
1290 * Fetch file's user id if it's available to this user
1293 function getFileUser( $file, $sk ) {
1294 if( !$file->userCan(File
::DELETED_USER
) ) {
1295 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1297 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1298 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1299 if( $file->isDeleted(File
::DELETED_USER
) )
1300 $link = '<span class="history-deleted">' . $link . '</span>';
1306 * Fetch file upload comment if it's available to this user
1309 function getFileComment( $file, $sk ) {
1310 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1311 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1313 $link = $sk->commentBlock( $file->getRawDescription() );
1314 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1315 $link = '<span class="history-deleted">' . $link . '</span>';
1320 function undelete() {
1321 global $wgOut, $wgUser;
1322 if ( wfReadOnly() ) {
1323 $wgOut->readOnlyPage();
1326 if( !is_null( $this->mTargetObj
) ) {
1327 $archive = new PageArchive( $this->mTargetObj
);
1328 $ok = $archive->undelete(
1329 $this->mTargetTimestamp
,
1331 $this->mFileVersions
,
1332 $this->mUnsuppress
);
1334 if( is_array($ok) ) {
1335 if ( $ok[1] ) // Undeleted file count
1336 wfRunHooks( 'FileUndeleteComplete', array(
1337 $this->mTargetObj
, $this->mFileVersions
,
1338 $wgUser, $this->mComment
) );
1340 $skin = $wgUser->getSkin();
1341 $link = $skin->linkKnown( $this->mTargetObj
);
1342 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1344 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1345 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1348 // Show file deletion warnings and errors
1349 $status = $archive->getFileStatus();
1350 if( $status && !$status->isGood() ) {
1351 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1354 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );