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() ) );
306 const UNDELETE_NOTHINGRESTORED
= 0; // No revisions could be restored
307 const UNDELETE_NOTAVAIL
= -1; // Not all requested revisions are available
308 const UNDELETE_UNKNOWNERR
= -2; // Unknown error
310 * Restore the given (or all) text and file revisions for the page.
311 * Once restored, the items will be removed from the archive tables.
312 * The deletion log will be updated with an undeletion notice.
314 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
315 * @param string $comment
316 * @param array $fileVersions
318 * @return array(number of revisions restored, number of file versions restored, log reason) on success or UNDELETE_* on failure
320 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
321 // If both the set of text revisions and file revisions are empty,
322 // restore everything. Otherwise, just restore the requested items.
323 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
325 $restoreText = $restoreAll ||
!empty( $timestamps );
326 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
328 if( $restoreFiles && $this->title
->getNamespace() == NS_IMAGE
) {
329 $img = wfLocalFile( $this->title
);
330 $this->fileStatus
= $img->restore( $fileVersions );
331 $filesRestored = $this->fileStatus
->successCount
;
337 $textRestored = $this->undeleteRevisions( $timestamps );
338 if($textRestored < 0) // It must be one of UNDELETE_*
339 return $textRestored;
346 $log = new LogPage( 'delete' );
348 if( $textRestored && $filesRestored ) {
349 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
350 $wgContLang->formatNum( $textRestored ),
351 $wgContLang->formatNum( $filesRestored ) );
352 } elseif( $textRestored ) {
353 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
354 $wgContLang->formatNum( $textRestored ) );
355 } elseif( $filesRestored ) {
356 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
357 $wgContLang->formatNum( $filesRestored ) );
359 wfDebug( "Undelete: nothing undeleted...\n" );
360 return self
::UNDELETE_NOTHINGRESTORED
;
363 if( trim( $comment ) != '' )
364 $reason .= ": {$comment}";
365 $log->addEntry( 'restore', $this->title
, $reason );
367 return array($textRestored, $filesRestored, $reason);
371 * This is the meaty bit -- restores archived revisions of the given page
372 * to the cur/old tables. If the page currently exists, all revisions will
373 * be stuffed into old, otherwise the most recent will go into cur.
375 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
376 * @param string $comment
377 * @param array $fileVersions
379 * @return int number of revisions restored on success or UNDELETE_* on failure
381 private function undeleteRevisions( $timestamps ) {
382 if ( wfReadOnly() ) return 0;
384 $restoreAll = empty( $timestamps );
386 $dbw = wfGetDB( DB_MASTER
);
388 # Does this page already exist? We'll have to update it...
389 $article = new Article( $this->title
);
390 $options = 'FOR UPDATE';
391 $page = $dbw->selectRow( 'page',
392 array( 'page_id', 'page_latest' ),
393 array( 'page_namespace' => $this->title
->getNamespace(),
394 'page_title' => $this->title
->getDBkey() ),
398 # Page already exists. Import the history, and if necessary
399 # we'll update the latest revision field in the record.
401 $pageId = $page->page_id
;
402 $previousRevId = $page->page_latest
;
404 # Have to create a new article...
405 $newid = $article->insertOn( $dbw );
411 $oldones = '1 = 1'; # All revisions...
413 $oldts = implode( ',',
414 array_map( array( &$dbw, 'addQuotes' ),
415 array_map( array( &$dbw, 'timestamp' ),
418 $oldones = "ar_timestamp IN ( {$oldts} )";
422 * Restore each revision...
424 $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" );
446 return self
::UNDELETE_NOTAVAIL
;
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 // Was anything restored at all?
485 // Attach the latest revision to the page...
486 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
488 if( $newid ||
$wasnew ) {
489 // Update site stats, link tables, etc
490 $article->createUpdates( $revision );
494 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true ) );
495 Article
::onArticleCreate( $this->title
);
497 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false ) );
498 Article
::onArticleEdit( $this->title
);
501 // Revision couldn't be created. This is very weird
502 return self
::UNDELETE_UNKNOWNERR
;
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(),
516 function getFileStatus() { return $this->fileStatus
; }
520 * The HTML form for Special:Undelete, which allows users with the appropriate
521 * permissions to view and restore deleted content.
522 * @addtogroup SpecialPage
525 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
526 var $mTargetTimestamp, $mAllowed, $mComment;
528 function UndeleteForm( $request, $par = "" ) {
530 $this->mAction
= $request->getVal( 'action' );
531 $this->mTarget
= $request->getVal( 'target' );
532 $this->mSearchPrefix
= $request->getText( 'prefix' );
533 $time = $request->getVal( 'timestamp' );
534 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
535 $this->mFile
= $request->getVal( 'file' );
537 $posted = $request->wasPosted() &&
538 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
539 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
540 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
541 $this->mDiff
= $request->getCheck( 'diff' );
542 $this->mComment
= $request->getText( 'wpComment' );
545 $this->mTarget
= $par;
547 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
548 $this->mAllowed
= true;
550 $this->mAllowed
= false;
551 $this->mTimestamp
= '';
552 $this->mRestore
= false;
554 if ( $this->mTarget
!== "" ) {
555 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
557 $this->mTargetObj
= NULL;
559 if( $this->mRestore
) {
560 $timestamps = array();
561 $this->mFileVersions
= array();
562 foreach( $_REQUEST as $key => $val ) {
564 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
565 array_push( $timestamps, $matches[1] );
568 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
569 $this->mFileVersions
[] = intval( $matches[1] );
572 rsort( $timestamps );
573 $this->mTargetTimestamp
= $timestamps;
579 if ( $this->mAllowed
) {
580 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
582 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
585 if( is_null( $this->mTargetObj
) ) {
586 $this->showSearchForm();
588 # List undeletable articles
589 if( $this->mSearchPrefix
) {
590 $result = PageArchive
::listPagesByPrefix(
591 $this->mSearchPrefix
);
592 $this->showList( $result );
596 if( $this->mTimestamp
!== '' ) {
597 return $this->showRevision( $this->mTimestamp
);
599 if( $this->mFile
!== null ) {
600 return $this->showFile( $this->mFile
);
602 if( $this->mRestore
&& $this->mAction
== "submit" ) {
603 return $this->undelete();
605 return $this->showHistory();
608 function showSearchForm() {
609 global $wgOut, $wgScript;
610 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
613 Xml
::openElement( 'form', array(
615 'action' => $wgScript ) ) .
617 Xml
::element( 'legend', array(),
618 wfMsg( 'undelete-search-box' ) ) .
619 Xml
::hidden( 'title',
620 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
621 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
622 'prefix', 'prefix', 20,
623 $this->mSearchPrefix
) .
624 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
629 /* private */ function showList( $result ) {
630 global $wgLang, $wgContLang, $wgUser, $wgOut;
632 if( $result->numRows() == 0 ) {
633 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
637 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
639 $sk = $wgUser->getSkin();
640 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
641 $wgOut->addHTML( "<ul>\n" );
642 while( $row = $result->fetchObject() ) {
643 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
644 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
645 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
646 $revs = wfMsgExt( 'undeleterevisions',
647 array( 'parseinline' ),
648 $wgLang->formatNum( $row->count
) );
649 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
652 $wgOut->addHTML( "</ul>\n" );
657 /* private */ function showRevision( $timestamp ) {
658 global $wgLang, $wgUser, $wgOut;
659 $self = SpecialPage
::getTitleFor( 'Undelete' );
660 $skin = $wgUser->getSkin();
662 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
664 $archive = new PageArchive( $this->mTargetObj
);
665 $rev = $archive->getRevision( $timestamp );
668 $wgOut->addWikiTexT( wfMsg( 'undeleterevision-missing' ) );
672 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
674 $link = $skin->makeKnownLinkObj(
675 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
676 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
678 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
679 $user = $skin->userLink( $rev->getUser(), $rev->getUserText() )
680 . $skin->userToolLinks( $rev->getUser(), $rev->getUserText() );
683 $previousRev = $archive->getPreviousRevision( $timestamp );
685 $this->showDiff( $previousRev, $rev );
686 if( $wgUser->getOption( 'diffonly' ) ) {
689 $wgOut->addHtml( '<hr />' );
692 $wgOut->addHtml( wfMsgHtml( 'undelete-nodiff' ) );
696 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user ) . '</p>' );
698 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
700 if( $this->mPreview
) {
701 $wgOut->addHtml( "<hr />\n" );
702 $wgOut->addWikiTextTitleTidy( $rev->getText(), $this->mTargetObj
, false );
706 wfElement( 'textarea', array(
707 'readonly' => 'readonly',
708 'cols' => intval( $wgUser->getOption( 'cols' ) ),
709 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
710 $rev->getText() . "\n" ) .
711 wfOpenElement( 'div' ) .
712 wfOpenElement( 'form', array(
714 'action' => $self->getLocalURL( "action=submit" ) ) ) .
715 wfElement( 'input', array(
718 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
719 wfElement( 'input', array(
721 'name' => 'timestamp',
722 'value' => $timestamp ) ) .
723 wfElement( 'input', array(
725 'name' => 'wpEditToken',
726 'value' => $wgUser->editToken() ) ) .
727 wfElement( 'input', array(
730 'value' => wfMsg( 'showpreview' ) ) ) .
731 wfElement( 'input', array(
734 'value' => wfMsg( 'showdiff' ) ) ) .
735 wfCloseElement( 'form' ) .
736 wfCloseElement( 'div' ) );
740 * Build a diff display between this and the previous either deleted
741 * or non-deleted edit.
742 * @param Revision $previousRev
743 * @param Revision $currentRev
744 * @return string HTML
746 function showDiff( $previousRev, $currentRev ) {
747 global $wgOut, $wgUser;
749 $diffEngine = new DifferenceEngine();
750 $diffEngine->showDiffStyle();
753 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
754 "<col class='diff-marker' />" .
755 "<col class='diff-content' />" .
756 "<col class='diff-marker' />" .
757 "<col class='diff-content' />" .
759 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
760 $this->diffHeader( $previousRev ) .
762 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
763 $this->diffHeader( $currentRev ) .
766 $diffEngine->generateDiffBody(
767 $previousRev->getText(), $currentRev->getText() ) .
773 private function diffHeader( $rev ) {
774 global $wgUser, $wgLang, $wgLang;
775 $sk = $wgUser->getSkin();
776 $isDeleted = !( $rev->getId() && $rev->getTitle() );
778 /// @fixme $rev->getTitle() is null for deleted revs...?
779 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
780 $targetQuery = 'target=' .
781 $this->mTargetObj
->getPrefixedUrl() .
783 wfTimestamp( TS_MW
, $rev->getTimestamp() );
785 /// @fixme getId() may return non-zero for deleted revs...
786 $targetPage = $rev->getTitle();
787 $targetQuery = 'oldid=' . $rev->getId();
790 '<div id="mw-diff-otitle1"><strong>' .
791 $sk->makeLinkObj( $targetPage,
792 wfMsgHtml( 'revisionasof',
793 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
795 ( $isDeleted ?
' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
797 '<div id="mw-diff-otitle2">' .
798 $sk->revUserTools( $rev ) . '<br/>' .
800 '<div id="mw-diff-otitle3">' .
801 $sk->revComment( $rev ) . '<br/>' .
806 * Show a deleted file version requested by the visitor.
808 function showFile( $key ) {
809 global $wgOut, $wgRequest;
812 # We mustn't allow the output to be Squid cached, otherwise
813 # if an admin previews a deleted image, and it's cached, then
814 # a user without appropriate permissions can toddle off and
815 # nab the image, and Squid will serve it
816 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
817 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
818 $wgRequest->response()->header( 'Pragma: no-cache' );
820 $store = FileStore
::get( 'deleted' );
821 $store->stream( $key );
824 /* private */ function showHistory() {
825 global $wgLang, $wgContLang, $wgUser, $wgOut;
827 $sk = $wgUser->getSkin();
828 if ( $this->mAllowed
) {
829 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
831 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
834 $archive = new PageArchive( $this->mTargetObj
);
836 $text = $archive->getLastRevisionText();
837 if( is_null( $text ) ) {
838 $wgOut->addWikiText( wfMsg( "nohistory" ) );
842 if ( $this->mAllowed
) {
843 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
845 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
848 # List all stored revisions
849 $revisions = $archive->listRevisions();
850 $files = $archive->listFiles();
852 $haveRevisions = $revisions && $revisions->numRows() > 0;
853 $haveFiles = $files && $files->numRows() > 0;
855 # Batch existence check on user and talk pages
856 if( $haveRevisions ) {
857 $batch = new LinkBatch();
858 while( $row = $revisions->fetchObject() ) {
859 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
860 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
863 $revisions->seek( 0 );
866 $batch = new LinkBatch();
867 while( $row = $files->fetchObject() ) {
868 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
869 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
875 if ( $this->mAllowed
) {
876 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
877 $action = $titleObj->getLocalURL( "action=submit" );
878 # Start the form here
879 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
880 $wgOut->addHtml( $top );
883 # Show relevant lines from the deletion log:
884 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
885 $logViewer = new LogViewer(
889 'page' => $this->mTargetObj
->getPrefixedText(),
893 ), LogViewer
::NO_ACTION_LINK
895 $logViewer->showList( $wgOut );
897 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
898 # Format the user-visible controls (comment field, submission button)
899 # in a nice little table
900 $align = $wgContLang->isRtl() ?
'left' : 'right';
902 Xml
::openElement( 'fieldset' ) .
903 Xml
::openElement( 'table' ) .
906 wfMsgWikiHtml( 'undeleteextrahelp' ) .
910 <td align='$align'>" .
911 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
914 Xml
::input( 'wpComment', 50, $this->mComment
) .
920 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
921 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
924 Xml
::closeElement( 'table' ) .
925 Xml
::closeElement( 'fieldset' );
927 $wgOut->addHtml( $table );
930 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
932 if( $haveRevisions ) {
933 # The page's stored (deleted) history:
934 $wgOut->addHTML("<ul>");
935 $target = urlencode( $this->mTarget
);
936 $remaining = $revisions->numRows();
937 $earliestLiveTime = $this->getEarliestTime( $this->mTargetObj
);
939 while( $row = $revisions->fetchObject() ) {
941 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
942 if ( $this->mAllowed
) {
943 $checkBox = Xml
::check( "ts$ts" );
944 $pageLink = $sk->makeKnownLinkObj( $titleObj,
945 $wgLang->timeanddate( $ts, true ),
946 "target=$target×tamp=$ts" );
947 if( ($remaining > 0) ||
948 ($earliestLiveTime && $ts > $earliestLiveTime ) ) {
950 $sk->makeKnownLinkObj( $titleObj,
952 "target=$target×tamp=$ts&diff=prev" ) .
955 // No older revision to diff against
960 $pageLink = $wgLang->timeanddate( $ts, true );
963 $userLink = $sk->userLink( $row->ar_user
, $row->ar_user_text
) . $sk->userToolLinks( $row->ar_user
, $row->ar_user_text
);
965 if (!is_null($size = $row->ar_len
)) {
967 $stxt = wfMsgHtml('historyempty');
969 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
972 $comment = $sk->commentBlock( $row->ar_comment
);
973 $wgOut->addHTML( "<li>$checkBox $pageLink $diffLink . . $userLink $stxt $comment</li>\n" );
977 $wgOut->addHTML("</ul>");
979 $wgOut->addWikiText( wfMsg( "nohistory" ) );
983 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'filehist' ) . "</h2>\n" );
984 $wgOut->addHtml( "<ul>" );
985 while( $row = $files->fetchObject() ) {
986 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
987 if ( $this->mAllowed
&& $row->fa_storage_key
) {
988 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
989 $key = urlencode( $row->fa_storage_key
);
990 $target = urlencode( $this->mTarget
);
991 $pageLink = $sk->makeKnownLinkObj( $titleObj,
992 $wgLang->timeanddate( $ts, true ),
993 "target=$target&file=$key" );
996 $pageLink = $wgLang->timeanddate( $ts, true );
998 $userLink = $sk->userLink( $row->fa_user
, $row->fa_user_text
) . $sk->userToolLinks( $row->fa_user
, $row->fa_user_text
);
1000 wfMsgHtml( 'widthheight',
1001 $wgLang->formatNum( $row->fa_width
),
1002 $wgLang->formatNum( $row->fa_height
) ) .
1004 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1006 $comment = $sk->commentBlock( $row->fa_description
);
1007 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
1010 $wgOut->addHTML( "</ul>" );
1013 if ( $this->mAllowed
) {
1014 # Slip in the hidden controls here
1015 $misc = Xml
::hidden( 'target', $this->mTarget
);
1016 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1017 $misc .= Xml
::closeElement( 'form' );
1018 $wgOut->addHtml( $misc );
1024 private function getEarliestTime( $title ) {
1025 $dbr = wfGetDB( DB_SLAVE
);
1026 if( $title->exists() ) {
1027 $min = $dbr->selectField( 'revision',
1028 'MIN(rev_timestamp)',
1029 array( 'rev_page' => $title->getArticleId() ),
1031 return wfTimestampOrNull( TS_MW
, $min );
1036 function undelete() {
1037 global $wgOut, $wgUser;
1038 if( !is_null( $this->mTargetObj
) ) {
1039 $archive = new PageArchive( $this->mTargetObj
);
1041 $ok = $archive->undelete(
1042 $this->mTargetTimestamp
,
1044 $this->mFileVersions
);
1046 if( is_array($ok) ) {
1047 $skin = $wgUser->getSkin();
1048 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
1049 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1051 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1054 // Show file deletion warnings and errors
1055 $status = $archive->getFileStatus();
1056 if ( $status && !$status->isGood() ) {
1057 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1060 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );