68e35b777d6e34906a8b22cca078d6f9783b7ec7
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' => 'ar_namespace,ar_title',
101 'ORDER BY' => '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
325 * @return array(number of file revisions restored, number of image revisions restored, log message)
326 * on success, false on failure
328 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
329 // If both the set of text revisions and file revisions are empty,
330 // restore everything. Otherwise, just restore the requested items.
331 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
333 $restoreText = $restoreAll ||
!empty( $timestamps );
334 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
336 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
337 $img = wfLocalFile( $this->title
);
338 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
339 if ( !$this->fileStatus
->isOk() ) {
342 $filesRestored = $this->fileStatus
->successCount
;
348 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
349 if( $textRestored === false ) { // It must be one of UNDELETE_*
358 $log = new LogPage( 'delete' );
360 if( $textRestored && $filesRestored ) {
361 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
362 $wgContLang->formatNum( $textRestored ),
363 $wgContLang->formatNum( $filesRestored ) );
364 } elseif( $textRestored ) {
365 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
366 $wgContLang->formatNum( $textRestored ) );
367 } elseif( $filesRestored ) {
368 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
369 $wgContLang->formatNum( $filesRestored ) );
371 wfDebug( "Undelete: nothing undeleted...\n" );
375 if( trim( $comment ) != '' ) {
376 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
378 $log->addEntry( 'restore', $this->title
, $reason );
380 return array( $textRestored, $filesRestored, $reason );
384 * This is the meaty bit -- restores archived revisions of the given page
385 * to the cur/old tables. If the page currently exists, all revisions will
386 * be stuffed into old, otherwise the most recent will go into cur.
388 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
389 * @param $comment String
390 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
392 * @return Mixed: number of revisions restored or false on failure
394 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
395 if ( wfReadOnly() ) {
398 $restoreAll = empty( $timestamps );
400 $dbw = wfGetDB( DB_MASTER
);
402 # Does this page already exist? We'll have to update it...
403 $article = WikiPage
::factory( $this->title
);
404 # Load latest data for the current page (bug 31179)
405 $article->loadPageData( 'fromdbmaster' );
406 $oldcountable = $article->isCountable();
408 $options = 'FOR UPDATE'; // lock page
409 $page = $dbw->selectRow( 'page',
410 array( 'page_id', 'page_latest' ),
411 array( 'page_namespace' => $this->title
->getNamespace(),
412 'page_title' => $this->title
->getDBkey() ),
418 # Page already exists. Import the history, and if necessary
419 # we'll update the latest revision field in the record.
421 $pageId = $page->page_id
;
422 $previousRevId = $page->page_latest
;
423 # Get the time span of this page
424 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
425 array( 'rev_id' => $previousRevId ),
427 if( $previousTimestamp === false ) {
428 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
432 # Have to create a new article...
435 $previousTimestamp = 0;
439 $oldones = '1 = 1'; # All revisions...
441 $oldts = implode( ',',
442 array_map( array( &$dbw, 'addQuotes' ),
443 array_map( array( &$dbw, 'timestamp' ),
446 $oldones = "ar_timestamp IN ( {$oldts} )";
450 * Select each archived revision...
452 $result = $dbw->select( 'archive',
468 'ar_namespace' => $this->title
->getNamespace(),
469 'ar_title' => $this->title
->getDBkey(),
472 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
474 $ret = $dbw->resultObject( $result );
475 $rev_count = $dbw->numRows( $result );
477 wfDebug( __METHOD__
. ": no revisions to restore\n" );
481 $ret->seek( $rev_count - 1 ); // move to last
482 $row = $ret->fetchObject(); // get newest archived rev
483 $ret->seek( 0 ); // move back
486 // Check the state of the newest to-be version...
487 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
488 return false; // we can't leave the current revision like this!
490 // Safe to insert now...
491 $newid = $article->insertOn( $dbw );
494 // Check if a deleted revision will become the current revision...
495 if( $row->ar_timestamp
> $previousTimestamp ) {
496 // Check the state of the newest to-be version...
497 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
498 return false; // we can't leave the current revision like this!
506 foreach ( $ret as $row ) {
507 // Check for key dupes due to shitty archive integrity.
508 if( $row->ar_rev_id
) {
509 $exists = $dbw->selectField( 'revision', '1',
510 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
512 continue; // don't throw DB errors
515 // Insert one revision at a time...maintaining deletion status
516 // unless we are specifically removing all restrictions...
517 $revision = Revision
::newFromArchiveRow( $row,
520 'deleted' => $unsuppress ?
0 : $row->ar_deleted
523 $revision->insertOn( $dbw );
526 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
528 # Now that it's safely stored, take it out of the archive
529 $dbw->delete( 'archive',
531 'ar_namespace' => $this->title
->getNamespace(),
532 'ar_title' => $this->title
->getDBkey(),
536 // Was anything restored at all?
537 if ( $restored == 0 ) {
541 $created = (bool)$newid;
543 // Attach the latest revision to the page...
544 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
545 if ( $created ||
$wasnew ) {
546 // Update site stats, link tables, etc
547 $user = User
::newFromName( $revision->getRawUserText(), false );
548 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
551 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
553 if( $this->title
->getNamespace() == NS_FILE
) {
554 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
564 function getFileStatus() { return $this->fileStatus
; }
568 * Special page allowing users with the appropriate permissions to view
569 * and restore deleted content.
571 * @ingroup SpecialPage
573 class SpecialUndelete
extends SpecialPage
{
574 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
575 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
582 function __construct() {
583 parent
::__construct( 'Undelete', 'deletedhistory' );
586 function loadRequest( $par ) {
587 $request = $this->getRequest();
588 $user = $this->getUser();
590 $this->mAction
= $request->getVal( 'action' );
591 if ( $par !== null && $par !== '' ) {
592 $this->mTarget
= $par;
594 $this->mTarget
= $request->getVal( 'target' );
596 $this->mTargetObj
= null;
597 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
598 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
600 $this->mSearchPrefix
= $request->getText( 'prefix' );
601 $time = $request->getVal( 'timestamp' );
602 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
603 $this->mFilename
= $request->getVal( 'file' );
605 $posted = $request->wasPosted() &&
606 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
607 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
608 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
609 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
610 $this->mDiff
= $request->getCheck( 'diff' );
611 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
612 $this->mComment
= $request->getText( 'wpComment' );
613 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
614 $this->mToken
= $request->getVal( 'token' );
616 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
617 $this->mAllowed
= true; // user can restore
618 $this->mCanView
= true; // user can view content
619 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
620 $this->mAllowed
= false; // user cannot restore
621 $this->mCanView
= true; // user can view content
622 $this->mRestore
= false;
623 } else { // user can only view the list of revisions
624 $this->mAllowed
= false;
625 $this->mCanView
= false;
626 $this->mTimestamp
= '';
627 $this->mRestore
= false;
630 if( $this->mRestore ||
$this->mInvert
) {
631 $timestamps = array();
632 $this->mFileVersions
= array();
633 foreach( $request->getValues() as $key => $val ) {
635 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
636 array_push( $timestamps, $matches[1] );
639 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
640 $this->mFileVersions
[] = intval( $matches[1] );
643 rsort( $timestamps );
644 $this->mTargetTimestamp
= $timestamps;
648 function execute( $par ) {
649 $this->checkPermissions();
650 $user = $this->getUser();
653 $this->outputHeader();
655 $this->loadRequest( $par );
657 $out = $this->getOutput();
659 if ( is_null( $this->mTargetObj
) ) {
660 $out->addWikiMsg( 'undelete-header' );
662 # Not all users can just browse every deleted page from the list
663 if ( $user->isAllowed( 'browsearchive' ) ) {
664 $this->showSearchForm();
669 if ( $this->mAllowed
) {
670 $out->setPageTitle( $this->msg( 'undeletepage' ) );
672 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
675 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
677 if ( $this->mTimestamp
!== '' ) {
678 $this->showRevision( $this->mTimestamp
);
679 } elseif ( $this->mFilename
!== null ) {
680 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
681 // Check if user is allowed to see this file
682 if ( !$file->exists() ) {
683 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
684 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
685 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
686 throw new PermissionsError( 'suppressrevision' );
688 throw new PermissionsError( 'deletedtext' );
690 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
691 $this->showFileConfirmationForm( $this->mFilename
);
693 $this->showFile( $this->mFilename
);
695 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
698 $this->showHistory();
702 function showSearchForm() {
705 $out = $this->getOutput();
706 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
708 Xml
::openElement( 'form', array(
710 'action' => $wgScript ) ) .
711 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
712 Html
::hidden( 'title',
713 $this->getTitle()->getPrefixedDbKey() ) .
714 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
715 'prefix', 'prefix', 20,
716 $this->mSearchPrefix
) . ' ' .
717 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
718 Xml
::closeElement( 'fieldset' ) .
719 Xml
::closeElement( 'form' )
722 # List undeletable articles
723 if( $this->mSearchPrefix
) {
724 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
725 $this->showList( $result );
730 * Generic list of deleted pages
732 * @param $result ResultWrapper
735 private function showList( $result ) {
736 $out = $this->getOutput();
738 if( $result->numRows() == 0 ) {
739 $out->addWikiMsg( 'undelete-no-results' );
743 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
745 $undelete = $this->getTitle();
746 $out->addHTML( "<ul>\n" );
747 foreach ( $result as $row ) {
748 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
749 $link = Linker
::linkKnown(
751 htmlspecialchars( $title->getPrefixedText() ),
753 array( 'target' => $title->getPrefixedText() )
755 $revs = wfMsgExt( 'undeleterevisions',
756 array( 'parseinline' ),
757 $this->getLanguage()->formatNum( $row->count
) );
758 $out->addHTML( "<li>{$link} ({$revs})</li>\n" );
761 $out->addHTML( "</ul>\n" );
766 private function showRevision( $timestamp ) {
767 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
771 $archive = new PageArchive( $this->mTargetObj
);
772 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
773 $rev = $archive->getRevision( $timestamp );
775 $out = $this->getOutput();
776 $user = $this->getUser();
779 $out->addWikiMsg( 'undeleterevision-missing' );
783 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
784 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
785 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
788 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
789 $out->addHTML( '<br />' );
790 // and we are allowed to see...
795 $previousRev = $archive->getPreviousRevision( $timestamp );
797 $this->showDiff( $previousRev, $rev );
798 if( $this->mDiffOnly
) {
801 $out->addHTML( '<hr />' );
804 $out->addWikiMsg( 'undelete-nodiff' );
808 $link = Linker
::linkKnown(
809 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
810 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
813 // date and time are separate parameters to facilitate localisation.
814 // $time is kept for backward compat reasons.
815 $time = $this->getLanguage()->timeAndDate( $timestamp, true );
816 $d = $this->getLanguage()->date( $timestamp, true );
817 $t = $this->getLanguage()->time( $timestamp, true );
818 $userLink = Linker
::revUserTools( $rev );
820 if( $this->mPreview
) {
821 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
823 $openDiv = '<div id="mw-undelete-revision">';
825 $out->addHTML( $openDiv );
827 // Revision delete links
828 if ( !$this->mDiff
) {
829 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
831 $out->addHTML( "$revdel " );
835 $out->addHTML( wfMessage( 'undelete-revision' )->rawParams( $link )->params(
836 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
837 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
839 if( $this->mPreview
) {
841 $popts = $out->parserOptions();
842 $popts->setEditSection( false );
843 $out->parserOptions( $popts );
844 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
848 Xml
::element( 'textarea', array(
849 'readonly' => 'readonly',
850 'cols' => intval( $user->getOption( 'cols' ) ),
851 'rows' => intval( $user->getOption( 'rows' ) ) ),
852 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) .
853 Xml
::openElement( 'div' ) .
854 Xml
::openElement( 'form', array(
856 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
857 Xml
::element( 'input', array(
860 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
861 Xml
::element( 'input', array(
863 'name' => 'timestamp',
864 'value' => $timestamp ) ) .
865 Xml
::element( 'input', array(
867 'name' => 'wpEditToken',
868 'value' => $user->getEditToken() ) ) .
869 Xml
::element( 'input', array(
872 'value' => wfMsg( 'showpreview' ) ) ) .
873 Xml
::element( 'input', array(
876 'value' => wfMsg( 'showdiff' ) ) ) .
877 Xml
::closeElement( 'form' ) .
878 Xml
::closeElement( 'div' ) );
882 * Build a diff display between this and the previous either deleted
883 * or non-deleted edit.
885 * @param $previousRev Revision
886 * @param $currentRev Revision
887 * @return String: HTML
889 function showDiff( $previousRev, $currentRev ) {
890 $diffEngine = new DifferenceEngine( $this->getContext() );
891 $diffEngine->showDiffStyle();
892 $this->getOutput()->addHTML(
894 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
895 "<col class='diff-marker' />" .
896 "<col class='diff-content' />" .
897 "<col class='diff-marker' />" .
898 "<col class='diff-content' />" .
900 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
901 $this->diffHeader( $previousRev, 'o' ) .
903 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
904 $this->diffHeader( $currentRev, 'n' ) .
907 $diffEngine->generateDiffBody(
908 $previousRev->getText(), $currentRev->getText() ) .
915 * @param $rev Revision
919 private function diffHeader( $rev, $prefix ) {
920 $isDeleted = !( $rev->getId() && $rev->getTitle() );
922 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
923 $targetPage = $this->getTitle();
924 $targetQuery = array(
925 'target' => $this->mTargetObj
->getPrefixedText(),
926 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
929 /// @todo FIXME: getId() may return non-zero for deleted revs...
930 $targetPage = $rev->getTitle();
931 $targetQuery = array( 'oldid' => $rev->getId() );
933 // Add show/hide deletion links if available
934 $rdel = Linker
::getRevDeleteLink( $this->getUser(), $rev, $this->mTargetObj
);
935 if ( $rdel ) $rdel = " $rdel";
937 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
943 $this->getLanguage()->timeanddate( $rev->getTimestamp(), true ),
944 $this->getLanguage()->date( $rev->getTimestamp(), true ),
945 $this->getLanguage()->time( $rev->getTimestamp(), true )
951 '<div id="mw-diff-'.$prefix.'title2">' .
952 Linker
::revUserTools( $rev ) . '<br />' .
954 '<div id="mw-diff-'.$prefix.'title3">' .
955 Linker
::revComment( $rev ) . $rdel . '<br />' .
960 * Show a form confirming whether a tokenless user really wants to see a file
962 private function showFileConfirmationForm( $key ) {
963 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
964 $this->getOutput()->addWikiMsg( 'undelete-show-file-confirm',
965 $this->mTargetObj
->getText(),
966 $this->getLanguage()->date( $file->getTimestamp() ),
967 $this->getLanguage()->time( $file->getTimestamp() ) );
968 $this->getOutput()->addHTML(
969 Xml
::openElement( 'form', array(
971 'action' => $this->getTitle()->getLocalURL(
972 'target=' . urlencode( $this->mTarget
) .
973 '&file=' . urlencode( $key ) .
974 '&token=' . urlencode( $this->getUser()->getEditToken( $key ) ) )
977 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
983 * Show a deleted file version requested by the visitor.
985 private function showFile( $key ) {
986 $this->getOutput()->disable();
988 # We mustn't allow the output to be Squid cached, otherwise
989 # if an admin previews a deleted image, and it's cached, then
990 # a user without appropriate permissions can toddle off and
991 # nab the image, and Squid will serve it
992 $response = $this->getRequest()->response();
993 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
994 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
995 $response->header( 'Pragma: no-cache' );
997 $repo = RepoGroup
::singleton()->getLocalRepo();
998 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
999 StreamFile
::stream( $path );
1002 private function showHistory() {
1003 $out = $this->getOutput();
1004 if( $this->mAllowed
) {
1005 $out->addModules( 'mediawiki.special.undelete' );
1008 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1009 array( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() )
1012 $archive = new PageArchive( $this->mTargetObj
);
1013 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1015 $text = $archive->getLastRevisionText();
1016 if( is_null( $text ) ) {
1017 $out->addWikiMsg( 'nohistory' );
1021 $out->addHTML( '<div class="mw-undelete-history">' );
1022 if ( $this->mAllowed
) {
1023 $out->addWikiMsg( 'undeletehistory' );
1024 $out->addWikiMsg( 'undeleterevdel' );
1026 $out->addWikiMsg( 'undeletehistorynoadmin' );
1028 $out->addHTML( '</div>' );
1030 # List all stored revisions
1031 $revisions = $archive->listRevisions();
1032 $files = $archive->listFiles();
1034 $haveRevisions = $revisions && $revisions->numRows() > 0;
1035 $haveFiles = $files && $files->numRows() > 0;
1037 # Batch existence check on user and talk pages
1038 if( $haveRevisions ) {
1039 $batch = new LinkBatch();
1040 foreach ( $revisions as $row ) {
1041 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1042 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1045 $revisions->seek( 0 );
1048 $batch = new LinkBatch();
1049 foreach ( $files as $row ) {
1050 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1051 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1057 if ( $this->mAllowed
) {
1058 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1059 # Start the form here
1060 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1061 $out->addHTML( $top );
1064 # Show relevant lines from the deletion log:
1065 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1066 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1067 # Show relevant lines from the suppression log:
1068 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1069 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1070 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1073 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1074 # Format the user-visible controls (comment field, submission button)
1075 # in a nice little table
1076 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1080 <td class='mw-input'>" .
1081 Xml
::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1082 'mw-undelete-unsuppress', $this->mUnsuppress
).
1086 $unsuppressBox = '';
1089 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1090 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1092 <td colspan='2' class='mw-undelete-extrahelp'>" .
1093 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1097 <td class='mw-label'>" .
1098 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1100 <td class='mw-input'>" .
1101 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1106 <td class='mw-submit'>" .
1107 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1108 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1112 Xml
::closeElement( 'table' ) .
1113 Xml
::closeElement( 'fieldset' );
1115 $out->addHTML( $table );
1118 $out->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1120 if( $haveRevisions ) {
1121 # The page's stored (deleted) history:
1122 $out->addHTML( '<ul>' );
1123 $remaining = $revisions->numRows();
1124 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1126 foreach ( $revisions as $row ) {
1128 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1131 $out->addHTML( '</ul>' );
1133 $out->addWikiMsg( 'nohistory' );
1137 $out->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1138 $out->addHTML( '<ul>' );
1139 foreach ( $files as $row ) {
1140 $out->addHTML( $this->formatFileRow( $row ) );
1143 $out->addHTML( '</ul>' );
1146 if ( $this->mAllowed
) {
1147 # Slip in the hidden controls here
1148 $misc = Html
::hidden( 'target', $this->mTarget
);
1149 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1150 $misc .= Xml
::closeElement( 'form' );
1151 $out->addHTML( $misc );
1157 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1158 $rev = Revision
::newFromArchiveRow( $row,
1159 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1161 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1162 // Build checkboxen...
1163 if( $this->mAllowed
) {
1164 if( $this->mInvert
) {
1165 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1166 $checkBox = Xml
::check( "ts$ts" );
1168 $checkBox = Xml
::check( "ts$ts", true );
1171 $checkBox = Xml
::check( "ts$ts" );
1176 // Build page & diff links...
1177 if( $this->mCanView
) {
1178 $titleObj = $this->getTitle();
1180 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1181 $pageLink = htmlspecialchars( $this->getLanguage()->timeanddate( $ts, true ) );
1182 $last = wfMsgHtml( 'diff' );
1183 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1184 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1185 $last = Linker
::linkKnown(
1187 wfMsgHtml( 'diff' ),
1190 'target' => $this->mTargetObj
->getPrefixedText(),
1196 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1197 $last = wfMsgHtml( 'diff' );
1200 $pageLink = htmlspecialchars( $this->getLanguage()->timeanddate( $ts, true ) );
1201 $last = wfMsgHtml( 'diff' );
1204 $userLink = Linker
::revUserTools( $rev );
1205 // Revision text size
1206 $size = $row->ar_len
;
1207 if( !is_null( $size ) ) {
1208 $stxt = Linker
::formatRevisionSize( $size );
1211 $comment = Linker
::revComment( $rev );
1212 // Revision delete links
1213 $revdlink = Linker
::getRevDeleteLink( $this->getUser(), $rev, $this->mTargetObj
);
1214 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1217 private function formatFileRow( $row ) {
1218 $file = ArchivedFile
::newFromRow( $row );
1220 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1221 if( $this->mAllowed
&& $row->fa_storage_key
) {
1222 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1223 $key = urlencode( $row->fa_storage_key
);
1224 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1227 $pageLink = $this->getLanguage()->timeanddate( $ts, true );
1229 $userLink = $this->getFileUser( $file );
1231 wfMsg( 'widthheight',
1232 $this->getLanguage()->formatNum( $row->fa_width
),
1233 $this->getLanguage()->formatNum( $row->fa_height
) ) .
1235 wfMsg( 'nbytes', $this->getLanguage()->formatNum( $row->fa_size
) ) .
1237 $data = htmlspecialchars( $data );
1238 $comment = $this->getFileComment( $file );
1240 // Add show/hide deletion links if available
1241 $user = $this->getUser();
1242 $canHide = $user->isAllowed( 'deleterevision' );
1243 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1244 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1245 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1248 'type' => 'filearchive',
1249 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1250 'ids' => $row->fa_id
1252 $revdlink = Linker
::revDeleteLink( $query,
1253 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1259 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1263 * Fetch revision text link if it's available to all users
1265 * @param $rev Revision
1268 function getPageLink( $rev, $titleObj, $ts ) {
1269 $time = htmlspecialchars( $this->getLanguage()->timeanddate( $ts, true ) );
1271 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1272 return '<span class="history-deleted">' . $time . '</span>';
1274 $link = Linker
::linkKnown(
1279 'target' => $this->mTargetObj
->getPrefixedText(),
1283 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1284 $link = '<span class="history-deleted">' . $link . '</span>';
1291 * Fetch image view link if it's available to all users
1294 * @return String: HTML fragment
1296 function getFileLink( $file, $titleObj, $ts, $key ) {
1297 if( !$file->userCan( File
::DELETED_FILE
, $this->getUser() ) ) {
1298 return '<span class="history-deleted">' . $this->getLanguage()->timeanddate( $ts, true ) . '</span>';
1300 $link = Linker
::linkKnown(
1302 $this->getLanguage()->timeanddate( $ts, true ),
1305 'target' => $this->mTargetObj
->getPrefixedText(),
1307 'token' => $this->getUser()->getEditToken( $key )
1310 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1311 $link = '<span class="history-deleted">' . $link . '</span>';
1318 * Fetch file's user id if it's available to this user
1321 * @return String: HTML fragment
1323 function getFileUser( $file ) {
1324 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1325 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1327 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1328 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1329 if( $file->isDeleted( File
::DELETED_USER
) ) {
1330 $link = '<span class="history-deleted">' . $link . '</span>';
1337 * Fetch file upload comment if it's available to this user
1340 * @return String: HTML fragment
1342 function getFileComment( $file ) {
1343 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1344 return '<span class="history-deleted"><span class="comment">' .
1345 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1347 $link = Linker
::commentBlock( $file->getRawDescription() );
1348 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1349 $link = '<span class="history-deleted">' . $link . '</span>';
1355 function undelete() {
1356 global $wgUploadMaintenance;
1358 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1359 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1362 if ( wfReadOnly() ) {
1363 throw new ReadOnlyError
;
1366 $out = $this->getOutput();
1367 $archive = new PageArchive( $this->mTargetObj
);
1368 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1369 $ok = $archive->undelete(
1370 $this->mTargetTimestamp
,
1372 $this->mFileVersions
,
1373 $this->mUnsuppress
);
1375 if( is_array( $ok ) ) {
1376 if ( $ok[1] ) { // Undeleted file count
1377 wfRunHooks( 'FileUndeleteComplete', array(
1378 $this->mTargetObj
, $this->mFileVersions
,
1379 $this->getUser(), $this->mComment
) );
1382 $link = Linker
::linkKnown( $this->mTargetObj
);
1383 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1385 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1386 $out->addWikiMsg( 'cannotundelete' );
1387 $out->addWikiMsg( 'undeleterevdel' );
1390 // Show file deletion warnings and errors
1391 $status = $archive->getFileStatus();
1392 if( $status && !$status->isGood() ) {
1393 $out->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );