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, $mTargetObj, $mFile;
574 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
576 function __construct( $request = null ) {
577 parent
::__construct( 'Undelete', 'deletedhistory' );
579 if ( $request === null ) {
581 $this->mRequest
= $wgRequest;
583 $this->mRequest
= $request;
587 function loadRequest() {
589 $this->mAction
= $this->mRequest
->getVal( 'action' );
590 $this->mTarget
= $this->mRequest
->getVal( 'target' );
591 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
592 $time = $this->mRequest
->getVal( 'timestamp' );
593 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
594 $this->mFile
= $this->mRequest
->getVal( 'file' );
596 $posted = $this->mRequest
->wasPosted() &&
597 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
598 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
599 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
600 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
601 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
602 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
603 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
604 $this->mToken
= $this->mRequest
->getVal( 'token' );
606 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
607 $this->mAllowed
= true; // user can restore
608 $this->mCanView
= true; // user can view content
609 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
610 $this->mAllowed
= false; // user cannot restore
611 $this->mCanView
= true; // user can view content
612 } else { // user can only view the list of revisions
613 $this->mAllowed
= false;
614 $this->mCanView
= false;
615 $this->mTimestamp
= '';
616 $this->mRestore
= false;
619 if( $this->mRestore ||
$this->mInvert
) {
620 $timestamps = array();
621 $this->mFileVersions
= array();
622 foreach( $_REQUEST as $key => $val ) {
624 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
625 array_push( $timestamps, $matches[1] );
628 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
629 $this->mFileVersions
[] = intval( $matches[1] );
632 rsort( $timestamps );
633 $this->mTargetTimestamp
= $timestamps;
637 function execute( $par ) {
638 global $wgOut, $wgUser;
641 if ( !$this->userCanExecute( $wgUser ) ) {
642 $this->displayRestrictionError();
645 $this->outputHeader();
647 $this->loadRequest();
649 if ( $this->mAllowed
) {
650 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
652 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
656 $this->mTarget
= $par;
658 if ( $this->mTarget
!== '' ) {
659 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
660 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj
);
662 $this->mTargetObj
= null;
665 if( is_null( $this->mTargetObj
) ) {
666 # Not all users can just browse every deleted page from the list
667 if( $wgUser->isAllowed( 'browsearchive' ) ) {
668 $this->showSearchForm();
670 # List undeletable articles
671 if( $this->mSearchPrefix
) {
672 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
673 $this->showList( $result );
676 $wgOut->addWikiMsg( 'undelete-header' );
680 if( $this->mTimestamp
!== '' ) {
681 return $this->showRevision( $this->mTimestamp
);
683 if( $this->mFile
!== null ) {
684 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
685 // Check if user is allowed to see this file
686 if ( !$file->exists() ) {
687 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile
);
689 } else if( !$file->userCan( File
::DELETED_FILE
) ) {
690 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
691 $wgOut->permissionRequired( 'suppressrevision' );
693 $wgOut->permissionRequired( 'deletedtext' );
696 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
697 $this->showFileConfirmationForm( $this->mFile
);
700 return $this->showFile( $this->mFile
);
703 if( $this->mRestore
&& $this->mAction
== 'submit' ) {
704 global $wgUploadMaintenance;
705 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
706 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
709 return $this->undelete();
711 if( $this->mInvert
&& $this->mAction
== 'submit' ) {
712 return $this->showHistory();
714 return $this->showHistory();
717 function showSearchForm() {
718 global $wgOut, $wgScript;
719 $wgOut->addWikiMsg( 'undelete-header' );
722 Xml
::openElement( 'form', array(
724 'action' => $wgScript ) ) .
725 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
726 Html
::hidden( 'title',
727 $this->getTitle()->getPrefixedDbKey() ) .
728 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
729 'prefix', 'prefix', 20,
730 $this->mSearchPrefix
) . ' ' .
731 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
732 Xml
::closeElement( 'fieldset' ) .
733 Xml
::closeElement( 'form' )
737 // Generic list of deleted pages
738 private function showList( $result ) {
739 global $wgLang, $wgUser, $wgOut;
741 if( $result->numRows() == 0 ) {
742 $wgOut->addWikiMsg( 'undelete-no-results' );
746 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
748 $sk = $wgUser->getSkin();
749 $undelete = $this->getTitle();
750 $wgOut->addHTML( "<ul>\n" );
751 foreach ( $result as $row ) {
752 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
753 $link = $sk->linkKnown(
755 htmlspecialchars( $title->getPrefixedText() ),
757 array( 'target' => $title->getPrefixedText() )
759 $revs = wfMsgExt( 'undeleterevisions',
760 array( 'parseinline' ),
761 $wgLang->formatNum( $row->count
) );
762 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
765 $wgOut->addHTML( "</ul>\n" );
770 private function showRevision( $timestamp ) {
771 global $wgLang, $wgUser, $wgOut;
773 $skin = $wgUser->getSkin();
775 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
779 $archive = new PageArchive( $this->mTargetObj
);
780 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
781 $rev = $archive->getRevision( $timestamp );
784 $wgOut->addWikiMsg( 'undeleterevision-missing' );
788 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
789 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
790 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
793 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
794 $wgOut->addHTML( '<br />' );
795 // and we are allowed to see...
799 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
801 $link = $skin->linkKnown(
802 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
803 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
807 $previousRev = $archive->getPreviousRevision( $timestamp );
809 $this->showDiff( $previousRev, $rev );
810 if( $wgUser->getOption( 'diffonly' ) ) {
813 $wgOut->addHTML( '<hr />' );
816 $wgOut->addWikiMsg( 'undelete-nodiff' );
820 // date and time are separate parameters to facilitate localisation.
821 // $time is kept for backward compat reasons.
822 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
823 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
824 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
825 $user = $skin->revUserTools( $rev );
827 if( $this->mPreview
) {
828 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
830 $openDiv = '<div id="mw-undelete-revision">';
832 $wgOut->addHTML( $openDiv );
834 // Revision delete links
835 if ( !$this->mDiff
) {
836 $revdel = $this->revDeleteLink( $rev );
838 $wgOut->addHTML( $revdel );
842 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
843 $wgOut->addHTML( '</div>' );
844 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
846 if( $this->mPreview
) {
848 $popts = $wgOut->parserOptions();
849 $popts->setEditSection( false );
850 $wgOut->parserOptions( $popts );
851 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
855 Xml
::element( 'textarea', array(
856 'readonly' => 'readonly',
857 'cols' => intval( $wgUser->getOption( 'cols' ) ),
858 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
859 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
860 Xml
::openElement( 'div' ) .
861 Xml
::openElement( 'form', array(
863 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
864 Xml
::element( 'input', array(
867 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
868 Xml
::element( 'input', array(
870 'name' => 'timestamp',
871 'value' => $timestamp ) ) .
872 Xml
::element( 'input', array(
874 'name' => 'wpEditToken',
875 'value' => $wgUser->editToken() ) ) .
876 Xml
::element( 'input', array(
879 'value' => wfMsg( 'showpreview' ) ) ) .
880 Xml
::element( 'input', array(
883 'value' => wfMsg( 'showdiff' ) ) ) .
884 Xml
::closeElement( 'form' ) .
885 Xml
::closeElement( 'div' ) );
889 * Get a revision-deletion link, or disabled link, or nothing, depending
890 * on user permissions & the settings on the revision.
892 * Will use forward-compatible revision ID in the Special:RevDelete link
893 * if possible, otherwise the timestamp-based ID which may break after
896 * @param Revision $rev
897 * @return string HTML fragment
899 function revDeleteLink( $rev ) {
901 $canHide = $wgUser->isAllowed( 'deleterevision' );
902 if( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
903 $skin = $wgUser->getSkin();
904 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
905 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
907 if ( $rev->getId() ) {
908 // RevDelete links using revision ID are stable across
909 // page deletion and undeletion; use when possible.
911 'type' => 'revision',
912 'target' => $this->mTargetObj
->getPrefixedDBkey(),
913 'ids' => $rev->getId()
916 // Older deleted entries didn't save a revision ID.
917 // We have to refer to these by timestamp, ick!
920 'target' => $this->mTargetObj
->getPrefixedDBkey(),
921 'ids' => $rev->getTimestamp()
924 return $skin->revDeleteLink( $query,
925 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
933 * Build a diff display between this and the previous either deleted
934 * or non-deleted edit.
936 * @param $previousRev Revision
937 * @param $currentRev Revision
938 * @return String: HTML
940 function showDiff( $previousRev, $currentRev ) {
943 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
944 $diffEngine->showDiffStyle();
947 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
948 "<col class='diff-marker' />" .
949 "<col class='diff-content' />" .
950 "<col class='diff-marker' />" .
951 "<col class='diff-content' />" .
953 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
954 $this->diffHeader( $previousRev, 'o' ) .
956 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
957 $this->diffHeader( $currentRev, 'n' ) .
960 $diffEngine->generateDiffBody(
961 $previousRev->getText(), $currentRev->getText() ) .
968 * @param $rev Revision
972 private function diffHeader( $rev, $prefix ) {
973 global $wgUser, $wgLang;
974 $sk = $wgUser->getSkin();
975 $isDeleted = !( $rev->getId() && $rev->getTitle() );
977 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
978 $targetPage = $this->getTitle();
979 $targetQuery = array(
980 'target' => $this->mTargetObj
->getPrefixedText(),
981 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
984 /// @todo FIXME: getId() may return non-zero for deleted revs...
985 $targetPage = $rev->getTitle();
986 $targetQuery = array( 'oldid' => $rev->getId() );
988 // Add show/hide deletion links if available
989 $del = $this->revDeleteLink( $rev );
991 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
996 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
997 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
998 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
1004 '<div id="mw-diff-'.$prefix.'title2">' .
1005 $sk->revUserTools( $rev ) . '<br />' .
1007 '<div id="mw-diff-'.$prefix.'title3">' .
1008 $sk->revComment( $rev ) . $del . '<br />' .
1013 * Show a form confirming whether a tokenless user really wants to see a file
1015 private function showFileConfirmationForm( $key ) {
1016 global $wgOut, $wgUser, $wgLang;
1017 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
1018 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
1019 $this->mTargetObj
->getText(),
1020 $wgLang->date( $file->getTimestamp() ),
1021 $wgLang->time( $file->getTimestamp() ) );
1023 Xml
::openElement( 'form', array(
1025 'action' => $this->getTitle()->getLocalURL(
1026 'target=' . urlencode( $this->mTarget
) .
1027 '&file=' . urlencode( $key ) .
1028 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
1031 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1037 * Show a deleted file version requested by the visitor.
1039 private function showFile( $key ) {
1040 global $wgOut, $wgRequest;
1043 # We mustn't allow the output to be Squid cached, otherwise
1044 # if an admin previews a deleted image, and it's cached, then
1045 # a user without appropriate permissions can toddle off and
1046 # nab the image, and Squid will serve it
1047 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1048 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1049 $wgRequest->response()->header( 'Pragma: no-cache' );
1052 require_once( "$IP/includes/StreamFile.php" );
1053 $repo = RepoGroup
::singleton()->getLocalRepo();
1054 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1055 wfStreamFile( $path );
1058 private function showHistory() {
1059 global $wgUser, $wgOut;
1061 $sk = $wgUser->getSkin();
1062 if( $this->mAllowed
) {
1063 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
1065 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
1068 $wgOut->wrapWikiMsg(
1069 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1070 array( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() )
1073 $archive = new PageArchive( $this->mTargetObj
);
1074 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1076 $text = $archive->getLastRevisionText();
1077 if( is_null( $text ) ) {
1078 $wgOut->addWikiMsg( 'nohistory' );
1082 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1083 if ( $this->mAllowed
) {
1084 $wgOut->addWikiMsg( 'undeletehistory' );
1085 $wgOut->addWikiMsg( 'undeleterevdel' );
1087 $wgOut->addWikiMsg( 'undeletehistorynoadmin' );
1089 $wgOut->addHTML( '</div>' );
1091 # List all stored revisions
1092 $revisions = $archive->listRevisions();
1093 $files = $archive->listFiles();
1095 $haveRevisions = $revisions && $revisions->numRows() > 0;
1096 $haveFiles = $files && $files->numRows() > 0;
1098 # Batch existence check on user and talk pages
1099 if( $haveRevisions ) {
1100 $batch = new LinkBatch();
1101 foreach ( $revisions as $row ) {
1102 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1103 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1106 $revisions->seek( 0 );
1109 $batch = new LinkBatch();
1110 foreach ( $files as $row ) {
1111 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1112 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1118 if ( $this->mAllowed
) {
1119 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1120 # Start the form here
1121 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1122 $wgOut->addHTML( $top );
1125 # Show relevant lines from the deletion log:
1126 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1127 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1128 # Show relevant lines from the suppression log:
1129 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1130 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1131 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1134 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1135 # Format the user-visible controls (comment field, submission button)
1136 # in a nice little table
1137 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1141 <td class='mw-input'>" .
1142 Xml
::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1143 'mw-undelete-unsuppress', $this->mUnsuppress
).
1147 $unsuppressBox = '';
1150 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1151 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1153 <td colspan='2' class='mw-undelete-extrahelp'>" .
1154 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1158 <td class='mw-label'>" .
1159 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1161 <td class='mw-input'>" .
1162 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1167 <td class='mw-submit'>" .
1168 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1169 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1170 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1174 Xml
::closeElement( 'table' ) .
1175 Xml
::closeElement( 'fieldset' );
1177 $wgOut->addHTML( $table );
1180 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1182 if( $haveRevisions ) {
1183 # The page's stored (deleted) history:
1184 $wgOut->addHTML( '<ul>' );
1185 $remaining = $revisions->numRows();
1186 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1188 foreach ( $revisions as $row ) {
1190 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1193 $wgOut->addHTML( '</ul>' );
1195 $wgOut->addWikiMsg( 'nohistory' );
1199 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1200 $wgOut->addHTML( '<ul>' );
1201 foreach ( $files as $row ) {
1202 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1205 $wgOut->addHTML( '</ul>' );
1208 if ( $this->mAllowed
) {
1209 # Slip in the hidden controls here
1210 $misc = Html
::hidden( 'target', $this->mTarget
);
1211 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
1212 $misc .= Xml
::closeElement( 'form' );
1213 $wgOut->addHTML( $misc );
1219 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1222 $rev = Revision
::newFromArchiveRow( $row,
1223 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1225 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1226 // Build checkboxen...
1227 if( $this->mAllowed
) {
1228 if( $this->mInvert
) {
1229 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1230 $checkBox = Xml
::check( "ts$ts" );
1232 $checkBox = Xml
::check( "ts$ts", true );
1235 $checkBox = Xml
::check( "ts$ts" );
1240 // Build page & diff links...
1241 if( $this->mCanView
) {
1242 $titleObj = $this->getTitle();
1244 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1245 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1246 $last = wfMsgHtml( 'diff' );
1247 } else if( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1248 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1249 $last = $sk->linkKnown(
1251 wfMsgHtml( 'diff' ),
1254 'target' => $this->mTargetObj
->getPrefixedText(),
1260 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1261 $last = wfMsgHtml( 'diff' );
1264 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1265 $last = wfMsgHtml( 'diff' );
1268 $userLink = $sk->revUserTools( $rev );
1269 // Revision text size
1270 $size = $row->ar_len
;
1271 if( !is_null( $size ) ) {
1272 $stxt = $sk->formatRevisionSize( $size );
1275 $comment = $sk->revComment( $rev );
1276 // Revision delete links
1277 $revdlink = $this->revDeleteLink( $rev );
1278 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1281 private function formatFileRow( $row, $sk ) {
1282 global $wgUser, $wgLang;
1284 $file = ArchivedFile
::newFromRow( $row );
1286 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1287 if( $this->mAllowed
&& $row->fa_storage_key
) {
1288 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1289 $key = urlencode( $row->fa_storage_key
);
1290 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1293 $pageLink = $wgLang->timeanddate( $ts, true );
1295 $userLink = $this->getFileUser( $file, $sk );
1297 wfMsg( 'widthheight',
1298 $wgLang->formatNum( $row->fa_width
),
1299 $wgLang->formatNum( $row->fa_height
) ) .
1301 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1303 $data = htmlspecialchars( $data );
1304 $comment = $this->getFileComment( $file, $sk );
1305 // Add show/hide deletion links if available
1306 $canHide = $wgUser->isAllowed( 'deleterevision' );
1307 if( $canHide ||
( $file->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
1308 if( !$file->userCan( File
::DELETED_RESTRICTED
) ) {
1309 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1312 'type' => 'filearchive',
1313 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1314 'ids' => $row->fa_id
1316 $revdlink = $sk->revDeleteLink( $query,
1317 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1322 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1326 * Fetch revision text link if it's available to all users
1328 * @param $rev Revision
1332 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1335 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1337 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1338 return '<span class="history-deleted">' . $time . '</span>';
1340 $link = $sk->linkKnown(
1345 'target' => $this->mTargetObj
->getPrefixedText(),
1349 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1350 $link = '<span class="history-deleted">' . $link . '</span>';
1357 * Fetch image view link if it's available to all users
1361 * @return String: HTML fragment
1363 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1364 global $wgLang, $wgUser;
1366 if( !$file->userCan( File
::DELETED_FILE
) ) {
1367 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1369 $link = $sk->linkKnown(
1371 $wgLang->timeanddate( $ts, true ),
1374 'target' => $this->mTargetObj
->getPrefixedText(),
1376 'token' => $wgUser->editToken( $key )
1379 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1380 $link = '<span class="history-deleted">' . $link . '</span>';
1387 * Fetch file's user id if it's available to this user
1391 * @return String: HTML fragment
1393 function getFileUser( $file, $sk ) {
1394 if( !$file->userCan( File
::DELETED_USER
) ) {
1395 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1397 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1398 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1399 if( $file->isDeleted( File
::DELETED_USER
) ) {
1400 $link = '<span class="history-deleted">' . $link . '</span>';
1407 * Fetch file upload comment if it's available to this user
1411 * @return String: HTML fragment
1413 function getFileComment( $file, $sk ) {
1414 if( !$file->userCan( File
::DELETED_COMMENT
) ) {
1415 return '<span class="history-deleted"><span class="comment">' .
1416 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1418 $link = $sk->commentBlock( $file->getRawDescription() );
1419 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1420 $link = '<span class="history-deleted">' . $link . '</span>';
1426 function undelete() {
1427 global $wgOut, $wgUser;
1428 if ( wfReadOnly() ) {
1429 $wgOut->readOnlyPage();
1432 if( !is_null( $this->mTargetObj
) ) {
1433 $archive = new PageArchive( $this->mTargetObj
);
1434 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1435 $ok = $archive->undelete(
1436 $this->mTargetTimestamp
,
1438 $this->mFileVersions
,
1439 $this->mUnsuppress
);
1441 if( is_array( $ok ) ) {
1442 if ( $ok[1] ) { // Undeleted file count
1443 wfRunHooks( 'FileUndeleteComplete', array(
1444 $this->mTargetObj
, $this->mFileVersions
,
1445 $wgUser, $this->mComment
) );
1448 $skin = $wgUser->getSkin();
1449 $link = $skin->linkKnown( $this->mTargetObj
);
1450 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1452 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );
1453 $wgOut->addWikiMsg( 'undeleterevdel' );
1456 // Show file deletion warnings and errors
1457 $status = $archive->getFileStatus();
1458 if( $status && !$status->isGood() ) {
1459 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1462 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );