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' => array( 'ar_namespace', 'ar_title' ),
101 'ORDER BY' => array( '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', 'ar_sha1'
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',
193 array( 'ar_namespace' => $this->title
->getNamespace(),
194 'ar_title' => $this->title
->getDBkey(),
195 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
198 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleID() ) );
205 * Return the most-previous revision, either live or deleted, against
206 * the deleted revision given by timestamp.
208 * May produce unexpected results in case of history merges or other
209 * unusual time issues.
211 * @param $timestamp String
212 * @return Revision or null
214 function getPreviousRevision( $timestamp ) {
215 $dbr = wfGetDB( DB_SLAVE
);
217 // Check the previous deleted revision...
218 $row = $dbr->selectRow( 'archive',
220 array( 'ar_namespace' => $this->title
->getNamespace(),
221 'ar_title' => $this->title
->getDBkey(),
223 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
226 'ORDER BY' => 'ar_timestamp DESC',
228 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
230 $row = $dbr->selectRow( array( 'page', 'revision' ),
231 array( 'rev_id', 'rev_timestamp' ),
233 'page_namespace' => $this->title
->getNamespace(),
234 'page_title' => $this->title
->getDBkey(),
235 'page_id = rev_page',
237 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
240 'ORDER BY' => 'rev_timestamp DESC',
242 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
243 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
245 if( $prevLive && $prevLive > $prevDeleted ) {
246 // Most prior revision was live
247 return Revision
::newFromId( $prevLiveId );
248 } elseif( $prevDeleted ) {
249 // Most prior revision was deleted
250 return $this->getRevision( $prevDeleted );
252 // No prior revision on this page.
258 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
260 * @param $row Object: database row
263 function getTextFromRow( $row ) {
264 if( is_null( $row->ar_text_id
) ) {
265 // An old row from MediaWiki 1.4 or previous.
266 // Text is embedded in this row in classic compression format.
267 return Revision
::getRevisionText( $row, 'ar_' );
269 // New-style: keyed to the text storage backend.
270 $dbr = wfGetDB( DB_SLAVE
);
271 $text = $dbr->selectRow( 'text',
272 array( 'old_text', 'old_flags' ),
273 array( 'old_id' => $row->ar_text_id
),
275 return Revision
::getRevisionText( $text );
280 * Fetch (and decompress if necessary) the stored text of the most
281 * recently edited deleted revision of the page.
283 * If there are no archived revisions for the page, returns NULL.
287 function getLastRevisionText() {
288 $dbr = wfGetDB( DB_SLAVE
);
289 $row = $dbr->selectRow( 'archive',
290 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
291 array( 'ar_namespace' => $this->title
->getNamespace(),
292 'ar_title' => $this->title
->getDBkey() ),
294 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
296 return $this->getTextFromRow( $row );
303 * Quick check if any archived revisions are present for the page.
307 function isDeleted() {
308 $dbr = wfGetDB( DB_SLAVE
);
309 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
310 array( 'ar_namespace' => $this->title
->getNamespace(),
311 'ar_title' => $this->title
->getDBkey() ) );
316 * Restore the given (or all) text and file revisions for the page.
317 * Once restored, the items will be removed from the archive tables.
318 * The deletion log will be updated with an undeletion notice.
320 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
321 * @param $comment String
322 * @param $fileVersions Array
323 * @param $unsuppress Boolean
324 * @param $user User doing the action, or null to use $wgUser
326 * @return array(number of file revisions restored, number of image revisions restored, log message)
327 * on success, false on failure
329 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
330 global $wgContLang, $wgUser;
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 if ( !$this->fileStatus
->isOk() ) {
345 $filesRestored = $this->fileStatus
->successCount
;
351 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
352 if( $textRestored === false ) { // It must be one of UNDELETE_*
361 if( $textRestored && $filesRestored ) {
362 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
363 $wgContLang->formatNum( $textRestored ),
364 $wgContLang->formatNum( $filesRestored ) );
365 } elseif( $textRestored ) {
366 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
367 $wgContLang->formatNum( $textRestored ) );
368 } elseif( $filesRestored ) {
369 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
370 $wgContLang->formatNum( $filesRestored ) );
372 wfDebug( "Undelete: nothing undeleted...\n" );
376 if( trim( $comment ) != '' ) {
377 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
380 if ( $user === null ) {
384 $logEntry = new ManualLogEntry( 'delete', 'restore' );
385 $logEntry->setPerformer( $user );
386 $logEntry->setTarget( $this->title
);
387 $logEntry->setComment( $reason );
388 $logid = $logEntry->insert();
389 $logEntry->publish( $logid );
391 return array( $textRestored, $filesRestored, $reason );
395 * This is the meaty bit -- restores archived revisions of the given page
396 * to the cur/old tables. If the page currently exists, all revisions will
397 * be stuffed into old, otherwise the most recent will go into cur.
399 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
400 * @param $comment String
401 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
403 * @return Mixed: number of revisions restored or false on failure
405 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
406 if ( wfReadOnly() ) {
409 $restoreAll = empty( $timestamps );
411 $dbw = wfGetDB( DB_MASTER
);
413 # Does this page already exist? We'll have to update it...
414 $article = WikiPage
::factory( $this->title
);
415 # Load latest data for the current page (bug 31179)
416 $article->loadPageData( 'fromdbmaster' );
417 $oldcountable = $article->isCountable();
419 $page = $dbw->selectRow( 'page',
420 array( 'page_id', 'page_latest' ),
421 array( 'page_namespace' => $this->title
->getNamespace(),
422 'page_title' => $this->title
->getDBkey() ),
424 array( 'FOR UPDATE' ) // lock page
428 # Page already exists. Import the history, and if necessary
429 # we'll update the latest revision field in the record.
431 $pageId = $page->page_id
;
432 $previousRevId = $page->page_latest
;
433 # Get the time span of this page
434 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
435 array( 'rev_id' => $previousRevId ),
437 if( $previousTimestamp === false ) {
438 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
442 # Have to create a new article...
445 $previousTimestamp = 0;
449 $oldones = '1 = 1'; # All revisions...
451 $oldts = implode( ',',
452 array_map( array( &$dbw, 'addQuotes' ),
453 array_map( array( &$dbw, 'timestamp' ),
456 $oldones = "ar_timestamp IN ( {$oldts} )";
460 * Select each archived revision...
462 $result = $dbw->select( 'archive',
478 'ar_namespace' => $this->title
->getNamespace(),
479 'ar_title' => $this->title
->getDBkey(),
482 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
484 $ret = $dbw->resultObject( $result );
485 $rev_count = $dbw->numRows( $result );
487 wfDebug( __METHOD__
. ": no revisions to restore\n" );
491 $ret->seek( $rev_count - 1 ); // move to last
492 $row = $ret->fetchObject(); // get newest archived rev
493 $ret->seek( 0 ); // move back
496 // Check the state of the newest to-be version...
497 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
498 return false; // we can't leave the current revision like this!
500 // Safe to insert now...
501 $newid = $article->insertOn( $dbw );
504 // Check if a deleted revision will become the current revision...
505 if( $row->ar_timestamp
> $previousTimestamp ) {
506 // Check the state of the newest to-be version...
507 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
508 return false; // we can't leave the current revision like this!
516 foreach ( $ret as $row ) {
517 // Check for key dupes due to shitty archive integrity.
518 if( $row->ar_rev_id
) {
519 $exists = $dbw->selectField( 'revision', '1',
520 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
522 continue; // don't throw DB errors
525 // Insert one revision at a time...maintaining deletion status
526 // unless we are specifically removing all restrictions...
527 $revision = Revision
::newFromArchiveRow( $row,
530 'deleted' => $unsuppress ?
0 : $row->ar_deleted
533 $revision->insertOn( $dbw );
536 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
538 # Now that it's safely stored, take it out of the archive
539 $dbw->delete( 'archive',
541 'ar_namespace' => $this->title
->getNamespace(),
542 'ar_title' => $this->title
->getDBkey(),
546 // Was anything restored at all?
547 if ( $restored == 0 ) {
551 $created = (bool)$newid;
553 // Attach the latest revision to the page...
554 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
555 if ( $created ||
$wasnew ) {
556 // Update site stats, link tables, etc
557 $user = User
::newFromName( $revision->getRawUserText(), false );
558 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
561 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
563 if( $this->title
->getNamespace() == NS_FILE
) {
564 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
574 function getFileStatus() { return $this->fileStatus
; }
578 * Special page allowing users with the appropriate permissions to view
579 * and restore deleted content.
581 * @ingroup SpecialPage
583 class SpecialUndelete
extends SpecialPage
{
584 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
585 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
592 function __construct() {
593 parent
::__construct( 'Undelete', 'deletedhistory' );
596 function loadRequest( $par ) {
597 $request = $this->getRequest();
598 $user = $this->getUser();
600 $this->mAction
= $request->getVal( 'action' );
601 if ( $par !== null && $par !== '' ) {
602 $this->mTarget
= $par;
604 $this->mTarget
= $request->getVal( 'target' );
606 $this->mTargetObj
= null;
607 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
608 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
610 $this->mSearchPrefix
= $request->getText( 'prefix' );
611 $time = $request->getVal( 'timestamp' );
612 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
613 $this->mFilename
= $request->getVal( 'file' );
615 $posted = $request->wasPosted() &&
616 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
617 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
618 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
619 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
620 $this->mDiff
= $request->getCheck( 'diff' );
621 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
622 $this->mComment
= $request->getText( 'wpComment' );
623 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
624 $this->mToken
= $request->getVal( 'token' );
626 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
627 $this->mAllowed
= true; // user can restore
628 $this->mCanView
= true; // user can view content
629 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
630 $this->mAllowed
= false; // user cannot restore
631 $this->mCanView
= true; // user can view content
632 $this->mRestore
= false;
633 } else { // user can only view the list of revisions
634 $this->mAllowed
= false;
635 $this->mCanView
= false;
636 $this->mTimestamp
= '';
637 $this->mRestore
= false;
640 if( $this->mRestore ||
$this->mInvert
) {
641 $timestamps = array();
642 $this->mFileVersions
= array();
643 foreach( $request->getValues() as $key => $val ) {
645 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
646 array_push( $timestamps, $matches[1] );
649 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
650 $this->mFileVersions
[] = intval( $matches[1] );
653 rsort( $timestamps );
654 $this->mTargetTimestamp
= $timestamps;
658 function execute( $par ) {
659 $this->checkPermissions();
660 $user = $this->getUser();
663 $this->outputHeader();
665 $this->loadRequest( $par );
667 $out = $this->getOutput();
669 if ( is_null( $this->mTargetObj
) ) {
670 $out->addWikiMsg( 'undelete-header' );
672 # Not all users can just browse every deleted page from the list
673 if ( $user->isAllowed( 'browsearchive' ) ) {
674 $this->showSearchForm();
679 if ( $this->mAllowed
) {
680 $out->setPageTitle( $this->msg( 'undeletepage' ) );
682 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
685 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
687 if ( $this->mTimestamp
!== '' ) {
688 $this->showRevision( $this->mTimestamp
);
689 } elseif ( $this->mFilename
!== null ) {
690 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
691 // Check if user is allowed to see this file
692 if ( !$file->exists() ) {
693 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
694 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
695 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
696 throw new PermissionsError( 'suppressrevision' );
698 throw new PermissionsError( 'deletedtext' );
700 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
701 $this->showFileConfirmationForm( $this->mFilename
);
703 $this->showFile( $this->mFilename
);
705 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
708 $this->showHistory();
712 function showSearchForm() {
715 $out = $this->getOutput();
716 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
718 Xml
::openElement( 'form', array(
720 'action' => $wgScript ) ) .
721 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
722 Html
::hidden( 'title',
723 $this->getTitle()->getPrefixedDbKey() ) .
724 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
725 'prefix', 'prefix', 20,
726 $this->mSearchPrefix
) . ' ' .
727 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
728 Xml
::closeElement( 'fieldset' ) .
729 Xml
::closeElement( 'form' )
732 # List undeletable articles
733 if( $this->mSearchPrefix
) {
734 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
735 $this->showList( $result );
740 * Generic list of deleted pages
742 * @param $result ResultWrapper
745 private function showList( $result ) {
746 $out = $this->getOutput();
748 if( $result->numRows() == 0 ) {
749 $out->addWikiMsg( 'undelete-no-results' );
753 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
755 $undelete = $this->getTitle();
756 $out->addHTML( "<ul>\n" );
757 foreach ( $result as $row ) {
758 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
759 if ( $title !== null ) {
760 $item = Linker
::linkKnown(
762 htmlspecialchars( $title->getPrefixedText() ),
764 array( 'target' => $title->getPrefixedText() )
767 // The title is no longer valid, show as text
768 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
769 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
771 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
772 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
775 $out->addHTML( "</ul>\n" );
780 private function showRevision( $timestamp ) {
781 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
785 $archive = new PageArchive( $this->mTargetObj
);
786 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
787 $rev = $archive->getRevision( $timestamp );
789 $out = $this->getOutput();
790 $user = $this->getUser();
793 $out->addWikiMsg( 'undeleterevision-missing' );
797 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
798 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
799 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
802 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
803 $out->addHTML( '<br />' );
804 // and we are allowed to see...
809 $previousRev = $archive->getPreviousRevision( $timestamp );
811 $this->showDiff( $previousRev, $rev );
812 if( $this->mDiffOnly
) {
815 $out->addHTML( '<hr />' );
818 $out->addWikiMsg( 'undelete-nodiff' );
822 $link = Linker
::linkKnown(
823 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
824 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
827 $lang = $this->getLanguage();
829 // date and time are separate parameters to facilitate localisation.
830 // $time is kept for backward compat reasons.
831 $time = $lang->userTimeAndDate( $timestamp, $user );
832 $d = $lang->userDate( $timestamp, $user );
833 $t = $lang->userTime( $timestamp, $user );
834 $userLink = Linker
::revUserTools( $rev );
836 if( $this->mPreview
) {
837 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
839 $openDiv = '<div id="mw-undelete-revision">';
841 $out->addHTML( $openDiv );
843 // Revision delete links
844 if ( !$this->mDiff
) {
845 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
847 $out->addHTML( "$revdel " );
851 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
852 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
853 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
855 if( $this->mPreview
) {
857 $popts = $out->parserOptions();
858 $popts->setEditSection( false );
859 $out->parserOptions( $popts );
860 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
864 Xml
::element( 'textarea', array(
865 'readonly' => 'readonly',
866 'cols' => intval( $user->getOption( 'cols' ) ),
867 'rows' => intval( $user->getOption( 'rows' ) ) ),
868 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) .
869 Xml
::openElement( 'div' ) .
870 Xml
::openElement( 'form', array(
872 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
873 Xml
::element( 'input', array(
876 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
877 Xml
::element( 'input', array(
879 'name' => 'timestamp',
880 'value' => $timestamp ) ) .
881 Xml
::element( 'input', array(
883 'name' => 'wpEditToken',
884 'value' => $user->getEditToken() ) ) .
885 Xml
::element( 'input', array(
888 'value' => $this->msg( 'showpreview' )->text() ) ) .
889 Xml
::element( 'input', array(
892 'value' => $this->msg( 'showdiff' )->text() ) ) .
893 Xml
::closeElement( 'form' ) .
894 Xml
::closeElement( 'div' ) );
898 * Build a diff display between this and the previous either deleted
899 * or non-deleted edit.
901 * @param $previousRev Revision
902 * @param $currentRev Revision
903 * @return String: HTML
905 function showDiff( $previousRev, $currentRev ) {
906 $diffEngine = new DifferenceEngine( $this->getContext() );
907 $diffEngine->showDiffStyle();
908 $this->getOutput()->addHTML(
910 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
911 "<col class='diff-marker' />" .
912 "<col class='diff-content' />" .
913 "<col class='diff-marker' />" .
914 "<col class='diff-content' />" .
916 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
917 $this->diffHeader( $previousRev, 'o' ) .
919 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
920 $this->diffHeader( $currentRev, 'n' ) .
923 $diffEngine->generateDiffBody(
924 $previousRev->getText( Revision
::FOR_THIS_USER
, $this->getUser() ),
925 $currentRev->getText( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
932 * @param $rev Revision
936 private function diffHeader( $rev, $prefix ) {
937 $isDeleted = !( $rev->getId() && $rev->getTitle() );
939 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
940 $targetPage = $this->getTitle();
941 $targetQuery = array(
942 'target' => $this->mTargetObj
->getPrefixedText(),
943 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
946 /// @todo FIXME: getId() may return non-zero for deleted revs...
947 $targetPage = $rev->getTitle();
948 $targetQuery = array( 'oldid' => $rev->getId() );
950 // Add show/hide deletion links if available
951 $user = $this->getUser();
952 $lang = $this->getLanguage();
953 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
954 if ( $rdel ) $rdel = " $rdel";
956 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
961 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
962 $lang->userDate( $rev->getTimestamp(), $user ),
963 $lang->userTime( $rev->getTimestamp(), $user )
969 '<div id="mw-diff-'.$prefix.'title2">' .
970 Linker
::revUserTools( $rev ) . '<br />' .
972 '<div id="mw-diff-'.$prefix.'title3">' .
973 Linker
::revComment( $rev ) . $rdel . '<br />' .
978 * Show a form confirming whether a tokenless user really wants to see a file
980 private function showFileConfirmationForm( $key ) {
981 $out = $this->getOutput();
982 $lang = $this->getLanguage();
983 $user = $this->getUser();
984 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
985 $out->addWikiMsg( 'undelete-show-file-confirm',
986 $this->mTargetObj
->getText(),
987 $lang->userDate( $file->getTimestamp(), $user ),
988 $lang->userTime( $file->getTimestamp(), $user ) );
990 Xml
::openElement( 'form', array(
992 'action' => $this->getTitle()->getLocalURL(
993 'target=' . urlencode( $this->mTarget
) .
994 '&file=' . urlencode( $key ) .
995 '&token=' . urlencode( $user->getEditToken( $key ) ) )
998 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1004 * Show a deleted file version requested by the visitor.
1006 private function showFile( $key ) {
1007 $this->getOutput()->disable();
1009 # We mustn't allow the output to be Squid cached, otherwise
1010 # if an admin previews a deleted image, and it's cached, then
1011 # a user without appropriate permissions can toddle off and
1012 # nab the image, and Squid will serve it
1013 $response = $this->getRequest()->response();
1014 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1015 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1016 $response->header( 'Pragma: no-cache' );
1018 $repo = RepoGroup
::singleton()->getLocalRepo();
1019 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1020 $repo->streamFile( $path );
1023 private function showHistory() {
1024 $out = $this->getOutput();
1025 if( $this->mAllowed
) {
1026 $out->addModules( 'mediawiki.special.undelete' );
1029 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1030 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1033 $archive = new PageArchive( $this->mTargetObj
);
1034 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1036 $text = $archive->getLastRevisionText();
1037 if( is_null( $text ) ) {
1038 $out->addWikiMsg( 'nohistory' );
1042 $out->addHTML( '<div class="mw-undelete-history">' );
1043 if ( $this->mAllowed
) {
1044 $out->addWikiMsg( 'undeletehistory' );
1045 $out->addWikiMsg( 'undeleterevdel' );
1047 $out->addWikiMsg( 'undeletehistorynoadmin' );
1049 $out->addHTML( '</div>' );
1051 # List all stored revisions
1052 $revisions = $archive->listRevisions();
1053 $files = $archive->listFiles();
1055 $haveRevisions = $revisions && $revisions->numRows() > 0;
1056 $haveFiles = $files && $files->numRows() > 0;
1058 # Batch existence check on user and talk pages
1059 if( $haveRevisions ) {
1060 $batch = new LinkBatch();
1061 foreach ( $revisions as $row ) {
1062 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1063 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1066 $revisions->seek( 0 );
1069 $batch = new LinkBatch();
1070 foreach ( $files as $row ) {
1071 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1072 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1078 if ( $this->mAllowed
) {
1079 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1080 # Start the form here
1081 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1082 $out->addHTML( $top );
1085 # Show relevant lines from the deletion log:
1086 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1087 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1088 # Show relevant lines from the suppression log:
1089 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1090 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1091 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1094 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1095 # Format the user-visible controls (comment field, submission button)
1096 # in a nice little table
1097 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1101 <td class='mw-input'>" .
1102 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1103 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1107 $unsuppressBox = '';
1110 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1111 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1113 <td colspan='2' class='mw-undelete-extrahelp'>" .
1114 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1118 <td class='mw-label'>" .
1119 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1121 <td class='mw-input'>" .
1122 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1127 <td class='mw-submit'>" .
1128 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1129 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1133 Xml
::closeElement( 'table' ) .
1134 Xml
::closeElement( 'fieldset' );
1136 $out->addHTML( $table );
1139 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1141 if( $haveRevisions ) {
1142 # The page's stored (deleted) history:
1143 $out->addHTML( '<ul>' );
1144 $remaining = $revisions->numRows();
1145 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1147 foreach ( $revisions as $row ) {
1149 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1152 $out->addHTML( '</ul>' );
1154 $out->addWikiMsg( 'nohistory' );
1158 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1159 $out->addHTML( '<ul>' );
1160 foreach ( $files as $row ) {
1161 $out->addHTML( $this->formatFileRow( $row ) );
1164 $out->addHTML( '</ul>' );
1167 if ( $this->mAllowed
) {
1168 # Slip in the hidden controls here
1169 $misc = Html
::hidden( 'target', $this->mTarget
);
1170 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1171 $misc .= Xml
::closeElement( 'form' );
1172 $out->addHTML( $misc );
1178 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1179 $rev = Revision
::newFromArchiveRow( $row,
1180 array( 'page' => $this->mTargetObj
->getArticleID() ) );
1182 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1183 // Build checkboxen...
1184 if( $this->mAllowed
) {
1185 if( $this->mInvert
) {
1186 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1187 $checkBox = Xml
::check( "ts$ts" );
1189 $checkBox = Xml
::check( "ts$ts", true );
1192 $checkBox = Xml
::check( "ts$ts" );
1197 $user = $this->getUser();
1198 // Build page & diff links...
1199 if( $this->mCanView
) {
1200 $titleObj = $this->getTitle();
1202 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1203 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1204 $last = $this->msg( 'diff' )->escaped();
1205 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1206 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1207 $last = Linker
::linkKnown(
1209 $this->msg( 'diff' )->escaped(),
1212 'target' => $this->mTargetObj
->getPrefixedText(),
1218 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1219 $last = $this->msg( 'diff' )->escaped();
1222 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1223 $last = $this->msg( 'diff' )->escaped();
1226 $userLink = Linker
::revUserTools( $rev );
1227 // Revision text size
1228 $size = $row->ar_len
;
1229 if( !is_null( $size ) ) {
1230 $revTextSize = Linker
::formatRevisionSize( $size );
1233 $comment = Linker
::revComment( $rev );
1234 // Revision delete links
1235 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1237 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1238 return "<li>$revisionRow</li>";
1241 private function formatFileRow( $row ) {
1242 $file = ArchivedFile
::newFromRow( $row );
1244 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1245 $user = $this->getUser();
1246 if( $this->mAllowed
&& $row->fa_storage_key
) {
1247 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1248 $key = urlencode( $row->fa_storage_key
);
1249 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1252 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1254 $userLink = $this->getFileUser( $file );
1255 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1256 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1257 $data = htmlspecialchars( $data . ' ' . $bytes );
1258 $comment = $this->getFileComment( $file );
1260 // Add show/hide deletion links if available
1261 $canHide = $user->isAllowed( 'deleterevision' );
1262 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1263 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1264 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1267 'type' => 'filearchive',
1268 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1269 'ids' => $row->fa_id
1271 $revdlink = Linker
::revDeleteLink( $query,
1272 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1278 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1282 * Fetch revision text link if it's available to all users
1284 * @param $rev Revision
1285 * @param $titleObj Title
1286 * @param $ts string Timestamp
1289 function getPageLink( $rev, $titleObj, $ts ) {
1290 $user = $this->getUser();
1291 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1293 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1294 return '<span class="history-deleted">' . $time . '</span>';
1296 $link = Linker
::linkKnown(
1298 htmlspecialchars( $time ),
1301 'target' => $this->mTargetObj
->getPrefixedText(),
1305 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1306 $link = '<span class="history-deleted">' . $link . '</span>';
1313 * Fetch image view link if it's available to all users
1316 * @param $titleObj Title
1317 * @param $ts string A timestamp
1318 * @param $key String: a storage key
1320 * @return String: HTML fragment
1322 function getFileLink( $file, $titleObj, $ts, $key ) {
1323 $user = $this->getUser();
1324 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1326 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1327 return '<span class="history-deleted">' . $time . '</span>';
1329 $link = Linker
::linkKnown(
1331 htmlspecialchars( $time ),
1334 'target' => $this->mTargetObj
->getPrefixedText(),
1336 'token' => $user->getEditToken( $key )
1339 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1340 $link = '<span class="history-deleted">' . $link . '</span>';
1347 * Fetch file's user id if it's available to this user
1350 * @return String: HTML fragment
1352 function getFileUser( $file ) {
1353 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1354 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1356 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1357 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1358 if( $file->isDeleted( File
::DELETED_USER
) ) {
1359 $link = '<span class="history-deleted">' . $link . '</span>';
1366 * Fetch file upload comment if it's available to this user
1369 * @return String: HTML fragment
1371 function getFileComment( $file ) {
1372 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1373 return '<span class="history-deleted"><span class="comment">' .
1374 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1376 $link = Linker
::commentBlock( $file->getRawDescription() );
1377 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1378 $link = '<span class="history-deleted">' . $link . '</span>';
1384 function undelete() {
1385 global $wgUploadMaintenance;
1387 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1388 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1391 if ( wfReadOnly() ) {
1392 throw new ReadOnlyError
;
1395 $out = $this->getOutput();
1396 $archive = new PageArchive( $this->mTargetObj
);
1397 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1398 $ok = $archive->undelete(
1399 $this->mTargetTimestamp
,
1401 $this->mFileVersions
,
1406 if( is_array( $ok ) ) {
1407 if ( $ok[1] ) { // Undeleted file count
1408 wfRunHooks( 'FileUndeleteComplete', array(
1409 $this->mTargetObj
, $this->mFileVersions
,
1410 $this->getUser(), $this->mComment
) );
1413 $link = Linker
::linkKnown( $this->mTargetObj
);
1414 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1416 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1417 $out->addWikiMsg( 'cannotundelete' );
1418 $out->addWikiMsg( 'undeleterevdel' );
1421 // Show file deletion warnings and errors
1422 $status = $archive->getFileStatus();
1423 if( $status && !$status->isGood() ) {
1424 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );