3795e7d681587a0acbda95083dda056496664315
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 );
83 protected static function listPages( $dbr, $condition ) {
84 return $dbr->resultObject(
95 'GROUP BY' => 'ar_namespace,ar_title',
96 'ORDER BY' => 'ar_namespace,ar_title',
104 * List the revisions of the given page. Returns result wrapper with
105 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
107 * @return ResultWrapper
109 function listRevisions() {
110 $dbr = wfGetDB( DB_SLAVE
);
111 $res = $dbr->select( 'archive',
113 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
114 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id'
116 array( 'ar_namespace' => $this->title
->getNamespace(),
117 'ar_title' => $this->title
->getDBkey() ),
118 'PageArchive::listRevisions',
119 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
120 $ret = $dbr->resultObject( $res );
125 * List the deleted file revisions for this page, if it's a file page.
126 * Returns a result wrapper with various filearchive fields, or null
127 * if not a file page.
129 * @return ResultWrapper
130 * @todo Does this belong in Image for fuller encapsulation?
132 function listFiles() {
133 if( $this->title
->getNamespace() == NS_FILE
) {
134 $dbr = wfGetDB( DB_SLAVE
);
135 $res = $dbr->select( 'filearchive',
155 array( 'fa_name' => $this->title
->getDBkey() ),
157 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
158 $ret = $dbr->resultObject( $res );
165 * Return a Revision object containing data for the deleted revision.
166 * Note that the result *may* or *may not* have a null page ID.
168 * @param $timestamp String
171 function getRevision( $timestamp ) {
172 $dbr = wfGetDB( DB_SLAVE
);
173 $row = $dbr->selectRow( 'archive',
186 array( 'ar_namespace' => $this->title
->getNamespace(),
187 'ar_title' => $this->title
->getDBkey(),
188 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
191 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
198 * Return the most-previous revision, either live or deleted, against
199 * the deleted revision given by timestamp.
201 * May produce unexpected results in case of history merges or other
202 * unusual time issues.
204 * @param $timestamp String
205 * @return Revision or null
207 function getPreviousRevision( $timestamp ) {
208 $dbr = wfGetDB( DB_SLAVE
);
210 // Check the previous deleted revision...
211 $row = $dbr->selectRow( 'archive',
213 array( 'ar_namespace' => $this->title
->getNamespace(),
214 'ar_title' => $this->title
->getDBkey(),
216 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
219 'ORDER BY' => 'ar_timestamp DESC',
221 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
223 $row = $dbr->selectRow( array( 'page', 'revision' ),
224 array( 'rev_id', 'rev_timestamp' ),
226 'page_namespace' => $this->title
->getNamespace(),
227 'page_title' => $this->title
->getDBkey(),
228 'page_id = rev_page',
230 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
233 'ORDER BY' => 'rev_timestamp DESC',
235 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
236 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
238 if( $prevLive && $prevLive > $prevDeleted ) {
239 // Most prior revision was live
240 return Revision
::newFromId( $prevLiveId );
241 } elseif( $prevDeleted ) {
242 // Most prior revision was deleted
243 return $this->getRevision( $prevDeleted );
245 // No prior revision on this page.
251 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
253 * @param $row Object: database row
256 function getTextFromRow( $row ) {
257 if( is_null( $row->ar_text_id
) ) {
258 // An old row from MediaWiki 1.4 or previous.
259 // Text is embedded in this row in classic compression format.
260 return Revision
::getRevisionText( $row, 'ar_' );
262 // New-style: keyed to the text storage backend.
263 $dbr = wfGetDB( DB_SLAVE
);
264 $text = $dbr->selectRow( 'text',
265 array( 'old_text', 'old_flags' ),
266 array( 'old_id' => $row->ar_text_id
),
268 return Revision
::getRevisionText( $text );
273 * Fetch (and decompress if necessary) the stored text of the most
274 * recently edited deleted revision of the page.
276 * If there are no archived revisions for the page, returns NULL.
280 function getLastRevisionText() {
281 $dbr = wfGetDB( DB_SLAVE
);
282 $row = $dbr->selectRow( 'archive',
283 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
284 array( 'ar_namespace' => $this->title
->getNamespace(),
285 'ar_title' => $this->title
->getDBkey() ),
287 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
289 return $this->getTextFromRow( $row );
296 * Quick check if any archived revisions are present for the page.
300 function isDeleted() {
301 $dbr = wfGetDB( DB_SLAVE
);
302 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
303 array( 'ar_namespace' => $this->title
->getNamespace(),
304 'ar_title' => $this->title
->getDBkey() ) );
309 * Restore the given (or all) text and file revisions for the page.
310 * Once restored, the items will be removed from the archive tables.
311 * The deletion log will be updated with an undeletion notice.
313 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
314 * @param $comment String
315 * @param $fileVersions Array
316 * @param $unsuppress Boolean
318 * @return array(number of file revisions restored, number of image revisions restored, log message)
319 * on success, false on failure
321 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
322 // If both the set of text revisions and file revisions are empty,
323 // restore everything. Otherwise, just restore the requested items.
324 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
326 $restoreText = $restoreAll ||
!empty( $timestamps );
327 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
329 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
330 $img = wfLocalFile( $this->title
);
331 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
332 if ( !$this->fileStatus
->isOk() ) {
335 $filesRestored = $this->fileStatus
->successCount
;
341 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
342 if( $textRestored === false ) { // It must be one of UNDELETE_*
351 $log = new LogPage( 'delete' );
353 if( $textRestored && $filesRestored ) {
354 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
355 $wgContLang->formatNum( $textRestored ),
356 $wgContLang->formatNum( $filesRestored ) );
357 } elseif( $textRestored ) {
358 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
359 $wgContLang->formatNum( $textRestored ) );
360 } elseif( $filesRestored ) {
361 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
362 $wgContLang->formatNum( $filesRestored ) );
364 wfDebug( "Undelete: nothing undeleted...\n" );
368 if( trim( $comment ) != '' ) {
369 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
371 $log->addEntry( 'restore', $this->title
, $reason );
373 return array( $textRestored, $filesRestored, $reason );
377 * This is the meaty bit -- restores archived revisions of the given page
378 * to the cur/old tables. If the page currently exists, all revisions will
379 * be stuffed into old, otherwise the most recent will go into cur.
381 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
382 * @param $comment String
383 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
385 * @return Mixed: number of revisions restored or false on failure
387 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
388 if ( wfReadOnly() ) {
391 $restoreAll = empty( $timestamps );
393 $dbw = wfGetDB( DB_MASTER
);
395 # Does this page already exist? We'll have to update it...
396 $article = new Article( $this->title
);
397 $options = 'FOR UPDATE'; // lock page
398 $page = $dbw->selectRow( 'page',
399 array( 'page_id', 'page_latest' ),
400 array( 'page_namespace' => $this->title
->getNamespace(),
401 'page_title' => $this->title
->getDBkey() ),
407 # Page already exists. Import the history, and if necessary
408 # we'll update the latest revision field in the record.
410 $pageId = $page->page_id
;
411 $previousRevId = $page->page_latest
;
412 # Get the time span of this page
413 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
414 array( 'rev_id' => $previousRevId ),
416 if( $previousTimestamp === false ) {
417 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
421 # Have to create a new article...
424 $previousTimestamp = 0;
428 $oldones = '1 = 1'; # All revisions...
430 $oldts = implode( ',',
431 array_map( array( &$dbw, 'addQuotes' ),
432 array_map( array( &$dbw, 'timestamp' ),
435 $oldones = "ar_timestamp IN ( {$oldts} )";
439 * Select each archived revision...
441 $result = $dbw->select( 'archive',
456 'ar_namespace' => $this->title
->getNamespace(),
457 'ar_title' => $this->title
->getDBkey(),
460 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
462 $ret = $dbw->resultObject( $result );
463 $rev_count = $dbw->numRows( $result );
465 wfDebug( __METHOD__
. ": no revisions to restore\n" );
469 $ret->seek( $rev_count - 1 ); // move to last
470 $row = $ret->fetchObject(); // get newest archived rev
471 $ret->seek( 0 ); // move back
474 // Check the state of the newest to-be version...
475 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
476 return false; // we can't leave the current revision like this!
478 // Safe to insert now...
479 $newid = $article->insertOn( $dbw );
482 // Check if a deleted revision will become the current revision...
483 if( $row->ar_timestamp
> $previousTimestamp ) {
484 // Check the state of the newest to-be version...
485 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
486 return false; // we can't leave the current revision like this!
494 foreach ( $ret as $row ) {
495 // Check for key dupes due to shitty archive integrity.
496 if( $row->ar_rev_id
) {
497 $exists = $dbw->selectField( 'revision', '1',
498 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
500 continue; // don't throw DB errors
503 // Insert one revision at a time...maintaining deletion status
504 // unless we are specifically removing all restrictions...
505 $revision = Revision
::newFromArchiveRow( $row,
508 'deleted' => $unsuppress ?
0 : $row->ar_deleted
511 $revision->insertOn( $dbw );
514 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
516 # Now that it's safely stored, take it out of the archive
517 $dbw->delete( 'archive',
519 'ar_namespace' => $this->title
->getNamespace(),
520 'ar_title' => $this->title
->getDBkey(),
524 // Was anything restored at all?
525 if ( $restored == 0 ) {
529 // Attach the latest revision to the page...
530 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
531 if ( $newid ||
$wasnew ) {
532 // Update site stats, link tables, etc
533 $article->createUpdates( $revision );
537 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
538 Article
::onArticleCreate( $this->title
);
540 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
541 Article
::onArticleEdit( $this->title
);
544 if( $this->title
->getNamespace() == NS_FILE
) {
545 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
555 function getFileStatus() { return $this->fileStatus
; }
559 * Special page allowing users with the appropriate permissions to view
560 * and restore deleted content.
562 * @ingroup SpecialPage
564 class SpecialUndelete
extends SpecialPage
{
565 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj, $mFile;
566 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
568 function __construct( $request = null ) {
569 parent
::__construct( 'Undelete', 'deletedhistory' );
571 if ( $request === null ) {
573 $this->mRequest
= $wgRequest;
575 $this->mRequest
= $request;
579 function loadRequest() {
581 $this->mAction
= $this->mRequest
->getVal( 'action' );
582 $this->mTarget
= $this->mRequest
->getVal( 'target' );
583 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
584 $time = $this->mRequest
->getVal( 'timestamp' );
585 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
586 $this->mFile
= $this->mRequest
->getVal( 'file' );
588 $posted = $this->mRequest
->wasPosted() &&
589 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
590 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
591 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
592 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
593 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
594 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
595 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
596 $this->mToken
= $this->mRequest
->getVal( 'token' );
598 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
599 $this->mAllowed
= true; // user can restore
600 $this->mCanView
= true; // user can view content
601 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
602 $this->mAllowed
= false; // user cannot restore
603 $this->mCanView
= true; // user can view content
604 } else { // user can only view the list of revisions
605 $this->mAllowed
= false;
606 $this->mCanView
= false;
607 $this->mTimestamp
= '';
608 $this->mRestore
= false;
611 if( $this->mRestore ||
$this->mInvert
) {
612 $timestamps = array();
613 $this->mFileVersions
= array();
614 foreach( $_REQUEST as $key => $val ) {
616 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
617 array_push( $timestamps, $matches[1] );
620 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
621 $this->mFileVersions
[] = intval( $matches[1] );
624 rsort( $timestamps );
625 $this->mTargetTimestamp
= $timestamps;
629 function execute( $par ) {
630 global $wgOut, $wgUser;
633 if ( !$this->userCanExecute( $wgUser ) ) {
634 $this->displayRestrictionError();
637 $this->outputHeader();
639 $this->loadRequest();
641 if ( $this->mAllowed
) {
642 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
644 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
648 $this->mTarget
= $par;
650 if ( $this->mTarget
!== '' ) {
651 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
652 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj
);
654 $this->mTargetObj
= null;
657 if( is_null( $this->mTargetObj
) ) {
658 # Not all users can just browse every deleted page from the list
659 if( $wgUser->isAllowed( 'browsearchive' ) ) {
660 $this->showSearchForm();
662 # List undeletable articles
663 if( $this->mSearchPrefix
) {
664 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
665 $this->showList( $result );
668 $wgOut->addWikiMsg( 'undelete-header' );
672 if( $this->mTimestamp
!== '' ) {
673 return $this->showRevision( $this->mTimestamp
);
675 if( $this->mFile
!== null ) {
676 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
677 // Check if user is allowed to see this file
678 if ( !$file->exists() ) {
679 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile
);
681 } else if( !$file->userCan( File
::DELETED_FILE
) ) {
682 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
683 $wgOut->permissionRequired( 'suppressrevision' );
685 $wgOut->permissionRequired( 'deletedtext' );
688 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
689 $this->showFileConfirmationForm( $this->mFile
);
692 return $this->showFile( $this->mFile
);
695 if( $this->mRestore
&& $this->mAction
== 'submit' ) {
696 global $wgUploadMaintenance;
697 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
698 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
701 return $this->undelete();
703 if( $this->mInvert
&& $this->mAction
== 'submit' ) {
704 return $this->showHistory();
706 return $this->showHistory();
709 function showSearchForm() {
710 global $wgOut, $wgScript;
711 $wgOut->addWikiMsg( 'undelete-header' );
714 Xml
::openElement( 'form', array(
716 'action' => $wgScript ) ) .
717 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
718 Html
::hidden( 'title',
719 $this->getTitle()->getPrefixedDbKey() ) .
720 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
721 'prefix', 'prefix', 20,
722 $this->mSearchPrefix
) . ' ' .
723 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
724 Xml
::closeElement( 'fieldset' ) .
725 Xml
::closeElement( 'form' )
729 // Generic list of deleted pages
730 private function showList( $result ) {
731 global $wgLang, $wgUser, $wgOut;
733 if( $result->numRows() == 0 ) {
734 $wgOut->addWikiMsg( 'undelete-no-results' );
738 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
740 $sk = $wgUser->getSkin();
741 $undelete = $this->getTitle();
742 $wgOut->addHTML( "<ul>\n" );
743 foreach ( $result as $row ) {
744 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
745 $link = $sk->linkKnown(
747 htmlspecialchars( $title->getPrefixedText() ),
749 array( 'target' => $title->getPrefixedText() )
751 $revs = wfMsgExt( 'undeleterevisions',
752 array( 'parseinline' ),
753 $wgLang->formatNum( $row->count
) );
754 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
757 $wgOut->addHTML( "</ul>\n" );
762 private function showRevision( $timestamp ) {
763 global $wgLang, $wgUser, $wgOut;
765 $skin = $wgUser->getSkin();
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 );
776 $wgOut->addWikiMsg( 'undeleterevision-missing' );
780 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
781 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
782 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
785 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
786 $wgOut->addHTML( '<br />' );
787 // and we are allowed to see...
791 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
793 $link = $skin->linkKnown(
794 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
795 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
799 $previousRev = $archive->getPreviousRevision( $timestamp );
801 $this->showDiff( $previousRev, $rev );
802 if( $wgUser->getOption( 'diffonly' ) ) {
805 $wgOut->addHTML( '<hr />' );
808 $wgOut->addWikiMsg( 'undelete-nodiff' );
812 // date and time are separate parameters to facilitate localisation.
813 // $time is kept for backward compat reasons.
814 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
815 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
816 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
817 $user = $skin->revUserTools( $rev );
819 if( $this->mPreview
) {
820 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
822 $openDiv = '<div id="mw-undelete-revision">';
824 $wgOut->addHTML( $openDiv );
826 // Revision delete links
827 if ( !$this->mDiff
) {
828 $revdel = $this->revDeleteLink( $rev );
830 $wgOut->addHTML( $revdel );
834 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
835 $wgOut->addHTML( '</div>' );
836 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
838 if( $this->mPreview
) {
840 $popts = $wgOut->parserOptions();
841 $popts->setEditSection( false );
842 $wgOut->parserOptions( $popts );
843 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
847 Xml
::element( 'textarea', array(
848 'readonly' => 'readonly',
849 'cols' => intval( $wgUser->getOption( 'cols' ) ),
850 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
851 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
852 Xml
::openElement( 'div' ) .
853 Xml
::openElement( 'form', array(
855 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
856 Xml
::element( 'input', array(
859 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
860 Xml
::element( 'input', array(
862 'name' => 'timestamp',
863 'value' => $timestamp ) ) .
864 Xml
::element( 'input', array(
866 'name' => 'wpEditToken',
867 'value' => $wgUser->editToken() ) ) .
868 Xml
::element( 'input', array(
871 'value' => wfMsg( 'showpreview' ) ) ) .
872 Xml
::element( 'input', array(
875 'value' => wfMsg( 'showdiff' ) ) ) .
876 Xml
::closeElement( 'form' ) .
877 Xml
::closeElement( 'div' ) );
881 * Get a revision-deletion link, or disabled link, or nothing, depending
882 * on user permissions & the settings on the revision.
884 * Will use forward-compatible revision ID in the Special:RevDelete link
885 * if possible, otherwise the timestamp-based ID which may break after
888 * @param Revision $rev
889 * @return string HTML fragment
891 function revDeleteLink( $rev ) {
893 $canHide = $wgUser->isAllowed( 'deleterevision' );
894 if( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
895 $skin = $wgUser->getSkin();
896 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
897 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
899 if ( $rev->getId() ) {
900 // RevDelete links using revision ID are stable across
901 // page deletion and undeletion; use when possible.
903 'type' => 'revision',
904 'target' => $this->mTargetObj
->getPrefixedDBkey(),
905 'ids' => $rev->getId()
908 // Older deleted entries didn't save a revision ID.
909 // We have to refer to these by timestamp, ick!
912 'target' => $this->mTargetObj
->getPrefixedDBkey(),
913 'ids' => $rev->getTimestamp()
916 return $skin->revDeleteLink( $query,
917 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
925 * Build a diff display between this and the previous either deleted
926 * or non-deleted edit.
928 * @param $previousRev Revision
929 * @param $currentRev Revision
930 * @return String: HTML
932 function showDiff( $previousRev, $currentRev ) {
935 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
936 $diffEngine->showDiffStyle();
939 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
940 "<col class='diff-marker' />" .
941 "<col class='diff-content' />" .
942 "<col class='diff-marker' />" .
943 "<col class='diff-content' />" .
945 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
946 $this->diffHeader( $previousRev, 'o' ) .
948 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
949 $this->diffHeader( $currentRev, 'n' ) .
952 $diffEngine->generateDiffBody(
953 $previousRev->getText(), $currentRev->getText() ) .
960 * @param $rev Revision
964 private function diffHeader( $rev, $prefix ) {
965 global $wgUser, $wgLang;
966 $sk = $wgUser->getSkin();
967 $isDeleted = !( $rev->getId() && $rev->getTitle() );
969 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
970 $targetPage = $this->getTitle();
971 $targetQuery = array(
972 'target' => $this->mTargetObj
->getPrefixedText(),
973 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
976 /// @todo Fixme getId() may return non-zero for deleted revs...
977 $targetPage = $rev->getTitle();
978 $targetQuery = array( 'oldid' => $rev->getId() );
980 // Add show/hide deletion links if available
981 $del .= $this->revDeleteLink( $rev );
983 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
988 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
989 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
990 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
996 '<div id="mw-diff-'.$prefix.'title2">' .
997 $sk->revUserTools( $rev ) . '<br />' .
999 '<div id="mw-diff-'.$prefix.'title3">' .
1000 $sk->revComment( $rev ) . $del . '<br />' .
1005 * Show a form confirming whether a tokenless user really wants to see a file
1007 private function showFileConfirmationForm( $key ) {
1008 global $wgOut, $wgUser, $wgLang;
1009 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
1010 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
1011 $this->mTargetObj
->getText(),
1012 $wgLang->date( $file->getTimestamp() ),
1013 $wgLang->time( $file->getTimestamp() ) );
1015 Xml
::openElement( 'form', array(
1017 'action' => $this->getTitle()->getLocalURL(
1018 'target=' . urlencode( $this->mTarget
) .
1019 '&file=' . urlencode( $key ) .
1020 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
1023 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1029 * Show a deleted file version requested by the visitor.
1031 private function showFile( $key ) {
1032 global $wgOut, $wgRequest;
1035 # We mustn't allow the output to be Squid cached, otherwise
1036 # if an admin previews a deleted image, and it's cached, then
1037 # a user without appropriate permissions can toddle off and
1038 # nab the image, and Squid will serve it
1039 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1040 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1041 $wgRequest->response()->header( 'Pragma: no-cache' );
1044 require_once( "$IP/includes/StreamFile.php" );
1045 $repo = RepoGroup
::singleton()->getLocalRepo();
1046 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1047 wfStreamFile( $path );
1050 private function showHistory() {
1051 global $wgUser, $wgOut;
1053 $sk = $wgUser->getSkin();
1054 if( $this->mAllowed
) {
1055 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
1057 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
1060 $wgOut->wrapWikiMsg(
1061 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1062 array( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() )
1065 $archive = new PageArchive( $this->mTargetObj
);
1066 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1068 $text = $archive->getLastRevisionText();
1069 if( is_null( $text ) ) {
1070 $wgOut->addWikiMsg( 'nohistory' );
1074 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1075 if ( $this->mAllowed
) {
1076 $wgOut->addWikiMsg( 'undeletehistory' );
1077 $wgOut->addWikiMsg( 'undeleterevdel' );
1079 $wgOut->addWikiMsg( 'undeletehistorynoadmin' );
1081 $wgOut->addHTML( '</div>' );
1083 # List all stored revisions
1084 $revisions = $archive->listRevisions();
1085 $files = $archive->listFiles();
1087 $haveRevisions = $revisions && $revisions->numRows() > 0;
1088 $haveFiles = $files && $files->numRows() > 0;
1090 # Batch existence check on user and talk pages
1091 if( $haveRevisions ) {
1092 $batch = new LinkBatch();
1093 foreach ( $revisions as $row ) {
1094 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1095 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1098 $revisions->seek( 0 );
1101 $batch = new LinkBatch();
1102 foreach ( $files as $row ) {
1103 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1104 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1110 if ( $this->mAllowed
) {
1111 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1112 # Start the form here
1113 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1114 $wgOut->addHTML( $top );
1117 # Show relevant lines from the deletion log:
1118 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1119 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1120 # Show relevant lines from the suppression log:
1121 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1122 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1123 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1126 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1127 # Format the user-visible controls (comment field, submission button)
1128 # in a nice little table
1129 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1133 <td class='mw-input'>" .
1134 Xml
::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1135 'mw-undelete-unsuppress', $this->mUnsuppress
).
1139 $unsuppressBox = '';
1142 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1143 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1145 <td colspan='2' class='mw-undelete-extrahelp'>" .
1146 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1150 <td class='mw-label'>" .
1151 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1153 <td class='mw-input'>" .
1154 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1159 <td class='mw-submit'>" .
1160 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1161 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1162 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1166 Xml
::closeElement( 'table' ) .
1167 Xml
::closeElement( 'fieldset' );
1169 $wgOut->addHTML( $table );
1172 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1174 if( $haveRevisions ) {
1175 # The page's stored (deleted) history:
1176 $wgOut->addHTML( '<ul>' );
1177 $remaining = $revisions->numRows();
1178 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1180 foreach ( $revisions as $row ) {
1182 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1185 $wgOut->addHTML( '</ul>' );
1187 $wgOut->addWikiMsg( 'nohistory' );
1191 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1192 $wgOut->addHTML( '<ul>' );
1193 foreach ( $files as $row ) {
1194 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1197 $wgOut->addHTML( '</ul>' );
1200 if ( $this->mAllowed
) {
1201 # Slip in the hidden controls here
1202 $misc = Html
::hidden( 'target', $this->mTarget
);
1203 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
1204 $misc .= Xml
::closeElement( 'form' );
1205 $wgOut->addHTML( $misc );
1211 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1214 $rev = Revision
::newFromArchiveRow( $row,
1215 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1217 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1218 // Build checkboxen...
1219 if( $this->mAllowed
) {
1220 if( $this->mInvert
) {
1221 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1222 $checkBox = Xml
::check( "ts$ts" );
1224 $checkBox = Xml
::check( "ts$ts", true );
1227 $checkBox = Xml
::check( "ts$ts" );
1232 // Build page & diff links...
1233 if( $this->mCanView
) {
1234 $titleObj = $this->getTitle();
1236 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1237 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1238 $last = wfMsgHtml( 'diff' );
1239 } else if( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1240 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1241 $last = $sk->linkKnown(
1243 wfMsgHtml( 'diff' ),
1246 'target' => $this->mTargetObj
->getPrefixedText(),
1252 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1253 $last = wfMsgHtml( 'diff' );
1256 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1257 $last = wfMsgHtml( 'diff' );
1260 $userLink = $sk->revUserTools( $rev );
1261 // Revision text size
1262 $size = $row->ar_len
;
1263 if( !is_null( $size ) ) {
1264 $stxt = $sk->formatRevisionSize( $size );
1267 $comment = $sk->revComment( $rev );
1268 // Revision delete links
1269 $revdlink = $this->revDeleteLink( $rev );
1270 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1273 private function formatFileRow( $row, $sk ) {
1274 global $wgUser, $wgLang;
1276 $file = ArchivedFile
::newFromRow( $row );
1278 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1279 if( $this->mAllowed
&& $row->fa_storage_key
) {
1280 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1281 $key = urlencode( $row->fa_storage_key
);
1282 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1285 $pageLink = $wgLang->timeanddate( $ts, true );
1287 $userLink = $this->getFileUser( $file, $sk );
1289 wfMsg( 'widthheight',
1290 $wgLang->formatNum( $row->fa_width
),
1291 $wgLang->formatNum( $row->fa_height
) ) .
1293 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1295 $data = htmlspecialchars( $data );
1296 $comment = $this->getFileComment( $file, $sk );
1297 // Add show/hide deletion links if available
1298 $canHide = $wgUser->isAllowed( 'deleterevision' );
1299 if( $canHide ||
( $file->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
1300 if( !$file->userCan( File
::DELETED_RESTRICTED
) ) {
1301 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1304 'type' => 'filearchive',
1305 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1306 'ids' => $row->fa_id
1308 $revdlink = $sk->revDeleteLink( $query,
1309 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1314 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1318 * Fetch revision text link if it's available to all users
1320 * @param $rev Revision
1324 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1327 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1329 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1330 return '<span class="history-deleted">' . $time . '</span>';
1332 $link = $sk->linkKnown(
1337 'target' => $this->mTargetObj
->getPrefixedText(),
1341 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1342 $link = '<span class="history-deleted">' . $link . '</span>';
1349 * Fetch image view link if it's available to all users
1353 * @return String: HTML fragment
1355 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1356 global $wgLang, $wgUser;
1358 if( !$file->userCan( File
::DELETED_FILE
) ) {
1359 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1361 $link = $sk->linkKnown(
1363 $wgLang->timeanddate( $ts, true ),
1366 'target' => $this->mTargetObj
->getPrefixedText(),
1368 'token' => $wgUser->editToken( $key )
1371 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1372 $link = '<span class="history-deleted">' . $link . '</span>';
1379 * Fetch file's user id if it's available to this user
1383 * @return String: HTML fragment
1385 function getFileUser( $file, $sk ) {
1386 if( !$file->userCan( File
::DELETED_USER
) ) {
1387 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1389 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1390 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1391 if( $file->isDeleted( File
::DELETED_USER
) ) {
1392 $link = '<span class="history-deleted">' . $link . '</span>';
1399 * Fetch file upload comment if it's available to this user
1403 * @return String: HTML fragment
1405 function getFileComment( $file, $sk ) {
1406 if( !$file->userCan( File
::DELETED_COMMENT
) ) {
1407 return '<span class="history-deleted"><span class="comment">' .
1408 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1410 $link = $sk->commentBlock( $file->getRawDescription() );
1411 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1412 $link = '<span class="history-deleted">' . $link . '</span>';
1418 function undelete() {
1419 global $wgOut, $wgUser;
1420 if ( wfReadOnly() ) {
1421 $wgOut->readOnlyPage();
1424 if( !is_null( $this->mTargetObj
) ) {
1425 $archive = new PageArchive( $this->mTargetObj
);
1426 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1427 $ok = $archive->undelete(
1428 $this->mTargetTimestamp
,
1430 $this->mFileVersions
,
1431 $this->mUnsuppress
);
1433 if( is_array( $ok ) ) {
1434 if ( $ok[1] ) { // Undeleted file count
1435 wfRunHooks( 'FileUndeleteComplete', array(
1436 $this->mTargetObj
, $this->mFileVersions
,
1437 $wgUser, $this->mComment
) );
1440 $skin = $wgUser->getSkin();
1441 $link = $skin->linkKnown( $this->mTargetObj
);
1442 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1444 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );
1445 $wgOut->addWikiMsg( 'undeleterevdel' );
1448 // Show file deletion warnings and errors
1449 $status = $archive->getFileStatus();
1450 if( $status && !$status->isGood() ) {
1451 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1454 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );