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 ) {
66 $dbr = wfGetDB( DB_SLAVE
);
68 $title = Title
::newFromText( $prefix );
70 $ns = $title->getNamespace();
71 $prefix = $title->getDBkey();
73 // Prolly won't work too good
74 // @todo handle bare namespace names cleanly?
78 'ar_namespace' => $ns,
79 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
83 $suppressedText = REVISION
::DELETED_TEXT | REVISION
::DELETED_RESTRICTED
;
84 if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
85 $conds[] = $dbr->bitAnd('ar_deleted', $suppressedText ) .
86 ' != ' . $suppressedText;
88 return self
::listPages( $dbr, $conds );
92 * @param $dbr DatabaseBase
94 * @return bool|ResultWrapper
96 protected static function listPages( $dbr, $condition ) {
97 return $dbr->resultObject(
108 'GROUP BY' => 'ar_namespace,ar_title',
109 'ORDER BY' => 'ar_namespace,ar_title',
117 * List the revisions of the given page. Returns result wrapper with
118 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
120 * @return ResultWrapper
122 function listRevisions() {
123 $dbr = wfGetDB( DB_SLAVE
);
124 $res = $dbr->select( 'archive',
126 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
127 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id'
129 array( 'ar_namespace' => $this->title
->getNamespace(),
130 'ar_title' => $this->title
->getDBkey() ),
131 'PageArchive::listRevisions',
132 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
133 $ret = $dbr->resultObject( $res );
138 * List the deleted file revisions for this page, if it's a file page.
139 * Returns a result wrapper with various filearchive fields, or null
140 * if not a file page.
142 * @return ResultWrapper
143 * @todo Does this belong in Image for fuller encapsulation?
145 function listFiles() {
146 if( $this->title
->getNamespace() == NS_FILE
) {
147 $dbr = wfGetDB( DB_SLAVE
);
148 $res = $dbr->select( 'filearchive',
168 array( 'fa_name' => $this->title
->getDBkey() ),
170 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
171 $ret = $dbr->resultObject( $res );
178 * Return a Revision object containing data for the deleted revision.
179 * Note that the result *may* or *may not* have a null page ID.
181 * @param $timestamp String
184 function getRevision( $timestamp ) {
185 $dbr = wfGetDB( DB_SLAVE
);
186 $row = $dbr->selectRow( 'archive',
199 array( 'ar_namespace' => $this->title
->getNamespace(),
200 'ar_title' => $this->title
->getDBkey(),
201 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
204 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
211 * Return the most-previous revision, either live or deleted, against
212 * the deleted revision given by timestamp.
214 * May produce unexpected results in case of history merges or other
215 * unusual time issues.
217 * @param $timestamp String
218 * @return Revision or null
220 function getPreviousRevision( $timestamp ) {
221 $dbr = wfGetDB( DB_SLAVE
);
223 // Check the previous deleted revision...
224 $row = $dbr->selectRow( 'archive',
226 array( 'ar_namespace' => $this->title
->getNamespace(),
227 'ar_title' => $this->title
->getDBkey(),
229 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
232 'ORDER BY' => 'ar_timestamp DESC',
234 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
236 $row = $dbr->selectRow( array( 'page', 'revision' ),
237 array( 'rev_id', 'rev_timestamp' ),
239 'page_namespace' => $this->title
->getNamespace(),
240 'page_title' => $this->title
->getDBkey(),
241 'page_id = rev_page',
243 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
246 'ORDER BY' => 'rev_timestamp DESC',
248 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
249 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
251 if( $prevLive && $prevLive > $prevDeleted ) {
252 // Most prior revision was live
253 return Revision
::newFromId( $prevLiveId );
254 } elseif( $prevDeleted ) {
255 // Most prior revision was deleted
256 return $this->getRevision( $prevDeleted );
258 // No prior revision on this page.
264 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
266 * @param $row Object: database row
269 function getTextFromRow( $row ) {
270 if( is_null( $row->ar_text_id
) ) {
271 // An old row from MediaWiki 1.4 or previous.
272 // Text is embedded in this row in classic compression format.
273 return Revision
::getRevisionText( $row, 'ar_' );
275 // New-style: keyed to the text storage backend.
276 $dbr = wfGetDB( DB_SLAVE
);
277 $text = $dbr->selectRow( 'text',
278 array( 'old_text', 'old_flags' ),
279 array( 'old_id' => $row->ar_text_id
),
281 return Revision
::getRevisionText( $text );
286 * Fetch (and decompress if necessary) the stored text of the most
287 * recently edited deleted revision of the page.
289 * If there are no archived revisions for the page, returns NULL.
293 function getLastRevisionText() {
294 $dbr = wfGetDB( DB_SLAVE
);
295 $row = $dbr->selectRow( 'archive',
296 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
297 array( 'ar_namespace' => $this->title
->getNamespace(),
298 'ar_title' => $this->title
->getDBkey() ),
300 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
302 return $this->getTextFromRow( $row );
309 * Quick check if any archived revisions are present for the page.
313 function isDeleted() {
314 $dbr = wfGetDB( DB_SLAVE
);
315 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
316 array( 'ar_namespace' => $this->title
->getNamespace(),
317 'ar_title' => $this->title
->getDBkey() ) );
322 * Restore the given (or all) text and file revisions for the page.
323 * Once restored, the items will be removed from the archive tables.
324 * The deletion log will be updated with an undeletion notice.
326 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
327 * @param $comment String
328 * @param $fileVersions Array
329 * @param $unsuppress Boolean
331 * @return array(number of file revisions restored, number of image revisions restored, log message)
332 * on success, false on failure
334 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
335 // If both the set of text revisions and file revisions are empty,
336 // restore everything. Otherwise, just restore the requested items.
337 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
339 $restoreText = $restoreAll ||
!empty( $timestamps );
340 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
342 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
343 $img = wfLocalFile( $this->title
);
344 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
345 if ( !$this->fileStatus
->isOk() ) {
348 $filesRestored = $this->fileStatus
->successCount
;
354 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
355 if( $textRestored === false ) { // It must be one of UNDELETE_*
364 $log = new LogPage( 'delete' );
366 if( $textRestored && $filesRestored ) {
367 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
368 $wgContLang->formatNum( $textRestored ),
369 $wgContLang->formatNum( $filesRestored ) );
370 } elseif( $textRestored ) {
371 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
372 $wgContLang->formatNum( $textRestored ) );
373 } elseif( $filesRestored ) {
374 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
375 $wgContLang->formatNum( $filesRestored ) );
377 wfDebug( "Undelete: nothing undeleted...\n" );
381 if( trim( $comment ) != '' ) {
382 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
384 $log->addEntry( 'restore', $this->title
, $reason );
386 return array( $textRestored, $filesRestored, $reason );
390 * This is the meaty bit -- restores archived revisions of the given page
391 * to the cur/old tables. If the page currently exists, all revisions will
392 * be stuffed into old, otherwise the most recent will go into cur.
394 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
395 * @param $comment String
396 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
398 * @return Mixed: number of revisions restored or false on failure
400 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
401 if ( wfReadOnly() ) {
404 $restoreAll = empty( $timestamps );
406 $dbw = wfGetDB( DB_MASTER
);
408 # Does this page already exist? We'll have to update it...
409 $article = new Article( $this->title
);
410 $options = 'FOR UPDATE'; // lock page
411 $page = $dbw->selectRow( 'page',
412 array( 'page_id', 'page_latest' ),
413 array( 'page_namespace' => $this->title
->getNamespace(),
414 'page_title' => $this->title
->getDBkey() ),
420 # Page already exists. Import the history, and if necessary
421 # we'll update the latest revision field in the record.
423 $pageId = $page->page_id
;
424 $previousRevId = $page->page_latest
;
425 # Get the time span of this page
426 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
427 array( 'rev_id' => $previousRevId ),
429 if( $previousTimestamp === false ) {
430 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
434 # Have to create a new article...
437 $previousTimestamp = 0;
441 $oldones = '1 = 1'; # All revisions...
443 $oldts = implode( ',',
444 array_map( array( &$dbw, 'addQuotes' ),
445 array_map( array( &$dbw, 'timestamp' ),
448 $oldones = "ar_timestamp IN ( {$oldts} )";
452 * Select each archived revision...
454 $result = $dbw->select( 'archive',
469 'ar_namespace' => $this->title
->getNamespace(),
470 'ar_title' => $this->title
->getDBkey(),
473 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
475 $ret = $dbw->resultObject( $result );
476 $rev_count = $dbw->numRows( $result );
478 wfDebug( __METHOD__
. ": no revisions to restore\n" );
482 $ret->seek( $rev_count - 1 ); // move to last
483 $row = $ret->fetchObject(); // get newest archived rev
484 $ret->seek( 0 ); // move back
487 // Check the state of the newest to-be version...
488 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
489 return false; // we can't leave the current revision like this!
491 // Safe to insert now...
492 $newid = $article->insertOn( $dbw );
495 // Check if a deleted revision will become the current revision...
496 if( $row->ar_timestamp
> $previousTimestamp ) {
497 // Check the state of the newest to-be version...
498 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
499 return false; // we can't leave the current revision like this!
507 foreach ( $ret as $row ) {
508 // Check for key dupes due to shitty archive integrity.
509 if( $row->ar_rev_id
) {
510 $exists = $dbw->selectField( 'revision', '1',
511 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
513 continue; // don't throw DB errors
516 // Insert one revision at a time...maintaining deletion status
517 // unless we are specifically removing all restrictions...
518 $revision = Revision
::newFromArchiveRow( $row,
521 'deleted' => $unsuppress ?
0 : $row->ar_deleted
524 $revision->insertOn( $dbw );
527 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
529 # Now that it's safely stored, take it out of the archive
530 $dbw->delete( 'archive',
532 'ar_namespace' => $this->title
->getNamespace(),
533 'ar_title' => $this->title
->getDBkey(),
537 // Was anything restored at all?
538 if ( $restored == 0 ) {
542 $created = (bool)$newid;
543 $oldcountable = $article->isCountable();
545 // Attach the latest revision to the page...
546 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
547 if ( $created ||
$wasnew ) {
548 // Update site stats, link tables, etc
549 $user = User
::newFromName( $revision->getRawUserText(), false );
550 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
553 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
555 if( $this->title
->getNamespace() == NS_FILE
) {
556 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
566 function getFileStatus() { return $this->fileStatus
; }
570 * Special page allowing users with the appropriate permissions to view
571 * and restore deleted content.
573 * @ingroup SpecialPage
575 class SpecialUndelete
extends SpecialPage
{
576 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
577 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
584 function __construct() {
585 parent
::__construct( 'Undelete', 'deletedhistory' );
588 function loadRequest() {
589 $request = $this->getRequest();
590 $user = $this->getUser();
592 $this->mAction
= $request->getVal( 'action' );
593 $this->mTarget
= $request->getVal( 'target' );
594 $this->mSearchPrefix
= $request->getText( 'prefix' );
595 $time = $request->getVal( 'timestamp' );
596 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
597 $this->mFilename
= $request->getVal( 'file' );
599 $posted = $request->wasPosted() &&
600 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
601 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
602 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
603 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
604 $this->mDiff
= $request->getCheck( 'diff' );
605 $this->mComment
= $request->getText( 'wpComment' );
606 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
607 $this->mToken
= $request->getVal( 'token' );
609 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
610 $this->mAllowed
= true; // user can restore
611 $this->mCanView
= true; // user can view content
612 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
613 $this->mAllowed
= false; // user cannot restore
614 $this->mCanView
= true; // user can view content
615 } else { // user can only view the list of revisions
616 $this->mAllowed
= false;
617 $this->mCanView
= false;
618 $this->mTimestamp
= '';
619 $this->mRestore
= false;
622 if( $this->mRestore ||
$this->mInvert
) {
623 $timestamps = array();
624 $this->mFileVersions
= array();
625 foreach( $request->getValues() as $key => $val ) {
627 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
628 array_push( $timestamps, $matches[1] );
631 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
632 $this->mFileVersions
[] = intval( $matches[1] );
635 rsort( $timestamps );
636 $this->mTargetTimestamp
= $timestamps;
640 function execute( $par ) {
642 if ( !$this->userCanExecute( $this->getUser() ) ) {
643 $this->displayRestrictionError();
646 $this->outputHeader();
648 $this->loadRequest();
650 $out = $this->getOutput();
652 if ( $this->mAllowed
) {
653 $out->setPageTitle( wfMsg( 'undeletepage' ) );
655 $out->setPageTitle( wfMsg( 'viewdeletedpage' ) );
659 $this->mTarget
= $par;
661 if ( $this->mTarget
!== '' ) {
662 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
663 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
665 $this->mTargetObj
= null;
668 if( is_null( $this->mTargetObj
) ) {
669 # Not all users can just browse every deleted page from the list
670 if( $this->getUser()->isAllowed( 'browsearchive' ) ) {
671 $this->showSearchForm();
673 # List undeletable articles
674 if( $this->mSearchPrefix
) {
675 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
676 $this->showList( $result );
679 $out->addWikiMsg( 'undelete-header' );
683 if( $this->mTimestamp
!== '' ) {
684 return $this->showRevision( $this->mTimestamp
);
686 if( $this->mFilename
!== null ) {
687 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
688 // Check if user is allowed to see this file
689 if ( !$file->exists() ) {
690 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
692 } elseif( !$file->userCan( File
::DELETED_FILE
) ) {
693 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
694 $out->permissionRequired( 'suppressrevision' );
696 $out->permissionRequired( 'deletedtext' );
699 } elseif ( !$this->getUser()->matchEditToken( $this->mToken
, $this->mFilename
) ) {
700 $this->showFileConfirmationForm( $this->mFilename
);
703 return $this->showFile( $this->mFilename
);
706 if( $this->mRestore
&& $this->mAction
== 'submit' ) {
707 global $wgUploadMaintenance;
708 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
709 $out->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
712 return $this->undelete();
714 if( $this->mInvert
&& $this->mAction
== 'submit' ) {
715 return $this->showHistory();
717 return $this->showHistory();
720 function showSearchForm() {
723 $this->getOutput()->addWikiMsg( 'undelete-header' );
725 $this->getOutput()->addHTML(
726 Xml
::openElement( 'form', array(
728 'action' => $wgScript ) ) .
729 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
730 Html
::hidden( 'title',
731 $this->getTitle()->getPrefixedDbKey() ) .
732 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
733 'prefix', 'prefix', 20,
734 $this->mSearchPrefix
) . ' ' .
735 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
736 Xml
::closeElement( 'fieldset' ) .
737 Xml
::closeElement( 'form' )
742 * Generic list of deleted pages
744 * @param $result ResultWrapper
747 private function showList( $result ) {
748 $out = $this->getOutput();
750 if( $result->numRows() == 0 ) {
751 $out->addWikiMsg( 'undelete-no-results' );
755 $out->addWikiMsg( 'undeletepagetext', $this->getLang()->formatNum( $result->numRows() ) );
757 $undelete = $this->getTitle();
758 $out->addHTML( "<ul>\n" );
759 foreach ( $result as $row ) {
760 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
761 $link = Linker
::linkKnown(
763 htmlspecialchars( $title->getPrefixedText() ),
765 array( 'target' => $title->getPrefixedText() )
767 $revs = wfMsgExt( 'undeleterevisions',
768 array( 'parseinline' ),
769 $this->getLang()->formatNum( $row->count
) );
770 $out->addHTML( "<li>{$link} ({$revs})</li>\n" );
773 $out->addHTML( "</ul>\n" );
778 private function showRevision( $timestamp ) {
779 $out = $this->getOutput();
781 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
785 $archive = new PageArchive( $this->mTargetObj
);
786 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
787 $rev = $archive->getRevision( $timestamp );
790 $out->addWikiMsg( 'undeleterevision-missing' );
794 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
795 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
796 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
799 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
800 $out->addHTML( '<br />' );
801 // and we are allowed to see...
805 $out->setPageTitle( wfMsg( 'undeletepage' ) );
808 $previousRev = $archive->getPreviousRevision( $timestamp );
810 $this->showDiff( $previousRev, $rev );
811 if( $this->getUser()->getOption( 'diffonly' ) ) {
814 $out->addHTML( '<hr />' );
817 $out->addWikiMsg( 'undelete-nodiff' );
821 $link = Linker
::linkKnown(
822 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
823 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
826 // date and time are separate parameters to facilitate localisation.
827 // $time is kept for backward compat reasons.
828 $time = $this->getLang()->timeAndDate( $timestamp, true );
829 $d = $this->getLang()->date( $timestamp, true );
830 $t = $this->getLang()->time( $timestamp, true );
831 $user = Linker
::revUserTools( $rev );
833 if( $this->mPreview
) {
834 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
836 $openDiv = '<div id="mw-undelete-revision">';
838 $out->addHTML( $openDiv );
840 // Revision delete links
841 if ( !$this->mDiff
) {
842 $revdel = $this->revDeleteLink( $rev );
844 $out->addHTML( $revdel );
848 $out->addHTML( wfMessage( 'undelete-revision' )->rawParams( $link )->params(
849 $time )->rawParams( $user )->params( $d, $t )->parse() . '</div>' );
850 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
852 if( $this->mPreview
) {
854 $popts = $out->parserOptions();
855 $popts->setEditSection( false );
856 $out->parserOptions( $popts );
857 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
861 Xml
::element( 'textarea', array(
862 'readonly' => 'readonly',
863 'cols' => intval( $this->getUser()->getOption( 'cols' ) ),
864 'rows' => intval( $this->getUser()->getOption( 'rows' ) ) ),
865 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
866 Xml
::openElement( 'div' ) .
867 Xml
::openElement( 'form', array(
869 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
870 Xml
::element( 'input', array(
873 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
874 Xml
::element( 'input', array(
876 'name' => 'timestamp',
877 'value' => $timestamp ) ) .
878 Xml
::element( 'input', array(
880 'name' => 'wpEditToken',
881 'value' => $this->getUser()->editToken() ) ) .
882 Xml
::element( 'input', array(
885 'value' => wfMsg( 'showpreview' ) ) ) .
886 Xml
::element( 'input', array(
889 'value' => wfMsg( 'showdiff' ) ) ) .
890 Xml
::closeElement( 'form' ) .
891 Xml
::closeElement( 'div' ) );
895 * Get a revision-deletion link, or disabled link, or nothing, depending
896 * on user permissions & the settings on the revision.
898 * Will use forward-compatible revision ID in the Special:RevDelete link
899 * if possible, otherwise the timestamp-based ID which may break after
902 * @param Revision $rev
903 * @return string HTML fragment
905 function revDeleteLink( $rev ) {
906 $canHide = $this->getUser()->isAllowed( 'deleterevision' );
907 if( $canHide ||
( $rev->getVisibility() && $this->getUser()->isAllowed( 'deletedhistory' ) ) ) {
908 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
909 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
911 if ( $rev->getId() ) {
912 // RevDelete links using revision ID are stable across
913 // page deletion and undeletion; use when possible.
915 'type' => 'revision',
916 'target' => $this->mTargetObj
->getPrefixedDBkey(),
917 'ids' => $rev->getId()
920 // Older deleted entries didn't save a revision ID.
921 // We have to refer to these by timestamp, ick!
924 'target' => $this->mTargetObj
->getPrefixedDBkey(),
925 'ids' => $rev->getTimestamp()
928 return Linker
::revDeleteLink( $query,
929 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
937 * Build a diff display between this and the previous either deleted
938 * or non-deleted edit.
940 * @param $previousRev Revision
941 * @param $currentRev Revision
942 * @return String: HTML
944 function showDiff( $previousRev, $currentRev ) {
945 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
946 $diffEngine->showDiffStyle();
947 $this->getOutput()->addHTML(
949 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
950 "<col class='diff-marker' />" .
951 "<col class='diff-content' />" .
952 "<col class='diff-marker' />" .
953 "<col class='diff-content' />" .
955 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
956 $this->diffHeader( $previousRev, 'o' ) .
958 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
959 $this->diffHeader( $currentRev, 'n' ) .
962 $diffEngine->generateDiffBody(
963 $previousRev->getText(), $currentRev->getText() ) .
970 * @param $rev Revision
974 private function diffHeader( $rev, $prefix ) {
975 $isDeleted = !( $rev->getId() && $rev->getTitle() );
977 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
978 $targetPage = $this->getTitle();
979 $targetQuery = array(
980 'target' => $this->mTargetObj
->getPrefixedText(),
981 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
984 /// @todo FIXME: getId() may return non-zero for deleted revs...
985 $targetPage = $rev->getTitle();
986 $targetQuery = array( 'oldid' => $rev->getId() );
988 // Add show/hide deletion links if available
989 $del = $this->revDeleteLink( $rev );
991 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
997 $this->getLang()->timeanddate( $rev->getTimestamp(), true ),
998 $this->getLang()->date( $rev->getTimestamp(), true ),
999 $this->getLang()->time( $rev->getTimestamp(), true )
1005 '<div id="mw-diff-'.$prefix.'title2">' .
1006 Linker
::revUserTools( $rev ) . '<br />' .
1008 '<div id="mw-diff-'.$prefix.'title3">' .
1009 Linker
::revComment( $rev ) . $del . '<br />' .
1014 * Show a form confirming whether a tokenless user really wants to see a file
1016 private function showFileConfirmationForm( $key ) {
1017 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
1018 $this->getOutput()->addWikiMsg( 'undelete-show-file-confirm',
1019 $this->mTargetObj
->getText(),
1020 $this->getLang()->date( $file->getTimestamp() ),
1021 $this->getLang()->time( $file->getTimestamp() ) );
1022 $this->getOutput()->addHTML(
1023 Xml
::openElement( 'form', array(
1025 'action' => $this->getTitle()->getLocalURL(
1026 'target=' . urlencode( $this->mTarget
) .
1027 '&file=' . urlencode( $key ) .
1028 '&token=' . urlencode( $this->getUser()->editToken( $key ) ) )
1031 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1037 * Show a deleted file version requested by the visitor.
1039 private function showFile( $key ) {
1040 $this->getOutput()->disable();
1042 # We mustn't allow the output to be Squid cached, otherwise
1043 # if an admin previews a deleted image, and it's cached, then
1044 # a user without appropriate permissions can toddle off and
1045 # nab the image, and Squid will serve it
1046 $response = $this->getRequest()->response();
1047 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1048 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1049 $response->header( 'Pragma: no-cache' );
1052 require_once( "$IP/includes/StreamFile.php" );
1053 $repo = RepoGroup
::singleton()->getLocalRepo();
1054 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1055 wfStreamFile( $path );
1058 private function showHistory() {
1059 $out = $this->getOutput();
1060 if( $this->mAllowed
) {
1061 $out->addModules( 'mediawiki.special.undelete' );
1062 $out->setPageTitle( wfMsg( 'undeletepage' ) );
1064 $out->setPageTitle( wfMsg( 'viewdeletedpage' ) );
1067 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1068 array( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() )
1071 $archive = new PageArchive( $this->mTargetObj
);
1072 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1074 $text = $archive->getLastRevisionText();
1075 if( is_null( $text ) ) {
1076 $out->addWikiMsg( 'nohistory' );
1080 $out->addHTML( '<div class="mw-undelete-history">' );
1081 if ( $this->mAllowed
) {
1082 $out->addWikiMsg( 'undeletehistory' );
1083 $out->addWikiMsg( 'undeleterevdel' );
1085 $out->addWikiMsg( 'undeletehistorynoadmin' );
1087 $out->addHTML( '</div>' );
1089 # List all stored revisions
1090 $revisions = $archive->listRevisions();
1091 $files = $archive->listFiles();
1093 $haveRevisions = $revisions && $revisions->numRows() > 0;
1094 $haveFiles = $files && $files->numRows() > 0;
1096 # Batch existence check on user and talk pages
1097 if( $haveRevisions ) {
1098 $batch = new LinkBatch();
1099 foreach ( $revisions as $row ) {
1100 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1101 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1104 $revisions->seek( 0 );
1107 $batch = new LinkBatch();
1108 foreach ( $files as $row ) {
1109 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1110 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1116 if ( $this->mAllowed
) {
1117 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1118 # Start the form here
1119 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1120 $out->addHTML( $top );
1123 # Show relevant lines from the deletion log:
1124 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1125 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
->getPrefixedText() );
1126 # Show relevant lines from the suppression log:
1127 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1128 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1129 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
->getPrefixedText() );
1132 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1133 # Format the user-visible controls (comment field, submission button)
1134 # in a nice little table
1135 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1139 <td class='mw-input'>" .
1140 Xml
::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1141 'mw-undelete-unsuppress', $this->mUnsuppress
).
1145 $unsuppressBox = '';
1148 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1149 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1151 <td colspan='2' class='mw-undelete-extrahelp'>" .
1152 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1156 <td class='mw-label'>" .
1157 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1159 <td class='mw-input'>" .
1160 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1165 <td class='mw-submit'>" .
1166 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1167 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1171 Xml
::closeElement( 'table' ) .
1172 Xml
::closeElement( 'fieldset' );
1174 $out->addHTML( $table );
1177 $out->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1179 if( $haveRevisions ) {
1180 # The page's stored (deleted) history:
1181 $out->addHTML( '<ul>' );
1182 $remaining = $revisions->numRows();
1183 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1185 foreach ( $revisions as $row ) {
1187 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1190 $out->addHTML( '</ul>' );
1192 $out->addWikiMsg( 'nohistory' );
1196 $out->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1197 $out->addHTML( '<ul>' );
1198 foreach ( $files as $row ) {
1199 $out->addHTML( $this->formatFileRow( $row ) );
1202 $out->addHTML( '</ul>' );
1205 if ( $this->mAllowed
) {
1206 # Slip in the hidden controls here
1207 $misc = Html
::hidden( 'target', $this->mTarget
);
1208 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->editToken() );
1209 $misc .= Xml
::closeElement( 'form' );
1210 $out->addHTML( $misc );
1216 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1217 $rev = Revision
::newFromArchiveRow( $row,
1218 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1220 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1221 // Build checkboxen...
1222 if( $this->mAllowed
) {
1223 if( $this->mInvert
) {
1224 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1225 $checkBox = Xml
::check( "ts$ts" );
1227 $checkBox = Xml
::check( "ts$ts", true );
1230 $checkBox = Xml
::check( "ts$ts" );
1235 // Build page & diff links...
1236 if( $this->mCanView
) {
1237 $titleObj = $this->getTitle();
1239 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1240 $pageLink = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1241 $last = wfMsgHtml( 'diff' );
1242 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1243 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1244 $last = Linker
::linkKnown(
1246 wfMsgHtml( 'diff' ),
1249 'target' => $this->mTargetObj
->getPrefixedText(),
1255 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1256 $last = wfMsgHtml( 'diff' );
1259 $pageLink = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1260 $last = wfMsgHtml( 'diff' );
1263 $userLink = Linker
::revUserTools( $rev );
1264 // Revision text size
1265 $size = $row->ar_len
;
1266 if( !is_null( $size ) ) {
1267 $stxt = Linker
::formatRevisionSize( $size );
1270 $comment = Linker
::revComment( $rev );
1271 // Revision delete links
1272 $revdlink = $this->revDeleteLink( $rev );
1273 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1276 private function formatFileRow( $row ) {
1277 $file = ArchivedFile
::newFromRow( $row );
1279 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1280 if( $this->mAllowed
&& $row->fa_storage_key
) {
1281 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1282 $key = urlencode( $row->fa_storage_key
);
1283 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1286 $pageLink = $this->getLang()->timeanddate( $ts, true );
1288 $userLink = $this->getFileUser( $file );
1290 wfMsg( 'widthheight',
1291 $this->getLang()->formatNum( $row->fa_width
),
1292 $this->getLang()->formatNum( $row->fa_height
) ) .
1294 wfMsg( 'nbytes', $this->getLang()->formatNum( $row->fa_size
) ) .
1296 $data = htmlspecialchars( $data );
1297 $comment = $this->getFileComment( $file );
1298 // Add show/hide deletion links if available
1299 $canHide = $this->getUser()->isAllowed( 'deleterevision' );
1300 if( $canHide ||
( $file->getVisibility() && $this->getUser()->isAllowed( 'deletedhistory' ) ) ) {
1301 if( !$file->userCan( File
::DELETED_RESTRICTED
) ) {
1302 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1305 'type' => 'filearchive',
1306 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1307 'ids' => $row->fa_id
1309 $revdlink = Linker
::revDeleteLink( $query,
1310 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1315 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1319 * Fetch revision text link if it's available to all users
1321 * @param $rev Revision
1324 function getPageLink( $rev, $titleObj, $ts ) {
1325 $time = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1327 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1328 return '<span class="history-deleted">' . $time . '</span>';
1330 $link = Linker
::linkKnown(
1335 'target' => $this->mTargetObj
->getPrefixedText(),
1339 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1340 $link = '<span class="history-deleted">' . $link . '</span>';
1347 * Fetch image view link if it's available to all users
1350 * @return String: HTML fragment
1352 function getFileLink( $file, $titleObj, $ts, $key ) {
1353 if( !$file->userCan( File
::DELETED_FILE
) ) {
1354 return '<span class="history-deleted">' . $this->getLang()->timeanddate( $ts, true ) . '</span>';
1356 $link = Linker
::linkKnown(
1358 $this->getLang()->timeanddate( $ts, true ),
1361 'target' => $this->mTargetObj
->getPrefixedText(),
1363 'token' => $this->getUser()->editToken( $key )
1366 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1367 $link = '<span class="history-deleted">' . $link . '</span>';
1374 * Fetch file's user id if it's available to this user
1377 * @return String: HTML fragment
1379 function getFileUser( $file ) {
1380 if( !$file->userCan( File
::DELETED_USER
) ) {
1381 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1383 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1384 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1385 if( $file->isDeleted( File
::DELETED_USER
) ) {
1386 $link = '<span class="history-deleted">' . $link . '</span>';
1393 * Fetch file upload comment if it's available to this user
1396 * @return String: HTML fragment
1398 function getFileComment( $file ) {
1399 if( !$file->userCan( File
::DELETED_COMMENT
) ) {
1400 return '<span class="history-deleted"><span class="comment">' .
1401 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1403 $link = Linker
::commentBlock( $file->getRawDescription() );
1404 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1405 $link = '<span class="history-deleted">' . $link . '</span>';
1411 function undelete() {
1412 if ( wfReadOnly() ) {
1413 throw new ReadOnlyError
;
1416 if( !is_null( $this->mTargetObj
) ) {
1417 $archive = new PageArchive( $this->mTargetObj
);
1418 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1419 $ok = $archive->undelete(
1420 $this->mTargetTimestamp
,
1422 $this->mFileVersions
,
1423 $this->mUnsuppress
);
1425 if( is_array( $ok ) ) {
1426 if ( $ok[1] ) { // Undeleted file count
1427 wfRunHooks( 'FileUndeleteComplete', array(
1428 $this->mTargetObj
, $this->mFileVersions
,
1429 $this->getUser(), $this->mComment
) );
1432 $link = Linker
::linkKnown( $this->mTargetObj
);
1433 $this->getOutput()->addHTML( wfMessage( 'undeletedpage' )->rawParams( $link )->parse() );
1435 $this->getOutput()->showFatalError( wfMsg( 'cannotundelete' ) );
1436 $this->getOutput()->addWikiMsg( 'undeleterevdel' );
1439 // Show file deletion warnings and errors
1440 $status = $archive->getFileStatus();
1441 if( $status && !$status->isGood() ) {
1442 $this->getOutput()->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1445 $this->getOutput()->showFatalError( wfMsg( 'cannotundelete' ) );