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',
120 'ar_content_format', 'ar_content_model'
122 array( 'ar_namespace' => $this->title
->getNamespace(),
123 'ar_title' => $this->title
->getDBkey() ),
124 'PageArchive::listRevisions',
125 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
126 $ret = $dbr->resultObject( $res );
131 * List the deleted file revisions for this page, if it's a file page.
132 * Returns a result wrapper with various filearchive fields, or null
133 * if not a file page.
135 * @return ResultWrapper
136 * @todo Does this belong in Image for fuller encapsulation?
138 function listFiles() {
139 if( $this->title
->getNamespace() == NS_FILE
) {
140 $dbr = wfGetDB( DB_SLAVE
);
141 $res = $dbr->select( 'filearchive',
161 array( 'fa_name' => $this->title
->getDBkey() ),
163 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
164 $ret = $dbr->resultObject( $res );
171 * Return a Revision object containing data for the deleted revision.
172 * Note that the result *may* or *may not* have a null page ID.
174 * @param $timestamp String
177 function getRevision( $timestamp ) {
178 $dbr = wfGetDB( DB_SLAVE
);
179 $row = $dbr->selectRow( 'archive',
196 array( 'ar_namespace' => $this->title
->getNamespace(),
197 'ar_title' => $this->title
->getDBkey(),
198 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
201 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleID() ) );
208 * Return the most-previous revision, either live or deleted, against
209 * the deleted revision given by timestamp.
211 * May produce unexpected results in case of history merges or other
212 * unusual time issues.
214 * @param $timestamp String
215 * @return Revision or null
217 function getPreviousRevision( $timestamp ) {
218 $dbr = wfGetDB( DB_SLAVE
);
220 // Check the previous deleted revision...
221 $row = $dbr->selectRow( 'archive',
223 array( 'ar_namespace' => $this->title
->getNamespace(),
224 'ar_title' => $this->title
->getDBkey(),
226 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
229 'ORDER BY' => 'ar_timestamp DESC',
231 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
233 $row = $dbr->selectRow( array( 'page', 'revision' ),
234 array( 'rev_id', 'rev_timestamp' ),
236 'page_namespace' => $this->title
->getNamespace(),
237 'page_title' => $this->title
->getDBkey(),
238 'page_id = rev_page',
240 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
243 'ORDER BY' => 'rev_timestamp DESC',
245 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
246 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
248 if( $prevLive && $prevLive > $prevDeleted ) {
249 // Most prior revision was live
250 return Revision
::newFromId( $prevLiveId );
251 } elseif( $prevDeleted ) {
252 // Most prior revision was deleted
253 return $this->getRevision( $prevDeleted );
255 // No prior revision on this page.
261 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
263 * @param $row Object: database row
266 function getTextFromRow( $row ) {
267 if( is_null( $row->ar_text_id
) ) {
268 // An old row from MediaWiki 1.4 or previous.
269 // Text is embedded in this row in classic compression format.
270 return Revision
::getRevisionText( $row, 'ar_' );
272 // New-style: keyed to the text storage backend.
273 $dbr = wfGetDB( DB_SLAVE
);
274 $text = $dbr->selectRow( 'text',
275 array( 'old_text', 'old_flags' ),
276 array( 'old_id' => $row->ar_text_id
),
278 return Revision
::getRevisionText( $text );
283 * Fetch (and decompress if necessary) the stored text of the most
284 * recently edited deleted revision of the page.
286 * If there are no archived revisions for the page, returns NULL.
290 function getLastRevisionText() {
291 $dbr = wfGetDB( DB_SLAVE
);
292 $row = $dbr->selectRow( 'archive',
293 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
294 array( 'ar_namespace' => $this->title
->getNamespace(),
295 'ar_title' => $this->title
->getDBkey() ),
297 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
299 return $this->getTextFromRow( $row );
306 * Quick check if any archived revisions are present for the page.
310 function isDeleted() {
311 $dbr = wfGetDB( DB_SLAVE
);
312 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
313 array( 'ar_namespace' => $this->title
->getNamespace(),
314 'ar_title' => $this->title
->getDBkey() ) );
319 * Restore the given (or all) text and file revisions for the page.
320 * Once restored, the items will be removed from the archive tables.
321 * The deletion log will be updated with an undeletion notice.
323 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
324 * @param $comment String
325 * @param $fileVersions Array
326 * @param $unsuppress Boolean
328 * @return array(number of file revisions restored, number of image revisions restored, log message)
329 * on success, false on failure
331 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
332 // If both the set of text revisions and file revisions are empty,
333 // restore everything. Otherwise, just restore the requested items.
334 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
336 $restoreText = $restoreAll ||
!empty( $timestamps );
337 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
339 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
340 $img = wfLocalFile( $this->title
);
341 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
342 if ( !$this->fileStatus
->isOk() ) {
345 $filesRestored = $this->fileStatus
->successCount
;
351 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
352 if( $textRestored === false ) { // It must be one of UNDELETE_*
361 $log = new LogPage( 'delete' );
363 if( $textRestored && $filesRestored ) {
364 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
365 $wgContLang->formatNum( $textRestored ),
366 $wgContLang->formatNum( $filesRestored ) );
367 } elseif( $textRestored ) {
368 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
369 $wgContLang->formatNum( $textRestored ) );
370 } elseif( $filesRestored ) {
371 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
372 $wgContLang->formatNum( $filesRestored ) );
374 wfDebug( "Undelete: nothing undeleted...\n" );
378 if( trim( $comment ) != '' ) {
379 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
381 $log->addEntry( 'restore', $this->title
, $reason );
383 return array( $textRestored, $filesRestored, $reason );
387 * This is the meaty bit -- restores archived revisions of the given page
388 * to the cur/old tables. If the page currently exists, all revisions will
389 * be stuffed into old, otherwise the most recent will go into cur.
391 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
392 * @param $comment String
393 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
395 * @return Mixed: number of revisions restored or false on failure
397 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
398 if ( wfReadOnly() ) {
401 $restoreAll = empty( $timestamps );
403 $dbw = wfGetDB( DB_MASTER
);
405 # Does this page already exist? We'll have to update it...
406 $article = WikiPage
::factory( $this->title
);
407 # Load latest data for the current page (bug 31179)
408 $article->loadPageData( 'fromdbmaster' );
409 $oldcountable = $article->isCountable();
411 $page = $dbw->selectRow( 'page',
412 array( 'page_id', 'page_latest' ),
413 array( 'page_namespace' => $this->title
->getNamespace(),
414 'page_title' => $this->title
->getDBkey() ),
416 array( 'FOR UPDATE' ) // lock page
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',
470 'ar_content_model' ),
472 'ar_namespace' => $this->title
->getNamespace(),
473 'ar_title' => $this->title
->getDBkey(),
476 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
478 $ret = $dbw->resultObject( $result );
479 $rev_count = $dbw->numRows( $result );
481 wfDebug( __METHOD__
. ": no revisions to restore\n" );
485 $ret->seek( $rev_count - 1 ); // move to last
486 $row = $ret->fetchObject(); // get newest archived rev
487 $ret->seek( 0 ); // move back
490 // Check the state of the newest to-be version...
491 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
492 return false; // we can't leave the current revision like this!
494 // Safe to insert now...
495 $newid = $article->insertOn( $dbw );
498 // Check if a deleted revision will become the current revision...
499 if( $row->ar_timestamp
> $previousTimestamp ) {
500 // Check the state of the newest to-be version...
501 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
502 return false; // we can't leave the current revision like this!
510 foreach ( $ret as $row ) {
511 // Check for key dupes due to shitty archive integrity.
512 if( $row->ar_rev_id
) {
513 $exists = $dbw->selectField( 'revision', '1',
514 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
516 continue; // don't throw DB errors
519 // Insert one revision at a time...maintaining deletion status
520 // unless we are specifically removing all restrictions...
521 $revision = Revision
::newFromArchiveRow( $row,
524 'deleted' => $unsuppress ?
0 : $row->ar_deleted
527 $revision->insertOn( $dbw );
530 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
532 # Now that it's safely stored, take it out of the archive
533 $dbw->delete( 'archive',
535 'ar_namespace' => $this->title
->getNamespace(),
536 'ar_title' => $this->title
->getDBkey(),
540 // Was anything restored at all?
541 if ( $restored == 0 ) {
545 $created = (bool)$newid;
547 // Attach the latest revision to the page...
548 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
549 if ( $created ||
$wasnew ) {
550 // Update site stats, link tables, etc
551 $user = User
::newFromName( $revision->getRawUserText(), false );
552 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
555 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
557 if( $this->title
->getNamespace() == NS_FILE
) {
558 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
568 function getFileStatus() { return $this->fileStatus
; }
572 * Special page allowing users with the appropriate permissions to view
573 * and restore deleted content.
575 * @ingroup SpecialPage
577 class SpecialUndelete
extends SpecialPage
{
578 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
579 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
586 function __construct() {
587 parent
::__construct( 'Undelete', 'deletedhistory' );
590 function loadRequest( $par ) {
591 $request = $this->getRequest();
592 $user = $this->getUser();
594 $this->mAction
= $request->getVal( 'action' );
595 if ( $par !== null && $par !== '' ) {
596 $this->mTarget
= $par;
598 $this->mTarget
= $request->getVal( 'target' );
600 $this->mTargetObj
= null;
601 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
602 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
604 $this->mSearchPrefix
= $request->getText( 'prefix' );
605 $time = $request->getVal( 'timestamp' );
606 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
607 $this->mFilename
= $request->getVal( 'file' );
609 $posted = $request->wasPosted() &&
610 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
611 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
612 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
613 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
614 $this->mDiff
= $request->getCheck( 'diff' );
615 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
616 $this->mComment
= $request->getText( 'wpComment' );
617 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
618 $this->mToken
= $request->getVal( 'token' );
620 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
621 $this->mAllowed
= true; // user can restore
622 $this->mCanView
= true; // user can view content
623 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
624 $this->mAllowed
= false; // user cannot restore
625 $this->mCanView
= true; // user can view content
626 $this->mRestore
= false;
627 } else { // user can only view the list of revisions
628 $this->mAllowed
= false;
629 $this->mCanView
= false;
630 $this->mTimestamp
= '';
631 $this->mRestore
= false;
634 if( $this->mRestore ||
$this->mInvert
) {
635 $timestamps = array();
636 $this->mFileVersions
= array();
637 foreach( $request->getValues() as $key => $val ) {
639 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
640 array_push( $timestamps, $matches[1] );
643 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
644 $this->mFileVersions
[] = intval( $matches[1] );
647 rsort( $timestamps );
648 $this->mTargetTimestamp
= $timestamps;
652 function execute( $par ) {
653 $this->checkPermissions();
654 $user = $this->getUser();
657 $this->outputHeader();
659 $this->loadRequest( $par );
661 $out = $this->getOutput();
663 if ( is_null( $this->mTargetObj
) ) {
664 $out->addWikiMsg( 'undelete-header' );
666 # Not all users can just browse every deleted page from the list
667 if ( $user->isAllowed( 'browsearchive' ) ) {
668 $this->showSearchForm();
673 if ( $this->mAllowed
) {
674 $out->setPageTitle( $this->msg( 'undeletepage' ) );
676 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
679 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
681 if ( $this->mTimestamp
!== '' ) {
682 $this->showRevision( $this->mTimestamp
);
683 } elseif ( $this->mFilename
!== null ) {
684 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
685 // Check if user is allowed to see this file
686 if ( !$file->exists() ) {
687 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
688 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
689 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
690 throw new PermissionsError( 'suppressrevision' );
692 throw new PermissionsError( 'deletedtext' );
694 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
695 $this->showFileConfirmationForm( $this->mFilename
);
697 $this->showFile( $this->mFilename
);
699 } elseif ( $this->mRestore
&& $this->mAction
== 'submit' ) {
702 $this->showHistory();
706 function showSearchForm() {
709 $out = $this->getOutput();
710 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
712 Xml
::openElement( 'form', array(
714 'action' => $wgScript ) ) .
715 Xml
::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
716 Html
::hidden( 'title',
717 $this->getTitle()->getPrefixedDbKey() ) .
718 Xml
::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
719 'prefix', 'prefix', 20,
720 $this->mSearchPrefix
) . ' ' .
721 Xml
::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
722 Xml
::closeElement( 'fieldset' ) .
723 Xml
::closeElement( 'form' )
726 # List undeletable articles
727 if( $this->mSearchPrefix
) {
728 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
729 $this->showList( $result );
734 * Generic list of deleted pages
736 * @param $result ResultWrapper
739 private function showList( $result ) {
740 $out = $this->getOutput();
742 if( $result->numRows() == 0 ) {
743 $out->addWikiMsg( 'undelete-no-results' );
747 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
749 $undelete = $this->getTitle();
750 $out->addHTML( "<ul>\n" );
751 foreach ( $result as $row ) {
752 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
753 if ( $title !== null ) {
754 $item = Linker
::linkKnown(
756 htmlspecialchars( $title->getPrefixedText() ),
758 array( 'target' => $title->getPrefixedText() )
761 // The title is no longer valid, show as text
762 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
763 $item = htmlspecialchars( $title->getPrefixedText() );
765 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
766 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
769 $out->addHTML( "</ul>\n" );
774 private function showRevision( $timestamp ) {
775 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
779 $archive = new PageArchive( $this->mTargetObj
);
780 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
781 $rev = $archive->getRevision( $timestamp );
783 $out = $this->getOutput();
784 $user = $this->getUser();
787 $out->addWikiMsg( 'undeleterevision-missing' );
791 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
792 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
793 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
796 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
797 $out->addHTML( '<br />' );
798 // and we are allowed to see...
803 $previousRev = $archive->getPreviousRevision( $timestamp );
805 $this->showDiff( $previousRev, $rev );
806 if( $this->mDiffOnly
) {
809 $out->addHTML( '<hr />' );
812 $out->addWikiMsg( 'undelete-nodiff' );
816 $link = Linker
::linkKnown(
817 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
818 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
821 $lang = $this->getLanguage();
823 // date and time are separate parameters to facilitate localisation.
824 // $time is kept for backward compat reasons.
825 $time = $lang->userTimeAndDate( $timestamp, $user );
826 $d = $lang->userDate( $timestamp, $user );
827 $t = $lang->userTime( $timestamp, $user );
828 $userLink = Linker
::revUserTools( $rev );
830 if( $this->mPreview
) {
831 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
833 $openDiv = '<div id="mw-undelete-revision">';
835 $out->addHTML( $openDiv );
837 // Revision delete links
838 if ( !$this->mDiff
) {
839 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
841 $out->addHTML( "$revdel " );
845 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
846 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
847 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
849 if( $this->mPreview
) {
851 $popts = $out->parserOptions();
852 $popts->setEditSection( false );
853 $out->parserOptions( $popts );
854 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
, $user ), $this->mTargetObj
, true );
858 Xml
::element( 'textarea', array(
859 'readonly' => 'readonly',
860 'cols' => intval( $user->getOption( 'cols' ) ),
861 'rows' => intval( $user->getOption( 'rows' ) ) ),
862 $rev->getText( Revision
::FOR_THIS_USER
, $user ) . "\n" ) .
863 Xml
::openElement( 'div' ) .
864 Xml
::openElement( 'form', array(
866 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
867 Xml
::element( 'input', array(
870 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
871 Xml
::element( 'input', array(
873 'name' => 'timestamp',
874 'value' => $timestamp ) ) .
875 Xml
::element( 'input', array(
877 'name' => 'wpEditToken',
878 'value' => $user->getEditToken() ) ) .
879 Xml
::element( 'input', array(
882 'value' => $this->msg( 'showpreview' )->text() ) ) .
883 Xml
::element( 'input', array(
886 'value' => $this->msg( 'showdiff' )->text() ) ) .
887 Xml
::closeElement( 'form' ) .
888 Xml
::closeElement( 'div' ) );
892 * Build a diff display between this and the previous either deleted
893 * or non-deleted edit.
895 * @param $previousRev Revision
896 * @param $currentRev Revision
897 * @return String: HTML
899 function showDiff( $previousRev, $currentRev ) {
900 $contentHandler = ContentHandler
::getForTitle( $this->getTitle() );
901 $diffEngine = $contentHandler->getDifferenceEngine( $this->getContext() );
902 $diffEngine->showDiffStyle();
903 $this->getOutput()->addHTML(
905 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
906 "<col class='diff-marker' />" .
907 "<col class='diff-content' />" .
908 "<col class='diff-marker' />" .
909 "<col class='diff-content' />" .
911 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
912 $this->diffHeader( $previousRev, 'o' ) .
914 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
915 $this->diffHeader( $currentRev, 'n' ) .
918 $diffEngine->generateContentDiffBody(
919 $previousRev->getContent(), $currentRev->getContent() ) .
926 * @param $rev Revision
930 private function diffHeader( $rev, $prefix ) {
931 $isDeleted = !( $rev->getId() && $rev->getTitle() );
933 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
934 $targetPage = $this->getTitle();
935 $targetQuery = array(
936 'target' => $this->mTargetObj
->getPrefixedText(),
937 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
940 /// @todo FIXME: getId() may return non-zero for deleted revs...
941 $targetPage = $rev->getTitle();
942 $targetQuery = array( 'oldid' => $rev->getId() );
944 // Add show/hide deletion links if available
945 $user = $this->getUser();
946 $lang = $this->getLanguage();
947 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
948 if ( $rdel ) $rdel = " $rdel";
950 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
955 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
956 $lang->userDate( $rev->getTimestamp(), $user ),
957 $lang->userTime( $rev->getTimestamp(), $user )
963 '<div id="mw-diff-'.$prefix.'title2">' .
964 Linker
::revUserTools( $rev ) . '<br />' .
966 '<div id="mw-diff-'.$prefix.'title3">' .
967 Linker
::revComment( $rev ) . $rdel . '<br />' .
972 * Show a form confirming whether a tokenless user really wants to see a file
974 private function showFileConfirmationForm( $key ) {
975 $out = $this->getOutput();
976 $lang = $this->getLanguage();
977 $user = $this->getUser();
978 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
979 $out->addWikiMsg( 'undelete-show-file-confirm',
980 $this->mTargetObj
->getText(),
981 $lang->userDate( $file->getTimestamp(), $user ),
982 $lang->userTime( $file->getTimestamp(), $user ) );
984 Xml
::openElement( 'form', array(
986 'action' => $this->getTitle()->getLocalURL(
987 'target=' . urlencode( $this->mTarget
) .
988 '&file=' . urlencode( $key ) .
989 '&token=' . urlencode( $user->getEditToken( $key ) ) )
992 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
998 * Show a deleted file version requested by the visitor.
1000 private function showFile( $key ) {
1001 $this->getOutput()->disable();
1003 # We mustn't allow the output to be Squid cached, otherwise
1004 # if an admin previews a deleted image, and it's cached, then
1005 # a user without appropriate permissions can toddle off and
1006 # nab the image, and Squid will serve it
1007 $response = $this->getRequest()->response();
1008 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1009 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1010 $response->header( 'Pragma: no-cache' );
1012 $repo = RepoGroup
::singleton()->getLocalRepo();
1013 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1014 $repo->streamFile( $path );
1017 private function showHistory() {
1018 $out = $this->getOutput();
1019 if( $this->mAllowed
) {
1020 $out->addModules( 'mediawiki.special.undelete' );
1023 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1024 array( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() )
1027 $archive = new PageArchive( $this->mTargetObj
);
1028 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1030 $text = $archive->getLastRevisionText();
1031 if( is_null( $text ) ) {
1032 $out->addWikiMsg( 'nohistory' );
1036 $out->addHTML( '<div class="mw-undelete-history">' );
1037 if ( $this->mAllowed
) {
1038 $out->addWikiMsg( 'undeletehistory' );
1039 $out->addWikiMsg( 'undeleterevdel' );
1041 $out->addWikiMsg( 'undeletehistorynoadmin' );
1043 $out->addHTML( '</div>' );
1045 # List all stored revisions
1046 $revisions = $archive->listRevisions();
1047 $files = $archive->listFiles();
1049 $haveRevisions = $revisions && $revisions->numRows() > 0;
1050 $haveFiles = $files && $files->numRows() > 0;
1052 # Batch existence check on user and talk pages
1053 if( $haveRevisions ) {
1054 $batch = new LinkBatch();
1055 foreach ( $revisions as $row ) {
1056 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1057 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1060 $revisions->seek( 0 );
1063 $batch = new LinkBatch();
1064 foreach ( $files as $row ) {
1065 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1066 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1072 if ( $this->mAllowed
) {
1073 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1074 # Start the form here
1075 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1076 $out->addHTML( $top );
1079 # Show relevant lines from the deletion log:
1080 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1081 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
1082 # Show relevant lines from the suppression log:
1083 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1084 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1085 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
1088 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1089 # Format the user-visible controls (comment field, submission button)
1090 # in a nice little table
1091 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1095 <td class='mw-input'>" .
1096 Xml
::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1097 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress
).
1101 $unsuppressBox = '';
1104 Xml
::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1105 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1107 <td colspan='2' class='mw-undelete-extrahelp'>" .
1108 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1112 <td class='mw-label'>" .
1113 Xml
::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1115 <td class='mw-input'>" .
1116 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1121 <td class='mw-submit'>" .
1122 Xml
::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1123 Xml
::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1127 Xml
::closeElement( 'table' ) .
1128 Xml
::closeElement( 'fieldset' );
1130 $out->addHTML( $table );
1133 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1135 if( $haveRevisions ) {
1136 # The page's stored (deleted) history:
1137 $out->addHTML( '<ul>' );
1138 $remaining = $revisions->numRows();
1139 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1141 foreach ( $revisions as $row ) {
1143 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1146 $out->addHTML( '</ul>' );
1148 $out->addWikiMsg( 'nohistory' );
1152 $out->addHTML( Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1153 $out->addHTML( '<ul>' );
1154 foreach ( $files as $row ) {
1155 $out->addHTML( $this->formatFileRow( $row ) );
1158 $out->addHTML( '</ul>' );
1161 if ( $this->mAllowed
) {
1162 # Slip in the hidden controls here
1163 $misc = Html
::hidden( 'target', $this->mTarget
);
1164 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1165 $misc .= Xml
::closeElement( 'form' );
1166 $out->addHTML( $misc );
1172 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1173 $rev = Revision
::newFromArchiveRow( $row,
1174 array( 'page' => $this->mTargetObj
->getArticleID() ) );
1176 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1177 // Build checkboxen...
1178 if( $this->mAllowed
) {
1179 if( $this->mInvert
) {
1180 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1181 $checkBox = Xml
::check( "ts$ts" );
1183 $checkBox = Xml
::check( "ts$ts", true );
1186 $checkBox = Xml
::check( "ts$ts" );
1191 $user = $this->getUser();
1192 // Build page & diff links...
1193 if( $this->mCanView
) {
1194 $titleObj = $this->getTitle();
1196 if( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
1197 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1198 $last = $this->msg( 'diff' )->escaped();
1199 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1200 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1201 $last = Linker
::linkKnown(
1203 $this->msg( 'diff' )->escaped(),
1206 'target' => $this->mTargetObj
->getPrefixedText(),
1212 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1213 $last = $this->msg( 'diff' )->escaped();
1216 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1217 $last = $this->msg( 'diff' )->escaped();
1220 $userLink = Linker
::revUserTools( $rev );
1221 // Revision text size
1222 $size = $row->ar_len
;
1223 if( !is_null( $size ) ) {
1224 $stxt = Linker
::formatRevisionSize( $size );
1227 $comment = Linker
::revComment( $rev );
1228 // Revision delete links
1229 $revdlink = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
1230 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1233 private function formatFileRow( $row ) {
1234 $file = ArchivedFile
::newFromRow( $row );
1236 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1237 $user = $this->getUser();
1238 if( $this->mAllowed
&& $row->fa_storage_key
) {
1239 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1240 $key = urlencode( $row->fa_storage_key
);
1241 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1244 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1246 $userLink = $this->getFileUser( $file );
1247 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1248 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )->plain();
1249 $data = htmlspecialchars( $data . ' ' . $bytes );
1250 $comment = $this->getFileComment( $file );
1252 // Add show/hide deletion links if available
1253 $canHide = $user->isAllowed( 'deleterevision' );
1254 if( $canHide ||
( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1255 if( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1256 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1259 'type' => 'filearchive',
1260 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1261 'ids' => $row->fa_id
1263 $revdlink = Linker
::revDeleteLink( $query,
1264 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1270 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1274 * Fetch revision text link if it's available to all users
1276 * @param $rev Revision
1277 * @param $titleObj Title
1278 * @param $ts string Timestamp
1281 function getPageLink( $rev, $titleObj, $ts ) {
1282 $user = $this->getUser();
1283 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1285 if( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1286 return '<span class="history-deleted">' . $time . '</span>';
1288 $link = Linker
::linkKnown(
1290 htmlspecialchars( $time ),
1293 'target' => $this->mTargetObj
->getPrefixedText(),
1297 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1298 $link = '<span class="history-deleted">' . $link . '</span>';
1305 * Fetch image view link if it's available to all users
1308 * @param $titleObj Title
1309 * @param $ts string A timestamp
1310 * @param $key String: a storage key
1312 * @return String: HTML fragment
1314 function getFileLink( $file, $titleObj, $ts, $key ) {
1315 $user = $this->getUser();
1316 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1318 if( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1319 return '<span class="history-deleted">' . $time . '</span>';
1321 $link = Linker
::linkKnown(
1323 htmlspecialchars( $time ),
1326 'target' => $this->mTargetObj
->getPrefixedText(),
1328 'token' => $user->getEditToken( $key )
1331 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1332 $link = '<span class="history-deleted">' . $link . '</span>';
1339 * Fetch file's user id if it's available to this user
1342 * @return String: HTML fragment
1344 function getFileUser( $file ) {
1345 if( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1346 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1348 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1349 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1350 if( $file->isDeleted( File
::DELETED_USER
) ) {
1351 $link = '<span class="history-deleted">' . $link . '</span>';
1358 * Fetch file upload comment if it's available to this user
1361 * @return String: HTML fragment
1363 function getFileComment( $file ) {
1364 if( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1365 return '<span class="history-deleted"><span class="comment">' .
1366 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1368 $link = Linker
::commentBlock( $file->getRawDescription() );
1369 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1370 $link = '<span class="history-deleted">' . $link . '</span>';
1376 function undelete() {
1377 global $wgUploadMaintenance;
1379 if ( $wgUploadMaintenance && $this->mTargetObj
->getNamespace() == NS_FILE
) {
1380 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1383 if ( wfReadOnly() ) {
1384 throw new ReadOnlyError
;
1387 $out = $this->getOutput();
1388 $archive = new PageArchive( $this->mTargetObj
);
1389 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1390 $ok = $archive->undelete(
1391 $this->mTargetTimestamp
,
1393 $this->mFileVersions
,
1394 $this->mUnsuppress
);
1396 if( is_array( $ok ) ) {
1397 if ( $ok[1] ) { // Undeleted file count
1398 wfRunHooks( 'FileUndeleteComplete', array(
1399 $this->mTargetObj
, $this->mFileVersions
,
1400 $this->getUser(), $this->mComment
) );
1403 $link = Linker
::linkKnown( $this->mTargetObj
);
1404 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1406 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1407 $out->addWikiMsg( 'cannotundelete' );
1408 $out->addWikiMsg( 'undeleterevdel' );
1411 // Show file deletion warnings and errors
1412 $status = $archive->getFileStatus();
1413 if( $status && !$status->isGood() ) {
1414 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );