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() ),
81 return self
::listPages( $dbr, $conds );
85 * @param $dbr DatabaseBase
87 * @return bool|ResultWrapper
89 protected static function listPages( $dbr, $condition ) {
90 return $dbr->resultObject(
101 'GROUP BY' => 'ar_namespace,ar_title',
102 'ORDER BY' => 'ar_namespace,ar_title',
110 * List the revisions of the given page. Returns result wrapper with
111 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
113 * @return ResultWrapper
115 function listRevisions() {
116 $dbr = wfGetDB( DB_SLAVE
);
117 $res = $dbr->select( 'archive',
119 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
120 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id'
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',
192 array( 'ar_namespace' => $this->title
->getNamespace(),
193 'ar_title' => $this->title
->getDBkey(),
194 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
197 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
204 * Return the most-previous revision, either live or deleted, against
205 * the deleted revision given by timestamp.
207 * May produce unexpected results in case of history merges or other
208 * unusual time issues.
210 * @param $timestamp String
211 * @return Revision or null
213 function getPreviousRevision( $timestamp ) {
214 $dbr = wfGetDB( DB_SLAVE
);
216 // Check the previous deleted revision...
217 $row = $dbr->selectRow( 'archive',
219 array( 'ar_namespace' => $this->title
->getNamespace(),
220 'ar_title' => $this->title
->getDBkey(),
222 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
225 'ORDER BY' => 'ar_timestamp DESC',
227 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
229 $row = $dbr->selectRow( array( 'page', 'revision' ),
230 array( 'rev_id', 'rev_timestamp' ),
232 'page_namespace' => $this->title
->getNamespace(),
233 'page_title' => $this->title
->getDBkey(),
234 'page_id = rev_page',
236 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
239 'ORDER BY' => 'rev_timestamp DESC',
241 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
242 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
244 if( $prevLive && $prevLive > $prevDeleted ) {
245 // Most prior revision was live
246 return Revision
::newFromId( $prevLiveId );
247 } elseif( $prevDeleted ) {
248 // Most prior revision was deleted
249 return $this->getRevision( $prevDeleted );
251 // No prior revision on this page.
257 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
259 * @param $row Object: database row
262 function getTextFromRow( $row ) {
263 if( is_null( $row->ar_text_id
) ) {
264 // An old row from MediaWiki 1.4 or previous.
265 // Text is embedded in this row in classic compression format.
266 return Revision
::getRevisionText( $row, 'ar_' );
268 // New-style: keyed to the text storage backend.
269 $dbr = wfGetDB( DB_SLAVE
);
270 $text = $dbr->selectRow( 'text',
271 array( 'old_text', 'old_flags' ),
272 array( 'old_id' => $row->ar_text_id
),
274 return Revision
::getRevisionText( $text );
279 * Fetch (and decompress if necessary) the stored text of the most
280 * recently edited deleted revision of the page.
282 * If there are no archived revisions for the page, returns NULL.
286 function getLastRevisionText() {
287 $dbr = wfGetDB( DB_SLAVE
);
288 $row = $dbr->selectRow( 'archive',
289 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
290 array( 'ar_namespace' => $this->title
->getNamespace(),
291 'ar_title' => $this->title
->getDBkey() ),
293 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
295 return $this->getTextFromRow( $row );
302 * Quick check if any archived revisions are present for the page.
306 function isDeleted() {
307 $dbr = wfGetDB( DB_SLAVE
);
308 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
309 array( 'ar_namespace' => $this->title
->getNamespace(),
310 'ar_title' => $this->title
->getDBkey() ) );
315 * Restore the given (or all) text and file revisions for the page.
316 * Once restored, the items will be removed from the archive tables.
317 * The deletion log will be updated with an undeletion notice.
319 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
320 * @param $comment String
321 * @param $fileVersions Array
322 * @param $unsuppress Boolean
324 * @return array(number of file revisions restored, number of image revisions restored, log message)
325 * on success, false on failure
327 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
328 // If both the set of text revisions and file revisions are empty,
329 // restore everything. Otherwise, just restore the requested items.
330 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
332 $restoreText = $restoreAll ||
!empty( $timestamps );
333 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
335 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
336 $img = wfLocalFile( $this->title
);
337 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
338 if ( !$this->fileStatus
->isOk() ) {
341 $filesRestored = $this->fileStatus
->successCount
;
347 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
348 if( $textRestored === false ) { // It must be one of UNDELETE_*
357 $log = new LogPage( 'delete' );
359 if( $textRestored && $filesRestored ) {
360 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
361 $wgContLang->formatNum( $textRestored ),
362 $wgContLang->formatNum( $filesRestored ) );
363 } elseif( $textRestored ) {
364 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
365 $wgContLang->formatNum( $textRestored ) );
366 } elseif( $filesRestored ) {
367 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
368 $wgContLang->formatNum( $filesRestored ) );
370 wfDebug( "Undelete: nothing undeleted...\n" );
374 if( trim( $comment ) != '' ) {
375 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
377 $log->addEntry( 'restore', $this->title
, $reason );
379 return array( $textRestored, $filesRestored, $reason );
383 * This is the meaty bit -- restores archived revisions of the given page
384 * to the cur/old tables. If the page currently exists, all revisions will
385 * be stuffed into old, otherwise the most recent will go into cur.
387 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
388 * @param $comment String
389 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
391 * @return Mixed: number of revisions restored or false on failure
393 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
394 if ( wfReadOnly() ) {
397 $restoreAll = empty( $timestamps );
399 $dbw = wfGetDB( DB_MASTER
);
401 # Does this page already exist? We'll have to update it...
402 $article = new Article( $this->title
);
403 $options = 'FOR UPDATE'; // lock page
404 $page = $dbw->selectRow( 'page',
405 array( 'page_id', 'page_latest' ),
406 array( 'page_namespace' => $this->title
->getNamespace(),
407 'page_title' => $this->title
->getDBkey() ),
413 # Page already exists. Import the history, and if necessary
414 # we'll update the latest revision field in the record.
416 $pageId = $page->page_id
;
417 $previousRevId = $page->page_latest
;
418 # Get the time span of this page
419 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
420 array( 'rev_id' => $previousRevId ),
422 if( $previousTimestamp === false ) {
423 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
427 # Have to create a new article...
430 $previousTimestamp = 0;
434 $oldones = '1 = 1'; # All revisions...
436 $oldts = implode( ',',
437 array_map( array( &$dbw, 'addQuotes' ),
438 array_map( array( &$dbw, 'timestamp' ),
441 $oldones = "ar_timestamp IN ( {$oldts} )";
445 * Select each archived revision...
447 $result = $dbw->select( 'archive',
462 'ar_namespace' => $this->title
->getNamespace(),
463 'ar_title' => $this->title
->getDBkey(),
466 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
468 $ret = $dbw->resultObject( $result );
469 $rev_count = $dbw->numRows( $result );
471 wfDebug( __METHOD__
. ": no revisions to restore\n" );
475 $ret->seek( $rev_count - 1 ); // move to last
476 $row = $ret->fetchObject(); // get newest archived rev
477 $ret->seek( 0 ); // move back
480 // Check the state of the newest to-be version...
481 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
482 return false; // we can't leave the current revision like this!
484 // Safe to insert now...
485 $newid = $article->insertOn( $dbw );
488 // Check if a deleted revision will become the current revision...
489 if( $row->ar_timestamp
> $previousTimestamp ) {
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!
500 foreach ( $ret as $row ) {
501 // Check for key dupes due to shitty archive integrity.
502 if( $row->ar_rev_id
) {
503 $exists = $dbw->selectField( 'revision', '1',
504 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
506 continue; // don't throw DB errors
509 // Insert one revision at a time...maintaining deletion status
510 // unless we are specifically removing all restrictions...
511 $revision = Revision
::newFromArchiveRow( $row,
514 'deleted' => $unsuppress ?
0 : $row->ar_deleted
517 $revision->insertOn( $dbw );
520 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
522 # Now that it's safely stored, take it out of the archive
523 $dbw->delete( 'archive',
525 'ar_namespace' => $this->title
->getNamespace(),
526 'ar_title' => $this->title
->getDBkey(),
530 // Was anything restored at all?
531 if ( $restored == 0 ) {
535 $created = (bool)$newid;
536 $oldcountable = $article->isCountable();
538 // Attach the latest revision to the page...
539 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
540 if ( $created ||
$wasnew ) {
541 // Update site stats, link tables, etc
542 $user = User
::newFromName( $revision->getRawUserText(), false );
543 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
546 wfRunHooks( 'ArticleUndelete', array( &$this->title
, $created, $comment ) );
548 if( $this->title
->getNamespace() == NS_FILE
) {
549 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
559 function getFileStatus() { return $this->fileStatus
; }
563 * Special page allowing users with the appropriate permissions to view
564 * and restore deleted content.
566 * @ingroup SpecialPage
568 class SpecialUndelete
extends SpecialPage
{
569 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
570 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
577 function __construct() {
578 parent
::__construct( 'Undelete', 'deletedhistory' );
581 function loadRequest() {
582 $request = $this->getRequest();
583 $user = $this->getUser();
585 $this->mAction
= $request->getVal( 'action' );
586 $this->mTarget
= $request->getVal( 'target' );
587 $this->mSearchPrefix
= $request->getText( 'prefix' );
588 $time = $request->getVal( 'timestamp' );
589 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
590 $this->mFilename
= $request->getVal( 'file' );
592 $posted = $request->wasPosted() &&
593 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
594 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
595 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
596 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
597 $this->mDiff
= $request->getCheck( 'diff' );
598 $this->mComment
= $request->getText( 'wpComment' );
599 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
600 $this->mToken
= $request->getVal( 'token' );
602 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
603 $this->mAllowed
= true; // user can restore
604 $this->mCanView
= true; // user can view content
605 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
606 $this->mAllowed
= false; // user cannot restore
607 $this->mCanView
= true; // user can view content
608 } else { // user can only view the list of revisions
609 $this->mAllowed
= false;
610 $this->mCanView
= false;
611 $this->mTimestamp
= '';
612 $this->mRestore
= false;
615 if( $this->mRestore ||
$this->mInvert
) {
616 $timestamps = array();
617 $this->mFileVersions
= array();
618 foreach( $request->getValues() as $key => $val ) {
620 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
621 array_push( $timestamps, $matches[1] );
624 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
625 $this->mFileVersions
[] = intval( $matches[1] );
628 rsort( $timestamps );
629 $this->mTargetTimestamp
= $timestamps;
633 function execute( $par ) {
635 if ( !$this->userCanExecute( $this->getUser() ) ) {
636 $this->displayRestrictionError();
640 if ( $this->getUser()->isBlocked() ) {
641 throw new UserBlockedError( $this->getUser()->getBlock() );
644 $this->outputHeader();
646 $this->loadRequest();
648 $out = $this->getOutput();
650 if ( $this->mAllowed
) {
651 $out->setPageTitle( wfMsg( 'undeletepage' ) );
653 $out->setPageTitle( wfMsg( 'viewdeletedpage' ) );
657 $this->mTarget
= $par;
659 if ( $this->mTarget
!== '' ) {
660 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
661 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
663 $this->mTargetObj
= null;
666 if( is_null( $this->mTargetObj
) ) {
667 # Not all users can just browse every deleted page from the list
668 if( $this->getUser()->isAllowed( 'browsearchive' ) ) {
669 $this->showSearchForm();
671 # List undeletable articles
672 if( $this->mSearchPrefix
) {
673 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
674 $this->showList( $result );
677 $out->addWikiMsg( 'undelete-header' );
681 if( $this->mTimestamp
!== '' ) {
682 return $this->showRevision( $this->mTimestamp
);
684 if( $this->mFilename
!== null ) {
685 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
686 // Check if user is allowed to see this file
687 if ( !$file->exists() ) {
688 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
690 } elseif( !$file->userCan( File
::DELETED_FILE
) ) {
691 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
692 $out->permissionRequired( 'suppressrevision' );
694 $out->permissionRequired( 'deletedtext' );
697 } elseif ( !$this->getUser()->matchEditToken( $this->mToken
, $this->mFilename
) ) {
698 $this->showFileConfirmationForm( $this->mFilename
);
701 return $this->showFile( $this->mFilename
);
704 if( $this->mRestore
&& $this->mAction
== 'submit' ) {
705 global $wgUploadMaintenance;
706 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
707 $out->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
710 return $this->undelete();
712 if( $this->mInvert
&& $this->mAction
== 'submit' ) {
713 return $this->showHistory();
715 return $this->showHistory();
718 function showSearchForm() {
721 $this->getOutput()->addWikiMsg( 'undelete-header' );
723 $this->getOutput()->addHTML(
724 Xml
::openElement( 'form', array(
726 'action' => $wgScript ) ) .
727 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
728 Html
::hidden( 'title',
729 $this->getTitle()->getPrefixedDbKey() ) .
730 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
731 'prefix', 'prefix', 20,
732 $this->mSearchPrefix
) . ' ' .
733 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
734 Xml
::closeElement( 'fieldset' ) .
735 Xml
::closeElement( 'form' )
740 * Generic list of deleted pages
742 * @param $result ResultWrapper
745 private function showList( $result ) {
746 $out = $this->getOutput();
748 if( $result->numRows() == 0 ) {
749 $out->addWikiMsg( 'undelete-no-results' );
753 $out->addWikiMsg( 'undeletepagetext', $this->getLang()->formatNum( $result->numRows() ) );
755 $undelete = $this->getTitle();
756 $out->addHTML( "<ul>\n" );
757 foreach ( $result as $row ) {
758 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
759 $link = Linker
::linkKnown(
761 htmlspecialchars( $title->getPrefixedText() ),
763 array( 'target' => $title->getPrefixedText() )
765 $revs = wfMsgExt( 'undeleterevisions',
766 array( 'parseinline' ),
767 $this->getLang()->formatNum( $row->count
) );
768 $out->addHTML( "<li>{$link} ({$revs})</li>\n" );
771 $out->addHTML( "</ul>\n" );
776 private function showRevision( $timestamp ) {
777 $out = $this->getOutput();
779 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
783 $archive = new PageArchive( $this->mTargetObj
);
784 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj
) );
785 $rev = $archive->getRevision( $timestamp );
788 $out->addWikiMsg( 'undeleterevision-missing' );
792 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
793 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
794 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
797 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
798 $out->addHTML( '<br />' );
799 // and we are allowed to see...
803 $out->setPageTitle( wfMsg( 'undeletepage' ) );
806 $previousRev = $archive->getPreviousRevision( $timestamp );
808 $this->showDiff( $previousRev, $rev );
809 if( $this->getUser()->getOption( 'diffonly' ) ) {
812 $out->addHTML( '<hr />' );
815 $out->addWikiMsg( 'undelete-nodiff' );
819 $link = Linker
::linkKnown(
820 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
821 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
824 // date and time are separate parameters to facilitate localisation.
825 // $time is kept for backward compat reasons.
826 $time = $this->getLang()->timeAndDate( $timestamp, true );
827 $d = $this->getLang()->date( $timestamp, true );
828 $t = $this->getLang()->time( $timestamp, true );
829 $user = Linker
::revUserTools( $rev );
831 if( $this->mPreview
) {
832 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
834 $openDiv = '<div id="mw-undelete-revision">';
836 $out->addHTML( $openDiv );
838 // Revision delete links
839 if ( !$this->mDiff
) {
840 $revdel = Linker
::getRevDeleteLink( $this->getUser(), $rev, $this->mTargetObj
);
842 $out->addHTML( "$revdel " );
846 $out->addHTML( wfMessage( 'undelete-revision' )->rawParams( $link )->params(
847 $time )->rawParams( $user )->params( $d, $t )->parse() . '</div>' );
848 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
850 if( $this->mPreview
) {
852 $popts = $out->parserOptions();
853 $popts->setEditSection( false );
854 $out->parserOptions( $popts );
855 $out->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
859 Xml
::element( 'textarea', array(
860 'readonly' => 'readonly',
861 'cols' => intval( $this->getUser()->getOption( 'cols' ) ),
862 'rows' => intval( $this->getUser()->getOption( 'rows' ) ) ),
863 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
864 Xml
::openElement( 'div' ) .
865 Xml
::openElement( 'form', array(
867 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
868 Xml
::element( 'input', array(
871 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
872 Xml
::element( 'input', array(
874 'name' => 'timestamp',
875 'value' => $timestamp ) ) .
876 Xml
::element( 'input', array(
878 'name' => 'wpEditToken',
879 'value' => $this->getUser()->editToken() ) ) .
880 Xml
::element( 'input', array(
883 'value' => wfMsg( 'showpreview' ) ) ) .
884 Xml
::element( 'input', array(
887 'value' => wfMsg( 'showdiff' ) ) ) .
888 Xml
::closeElement( 'form' ) .
889 Xml
::closeElement( 'div' ) );
893 * Build a diff display between this and the previous either deleted
894 * or non-deleted edit.
896 * @param $previousRev Revision
897 * @param $currentRev Revision
898 * @return String: HTML
900 function showDiff( $previousRev, $currentRev ) {
901 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
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->generateDiffBody(
919 $previousRev->getText(), $currentRev->getText() ) .
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 $rdel = Linker
::getRevDeleteLink( $this->getUser(), $rev, $this->mTargetObj
);
946 if ( $rdel ) $rdel = " $rdel";
948 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
954 $this->getLang()->timeanddate( $rev->getTimestamp(), true ),
955 $this->getLang()->date( $rev->getTimestamp(), true ),
956 $this->getLang()->time( $rev->getTimestamp(), true )
962 '<div id="mw-diff-'.$prefix.'title2">' .
963 Linker
::revUserTools( $rev ) . '<br />' .
965 '<div id="mw-diff-'.$prefix.'title3">' .
966 Linker
::revComment( $rev ) . $rdel . '<br />' .
971 * Show a form confirming whether a tokenless user really wants to see a file
973 private function showFileConfirmationForm( $key ) {
974 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
975 $this->getOutput()->addWikiMsg( 'undelete-show-file-confirm',
976 $this->mTargetObj
->getText(),
977 $this->getLang()->date( $file->getTimestamp() ),
978 $this->getLang()->time( $file->getTimestamp() ) );
979 $this->getOutput()->addHTML(
980 Xml
::openElement( 'form', array(
982 'action' => $this->getTitle()->getLocalURL(
983 'target=' . urlencode( $this->mTarget
) .
984 '&file=' . urlencode( $key ) .
985 '&token=' . urlencode( $this->getUser()->editToken( $key ) ) )
988 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
994 * Show a deleted file version requested by the visitor.
996 private function showFile( $key ) {
997 $this->getOutput()->disable();
999 # We mustn't allow the output to be Squid cached, otherwise
1000 # if an admin previews a deleted image, and it's cached, then
1001 # a user without appropriate permissions can toddle off and
1002 # nab the image, and Squid will serve it
1003 $response = $this->getRequest()->response();
1004 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1005 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1006 $response->header( 'Pragma: no-cache' );
1008 $repo = RepoGroup
::singleton()->getLocalRepo();
1009 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1010 StreamFile
::stream( $path );
1013 private function showHistory() {
1014 $out = $this->getOutput();
1015 if( $this->mAllowed
) {
1016 $out->addModules( 'mediawiki.special.undelete' );
1017 $out->setPageTitle( wfMsg( 'undeletepage' ) );
1019 $out->setPageTitle( wfMsg( 'viewdeletedpage' ) );
1022 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1023 array( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() )
1026 $archive = new PageArchive( $this->mTargetObj
);
1027 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj
) );
1029 $text = $archive->getLastRevisionText();
1030 if( is_null( $text ) ) {
1031 $out->addWikiMsg( 'nohistory' );
1035 $out->addHTML( '<div class="mw-undelete-history">' );
1036 if ( $this->mAllowed
) {
1037 $out->addWikiMsg( 'undeletehistory' );
1038 $out->addWikiMsg( 'undeleterevdel' );
1040 $out->addWikiMsg( 'undeletehistorynoadmin' );
1042 $out->addHTML( '</div>' );
1044 # List all stored revisions
1045 $revisions = $archive->listRevisions();
1046 $files = $archive->listFiles();
1048 $haveRevisions = $revisions && $revisions->numRows() > 0;
1049 $haveFiles = $files && $files->numRows() > 0;
1051 # Batch existence check on user and talk pages
1052 if( $haveRevisions ) {
1053 $batch = new LinkBatch();
1054 foreach ( $revisions as $row ) {
1055 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1056 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1059 $revisions->seek( 0 );
1062 $batch = new LinkBatch();
1063 foreach ( $files as $row ) {
1064 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1065 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1071 if ( $this->mAllowed
) {
1072 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1073 # Start the form here
1074 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1075 $out->addHTML( $top );
1078 # Show relevant lines from the deletion log:
1079 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1080 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
->getPrefixedText() );
1081 # Show relevant lines from the suppression log:
1082 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1083 $out->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1084 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
->getPrefixedText() );
1087 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1088 # Format the user-visible controls (comment field, submission button)
1089 # in a nice little table
1090 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1094 <td class='mw-input'>" .
1095 Xml
::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1096 'mw-undelete-unsuppress', $this->mUnsuppress
).
1100 $unsuppressBox = '';
1103 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1104 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1106 <td colspan='2' class='mw-undelete-extrahelp'>" .
1107 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1111 <td class='mw-label'>" .
1112 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1114 <td class='mw-input'>" .
1115 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1120 <td class='mw-submit'>" .
1121 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1122 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1126 Xml
::closeElement( 'table' ) .
1127 Xml
::closeElement( 'fieldset' );
1129 $out->addHTML( $table );
1132 $out->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1134 if( $haveRevisions ) {
1135 # The page's stored (deleted) history:
1136 $out->addHTML( '<ul>' );
1137 $remaining = $revisions->numRows();
1138 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1140 foreach ( $revisions as $row ) {
1142 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1145 $out->addHTML( '</ul>' );
1147 $out->addWikiMsg( 'nohistory' );
1151 $out->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1152 $out->addHTML( '<ul>' );
1153 foreach ( $files as $row ) {
1154 $out->addHTML( $this->formatFileRow( $row ) );
1157 $out->addHTML( '</ul>' );
1160 if ( $this->mAllowed
) {
1161 # Slip in the hidden controls here
1162 $misc = Html
::hidden( 'target', $this->mTarget
);
1163 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->editToken() );
1164 $misc .= Xml
::closeElement( 'form' );
1165 $out->addHTML( $misc );
1171 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1172 $rev = Revision
::newFromArchiveRow( $row,
1173 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1175 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1176 // Build checkboxen...
1177 if( $this->mAllowed
) {
1178 if( $this->mInvert
) {
1179 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1180 $checkBox = Xml
::check( "ts$ts" );
1182 $checkBox = Xml
::check( "ts$ts", true );
1185 $checkBox = Xml
::check( "ts$ts" );
1190 // Build page & diff links...
1191 if( $this->mCanView
) {
1192 $titleObj = $this->getTitle();
1194 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1195 $pageLink = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1196 $last = wfMsgHtml( 'diff' );
1197 } elseif( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1198 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1199 $last = Linker
::linkKnown(
1201 wfMsgHtml( 'diff' ),
1204 'target' => $this->mTargetObj
->getPrefixedText(),
1210 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1211 $last = wfMsgHtml( 'diff' );
1214 $pageLink = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1215 $last = wfMsgHtml( 'diff' );
1218 $userLink = Linker
::revUserTools( $rev );
1219 // Revision text size
1220 $size = $row->ar_len
;
1221 if( !is_null( $size ) ) {
1222 $stxt = Linker
::formatRevisionSize( $size );
1225 $comment = Linker
::revComment( $rev );
1226 // Revision delete links
1227 $revdlink = Linker
::getRevDeleteLink( $this->getUser(), $rev, $this->mTargetObj
);
1228 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1231 private function formatFileRow( $row ) {
1232 $file = ArchivedFile
::newFromRow( $row );
1234 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1235 if( $this->mAllowed
&& $row->fa_storage_key
) {
1236 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1237 $key = urlencode( $row->fa_storage_key
);
1238 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1241 $pageLink = $this->getLang()->timeanddate( $ts, true );
1243 $userLink = $this->getFileUser( $file );
1245 wfMsg( 'widthheight',
1246 $this->getLang()->formatNum( $row->fa_width
),
1247 $this->getLang()->formatNum( $row->fa_height
) ) .
1249 wfMsg( 'nbytes', $this->getLang()->formatNum( $row->fa_size
) ) .
1251 $data = htmlspecialchars( $data );
1252 $comment = $this->getFileComment( $file );
1254 // Add show/hide deletion links if available
1255 $canHide = $this->getUser()->isAllowed( 'deleterevision' );
1256 if( $canHide ||
( $file->getVisibility() && $this->getUser()->isAllowed( 'deletedhistory' ) ) ) {
1257 if( !$file->userCan( File
::DELETED_RESTRICTED
) ) {
1258 $revdlink = Linker
::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1261 'type' => 'filearchive',
1262 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1263 'ids' => $row->fa_id
1265 $revdlink = Linker
::revDeleteLink( $query,
1266 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1272 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1276 * Fetch revision text link if it's available to all users
1278 * @param $rev Revision
1281 function getPageLink( $rev, $titleObj, $ts ) {
1282 $time = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1284 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1285 return '<span class="history-deleted">' . $time . '</span>';
1287 $link = Linker
::linkKnown(
1292 'target' => $this->mTargetObj
->getPrefixedText(),
1296 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1297 $link = '<span class="history-deleted">' . $link . '</span>';
1304 * Fetch image view link if it's available to all users
1307 * @return String: HTML fragment
1309 function getFileLink( $file, $titleObj, $ts, $key ) {
1310 if( !$file->userCan( File
::DELETED_FILE
) ) {
1311 return '<span class="history-deleted">' . $this->getLang()->timeanddate( $ts, true ) . '</span>';
1313 $link = Linker
::linkKnown(
1315 $this->getLang()->timeanddate( $ts, true ),
1318 'target' => $this->mTargetObj
->getPrefixedText(),
1320 'token' => $this->getUser()->editToken( $key )
1323 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1324 $link = '<span class="history-deleted">' . $link . '</span>';
1331 * Fetch file's user id if it's available to this user
1334 * @return String: HTML fragment
1336 function getFileUser( $file ) {
1337 if( !$file->userCan( File
::DELETED_USER
) ) {
1338 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1340 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1341 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1342 if( $file->isDeleted( File
::DELETED_USER
) ) {
1343 $link = '<span class="history-deleted">' . $link . '</span>';
1350 * Fetch file upload comment if it's available to this user
1353 * @return String: HTML fragment
1355 function getFileComment( $file ) {
1356 if( !$file->userCan( File
::DELETED_COMMENT
) ) {
1357 return '<span class="history-deleted"><span class="comment">' .
1358 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1360 $link = Linker
::commentBlock( $file->getRawDescription() );
1361 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1362 $link = '<span class="history-deleted">' . $link . '</span>';
1368 function undelete() {
1369 if ( wfReadOnly() ) {
1370 throw new ReadOnlyError
;
1373 if( !is_null( $this->mTargetObj
) ) {
1374 $archive = new PageArchive( $this->mTargetObj
);
1375 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj
) );
1376 $ok = $archive->undelete(
1377 $this->mTargetTimestamp
,
1379 $this->mFileVersions
,
1380 $this->mUnsuppress
);
1382 if( is_array( $ok ) ) {
1383 if ( $ok[1] ) { // Undeleted file count
1384 wfRunHooks( 'FileUndeleteComplete', array(
1385 $this->mTargetObj
, $this->mFileVersions
,
1386 $this->getUser(), $this->mComment
) );
1389 $link = Linker
::linkKnown( $this->mTargetObj
);
1390 $this->getOutput()->addHTML( wfMessage( 'undeletedpage' )->rawParams( $link )->parse() );
1392 $this->getOutput()->showFatalError( wfMsg( 'cannotundelete' ) );
1393 $this->getOutput()->addWikiMsg( 'undeleterevdel' );
1396 // Show file deletion warnings and errors
1397 $status = $archive->getFileStatus();
1398 if( $status && !$status->isGood() ) {
1399 $this->getOutput()->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1402 $this->getOutput()->showFatalError( wfMsg( 'cannotundelete' ) );