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
33 function __construct( $title ) {
34 if( is_null( $title ) ) {
35 throw new MWException( __METHOD__
. ' given a null title.' );
37 $this->title
= $title;
41 * List all deleted pages recorded in the archive table. Returns result
42 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
45 * @return ResultWrapper
47 public static function listAllPages() {
48 $dbr = wfGetDB( DB_SLAVE
);
49 return self
::listPages( $dbr, '' );
53 * List deleted pages recorded in the archive table matching the
55 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
57 * @param $prefix String: title prefix
58 * @return ResultWrapper
60 public static function listPagesByPrefix( $prefix ) {
61 $dbr = wfGetDB( DB_SLAVE
);
63 $title = Title
::newFromText( $prefix );
65 $ns = $title->getNamespace();
66 $prefix = $title->getDBkey();
68 // Prolly won't work too good
69 // @todo handle bare namespace names cleanly?
73 'ar_namespace' => $ns,
74 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
76 return self
::listPages( $dbr, $conds );
79 protected static function listPages( $dbr, $condition ) {
80 return $dbr->resultObject(
91 'GROUP BY' => 'ar_namespace,ar_title',
92 'ORDER BY' => 'ar_namespace,ar_title',
100 * List the revisions of the given page. Returns result wrapper with
101 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
103 * @return ResultWrapper
105 function listRevisions() {
106 $dbr = wfGetDB( DB_SLAVE
);
107 $res = $dbr->select( 'archive',
108 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
109 array( 'ar_namespace' => $this->title
->getNamespace(),
110 'ar_title' => $this->title
->getDBkey() ),
111 'PageArchive::listRevisions',
112 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
113 $ret = $dbr->resultObject( $res );
118 * List the deleted file revisions for this page, if it's a file page.
119 * Returns a result wrapper with various filearchive fields, or null
120 * if not a file page.
122 * @return ResultWrapper
123 * @todo Does this belong in Image for fuller encapsulation?
125 function listFiles() {
126 if( $this->title
->getNamespace() == NS_FILE
) {
127 $dbr = wfGetDB( DB_SLAVE
);
128 $res = $dbr->select( 'filearchive',
148 array( 'fa_name' => $this->title
->getDBkey() ),
150 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
151 $ret = $dbr->resultObject( $res );
158 * Fetch (and decompress if necessary) the stored text for the deleted
159 * revision of the page with the given timestamp.
161 * @param $timestamp String
163 * @deprecated Use getRevision() for more flexible information
165 function getRevisionText( $timestamp ) {
166 $rev = $this->getRevision( $timestamp );
167 return $rev ?
$rev->getText() : null;
171 * Return a Revision object containing data for the deleted revision.
172 * Note that the result *may* or *may not* have a null page ID.
174 * @param $timestamp String
177 function getRevision( $timestamp ) {
178 $dbr = wfGetDB( DB_SLAVE
);
179 $row = $dbr->selectRow( 'archive',
192 array( 'ar_namespace' => $this->title
->getNamespace(),
193 'ar_title' => $this->title
->getDBkey(),
194 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
197 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
204 * Return the most-previous revision, either live or deleted, against
205 * the deleted revision given by timestamp.
207 * May produce unexpected results in case of history merges or other
208 * unusual time issues.
210 * @param $timestamp String
211 * @return Revision or null
213 function getPreviousRevision( $timestamp ) {
214 $dbr = wfGetDB( DB_SLAVE
);
216 // Check the previous deleted revision...
217 $row = $dbr->selectRow( 'archive',
219 array( 'ar_namespace' => $this->title
->getNamespace(),
220 'ar_title' => $this->title
->getDBkey(),
222 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
225 'ORDER BY' => 'ar_timestamp DESC',
227 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
229 $row = $dbr->selectRow( array( 'page', 'revision' ),
230 array( 'rev_id', 'rev_timestamp' ),
232 'page_namespace' => $this->title
->getNamespace(),
233 'page_title' => $this->title
->getDBkey(),
234 'page_id = rev_page',
236 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
239 'ORDER BY' => 'rev_timestamp DESC',
241 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
242 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
244 if( $prevLive && $prevLive > $prevDeleted ) {
245 // Most prior revision was live
246 return Revision
::newFromId( $prevLiveId );
247 } elseif( $prevDeleted ) {
248 // Most prior revision was deleted
249 return $this->getRevision( $prevDeleted );
251 // No prior revision on this page.
257 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
259 * @param $row Object: database row
262 function getTextFromRow( $row ) {
263 if( is_null( $row->ar_text_id
) ) {
264 // An old row from MediaWiki 1.4 or previous.
265 // Text is embedded in this row in classic compression format.
266 return Revision
::getRevisionText( $row, "ar_" );
268 // New-style: keyed to the text storage backend.
269 $dbr = wfGetDB( DB_SLAVE
);
270 $text = $dbr->selectRow( 'text',
271 array( 'old_text', 'old_flags' ),
272 array( 'old_id' => $row->ar_text_id
),
274 return Revision
::getRevisionText( $text );
280 * Fetch (and decompress if necessary) the stored text of the most
281 * recently edited deleted revision of the page.
283 * If there are no archived revisions for the page, returns NULL.
287 function getLastRevisionText() {
288 $dbr = wfGetDB( DB_SLAVE
);
289 $row = $dbr->selectRow( 'archive',
290 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
291 array( 'ar_namespace' => $this->title
->getNamespace(),
292 'ar_title' => $this->title
->getDBkey() ),
294 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
296 return $this->getTextFromRow( $row );
303 * Quick check if any archived revisions are present for the page.
307 function isDeleted() {
308 $dbr = wfGetDB( DB_SLAVE
);
309 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
310 array( 'ar_namespace' => $this->title
->getNamespace(),
311 'ar_title' => $this->title
->getDBkey() ) );
316 * Restore the given (or all) text and file revisions for the page.
317 * Once restored, the items will be removed from the archive tables.
318 * The deletion log will be updated with an undeletion notice.
320 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
321 * @param $comment String
322 * @param $fileVersions Array
323 * @param $unsuppress Boolean
325 * @return array(number of file revisions restored, number of image revisions restored, log message)
326 * on success, false on failure
328 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
329 // If both the set of text revisions and file revisions are empty,
330 // restore everything. Otherwise, just restore the requested items.
331 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
333 $restoreText = $restoreAll ||
!empty( $timestamps );
334 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
336 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
337 $img = wfLocalFile( $this->title
);
338 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
339 if ( !$this->fileStatus
->isOk() ) {
342 $filesRestored = $this->fileStatus
->successCount
;
348 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
349 if($textRestored === false) // It must be one of UNDELETE_*
357 $log = new LogPage( 'delete' );
359 if( $textRestored && $filesRestored ) {
360 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
361 $wgContLang->formatNum( $textRestored ),
362 $wgContLang->formatNum( $filesRestored ) );
363 } elseif( $textRestored ) {
364 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
365 $wgContLang->formatNum( $textRestored ) );
366 } elseif( $filesRestored ) {
367 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
368 $wgContLang->formatNum( $filesRestored ) );
370 wfDebug( "Undelete: nothing undeleted...\n" );
374 if( trim( $comment ) != '' )
375 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
376 $log->addEntry( 'restore', $this->title
, $reason );
378 return array($textRestored, $filesRestored, $reason);
382 * This is the meaty bit -- restores archived revisions of the given page
383 * to the cur/old tables. If the page currently exists, all revisions will
384 * be stuffed into old, otherwise the most recent will go into cur.
386 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
387 * @param $comment String
388 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
390 * @return Mixed: number of revisions restored or false on failure
392 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
395 $restoreAll = empty( $timestamps );
397 $dbw = wfGetDB( DB_MASTER
);
399 # Does this page already exist? We'll have to update it...
400 $article = new Article( $this->title
);
401 $options = 'FOR UPDATE'; // lock page
402 $page = $dbw->selectRow( 'page',
403 array( 'page_id', 'page_latest' ),
404 array( 'page_namespace' => $this->title
->getNamespace(),
405 'page_title' => $this->title
->getDBkey() ),
411 # Page already exists. Import the history, and if necessary
412 # we'll update the latest revision field in the record.
414 $pageId = $page->page_id
;
415 $previousRevId = $page->page_latest
;
416 # Get the time span of this page
417 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
418 array( 'rev_id' => $previousRevId ),
420 if( $previousTimestamp === false ) {
421 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
425 # Have to create a new article...
428 $previousTimestamp = 0;
432 $oldones = '1 = 1'; # All revisions...
434 $oldts = implode( ',',
435 array_map( array( &$dbw, 'addQuotes' ),
436 array_map( array( &$dbw, 'timestamp' ),
439 $oldones = "ar_timestamp IN ( {$oldts} )";
443 * Select each archived revision...
445 $result = $dbw->select( 'archive',
460 'ar_namespace' => $this->title
->getNamespace(),
461 'ar_title' => $this->title
->getDBkey(),
464 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
466 $ret = $dbw->resultObject( $result );
467 $rev_count = $dbw->numRows( $result );
469 wfDebug( __METHOD__
.": no revisions to restore\n" );
473 $ret->seek( $rev_count - 1 ); // move to last
474 $row = $ret->fetchObject(); // get newest archived rev
475 $ret->seek( 0 ); // move back
478 // Check the state of the newest to-be version...
479 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
480 return false; // we can't leave the current revision like this!
482 // Safe to insert now...
483 $newid = $article->insertOn( $dbw );
486 // Check if a deleted revision will become the current revision...
487 if( $row->ar_timestamp
> $previousTimestamp ) {
488 // Check the state of the newest to-be version...
489 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
490 return false; // we can't leave the current revision like this!
498 foreach ( $ret as $row ) {
499 // Check for key dupes due to shitty archive integrity.
500 if( $row->ar_rev_id
) {
501 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
502 if( $exists ) continue; // don't throw DB errors
504 // Insert one revision at a time...maintaining deletion status
505 // unless we are specifically removing all restrictions...
506 $revision = Revision
::newFromArchiveRow( $row,
509 'deleted' => $unsuppress ?
0 : $row->ar_deleted
512 $revision->insertOn( $dbw );
515 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
517 # Now that it's safely stored, take it out of the archive
518 $dbw->delete( 'archive',
520 'ar_namespace' => $this->title
->getNamespace(),
521 'ar_title' => $this->title
->getDBkey(),
525 // Was anything restored at all?
530 // Attach the latest revision to the page...
531 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
532 if( $newid ||
$wasnew ) {
533 // Update site stats, link tables, etc
534 $article->createUpdates( $revision );
538 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
539 Article
::onArticleCreate( $this->title
);
541 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
542 Article
::onArticleEdit( $this->title
);
545 if( $this->title
->getNamespace() == NS_FILE
) {
546 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
550 // Revision couldn't be created. This is very weird
551 wfDebug( "Undelete: unknown error...\n" );
558 function getFileStatus() { return $this->fileStatus
; }
562 * Special page allowing users with the appropriate permissions to view
563 * and restore deleted content.
565 * @ingroup SpecialPage
567 class SpecialUndelete
extends SpecialPage
{
568 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
569 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
571 function __construct( $request = null ) {
572 parent
::__construct( 'Undelete', 'deletedhistory' );
574 if ( $request === null ) {
576 $this->mRequest
= $wgRequest;
578 $this->mRequest
= $request;
582 function loadRequest() {
584 $this->mAction
= $this->mRequest
->getVal( 'action' );
585 $this->mTarget
= $this->mRequest
->getVal( 'target' );
586 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
587 $time = $this->mRequest
->getVal( 'timestamp' );
588 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
589 $this->mFile
= $this->mRequest
->getVal( 'file' );
591 $posted = $this->mRequest
->wasPosted() &&
592 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
593 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
594 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
595 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
596 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
597 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
598 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
599 $this->mToken
= $this->mRequest
->getVal( 'token' );
601 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
602 $this->mAllowed
= true; // user can restore
603 $this->mCanView
= true; // user can view content
604 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
605 $this->mAllowed
= false; // user cannot restore
606 $this->mCanView
= true; // user can view content
607 } else { // user can only view the list of revisions
608 $this->mAllowed
= false;
609 $this->mCanView
= false;
610 $this->mTimestamp
= '';
611 $this->mRestore
= false;
614 if( $this->mRestore ||
$this->mInvert
) {
615 $timestamps = array();
616 $this->mFileVersions
= array();
617 foreach( $_REQUEST as $key => $val ) {
619 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
620 array_push( $timestamps, $matches[1] );
623 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
624 $this->mFileVersions
[] = intval( $matches[1] );
627 rsort( $timestamps );
628 $this->mTargetTimestamp
= $timestamps;
632 function execute( $par ) {
633 global $wgOut, $wgUser;
636 if ( !$this->userCanExecute( $wgUser ) ) {
637 $this->displayRestrictionError();
640 $this->outputHeader();
642 $this->loadRequest();
644 if ( $this->mAllowed
) {
645 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
647 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
651 $this->mTarget
= $par;
653 if ( $this->mTarget
!== '' ) {
654 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
655 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj
);
657 $this->mTargetObj
= null;
660 if( is_null( $this->mTargetObj
) ) {
661 # Not all users can just browse every deleted page from the list
662 if( $wgUser->isAllowed( 'browsearchive' ) ) {
663 $this->showSearchForm();
665 # List undeletable articles
666 if( $this->mSearchPrefix
) {
667 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
668 $this->showList( $result );
671 $wgOut->addWikiMsg( 'undelete-header' );
675 if( $this->mTimestamp
!== '' ) {
676 return $this->showRevision( $this->mTimestamp
);
678 if( $this->mFile
!== null ) {
679 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
680 // Check if user is allowed to see this file
681 if ( !$file->exists() ) {
682 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile
);
684 } else if( !$file->userCan( File
::DELETED_FILE
) ) {
685 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
686 $wgOut->permissionRequired( 'suppressrevision' );
688 $wgOut->permissionRequired( 'deletedtext' );
691 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
692 $this->showFileConfirmationForm( $this->mFile
);
695 return $this->showFile( $this->mFile
);
698 if( $this->mRestore
&& $this->mAction
== "submit" ) {
699 global $wgUploadMaintenance;
700 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
701 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
704 return $this->undelete();
706 if( $this->mInvert
&& $this->mAction
== "submit" ) {
707 return $this->showHistory( );
709 return $this->showHistory();
712 function showSearchForm() {
713 global $wgOut, $wgScript;
714 $wgOut->addWikiMsg( 'undelete-header' );
717 Xml
::openElement( 'form', array(
719 'action' => $wgScript ) ) .
720 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
721 Html
::hidden( 'title',
722 $this->getTitle()->getPrefixedDbKey() ) .
723 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
724 'prefix', 'prefix', 20,
725 $this->mSearchPrefix
) . ' ' .
726 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
727 Xml
::closeElement( 'fieldset' ) .
728 Xml
::closeElement( 'form' )
732 // Generic list of deleted pages
733 private function showList( $result ) {
734 global $wgLang, $wgUser, $wgOut;
736 if( $result->numRows() == 0 ) {
737 $wgOut->addWikiMsg( 'undelete-no-results' );
741 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
743 $sk = $wgUser->getSkin();
744 $undelete = $this->getTitle();
745 $wgOut->addHTML( "<ul>\n" );
746 foreach ( $result as $row ) {
747 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
748 $link = $sk->linkKnown(
750 htmlspecialchars( $title->getPrefixedText() ),
752 array( 'target' => $title->getPrefixedText() )
754 $revs = wfMsgExt( 'undeleterevisions',
755 array( 'parseinline' ),
756 $wgLang->formatNum( $row->count
) );
757 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
760 $wgOut->addHTML( "</ul>\n" );
765 private function showRevision( $timestamp ) {
766 global $wgLang, $wgUser, $wgOut;
768 $skin = $wgUser->getSkin();
770 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
772 $archive = new PageArchive( $this->mTargetObj
);
773 $rev = $archive->getRevision( $timestamp );
776 $wgOut->addWikiMsg( 'undeleterevision-missing' );
780 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
781 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
782 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
785 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
786 $wgOut->addHTML( '<br />' );
787 // and we are allowed to see...
791 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
793 $link = $skin->linkKnown(
794 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
795 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
799 $previousRev = $archive->getPreviousRevision( $timestamp );
801 $this->showDiff( $previousRev, $rev );
802 if( $wgUser->getOption( 'diffonly' ) ) {
805 $wgOut->addHTML( '<hr />' );
808 $wgOut->addWikiMsg( 'undelete-nodiff' );
812 // date and time are separate parameters to facilitate localisation.
813 // $time is kept for backward compat reasons.
814 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
815 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
816 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
817 $user = $skin->revUserTools( $rev );
819 if( $this->mPreview
) {
820 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
822 $openDiv = '<div id="mw-undelete-revision">';
825 // Revision delete links
826 $canHide = $wgUser->isAllowed( 'deleterevision' );
828 $revdlink = ''; // diffs already have revision delete links
829 } else if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
830 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
831 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
835 'target' => $this->mTargetObj
->getPrefixedDBkey(),
836 'ids' => $rev->getTimestamp()
838 $revdlink = $skin->revDeleteLink( $query,
839 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
845 $wgOut->addHTML( $openDiv . $revdlink . wfMsgWikiHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</div>' );
846 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
848 if( $this->mPreview
) {
850 $popts = $wgOut->parserOptions();
851 $popts->setEditSection( false );
852 $wgOut->parserOptions( $popts );
853 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
857 Xml
::element( 'textarea', array(
858 'readonly' => 'readonly',
859 'cols' => intval( $wgUser->getOption( 'cols' ) ),
860 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
861 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
862 Xml
::openElement( 'div' ) .
863 Xml
::openElement( 'form', array(
865 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
866 Xml
::element( 'input', array(
869 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
870 Xml
::element( 'input', array(
872 'name' => 'timestamp',
873 'value' => $timestamp ) ) .
874 Xml
::element( 'input', array(
876 'name' => 'wpEditToken',
877 'value' => $wgUser->editToken() ) ) .
878 Xml
::element( 'input', array(
881 'value' => wfMsg( 'showpreview' ) ) ) .
882 Xml
::element( 'input', array(
885 'value' => wfMsg( 'showdiff' ) ) ) .
886 Xml
::closeElement( 'form' ) .
887 Xml
::closeElement( 'div' ) );
891 * Build a diff display between this and the previous either deleted
892 * or non-deleted edit.
894 * @param $previousRev Revision
895 * @param $currentRev Revision
896 * @return String: HTML
898 function showDiff( $previousRev, $currentRev ) {
901 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
902 $diffEngine->showDiffStyle();
905 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
906 "<col class='diff-marker' />" .
907 "<col class='diff-content' />" .
908 "<col class='diff-marker' />" .
909 "<col class='diff-content' />" .
911 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
912 $this->diffHeader( $previousRev, 'o' ) .
914 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
915 $this->diffHeader( $currentRev, 'n' ) .
918 $diffEngine->generateDiffBody(
919 $previousRev->getText(), $currentRev->getText() ) .
925 private function diffHeader( $rev, $prefix ) {
926 global $wgUser, $wgLang;
927 $sk = $wgUser->getSkin();
928 $isDeleted = !( $rev->getId() && $rev->getTitle() );
930 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
931 $targetPage = $this->getTitle();
932 $targetQuery = array(
933 'target' => $this->mTargetObj
->getPrefixedText(),
934 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
937 /// @todo Fixme getId() may return non-zero for deleted revs...
938 $targetPage = $rev->getTitle();
939 $targetQuery = array( 'oldid' => $rev->getId() );
941 // Add show/hide deletion links if available
942 $canHide = $wgUser->isAllowed( 'deleterevision' );
943 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
945 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
946 $del .= $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
950 'target' => $this->mTargetObj
->getPrefixedDbkey(),
951 'ids' => $rev->getTimestamp()
953 $del .= $sk->revDeleteLink( $query,
954 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
960 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
965 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
966 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
967 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
973 '<div id="mw-diff-'.$prefix.'title2">' .
974 $sk->revUserTools( $rev ) . '<br />' .
976 '<div id="mw-diff-'.$prefix.'title3">' .
977 $sk->revComment( $rev ) . $del . '<br />' .
982 * Show a form confirming whether a tokenless user really wants to see a file
984 private function showFileConfirmationForm( $key ) {
985 global $wgOut, $wgUser, $wgLang;
986 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
987 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
988 $this->mTargetObj
->getText(),
989 $wgLang->date( $file->getTimestamp() ),
990 $wgLang->time( $file->getTimestamp() ) );
992 Xml
::openElement( 'form', array(
994 'action' => $this->getTitle()->getLocalUrl(
995 'target=' . urlencode( $this->mTarget
) .
996 '&file=' . urlencode( $key ) .
997 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
1000 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1006 * Show a deleted file version requested by the visitor.
1008 private function showFile( $key ) {
1009 global $wgOut, $wgRequest;
1012 # We mustn't allow the output to be Squid cached, otherwise
1013 # if an admin previews a deleted image, and it's cached, then
1014 # a user without appropriate permissions can toddle off and
1015 # nab the image, and Squid will serve it
1016 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1017 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1018 $wgRequest->response()->header( 'Pragma: no-cache' );
1021 require_once( "$IP/includes/StreamFile.php" );
1022 $repo = RepoGroup
::singleton()->getLocalRepo();
1023 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1024 wfStreamFile( $path );
1027 private function showHistory( ) {
1028 global $wgUser, $wgOut;
1030 $sk = $wgUser->getSkin();
1031 if( $this->mAllowed
) {
1032 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
1034 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
1037 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n", array ( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() ) );
1039 $archive = new PageArchive( $this->mTargetObj
);
1041 $text = $archive->getLastRevisionText();
1042 if( is_null( $text ) ) {
1043 $wgOut->addWikiMsg( "nohistory" );
1047 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1048 if ( $this->mAllowed
) {
1049 $wgOut->addWikiMsg( "undeletehistory" );
1050 $wgOut->addWikiMsg( "undeleterevdel" );
1052 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
1054 $wgOut->addHTML( '</div>' );
1056 # List all stored revisions
1057 $revisions = $archive->listRevisions();
1058 $files = $archive->listFiles();
1060 $haveRevisions = $revisions && $revisions->numRows() > 0;
1061 $haveFiles = $files && $files->numRows() > 0;
1063 # Batch existence check on user and talk pages
1064 if( $haveRevisions ) {
1065 $batch = new LinkBatch();
1066 foreach ( $revisions as $row ) {
1067 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1068 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1071 $revisions->seek( 0 );
1074 $batch = new LinkBatch();
1075 foreach ( $files as $row ) {
1076 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1077 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1083 if ( $this->mAllowed
) {
1084 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1085 # Start the form here
1086 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1087 $wgOut->addHTML( $top );
1090 # Show relevant lines from the deletion log:
1091 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1092 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1093 # Show relevant lines from the suppression log:
1094 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1095 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1096 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1099 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1100 # Format the user-visible controls (comment field, submission button)
1101 # in a nice little table
1102 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1106 <td class='mw-input'>" .
1107 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1108 'mw-undelete-unsuppress', $this->mUnsuppress
).
1112 $unsuppressBox = "";
1115 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1116 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1118 <td colspan='2' class='mw-undelete-extrahelp'>" .
1119 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1123 <td class='mw-label'>" .
1124 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1126 <td class='mw-input'>" .
1127 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1132 <td class='mw-submit'>" .
1133 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1134 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1135 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1139 Xml
::closeElement( 'table' ) .
1140 Xml
::closeElement( 'fieldset' );
1142 $wgOut->addHTML( $table );
1145 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1147 if( $haveRevisions ) {
1148 # The page's stored (deleted) history:
1149 $wgOut->addHTML("<ul>");
1150 $remaining = $revisions->numRows();
1151 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1153 foreach ( $revisions as $row ) {
1155 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1158 $wgOut->addHTML("</ul>");
1160 $wgOut->addWikiMsg( "nohistory" );
1164 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1165 $wgOut->addHTML( "<ul>" );
1166 foreach ( $files as $row ) {
1167 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1170 $wgOut->addHTML( "</ul>" );
1173 if ( $this->mAllowed
) {
1174 # Slip in the hidden controls here
1175 $misc = Html
::hidden( 'target', $this->mTarget
);
1176 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
1177 $misc .= Xml
::closeElement( 'form' );
1178 $wgOut->addHTML( $misc );
1184 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1185 global $wgUser, $wgLang;
1187 $rev = Revision
::newFromArchiveRow( $row,
1188 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1190 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1191 // Build checkboxen...
1192 if( $this->mAllowed
) {
1193 if( $this->mInvert
) {
1194 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1195 $checkBox = Xml
::check( "ts$ts");
1197 $checkBox = Xml
::check( "ts$ts", true );
1200 $checkBox = Xml
::check( "ts$ts" );
1205 // Build page & diff links...
1206 if( $this->mCanView
) {
1207 $titleObj = $this->getTitle();
1209 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1210 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1211 $last = wfMsgHtml('diff');
1212 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1213 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1214 $last = $sk->linkKnown(
1219 'target' => $this->mTargetObj
->getPrefixedText(),
1225 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1226 $last = wfMsgHtml('diff');
1229 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1230 $last = wfMsgHtml('diff');
1233 $userLink = $sk->revUserTools( $rev );
1234 // Revision text size
1235 if( !is_null($size = $row->ar_len
) ) {
1236 $stxt = $sk->formatRevisionSize( $size );
1239 $comment = $sk->revComment( $rev );
1240 // Revision delete links
1241 $canHide = $wgUser->isAllowed( 'deleterevision' );
1242 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1243 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1244 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1247 'type' => 'archive',
1248 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1251 $revdlink = $sk->revDeleteLink( $query,
1252 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
1257 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1260 private function formatFileRow( $row, $sk ) {
1261 global $wgUser, $wgLang;
1263 $file = ArchivedFile
::newFromRow( $row );
1265 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1266 if( $this->mAllowed
&& $row->fa_storage_key
) {
1267 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1268 $key = urlencode( $row->fa_storage_key
);
1269 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1272 $pageLink = $wgLang->timeanddate( $ts, true );
1274 $userLink = $this->getFileUser( $file, $sk );
1276 wfMsg( 'widthheight',
1277 $wgLang->formatNum( $row->fa_width
),
1278 $wgLang->formatNum( $row->fa_height
) ) .
1280 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1282 $data = htmlspecialchars( $data );
1283 $comment = $this->getFileComment( $file, $sk );
1284 // Add show/hide deletion links if available
1285 $canHide = $wgUser->isAllowed( 'deleterevision' );
1286 if( $canHide ||
($file->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1287 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1288 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1291 'type' => 'filearchive',
1292 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1293 'ids' => $row->fa_id
1295 $revdlink = $sk->revDeleteLink( $query,
1296 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1301 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1305 * Fetch revision text link if it's available to all users
1308 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1311 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1313 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1314 return '<span class="history-deleted">' . $time . '</span>';
1316 $link = $sk->linkKnown(
1321 'target' => $this->mTargetObj
->getPrefixedText(),
1325 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1326 $link = '<span class="history-deleted">' . $link . '</span>';
1332 * Fetch image view link if it's available to all users
1334 * @return String: HTML fragment
1336 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1337 global $wgLang, $wgUser;
1339 if( !$file->userCan(File
::DELETED_FILE
) ) {
1340 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1342 $link = $sk->linkKnown(
1344 $wgLang->timeanddate( $ts, true ),
1347 'target' => $this->mTargetObj
->getPrefixedText(),
1349 'token' => $wgUser->editToken( $key )
1352 if( $file->isDeleted(File
::DELETED_FILE
) )
1353 $link = '<span class="history-deleted">' . $link . '</span>';
1359 * Fetch file's user id if it's available to this user
1361 * @return String: HTML fragment
1363 function getFileUser( $file, $sk ) {
1364 if( !$file->userCan(File
::DELETED_USER
) ) {
1365 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1367 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1368 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1369 if( $file->isDeleted(File
::DELETED_USER
) )
1370 $link = '<span class="history-deleted">' . $link . '</span>';
1376 * Fetch file upload comment if it's available to this user
1378 * @return String: HTML fragment
1380 function getFileComment( $file, $sk ) {
1381 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1382 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1384 $link = $sk->commentBlock( $file->getRawDescription() );
1385 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1386 $link = '<span class="history-deleted">' . $link . '</span>';
1391 function undelete() {
1392 global $wgOut, $wgUser;
1393 if ( wfReadOnly() ) {
1394 $wgOut->readOnlyPage();
1397 if( !is_null( $this->mTargetObj
) ) {
1398 $archive = new PageArchive( $this->mTargetObj
);
1399 $ok = $archive->undelete(
1400 $this->mTargetTimestamp
,
1402 $this->mFileVersions
,
1403 $this->mUnsuppress
);
1405 if( is_array($ok) ) {
1406 if ( $ok[1] ) // Undeleted file count
1407 wfRunHooks( 'FileUndeleteComplete', array(
1408 $this->mTargetObj
, $this->mFileVersions
,
1409 $wgUser, $this->mComment
) );
1411 $skin = $wgUser->getSkin();
1412 $link = $skin->linkKnown( $this->mTargetObj
);
1413 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1415 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1416 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1419 // Show file deletion warnings and errors
1420 $status = $archive->getFileStatus();
1421 if( $status && !$status->isGood() ) {
1422 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1425 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );