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',
191 array( 'ar_namespace' => $this->title
->getNamespace(),
192 'ar_title' => $this->title
->getDBkey(),
193 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
196 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
203 * Return the most-previous revision, either live or deleted, against
204 * the deleted revision given by timestamp.
206 * May produce unexpected results in case of history merges or other
207 * unusual time issues.
209 * @param $timestamp String
210 * @return Revision or null
212 function getPreviousRevision( $timestamp ) {
213 $dbr = wfGetDB( DB_SLAVE
);
215 // Check the previous deleted revision...
216 $row = $dbr->selectRow( 'archive',
218 array( 'ar_namespace' => $this->title
->getNamespace(),
219 'ar_title' => $this->title
->getDBkey(),
221 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
224 'ORDER BY' => 'ar_timestamp DESC',
226 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
228 $row = $dbr->selectRow( array( 'page', 'revision' ),
229 array( 'rev_id', 'rev_timestamp' ),
231 'page_namespace' => $this->title
->getNamespace(),
232 'page_title' => $this->title
->getDBkey(),
233 'page_id = rev_page',
235 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
238 'ORDER BY' => 'rev_timestamp DESC',
240 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
241 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
243 if( $prevLive && $prevLive > $prevDeleted ) {
244 // Most prior revision was live
245 return Revision
::newFromId( $prevLiveId );
246 } elseif( $prevDeleted ) {
247 // Most prior revision was deleted
248 return $this->getRevision( $prevDeleted );
250 // No prior revision on this page.
256 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
258 * @param $row Object: database row
261 function getTextFromRow( $row ) {
262 if( is_null( $row->ar_text_id
) ) {
263 // An old row from MediaWiki 1.4 or previous.
264 // Text is embedded in this row in classic compression format.
265 return Revision
::getRevisionText( $row, 'ar_' );
267 // New-style: keyed to the text storage backend.
268 $dbr = wfGetDB( DB_SLAVE
);
269 $text = $dbr->selectRow( 'text',
270 array( 'old_text', 'old_flags' ),
271 array( 'old_id' => $row->ar_text_id
),
273 return Revision
::getRevisionText( $text );
278 * Fetch (and decompress if necessary) the stored text of the most
279 * recently edited deleted revision of the page.
281 * If there are no archived revisions for the page, returns NULL.
285 function getLastRevisionText() {
286 $dbr = wfGetDB( DB_SLAVE
);
287 $row = $dbr->selectRow( 'archive',
288 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
289 array( 'ar_namespace' => $this->title
->getNamespace(),
290 'ar_title' => $this->title
->getDBkey() ),
292 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
294 return $this->getTextFromRow( $row );
301 * Quick check if any archived revisions are present for the page.
305 function isDeleted() {
306 $dbr = wfGetDB( DB_SLAVE
);
307 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
308 array( 'ar_namespace' => $this->title
->getNamespace(),
309 'ar_title' => $this->title
->getDBkey() ) );
314 * Restore the given (or all) text and file revisions for the page.
315 * Once restored, the items will be removed from the archive tables.
316 * The deletion log will be updated with an undeletion notice.
318 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
319 * @param $comment String
320 * @param $fileVersions Array
321 * @param $unsuppress Boolean
323 * @return array(number of file revisions restored, number of image revisions restored, log message)
324 * on success, false on failure
326 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
327 // If both the set of text revisions and file revisions are empty,
328 // restore everything. Otherwise, just restore the requested items.
329 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
331 $restoreText = $restoreAll ||
!empty( $timestamps );
332 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
334 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
335 $img = wfLocalFile( $this->title
);
336 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
337 if ( !$this->fileStatus
->isOk() ) {
340 $filesRestored = $this->fileStatus
->successCount
;
346 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
347 if( $textRestored === false ) { // It must be one of UNDELETE_*
356 $log = new LogPage( 'delete' );
358 if( $textRestored && $filesRestored ) {
359 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
360 $wgContLang->formatNum( $textRestored ),
361 $wgContLang->formatNum( $filesRestored ) );
362 } elseif( $textRestored ) {
363 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
364 $wgContLang->formatNum( $textRestored ) );
365 } elseif( $filesRestored ) {
366 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
367 $wgContLang->formatNum( $filesRestored ) );
369 wfDebug( "Undelete: nothing undeleted...\n" );
373 if( trim( $comment ) != '' ) {
374 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
376 $log->addEntry( 'restore', $this->title
, $reason );
378 return array( $textRestored, $filesRestored, $reason );
382 * This is the meaty bit -- restores archived revisions of the given page
383 * to the cur/old tables. If the page currently exists, all revisions will
384 * be stuffed into old, otherwise the most recent will go into cur.
386 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
387 * @param $comment String
388 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
390 * @return Mixed: number of revisions restored or false on failure
392 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
393 if ( wfReadOnly() ) {
396 $restoreAll = empty( $timestamps );
398 $dbw = wfGetDB( DB_MASTER
);
400 # Does this page already exist? We'll have to update it...
401 $article = WikiPage
::factory( $this->title
);
402 # Load latest data for the current page (bug 31179)
403 $article->loadPageData( 'fromdbmaster' );
404 $oldcountable = $article->isCountable();
406 $options = 'FOR UPDATE'; // lock page
407 $page = $dbw->selectRow( 'page',
408 array( 'page_id', 'page_latest' ),
409 array( 'page_namespace' => $this->title
->getNamespace(),
410 'page_title' => $this->title
->getDBkey() ),
416 # Page already exists. Import the history, and if necessary
417 # we'll update the latest revision field in the record.
419 $pageId = $page->page_id
;
420 $previousRevId = $page->page_latest
;
421 # Get the time span of this page
422 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
423 array( 'rev_id' => $previousRevId ),
425 if( $previousTimestamp === false ) {
426 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
430 # Have to create a new article...
433 $previousTimestamp = 0;
437 $oldones = '1 = 1'; # All revisions...
439 $oldts = implode( ',',
440 array_map( array( &$dbw, 'addQuotes' ),
441 array_map( array( &$dbw, 'timestamp' ),
444 $oldones = "ar_timestamp IN ( {$oldts} )";
448 * Select each archived revision...
450 $result = $dbw->select( 'archive',
466 'ar_namespace' => $this->title
->getNamespace(),
467 'ar_title' => $this->title
->getDBkey(),
470 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
472 $ret = $dbw->resultObject( $result );
473 $rev_count = $dbw->numRows( $result );
475 wfDebug( __METHOD__
. ": no revisions to restore\n" );
479 $ret->seek( $rev_count - 1 ); // move to last
480 $row = $ret->fetchObject(); // get newest archived rev
481 $ret->seek( 0 ); // move back
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!
488 // Safe to insert now...
489 $newid = $article->insertOn( $dbw );
492 // Check if a deleted revision will become the current revision...
493 if( $row->ar_timestamp
> $previousTimestamp ) {
494 // Check the state of the newest to-be version...
495 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
496 return false; // we can't leave the current revision like this!
504 foreach ( $ret as $row ) {
505 // Check for key dupes due to shitty archive integrity.
506 if( $row->ar_rev_id
) {
507 $exists = $dbw->selectField( 'revision', '1',
508 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
510 continue; // don't throw DB errors
513 // Insert one revision at a time...maintaining deletion status
514 // unless we are specifically removing all restrictions...
515 $revision = Revision
::newFromArchiveRow( $row,
518 'deleted' => $unsuppress ?
0 : $row->ar_deleted
521 $revision->insertOn( $dbw );
524 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
526 # Now that it's safely stored, take it out of the archive
527 $dbw->delete( 'archive',
529 'ar_namespace' => $this->title
->getNamespace(),
530 'ar_title' => $this->title
->getDBkey(),
534 // Was anything restored at all?
535 if ( $restored == 0 ) {
539 $created = (bool)$newid;
541 // Attach the latest revision to the page...
542 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
543 if ( $created ||
$wasnew ) {
544 // Update site stats, link tables, etc
545 $user = User
::newFromName( $revision->getRawUserText(), false );
546 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
549 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
551 if( $this->title
->getNamespace() == NS_FILE
) {
552 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
562 function getFileStatus() { return $this->fileStatus
; }
566 * Special page allowing users with the appropriate permissions to view
567 * and restore deleted content.
569 * @ingroup SpecialPage
571 class SpecialUndelete
extends SpecialPage
{
572 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
573 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
580 function __construct() {
581 parent
::__construct( 'Undelete', 'deletedhistory' );
584 function loadRequest( $par ) {
585 $request = $this->getRequest();
586 $user = $this->getUser();
588 $this->mAction
= $request->getVal( 'action' );
589 if ( $par !== null && $par !== '' ) {
590 $this->mTarget
= $par;
592 $this->mTarget
= $request->getVal( 'target' );
594 $this->mTargetObj
= null;
595 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
596 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
598 $this->mSearchPrefix
= $request->getText( 'prefix' );
599 $time = $request->getVal( 'timestamp' );
600 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
601 $this->mFilename
= $request->getVal( 'file' );
603 $posted = $request->wasPosted() &&
604 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
605 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
606 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
607 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
608 $this->mDiff
= $request->getCheck( 'diff' );
609 $this->mComment
= $request->getText( 'wpComment' );
610 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
611 $this->mToken
= $request->getVal( 'token' );
613 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
614 $this->mAllowed
= true; // user can restore
615 $this->mCanView
= true; // user can view content
616 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
617 $this->mAllowed
= false; // user cannot restore
618 $this->mCanView
= true; // user can view content
619 $this->mRestore
= false;
620 } else { // user can only view the list of revisions
621 $this->mAllowed
= false;
622 $this->mCanView
= false;
623 $this->mTimestamp
= '';
624 $this->mRestore
= false;
627 if( $this->mRestore ||
$this->mInvert
) {
628 $timestamps = array();
629 $this->mFileVersions
= array();
630 foreach( $request->getValues() as $key => $val ) {
632 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
633 array_push( $timestamps, $matches[1] );
636 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
637 $this->mFileVersions
[] = intval( $matches[1] );
640 rsort( $timestamps );
641 $this->mTargetTimestamp
= $timestamps;
645 function execute( $par ) {
646 $this->checkPermissions();
647 $user = $this->getUser();
650 $this->outputHeader();
652 $this->loadRequest( $par );
654 $out = $this->getOutput();
656 if ( is_null( $this->mTargetObj
) ) {
657 $out->addWikiMsg( 'undelete-header' );
659 # Not all users can just browse every deleted page from the list
660 if ( $user->isAllowed( 'browsearchive' ) ) {
661 $this->showSearchForm();
666 if ( $this->mAllowed
) {
667 $out->setPageTitle( $this->msg( 'undeletepage' ) );
669 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
672 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
674 if ( $this->mTimestamp
!== '' ) {
675 $this->showRevision( $this->mTimestamp
);
676 } elseif ( $this->mFilename
!== null ) {
677 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
678 // Check if user is allowed to see this file
679 if ( !$file->exists() ) {
680 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
681 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
682 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
683 throw new PermissionsError( 'suppressrevision' );
685 throw new PermissionsError( 'deletedtext' );
687 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
688 $this->showFileConfirmationForm( $this->mFilename
);
690 $this->showFile( $this->mFilename
);
692 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
695 $this->showHistory();
699 function showSearchForm() {
702 $out = $this->getOutput();
703 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
705 Xml
::openElement( 'form', array(
707 'action' => $wgScript ) ) .
708 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
709 Html
::hidden( 'title',
710 $this->getTitle()->getPrefixedDbKey() ) .
711 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
712 'prefix', 'prefix', 20,
713 $this->mSearchPrefix
) . ' ' .
714 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
715 Xml
::closeElement( 'fieldset' ) .
716 Xml
::closeElement( 'form' )
719 # List undeletable articles
720 if( $this->mSearchPrefix
) {
721 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
722 $this->showList( $result );
727 * Generic list of deleted pages
729 * @param $result ResultWrapper
732 private function showList( $result ) {
733 $out = $this->getOutput();
735 if( $result->numRows() == 0 ) {
736 $out->addWikiMsg( 'undelete-no-results' );
740 $out->addWikiMsg( 'undeletepagetext', $this->getLang()->formatNum( $result->numRows() ) );
742 $undelete = $this->getTitle();
743 $out->addHTML( "<ul>\n" );
744 foreach ( $result as $row ) {
745 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
746 $link = Linker
::linkKnown(
748 htmlspecialchars( $title->getPrefixedText() ),
750 array( 'target' => $title->getPrefixedText() )
752 $revs = wfMsgExt( 'undeleterevisions',
753 array( 'parseinline' ),
754 $this->getLang()->formatNum( $row->count
) );
755 $out->addHTML( "<li>{$link} ({$revs})</li>\n" );
758 $out->addHTML( "</ul>\n" );
763 private function showRevision( $timestamp ) {
764 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
768 $archive = new PageArchive( $this->mTargetObj
);
769 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
770 $rev = $archive->getRevision( $timestamp );
772 $out = $this->getOutput();
773 $user = $this->getUser();
776 $out->addWikiMsg( 'undeleterevision-missing' );
780 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
781 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
782 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
785 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
786 $out->addHTML( '<br />' );
787 // and we are allowed to see...
792 $previousRev = $archive->getPreviousRevision( $timestamp );
794 $this->showDiff( $previousRev, $rev );
795 if( $user->getOption( 'diffonly' ) ) {
798 $out->addHTML( '<hr />' );
801 $out->addWikiMsg( 'undelete-nodiff' );
805 $link = Linker
::linkKnown(
806 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
807 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
810 // date and time are separate parameters to facilitate localisation.
811 // $time is kept for backward compat reasons.
812 $time = $this->getLang()->timeAndDate( $timestamp, true );
813 $d = $this->getLang()->date( $timestamp, true );
814 $t = $this->getLang()->time( $timestamp, true );
815 $userLink = Linker
::revUserTools( $rev );
817 if( $this->mPreview
) {
818 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
820 $openDiv = '<div id="mw-undelete-revision">';
822 $out->addHTML( $openDiv );
824 // Revision delete links
825 if ( !$this->mDiff
) {
826 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
828 $out->addHTML( "$revdel " );
832 $out->addHTML( wfMessage( 'undelete-revision' )->rawParams( $link )->params(
833 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
834 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
836 if( $this->mPreview
) {
838 $popts = $out->parserOptions();
839 $popts->setEditSection( false );
840 $out->parserOptions( $popts );
841 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
845 Xml
::element( 'textarea', array(
846 'readonly' => 'readonly',
847 'cols' => intval( $user->getOption( 'cols' ) ),
848 'rows' => intval( $user->getOption( 'rows' ) ) ),
849 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) .
850 Xml
::openElement( 'div' ) .
851 Xml
::openElement( 'form', array(
853 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
854 Xml
::element( 'input', array(
857 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
858 Xml
::element( 'input', array(
860 'name' => 'timestamp',
861 'value' => $timestamp ) ) .
862 Xml
::element( 'input', array(
864 'name' => 'wpEditToken',
865 'value' => $user->editToken() ) ) .
866 Xml
::element( 'input', array(
869 'value' => wfMsg( 'showpreview' ) ) ) .
870 Xml
::element( 'input', array(
873 'value' => wfMsg( 'showdiff' ) ) ) .
874 Xml
::closeElement( 'form' ) .
875 Xml
::closeElement( 'div' ) );
879 * Build a diff display between this and the previous either deleted
880 * or non-deleted edit.
882 * @param $previousRev Revision
883 * @param $currentRev Revision
884 * @return String: HTML
886 function showDiff( $previousRev, $currentRev ) {
887 $diffEngine = new DifferenceEngine( $this->getContext() );
888 $diffEngine->showDiffStyle();
889 $this->getOutput()->addHTML(
891 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
892 "<col class='diff-marker' />" .
893 "<col class='diff-content' />" .
894 "<col class='diff-marker' />" .
895 "<col class='diff-content' />" .
897 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
898 $this->diffHeader( $previousRev, 'o' ) .
900 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
901 $this->diffHeader( $currentRev, 'n' ) .
904 $diffEngine->generateDiffBody(
905 $previousRev->getText(), $currentRev->getText() ) .
912 * @param $rev Revision
916 private function diffHeader( $rev, $prefix ) {
917 $isDeleted = !( $rev->getId() && $rev->getTitle() );
919 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
920 $targetPage = $this->getTitle();
921 $targetQuery = array(
922 'target' => $this->mTargetObj
->getPrefixedText(),
923 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
926 /// @todo FIXME: getId() may return non-zero for deleted revs...
927 $targetPage = $rev->getTitle();
928 $targetQuery = array( 'oldid' => $rev->getId() );
930 // Add show/hide deletion links if available
931 $rdel = Linker
::getRevDeleteLink( $this->getUser(), $rev, $this->mTargetObj
);
932 if ( $rdel ) $rdel = " $rdel";
934 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
940 $this->getLang()->timeanddate( $rev->getTimestamp(), true ),
941 $this->getLang()->date( $rev->getTimestamp(), true ),
942 $this->getLang()->time( $rev->getTimestamp(), true )
948 '<div id="mw-diff-'.$prefix.'title2">' .
949 Linker
::revUserTools( $rev ) . '<br />' .
951 '<div id="mw-diff-'.$prefix.'title3">' .
952 Linker
::revComment( $rev ) . $rdel . '<br />' .
957 * Show a form confirming whether a tokenless user really wants to see a file
959 private function showFileConfirmationForm( $key ) {
960 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
961 $this->getOutput()->addWikiMsg( 'undelete-show-file-confirm',
962 $this->mTargetObj
->getText(),
963 $this->getLang()->date( $file->getTimestamp() ),
964 $this->getLang()->time( $file->getTimestamp() ) );
965 $this->getOutput()->addHTML(
966 Xml
::openElement( 'form', array(
968 'action' => $this->getTitle()->getLocalURL(
969 'target=' . urlencode( $this->mTarget
) .
970 '&file=' . urlencode( $key ) .
971 '&token=' . urlencode( $this->getUser()->editToken( $key ) ) )
974 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
980 * Show a deleted file version requested by the visitor.
982 private function showFile( $key ) {
983 $this->getOutput()->disable();
985 # We mustn't allow the output to be Squid cached, otherwise
986 # if an admin previews a deleted image, and it's cached, then
987 # a user without appropriate permissions can toddle off and
988 # nab the image, and Squid will serve it
989 $response = $this->getRequest()->response();
990 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
991 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
992 $response->header( 'Pragma: no-cache' );
994 $repo = RepoGroup
::singleton()->getLocalRepo();
995 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
996 StreamFile
::stream( $path );
999 private function showHistory() {
1000 $out = $this->getOutput();
1001 if( $this->mAllowed
) {
1002 $out->addModules( 'mediawiki.special.undelete' );
1005 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1006 array( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() )
1009 $archive = new PageArchive( $this->mTargetObj
);
1010 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1012 $text = $archive->getLastRevisionText();
1013 if( is_null( $text ) ) {
1014 $out->addWikiMsg( 'nohistory' );
1018 $out->addHTML( '<div class="mw-undelete-history">' );
1019 if ( $this->mAllowed
) {
1020 $out->addWikiMsg( 'undeletehistory' );
1021 $out->addWikiMsg( 'undeleterevdel' );
1023 $out->addWikiMsg( 'undeletehistorynoadmin' );
1025 $out->addHTML( '</div>' );
1027 # List all stored revisions
1028 $revisions = $archive->listRevisions();
1029 $files = $archive->listFiles();
1031 $haveRevisions = $revisions && $revisions->numRows() > 0;
1032 $haveFiles = $files && $files->numRows() > 0;
1034 # Batch existence check on user and talk pages
1035 if( $haveRevisions ) {
1036 $batch = new LinkBatch();
1037 foreach ( $revisions as $row ) {
1038 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1039 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1042 $revisions->seek( 0 );
1045 $batch = new LinkBatch();
1046 foreach ( $files as $row ) {
1047 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1048 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1054 if ( $this->mAllowed
) {
1055 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1056 # Start the form here
1057 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1058 $out->addHTML( $top );
1061 # Show relevant lines from the deletion log:
1062 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1063 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1064 # Show relevant lines from the suppression log:
1065 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1066 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1067 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1070 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1071 # Format the user-visible controls (comment field, submission button)
1072 # in a nice little table
1073 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1077 <td class='mw-input'>" .
1078 Xml
::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1079 'mw-undelete-unsuppress', $this->mUnsuppress
).
1083 $unsuppressBox = '';
1086 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1087 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1089 <td colspan='2' class='mw-undelete-extrahelp'>" .
1090 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1094 <td class='mw-label'>" .
1095 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1097 <td class='mw-input'>" .
1098 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1103 <td class='mw-submit'>" .
1104 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1105 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1109 Xml
::closeElement( 'table' ) .
1110 Xml
::closeElement( 'fieldset' );
1112 $out->addHTML( $table );
1115 $out->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1117 if( $haveRevisions ) {
1118 # The page's stored (deleted) history:
1119 $out->addHTML( '<ul>' );
1120 $remaining = $revisions->numRows();
1121 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1123 foreach ( $revisions as $row ) {
1125 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1128 $out->addHTML( '</ul>' );
1130 $out->addWikiMsg( 'nohistory' );
1134 $out->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1135 $out->addHTML( '<ul>' );
1136 foreach ( $files as $row ) {
1137 $out->addHTML( $this->formatFileRow( $row ) );
1140 $out->addHTML( '</ul>' );
1143 if ( $this->mAllowed
) {
1144 # Slip in the hidden controls here
1145 $misc = Html
::hidden( 'target', $this->mTarget
);
1146 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->editToken() );
1147 $misc .= Xml
::closeElement( 'form' );
1148 $out->addHTML( $misc );
1154 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1155 $rev = Revision
::newFromArchiveRow( $row,
1156 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1158 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1159 // Build checkboxen...
1160 if( $this->mAllowed
) {
1161 if( $this->mInvert
) {
1162 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1163 $checkBox = Xml
::check( "ts$ts" );
1165 $checkBox = Xml
::check( "ts$ts", true );
1168 $checkBox = Xml
::check( "ts$ts" );
1173 // Build page & diff links...
1174 if( $this->mCanView
) {
1175 $titleObj = $this->getTitle();
1177 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1178 $pageLink = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1179 $last = wfMsgHtml( 'diff' );
1180 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1181 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1182 $last = Linker
::linkKnown(
1184 wfMsgHtml( 'diff' ),
1187 'target' => $this->mTargetObj
->getPrefixedText(),
1193 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1194 $last = wfMsgHtml( 'diff' );
1197 $pageLink = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1198 $last = wfMsgHtml( 'diff' );
1201 $userLink = Linker
::revUserTools( $rev );
1202 // Revision text size
1203 $size = $row->ar_len
;
1204 if( !is_null( $size ) ) {
1205 $stxt = Linker
::formatRevisionSize( $size );
1208 $comment = Linker
::revComment( $rev );
1209 // Revision delete links
1210 $revdlink = Linker
::getRevDeleteLink( $this->getUser(), $rev, $this->mTargetObj
);
1211 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1214 private function formatFileRow( $row ) {
1215 $file = ArchivedFile
::newFromRow( $row );
1217 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1218 if( $this->mAllowed
&& $row->fa_storage_key
) {
1219 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1220 $key = urlencode( $row->fa_storage_key
);
1221 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1224 $pageLink = $this->getLang()->timeanddate( $ts, true );
1226 $userLink = $this->getFileUser( $file );
1228 wfMsg( 'widthheight',
1229 $this->getLang()->formatNum( $row->fa_width
),
1230 $this->getLang()->formatNum( $row->fa_height
) ) .
1232 wfMsg( 'nbytes', $this->getLang()->formatNum( $row->fa_size
) ) .
1234 $data = htmlspecialchars( $data );
1235 $comment = $this->getFileComment( $file );
1237 // Add show/hide deletion links if available
1238 $user = $this->getUser();
1239 $canHide = $user->isAllowed( 'deleterevision' );
1240 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1241 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1242 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1245 'type' => 'filearchive',
1246 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1247 'ids' => $row->fa_id
1249 $revdlink = Linker
::revDeleteLink( $query,
1250 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1256 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1260 * Fetch revision text link if it's available to all users
1262 * @param $rev Revision
1265 function getPageLink( $rev, $titleObj, $ts ) {
1266 $time = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1268 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1269 return '<span class="history-deleted">' . $time . '</span>';
1271 $link = Linker
::linkKnown(
1276 'target' => $this->mTargetObj
->getPrefixedText(),
1280 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1281 $link = '<span class="history-deleted">' . $link . '</span>';
1288 * Fetch image view link if it's available to all users
1291 * @return String: HTML fragment
1293 function getFileLink( $file, $titleObj, $ts, $key ) {
1294 if( !$file->userCan( File
::DELETED_FILE
, $this->getUser() ) ) {
1295 return '<span class="history-deleted">' . $this->getLang()->timeanddate( $ts, true ) . '</span>';
1297 $link = Linker
::linkKnown(
1299 $this->getLang()->timeanddate( $ts, true ),
1302 'target' => $this->mTargetObj
->getPrefixedText(),
1304 'token' => $this->getUser()->editToken( $key )
1307 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1308 $link = '<span class="history-deleted">' . $link . '</span>';
1315 * Fetch file's user id if it's available to this user
1318 * @return String: HTML fragment
1320 function getFileUser( $file ) {
1321 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1322 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1324 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1325 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1326 if( $file->isDeleted( File
::DELETED_USER
) ) {
1327 $link = '<span class="history-deleted">' . $link . '</span>';
1334 * Fetch file upload comment if it's available to this user
1337 * @return String: HTML fragment
1339 function getFileComment( $file ) {
1340 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1341 return '<span class="history-deleted"><span class="comment">' .
1342 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1344 $link = Linker
::commentBlock( $file->getRawDescription() );
1345 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1346 $link = '<span class="history-deleted">' . $link . '</span>';
1352 function undelete() {
1353 global $wgUploadMaintenance;
1355 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1356 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1359 if ( wfReadOnly() ) {
1360 throw new ReadOnlyError
;
1363 $out = $this->getOutput();
1364 $archive = new PageArchive( $this->mTargetObj
);
1365 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1366 $ok = $archive->undelete(
1367 $this->mTargetTimestamp
,
1369 $this->mFileVersions
,
1370 $this->mUnsuppress
);
1372 if( is_array( $ok ) ) {
1373 if ( $ok[1] ) { // Undeleted file count
1374 wfRunHooks( 'FileUndeleteComplete', array(
1375 $this->mTargetObj
, $this->mFileVersions
,
1376 $this->getUser(), $this->mComment
) );
1379 $link = Linker
::linkKnown( $this->mTargetObj
);
1380 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1382 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1383 $out->addWikiMsg( 'cannotundelete' );
1384 $out->addWikiMsg( 'undeleterevdel' );
1387 // Show file deletion warnings and errors
1388 $status = $archive->getFileStatus();
1389 if( $status && !$status->isGood() ) {
1390 $out->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );