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( $startTime, $limit ) {
101 $whereClause = array( 'ar_namespace' => $this->title
->getNamespace(),
102 'ar_title' => $this->title
->getDBkey() );
104 $whereClause[] = "ar_timestamp < $startTime";
106 $dbr = wfGetDB( DB_SLAVE
);
107 $res = $dbr->select( 'archive',
108 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len' ),
110 'PageArchive::listRevisions',
111 array( 'ORDER BY' => 'ar_timestamp DESC',
112 'LIMIT' => intval($limit) ) );
113 $ret = $dbr->resultObject( $res );
118 * List the deleted file revisions for this page, if it's a file page.
119 * Returns a result wrapper with various filearchive fields, or null
120 * if not a file page.
122 * @return ResultWrapper
123 * @todo Does this belong in Image for fuller encapsulation?
125 function listFiles() {
126 if( $this->title
->getNamespace() == NS_IMAGE
) {
127 $dbr = wfGetDB( DB_SLAVE
);
128 $res = $dbr->select( 'filearchive',
140 array( 'fa_name' => $this->title
->getDbKey() ),
142 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
143 $ret = $dbr->resultObject( $res );
150 * Fetch (and decompress if necessary) the stored text for the deleted
151 * revision of the page with the given timestamp.
154 * @deprecated Use getRevision() for more flexible information
156 function getRevisionText( $timestamp ) {
157 $rev = $this->getRevision( $timestamp );
158 return $rev ?
$rev->getText() : null;
162 * Return a Revision object containing data for the deleted revision.
163 * Note that the result *may* or *may not* have a null page ID.
164 * @param string $timestamp
167 function getRevision( $timestamp ) {
168 $dbr = wfGetDB( DB_SLAVE
);
169 $row = $dbr->selectRow( 'archive',
181 array( 'ar_namespace' => $this->title
->getNamespace(),
182 'ar_title' => $this->title
->getDbkey(),
183 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
186 return new Revision( array(
187 'page' => $this->title
->getArticleId(),
188 'id' => $row->ar_rev_id
,
189 'text' => ($row->ar_text_id
191 : Revision
::getRevisionText( $row, 'ar_' ) ),
192 'comment' => $row->ar_comment
,
193 'user' => $row->ar_user
,
194 'user_text' => $row->ar_user_text
,
195 'timestamp' => $row->ar_timestamp
,
196 'minor_edit' => $row->ar_minor_edit
,
197 'text_id' => $row->ar_text_id
) );
204 * Return the most-previous revision, either live or deleted, against
205 * the deleted revision given by timestamp.
207 * May produce unexpected results in case of history merges or other
208 * unusual time issues.
210 * @param string $timestamp
211 * @return Revision or null
213 function getPreviousRevision( $timestamp ) {
214 $dbr = wfGetDB( DB_SLAVE
);
216 // Check the previous deleted revision...
217 $row = $dbr->selectRow( 'archive',
219 array( 'ar_namespace' => $this->title
->getNamespace(),
220 'ar_title' => $this->title
->getDbkey(),
222 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
225 'ORDER BY' => 'ar_timestamp DESC',
227 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
229 $row = $dbr->selectRow( array( 'page', 'revision' ),
230 array( 'rev_id', 'rev_timestamp' ),
232 'page_namespace' => $this->title
->getNamespace(),
233 'page_title' => $this->title
->getDbkey(),
234 'page_id = rev_page',
236 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
239 'ORDER BY' => 'rev_timestamp DESC',
241 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
242 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
244 if( $prevLive && $prevLive > $prevDeleted ) {
245 // Most prior revision was live
246 return Revision
::newFromId( $prevLiveId );
247 } elseif( $prevDeleted ) {
248 // Most prior revision was deleted
249 return $this->getRevision( $prevDeleted );
251 // No prior revision on this page.
257 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
259 function getTextFromRow( $row ) {
260 if( is_null( $row->ar_text_id
) ) {
261 // An old row from MediaWiki 1.4 or previous.
262 // Text is embedded in this row in classic compression format.
263 return Revision
::getRevisionText( $row, "ar_" );
265 // New-style: keyed to the text storage backend.
266 $dbr = wfGetDB( DB_SLAVE
);
267 $text = $dbr->selectRow( 'text',
268 array( 'old_text', 'old_flags' ),
269 array( 'old_id' => $row->ar_text_id
),
271 return Revision
::getRevisionText( $text );
277 * Fetch (and decompress if necessary) the stored text of the most
278 * recently edited deleted revision of the page.
280 * If there are no archived revisions for the page, returns NULL.
284 function getLastRevisionText() {
285 $dbr = wfGetDB( DB_SLAVE
);
286 $row = $dbr->selectRow( 'archive',
287 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
288 array( 'ar_namespace' => $this->title
->getNamespace(),
289 'ar_title' => $this->title
->getDBkey() ),
290 'PageArchive::getLastRevisionText',
291 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
293 return $this->getTextFromRow( $row );
300 * Quick check if any archived revisions are present for the page.
303 function isDeleted() {
304 $dbr = wfGetDB( DB_SLAVE
);
305 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
306 array( 'ar_namespace' => $this->title
->getNamespace(),
307 'ar_title' => $this->title
->getDBkey() ) );
311 const UNDELETE_NOTHINGRESTORED
= 0; // No revisions could be restored
312 const UNDELETE_NOTAVAIL
= -1; // Not all requested revisions are available
313 const UNDELETE_UNKNOWNERR
= -2; // Unknown error
315 * Restore the given (or all) text and file revisions for the page.
316 * Once restored, the items will be removed from the archive tables.
317 * The deletion log will be updated with an undeletion notice.
319 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
320 * @param string $comment
321 * @param array $fileVersions
323 * @return array(number of revisions restored, number of file versions restored, log reason) on success or UNDELETE_* on failure
325 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
326 // If both the set of text revisions and file revisions are empty,
327 // restore everything. Otherwise, just restore the requested items.
328 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
330 $restoreText = $restoreAll ||
!empty( $timestamps );
331 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
333 if( $restoreFiles && $this->title
->getNamespace() == NS_IMAGE
) {
334 $img = wfLocalFile( $this->title
);
335 $this->fileStatus
= $img->restore( $fileVersions );
336 $filesRestored = $this->fileStatus
->successCount
;
342 $textRestored = $this->undeleteRevisions( $timestamps );
343 if($textRestored < 0) // It must be one of UNDELETE_*
344 return $textRestored;
351 $log = new LogPage( 'delete' );
353 if( $textRestored && $filesRestored ) {
354 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
355 $wgContLang->formatNum( $textRestored ),
356 $wgContLang->formatNum( $filesRestored ) );
357 } elseif( $textRestored ) {
358 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
359 $wgContLang->formatNum( $textRestored ) );
360 } elseif( $filesRestored ) {
361 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
362 $wgContLang->formatNum( $filesRestored ) );
364 wfDebug( "Undelete: nothing undeleted...\n" );
365 return self
::UNDELETE_NOTHINGRESTORED
;
368 if( trim( $comment ) != '' )
369 $reason .= ": {$comment}";
370 $log->addEntry( 'restore', $this->title
, $reason );
372 return array($textRestored, $filesRestored, $reason);
376 * This is the meaty bit -- restores archived revisions of the given page
377 * to the cur/old tables. If the page currently exists, all revisions will
378 * be stuffed into old, otherwise the most recent will go into cur.
380 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
381 * @param string $comment
382 * @param array $fileVersions
384 * @return int number of revisions restored on success or UNDELETE_* on failure
386 private function undeleteRevisions( $timestamps ) {
387 if ( wfReadOnly() ) return 0;
389 $restoreAll = empty( $timestamps );
391 $dbw = wfGetDB( DB_MASTER
);
393 # Does this page already exist? We'll have to update it...
394 $article = new Article( $this->title
);
395 $options = 'FOR UPDATE';
396 $page = $dbw->selectRow( 'page',
397 array( 'page_id', 'page_latest' ),
398 array( 'page_namespace' => $this->title
->getNamespace(),
399 'page_title' => $this->title
->getDBkey() ),
403 # Page already exists. Import the history, and if necessary
404 # we'll update the latest revision field in the record.
406 $pageId = $page->page_id
;
407 $previousRevId = $page->page_latest
;
409 # Have to create a new article...
410 $newid = $article->insertOn( $dbw );
416 $oldones = '1 = 1'; # All revisions...
418 $oldts = implode( ',',
419 array_map( array( &$dbw, 'addQuotes' ),
420 array_map( array( &$dbw, 'timestamp' ),
423 $oldones = "ar_timestamp IN ( {$oldts} )";
427 * Restore each revision...
429 $result = $dbw->select( 'archive',
442 'ar_namespace' => $this->title
->getNamespace(),
443 'ar_title' => $this->title
->getDBkey(),
447 'ORDER BY' => 'ar_timestamp' )
449 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
450 wfDebug( __METHOD__
.": couldn't find all requested rows\n" );
451 return self
::UNDELETE_NOTAVAIL
;
457 while( $row = $dbw->fetchObject( $result ) ) {
458 if( $row->ar_text_id
) {
459 // Revision was deleted in 1.5+; text is in
460 // the regular text table, use the reference.
461 // Specify null here so the so the text is
462 // dereferenced for page length info if needed.
465 // Revision was deleted in 1.4 or earlier.
466 // Text is squashed into the archive row, and
467 // a new text table entry will be created for it.
468 $revText = Revision
::getRevisionText( $row, 'ar_' );
470 $revision = new Revision( array(
472 'id' => $row->ar_rev_id
,
474 'comment' => $row->ar_comment
,
475 'user' => $row->ar_user
,
476 'user_text' => $row->ar_user_text
,
477 'timestamp' => $row->ar_timestamp
,
478 'minor_edit' => $row->ar_minor_edit
,
479 'text_id' => $row->ar_text_id
,
480 'len' => $row->ar_len
482 $revision->insertOn( $dbw );
485 // Was anything restored at all?
490 // Attach the latest revision to the page...
491 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
493 if( $newid ||
$wasnew ) {
494 // Update site stats, link tables, etc
495 $article->createUpdates( $revision );
499 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true ) );
500 Article
::onArticleCreate( $this->title
);
502 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false ) );
503 Article
::onArticleEdit( $this->title
);
506 // Revision couldn't be created. This is very weird
507 return self
::UNDELETE_UNKNOWNERR
;
510 # Now that it's safely stored, take it out of the archive
511 $dbw->delete( 'archive',
513 'ar_namespace' => $this->title
->getNamespace(),
514 'ar_title' => $this->title
->getDBkey(),
521 function getFileStatus() { return $this->fileStatus
; }
525 * The HTML form for Special:Undelete, which allows users with the appropriate
526 * permissions to view and restore deleted content.
527 * @addtogroup SpecialPage
530 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
531 var $mTargetTimestamp, $mAllowed, $mComment;
533 function UndeleteForm( $request, $par = "" ) {
535 $this->mAction
= $request->getVal( 'action' );
536 $this->mTarget
= $request->getVal( 'target' );
537 $this->mSearchPrefix
= $request->getText( 'prefix' );
538 $time = $request->getVal( 'timestamp' );
539 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
540 $this->mFile
= $request->getVal( 'file' );
542 $posted = $request->wasPosted() &&
543 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
544 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
545 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
546 $this->mDiff
= $request->getCheck( 'diff' );
547 $this->mComment
= $request->getText( 'wpComment' );
550 $this->mTarget
= $par;
552 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
553 $this->mAllowed
= true;
555 $this->mAllowed
= false;
556 $this->mTimestamp
= '';
557 $this->mRestore
= false;
559 if ( $this->mTarget
!== "" ) {
560 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
562 $this->mTargetObj
= NULL;
564 if( $this->mRestore
) {
565 $timestamps = array();
566 $this->mFileVersions
= array();
567 foreach( $_REQUEST as $key => $val ) {
569 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
570 array_push( $timestamps, $matches[1] );
573 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
574 $this->mFileVersions
[] = intval( $matches[1] );
577 rsort( $timestamps );
578 $this->mTargetTimestamp
= $timestamps;
584 if ( $this->mAllowed
) {
585 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
587 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
590 if( is_null( $this->mTargetObj
) ) {
591 $this->showSearchForm();
593 # List undeletable articles
594 if( $this->mSearchPrefix
) {
595 $result = PageArchive
::listPagesByPrefix(
596 $this->mSearchPrefix
);
597 $this->showList( $result );
601 if( $this->mTimestamp
!== '' ) {
602 return $this->showRevision( $this->mTimestamp
);
604 if( $this->mFile
!== null ) {
605 return $this->showFile( $this->mFile
);
607 if( $this->mRestore
&& $this->mAction
== "submit" ) {
608 return $this->undelete();
610 return $this->showHistory();
613 function showSearchForm() {
614 global $wgOut, $wgScript;
615 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
618 Xml
::openElement( 'form', array(
620 'action' => $wgScript ) ) .
622 Xml
::element( 'legend', array(),
623 wfMsg( 'undelete-search-box' ) ) .
624 Xml
::hidden( 'title',
625 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
626 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
627 'prefix', 'prefix', 20,
628 $this->mSearchPrefix
) .
629 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
634 /* private */ function showList( $result ) {
635 global $wgLang, $wgContLang, $wgUser, $wgOut;
637 if( $result->numRows() == 0 ) {
638 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
642 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
644 $sk = $wgUser->getSkin();
645 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
646 $wgOut->addHTML( "<ul>\n" );
647 while( $row = $result->fetchObject() ) {
648 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
649 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
650 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
651 $revs = wfMsgExt( 'undeleterevisions',
652 array( 'parseinline' ),
653 $wgLang->formatNum( $row->count
) );
654 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
657 $wgOut->addHTML( "</ul>\n" );
662 /* private */ function showRevision( $timestamp ) {
663 global $wgLang, $wgUser, $wgOut;
664 $self = SpecialPage
::getTitleFor( 'Undelete' );
665 $skin = $wgUser->getSkin();
667 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
669 $archive = new PageArchive( $this->mTargetObj
);
670 $rev = $archive->getRevision( $timestamp );
673 $wgOut->addWikiTexT( wfMsg( 'undeleterevision-missing' ) );
677 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
679 $link = $skin->makeKnownLinkObj(
680 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
681 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
683 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
684 $user = $skin->userLink( $rev->getUser(), $rev->getUserText() )
685 . $skin->userToolLinks( $rev->getUser(), $rev->getUserText() );
688 $previousRev = $archive->getPreviousRevision( $timestamp );
690 $this->showDiff( $previousRev, $rev );
691 if( $wgUser->getOption( 'diffonly' ) ) {
694 $wgOut->addHtml( '<hr />' );
697 $wgOut->addHtml( wfMsgHtml( 'undelete-nodiff' ) );
701 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user ) . '</p>' );
703 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
705 if( $this->mPreview
) {
706 $wgOut->addHtml( "<hr />\n" );
707 $wgOut->addWikiTextTitleTidy( $rev->getText(), $this->mTargetObj
, false );
711 wfElement( 'textarea', array(
712 'readonly' => 'readonly',
713 'cols' => intval( $wgUser->getOption( 'cols' ) ),
714 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
715 $rev->getText() . "\n" ) .
716 wfOpenElement( 'div' ) .
717 wfOpenElement( 'form', array(
719 'action' => $self->getLocalURL( "action=submit" ) ) ) .
720 wfElement( 'input', array(
723 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
724 wfElement( 'input', array(
726 'name' => 'timestamp',
727 'value' => $timestamp ) ) .
728 wfElement( 'input', array(
730 'name' => 'wpEditToken',
731 'value' => $wgUser->editToken() ) ) .
732 wfElement( 'input', array(
735 'value' => wfMsg( 'showpreview' ) ) ) .
736 wfElement( 'input', array(
739 'value' => wfMsg( 'showdiff' ) ) ) .
740 wfCloseElement( 'form' ) .
741 wfCloseElement( 'div' ) );
745 * Build a diff display between this and the previous either deleted
746 * or non-deleted edit.
747 * @param Revision $previousRev
748 * @param Revision $currentRev
749 * @return string HTML
751 function showDiff( $previousRev, $currentRev ) {
752 global $wgOut, $wgUser;
754 $diffEngine = new DifferenceEngine();
755 $diffEngine->showDiffStyle();
758 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
759 "<col class='diff-marker' />" .
760 "<col class='diff-content' />" .
761 "<col class='diff-marker' />" .
762 "<col class='diff-content' />" .
764 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
765 $this->diffHeader( $previousRev ) .
767 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
768 $this->diffHeader( $currentRev ) .
771 $diffEngine->generateDiffBody(
772 $previousRev->getText(), $currentRev->getText() ) .
778 private function diffHeader( $rev ) {
779 global $wgUser, $wgLang, $wgLang;
780 $sk = $wgUser->getSkin();
781 $isDeleted = !( $rev->getId() && $rev->getTitle() );
783 /// @fixme $rev->getTitle() is null for deleted revs...?
784 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
785 $targetQuery = 'target=' .
786 $this->mTargetObj
->getPrefixedUrl() .
788 wfTimestamp( TS_MW
, $rev->getTimestamp() );
790 /// @fixme getId() may return non-zero for deleted revs...
791 $targetPage = $rev->getTitle();
792 $targetQuery = 'oldid=' . $rev->getId();
795 '<div id="mw-diff-otitle1"><strong>' .
796 $sk->makeLinkObj( $targetPage,
797 wfMsgHtml( 'revisionasof',
798 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
800 ( $isDeleted ?
' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
802 '<div id="mw-diff-otitle2">' .
803 $sk->revUserTools( $rev ) . '<br/>' .
805 '<div id="mw-diff-otitle3">' .
806 $sk->revComment( $rev ) . '<br/>' .
811 * Show a deleted file version requested by the visitor.
813 function showFile( $key ) {
814 global $wgOut, $wgRequest;
817 # We mustn't allow the output to be Squid cached, otherwise
818 # if an admin previews a deleted image, and it's cached, then
819 # a user without appropriate permissions can toddle off and
820 # nab the image, and Squid will serve it
821 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
822 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
823 $wgRequest->response()->header( 'Pragma: no-cache' );
825 $store = FileStore
::get( 'deleted' );
826 $store->stream( $key );
829 /* private */ function showHistory() {
830 global $wgLang, $wgContLang, $wgUser, $wgOut, $wgRequest;
832 $sk = $wgUser->getSkin();
833 if ( $this->mAllowed
) {
834 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
836 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
839 $archive = new PageArchive( $this->mTargetObj
);
841 $text = $archive->getLastRevisionText();
842 if( is_null( $text ) ) {
843 $wgOut->addWikiText( wfMsg( "nohistory" ) );
847 if ( $this->mAllowed
) {
848 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
850 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
853 # List all stored revisions
854 $tmpLimit = $wgRequest->getIntOrNull ( 'limit' );
855 $tmpLimit = (is_null($tmpLimit))?
5001 : $tmpLimit +
1;
856 $revisions = $archive->listRevisions( $wgRequest->getVal ( 'offset' ),
858 $files = $archive->listFiles();
860 $haveRevisions = $revisions && $revisions->numRows() > 0;
861 $haveFiles = $files && $files->numRows() > 0;
864 if ( $revisions && $revisions->numRows() >= $tmpLimit ) {
865 $revisions->seek ( $revisions->numRows() - 2 );
866 $tmp = $revisions->fetchObject();
867 $revisions->rewind ( );
869 $titleObj = SpecialPage
::getTitleFor ( 'Undelete' );
870 $tmplink = $sk->makeKnownLinkObj ( $titleObj, wfMsg( 'undelete-next-revs', 5000 ),
871 "target={$this->mTarget}&limit=5000&offset={$tmp->ar_timestamp}" );
873 $wgOut->addHTML ( wfMsg ( 'undelete-more-revs', $tmpLimit - 1, $tmplink ) );
876 # Batch existence check on user and talk pages
877 if( $haveRevisions ) {
878 $batch = new LinkBatch();
879 while( $row = $revisions->fetchObject() ) {
880 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
881 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
884 $revisions->seek( 0 );
887 $batch = new LinkBatch();
888 while( $row = $files->fetchObject() ) {
889 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
890 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
896 if ( $this->mAllowed
) {
897 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
898 $action = $titleObj->getLocalURL( "action=submit" );
899 # Start the form here
900 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
901 $wgOut->addHtml( $top );
904 # Show relevant lines from the deletion log:
905 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
906 $logViewer = new LogViewer(
910 'page' => $this->mTargetObj
->getPrefixedText(),
914 ), LogViewer
::NO_ACTION_LINK
916 $logViewer->showList( $wgOut );
918 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
919 # Format the user-visible controls (comment field, submission button)
920 # in a nice little table
921 $align = $wgContLang->isRtl() ?
'left' : 'right';
923 Xml
::openElement( 'fieldset' ) .
924 Xml
::openElement( 'table' ) .
927 wfMsgWikiHtml( 'undeleteextrahelp' ) .
931 <td align='$align'>" .
932 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
935 Xml
::input( 'wpComment', 50, $this->mComment
) .
941 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
942 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
945 Xml
::closeElement( 'table' ) .
946 Xml
::closeElement( 'fieldset' );
948 $wgOut->addHtml( $table );
951 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
953 if( $haveRevisions ) {
954 # The page's stored (deleted) history:
955 $wgOut->addHTML("<ul>");
956 $target = urlencode( $this->mTarget
);
957 $remaining = $revisions->numRows();
958 $earliestLiveTime = $this->getEarliestTime( $this->mTargetObj
);
960 if ( $hasMore ) $remaining --;
962 while( ( $row = $revisions->fetchObject() ) && $remaining-- ) {
963 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
964 if ( $this->mAllowed
) {
965 $checkBox = Xml
::check( "ts$ts" );
966 $pageLink = $sk->makeKnownLinkObj( $titleObj,
967 $wgLang->timeanddate( $ts, true ),
968 "target=$target×tamp=$ts" );
969 if( ($remaining > 0 ||
$hasMore ) ||
970 ($earliestLiveTime && $ts > $earliestLiveTime ) ) {
972 $sk->makeKnownLinkObj( $titleObj,
974 "target=$target×tamp=$ts&diff=prev" ) .
977 // No older revision to diff against
982 $pageLink = $wgLang->timeanddate( $ts, true );
985 $userLink = $sk->userLink( $row->ar_user
, $row->ar_user_text
) . $sk->userToolLinks( $row->ar_user
, $row->ar_user_text
);
987 if (!is_null($size = $row->ar_len
)) {
989 $stxt = wfMsgHtml('historyempty');
991 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
994 $comment = $sk->commentBlock( $row->ar_comment
);
995 $wgOut->addHTML( "<li>$checkBox $pageLink $diffLink . . $userLink $stxt $comment</li>\n" );
999 $wgOut->addHTML("</ul>");
1001 $wgOut->addWikiText( wfMsg( "nohistory" ) );
1005 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'filehist' ) . "</h2>\n" );
1006 $wgOut->addHtml( "<ul>" );
1007 while( $row = $files->fetchObject() ) {
1008 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1009 if ( $this->mAllowed
&& $row->fa_storage_key
) {
1010 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1011 $key = urlencode( $row->fa_storage_key
);
1012 $target = urlencode( $this->mTarget
);
1013 $pageLink = $sk->makeKnownLinkObj( $titleObj,
1014 $wgLang->timeanddate( $ts, true ),
1015 "target=$target&file=$key" );
1018 $pageLink = $wgLang->timeanddate( $ts, true );
1020 $userLink = $sk->userLink( $row->fa_user
, $row->fa_user_text
) . $sk->userToolLinks( $row->fa_user
, $row->fa_user_text
);
1022 wfMsgHtml( 'widthheight',
1023 $wgLang->formatNum( $row->fa_width
),
1024 $wgLang->formatNum( $row->fa_height
) ) .
1026 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1028 $comment = $sk->commentBlock( $row->fa_description
);
1029 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
1032 $wgOut->addHTML( "</ul>" );
1035 if ( $this->mAllowed
) {
1036 # Slip in the hidden controls here
1037 $misc = Xml
::hidden( 'target', $this->mTarget
);
1038 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1039 $misc .= Xml
::closeElement( 'form' );
1040 $wgOut->addHtml( $misc );
1046 private function getEarliestTime( $title ) {
1047 $dbr = wfGetDB( DB_SLAVE
);
1048 if( $title->exists() ) {
1049 $min = $dbr->selectField( 'revision',
1050 'MIN(rev_timestamp)',
1051 array( 'rev_page' => $title->getArticleId() ),
1053 return wfTimestampOrNull( TS_MW
, $min );
1058 function undelete() {
1059 global $wgOut, $wgUser;
1060 if( !is_null( $this->mTargetObj
) ) {
1061 $archive = new PageArchive( $this->mTargetObj
);
1063 $ok = $archive->undelete(
1064 $this->mTargetTimestamp
,
1066 $this->mFileVersions
);
1068 if( is_array($ok) ) {
1069 $skin = $wgUser->getSkin();
1070 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
1071 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1073 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1076 // Show file deletion warnings and errors
1077 $status = $archive->getFileStatus();
1078 if ( $status && !$status->isGood() ) {
1079 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1082 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );