3 * Implements Special:Undelete
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Used to show archived pages and eventually restore them.
27 * @ingroup SpecialPage
37 function __construct( $title ) {
38 if( is_null( $title ) ) {
39 throw new MWException( __METHOD__
. ' given a null title.' );
41 $this->title
= $title;
45 * List all deleted pages recorded in the archive table. Returns result
46 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
49 * @return ResultWrapper
51 public static function listAllPages() {
52 $dbr = wfGetDB( DB_SLAVE
);
53 return self
::listPages( $dbr, '' );
57 * List deleted pages recorded in the archive table matching the
59 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
61 * @param $prefix String: title prefix
62 * @return ResultWrapper
64 public static function listPagesByPrefix( $prefix ) {
65 $dbr = wfGetDB( DB_SLAVE
);
67 $title = Title
::newFromText( $prefix );
69 $ns = $title->getNamespace();
70 $prefix = $title->getDBkey();
72 // Prolly won't work too good
73 // @todo handle bare namespace names cleanly?
77 'ar_namespace' => $ns,
78 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
80 return self
::listPages( $dbr, $conds );
84 * @param $dbr DatabaseBase
86 * @return bool|ResultWrapper
88 protected static function listPages( $dbr, $condition ) {
89 return $dbr->resultObject(
100 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
101 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
109 * List the revisions of the given page. Returns result wrapper with
110 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
112 * @return ResultWrapper
114 function listRevisions() {
115 $dbr = wfGetDB( DB_SLAVE
);
116 $res = $dbr->select( 'archive',
118 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
119 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1'
121 array( 'ar_namespace' => $this->title
->getNamespace(),
122 'ar_title' => $this->title
->getDBkey() ),
123 'PageArchive::listRevisions',
124 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
125 $ret = $dbr->resultObject( $res );
130 * List the deleted file revisions for this page, if it's a file page.
131 * Returns a result wrapper with various filearchive fields, or null
132 * if not a file page.
134 * @return ResultWrapper
135 * @todo Does this belong in Image for fuller encapsulation?
137 function listFiles() {
138 if( $this->title
->getNamespace() == NS_FILE
) {
139 $dbr = wfGetDB( DB_SLAVE
);
140 $res = $dbr->select( 'filearchive',
160 array( 'fa_name' => $this->title
->getDBkey() ),
162 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
163 $ret = $dbr->resultObject( $res );
170 * Return a Revision object containing data for the deleted revision.
171 * Note that the result *may* or *may not* have a null page ID.
173 * @param $timestamp String
176 function getRevision( $timestamp ) {
177 $dbr = wfGetDB( DB_SLAVE
);
178 $row = $dbr->selectRow( 'archive',
193 array( 'ar_namespace' => $this->title
->getNamespace(),
194 'ar_title' => $this->title
->getDBkey(),
195 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
198 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleID() ) );
205 * Return the most-previous revision, either live or deleted, against
206 * the deleted revision given by timestamp.
208 * May produce unexpected results in case of history merges or other
209 * unusual time issues.
211 * @param $timestamp String
212 * @return Revision or null
214 function getPreviousRevision( $timestamp ) {
215 $dbr = wfGetDB( DB_SLAVE
);
217 // Check the previous deleted revision...
218 $row = $dbr->selectRow( 'archive',
220 array( 'ar_namespace' => $this->title
->getNamespace(),
221 'ar_title' => $this->title
->getDBkey(),
223 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
226 'ORDER BY' => 'ar_timestamp DESC',
228 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
230 $row = $dbr->selectRow( array( 'page', 'revision' ),
231 array( 'rev_id', 'rev_timestamp' ),
233 'page_namespace' => $this->title
->getNamespace(),
234 'page_title' => $this->title
->getDBkey(),
235 'page_id = rev_page',
237 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
240 'ORDER BY' => 'rev_timestamp DESC',
242 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
243 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
245 if( $prevLive && $prevLive > $prevDeleted ) {
246 // Most prior revision was live
247 return Revision
::newFromId( $prevLiveId );
248 } elseif( $prevDeleted ) {
249 // Most prior revision was deleted
250 return $this->getRevision( $prevDeleted );
252 // No prior revision on this page.
258 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
260 * @param $row Object: database row
263 function getTextFromRow( $row ) {
264 if( is_null( $row->ar_text_id
) ) {
265 // An old row from MediaWiki 1.4 or previous.
266 // Text is embedded in this row in classic compression format.
267 return Revision
::getRevisionText( $row, 'ar_' );
269 // New-style: keyed to the text storage backend.
270 $dbr = wfGetDB( DB_SLAVE
);
271 $text = $dbr->selectRow( 'text',
272 array( 'old_text', 'old_flags' ),
273 array( 'old_id' => $row->ar_text_id
),
275 return Revision
::getRevisionText( $text );
280 * Fetch (and decompress if necessary) the stored text of the most
281 * recently edited deleted revision of the page.
283 * If there are no archived revisions for the page, returns NULL.
287 function getLastRevisionText() {
288 $dbr = wfGetDB( DB_SLAVE
);
289 $row = $dbr->selectRow( 'archive',
290 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
291 array( 'ar_namespace' => $this->title
->getNamespace(),
292 'ar_title' => $this->title
->getDBkey() ),
294 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
296 return $this->getTextFromRow( $row );
303 * Quick check if any archived revisions are present for the page.
307 function isDeleted() {
308 $dbr = wfGetDB( DB_SLAVE
);
309 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
310 array( 'ar_namespace' => $this->title
->getNamespace(),
311 'ar_title' => $this->title
->getDBkey() ) );
316 * Restore the given (or all) text and file revisions for the page.
317 * Once restored, the items will be removed from the archive tables.
318 * The deletion log will be updated with an undeletion notice.
320 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
321 * @param $comment String
322 * @param $fileVersions Array
323 * @param $unsuppress Boolean
324 * @param $user User doing the action, or null to use $wgUser
326 * @return array(number of file revisions restored, number of image revisions restored, log message)
327 * on success, false on failure
329 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false, User
$user = null ) {
330 global $wgContLang, $wgUser;
332 // If both the set of text revisions and file revisions are empty,
333 // restore everything. Otherwise, just restore the requested items.
334 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
336 $restoreText = $restoreAll ||
!empty( $timestamps );
337 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
339 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
340 $img = wfLocalFile( $this->title
);
341 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
342 if ( !$this->fileStatus
->isOk() ) {
345 $filesRestored = $this->fileStatus
->successCount
;
351 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
352 if( $textRestored === false ) { // It must be one of UNDELETE_*
361 if( $textRestored && $filesRestored ) {
362 $reason = wfMessage( 'undeletedrevisions-files' )
363 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
364 } elseif( $textRestored ) {
365 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
366 ->inContentLanguage()->text();
367 } elseif( $filesRestored ) {
368 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
369 ->inContentLanguage()->text();
371 wfDebug( "Undelete: nothing undeleted...\n" );
375 if( trim( $comment ) != '' ) {
376 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
379 if ( $user === null ) {
383 $logEntry = new ManualLogEntry( 'delete', 'restore' );
384 $logEntry->setPerformer( $user );
385 $logEntry->setTarget( $this->title
);
386 $logEntry->setComment( $reason );
387 $logid = $logEntry->insert();
388 $logEntry->publish( $logid );
390 return array( $textRestored, $filesRestored, $reason );
394 * This is the meaty bit -- restores archived revisions of the given page
395 * to the cur/old tables. If the page currently exists, all revisions will
396 * be stuffed into old, otherwise the most recent will go into cur.
398 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
399 * @param $comment String
400 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
402 * @return Mixed: number of revisions restored or false on failure
404 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
405 if ( wfReadOnly() ) {
408 $restoreAll = empty( $timestamps );
410 $dbw = wfGetDB( DB_MASTER
);
412 # Does this page already exist? We'll have to update it...
413 $article = WikiPage
::factory( $this->title
);
414 # Load latest data for the current page (bug 31179)
415 $article->loadPageData( 'fromdbmaster' );
416 $oldcountable = $article->isCountable();
418 $page = $dbw->selectRow( 'page',
419 array( 'page_id', 'page_latest' ),
420 array( 'page_namespace' => $this->title
->getNamespace(),
421 'page_title' => $this->title
->getDBkey() ),
423 array( 'FOR UPDATE' ) // lock page
427 # Page already exists. Import the history, and if necessary
428 # we'll update the latest revision field in the record.
430 $pageId = $page->page_id
;
431 $previousRevId = $page->page_latest
;
432 # Get the time span of this page
433 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
434 array( 'rev_id' => $previousRevId ),
436 if( $previousTimestamp === false ) {
437 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
441 # Have to create a new article...
444 $previousTimestamp = 0;
448 $oldones = '1 = 1'; # All revisions...
450 $oldts = implode( ',',
451 array_map( array( &$dbw, 'addQuotes' ),
452 array_map( array( &$dbw, 'timestamp' ),
455 $oldones = "ar_timestamp IN ( {$oldts} )";
459 * Select each archived revision...
461 $result = $dbw->select( 'archive',
477 'ar_namespace' => $this->title
->getNamespace(),
478 'ar_title' => $this->title
->getDBkey(),
481 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
483 $ret = $dbw->resultObject( $result );
484 $rev_count = $dbw->numRows( $result );
486 wfDebug( __METHOD__
. ": no revisions to restore\n" );
490 $ret->seek( $rev_count - 1 ); // move to last
491 $row = $ret->fetchObject(); // get newest archived rev
492 $ret->seek( 0 ); // move back
495 // Check the state of the newest to-be version...
496 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
497 return false; // we can't leave the current revision like this!
499 // Safe to insert now...
500 $newid = $article->insertOn( $dbw );
503 // Check if a deleted revision will become the current revision...
504 if( $row->ar_timestamp
> $previousTimestamp ) {
505 // Check the state of the newest to-be version...
506 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
507 return false; // we can't leave the current revision like this!
515 foreach ( $ret as $row ) {
516 // Check for key dupes due to shitty archive integrity.
517 if( $row->ar_rev_id
) {
518 $exists = $dbw->selectField( 'revision', '1',
519 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
521 continue; // don't throw DB errors
524 // Insert one revision at a time...maintaining deletion status
525 // unless we are specifically removing all restrictions...
526 $revision = Revision
::newFromArchiveRow( $row,
529 'deleted' => $unsuppress ?
0 : $row->ar_deleted
532 $revision->insertOn( $dbw );
535 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
537 # Now that it's safely stored, take it out of the archive
538 $dbw->delete( 'archive',
540 'ar_namespace' => $this->title
->getNamespace(),
541 'ar_title' => $this->title
->getDBkey(),
545 // Was anything restored at all?
546 if ( $restored == 0 ) {
550 $created = (bool)$newid;
552 // Attach the latest revision to the page...
553 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
554 if ( $created ||
$wasnew ) {
555 // Update site stats, link tables, etc
556 $user = User
::newFromName( $revision->getRawUserText(), false );
557 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
560 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
562 if( $this->title
->getNamespace() == NS_FILE
) {
563 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
573 function getFileStatus() { return $this->fileStatus
; }
577 * Special page allowing users with the appropriate permissions to view
578 * and restore deleted content.
580 * @ingroup SpecialPage
582 class SpecialUndelete
extends SpecialPage
{
583 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
584 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
591 function __construct() {
592 parent
::__construct( 'Undelete', 'deletedhistory' );
595 function loadRequest( $par ) {
596 $request = $this->getRequest();
597 $user = $this->getUser();
599 $this->mAction
= $request->getVal( 'action' );
600 if ( $par !== null && $par !== '' ) {
601 $this->mTarget
= $par;
603 $this->mTarget
= $request->getVal( 'target' );
605 $this->mTargetObj
= null;
606 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
607 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
609 $this->mSearchPrefix
= $request->getText( 'prefix' );
610 $time = $request->getVal( 'timestamp' );
611 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
612 $this->mFilename
= $request->getVal( 'file' );
614 $posted = $request->wasPosted() &&
615 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
616 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
617 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
618 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
619 $this->mDiff
= $request->getCheck( 'diff' );
620 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
621 $this->mComment
= $request->getText( 'wpComment' );
622 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
623 $this->mToken
= $request->getVal( 'token' );
625 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
626 $this->mAllowed
= true; // user can restore
627 $this->mCanView
= true; // user can view content
628 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
629 $this->mAllowed
= false; // user cannot restore
630 $this->mCanView
= true; // user can view content
631 $this->mRestore
= false;
632 } else { // user can only view the list of revisions
633 $this->mAllowed
= false;
634 $this->mCanView
= false;
635 $this->mTimestamp
= '';
636 $this->mRestore
= false;
639 if( $this->mRestore ||
$this->mInvert
) {
640 $timestamps = array();
641 $this->mFileVersions
= array();
642 foreach( $request->getValues() as $key => $val ) {
644 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
645 array_push( $timestamps, $matches[1] );
648 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
649 $this->mFileVersions
[] = intval( $matches[1] );
652 rsort( $timestamps );
653 $this->mTargetTimestamp
= $timestamps;
657 function execute( $par ) {
658 $this->checkPermissions();
659 $user = $this->getUser();
662 $this->outputHeader();
664 $this->loadRequest( $par );
666 $out = $this->getOutput();
668 if ( is_null( $this->mTargetObj
) ) {
669 $out->addWikiMsg( 'undelete-header' );
671 # Not all users can just browse every deleted page from the list
672 if ( $user->isAllowed( 'browsearchive' ) ) {
673 $this->showSearchForm();
678 if ( $this->mAllowed
) {
679 $out->setPageTitle( $this->msg( 'undeletepage' ) );
681 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
684 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
686 if ( $this->mTimestamp
!== '' ) {
687 $this->showRevision( $this->mTimestamp
);
688 } elseif ( $this->mFilename
!== null ) {
689 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
690 // Check if user is allowed to see this file
691 if ( !$file->exists() ) {
692 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
693 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
694 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
695 throw new PermissionsError( 'suppressrevision' );
697 throw new PermissionsError( 'deletedtext' );
699 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
700 $this->showFileConfirmationForm( $this->mFilename
);
702 $this->showFile( $this->mFilename
);
704 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
707 $this->showHistory();
711 function showSearchForm() {
714 $out = $this->getOutput();
715 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
717 Xml
::openElement( 'form', array(
719 'action' => $wgScript ) ) .
720 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
721 Html
::hidden( 'title',
722 $this->getTitle()->getPrefixedDbKey() ) .
723 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
724 'prefix', 'prefix', 20,
725 $this->mSearchPrefix
) . ' ' .
726 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
727 Xml
::closeElement( 'fieldset' ) .
728 Xml
::closeElement( 'form' )
731 # List undeletable articles
732 if( $this->mSearchPrefix
) {
733 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
734 $this->showList( $result );
739 * Generic list of deleted pages
741 * @param $result ResultWrapper
744 private function showList( $result ) {
745 $out = $this->getOutput();
747 if( $result->numRows() == 0 ) {
748 $out->addWikiMsg( 'undelete-no-results' );
752 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
754 $undelete = $this->getTitle();
755 $out->addHTML( "<ul>\n" );
756 foreach ( $result as $row ) {
757 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
758 if ( $title !== null ) {
759 $item = Linker
::linkKnown(
761 htmlspecialchars( $title->getPrefixedText() ),
763 array( 'target' => $title->getPrefixedText() )
766 // The title is no longer valid, show as text
767 $item = Html
::element( 'span', array( 'class' => 'mw-invalidtitle' ),
768 Linker
::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace
, $row->ar_title
) );
770 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
771 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
774 $out->addHTML( "</ul>\n" );
779 private function showRevision( $timestamp ) {
780 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
784 $archive = new PageArchive( $this->mTargetObj
);
785 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
786 $rev = $archive->getRevision( $timestamp );
788 $out = $this->getOutput();
789 $user = $this->getUser();
792 $out->addWikiMsg( 'undeleterevision-missing' );
796 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
797 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
798 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
801 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
802 $out->addHTML( '<br />' );
803 // and we are allowed to see...
808 $previousRev = $archive->getPreviousRevision( $timestamp );
810 $this->showDiff( $previousRev, $rev );
811 if( $this->mDiffOnly
) {
814 $out->addHTML( '<hr />' );
817 $out->addWikiMsg( 'undelete-nodiff' );
821 $link = Linker
::linkKnown(
822 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
823 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
826 $lang = $this->getLanguage();
828 // date and time are separate parameters to facilitate localisation.
829 // $time is kept for backward compat reasons.
830 $time = $lang->userTimeAndDate( $timestamp, $user );
831 $d = $lang->userDate( $timestamp, $user );
832 $t = $lang->userTime( $timestamp, $user );
833 $userLink = Linker
::revUserTools( $rev );
835 if( $this->mPreview
) {
836 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
838 $openDiv = '<div id="mw-undelete-revision">';
840 $out->addHTML( $openDiv );
842 // Revision delete links
843 if ( !$this->mDiff
) {
844 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
846 $out->addHTML( "$revdel " );
850 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
851 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
852 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
854 if( $this->mPreview
) {
856 $popts = $out->parserOptions();
857 $popts->setEditSection( false );
858 $out->parserOptions( $popts );
859 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
863 Xml
::element( 'textarea', array(
864 'readonly' => 'readonly',
865 'cols' => intval( $user->getOption( 'cols' ) ),
866 'rows' => intval( $user->getOption( 'rows' ) ) ),
867 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) .
868 Xml
::openElement( 'div' ) .
869 Xml
::openElement( 'form', array(
871 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
872 Xml
::element( 'input', array(
875 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
876 Xml
::element( 'input', array(
878 'name' => 'timestamp',
879 'value' => $timestamp ) ) .
880 Xml
::element( 'input', array(
882 'name' => 'wpEditToken',
883 'value' => $user->getEditToken() ) ) .
884 Xml
::element( 'input', array(
887 'value' => $this->msg( 'showpreview' )->text() ) ) .
888 Xml
::element( 'input', array(
891 'value' => $this->msg( 'showdiff' )->text() ) ) .
892 Xml
::closeElement( 'form' ) .
893 Xml
::closeElement( 'div' ) );
897 * Build a diff display between this and the previous either deleted
898 * or non-deleted edit.
900 * @param $previousRev Revision
901 * @param $currentRev Revision
902 * @return String: HTML
904 function showDiff( $previousRev, $currentRev ) {
905 $diffEngine = new DifferenceEngine( $this->getContext() );
906 $diffEngine->showDiffStyle();
907 $this->getOutput()->addHTML(
909 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
910 "<col class='diff-marker' />" .
911 "<col class='diff-content' />" .
912 "<col class='diff-marker' />" .
913 "<col class='diff-content' />" .
915 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
916 $this->diffHeader( $previousRev, 'o' ) .
918 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
919 $this->diffHeader( $currentRev, 'n' ) .
922 $diffEngine->generateDiffBody(
923 $previousRev->getText( Revision
::FOR_THIS_USER
, $this->getUser() ),
924 $currentRev->getText( Revision
::FOR_THIS_USER
, $this->getUser() ) ) .
931 * @param $rev Revision
935 private function diffHeader( $rev, $prefix ) {
936 $isDeleted = !( $rev->getId() && $rev->getTitle() );
938 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
939 $targetPage = $this->getTitle();
940 $targetQuery = array(
941 'target' => $this->mTargetObj
->getPrefixedText(),
942 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
945 /// @todo FIXME: getId() may return non-zero for deleted revs...
946 $targetPage = $rev->getTitle();
947 $targetQuery = array( 'oldid' => $rev->getId() );
949 // Add show/hide deletion links if available
950 $user = $this->getUser();
951 $lang = $this->getLanguage();
952 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
953 if ( $rdel ) $rdel = " $rdel";
955 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
960 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
961 $lang->userDate( $rev->getTimestamp(), $user ),
962 $lang->userTime( $rev->getTimestamp(), $user )
968 '<div id="mw-diff-'.$prefix.'title2">' .
969 Linker
::revUserTools( $rev ) . '<br />' .
971 '<div id="mw-diff-'.$prefix.'title3">' .
972 Linker
::revComment( $rev ) . $rdel . '<br />' .
977 * Show a form confirming whether a tokenless user really wants to see a file
979 private function showFileConfirmationForm( $key ) {
980 $out = $this->getOutput();
981 $lang = $this->getLanguage();
982 $user = $this->getUser();
983 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
984 $out->addWikiMsg( 'undelete-show-file-confirm',
985 $this->mTargetObj
->getText(),
986 $lang->userDate( $file->getTimestamp(), $user ),
987 $lang->userTime( $file->getTimestamp(), $user ) );
989 Xml
::openElement( 'form', array(
991 'action' => $this->getTitle()->getLocalURL(
992 'target=' . urlencode( $this->mTarget
) .
993 '&file=' . urlencode( $key ) .
994 '&token=' . urlencode( $user->getEditToken( $key ) ) )
997 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1003 * Show a deleted file version requested by the visitor.
1005 private function showFile( $key ) {
1006 $this->getOutput()->disable();
1008 # We mustn't allow the output to be Squid cached, otherwise
1009 # if an admin previews a deleted image, and it's cached, then
1010 # a user without appropriate permissions can toddle off and
1011 # nab the image, and Squid will serve it
1012 $response = $this->getRequest()->response();
1013 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1014 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1015 $response->header( 'Pragma: no-cache' );
1017 $repo = RepoGroup
::singleton()->getLocalRepo();
1018 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1019 $repo->streamFile( $path );
1022 private function showHistory() {
1023 $out = $this->getOutput();
1024 if( $this->mAllowed
) {
1025 $out->addModules( 'mediawiki.special.undelete' );
1028 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1029 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1032 $archive = new PageArchive( $this->mTargetObj
);
1033 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1035 $text = $archive->getLastRevisionText();
1036 if( is_null( $text ) ) {
1037 $out->addWikiMsg( 'nohistory' );
1041 $out->addHTML( '<div class="mw-undelete-history">' );
1042 if ( $this->mAllowed
) {
1043 $out->addWikiMsg( 'undeletehistory' );
1044 $out->addWikiMsg( 'undeleterevdel' );
1046 $out->addWikiMsg( 'undeletehistorynoadmin' );
1048 $out->addHTML( '</div>' );
1050 # List all stored revisions
1051 $revisions = $archive->listRevisions();
1052 $files = $archive->listFiles();
1054 $haveRevisions = $revisions && $revisions->numRows() > 0;
1055 $haveFiles = $files && $files->numRows() > 0;
1057 # Batch existence check on user and talk pages
1058 if( $haveRevisions ) {
1059 $batch = new LinkBatch();
1060 foreach ( $revisions as $row ) {
1061 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1062 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1065 $revisions->seek( 0 );
1068 $batch = new LinkBatch();
1069 foreach ( $files as $row ) {
1070 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1071 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1077 if ( $this->mAllowed
) {
1078 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1079 # Start the form here
1080 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1081 $out->addHTML( $top );
1084 # Show relevant lines from the deletion log:
1085 $deleteLogPage = new LogPage( 'delete' );
1086 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1087 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1088 # Show relevant lines from the suppression log:
1089 $suppressLogPage = new LogPage( 'suppress' );
1090 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1091 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1092 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1095 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1096 # Format the user-visible controls (comment field, submission button)
1097 # in a nice little table
1098 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1102 <td class='mw-input'>" .
1103 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1104 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1108 $unsuppressBox = '';
1111 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1112 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1114 <td colspan='2' class='mw-undelete-extrahelp'>" .
1115 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1119 <td class='mw-label'>" .
1120 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1122 <td class='mw-input'>" .
1123 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1128 <td class='mw-submit'>" .
1129 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1130 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1134 Xml
::closeElement( 'table' ) .
1135 Xml
::closeElement( 'fieldset' );
1137 $out->addHTML( $table );
1140 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1142 if( $haveRevisions ) {
1143 # The page's stored (deleted) history:
1144 $out->addHTML( '<ul>' );
1145 $remaining = $revisions->numRows();
1146 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1148 foreach ( $revisions as $row ) {
1150 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1153 $out->addHTML( '</ul>' );
1155 $out->addWikiMsg( 'nohistory' );
1159 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1160 $out->addHTML( '<ul>' );
1161 foreach ( $files as $row ) {
1162 $out->addHTML( $this->formatFileRow( $row ) );
1165 $out->addHTML( '</ul>' );
1168 if ( $this->mAllowed
) {
1169 # Slip in the hidden controls here
1170 $misc = Html
::hidden( 'target', $this->mTarget
);
1171 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1172 $misc .= Xml
::closeElement( 'form' );
1173 $out->addHTML( $misc );
1179 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1180 $rev = Revision
::newFromArchiveRow( $row,
1181 array( 'page' => $this->mTargetObj
->getArticleID() ) );
1183 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1184 // Build checkboxen...
1185 if( $this->mAllowed
) {
1186 if( $this->mInvert
) {
1187 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1188 $checkBox = Xml
::check( "ts$ts" );
1190 $checkBox = Xml
::check( "ts$ts", true );
1193 $checkBox = Xml
::check( "ts$ts" );
1198 $user = $this->getUser();
1199 // Build page & diff links...
1200 if( $this->mCanView
) {
1201 $titleObj = $this->getTitle();
1203 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1204 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1205 $last = $this->msg( 'diff' )->escaped();
1206 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1207 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1208 $last = Linker
::linkKnown(
1210 $this->msg( 'diff' )->escaped(),
1213 'target' => $this->mTargetObj
->getPrefixedText(),
1219 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1220 $last = $this->msg( 'diff' )->escaped();
1223 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1224 $last = $this->msg( 'diff' )->escaped();
1227 $userLink = Linker
::revUserTools( $rev );
1228 // Revision text size
1229 $size = $row->ar_len
;
1230 if( !is_null( $size ) ) {
1231 $revTextSize = Linker
::formatRevisionSize( $size );
1234 $comment = Linker
::revComment( $rev );
1235 // Revision delete links
1236 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1238 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1239 return "<li>$revisionRow</li>";
1242 private function formatFileRow( $row ) {
1243 $file = ArchivedFile
::newFromRow( $row );
1245 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1246 $user = $this->getUser();
1247 if( $this->mAllowed
&& $row->fa_storage_key
) {
1248 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1249 $key = urlencode( $row->fa_storage_key
);
1250 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1253 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1255 $userLink = $this->getFileUser( $file );
1256 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1257 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1258 $data = htmlspecialchars( $data . ' ' . $bytes );
1259 $comment = $this->getFileComment( $file );
1261 // Add show/hide deletion links if available
1262 $canHide = $user->isAllowed( 'deleterevision' );
1263 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1264 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1265 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1268 'type' => 'filearchive',
1269 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1270 'ids' => $row->fa_id
1272 $revdlink = Linker
::revDeleteLink( $query,
1273 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1279 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1283 * Fetch revision text link if it's available to all users
1285 * @param $rev Revision
1286 * @param $titleObj Title
1287 * @param $ts string Timestamp
1290 function getPageLink( $rev, $titleObj, $ts ) {
1291 $user = $this->getUser();
1292 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1294 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1295 return '<span class="history-deleted">' . $time . '</span>';
1297 $link = Linker
::linkKnown(
1299 htmlspecialchars( $time ),
1302 'target' => $this->mTargetObj
->getPrefixedText(),
1306 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1307 $link = '<span class="history-deleted">' . $link . '</span>';
1314 * Fetch image view link if it's available to all users
1317 * @param $titleObj Title
1318 * @param $ts string A timestamp
1319 * @param $key String: a storage key
1321 * @return String: HTML fragment
1323 function getFileLink( $file, $titleObj, $ts, $key ) {
1324 $user = $this->getUser();
1325 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1327 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1328 return '<span class="history-deleted">' . $time . '</span>';
1330 $link = Linker
::linkKnown(
1332 htmlspecialchars( $time ),
1335 'target' => $this->mTargetObj
->getPrefixedText(),
1337 'token' => $user->getEditToken( $key )
1340 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1341 $link = '<span class="history-deleted">' . $link . '</span>';
1348 * Fetch file's user id if it's available to this user
1351 * @return String: HTML fragment
1353 function getFileUser( $file ) {
1354 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1355 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1357 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1358 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1359 if( $file->isDeleted( File
::DELETED_USER
) ) {
1360 $link = '<span class="history-deleted">' . $link . '</span>';
1367 * Fetch file upload comment if it's available to this user
1370 * @return String: HTML fragment
1372 function getFileComment( $file ) {
1373 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1374 return '<span class="history-deleted"><span class="comment">' .
1375 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1377 $link = Linker
::commentBlock( $file->getRawDescription() );
1378 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1379 $link = '<span class="history-deleted">' . $link . '</span>';
1385 function undelete() {
1386 global $wgUploadMaintenance;
1388 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1389 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1392 if ( wfReadOnly() ) {
1393 throw new ReadOnlyError
;
1396 $out = $this->getOutput();
1397 $archive = new PageArchive( $this->mTargetObj
);
1398 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1399 $ok = $archive->undelete(
1400 $this->mTargetTimestamp
,
1402 $this->mFileVersions
,
1407 if( is_array( $ok ) ) {
1408 if ( $ok[1] ) { // Undeleted file count
1409 wfRunHooks( 'FileUndeleteComplete', array(
1410 $this->mTargetObj
, $this->mFileVersions
,
1411 $this->getUser(), $this->mComment
) );
1414 $link = Linker
::linkKnown( $this->mTargetObj
);
1415 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1417 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1418 $out->addWikiMsg( 'cannotundelete' );
1419 $out->addWikiMsg( 'undeleterevdel' );
1422 // Show file deletion warnings and errors
1423 $status = $archive->getFileStatus();
1424 if( $status && !$status->isGood() ) {
1425 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );