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 );
83 protected static function listPages( $dbr, $condition ) {
84 return $dbr->resultObject(
95 'GROUP BY' => 'ar_namespace,ar_title',
96 'ORDER BY' => 'ar_namespace,ar_title',
104 * List the revisions of the given page. Returns result wrapper with
105 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
107 * @return ResultWrapper
109 function listRevisions() {
110 $dbr = wfGetDB( DB_SLAVE
);
111 $res = $dbr->select( 'archive',
112 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
113 array( 'ar_namespace' => $this->title
->getNamespace(),
114 'ar_title' => $this->title
->getDBkey() ),
115 'PageArchive::listRevisions',
116 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
117 $ret = $dbr->resultObject( $res );
122 * List the deleted file revisions for this page, if it's a file page.
123 * Returns a result wrapper with various filearchive fields, or null
124 * if not a file page.
126 * @return ResultWrapper
127 * @todo Does this belong in Image for fuller encapsulation?
129 function listFiles() {
130 if( $this->title
->getNamespace() == NS_FILE
) {
131 $dbr = wfGetDB( DB_SLAVE
);
132 $res = $dbr->select( 'filearchive',
152 array( 'fa_name' => $this->title
->getDBkey() ),
154 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
155 $ret = $dbr->resultObject( $res );
162 * Fetch (and decompress if necessary) the stored text for the deleted
163 * revision of the page with the given timestamp.
165 * @param $timestamp String
167 * @deprecated Use getRevision() for more flexible information
169 function getRevisionText( $timestamp ) {
170 $rev = $this->getRevision( $timestamp );
171 return $rev ?
$rev->getText() : null;
175 * Return a Revision object containing data for the deleted revision.
176 * Note that the result *may* or *may not* have a null page ID.
178 * @param $timestamp String
181 function getRevision( $timestamp ) {
182 $dbr = wfGetDB( DB_SLAVE
);
183 $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 );
284 * Fetch (and decompress if necessary) the stored text of the most
285 * recently edited deleted revision of the page.
287 * If there are no archived revisions for the page, returns NULL.
291 function getLastRevisionText() {
292 $dbr = wfGetDB( DB_SLAVE
);
293 $row = $dbr->selectRow( 'archive',
294 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
295 array( 'ar_namespace' => $this->title
->getNamespace(),
296 'ar_title' => $this->title
->getDBkey() ),
298 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
300 return $this->getTextFromRow( $row );
307 * Quick check if any archived revisions are present for the page.
311 function isDeleted() {
312 $dbr = wfGetDB( DB_SLAVE
);
313 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
314 array( 'ar_namespace' => $this->title
->getNamespace(),
315 'ar_title' => $this->title
->getDBkey() ) );
320 * Restore the given (or all) text and file revisions for the page.
321 * Once restored, the items will be removed from the archive tables.
322 * The deletion log will be updated with an undeletion notice.
324 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
325 * @param $comment String
326 * @param $fileVersions Array
327 * @param $unsuppress Boolean
329 * @return array(number of file revisions restored, number of image revisions restored, log message)
330 * on success, false on failure
332 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
333 // If both the set of text revisions and file revisions are empty,
334 // restore everything. Otherwise, just restore the requested items.
335 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
337 $restoreText = $restoreAll ||
!empty( $timestamps );
338 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
340 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
341 $img = wfLocalFile( $this->title
);
342 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
343 if ( !$this->fileStatus
->isOk() ) {
346 $filesRestored = $this->fileStatus
->successCount
;
352 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
353 if( $textRestored === false ) { // It must be one of UNDELETE_*
362 $log = new LogPage( 'delete' );
364 if( $textRestored && $filesRestored ) {
365 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
366 $wgContLang->formatNum( $textRestored ),
367 $wgContLang->formatNum( $filesRestored ) );
368 } elseif( $textRestored ) {
369 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
370 $wgContLang->formatNum( $textRestored ) );
371 } elseif( $filesRestored ) {
372 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
373 $wgContLang->formatNum( $filesRestored ) );
375 wfDebug( "Undelete: nothing undeleted...\n" );
379 if( trim( $comment ) != '' )
380 $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 = '' ) {
400 $restoreAll = empty( $timestamps );
402 $dbw = wfGetDB( DB_MASTER
);
404 # Does this page already exist? We'll have to update it...
405 $article = new Article( $this->title
);
406 $options = 'FOR UPDATE'; // lock page
407 $page = $dbw->selectRow( 'page',
408 array( 'page_id', 'page_latest' ),
409 array( 'page_namespace' => $this->title
->getNamespace(),
410 'page_title' => $this->title
->getDBkey() ),
416 # Page already exists. Import the history, and if necessary
417 # we'll update the latest revision field in the record.
419 $pageId = $page->page_id
;
420 $previousRevId = $page->page_latest
;
421 # Get the time span of this page
422 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
423 array( 'rev_id' => $previousRevId ),
425 if( $previousTimestamp === false ) {
426 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
430 # Have to create a new article...
433 $previousTimestamp = 0;
437 $oldones = '1 = 1'; # All revisions...
439 $oldts = implode( ',',
440 array_map( array( &$dbw, 'addQuotes' ),
441 array_map( array( &$dbw, 'timestamp' ),
444 $oldones = "ar_timestamp IN ( {$oldts} )";
448 * Select each archived revision...
450 $result = $dbw->select( 'archive',
465 'ar_namespace' => $this->title
->getNamespace(),
466 'ar_title' => $this->title
->getDBkey(),
469 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
471 $ret = $dbw->resultObject( $result );
472 $rev_count = $dbw->numRows( $result );
474 wfDebug( __METHOD__
.": no revisions to restore\n" );
478 $ret->seek( $rev_count - 1 ); // move to last
479 $row = $ret->fetchObject(); // get newest archived rev
480 $ret->seek( 0 ); // move back
483 // Check the state of the newest to-be version...
484 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
485 return false; // we can't leave the current revision like this!
487 // Safe to insert now...
488 $newid = $article->insertOn( $dbw );
491 // Check if a deleted revision will become the current revision...
492 if( $row->ar_timestamp
> $previousTimestamp ) {
493 // Check the state of the newest to-be version...
494 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
495 return false; // we can't leave the current revision like this!
503 foreach ( $ret as $row ) {
504 // Check for key dupes due to shitty archive integrity.
505 if( $row->ar_rev_id
) {
506 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
507 if( $exists ) 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?
535 // Attach the latest revision to the page...
536 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
537 if( $newid ||
$wasnew ) {
538 // Update site stats, link tables, etc
539 $article->createUpdates( $revision );
543 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
544 Article
::onArticleCreate( $this->title
);
546 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
547 Article
::onArticleEdit( $this->title
);
550 if( $this->title
->getNamespace() == NS_FILE
) {
551 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
555 // Revision couldn't be created. This is very weird
556 wfDebug( "Undelete: unknown error...\n" );
566 function getFileStatus() { return $this->fileStatus
; }
570 * Special page allowing users with the appropriate permissions to view
571 * and restore deleted content.
573 * @ingroup SpecialPage
575 class SpecialUndelete
extends SpecialPage
{
576 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj, $mFile;
577 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
579 function __construct( $request = null ) {
580 parent
::__construct( 'Undelete', 'deletedhistory' );
582 if ( $request === null ) {
584 $this->mRequest
= $wgRequest;
586 $this->mRequest
= $request;
590 function loadRequest() {
592 $this->mAction
= $this->mRequest
->getVal( 'action' );
593 $this->mTarget
= $this->mRequest
->getVal( 'target' );
594 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
595 $time = $this->mRequest
->getVal( 'timestamp' );
596 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
597 $this->mFile
= $this->mRequest
->getVal( 'file' );
599 $posted = $this->mRequest
->wasPosted() &&
600 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
601 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
602 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
603 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
604 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
605 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
606 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
607 $this->mToken
= $this->mRequest
->getVal( 'token' );
609 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
610 $this->mAllowed
= true; // user can restore
611 $this->mCanView
= true; // user can view content
612 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
613 $this->mAllowed
= false; // user cannot restore
614 $this->mCanView
= true; // user can view content
615 } else { // user can only view the list of revisions
616 $this->mAllowed
= false;
617 $this->mCanView
= false;
618 $this->mTimestamp
= '';
619 $this->mRestore
= false;
622 if( $this->mRestore ||
$this->mInvert
) {
623 $timestamps = array();
624 $this->mFileVersions
= array();
625 foreach( $_REQUEST as $key => $val ) {
627 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
628 array_push( $timestamps, $matches[1] );
631 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
632 $this->mFileVersions
[] = intval( $matches[1] );
635 rsort( $timestamps );
636 $this->mTargetTimestamp
= $timestamps;
640 function execute( $par ) {
641 global $wgOut, $wgUser;
644 if ( !$this->userCanExecute( $wgUser ) ) {
645 $this->displayRestrictionError();
648 $this->outputHeader();
650 $this->loadRequest();
652 if ( $this->mAllowed
) {
653 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
655 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
659 $this->mTarget
= $par;
661 if ( $this->mTarget
!== '' ) {
662 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
663 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj
);
665 $this->mTargetObj
= null;
668 if( is_null( $this->mTargetObj
) ) {
669 # Not all users can just browse every deleted page from the list
670 if( $wgUser->isAllowed( 'browsearchive' ) ) {
671 $this->showSearchForm();
673 # List undeletable articles
674 if( $this->mSearchPrefix
) {
675 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
676 $this->showList( $result );
679 $wgOut->addWikiMsg( 'undelete-header' );
683 if( $this->mTimestamp
!== '' ) {
684 return $this->showRevision( $this->mTimestamp
);
686 if( $this->mFile
!== null ) {
687 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
688 // Check if user is allowed to see this file
689 if ( !$file->exists() ) {
690 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile
);
692 } else if( !$file->userCan( File
::DELETED_FILE
) ) {
693 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
694 $wgOut->permissionRequired( 'suppressrevision' );
696 $wgOut->permissionRequired( 'deletedtext' );
699 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
700 $this->showFileConfirmationForm( $this->mFile
);
703 return $this->showFile( $this->mFile
);
706 if( $this->mRestore
&& $this->mAction
== "submit" ) {
707 global $wgUploadMaintenance;
708 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
709 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
712 return $this->undelete();
714 if( $this->mInvert
&& $this->mAction
== "submit" ) {
715 return $this->showHistory( );
717 return $this->showHistory();
720 function showSearchForm() {
721 global $wgOut, $wgScript;
722 $wgOut->addWikiMsg( 'undelete-header' );
725 Xml
::openElement( 'form', array(
727 'action' => $wgScript ) ) .
728 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
729 Html
::hidden( 'title',
730 $this->getTitle()->getPrefixedDbKey() ) .
731 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
732 'prefix', 'prefix', 20,
733 $this->mSearchPrefix
) . ' ' .
734 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
735 Xml
::closeElement( 'fieldset' ) .
736 Xml
::closeElement( 'form' )
740 // Generic list of deleted pages
741 private function showList( $result ) {
742 global $wgLang, $wgUser, $wgOut;
744 if( $result->numRows() == 0 ) {
745 $wgOut->addWikiMsg( 'undelete-no-results' );
749 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
751 $sk = $wgUser->getSkin();
752 $undelete = $this->getTitle();
753 $wgOut->addHTML( "<ul>\n" );
754 foreach ( $result as $row ) {
755 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
756 $link = $sk->linkKnown(
758 htmlspecialchars( $title->getPrefixedText() ),
760 array( 'target' => $title->getPrefixedText() )
762 $revs = wfMsgExt( 'undeleterevisions',
763 array( 'parseinline' ),
764 $wgLang->formatNum( $row->count
) );
765 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
768 $wgOut->addHTML( "</ul>\n" );
773 private function showRevision( $timestamp ) {
774 global $wgLang, $wgUser, $wgOut;
776 $skin = $wgUser->getSkin();
778 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
780 $archive = new PageArchive( $this->mTargetObj
);
781 $rev = $archive->getRevision( $timestamp );
784 $wgOut->addWikiMsg( 'undeleterevision-missing' );
788 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
789 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
790 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
793 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
794 $wgOut->addHTML( '<br />' );
795 // and we are allowed to see...
799 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
801 $link = $skin->linkKnown(
802 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
803 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
807 $previousRev = $archive->getPreviousRevision( $timestamp );
809 $this->showDiff( $previousRev, $rev );
810 if( $wgUser->getOption( 'diffonly' ) ) {
813 $wgOut->addHTML( '<hr />' );
816 $wgOut->addWikiMsg( 'undelete-nodiff' );
820 // date and time are separate parameters to facilitate localisation.
821 // $time is kept for backward compat reasons.
822 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
823 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
824 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
825 $user = $skin->revUserTools( $rev );
827 if( $this->mPreview
) {
828 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
830 $openDiv = '<div id="mw-undelete-revision">';
832 $wgOut->addHTML( $openDiv );
834 // Revision delete links
835 $canHide = $wgUser->isAllowed( 'deleterevision' );
836 if ( !$this->mDiff
&& ( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) ) {
837 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
838 $wgOut->addHTML( $skin->revDeleteLinkDisabled( $canHide ) ); // revision was hidden from sysops
842 'target' => $this->mTargetObj
->getPrefixedDBkey(),
843 'ids' => $rev->getTimestamp()
845 $wgOut->addHTML( $skin->revDeleteLink( $query,
846 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide ) );
850 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
851 $wgOut->addHTML( '</div>' );
852 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
854 if( $this->mPreview
) {
856 $popts = $wgOut->parserOptions();
857 $popts->setEditSection( false );
858 $wgOut->parserOptions( $popts );
859 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
863 Xml
::element( 'textarea', array(
864 'readonly' => 'readonly',
865 'cols' => intval( $wgUser->getOption( 'cols' ) ),
866 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
867 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
868 Xml
::openElement( 'div' ) .
869 Xml
::openElement( 'form', array(
871 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
872 Xml
::element( 'input', array(
875 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
876 Xml
::element( 'input', array(
878 'name' => 'timestamp',
879 'value' => $timestamp ) ) .
880 Xml
::element( 'input', array(
882 'name' => 'wpEditToken',
883 'value' => $wgUser->editToken() ) ) .
884 Xml
::element( 'input', array(
887 'value' => wfMsg( 'showpreview' ) ) ) .
888 Xml
::element( 'input', array(
891 'value' => wfMsg( 'showdiff' ) ) ) .
892 Xml
::closeElement( 'form' ) .
893 Xml
::closeElement( 'div' ) );
897 * Build a diff display between this and the previous either deleted
898 * or non-deleted edit.
900 * @param $previousRev Revision
901 * @param $currentRev Revision
902 * @return String: HTML
904 function showDiff( $previousRev, $currentRev ) {
907 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
908 $diffEngine->showDiffStyle();
911 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
912 "<col class='diff-marker' />" .
913 "<col class='diff-content' />" .
914 "<col class='diff-marker' />" .
915 "<col class='diff-content' />" .
917 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
918 $this->diffHeader( $previousRev, 'o' ) .
920 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
921 $this->diffHeader( $currentRev, 'n' ) .
924 $diffEngine->generateDiffBody(
925 $previousRev->getText(), $currentRev->getText() ) .
932 * @param $rev Revision
936 private function diffHeader( $rev, $prefix ) {
937 global $wgUser, $wgLang;
938 $sk = $wgUser->getSkin();
939 $isDeleted = !( $rev->getId() && $rev->getTitle() );
941 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
942 $targetPage = $this->getTitle();
943 $targetQuery = array(
944 'target' => $this->mTargetObj
->getPrefixedText(),
945 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
948 /// @todo Fixme getId() may return non-zero for deleted revs...
949 $targetPage = $rev->getTitle();
950 $targetQuery = array( 'oldid' => $rev->getId() );
952 // Add show/hide deletion links if available
953 $canHide = $wgUser->isAllowed( 'deleterevision' );
954 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
956 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
957 $del .= $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
961 'target' => $this->mTargetObj
->getPrefixedDbkey(),
962 'ids' => $rev->getTimestamp()
964 $del .= $sk->revDeleteLink( $query,
965 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
971 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
976 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
977 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
978 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
984 '<div id="mw-diff-'.$prefix.'title2">' .
985 $sk->revUserTools( $rev ) . '<br />' .
987 '<div id="mw-diff-'.$prefix.'title3">' .
988 $sk->revComment( $rev ) . $del . '<br />' .
993 * Show a form confirming whether a tokenless user really wants to see a file
995 private function showFileConfirmationForm( $key ) {
996 global $wgOut, $wgUser, $wgLang;
997 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
998 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
999 $this->mTargetObj
->getText(),
1000 $wgLang->date( $file->getTimestamp() ),
1001 $wgLang->time( $file->getTimestamp() ) );
1003 Xml
::openElement( 'form', array(
1005 'action' => $this->getTitle()->getLocalUrl(
1006 'target=' . urlencode( $this->mTarget
) .
1007 '&file=' . urlencode( $key ) .
1008 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
1011 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1017 * Show a deleted file version requested by the visitor.
1019 private function showFile( $key ) {
1020 global $wgOut, $wgRequest;
1023 # We mustn't allow the output to be Squid cached, otherwise
1024 # if an admin previews a deleted image, and it's cached, then
1025 # a user without appropriate permissions can toddle off and
1026 # nab the image, and Squid will serve it
1027 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1028 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1029 $wgRequest->response()->header( 'Pragma: no-cache' );
1032 require_once( "$IP/includes/StreamFile.php" );
1033 $repo = RepoGroup
::singleton()->getLocalRepo();
1034 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1035 wfStreamFile( $path );
1038 private function showHistory( ) {
1039 global $wgUser, $wgOut;
1041 $sk = $wgUser->getSkin();
1042 if( $this->mAllowed
) {
1043 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
1045 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
1048 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n", array ( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() ) );
1050 $archive = new PageArchive( $this->mTargetObj
);
1052 $text = $archive->getLastRevisionText();
1053 if( is_null( $text ) ) {
1054 $wgOut->addWikiMsg( "nohistory" );
1058 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1059 if ( $this->mAllowed
) {
1060 $wgOut->addWikiMsg( "undeletehistory" );
1061 $wgOut->addWikiMsg( "undeleterevdel" );
1063 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
1065 $wgOut->addHTML( '</div>' );
1067 # List all stored revisions
1068 $revisions = $archive->listRevisions();
1069 $files = $archive->listFiles();
1071 $haveRevisions = $revisions && $revisions->numRows() > 0;
1072 $haveFiles = $files && $files->numRows() > 0;
1074 # Batch existence check on user and talk pages
1075 if( $haveRevisions ) {
1076 $batch = new LinkBatch();
1077 foreach ( $revisions as $row ) {
1078 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1079 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1082 $revisions->seek( 0 );
1085 $batch = new LinkBatch();
1086 foreach ( $files as $row ) {
1087 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1088 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1094 if ( $this->mAllowed
) {
1095 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1096 # Start the form here
1097 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1098 $wgOut->addHTML( $top );
1101 # Show relevant lines from the deletion log:
1102 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1103 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1104 # Show relevant lines from the suppression log:
1105 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1106 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1107 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1110 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1111 # Format the user-visible controls (comment field, submission button)
1112 # in a nice little table
1113 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1117 <td class='mw-input'>" .
1118 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1119 'mw-undelete-unsuppress', $this->mUnsuppress
).
1123 $unsuppressBox = "";
1126 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1127 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1129 <td colspan='2' class='mw-undelete-extrahelp'>" .
1130 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1134 <td class='mw-label'>" .
1135 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1137 <td class='mw-input'>" .
1138 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1143 <td class='mw-submit'>" .
1144 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1145 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1146 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1150 Xml
::closeElement( 'table' ) .
1151 Xml
::closeElement( 'fieldset' );
1153 $wgOut->addHTML( $table );
1156 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1158 if( $haveRevisions ) {
1159 # The page's stored (deleted) history:
1160 $wgOut->addHTML("<ul>");
1161 $remaining = $revisions->numRows();
1162 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1164 foreach ( $revisions as $row ) {
1166 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1169 $wgOut->addHTML("</ul>");
1171 $wgOut->addWikiMsg( "nohistory" );
1175 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1176 $wgOut->addHTML( "<ul>" );
1177 foreach ( $files as $row ) {
1178 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1181 $wgOut->addHTML( "</ul>" );
1184 if ( $this->mAllowed
) {
1185 # Slip in the hidden controls here
1186 $misc = Html
::hidden( 'target', $this->mTarget
);
1187 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
1188 $misc .= Xml
::closeElement( 'form' );
1189 $wgOut->addHTML( $misc );
1195 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1196 global $wgUser, $wgLang;
1198 $rev = Revision
::newFromArchiveRow( $row,
1199 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1201 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1202 // Build checkboxen...
1203 if( $this->mAllowed
) {
1204 if( $this->mInvert
) {
1205 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1206 $checkBox = Xml
::check( "ts$ts");
1208 $checkBox = Xml
::check( "ts$ts", true );
1211 $checkBox = Xml
::check( "ts$ts" );
1216 // Build page & diff links...
1217 if( $this->mCanView
) {
1218 $titleObj = $this->getTitle();
1220 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1221 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1222 $last = wfMsgHtml('diff');
1223 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1224 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1225 $last = $sk->linkKnown(
1230 'target' => $this->mTargetObj
->getPrefixedText(),
1236 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1237 $last = wfMsgHtml('diff');
1240 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1241 $last = wfMsgHtml('diff');
1244 $userLink = $sk->revUserTools( $rev );
1245 // Revision text size
1246 if( !is_null($size = $row->ar_len
) ) {
1247 $stxt = $sk->formatRevisionSize( $size );
1250 $comment = $sk->revComment( $rev );
1251 // Revision delete links
1252 $canHide = $wgUser->isAllowed( 'deleterevision' );
1253 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' )) ) {
1254 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1255 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1258 'type' => 'archive',
1259 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1262 $revdlink = $sk->revDeleteLink( $query,
1263 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
1268 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1271 private function formatFileRow( $row, $sk ) {
1272 global $wgUser, $wgLang;
1274 $file = ArchivedFile
::newFromRow( $row );
1276 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1277 if( $this->mAllowed
&& $row->fa_storage_key
) {
1278 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1279 $key = urlencode( $row->fa_storage_key
);
1280 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1283 $pageLink = $wgLang->timeanddate( $ts, true );
1285 $userLink = $this->getFileUser( $file, $sk );
1287 wfMsg( 'widthheight',
1288 $wgLang->formatNum( $row->fa_width
),
1289 $wgLang->formatNum( $row->fa_height
) ) .
1291 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1293 $data = htmlspecialchars( $data );
1294 $comment = $this->getFileComment( $file, $sk );
1295 // Add show/hide deletion links if available
1296 $canHide = $wgUser->isAllowed( 'deleterevision' );
1297 if( $canHide ||
( $file->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
1298 if( !$file->userCan( File
::DELETED_RESTRICTED
) ) {
1299 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1302 'type' => 'filearchive',
1303 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1304 'ids' => $row->fa_id
1306 $revdlink = $sk->revDeleteLink( $query,
1307 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1312 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1316 * Fetch revision text link if it's available to all users
1318 * @param $rev Revision
1322 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1325 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1327 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1328 return '<span class="history-deleted">' . $time . '</span>';
1330 $link = $sk->linkKnown(
1335 'target' => $this->mTargetObj
->getPrefixedText(),
1339 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1340 $link = '<span class="history-deleted">' . $link . '</span>';
1346 * Fetch image view link if it's available to all users
1350 * @return String: HTML fragment
1352 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1353 global $wgLang, $wgUser;
1355 if( !$file->userCan( File
::DELETED_FILE
) ) {
1356 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1358 $link = $sk->linkKnown(
1360 $wgLang->timeanddate( $ts, true ),
1363 'target' => $this->mTargetObj
->getPrefixedText(),
1365 'token' => $wgUser->editToken( $key )
1368 if( $file->isDeleted(File
::DELETED_FILE
) )
1369 $link = '<span class="history-deleted">' . $link . '</span>';
1375 * Fetch file's user id if it's available to this user
1379 * @return String: HTML fragment
1381 function getFileUser( $file, $sk ) {
1382 if( !$file->userCan( File
::DELETED_USER
) ) {
1383 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1385 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1386 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1387 if( $file->isDeleted( File
::DELETED_USER
) )
1388 $link = '<span class="history-deleted">' . $link . '</span>';
1394 * Fetch file upload comment if it's available to this user
1398 * @return String: HTML fragment
1400 function getFileComment( $file, $sk ) {
1401 if( !$file->userCan( File
::DELETED_COMMENT
) ) {
1402 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1404 $link = $sk->commentBlock( $file->getRawDescription() );
1405 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1406 $link = '<span class="history-deleted">' . $link . '</span>';
1411 function undelete() {
1412 global $wgOut, $wgUser;
1413 if ( wfReadOnly() ) {
1414 $wgOut->readOnlyPage();
1417 if( !is_null( $this->mTargetObj
) ) {
1418 $archive = new PageArchive( $this->mTargetObj
);
1419 $ok = $archive->undelete(
1420 $this->mTargetTimestamp
,
1422 $this->mFileVersions
,
1423 $this->mUnsuppress
);
1425 if( is_array($ok) ) {
1426 if ( $ok[1] ) // Undeleted file count
1427 wfRunHooks( 'FileUndeleteComplete', array(
1428 $this->mTargetObj
, $this->mFileVersions
,
1429 $wgUser, $this->mComment
) );
1431 $skin = $wgUser->getSkin();
1432 $link = $skin->linkKnown( $this->mTargetObj
);
1433 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1435 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1436 $wgOut->addWikiMsg( 'undeleterevdel' );
1439 // Show file deletion warnings and errors
1440 $status = $archive->getFileStatus();
1441 if( $status && !$status->isGood() ) {
1442 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1445 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );