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
39 protected $fileStatus;
44 protected $revisionStatus;
46 function __construct( $title ) {
47 if( is_null( $title ) ) {
48 throw new MWException( __METHOD__
. ' given a null title.' );
50 $this->title
= $title;
54 * List all deleted pages recorded in the archive table. Returns result
55 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
58 * @return ResultWrapper
60 public static function listAllPages() {
61 $dbr = wfGetDB( DB_SLAVE
);
62 return self
::listPages( $dbr, '' );
66 * List deleted pages recorded in the archive table matching the
68 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
70 * @param $prefix String: title prefix
71 * @return ResultWrapper
73 public static function listPagesByPrefix( $prefix ) {
74 $dbr = wfGetDB( DB_SLAVE
);
76 $title = Title
::newFromText( $prefix );
78 $ns = $title->getNamespace();
79 $prefix = $title->getDBkey();
81 // Prolly won't work too good
82 // @todo handle bare namespace names cleanly?
86 'ar_namespace' => $ns,
87 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
89 return self
::listPages( $dbr, $conds );
93 * @param $dbr DatabaseBase
95 * @return bool|ResultWrapper
97 protected static function listPages( $dbr, $condition ) {
98 return $dbr->resultObject(
104 'count' => 'COUNT(*)'
109 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
110 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
118 * List the revisions of the given page. Returns result wrapper with
119 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
121 * @return ResultWrapper
123 function listRevisions() {
124 global $wgContentHandlerNoDB;
126 $dbr = wfGetDB( DB_SLAVE
);
129 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
130 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
133 if ( !$wgContentHandlerNoDB ) {
134 $fields[] = 'ar_content_format';
135 $fields[] = 'ar_content_model';
138 $res = $dbr->select( 'archive',
140 array( 'ar_namespace' => $this->title
->getNamespace(),
141 'ar_title' => $this->title
->getDBkey() ),
142 'PageArchive::listRevisions',
143 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
144 $ret = $dbr->resultObject( $res );
149 * List the deleted file revisions for this page, if it's a file page.
150 * Returns a result wrapper with various filearchive fields, or null
151 * if not a file page.
153 * @return ResultWrapper
154 * @todo Does this belong in Image for fuller encapsulation?
156 function listFiles() {
157 if( $this->title
->getNamespace() == NS_FILE
) {
158 $dbr = wfGetDB( DB_SLAVE
);
159 $res = $dbr->select( 'filearchive',
179 array( 'fa_name' => $this->title
->getDBkey() ),
181 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
182 $ret = $dbr->resultObject( $res );
189 * Return a Revision object containing data for the deleted revision.
190 * Note that the result *may* or *may not* have a null page ID.
192 * @param $timestamp String
195 function getRevision( $timestamp ) {
196 global $wgContentHandlerNoDB;
198 $dbr = wfGetDB( DB_SLAVE
);
215 if ( !$wgContentHandlerNoDB ) {
216 $fields[] = 'ar_content_format';
217 $fields[] = 'ar_content_model';
220 $row = $dbr->selectRow( 'archive',
222 array( 'ar_namespace' => $this->title
->getNamespace(),
223 'ar_title' => $this->title
->getDBkey(),
224 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
227 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleID() ) );
234 * Return the most-previous revision, either live or deleted, against
235 * the deleted revision given by timestamp.
237 * May produce unexpected results in case of history merges or other
238 * unusual time issues.
240 * @param $timestamp String
241 * @return Revision or null
243 function getPreviousRevision( $timestamp ) {
244 $dbr = wfGetDB( DB_SLAVE
);
246 // Check the previous deleted revision...
247 $row = $dbr->selectRow( 'archive',
249 array( 'ar_namespace' => $this->title
->getNamespace(),
250 'ar_title' => $this->title
->getDBkey(),
252 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
255 'ORDER BY' => 'ar_timestamp DESC',
257 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
259 $row = $dbr->selectRow( array( 'page', 'revision' ),
260 array( 'rev_id', 'rev_timestamp' ),
262 'page_namespace' => $this->title
->getNamespace(),
263 'page_title' => $this->title
->getDBkey(),
264 'page_id = rev_page',
266 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
269 'ORDER BY' => 'rev_timestamp DESC',
271 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
272 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
274 if( $prevLive && $prevLive > $prevDeleted ) {
275 // Most prior revision was live
276 return Revision
::newFromId( $prevLiveId );
277 } elseif( $prevDeleted ) {
278 // Most prior revision was deleted
279 return $this->getRevision( $prevDeleted );
281 // No prior revision on this page.
287 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
289 * @param $row Object: database row
292 function getTextFromRow( $row ) {
293 if( is_null( $row->ar_text_id
) ) {
294 // An old row from MediaWiki 1.4 or previous.
295 // Text is embedded in this row in classic compression format.
296 return Revision
::getRevisionText( $row, 'ar_' );
298 // New-style: keyed to the text storage backend.
299 $dbr = wfGetDB( DB_SLAVE
);
300 $text = $dbr->selectRow( 'text',
301 array( 'old_text', 'old_flags' ),
302 array( 'old_id' => $row->ar_text_id
),
304 return Revision
::getRevisionText( $text );
309 * Fetch (and decompress if necessary) the stored text of the most
310 * recently edited deleted revision of the page.
312 * If there are no archived revisions for the page, returns NULL.
316 function getLastRevisionText() {
317 $dbr = wfGetDB( DB_SLAVE
);
318 $row = $dbr->selectRow( 'archive',
319 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
320 array( 'ar_namespace' => $this->title
->getNamespace(),
321 'ar_title' => $this->title
->getDBkey() ),
323 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
325 return $this->getTextFromRow( $row );
332 * Quick check if any archived revisions are present for the page.
336 function isDeleted() {
337 $dbr = wfGetDB( DB_SLAVE
);
338 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
339 array( 'ar_namespace' => $this->title
->getNamespace(),
340 'ar_title' => $this->title
->getDBkey() ) );
345 * Restore the given (or all) text and file revisions for the page.
346 * Once restored, the items will be removed from the archive tables.
347 * The deletion log will be updated with an undeletion notice.
349 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
350 * @param $comment String
351 * @param $fileVersions Array
352 * @param $unsuppress Boolean
353 * @param $user User doing the action, or null to use $wgUser
355 * @return array(number of file revisions restored, number of image revisions restored, log message)
356 * on success, false on failure
358 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
359 global $wgContLang, $wgUser;
361 // If both the set of text revisions and file revisions are empty,
362 // restore everything. Otherwise, just restore the requested items.
363 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
365 $restoreText = $restoreAll ||
!empty( $timestamps );
366 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
368 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
369 $img = wfLocalFile( $this->title
);
370 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
371 if ( !$this->fileStatus
->isOK() ) {
374 $filesRestored = $this->fileStatus
->successCount
;
380 $this->revisionStatus
= $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
381 if( !$this->revisionStatus
->isOK() ) {
385 $textRestored = $this->revisionStatus
->getValue();
392 if( $textRestored && $filesRestored ) {
393 $reason = wfMessage( 'undeletedrevisions-files' )
394 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
395 } elseif( $textRestored ) {
396 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
397 ->inContentLanguage()->text();
398 } elseif( $filesRestored ) {
399 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
400 ->inContentLanguage()->text();
402 wfDebug( "Undelete: nothing undeleted...\n" );
406 if( trim( $comment ) != '' ) {
407 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
410 if ( $user === null ) {
414 $logEntry = new ManualLogEntry( 'delete', 'restore' );
415 $logEntry->setPerformer( $user );
416 $logEntry->setTarget( $this->title
);
417 $logEntry->setComment( $reason );
418 $logid = $logEntry->insert();
419 $logEntry->publish( $logid );
421 return array( $textRestored, $filesRestored, $reason );
425 * This is the meaty bit -- restores archived revisions of the given page
426 * to the cur/old tables. If the page currently exists, all revisions will
427 * be stuffed into old, otherwise the most recent will go into cur.
429 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
430 * @param $comment String
431 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
433 * @return Status, containing the number of revisions restored on success
435 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
436 global $wgContentHandlerNoDB;
438 if ( wfReadOnly() ) {
439 throw new ReadOnlyError();
441 $restoreAll = empty( $timestamps );
443 $dbw = wfGetDB( DB_MASTER
);
445 # Does this page already exist? We'll have to update it...
446 $article = WikiPage
::factory( $this->title
);
447 # Load latest data for the current page (bug 31179)
448 $article->loadPageData( 'fromdbmaster' );
449 $oldcountable = $article->isCountable();
451 $page = $dbw->selectRow( 'page',
452 array( 'page_id', 'page_latest' ),
453 array( 'page_namespace' => $this->title
->getNamespace(),
454 'page_title' => $this->title
->getDBkey() ),
456 array( 'FOR UPDATE' ) // lock page
460 # Page already exists. Import the history, and if necessary
461 # we'll update the latest revision field in the record.
463 $pageId = $page->page_id
;
464 $previousRevId = $page->page_latest
;
465 # Get the time span of this page
466 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
467 array( 'rev_id' => $previousRevId ),
470 if( $previousTimestamp === false ) {
471 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
473 $status = Status
::newGood( 0 );
474 $status->warning( 'undeleterevision-missing' );
479 # Have to create a new article...
482 $previousTimestamp = 0;
486 $oldones = '1 = 1'; # All revisions...
488 $oldts = implode( ',',
489 array_map( array( &$dbw, 'addQuotes' ),
490 array_map( array( &$dbw, 'timestamp' ),
493 $oldones = "ar_timestamp IN ( {$oldts} )";
511 if ( !$wgContentHandlerNoDB ) {
512 $fields[] = 'ar_content_format';
513 $fields[] = 'ar_content_model';
517 * Select each archived revision...
519 $result = $dbw->select( 'archive',
522 'ar_namespace' => $this->title
->getNamespace(),
523 'ar_title' => $this->title
->getDBkey(),
526 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
528 $ret = $dbw->resultObject( $result );
529 $rev_count = $dbw->numRows( $result );
531 wfDebug( __METHOD__
. ": no revisions to restore\n" );
533 $status = Status
::newGood( 0 );
534 $status->warning( "undelete-no-results" );
538 $ret->seek( $rev_count - 1 ); // move to last
539 $row = $ret->fetchObject(); // get newest archived rev
540 $ret->seek( 0 ); // move back
542 // grab the content to check consistency with global state before restoring the page.
543 $revision = Revision
::newFromArchiveRow( $row,
545 'title' => $article->getTitle(), // used to derive default content model
548 $m = $revision->getContentModel();
550 $user = User
::newFromName( $revision->getRawUserText(), false );
551 $content = $revision->getContent( Revision
::RAW
);
553 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
554 $status = $content->prepareSave( $article, 0, -1, $user );
556 if ( !$status->isOK() ) {
561 // Check the state of the newest to-be version...
562 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
563 return Status
::newFatal( "undeleterevdel" );
565 // Safe to insert now...
566 $newid = $article->insertOn( $dbw );
569 // Check if a deleted revision will become the current revision...
570 if( $row->ar_timestamp
> $previousTimestamp ) {
571 // Check the state of the newest to-be version...
572 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
573 return Status
::newFatal( "undeleterevdel" );
581 foreach ( $ret as $row ) {
582 // Check for key dupes due to shitty archive integrity.
583 if( $row->ar_rev_id
) {
584 $exists = $dbw->selectField( 'revision', '1',
585 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
587 continue; // don't throw DB errors
590 // Insert one revision at a time...maintaining deletion status
591 // unless we are specifically removing all restrictions...
592 $revision = Revision
::newFromArchiveRow( $row,
595 'deleted' => $unsuppress ?
0 : $row->ar_deleted
598 $revision->insertOn( $dbw );
601 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
603 # Now that it's safely stored, take it out of the archive
604 $dbw->delete( 'archive',
606 'ar_namespace' => $this->title
->getNamespace(),
607 'ar_title' => $this->title
->getDBkey(),
611 // Was anything restored at all?
612 if ( $restored == 0 ) {
613 return Status
::newGood( 0 );
616 $created = (bool)$newid;
618 // Attach the latest revision to the page...
619 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
620 if ( $created ||
$wasnew ) {
621 // Update site stats, link tables, etc
622 $user = User
::newFromName( $revision->getRawUserText(), false );
623 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
626 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
628 if( $this->title
->getNamespace() == NS_FILE
) {
629 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
633 return Status
::newGood( $restored );
639 function getFileStatus() { return $this->fileStatus
; }
644 function getRevisionStatus() { return $this->revisionStatus
; }
648 * Special page allowing users with the appropriate permissions to view
649 * and restore deleted content.
651 * @ingroup SpecialPage
653 class SpecialUndelete
extends SpecialPage
{
654 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
655 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
662 function __construct() {
663 parent
::__construct( 'Undelete', 'deletedhistory' );
666 function loadRequest( $par ) {
667 $request = $this->getRequest();
668 $user = $this->getUser();
670 $this->mAction
= $request->getVal( 'action' );
671 if ( $par !== null && $par !== '' ) {
672 $this->mTarget
= $par;
674 $this->mTarget
= $request->getVal( 'target' );
676 $this->mTargetObj
= null;
677 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
678 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
680 $this->mSearchPrefix
= $request->getText( 'prefix' );
681 $time = $request->getVal( 'timestamp' );
682 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
683 $this->mFilename
= $request->getVal( 'file' );
685 $posted = $request->wasPosted() &&
686 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
687 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
688 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
689 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
690 $this->mDiff
= $request->getCheck( 'diff' );
691 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
692 $this->mComment
= $request->getText( 'wpComment' );
693 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
694 $this->mToken
= $request->getVal( 'token' );
696 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
697 $this->mAllowed
= true; // user can restore
698 $this->mCanView
= true; // user can view content
699 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
700 $this->mAllowed
= false; // user cannot restore
701 $this->mCanView
= true; // user can view content
702 $this->mRestore
= false;
703 } else { // user can only view the list of revisions
704 $this->mAllowed
= false;
705 $this->mCanView
= false;
706 $this->mTimestamp
= '';
707 $this->mRestore
= false;
710 if( $this->mRestore ||
$this->mInvert
) {
711 $timestamps = array();
712 $this->mFileVersions
= array();
713 foreach( $request->getValues() as $key => $val ) {
715 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
716 array_push( $timestamps, $matches[1] );
719 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
720 $this->mFileVersions
[] = intval( $matches[1] );
723 rsort( $timestamps );
724 $this->mTargetTimestamp
= $timestamps;
728 function execute( $par ) {
729 $this->checkPermissions();
730 $user = $this->getUser();
733 $this->outputHeader();
735 $this->loadRequest( $par );
737 $out = $this->getOutput();
739 if ( is_null( $this->mTargetObj
) ) {
740 $out->addWikiMsg( 'undelete-header' );
742 # Not all users can just browse every deleted page from the list
743 if ( $user->isAllowed( 'browsearchive' ) ) {
744 $this->showSearchForm();
749 if ( $this->mAllowed
) {
750 $out->setPageTitle( $this->msg( 'undeletepage' ) );
752 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
755 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
757 if ( $this->mTimestamp
!== '' ) {
758 $this->showRevision( $this->mTimestamp
);
759 } elseif ( $this->mFilename
!== null ) {
760 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
761 // Check if user is allowed to see this file
762 if ( !$file->exists() ) {
763 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
764 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
765 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
766 throw new PermissionsError( 'suppressrevision' );
768 throw new PermissionsError( 'deletedtext' );
770 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
771 $this->showFileConfirmationForm( $this->mFilename
);
773 $this->showFile( $this->mFilename
);
775 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
778 $this->showHistory();
782 function showSearchForm() {
785 $out = $this->getOutput();
786 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
788 Xml
::openElement( 'form', array(
790 'action' => $wgScript ) ) .
791 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
792 Html
::hidden( 'title',
793 $this->getTitle()->getPrefixedDbKey() ) .
794 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
795 'prefix', 'prefix', 20,
796 $this->mSearchPrefix
) . ' ' .
797 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
798 Xml
::closeElement( 'fieldset' ) .
799 Xml
::closeElement( 'form' )
802 # List undeletable articles
803 if( $this->mSearchPrefix
) {
804 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
805 $this->showList( $result );
810 * Generic list of deleted pages
812 * @param $result ResultWrapper
815 private function showList( $result ) {
816 $out = $this->getOutput();
818 if( $result->numRows() == 0 ) {
819 $out->addWikiMsg( 'undelete-no-results' );
823 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
825 $undelete = $this->getTitle();
826 $out->addHTML( "<ul>\n" );
827 foreach ( $result as $row ) {
828 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
829 if ( $title !== null ) {
830 $item = Linker
::linkKnown(
832 htmlspecialchars( $title->getPrefixedText() ),
834 array( 'target' => $title->getPrefixedText() )
837 // The title is no longer valid, show as text
838 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
839 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
841 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
842 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
845 $out->addHTML( "</ul>\n" );
850 private function showRevision( $timestamp ) {
851 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
855 $archive = new PageArchive( $this->mTargetObj
);
856 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
857 $rev = $archive->getRevision( $timestamp );
859 $out = $this->getOutput();
860 $user = $this->getUser();
863 $out->addWikiMsg( 'undeleterevision-missing' );
867 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
868 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
869 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
872 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
873 $out->addHTML( '<br />' );
874 // and we are allowed to see...
879 $previousRev = $archive->getPreviousRevision( $timestamp );
881 $this->showDiff( $previousRev, $rev );
882 if( $this->mDiffOnly
) {
885 $out->addHTML( '<hr />' );
888 $out->addWikiMsg( 'undelete-nodiff' );
892 $link = Linker
::linkKnown(
893 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
894 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
897 $lang = $this->getLanguage();
899 // date and time are separate parameters to facilitate localisation.
900 // $time is kept for backward compat reasons.
901 $time = $lang->userTimeAndDate( $timestamp, $user );
902 $d = $lang->userDate( $timestamp, $user );
903 $t = $lang->userTime( $timestamp, $user );
904 $userLink = Linker
::revUserTools( $rev );
906 if( $this->mPreview
) {
907 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
909 $openDiv = '<div id="mw-undelete-revision">';
911 $out->addHTML( $openDiv );
913 // Revision delete links
914 if ( !$this->mDiff
) {
915 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
917 $out->addHTML( "$revdel " );
921 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
922 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
923 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
925 if( $this->mPreview
) {
927 //FIXME: ContentHandler will have to provide some specialized magic to do this
928 $popts = $out->parserOptions();
929 $popts->setEditSection( false );
930 $out->parserOptions( $popts );
931 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
934 //FIXME: ContentHandler will have to provide some specialized magic for reviewing content before undeletion
937 Xml
::element( 'textarea', array(
938 'readonly' => 'readonly',
939 'cols' => intval( $user->getOption( 'cols' ) ),
940 'rows' => intval( $user->getOption( 'rows' ) ) ),
941 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) .
942 Xml
::openElement( 'div' ) .
943 Xml
::openElement( 'form', array(
945 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
946 Xml
::element( 'input', array(
949 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
950 Xml
::element( 'input', array(
952 'name' => 'timestamp',
953 'value' => $timestamp ) ) .
954 Xml
::element( 'input', array(
956 'name' => 'wpEditToken',
957 'value' => $user->getEditToken() ) ) .
958 Xml
::element( 'input', array(
961 'value' => $this->msg( 'showpreview' )->text() ) ) .
962 Xml
::element( 'input', array(
965 'value' => $this->msg( 'showdiff' )->text() ) ) .
966 Xml
::closeElement( 'form' ) .
967 Xml
::closeElement( 'div' ) );
971 * Build a diff display between this and the previous either deleted
972 * or non-deleted edit.
974 * @param $previousRev Revision
975 * @param $currentRev Revision
976 * @return String: HTML
978 function showDiff( $previousRev, $currentRev ) {
979 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $this->getContext() );
980 $diffEngine->showDiffStyle();
981 $this->getOutput()->addHTML(
983 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
984 "<col class='diff-marker' />" .
985 "<col class='diff-content' />" .
986 "<col class='diff-marker' />" .
987 "<col class='diff-content' />" .
989 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
990 $this->diffHeader( $previousRev, 'o' ) .
992 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
993 $this->diffHeader( $currentRev, 'n' ) .
996 $diffEngine->generateContentDiffBody(
997 $previousRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ),
998 $currentRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
1005 * @param $rev Revision
1009 private function diffHeader( $rev, $prefix ) {
1010 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1012 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1013 $targetPage = $this->getTitle();
1014 $targetQuery = array(
1015 'target' => $this->mTargetObj
->getPrefixedText(),
1016 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
1019 /// @todo FIXME: getId() may return non-zero for deleted revs...
1020 $targetPage = $rev->getTitle();
1021 $targetQuery = array( 'oldid' => $rev->getId() );
1023 // Add show/hide deletion links if available
1024 $user = $this->getUser();
1025 $lang = $this->getLanguage();
1026 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1027 if ( $rdel ) $rdel = " $rdel";
1029 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1034 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1035 $lang->userDate( $rev->getTimestamp(), $user ),
1036 $lang->userTime( $rev->getTimestamp(), $user )
1042 '<div id="mw-diff-'.$prefix.'title2">' .
1043 Linker
::revUserTools( $rev ) . '<br />' .
1045 '<div id="mw-diff-'.$prefix.'title3">' .
1046 Linker
::revComment( $rev ) . $rdel . '<br />' .
1051 * Show a form confirming whether a tokenless user really wants to see a file
1053 private function showFileConfirmationForm( $key ) {
1054 $out = $this->getOutput();
1055 $lang = $this->getLanguage();
1056 $user = $this->getUser();
1057 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1058 $out->addWikiMsg( 'undelete-show-file-confirm',
1059 $this->mTargetObj
->getText(),
1060 $lang->userDate( $file->getTimestamp(), $user ),
1061 $lang->userTime( $file->getTimestamp(), $user ) );
1063 Xml
::openElement( 'form', array(
1065 'action' => $this->getTitle()->getLocalURL(
1066 'target=' . urlencode( $this->mTarget
) .
1067 '&file=' . urlencode( $key ) .
1068 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1071 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1077 * Show a deleted file version requested by the visitor.
1079 private function showFile( $key ) {
1080 $this->getOutput()->disable();
1082 # We mustn't allow the output to be Squid cached, otherwise
1083 # if an admin previews a deleted image, and it's cached, then
1084 # a user without appropriate permissions can toddle off and
1085 # nab the image, and Squid will serve it
1086 $response = $this->getRequest()->response();
1087 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1088 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1089 $response->header( 'Pragma: no-cache' );
1091 $repo = RepoGroup
::singleton()->getLocalRepo();
1092 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1093 $repo->streamFile( $path );
1096 private function showHistory() {
1097 $out = $this->getOutput();
1098 if( $this->mAllowed
) {
1099 $out->addModules( 'mediawiki.special.undelete' );
1102 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1103 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1106 $archive = new PageArchive( $this->mTargetObj
);
1107 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1109 $text = $archive->getLastRevisionText();
1110 if( is_null( $text ) ) {
1111 $out->addWikiMsg( 'nohistory' );
1115 $out->addHTML( '<div class="mw-undelete-history">' );
1116 if ( $this->mAllowed
) {
1117 $out->addWikiMsg( 'undeletehistory' );
1118 $out->addWikiMsg( 'undeleterevdel' );
1120 $out->addWikiMsg( 'undeletehistorynoadmin' );
1122 $out->addHTML( '</div>' );
1124 # List all stored revisions
1125 $revisions = $archive->listRevisions();
1126 $files = $archive->listFiles();
1128 $haveRevisions = $revisions && $revisions->numRows() > 0;
1129 $haveFiles = $files && $files->numRows() > 0;
1131 # Batch existence check on user and talk pages
1132 if( $haveRevisions ) {
1133 $batch = new LinkBatch();
1134 foreach ( $revisions as $row ) {
1135 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1136 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1139 $revisions->seek( 0 );
1142 $batch = new LinkBatch();
1143 foreach ( $files as $row ) {
1144 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1145 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1151 if ( $this->mAllowed
) {
1152 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1153 # Start the form here
1154 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1155 $out->addHTML( $top );
1158 # Show relevant lines from the deletion log:
1159 $deleteLogPage = new LogPage( 'delete' );
1160 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1161 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1162 # Show relevant lines from the suppression log:
1163 $suppressLogPage = new LogPage( 'suppress' );
1164 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1165 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1166 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1169 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1170 # Format the user-visible controls (comment field, submission button)
1171 # in a nice little table
1172 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1176 <td class='mw-input'>" .
1177 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1178 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1182 $unsuppressBox = '';
1185 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1186 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1188 <td colspan='2' class='mw-undelete-extrahelp'>" .
1189 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1193 <td class='mw-label'>" .
1194 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1196 <td class='mw-input'>" .
1197 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1202 <td class='mw-submit'>" .
1203 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1204 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1208 Xml
::closeElement( 'table' ) .
1209 Xml
::closeElement( 'fieldset' );
1211 $out->addHTML( $table );
1214 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1216 if( $haveRevisions ) {
1217 # The page's stored (deleted) history:
1218 $out->addHTML( '<ul>' );
1219 $remaining = $revisions->numRows();
1220 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1222 foreach ( $revisions as $row ) {
1224 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1227 $out->addHTML( '</ul>' );
1229 $out->addWikiMsg( 'nohistory' );
1233 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1234 $out->addHTML( '<ul>' );
1235 foreach ( $files as $row ) {
1236 $out->addHTML( $this->formatFileRow( $row ) );
1239 $out->addHTML( '</ul>' );
1242 if ( $this->mAllowed
) {
1243 # Slip in the hidden controls here
1244 $misc = Html
::hidden( 'target', $this->mTarget
);
1245 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1246 $misc .= Xml
::closeElement( 'form' );
1247 $out->addHTML( $misc );
1253 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1254 $rev = Revision
::newFromArchiveRow( $row,
1255 array( 'page' => $this->mTargetObj
->getArticleID() ) );
1257 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1258 // Build checkboxen...
1259 if( $this->mAllowed
) {
1260 if( $this->mInvert
) {
1261 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1262 $checkBox = Xml
::check( "ts$ts" );
1264 $checkBox = Xml
::check( "ts$ts", true );
1267 $checkBox = Xml
::check( "ts$ts" );
1272 $user = $this->getUser();
1273 // Build page & diff links...
1274 if( $this->mCanView
) {
1275 $titleObj = $this->getTitle();
1277 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1278 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1279 $last = $this->msg( 'diff' )->escaped();
1280 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1281 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1282 $last = Linker
::linkKnown(
1284 $this->msg( 'diff' )->escaped(),
1287 'target' => $this->mTargetObj
->getPrefixedText(),
1293 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1294 $last = $this->msg( 'diff' )->escaped();
1297 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1298 $last = $this->msg( 'diff' )->escaped();
1301 $userLink = Linker
::revUserTools( $rev );
1302 // Revision text size
1303 $size = $row->ar_len
;
1304 if( !is_null( $size ) ) {
1305 $revTextSize = Linker
::formatRevisionSize( $size );
1308 $comment = Linker
::revComment( $rev );
1309 // Revision delete links
1310 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1312 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1313 return "<li>$revisionRow</li>";
1316 private function formatFileRow( $row ) {
1317 $file = ArchivedFile
::newFromRow( $row );
1319 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1320 $user = $this->getUser();
1321 if( $this->mAllowed
&& $row->fa_storage_key
) {
1322 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1323 $key = urlencode( $row->fa_storage_key
);
1324 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1327 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1329 $userLink = $this->getFileUser( $file );
1330 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1331 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1332 $data = htmlspecialchars( $data . ' ' . $bytes );
1333 $comment = $this->getFileComment( $file );
1335 // Add show/hide deletion links if available
1336 $canHide = $user->isAllowed( 'deleterevision' );
1337 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1338 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1339 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1342 'type' => 'filearchive',
1343 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1344 'ids' => $row->fa_id
1346 $revdlink = Linker
::revDeleteLink( $query,
1347 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1353 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1357 * Fetch revision text link if it's available to all users
1359 * @param $rev Revision
1360 * @param $titleObj Title
1361 * @param $ts string Timestamp
1364 function getPageLink( $rev, $titleObj, $ts ) {
1365 $user = $this->getUser();
1366 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1368 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1369 return '<span class="history-deleted">' . $time . '</span>';
1371 $link = Linker
::linkKnown(
1373 htmlspecialchars( $time ),
1376 'target' => $this->mTargetObj
->getPrefixedText(),
1380 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1381 $link = '<span class="history-deleted">' . $link . '</span>';
1388 * Fetch image view link if it's available to all users
1391 * @param $titleObj Title
1392 * @param $ts string A timestamp
1393 * @param $key String: a storage key
1395 * @return String: HTML fragment
1397 function getFileLink( $file, $titleObj, $ts, $key ) {
1398 $user = $this->getUser();
1399 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1401 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1402 return '<span class="history-deleted">' . $time . '</span>';
1404 $link = Linker
::linkKnown(
1406 htmlspecialchars( $time ),
1409 'target' => $this->mTargetObj
->getPrefixedText(),
1411 'token' => $user->getEditToken( $key )
1414 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1415 $link = '<span class="history-deleted">' . $link . '</span>';
1422 * Fetch file's user id if it's available to this user
1425 * @return String: HTML fragment
1427 function getFileUser( $file ) {
1428 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1429 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1431 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1432 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1433 if( $file->isDeleted( File
::DELETED_USER
) ) {
1434 $link = '<span class="history-deleted">' . $link . '</span>';
1441 * Fetch file upload comment if it's available to this user
1444 * @return String: HTML fragment
1446 function getFileComment( $file ) {
1447 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1448 return '<span class="history-deleted"><span class="comment">' .
1449 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1451 $link = Linker
::commentBlock( $file->getRawDescription() );
1452 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1453 $link = '<span class="history-deleted">' . $link . '</span>';
1459 function undelete() {
1460 global $wgUploadMaintenance;
1462 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1463 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1466 if ( wfReadOnly() ) {
1467 throw new ReadOnlyError
;
1470 $out = $this->getOutput();
1471 $archive = new PageArchive( $this->mTargetObj
);
1472 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1473 $ok = $archive->undelete(
1474 $this->mTargetTimestamp
,
1476 $this->mFileVersions
,
1481 if( is_array( $ok ) ) {
1482 if ( $ok[1] ) { // Undeleted file count
1483 wfRunHooks( 'FileUndeleteComplete', array(
1484 $this->mTargetObj
, $this->mFileVersions
,
1485 $this->getUser(), $this->mComment
) );
1488 $link = Linker
::linkKnown( $this->mTargetObj
);
1489 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1491 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1494 // Show revision undeletion warnings and errors
1495 $status = $archive->getRevisionStatus();
1496 if( $status && !$status->isGood() ) {
1497 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'cannotundelete', 'cannotundelete' ) . '</div>' );
1500 // Show file undeletion warnings and errors
1501 $status = $archive->getFileStatus();
1502 if( $status && !$status->isGood() ) {
1503 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );