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 global $wgContentHandlerNoDB;
117 $dbr = wfGetDB( DB_SLAVE
);
120 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
121 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
124 if ( !$wgContentHandlerNoDB ) {
125 $fields[] = 'ar_content_format';
126 $fields[] = 'ar_content_model';
129 $res = $dbr->select( 'archive',
131 array( 'ar_namespace' => $this->title
->getNamespace(),
132 'ar_title' => $this->title
->getDBkey() ),
133 'PageArchive::listRevisions',
134 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
135 $ret = $dbr->resultObject( $res );
140 * List the deleted file revisions for this page, if it's a file page.
141 * Returns a result wrapper with various filearchive fields, or null
142 * if not a file page.
144 * @return ResultWrapper
145 * @todo Does this belong in Image for fuller encapsulation?
147 function listFiles() {
148 if( $this->title
->getNamespace() == NS_FILE
) {
149 $dbr = wfGetDB( DB_SLAVE
);
150 $res = $dbr->select( 'filearchive',
170 array( 'fa_name' => $this->title
->getDBkey() ),
172 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
173 $ret = $dbr->resultObject( $res );
180 * Return a Revision object containing data for the deleted revision.
181 * Note that the result *may* or *may not* have a null page ID.
183 * @param $timestamp String
186 function getRevision( $timestamp ) {
187 global $wgContentHandlerNoDB;
189 $dbr = wfGetDB( DB_SLAVE
);
206 if ( !$wgContentHandlerNoDB ) {
207 $fields[] = 'ar_content_format';
208 $fields[] = 'ar_content_model';
211 $row = $dbr->selectRow( 'archive',
213 array( 'ar_namespace' => $this->title
->getNamespace(),
214 'ar_title' => $this->title
->getDBkey(),
215 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
218 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleID() ) );
225 * Return the most-previous revision, either live or deleted, against
226 * the deleted revision given by timestamp.
228 * May produce unexpected results in case of history merges or other
229 * unusual time issues.
231 * @param $timestamp String
232 * @return Revision or null
234 function getPreviousRevision( $timestamp ) {
235 $dbr = wfGetDB( DB_SLAVE
);
237 // Check the previous deleted revision...
238 $row = $dbr->selectRow( 'archive',
240 array( 'ar_namespace' => $this->title
->getNamespace(),
241 'ar_title' => $this->title
->getDBkey(),
243 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
246 'ORDER BY' => 'ar_timestamp DESC',
248 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
250 $row = $dbr->selectRow( array( 'page', 'revision' ),
251 array( 'rev_id', 'rev_timestamp' ),
253 'page_namespace' => $this->title
->getNamespace(),
254 'page_title' => $this->title
->getDBkey(),
255 'page_id = rev_page',
257 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
260 'ORDER BY' => 'rev_timestamp DESC',
262 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
263 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
265 if( $prevLive && $prevLive > $prevDeleted ) {
266 // Most prior revision was live
267 return Revision
::newFromId( $prevLiveId );
268 } elseif( $prevDeleted ) {
269 // Most prior revision was deleted
270 return $this->getRevision( $prevDeleted );
272 // No prior revision on this page.
278 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
280 * @param $row Object: database row
283 function getTextFromRow( $row ) {
284 if( is_null( $row->ar_text_id
) ) {
285 // An old row from MediaWiki 1.4 or previous.
286 // Text is embedded in this row in classic compression format.
287 return Revision
::getRevisionText( $row, 'ar_' );
289 // New-style: keyed to the text storage backend.
290 $dbr = wfGetDB( DB_SLAVE
);
291 $text = $dbr->selectRow( 'text',
292 array( 'old_text', 'old_flags' ),
293 array( 'old_id' => $row->ar_text_id
),
295 return Revision
::getRevisionText( $text );
300 * Fetch (and decompress if necessary) the stored text of the most
301 * recently edited deleted revision of the page.
303 * If there are no archived revisions for the page, returns NULL.
307 function getLastRevisionText() {
308 $dbr = wfGetDB( DB_SLAVE
);
309 $row = $dbr->selectRow( 'archive',
310 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
311 array( 'ar_namespace' => $this->title
->getNamespace(),
312 'ar_title' => $this->title
->getDBkey() ),
314 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
316 return $this->getTextFromRow( $row );
323 * Quick check if any archived revisions are present for the page.
327 function isDeleted() {
328 $dbr = wfGetDB( DB_SLAVE
);
329 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
330 array( 'ar_namespace' => $this->title
->getNamespace(),
331 'ar_title' => $this->title
->getDBkey() ) );
336 * Restore the given (or all) text and file revisions for the page.
337 * Once restored, the items will be removed from the archive tables.
338 * The deletion log will be updated with an undeletion notice.
340 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
341 * @param $comment String
342 * @param $fileVersions Array
343 * @param $unsuppress Boolean
344 * @param $user User doing the action, or null to use $wgUser
346 * @return array(number of file revisions restored, number of image revisions restored, log message)
347 * on success, false on failure
349 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
350 global $wgContLang, $wgUser;
352 // If both the set of text revisions and file revisions are empty,
353 // restore everything. Otherwise, just restore the requested items.
354 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
356 $restoreText = $restoreAll ||
!empty( $timestamps );
357 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
359 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
360 $img = wfLocalFile( $this->title
);
361 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
362 if ( !$this->fileStatus
->isOk() ) {
365 $filesRestored = $this->fileStatus
->successCount
;
371 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
372 if( $textRestored === false ) { // It must be one of UNDELETE_*
381 if( $textRestored && $filesRestored ) {
382 $reason = wfMessage( 'undeletedrevisions-files' )
383 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
384 } elseif( $textRestored ) {
385 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
386 ->inContentLanguage()->text();
387 } elseif( $filesRestored ) {
388 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
389 ->inContentLanguage()->text();
391 wfDebug( "Undelete: nothing undeleted...\n" );
395 if( trim( $comment ) != '' ) {
396 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
399 if ( $user === null ) {
403 $logEntry = new ManualLogEntry( 'delete', 'restore' );
404 $logEntry->setPerformer( $user );
405 $logEntry->setTarget( $this->title
);
406 $logEntry->setComment( $reason );
407 $logid = $logEntry->insert();
408 $logEntry->publish( $logid );
410 return array( $textRestored, $filesRestored, $reason );
414 * This is the meaty bit -- restores archived revisions of the given page
415 * to the cur/old tables. If the page currently exists, all revisions will
416 * be stuffed into old, otherwise the most recent will go into cur.
418 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
419 * @param $comment String
420 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
422 * @return Mixed: number of revisions restored or false on failure
424 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
425 global $wgContentHandlerNoDB;
427 if ( wfReadOnly() ) {
430 $restoreAll = empty( $timestamps );
432 $dbw = wfGetDB( DB_MASTER
);
434 # Does this page already exist? We'll have to update it...
435 $article = WikiPage
::factory( $this->title
);
436 # Load latest data for the current page (bug 31179)
437 $article->loadPageData( 'fromdbmaster' );
438 $oldcountable = $article->isCountable();
440 $page = $dbw->selectRow( 'page',
441 array( 'page_id', 'page_latest' ),
442 array( 'page_namespace' => $this->title
->getNamespace(),
443 'page_title' => $this->title
->getDBkey() ),
445 array( 'FOR UPDATE' ) // lock page
449 # Page already exists. Import the history, and if necessary
450 # we'll update the latest revision field in the record.
452 $pageId = $page->page_id
;
453 $previousRevId = $page->page_latest
;
454 # Get the time span of this page
455 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
456 array( 'rev_id' => $previousRevId ),
458 if( $previousTimestamp === false ) {
459 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
463 # Have to create a new article...
466 $previousTimestamp = 0;
470 $oldones = '1 = 1'; # All revisions...
472 $oldts = implode( ',',
473 array_map( array( &$dbw, 'addQuotes' ),
474 array_map( array( &$dbw, 'timestamp' ),
477 $oldones = "ar_timestamp IN ( {$oldts} )";
495 if ( !$wgContentHandlerNoDB ) {
496 $fields[] = 'ar_content_format';
497 $fields[] = 'ar_content_model';
501 * Select each archived revision...
503 $result = $dbw->select( 'archive',
506 'ar_namespace' => $this->title
->getNamespace(),
507 'ar_title' => $this->title
->getDBkey(),
510 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
512 $ret = $dbw->resultObject( $result );
513 $rev_count = $dbw->numRows( $result );
515 wfDebug( __METHOD__
. ": no revisions to restore\n" );
519 $ret->seek( $rev_count - 1 ); // move to last
520 $row = $ret->fetchObject(); // get newest archived rev
521 $ret->seek( 0 ); // move back
524 // Check the state of the newest to-be version...
525 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
526 return false; // we can't leave the current revision like this!
528 // Safe to insert now...
529 $newid = $article->insertOn( $dbw );
532 // Check if a deleted revision will become the current revision...
533 if( $row->ar_timestamp
> $previousTimestamp ) {
534 // Check the state of the newest to-be version...
535 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
536 return false; // we can't leave the current revision like this!
544 foreach ( $ret as $row ) {
545 // Check for key dupes due to shitty archive integrity.
546 if( $row->ar_rev_id
) {
547 $exists = $dbw->selectField( 'revision', '1',
548 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
550 continue; // don't throw DB errors
553 // Insert one revision at a time...maintaining deletion status
554 // unless we are specifically removing all restrictions...
555 $revision = Revision
::newFromArchiveRow( $row,
558 'deleted' => $unsuppress ?
0 : $row->ar_deleted
561 $revision->insertOn( $dbw );
564 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
566 # Now that it's safely stored, take it out of the archive
567 $dbw->delete( 'archive',
569 'ar_namespace' => $this->title
->getNamespace(),
570 'ar_title' => $this->title
->getDBkey(),
574 // Was anything restored at all?
575 if ( $restored == 0 ) {
579 $created = (bool)$newid;
581 // Attach the latest revision to the page...
582 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
583 if ( $created ||
$wasnew ) {
584 // Update site stats, link tables, etc
585 $user = User
::newFromName( $revision->getRawUserText(), false );
586 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
589 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
591 if( $this->title
->getNamespace() == NS_FILE
) {
592 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
602 function getFileStatus() { return $this->fileStatus
; }
606 * Special page allowing users with the appropriate permissions to view
607 * and restore deleted content.
609 * @ingroup SpecialPage
611 class SpecialUndelete
extends SpecialPage
{
612 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
613 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
620 function __construct() {
621 parent
::__construct( 'Undelete', 'deletedhistory' );
624 function loadRequest( $par ) {
625 $request = $this->getRequest();
626 $user = $this->getUser();
628 $this->mAction
= $request->getVal( 'action' );
629 if ( $par !== null && $par !== '' ) {
630 $this->mTarget
= $par;
632 $this->mTarget
= $request->getVal( 'target' );
634 $this->mTargetObj
= null;
635 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
636 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
638 $this->mSearchPrefix
= $request->getText( 'prefix' );
639 $time = $request->getVal( 'timestamp' );
640 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
641 $this->mFilename
= $request->getVal( 'file' );
643 $posted = $request->wasPosted() &&
644 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
645 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
646 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
647 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
648 $this->mDiff
= $request->getCheck( 'diff' );
649 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
650 $this->mComment
= $request->getText( 'wpComment' );
651 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
652 $this->mToken
= $request->getVal( 'token' );
654 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
655 $this->mAllowed
= true; // user can restore
656 $this->mCanView
= true; // user can view content
657 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
658 $this->mAllowed
= false; // user cannot restore
659 $this->mCanView
= true; // user can view content
660 $this->mRestore
= false;
661 } else { // user can only view the list of revisions
662 $this->mAllowed
= false;
663 $this->mCanView
= false;
664 $this->mTimestamp
= '';
665 $this->mRestore
= false;
668 if( $this->mRestore ||
$this->mInvert
) {
669 $timestamps = array();
670 $this->mFileVersions
= array();
671 foreach( $request->getValues() as $key => $val ) {
673 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
674 array_push( $timestamps, $matches[1] );
677 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
678 $this->mFileVersions
[] = intval( $matches[1] );
681 rsort( $timestamps );
682 $this->mTargetTimestamp
= $timestamps;
686 function execute( $par ) {
687 $this->checkPermissions();
688 $user = $this->getUser();
691 $this->outputHeader();
693 $this->loadRequest( $par );
695 $out = $this->getOutput();
697 if ( is_null( $this->mTargetObj
) ) {
698 $out->addWikiMsg( 'undelete-header' );
700 # Not all users can just browse every deleted page from the list
701 if ( $user->isAllowed( 'browsearchive' ) ) {
702 $this->showSearchForm();
707 if ( $this->mAllowed
) {
708 $out->setPageTitle( $this->msg( 'undeletepage' ) );
710 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
713 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
715 if ( $this->mTimestamp
!== '' ) {
716 $this->showRevision( $this->mTimestamp
);
717 } elseif ( $this->mFilename
!== null ) {
718 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
719 // Check if user is allowed to see this file
720 if ( !$file->exists() ) {
721 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
722 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
723 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
724 throw new PermissionsError( 'suppressrevision' );
726 throw new PermissionsError( 'deletedtext' );
728 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
729 $this->showFileConfirmationForm( $this->mFilename
);
731 $this->showFile( $this->mFilename
);
733 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
736 $this->showHistory();
740 function showSearchForm() {
743 $out = $this->getOutput();
744 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
746 Xml
::openElement( 'form', array(
748 'action' => $wgScript ) ) .
749 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
750 Html
::hidden( 'title',
751 $this->getTitle()->getPrefixedDbKey() ) .
752 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
753 'prefix', 'prefix', 20,
754 $this->mSearchPrefix
) . ' ' .
755 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
756 Xml
::closeElement( 'fieldset' ) .
757 Xml
::closeElement( 'form' )
760 # List undeletable articles
761 if( $this->mSearchPrefix
) {
762 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
763 $this->showList( $result );
768 * Generic list of deleted pages
770 * @param $result ResultWrapper
773 private function showList( $result ) {
774 $out = $this->getOutput();
776 if( $result->numRows() == 0 ) {
777 $out->addWikiMsg( 'undelete-no-results' );
781 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
783 $undelete = $this->getTitle();
784 $out->addHTML( "<ul>\n" );
785 foreach ( $result as $row ) {
786 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
787 if ( $title !== null ) {
788 $item = Linker
::linkKnown(
790 htmlspecialchars( $title->getPrefixedText() ),
792 array( 'target' => $title->getPrefixedText() )
795 // The title is no longer valid, show as text
796 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
797 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
799 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
800 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
803 $out->addHTML( "</ul>\n" );
808 private function showRevision( $timestamp ) {
809 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
813 $archive = new PageArchive( $this->mTargetObj
);
814 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
815 $rev = $archive->getRevision( $timestamp );
817 $out = $this->getOutput();
818 $user = $this->getUser();
821 $out->addWikiMsg( 'undeleterevision-missing' );
825 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
826 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
827 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
830 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
831 $out->addHTML( '<br />' );
832 // and we are allowed to see...
837 $previousRev = $archive->getPreviousRevision( $timestamp );
839 $this->showDiff( $previousRev, $rev );
840 if( $this->mDiffOnly
) {
843 $out->addHTML( '<hr />' );
846 $out->addWikiMsg( 'undelete-nodiff' );
850 $link = Linker
::linkKnown(
851 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
852 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
855 $lang = $this->getLanguage();
857 // date and time are separate parameters to facilitate localisation.
858 // $time is kept for backward compat reasons.
859 $time = $lang->userTimeAndDate( $timestamp, $user );
860 $d = $lang->userDate( $timestamp, $user );
861 $t = $lang->userTime( $timestamp, $user );
862 $userLink = Linker
::revUserTools( $rev );
864 if( $this->mPreview
) {
865 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
867 $openDiv = '<div id="mw-undelete-revision">';
869 $out->addHTML( $openDiv );
871 // Revision delete links
872 if ( !$this->mDiff
) {
873 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
875 $out->addHTML( "$revdel " );
879 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
880 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
881 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
883 if( $this->mPreview
) {
885 //FIXME: ContentHandler will have to provide some specialized magic to do this
886 $popts = $out->parserOptions();
887 $popts->setEditSection( false );
888 $out->parserOptions( $popts );
889 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
892 //FIXME: ContentHandler will have to provide some specialized magic for reviewing content before undeletion
895 Xml
::element( 'textarea', array(
896 'readonly' => 'readonly',
897 'cols' => intval( $user->getOption( 'cols' ) ),
898 'rows' => intval( $user->getOption( 'rows' ) ) ),
899 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) .
900 Xml
::openElement( 'div' ) .
901 Xml
::openElement( 'form', array(
903 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
904 Xml
::element( 'input', array(
907 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
908 Xml
::element( 'input', array(
910 'name' => 'timestamp',
911 'value' => $timestamp ) ) .
912 Xml
::element( 'input', array(
914 'name' => 'wpEditToken',
915 'value' => $user->getEditToken() ) ) .
916 Xml
::element( 'input', array(
919 'value' => $this->msg( 'showpreview' )->text() ) ) .
920 Xml
::element( 'input', array(
923 'value' => $this->msg( 'showdiff' )->text() ) ) .
924 Xml
::closeElement( 'form' ) .
925 Xml
::closeElement( 'div' ) );
929 * Build a diff display between this and the previous either deleted
930 * or non-deleted edit.
932 * @param $previousRev Revision
933 * @param $currentRev Revision
934 * @return String: HTML
936 function showDiff( $previousRev, $currentRev ) {
937 $contentHandler = ContentHandler
::getForTitle( $this->getTitle() );
938 $diffEngine = $contentHandler->createDifferenceEngine( $this->getContext() );
939 $diffEngine->showDiffStyle();
940 $this->getOutput()->addHTML(
942 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
943 "<col class='diff-marker' />" .
944 "<col class='diff-content' />" .
945 "<col class='diff-marker' />" .
946 "<col class='diff-content' />" .
948 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
949 $this->diffHeader( $previousRev, 'o' ) .
951 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
952 $this->diffHeader( $currentRev, 'n' ) .
955 $diffEngine->generateContentDiffBody(
956 $previousRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ),
957 $currentRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
964 * @param $rev Revision
968 private function diffHeader( $rev, $prefix ) {
969 $isDeleted = !( $rev->getId() && $rev->getTitle() );
971 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
972 $targetPage = $this->getTitle();
973 $targetQuery = array(
974 'target' => $this->mTargetObj
->getPrefixedText(),
975 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
978 /// @todo FIXME: getId() may return non-zero for deleted revs...
979 $targetPage = $rev->getTitle();
980 $targetQuery = array( 'oldid' => $rev->getId() );
982 // Add show/hide deletion links if available
983 $user = $this->getUser();
984 $lang = $this->getLanguage();
985 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
986 if ( $rdel ) $rdel = " $rdel";
988 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
993 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
994 $lang->userDate( $rev->getTimestamp(), $user ),
995 $lang->userTime( $rev->getTimestamp(), $user )
1001 '<div id="mw-diff-'.$prefix.'title2">' .
1002 Linker
::revUserTools( $rev ) . '<br />' .
1004 '<div id="mw-diff-'.$prefix.'title3">' .
1005 Linker
::revComment( $rev ) . $rdel . '<br />' .
1010 * Show a form confirming whether a tokenless user really wants to see a file
1012 private function showFileConfirmationForm( $key ) {
1013 $out = $this->getOutput();
1014 $lang = $this->getLanguage();
1015 $user = $this->getUser();
1016 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1017 $out->addWikiMsg( 'undelete-show-file-confirm',
1018 $this->mTargetObj
->getText(),
1019 $lang->userDate( $file->getTimestamp(), $user ),
1020 $lang->userTime( $file->getTimestamp(), $user ) );
1022 Xml
::openElement( 'form', array(
1024 'action' => $this->getTitle()->getLocalURL(
1025 'target=' . urlencode( $this->mTarget
) .
1026 '&file=' . urlencode( $key ) .
1027 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1030 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1036 * Show a deleted file version requested by the visitor.
1038 private function showFile( $key ) {
1039 $this->getOutput()->disable();
1041 # We mustn't allow the output to be Squid cached, otherwise
1042 # if an admin previews a deleted image, and it's cached, then
1043 # a user without appropriate permissions can toddle off and
1044 # nab the image, and Squid will serve it
1045 $response = $this->getRequest()->response();
1046 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1047 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1048 $response->header( 'Pragma: no-cache' );
1050 $repo = RepoGroup
::singleton()->getLocalRepo();
1051 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1052 $repo->streamFile( $path );
1055 private function showHistory() {
1056 $out = $this->getOutput();
1057 if( $this->mAllowed
) {
1058 $out->addModules( 'mediawiki.special.undelete' );
1061 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1062 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1065 $archive = new PageArchive( $this->mTargetObj
);
1066 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1068 $text = $archive->getLastRevisionText();
1069 if( is_null( $text ) ) {
1070 $out->addWikiMsg( 'nohistory' );
1074 $out->addHTML( '<div class="mw-undelete-history">' );
1075 if ( $this->mAllowed
) {
1076 $out->addWikiMsg( 'undeletehistory' );
1077 $out->addWikiMsg( 'undeleterevdel' );
1079 $out->addWikiMsg( 'undeletehistorynoadmin' );
1081 $out->addHTML( '</div>' );
1083 # List all stored revisions
1084 $revisions = $archive->listRevisions();
1085 $files = $archive->listFiles();
1087 $haveRevisions = $revisions && $revisions->numRows() > 0;
1088 $haveFiles = $files && $files->numRows() > 0;
1090 # Batch existence check on user and talk pages
1091 if( $haveRevisions ) {
1092 $batch = new LinkBatch();
1093 foreach ( $revisions as $row ) {
1094 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1095 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1098 $revisions->seek( 0 );
1101 $batch = new LinkBatch();
1102 foreach ( $files as $row ) {
1103 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1104 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1110 if ( $this->mAllowed
) {
1111 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1112 # Start the form here
1113 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1114 $out->addHTML( $top );
1117 # Show relevant lines from the deletion log:
1118 $deleteLogPage = new LogPage( 'delete' );
1119 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1120 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1121 # Show relevant lines from the suppression log:
1122 $suppressLogPage = new LogPage( 'suppress' );
1123 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1124 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1125 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1128 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1129 # Format the user-visible controls (comment field, submission button)
1130 # in a nice little table
1131 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1135 <td class='mw-input'>" .
1136 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1137 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1141 $unsuppressBox = '';
1144 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1145 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1147 <td colspan='2' class='mw-undelete-extrahelp'>" .
1148 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1152 <td class='mw-label'>" .
1153 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1155 <td class='mw-input'>" .
1156 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1161 <td class='mw-submit'>" .
1162 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1163 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1167 Xml
::closeElement( 'table' ) .
1168 Xml
::closeElement( 'fieldset' );
1170 $out->addHTML( $table );
1173 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1175 if( $haveRevisions ) {
1176 # The page's stored (deleted) history:
1177 $out->addHTML( '<ul>' );
1178 $remaining = $revisions->numRows();
1179 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1181 foreach ( $revisions as $row ) {
1183 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1186 $out->addHTML( '</ul>' );
1188 $out->addWikiMsg( 'nohistory' );
1192 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1193 $out->addHTML( '<ul>' );
1194 foreach ( $files as $row ) {
1195 $out->addHTML( $this->formatFileRow( $row ) );
1198 $out->addHTML( '</ul>' );
1201 if ( $this->mAllowed
) {
1202 # Slip in the hidden controls here
1203 $misc = Html
::hidden( 'target', $this->mTarget
);
1204 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1205 $misc .= Xml
::closeElement( 'form' );
1206 $out->addHTML( $misc );
1212 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1213 $rev = Revision
::newFromArchiveRow( $row,
1214 array( 'page' => $this->mTargetObj
->getArticleID() ) );
1216 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1217 // Build checkboxen...
1218 if( $this->mAllowed
) {
1219 if( $this->mInvert
) {
1220 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1221 $checkBox = Xml
::check( "ts$ts" );
1223 $checkBox = Xml
::check( "ts$ts", true );
1226 $checkBox = Xml
::check( "ts$ts" );
1231 $user = $this->getUser();
1232 // Build page & diff links...
1233 if( $this->mCanView
) {
1234 $titleObj = $this->getTitle();
1236 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1237 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1238 $last = $this->msg( 'diff' )->escaped();
1239 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1240 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1241 $last = Linker
::linkKnown(
1243 $this->msg( 'diff' )->escaped(),
1246 'target' => $this->mTargetObj
->getPrefixedText(),
1252 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1253 $last = $this->msg( 'diff' )->escaped();
1256 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1257 $last = $this->msg( 'diff' )->escaped();
1260 $userLink = Linker
::revUserTools( $rev );
1261 // Revision text size
1262 $size = $row->ar_len
;
1263 if( !is_null( $size ) ) {
1264 $revTextSize = Linker
::formatRevisionSize( $size );
1267 $comment = Linker
::revComment( $rev );
1268 // Revision delete links
1269 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1271 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1272 return "<li>$revisionRow</li>";
1275 private function formatFileRow( $row ) {
1276 $file = ArchivedFile
::newFromRow( $row );
1278 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1279 $user = $this->getUser();
1280 if( $this->mAllowed
&& $row->fa_storage_key
) {
1281 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1282 $key = urlencode( $row->fa_storage_key
);
1283 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1286 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1288 $userLink = $this->getFileUser( $file );
1289 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1290 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1291 $data = htmlspecialchars( $data . ' ' . $bytes );
1292 $comment = $this->getFileComment( $file );
1294 // Add show/hide deletion links if available
1295 $canHide = $user->isAllowed( 'deleterevision' );
1296 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1297 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1298 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1301 'type' => 'filearchive',
1302 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1303 'ids' => $row->fa_id
1305 $revdlink = Linker
::revDeleteLink( $query,
1306 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1312 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1316 * Fetch revision text link if it's available to all users
1318 * @param $rev Revision
1319 * @param $titleObj Title
1320 * @param $ts string Timestamp
1323 function getPageLink( $rev, $titleObj, $ts ) {
1324 $user = $this->getUser();
1325 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1327 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1328 return '<span class="history-deleted">' . $time . '</span>';
1330 $link = Linker
::linkKnown(
1332 htmlspecialchars( $time ),
1335 'target' => $this->mTargetObj
->getPrefixedText(),
1339 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1340 $link = '<span class="history-deleted">' . $link . '</span>';
1347 * Fetch image view link if it's available to all users
1350 * @param $titleObj Title
1351 * @param $ts string A timestamp
1352 * @param $key String: a storage key
1354 * @return String: HTML fragment
1356 function getFileLink( $file, $titleObj, $ts, $key ) {
1357 $user = $this->getUser();
1358 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1360 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1361 return '<span class="history-deleted">' . $time . '</span>';
1363 $link = Linker
::linkKnown(
1365 htmlspecialchars( $time ),
1368 'target' => $this->mTargetObj
->getPrefixedText(),
1370 'token' => $user->getEditToken( $key )
1373 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1374 $link = '<span class="history-deleted">' . $link . '</span>';
1381 * Fetch file's user id if it's available to this user
1384 * @return String: HTML fragment
1386 function getFileUser( $file ) {
1387 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1388 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1390 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1391 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1392 if( $file->isDeleted( File
::DELETED_USER
) ) {
1393 $link = '<span class="history-deleted">' . $link . '</span>';
1400 * Fetch file upload comment if it's available to this user
1403 * @return String: HTML fragment
1405 function getFileComment( $file ) {
1406 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1407 return '<span class="history-deleted"><span class="comment">' .
1408 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1410 $link = Linker
::commentBlock( $file->getRawDescription() );
1411 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1412 $link = '<span class="history-deleted">' . $link . '</span>';
1418 function undelete() {
1419 global $wgUploadMaintenance;
1421 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1422 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1425 if ( wfReadOnly() ) {
1426 throw new ReadOnlyError
;
1429 $out = $this->getOutput();
1430 $archive = new PageArchive( $this->mTargetObj
);
1431 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1432 $ok = $archive->undelete(
1433 $this->mTargetTimestamp
,
1435 $this->mFileVersions
,
1440 if( is_array( $ok ) ) {
1441 if ( $ok[1] ) { // Undeleted file count
1442 wfRunHooks( 'FileUndeleteComplete', array(
1443 $this->mTargetObj
, $this->mFileVersions
,
1444 $this->getUser(), $this->mComment
) );
1447 $link = Linker
::linkKnown( $this->mTargetObj
);
1448 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1450 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1451 $out->addWikiMsg( 'cannotundelete' );
1452 $out->addWikiMsg( 'undeleterevdel' );
1455 // Show file deletion warnings and errors
1456 $status = $archive->getFileStatus();
1457 if( $status && !$status->isGood() ) {
1458 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );