4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
7 * @addtogroup Special pages
13 function wfSpecialUndelete( $par ) {
16 $form = new UndeleteForm( $wgRequest, $par );
22 * @addtogroup SpecialPage
27 function __construct( $title ) {
28 if( is_null( $title ) ) {
29 throw new MWException( 'Archiver() given a null title.');
31 $this->title
= $title;
35 * List all deleted pages recorded in the archive table. Returns result
36 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
39 * @return ResultWrapper
41 public static function listAllPages() {
42 $dbr = wfGetDB( DB_SLAVE
);
43 return self
::listPages( $dbr, '' );
47 * List deleted pages recorded in the archive table matching the
49 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
51 * @return ResultWrapper
53 public static function listPagesByPrefix( $prefix ) {
54 $dbr = wfGetDB( DB_SLAVE
);
56 $title = Title
::newFromText( $prefix );
58 $ns = $title->getNamespace();
59 $encPrefix = $dbr->escapeLike( $title->getDbKey() );
61 // Prolly won't work too good
62 // @todo handle bare namespace names cleanly?
64 $encPrefix = $dbr->escapeLike( $prefix );
67 'ar_namespace' => $ns,
68 "ar_title LIKE '$encPrefix%'",
70 return self
::listPages( $dbr, $conds );
73 protected static function listPages( $dbr, $condition ) {
74 return $dbr->resultObject(
85 'GROUP BY' => 'ar_namespace,ar_title',
86 'ORDER BY' => 'ar_namespace,ar_title',
94 * List the revisions of the given page. Returns result wrapper with
95 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
97 * @return ResultWrapper
99 function listRevisions() {
100 $dbr = wfGetDB( DB_SLAVE
);
101 $res = $dbr->select( 'archive',
102 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_rev_id', 'ar_deleted', 'ar_len' ),
103 array( 'ar_namespace' => $this->title
->getNamespace(),
104 'ar_title' => $this->title
->getDBkey() ),
105 'PageArchive::listRevisions',
106 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
107 $ret = $dbr->resultObject( $res );
112 * List the deleted file revisions for this page, if it's a file page.
113 * Returns a result wrapper with various filearchive fields, or null
114 * if not a file page.
116 * @return ResultWrapper
117 * @fixme Does this belong in Image for fuller encapsulation?
119 function listFiles() {
120 if( $this->title
->getNamespace() == NS_IMAGE
) {
121 $dbr = wfGetDB( DB_SLAVE
);
122 $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;
156 function getRevisionConds( $timestamp, $id ) {
159 return "ar_rev_id=$id";
160 } else if ( $timestamp ) {
161 return "ar_timestamp=$timestamp";
163 return 'ar_rev_id=0';
168 * Return a Revision object containing data for the deleted revision.
169 * Note that the result *may* have a null page ID.
170 * @param string $timestamp or $id
173 function getRevision( $timestamp, $id=null ) {
174 $dbr = wfGetDB( DB_SLAVE
);
175 $row = $dbr->selectRow( 'archive',
188 array( 'ar_namespace' => $this->title
->getNamespace(),
189 'ar_title' => $this->title
->getDbkey(),
190 $this->getRevisionConds( $dbr->timestamp($timestamp), $id ) ),
193 return new Revision( array(
194 'page' => $this->title
->getArticleId(),
195 'id' => $row->ar_rev_id
,
196 'text' => ($row->ar_text_id
198 : Revision
::getRevisionText( $row, 'ar_' ) ),
199 'comment' => $row->ar_comment
,
200 'user' => $row->ar_user
,
201 'user_text' => $row->ar_user_text
,
202 'timestamp' => $row->ar_timestamp
,
203 'minor_edit' => $row->ar_minor_edit
,
204 'text_id' => $row->ar_text_id
,
205 'deleted' => $row->ar_deleted
,
206 'len' => $row->ar_len
) );
213 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
215 function getTextFromRow( $row ) {
216 if( is_null( $row->ar_text_id
) ) {
217 // An old row from MediaWiki 1.4 or previous.
218 // Text is embedded in this row in classic compression format.
219 return Revision
::getRevisionText( $row, "ar_" );
221 // New-style: keyed to the text storage backend.
222 $dbr = wfGetDB( DB_SLAVE
);
223 $text = $dbr->selectRow( 'text',
224 array( 'old_text', 'old_flags' ),
225 array( 'old_id' => $row->ar_text_id
),
227 return Revision
::getRevisionText( $text );
233 * Fetch (and decompress if necessary) the stored text of the most
234 * recently edited deleted revision of the page.
236 * If there are no archived revisions for the page, returns NULL.
240 function getLastRevisionText() {
241 $dbr = wfGetDB( DB_SLAVE
);
242 $row = $dbr->selectRow( 'archive',
243 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
244 array( 'ar_namespace' => $this->title
->getNamespace(),
245 'ar_title' => $this->title
->getDBkey() ),
246 'PageArchive::getLastRevisionText',
247 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
249 return $this->getTextFromRow( $row );
256 * Quick check if any archived revisions are present for the page.
259 function isDeleted() {
260 $dbr = wfGetDB( DB_SLAVE
);
261 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
262 array( 'ar_namespace' => $this->title
->getNamespace(),
263 'ar_title' => $this->title
->getDBkey() ) );
268 * Restore the given (or all) text and file revisions for the page.
269 * Once restored, the items will be removed from the archive tables.
270 * The deletion log will be updated with an undeletion notice.
272 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
273 * @param string $comment
274 * @param array $fileVersions
276 * @return true on success.
278 function undelete( $timestamps, $comment = '', $fileVersions = array(), $Unsuppress = false) {
279 // If both the set of text revisions and file revisions are empty,
280 // restore everything. Otherwise, just restore the requested items.
281 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
283 $restoreText = $restoreAll ||
!empty( $timestamps );
284 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
287 $textRestored = $this->undeleteRevisions( $timestamps, $Unsuppress );
292 if ( $restoreText && !$textRestored) {
293 // if the image page didn't restore right, don't restore the file either!!!
295 } else if( $restoreFiles && $this->title
->getNamespace() == NS_IMAGE
) {
296 $img = new Image( $this->title
);
297 $filesRestored = $img->restore( $fileVersions, $Unsuppress );
304 $log = new LogPage( 'delete' );
306 if( $textRestored && $filesRestored ) {
307 $reason = wfMsgExt( 'undeletedrevisions-files', array('parsemag'),
308 $wgContLang->formatNum( $textRestored ),
309 $wgContLang->formatNum( $filesRestored ) );
310 } elseif( $textRestored ) {
311 $reason = wfMsgExt( 'undeletedrevisions', array('parsemag'),
312 $wgContLang->formatNum( $textRestored ) );
313 } elseif( $filesRestored ) {
314 $reason = wfMsgExt( 'undeletedfiles', array('parsemag'),
315 $wgContLang->formatNum( $filesRestored ) );
317 wfDebug( "Undelete: nothing undeleted...\n" );
321 if( trim( $comment ) != '' )
322 $reason .= ": {$comment}";
323 $log->addEntry( 'restore', $this->title
, $reason );
329 * This is the meaty bit -- restores archived revisions of the given page
330 * to the cur/old tables. If the page currently exists, all revisions will
331 * be stuffed into old, otherwise the most recent will go into cur.
333 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
334 * @param string $comment
335 * @param array $fileVersions
336 * @param bool $Unsuppress, remove all ar_deleted/fa_deleted restrictions of seletected revs
338 * @return int number of revisions restored
340 private function undeleteRevisions( $timestamps, $Unsuppress = false ) {
343 $restoreAll = empty( $timestamps );
345 $dbw = wfGetDB( DB_MASTER
);
346 $page = $dbw->tableName( 'archive' );
348 # Does this page already exist? We'll have to update it...
349 $article = new Article( $this->title
);
350 $options = ( $wgDBtype == 'postgres' )
351 ?
'' // pg doesn't support this?
353 $page = $dbw->selectRow( 'page',
354 array( 'page_id', 'page_latest' ),
355 array( 'page_namespace' => $this->title
->getNamespace(),
356 'page_title' => $this->title
->getDBkey() ),
360 # Page already exists. Import the history, and if necessary
361 # we'll update the latest revision field in the record.
363 $pageId = $page->page_id
;
364 $previousRevId = $page->page_latest
;
366 # Have to create a new article...
367 $newid = $article->insertOn( $dbw );
373 $oldones = '1 = 1'; # All revisions...
375 $oldts = implode( ',',
376 array_map( array( &$dbw, 'addQuotes' ),
377 array_map( array( &$dbw, 'timestamp' ),
380 $oldones = "ar_timestamp IN ( {$oldts} )";
384 * Select each archived revision...
386 $result = $dbw->select( 'archive',
400 'ar_namespace' => $this->title
->getNamespace(),
401 'ar_title' => $this->title
->getDBkey(),
405 'ORDER BY' => 'ar_timestamp' )
407 $rev_count = $dbw->numRows( $result );
408 if( $rev_count < count( $timestamps ) ) {
409 # Remove page stub per failure
410 $dbw->delete( 'page', array( 'page_id' => $pageId ), __METHOD__
);
411 wfDebug( __METHOD__
.": couldn't find all requested rows\n" );
415 $ret = $dbw->resultObject( $result );
416 // FIXME: We don't currently handle well changing the top revision's settings
417 $ret->seek( $rev_count - 1 );
418 $last_row = $ret->fetchObject();
419 if ( !$Unsuppress && $last_row->ar_deleted
&& $last_row->ar_rev_id
> $previousRevId ) {
420 # Remove page stub per failure
421 $dbw->delete( 'page', array( 'page_id' => $pageId ), __METHOD__
);
422 wfDebug( __METHOD__
.": couldn't find all requested rows or not all of them could be restored\n" );
430 while( $row = $ret->fetchObject() ) {
431 if( $row->ar_text_id
) {
432 // Revision was deleted in 1.5+; text is in
433 // the regular text table, use the reference.
434 // Specify null here so the so the text is
435 // dereferenced for page length info if needed.
438 // Revision was deleted in 1.4 or earlier.
439 // Text is squashed into the archive row, and
440 // a new text table entry will be created for it.
441 $revText = Revision
::getRevisionText( $row, 'ar_' );
443 $revision = new Revision( array(
445 'id' => $row->ar_rev_id
,
447 'comment' => $row->ar_comment
,
448 'user' => $row->ar_user
,
449 'user_text' => $row->ar_user_text
,
450 'timestamp' => $row->ar_timestamp
,
451 'minor_edit' => $row->ar_minor_edit
,
452 'text_id' => $row->ar_text_id
,
453 'deleted' => ($Unsuppress) ?
0 : $row->ar_deleted
,
454 'len' => $row->ar_len
456 $revision->insertOn( $dbw );
461 # FIXME: Update latest if newer as well...
463 // Attach the latest revision to the page...
464 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
466 // Update site stats, link tables, etc
467 $article->createUpdates( $revision );
471 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true ) );
472 Article
::onArticleCreate( $this->title
);
474 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false ) );
475 Article
::onArticleEdit( $this->title
);
478 # Something went terribly wrong!
481 # Now that it's safely stored, take it out of the archive
482 $dbw->delete( 'archive',
484 'ar_namespace' => $this->title
->getNamespace(),
485 'ar_title' => $this->title
->getDBkey(),
496 * @addtogroup SpecialPage
499 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
500 var $mTargetTimestamp, $mAllowed, $mComment;
502 function UndeleteForm( $request, $par = "" ) {
504 $this->mAction
= $request->getVal( 'action' );
505 $this->mTarget
= $request->getVal( 'target' );
506 $this->mSearchPrefix
= $request->getText( 'prefix' );
507 $time = $request->getVal( 'timestamp' );
508 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
509 $this->mFile
= $request->getVal( 'file' );
511 $posted = $request->wasPosted() &&
512 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
513 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
514 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
515 $this->mComment
= $request->getText( 'wpComment' );
516 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'oversight' );
519 $this->mTarget
= $par;
521 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
522 $this->mAllowed
= true;
524 $this->mAllowed
= false;
525 $this->mTimestamp
= '';
526 $this->mRestore
= false;
528 if ( $this->mTarget
!== "" ) {
529 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
531 $this->mTargetObj
= NULL;
533 if( $this->mRestore
) {
534 $timestamps = array();
535 $this->mFileVersions
= array();
536 foreach( $_REQUEST as $key => $val ) {
538 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
539 array_push( $timestamps, $matches[1] );
542 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
543 $this->mFileVersions
[] = intval( $matches[1] );
546 rsort( $timestamps );
547 $this->mTargetTimestamp
= $timestamps;
552 global $wgOut, $wgUser;
553 if ( $this->mAllowed
) {
554 $wgOut->setPagetitle( wfMsgHtml( "undeletepage" ) );
556 $wgOut->setPagetitle( wfMsgHtml( "viewdeletedpage" ) );
559 if( is_null( $this->mTargetObj
) ) {
560 # Not all users can just browse every deleted page from the list
561 if ( $wgUser->isAllowed( 'browsearchive' ) ) {
562 $this->showSearchForm();
564 # List undeletable articles
565 if( $this->mSearchPrefix
) {
566 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
567 $this->showList( $result );
570 $wgOut->addWikiText( wfMsgHtml( 'undelete-header' ) );
574 if( $this->mTimestamp
!== '' ) {
575 return $this->showRevision( $this->mTimestamp
);
577 if( $this->mFile
!== null ) {
578 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
579 // Check if user is allowed to see this file
580 if ( !$file->userCan( Image
::DELETED_FILE
) ) {
581 $wgOut->permissionRequired( 'hiderevision' );
584 return $this->showFile( $this->mFile
);
587 if( $this->mRestore
&& $this->mAction
== "submit" ) {
588 return $this->undelete();
590 return $this->showHistory();
593 function showSearchForm() {
594 global $wgOut, $wgScript;
595 $wgOut->addWikiText( wfMsgHtml( 'undelete-header' ) );
598 Xml
::openElement( 'form', array(
600 'action' => $wgScript ) ) .
602 Xml
::element( 'legend', array(),
603 wfMsg( 'undelete-search-box' ) ) .
604 Xml
::hidden( 'title',
605 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
606 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
607 'prefix', 'prefix', 20,
608 $this->mSearchPrefix
) .
609 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
614 // Generic list of deleted pages
615 /* private */ function showList( $result ) {
616 global $wgLang, $wgContLang, $wgUser, $wgOut;
618 if( $result->numRows() == 0 ) {
619 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
623 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
625 $sk = $wgUser->getSkin();
626 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
627 $wgOut->addHTML( "<ul>\n" );
628 while( $row = $result->fetchObject() ) {
629 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
630 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
631 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
632 $revs = wfMsgExt( 'undeleterevisions',
633 array( 'parseinline' ),
634 $wgLang->formatNum( $row->count
) );
635 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
638 $wgOut->addHTML( "</ul>\n" );
643 /* private */ function showRevision( $timestamp ) {
644 global $wgLang, $wgUser, $wgOut;
645 $self = SpecialPage
::getTitleFor( 'Undelete' );
646 $skin = $wgUser->getSkin();
648 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
650 $archive = new PageArchive( $this->mTargetObj
);
651 $rev = $archive->getRevision( $timestamp );
653 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
654 $link = $skin->makeKnownLinkObj( $self, htmlspecialchars( $this->mTargetObj
->getPrefixedText() ),
655 'target=' . $this->mTargetObj
->getPrefixedUrl() );
656 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link,
657 htmlspecialchars( $wgLang->timeAndDate( $timestamp ) ) ) . '</p>' );
660 $wgOut->addWikiText( wfMsg( 'undeleterevision-missing' ) );
664 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
665 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
666 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-permission' ) );
669 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-view' ) );
670 $wgOut->addHTML( '<br/>' );
671 // and we are allowed to see...
675 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
677 if( $this->mPreview
) {
678 $wgOut->addHtml( "<hr />\n" );
679 $wgOut->addWikiTextTitle( $rev->revText(), $this->mTargetObj
, false );
683 wfElement( 'textarea', array(
685 'cols' => intval( $wgUser->getOption( 'cols' ) ),
686 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
687 $rev->revText() . "\n" ) .
688 wfOpenElement( 'div' ) .
689 wfOpenElement( 'form', array(
691 'action' => $self->getLocalURL( "action=submit" ) ) ) .
692 wfElement( 'input', array(
695 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
696 wfElement( 'input', array(
698 'name' => 'timestamp',
699 'value' => $timestamp ) ) .
700 wfElement( 'input', array(
702 'name' => 'wpEditToken',
703 'value' => $wgUser->editToken() ) ) .
704 wfElement( 'input', array(
708 wfElement( 'input', array(
710 'value' => wfMsg( 'showpreview' ) ) ) .
711 wfCloseElement( 'form' ) .
712 wfCloseElement( 'div' ) );
716 * Show a deleted file version requested by the visitor.
718 function showFile( $key ) {
719 global $wgOut, $wgRequest;
722 # We mustn't allow the output to be Squid cached, otherwise
723 # if an admin previews a deleted image, and it's cached, then
724 # a user without appropriate permissions can toddle off and
725 # nab the image, and Squid will serve it
726 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
727 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
728 $wgRequest->response()->header( 'Pragma: no-cache' );
730 $store = FileStore
::get( 'deleted' );
731 $store->stream( $key );
734 /* private */ function showHistory() {
735 global $wgLang, $wgUser, $wgOut;
737 $this->sk
= $wgUser->getSkin();
738 if ( $this->mAllowed
) {
739 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
741 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
744 $archive = new PageArchive( $this->mTargetObj
);
746 $text = $archive->getLastRevisionText();
747 if( is_null( $text ) ) {
748 $wgOut->addWikiText( wfMsg( "nohistory" ) );
752 if ( $this->mAllowed
) {
753 $wgOut->addWikiText( '<p>' . wfMsgHtml( "undeletehistory" ) . '</p>' );
754 $wgOut->addHtml( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
756 $wgOut->addWikiText( wfMsgHtml( "undeletehistorynoadmin" ) );
759 # List all stored revisions
760 $revisions = $archive->listRevisions();
761 $files = $archive->listFiles();
763 $haveRevisions = $revisions && $revisions->numRows() > 0;
764 $haveFiles = $files && $files->numRows() > 0;
766 # Batch existence check on user and talk pages
767 if( $haveRevisions ) {
768 $batch = new LinkBatch();
769 while( $row = $revisions->fetchObject() ) {
770 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
771 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
774 $revisions->seek( 0 );
777 $batch = new LinkBatch();
778 while( $row = $files->fetchObject() ) {
779 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
780 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
786 if ( $this->mAllowed
) {
787 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
788 $action = $titleObj->getLocalURL( "action=submit" );
789 # Start the form here
790 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
791 $wgOut->addHtml( $top );
794 # Show relevant lines from the deletion log:
795 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
796 $logViewer = new LogViewer(
799 array( 'page' => $this->mTargetObj
->getPrefixedText(),
800 'type' => 'delete' ) ) ) );
801 $logViewer->showList( $wgOut );
802 # Show relevant lines from the oversight log if user is allowed to see it:
803 if ( $wgUser->isAllowed( 'oversight' ) ) {
804 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'oversight' ) ) . "</h2>\n" );
805 $logViewer = new LogViewer(
808 array( 'page' => $this->mTargetObj
->getPrefixedText(),
809 'type' => 'oversight' ) ) ) );
810 $logViewer->showList( $wgOut );
812 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
813 # Format the user-visible controls (comment field, submission button)
814 # in a nice little table
815 $table = '<fieldset><table><tr>';
816 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
817 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
818 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment
) . '</td>';
819 if ( $wgUser->isAllowed( 'oversight' ) ) {
820 $table .= '</tr><tr><td> </td><td>';
821 $table .= Xml
::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress', 'wpUnsuppress', false, array( 'tabindex' => '2' ) );
823 $table .= '</tr><tr><td> </td><td>';
824 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
825 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
826 $table .= '</td></tr></table></fieldset>';
827 $wgOut->addHtml( $table );
830 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
832 if( $haveRevisions ) {
833 # The page's stored (deleted) history:
834 $wgOut->addHTML("<ul>");
835 $target = urlencode( $this->mTarget
);
836 while( $row = $revisions->fetchObject() ) {
837 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
838 // We don't handle top edits with rev_deleted
839 if ( $this->mAllowed
) {
840 $checkBox = wfCheck( "ts$ts", false );
841 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
842 $pageLink = $this->getPageLink( $row, $titleObj, $ts, $target );
845 $pageLink = $wgLang->timeanddate( $ts, true );
847 $userLink = $this->getUser( $row );
849 if (!is_null($size = $row->ar_len
)) {
851 $stxt = wfMsgHtml('historyempty');
853 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
855 $comment = $this->getComment( $row );
857 if( $wgUser->isAllowed( 'deleterevision' ) ) {
858 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
859 if( !$this->userCan( $row, Revision
::DELETED_RESTRICTED
) ) {
860 // If revision was hidden from sysops
861 $del = wfMsgHtml( 'rev-delundel' );
863 $del = $this->sk
->makeKnownLinkObj( $revdel,
864 wfMsg( 'rev-delundel' ),
865 'target=' . urlencode( $this->mTarget
) .
866 '&arid=' . urlencode( $row->ar_rev_id
) );
867 // Bolden oversighted content
868 if( $this->isDeleted( $row, Revision
::DELETED_RESTRICTED
) )
869 $del = "<strong>$del</strong>";
871 $rd = "<tt>(<small>$del</small>)</tt>";
875 if( $this->isDeleted( $row, Revision
::DELETED_TEXT
) )
876 $dflag = ' <tt>' . wfMsgHtml( 'deletedrev' ) . '</tt>';
878 // Do we still need checkboxes?
879 $wgOut->addHTML( "<li>$checkBox $rd $pageLink . . $userLink $stxt $comment$dflag</li>\n" );
882 $wgOut->addHTML("</ul>");
884 $wgOut->addWikiText( wfMsg( "nohistory" ) );
889 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
890 $wgOut->addHtml( "<ul>" );
891 while( $row = $files->fetchObject() ) {
892 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
893 if ( $this->mAllowed
&& $row->fa_storage_key
) {
894 if ( !$this->userCan( $row, Revision
::DELETED_RESTRICTED
) )
895 // Grey out boxes to files restricted beyond this user.
896 // In the future, all image storage may use FileStore, allowing
897 // for such items to be restored and retain their fa_deleted status
898 $checkBox = wfCheck( "", false, array("disabled" => "disabled") );
900 $checkBox = wfCheck( "fileid" . $row->fa_id
);
901 $key = urlencode( $row->fa_storage_key
);
902 $target = urlencode( $this->mTarget
);
903 $pageLink = $this->getFileLink( $row, $titleObj, $ts, $target, $key );
906 $pageLink = $wgLang->timeanddate( $ts, true );
908 $userLink = $this->getFileUser( $row );
910 wfMsgHtml( 'widthheight',
911 $wgLang->formatNum( $row->fa_width
),
912 $wgLang->formatNum( $row->fa_height
) ) .
914 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
916 $comment = $this->getFileComment( $row );
918 if( $wgUser->isAllowed( 'deleterevision' ) ) {
919 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
920 if( !$this->userCan( $row, Image
::DELETED_RESTRICTED
) ) {
921 // If revision was hidden from sysops
922 $del = wfMsgHtml( 'rev-delundel' );
924 $del = $this->sk
->makeKnownLinkObj( $revdel,
925 wfMsg( 'rev-delundel' ),
926 'target=' . urlencode( $this->mTarget
) .
927 '&fileid=' . urlencode( $row->fa_id
) );
928 // Bolden oversighted content
929 if( $this->isDeleted( $row, Image
::DELETED_RESTRICTED
) )
930 $del = "<strong>$del</strong>";
932 $rd = "<tt>(<small>$del</small>)</tt>";
934 $wgOut->addHTML( "<li>$checkBox $rd $pageLink . . $userLink $data $comment</li>\n" );
937 $wgOut->addHTML( "</ul>" );
940 if ( $this->mAllowed
) {
941 # Slip in the hidden controls here
942 $misc = wfHidden( 'target', $this->mTarget
);
943 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
944 $wgOut->addHtml( $misc . '</form>' );
951 * Fetch revision text link if it's available to all users
954 function getPageLink( $row, $titleObj, $ts, $target ) {
957 if ( !$this->userCan($row, Revision
::DELETED_TEXT
) ) {
958 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
960 $link = $this->sk
->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ), "target=$target×tamp=$ts" );
961 if ( $this->isDeleted($row, Revision
::DELETED_TEXT
) )
962 $link = '<span class="history-deleted">' . $link . '</span>';
968 * Fetch image view link if it's available to all users
971 function getFileLink( $row, $titleObj, $ts, $target, $key ) {
974 if ( !$this->userCan($row, Image
::DELETED_FILE
) ) {
975 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
977 $link = $this->sk
->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ), "target=$target&file=$key" );
978 if ( $this->isDeleted($row, Image
::DELETED_FILE
) )
979 $link = '<span class="history-deleted">' . $link . '</span>';
985 * Fetch revision's user id if it's available to this user
988 function getUser( $row ) {
989 if ( !$this->userCan($row, Revision
::DELETED_USER
) ) {
990 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
992 $link = $this->sk
->userLink( $row->ar_user
, $row->ar_user_text
) . $this->sk
->userToolLinks( $row->ar_user
, $row->ar_user_text
);
993 if ( $this->isDeleted($row, Revision
::DELETED_USER
) )
994 $link = '<span class="history-deleted">' . $link . '</span>';
1000 * Fetch file's user id if it's available to this user
1003 function getFileUser( $row ) {
1004 if ( !$this->userCan($row, Image
::DELETED_USER
) ) {
1005 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1007 $link = $this->sk
->userLink( $row->fa_user
, $row->fa_user_text
) . $this->sk
->userToolLinks( $row->fa_user
, $row->fa_user_text
);
1008 if ( $this->isDeleted($row, Image
::DELETED_USER
) )
1009 $link = '<span class="history-deleted">' . $link . '</span>';
1015 * Fetch revision comment if it's available to this user
1018 function getComment( $row ) {
1019 if ( !$this->userCan($row, Revision
::DELETED_COMMENT
) ) {
1020 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1022 $link = $this->sk
->commentBlock( $row->ar_comment
);
1023 if ( $this->isDeleted($row, Revision
::DELETED_COMMENT
) )
1024 $link = '<span class="history-deleted">' . $link . '</span>';
1030 * Fetch file upload comment if it's available to this user
1033 function getFileComment( $row ) {
1034 if ( !$this->userCan($row, Image
::DELETED_COMMENT
) ) {
1035 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1037 $link = $this->sk
->commentBlock( $row->fa_description
);
1038 if ( $this->isDeleted($row, Image
::DELETED_COMMENT
) )
1039 $link = '<span class="history-deleted">' . $link . '</span>';
1045 * int $field one of DELETED_* bitfield constants
1046 * for file or revision rows
1049 function isDeleted( $row, $field ) {
1050 if ( isset($row->ar_deleted
) )
1052 return ($row->ar_deleted
& $field) == $field;
1053 else if ( isset($row->fa_deleted
) )
1055 return ($row->fa_deleted
& $field) == $field;
1060 * Determine if the current user is allowed to view a particular
1061 * field of this revision, if it's marked as deleted.
1065 function userCan( $row, $field ) {
1068 if( isset($row->ar_deleted
) && ($row->ar_deleted
& $field) == $field ) {
1070 $permission = ( $row->ar_deleted
& Revision
::DELETED_RESTRICTED
) == Revision
::DELETED_RESTRICTED
1073 wfDebug( "Checking for $permission due to $field match on $row->ar_deleted\n" );
1074 return $wgUser->isAllowed( $permission );
1075 } else if( isset($row->fa_deleted
) && ($row->fa_deleted
& $field) == $field ) {
1077 $permission = ( $row->fa_deleted
& Image
::DELETED_RESTRICTED
) == Image
::DELETED_RESTRICTED
1080 wfDebug( "Checking for $permission due to $field match on $row->fa_deleted\n" );
1081 return $wgUser->isAllowed( $permission );
1087 function undelete() {
1088 global $wgOut, $wgUser;
1089 if( !is_null( $this->mTargetObj
) ) {
1090 $archive = new PageArchive( $this->mTargetObj
);
1092 $ok = $archive->undelete(
1093 $this->mTargetTimestamp
,
1095 $this->mFileVersions
,
1096 $this->mUnsuppress
);
1098 $skin = $wgUser->getSkin();
1099 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
1100 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1103 // Give user some idea of what is going on ...
1104 // This can happen if the top revision would end up being deleted
1105 $wgOut->addHtml( '<p>' . wfMsgHtml( "cannotundelete" ) . '</p>' );
1106 $wgOut->addHtml( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1107 $wgOut->returnToMain( false, $this->mTargetObj
);
1110 $wgOut->showFatalError( wfMsgHtml( "cannotundelete" ) );