d8e0b97c456112643e665d5279f3f6b5d2808660
3 * Implements Special:Undelete
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Used to show archived pages and eventually restore them.
27 * @ingroup SpecialPage
37 function __construct( $title ) {
38 if( is_null( $title ) ) {
39 throw new MWException( __METHOD__
. ' given a null title.' );
41 $this->title
= $title;
45 * List all deleted pages recorded in the archive table. Returns result
46 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
49 * @return ResultWrapper
51 public static function listAllPages() {
52 $dbr = wfGetDB( DB_SLAVE
);
53 return self
::listPages( $dbr, '' );
57 * List deleted pages recorded in the archive table matching the
59 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
61 * @param $prefix String: title prefix
62 * @return ResultWrapper
64 public static function listPagesByPrefix( $prefix ) {
65 $dbr = wfGetDB( DB_SLAVE
);
67 $title = Title
::newFromText( $prefix );
69 $ns = $title->getNamespace();
70 $prefix = $title->getDBkey();
72 // Prolly won't work too good
73 // @todo handle bare namespace names cleanly?
77 'ar_namespace' => $ns,
78 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
80 return self
::listPages( $dbr, $conds );
84 * @param $dbr DatabaseBase
86 * @return bool|ResultWrapper
88 protected static function listPages( $dbr, $condition ) {
89 return $dbr->resultObject(
100 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
101 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
109 * List the revisions of the given page. Returns result wrapper with
110 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
112 * @return ResultWrapper
114 function listRevisions() {
115 $dbr = wfGetDB( DB_SLAVE
);
116 $res = $dbr->select( 'archive',
118 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
119 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1'
121 array( 'ar_namespace' => $this->title
->getNamespace(),
122 'ar_title' => $this->title
->getDBkey() ),
124 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
125 $ret = $dbr->resultObject( $res );
130 * List the deleted file revisions for this page, if it's a file page.
131 * Returns a result wrapper with various filearchive fields, or null
132 * if not a file page.
134 * @return ResultWrapper
135 * @todo Does this belong in Image for fuller encapsulation?
137 function listFiles() {
138 if( $this->title
->getNamespace() == NS_FILE
) {
139 $dbr = wfGetDB( DB_SLAVE
);
140 $res = $dbr->select( 'filearchive',
160 array( 'fa_name' => $this->title
->getDBkey() ),
162 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
163 $ret = $dbr->resultObject( $res );
170 * Return a Revision object containing data for the deleted revision.
171 * Note that the result *may* or *may not* have a null page ID.
173 * @param $timestamp String
176 function getRevision( $timestamp ) {
177 $dbr = wfGetDB( DB_SLAVE
);
178 $row = $dbr->selectRow( 'archive',
193 array( 'ar_namespace' => $this->title
->getNamespace(),
194 'ar_title' => $this->title
->getDBkey(),
195 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
198 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleID() ) );
205 * Return the most-previous revision, either live or deleted, against
206 * the deleted revision given by timestamp.
208 * May produce unexpected results in case of history merges or other
209 * unusual time issues.
211 * @param $timestamp String
212 * @return Revision or null
214 function getPreviousRevision( $timestamp ) {
215 $dbr = wfGetDB( DB_SLAVE
);
217 // Check the previous deleted revision...
218 $row = $dbr->selectRow( 'archive',
220 array( 'ar_namespace' => $this->title
->getNamespace(),
221 'ar_title' => $this->title
->getDBkey(),
223 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
226 'ORDER BY' => 'ar_timestamp DESC',
228 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
230 $row = $dbr->selectRow( array( 'page', 'revision' ),
231 array( 'rev_id', 'rev_timestamp' ),
233 'page_namespace' => $this->title
->getNamespace(),
234 'page_title' => $this->title
->getDBkey(),
235 'page_id = rev_page',
237 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
240 'ORDER BY' => 'rev_timestamp DESC',
242 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
243 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
245 if( $prevLive && $prevLive > $prevDeleted ) {
246 // Most prior revision was live
247 return Revision
::newFromId( $prevLiveId );
248 } elseif( $prevDeleted ) {
249 // Most prior revision was deleted
250 return $this->getRevision( $prevDeleted );
252 // No prior revision on this page.
258 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
260 * @param $row Object: database row
263 function getTextFromRow( $row ) {
264 if( is_null( $row->ar_text_id
) ) {
265 // An old row from MediaWiki 1.4 or previous.
266 // Text is embedded in this row in classic compression format.
267 return Revision
::getRevisionText( $row, 'ar_' );
269 // New-style: keyed to the text storage backend.
270 $dbr = wfGetDB( DB_SLAVE
);
271 $text = $dbr->selectRow( 'text',
272 array( 'old_text', 'old_flags' ),
273 array( 'old_id' => $row->ar_text_id
),
275 return Revision
::getRevisionText( $text );
280 * Fetch (and decompress if necessary) the stored text of the most
281 * recently edited deleted revision of the page.
283 * If there are no archived revisions for the page, returns NULL.
287 function getLastRevisionText() {
288 $dbr = wfGetDB( DB_SLAVE
);
289 $row = $dbr->selectRow( 'archive',
290 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
291 array( 'ar_namespace' => $this->title
->getNamespace(),
292 'ar_title' => $this->title
->getDBkey() ),
294 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
296 return $this->getTextFromRow( $row );
303 * Quick check if any archived revisions are present for the page.
307 function isDeleted() {
308 $dbr = wfGetDB( DB_SLAVE
);
309 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
310 array( 'ar_namespace' => $this->title
->getNamespace(),
311 'ar_title' => $this->title
->getDBkey() ),
318 * Restore the given (or all) text and file revisions for the page.
319 * Once restored, the items will be removed from the archive tables.
320 * The deletion log will be updated with an undeletion notice.
322 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
323 * @param $comment String
324 * @param $fileVersions Array
325 * @param $unsuppress Boolean
326 * @param $user User doing the action, or null to use $wgUser
328 * @return array(number of file revisions restored, number of image revisions restored, log message)
329 * on success, false on failure
331 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
334 // If both the set of text revisions and file revisions are empty,
335 // restore everything. Otherwise, just restore the requested items.
336 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
338 $restoreText = $restoreAll ||
!empty( $timestamps );
339 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
341 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
342 $img = wfLocalFile( $this->title
);
343 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
344 if ( !$this->fileStatus
->isOk() ) {
347 $filesRestored = $this->fileStatus
->successCount
;
353 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
354 if( $textRestored === false ) { // It must be one of UNDELETE_*
363 if( $textRestored && $filesRestored ) {
364 $reason = wfMessage( 'undeletedrevisions-files' )
365 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
366 } elseif( $textRestored ) {
367 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
368 ->inContentLanguage()->text();
369 } elseif( $filesRestored ) {
370 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
371 ->inContentLanguage()->text();
373 wfDebug( "Undelete: nothing undeleted...\n" );
377 if( trim( $comment ) != '' ) {
378 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
381 if ( $user === null ) {
385 $logEntry = new ManualLogEntry( 'delete', 'restore' );
386 $logEntry->setPerformer( $user );
387 $logEntry->setTarget( $this->title
);
388 $logEntry->setComment( $reason );
389 $logid = $logEntry->insert();
390 $logEntry->publish( $logid );
392 return array( $textRestored, $filesRestored, $reason );
396 * This is the meaty bit -- restores archived revisions of the given page
397 * to the cur/old tables. If the page currently exists, all revisions will
398 * be stuffed into old, otherwise the most recent will go into cur.
400 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
401 * @param $comment String
402 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
404 * @return Mixed: number of revisions restored or false on failure
406 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
407 if ( wfReadOnly() ) {
410 $restoreAll = empty( $timestamps );
412 $dbw = wfGetDB( DB_MASTER
);
414 # Does this page already exist? We'll have to update it...
415 $article = WikiPage
::factory( $this->title
);
416 # Load latest data for the current page (bug 31179)
417 $article->loadPageData( 'fromdbmaster' );
418 $oldcountable = $article->isCountable();
420 $page = $dbw->selectRow( 'page',
421 array( 'page_id', 'page_latest' ),
422 array( 'page_namespace' => $this->title
->getNamespace(),
423 'page_title' => $this->title
->getDBkey() ),
425 array( 'FOR UPDATE' ) // lock page
429 # Page already exists. Import the history, and if necessary
430 # we'll update the latest revision field in the record.
432 $pageId = $page->page_id
;
433 $previousRevId = $page->page_latest
;
434 # Get the time span of this page
435 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
436 array( 'rev_id' => $previousRevId ),
438 if( $previousTimestamp === false ) {
439 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
443 # Have to create a new article...
446 $previousTimestamp = 0;
450 $oldones = '1 = 1'; # All revisions...
452 $oldts = implode( ',',
453 array_map( array( &$dbw, 'addQuotes' ),
454 array_map( array( &$dbw, 'timestamp' ),
457 $oldones = "ar_timestamp IN ( {$oldts} )";
461 * Select each archived revision...
463 $result = $dbw->select( 'archive',
479 'ar_namespace' => $this->title
->getNamespace(),
480 'ar_title' => $this->title
->getDBkey(),
483 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
485 $ret = $dbw->resultObject( $result );
486 $rev_count = $dbw->numRows( $result );
488 wfDebug( __METHOD__
. ": no revisions to restore\n" );
492 $ret->seek( $rev_count - 1 ); // move to last
493 $row = $ret->fetchObject(); // get newest archived rev
494 $ret->seek( 0 ); // move back
497 // Check the state of the newest to-be version...
498 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
499 return false; // we can't leave the current revision like this!
501 // Safe to insert now...
502 $newid = $article->insertOn( $dbw );
505 // Check if a deleted revision will become the current revision...
506 if( $row->ar_timestamp
> $previousTimestamp ) {
507 // Check the state of the newest to-be version...
508 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
509 return false; // we can't leave the current revision like this!
517 foreach ( $ret as $row ) {
518 // Check for key dupes due to shitty archive integrity.
519 if( $row->ar_rev_id
) {
520 $exists = $dbw->selectField( 'revision', '1',
521 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
523 continue; // don't throw DB errors
526 // Insert one revision at a time...maintaining deletion status
527 // unless we are specifically removing all restrictions...
528 $revision = Revision
::newFromArchiveRow( $row,
531 'deleted' => $unsuppress ?
0 : $row->ar_deleted
534 $revision->insertOn( $dbw );
537 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
539 # Now that it's safely stored, take it out of the archive
540 $dbw->delete( 'archive',
542 'ar_namespace' => $this->title
->getNamespace(),
543 'ar_title' => $this->title
->getDBkey(),
547 // Was anything restored at all?
548 if ( $restored == 0 ) {
552 $created = (bool)$newid;
554 // Attach the latest revision to the page...
555 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
556 if ( $created ||
$wasnew ) {
557 // Update site stats, link tables, etc
558 $user = User
::newFromName( $revision->getRawUserText(), false );
559 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
562 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
564 if( $this->title
->getNamespace() == NS_FILE
) {
565 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
575 function getFileStatus() { return $this->fileStatus
; }
579 * Special page allowing users with the appropriate permissions to view
580 * and restore deleted content.
582 * @ingroup SpecialPage
584 class SpecialUndelete
extends SpecialPage
{
585 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
586 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
593 function __construct() {
594 parent
::__construct( 'Undelete', 'deletedhistory' );
597 function loadRequest( $par ) {
598 $request = $this->getRequest();
599 $user = $this->getUser();
601 $this->mAction
= $request->getVal( 'action' );
602 if ( $par !== null && $par !== '' ) {
603 $this->mTarget
= $par;
605 $this->mTarget
= $request->getVal( 'target' );
607 $this->mTargetObj
= null;
608 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
609 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
611 $this->mSearchPrefix
= $request->getText( 'prefix' );
612 $time = $request->getVal( 'timestamp' );
613 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
614 $this->mFilename
= $request->getVal( 'file' );
616 $posted = $request->wasPosted() &&
617 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
618 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
619 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
620 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
621 $this->mDiff
= $request->getCheck( 'diff' );
622 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
623 $this->mComment
= $request->getText( 'wpComment' );
624 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
625 $this->mToken
= $request->getVal( 'token' );
627 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
628 $this->mAllowed
= true; // user can restore
629 $this->mCanView
= true; // user can view content
630 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
631 $this->mAllowed
= false; // user cannot restore
632 $this->mCanView
= true; // user can view content
633 $this->mRestore
= false;
634 } else { // user can only view the list of revisions
635 $this->mAllowed
= false;
636 $this->mCanView
= false;
637 $this->mTimestamp
= '';
638 $this->mRestore
= false;
641 if( $this->mRestore ||
$this->mInvert
) {
642 $timestamps = array();
643 $this->mFileVersions
= array();
644 foreach( $request->getValues() as $key => $val ) {
646 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
647 array_push( $timestamps, $matches[1] );
650 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
651 $this->mFileVersions
[] = intval( $matches[1] );
654 rsort( $timestamps );
655 $this->mTargetTimestamp
= $timestamps;
659 function execute( $par ) {
660 $this->checkPermissions();
661 $user = $this->getUser();
664 $this->outputHeader();
666 $this->loadRequest( $par );
668 $out = $this->getOutput();
670 if ( is_null( $this->mTargetObj
) ) {
671 $out->addWikiMsg( 'undelete-header' );
673 # Not all users can just browse every deleted page from the list
674 if ( $user->isAllowed( 'browsearchive' ) ) {
675 $this->showSearchForm();
680 if ( $this->mAllowed
) {
681 $out->setPageTitle( $this->msg( 'undeletepage' ) );
683 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
686 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
688 if ( $this->mTimestamp
!== '' ) {
689 $this->showRevision( $this->mTimestamp
);
690 } elseif ( $this->mFilename
!== null ) {
691 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
692 // Check if user is allowed to see this file
693 if ( !$file->exists() ) {
694 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
695 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
696 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
697 throw new PermissionsError( 'suppressrevision' );
699 throw new PermissionsError( 'deletedtext' );
701 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
702 $this->showFileConfirmationForm( $this->mFilename
);
704 $this->showFile( $this->mFilename
);
706 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
709 $this->showHistory();
713 function showSearchForm() {
716 $out = $this->getOutput();
717 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
719 Xml
::openElement( 'form', array(
721 'action' => $wgScript ) ) .
722 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
723 Html
::hidden( 'title',
724 $this->getTitle()->getPrefixedDbKey() ) .
725 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
726 'prefix', 'prefix', 20,
727 $this->mSearchPrefix
) . ' ' .
728 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
729 Xml
::closeElement( 'fieldset' ) .
730 Xml
::closeElement( 'form' )
733 # List undeletable articles
734 if( $this->mSearchPrefix
) {
735 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
736 $this->showList( $result );
741 * Generic list of deleted pages
743 * @param $result ResultWrapper
746 private function showList( $result ) {
747 $out = $this->getOutput();
749 if( $result->numRows() == 0 ) {
750 $out->addWikiMsg( 'undelete-no-results' );
754 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
756 $undelete = $this->getTitle();
757 $out->addHTML( "<ul>\n" );
758 foreach ( $result as $row ) {
759 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
760 if ( $title !== null ) {
761 $item = Linker
::linkKnown(
763 htmlspecialchars( $title->getPrefixedText() ),
765 array( 'target' => $title->getPrefixedText() )
768 // The title is no longer valid, show as text
769 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
770 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
772 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
773 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
776 $out->addHTML( "</ul>\n" );
781 private function showRevision( $timestamp ) {
782 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
786 $archive = new PageArchive( $this->mTargetObj
);
787 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
788 $rev = $archive->getRevision( $timestamp );
790 $out = $this->getOutput();
791 $user = $this->getUser();
794 $out->addWikiMsg( 'undeleterevision-missing' );
798 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
799 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
800 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
803 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
804 $out->addHTML( '<br />' );
805 // and we are allowed to see...
810 $previousRev = $archive->getPreviousRevision( $timestamp );
812 $this->showDiff( $previousRev, $rev );
813 if( $this->mDiffOnly
) {
816 $out->addHTML( '<hr />' );
819 $out->addWikiMsg( 'undelete-nodiff' );
823 $link = Linker
::linkKnown(
824 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
825 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
828 $lang = $this->getLanguage();
830 // date and time are separate parameters to facilitate localisation.
831 // $time is kept for backward compat reasons.
832 $time = $lang->userTimeAndDate( $timestamp, $user );
833 $d = $lang->userDate( $timestamp, $user );
834 $t = $lang->userTime( $timestamp, $user );
835 $userLink = Linker
::revUserTools( $rev );
837 if( $this->mPreview
) {
838 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
840 $openDiv = '<div id="mw-undelete-revision">';
842 $out->addHTML( $openDiv );
844 // Revision delete links
845 if ( !$this->mDiff
) {
846 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
848 $out->addHTML( "$revdel " );
852 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
853 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
854 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
856 if( $this->mPreview
) {
858 $popts = $out->parserOptions();
859 $popts->setEditSection( false );
860 $out->parserOptions( $popts );
861 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
865 Xml
::element( 'textarea', array(
866 'readonly' => 'readonly',
867 'cols' => intval( $user->getOption( 'cols' ) ),
868 'rows' => intval( $user->getOption( 'rows' ) ) ),
869 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) .
870 Xml
::openElement( 'div' ) .
871 Xml
::openElement( 'form', array(
873 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
874 Xml
::element( 'input', array(
877 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
878 Xml
::element( 'input', array(
880 'name' => 'timestamp',
881 'value' => $timestamp ) ) .
882 Xml
::element( 'input', array(
884 'name' => 'wpEditToken',
885 'value' => $user->getEditToken() ) ) .
886 Xml
::element( 'input', array(
889 'value' => $this->msg( 'showpreview' )->text() ) ) .
890 Xml
::element( 'input', array(
893 'value' => $this->msg( 'showdiff' )->text() ) ) .
894 Xml
::closeElement( 'form' ) .
895 Xml
::closeElement( 'div' ) );
899 * Build a diff display between this and the previous either deleted
900 * or non-deleted edit.
902 * @param $previousRev Revision
903 * @param $currentRev Revision
904 * @return String: HTML
906 function showDiff( $previousRev, $currentRev ) {
907 $diffEngine = new DifferenceEngine( $this->getContext() );
908 $diffEngine->showDiffStyle();
909 $this->getOutput()->addHTML(
911 "<table width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
912 "<col class='diff-marker' />" .
913 "<col class='diff-content' />" .
914 "<col class='diff-marker' />" .
915 "<col class='diff-content' />" .
917 "<td colspan='2' width='50%' style='text-align: center' class='diff-otitle'>" .
918 $this->diffHeader( $previousRev, 'o' ) .
920 "<td colspan='2' width='50%' style='text-align: center' class='diff-ntitle'>" .
921 $this->diffHeader( $currentRev, 'n' ) .
924 $diffEngine->generateDiffBody(
925 $previousRev->getText( Revision
::FOR_THIS_USER
, $this->getUser() ),
926 $currentRev->getText( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
933 * @param $rev Revision
937 private function diffHeader( $rev, $prefix ) {
938 $isDeleted = !( $rev->getId() && $rev->getTitle() );
940 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
941 $targetPage = $this->getTitle();
942 $targetQuery = array(
943 'target' => $this->mTargetObj
->getPrefixedText(),
944 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
947 /// @todo FIXME: getId() may return non-zero for deleted revs...
948 $targetPage = $rev->getTitle();
949 $targetQuery = array( 'oldid' => $rev->getId() );
951 // Add show/hide deletion links if available
952 $user = $this->getUser();
953 $lang = $this->getLanguage();
954 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
955 if ( $rdel ) $rdel = " $rdel";
957 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
962 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
963 $lang->userDate( $rev->getTimestamp(), $user ),
964 $lang->userTime( $rev->getTimestamp(), $user )
970 '<div id="mw-diff-'.$prefix.'title2">' .
971 Linker
::revUserTools( $rev ) . '<br />' .
973 '<div id="mw-diff-'.$prefix.'title3">' .
974 Linker
::revComment( $rev ) . $rdel . '<br />' .
979 * Show a form confirming whether a tokenless user really wants to see a file
981 private function showFileConfirmationForm( $key ) {
982 $out = $this->getOutput();
983 $lang = $this->getLanguage();
984 $user = $this->getUser();
985 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
986 $out->addWikiMsg( 'undelete-show-file-confirm',
987 $this->mTargetObj
->getText(),
988 $lang->userDate( $file->getTimestamp(), $user ),
989 $lang->userTime( $file->getTimestamp(), $user ) );
991 Xml
::openElement( 'form', array(
993 'action' => $this->getTitle()->getLocalURL(
994 'target=' . urlencode( $this->mTarget
) .
995 '&file=' . urlencode( $key ) .
996 '&token=' . urlencode( $user->getEditToken( $key ) ) )
999 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1005 * Show a deleted file version requested by the visitor.
1007 private function showFile( $key ) {
1008 $this->getOutput()->disable();
1010 # We mustn't allow the output to be Squid cached, otherwise
1011 # if an admin previews a deleted image, and it's cached, then
1012 # a user without appropriate permissions can toddle off and
1013 # nab the image, and Squid will serve it
1014 $response = $this->getRequest()->response();
1015 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1016 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1017 $response->header( 'Pragma: no-cache' );
1019 $repo = RepoGroup
::singleton()->getLocalRepo();
1020 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1021 $repo->streamFile( $path );
1024 private function showHistory() {
1025 $out = $this->getOutput();
1026 if( $this->mAllowed
) {
1027 $out->addModules( 'mediawiki.special.undelete' );
1030 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1031 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1034 $archive = new PageArchive( $this->mTargetObj
);
1035 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1037 $text = $archive->getLastRevisionText();
1038 if( is_null( $text ) ) {
1039 $out->addWikiMsg( 'nohistory' );
1043 $out->addHTML( '<div class="mw-undelete-history">' );
1044 if ( $this->mAllowed
) {
1045 $out->addWikiMsg( 'undeletehistory' );
1046 $out->addWikiMsg( 'undeleterevdel' );
1048 $out->addWikiMsg( 'undeletehistorynoadmin' );
1050 $out->addHTML( '</div>' );
1052 # List all stored revisions
1053 $revisions = $archive->listRevisions();
1054 $files = $archive->listFiles();
1056 $haveRevisions = $revisions && $revisions->numRows() > 0;
1057 $haveFiles = $files && $files->numRows() > 0;
1059 # Batch existence check on user and talk pages
1060 if( $haveRevisions ) {
1061 $batch = new LinkBatch();
1062 foreach ( $revisions as $row ) {
1063 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1064 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1067 $revisions->seek( 0 );
1070 $batch = new LinkBatch();
1071 foreach ( $files as $row ) {
1072 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1073 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1079 if ( $this->mAllowed
) {
1080 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1081 # Start the form here
1082 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1083 $out->addHTML( $top );
1086 # Show relevant lines from the deletion log:
1087 $deleteLogPage = new LogPage( 'delete' );
1088 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1089 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1090 # Show relevant lines from the suppression log:
1091 $suppressLogPage = new LogPage( 'suppress' );
1092 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1093 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1094 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1097 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1098 # Format the user-visible controls (comment field, submission button)
1099 # in a nice little table
1100 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1104 <td class='mw-input'>" .
1105 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1106 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1110 $unsuppressBox = '';
1113 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1114 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1116 <td colspan='2' class='mw-undelete-extrahelp'>" .
1117 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1121 <td class='mw-label'>" .
1122 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1124 <td class='mw-input'>" .
1125 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1130 <td class='mw-submit'>" .
1131 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1132 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1136 Xml
::closeElement( 'table' ) .
1137 Xml
::closeElement( 'fieldset' );
1139 $out->addHTML( $table );
1142 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1144 if( $haveRevisions ) {
1145 # The page's stored (deleted) history:
1146 $out->addHTML( '<ul>' );
1147 $remaining = $revisions->numRows();
1148 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1150 foreach ( $revisions as $row ) {
1152 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1155 $out->addHTML( '</ul>' );
1157 $out->addWikiMsg( 'nohistory' );
1161 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1162 $out->addHTML( '<ul>' );
1163 foreach ( $files as $row ) {
1164 $out->addHTML( $this->formatFileRow( $row ) );
1167 $out->addHTML( '</ul>' );
1170 if ( $this->mAllowed
) {
1171 # Slip in the hidden controls here
1172 $misc = Html
::hidden( 'target', $this->mTarget
);
1173 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1174 $misc .= Xml
::closeElement( 'form' );
1175 $out->addHTML( $misc );
1181 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1182 $rev = Revision
::newFromArchiveRow( $row,
1183 array( 'page' => $this->mTargetObj
->getArticleID() ) );
1185 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1186 // Build checkboxen...
1187 if( $this->mAllowed
) {
1188 if( $this->mInvert
) {
1189 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1190 $checkBox = Xml
::check( "ts$ts" );
1192 $checkBox = Xml
::check( "ts$ts", true );
1195 $checkBox = Xml
::check( "ts$ts" );
1200 $user = $this->getUser();
1201 // Build page & diff links...
1202 if( $this->mCanView
) {
1203 $titleObj = $this->getTitle();
1205 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1206 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1207 $last = $this->msg( 'diff' )->escaped();
1208 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1209 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1210 $last = Linker
::linkKnown(
1212 $this->msg( 'diff' )->escaped(),
1215 'target' => $this->mTargetObj
->getPrefixedText(),
1221 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1222 $last = $this->msg( 'diff' )->escaped();
1225 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1226 $last = $this->msg( 'diff' )->escaped();
1229 $userLink = Linker
::revUserTools( $rev );
1230 // Revision text size
1231 $size = $row->ar_len
;
1232 if( !is_null( $size ) ) {
1233 $revTextSize = Linker
::formatRevisionSize( $size );
1236 $comment = Linker
::revComment( $rev );
1237 // Revision delete links
1238 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1240 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1241 return "<li>$revisionRow</li>";
1244 private function formatFileRow( $row ) {
1245 $file = ArchivedFile
::newFromRow( $row );
1247 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1248 $user = $this->getUser();
1249 if( $this->mAllowed
&& $row->fa_storage_key
) {
1250 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1251 $key = urlencode( $row->fa_storage_key
);
1252 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1255 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1257 $userLink = $this->getFileUser( $file );
1258 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1259 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1260 $data = htmlspecialchars( $data . ' ' . $bytes );
1261 $comment = $this->getFileComment( $file );
1263 // Add show/hide deletion links if available
1264 $canHide = $user->isAllowed( 'deleterevision' );
1265 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1266 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1267 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1270 'type' => 'filearchive',
1271 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1272 'ids' => $row->fa_id
1274 $revdlink = Linker
::revDeleteLink( $query,
1275 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1281 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1285 * Fetch revision text link if it's available to all users
1287 * @param $rev Revision
1288 * @param $titleObj Title
1289 * @param $ts string Timestamp
1292 function getPageLink( $rev, $titleObj, $ts ) {
1293 $user = $this->getUser();
1294 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1296 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1297 return '<span class="history-deleted">' . $time . '</span>';
1299 $link = Linker
::linkKnown(
1301 htmlspecialchars( $time ),
1304 'target' => $this->mTargetObj
->getPrefixedText(),
1308 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1309 $link = '<span class="history-deleted">' . $link . '</span>';
1316 * Fetch image view link if it's available to all users
1319 * @param $titleObj Title
1320 * @param $ts string A timestamp
1321 * @param $key String: a storage key
1323 * @return String: HTML fragment
1325 function getFileLink( $file, $titleObj, $ts, $key ) {
1326 $user = $this->getUser();
1327 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1329 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1330 return '<span class="history-deleted">' . $time . '</span>';
1332 $link = Linker
::linkKnown(
1334 htmlspecialchars( $time ),
1337 'target' => $this->mTargetObj
->getPrefixedText(),
1339 'token' => $user->getEditToken( $key )
1342 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1343 $link = '<span class="history-deleted">' . $link . '</span>';
1350 * Fetch file's user id if it's available to this user
1353 * @return String: HTML fragment
1355 function getFileUser( $file ) {
1356 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1357 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1359 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1360 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1361 if( $file->isDeleted( File
::DELETED_USER
) ) {
1362 $link = '<span class="history-deleted">' . $link . '</span>';
1369 * Fetch file upload comment if it's available to this user
1372 * @return String: HTML fragment
1374 function getFileComment( $file ) {
1375 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1376 return '<span class="history-deleted"><span class="comment">' .
1377 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1379 $link = Linker
::commentBlock( $file->getRawDescription() );
1380 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1381 $link = '<span class="history-deleted">' . $link . '</span>';
1387 function undelete() {
1388 global $wgUploadMaintenance;
1390 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1391 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1394 if ( wfReadOnly() ) {
1395 throw new ReadOnlyError
;
1398 $out = $this->getOutput();
1399 $archive = new PageArchive( $this->mTargetObj
);
1400 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1401 $ok = $archive->undelete(
1402 $this->mTargetTimestamp
,
1404 $this->mFileVersions
,
1409 if( is_array( $ok ) ) {
1410 if ( $ok[1] ) { // Undeleted file count
1411 wfRunHooks( 'FileUndeleteComplete', array(
1412 $this->mTargetObj
, $this->mFileVersions
,
1413 $this->getUser(), $this->mComment
) );
1416 $link = Linker
::linkKnown( $this->mTargetObj
);
1417 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1419 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1420 $out->addWikiMsg( 'cannotundelete' );
1421 $out->addWikiMsg( 'undeleterevdel' );
1424 // Show file deletion warnings and errors
1425 $status = $archive->getFileStatus();
1426 if( $status && !$status->isGood() ) {
1427 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );