aae57650584003f8d194e4f0c3b173bbfeda209a
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 * Return a Revision object containing data for the deleted revision.
163 * Note that the result *may* or *may not* have a null page ID.
165 * @param $timestamp String
168 function getRevision( $timestamp ) {
169 $dbr = wfGetDB( DB_SLAVE
);
170 $row = $dbr->selectRow( 'archive',
183 array( 'ar_namespace' => $this->title
->getNamespace(),
184 'ar_title' => $this->title
->getDBkey(),
185 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
188 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
195 * Return the most-previous revision, either live or deleted, against
196 * the deleted revision given by timestamp.
198 * May produce unexpected results in case of history merges or other
199 * unusual time issues.
201 * @param $timestamp String
202 * @return Revision or null
204 function getPreviousRevision( $timestamp ) {
205 $dbr = wfGetDB( DB_SLAVE
);
207 // Check the previous deleted revision...
208 $row = $dbr->selectRow( 'archive',
210 array( 'ar_namespace' => $this->title
->getNamespace(),
211 'ar_title' => $this->title
->getDBkey(),
213 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
216 'ORDER BY' => 'ar_timestamp DESC',
218 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
220 $row = $dbr->selectRow( array( 'page', 'revision' ),
221 array( 'rev_id', 'rev_timestamp' ),
223 'page_namespace' => $this->title
->getNamespace(),
224 'page_title' => $this->title
->getDBkey(),
225 'page_id = rev_page',
227 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
230 'ORDER BY' => 'rev_timestamp DESC',
232 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
233 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
235 if( $prevLive && $prevLive > $prevDeleted ) {
236 // Most prior revision was live
237 return Revision
::newFromId( $prevLiveId );
238 } elseif( $prevDeleted ) {
239 // Most prior revision was deleted
240 return $this->getRevision( $prevDeleted );
242 // No prior revision on this page.
248 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
250 * @param $row Object: database row
253 function getTextFromRow( $row ) {
254 if( is_null( $row->ar_text_id
) ) {
255 // An old row from MediaWiki 1.4 or previous.
256 // Text is embedded in this row in classic compression format.
257 return Revision
::getRevisionText( $row, "ar_" );
259 // New-style: keyed to the text storage backend.
260 $dbr = wfGetDB( DB_SLAVE
);
261 $text = $dbr->selectRow( 'text',
262 array( 'old_text', 'old_flags' ),
263 array( 'old_id' => $row->ar_text_id
),
265 return Revision
::getRevisionText( $text );
271 * Fetch (and decompress if necessary) the stored text of the most
272 * recently edited deleted revision of the page.
274 * If there are no archived revisions for the page, returns NULL.
278 function getLastRevisionText() {
279 $dbr = wfGetDB( DB_SLAVE
);
280 $row = $dbr->selectRow( 'archive',
281 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
282 array( 'ar_namespace' => $this->title
->getNamespace(),
283 'ar_title' => $this->title
->getDBkey() ),
285 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
287 return $this->getTextFromRow( $row );
294 * Quick check if any archived revisions are present for the page.
298 function isDeleted() {
299 $dbr = wfGetDB( DB_SLAVE
);
300 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
301 array( 'ar_namespace' => $this->title
->getNamespace(),
302 'ar_title' => $this->title
->getDBkey() ) );
307 * Restore the given (or all) text and file revisions for the page.
308 * Once restored, the items will be removed from the archive tables.
309 * The deletion log will be updated with an undeletion notice.
311 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
312 * @param $comment String
313 * @param $fileVersions Array
314 * @param $unsuppress Boolean
316 * @return array(number of file revisions restored, number of image revisions restored, log message)
317 * on success, false on failure
319 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
320 // If both the set of text revisions and file revisions are empty,
321 // restore everything. Otherwise, just restore the requested items.
322 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
324 $restoreText = $restoreAll ||
!empty( $timestamps );
325 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
327 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
328 $img = wfLocalFile( $this->title
);
329 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
330 if ( !$this->fileStatus
->isOk() ) {
333 $filesRestored = $this->fileStatus
->successCount
;
339 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
340 if( $textRestored === false ) { // It must be one of UNDELETE_*
349 $log = new LogPage( 'delete' );
351 if( $textRestored && $filesRestored ) {
352 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
353 $wgContLang->formatNum( $textRestored ),
354 $wgContLang->formatNum( $filesRestored ) );
355 } elseif( $textRestored ) {
356 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
357 $wgContLang->formatNum( $textRestored ) );
358 } elseif( $filesRestored ) {
359 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
360 $wgContLang->formatNum( $filesRestored ) );
362 wfDebug( "Undelete: nothing undeleted...\n" );
366 if( trim( $comment ) != '' )
367 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
368 $log->addEntry( 'restore', $this->title
, $reason );
370 return array($textRestored, $filesRestored, $reason);
374 * This is the meaty bit -- restores archived revisions of the given page
375 * to the cur/old tables. If the page currently exists, all revisions will
376 * be stuffed into old, otherwise the most recent will go into cur.
378 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
379 * @param $comment String
380 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
382 * @return Mixed: number of revisions restored or false on failure
384 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
387 $restoreAll = empty( $timestamps );
389 $dbw = wfGetDB( DB_MASTER
);
391 # Does this page already exist? We'll have to update it...
392 $article = new Article( $this->title
);
393 $options = 'FOR UPDATE'; // lock page
394 $page = $dbw->selectRow( 'page',
395 array( 'page_id', 'page_latest' ),
396 array( 'page_namespace' => $this->title
->getNamespace(),
397 'page_title' => $this->title
->getDBkey() ),
403 # Page already exists. Import the history, and if necessary
404 # we'll update the latest revision field in the record.
406 $pageId = $page->page_id
;
407 $previousRevId = $page->page_latest
;
408 # Get the time span of this page
409 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
410 array( 'rev_id' => $previousRevId ),
412 if( $previousTimestamp === false ) {
413 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
417 # Have to create a new article...
420 $previousTimestamp = 0;
424 $oldones = '1 = 1'; # All revisions...
426 $oldts = implode( ',',
427 array_map( array( &$dbw, 'addQuotes' ),
428 array_map( array( &$dbw, 'timestamp' ),
431 $oldones = "ar_timestamp IN ( {$oldts} )";
435 * Select each archived revision...
437 $result = $dbw->select( 'archive',
452 'ar_namespace' => $this->title
->getNamespace(),
453 'ar_title' => $this->title
->getDBkey(),
456 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
458 $ret = $dbw->resultObject( $result );
459 $rev_count = $dbw->numRows( $result );
461 wfDebug( __METHOD__
.": no revisions to restore\n" );
465 $ret->seek( $rev_count - 1 ); // move to last
466 $row = $ret->fetchObject(); // get newest archived rev
467 $ret->seek( 0 ); // move back
470 // Check the state of the newest to-be version...
471 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
472 return false; // we can't leave the current revision like this!
474 // Safe to insert now...
475 $newid = $article->insertOn( $dbw );
478 // Check if a deleted revision will become the current revision...
479 if( $row->ar_timestamp
> $previousTimestamp ) {
480 // Check the state of the newest to-be version...
481 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
482 return false; // we can't leave the current revision like this!
490 foreach ( $ret as $row ) {
491 // Check for key dupes due to shitty archive integrity.
492 if( $row->ar_rev_id
) {
493 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
494 if( $exists ) continue; // don't throw DB errors
496 // Insert one revision at a time...maintaining deletion status
497 // unless we are specifically removing all restrictions...
498 $revision = Revision
::newFromArchiveRow( $row,
501 'deleted' => $unsuppress ?
0 : $row->ar_deleted
504 $revision->insertOn( $dbw );
507 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
509 # Now that it's safely stored, take it out of the archive
510 $dbw->delete( 'archive',
512 'ar_namespace' => $this->title
->getNamespace(),
513 'ar_title' => $this->title
->getDBkey(),
517 // Was anything restored at all?
522 // Attach the latest revision to the page...
523 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
524 if( $newid ||
$wasnew ) {
525 // Update site stats, link tables, etc
526 $article->createUpdates( $revision );
530 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
531 Article
::onArticleCreate( $this->title
);
533 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
534 Article
::onArticleEdit( $this->title
);
537 if( $this->title
->getNamespace() == NS_FILE
) {
538 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
542 // Revision couldn't be created. This is very weird
543 wfDebug( "Undelete: unknown error...\n" );
553 function getFileStatus() { return $this->fileStatus
; }
557 * Special page allowing users with the appropriate permissions to view
558 * and restore deleted content.
560 * @ingroup SpecialPage
562 class SpecialUndelete
extends SpecialPage
{
563 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj, $mFile;
564 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
566 function __construct( $request = null ) {
567 parent
::__construct( 'Undelete', 'deletedhistory' );
569 if ( $request === null ) {
571 $this->mRequest
= $wgRequest;
573 $this->mRequest
= $request;
577 function loadRequest() {
579 $this->mAction
= $this->mRequest
->getVal( 'action' );
580 $this->mTarget
= $this->mRequest
->getVal( 'target' );
581 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
582 $time = $this->mRequest
->getVal( 'timestamp' );
583 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
584 $this->mFile
= $this->mRequest
->getVal( 'file' );
586 $posted = $this->mRequest
->wasPosted() &&
587 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
588 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
589 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
590 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
591 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
592 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
593 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
594 $this->mToken
= $this->mRequest
->getVal( 'token' );
596 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
597 $this->mAllowed
= true; // user can restore
598 $this->mCanView
= true; // user can view content
599 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
600 $this->mAllowed
= false; // user cannot restore
601 $this->mCanView
= true; // user can view content
602 } else { // user can only view the list of revisions
603 $this->mAllowed
= false;
604 $this->mCanView
= false;
605 $this->mTimestamp
= '';
606 $this->mRestore
= false;
609 if( $this->mRestore ||
$this->mInvert
) {
610 $timestamps = array();
611 $this->mFileVersions
= array();
612 foreach( $_REQUEST as $key => $val ) {
614 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
615 array_push( $timestamps, $matches[1] );
618 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
619 $this->mFileVersions
[] = intval( $matches[1] );
622 rsort( $timestamps );
623 $this->mTargetTimestamp
= $timestamps;
627 function execute( $par ) {
628 global $wgOut, $wgUser;
631 if ( !$this->userCanExecute( $wgUser ) ) {
632 $this->displayRestrictionError();
635 $this->outputHeader();
637 $this->loadRequest();
639 if ( $this->mAllowed
) {
640 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
642 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
646 $this->mTarget
= $par;
648 if ( $this->mTarget
!== '' ) {
649 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
650 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj
);
652 $this->mTargetObj
= null;
655 if( is_null( $this->mTargetObj
) ) {
656 # Not all users can just browse every deleted page from the list
657 if( $wgUser->isAllowed( 'browsearchive' ) ) {
658 $this->showSearchForm();
660 # List undeletable articles
661 if( $this->mSearchPrefix
) {
662 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
663 $this->showList( $result );
666 $wgOut->addWikiMsg( 'undelete-header' );
670 if( $this->mTimestamp
!== '' ) {
671 return $this->showRevision( $this->mTimestamp
);
673 if( $this->mFile
!== null ) {
674 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
675 // Check if user is allowed to see this file
676 if ( !$file->exists() ) {
677 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile
);
679 } else if( !$file->userCan( File
::DELETED_FILE
) ) {
680 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
681 $wgOut->permissionRequired( 'suppressrevision' );
683 $wgOut->permissionRequired( 'deletedtext' );
686 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
687 $this->showFileConfirmationForm( $this->mFile
);
690 return $this->showFile( $this->mFile
);
693 if( $this->mRestore
&& $this->mAction
== "submit" ) {
694 global $wgUploadMaintenance;
695 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
696 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
699 return $this->undelete();
701 if( $this->mInvert
&& $this->mAction
== "submit" ) {
702 return $this->showHistory( );
704 return $this->showHistory();
707 function showSearchForm() {
708 global $wgOut, $wgScript;
709 $wgOut->addWikiMsg( 'undelete-header' );
712 Xml
::openElement( 'form', array(
714 'action' => $wgScript ) ) .
715 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
716 Html
::hidden( 'title',
717 $this->getTitle()->getPrefixedDbKey() ) .
718 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
719 'prefix', 'prefix', 20,
720 $this->mSearchPrefix
) . ' ' .
721 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
722 Xml
::closeElement( 'fieldset' ) .
723 Xml
::closeElement( 'form' )
727 // Generic list of deleted pages
728 private function showList( $result ) {
729 global $wgLang, $wgUser, $wgOut;
731 if( $result->numRows() == 0 ) {
732 $wgOut->addWikiMsg( 'undelete-no-results' );
736 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
738 $sk = $wgUser->getSkin();
739 $undelete = $this->getTitle();
740 $wgOut->addHTML( "<ul>\n" );
741 foreach ( $result as $row ) {
742 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
743 $link = $sk->linkKnown(
745 htmlspecialchars( $title->getPrefixedText() ),
747 array( 'target' => $title->getPrefixedText() )
749 $revs = wfMsgExt( 'undeleterevisions',
750 array( 'parseinline' ),
751 $wgLang->formatNum( $row->count
) );
752 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
755 $wgOut->addHTML( "</ul>\n" );
760 private function showRevision( $timestamp ) {
761 global $wgLang, $wgUser, $wgOut;
763 $skin = $wgUser->getSkin();
765 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
767 $archive = new PageArchive( $this->mTargetObj
);
768 $rev = $archive->getRevision( $timestamp );
771 $wgOut->addWikiMsg( 'undeleterevision-missing' );
775 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
776 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
777 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
780 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
781 $wgOut->addHTML( '<br />' );
782 // and we are allowed to see...
786 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
788 $link = $skin->linkKnown(
789 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
790 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
794 $previousRev = $archive->getPreviousRevision( $timestamp );
796 $this->showDiff( $previousRev, $rev );
797 if( $wgUser->getOption( 'diffonly' ) ) {
800 $wgOut->addHTML( '<hr />' );
803 $wgOut->addWikiMsg( 'undelete-nodiff' );
807 // date and time are separate parameters to facilitate localisation.
808 // $time is kept for backward compat reasons.
809 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
810 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
811 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
812 $user = $skin->revUserTools( $rev );
814 if( $this->mPreview
) {
815 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
817 $openDiv = '<div id="mw-undelete-revision">';
819 $wgOut->addHTML( $openDiv );
821 // Revision delete links
822 $canHide = $wgUser->isAllowed( 'deleterevision' );
823 if ( !$this->mDiff
&& ( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) ) {
824 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
825 $wgOut->addHTML( $skin->revDeleteLinkDisabled( $canHide ) ); // revision was hidden from sysops
829 'target' => $this->mTargetObj
->getPrefixedDBkey(),
830 'ids' => $rev->getTimestamp()
832 $wgOut->addHTML( $skin->revDeleteLink( $query,
833 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide ) );
837 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
838 $wgOut->addHTML( '</div>' );
839 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
841 if( $this->mPreview
) {
843 $popts = $wgOut->parserOptions();
844 $popts->setEditSection( false );
845 $wgOut->parserOptions( $popts );
846 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
850 Xml
::element( 'textarea', array(
851 'readonly' => 'readonly',
852 'cols' => intval( $wgUser->getOption( 'cols' ) ),
853 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
854 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
855 Xml
::openElement( 'div' ) .
856 Xml
::openElement( 'form', array(
858 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
859 Xml
::element( 'input', array(
862 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
863 Xml
::element( 'input', array(
865 'name' => 'timestamp',
866 'value' => $timestamp ) ) .
867 Xml
::element( 'input', array(
869 'name' => 'wpEditToken',
870 'value' => $wgUser->editToken() ) ) .
871 Xml
::element( 'input', array(
874 'value' => wfMsg( 'showpreview' ) ) ) .
875 Xml
::element( 'input', array(
878 'value' => wfMsg( 'showdiff' ) ) ) .
879 Xml
::closeElement( 'form' ) .
880 Xml
::closeElement( 'div' ) );
884 * Build a diff display between this and the previous either deleted
885 * or non-deleted edit.
887 * @param $previousRev Revision
888 * @param $currentRev Revision
889 * @return String: HTML
891 function showDiff( $previousRev, $currentRev ) {
894 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
895 $diffEngine->showDiffStyle();
898 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
899 "<col class='diff-marker' />" .
900 "<col class='diff-content' />" .
901 "<col class='diff-marker' />" .
902 "<col class='diff-content' />" .
904 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
905 $this->diffHeader( $previousRev, 'o' ) .
907 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
908 $this->diffHeader( $currentRev, 'n' ) .
911 $diffEngine->generateDiffBody(
912 $previousRev->getText(), $currentRev->getText() ) .
919 * @param $rev Revision
923 private function diffHeader( $rev, $prefix ) {
924 global $wgUser, $wgLang;
925 $sk = $wgUser->getSkin();
926 $isDeleted = !( $rev->getId() && $rev->getTitle() );
928 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
929 $targetPage = $this->getTitle();
930 $targetQuery = array(
931 'target' => $this->mTargetObj
->getPrefixedText(),
932 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
935 /// @todo Fixme getId() may return non-zero for deleted revs...
936 $targetPage = $rev->getTitle();
937 $targetQuery = array( 'oldid' => $rev->getId() );
939 // Add show/hide deletion links if available
940 $canHide = $wgUser->isAllowed( 'deleterevision' );
941 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
943 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
944 $del .= $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
948 'target' => $this->mTargetObj
->getPrefixedDbkey(),
949 'ids' => $rev->getTimestamp()
951 $del .= $sk->revDeleteLink( $query,
952 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
958 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
963 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
964 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
965 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
971 '<div id="mw-diff-'.$prefix.'title2">' .
972 $sk->revUserTools( $rev ) . '<br />' .
974 '<div id="mw-diff-'.$prefix.'title3">' .
975 $sk->revComment( $rev ) . $del . '<br />' .
980 * Show a form confirming whether a tokenless user really wants to see a file
982 private function showFileConfirmationForm( $key ) {
983 global $wgOut, $wgUser, $wgLang;
984 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
985 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
986 $this->mTargetObj
->getText(),
987 $wgLang->date( $file->getTimestamp() ),
988 $wgLang->time( $file->getTimestamp() ) );
990 Xml
::openElement( 'form', array(
992 'action' => $this->getTitle()->getLocalUrl(
993 'target=' . urlencode( $this->mTarget
) .
994 '&file=' . urlencode( $key ) .
995 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
998 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1004 * Show a deleted file version requested by the visitor.
1006 private function showFile( $key ) {
1007 global $wgOut, $wgRequest;
1010 # We mustn't allow the output to be Squid cached, otherwise
1011 # if an admin previews a deleted image, and it's cached, then
1012 # a user without appropriate permissions can toddle off and
1013 # nab the image, and Squid will serve it
1014 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1015 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1016 $wgRequest->response()->header( 'Pragma: no-cache' );
1019 require_once( "$IP/includes/StreamFile.php" );
1020 $repo = RepoGroup
::singleton()->getLocalRepo();
1021 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1022 wfStreamFile( $path );
1025 private function showHistory( ) {
1026 global $wgUser, $wgOut;
1028 $sk = $wgUser->getSkin();
1029 if( $this->mAllowed
) {
1030 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
1032 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
1035 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n", array ( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() ) );
1037 $archive = new PageArchive( $this->mTargetObj
);
1039 $text = $archive->getLastRevisionText();
1040 if( is_null( $text ) ) {
1041 $wgOut->addWikiMsg( "nohistory" );
1045 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1046 if ( $this->mAllowed
) {
1047 $wgOut->addWikiMsg( "undeletehistory" );
1048 $wgOut->addWikiMsg( "undeleterevdel" );
1050 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
1052 $wgOut->addHTML( '</div>' );
1054 # List all stored revisions
1055 $revisions = $archive->listRevisions();
1056 $files = $archive->listFiles();
1058 $haveRevisions = $revisions && $revisions->numRows() > 0;
1059 $haveFiles = $files && $files->numRows() > 0;
1061 # Batch existence check on user and talk pages
1062 if( $haveRevisions ) {
1063 $batch = new LinkBatch();
1064 foreach ( $revisions as $row ) {
1065 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1066 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1069 $revisions->seek( 0 );
1072 $batch = new LinkBatch();
1073 foreach ( $files as $row ) {
1074 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1075 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1081 if ( $this->mAllowed
) {
1082 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1083 # Start the form here
1084 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1085 $wgOut->addHTML( $top );
1088 # Show relevant lines from the deletion log:
1089 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1090 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1091 # Show relevant lines from the suppression log:
1092 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1093 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1094 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1097 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1098 # Format the user-visible controls (comment field, submission button)
1099 # in a nice little table
1100 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1104 <td class='mw-input'>" .
1105 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1106 'mw-undelete-unsuppress', $this->mUnsuppress
).
1110 $unsuppressBox = "";
1113 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1114 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1116 <td colspan='2' class='mw-undelete-extrahelp'>" .
1117 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1121 <td class='mw-label'>" .
1122 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1124 <td class='mw-input'>" .
1125 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1130 <td class='mw-submit'>" .
1131 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1132 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1133 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1137 Xml
::closeElement( 'table' ) .
1138 Xml
::closeElement( 'fieldset' );
1140 $wgOut->addHTML( $table );
1143 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1145 if( $haveRevisions ) {
1146 # The page's stored (deleted) history:
1147 $wgOut->addHTML("<ul>");
1148 $remaining = $revisions->numRows();
1149 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1151 foreach ( $revisions as $row ) {
1153 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1156 $wgOut->addHTML("</ul>");
1158 $wgOut->addWikiMsg( "nohistory" );
1162 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1163 $wgOut->addHTML( "<ul>" );
1164 foreach ( $files as $row ) {
1165 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1168 $wgOut->addHTML( "</ul>" );
1171 if ( $this->mAllowed
) {
1172 # Slip in the hidden controls here
1173 $misc = Html
::hidden( 'target', $this->mTarget
);
1174 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
1175 $misc .= Xml
::closeElement( 'form' );
1176 $wgOut->addHTML( $misc );
1182 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1183 global $wgUser, $wgLang;
1185 $rev = Revision
::newFromArchiveRow( $row,
1186 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1188 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1189 // Build checkboxen...
1190 if( $this->mAllowed
) {
1191 if( $this->mInvert
) {
1192 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1193 $checkBox = Xml
::check( "ts$ts");
1195 $checkBox = Xml
::check( "ts$ts", true );
1198 $checkBox = Xml
::check( "ts$ts" );
1203 // Build page & diff links...
1204 if( $this->mCanView
) {
1205 $titleObj = $this->getTitle();
1207 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1208 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1209 $last = wfMsgHtml('diff');
1210 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1211 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1212 $last = $sk->linkKnown(
1217 'target' => $this->mTargetObj
->getPrefixedText(),
1223 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1224 $last = wfMsgHtml('diff');
1227 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1228 $last = wfMsgHtml('diff');
1231 $userLink = $sk->revUserTools( $rev );
1232 // Revision text size
1233 if( !is_null($size = $row->ar_len
) ) {
1234 $stxt = $sk->formatRevisionSize( $size );
1237 $comment = $sk->revComment( $rev );
1238 // Revision delete links
1239 $canHide = $wgUser->isAllowed( 'deleterevision' );
1240 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' )) ) {
1241 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1242 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1245 'type' => 'archive',
1246 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1249 $revdlink = $sk->revDeleteLink( $query,
1250 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
1255 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1258 private function formatFileRow( $row, $sk ) {
1259 global $wgUser, $wgLang;
1261 $file = ArchivedFile
::newFromRow( $row );
1263 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1264 if( $this->mAllowed
&& $row->fa_storage_key
) {
1265 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1266 $key = urlencode( $row->fa_storage_key
);
1267 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1270 $pageLink = $wgLang->timeanddate( $ts, true );
1272 $userLink = $this->getFileUser( $file, $sk );
1274 wfMsg( 'widthheight',
1275 $wgLang->formatNum( $row->fa_width
),
1276 $wgLang->formatNum( $row->fa_height
) ) .
1278 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1280 $data = htmlspecialchars( $data );
1281 $comment = $this->getFileComment( $file, $sk );
1282 // Add show/hide deletion links if available
1283 $canHide = $wgUser->isAllowed( 'deleterevision' );
1284 if( $canHide ||
( $file->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
1285 if( !$file->userCan( File
::DELETED_RESTRICTED
) ) {
1286 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1289 'type' => 'filearchive',
1290 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1291 'ids' => $row->fa_id
1293 $revdlink = $sk->revDeleteLink( $query,
1294 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1299 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1303 * Fetch revision text link if it's available to all users
1305 * @param $rev Revision
1309 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1312 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1314 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1315 return '<span class="history-deleted">' . $time . '</span>';
1317 $link = $sk->linkKnown(
1322 'target' => $this->mTargetObj
->getPrefixedText(),
1326 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1327 $link = '<span class="history-deleted">' . $link . '</span>';
1333 * Fetch image view link if it's available to all users
1337 * @return String: HTML fragment
1339 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1340 global $wgLang, $wgUser;
1342 if( !$file->userCan( File
::DELETED_FILE
) ) {
1343 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1345 $link = $sk->linkKnown(
1347 $wgLang->timeanddate( $ts, true ),
1350 'target' => $this->mTargetObj
->getPrefixedText(),
1352 'token' => $wgUser->editToken( $key )
1355 if( $file->isDeleted(File
::DELETED_FILE
) )
1356 $link = '<span class="history-deleted">' . $link . '</span>';
1362 * Fetch file's user id if it's available to this user
1366 * @return String: HTML fragment
1368 function getFileUser( $file, $sk ) {
1369 if( !$file->userCan( File
::DELETED_USER
) ) {
1370 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1372 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1373 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1374 if( $file->isDeleted( File
::DELETED_USER
) )
1375 $link = '<span class="history-deleted">' . $link . '</span>';
1381 * Fetch file upload comment if it's available to this user
1385 * @return String: HTML fragment
1387 function getFileComment( $file, $sk ) {
1388 if( !$file->userCan( File
::DELETED_COMMENT
) ) {
1389 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1391 $link = $sk->commentBlock( $file->getRawDescription() );
1392 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1393 $link = '<span class="history-deleted">' . $link . '</span>';
1398 function undelete() {
1399 global $wgOut, $wgUser;
1400 if ( wfReadOnly() ) {
1401 $wgOut->readOnlyPage();
1404 if( !is_null( $this->mTargetObj
) ) {
1405 $archive = new PageArchive( $this->mTargetObj
);
1406 $ok = $archive->undelete(
1407 $this->mTargetTimestamp
,
1409 $this->mFileVersions
,
1410 $this->mUnsuppress
);
1412 if( is_array($ok) ) {
1413 if ( $ok[1] ) // Undeleted file count
1414 wfRunHooks( 'FileUndeleteComplete', array(
1415 $this->mTargetObj
, $this->mFileVersions
,
1416 $wgUser, $this->mComment
) );
1418 $skin = $wgUser->getSkin();
1419 $link = $skin->linkKnown( $this->mTargetObj
);
1420 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1422 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1423 $wgOut->addWikiMsg( 'undeleterevdel' );
1426 // Show file deletion warnings and errors
1427 $status = $archive->getFileStatus();
1428 if( $status && !$status->isGood() ) {
1429 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1432 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );