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', 'ar_deleted' ),
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',
143 array( 'fa_name' => $this->title
->getDBkey() ),
145 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
146 $ret = $dbr->resultObject( $res );
153 * Fetch (and decompress if necessary) the stored text for the deleted
154 * revision of the page with the given timestamp.
157 * @deprecated Use getRevision() for more flexible information
159 function getRevisionText( $timestamp ) {
160 $rev = $this->getRevision( $timestamp );
161 return $rev ?
$rev->getText() : null;
165 * Return a Revision object containing data for the deleted revision.
166 * Note that the result *may* or *may not* have a null page ID.
167 * @param string $timestamp
170 function getRevision( $timestamp ) {
171 $dbr = wfGetDB( DB_SLAVE
);
172 $row = $dbr->selectRow( 'archive',
185 array( 'ar_namespace' => $this->title
->getNamespace(),
186 'ar_title' => $this->title
->getDBkey(),
187 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
190 return new Revision( array(
191 'page' => $this->title
->getArticleId(),
192 'id' => $row->ar_rev_id
,
193 'text' => ($row->ar_text_id
195 : Revision
::getRevisionText( $row, 'ar_' ) ),
196 'comment' => $row->ar_comment
,
197 'user' => $row->ar_user
,
198 'user_text' => $row->ar_user_text
,
199 'timestamp' => $row->ar_timestamp
,
200 'minor_edit' => $row->ar_minor_edit
,
201 'text_id' => $row->ar_text_id
,
202 'deleted' => $row->ar_deleted
,
203 'len' => $row->ar_len
) );
210 * Return the most-previous revision, either live or deleted, against
211 * the deleted revision given by timestamp.
213 * May produce unexpected results in case of history merges or other
214 * unusual time issues.
216 * @param string $timestamp
217 * @return Revision or null
219 function getPreviousRevision( $timestamp ) {
220 $dbr = wfGetDB( DB_SLAVE
);
222 // Check the previous deleted revision...
223 $row = $dbr->selectRow( 'archive',
225 array( 'ar_namespace' => $this->title
->getNamespace(),
226 'ar_title' => $this->title
->getDBkey(),
228 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
231 'ORDER BY' => 'ar_timestamp DESC',
233 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
235 $row = $dbr->selectRow( array( 'page', 'revision' ),
236 array( 'rev_id', 'rev_timestamp' ),
238 'page_namespace' => $this->title
->getNamespace(),
239 'page_title' => $this->title
->getDBkey(),
240 'page_id = rev_page',
242 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
245 'ORDER BY' => 'rev_timestamp DESC',
247 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
248 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
250 if( $prevLive && $prevLive > $prevDeleted ) {
251 // Most prior revision was live
252 return Revision
::newFromId( $prevLiveId );
253 } elseif( $prevDeleted ) {
254 // Most prior revision was deleted
255 return $this->getRevision( $prevDeleted );
257 // No prior revision on this page.
263 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
265 function getTextFromRow( $row ) {
266 if( is_null( $row->ar_text_id
) ) {
267 // An old row from MediaWiki 1.4 or previous.
268 // Text is embedded in this row in classic compression format.
269 return Revision
::getRevisionText( $row, "ar_" );
271 // New-style: keyed to the text storage backend.
272 $dbr = wfGetDB( DB_SLAVE
);
273 $text = $dbr->selectRow( 'text',
274 array( 'old_text', 'old_flags' ),
275 array( 'old_id' => $row->ar_text_id
),
277 return Revision
::getRevisionText( $text );
283 * Fetch (and decompress if necessary) the stored text of the most
284 * recently edited deleted revision of the page.
286 * If there are no archived revisions for the page, returns NULL.
290 function getLastRevisionText() {
291 $dbr = wfGetDB( DB_SLAVE
);
292 $row = $dbr->selectRow( 'archive',
293 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
294 array( 'ar_namespace' => $this->title
->getNamespace(),
295 'ar_title' => $this->title
->getDBkey() ),
296 'PageArchive::getLastRevisionText',
297 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
299 return $this->getTextFromRow( $row );
306 * Quick check if any archived revisions are present for the page.
309 function isDeleted() {
310 $dbr = wfGetDB( DB_SLAVE
);
311 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
312 array( 'ar_namespace' => $this->title
->getNamespace(),
313 'ar_title' => $this->title
->getDBkey() ) );
318 * Restore the given (or all) text and file revisions for the page.
319 * Once restored, the items will be removed from the archive tables.
320 * The deletion log will be updated with an undeletion notice.
322 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
323 * @param string $comment
324 * @param array $fileVersions
325 * @param bool $unsuppress
327 * @return array(number of file revisions restored, number of image revisions restored, log message)
328 * on success, false on failure
330 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
331 // If both the set of text revisions and file revisions are empty,
332 // restore everything. Otherwise, just restore the requested items.
333 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
335 $restoreText = $restoreAll ||
!empty( $timestamps );
336 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
338 if( $restoreFiles && $this->title
->getNamespace() == NS_IMAGE
) {
339 $img = wfLocalFile( $this->title
);
340 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
341 $filesRestored = $this->fileStatus
->successCount
;
347 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress );
348 if($textRestored === false) // It must be one of UNDELETE_*
356 $log = new LogPage( 'delete' );
358 if( $textRestored && $filesRestored ) {
359 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
360 $wgContLang->formatNum( $textRestored ),
361 $wgContLang->formatNum( $filesRestored ) );
362 } elseif( $textRestored ) {
363 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
364 $wgContLang->formatNum( $textRestored ) );
365 } elseif( $filesRestored ) {
366 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
367 $wgContLang->formatNum( $filesRestored ) );
369 wfDebug( "Undelete: nothing undeleted...\n" );
373 if( trim( $comment ) != '' )
374 $reason .= ": {$comment}";
375 $log->addEntry( 'restore', $this->title
, $reason );
377 return array($textRestored, $filesRestored, $reason);
381 * This is the meaty bit -- restores archived revisions of the given page
382 * to the cur/old tables. If the page currently exists, all revisions will
383 * be stuffed into old, otherwise the most recent will go into cur.
385 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
386 * @param string $comment
387 * @param array $fileVersions
388 * @param bool $unsuppress, remove all ar_deleted/fa_deleted restrictions of seletected revs
390 * @return mixed number of revisions restored or false on failure
392 private function undeleteRevisions( $timestamps, $unsuppress = false ) {
395 $restoreAll = empty( $timestamps );
397 $dbw = wfGetDB( DB_MASTER
);
399 # Does this page already exist? We'll have to update it...
400 $article = new Article( $this->title
);
401 $options = 'FOR UPDATE';
402 $page = $dbw->selectRow( 'page',
403 array( 'page_id', 'page_latest' ),
404 array( 'page_namespace' => $this->title
->getNamespace(),
405 'page_title' => $this->title
->getDBkey() ),
410 # Page already exists. Import the history, and if necessary
411 # we'll update the latest revision field in the record.
413 $pageId = $page->page_id
;
414 $previousRevId = $page->page_latest
;
415 # Get the time span of this page
416 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
417 array( 'rev_id' => $previousRevId ),
419 if( $previousTimestamp === false ) {
420 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
424 # Have to create a new article...
427 $previousTimestamp = 0;
431 $oldones = '1 = 1'; # All revisions...
433 $oldts = implode( ',',
434 array_map( array( &$dbw, 'addQuotes' ),
435 array_map( array( &$dbw, 'timestamp' ),
438 $oldones = "ar_timestamp IN ( {$oldts} )";
442 * Select each archived revision...
444 $result = $dbw->select( 'archive',
459 'ar_namespace' => $this->title
->getNamespace(),
460 'ar_title' => $this->title
->getDBkey(),
464 'ORDER BY' => 'ar_timestamp' )
466 $ret = $dbw->resultObject( $result );
468 $rev_count = $dbw->numRows( $result );
470 # We need to seek around as just using DESC in the ORDER BY
471 # would leave the revisions inserted in the wrong order
472 $first = $ret->fetchObject();
473 $ret->seek( $rev_count - 1 );
474 $last = $ret->fetchObject();
475 // We don't handle well changing the top revision's settings
476 if( !$unsuppress && $last->ar_deleted
&& $last->ar_timestamp
> $previousTimestamp ) {
477 wfDebug( __METHOD__
.": restoration would result in a deleted top revision\n" );
484 $newid = $article->insertOn( $dbw );
491 while( $row = $ret->fetchObject() ) {
492 if( $row->ar_text_id
) {
493 // Revision was deleted in 1.5+; text is in
494 // the regular text table, use the reference.
495 // Specify null here so the so the text is
496 // dereferenced for page length info if needed.
499 // Revision was deleted in 1.4 or earlier.
500 // Text is squashed into the archive row, and
501 // a new text table entry will be created for it.
502 $revText = Revision
::getRevisionText( $row, 'ar_' );
504 $revision = new Revision( array(
506 'id' => $row->ar_rev_id
,
508 'comment' => $row->ar_comment
,
509 'user' => $row->ar_user
,
510 'user_text' => $row->ar_user_text
,
511 'timestamp' => $row->ar_timestamp
,
512 'minor_edit' => $row->ar_minor_edit
,
513 'text_id' => $row->ar_text_id
,
514 'deleted' => $unsuppress ?
0 : $row->ar_deleted
,
515 'len' => $row->ar_len
517 $revision->insertOn( $dbw );
520 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
522 // Was anything restored at all?
527 // Attach the latest revision to the page...
528 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
530 if( $newid ||
$wasnew ) {
531 // Update site stats, link tables, etc
532 $article->createUpdates( $revision );
536 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true ) );
537 Article
::onArticleCreate( $this->title
);
539 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false ) );
540 Article
::onArticleEdit( $this->title
);
543 if( $this->title
->getNamespace() == NS_IMAGE
) {
544 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
548 // Revision couldn't be created. This is very weird
549 return self
::UNDELETE_UNKNOWNERR
;
552 # Now that it's safely stored, take it out of the archive
553 $dbw->delete( 'archive',
555 'ar_namespace' => $this->title
->getNamespace(),
556 'ar_title' => $this->title
->getDBkey(),
563 function getFileStatus() { return $this->fileStatus
; }
567 * The HTML form for Special:Undelete, which allows users with the appropriate
568 * permissions to view and restore deleted content.
569 * @addtogroup SpecialPage
572 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
573 var $mTargetTimestamp, $mAllowed, $mComment;
575 function UndeleteForm( $request, $par = "" ) {
577 $this->mAction
= $request->getVal( 'action' );
578 $this->mTarget
= $request->getVal( 'target' );
579 $this->mSearchPrefix
= $request->getText( 'prefix' );
580 $time = $request->getVal( 'timestamp' );
581 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
582 $this->mFile
= $request->getVal( 'file' );
584 $posted = $request->wasPosted() &&
585 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
586 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
587 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
588 $this->mDiff
= $request->getCheck( 'diff' );
589 $this->mComment
= $request->getText( 'wpComment' );
590 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'oversight' );
593 $this->mTarget
= $par;
595 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
596 $this->mAllowed
= true;
598 $this->mAllowed
= false;
599 $this->mTimestamp
= '';
600 $this->mRestore
= false;
602 if ( $this->mTarget
!== "" ) {
603 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
605 $this->mTargetObj
= NULL;
607 if( $this->mRestore
) {
608 $timestamps = array();
609 $this->mFileVersions
= array();
610 foreach( $_REQUEST as $key => $val ) {
612 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
613 array_push( $timestamps, $matches[1] );
616 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
617 $this->mFileVersions
[] = intval( $matches[1] );
620 rsort( $timestamps );
621 $this->mTargetTimestamp
= $timestamps;
626 global $wgOut, $wgUser;
627 if ( $this->mAllowed
) {
628 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
630 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
633 if( is_null( $this->mTargetObj
) ) {
634 # Not all users can just browse every deleted page from the list
635 if( $wgUser->isAllowed( 'browsearchive' ) ) {
636 $this->showSearchForm();
638 # List undeletable articles
639 if( $this->mSearchPrefix
) {
640 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
641 $this->showList( $result );
644 $wgOut->addWikiText( wfMsgHtml( 'undelete-header' ) );
648 if( $this->mTimestamp
!== '' ) {
649 return $this->showRevision( $this->mTimestamp
);
651 if( $this->mFile
!== null ) {
652 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
653 // Check if user is allowed to see this file
654 if( !$file->userCan( File
::DELETED_FILE
) ) {
655 $wgOut->permissionRequired( 'hiderevision' );
658 return $this->showFile( $this->mFile
);
661 if( $this->mRestore
&& $this->mAction
== "submit" ) {
662 return $this->undelete();
664 return $this->showHistory();
667 function showSearchForm() {
668 global $wgOut, $wgScript;
669 $wgOut->addWikiMsg( 'undelete-header' );
672 Xml
::openElement( 'form', array(
674 'action' => $wgScript ) ) .
676 Xml
::element( 'legend', array(),
677 wfMsg( 'undelete-search-box' ) ) .
678 Xml
::hidden( 'title',
679 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
680 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
681 'prefix', 'prefix', 20,
682 $this->mSearchPrefix
) .
683 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
688 // Generic list of deleted pages
689 private function showList( $result ) {
690 global $wgLang, $wgContLang, $wgUser, $wgOut;
692 if( $result->numRows() == 0 ) {
693 $wgOut->addWikiMsg( 'undelete-no-results' );
697 $wgOut->addWikiMsg( "undeletepagetext" );
699 $sk = $wgUser->getSkin();
700 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
701 $wgOut->addHTML( "<ul>\n" );
702 while( $row = $result->fetchObject() ) {
703 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
704 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ),
705 'target=' . $title->getPrefixedUrl() );
706 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
707 $revs = wfMsgExt( 'undeleterevisions',
708 array( 'parseinline' ),
709 $wgLang->formatNum( $row->count
) );
710 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
713 $wgOut->addHTML( "</ul>\n" );
718 private function showRevision( $timestamp ) {
719 global $wgLang, $wgUser, $wgOut;
720 $self = SpecialPage
::getTitleFor( 'Undelete' );
721 $skin = $wgUser->getSkin();
723 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
725 $archive = new PageArchive( $this->mTargetObj
);
726 $rev = $archive->getRevision( $timestamp );
729 $wgOut->addWikiMsg( 'undeleterevision-missing' );
733 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
734 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
735 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-permission' ) );
738 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-view' ) );
739 $wgOut->addHTML( '<br/>' );
740 // and we are allowed to see...
744 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
746 $link = $skin->makeKnownLinkObj(
747 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
748 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
750 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
751 $user = $skin->revUserTools( $rev );
754 $previousRev = $archive->getPreviousRevision( $timestamp );
756 $this->showDiff( $previousRev, $rev );
757 if( $wgUser->getOption( 'diffonly' ) ) {
760 $wgOut->addHtml( '<hr />' );
763 $wgOut->addHtml( wfMsgHtml( 'undelete-nodiff' ) );
767 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user ) . '</p>' );
769 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
771 if( $this->mPreview
) {
772 $wgOut->addHtml( "<hr />\n" );
773 $wgOut->addWikiTextTitleTidy( $rev->revText(), $this->mTargetObj
, false );
777 wfElement( 'textarea', array(
778 'readonly' => 'readonly',
779 'cols' => intval( $wgUser->getOption( 'cols' ) ),
780 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
781 $rev->revText() . "\n" ) .
782 wfOpenElement( 'div' ) .
783 wfOpenElement( 'form', array(
785 'action' => $self->getLocalURL( "action=submit" ) ) ) .
786 wfElement( 'input', array(
789 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
790 wfElement( 'input', array(
792 'name' => 'timestamp',
793 'value' => $timestamp ) ) .
794 wfElement( 'input', array(
796 'name' => 'wpEditToken',
797 'value' => $wgUser->editToken() ) ) .
798 wfElement( 'input', array(
801 'value' => wfMsg( 'showpreview' ) ) ) .
802 wfElement( 'input', array(
805 'value' => wfMsg( 'showdiff' ) ) ) .
806 wfCloseElement( 'form' ) .
807 wfCloseElement( 'div' ) );
811 * Build a diff display between this and the previous either deleted
812 * or non-deleted edit.
813 * @param Revision $previousRev
814 * @param Revision $currentRev
815 * @return string HTML
817 function showDiff( $previousRev, $currentRev ) {
818 global $wgOut, $wgUser;
820 $diffEngine = new DifferenceEngine();
821 $diffEngine->showDiffStyle();
824 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
825 "<col class='diff-marker' />" .
826 "<col class='diff-content' />" .
827 "<col class='diff-marker' />" .
828 "<col class='diff-content' />" .
830 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
831 $this->diffHeader( $previousRev ) .
833 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
834 $this->diffHeader( $currentRev ) .
837 $diffEngine->generateDiffBody(
838 $previousRev->getText(), $currentRev->getText() ) .
844 private function diffHeader( $rev ) {
845 global $wgUser, $wgLang, $wgLang;
846 $sk = $wgUser->getSkin();
847 $isDeleted = !( $rev->getId() && $rev->getTitle() );
849 /// @fixme $rev->getTitle() is null for deleted revs...?
850 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
851 $targetQuery = 'target=' .
852 $this->mTargetObj
->getPrefixedUrl() .
854 wfTimestamp( TS_MW
, $rev->getTimestamp() );
856 /// @fixme getId() may return non-zero for deleted revs...
857 $targetPage = $rev->getTitle();
858 $targetQuery = 'oldid=' . $rev->getId();
861 '<div id="mw-diff-otitle1"><strong>' .
862 $sk->makeLinkObj( $targetPage,
863 wfMsgHtml( 'revisionasof',
864 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
866 ( $isDeleted ?
' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
868 '<div id="mw-diff-otitle2">' .
869 $sk->revUserTools( $rev ) . '<br/>' .
871 '<div id="mw-diff-otitle3">' .
872 $sk->revComment( $rev ) . '<br/>' .
877 * Show a deleted file version requested by the visitor.
879 private function showFile( $key ) {
880 global $wgOut, $wgRequest;
883 # We mustn't allow the output to be Squid cached, otherwise
884 # if an admin previews a deleted image, and it's cached, then
885 # a user without appropriate permissions can toddle off and
886 # nab the image, and Squid will serve it
887 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
888 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
889 $wgRequest->response()->header( 'Pragma: no-cache' );
891 $store = FileStore
::get( 'deleted' );
892 $store->stream( $key );
895 private function showHistory() {
896 global $wgLang, $wgContLang, $wgUser, $wgOut;
898 $sk = $wgUser->getSkin();
899 if( $this->mAllowed
) {
900 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
902 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
905 $wgOut->addWikiText( wfMsgHtml( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText()) );
907 $archive = new PageArchive( $this->mTargetObj
);
909 $text = $archive->getLastRevisionText();
910 if( is_null( $text ) ) {
911 $wgOut->addWikiMsg( "nohistory" );
915 if ( $this->mAllowed
) {
916 $wgOut->addWikiMsg( "undeletehistory" );
917 $wgOut->addWikiMsg( "undeleterevdel" );
919 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
922 # List all stored revisions
923 $revisions = $archive->listRevisions();
924 $files = $archive->listFiles();
926 $haveRevisions = $revisions && $revisions->numRows() > 0;
927 $haveFiles = $files && $files->numRows() > 0;
929 # Batch existence check on user and talk pages
930 if( $haveRevisions ) {
931 $batch = new LinkBatch();
932 while( $row = $revisions->fetchObject() ) {
933 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
934 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
937 $revisions->seek( 0 );
940 $batch = new LinkBatch();
941 while( $row = $files->fetchObject() ) {
942 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
943 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
949 if ( $this->mAllowed
) {
950 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
951 $action = $titleObj->getLocalURL( "action=submit" );
952 # Start the form here
953 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
954 $wgOut->addHtml( $top );
957 # Show relevant lines from the deletion log:
958 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
959 $logViewer = new LogViewer(
963 'page' => $this->mTargetObj
->getPrefixedText(),
967 ), LogViewer
::NO_ACTION_LINK
969 $logViewer->showList( $wgOut );
971 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
972 # Format the user-visible controls (comment field, submission button)
973 # in a nice little table
974 $align = $wgContLang->isRtl() ?
'left' : 'right';
975 if( $wgUser->isAllowed( 'oversight' ) ) {
980 Xml
::check( 'wpUnsuppress', $this->mUnsuppress
, array('id' => 'mw-undelete-unsupress') ) . ' ' .
981 Xml
::label( wfMsgHtml('revdelete-unsuppress'), 'mw-undelete-unsupress' ) .
988 Xml
::openElement( 'fieldset' ) .
989 Xml
::element( 'legend', null, wfMsg( 'undelete') ).
990 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
993 wfMsgWikiHtml( 'undeleteextrahelp' ) .
997 <td align='$align'>" .
998 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1001 Xml
::input( 'wpComment', 50, $this->mComment
) .
1007 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
1008 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
1012 Xml
::closeElement( 'table' ) .
1013 Xml
::closeElement( 'fieldset' );
1015 $wgOut->addHtml( $table );
1018 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1020 if( $haveRevisions ) {
1021 # The page's stored (deleted) history:
1022 $wgOut->addHTML("<ul>");
1023 $target = urlencode( $this->mTarget
);
1024 $remaining = $revisions->numRows();
1025 $earliestLiveTime = $this->getEarliestTime( $this->mTargetObj
);
1027 while( $row = $revisions->fetchObject() ) {
1029 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1032 $wgOut->addHTML("</ul>");
1034 $wgOut->addWikiMsg( "nohistory" );
1038 $wgOut->addHtml( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1039 $wgOut->addHtml( "<ul>" );
1040 while( $row = $files->fetchObject() ) {
1041 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1044 $wgOut->addHTML( "</ul>" );
1047 if ( $this->mAllowed
) {
1048 # Slip in the hidden controls here
1049 $misc = Xml
::hidden( 'target', $this->mTarget
);
1050 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1051 $misc .= Xml
::closeElement( 'form' );
1052 $wgOut->addHtml( $misc );
1058 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1059 global $wgUser, $wgLang;
1061 $rev = new Revision( array(
1062 'page' => $this->mTargetObj
->getArticleId(),
1063 'comment' => $row->ar_comment
,
1064 'user' => $row->ar_user
,
1065 'user_text' => $row->ar_user_text
,
1066 'timestamp' => $row->ar_timestamp
,
1067 'minor_edit' => $row->ar_minor_edit
,
1068 'deleted' => $row->ar_deleted
,
1069 'len' => $row->ar_len
) );
1072 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1073 if( $this->mAllowed
) {
1074 $checkBox = Xml
::check( "ts$ts" );
1075 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1076 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1078 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1079 $last = wfMsgHtml('diff');
1080 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1081 $last = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml('diff'),
1082 "target=" . $this->mTargetObj
->getPrefixedUrl() . "×tamp=$ts&diff=prev" );
1084 $last = wfMsgHtml('diff');
1088 $pageLink = $wgLang->timeanddate( $ts, true );
1089 $last = wfMsgHtml('diff');
1091 $userLink = $sk->revUserTools( $rev );
1093 if(!is_null($size = $row->ar_len
)) {
1095 $stxt = wfMsgHtml('historyempty');
1097 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
1099 $comment = $sk->revComment( $rev );
1101 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1102 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1103 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1104 // If revision was hidden from sysops
1105 $del = wfMsgHtml('rev-delundel');
1107 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1108 $del = $sk->makeKnownLinkObj( $revdel,
1109 wfMsgHtml('rev-delundel'),
1110 'target=' . $this->mTargetObj
->getPrefixedUrl() . "&artimestamp=$ts" );
1111 // Bolden oversighted content
1112 if( $rev->isDeleted( Revision
::DELETED_RESTRICTED
) )
1113 $del = "<strong>$del</strong>";
1115 $revdlink = "<tt>(<small>$del</small>)</tt>";
1118 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1121 private function formatFileRow( $row, $sk ) {
1122 global $wgUser, $wgLang;
1124 $file = ArchivedFile
::newFromRow( $row );
1126 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1127 if( $this->mAllowed
&& $row->fa_storage_key
) {
1128 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1129 $key = urlencode( $row->fa_storage_key
);
1130 $target = urlencode( $this->mTarget
);
1131 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1132 $pageLink = $this->getFileLink( $file, $titleObj, $ts, $key, $sk );
1135 $pageLink = $wgLang->timeanddate( $ts, true );
1137 $userLink = $this->getFileUser( $file, $sk );
1139 wfMsgHtml( 'widthheight',
1140 $wgLang->formatNum( $row->fa_width
),
1141 $wgLang->formatNum( $row->fa_height
) ) .
1143 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1145 $comment = $this->getFileComment( $file, $sk );
1147 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1148 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1149 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1150 // If revision was hidden from sysops
1151 $del = wfMsgHtml('rev-delundel');
1153 $del = $sk->makeKnownLinkObj( $revdel,
1154 wfMsgHtml('rev-delundel'),
1155 'target=' . $this->mTargetObj
->getPrefixedUrl() .
1156 '&fileid=' . $row->fa_id
);
1157 // Bolden oversighted content
1158 if( $file->isDeleted( File
::DELETED_RESTRICTED
) )
1159 $del = "<strong>$del</strong>";
1161 $revdlink = "<tt>(<small>$del</small>)</tt>";
1163 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1166 private function getEarliestTime( $title ) {
1167 $dbr = wfGetDB( DB_SLAVE
);
1168 if( $title->exists() ) {
1169 $min = $dbr->selectField( 'revision',
1170 'MIN(rev_timestamp)',
1171 array( 'rev_page' => $title->getArticleId() ),
1173 return wfTimestampOrNull( TS_MW
, $min );
1179 * Fetch revision text link if it's available to all users
1182 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1185 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1186 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1188 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1189 "target=".$this->mTargetObj
->getPrefixedUrl()."×tamp=$ts" );
1190 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1191 $link = '<span class="history-deleted">' . $link . '</span>';
1197 * Fetch image view link if it's available to all users
1200 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1203 if( !$file->userCan(File
::DELETED_FILE
) ) {
1204 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1206 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1207 "target=".$this->mTargetObj
->getPrefixedUrl()."&file=$key" );
1208 if( $file->isDeleted(File
::DELETED_FILE
) )
1209 $link = '<span class="history-deleted">' . $link . '</span>';
1215 * Fetch file's user id if it's available to this user
1218 function getFileUser( $file, $sk ) {
1219 if( !$file->userCan(File
::DELETED_USER
) ) {
1220 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1222 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1223 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1224 if( $file->isDeleted(File
::DELETED_USER
) )
1225 $link = '<span class="history-deleted">' . $link . '</span>';
1231 * Fetch file upload comment if it's available to this user
1234 function getFileComment( $file, $sk ) {
1235 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1236 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1238 $link = $sk->commentBlock( $file->getRawDescription() );
1239 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1240 $link = '<span class="history-deleted">' . $link . '</span>';
1245 function undelete() {
1246 global $wgOut, $wgUser;
1247 if ( wfReadOnly() ) {
1248 $wgOut->readOnlyPage();
1251 if( !is_null( $this->mTargetObj
) ) {
1252 $archive = new PageArchive( $this->mTargetObj
);
1253 $ok = $archive->undelete(
1254 $this->mTargetTimestamp
,
1256 $this->mFileVersions
,
1257 $this->mUnsuppress
);
1259 if( is_array($ok) ) {
1260 if ( $ok[1] ) // Undeleted file count
1261 wfRunHooks( 'FileUndeleteComplete', array(
1262 $this->mTargetObj
, $this->mFileVersions
,
1263 $wgUser, $this->mComment
) );
1265 $skin = $wgUser->getSkin();
1266 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
1267 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1269 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1270 $wgOut->addHtml( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1273 // Show file deletion warnings and errors
1274 $status = $archive->getFileStatus();
1275 if( $status && !$status->isGood() ) {
1276 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1279 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );