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_FILE
) {
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_FILE
) {
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_FILE
) {
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, $mToken;
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' );
591 $this->mToken
= $request->getVal( 'token' );
594 $this->mTarget
= $par;
596 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
597 $this->mAllowed
= true;
599 $this->mAllowed
= false;
600 $this->mTimestamp
= '';
601 $this->mRestore
= false;
603 if ( $this->mTarget
!== "" ) {
604 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
606 $this->mTargetObj
= NULL;
608 if( $this->mRestore ||
$this->mInvert
) {
609 $timestamps = array();
610 $this->mFileVersions
= array();
611 foreach( $_REQUEST as $key => $val ) {
613 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
614 array_push( $timestamps, $matches[1] );
617 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
618 $this->mFileVersions
[] = intval( $matches[1] );
621 rsort( $timestamps );
622 $this->mTargetTimestamp
= $timestamps;
627 global $wgOut, $wgUser;
628 if ( $this->mAllowed
) {
629 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
631 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
634 if( is_null( $this->mTargetObj
) ) {
635 # Not all users can just browse every deleted page from the list
636 if( $wgUser->isAllowed( 'browsearchive' ) ) {
637 $this->showSearchForm();
639 # List undeletable articles
640 if( $this->mSearchPrefix
) {
641 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
642 $this->showList( $result );
645 $wgOut->addWikiMsg( 'undelete-header' );
649 if( $this->mTimestamp
!== '' ) {
650 return $this->showRevision( $this->mTimestamp
);
652 if( $this->mFile
!== null ) {
653 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
654 // Check if user is allowed to see this file
655 if( !$file->userCan( File
::DELETED_FILE
) ) {
656 $wgOut->permissionRequired( 'suppressrevision' );
658 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
659 $this->showFileConfirmationForm( $this->mFile
);
662 return $this->showFile( $this->mFile
);
665 if( $this->mRestore
&& $this->mAction
== "submit" ) {
666 return $this->undelete();
668 if( $this->mInvert
&& $this->mAction
== "submit" ) {
669 return $this->showHistory( );
671 return $this->showHistory();
674 function showSearchForm() {
675 global $wgOut, $wgScript;
676 $wgOut->addWikiMsg( 'undelete-header' );
679 Xml
::openElement( 'form', array(
681 'action' => $wgScript ) ) .
682 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
683 Xml
::hidden( 'title',
684 SpecialPage
::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
685 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
686 'prefix', 'prefix', 20,
687 $this->mSearchPrefix
) . ' ' .
688 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
689 Xml
::closeElement( 'fieldset' ) .
690 Xml
::closeElement( 'form' )
694 // Generic list of deleted pages
695 private function showList( $result ) {
696 global $wgLang, $wgContLang, $wgUser, $wgOut;
698 if( $result->numRows() == 0 ) {
699 $wgOut->addWikiMsg( 'undelete-no-results' );
703 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
705 $sk = $wgUser->getSkin();
706 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
707 $wgOut->addHTML( "<ul>\n" );
708 while( $row = $result->fetchObject() ) {
709 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
710 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ),
711 'target=' . $title->getPrefixedUrl() );
712 $revs = wfMsgExt( 'undeleterevisions',
713 array( 'parseinline' ),
714 $wgLang->formatNum( $row->count
) );
715 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
718 $wgOut->addHTML( "</ul>\n" );
723 private function showRevision( $timestamp ) {
724 global $wgLang, $wgUser, $wgOut;
725 $self = SpecialPage
::getTitleFor( 'Undelete' );
726 $skin = $wgUser->getSkin();
728 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
730 $archive = new PageArchive( $this->mTargetObj
);
731 $rev = $archive->getRevision( $timestamp );
734 $wgOut->addWikiMsg( 'undeleterevision-missing' );
738 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
739 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
740 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-permission' );
743 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-view' );
744 $wgOut->addHTML( '<br/>' );
745 // and we are allowed to see...
749 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
751 $link = $skin->makeKnownLinkObj(
752 SpecialPage
::getTitleFor( 'Undelete', $this->mTargetObj
->getPrefixedDBkey() ),
753 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
757 $previousRev = $archive->getPreviousRevision( $timestamp );
759 $this->showDiff( $previousRev, $rev );
760 if( $wgUser->getOption( 'diffonly' ) ) {
763 $wgOut->addHTML( '<hr />' );
766 $wgOut->addWikiMsg( 'undelete-nodiff' );
770 // date and time are separate parameters to facilitate localisation.
771 // $time is kept for backward compat reasons.
772 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
773 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
774 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
775 $user = $skin->revUserTools( $rev );
777 if( $this->mPreview
) {
778 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
780 $openDiv = '<div id="mw-undelete-revision">';
783 $wgOut->addHTML( $openDiv . wfMsgWikiHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</div>' );
784 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
786 if( $this->mPreview
) {
788 $popts = $wgOut->parserOptions();
789 $popts->setEditSection( false );
790 $wgOut->parserOptions( $popts );
791 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
795 Xml
::element( 'textarea', array(
796 'readonly' => 'readonly',
797 'cols' => intval( $wgUser->getOption( 'cols' ) ),
798 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
799 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
800 Xml
::openElement( 'div' ) .
801 Xml
::openElement( 'form', array(
803 'action' => $self->getLocalURL( "action=submit" ) ) ) .
804 Xml
::element( 'input', array(
807 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
808 Xml
::element( 'input', array(
810 'name' => 'timestamp',
811 'value' => $timestamp ) ) .
812 Xml
::element( 'input', array(
814 'name' => 'wpEditToken',
815 'value' => $wgUser->editToken() ) ) .
816 Xml
::element( 'input', array(
819 'value' => wfMsg( 'showpreview' ) ) ) .
820 Xml
::element( 'input', array(
823 'value' => wfMsg( 'showdiff' ) ) ) .
824 Xml
::closeElement( 'form' ) .
825 Xml
::closeElement( 'div' ) );
829 * Build a diff display between this and the previous either deleted
830 * or non-deleted edit.
831 * @param Revision $previousRev
832 * @param Revision $currentRev
833 * @return string HTML
835 function showDiff( $previousRev, $currentRev ) {
838 $diffEngine = new DifferenceEngine();
839 $diffEngine->showDiffStyle();
842 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
843 "<col class='diff-marker' />" .
844 "<col class='diff-content' />" .
845 "<col class='diff-marker' />" .
846 "<col class='diff-content' />" .
848 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
849 $this->diffHeader( $previousRev, 'o' ) .
851 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
852 $this->diffHeader( $currentRev, 'n' ) .
855 $diffEngine->generateDiffBody(
856 $previousRev->getText(), $currentRev->getText() ) .
862 private function diffHeader( $rev, $prefix ) {
863 global $wgUser, $wgLang, $wgLang;
864 $sk = $wgUser->getSkin();
865 $isDeleted = !( $rev->getId() && $rev->getTitle() );
867 /// @fixme $rev->getTitle() is null for deleted revs...?
868 $targetPage = SpecialPage
::getTitleFor( 'Undelete' );
869 $targetQuery = array(
870 'target' => $this->mTargetObj
->getPrefixedUrl(),
871 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
874 /// @fixme getId() may return non-zero for deleted revs...
875 $targetPage = $rev->getTitle();
876 $targetQuery = array( 'oldid' => $rev->getId() );
878 // Add show/hide link if available
879 if( $wgUser->isAllowed( 'deleterevision' ) ) {
880 // If revision was hidden from sysops
881 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
882 $del = ' ' . Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
883 '(' . wfMsgHtml('rev-delundel') . ')' );
884 // Otherwise, show the link...
886 $query = array( 'target' => $this->mTargetObj
->getPrefixedDbkey(),
887 'artimestamp' => $rev->getTimestamp() );
888 $del = ' ' . $sk->revDeleteLink( $query,
889 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
895 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
900 $wgLang->timeanddate( $rev->getTimestamp(), true )
905 ( $isDeleted ?
' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
907 '<div id="mw-diff-'.$prefix.'title2">' .
908 $sk->revUserTools( $rev ) . '<br/>' .
910 '<div id="mw-diff-'.$prefix.'title3">' .
911 $sk->revComment( $rev ) . $del . '<br/>' .
916 * Show a form confirming whether a tokenless user really wants to see a file
918 private function showFileConfirmationForm( $key ) {
919 global $wgOut, $wgUser, $wgLang;
920 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
921 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
922 $this->mTargetObj
->getText(),
923 $wgLang->date( $file->getTimestamp() ),
924 $wgLang->time( $file->getTimestamp() ) );
926 Xml
::openElement( 'form', array(
928 'action' => SpecialPage
::getTitleFor( 'Undelete' )->getLocalUrl(
929 'target=' . urlencode( $this->mTarget
) .
930 '&file=' . urlencode( $key ) .
931 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
934 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
940 * Show a deleted file version requested by the visitor.
942 private function showFile( $key ) {
943 global $wgOut, $wgRequest;
946 # We mustn't allow the output to be Squid cached, otherwise
947 # if an admin previews a deleted image, and it's cached, then
948 # a user without appropriate permissions can toddle off and
949 # nab the image, and Squid will serve it
950 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
951 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
952 $wgRequest->response()->header( 'Pragma: no-cache' );
954 $store = FileStore
::get( 'deleted' );
955 $store->stream( $key );
958 private function showHistory( ) {
959 global $wgLang, $wgUser, $wgOut;
961 $sk = $wgUser->getSkin();
962 if( $this->mAllowed
) {
963 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
965 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
968 $wgOut->addWikiMsg( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() );
970 $archive = new PageArchive( $this->mTargetObj
);
972 $text = $archive->getLastRevisionText();
973 if( is_null( $text ) ) {
974 $wgOut->addWikiMsg( "nohistory" );
978 if ( $this->mAllowed
) {
979 $wgOut->addWikiMsg( "undeletehistory" );
980 $wgOut->addWikiMsg( "undeleterevdel" );
982 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
985 # List all stored revisions
986 $revisions = $archive->listRevisions();
987 $files = $archive->listFiles();
989 $haveRevisions = $revisions && $revisions->numRows() > 0;
990 $haveFiles = $files && $files->numRows() > 0;
992 # Batch existence check on user and talk pages
993 if( $haveRevisions ) {
994 $batch = new LinkBatch();
995 while( $row = $revisions->fetchObject() ) {
996 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
997 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1000 $revisions->seek( 0 );
1003 $batch = new LinkBatch();
1004 while( $row = $files->fetchObject() ) {
1005 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1006 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1012 if ( $this->mAllowed
) {
1013 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1014 $action = $titleObj->getLocalURL( "action=submit" );
1015 # Start the form here
1016 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1017 $wgOut->addHTML( $top );
1020 # Show relevant lines from the deletion log:
1021 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1022 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1023 # Show relevant lines from the suppression log:
1024 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1025 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1026 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1029 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1030 # Format the user-visible controls (comment field, submission button)
1031 # in a nice little table
1032 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1036 <td class='mw-input'>" .
1037 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1038 'mw-undelete-unsuppress', $this->mUnsuppress
).
1042 $unsuppressBox = "";
1045 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1046 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1049 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1053 <td class='mw-label'>" .
1054 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1056 <td class='mw-input'>" .
1057 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1062 <td class='mw-submit'>" .
1063 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1064 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1065 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1069 Xml
::closeElement( 'table' ) .
1070 Xml
::closeElement( 'fieldset' );
1072 $wgOut->addHTML( $table );
1075 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1077 if( $haveRevisions ) {
1078 # The page's stored (deleted) history:
1079 $wgOut->addHTML("<ul>");
1080 $target = urlencode( $this->mTarget
);
1081 $remaining = $revisions->numRows();
1082 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1084 while( $row = $revisions->fetchObject() ) {
1086 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1089 $wgOut->addHTML("</ul>");
1091 $wgOut->addWikiMsg( "nohistory" );
1095 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1096 $wgOut->addHTML( "<ul>" );
1097 while( $row = $files->fetchObject() ) {
1098 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1101 $wgOut->addHTML( "</ul>" );
1104 if ( $this->mAllowed
) {
1105 # Slip in the hidden controls here
1106 $misc = Xml
::hidden( 'target', $this->mTarget
);
1107 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1108 $misc .= Xml
::closeElement( 'form' );
1109 $wgOut->addHTML( $misc );
1115 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1116 global $wgUser, $wgLang;
1118 $rev = new Revision( array(
1119 'page' => $this->mTargetObj
->getArticleId(),
1120 'comment' => $row->ar_comment
,
1121 'user' => $row->ar_user
,
1122 'user_text' => $row->ar_user_text
,
1123 'timestamp' => $row->ar_timestamp
,
1124 'minor_edit' => $row->ar_minor_edit
,
1125 'deleted' => $row->ar_deleted
,
1126 'len' => $row->ar_len
) );
1129 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1130 if( $this->mAllowed
) {
1131 if( $this->mInvert
){
1132 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1133 $checkBox = Xml
::check( "ts$ts");
1135 $checkBox = Xml
::check( "ts$ts", true );
1138 $checkBox = Xml
::check( "ts$ts" );
1140 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1141 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1143 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1144 $last = wfMsgHtml('diff');
1145 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1146 $last = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml('diff'),
1147 "target=" . $this->mTargetObj
->getPrefixedUrl() . "×tamp=$ts&diff=prev" );
1149 $last = wfMsgHtml('diff');
1153 $pageLink = $wgLang->timeanddate( $ts, true );
1154 $last = wfMsgHtml('diff');
1156 $userLink = $sk->revUserTools( $rev );
1158 if(!is_null($size = $row->ar_len
)) {
1159 $stxt = $sk->formatRevisionSize( $size );
1161 $comment = $sk->revComment( $rev );
1163 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1164 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1165 // If revision was hidden from sysops
1166 $revdlink = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
1167 '('.wfMsgHtml('rev-delundel').')' );
1169 $query = array( 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1170 'artimestamp' => $ts
1172 $revdlink = $sk->revDeleteLink( $query, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1176 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1179 private function formatFileRow( $row, $sk ) {
1180 global $wgUser, $wgLang;
1182 $file = ArchivedFile
::newFromRow( $row );
1184 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1185 if( $this->mAllowed
&& $row->fa_storage_key
) {
1186 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1187 $key = urlencode( $row->fa_storage_key
);
1188 $target = urlencode( $this->mTarget
);
1189 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
1190 $pageLink = $this->getFileLink( $file, $titleObj, $ts, $key, $sk );
1193 $pageLink = $wgLang->timeanddate( $ts, true );
1195 $userLink = $this->getFileUser( $file, $sk );
1197 wfMsg( 'widthheight',
1198 $wgLang->formatNum( $row->fa_width
),
1199 $wgLang->formatNum( $row->fa_height
) ) .
1201 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1203 $data = htmlspecialchars( $data );
1204 $comment = $this->getFileComment( $file, $sk );
1206 if( $wgUser->isAllowed( 'deleterevision' ) ) {
1207 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1208 // If revision was hidden from sysops
1209 $revdlink = Xml
::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.wfMsgHtml('rev-delundel').')' );
1211 $query = array( 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1212 'fileid' => $row->fa_id
1214 $revdlink = $sk->revDeleteLink( $query, $file->isDeleted( File
::DELETED_RESTRICTED
) );
1217 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1221 * Fetch revision text link if it's available to all users
1224 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1227 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1228 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1230 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1231 "target=".$this->mTargetObj
->getPrefixedUrl()."×tamp=$ts" );
1232 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1233 $link = '<span class="history-deleted">' . $link . '</span>';
1239 * Fetch image view link if it's available to all users
1242 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1243 global $wgLang, $wgUser;
1245 if( !$file->userCan(File
::DELETED_FILE
) ) {
1246 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1248 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
1249 "target=".$this->mTargetObj
->getPrefixedUrl().
1251 "&token=" . urlencode( $wgUser->editToken( $key ) ) );
1252 if( $file->isDeleted(File
::DELETED_FILE
) )
1253 $link = '<span class="history-deleted">' . $link . '</span>';
1259 * Fetch file's user id if it's available to this user
1262 function getFileUser( $file, $sk ) {
1263 if( !$file->userCan(File
::DELETED_USER
) ) {
1264 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1266 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1267 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1268 if( $file->isDeleted(File
::DELETED_USER
) )
1269 $link = '<span class="history-deleted">' . $link . '</span>';
1275 * Fetch file upload comment if it's available to this user
1278 function getFileComment( $file, $sk ) {
1279 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1280 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1282 $link = $sk->commentBlock( $file->getRawDescription() );
1283 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1284 $link = '<span class="history-deleted">' . $link . '</span>';
1289 function undelete() {
1290 global $wgOut, $wgUser;
1291 if ( wfReadOnly() ) {
1292 $wgOut->readOnlyPage();
1295 if( !is_null( $this->mTargetObj
) ) {
1296 $archive = new PageArchive( $this->mTargetObj
);
1297 $ok = $archive->undelete(
1298 $this->mTargetTimestamp
,
1300 $this->mFileVersions
,
1301 $this->mUnsuppress
);
1303 if( is_array($ok) ) {
1304 if ( $ok[1] ) // Undeleted file count
1305 wfRunHooks( 'FileUndeleteComplete', array(
1306 $this->mTargetObj
, $this->mFileVersions
,
1307 $wgUser, $this->mComment
) );
1309 $skin = $wgUser->getSkin();
1310 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
1311 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1313 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1314 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1317 // Show file deletion warnings and errors
1318 $status = $archive->getFileStatus();
1319 if( $status && !$status->isGood() ) {
1320 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1323 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );