4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
14 function wfSpecialUndelete( $par ) {
17 $form = new UndeleteForm( $wgRequest, $par );
22 * Used to show archived pages and eventually restore them.
23 * @ingroup SpecialPage
29 function __construct( $title ) {
30 if( is_null( $title ) ) {
31 throw new MWException( 'Archiver() given a null title.');
33 $this->title
= $title;
37 * List all deleted pages recorded in the archive table. Returns result
38 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
41 * @return ResultWrapper
43 public static function listAllPages() {
44 $dbr = wfGetDB( DB_SLAVE
);
45 return self
::listPages( $dbr, '' );
49 * List deleted pages recorded in the archive table matching the
51 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
53 * @return ResultWrapper
55 public static function listPagesByPrefix( $prefix ) {
56 $dbr = wfGetDB( DB_SLAVE
);
58 $title = Title
::newFromText( $prefix );
60 $ns = $title->getNamespace();
61 $encPrefix = $dbr->escapeLike( $title->getDBkey() );
63 // Prolly won't work too good
64 // @todo handle bare namespace names cleanly?
66 $encPrefix = $dbr->escapeLike( $prefix );
69 'ar_namespace' => $ns,
70 "ar_title LIKE '$encPrefix%'",
72 return self
::listPages( $dbr, $conds );
75 protected static function listPages( $dbr, $condition ) {
76 return $dbr->resultObject(
87 'GROUP BY' => 'ar_namespace,ar_title',
88 'ORDER BY' => 'ar_namespace,ar_title',
96 * List the revisions of the given page. Returns result wrapper with
97 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
99 * @return ResultWrapper
101 function listRevisions() {
102 $dbr = wfGetDB( DB_SLAVE
);
103 $res = $dbr->select( 'archive',
104 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
105 array( 'ar_namespace' => $this->title
->getNamespace(),
106 'ar_title' => $this->title
->getDBkey() ),
107 'PageArchive::listRevisions',
108 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
109 $ret = $dbr->resultObject( $res );
114 * List the deleted file revisions for this page, if it's a file page.
115 * Returns a result wrapper with various filearchive fields, or null
116 * if not a file page.
118 * @return ResultWrapper
119 * @todo Does this belong in Image for fuller encapsulation?
121 function listFiles() {
122 if( $this->title
->getNamespace() == NS_IMAGE
) {
123 $dbr = wfGetDB( DB_SLAVE
);
124 $res = $dbr->select( 'filearchive',
144 array( 'fa_name' => $this->title
->getDBkey() ),
146 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
147 $ret = $dbr->resultObject( $res );
154 * Fetch (and decompress if necessary) the stored text for the deleted
155 * revision of the page with the given timestamp.
158 * @deprecated Use getRevision() for more flexible information
160 function getRevisionText( $timestamp ) {
161 $rev = $this->getRevision( $timestamp );
162 return $rev ?
$rev->getText() : null;
166 * Return a Revision object containing data for the deleted revision.
167 * Note that the result *may* or *may not* have a null page ID.
168 * @param string $timestamp
171 function getRevision( $timestamp ) {
172 $dbr = wfGetDB( DB_SLAVE
);
173 $row = $dbr->selectRow( 'archive',
186 array( 'ar_namespace' => $this->title
->getNamespace(),
187 'ar_title' => $this->title
->getDBkey(),
188 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
191 return new Revision( array(
192 'page' => $this->title
->getArticleId(),
193 'id' => $row->ar_rev_id
,
194 'text' => ($row->ar_text_id
196 : Revision
::getRevisionText( $row, 'ar_' ) ),
197 'comment' => $row->ar_comment
,
198 'user' => $row->ar_user
,
199 'user_text' => $row->ar_user_text
,
200 'timestamp' => $row->ar_timestamp
,
201 'minor_edit' => $row->ar_minor_edit
,
202 'text_id' => $row->ar_text_id
,
203 'deleted' => $row->ar_deleted
,
204 'len' => $row->ar_len
) );
211 * Return the most-previous revision, either live or deleted, against
212 * the deleted revision given by timestamp.
214 * May produce unexpected results in case of history merges or other
215 * unusual time issues.
217 * @param string $timestamp
218 * @return Revision or null
220 function getPreviousRevision( $timestamp ) {
221 $dbr = wfGetDB( DB_SLAVE
);
223 // Check the previous deleted revision...
224 $row = $dbr->selectRow( 'archive',
226 array( 'ar_namespace' => $this->title
->getNamespace(),
227 'ar_title' => $this->title
->getDBkey(),
229 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
232 'ORDER BY' => 'ar_timestamp DESC',
234 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
236 $row = $dbr->selectRow( array( 'page', 'revision' ),
237 array( 'rev_id', 'rev_timestamp' ),
239 'page_namespace' => $this->title
->getNamespace(),
240 'page_title' => $this->title
->getDBkey(),
241 'page_id = rev_page',
243 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
246 'ORDER BY' => 'rev_timestamp DESC',
248 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
249 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
251 if( $prevLive && $prevLive > $prevDeleted ) {
252 // Most prior revision was live
253 return Revision
::newFromId( $prevLiveId );
254 } elseif( $prevDeleted ) {
255 // Most prior revision was deleted
256 return $this->getRevision( $prevDeleted );
258 // No prior revision on this page.
264 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
266 function getTextFromRow( $row ) {
267 if( is_null( $row->ar_text_id
) ) {
268 // An old row from MediaWiki 1.4 or previous.
269 // Text is embedded in this row in classic compression format.
270 return Revision
::getRevisionText( $row, "ar_" );
272 // New-style: keyed to the text storage backend.
273 $dbr = wfGetDB( DB_SLAVE
);
274 $text = $dbr->selectRow( 'text',
275 array( 'old_text', 'old_flags' ),
276 array( 'old_id' => $row->ar_text_id
),
278 return Revision
::getRevisionText( $text );
284 * Fetch (and decompress if necessary) the stored text of the most
285 * recently edited deleted revision of the page.
287 * If there are no archived revisions for the page, returns NULL.
291 function getLastRevisionText() {
292 $dbr = wfGetDB( DB_SLAVE
);
293 $row = $dbr->selectRow( 'archive',
294 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
295 array( 'ar_namespace' => $this->title
->getNamespace(),
296 'ar_title' => $this->title
->getDBkey() ),
297 'PageArchive::getLastRevisionText',
298 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
300 return $this->getTextFromRow( $row );
307 * Quick check if any archived revisions are present for the page.
310 function isDeleted() {
311 $dbr = wfGetDB( DB_SLAVE
);
312 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
313 array( 'ar_namespace' => $this->title
->getNamespace(),
314 'ar_title' => $this->title
->getDBkey() ) );
319 * Restore the given (or all) text and file revisions for the page.
320 * Once restored, the items will be removed from the archive tables.
321 * The deletion log will be updated with an undeletion notice.
323 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
324 * @param string $comment
325 * @param array $fileVersions
326 * @param bool $unsuppress
328 * @return array(number of file revisions restored, number of image revisions restored, log message)
329 * on success, false on failure
331 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
332 // If both the set of text revisions and file revisions are empty,
333 // restore everything. Otherwise, just restore the requested items.
334 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
336 $restoreText = $restoreAll ||
!empty( $timestamps );
337 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
339 if( $restoreFiles && $this->title
->getNamespace() == NS_IMAGE
) {
340 $img = wfLocalFile( $this->title
);
341 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
342 $filesRestored = $this->fileStatus
->successCount
;
348 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress );
349 if($textRestored === false) // It must be one of UNDELETE_*
357 $log = new LogPage( 'delete' );
359 if( $textRestored && $filesRestored ) {
360 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
361 $wgContLang->formatNum( $textRestored ),
362 $wgContLang->formatNum( $filesRestored ) );
363 } elseif( $textRestored ) {
364 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
365 $wgContLang->formatNum( $textRestored ) );
366 } elseif( $filesRestored ) {
367 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
368 $wgContLang->formatNum( $filesRestored ) );
370 wfDebug( "Undelete: nothing undeleted...\n" );
374 if( trim( $comment ) != '' )
375 $reason .= ": {$comment}";
376 $log->addEntry( 'restore', $this->title
, $reason );
378 return array($textRestored, $filesRestored, $reason);
382 * This is the meaty bit -- restores archived revisions of the given page
383 * to the cur/old tables. If the page currently exists, all revisions will
384 * be stuffed into old, otherwise the most recent will go into cur.
386 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
387 * @param string $comment
388 * @param array $fileVersions
389 * @param bool $unsuppress, remove all ar_deleted/fa_deleted restrictions of seletected revs
391 * @return mixed number of revisions restored or false on failure
393 private function undeleteRevisions( $timestamps, $unsuppress = false ) {
396 $restoreAll = empty( $timestamps );
398 $dbw = wfGetDB( DB_MASTER
);
400 # Does this page already exist? We'll have to update it...
401 $article = new Article( $this->title
);
402 $options = 'FOR UPDATE';
403 $page = $dbw->selectRow( 'page',
404 array( 'page_id', 'page_latest' ),
405 array( 'page_namespace' => $this->title
->getNamespace(),
406 'page_title' => $this->title
->getDBkey() ),
411 # Page already exists. Import the history, and if necessary
412 # we'll update the latest revision field in the record.
414 $pageId = $page->page_id
;
415 $previousRevId = $page->page_latest
;
416 # Get the time span of this page
417 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
418 array( 'rev_id' => $previousRevId ),
420 if( $previousTimestamp === false ) {
421 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
425 # Have to create a new article...
428 $previousTimestamp = 0;
432 $oldones = '1 = 1'; # All revisions...
434 $oldts = implode( ',',
435 array_map( array( &$dbw, 'addQuotes' ),
436 array_map( array( &$dbw, 'timestamp' ),
439 $oldones = "ar_timestamp IN ( {$oldts} )";
443 * Select each archived revision...
445 $result = $dbw->select( 'archive',
460 'ar_namespace' => $this->title
->getNamespace(),
461 'ar_title' => $this->title
->getDBkey(),
465 'ORDER BY' => 'ar_timestamp' )
467 $ret = $dbw->resultObject( $result );
469 $rev_count = $dbw->numRows( $result );
471 # We need to seek around as just using DESC in the ORDER BY
472 # would leave the revisions inserted in the wrong order
473 $first = $ret->fetchObject();
474 $ret->seek( $rev_count - 1 );
475 $last = $ret->fetchObject();
476 // We don't handle well changing the top revision's settings
477 if( !$unsuppress && $last->ar_deleted
&& $last->ar_timestamp
> $previousTimestamp ) {
478 wfDebug( __METHOD__
.": restoration would result in a deleted top revision\n" );
485 $newid = $article->insertOn( $dbw );
492 while( $row = $ret->fetchObject() ) {
493 if( $row->ar_text_id
) {
494 // Revision was deleted in 1.5+; text is in
495 // the regular text table, use the reference.
496 // Specify null here so the so the text is
497 // dereferenced for page length info if needed.
500 // Revision was deleted in 1.4 or earlier.
501 // Text is squashed into the archive row, and
502 // a new text table entry will be created for it.
503 $revText = Revision
::getRevisionText( $row, 'ar_' );
505 // Check for key dupes due to shitty archive integrity.
506 if( $row->ar_rev_id
) {
507 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
508 if( $exists ) continue; // don't throw DB errors
511 $revision = new Revision( array(
513 'id' => $row->ar_rev_id
,
515 'comment' => $row->ar_comment
,
516 'user' => $row->ar_user
,
517 'user_text' => $row->ar_user_text
,
518 'timestamp' => $row->ar_timestamp
,
519 'minor_edit' => $row->ar_minor_edit
,
520 'text_id' => $row->ar_text_id
,
521 'deleted' => $unsuppress ?
0 : $row->ar_deleted
,
522 'len' => $row->ar_len
524 $revision->insertOn( $dbw );
527 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
529 # Now that it's safely stored, take it out of the archive
530 $dbw->delete( 'archive',
532 'ar_namespace' => $this->title
->getNamespace(),
533 'ar_title' => $this->title
->getDBkey(),
537 // Was anything restored at all?
542 // Attach the latest revision to the page...
543 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
545 if( $newid ||
$wasnew ) {
546 // Update site stats, link tables, etc
547 $article->createUpdates( $revision );
551 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true ) );
552 Article
::onArticleCreate( $this->title
);
554 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false ) );
555 Article
::onArticleEdit( $this->title
);
558 if( $this->title
->getNamespace() == NS_IMAGE
) {
559 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
563 // Revision couldn't be created. This is very weird
564 return self
::UNDELETE_UNKNOWNERR
;
570 function getFileStatus() { return $this->fileStatus
; }
574 * The HTML form for Special:Undelete, which allows users with the appropriate
575 * permissions to view and restore deleted content.
576 * @ingroup SpecialPage
579 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
580 var $mTargetTimestamp, $mAllowed, $mComment;
582 function UndeleteForm( $request, $par = "" ) {
584 $this->mAction
= $request->getVal( 'action' );
585 $this->mTarget
= $request->getVal( 'target' );
586 $this->mSearchPrefix
= $request->getText( 'prefix' );
587 $time = $request->getVal( 'timestamp' );
588 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
589 $this->mFile
= $request->getVal( 'file' );
591 $posted = $request->wasPosted() &&
592 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
593 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
594 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
595 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
596 $this->mDiff
= $request->getCheck( 'diff' );
597 $this->mComment
= $request->getText( 'wpComment' );
598 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
601 $this->mTarget
= $par;
603 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
604 $this->mAllowed
= true;
606 $this->mAllowed
= false;
607 $this->mTimestamp
= '';
608 $this->mRestore
= false;
610 if ( $this->mTarget
!== "" ) {
611 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
613 $this->mTargetObj
= NULL;
615 if( $this->mRestore ||
$this->mInvert
) {
616 $timestamps = array();
617 $this->mFileVersions
= array();
618 foreach( $_REQUEST as $key => $val ) {
620 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
621 array_push( $timestamps, $matches[1] );
624 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
625 $this->mFileVersions
[] = intval( $matches[1] );
628 rsort( $timestamps );
629 $this->mTargetTimestamp
= $timestamps;
634 global $wgOut, $wgUser;
635 if ( $this->mAllowed
) {
636 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
638 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
641 if( is_null( $this->mTargetObj
) ) {
642 # Not all users can just browse every deleted page from the list
643 if( $wgUser->isAllowed( 'browsearchive' ) ) {
644 $this->showSearchForm();
646 # List undeletable articles
647 if( $this->mSearchPrefix
) {
648 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
649 $this->showList( $result );
652 $wgOut->addWikiText( wfMsgHtml( 'undelete-header' ) );
656 if( $this->mTimestamp
!== '' ) {
657 return $this->showRevision( $this->mTimestamp
);
659 if( $this->mFile
!== null ) {
660 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
661 // Check if user is allowed to see this file
662 if( !$file->userCan( File
::DELETED_FILE
) ) {
663 $wgOut->permissionRequired( 'suppressrevision' );
666 return $this->showFile( $this->mFile
);
669 if( $this->mRestore
&& $this->mAction
== "submit" ) {
670 return $this->undelete();
672 if( $this->mInvert
&& $this->mAction
== "submit" ) {
673 return $this->showHistory( );
675 return $this->showHistory();
678 function showSearchForm() {
679 global $wgOut, $wgScript;
680 $wgOut->addWikiMsg( 'undelete-header' );
683 Xml
::openElement( 'form', array(
685 'action' => $wgScript ) ) .
687 Xml
::element( 'legend', array(),
688 wfMsg( 'undelete-search-box' ) ) .
689 Xml
::hidden( 'title',
690 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
691 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
692 'prefix', 'prefix', 20,
693 $this->mSearchPrefix
) .
694 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
699 // Generic list of deleted pages
700 private function showList( $result ) {
701 global $wgLang, $wgContLang, $wgUser, $wgOut;
703 if( $result->numRows() == 0 ) {
704 $wgOut->addWikiMsg( 'undelete-no-results' );
708 $wgOut->addWikiMsg( "undeletepagetext" );
710 $sk = $wgUser->getSkin();
711 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
712 $wgOut->addHTML( "<ul>\n" );
713 while( $row = $result->fetchObject() ) {
714 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
715 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ),
716 'target=' . $title->getPrefixedUrl() );
717 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
718 $revs = wfMsgExt( 'undeleterevisions',
719 array( 'parseinline' ),
720 $wgLang->formatNum( $row->count
) );
721 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
724 $wgOut->addHTML( "</ul>\n" );
729 private function showRevision( $timestamp ) {
730 global $wgLang, $wgUser, $wgOut;
731 $self = SpecialPage
::getTitleFor( 'Undelete' );
732 $skin = $wgUser->getSkin();
734 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
736 $archive = new PageArchive( $this->mTargetObj
);
737 $rev = $archive->getRevision( $timestamp );
740 $wgOut->addWikiMsg( 'undeleterevision-missing' );
744 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
745 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
746 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-permission' ) );
749 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-view' ) );
750 $wgOut->addHTML( '<br/>' );
751 // and we are allowed to see...
755 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
757 $link = $skin->makeKnownLinkObj(
758 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
759 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
763 $previousRev = $archive->getPreviousRevision( $timestamp );
765 $this->showDiff( $previousRev, $rev );
766 if( $wgUser->getOption( 'diffonly' ) ) {
769 $wgOut->addHtml( '<hr />' );
772 $wgOut->addHtml( wfMsgHtml( 'undelete-nodiff' ) );
776 // date and time are separate parameters to facilitate localisation.
777 // $time is kept for backward compat reasons.
778 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
779 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
780 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
781 $user = $skin->revUserTools( $rev );
783 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</p>' );
785 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
787 if( $this->mPreview
) {
788 $wgOut->addHtml( "<hr />\n" );
791 $popts = $wgOut->parserOptions();
792 $popts->setEditSection( false );
793 $wgOut->parserOptions( $popts );
794 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
798 wfElement( 'textarea', array(
799 'readonly' => 'readonly',
800 'cols' => intval( $wgUser->getOption( 'cols' ) ),
801 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
802 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
803 wfOpenElement( 'div' ) .
804 wfOpenElement( 'form', array(
806 'action' => $self->getLocalURL( "action=submit" ) ) ) .
807 wfElement( 'input', array(
810 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
811 wfElement( 'input', array(
813 'name' => 'timestamp',
814 'value' => $timestamp ) ) .
815 wfElement( 'input', array(
817 'name' => 'wpEditToken',
818 'value' => $wgUser->editToken() ) ) .
819 wfElement( 'input', array(
822 'value' => wfMsg( 'showpreview' ) ) ) .
823 wfElement( 'input', array(
826 'value' => wfMsg( 'showdiff' ) ) ) .
827 wfCloseElement( 'form' ) .
828 wfCloseElement( 'div' ) );
832 * Build a diff display between this and the previous either deleted
833 * or non-deleted edit.
834 * @param Revision $previousRev
835 * @param Revision $currentRev
836 * @return string HTML
838 function showDiff( $previousRev, $currentRev ) {
839 global $wgOut, $wgUser;
841 $diffEngine = new DifferenceEngine();
842 $diffEngine->showDiffStyle();
845 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
846 "<col class='diff-marker' />" .
847 "<col class='diff-content' />" .
848 "<col class='diff-marker' />" .
849 "<col class='diff-content' />" .
851 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
852 $this->diffHeader( $previousRev ) .
854 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
855 $this->diffHeader( $currentRev ) .
858 $diffEngine->generateDiffBody(
859 $previousRev->getText(), $currentRev->getText() ) .
865 private function diffHeader( $rev ) {
866 global $wgUser, $wgLang, $wgLang;
867 $sk = $wgUser->getSkin();
868 $isDeleted = !( $rev->getId() && $rev->getTitle() );
870 /// @fixme $rev->getTitle() is null for deleted revs...?
871 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
872 $targetQuery = 'target=' .
873 $this->mTargetObj
->getPrefixedUrl() .
875 wfTimestamp( TS_MW
, $rev->getTimestamp() );
877 /// @fixme getId() may return non-zero for deleted revs...
878 $targetPage = $rev->getTitle();
879 $targetQuery = 'oldid=' . $rev->getId();
882 '<div id="mw-diff-otitle1"><strong>' .
883 $sk->makeLinkObj( $targetPage,
884 wfMsgHtml( 'revisionasof',
885 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
887 ( $isDeleted ?
' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
889 '<div id="mw-diff-otitle2">' .
890 $sk->revUserTools( $rev ) . '<br/>' .
892 '<div id="mw-diff-otitle3">' .
893 $sk->revComment( $rev ) . '<br/>' .
898 * Show a deleted file version requested by the visitor.
900 private function showFile( $key ) {
901 global $wgOut, $wgRequest;
904 # We mustn't allow the output to be Squid cached, otherwise
905 # if an admin previews a deleted image, and it's cached, then
906 # a user without appropriate permissions can toddle off and
907 # nab the image, and Squid will serve it
908 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
909 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
910 $wgRequest->response()->header( 'Pragma: no-cache' );
912 $store = FileStore
::get( 'deleted' );
913 $store->stream( $key );
916 private function showHistory( ) {
917 global $wgLang, $wgUser, $wgOut;
919 $sk = $wgUser->getSkin();
920 if( $this->mAllowed
) {
921 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
923 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
926 $wgOut->addWikiText( wfMsgHtml( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText()) );
928 $archive = new PageArchive( $this->mTargetObj
);
930 $text = $archive->getLastRevisionText();
931 if( is_null( $text ) ) {
932 $wgOut->addWikiMsg( "nohistory" );
936 if ( $this->mAllowed
) {
937 $wgOut->addWikiMsg( "undeletehistory" );
938 $wgOut->addWikiMsg( "undeleterevdel" );
940 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
943 # List all stored revisions
944 $revisions = $archive->listRevisions();
945 $files = $archive->listFiles();
947 $haveRevisions = $revisions && $revisions->numRows() > 0;
948 $haveFiles = $files && $files->numRows() > 0;
950 # Batch existence check on user and talk pages
951 if( $haveRevisions ) {
952 $batch = new LinkBatch();
953 while( $row = $revisions->fetchObject() ) {
954 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
955 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
958 $revisions->seek( 0 );
961 $batch = new LinkBatch();
962 while( $row = $files->fetchObject() ) {
963 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
964 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
970 if ( $this->mAllowed
) {
971 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
972 $action = $titleObj->getLocalURL( "action=submit" );
973 # Start the form here
974 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
975 $wgOut->addHtml( $top );
978 # Show relevant lines from the deletion log:
979 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
980 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
982 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
983 # Format the user-visible controls (comment field, submission button)
984 # in a nice little table
985 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
989 <td class='mw-input'>" .
990 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
991 'mw-undelete-unsuppress', $this->mUnsuppress
).
998 Xml
::openElement( 'fieldset' ) .
999 Xml
::element( 'legend', null, wfMsg( 'undelete-fieldset-title' ) ).
1000 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1003 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1007 <td class='mw-label'>" .
1008 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1010 <td class='mw-input'>" .
1011 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1016 <td class='mw-submit'>" .
1017 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
1018 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
1019 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1023 Xml
::closeElement( 'table' ) .
1024 Xml
::closeElement( 'fieldset' );
1026 $wgOut->addHtml( $table );
1029 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1031 if( $haveRevisions ) {
1032 # The page's stored (deleted) history:
1033 $wgOut->addHTML("<ul>");
1034 $target = urlencode( $this->mTarget
);
1035 $remaining = $revisions->numRows();
1036 $earliestLiveTime = $this->getEarliestTime( $this->mTargetObj
);
1038 while( $row = $revisions->fetchObject() ) {
1040 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1043 $wgOut->addHTML("</ul>");
1045 $wgOut->addWikiMsg( "nohistory" );
1049 $wgOut->addHtml( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1050 $wgOut->addHtml( "<ul>" );
1051 while( $row = $files->fetchObject() ) {
1052 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1055 $wgOut->addHTML( "</ul>" );
1058 if ( $this->mAllowed
) {
1059 # Slip in the hidden controls here
1060 $misc = Xml
::hidden( 'target', $this->mTarget
);
1061 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1062 $misc .= Xml
::closeElement( 'form' );
1063 $wgOut->addHtml( $misc );
1069 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1070 global $wgUser, $wgLang;
1072 $rev = new Revision( array(
1073 'page' => $this->mTargetObj
->getArticleId(),
1074 'comment' => $row->ar_comment
,
1075 'user' => $row->ar_user
,
1076 'user_text' => $row->ar_user_text
,
1077 'timestamp' => $row->ar_timestamp
,
1078 'minor_edit' => $row->ar_minor_edit
,
1079 'deleted' => $row->ar_deleted
,
1080 'len' => $row->ar_len
) );
1083 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1084 if( $this->mAllowed
) {
1085 if( $this->mInvert
){
1086 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1087 $checkBox = Xml
::check( "ts$ts");
1089 $checkBox = Xml
::check( "ts$ts", true );
1092 $checkBox = Xml
::check( "ts$ts" );
1094 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1095 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1097 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1098 $last = wfMsgHtml('diff');
1099 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1100 $last = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml('diff'),
1101 "target=" . $this->mTargetObj
->getPrefixedUrl() . "×tamp=$ts&diff=prev" );
1103 $last = wfMsgHtml('diff');
1107 $pageLink = $wgLang->timeanddate( $ts, true );
1108 $last = wfMsgHtml('diff');
1110 $userLink = $sk->revUserTools( $rev );
1112 if(!is_null($size = $row->ar_len
)) {
1113 $stxt = $sk->formatRevisionSize( $size );
1115 $comment = $sk->revComment( $rev );
1117 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1118 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1119 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1120 // If revision was hidden from sysops
1121 $del = wfMsgHtml('rev-delundel');
1123 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1124 $del = $sk->makeKnownLinkObj( $revdel,
1125 wfMsgHtml('rev-delundel'),
1126 'target=' . $this->mTargetObj
->getPrefixedUrl() . "&artimestamp=$ts" );
1127 // Bolden oversighted content
1128 if( $rev->isDeleted( Revision
::DELETED_RESTRICTED
) )
1129 $del = "<strong>$del</strong>";
1131 $revdlink = "<tt>(<small>$del</small>)</tt>";
1134 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1137 private function formatFileRow( $row, $sk ) {
1138 global $wgUser, $wgLang;
1140 $file = ArchivedFile
::newFromRow( $row );
1142 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1143 if( $this->mAllowed
&& $row->fa_storage_key
) {
1144 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1145 $key = urlencode( $row->fa_storage_key
);
1146 $target = urlencode( $this->mTarget
);
1147 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1148 $pageLink = $this->getFileLink( $file, $titleObj, $ts, $key, $sk );
1151 $pageLink = $wgLang->timeanddate( $ts, true );
1153 $userLink = $this->getFileUser( $file, $sk );
1155 wfMsg( 'widthheight',
1156 $wgLang->formatNum( $row->fa_width
),
1157 $wgLang->formatNum( $row->fa_height
) ) .
1159 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1161 $data = htmlspecialchars( $data );
1162 $comment = $this->getFileComment( $file, $sk );
1164 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1165 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1166 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1167 // If revision was hidden from sysops
1168 $del = wfMsgHtml('rev-delundel');
1170 $del = $sk->makeKnownLinkObj( $revdel,
1171 wfMsgHtml('rev-delundel'),
1172 'target=' . $this->mTargetObj
->getPrefixedUrl() .
1173 '&fileid=' . $row->fa_id
);
1174 // Bolden oversighted content
1175 if( $file->isDeleted( File
::DELETED_RESTRICTED
) )
1176 $del = "<strong>$del</strong>";
1178 $revdlink = "<tt>(<small>$del</small>)</tt>";
1180 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1183 private function getEarliestTime( $title ) {
1184 $dbr = wfGetDB( DB_SLAVE
);
1185 if( $title->exists() ) {
1186 $min = $dbr->selectField( 'revision',
1187 'MIN(rev_timestamp)',
1188 array( 'rev_page' => $title->getArticleId() ),
1190 return wfTimestampOrNull( TS_MW
, $min );
1196 * Fetch revision text link if it's available to all users
1199 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1202 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1203 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1205 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1206 "target=".$this->mTargetObj
->getPrefixedUrl()."×tamp=$ts" );
1207 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1208 $link = '<span class="history-deleted">' . $link . '</span>';
1214 * Fetch image view link if it's available to all users
1217 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1220 if( !$file->userCan(File
::DELETED_FILE
) ) {
1221 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1223 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1224 "target=".$this->mTargetObj
->getPrefixedUrl()."&file=$key" );
1225 if( $file->isDeleted(File
::DELETED_FILE
) )
1226 $link = '<span class="history-deleted">' . $link . '</span>';
1232 * Fetch file's user id if it's available to this user
1235 function getFileUser( $file, $sk ) {
1236 if( !$file->userCan(File
::DELETED_USER
) ) {
1237 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1239 $link = $sk->userLink( $file->getUser(), $file->getUserText() ) .
1240 $sk->userToolLinks( $file->getUser(), $file->getUserText() );
1241 if( $file->isDeleted(File
::DELETED_USER
) )
1242 $link = '<span class="history-deleted">' . $link . '</span>';
1248 * Fetch file upload comment if it's available to this user
1251 function getFileComment( $file, $sk ) {
1252 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1253 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1255 $link = $sk->commentBlock( $file->getRawDescription() );
1256 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1257 $link = '<span class="history-deleted">' . $link . '</span>';
1262 function undelete() {
1263 global $wgOut, $wgUser;
1264 if ( wfReadOnly() ) {
1265 $wgOut->readOnlyPage();
1268 if( !is_null( $this->mTargetObj
) ) {
1269 $archive = new PageArchive( $this->mTargetObj
);
1270 $ok = $archive->undelete(
1271 $this->mTargetTimestamp
,
1273 $this->mFileVersions
,
1274 $this->mUnsuppress
);
1276 if( is_array($ok) ) {
1277 if ( $ok[1] ) // Undeleted file count
1278 wfRunHooks( 'FileUndeleteComplete', array(
1279 $this->mTargetObj
, $this->mFileVersions
,
1280 $wgUser, $this->mComment
) );
1282 $skin = $wgUser->getSkin();
1283 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
1284 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1286 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1287 $wgOut->addHtml( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1290 // Show file deletion warnings and errors
1291 $status = $archive->getFileStatus();
1292 if( $status && !$status->isGood() ) {
1293 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1296 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );