e0f77679f77aecc92ff85b2ff610a5c78fe2231b
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',
113 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
114 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id'
116 array( 'ar_namespace' => $this->title
->getNamespace(),
117 'ar_title' => $this->title
->getDBkey() ),
118 'PageArchive::listRevisions',
119 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
120 $ret = $dbr->resultObject( $res );
125 * List the deleted file revisions for this page, if it's a file page.
126 * Returns a result wrapper with various filearchive fields, or null
127 * if not a file page.
129 * @return ResultWrapper
130 * @todo Does this belong in Image for fuller encapsulation?
132 function listFiles() {
133 if( $this->title
->getNamespace() == NS_FILE
) {
134 $dbr = wfGetDB( DB_SLAVE
);
135 $res = $dbr->select( 'filearchive',
155 array( 'fa_name' => $this->title
->getDBkey() ),
157 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
158 $ret = $dbr->resultObject( $res );
165 * Return a Revision object containing data for the deleted revision.
166 * Note that the result *may* or *may not* have a null page ID.
168 * @param $timestamp String
171 function getRevision( $timestamp ) {
172 $dbr = wfGetDB( DB_SLAVE
);
173 $row = $dbr->selectRow( 'archive',
186 array( 'ar_namespace' => $this->title
->getNamespace(),
187 'ar_title' => $this->title
->getDBkey(),
188 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
191 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
198 * Return the most-previous revision, either live or deleted, against
199 * the deleted revision given by timestamp.
201 * May produce unexpected results in case of history merges or other
202 * unusual time issues.
204 * @param $timestamp String
205 * @return Revision or null
207 function getPreviousRevision( $timestamp ) {
208 $dbr = wfGetDB( DB_SLAVE
);
210 // Check the previous deleted revision...
211 $row = $dbr->selectRow( 'archive',
213 array( 'ar_namespace' => $this->title
->getNamespace(),
214 'ar_title' => $this->title
->getDBkey(),
216 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
219 'ORDER BY' => 'ar_timestamp DESC',
221 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
223 $row = $dbr->selectRow( array( 'page', 'revision' ),
224 array( 'rev_id', 'rev_timestamp' ),
226 'page_namespace' => $this->title
->getNamespace(),
227 'page_title' => $this->title
->getDBkey(),
228 'page_id = rev_page',
230 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
233 'ORDER BY' => 'rev_timestamp DESC',
235 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
236 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
238 if( $prevLive && $prevLive > $prevDeleted ) {
239 // Most prior revision was live
240 return Revision
::newFromId( $prevLiveId );
241 } elseif( $prevDeleted ) {
242 // Most prior revision was deleted
243 return $this->getRevision( $prevDeleted );
245 // No prior revision on this page.
251 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
253 * @param $row Object: database row
256 function getTextFromRow( $row ) {
257 if( is_null( $row->ar_text_id
) ) {
258 // An old row from MediaWiki 1.4 or previous.
259 // Text is embedded in this row in classic compression format.
260 return Revision
::getRevisionText( $row, 'ar_' );
262 // New-style: keyed to the text storage backend.
263 $dbr = wfGetDB( DB_SLAVE
);
264 $text = $dbr->selectRow( 'text',
265 array( 'old_text', 'old_flags' ),
266 array( 'old_id' => $row->ar_text_id
),
268 return Revision
::getRevisionText( $text );
273 * Fetch (and decompress if necessary) the stored text of the most
274 * recently edited deleted revision of the page.
276 * If there are no archived revisions for the page, returns NULL.
280 function getLastRevisionText() {
281 $dbr = wfGetDB( DB_SLAVE
);
282 $row = $dbr->selectRow( 'archive',
283 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
284 array( 'ar_namespace' => $this->title
->getNamespace(),
285 'ar_title' => $this->title
->getDBkey() ),
287 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
289 return $this->getTextFromRow( $row );
296 * Quick check if any archived revisions are present for the page.
300 function isDeleted() {
301 $dbr = wfGetDB( DB_SLAVE
);
302 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
303 array( 'ar_namespace' => $this->title
->getNamespace(),
304 'ar_title' => $this->title
->getDBkey() ) );
309 * Restore the given (or all) text and file revisions for the page.
310 * Once restored, the items will be removed from the archive tables.
311 * The deletion log will be updated with an undeletion notice.
313 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
314 * @param $comment String
315 * @param $fileVersions Array
316 * @param $unsuppress Boolean
318 * @return array(number of file revisions restored, number of image revisions restored, log message)
319 * on success, false on failure
321 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
322 // If both the set of text revisions and file revisions are empty,
323 // restore everything. Otherwise, just restore the requested items.
324 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
326 $restoreText = $restoreAll ||
!empty( $timestamps );
327 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
329 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
330 $img = wfLocalFile( $this->title
);
331 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
332 if ( !$this->fileStatus
->isOk() ) {
335 $filesRestored = $this->fileStatus
->successCount
;
341 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
342 if( $textRestored === false ) { // It must be one of UNDELETE_*
351 $log = new LogPage( 'delete' );
353 if( $textRestored && $filesRestored ) {
354 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
355 $wgContLang->formatNum( $textRestored ),
356 $wgContLang->formatNum( $filesRestored ) );
357 } elseif( $textRestored ) {
358 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
359 $wgContLang->formatNum( $textRestored ) );
360 } elseif( $filesRestored ) {
361 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
362 $wgContLang->formatNum( $filesRestored ) );
364 wfDebug( "Undelete: nothing undeleted...\n" );
368 if( trim( $comment ) != '' ) {
369 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
371 $log->addEntry( 'restore', $this->title
, $reason );
373 return array( $textRestored, $filesRestored, $reason );
377 * This is the meaty bit -- restores archived revisions of the given page
378 * to the cur/old tables. If the page currently exists, all revisions will
379 * be stuffed into old, otherwise the most recent will go into cur.
381 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
382 * @param $comment String
383 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
385 * @return Mixed: number of revisions restored or false on failure
387 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
388 if ( wfReadOnly() ) {
391 $restoreAll = empty( $timestamps );
393 $dbw = wfGetDB( DB_MASTER
);
395 # Does this page already exist? We'll have to update it...
396 $article = new Article( $this->title
);
397 $options = 'FOR UPDATE'; // lock page
398 $page = $dbw->selectRow( 'page',
399 array( 'page_id', 'page_latest' ),
400 array( 'page_namespace' => $this->title
->getNamespace(),
401 'page_title' => $this->title
->getDBkey() ),
407 # Page already exists. Import the history, and if necessary
408 # we'll update the latest revision field in the record.
410 $pageId = $page->page_id
;
411 $previousRevId = $page->page_latest
;
412 # Get the time span of this page
413 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
414 array( 'rev_id' => $previousRevId ),
416 if( $previousTimestamp === false ) {
417 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
421 # Have to create a new article...
424 $previousTimestamp = 0;
428 $oldones = '1 = 1'; # All revisions...
430 $oldts = implode( ',',
431 array_map( array( &$dbw, 'addQuotes' ),
432 array_map( array( &$dbw, 'timestamp' ),
435 $oldones = "ar_timestamp IN ( {$oldts} )";
439 * Select each archived revision...
441 $result = $dbw->select( 'archive',
456 'ar_namespace' => $this->title
->getNamespace(),
457 'ar_title' => $this->title
->getDBkey(),
460 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
462 $ret = $dbw->resultObject( $result );
463 $rev_count = $dbw->numRows( $result );
465 wfDebug( __METHOD__
. ": no revisions to restore\n" );
469 $ret->seek( $rev_count - 1 ); // move to last
470 $row = $ret->fetchObject(); // get newest archived rev
471 $ret->seek( 0 ); // move back
474 // Check the state of the newest to-be version...
475 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
476 return false; // we can't leave the current revision like this!
478 // Safe to insert now...
479 $newid = $article->insertOn( $dbw );
482 // Check if a deleted revision will become the current revision...
483 if( $row->ar_timestamp
> $previousTimestamp ) {
484 // Check the state of the newest to-be version...
485 if( !$unsuppress && ( $row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
486 return false; // we can't leave the current revision like this!
494 foreach ( $ret as $row ) {
495 // Check for key dupes due to shitty archive integrity.
496 if( $row->ar_rev_id
) {
497 $exists = $dbw->selectField( 'revision', '1',
498 array( 'rev_id' => $row->ar_rev_id
), __METHOD__
);
500 continue; // don't throw DB errors
503 // Insert one revision at a time...maintaining deletion status
504 // unless we are specifically removing all restrictions...
505 $revision = Revision
::newFromArchiveRow( $row,
508 'deleted' => $unsuppress ?
0 : $row->ar_deleted
511 $revision->insertOn( $dbw );
514 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
516 # Now that it's safely stored, take it out of the archive
517 $dbw->delete( 'archive',
519 'ar_namespace' => $this->title
->getNamespace(),
520 'ar_title' => $this->title
->getDBkey(),
524 // Was anything restored at all?
525 if ( $restored == 0 ) {
529 // Attach the latest revision to the page...
530 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
531 if ( $newid ||
$wasnew ) {
532 // Update site stats, link tables, etc
533 $article->createUpdates( $revision );
537 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
538 Article
::onArticleCreate( $this->title
);
540 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
541 Article
::onArticleEdit( $this->title
);
544 if( $this->title
->getNamespace() == NS_FILE
) {
545 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
555 function getFileStatus() { return $this->fileStatus
; }
559 * Special page allowing users with the appropriate permissions to view
560 * and restore deleted content.
562 * @ingroup SpecialPage
564 class SpecialUndelete
extends SpecialPage
{
565 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj, $mFile;
566 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
568 function __construct( $request = null ) {
569 parent
::__construct( 'Undelete', 'deletedhistory' );
571 if ( $request === null ) {
573 $this->mRequest
= $wgRequest;
575 $this->mRequest
= $request;
579 function loadRequest() {
581 $this->mAction
= $this->mRequest
->getVal( 'action' );
582 $this->mTarget
= $this->mRequest
->getVal( 'target' );
583 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
584 $time = $this->mRequest
->getVal( 'timestamp' );
585 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
586 $this->mFile
= $this->mRequest
->getVal( 'file' );
588 $posted = $this->mRequest
->wasPosted() &&
589 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
590 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
591 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
592 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
593 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
594 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
595 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
596 $this->mToken
= $this->mRequest
->getVal( 'token' );
598 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
599 $this->mAllowed
= true; // user can restore
600 $this->mCanView
= true; // user can view content
601 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
602 $this->mAllowed
= false; // user cannot restore
603 $this->mCanView
= true; // user can view content
604 } else { // user can only view the list of revisions
605 $this->mAllowed
= false;
606 $this->mCanView
= false;
607 $this->mTimestamp
= '';
608 $this->mRestore
= false;
611 if( $this->mRestore ||
$this->mInvert
) {
612 $timestamps = array();
613 $this->mFileVersions
= array();
614 foreach( $_REQUEST as $key => $val ) {
616 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
617 array_push( $timestamps, $matches[1] );
620 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
621 $this->mFileVersions
[] = intval( $matches[1] );
624 rsort( $timestamps );
625 $this->mTargetTimestamp
= $timestamps;
629 function execute( $par ) {
630 global $wgOut, $wgUser;
633 if ( !$this->userCanExecute( $wgUser ) ) {
634 $this->displayRestrictionError();
637 $this->outputHeader();
639 $this->loadRequest();
641 if ( $this->mAllowed
) {
642 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
644 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
648 $this->mTarget
= $par;
650 if ( $this->mTarget
!== '' ) {
651 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
652 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj
);
654 $this->mTargetObj
= null;
657 if( is_null( $this->mTargetObj
) ) {
658 # Not all users can just browse every deleted page from the list
659 if( $wgUser->isAllowed( 'browsearchive' ) ) {
660 $this->showSearchForm();
662 # List undeletable articles
663 if( $this->mSearchPrefix
) {
664 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
665 $this->showList( $result );
668 $wgOut->addWikiMsg( 'undelete-header' );
672 if( $this->mTimestamp
!== '' ) {
673 return $this->showRevision( $this->mTimestamp
);
675 if( $this->mFile
!== null ) {
676 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
677 // Check if user is allowed to see this file
678 if ( !$file->exists() ) {
679 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile
);
681 } else if( !$file->userCan( File
::DELETED_FILE
) ) {
682 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
683 $wgOut->permissionRequired( 'suppressrevision' );
685 $wgOut->permissionRequired( 'deletedtext' );
688 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
689 $this->showFileConfirmationForm( $this->mFile
);
692 return $this->showFile( $this->mFile
);
695 if( $this->mRestore
&& $this->mAction
== 'submit' ) {
696 global $wgUploadMaintenance;
697 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
698 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
701 return $this->undelete();
703 if( $this->mInvert
&& $this->mAction
== 'submit' ) {
704 return $this->showHistory();
706 return $this->showHistory();
709 function showSearchForm() {
710 global $wgOut, $wgScript;
711 $wgOut->addWikiMsg( 'undelete-header' );
714 Xml
::openElement( 'form', array(
716 'action' => $wgScript ) ) .
717 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
718 Html
::hidden( 'title',
719 $this->getTitle()->getPrefixedDbKey() ) .
720 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
721 'prefix', 'prefix', 20,
722 $this->mSearchPrefix
) . ' ' .
723 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
724 Xml
::closeElement( 'fieldset' ) .
725 Xml
::closeElement( 'form' )
729 // Generic list of deleted pages
730 private function showList( $result ) {
731 global $wgLang, $wgUser, $wgOut;
733 if( $result->numRows() == 0 ) {
734 $wgOut->addWikiMsg( 'undelete-no-results' );
738 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
740 $sk = $wgUser->getSkin();
741 $undelete = $this->getTitle();
742 $wgOut->addHTML( "<ul>\n" );
743 foreach ( $result as $row ) {
744 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
745 $link = $sk->linkKnown(
747 htmlspecialchars( $title->getPrefixedText() ),
749 array( 'target' => $title->getPrefixedText() )
751 $revs = wfMsgExt( 'undeleterevisions',
752 array( 'parseinline' ),
753 $wgLang->formatNum( $row->count
) );
754 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
757 $wgOut->addHTML( "</ul>\n" );
762 private function showRevision( $timestamp ) {
763 global $wgLang, $wgUser, $wgOut;
765 $skin = $wgUser->getSkin();
767 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
771 $archive = new PageArchive( $this->mTargetObj
);
772 $rev = $archive->getRevision( $timestamp );
775 $wgOut->addWikiMsg( 'undeleterevision-missing' );
779 if( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
780 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
781 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
784 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
785 $wgOut->addHTML( '<br />' );
786 // and we are allowed to see...
790 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
792 $link = $skin->linkKnown(
793 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
794 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
798 $previousRev = $archive->getPreviousRevision( $timestamp );
800 $this->showDiff( $previousRev, $rev );
801 if( $wgUser->getOption( 'diffonly' ) ) {
804 $wgOut->addHTML( '<hr />' );
807 $wgOut->addWikiMsg( 'undelete-nodiff' );
811 // date and time are separate parameters to facilitate localisation.
812 // $time is kept for backward compat reasons.
813 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
814 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
815 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
816 $user = $skin->revUserTools( $rev );
818 if( $this->mPreview
) {
819 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
821 $openDiv = '<div id="mw-undelete-revision">';
823 $wgOut->addHTML( $openDiv );
825 // Revision delete links
826 if ( !$this->mDiff
) {
827 $revdel = $this->revDeleteLink( $rev );
829 $wgOut->addHTML( $revdel );
833 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
834 $wgOut->addHTML( '</div>' );
835 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
837 if( $this->mPreview
) {
839 $popts = $wgOut->parserOptions();
840 $popts->setEditSection( false );
841 $wgOut->parserOptions( $popts );
842 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
846 Xml
::element( 'textarea', array(
847 'readonly' => 'readonly',
848 'cols' => intval( $wgUser->getOption( 'cols' ) ),
849 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
850 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
851 Xml
::openElement( 'div' ) .
852 Xml
::openElement( 'form', array(
854 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
855 Xml
::element( 'input', array(
858 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
859 Xml
::element( 'input', array(
861 'name' => 'timestamp',
862 'value' => $timestamp ) ) .
863 Xml
::element( 'input', array(
865 'name' => 'wpEditToken',
866 'value' => $wgUser->editToken() ) ) .
867 Xml
::element( 'input', array(
870 'value' => wfMsg( 'showpreview' ) ) ) .
871 Xml
::element( 'input', array(
874 'value' => wfMsg( 'showdiff' ) ) ) .
875 Xml
::closeElement( 'form' ) .
876 Xml
::closeElement( 'div' ) );
880 * Get a revision-deletion link, or disabled link, or nothing, depending
881 * on user permissions & the settings on the revision.
883 * Will use forward-compatible revision ID in the Special:RevDelete link
884 * if possible, otherwise the timestamp-based ID which may break after
887 * @param Revision $rev
888 * @return string HTML fragment
890 function revDeleteLink( $rev ) {
892 $canHide = $wgUser->isAllowed( 'deleterevision' );
893 if( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
894 $skin = $wgUser->getSkin();
895 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
896 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
898 if ( $rev->getId() ) {
899 // RevDelete links using revision ID are stable across
900 // page deletion and undeletion; use when possible.
902 'type' => 'revision',
903 'target' => $this->mTargetObj
->getPrefixedDBkey(),
904 'ids' => $rev->getId()
907 // Older deleted entries didn't save a revision ID.
908 // We have to refer to these by timestamp, ick!
911 'target' => $this->mTargetObj
->getPrefixedDBkey(),
912 'ids' => $rev->getTimestamp()
915 return $skin->revDeleteLink( $query,
916 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
924 * Build a diff display between this and the previous either deleted
925 * or non-deleted edit.
927 * @param $previousRev Revision
928 * @param $currentRev Revision
929 * @return String: HTML
931 function showDiff( $previousRev, $currentRev ) {
934 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
935 $diffEngine->showDiffStyle();
938 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
939 "<col class='diff-marker' />" .
940 "<col class='diff-content' />" .
941 "<col class='diff-marker' />" .
942 "<col class='diff-content' />" .
944 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
945 $this->diffHeader( $previousRev, 'o' ) .
947 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
948 $this->diffHeader( $currentRev, 'n' ) .
951 $diffEngine->generateDiffBody(
952 $previousRev->getText(), $currentRev->getText() ) .
959 * @param $rev Revision
963 private function diffHeader( $rev, $prefix ) {
964 global $wgUser, $wgLang;
965 $sk = $wgUser->getSkin();
966 $isDeleted = !( $rev->getId() && $rev->getTitle() );
968 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
969 $targetPage = $this->getTitle();
970 $targetQuery = array(
971 'target' => $this->mTargetObj
->getPrefixedText(),
972 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
975 /// @todo Fixme getId() may return non-zero for deleted revs...
976 $targetPage = $rev->getTitle();
977 $targetQuery = array( 'oldid' => $rev->getId() );
979 // Add show/hide deletion links if available
980 $del .= $this->revDeleteLink( $rev );
982 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
987 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
988 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
989 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
995 '<div id="mw-diff-'.$prefix.'title2">' .
996 $sk->revUserTools( $rev ) . '<br />' .
998 '<div id="mw-diff-'.$prefix.'title3">' .
999 $sk->revComment( $rev ) . $del . '<br />' .
1004 * Show a form confirming whether a tokenless user really wants to see a file
1006 private function showFileConfirmationForm( $key ) {
1007 global $wgOut, $wgUser, $wgLang;
1008 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
1009 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
1010 $this->mTargetObj
->getText(),
1011 $wgLang->date( $file->getTimestamp() ),
1012 $wgLang->time( $file->getTimestamp() ) );
1014 Xml
::openElement( 'form', array(
1016 'action' => $this->getTitle()->getLocalURL(
1017 'target=' . urlencode( $this->mTarget
) .
1018 '&file=' . urlencode( $key ) .
1019 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
1022 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1028 * Show a deleted file version requested by the visitor.
1030 private function showFile( $key ) {
1031 global $wgOut, $wgRequest;
1034 # We mustn't allow the output to be Squid cached, otherwise
1035 # if an admin previews a deleted image, and it's cached, then
1036 # a user without appropriate permissions can toddle off and
1037 # nab the image, and Squid will serve it
1038 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1039 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1040 $wgRequest->response()->header( 'Pragma: no-cache' );
1043 require_once( "$IP/includes/StreamFile.php" );
1044 $repo = RepoGroup
::singleton()->getLocalRepo();
1045 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1046 wfStreamFile( $path );
1049 private function showHistory() {
1050 global $wgUser, $wgOut;
1052 $sk = $wgUser->getSkin();
1053 if( $this->mAllowed
) {
1054 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
1056 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
1059 $wgOut->wrapWikiMsg(
1060 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1061 array( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() )
1064 $archive = new PageArchive( $this->mTargetObj
);
1066 $text = $archive->getLastRevisionText();
1067 if( is_null( $text ) ) {
1068 $wgOut->addWikiMsg( 'nohistory' );
1072 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1073 if ( $this->mAllowed
) {
1074 $wgOut->addWikiMsg( 'undeletehistory' );
1075 $wgOut->addWikiMsg( 'undeleterevdel' );
1077 $wgOut->addWikiMsg( 'undeletehistorynoadmin' );
1079 $wgOut->addHTML( '</div>' );
1081 # List all stored revisions
1082 $revisions = $archive->listRevisions();
1083 $files = $archive->listFiles();
1085 $haveRevisions = $revisions && $revisions->numRows() > 0;
1086 $haveFiles = $files && $files->numRows() > 0;
1088 # Batch existence check on user and talk pages
1089 if( $haveRevisions ) {
1090 $batch = new LinkBatch();
1091 foreach ( $revisions as $row ) {
1092 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1093 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1096 $revisions->seek( 0 );
1099 $batch = new LinkBatch();
1100 foreach ( $files as $row ) {
1101 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1102 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1108 if ( $this->mAllowed
) {
1109 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1110 # Start the form here
1111 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1112 $wgOut->addHTML( $top );
1115 # Show relevant lines from the deletion log:
1116 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1117 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1118 # Show relevant lines from the suppression log:
1119 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1120 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1121 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1124 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1125 # Format the user-visible controls (comment field, submission button)
1126 # in a nice little table
1127 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1131 <td class='mw-input'>" .
1132 Xml
::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1133 'mw-undelete-unsuppress', $this->mUnsuppress
).
1137 $unsuppressBox = '';
1140 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1141 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1143 <td colspan='2' class='mw-undelete-extrahelp'>" .
1144 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1148 <td class='mw-label'>" .
1149 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1151 <td class='mw-input'>" .
1152 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1157 <td class='mw-submit'>" .
1158 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1159 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1160 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1164 Xml
::closeElement( 'table' ) .
1165 Xml
::closeElement( 'fieldset' );
1167 $wgOut->addHTML( $table );
1170 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1172 if( $haveRevisions ) {
1173 # The page's stored (deleted) history:
1174 $wgOut->addHTML( '<ul>' );
1175 $remaining = $revisions->numRows();
1176 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1178 foreach ( $revisions as $row ) {
1180 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1183 $wgOut->addHTML( '</ul>' );
1185 $wgOut->addWikiMsg( 'nohistory' );
1189 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1190 $wgOut->addHTML( '<ul>' );
1191 foreach ( $files as $row ) {
1192 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1195 $wgOut->addHTML( '</ul>' );
1198 if ( $this->mAllowed
) {
1199 # Slip in the hidden controls here
1200 $misc = Html
::hidden( 'target', $this->mTarget
);
1201 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
1202 $misc .= Xml
::closeElement( 'form' );
1203 $wgOut->addHTML( $misc );
1209 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1212 $rev = Revision
::newFromArchiveRow( $row,
1213 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1215 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1216 // Build checkboxen...
1217 if( $this->mAllowed
) {
1218 if( $this->mInvert
) {
1219 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1220 $checkBox = Xml
::check( "ts$ts" );
1222 $checkBox = Xml
::check( "ts$ts", true );
1225 $checkBox = Xml
::check( "ts$ts" );
1230 // Build page & diff links...
1231 if( $this->mCanView
) {
1232 $titleObj = $this->getTitle();
1234 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1235 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1236 $last = wfMsgHtml( 'diff' );
1237 } else if( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1238 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1239 $last = $sk->linkKnown(
1241 wfMsgHtml( 'diff' ),
1244 'target' => $this->mTargetObj
->getPrefixedText(),
1250 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1251 $last = wfMsgHtml( 'diff' );
1254 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1255 $last = wfMsgHtml( 'diff' );
1258 $userLink = $sk->revUserTools( $rev );
1259 // Revision text size
1260 $size = $row->ar_len
;
1261 if( !is_null( $size ) ) {
1262 $stxt = $sk->formatRevisionSize( $size );
1265 $comment = $sk->revComment( $rev );
1266 // Revision delete links
1267 $revdlink = $this->revDeleteLink( $rev );
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>';
1347 * Fetch image view link if it's available to all users
1351 * @return String: HTML fragment
1353 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1354 global $wgLang, $wgUser;
1356 if( !$file->userCan( File
::DELETED_FILE
) ) {
1357 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1359 $link = $sk->linkKnown(
1361 $wgLang->timeanddate( $ts, true ),
1364 'target' => $this->mTargetObj
->getPrefixedText(),
1366 'token' => $wgUser->editToken( $key )
1369 if( $file->isDeleted( File
::DELETED_FILE
) ) {
1370 $link = '<span class="history-deleted">' . $link . '</span>';
1377 * Fetch file's user id if it's available to this user
1381 * @return String: HTML fragment
1383 function getFileUser( $file, $sk ) {
1384 if( !$file->userCan( File
::DELETED_USER
) ) {
1385 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1387 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1388 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1389 if( $file->isDeleted( File
::DELETED_USER
) ) {
1390 $link = '<span class="history-deleted">' . $link . '</span>';
1397 * Fetch file upload comment if it's available to this user
1401 * @return String: HTML fragment
1403 function getFileComment( $file, $sk ) {
1404 if( !$file->userCan( File
::DELETED_COMMENT
) ) {
1405 return '<span class="history-deleted"><span class="comment">' .
1406 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1408 $link = $sk->commentBlock( $file->getRawDescription() );
1409 if( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1410 $link = '<span class="history-deleted">' . $link . '</span>';
1416 function undelete() {
1417 global $wgOut, $wgUser;
1418 if ( wfReadOnly() ) {
1419 $wgOut->readOnlyPage();
1422 if( !is_null( $this->mTargetObj
) ) {
1423 $archive = new PageArchive( $this->mTargetObj
);
1424 $ok = $archive->undelete(
1425 $this->mTargetTimestamp
,
1427 $this->mFileVersions
,
1428 $this->mUnsuppress
);
1430 if( is_array( $ok ) ) {
1431 if ( $ok[1] ) { // Undeleted file count
1432 wfRunHooks( 'FileUndeleteComplete', array(
1433 $this->mTargetObj
, $this->mFileVersions
,
1434 $wgUser, $this->mComment
) );
1437 $skin = $wgUser->getSkin();
1438 $link = $skin->linkKnown( $this->mTargetObj
);
1439 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1441 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );
1442 $wgOut->addWikiMsg( 'undeleterevdel' );
1445 // Show file deletion warnings and errors
1446 $status = $archive->getFileStatus();
1447 if( $status && !$status->isGood() ) {
1448 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1451 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );