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, $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->mPreview
= $request->getCheck( 'preview' ) && $posted;
595 $this->mDiff
= $request->getCheck( 'diff' );
596 $this->mComment
= $request->getText( 'wpComment' );
597 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
600 $this->mTarget
= $par;
602 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
603 $this->mAllowed
= true;
605 $this->mAllowed
= false;
606 $this->mTimestamp
= '';
607 $this->mRestore
= false;
609 if ( $this->mTarget
!== "" ) {
610 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
612 $this->mTargetObj
= NULL;
614 if( $this->mRestore
) {
615 $timestamps = array();
616 $this->mFileVersions
= array();
617 foreach( $_REQUEST as $key => $val ) {
619 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
620 array_push( $timestamps, $matches[1] );
623 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
624 $this->mFileVersions
[] = intval( $matches[1] );
627 rsort( $timestamps );
628 $this->mTargetTimestamp
= $timestamps;
633 global $wgOut, $wgUser;
634 if ( $this->mAllowed
) {
635 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
637 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
640 if( is_null( $this->mTargetObj
) ) {
641 # Not all users can just browse every deleted page from the list
642 if( $wgUser->isAllowed( 'browsearchive' ) ) {
643 $this->showSearchForm();
645 # List undeletable articles
646 if( $this->mSearchPrefix
) {
647 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
648 $this->showList( $result );
651 $wgOut->addWikiText( wfMsgHtml( 'undelete-header' ) );
655 if( $this->mTimestamp
!== '' ) {
656 return $this->showRevision( $this->mTimestamp
);
658 if( $this->mFile
!== null ) {
659 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
660 // Check if user is allowed to see this file
661 if( !$file->userCan( File
::DELETED_FILE
) ) {
662 $wgOut->permissionRequired( 'suppressrevision' );
665 return $this->showFile( $this->mFile
);
668 if( $this->mRestore
&& $this->mAction
== "submit" ) {
669 return $this->undelete();
671 return $this->showHistory();
674 function showSearchForm() {
675 global $wgOut, $wgScript;
676 $wgOut->addWikiMsg( 'undelete-header' );
679 Xml
::openElement( 'form', array(
681 'action' => $wgScript ) ) .
683 Xml
::element( 'legend', array(),
684 wfMsg( 'undelete-search-box' ) ) .
685 Xml
::hidden( 'title',
686 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
687 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
688 'prefix', 'prefix', 20,
689 $this->mSearchPrefix
) .
690 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
695 // Generic list of deleted pages
696 private function showList( $result ) {
697 global $wgLang, $wgContLang, $wgUser, $wgOut;
699 if( $result->numRows() == 0 ) {
700 $wgOut->addWikiMsg( 'undelete-no-results' );
704 $wgOut->addWikiMsg( "undeletepagetext" );
706 $sk = $wgUser->getSkin();
707 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
708 $wgOut->addHTML( "<ul>\n" );
709 while( $row = $result->fetchObject() ) {
710 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
711 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ),
712 'target=' . $title->getPrefixedUrl() );
713 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
714 $revs = wfMsgExt( 'undeleterevisions',
715 array( 'parseinline' ),
716 $wgLang->formatNum( $row->count
) );
717 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
720 $wgOut->addHTML( "</ul>\n" );
725 private function showRevision( $timestamp ) {
726 global $wgLang, $wgUser, $wgOut;
727 $self = SpecialPage
::getTitleFor( 'Undelete' );
728 $skin = $wgUser->getSkin();
730 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
732 $archive = new PageArchive( $this->mTargetObj
);
733 $rev = $archive->getRevision( $timestamp );
736 $wgOut->addWikiMsg( 'undeleterevision-missing' );
740 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
741 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
742 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-permission' ) );
745 $wgOut->addWikiText( wfMsg( 'rev-deleted-text-view' ) );
746 $wgOut->addHTML( '<br/>' );
747 // and we are allowed to see...
751 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
753 $link = $skin->makeKnownLinkObj(
754 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
755 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
759 $previousRev = $archive->getPreviousRevision( $timestamp );
761 $this->showDiff( $previousRev, $rev );
762 if( $wgUser->getOption( 'diffonly' ) ) {
765 $wgOut->addHtml( '<hr />' );
768 $wgOut->addHtml( wfMsgHtml( 'undelete-nodiff' ) );
772 // date and time are separate parameters to facilitate localisation.
773 // $time is kept for backward compat reasons.
774 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
775 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
776 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
777 $user = $skin->revUserTools( $rev );
779 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</p>' );
781 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
783 if( $this->mPreview
) {
784 $wgOut->addHtml( "<hr />\n" );
787 $popts = $wgOut->parserOptions();
788 $popts->setEditSection( false );
789 $wgOut->parserOptions( $popts );
790 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
794 wfElement( 'textarea', array(
795 'readonly' => 'readonly',
796 'cols' => intval( $wgUser->getOption( 'cols' ) ),
797 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
798 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
799 wfOpenElement( 'div' ) .
800 wfOpenElement( 'form', array(
802 'action' => $self->getLocalURL( "action=submit" ) ) ) .
803 wfElement( 'input', array(
806 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
807 wfElement( 'input', array(
809 'name' => 'timestamp',
810 'value' => $timestamp ) ) .
811 wfElement( 'input', array(
813 'name' => 'wpEditToken',
814 'value' => $wgUser->editToken() ) ) .
815 wfElement( 'input', array(
818 'value' => wfMsg( 'showpreview' ) ) ) .
819 wfElement( 'input', array(
822 'value' => wfMsg( 'showdiff' ) ) ) .
823 wfCloseElement( 'form' ) .
824 wfCloseElement( 'div' ) );
828 * Build a diff display between this and the previous either deleted
829 * or non-deleted edit.
830 * @param Revision $previousRev
831 * @param Revision $currentRev
832 * @return string HTML
834 function showDiff( $previousRev, $currentRev ) {
835 global $wgOut, $wgUser;
837 $diffEngine = new DifferenceEngine();
838 $diffEngine->showDiffStyle();
841 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
842 "<col class='diff-marker' />" .
843 "<col class='diff-content' />" .
844 "<col class='diff-marker' />" .
845 "<col class='diff-content' />" .
847 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
848 $this->diffHeader( $previousRev ) .
850 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
851 $this->diffHeader( $currentRev ) .
854 $diffEngine->generateDiffBody(
855 $previousRev->getText(), $currentRev->getText() ) .
861 private function diffHeader( $rev ) {
862 global $wgUser, $wgLang, $wgLang;
863 $sk = $wgUser->getSkin();
864 $isDeleted = !( $rev->getId() && $rev->getTitle() );
866 /// @fixme $rev->getTitle() is null for deleted revs...?
867 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
868 $targetQuery = 'target=' .
869 $this->mTargetObj
->getPrefixedUrl() .
871 wfTimestamp( TS_MW
, $rev->getTimestamp() );
873 /// @fixme getId() may return non-zero for deleted revs...
874 $targetPage = $rev->getTitle();
875 $targetQuery = 'oldid=' . $rev->getId();
878 '<div id="mw-diff-otitle1"><strong>' .
879 $sk->makeLinkObj( $targetPage,
880 wfMsgHtml( 'revisionasof',
881 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
883 ( $isDeleted ?
' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
885 '<div id="mw-diff-otitle2">' .
886 $sk->revUserTools( $rev ) . '<br/>' .
888 '<div id="mw-diff-otitle3">' .
889 $sk->revComment( $rev ) . '<br/>' .
894 * Show a deleted file version requested by the visitor.
896 private function showFile( $key ) {
897 global $wgOut, $wgRequest;
900 # We mustn't allow the output to be Squid cached, otherwise
901 # if an admin previews a deleted image, and it's cached, then
902 # a user without appropriate permissions can toddle off and
903 # nab the image, and Squid will serve it
904 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
905 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
906 $wgRequest->response()->header( 'Pragma: no-cache' );
908 $store = FileStore
::get( 'deleted' );
909 $store->stream( $key );
912 private function showHistory() {
913 global $wgLang, $wgUser, $wgOut;
915 $sk = $wgUser->getSkin();
916 if( $this->mAllowed
) {
917 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
919 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
922 $wgOut->addWikiText( wfMsgHtml( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText()) );
924 $archive = new PageArchive( $this->mTargetObj
);
926 $text = $archive->getLastRevisionText();
927 if( is_null( $text ) ) {
928 $wgOut->addWikiMsg( "nohistory" );
932 if ( $this->mAllowed
) {
933 $wgOut->addWikiMsg( "undeletehistory" );
934 $wgOut->addWikiMsg( "undeleterevdel" );
936 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
939 # List all stored revisions
940 $revisions = $archive->listRevisions();
941 $files = $archive->listFiles();
943 $haveRevisions = $revisions && $revisions->numRows() > 0;
944 $haveFiles = $files && $files->numRows() > 0;
946 # Batch existence check on user and talk pages
947 if( $haveRevisions ) {
948 $batch = new LinkBatch();
949 while( $row = $revisions->fetchObject() ) {
950 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
951 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
954 $revisions->seek( 0 );
957 $batch = new LinkBatch();
958 while( $row = $files->fetchObject() ) {
959 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
960 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
966 if ( $this->mAllowed
) {
967 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
968 $action = $titleObj->getLocalURL( "action=submit" );
969 # Start the form here
970 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
971 $wgOut->addHtml( $top );
974 # Show relevant lines from the deletion log:
975 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
976 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
978 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
979 # Format the user-visible controls (comment field, submission button)
980 # in a nice little table
981 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
985 <td class='mw-input'>" .
986 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
987 'mw-undelete-unsuppress', $this->mUnsuppress
).
994 Xml
::openElement( 'fieldset' ) .
995 Xml
::element( 'legend', null, wfMsg( 'undelete-fieldset-title' ) ).
996 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
999 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1003 <td class='mw-label'>" .
1004 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1006 <td class='mw-input'>" .
1007 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1012 <td class='mw-submit'>" .
1013 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
1014 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
1018 Xml
::closeElement( 'table' ) .
1019 Xml
::closeElement( 'fieldset' );
1021 $wgOut->addHtml( $table );
1024 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1026 if( $haveRevisions ) {
1027 # The page's stored (deleted) history:
1028 $wgOut->addHTML("<ul>");
1029 $target = urlencode( $this->mTarget
);
1030 $remaining = $revisions->numRows();
1031 $earliestLiveTime = $this->getEarliestTime( $this->mTargetObj
);
1033 while( $row = $revisions->fetchObject() ) {
1035 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1038 $wgOut->addHTML("</ul>");
1040 $wgOut->addWikiMsg( "nohistory" );
1044 $wgOut->addHtml( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1045 $wgOut->addHtml( "<ul>" );
1046 while( $row = $files->fetchObject() ) {
1047 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1050 $wgOut->addHTML( "</ul>" );
1053 if ( $this->mAllowed
) {
1054 # Slip in the hidden controls here
1055 $misc = Xml
::hidden( 'target', $this->mTarget
);
1056 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1057 $misc .= Xml
::closeElement( 'form' );
1058 $wgOut->addHtml( $misc );
1064 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1065 global $wgUser, $wgLang;
1067 $rev = new Revision( array(
1068 'page' => $this->mTargetObj
->getArticleId(),
1069 'comment' => $row->ar_comment
,
1070 'user' => $row->ar_user
,
1071 'user_text' => $row->ar_user_text
,
1072 'timestamp' => $row->ar_timestamp
,
1073 'minor_edit' => $row->ar_minor_edit
,
1074 'deleted' => $row->ar_deleted
,
1075 'len' => $row->ar_len
) );
1078 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1079 if( $this->mAllowed
) {
1080 $checkBox = Xml
::check( "ts$ts" );
1081 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1082 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1084 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1085 $last = wfMsgHtml('diff');
1086 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1087 $last = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml('diff'),
1088 "target=" . $this->mTargetObj
->getPrefixedUrl() . "×tamp=$ts&diff=prev" );
1090 $last = wfMsgHtml('diff');
1094 $pageLink = $wgLang->timeanddate( $ts, true );
1095 $last = wfMsgHtml('diff');
1097 $userLink = $sk->revUserTools( $rev );
1099 if(!is_null($size = $row->ar_len
)) {
1100 $stxt = $sk->formatRevisionSize( $size );
1102 $comment = $sk->revComment( $rev );
1104 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1105 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1106 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1107 // If revision was hidden from sysops
1108 $del = wfMsgHtml('rev-delundel');
1110 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1111 $del = $sk->makeKnownLinkObj( $revdel,
1112 wfMsgHtml('rev-delundel'),
1113 'target=' . $this->mTargetObj
->getPrefixedUrl() . "&artimestamp=$ts" );
1114 // Bolden oversighted content
1115 if( $rev->isDeleted( Revision
::DELETED_RESTRICTED
) )
1116 $del = "<strong>$del</strong>";
1118 $revdlink = "<tt>(<small>$del</small>)</tt>";
1121 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1124 private function formatFileRow( $row, $sk ) {
1125 global $wgUser, $wgLang;
1127 $file = ArchivedFile
::newFromRow( $row );
1129 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1130 if( $this->mAllowed
&& $row->fa_storage_key
) {
1131 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1132 $key = urlencode( $row->fa_storage_key
);
1133 $target = urlencode( $this->mTarget
);
1134 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1135 $pageLink = $this->getFileLink( $file, $titleObj, $ts, $key, $sk );
1138 $pageLink = $wgLang->timeanddate( $ts, true );
1140 $userLink = $this->getFileUser( $file, $sk );
1142 wfMsg( 'widthheight',
1143 $wgLang->formatNum( $row->fa_width
),
1144 $wgLang->formatNum( $row->fa_height
) ) .
1146 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1148 $data = htmlspecialchars( $data );
1149 $comment = $this->getFileComment( $file, $sk );
1151 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1152 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1153 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1154 // If revision was hidden from sysops
1155 $del = wfMsgHtml('rev-delundel');
1157 $del = $sk->makeKnownLinkObj( $revdel,
1158 wfMsgHtml('rev-delundel'),
1159 'target=' . $this->mTargetObj
->getPrefixedUrl() .
1160 '&fileid=' . $row->fa_id
);
1161 // Bolden oversighted content
1162 if( $file->isDeleted( File
::DELETED_RESTRICTED
) )
1163 $del = "<strong>$del</strong>";
1165 $revdlink = "<tt>(<small>$del</small>)</tt>";
1167 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1170 private function getEarliestTime( $title ) {
1171 $dbr = wfGetDB( DB_SLAVE
);
1172 if( $title->exists() ) {
1173 $min = $dbr->selectField( 'revision',
1174 'MIN(rev_timestamp)',
1175 array( 'rev_page' => $title->getArticleId() ),
1177 return wfTimestampOrNull( TS_MW
, $min );
1183 * Fetch revision text link if it's available to all users
1186 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1189 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1190 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1192 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1193 "target=".$this->mTargetObj
->getPrefixedUrl()."×tamp=$ts" );
1194 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1195 $link = '<span class="history-deleted">' . $link . '</span>';
1201 * Fetch image view link if it's available to all users
1204 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1207 if( !$file->userCan(File
::DELETED_FILE
) ) {
1208 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1210 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1211 "target=".$this->mTargetObj
->getPrefixedUrl()."&file=$key" );
1212 if( $file->isDeleted(File
::DELETED_FILE
) )
1213 $link = '<span class="history-deleted">' . $link . '</span>';
1219 * Fetch file's user id if it's available to this user
1222 function getFileUser( $file, $sk ) {
1223 if( !$file->userCan(File
::DELETED_USER
) ) {
1224 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1226 $link = $sk->userLink( $file->getUser(), $file->getUserText() ) .
1227 $sk->userToolLinks( $file->getUser(), $file->getUserText() );
1228 if( $file->isDeleted(File
::DELETED_USER
) )
1229 $link = '<span class="history-deleted">' . $link . '</span>';
1235 * Fetch file upload comment if it's available to this user
1238 function getFileComment( $file, $sk ) {
1239 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1240 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1242 $link = $sk->commentBlock( $file->getRawDescription() );
1243 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1244 $link = '<span class="history-deleted">' . $link . '</span>';
1249 function undelete() {
1250 global $wgOut, $wgUser;
1251 if ( wfReadOnly() ) {
1252 $wgOut->readOnlyPage();
1255 if( !is_null( $this->mTargetObj
) ) {
1256 $archive = new PageArchive( $this->mTargetObj
);
1257 $ok = $archive->undelete(
1258 $this->mTargetTimestamp
,
1260 $this->mFileVersions
,
1261 $this->mUnsuppress
);
1263 if( is_array($ok) ) {
1264 if ( $ok[1] ) // Undeleted file count
1265 wfRunHooks( 'FileUndeleteComplete', array(
1266 $this->mTargetObj
, $this->mFileVersions
,
1267 $wgUser, $this->mComment
) );
1269 $skin = $wgUser->getSkin();
1270 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
1271 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1273 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1274 $wgOut->addHtml( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1277 // Show file deletion warnings and errors
1278 $status = $archive->getFileStatus();
1279 if( $status && !$status->isGood() ) {
1280 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1283 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );