3 * Implements Special:Undelete
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Used to show archived pages and eventually restore them.
27 * @ingroup SpecialPage
37 function __construct( $title ) {
38 if( is_null( $title ) ) {
39 throw new MWException( __METHOD__
. ' given a null title.' );
41 $this->title
= $title;
45 * List all deleted pages recorded in the archive table. Returns result
46 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
49 * @return ResultWrapper
51 public static function listAllPages() {
52 $dbr = wfGetDB( DB_SLAVE
);
53 return self
::listPages( $dbr, '' );
57 * List deleted pages recorded in the archive table matching the
59 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
61 * @param $prefix String: title prefix
62 * @return ResultWrapper
64 public static function listPagesByPrefix( $prefix ) {
65 $dbr = wfGetDB( DB_SLAVE
);
67 $title = Title
::newFromText( $prefix );
69 $ns = $title->getNamespace();
70 $prefix = $title->getDBkey();
72 // Prolly won't work too good
73 // @todo handle bare namespace names cleanly?
77 'ar_namespace' => $ns,
78 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
80 return self
::listPages( $dbr, $conds );
84 * @param $dbr DatabaseBase
86 * @return bool|ResultWrapper
88 protected static function listPages( $dbr, $condition ) {
89 return $dbr->resultObject(
100 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
101 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
109 * List the revisions of the given page. Returns result wrapper with
110 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
112 * @return ResultWrapper
114 function listRevisions() {
115 $dbr = wfGetDB( DB_SLAVE
);
116 $res = $dbr->select( 'archive',
118 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
119 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1'
121 array( 'ar_namespace' => $this->title
->getNamespace(),
122 'ar_title' => $this->title
->getDBkey() ),
123 'PageArchive::listRevisions',
124 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
125 $ret = $dbr->resultObject( $res );
130 * List the deleted file revisions for this page, if it's a file page.
131 * Returns a result wrapper with various filearchive fields, or null
132 * if not a file page.
134 * @return ResultWrapper
135 * @todo Does this belong in Image for fuller encapsulation?
137 function listFiles() {
138 if( $this->title
->getNamespace() == NS_FILE
) {
139 $dbr = wfGetDB( DB_SLAVE
);
140 $res = $dbr->select( 'filearchive',
160 array( 'fa_name' => $this->title
->getDBkey() ),
162 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
163 $ret = $dbr->resultObject( $res );
170 * Return a Revision object containing data for the deleted revision.
171 * Note that the result *may* or *may not* have a null page ID.
173 * @param $timestamp String
176 function getRevision( $timestamp ) {
177 $dbr = wfGetDB( DB_SLAVE
);
178 $row = $dbr->selectRow( 'archive',
193 array( 'ar_namespace' => $this->title
->getNamespace(),
194 'ar_title' => $this->title
->getDBkey(),
195 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
198 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleID() ) );
205 * Return the most-previous revision, either live or deleted, against
206 * the deleted revision given by timestamp.
208 * May produce unexpected results in case of history merges or other
209 * unusual time issues.
211 * @param $timestamp String
212 * @return Revision or null
214 function getPreviousRevision( $timestamp ) {
215 $dbr = wfGetDB( DB_SLAVE
);
217 // Check the previous deleted revision...
218 $row = $dbr->selectRow( 'archive',
220 array( 'ar_namespace' => $this->title
->getNamespace(),
221 'ar_title' => $this->title
->getDBkey(),
223 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
226 'ORDER BY' => 'ar_timestamp DESC',
228 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
230 $row = $dbr->selectRow( array( 'page', 'revision' ),
231 array( 'rev_id', 'rev_timestamp' ),
233 'page_namespace' => $this->title
->getNamespace(),
234 'page_title' => $this->title
->getDBkey(),
235 'page_id = rev_page',
237 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
240 'ORDER BY' => 'rev_timestamp DESC',
242 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
243 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
245 if( $prevLive && $prevLive > $prevDeleted ) {
246 // Most prior revision was live
247 return Revision
::newFromId( $prevLiveId );
248 } elseif( $prevDeleted ) {
249 // Most prior revision was deleted
250 return $this->getRevision( $prevDeleted );
252 // No prior revision on this page.
258 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
260 * @param $row Object: database row
263 function getTextFromRow( $row ) {
264 if( is_null( $row->ar_text_id
) ) {
265 // An old row from MediaWiki 1.4 or previous.
266 // Text is embedded in this row in classic compression format.
267 return Revision
::getRevisionText( $row, 'ar_' );
269 // New-style: keyed to the text storage backend.
270 $dbr = wfGetDB( DB_SLAVE
);
271 $text = $dbr->selectRow( 'text',
272 array( 'old_text', 'old_flags' ),
273 array( 'old_id' => $row->ar_text_id
),
275 return Revision
::getRevisionText( $text );
280 * Fetch (and decompress if necessary) the stored text of the most
281 * recently edited deleted revision of the page.
283 * If there are no archived revisions for the page, returns NULL.
287 function getLastRevisionText() {
288 $dbr = wfGetDB( DB_SLAVE
);
289 $row = $dbr->selectRow( 'archive',
290 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
291 array( 'ar_namespace' => $this->title
->getNamespace(),
292 'ar_title' => $this->title
->getDBkey() ),
294 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
296 return $this->getTextFromRow( $row );
303 * Quick check if any archived revisions are present for the page.
307 function isDeleted() {
308 $dbr = wfGetDB( DB_SLAVE
);
309 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
310 array( 'ar_namespace' => $this->title
->getNamespace(),
311 'ar_title' => $this->title
->getDBkey() ) );
316 * Restore the given (or all) text and file revisions for the page.
317 * Once restored, the items will be removed from the archive tables.
318 * The deletion log will be updated with an undeletion notice.
320 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
321 * @param $comment String
322 * @param $fileVersions Array
323 * @param $unsuppress Boolean
325 * @return array(number of file revisions restored, number of image revisions restored, log message)
326 * on success, false on failure
328 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
329 // If both the set of text revisions and file revisions are empty,
330 // restore everything. Otherwise, just restore the requested items.
331 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
333 $restoreText = $restoreAll ||
!empty( $timestamps );
334 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
336 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
337 $img = wfLocalFile( $this->title
);
338 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
339 if ( !$this->fileStatus
->isOk() ) {
342 $filesRestored = $this->fileStatus
->successCount
;
348 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
349 if( $textRestored === false ) { // It must be one of UNDELETE_*
358 $log = new LogPage( 'delete' );
360 if( $textRestored && $filesRestored ) {
361 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
362 $wgContLang->formatNum( $textRestored ),
363 $wgContLang->formatNum( $filesRestored ) );
364 } elseif( $textRestored ) {
365 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
366 $wgContLang->formatNum( $textRestored ) );
367 } elseif( $filesRestored ) {
368 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
369 $wgContLang->formatNum( $filesRestored ) );
371 wfDebug( "Undelete: nothing undeleted...\n" );
375 if( trim( $comment ) != '' ) {
376 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
378 $log->addEntry( 'restore', $this->title
, $reason );
380 return array( $textRestored, $filesRestored, $reason );
384 * This is the meaty bit -- restores archived revisions of the given page
385 * to the cur/old tables. If the page currently exists, all revisions will
386 * be stuffed into old, otherwise the most recent will go into cur.
388 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
389 * @param $comment String
390 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
392 * @return Mixed: number of revisions restored or false on failure
394 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
395 if ( wfReadOnly() ) {
398 $restoreAll = empty( $timestamps );
400 $dbw = wfGetDB( DB_MASTER
);
402 # Does this page already exist? We'll have to update it...
403 $article = WikiPage
::factory( $this->title
);
404 # Load latest data for the current page (bug 31179)
405 $article->loadPageData( 'fromdbmaster' );
406 $oldcountable = $article->isCountable();
408 $page = $dbw->selectRow( 'page',
409 array( 'page_id', 'page_latest' ),
410 array( 'page_namespace' => $this->title
->getNamespace(),
411 'page_title' => $this->title
->getDBkey() ),
413 array( 'FOR UPDATE' ) // lock page
417 # Page already exists. Import the history, and if necessary
418 # we'll update the latest revision field in the record.
420 $pageId = $page->page_id
;
421 $previousRevId = $page->page_latest
;
422 # Get the time span of this page
423 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
424 array( 'rev_id' => $previousRevId ),
426 if( $previousTimestamp === false ) {
427 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
431 # Have to create a new article...
434 $previousTimestamp = 0;
438 $oldones = '1 = 1'; # All revisions...
440 $oldts = implode( ',',
441 array_map( array( &$dbw, 'addQuotes' ),
442 array_map( array( &$dbw, 'timestamp' ),
445 $oldones = "ar_timestamp IN ( {$oldts} )";
449 * Select each archived revision...
451 $result = $dbw->select( 'archive',
467 'ar_namespace' => $this->title
->getNamespace(),
468 'ar_title' => $this->title
->getDBkey(),
471 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
473 $ret = $dbw->resultObject( $result );
474 $rev_count = $dbw->numRows( $result );
476 wfDebug( __METHOD__
. ": no revisions to restore\n" );
480 $ret->seek( $rev_count - 1 ); // move to last
481 $row = $ret->fetchObject(); // get newest archived rev
482 $ret->seek( 0 ); // move back
485 // Check the state of the newest to-be version...
486 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
487 return false; // we can't leave the current revision like this!
489 // Safe to insert now...
490 $newid = $article->insertOn( $dbw );
493 // Check if a deleted revision will become the current revision...
494 if( $row->ar_timestamp
> $previousTimestamp ) {
495 // Check the state of the newest to-be version...
496 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
497 return false; // we can't leave the current revision like this!
505 foreach ( $ret as $row ) {
506 // Check for key dupes due to shitty archive integrity.
507 if( $row->ar_rev_id
) {
508 $exists = $dbw->selectField( 'revision', '1',
509 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
511 continue; // don't throw DB errors
514 // Insert one revision at a time...maintaining deletion status
515 // unless we are specifically removing all restrictions...
516 $revision = Revision
::newFromArchiveRow( $row,
519 'deleted' => $unsuppress ?
0 : $row->ar_deleted
522 $revision->insertOn( $dbw );
525 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
527 # Now that it's safely stored, take it out of the archive
528 $dbw->delete( 'archive',
530 'ar_namespace' => $this->title
->getNamespace(),
531 'ar_title' => $this->title
->getDBkey(),
535 // Was anything restored at all?
536 if ( $restored == 0 ) {
540 $created = (bool)$newid;
542 // Attach the latest revision to the page...
543 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
544 if ( $created ||
$wasnew ) {
545 // Update site stats, link tables, etc
546 $user = User
::newFromName( $revision->getRawUserText(), false );
547 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
550 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
552 if( $this->title
->getNamespace() == NS_FILE
) {
553 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
563 function getFileStatus() { return $this->fileStatus
; }
567 * Special page allowing users with the appropriate permissions to view
568 * and restore deleted content.
570 * @ingroup SpecialPage
572 class SpecialUndelete
extends SpecialPage
{
573 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
574 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
581 function __construct() {
582 parent
::__construct( 'Undelete', 'deletedhistory' );
585 function loadRequest( $par ) {
586 $request = $this->getRequest();
587 $user = $this->getUser();
589 $this->mAction
= $request->getVal( 'action' );
590 if ( $par !== null && $par !== '' ) {
591 $this->mTarget
= $par;
593 $this->mTarget
= $request->getVal( 'target' );
595 $this->mTargetObj
= null;
596 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
597 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
599 $this->mSearchPrefix
= $request->getText( 'prefix' );
600 $time = $request->getVal( 'timestamp' );
601 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
602 $this->mFilename
= $request->getVal( 'file' );
604 $posted = $request->wasPosted() &&
605 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
606 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
607 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
608 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
609 $this->mDiff
= $request->getCheck( 'diff' );
610 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
611 $this->mComment
= $request->getText( 'wpComment' );
612 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
613 $this->mToken
= $request->getVal( 'token' );
615 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
616 $this->mAllowed
= true; // user can restore
617 $this->mCanView
= true; // user can view content
618 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
619 $this->mAllowed
= false; // user cannot restore
620 $this->mCanView
= true; // user can view content
621 $this->mRestore
= false;
622 } else { // user can only view the list of revisions
623 $this->mAllowed
= false;
624 $this->mCanView
= false;
625 $this->mTimestamp
= '';
626 $this->mRestore
= false;
629 if( $this->mRestore ||
$this->mInvert
) {
630 $timestamps = array();
631 $this->mFileVersions
= array();
632 foreach( $request->getValues() as $key => $val ) {
634 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
635 array_push( $timestamps, $matches[1] );
638 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
639 $this->mFileVersions
[] = intval( $matches[1] );
642 rsort( $timestamps );
643 $this->mTargetTimestamp
= $timestamps;
647 function execute( $par ) {
648 $this->checkPermissions();
649 $user = $this->getUser();
652 $this->outputHeader();
654 $this->loadRequest( $par );
656 $out = $this->getOutput();
658 if ( is_null( $this->mTargetObj
) ) {
659 $out->addWikiMsg( 'undelete-header' );
661 # Not all users can just browse every deleted page from the list
662 if ( $user->isAllowed( 'browsearchive' ) ) {
663 $this->showSearchForm();
668 if ( $this->mAllowed
) {
669 $out->setPageTitle( $this->msg( 'undeletepage' ) );
671 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
674 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
676 if ( $this->mTimestamp
!== '' ) {
677 $this->showRevision( $this->mTimestamp
);
678 } elseif ( $this->mFilename
!== null ) {
679 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
680 // Check if user is allowed to see this file
681 if ( !$file->exists() ) {
682 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
683 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
684 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
685 throw new PermissionsError( 'suppressrevision' );
687 throw new PermissionsError( 'deletedtext' );
689 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
690 $this->showFileConfirmationForm( $this->mFilename
);
692 $this->showFile( $this->mFilename
);
694 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
697 $this->showHistory();
701 function showSearchForm() {
704 $out = $this->getOutput();
705 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
707 Xml
::openElement( 'form', array(
709 'action' => $wgScript ) ) .
710 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
711 Html
::hidden( 'title',
712 $this->getTitle()->getPrefixedDbKey() ) .
713 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
714 'prefix', 'prefix', 20,
715 $this->mSearchPrefix
) . ' ' .
716 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
717 Xml
::closeElement( 'fieldset' ) .
718 Xml
::closeElement( 'form' )
721 # List undeletable articles
722 if( $this->mSearchPrefix
) {
723 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
724 $this->showList( $result );
729 * Generic list of deleted pages
731 * @param $result ResultWrapper
734 private function showList( $result ) {
735 $out = $this->getOutput();
737 if( $result->numRows() == 0 ) {
738 $out->addWikiMsg( 'undelete-no-results' );
742 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
744 $undelete = $this->getTitle();
745 $out->addHTML( "<ul>\n" );
746 foreach ( $result as $row ) {
747 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
748 if ( $title !== null ) {
749 $item = Linker
::linkKnown(
751 htmlspecialchars( $title->getPrefixedText() ),
753 array( 'target' => $title->getPrefixedText() )
756 // The title is no longer valid, show as text
757 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
758 $item = htmlspecialchars( $title->getPrefixedText() );
760 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
761 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
764 $out->addHTML( "</ul>\n" );
769 private function showRevision( $timestamp ) {
770 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
774 $archive = new PageArchive( $this->mTargetObj
);
775 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
776 $rev = $archive->getRevision( $timestamp );
778 $out = $this->getOutput();
779 $user = $this->getUser();
782 $out->addWikiMsg( 'undeleterevision-missing' );
786 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
787 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
788 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
791 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
792 $out->addHTML( '<br />' );
793 // and we are allowed to see...
798 $previousRev = $archive->getPreviousRevision( $timestamp );
800 $this->showDiff( $previousRev, $rev );
801 if( $this->mDiffOnly
) {
804 $out->addHTML( '<hr />' );
807 $out->addWikiMsg( 'undelete-nodiff' );
811 $link = Linker
::linkKnown(
812 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
813 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
816 $lang = $this->getLanguage();
818 // date and time are separate parameters to facilitate localisation.
819 // $time is kept for backward compat reasons.
820 $time = $lang->userTimeAndDate( $timestamp, $user );
821 $d = $lang->userDate( $timestamp, $user );
822 $t = $lang->userTime( $timestamp, $user );
823 $userLink = Linker
::revUserTools( $rev );
825 if( $this->mPreview
) {
826 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
828 $openDiv = '<div id="mw-undelete-revision">';
830 $out->addHTML( $openDiv );
832 // Revision delete links
833 if ( !$this->mDiff
) {
834 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
836 $out->addHTML( "$revdel " );
840 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
841 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
842 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
844 if( $this->mPreview
) {
846 $popts = $out->parserOptions();
847 $popts->setEditSection( false );
848 $out->parserOptions( $popts );
849 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
853 Xml
::element( 'textarea', array(
854 'readonly' => 'readonly',
855 'cols' => intval( $user->getOption( 'cols' ) ),
856 'rows' => intval( $user->getOption( 'rows' ) ) ),
857 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) .
858 Xml
::openElement( 'div' ) .
859 Xml
::openElement( 'form', array(
861 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
862 Xml
::element( 'input', array(
865 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
866 Xml
::element( 'input', array(
868 'name' => 'timestamp',
869 'value' => $timestamp ) ) .
870 Xml
::element( 'input', array(
872 'name' => 'wpEditToken',
873 'value' => $user->getEditToken() ) ) .
874 Xml
::element( 'input', array(
877 'value' => $this->msg( 'showpreview' )->text() ) ) .
878 Xml
::element( 'input', array(
881 'value' => $this->msg( 'showdiff' )->text() ) ) .
882 Xml
::closeElement( 'form' ) .
883 Xml
::closeElement( 'div' ) );
887 * Build a diff display between this and the previous either deleted
888 * or non-deleted edit.
890 * @param $previousRev Revision
891 * @param $currentRev Revision
892 * @return String: HTML
894 function showDiff( $previousRev, $currentRev ) {
895 $diffEngine = new DifferenceEngine( $this->getContext() );
896 $diffEngine->showDiffStyle();
897 $this->getOutput()->addHTML(
899 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
900 "<col class='diff-marker' />" .
901 "<col class='diff-content' />" .
902 "<col class='diff-marker' />" .
903 "<col class='diff-content' />" .
905 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
906 $this->diffHeader( $previousRev, 'o' ) .
908 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
909 $this->diffHeader( $currentRev, 'n' ) .
912 $diffEngine->generateDiffBody(
913 $previousRev->getText(), $currentRev->getText() ) .
920 * @param $rev Revision
924 private function diffHeader( $rev, $prefix ) {
925 $isDeleted = !( $rev->getId() && $rev->getTitle() );
927 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
928 $targetPage = $this->getTitle();
929 $targetQuery = array(
930 'target' => $this->mTargetObj
->getPrefixedText(),
931 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
934 /// @todo FIXME: getId() may return non-zero for deleted revs...
935 $targetPage = $rev->getTitle();
936 $targetQuery = array( 'oldid' => $rev->getId() );
938 // Add show/hide deletion links if available
939 $user = $this->getUser();
940 $lang = $this->getLanguage();
941 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
942 if ( $rdel ) $rdel = " $rdel";
944 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
949 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
950 $lang->userDate( $rev->getTimestamp(), $user ),
951 $lang->userTime( $rev->getTimestamp(), $user )
957 '<div id="mw-diff-'.$prefix.'title2">' .
958 Linker
::revUserTools( $rev ) . '<br />' .
960 '<div id="mw-diff-'.$prefix.'title3">' .
961 Linker
::revComment( $rev ) . $rdel . '<br />' .
966 * Show a form confirming whether a tokenless user really wants to see a file
968 private function showFileConfirmationForm( $key ) {
969 $out = $this->getOutput();
970 $lang = $this->getLanguage();
971 $user = $this->getUser();
972 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
973 $out->addWikiMsg( 'undelete-show-file-confirm',
974 $this->mTargetObj
->getText(),
975 $lang->userDate( $file->getTimestamp(), $user ),
976 $lang->userTime( $file->getTimestamp(), $user ) );
978 Xml
::openElement( 'form', array(
980 'action' => $this->getTitle()->getLocalURL(
981 'target=' . urlencode( $this->mTarget
) .
982 '&file=' . urlencode( $key ) .
983 '&token=' . urlencode( $user->getEditToken( $key ) ) )
986 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
992 * Show a deleted file version requested by the visitor.
994 private function showFile( $key ) {
995 $this->getOutput()->disable();
997 # We mustn't allow the output to be Squid cached, otherwise
998 # if an admin previews a deleted image, and it's cached, then
999 # a user without appropriate permissions can toddle off and
1000 # nab the image, and Squid will serve it
1001 $response = $this->getRequest()->response();
1002 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1003 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1004 $response->header( 'Pragma: no-cache' );
1006 $repo = RepoGroup
::singleton()->getLocalRepo();
1007 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1008 $repo->streamFile( $path );
1011 private function showHistory() {
1012 $out = $this->getOutput();
1013 if( $this->mAllowed
) {
1014 $out->addModules( 'mediawiki.special.undelete' );
1017 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1018 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) )
1021 $archive = new PageArchive( $this->mTargetObj
);
1022 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1024 $text = $archive->getLastRevisionText();
1025 if( is_null( $text ) ) {
1026 $out->addWikiMsg( 'nohistory' );
1030 $out->addHTML( '<div class="mw-undelete-history">' );
1031 if ( $this->mAllowed
) {
1032 $out->addWikiMsg( 'undeletehistory' );
1033 $out->addWikiMsg( 'undeleterevdel' );
1035 $out->addWikiMsg( 'undeletehistorynoadmin' );
1037 $out->addHTML( '</div>' );
1039 # List all stored revisions
1040 $revisions = $archive->listRevisions();
1041 $files = $archive->listFiles();
1043 $haveRevisions = $revisions && $revisions->numRows() > 0;
1044 $haveFiles = $files && $files->numRows() > 0;
1046 # Batch existence check on user and talk pages
1047 if( $haveRevisions ) {
1048 $batch = new LinkBatch();
1049 foreach ( $revisions as $row ) {
1050 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1051 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1054 $revisions->seek( 0 );
1057 $batch = new LinkBatch();
1058 foreach ( $files as $row ) {
1059 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1060 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1066 if ( $this->mAllowed
) {
1067 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1068 # Start the form here
1069 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1070 $out->addHTML( $top );
1073 # Show relevant lines from the deletion log:
1074 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1075 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1076 # Show relevant lines from the suppression log:
1077 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1078 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1079 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1082 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1083 # Format the user-visible controls (comment field, submission button)
1084 # in a nice little table
1085 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1089 <td class='mw-input'>" .
1090 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1091 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1095 $unsuppressBox = '';
1098 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1099 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1101 <td colspan='2' class='mw-undelete-extrahelp'>" .
1102 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1106 <td class='mw-label'>" .
1107 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1109 <td class='mw-input'>" .
1110 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1115 <td class='mw-submit'>" .
1116 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1117 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1121 Xml
::closeElement( 'table' ) .
1122 Xml
::closeElement( 'fieldset' );
1124 $out->addHTML( $table );
1127 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1129 if( $haveRevisions ) {
1130 # The page's stored (deleted) history:
1131 $out->addHTML( '<ul>' );
1132 $remaining = $revisions->numRows();
1133 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1135 foreach ( $revisions as $row ) {
1137 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1140 $out->addHTML( '</ul>' );
1142 $out->addWikiMsg( 'nohistory' );
1146 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1147 $out->addHTML( '<ul>' );
1148 foreach ( $files as $row ) {
1149 $out->addHTML( $this->formatFileRow( $row ) );
1152 $out->addHTML( '</ul>' );
1155 if ( $this->mAllowed
) {
1156 # Slip in the hidden controls here
1157 $misc = Html
::hidden( 'target', $this->mTarget
);
1158 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1159 $misc .= Xml
::closeElement( 'form' );
1160 $out->addHTML( $misc );
1166 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1167 $rev = Revision
::newFromArchiveRow( $row,
1168 array( 'page' => $this->mTargetObj
->getArticleID() ) );
1170 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1171 // Build checkboxen...
1172 if( $this->mAllowed
) {
1173 if( $this->mInvert
) {
1174 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1175 $checkBox = Xml
::check( "ts$ts" );
1177 $checkBox = Xml
::check( "ts$ts", true );
1180 $checkBox = Xml
::check( "ts$ts" );
1185 $user = $this->getUser();
1186 // Build page & diff links...
1187 if( $this->mCanView
) {
1188 $titleObj = $this->getTitle();
1190 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1191 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1192 $last = $this->msg( 'diff' )->escaped();
1193 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1194 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1195 $last = Linker
::linkKnown(
1197 $this->msg( 'diff' )->escaped(),
1200 'target' => $this->mTargetObj
->getPrefixedText(),
1206 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1207 $last = $this->msg( 'diff' )->escaped();
1210 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1211 $last = $this->msg( 'diff' )->escaped();
1214 $userLink = Linker
::revUserTools( $rev );
1215 // Revision text size
1216 $size = $row->ar_len
;
1217 if( !is_null( $size ) ) {
1218 $revTextSize = Linker
::formatRevisionSize( $size );
1221 $comment = Linker
::revComment( $rev );
1222 // Revision delete links
1223 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1225 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1226 return "<li>$revisionRow</li>";
1229 private function formatFileRow( $row ) {
1230 $file = ArchivedFile
::newFromRow( $row );
1232 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1233 $user = $this->getUser();
1234 if( $this->mAllowed
&& $row->fa_storage_key
) {
1235 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1236 $key = urlencode( $row->fa_storage_key
);
1237 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1240 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1242 $userLink = $this->getFileUser( $file );
1243 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1244 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1245 $data = htmlspecialchars( $data . ' ' . $bytes );
1246 $comment = $this->getFileComment( $file );
1248 // Add show/hide deletion links if available
1249 $canHide = $user->isAllowed( 'deleterevision' );
1250 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1251 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1252 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1255 'type' => 'filearchive',
1256 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1257 'ids' => $row->fa_id
1259 $revdlink = Linker
::revDeleteLink( $query,
1260 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1266 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1270 * Fetch revision text link if it's available to all users
1272 * @param $rev Revision
1273 * @param $titleObj Title
1274 * @param $ts string Timestamp
1277 function getPageLink( $rev, $titleObj, $ts ) {
1278 $user = $this->getUser();
1279 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1281 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1282 return '<span class="history-deleted">' . $time . '</span>';
1284 $link = Linker
::linkKnown(
1286 htmlspecialchars( $time ),
1289 'target' => $this->mTargetObj
->getPrefixedText(),
1293 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1294 $link = '<span class="history-deleted">' . $link . '</span>';
1301 * Fetch image view link if it's available to all users
1304 * @param $titleObj Title
1305 * @param $ts string A timestamp
1306 * @param $key String: a storage key
1308 * @return String: HTML fragment
1310 function getFileLink( $file, $titleObj, $ts, $key ) {
1311 $user = $this->getUser();
1312 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1314 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1315 return '<span class="history-deleted">' . $time . '</span>';
1317 $link = Linker
::linkKnown(
1319 htmlspecialchars( $time ),
1322 'target' => $this->mTargetObj
->getPrefixedText(),
1324 'token' => $user->getEditToken( $key )
1327 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1328 $link = '<span class="history-deleted">' . $link . '</span>';
1335 * Fetch file's user id if it's available to this user
1338 * @return String: HTML fragment
1340 function getFileUser( $file ) {
1341 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1342 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1344 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1345 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1346 if( $file->isDeleted( File
::DELETED_USER
) ) {
1347 $link = '<span class="history-deleted">' . $link . '</span>';
1354 * Fetch file upload comment if it's available to this user
1357 * @return String: HTML fragment
1359 function getFileComment( $file ) {
1360 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1361 return '<span class="history-deleted"><span class="comment">' .
1362 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1364 $link = Linker
::commentBlock( $file->getRawDescription() );
1365 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1366 $link = '<span class="history-deleted">' . $link . '</span>';
1372 function undelete() {
1373 global $wgUploadMaintenance;
1375 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1376 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1379 if ( wfReadOnly() ) {
1380 throw new ReadOnlyError
;
1383 $out = $this->getOutput();
1384 $archive = new PageArchive( $this->mTargetObj
);
1385 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1386 $ok = $archive->undelete(
1387 $this->mTargetTimestamp
,
1389 $this->mFileVersions
,
1390 $this->mUnsuppress
);
1392 if( is_array( $ok ) ) {
1393 if ( $ok[1] ) { // Undeleted file count
1394 wfRunHooks( 'FileUndeleteComplete', array(
1395 $this->mTargetObj
, $this->mFileVersions
,
1396 $this->getUser(), $this->mComment
) );
1399 $link = Linker
::linkKnown( $this->mTargetObj
);
1400 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1402 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1403 $out->addWikiMsg( 'cannotundelete' );
1404 $out->addWikiMsg( 'undeleterevdel' );
1407 // Show file deletion warnings and errors
1408 $status = $archive->getFileStatus();
1409 if( $status && !$status->isGood() ) {
1410 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );