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
38 protected $fileStatus;
43 protected $revisionStatus;
45 function __construct( $title ) {
46 if ( is_null( $title ) ) {
47 throw new MWException( __METHOD__
. ' given a null title.' );
49 $this->title
= $title;
53 * List all deleted pages recorded in the archive table. Returns result
54 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
57 * @return ResultWrapper
59 public static function listAllPages() {
60 $dbr = wfGetDB( DB_SLAVE
);
61 return self
::listPages( $dbr, '' );
65 * List deleted pages recorded in the archive table matching the
67 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
69 * @param string $prefix Title prefix
70 * @return ResultWrapper
72 public static function listPagesByPrefix( $prefix ) {
73 $dbr = wfGetDB( DB_SLAVE
);
75 $title = Title
::newFromText( $prefix );
77 $ns = $title->getNamespace();
78 $prefix = $title->getDBkey();
80 // Prolly won't work too good
81 // @todo handle bare namespace names cleanly?
86 'ar_namespace' => $ns,
87 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
90 return self
::listPages( $dbr, $conds );
94 * @param DatabaseBase $dbr
95 * @param string|array $condition
96 * @return bool|ResultWrapper
98 protected static function listPages( $dbr, $condition ) {
99 return $dbr->resultObject( $dbr->select(
104 'count' => 'COUNT(*)'
109 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
110 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
117 * List the revisions of the given page. Returns result wrapper with
118 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
120 * @return ResultWrapper
122 function listRevisions() {
123 global $wgContentHandlerUseDB;
125 $dbr = wfGetDB( DB_SLAVE
);
128 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
129 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
132 if ( $wgContentHandlerUseDB ) {
133 $fields[] = 'ar_content_format';
134 $fields[] = 'ar_content_model';
137 $res = $dbr->select( 'archive',
139 array( 'ar_namespace' => $this->title
->getNamespace(),
140 'ar_title' => $this->title
->getDBkey() ),
142 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
144 return $dbr->resultObject( $res );
148 * List the deleted file revisions for this page, if it's a file page.
149 * Returns a result wrapper with various filearchive fields, or null
150 * if not a file page.
152 * @return ResultWrapper
153 * @todo Does this belong in Image for fuller encapsulation?
155 function listFiles() {
156 if ( $this->title
->getNamespace() != NS_FILE
) {
160 $dbr = wfGetDB( DB_SLAVE
);
163 ArchivedFile
::selectFields(),
164 array( 'fa_name' => $this->title
->getDBkey() ),
166 array( 'ORDER BY' => 'fa_timestamp DESC' )
169 return $dbr->resultObject( $res );
173 * Return a Revision object containing data for the deleted revision.
174 * Note that the result *may* or *may not* have a null page ID.
176 * @param string $timestamp
177 * @return Revision|null
179 function getRevision( $timestamp ) {
180 global $wgContentHandlerUseDB;
182 $dbr = wfGetDB( DB_SLAVE
);
199 if ( $wgContentHandlerUseDB ) {
200 $fields[] = 'ar_content_format';
201 $fields[] = 'ar_content_model';
204 $row = $dbr->selectRow( 'archive',
206 array( 'ar_namespace' => $this->title
->getNamespace(),
207 'ar_title' => $this->title
->getDBkey(),
208 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
212 return Revision
::newFromArchiveRow( $row, array( 'title' => $this->title
) );
219 * Return the most-previous revision, either live or deleted, against
220 * the deleted revision given by timestamp.
222 * May produce unexpected results in case of history merges or other
223 * unusual time issues.
225 * @param string $timestamp
226 * @return Revision|null Null when there is no previous revision
228 function getPreviousRevision( $timestamp ) {
229 $dbr = wfGetDB( DB_SLAVE
);
231 // Check the previous deleted revision...
232 $row = $dbr->selectRow( 'archive',
234 array( 'ar_namespace' => $this->title
->getNamespace(),
235 'ar_title' => $this->title
->getDBkey(),
237 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
240 'ORDER BY' => 'ar_timestamp DESC',
242 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
244 $row = $dbr->selectRow( array( 'page', 'revision' ),
245 array( 'rev_id', 'rev_timestamp' ),
247 'page_namespace' => $this->title
->getNamespace(),
248 'page_title' => $this->title
->getDBkey(),
249 'page_id = rev_page',
251 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
254 'ORDER BY' => 'rev_timestamp DESC',
256 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
257 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
259 if ( $prevLive && $prevLive > $prevDeleted ) {
260 // Most prior revision was live
261 return Revision
::newFromId( $prevLiveId );
262 } elseif ( $prevDeleted ) {
263 // Most prior revision was deleted
264 return $this->getRevision( $prevDeleted );
267 // No prior revision on this page.
272 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
274 * @param object $row Database row
277 function getTextFromRow( $row ) {
278 if ( is_null( $row->ar_text_id
) ) {
279 // An old row from MediaWiki 1.4 or previous.
280 // Text is embedded in this row in classic compression format.
281 return Revision
::getRevisionText( $row, 'ar_' );
284 // New-style: keyed to the text storage backend.
285 $dbr = wfGetDB( DB_SLAVE
);
286 $text = $dbr->selectRow( 'text',
287 array( 'old_text', 'old_flags' ),
288 array( 'old_id' => $row->ar_text_id
),
291 return Revision
::getRevisionText( $text );
295 * Fetch (and decompress if necessary) the stored text of the most
296 * recently edited deleted revision of the page.
298 * If there are no archived revisions for the page, returns NULL.
300 * @return string|null
302 function getLastRevisionText() {
303 $dbr = wfGetDB( DB_SLAVE
);
304 $row = $dbr->selectRow( 'archive',
305 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
306 array( 'ar_namespace' => $this->title
->getNamespace(),
307 'ar_title' => $this->title
->getDBkey() ),
309 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
312 return $this->getTextFromRow( $row );
319 * Quick check if any archived revisions are present for the page.
323 function isDeleted() {
324 $dbr = wfGetDB( DB_SLAVE
);
325 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
326 array( 'ar_namespace' => $this->title
->getNamespace(),
327 'ar_title' => $this->title
->getDBkey() ),
335 * Restore the given (or all) text and file revisions for the page.
336 * Once restored, the items will be removed from the archive tables.
337 * The deletion log will be updated with an undeletion notice.
339 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
340 * @param string $comment
341 * @param array $fileVersions
342 * @param bool $unsuppress
343 * @param User $user User performing the action, or null to use $wgUser
345 * @return array(number of file revisions restored, number of image revisions restored, log message)
346 * on success, false on failure
348 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
349 // If both the set of text revisions and file revisions are empty,
350 // restore everything. Otherwise, just restore the requested items.
351 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
353 $restoreText = $restoreAll ||
!empty( $timestamps );
354 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
356 if ( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
357 $img = wfLocalFile( $this->title
);
358 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
359 if ( !$this->fileStatus
->isOK() ) {
362 $filesRestored = $this->fileStatus
->successCount
;
367 if ( $restoreText ) {
368 $this->revisionStatus
= $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
369 if ( !$this->revisionStatus
->isOK() ) {
373 $textRestored = $this->revisionStatus
->getValue();
380 if ( $textRestored && $filesRestored ) {
381 $reason = wfMessage( 'undeletedrevisions-files' )
382 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
383 } elseif ( $textRestored ) {
384 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
385 ->inContentLanguage()->text();
386 } elseif ( $filesRestored ) {
387 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
388 ->inContentLanguage()->text();
390 wfDebug( "Undelete: nothing undeleted...\n" );
394 if ( trim( $comment ) != '' ) {
395 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
398 if ( $user === null ) {
403 $logEntry = new ManualLogEntry( 'delete', 'restore' );
404 $logEntry->setPerformer( $user );
405 $logEntry->setTarget( $this->title
);
406 $logEntry->setComment( $reason );
408 wfRunHooks( 'ArticleUndeleteLogEntry', array( $this, &$logEntry, $user ) );
410 $logid = $logEntry->insert();
411 $logEntry->publish( $logid );
413 return array( $textRestored, $filesRestored, $reason );
417 * This is the meaty bit -- restores archived revisions of the given page
418 * to the cur/old tables. If the page currently exists, all revisions will
419 * be stuffed into old, otherwise the most recent will go into cur.
421 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
422 * @param bool $unsuppress Remove all ar_deleted/fa_deleted restrictions of seletected revs
423 * @param string $comment
424 * @throws ReadOnlyError
425 * @return Status Object containing the number of revisions restored on success
427 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
428 global $wgContentHandlerUseDB;
430 if ( wfReadOnly() ) {
431 throw new ReadOnlyError();
434 $restoreAll = empty( $timestamps );
435 $dbw = wfGetDB( DB_MASTER
);
437 # Does this page already exist? We'll have to update it...
438 $article = WikiPage
::factory( $this->title
);
439 # Load latest data for the current page (bug 31179)
440 $article->loadPageData( 'fromdbmaster' );
441 $oldcountable = $article->isCountable();
443 $page = $dbw->selectRow( 'page',
444 array( 'page_id', 'page_latest' ),
445 array( 'page_namespace' => $this->title
->getNamespace(),
446 'page_title' => $this->title
->getDBkey() ),
448 array( 'FOR UPDATE' ) // lock page
453 # Page already exists. Import the history, and if necessary
454 # we'll update the latest revision field in the record.
456 $previousRevId = $page->page_latest
;
458 # Get the time span of this page
459 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
460 array( 'rev_id' => $previousRevId ),
463 if ( $previousTimestamp === false ) {
464 wfDebug( __METHOD__
. ": existing page refers to a page_latest that does not exist\n" );
466 $status = Status
::newGood( 0 );
467 $status->warning( 'undeleterevision-missing' );
472 # Have to create a new article...
475 $previousTimestamp = 0;
479 $oldones = '1 = 1'; # All revisions...
481 $oldts = implode( ',',
482 array_map( array( &$dbw, 'addQuotes' ),
483 array_map( array( &$dbw, 'timestamp' ),
486 $oldones = "ar_timestamp IN ( {$oldts} )";
505 if ( $wgContentHandlerUseDB ) {
506 $fields[] = 'ar_content_format';
507 $fields[] = 'ar_content_model';
511 * Select each archived revision...
513 $result = $dbw->select( 'archive',
516 'ar_namespace' => $this->title
->getNamespace(),
517 'ar_title' => $this->title
->getDBkey(),
520 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
522 $ret = $dbw->resultObject( $result );
523 $rev_count = $dbw->numRows( $result );
526 wfDebug( __METHOD__
. ": no revisions to restore\n" );
528 $status = Status
::newGood( 0 );
529 $status->warning( "undelete-no-results" );
533 $ret->seek( $rev_count - 1 ); // move to last
534 $row = $ret->fetchObject(); // get newest archived rev
535 $ret->seek( 0 ); // move back
537 // grab the content to check consistency with global state before restoring the page.
538 $revision = Revision
::newFromArchiveRow( $row,
540 'title' => $article->getTitle(), // used to derive default content model
543 $user = User
::newFromName( $revision->getRawUserText(), false );
544 $content = $revision->getContent( Revision
::RAW
);
546 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
547 $status = $content->prepareSave( $article, 0, -1, $user );
549 if ( !$status->isOK() ) {
554 // Check the state of the newest to-be version...
555 if ( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
556 return Status
::newFatal( "undeleterevdel" );
558 // Safe to insert now...
559 $newid = $article->insertOn( $dbw );
562 // Check if a deleted revision will become the current revision...
563 if ( $row->ar_timestamp
> $previousTimestamp ) {
564 // Check the state of the newest to-be version...
565 if ( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
566 return Status
::newFatal( "undeleterevdel" );
571 $pageId = $article->getId();
577 foreach ( $ret as $row ) {
578 // Check for key dupes due to shitty archive integrity.
579 if ( $row->ar_rev_id
) {
580 $exists = $dbw->selectField( 'revision', '1',
581 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
583 continue; // don't throw DB errors
586 // Insert one revision at a time...maintaining deletion status
587 // unless we are specifically removing all restrictions...
588 $revision = Revision
::newFromArchiveRow( $row,
591 'title' => $this->title
,
592 'deleted' => $unsuppress ?
0 : $row->ar_deleted
595 $revision->insertOn( $dbw );
598 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
600 # Now that it's safely stored, take it out of the archive
601 $dbw->delete( 'archive',
603 'ar_namespace' => $this->title
->getNamespace(),
604 'ar_title' => $this->title
->getDBkey(),
608 // Was anything restored at all?
609 if ( $restored == 0 ) {
610 return Status
::newGood( 0 );
613 $created = (bool)$newid;
615 // Attach the latest revision to the page...
616 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
617 if ( $created ||
$wasnew ) {
618 // Update site stats, link tables, etc
619 $user = User
::newFromName( $revision->getRawUserText(), false );
620 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
623 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
625 if ( $this->title
->getNamespace() == NS_FILE
) {
626 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
630 return Status
::newGood( $restored );
636 function getFileStatus() {
637 return $this->fileStatus
;
643 function getRevisionStatus() {
644 return $this->revisionStatus
;
649 * Special page allowing users with the appropriate permissions to view
650 * and restore deleted content.
652 * @ingroup SpecialPage
654 class SpecialUndelete
extends SpecialPage
{
655 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
656 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
663 function __construct() {
664 parent
::__construct( 'Undelete', 'deletedhistory' );
667 function loadRequest( $par ) {
668 $request = $this->getRequest();
669 $user = $this->getUser();
671 $this->mAction
= $request->getVal( 'action' );
672 if ( $par !== null && $par !== '' ) {
673 $this->mTarget
= $par;
675 $this->mTarget
= $request->getVal( 'target' );
678 $this->mTargetObj
= null;
680 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
681 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
684 $this->mSearchPrefix
= $request->getText( 'prefix' );
685 $time = $request->getVal( 'timestamp' );
686 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
687 $this->mFilename
= $request->getVal( 'file' );
689 $posted = $request->wasPosted() &&
690 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
691 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
692 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
693 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
694 $this->mDiff
= $request->getCheck( 'diff' );
695 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
696 $this->mComment
= $request->getText( 'wpComment' );
697 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
698 $this->mToken
= $request->getVal( 'token' );
700 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
701 $this->mAllowed
= true; // user can restore
702 $this->mCanView
= true; // user can view content
703 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
704 $this->mAllowed
= false; // user cannot restore
705 $this->mCanView
= true; // user can view content
706 $this->mRestore
= false;
707 } else { // user can only view the list of revisions
708 $this->mAllowed
= false;
709 $this->mCanView
= false;
710 $this->mTimestamp
= '';
711 $this->mRestore
= false;
714 if ( $this->mRestore ||
$this->mInvert
) {
715 $timestamps = array();
716 $this->mFileVersions
= array();
717 foreach ( $request->getValues() as $key => $val ) {
719 if ( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
720 array_push( $timestamps, $matches[1] );
723 if ( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
724 $this->mFileVersions
[] = intval( $matches[1] );
727 rsort( $timestamps );
728 $this->mTargetTimestamp
= $timestamps;
732 function execute( $par ) {
733 $this->checkPermissions();
734 $user = $this->getUser();
737 $this->outputHeader();
739 $this->loadRequest( $par );
741 $out = $this->getOutput();
743 if ( is_null( $this->mTargetObj
) ) {
744 $out->addWikiMsg( 'undelete-header' );
746 # Not all users can just browse every deleted page from the list
747 if ( $user->isAllowed( 'browsearchive' ) ) {
748 $this->showSearchForm();
753 if ( $this->mAllowed
) {
754 $out->setPageTitle( $this->msg( 'undeletepage' ) );
756 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
759 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
761 if ( $this->mTimestamp
!== '' ) {
762 $this->showRevision( $this->mTimestamp
);
763 } elseif ( $this->mFilename
!== null && $this->mTargetObj
->inNamespace( NS_FILE
) ) {
764 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
765 // Check if user is allowed to see this file
766 if ( !$file->exists() ) {
767 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
768 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
769 if ( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
770 throw new PermissionsError( 'suppressrevision' );
772 throw new PermissionsError( 'deletedtext' );
774 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
775 $this->showFileConfirmationForm( $this->mFilename
);
777 $this->showFile( $this->mFilename
);
779 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
782 $this->showHistory();
786 function showSearchForm() {
789 $out = $this->getOutput();
790 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
792 Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
793 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
794 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
796 $this->msg( 'undelete-search-prefix' )->text(),
800 $this->mSearchPrefix
,
801 array( 'autofocus' => true )
803 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
804 Xml
::closeElement( 'fieldset' ) .
805 Xml
::closeElement( 'form' )
808 # List undeletable articles
809 if ( $this->mSearchPrefix
) {
810 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
811 $this->showList( $result );
816 * Generic list of deleted pages
818 * @param ResultWrapper $result
821 private function showList( $result ) {
822 $out = $this->getOutput();
824 if ( $result->numRows() == 0 ) {
825 $out->addWikiMsg( 'undelete-no-results' );
829 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
831 $undelete = $this->getTitle();
832 $out->addHTML( "<ul>\n" );
833 foreach ( $result as $row ) {
834 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
835 if ( $title !== null ) {
836 $item = Linker
::linkKnown(
838 htmlspecialchars( $title->getPrefixedText() ),
840 array( 'target' => $title->getPrefixedText() )
843 // The title is no longer valid, show as text
844 $item = Html
::element(
846 array( 'class' => 'mw-invalidtitle' ),
847 Linker
::getInvalidTitleDescription(
854 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
855 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
858 $out->addHTML( "</ul>\n" );
863 private function showRevision( $timestamp ) {
864 if ( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
868 $archive = new PageArchive( $this->mTargetObj
);
869 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) ) ) {
872 $rev = $archive->getRevision( $timestamp );
874 $out = $this->getOutput();
875 $user = $this->getUser();
878 $out->addWikiMsg( 'undeleterevision-missing' );
882 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
883 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
885 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
886 'rev-deleted-text-permission'
892 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
893 'rev-deleted-text-view'
895 $out->addHTML( '<br />' );
896 // and we are allowed to see...
899 if ( $this->mDiff
) {
900 $previousRev = $archive->getPreviousRevision( $timestamp );
901 if ( $previousRev ) {
902 $this->showDiff( $previousRev, $rev );
903 if ( $this->mDiffOnly
) {
907 $out->addHTML( '<hr />' );
909 $out->addWikiMsg( 'undelete-nodiff' );
913 $link = Linker
::linkKnown(
914 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
915 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
918 $lang = $this->getLanguage();
920 // date and time are separate parameters to facilitate localisation.
921 // $time is kept for backward compat reasons.
922 $time = $lang->userTimeAndDate( $timestamp, $user );
923 $d = $lang->userDate( $timestamp, $user );
924 $t = $lang->userTime( $timestamp, $user );
925 $userLink = Linker
::revUserTools( $rev );
927 $content = $rev->getContent( Revision
::FOR_THIS_USER
, $user );
929 $isText = ( $content instanceof TextContent
);
931 if ( $this->mPreview ||
$isText ) {
932 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
934 $openDiv = '<div id="mw-undelete-revision">';
936 $out->addHTML( $openDiv );
938 // Revision delete links
939 if ( !$this->mDiff
) {
940 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
942 $out->addHTML( "$revdel " );
946 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
947 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
949 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) ) ) {
953 if ( $this->mPreview ||
!$isText ) {
954 // NOTE: non-text content has no source view, so always use rendered preview
957 $popts = $out->parserOptions();
958 $popts->setEditSection( false );
960 $pout = $content->getParserOutput( $this->mTargetObj
, $rev->getId(), $popts, true );
961 $out->addParserOutput( $pout );
965 // source view for textual content
966 $sourceView = Xml
::element(
969 'readonly' => 'readonly',
970 'cols' => $user->getIntOption( 'cols' ),
971 'rows' => $user->getIntOption( 'rows' )
973 $content->getNativeData() . "\n"
976 $previewButton = Xml
::element( 'input', array(
979 'value' => $this->msg( 'showpreview' )->text()
986 $diffButton = Xml
::element( 'input', array(
989 'value' => $this->msg( 'showdiff' )->text() ) );
993 Xml
::openElement( 'div', array(
994 'style' => 'clear: both' ) ) .
995 Xml
::openElement( 'form', array(
997 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
998 Xml
::element( 'input', array(
1001 'value' => $this->mTargetObj
->getPrefixedDBkey() ) ) .
1002 Xml
::element( 'input', array(
1004 'name' => 'timestamp',
1005 'value' => $timestamp ) ) .
1006 Xml
::element( 'input', array(
1008 'name' => 'wpEditToken',
1009 'value' => $user->getEditToken() ) ) .
1012 Xml
::closeElement( 'form' ) .
1013 Xml
::closeElement( 'div' )
1018 * Build a diff display between this and the previous either deleted
1019 * or non-deleted edit.
1021 * @param Revision $previousRev
1022 * @param Revision $currentRev
1023 * @return string HTML
1025 function showDiff( $previousRev, $currentRev ) {
1026 $diffContext = clone $this->getContext();
1027 $diffContext->setTitle( $currentRev->getTitle() );
1028 $diffContext->setWikiPage( WikiPage
::factory( $currentRev->getTitle() ) );
1030 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1031 $diffEngine->showDiffStyle();
1032 $this->getOutput()->addHTML( "<div>" .
1033 "<table style='width: 98%;' cellpadding='0' cellspacing='4' class='diff'>" .
1034 "<col class='diff-marker' />" .
1035 "<col class='diff-content' />" .
1036 "<col class='diff-marker' />" .
1037 "<col class='diff-content' />" .
1039 "<td colspan='2' style='width: 50%; text-align: center' class='diff-otitle'>" .
1040 $this->diffHeader( $previousRev, 'o' ) .
1042 "<td colspan='2' style='width: 50%; text-align: center' class='diff-ntitle'>" .
1043 $this->diffHeader( $currentRev, 'n' ) .
1046 $diffEngine->generateContentDiffBody(
1047 $previousRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ),
1048 $currentRev->getContent( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
1055 * @param Revision $rev
1056 * @param string $prefix
1059 private function diffHeader( $rev, $prefix ) {
1060 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1062 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1063 $targetPage = $this->getTitle();
1064 $targetQuery = array(
1065 'target' => $this->mTargetObj
->getPrefixedText(),
1066 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
1069 /// @todo FIXME: getId() may return non-zero for deleted revs...
1070 $targetPage = $rev->getTitle();
1071 $targetQuery = array( 'oldid' => $rev->getId() );
1074 // Add show/hide deletion links if available
1075 $user = $this->getUser();
1076 $lang = $this->getLanguage();
1077 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1083 return '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1088 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1089 $lang->userDate( $rev->getTimestamp(), $user ),
1090 $lang->userTime( $rev->getTimestamp(), $user )
1096 '<div id="mw-diff-' . $prefix . 'title2">' .
1097 Linker
::revUserTools( $rev ) . '<br />' .
1099 '<div id="mw-diff-' . $prefix . 'title3">' .
1100 Linker
::revComment( $rev ) . $rdel . '<br />' .
1105 * Show a form confirming whether a tokenless user really wants to see a file
1107 private function showFileConfirmationForm( $key ) {
1108 $out = $this->getOutput();
1109 $lang = $this->getLanguage();
1110 $user = $this->getUser();
1111 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1112 $out->addWikiMsg( 'undelete-show-file-confirm',
1113 $this->mTargetObj
->getText(),
1114 $lang->userDate( $file->getTimestamp(), $user ),
1115 $lang->userTime( $file->getTimestamp(), $user ) );
1117 Xml
::openElement( 'form', array(
1119 'action' => $this->getTitle()->getLocalURL( array(
1120 'target' => $this->mTarget
,
1122 'token' => $user->getEditToken( $key ),
1126 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1132 * Show a deleted file version requested by the visitor.
1134 private function showFile( $key ) {
1135 $this->getOutput()->disable();
1137 # We mustn't allow the output to be Squid cached, otherwise
1138 # if an admin previews a deleted image, and it's cached, then
1139 # a user without appropriate permissions can toddle off and
1140 # nab the image, and Squid will serve it
1141 $response = $this->getRequest()->response();
1142 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1143 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1144 $response->header( 'Pragma: no-cache' );
1146 $repo = RepoGroup
::singleton()->getLocalRepo();
1147 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1148 $repo->streamFile( $path );
1151 private function showHistory() {
1152 $out = $this->getOutput();
1153 if ( $this->mAllowed
) {
1154 $out->addModules( 'mediawiki.special.undelete' );
1157 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1158 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1161 $archive = new PageArchive( $this->mTargetObj
);
1162 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1164 $text = $archive->getLastRevisionText();
1165 if( is_null( $text ) ) {
1166 $out->addWikiMsg( 'nohistory' );
1170 $out->addHTML( '<div class="mw-undelete-history">' );
1171 if ( $this->mAllowed
) {
1172 $out->addWikiMsg( 'undeletehistory' );
1173 $out->addWikiMsg( 'undeleterevdel' );
1175 $out->addWikiMsg( 'undeletehistorynoadmin' );
1177 $out->addHTML( '</div>' );
1179 # List all stored revisions
1180 $revisions = $archive->listRevisions();
1181 $files = $archive->listFiles();
1183 $haveRevisions = $revisions && $revisions->numRows() > 0;
1184 $haveFiles = $files && $files->numRows() > 0;
1186 # Batch existence check on user and talk pages
1187 if ( $haveRevisions ) {
1188 $batch = new LinkBatch();
1189 foreach ( $revisions as $row ) {
1190 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1191 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1194 $revisions->seek( 0 );
1197 $batch = new LinkBatch();
1198 foreach ( $files as $row ) {
1199 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1200 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1206 if ( $this->mAllowed
) {
1207 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1208 # Start the form here
1209 $top = Xml
::openElement(
1211 array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' )
1213 $out->addHTML( $top );
1216 # Show relevant lines from the deletion log:
1217 $deleteLogPage = new LogPage( 'delete' );
1218 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1219 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1220 # Show relevant lines from the suppression log:
1221 $suppressLogPage = new LogPage( 'suppress' );
1222 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1223 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1224 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1227 if ( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1228 # Format the user-visible controls (comment field, submission button)
1229 # in a nice little table
1230 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1234 <td class='mw-input'>" .
1235 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1236 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
) .
1240 $unsuppressBox = '';
1244 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1245 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1247 <td colspan='2' class='mw-undelete-extrahelp'>" .
1248 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1252 <td class='mw-label'>" .
1253 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1255 <td class='mw-input'>" .
1256 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment', 'autofocus' => true ) ) .
1261 <td class='mw-submit'>" .
1262 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1263 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1267 Xml
::closeElement( 'table' ) .
1268 Xml
::closeElement( 'fieldset' );
1270 $out->addHTML( $table );
1273 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1275 if ( $haveRevisions ) {
1276 # The page's stored (deleted) history:
1277 $out->addHTML( '<ul>' );
1278 $remaining = $revisions->numRows();
1279 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1281 foreach ( $revisions as $row ) {
1283 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1286 $out->addHTML( '</ul>' );
1288 $out->addWikiMsg( 'nohistory' );
1292 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1293 $out->addHTML( '<ul>' );
1294 foreach ( $files as $row ) {
1295 $out->addHTML( $this->formatFileRow( $row ) );
1298 $out->addHTML( '</ul>' );
1301 if ( $this->mAllowed
) {
1302 # Slip in the hidden controls here
1303 $misc = Html
::hidden( 'target', $this->mTarget
);
1304 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1305 $misc .= Xml
::closeElement( 'form' );
1306 $out->addHTML( $misc );
1312 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1313 $rev = Revision
::newFromArchiveRow( $row,
1315 'title' => $this->mTargetObj
1319 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1320 // Build checkboxen...
1321 if ( $this->mAllowed
) {
1322 if ( $this->mInvert
) {
1323 if ( in_array( $ts, $this->mTargetTimestamp
) ) {
1324 $checkBox = Xml
::check( "ts$ts" );
1326 $checkBox = Xml
::check( "ts$ts", true );
1329 $checkBox = Xml
::check( "ts$ts" );
1335 // Build page & diff links...
1336 $user = $this->getUser();
1337 if ( $this->mCanView
) {
1338 $titleObj = $this->getTitle();
1340 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1341 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1342 $last = $this->msg( 'diff' )->escaped();
1343 } elseif ( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1344 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1345 $last = Linker
::linkKnown(
1347 $this->msg( 'diff' )->escaped(),
1350 'target' => $this->mTargetObj
->getPrefixedText(),
1356 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1357 $last = $this->msg( 'diff' )->escaped();
1360 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1361 $last = $this->msg( 'diff' )->escaped();
1365 $userLink = Linker
::revUserTools( $rev );
1367 // Revision text size
1368 $size = $row->ar_len
;
1369 if ( !is_null( $size ) ) {
1370 $revTextSize = Linker
::formatRevisionSize( $size );
1374 $comment = Linker
::revComment( $rev );
1376 // Revision delete links
1377 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1379 $revisionRow = $this->msg( 'undelete-revisionrow' )
1380 ->rawParams( $checkBox, $revdlink, $last, $pageLink, $userLink, $revTextSize, $comment )
1383 return "<li>$revisionRow</li>";
1386 private function formatFileRow( $row ) {
1387 $file = ArchivedFile
::newFromRow( $row );
1388 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1389 $user = $this->getUser();
1391 if ( $this->mAllowed
&& $row->fa_storage_key
) {
1392 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1393 $key = urlencode( $row->fa_storage_key
);
1394 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1397 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1399 $userLink = $this->getFileUser( $file );
1400 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1401 $bytes = $this->msg( 'parentheses' )
1402 ->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )
1404 $data = htmlspecialchars( $data . ' ' . $bytes );
1405 $comment = $this->getFileComment( $file );
1407 // Add show/hide deletion links if available
1408 $canHide = $user->isAllowed( 'deleterevision' );
1409 if ( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1410 if ( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1411 // Revision was hidden from sysops
1412 $revdlink = Linker
::revDeleteLinkDisabled( $canHide );
1415 'type' => 'filearchive',
1416 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1417 'ids' => $row->fa_id
1419 $revdlink = Linker
::revDeleteLink( $query,
1420 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1426 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1430 * Fetch revision text link if it's available to all users
1432 * @param Revision $rev
1433 * @param Title $titleObj
1434 * @param string $ts Timestamp
1437 function getPageLink( $rev, $titleObj, $ts ) {
1438 $user = $this->getUser();
1439 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1441 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1442 return '<span class="history-deleted">' . $time . '</span>';
1445 $link = Linker
::linkKnown(
1447 htmlspecialchars( $time ),
1450 'target' => $this->mTargetObj
->getPrefixedText(),
1455 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1456 $link = '<span class="history-deleted">' . $link . '</span>';
1463 * Fetch image view link if it's available to all users
1465 * @param File|ArchivedFile $file
1466 * @param Title $titleObj
1467 * @param string $ts A timestamp
1468 * @param string $key a storage key
1470 * @return string HTML fragment
1472 function getFileLink( $file, $titleObj, $ts, $key ) {
1473 $user = $this->getUser();
1474 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1476 if ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1477 return '<span class="history-deleted">' . $time . '</span>';
1480 $link = Linker
::linkKnown(
1482 htmlspecialchars( $time ),
1485 'target' => $this->mTargetObj
->getPrefixedText(),
1487 'token' => $user->getEditToken( $key )
1491 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1492 $link = '<span class="history-deleted">' . $link . '</span>';
1499 * Fetch file's user id if it's available to this user
1501 * @param File|ArchivedFile $file
1502 * @return string HTML fragment
1504 function getFileUser( $file ) {
1505 if ( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1506 return '<span class="history-deleted">' .
1507 $this->msg( 'rev-deleted-user' )->escaped() .
1511 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1512 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1514 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1515 $link = '<span class="history-deleted">' . $link . '</span>';
1522 * Fetch file upload comment if it's available to this user
1524 * @param File|ArchivedFile $file
1525 * @return string HTML fragment
1527 function getFileComment( $file ) {
1528 if ( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1529 return '<span class="history-deleted"><span class="comment">' .
1530 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1533 $link = Linker
::commentBlock( $file->getRawDescription() );
1535 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1536 $link = '<span class="history-deleted">' . $link . '</span>';
1542 function undelete() {
1543 global $wgUploadMaintenance;
1545 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1546 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1549 if ( wfReadOnly() ) {
1550 throw new ReadOnlyError
;
1553 $out = $this->getOutput();
1554 $archive = new PageArchive( $this->mTargetObj
);
1555 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1556 $ok = $archive->undelete(
1557 $this->mTargetTimestamp
,
1559 $this->mFileVersions
,
1564 if ( is_array( $ok ) ) {
1565 if ( $ok[1] ) { // Undeleted file count
1566 wfRunHooks( 'FileUndeleteComplete', array(
1567 $this->mTargetObj
, $this->mFileVersions
,
1568 $this->getUser(), $this->mComment
) );
1571 $link = Linker
::linkKnown( $this->mTargetObj
);
1572 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1574 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1577 // Show revision undeletion warnings and errors
1578 $status = $archive->getRevisionStatus();
1579 if ( $status && !$status->isGood() ) {
1580 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'cannotundelete', 'cannotundelete' ) . '</div>' );
1583 // Show file undeletion warnings and errors
1584 $status = $archive->getFileStatus();
1585 if ( $status && !$status->isGood() ) {
1586 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );
1590 protected function getGroupName() {