3 * Implements Special:Undelete
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Used to show archived pages and eventually restore them.
27 * @ingroup SpecialPage
37 function __construct( $title ) {
38 if( is_null( $title ) ) {
39 throw new MWException( __METHOD__
. ' given a null title.' );
41 $this->title
= $title;
45 * List all deleted pages recorded in the archive table. Returns result
46 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
49 * @return ResultWrapper
51 public static function listAllPages() {
52 $dbr = wfGetDB( DB_SLAVE
);
53 return self
::listPages( $dbr, '' );
57 * List deleted pages recorded in the archive table matching the
59 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
61 * @param $prefix String: title prefix
62 * @return ResultWrapper
64 public static function listPagesByPrefix( $prefix ) {
65 $dbr = wfGetDB( DB_SLAVE
);
67 $title = Title
::newFromText( $prefix );
69 $ns = $title->getNamespace();
70 $prefix = $title->getDBkey();
72 // Prolly won't work too good
73 // @todo handle bare namespace names cleanly?
77 'ar_namespace' => $ns,
78 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
80 return self
::listPages( $dbr, $conds );
84 * @param $dbr DatabaseBase
86 * @return bool|ResultWrapper
88 protected static function listPages( $dbr, $condition ) {
89 return $dbr->resultObject(
100 'GROUP BY' => 'ar_namespace,ar_title',
101 'ORDER BY' => 'ar_namespace,ar_title',
109 * List the revisions of the given page. Returns result wrapper with
110 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
112 * @return ResultWrapper
114 function listRevisions() {
115 $dbr = wfGetDB( DB_SLAVE
);
116 $res = $dbr->select( 'archive',
118 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
119 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id'
121 array( 'ar_namespace' => $this->title
->getNamespace(),
122 'ar_title' => $this->title
->getDBkey() ),
123 'PageArchive::listRevisions',
124 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
125 $ret = $dbr->resultObject( $res );
130 * List the deleted file revisions for this page, if it's a file page.
131 * Returns a result wrapper with various filearchive fields, or null
132 * if not a file page.
134 * @return ResultWrapper
135 * @todo Does this belong in Image for fuller encapsulation?
137 function listFiles() {
138 if( $this->title
->getNamespace() == NS_FILE
) {
139 $dbr = wfGetDB( DB_SLAVE
);
140 $res = $dbr->select( 'filearchive',
160 array( 'fa_name' => $this->title
->getDBkey() ),
162 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
163 $ret = $dbr->resultObject( $res );
170 * Return a Revision object containing data for the deleted revision.
171 * Note that the result *may* or *may not* have a null page ID.
173 * @param $timestamp String
176 function getRevision( $timestamp ) {
177 $dbr = wfGetDB( DB_SLAVE
);
178 $row = $dbr->selectRow( 'archive',
191 array( 'ar_namespace' => $this->title
->getNamespace(),
192 'ar_title' => $this->title
->getDBkey(),
193 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
196 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
203 * Return the most-previous revision, either live or deleted, against
204 * the deleted revision given by timestamp.
206 * May produce unexpected results in case of history merges or other
207 * unusual time issues.
209 * @param $timestamp String
210 * @return Revision or null
212 function getPreviousRevision( $timestamp ) {
213 $dbr = wfGetDB( DB_SLAVE
);
215 // Check the previous deleted revision...
216 $row = $dbr->selectRow( 'archive',
218 array( 'ar_namespace' => $this->title
->getNamespace(),
219 'ar_title' => $this->title
->getDBkey(),
221 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
224 'ORDER BY' => 'ar_timestamp DESC',
226 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
228 $row = $dbr->selectRow( array( 'page', 'revision' ),
229 array( 'rev_id', 'rev_timestamp' ),
231 'page_namespace' => $this->title
->getNamespace(),
232 'page_title' => $this->title
->getDBkey(),
233 'page_id = rev_page',
235 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
238 'ORDER BY' => 'rev_timestamp DESC',
240 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
241 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
243 if( $prevLive && $prevLive > $prevDeleted ) {
244 // Most prior revision was live
245 return Revision
::newFromId( $prevLiveId );
246 } elseif( $prevDeleted ) {
247 // Most prior revision was deleted
248 return $this->getRevision( $prevDeleted );
250 // No prior revision on this page.
256 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
258 * @param $row Object: database row
261 function getTextFromRow( $row ) {
262 if( is_null( $row->ar_text_id
) ) {
263 // An old row from MediaWiki 1.4 or previous.
264 // Text is embedded in this row in classic compression format.
265 return Revision
::getRevisionText( $row, 'ar_' );
267 // New-style: keyed to the text storage backend.
268 $dbr = wfGetDB( DB_SLAVE
);
269 $text = $dbr->selectRow( 'text',
270 array( 'old_text', 'old_flags' ),
271 array( 'old_id' => $row->ar_text_id
),
273 return Revision
::getRevisionText( $text );
278 * Fetch (and decompress if necessary) the stored text of the most
279 * recently edited deleted revision of the page.
281 * If there are no archived revisions for the page, returns NULL.
285 function getLastRevisionText() {
286 $dbr = wfGetDB( DB_SLAVE
);
287 $row = $dbr->selectRow( 'archive',
288 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
289 array( 'ar_namespace' => $this->title
->getNamespace(),
290 'ar_title' => $this->title
->getDBkey() ),
292 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
294 return $this->getTextFromRow( $row );
301 * Quick check if any archived revisions are present for the page.
305 function isDeleted() {
306 $dbr = wfGetDB( DB_SLAVE
);
307 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
308 array( 'ar_namespace' => $this->title
->getNamespace(),
309 'ar_title' => $this->title
->getDBkey() ) );
314 * Restore the given (or all) text and file revisions for the page.
315 * Once restored, the items will be removed from the archive tables.
316 * The deletion log will be updated with an undeletion notice.
318 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
319 * @param $comment String
320 * @param $fileVersions Array
321 * @param $unsuppress Boolean
323 * @return array(number of file revisions restored, number of image revisions restored, log message)
324 * on success, false on failure
326 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
327 // If both the set of text revisions and file revisions are empty,
328 // restore everything. Otherwise, just restore the requested items.
329 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
331 $restoreText = $restoreAll ||
!empty( $timestamps );
332 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
334 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
335 $img = wfLocalFile( $this->title
);
336 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
337 if ( !$this->fileStatus
->isOk() ) {
340 $filesRestored = $this->fileStatus
->successCount
;
346 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
347 if( $textRestored === false ) { // It must be one of UNDELETE_*
356 $log = new LogPage( 'delete' );
358 if( $textRestored && $filesRestored ) {
359 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
360 $wgContLang->formatNum( $textRestored ),
361 $wgContLang->formatNum( $filesRestored ) );
362 } elseif( $textRestored ) {
363 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
364 $wgContLang->formatNum( $textRestored ) );
365 } elseif( $filesRestored ) {
366 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
367 $wgContLang->formatNum( $filesRestored ) );
369 wfDebug( "Undelete: nothing undeleted...\n" );
373 if( trim( $comment ) != '' ) {
374 $reason .= wfMsgForContent( 'colon-separator' ) . $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 $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
387 * @param $comment String
388 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
390 * @return Mixed: number of revisions restored or false on failure
392 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
393 if ( wfReadOnly() ) {
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'; // lock page
403 $page = $dbw->selectRow( 'page',
404 array( 'page_id', 'page_latest' ),
405 array( 'page_namespace' => $this->title
->getNamespace(),
406 'page_title' => $this->title
->getDBkey() ),
412 # Page already exists. Import the history, and if necessary
413 # we'll update the latest revision field in the record.
415 $pageId = $page->page_id
;
416 $previousRevId = $page->page_latest
;
417 # Get the time span of this page
418 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
419 array( 'rev_id' => $previousRevId ),
421 if( $previousTimestamp === false ) {
422 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
426 # Have to create a new article...
429 $previousTimestamp = 0;
433 $oldones = '1 = 1'; # All revisions...
435 $oldts = implode( ',',
436 array_map( array( &$dbw, 'addQuotes' ),
437 array_map( array( &$dbw, 'timestamp' ),
440 $oldones = "ar_timestamp IN ( {$oldts} )";
444 * Select each archived revision...
446 $result = $dbw->select( 'archive',
461 'ar_namespace' => $this->title
->getNamespace(),
462 'ar_title' => $this->title
->getDBkey(),
465 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
467 $ret = $dbw->resultObject( $result );
468 $rev_count = $dbw->numRows( $result );
470 wfDebug( __METHOD__
. ": no revisions to restore\n" );
474 $ret->seek( $rev_count - 1 ); // move to last
475 $row = $ret->fetchObject(); // get newest archived rev
476 $ret->seek( 0 ); // move back
479 // Check the state of the newest to-be version...
480 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
481 return false; // we can't leave the current revision like this!
483 // Safe to insert now...
484 $newid = $article->insertOn( $dbw );
487 // Check if a deleted revision will become the current revision...
488 if( $row->ar_timestamp
> $previousTimestamp ) {
489 // Check the state of the newest to-be version...
490 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
491 return false; // we can't leave the current revision like this!
499 foreach ( $ret as $row ) {
500 // Check for key dupes due to shitty archive integrity.
501 if( $row->ar_rev_id
) {
502 $exists = $dbw->selectField( 'revision', '1',
503 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
505 continue; // don't throw DB errors
508 // Insert one revision at a time...maintaining deletion status
509 // unless we are specifically removing all restrictions...
510 $revision = Revision
::newFromArchiveRow( $row,
513 'deleted' => $unsuppress ?
0 : $row->ar_deleted
516 $revision->insertOn( $dbw );
519 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
521 # Now that it's safely stored, take it out of the archive
522 $dbw->delete( 'archive',
524 'ar_namespace' => $this->title
->getNamespace(),
525 'ar_title' => $this->title
->getDBkey(),
529 // Was anything restored at all?
530 if ( $restored == 0 ) {
534 $created = (bool)$newid;
536 // Attach the latest revision to the page...
537 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
538 if ( $created ||
$wasnew ) {
539 // Update site stats, link tables, etc
540 $article->editUpdates( $revision->getText(), $revision->getComment(),
541 $revision->isMinor(), wfTimestamp(), $revision->getId(), true, null, $created );
544 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
547 Article
::onArticleCreate( $this->title
);
549 Article
::onArticleEdit( $this->title
);
552 if( $this->title
->getNamespace() == NS_FILE
) {
553 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
563 function getFileStatus() { return $this->fileStatus
; }
567 * Special page allowing users with the appropriate permissions to view
568 * and restore deleted content.
570 * @ingroup SpecialPage
572 class SpecialUndelete
extends SpecialPage
{
573 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
574 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
581 function __construct( $request = null ) {
582 parent
::__construct( 'Undelete', 'deletedhistory' );
584 if ( $request === null ) {
586 $this->mRequest
= $wgRequest;
588 $this->mRequest
= $request;
592 function loadRequest() {
594 $this->mAction
= $this->mRequest
->getVal( 'action' );
595 $this->mTarget
= $this->mRequest
->getVal( 'target' );
596 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
597 $time = $this->mRequest
->getVal( 'timestamp' );
598 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
599 $this->mFilename
= $this->mRequest
->getVal( 'file' );
601 $posted = $this->mRequest
->wasPosted() &&
602 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
603 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
604 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
605 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
606 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
607 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
608 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
609 $this->mToken
= $this->mRequest
->getVal( 'token' );
611 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
612 $this->mAllowed
= true; // user can restore
613 $this->mCanView
= true; // user can view content
614 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
615 $this->mAllowed
= false; // user cannot restore
616 $this->mCanView
= true; // user can view content
617 } else { // user can only view the list of revisions
618 $this->mAllowed
= false;
619 $this->mCanView
= false;
620 $this->mTimestamp
= '';
621 $this->mRestore
= false;
624 if( $this->mRestore ||
$this->mInvert
) {
625 $timestamps = array();
626 $this->mFileVersions
= array();
627 foreach( $_REQUEST as $key => $val ) {
629 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
630 array_push( $timestamps, $matches[1] );
633 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
634 $this->mFileVersions
[] = intval( $matches[1] );
637 rsort( $timestamps );
638 $this->mTargetTimestamp
= $timestamps;
642 function execute( $par ) {
643 global $wgOut, $wgUser;
646 if ( !$this->userCanExecute( $wgUser ) ) {
647 $this->displayRestrictionError();
650 $this->outputHeader();
652 $this->loadRequest();
654 if ( $this->mAllowed
) {
655 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
657 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
661 $this->mTarget
= $par;
663 if ( $this->mTarget
!== '' ) {
664 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
665 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj
);
667 $this->mTargetObj
= null;
670 if( is_null( $this->mTargetObj
) ) {
671 # Not all users can just browse every deleted page from the list
672 if( $wgUser->isAllowed( 'browsearchive' ) ) {
673 $this->showSearchForm();
675 # List undeletable articles
676 if( $this->mSearchPrefix
) {
677 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
678 $this->showList( $result );
681 $wgOut->addWikiMsg( 'undelete-header' );
685 if( $this->mTimestamp
!== '' ) {
686 return $this->showRevision( $this->mTimestamp
);
688 if( $this->mFilename
!== null ) {
689 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
690 // Check if user is allowed to see this file
691 if ( !$file->exists() ) {
692 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
694 } elseif( !$file->userCan( File
::DELETED_FILE
) ) {
695 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
696 $wgOut->permissionRequired( 'suppressrevision' );
698 $wgOut->permissionRequired( 'deletedtext' );
701 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFilename
) ) {
702 $this->showFileConfirmationForm( $this->mFilename
);
705 return $this->showFile( $this->mFilename
);
708 if( $this->mRestore
&& $this->mAction
== 'submit' ) {
709 global $wgUploadMaintenance;
710 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
711 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
714 return $this->undelete();
716 if( $this->mInvert
&& $this->mAction
== 'submit' ) {
717 return $this->showHistory();
719 return $this->showHistory();
722 function showSearchForm() {
723 global $wgOut, $wgScript;
724 $wgOut->addWikiMsg( 'undelete-header' );
727 Xml
::openElement( 'form', array(
729 'action' => $wgScript ) ) .
730 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
731 Html
::hidden( 'title',
732 $this->getTitle()->getPrefixedDbKey() ) .
733 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
734 'prefix', 'prefix', 20,
735 $this->mSearchPrefix
) . ' ' .
736 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
737 Xml
::closeElement( 'fieldset' ) .
738 Xml
::closeElement( 'form' )
743 * Generic list of deleted pages
745 * @param $result ResultWrapper
748 private function showList( $result ) {
749 global $wgLang, $wgUser, $wgOut;
751 if( $result->numRows() == 0 ) {
752 $wgOut->addWikiMsg( 'undelete-no-results' );
756 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
758 $sk = $wgUser->getSkin();
759 $undelete = $this->getTitle();
760 $wgOut->addHTML( "<ul>\n" );
761 foreach ( $result as $row ) {
762 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
763 $link = $sk->linkKnown(
765 htmlspecialchars( $title->getPrefixedText() ),
767 array( 'target' => $title->getPrefixedText() )
769 $revs = wfMsgExt( 'undeleterevisions',
770 array( 'parseinline' ),
771 $wgLang->formatNum( $row->count
) );
772 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
775 $wgOut->addHTML( "</ul>\n" );
780 private function showRevision( $timestamp ) {
781 global $wgLang, $wgUser, $wgOut;
783 $skin = $wgUser->getSkin();
785 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
789 $archive = new PageArchive( $this->mTargetObj
);
790 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
791 $rev = $archive->getRevision( $timestamp );
794 $wgOut->addWikiMsg( 'undeleterevision-missing' );
798 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
799 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
800 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
803 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
804 $wgOut->addHTML( '<br />' );
805 // and we are allowed to see...
809 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
811 $link = $skin->linkKnown(
812 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
813 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
817 $previousRev = $archive->getPreviousRevision( $timestamp );
819 $this->showDiff( $previousRev, $rev );
820 if( $wgUser->getOption( 'diffonly' ) ) {
823 $wgOut->addHTML( '<hr />' );
826 $wgOut->addWikiMsg( 'undelete-nodiff' );
830 // date and time are separate parameters to facilitate localisation.
831 // $time is kept for backward compat reasons.
832 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
833 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
834 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
835 $user = $skin->revUserTools( $rev );
837 if( $this->mPreview
) {
838 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
840 $openDiv = '<div id="mw-undelete-revision">';
842 $wgOut->addHTML( $openDiv );
844 // Revision delete links
845 if ( !$this->mDiff
) {
846 $revdel = $this->revDeleteLink( $rev );
848 $wgOut->addHTML( $revdel );
852 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
853 $wgOut->addHTML( '</div>' );
854 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
856 if( $this->mPreview
) {
858 $popts = $wgOut->parserOptions();
859 $popts->setEditSection( false );
860 $wgOut->parserOptions( $popts );
861 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
865 Xml
::element( 'textarea', array(
866 'readonly' => 'readonly',
867 'cols' => intval( $wgUser->getOption( 'cols' ) ),
868 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
869 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
870 Xml
::openElement( 'div' ) .
871 Xml
::openElement( 'form', array(
873 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
874 Xml
::element( 'input', array(
877 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
878 Xml
::element( 'input', array(
880 'name' => 'timestamp',
881 'value' => $timestamp ) ) .
882 Xml
::element( 'input', array(
884 'name' => 'wpEditToken',
885 'value' => $wgUser->editToken() ) ) .
886 Xml
::element( 'input', array(
889 'value' => wfMsg( 'showpreview' ) ) ) .
890 Xml
::element( 'input', array(
893 'value' => wfMsg( 'showdiff' ) ) ) .
894 Xml
::closeElement( 'form' ) .
895 Xml
::closeElement( 'div' ) );
899 * Get a revision-deletion link, or disabled link, or nothing, depending
900 * on user permissions & the settings on the revision.
902 * Will use forward-compatible revision ID in the Special:RevDelete link
903 * if possible, otherwise the timestamp-based ID which may break after
906 * @param Revision $rev
907 * @return string HTML fragment
909 function revDeleteLink( $rev ) {
911 $canHide = $wgUser->isAllowed( 'deleterevision' );
912 if( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
913 $skin = $wgUser->getSkin();
914 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
915 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
917 if ( $rev->getId() ) {
918 // RevDelete links using revision ID are stable across
919 // page deletion and undeletion; use when possible.
921 'type' => 'revision',
922 'target' => $this->mTargetObj
->getPrefixedDBkey(),
923 'ids' => $rev->getId()
926 // Older deleted entries didn't save a revision ID.
927 // We have to refer to these by timestamp, ick!
930 'target' => $this->mTargetObj
->getPrefixedDBkey(),
931 'ids' => $rev->getTimestamp()
934 return $skin->revDeleteLink( $query,
935 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
943 * Build a diff display between this and the previous either deleted
944 * or non-deleted edit.
946 * @param $previousRev Revision
947 * @param $currentRev Revision
948 * @return String: HTML
950 function showDiff( $previousRev, $currentRev ) {
953 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
954 $diffEngine->showDiffStyle();
957 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
958 "<col class='diff-marker' />" .
959 "<col class='diff-content' />" .
960 "<col class='diff-marker' />" .
961 "<col class='diff-content' />" .
963 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
964 $this->diffHeader( $previousRev, 'o' ) .
966 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
967 $this->diffHeader( $currentRev, 'n' ) .
970 $diffEngine->generateDiffBody(
971 $previousRev->getText(), $currentRev->getText() ) .
978 * @param $rev Revision
982 private function diffHeader( $rev, $prefix ) {
983 global $wgUser, $wgLang;
984 $sk = $wgUser->getSkin();
985 $isDeleted = !( $rev->getId() && $rev->getTitle() );
987 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
988 $targetPage = $this->getTitle();
989 $targetQuery = array(
990 'target' => $this->mTargetObj
->getPrefixedText(),
991 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
994 /// @todo FIXME: getId() may return non-zero for deleted revs...
995 $targetPage = $rev->getTitle();
996 $targetQuery = array( 'oldid' => $rev->getId() );
998 // Add show/hide deletion links if available
999 $del = $this->revDeleteLink( $rev );
1001 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1006 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
1007 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
1008 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
1014 '<div id="mw-diff-'.$prefix.'title2">' .
1015 $sk->revUserTools( $rev ) . '<br />' .
1017 '<div id="mw-diff-'.$prefix.'title3">' .
1018 $sk->revComment( $rev ) . $del . '<br />' .
1023 * Show a form confirming whether a tokenless user really wants to see a file
1025 private function showFileConfirmationForm( $key ) {
1026 global $wgOut, $wgUser, $wgLang;
1027 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1028 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
1029 $this->mTargetObj
->getText(),
1030 $wgLang->date( $file->getTimestamp() ),
1031 $wgLang->time( $file->getTimestamp() ) );
1033 Xml
::openElement( 'form', array(
1035 'action' => $this->getTitle()->getLocalURL(
1036 'target=' . urlencode( $this->mTarget
) .
1037 '&file=' . urlencode( $key ) .
1038 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
1041 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1047 * Show a deleted file version requested by the visitor.
1049 private function showFile( $key ) {
1050 global $wgOut, $wgRequest;
1053 # We mustn't allow the output to be Squid cached, otherwise
1054 # if an admin previews a deleted image, and it's cached, then
1055 # a user without appropriate permissions can toddle off and
1056 # nab the image, and Squid will serve it
1057 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1058 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1059 $wgRequest->response()->header( 'Pragma: no-cache' );
1062 require_once( "$IP/includes/StreamFile.php" );
1063 $repo = RepoGroup
::singleton()->getLocalRepo();
1064 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1065 wfStreamFile( $path );
1068 private function showHistory() {
1069 global $wgUser, $wgOut;
1071 $sk = $wgUser->getSkin();
1072 if( $this->mAllowed
) {
1073 $wgOut->addModules( 'mediawiki.special.undelete' );
1074 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
1076 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
1078 $wgOut->wrapWikiMsg(
1079 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1080 array( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() )
1083 $archive = new PageArchive( $this->mTargetObj
);
1084 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1086 $text = $archive->getLastRevisionText();
1087 if( is_null( $text ) ) {
1088 $wgOut->addWikiMsg( 'nohistory' );
1092 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1093 if ( $this->mAllowed
) {
1094 $wgOut->addWikiMsg( 'undeletehistory' );
1095 $wgOut->addWikiMsg( 'undeleterevdel' );
1097 $wgOut->addWikiMsg( 'undeletehistorynoadmin' );
1099 $wgOut->addHTML( '</div>' );
1101 # List all stored revisions
1102 $revisions = $archive->listRevisions();
1103 $files = $archive->listFiles();
1105 $haveRevisions = $revisions && $revisions->numRows() > 0;
1106 $haveFiles = $files && $files->numRows() > 0;
1108 # Batch existence check on user and talk pages
1109 if( $haveRevisions ) {
1110 $batch = new LinkBatch();
1111 foreach ( $revisions as $row ) {
1112 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1113 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1116 $revisions->seek( 0 );
1119 $batch = new LinkBatch();
1120 foreach ( $files as $row ) {
1121 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1122 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1128 if ( $this->mAllowed
) {
1129 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1130 # Start the form here
1131 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1132 $wgOut->addHTML( $top );
1135 # Show relevant lines from the deletion log:
1136 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1137 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1138 # Show relevant lines from the suppression log:
1139 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1140 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1141 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1144 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1145 # Format the user-visible controls (comment field, submission button)
1146 # in a nice little table
1147 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1151 <td class='mw-input'>" .
1152 Xml
::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1153 'mw-undelete-unsuppress', $this->mUnsuppress
).
1157 $unsuppressBox = '';
1160 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1161 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1163 <td colspan='2' class='mw-undelete-extrahelp'>" .
1164 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1168 <td class='mw-label'>" .
1169 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1171 <td class='mw-input'>" .
1172 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1177 <td class='mw-submit'>" .
1178 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1179 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1183 Xml
::closeElement( 'table' ) .
1184 Xml
::closeElement( 'fieldset' );
1186 $wgOut->addHTML( $table );
1189 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1191 if( $haveRevisions ) {
1192 # The page's stored (deleted) history:
1193 $wgOut->addHTML( '<ul>' );
1194 $remaining = $revisions->numRows();
1195 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1197 foreach ( $revisions as $row ) {
1199 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1202 $wgOut->addHTML( '</ul>' );
1204 $wgOut->addWikiMsg( 'nohistory' );
1208 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1209 $wgOut->addHTML( '<ul>' );
1210 foreach ( $files as $row ) {
1211 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1214 $wgOut->addHTML( '</ul>' );
1217 if ( $this->mAllowed
) {
1218 # Slip in the hidden controls here
1219 $misc = Html
::hidden( 'target', $this->mTarget
);
1220 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
1221 $misc .= Xml
::closeElement( 'form' );
1222 $wgOut->addHTML( $misc );
1228 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1231 $rev = Revision
::newFromArchiveRow( $row,
1232 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1234 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1235 // Build checkboxen...
1236 if( $this->mAllowed
) {
1237 if( $this->mInvert
) {
1238 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1239 $checkBox = Xml
::check( "ts$ts" );
1241 $checkBox = Xml
::check( "ts$ts", true );
1244 $checkBox = Xml
::check( "ts$ts" );
1249 // Build page & diff links...
1250 if( $this->mCanView
) {
1251 $titleObj = $this->getTitle();
1253 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1254 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1255 $last = wfMsgHtml( 'diff' );
1256 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1257 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1258 $last = $sk->linkKnown(
1260 wfMsgHtml( 'diff' ),
1263 'target' => $this->mTargetObj
->getPrefixedText(),
1269 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1270 $last = wfMsgHtml( 'diff' );
1273 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1274 $last = wfMsgHtml( 'diff' );
1277 $userLink = $sk->revUserTools( $rev );
1278 // Revision text size
1279 $size = $row->ar_len
;
1280 if( !is_null( $size ) ) {
1281 $stxt = $sk->formatRevisionSize( $size );
1284 $comment = $sk->revComment( $rev );
1285 // Revision delete links
1286 $revdlink = $this->revDeleteLink( $rev );
1287 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1290 private function formatFileRow( $row, $sk ) {
1291 global $wgUser, $wgLang;
1293 $file = ArchivedFile
::newFromRow( $row );
1295 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1296 if( $this->mAllowed
&& $row->fa_storage_key
) {
1297 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1298 $key = urlencode( $row->fa_storage_key
);
1299 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1302 $pageLink = $wgLang->timeanddate( $ts, true );
1304 $userLink = $this->getFileUser( $file, $sk );
1306 wfMsg( 'widthheight',
1307 $wgLang->formatNum( $row->fa_width
),
1308 $wgLang->formatNum( $row->fa_height
) ) .
1310 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1312 $data = htmlspecialchars( $data );
1313 $comment = $this->getFileComment( $file, $sk );
1314 // Add show/hide deletion links if available
1315 $canHide = $wgUser->isAllowed( 'deleterevision' );
1316 if( $canHide ||
( $file->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
1317 if( !$file->userCan( File
::DELETED_RESTRICTED
) ) {
1318 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1321 'type' => 'filearchive',
1322 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1323 'ids' => $row->fa_id
1325 $revdlink = $sk->revDeleteLink( $query,
1326 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1331 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1335 * Fetch revision text link if it's available to all users
1337 * @param $rev Revision
1341 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1344 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1346 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1347 return '<span class="history-deleted">' . $time . '</span>';
1349 $link = $sk->linkKnown(
1354 'target' => $this->mTargetObj
->getPrefixedText(),
1358 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1359 $link = '<span class="history-deleted">' . $link . '</span>';
1366 * Fetch image view link if it's available to all users
1370 * @return String: HTML fragment
1372 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1373 global $wgLang, $wgUser;
1375 if( !$file->userCan( File
::DELETED_FILE
) ) {
1376 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1378 $link = $sk->linkKnown(
1380 $wgLang->timeanddate( $ts, true ),
1383 'target' => $this->mTargetObj
->getPrefixedText(),
1385 'token' => $wgUser->editToken( $key )
1388 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1389 $link = '<span class="history-deleted">' . $link . '</span>';
1396 * Fetch file's user id if it's available to this user
1400 * @return String: HTML fragment
1402 function getFileUser( $file, $sk ) {
1403 if( !$file->userCan( File
::DELETED_USER
) ) {
1404 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1406 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1407 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1408 if( $file->isDeleted( File
::DELETED_USER
) ) {
1409 $link = '<span class="history-deleted">' . $link . '</span>';
1416 * Fetch file upload comment if it's available to this user
1420 * @return String: HTML fragment
1422 function getFileComment( $file, $sk ) {
1423 if( !$file->userCan( File
::DELETED_COMMENT
) ) {
1424 return '<span class="history-deleted"><span class="comment">' .
1425 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1427 $link = $sk->commentBlock( $file->getRawDescription() );
1428 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1429 $link = '<span class="history-deleted">' . $link . '</span>';
1435 function undelete() {
1436 global $wgOut, $wgUser;
1437 if ( wfReadOnly() ) {
1438 $wgOut->readOnlyPage();
1441 if( !is_null( $this->mTargetObj
) ) {
1442 $archive = new PageArchive( $this->mTargetObj
);
1443 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1444 $ok = $archive->undelete(
1445 $this->mTargetTimestamp
,
1447 $this->mFileVersions
,
1448 $this->mUnsuppress
);
1450 if( is_array( $ok ) ) {
1451 if ( $ok[1] ) { // Undeleted file count
1452 wfRunHooks( 'FileUndeleteComplete', array(
1453 $this->mTargetObj
, $this->mFileVersions
,
1454 $wgUser, $this->mComment
) );
1457 $skin = $wgUser->getSkin();
1458 $link = $skin->linkKnown( $this->mTargetObj
);
1459 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1461 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );
1462 $wgOut->addWikiMsg( 'undeleterevdel' );
1465 // Show file deletion warnings and errors
1466 $status = $archive->getFileStatus();
1467 if( $status && !$status->isGood() ) {
1468 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1471 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );