4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
7 * @addtogroup SpecialPage
13 function wfSpecialUndelete( $par ) {
16 $form = new UndeleteForm( $wgRequest, $par );
21 * Used to show archived pages and eventually restore them.
22 * @addtogroup SpecialPage
28 function __construct( $title ) {
29 if( is_null( $title ) ) {
30 throw new MWException( 'Archiver() given a null title.');
32 $this->title
= $title;
36 * List all deleted pages recorded in the archive table. Returns result
37 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
40 * @return ResultWrapper
42 public static function listAllPages() {
43 $dbr = wfGetDB( DB_SLAVE
);
44 return self
::listPages( $dbr, '' );
48 * List deleted pages recorded in the archive table matching the
50 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
52 * @return ResultWrapper
54 public static function listPagesByPrefix( $prefix ) {
55 $dbr = wfGetDB( DB_SLAVE
);
57 $title = Title
::newFromText( $prefix );
59 $ns = $title->getNamespace();
60 $encPrefix = $dbr->escapeLike( $title->getDBkey() );
62 // Prolly won't work too good
63 // @todo handle bare namespace names cleanly?
65 $encPrefix = $dbr->escapeLike( $prefix );
68 'ar_namespace' => $ns,
69 "ar_title LIKE '$encPrefix%'",
71 return self
::listPages( $dbr, $conds );
74 protected static function listPages( $dbr, $condition ) {
75 return $dbr->resultObject(
86 'GROUP BY' => 'ar_namespace,ar_title',
87 'ORDER BY' => 'ar_namespace,ar_title',
95 * List the revisions of the given page. Returns result wrapper with
96 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
98 * @return ResultWrapper
100 function listRevisions() {
101 $dbr = wfGetDB( DB_SLAVE
);
102 $res = $dbr->select( 'archive',
103 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len' ),
104 array( 'ar_namespace' => $this->title
->getNamespace(),
105 'ar_title' => $this->title
->getDBkey() ),
106 'PageArchive::listRevisions',
107 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
108 $ret = $dbr->resultObject( $res );
113 * List the deleted file revisions for this page, if it's a file page.
114 * Returns a result wrapper with various filearchive fields, or null
115 * if not a file page.
117 * @return ResultWrapper
118 * @todo Does this belong in Image for fuller encapsulation?
120 function listFiles() {
121 if( $this->title
->getNamespace() == NS_IMAGE
) {
122 $dbr = wfGetDB( DB_SLAVE
);
123 $res = $dbr->select( 'filearchive',
135 array( 'fa_name' => $this->title
->getDBkey() ),
137 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
138 $ret = $dbr->resultObject( $res );
145 * Fetch (and decompress if necessary) the stored text for the deleted
146 * revision of the page with the given timestamp.
149 * @deprecated Use getRevision() for more flexible information
151 function getRevisionText( $timestamp ) {
152 $rev = $this->getRevision( $timestamp );
153 return $rev ?
$rev->getText() : null;
157 * Return a Revision object containing data for the deleted revision.
158 * Note that the result *may* or *may not* have a null page ID.
159 * @param string $timestamp
162 function getRevision( $timestamp ) {
163 $dbr = wfGetDB( DB_SLAVE
);
164 $row = $dbr->selectRow( 'archive',
176 array( 'ar_namespace' => $this->title
->getNamespace(),
177 'ar_title' => $this->title
->getDBkey(),
178 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
181 return new Revision( array(
182 'page' => $this->title
->getArticleId(),
183 'id' => $row->ar_rev_id
,
184 'text' => ($row->ar_text_id
186 : Revision
::getRevisionText( $row, 'ar_' ) ),
187 'comment' => $row->ar_comment
,
188 'user' => $row->ar_user
,
189 'user_text' => $row->ar_user_text
,
190 'timestamp' => $row->ar_timestamp
,
191 'minor_edit' => $row->ar_minor_edit
,
192 'text_id' => $row->ar_text_id
) );
199 * Return the most-previous revision, either live or deleted, against
200 * the deleted revision given by timestamp.
202 * May produce unexpected results in case of history merges or other
203 * unusual time issues.
205 * @param string $timestamp
206 * @return Revision or null
208 function getPreviousRevision( $timestamp ) {
209 $dbr = wfGetDB( DB_SLAVE
);
211 // Check the previous deleted revision...
212 $row = $dbr->selectRow( 'archive',
214 array( 'ar_namespace' => $this->title
->getNamespace(),
215 'ar_title' => $this->title
->getDBkey(),
217 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
220 'ORDER BY' => 'ar_timestamp DESC',
222 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
224 $row = $dbr->selectRow( array( 'page', 'revision' ),
225 array( 'rev_id', 'rev_timestamp' ),
227 'page_namespace' => $this->title
->getNamespace(),
228 'page_title' => $this->title
->getDBkey(),
229 'page_id = rev_page',
231 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
234 'ORDER BY' => 'rev_timestamp DESC',
236 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
237 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
239 if( $prevLive && $prevLive > $prevDeleted ) {
240 // Most prior revision was live
241 return Revision
::newFromId( $prevLiveId );
242 } elseif( $prevDeleted ) {
243 // Most prior revision was deleted
244 return $this->getRevision( $prevDeleted );
246 // No prior revision on this page.
252 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
254 function getTextFromRow( $row ) {
255 if( is_null( $row->ar_text_id
) ) {
256 // An old row from MediaWiki 1.4 or previous.
257 // Text is embedded in this row in classic compression format.
258 return Revision
::getRevisionText( $row, "ar_" );
260 // New-style: keyed to the text storage backend.
261 $dbr = wfGetDB( DB_SLAVE
);
262 $text = $dbr->selectRow( 'text',
263 array( 'old_text', 'old_flags' ),
264 array( 'old_id' => $row->ar_text_id
),
266 return Revision
::getRevisionText( $text );
272 * Fetch (and decompress if necessary) the stored text of the most
273 * recently edited deleted revision of the page.
275 * If there are no archived revisions for the page, returns NULL.
279 function getLastRevisionText() {
280 $dbr = wfGetDB( DB_SLAVE
);
281 $row = $dbr->selectRow( 'archive',
282 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
283 array( 'ar_namespace' => $this->title
->getNamespace(),
284 'ar_title' => $this->title
->getDBkey() ),
285 'PageArchive::getLastRevisionText',
286 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
288 return $this->getTextFromRow( $row );
295 * Quick check if any archived revisions are present for the page.
298 function isDeleted() {
299 $dbr = wfGetDB( DB_SLAVE
);
300 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
301 array( 'ar_namespace' => $this->title
->getNamespace(),
302 'ar_title' => $this->title
->getDBkey() ) );
307 * Restore the given (or all) text and file revisions for the page.
308 * Once restored, the items will be removed from the archive tables.
309 * The deletion log will be updated with an undeletion notice.
311 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
312 * @param string $comment
313 * @param array $fileVersions
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() ) {
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_IMAGE
) {
327 $img = wfLocalFile( $this->title
);
328 $this->fileStatus
= $img->restore( $fileVersions );
329 $filesRestored = $this->fileStatus
->successCount
;
335 $textRestored = $this->undeleteRevisions( $timestamps );
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
377 * @return mixed number of revisions restored or false on failure
379 private function undeleteRevisions( $timestamps ) {
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() ),
397 # Page already exists. Import the history, and if necessary
398 # we'll update the latest revision field in the record.
400 $pageId = $page->page_id
;
401 $previousRevId = $page->page_latest
;
403 # Have to create a new article...
404 $newid = $article->insertOn( $dbw );
410 $oldones = '1 = 1'; # All revisions...
412 $oldts = implode( ',',
413 array_map( array( &$dbw, 'addQuotes' ),
414 array_map( array( &$dbw, 'timestamp' ),
417 $oldones = "ar_timestamp IN ( {$oldts} )";
421 * Restore each revision...
423 $result = $dbw->select( 'archive',
437 'ar_namespace' => $this->title
->getNamespace(),
438 'ar_title' => $this->title
->getDBkey(),
442 'ORDER BY' => 'ar_timestamp' )
444 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
445 wfDebug( __METHOD__
.": couldn't find all requested rows\n" );
452 while( $row = $dbw->fetchObject( $result ) ) {
453 if( $row->ar_text_id
) {
454 // Revision was deleted in 1.5+; text is in
455 // the regular text table, use the reference.
456 // Specify null here so the so the text is
457 // dereferenced for page length info if needed.
460 // Revision was deleted in 1.4 or earlier.
461 // Text is squashed into the archive row, and
462 // a new text table entry will be created for it.
463 $revText = Revision
::getRevisionText( $row, 'ar_' );
465 $revision = new Revision( array(
467 'id' => $row->ar_rev_id
,
469 'comment' => $row->ar_comment
,
470 'user' => $row->ar_user
,
471 'user_text' => $row->ar_user_text
,
472 'timestamp' => $row->ar_timestamp
,
473 'minor_edit' => $row->ar_minor_edit
,
474 'text_id' => $row->ar_text_id
,
475 'len' => $row->ar_len
477 $revision->insertOn( $dbw );
480 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
482 // Was anything restored at all?
487 // Attach the latest revision to the page...
488 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
490 if( $newid ||
$wasnew ) {
491 // Update site stats, link tables, etc
492 $article->createUpdates( $revision );
496 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true ) );
497 Article
::onArticleCreate( $this->title
);
499 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false ) );
500 Article
::onArticleEdit( $this->title
);
503 if( $this->title
->getNamespace() == NS_IMAGE
) {
504 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
508 // Revision couldn't be created. This is very weird
509 return self
::UNDELETE_UNKNOWNERR
;
512 # Now that it's safely stored, take it out of the archive
513 $dbw->delete( 'archive',
515 'ar_namespace' => $this->title
->getNamespace(),
516 'ar_title' => $this->title
->getDBkey(),
523 function getFileStatus() { return $this->fileStatus
; }
527 * The HTML form for Special:Undelete, which allows users with the appropriate
528 * permissions to view and restore deleted content.
529 * @addtogroup SpecialPage
532 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
533 var $mTargetTimestamp, $mAllowed, $mComment;
535 function UndeleteForm( $request, $par = "" ) {
537 $this->mAction
= $request->getVal( 'action' );
538 $this->mTarget
= $request->getVal( 'target' );
539 $this->mSearchPrefix
= $request->getText( 'prefix' );
540 $time = $request->getVal( 'timestamp' );
541 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
542 $this->mFile
= $request->getVal( 'file' );
544 $posted = $request->wasPosted() &&
545 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
546 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
547 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
548 $this->mDiff
= $request->getCheck( 'diff' );
549 $this->mComment
= $request->getText( 'wpComment' );
552 $this->mTarget
= $par;
554 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
555 $this->mAllowed
= true;
557 $this->mAllowed
= false;
558 $this->mTimestamp
= '';
559 $this->mRestore
= false;
561 if ( $this->mTarget
!== "" ) {
562 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
564 $this->mTargetObj
= NULL;
566 if( $this->mRestore
) {
567 $timestamps = array();
568 $this->mFileVersions
= array();
569 foreach( $_REQUEST as $key => $val ) {
571 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
572 array_push( $timestamps, $matches[1] );
575 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
576 $this->mFileVersions
[] = intval( $matches[1] );
579 rsort( $timestamps );
580 $this->mTargetTimestamp
= $timestamps;
586 if ( $this->mAllowed
) {
587 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
589 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
592 if( is_null( $this->mTargetObj
) ) {
593 $this->showSearchForm();
595 # List undeletable articles
596 if( $this->mSearchPrefix
) {
597 $result = PageArchive
::listPagesByPrefix(
598 $this->mSearchPrefix
);
599 $this->showList( $result );
603 if( $this->mTimestamp
!== '' ) {
604 return $this->showRevision( $this->mTimestamp
);
606 if( $this->mFile
!== null ) {
607 return $this->showFile( $this->mFile
);
609 if( $this->mRestore
&& $this->mAction
== "submit" ) {
610 return $this->undelete();
612 return $this->showHistory();
615 function showSearchForm() {
616 global $wgOut, $wgScript;
617 $wgOut->addWikiMsg( 'undelete-header' );
620 Xml
::openElement( 'form', array(
622 'action' => $wgScript ) ) .
624 Xml
::element( 'legend', array(),
625 wfMsg( 'undelete-search-box' ) ) .
626 Xml
::hidden( 'title',
627 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
628 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
629 'prefix', 'prefix', 20,
630 $this->mSearchPrefix
) .
631 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
636 /* private */ function showList( $result ) {
637 global $wgLang, $wgContLang, $wgUser, $wgOut;
639 if( $result->numRows() == 0 ) {
640 $wgOut->addWikiMsg( 'undelete-no-results' );
644 $wgOut->addWikiMsg( "undeletepagetext" );
646 $sk = $wgUser->getSkin();
647 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
648 $wgOut->addHTML( "<ul>\n" );
649 while( $row = $result->fetchObject() ) {
650 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
651 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
652 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
653 $revs = wfMsgExt( 'undeleterevisions',
654 array( 'parseinline' ),
655 $wgLang->formatNum( $row->count
) );
656 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
659 $wgOut->addHTML( "</ul>\n" );
664 /* private */ function showRevision( $timestamp ) {
665 global $wgLang, $wgUser, $wgOut;
666 $self = SpecialPage
::getTitleFor( 'Undelete' );
667 $skin = $wgUser->getSkin();
669 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
671 $archive = new PageArchive( $this->mTargetObj
);
672 $rev = $archive->getRevision( $timestamp );
675 $wgOut->addWikiMsg( 'undeleterevision-missing' );
679 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
681 $link = $skin->makeKnownLinkObj(
682 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
683 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
685 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
686 $user = $skin->userLink( $rev->getUser(), $rev->getUserText() )
687 . $skin->userToolLinks( $rev->getUser(), $rev->getUserText() );
690 $previousRev = $archive->getPreviousRevision( $timestamp );
692 $this->showDiff( $previousRev, $rev );
693 if( $wgUser->getOption( 'diffonly' ) ) {
696 $wgOut->addHtml( '<hr />' );
699 $wgOut->addHtml( wfMsgHtml( 'undelete-nodiff' ) );
703 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user ) . '</p>' );
705 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
707 if( $this->mPreview
) {
708 $wgOut->addHtml( "<hr />\n" );
709 $wgOut->addWikiTextTitleTidy( $rev->getText(), $this->mTargetObj
, false );
713 wfElement( 'textarea', array(
714 'readonly' => 'readonly',
715 'cols' => intval( $wgUser->getOption( 'cols' ) ),
716 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
717 $rev->getText() . "\n" ) .
718 wfOpenElement( 'div' ) .
719 wfOpenElement( 'form', array(
721 'action' => $self->getLocalURL( "action=submit" ) ) ) .
722 wfElement( 'input', array(
725 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
726 wfElement( 'input', array(
728 'name' => 'timestamp',
729 'value' => $timestamp ) ) .
730 wfElement( 'input', array(
732 'name' => 'wpEditToken',
733 'value' => $wgUser->editToken() ) ) .
734 wfElement( 'input', array(
737 'value' => wfMsg( 'showpreview' ) ) ) .
738 wfElement( 'input', array(
741 'value' => wfMsg( 'showdiff' ) ) ) .
742 wfCloseElement( 'form' ) .
743 wfCloseElement( 'div' ) );
747 * Build a diff display between this and the previous either deleted
748 * or non-deleted edit.
749 * @param Revision $previousRev
750 * @param Revision $currentRev
751 * @return string HTML
753 function showDiff( $previousRev, $currentRev ) {
754 global $wgOut, $wgUser;
756 $diffEngine = new DifferenceEngine();
757 $diffEngine->showDiffStyle();
760 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
761 "<col class='diff-marker' />" .
762 "<col class='diff-content' />" .
763 "<col class='diff-marker' />" .
764 "<col class='diff-content' />" .
766 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
767 $this->diffHeader( $previousRev ) .
769 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
770 $this->diffHeader( $currentRev ) .
773 $diffEngine->generateDiffBody(
774 $previousRev->getText(), $currentRev->getText() ) .
780 private function diffHeader( $rev ) {
781 global $wgUser, $wgLang, $wgLang;
782 $sk = $wgUser->getSkin();
783 $isDeleted = !( $rev->getId() && $rev->getTitle() );
785 /// @fixme $rev->getTitle() is null for deleted revs...?
786 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
787 $targetQuery = 'target=' .
788 $this->mTargetObj
->getPrefixedUrl() .
790 wfTimestamp( TS_MW
, $rev->getTimestamp() );
792 /// @fixme getId() may return non-zero for deleted revs...
793 $targetPage = $rev->getTitle();
794 $targetQuery = 'oldid=' . $rev->getId();
797 '<div id="mw-diff-otitle1"><strong>' .
798 $sk->makeLinkObj( $targetPage,
799 wfMsgHtml( 'revisionasof',
800 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
802 ( $isDeleted ?
' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
804 '<div id="mw-diff-otitle2">' .
805 $sk->revUserTools( $rev ) . '<br/>' .
807 '<div id="mw-diff-otitle3">' .
808 $sk->revComment( $rev ) . '<br/>' .
813 * Show a deleted file version requested by the visitor.
815 function showFile( $key ) {
816 global $wgOut, $wgRequest;
819 # We mustn't allow the output to be Squid cached, otherwise
820 # if an admin previews a deleted image, and it's cached, then
821 # a user without appropriate permissions can toddle off and
822 # nab the image, and Squid will serve it
823 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
824 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
825 $wgRequest->response()->header( 'Pragma: no-cache' );
827 $store = FileStore
::get( 'deleted' );
828 $store->stream( $key );
831 /* private */ function showHistory() {
832 global $wgLang, $wgContLang, $wgUser, $wgOut;
834 $sk = $wgUser->getSkin();
835 if ( $this->mAllowed
) {
836 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
838 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
841 $archive = new PageArchive( $this->mTargetObj
);
843 $text = $archive->getLastRevisionText();
844 if( is_null( $text ) ) {
845 $wgOut->addWikiMsg( "nohistory" );
849 if ( $this->mAllowed
) {
850 $wgOut->addWikiMsg( "undeletehistory" );
852 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
855 # List all stored revisions
856 $revisions = $archive->listRevisions();
857 $files = $archive->listFiles();
859 $haveRevisions = $revisions && $revisions->numRows() > 0;
860 $haveFiles = $files && $files->numRows() > 0;
862 # Batch existence check on user and talk pages
863 if( $haveRevisions ) {
864 $batch = new LinkBatch();
865 while( $row = $revisions->fetchObject() ) {
866 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
867 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
870 $revisions->seek( 0 );
873 $batch = new LinkBatch();
874 while( $row = $files->fetchObject() ) {
875 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
876 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
882 if ( $this->mAllowed
) {
883 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
884 $action = $titleObj->getLocalURL( "action=submit" );
885 # Start the form here
886 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
887 $wgOut->addHtml( $top );
890 # Show relevant lines from the deletion log:
891 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
892 $logViewer = new LogViewer(
896 'page' => $this->mTargetObj
->getPrefixedText(),
900 ), LogViewer
::NO_ACTION_LINK
902 $logViewer->showList( $wgOut );
904 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
905 # Format the user-visible controls (comment field, submission button)
906 # in a nice little table
907 $align = $wgContLang->isRtl() ?
'left' : 'right';
909 Xml
::openElement( 'fieldset' ) .
910 Xml
::element( 'legend', null, wfMsg( 'undelete') ).
911 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
914 wfMsgWikiHtml( 'undeleteextrahelp' ) .
918 <td align='$align'>" .
919 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
922 Xml
::input( 'wpComment', 50, $this->mComment
) .
928 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
929 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
932 Xml
::closeElement( 'table' ) .
933 Xml
::closeElement( 'fieldset' );
935 $wgOut->addHtml( $table );
938 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
940 if( $haveRevisions ) {
941 # The page's stored (deleted) history:
942 $wgOut->addHTML("<ul>");
943 $target = urlencode( $this->mTarget
);
944 $remaining = $revisions->numRows();
945 $earliestLiveTime = $this->getEarliestTime( $this->mTargetObj
);
947 while( $row = $revisions->fetchObject() ) {
949 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
950 if ( $this->mAllowed
) {
951 $checkBox = Xml
::check( "ts$ts" );
952 $pageLink = $sk->makeKnownLinkObj( $titleObj,
953 $wgLang->timeanddate( $ts, true ),
954 "target=$target×tamp=$ts" );
955 if( ($remaining > 0) ||
956 ($earliestLiveTime && $ts > $earliestLiveTime ) ) {
958 $sk->makeKnownLinkObj( $titleObj,
960 "target=$target×tamp=$ts&diff=prev" ) .
963 // No older revision to diff against
968 $pageLink = $wgLang->timeanddate( $ts, true );
971 $userLink = $sk->userLink( $row->ar_user
, $row->ar_user_text
) . $sk->userToolLinks( $row->ar_user
, $row->ar_user_text
);
973 if (!is_null($size = $row->ar_len
)) {
975 $stxt = wfMsgHtml('historyempty');
977 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
980 $comment = $sk->commentBlock( $row->ar_comment
);
981 $wgOut->addHTML( "<li>$checkBox $pageLink $diffLink . . $userLink $stxt $comment</li>\n" );
985 $wgOut->addHTML("</ul>");
987 $wgOut->addWikiMsg( "nohistory" );
991 $wgOut->addHtml( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
992 $wgOut->addHtml( "<ul>" );
993 while( $row = $files->fetchObject() ) {
994 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
995 if ( $this->mAllowed
&& $row->fa_storage_key
) {
996 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
997 $key = urlencode( $row->fa_storage_key
);
998 $target = urlencode( $this->mTarget
);
999 $pageLink = $sk->makeKnownLinkObj( $titleObj,
1000 $wgLang->timeanddate( $ts, true ),
1001 "target=$target&file=$key" );
1004 $pageLink = $wgLang->timeanddate( $ts, true );
1006 $userLink = $sk->userLink( $row->fa_user
, $row->fa_user_text
) . $sk->userToolLinks( $row->fa_user
, $row->fa_user_text
);
1008 wfMsgHtml( 'widthheight',
1009 $wgLang->formatNum( $row->fa_width
),
1010 $wgLang->formatNum( $row->fa_height
) ) .
1012 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1014 $comment = $sk->commentBlock( $row->fa_description
);
1015 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
1018 $wgOut->addHTML( "</ul>" );
1021 if ( $this->mAllowed
) {
1022 # Slip in the hidden controls here
1023 $misc = Xml
::hidden( 'target', $this->mTarget
);
1024 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1025 $misc .= Xml
::closeElement( 'form' );
1026 $wgOut->addHtml( $misc );
1032 private function getEarliestTime( $title ) {
1033 $dbr = wfGetDB( DB_SLAVE
);
1034 if( $title->exists() ) {
1035 $min = $dbr->selectField( 'revision',
1036 'MIN(rev_timestamp)',
1037 array( 'rev_page' => $title->getArticleId() ),
1039 return wfTimestampOrNull( TS_MW
, $min );
1044 function undelete() {
1045 global $wgOut, $wgUser;
1046 if ( wfReadOnly() ) {
1047 $wgOut->readOnlyPage();
1050 if( !is_null( $this->mTargetObj
) ) {
1051 $archive = new PageArchive( $this->mTargetObj
);
1053 $ok = $archive->undelete(
1054 $this->mTargetTimestamp
,
1056 $this->mFileVersions
);
1058 if( is_array($ok) ) {
1059 if ( $ok[1] ) // Undeleted file count
1060 wfRunHooks( 'FileUndeleteComplete', array(
1061 $this->mTargetObj
, $this->mFileVersions
,
1062 $wgUser, $this->mComment
) );
1064 $skin = $wgUser->getSkin();
1065 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
1066 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1068 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1071 // Show file deletion warnings and errors
1072 $status = $archive->getFileStatus();
1073 if ( $status && !$status->isGood() ) {
1074 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1077 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );