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(),
464 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
466 $ret = $dbw->resultObject( $result );
467 $rev_count = $dbw->numRows( $result );
470 $newid = $article->insertOn( $dbw );
477 while( $row = $ret->fetchObject() ) {
478 if( $row->ar_text_id
) {
479 // Revision was deleted in 1.5+; text is in
480 // the regular text table, use the reference.
481 // Specify null here so the so the text is
482 // dereferenced for page length info if needed.
485 // Revision was deleted in 1.4 or earlier.
486 // Text is squashed into the archive row, and
487 // a new text table entry will be created for it.
488 $revText = Revision
::getRevisionText( $row, 'ar_' );
490 // Check for key dupes due to shitty archive integrity.
491 if( $row->ar_rev_id
) {
492 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
493 if( $exists ) continue; // don't throw DB errors
496 $revision = new Revision( array(
498 'id' => $row->ar_rev_id
,
500 'comment' => $row->ar_comment
,
501 'user' => $row->ar_user
,
502 'user_text' => $row->ar_user_text
,
503 'timestamp' => $row->ar_timestamp
,
504 'minor_edit' => $row->ar_minor_edit
,
505 'text_id' => $row->ar_text_id
,
506 'deleted' => $unsuppress ?
0 : $row->ar_deleted
,
507 'len' => $row->ar_len
509 $revision->insertOn( $dbw );
512 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
514 # Now that it's safely stored, take it out of the archive
515 $dbw->delete( 'archive',
517 'ar_namespace' => $this->title
->getNamespace(),
518 'ar_title' => $this->title
->getDBkey(),
522 // Was anything restored at all?
527 // Attach the latest revision to the page...
528 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
529 if( $newid ||
$wasnew ) {
530 // Update site stats, link tables, etc
531 $article->createUpdates( $revision );
532 // We don't handle well with top revision deleted
533 if( $revision->getVisibility() ) {
534 $dbw->update( 'revision',
535 array( 'rev_deleted' => 0 ),
536 array( 'rev_id' => $revision->getId() ),
543 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true ) );
544 Article
::onArticleCreate( $this->title
);
546 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false ) );
547 Article
::onArticleEdit( $this->title
);
550 if( $this->title
->getNamespace() == NS_IMAGE
) {
551 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
555 // Revision couldn't be created. This is very weird
556 return self
::UNDELETE_UNKNOWNERR
;
562 function getFileStatus() { return $this->fileStatus
; }
566 * The HTML form for Special:Undelete, which allows users with the appropriate
567 * permissions to view and restore deleted content.
568 * @ingroup SpecialPage
571 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
572 var $mTargetTimestamp, $mAllowed, $mComment;
574 function UndeleteForm( $request, $par = "" ) {
576 $this->mAction
= $request->getVal( 'action' );
577 $this->mTarget
= $request->getVal( 'target' );
578 $this->mSearchPrefix
= $request->getText( 'prefix' );
579 $time = $request->getVal( 'timestamp' );
580 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
581 $this->mFile
= $request->getVal( 'file' );
583 $posted = $request->wasPosted() &&
584 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
585 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
586 $this->mInvert
= $request->getCheck( 'invert' ) && $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( 'suppressrevision' );
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 ||
$this->mInvert
) {
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( 'suppressrevision' );
658 return $this->showFile( $this->mFile
);
661 if( $this->mRestore
&& $this->mAction
== "submit" ) {
662 return $this->undelete();
664 if( $this->mInvert
&& $this->mAction
== "submit" ) {
665 return $this->showHistory( );
667 return $this->showHistory();
670 function showSearchForm() {
671 global $wgOut, $wgScript;
672 $wgOut->addWikiMsg( 'undelete-header' );
675 Xml
::openElement( 'form', array(
677 'action' => $wgScript ) ) .
679 Xml
::element( 'legend', array(),
680 wfMsg( 'undelete-search-box' ) ) .
681 Xml
::hidden( 'title',
682 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
683 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
684 'prefix', 'prefix', 20,
685 $this->mSearchPrefix
) .
686 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
691 // Generic list of deleted pages
692 private function showList( $result ) {
693 global $wgLang, $wgContLang, $wgUser, $wgOut;
695 if( $result->numRows() == 0 ) {
696 $wgOut->addWikiMsg( 'undelete-no-results' );
700 $wgOut->addWikiMsg( "undeletepagetext" );
702 $sk = $wgUser->getSkin();
703 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
704 $wgOut->addHTML( "<ul>\n" );
705 while( $row = $result->fetchObject() ) {
706 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
707 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ),
708 'target=' . $title->getPrefixedUrl() );
709 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
710 $revs = wfMsgExt( 'undeleterevisions',
711 array( 'parseinline' ),
712 $wgLang->formatNum( $row->count
) );
713 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
716 $wgOut->addHTML( "</ul>\n" );
721 private function showRevision( $timestamp ) {
722 global $wgLang, $wgUser, $wgOut;
723 $self = SpecialPage
::getTitleFor( 'Undelete' );
724 $skin = $wgUser->getSkin();
726 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
728 $archive = new PageArchive( $this->mTargetObj
);
729 $rev = $archive->getRevision( $timestamp );
732 $wgOut->addWikiMsg( 'undeleterevision-missing' );
736 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
737 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
738 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-permission' ) );
741 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-view' ) );
742 $wgOut->addHTML( '<br/>' );
743 // and we are allowed to see...
747 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
749 $link = $skin->makeKnownLinkObj(
750 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
751 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
755 $previousRev = $archive->getPreviousRevision( $timestamp );
757 $this->showDiff( $previousRev, $rev );
758 if( $wgUser->getOption( 'diffonly' ) ) {
761 $wgOut->addHTML( '<hr />' );
764 $wgOut->addHTML( wfMsgHtml( 'undelete-nodiff' ) );
768 // date and time are separate parameters to facilitate localisation.
769 // $time is kept for backward compat reasons.
770 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
771 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
772 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
773 $user = $skin->revUserTools( $rev );
775 $wgOut->addHTML( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</p>' );
777 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
779 if( $this->mPreview
) {
780 $wgOut->addHTML( "<hr />\n" );
783 $popts = $wgOut->parserOptions();
784 $popts->setEditSection( false );
785 $wgOut->parserOptions( $popts );
786 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
790 wfElement( 'textarea', array(
791 'readonly' => 'readonly',
792 'cols' => intval( $wgUser->getOption( 'cols' ) ),
793 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
794 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
795 wfOpenElement( 'div' ) .
796 wfOpenElement( 'form', array(
798 'action' => $self->getLocalURL( "action=submit" ) ) ) .
799 wfElement( 'input', array(
802 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
803 wfElement( 'input', array(
805 'name' => 'timestamp',
806 'value' => $timestamp ) ) .
807 wfElement( 'input', array(
809 'name' => 'wpEditToken',
810 'value' => $wgUser->editToken() ) ) .
811 wfElement( 'input', array(
814 'value' => wfMsg( 'showpreview' ) ) ) .
815 wfElement( 'input', array(
818 'value' => wfMsg( 'showdiff' ) ) ) .
819 wfCloseElement( 'form' ) .
820 wfCloseElement( 'div' ) );
824 * Build a diff display between this and the previous either deleted
825 * or non-deleted edit.
826 * @param Revision $previousRev
827 * @param Revision $currentRev
828 * @return string HTML
830 function showDiff( $previousRev, $currentRev ) {
831 global $wgOut, $wgUser;
833 $diffEngine = new DifferenceEngine();
834 $diffEngine->showDiffStyle();
837 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
838 "<col class='diff-marker' />" .
839 "<col class='diff-content' />" .
840 "<col class='diff-marker' />" .
841 "<col class='diff-content' />" .
843 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
844 $this->diffHeader( $previousRev, 'o' ) .
846 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
847 $this->diffHeader( $currentRev, 'n' ) .
850 $diffEngine->generateDiffBody(
851 $previousRev->getText(), $currentRev->getText() ) .
857 private function diffHeader( $rev, $prefix ) {
858 global $wgUser, $wgLang, $wgLang;
859 $sk = $wgUser->getSkin();
860 $isDeleted = !( $rev->getId() && $rev->getTitle() );
862 /// @fixme $rev->getTitle() is null for deleted revs...?
863 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
864 $targetQuery = 'target=' .
865 $this->mTargetObj
->getPrefixedUrl() .
867 wfTimestamp( TS_MW
, $rev->getTimestamp() );
869 /// @fixme getId() may return non-zero for deleted revs...
870 $targetPage = $rev->getTitle();
871 $targetQuery = 'oldid=' . $rev->getId();
874 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
875 $sk->makeLinkObj( $targetPage,
876 wfMsgHtml( 'revisionasof',
877 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
879 ( $isDeleted ?
' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
881 '<div id="mw-diff-'.$prefix.'title2">' .
882 $sk->revUserTools( $rev ) . '<br/>' .
884 '<div id="mw-diff-'.$prefix.'title3">' .
885 $sk->revComment( $rev ) . '<br/>' .
890 * Show a deleted file version requested by the visitor.
892 private function showFile( $key ) {
893 global $wgOut, $wgRequest;
896 # We mustn't allow the output to be Squid cached, otherwise
897 # if an admin previews a deleted image, and it's cached, then
898 # a user without appropriate permissions can toddle off and
899 # nab the image, and Squid will serve it
900 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
901 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
902 $wgRequest->response()->header( 'Pragma: no-cache' );
904 $store = FileStore
::get( 'deleted' );
905 $store->stream( $key );
908 private function showHistory( ) {
909 global $wgLang, $wgUser, $wgOut;
911 $sk = $wgUser->getSkin();
912 if( $this->mAllowed
) {
913 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
915 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
918 $wgOut->addWikiText( wfMsgHtml( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText()) );
920 $archive = new PageArchive( $this->mTargetObj
);
922 $text = $archive->getLastRevisionText();
923 if( is_null( $text ) ) {
924 $wgOut->addWikiMsg( "nohistory" );
928 if ( $this->mAllowed
) {
929 $wgOut->addWikiMsg( "undeletehistory" );
930 $wgOut->addWikiMsg( "undeleterevdel" );
932 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
935 # List all stored revisions
936 $revisions = $archive->listRevisions();
937 $files = $archive->listFiles();
939 $haveRevisions = $revisions && $revisions->numRows() > 0;
940 $haveFiles = $files && $files->numRows() > 0;
942 # Batch existence check on user and talk pages
943 if( $haveRevisions ) {
944 $batch = new LinkBatch();
945 while( $row = $revisions->fetchObject() ) {
946 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
947 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
950 $revisions->seek( 0 );
953 $batch = new LinkBatch();
954 while( $row = $files->fetchObject() ) {
955 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
956 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
962 if ( $this->mAllowed
) {
963 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
964 $action = $titleObj->getLocalURL( "action=submit" );
965 # Start the form here
966 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
967 $wgOut->addHTML( $top );
970 # Show relevant lines from the deletion log:
971 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
972 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
974 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
975 # Format the user-visible controls (comment field, submission button)
976 # in a nice little table
977 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
981 <td class='mw-input'>" .
982 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
983 'mw-undelete-unsuppress', $this->mUnsuppress
).
990 Xml
::openElement( 'fieldset' ) .
991 Xml
::element( 'legend', null, wfMsg( 'undelete-fieldset-title' ) ).
992 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
995 wfMsgWikiHtml( 'undeleteextrahelp' ) .
999 <td class='mw-label'>" .
1000 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1002 <td class='mw-input'>" .
1003 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1008 <td class='mw-submit'>" .
1009 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
1010 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
1011 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1015 Xml
::closeElement( 'table' ) .
1016 Xml
::closeElement( 'fieldset' );
1018 $wgOut->addHTML( $table );
1021 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1023 if( $haveRevisions ) {
1024 # The page's stored (deleted) history:
1025 $wgOut->addHTML("<ul>");
1026 $target = urlencode( $this->mTarget
);
1027 $remaining = $revisions->numRows();
1028 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1030 while( $row = $revisions->fetchObject() ) {
1032 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1035 $wgOut->addHTML("</ul>");
1037 $wgOut->addWikiMsg( "nohistory" );
1041 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1042 $wgOut->addHTML( "<ul>" );
1043 while( $row = $files->fetchObject() ) {
1044 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1047 $wgOut->addHTML( "</ul>" );
1050 if ( $this->mAllowed
) {
1051 # Slip in the hidden controls here
1052 $misc = Xml
::hidden( 'target', $this->mTarget
);
1053 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1054 $misc .= Xml
::closeElement( 'form' );
1055 $wgOut->addHTML( $misc );
1061 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1062 global $wgUser, $wgLang;
1064 $rev = new Revision( array(
1065 'page' => $this->mTargetObj
->getArticleId(),
1066 'comment' => $row->ar_comment
,
1067 'user' => $row->ar_user
,
1068 'user_text' => $row->ar_user_text
,
1069 'timestamp' => $row->ar_timestamp
,
1070 'minor_edit' => $row->ar_minor_edit
,
1071 'deleted' => $row->ar_deleted
,
1072 'len' => $row->ar_len
) );
1075 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1076 if( $this->mAllowed
) {
1077 if( $this->mInvert
){
1078 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1079 $checkBox = Xml
::check( "ts$ts");
1081 $checkBox = Xml
::check( "ts$ts", true );
1084 $checkBox = Xml
::check( "ts$ts" );
1086 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1087 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1089 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1090 $last = wfMsgHtml('diff');
1091 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1092 $last = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml('diff'),
1093 "target=" . $this->mTargetObj
->getPrefixedUrl() . "×tamp=$ts&diff=prev" );
1095 $last = wfMsgHtml('diff');
1099 $pageLink = $wgLang->timeanddate( $ts, true );
1100 $last = wfMsgHtml('diff');
1102 $userLink = $sk->revUserTools( $rev );
1104 if(!is_null($size = $row->ar_len
)) {
1105 $stxt = $sk->formatRevisionSize( $size );
1107 $comment = $sk->revComment( $rev );
1109 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1110 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1111 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1112 // If revision was hidden from sysops
1113 $del = wfMsgHtml('rev-delundel');
1115 $del = $sk->makeKnownLinkObj( $revdel,
1116 wfMsgHtml('rev-delundel'),
1117 'target=' . $this->mTargetObj
->getPrefixedUrl() . "&artimestamp=$ts" );
1118 // Bolden oversighted content
1119 if( $rev->isDeleted( Revision
::DELETED_RESTRICTED
) )
1120 $del = "<strong>$del</strong>";
1122 $revdlink = "<tt>(<small>$del</small>)</tt>";
1125 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1128 private function formatFileRow( $row, $sk ) {
1129 global $wgUser, $wgLang;
1131 $file = ArchivedFile
::newFromRow( $row );
1133 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1134 if( $this->mAllowed
&& $row->fa_storage_key
) {
1135 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1136 $key = urlencode( $row->fa_storage_key
);
1137 $target = urlencode( $this->mTarget
);
1138 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1139 $pageLink = $this->getFileLink( $file, $titleObj, $ts, $key, $sk );
1142 $pageLink = $wgLang->timeanddate( $ts, true );
1144 $userLink = $this->getFileUser( $file, $sk );
1146 wfMsg( 'widthheight',
1147 $wgLang->formatNum( $row->fa_width
),
1148 $wgLang->formatNum( $row->fa_height
) ) .
1150 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1152 $data = htmlspecialchars( $data );
1153 $comment = $this->getFileComment( $file, $sk );
1155 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1156 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1157 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1158 // If revision was hidden from sysops
1159 $del = wfMsgHtml('rev-delundel');
1161 $del = $sk->makeKnownLinkObj( $revdel,
1162 wfMsgHtml('rev-delundel'),
1163 'target=' . $this->mTargetObj
->getPrefixedUrl() .
1164 '&fileid=' . $row->fa_id
);
1165 // Bolden oversighted content
1166 if( $file->isDeleted( File
::DELETED_RESTRICTED
) )
1167 $del = "<strong>$del</strong>";
1169 $revdlink = "<tt>(<small>$del</small>)</tt>";
1171 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1175 * Fetch revision text link if it's available to all users
1178 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1181 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1182 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1184 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1185 "target=".$this->mTargetObj
->getPrefixedUrl()."×tamp=$ts" );
1186 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1187 $link = '<span class="history-deleted">' . $link . '</span>';
1193 * Fetch image view link if it's available to all users
1196 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1199 if( !$file->userCan(File
::DELETED_FILE
) ) {
1200 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1202 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1203 "target=".$this->mTargetObj
->getPrefixedUrl()."&file=$key" );
1204 if( $file->isDeleted(File
::DELETED_FILE
) )
1205 $link = '<span class="history-deleted">' . $link . '</span>';
1211 * Fetch file's user id if it's available to this user
1214 function getFileUser( $file, $sk ) {
1215 if( !$file->userCan(File
::DELETED_USER
) ) {
1216 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1218 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1219 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1220 if( $file->isDeleted(File
::DELETED_USER
) )
1221 $link = '<span class="history-deleted">' . $link . '</span>';
1227 * Fetch file upload comment if it's available to this user
1230 function getFileComment( $file, $sk ) {
1231 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1232 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1234 $link = $sk->commentBlock( $file->getRawDescription() );
1235 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1236 $link = '<span class="history-deleted">' . $link . '</span>';
1241 function undelete() {
1242 global $wgOut, $wgUser;
1243 if ( wfReadOnly() ) {
1244 $wgOut->readOnlyPage();
1247 if( !is_null( $this->mTargetObj
) ) {
1248 $archive = new PageArchive( $this->mTargetObj
);
1249 $ok = $archive->undelete(
1250 $this->mTargetTimestamp
,
1252 $this->mFileVersions
,
1253 $this->mUnsuppress
);
1255 if( is_array($ok) ) {
1256 if ( $ok[1] ) // Undeleted file count
1257 wfRunHooks( 'FileUndeleteComplete', array(
1258 $this->mTargetObj
, $this->mFileVersions
,
1259 $wgUser, $this->mComment
) );
1261 $skin = $wgUser->getSkin();
1262 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
1263 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1265 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1266 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1269 // Show file deletion warnings and errors
1270 $status = $archive->getFileStatus();
1271 if( $status && !$status->isGood() ) {
1272 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1275 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );