4 * Used to show archived pages and eventually restore them.
11 function __construct( $title ) {
12 if( is_null( $title ) ) {
13 throw new MWException( __METHOD__
. ' given a null title.' );
15 $this->title
= $title;
19 * List all deleted pages recorded in the archive table. Returns result
20 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
23 * @return ResultWrapper
25 public static function listAllPages() {
26 $dbr = wfGetDB( DB_SLAVE
);
27 return self
::listPages( $dbr, '' );
31 * List deleted pages recorded in the archive table matching the
33 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
35 * @param $prefix String: title prefix
36 * @return ResultWrapper
38 public static function listPagesByPrefix( $prefix ) {
39 $dbr = wfGetDB( DB_SLAVE
);
41 $title = Title
::newFromText( $prefix );
43 $ns = $title->getNamespace();
44 $prefix = $title->getDBkey();
46 // Prolly won't work too good
47 // @todo handle bare namespace names cleanly?
51 'ar_namespace' => $ns,
52 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
54 return self
::listPages( $dbr, $conds );
57 protected static function listPages( $dbr, $condition ) {
58 return $dbr->resultObject(
69 'GROUP BY' => 'ar_namespace,ar_title',
70 'ORDER BY' => 'ar_namespace,ar_title',
78 * List the revisions of the given page. Returns result wrapper with
79 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
81 * @return ResultWrapper
83 function listRevisions() {
84 $dbr = wfGetDB( DB_SLAVE
);
85 $res = $dbr->select( 'archive',
86 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
87 array( 'ar_namespace' => $this->title
->getNamespace(),
88 'ar_title' => $this->title
->getDBkey() ),
89 'PageArchive::listRevisions',
90 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
91 $ret = $dbr->resultObject( $res );
96 * List the deleted file revisions for this page, if it's a file page.
97 * Returns a result wrapper with various filearchive fields, or null
100 * @return ResultWrapper
101 * @todo Does this belong in Image for fuller encapsulation?
103 function listFiles() {
104 if( $this->title
->getNamespace() == NS_FILE
) {
105 $dbr = wfGetDB( DB_SLAVE
);
106 $res = $dbr->select( 'filearchive',
126 array( 'fa_name' => $this->title
->getDBkey() ),
128 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
129 $ret = $dbr->resultObject( $res );
136 * Fetch (and decompress if necessary) the stored text for the deleted
137 * revision of the page with the given timestamp.
139 * @param $timestamp String
141 * @deprecated Use getRevision() for more flexible information
143 function getRevisionText( $timestamp ) {
144 $rev = $this->getRevision( $timestamp );
145 return $rev ?
$rev->getText() : null;
149 * Return a Revision object containing data for the deleted revision.
150 * Note that the result *may* or *may not* have a null page ID.
152 * @param $timestamp String
155 function getRevision( $timestamp ) {
156 $dbr = wfGetDB( DB_SLAVE
);
157 $row = $dbr->selectRow( 'archive',
170 array( 'ar_namespace' => $this->title
->getNamespace(),
171 'ar_title' => $this->title
->getDBkey(),
172 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
175 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
182 * Return the most-previous revision, either live or deleted, against
183 * the deleted revision given by timestamp.
185 * May produce unexpected results in case of history merges or other
186 * unusual time issues.
188 * @param $timestamp String
189 * @return Revision or null
191 function getPreviousRevision( $timestamp ) {
192 $dbr = wfGetDB( DB_SLAVE
);
194 // Check the previous deleted revision...
195 $row = $dbr->selectRow( 'archive',
197 array( 'ar_namespace' => $this->title
->getNamespace(),
198 'ar_title' => $this->title
->getDBkey(),
200 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
203 'ORDER BY' => 'ar_timestamp DESC',
205 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
207 $row = $dbr->selectRow( array( 'page', 'revision' ),
208 array( 'rev_id', 'rev_timestamp' ),
210 'page_namespace' => $this->title
->getNamespace(),
211 'page_title' => $this->title
->getDBkey(),
212 'page_id = rev_page',
214 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
217 'ORDER BY' => 'rev_timestamp DESC',
219 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
220 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
222 if( $prevLive && $prevLive > $prevDeleted ) {
223 // Most prior revision was live
224 return Revision
::newFromId( $prevLiveId );
225 } elseif( $prevDeleted ) {
226 // Most prior revision was deleted
227 return $this->getRevision( $prevDeleted );
229 // No prior revision on this page.
235 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
237 * @param $row Object: database row
240 function getTextFromRow( $row ) {
241 if( is_null( $row->ar_text_id
) ) {
242 // An old row from MediaWiki 1.4 or previous.
243 // Text is embedded in this row in classic compression format.
244 return Revision
::getRevisionText( $row, "ar_" );
246 // New-style: keyed to the text storage backend.
247 $dbr = wfGetDB( DB_SLAVE
);
248 $text = $dbr->selectRow( 'text',
249 array( 'old_text', 'old_flags' ),
250 array( 'old_id' => $row->ar_text_id
),
252 return Revision
::getRevisionText( $text );
258 * Fetch (and decompress if necessary) the stored text of the most
259 * recently edited deleted revision of the page.
261 * If there are no archived revisions for the page, returns NULL.
265 function getLastRevisionText() {
266 $dbr = wfGetDB( DB_SLAVE
);
267 $row = $dbr->selectRow( 'archive',
268 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
269 array( 'ar_namespace' => $this->title
->getNamespace(),
270 'ar_title' => $this->title
->getDBkey() ),
272 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
274 return $this->getTextFromRow( $row );
281 * Quick check if any archived revisions are present for the page.
285 function isDeleted() {
286 $dbr = wfGetDB( DB_SLAVE
);
287 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
288 array( 'ar_namespace' => $this->title
->getNamespace(),
289 'ar_title' => $this->title
->getDBkey() ) );
294 * Restore the given (or all) text and file revisions for the page.
295 * Once restored, the items will be removed from the archive tables.
296 * The deletion log will be updated with an undeletion notice.
298 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
299 * @param $comment String
300 * @param $fileVersions Array
301 * @param $unsuppress Boolean
303 * @return array(number of file revisions restored, number of image revisions restored, log message)
304 * on success, false on failure
306 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
307 // If both the set of text revisions and file revisions are empty,
308 // restore everything. Otherwise, just restore the requested items.
309 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
311 $restoreText = $restoreAll ||
!empty( $timestamps );
312 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
314 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
315 $img = wfLocalFile( $this->title
);
316 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
317 $filesRestored = $this->fileStatus
->successCount
;
323 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
324 if($textRestored === false) // It must be one of UNDELETE_*
332 $log = new LogPage( 'delete' );
334 if( $textRestored && $filesRestored ) {
335 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
336 $wgContLang->formatNum( $textRestored ),
337 $wgContLang->formatNum( $filesRestored ) );
338 } elseif( $textRestored ) {
339 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
340 $wgContLang->formatNum( $textRestored ) );
341 } elseif( $filesRestored ) {
342 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
343 $wgContLang->formatNum( $filesRestored ) );
345 wfDebug( "Undelete: nothing undeleted...\n" );
349 if( trim( $comment ) != '' )
350 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
351 $log->addEntry( 'restore', $this->title
, $reason );
353 return array($textRestored, $filesRestored, $reason);
357 * This is the meaty bit -- restores archived revisions of the given page
358 * to the cur/old tables. If the page currently exists, all revisions will
359 * be stuffed into old, otherwise the most recent will go into cur.
361 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
362 * @param $comment String
363 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
365 * @return Mixed: number of revisions restored or false on failure
367 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
370 $restoreAll = empty( $timestamps );
372 $dbw = wfGetDB( DB_MASTER
);
374 # Does this page already exist? We'll have to update it...
375 $article = new Article( $this->title
);
376 $options = 'FOR UPDATE'; // lock page
377 $page = $dbw->selectRow( 'page',
378 array( 'page_id', 'page_latest' ),
379 array( 'page_namespace' => $this->title
->getNamespace(),
380 'page_title' => $this->title
->getDBkey() ),
386 # Page already exists. Import the history, and if necessary
387 # we'll update the latest revision field in the record.
389 $pageId = $page->page_id
;
390 $previousRevId = $page->page_latest
;
391 # Get the time span of this page
392 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
393 array( 'rev_id' => $previousRevId ),
395 if( $previousTimestamp === false ) {
396 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
400 # Have to create a new article...
403 $previousTimestamp = 0;
407 $oldones = '1 = 1'; # All revisions...
409 $oldts = implode( ',',
410 array_map( array( &$dbw, 'addQuotes' ),
411 array_map( array( &$dbw, 'timestamp' ),
414 $oldones = "ar_timestamp IN ( {$oldts} )";
418 * Select each archived revision...
420 $result = $dbw->select( 'archive',
435 'ar_namespace' => $this->title
->getNamespace(),
436 'ar_title' => $this->title
->getDBkey(),
439 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
441 $ret = $dbw->resultObject( $result );
442 $rev_count = $dbw->numRows( $result );
444 wfDebug( __METHOD__
.": no revisions to restore\n" );
448 $ret->seek( $rev_count - 1 ); // move to last
449 $row = $ret->fetchObject(); // get newest archived rev
450 $ret->seek( 0 ); // move back
453 // Check the state of the newest to-be version...
454 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
455 return false; // we can't leave the current revision like this!
457 // Safe to insert now...
458 $newid = $article->insertOn( $dbw );
461 // Check if a deleted revision will become the current revision...
462 if( $row->ar_timestamp
> $previousTimestamp ) {
463 // Check the state of the newest to-be version...
464 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
465 return false; // we can't leave the current revision like this!
473 while( $row = $ret->fetchObject() ) {
474 // Check for key dupes due to shitty archive integrity.
475 if( $row->ar_rev_id
) {
476 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
477 if( $exists ) continue; // don't throw DB errors
479 // Insert one revision at a time...maintaining deletion status
480 // unless we are specifically removing all restrictions...
481 $revision = Revision
::newFromArchiveRow( $row,
484 'deleted' => $unsuppress ?
0 : $row->ar_deleted
487 $revision->insertOn( $dbw );
490 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
492 # Now that it's safely stored, take it out of the archive
493 $dbw->delete( 'archive',
495 'ar_namespace' => $this->title
->getNamespace(),
496 'ar_title' => $this->title
->getDBkey(),
500 // Was anything restored at all?
505 // Attach the latest revision to the page...
506 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
507 if( $newid ||
$wasnew ) {
508 // Update site stats, link tables, etc
509 $article->createUpdates( $revision );
513 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
514 Article
::onArticleCreate( $this->title
);
516 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
517 Article
::onArticleEdit( $this->title
);
520 if( $this->title
->getNamespace() == NS_FILE
) {
521 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
525 // Revision couldn't be created. This is very weird
526 return self
::UNDELETE_UNKNOWNERR
;
532 function getFileStatus() { return $this->fileStatus
; }
536 * Special page allowing users with the appropriate permissions to view
537 * and restore deleted content.
539 * @ingroup SpecialPage
541 class UndeleteForm
extends SpecialPage
{
542 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
543 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
545 function __construct( $request = null ) {
546 parent
::__construct( 'Undelete', 'deletedhistory' );
548 if ( $request === null ) {
550 $this->mRequest
= $wgRequest;
552 $this->mRequest
= $request;
556 function loadRequest() {
558 $this->mAction
= $this->mRequest
->getVal( 'action' );
559 $this->mTarget
= $this->mRequest
->getVal( 'target' );
560 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
561 $time = $this->mRequest
->getVal( 'timestamp' );
562 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
563 $this->mFile
= $this->mRequest
->getVal( 'file' );
565 $posted = $this->mRequest
->wasPosted() &&
566 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
567 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
568 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
569 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
570 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
571 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
572 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
573 $this->mToken
= $this->mRequest
->getVal( 'token' );
575 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
576 $this->mAllowed
= true; // user can restore
577 $this->mCanView
= true; // user can view content
578 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
579 $this->mAllowed
= false; // user cannot restore
580 $this->mCanView
= true; // user can view content
581 } else { // user can only view the list of revisions
582 $this->mAllowed
= false;
583 $this->mCanView
= false;
584 $this->mTimestamp
= '';
585 $this->mRestore
= false;
588 if( $this->mRestore ||
$this->mInvert
) {
589 $timestamps = array();
590 $this->mFileVersions
= array();
591 foreach( $_REQUEST as $key => $val ) {
593 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
594 array_push( $timestamps, $matches[1] );
597 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
598 $this->mFileVersions
[] = intval( $matches[1] );
601 rsort( $timestamps );
602 $this->mTargetTimestamp
= $timestamps;
606 function execute( $par ) {
607 global $wgOut, $wgUser;
610 if ( !$this->userCanExecute( $wgUser ) ) {
611 $this->displayRestrictionError();
614 $this->outputHeader();
616 $this->loadRequest();
618 if ( $this->mAllowed
) {
619 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
621 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
625 $this->mTarget
= $par;
627 if ( $this->mTarget
!== '' ) {
628 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
630 $this->mTargetObj
= null;
633 if( is_null( $this->mTargetObj
) ) {
634 # Not all users can just browse every deleted page from the list
635 if( $wgUser->isAllowed( 'browsearchive' ) ) {
636 $this->showSearchForm();
638 # List undeletable articles
639 if( $this->mSearchPrefix
) {
640 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
641 $this->showList( $result );
644 $wgOut->addWikiMsg( 'undelete-header' );
648 if( $this->mTimestamp
!== '' ) {
649 return $this->showRevision( $this->mTimestamp
);
651 if( $this->mFile
!== null ) {
652 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
653 // Check if user is allowed to see this file
654 if ( !$file->exists() ) {
655 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile
);
657 } else if( !$file->userCan( File
::DELETED_FILE
) ) {
658 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
659 $wgOut->permissionRequired( 'suppressrevision' );
661 $wgOut->permissionRequired( 'deletedtext' );
664 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
665 $this->showFileConfirmationForm( $this->mFile
);
668 return $this->showFile( $this->mFile
);
671 if( $this->mRestore
&& $this->mAction
== "submit" ) {
672 global $wgUploadMaintenance;
673 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
674 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
677 return $this->undelete();
679 if( $this->mInvert
&& $this->mAction
== "submit" ) {
680 return $this->showHistory( );
682 return $this->showHistory();
685 function showSearchForm() {
686 global $wgOut, $wgScript;
687 $wgOut->addWikiMsg( 'undelete-header' );
690 Xml
::openElement( 'form', array(
692 'action' => $wgScript ) ) .
693 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
694 Xml
::hidden( 'title',
695 $this->getTitle()->getPrefixedDbKey() ) .
696 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
697 'prefix', 'prefix', 20,
698 $this->mSearchPrefix
) . ' ' .
699 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
700 Xml
::closeElement( 'fieldset' ) .
701 Xml
::closeElement( 'form' )
705 // Generic list of deleted pages
706 private function showList( $result ) {
707 global $wgLang, $wgContLang, $wgUser, $wgOut;
709 if( $result->numRows() == 0 ) {
710 $wgOut->addWikiMsg( 'undelete-no-results' );
714 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
716 $sk = $wgUser->getSkin();
717 $undelete = $this->getTitle();
718 $wgOut->addHTML( "<ul>\n" );
719 while( $row = $result->fetchObject() ) {
720 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
721 $link = $sk->linkKnown(
723 htmlspecialchars( $title->getPrefixedText() ),
725 array( 'target' => $title->getPrefixedText() )
727 $revs = wfMsgExt( 'undeleterevisions',
728 array( 'parseinline' ),
729 $wgLang->formatNum( $row->count
) );
730 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
733 $wgOut->addHTML( "</ul>\n" );
738 private function showRevision( $timestamp ) {
739 global $wgLang, $wgUser, $wgOut;
741 $skin = $wgUser->getSkin();
743 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
745 $archive = new PageArchive( $this->mTargetObj
);
746 $rev = $archive->getRevision( $timestamp );
749 $wgOut->addWikiMsg( 'undeleterevision-missing' );
753 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
754 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
755 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
758 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
759 $wgOut->addHTML( '<br />' );
760 // and we are allowed to see...
764 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
766 $link = $skin->linkKnown(
767 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
768 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
772 $previousRev = $archive->getPreviousRevision( $timestamp );
774 $this->showDiff( $previousRev, $rev );
775 if( $wgUser->getOption( 'diffonly' ) ) {
778 $wgOut->addHTML( '<hr />' );
781 $wgOut->addWikiMsg( 'undelete-nodiff' );
785 // date and time are separate parameters to facilitate localisation.
786 // $time is kept for backward compat reasons.
787 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
788 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
789 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
790 $user = $skin->revUserTools( $rev );
792 if( $this->mPreview
) {
793 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
795 $openDiv = '<div id="mw-undelete-revision">';
798 // Revision delete links
799 $canHide = $wgUser->isAllowed( 'deleterevision' );
801 $revdlink = ''; // diffs already have revision delete links
802 } else if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
803 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
804 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
808 'target' => $this->mTargetObj
->getPrefixedDBkey(),
809 'ids' => $rev->getTimestamp()
811 $revdlink = $skin->revDeleteLink( $query,
812 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
818 $wgOut->addHTML( $openDiv . $revdlink . wfMsgWikiHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</div>' );
819 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
821 if( $this->mPreview
) {
823 $popts = $wgOut->parserOptions();
824 $popts->setEditSection( false );
825 $wgOut->parserOptions( $popts );
826 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
830 Xml
::element( 'textarea', array(
831 'readonly' => 'readonly',
832 'cols' => intval( $wgUser->getOption( 'cols' ) ),
833 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
834 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
835 Xml
::openElement( 'div' ) .
836 Xml
::openElement( 'form', array(
838 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
839 Xml
::element( 'input', array(
842 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
843 Xml
::element( 'input', array(
845 'name' => 'timestamp',
846 'value' => $timestamp ) ) .
847 Xml
::element( 'input', array(
849 'name' => 'wpEditToken',
850 'value' => $wgUser->editToken() ) ) .
851 Xml
::element( 'input', array(
854 'value' => wfMsg( 'showpreview' ) ) ) .
855 Xml
::element( 'input', array(
858 'value' => wfMsg( 'showdiff' ) ) ) .
859 Xml
::closeElement( 'form' ) .
860 Xml
::closeElement( 'div' ) );
864 * Build a diff display between this and the previous either deleted
865 * or non-deleted edit.
867 * @param $previousRev Revision
868 * @param $currentRev Revision
869 * @return String: HTML
871 function showDiff( $previousRev, $currentRev ) {
874 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
875 $diffEngine->showDiffStyle();
878 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
879 "<col class='diff-marker' />" .
880 "<col class='diff-content' />" .
881 "<col class='diff-marker' />" .
882 "<col class='diff-content' />" .
884 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
885 $this->diffHeader( $previousRev, 'o' ) .
887 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
888 $this->diffHeader( $currentRev, 'n' ) .
891 $diffEngine->generateDiffBody(
892 $previousRev->getText(), $currentRev->getText() ) .
898 private function diffHeader( $rev, $prefix ) {
899 global $wgUser, $wgLang;
900 $sk = $wgUser->getSkin();
901 $isDeleted = !( $rev->getId() && $rev->getTitle() );
903 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
904 $targetPage = $this->getTitle();
905 $targetQuery = array(
906 'target' => $this->mTargetObj
->getPrefixedText(),
907 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
910 /// @todo Fixme getId() may return non-zero for deleted revs...
911 $targetPage = $rev->getTitle();
912 $targetQuery = array( 'oldid' => $rev->getId() );
914 // Add show/hide deletion links if available
915 $canHide = $wgUser->isAllowed( 'deleterevision' );
916 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
918 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
919 $del .= $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
923 'target' => $this->mTargetObj
->getPrefixedDbkey(),
924 'ids' => $rev->getTimestamp()
926 $del .= $sk->revDeleteLink( $query,
927 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
933 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
938 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
939 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
940 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
946 '<div id="mw-diff-'.$prefix.'title2">' .
947 $sk->revUserTools( $rev ) . '<br />' .
949 '<div id="mw-diff-'.$prefix.'title3">' .
950 $sk->revComment( $rev ) . $del . '<br />' .
955 * Show a form confirming whether a tokenless user really wants to see a file
957 private function showFileConfirmationForm( $key ) {
958 global $wgOut, $wgUser, $wgLang;
959 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
960 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
961 $this->mTargetObj
->getText(),
962 $wgLang->date( $file->getTimestamp() ),
963 $wgLang->time( $file->getTimestamp() ) );
965 Xml
::openElement( 'form', array(
967 'action' => $this->getTitle()->getLocalUrl(
968 'target=' . urlencode( $this->mTarget
) .
969 '&file=' . urlencode( $key ) .
970 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
973 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
979 * Show a deleted file version requested by the visitor.
981 private function showFile( $key ) {
982 global $wgOut, $wgRequest;
985 # We mustn't allow the output to be Squid cached, otherwise
986 # if an admin previews a deleted image, and it's cached, then
987 # a user without appropriate permissions can toddle off and
988 # nab the image, and Squid will serve it
989 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
990 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
991 $wgRequest->response()->header( 'Pragma: no-cache' );
994 require_once( "$IP/includes/StreamFile.php" );
995 $repo = RepoGroup
::singleton()->getLocalRepo();
996 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
997 wfStreamFile( $path );
1000 private function showHistory( ) {
1001 global $wgLang, $wgUser, $wgOut;
1003 $sk = $wgUser->getSkin();
1004 if( $this->mAllowed
) {
1005 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
1007 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
1010 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n", array ( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() ) );
1012 $archive = new PageArchive( $this->mTargetObj
);
1014 $text = $archive->getLastRevisionText();
1015 if( is_null( $text ) ) {
1016 $wgOut->addWikiMsg( "nohistory" );
1020 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1021 if ( $this->mAllowed
) {
1022 $wgOut->addWikiMsg( "undeletehistory" );
1023 $wgOut->addWikiMsg( "undeleterevdel" );
1025 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
1027 $wgOut->addHTML( '</div>' );
1029 # List all stored revisions
1030 $revisions = $archive->listRevisions();
1031 $files = $archive->listFiles();
1033 $haveRevisions = $revisions && $revisions->numRows() > 0;
1034 $haveFiles = $files && $files->numRows() > 0;
1036 # Batch existence check on user and talk pages
1037 if( $haveRevisions ) {
1038 $batch = new LinkBatch();
1039 while( $row = $revisions->fetchObject() ) {
1040 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1041 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1044 $revisions->seek( 0 );
1047 $batch = new LinkBatch();
1048 while( $row = $files->fetchObject() ) {
1049 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1050 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1056 if ( $this->mAllowed
) {
1057 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1058 # Start the form here
1059 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1060 $wgOut->addHTML( $top );
1063 # Show relevant lines from the deletion log:
1064 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1065 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1066 # Show relevant lines from the suppression log:
1067 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1068 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1069 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1072 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1073 # Format the user-visible controls (comment field, submission button)
1074 # in a nice little table
1075 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1079 <td class='mw-input'>" .
1080 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1081 'mw-undelete-unsuppress', $this->mUnsuppress
).
1085 $unsuppressBox = "";
1088 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1089 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1091 <td colspan='2' class='mw-undelete-extrahelp'>" .
1092 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1096 <td class='mw-label'>" .
1097 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1099 <td class='mw-input'>" .
1100 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1105 <td class='mw-submit'>" .
1106 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1107 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1108 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1112 Xml
::closeElement( 'table' ) .
1113 Xml
::closeElement( 'fieldset' );
1115 $wgOut->addHTML( $table );
1118 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1120 if( $haveRevisions ) {
1121 # The page's stored (deleted) history:
1122 $wgOut->addHTML("<ul>");
1123 $target = urlencode( $this->mTarget
);
1124 $remaining = $revisions->numRows();
1125 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1127 while( $row = $revisions->fetchObject() ) {
1129 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1132 $wgOut->addHTML("</ul>");
1134 $wgOut->addWikiMsg( "nohistory" );
1138 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1139 $wgOut->addHTML( "<ul>" );
1140 while( $row = $files->fetchObject() ) {
1141 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1144 $wgOut->addHTML( "</ul>" );
1147 if ( $this->mAllowed
) {
1148 # Slip in the hidden controls here
1149 $misc = Xml
::hidden( 'target', $this->mTarget
);
1150 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1151 $misc .= Xml
::closeElement( 'form' );
1152 $wgOut->addHTML( $misc );
1158 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1159 global $wgUser, $wgLang;
1161 $rev = Revision
::newFromArchiveRow( $row,
1162 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1164 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1165 // Build checkboxen...
1166 if( $this->mAllowed
) {
1167 if( $this->mInvert
) {
1168 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1169 $checkBox = Xml
::check( "ts$ts");
1171 $checkBox = Xml
::check( "ts$ts", true );
1174 $checkBox = Xml
::check( "ts$ts" );
1179 // Build page & diff links...
1180 if( $this->mCanView
) {
1181 $titleObj = $this->getTitle();
1183 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1184 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1185 $last = wfMsgHtml('diff');
1186 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1187 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1188 $last = $sk->linkKnown(
1193 'target' => $this->mTargetObj
->getPrefixedText(),
1199 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1200 $last = wfMsgHtml('diff');
1203 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1204 $last = wfMsgHtml('diff');
1207 $userLink = $sk->revUserTools( $rev );
1208 // Revision text size
1209 if( !is_null($size = $row->ar_len
) ) {
1210 $stxt = $sk->formatRevisionSize( $size );
1213 $comment = $sk->revComment( $rev );
1214 // Revision delete links
1215 $canHide = $wgUser->isAllowed( 'deleterevision' );
1216 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1217 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1218 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1221 'type' => 'archive',
1222 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1225 $revdlink = $sk->revDeleteLink( $query,
1226 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
1231 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1234 private function formatFileRow( $row, $sk ) {
1235 global $wgUser, $wgLang;
1237 $file = ArchivedFile
::newFromRow( $row );
1239 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1240 if( $this->mAllowed
&& $row->fa_storage_key
) {
1241 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1242 $key = urlencode( $row->fa_storage_key
);
1243 $target = urlencode( $this->mTarget
);
1244 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1247 $pageLink = $wgLang->timeanddate( $ts, true );
1249 $userLink = $this->getFileUser( $file, $sk );
1251 wfMsg( 'widthheight',
1252 $wgLang->formatNum( $row->fa_width
),
1253 $wgLang->formatNum( $row->fa_height
) ) .
1255 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1257 $data = htmlspecialchars( $data );
1258 $comment = $this->getFileComment( $file, $sk );
1259 // Add show/hide deletion links if available
1260 $canHide = $wgUser->isAllowed( 'deleterevision' );
1261 if( $canHide ||
($file->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1262 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1263 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1266 'type' => 'filearchive',
1267 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1268 'ids' => $row->fa_id
1270 $revdlink = $sk->revDeleteLink( $query,
1271 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1276 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1280 * Fetch revision text link if it's available to all users
1283 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1286 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1288 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1289 return '<span class="history-deleted">' . $time . '</span>';
1291 $link = $sk->linkKnown(
1296 'target' => $this->mTargetObj
->getPrefixedText(),
1300 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1301 $link = '<span class="history-deleted">' . $link . '</span>';
1307 * Fetch image view link if it's available to all users
1309 * @return String: HTML fragment
1311 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1312 global $wgLang, $wgUser;
1314 if( !$file->userCan(File
::DELETED_FILE
) ) {
1315 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1317 $link = $sk->linkKnown(
1319 $wgLang->timeanddate( $ts, true ),
1322 'target' => $this->mTargetObj
->getPrefixedText(),
1324 'token' => $wgUser->editToken( $key )
1327 if( $file->isDeleted(File
::DELETED_FILE
) )
1328 $link = '<span class="history-deleted">' . $link . '</span>';
1334 * Fetch file's user id if it's available to this user
1336 * @return String: HTML fragment
1338 function getFileUser( $file, $sk ) {
1339 if( !$file->userCan(File
::DELETED_USER
) ) {
1340 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1342 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1343 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1344 if( $file->isDeleted(File
::DELETED_USER
) )
1345 $link = '<span class="history-deleted">' . $link . '</span>';
1351 * Fetch file upload comment if it's available to this user
1353 * @return String: HTML fragment
1355 function getFileComment( $file, $sk ) {
1356 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1357 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1359 $link = $sk->commentBlock( $file->getRawDescription() );
1360 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1361 $link = '<span class="history-deleted">' . $link . '</span>';
1366 function undelete() {
1367 global $wgOut, $wgUser;
1368 if ( wfReadOnly() ) {
1369 $wgOut->readOnlyPage();
1372 if( !is_null( $this->mTargetObj
) ) {
1373 $archive = new PageArchive( $this->mTargetObj
);
1374 $ok = $archive->undelete(
1375 $this->mTargetTimestamp
,
1377 $this->mFileVersions
,
1378 $this->mUnsuppress
);
1380 if( is_array($ok) ) {
1381 if ( $ok[1] ) // Undeleted file count
1382 wfRunHooks( 'FileUndeleteComplete', array(
1383 $this->mTargetObj
, $this->mFileVersions
,
1384 $wgUser, $this->mComment
) );
1386 $skin = $wgUser->getSkin();
1387 $link = $skin->linkKnown( $this->mTargetObj
);
1388 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1390 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1391 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1394 // Show file deletion warnings and errors
1395 $status = $archive->getFileStatus();
1396 if( $status && !$status->isGood() ) {
1397 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1400 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );