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 $filesRestored = $this->fileStatus
->successCount
;
345 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
346 if($textRestored === false) // It must be one of UNDELETE_*
354 $log = new LogPage( 'delete' );
356 if( $textRestored && $filesRestored ) {
357 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
358 $wgContLang->formatNum( $textRestored ),
359 $wgContLang->formatNum( $filesRestored ) );
360 } elseif( $textRestored ) {
361 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
362 $wgContLang->formatNum( $textRestored ) );
363 } elseif( $filesRestored ) {
364 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
365 $wgContLang->formatNum( $filesRestored ) );
367 wfDebug( "Undelete: nothing undeleted...\n" );
371 if( trim( $comment ) != '' )
372 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
373 $log->addEntry( 'restore', $this->title
, $reason );
375 return array($textRestored, $filesRestored, $reason);
379 * This is the meaty bit -- restores archived revisions of the given page
380 * to the cur/old tables. If the page currently exists, all revisions will
381 * be stuffed into old, otherwise the most recent will go into cur.
383 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
384 * @param $comment String
385 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
387 * @return Mixed: number of revisions restored or false on failure
389 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
392 $restoreAll = empty( $timestamps );
394 $dbw = wfGetDB( DB_MASTER
);
396 # Does this page already exist? We'll have to update it...
397 $article = new Article( $this->title
);
398 $options = 'FOR UPDATE'; // lock page
399 $page = $dbw->selectRow( 'page',
400 array( 'page_id', 'page_latest' ),
401 array( 'page_namespace' => $this->title
->getNamespace(),
402 'page_title' => $this->title
->getDBkey() ),
408 # Page already exists. Import the history, and if necessary
409 # we'll update the latest revision field in the record.
411 $pageId = $page->page_id
;
412 $previousRevId = $page->page_latest
;
413 # Get the time span of this page
414 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
415 array( 'rev_id' => $previousRevId ),
417 if( $previousTimestamp === false ) {
418 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
422 # Have to create a new article...
425 $previousTimestamp = 0;
429 $oldones = '1 = 1'; # All revisions...
431 $oldts = implode( ',',
432 array_map( array( &$dbw, 'addQuotes' ),
433 array_map( array( &$dbw, 'timestamp' ),
436 $oldones = "ar_timestamp IN ( {$oldts} )";
440 * Select each archived revision...
442 $result = $dbw->select( 'archive',
457 'ar_namespace' => $this->title
->getNamespace(),
458 'ar_title' => $this->title
->getDBkey(),
461 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
463 $ret = $dbw->resultObject( $result );
464 $rev_count = $dbw->numRows( $result );
466 wfDebug( __METHOD__
.": no revisions to restore\n" );
470 $ret->seek( $rev_count - 1 ); // move to last
471 $row = $ret->fetchObject(); // get newest archived rev
472 $ret->seek( 0 ); // move back
475 // Check the state of the newest to-be version...
476 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
477 return false; // we can't leave the current revision like this!
479 // Safe to insert now...
480 $newid = $article->insertOn( $dbw );
483 // Check if a deleted revision will become the current revision...
484 if( $row->ar_timestamp
> $previousTimestamp ) {
485 // Check the state of the newest to-be version...
486 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
487 return false; // we can't leave the current revision like this!
495 foreach ( $ret as $row ) {
496 // Check for key dupes due to shitty archive integrity.
497 if( $row->ar_rev_id
) {
498 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
499 if( $exists ) continue; // don't throw DB errors
501 // Insert one revision at a time...maintaining deletion status
502 // unless we are specifically removing all restrictions...
503 $revision = Revision
::newFromArchiveRow( $row,
506 'deleted' => $unsuppress ?
0 : $row->ar_deleted
509 $revision->insertOn( $dbw );
512 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
514 # Now that it's safely stored, take it out of the archive
515 $dbw->delete( 'archive',
517 'ar_namespace' => $this->title
->getNamespace(),
518 'ar_title' => $this->title
->getDBkey(),
522 // Was anything restored at all?
527 // Attach the latest revision to the page...
528 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
529 if( $newid ||
$wasnew ) {
530 // Update site stats, link tables, etc
531 $article->createUpdates( $revision );
535 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
536 Article
::onArticleCreate( $this->title
);
538 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
539 Article
::onArticleEdit( $this->title
);
542 if( $this->title
->getNamespace() == NS_FILE
) {
543 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
547 // Revision couldn't be created. This is very weird
548 wfDebug( "Undelete: unknown error...\n" );
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;
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)) return 0;
769 $archive = new PageArchive( $this->mTargetObj
);
770 $rev = $archive->getRevision( $timestamp );
773 $wgOut->addWikiMsg( 'undeleterevision-missing' );
777 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
778 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
779 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
782 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
783 $wgOut->addHTML( '<br />' );
784 // and we are allowed to see...
788 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
790 $link = $skin->linkKnown(
791 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
792 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
796 $previousRev = $archive->getPreviousRevision( $timestamp );
798 $this->showDiff( $previousRev, $rev );
799 if( $wgUser->getOption( 'diffonly' ) ) {
802 $wgOut->addHTML( '<hr />' );
805 $wgOut->addWikiMsg( 'undelete-nodiff' );
809 // date and time are separate parameters to facilitate localisation.
810 // $time is kept for backward compat reasons.
811 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
812 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
813 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
814 $user = $skin->revUserTools( $rev );
816 if( $this->mPreview
) {
817 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
819 $openDiv = '<div id="mw-undelete-revision">';
822 // Revision delete links
823 $canHide = $wgUser->isAllowed( 'deleterevision' );
825 $revdlink = ''; // diffs already have revision delete links
826 } else if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
827 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
828 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
832 'target' => $this->mTargetObj
->getPrefixedDBkey(),
833 'ids' => $rev->getTimestamp()
835 $revdlink = $skin->revDeleteLink( $query,
836 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
842 $wgOut->addHTML( $openDiv . $revdlink . wfMsgWikiHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</div>' );
843 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
845 if( $this->mPreview
) {
847 $popts = $wgOut->parserOptions();
848 $popts->setEditSection( false );
849 $wgOut->parserOptions( $popts );
850 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
854 Xml
::element( 'textarea', array(
855 'readonly' => 'readonly',
856 'cols' => intval( $wgUser->getOption( 'cols' ) ),
857 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
858 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
859 Xml
::openElement( 'div' ) .
860 Xml
::openElement( 'form', array(
862 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
863 Xml
::element( 'input', array(
866 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
867 Xml
::element( 'input', array(
869 'name' => 'timestamp',
870 'value' => $timestamp ) ) .
871 Xml
::element( 'input', array(
873 'name' => 'wpEditToken',
874 'value' => $wgUser->editToken() ) ) .
875 Xml
::element( 'input', array(
878 'value' => wfMsg( 'showpreview' ) ) ) .
879 Xml
::element( 'input', array(
882 'value' => wfMsg( 'showdiff' ) ) ) .
883 Xml
::closeElement( 'form' ) .
884 Xml
::closeElement( 'div' ) );
888 * Build a diff display between this and the previous either deleted
889 * or non-deleted edit.
891 * @param $previousRev Revision
892 * @param $currentRev Revision
893 * @return String: HTML
895 function showDiff( $previousRev, $currentRev ) {
898 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
899 $diffEngine->showDiffStyle();
902 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
903 "<col class='diff-marker' />" .
904 "<col class='diff-content' />" .
905 "<col class='diff-marker' />" .
906 "<col class='diff-content' />" .
908 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
909 $this->diffHeader( $previousRev, 'o' ) .
911 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
912 $this->diffHeader( $currentRev, 'n' ) .
915 $diffEngine->generateDiffBody(
916 $previousRev->getText(), $currentRev->getText() ) .
922 private function diffHeader( $rev, $prefix ) {
923 global $wgUser, $wgLang;
924 $sk = $wgUser->getSkin();
925 $isDeleted = !( $rev->getId() && $rev->getTitle() );
927 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
928 $targetPage = $this->getTitle();
929 $targetQuery = array(
930 'target' => $this->mTargetObj
->getPrefixedText(),
931 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
934 /// @todo Fixme getId() may return non-zero for deleted revs...
935 $targetPage = $rev->getTitle();
936 $targetQuery = array( 'oldid' => $rev->getId() );
938 // Add show/hide deletion links if available
939 $canHide = $wgUser->isAllowed( 'deleterevision' );
940 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
942 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
943 $del .= $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
947 'target' => $this->mTargetObj
->getPrefixedDbkey(),
948 'ids' => $rev->getTimestamp()
950 $del .= $sk->revDeleteLink( $query,
951 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
957 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
962 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
963 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
964 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
970 '<div id="mw-diff-'.$prefix.'title2">' .
971 $sk->revUserTools( $rev ) . '<br />' .
973 '<div id="mw-diff-'.$prefix.'title3">' .
974 $sk->revComment( $rev ) . $del . '<br />' .
979 * Show a form confirming whether a tokenless user really wants to see a file
981 private function showFileConfirmationForm( $key ) {
982 global $wgOut, $wgUser, $wgLang;
983 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
984 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
985 $this->mTargetObj
->getText(),
986 $wgLang->date( $file->getTimestamp() ),
987 $wgLang->time( $file->getTimestamp() ) );
989 Xml
::openElement( 'form', array(
991 'action' => $this->getTitle()->getLocalUrl(
992 'target=' . urlencode( $this->mTarget
) .
993 '&file=' . urlencode( $key ) .
994 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
997 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1003 * Show a deleted file version requested by the visitor.
1005 private function showFile( $key ) {
1006 global $wgOut, $wgRequest;
1009 # We mustn't allow the output to be Squid cached, otherwise
1010 # if an admin previews a deleted image, and it's cached, then
1011 # a user without appropriate permissions can toddle off and
1012 # nab the image, and Squid will serve it
1013 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1014 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1015 $wgRequest->response()->header( 'Pragma: no-cache' );
1018 require_once( "$IP/includes/StreamFile.php" );
1019 $repo = RepoGroup
::singleton()->getLocalRepo();
1020 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1021 wfStreamFile( $path );
1024 private function showHistory( ) {
1025 global $wgUser, $wgOut;
1027 $sk = $wgUser->getSkin();
1028 if( $this->mAllowed
) {
1029 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
1031 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
1034 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n", array ( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() ) );
1036 $archive = new PageArchive( $this->mTargetObj
);
1038 $text = $archive->getLastRevisionText();
1039 if( is_null( $text ) ) {
1040 $wgOut->addWikiMsg( "nohistory" );
1044 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1045 if ( $this->mAllowed
) {
1046 $wgOut->addWikiMsg( "undeletehistory" );
1047 $wgOut->addWikiMsg( "undeleterevdel" );
1049 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
1051 $wgOut->addHTML( '</div>' );
1053 # List all stored revisions
1054 $revisions = $archive->listRevisions();
1055 $files = $archive->listFiles();
1057 $haveRevisions = $revisions && $revisions->numRows() > 0;
1058 $haveFiles = $files && $files->numRows() > 0;
1060 # Batch existence check on user and talk pages
1061 if( $haveRevisions ) {
1062 $batch = new LinkBatch();
1063 foreach ( $revisions as $row ) {
1064 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1065 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1068 $revisions->seek( 0 );
1071 $batch = new LinkBatch();
1072 foreach ( $files as $row ) {
1073 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1074 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1080 if ( $this->mAllowed
) {
1081 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1082 # Start the form here
1083 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1084 $wgOut->addHTML( $top );
1087 # Show relevant lines from the deletion log:
1088 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1089 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1090 # Show relevant lines from the suppression log:
1091 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1092 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1093 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1096 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1097 # Format the user-visible controls (comment field, submission button)
1098 # in a nice little table
1099 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1103 <td class='mw-input'>" .
1104 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1105 'mw-undelete-unsuppress', $this->mUnsuppress
).
1109 $unsuppressBox = "";
1112 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1113 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1115 <td colspan='2' class='mw-undelete-extrahelp'>" .
1116 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1120 <td class='mw-label'>" .
1121 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1123 <td class='mw-input'>" .
1124 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1129 <td class='mw-submit'>" .
1130 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1131 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1132 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1136 Xml
::closeElement( 'table' ) .
1137 Xml
::closeElement( 'fieldset' );
1139 $wgOut->addHTML( $table );
1142 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1144 if( $haveRevisions ) {
1145 # The page's stored (deleted) history:
1146 $wgOut->addHTML("<ul>");
1147 $remaining = $revisions->numRows();
1148 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1150 foreach ( $revisions as $row ) {
1152 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1155 $wgOut->addHTML("</ul>");
1157 $wgOut->addWikiMsg( "nohistory" );
1161 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1162 $wgOut->addHTML( "<ul>" );
1163 foreach ( $files as $row ) {
1164 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1167 $wgOut->addHTML( "</ul>" );
1170 if ( $this->mAllowed
) {
1171 # Slip in the hidden controls here
1172 $misc = Html
::hidden( 'target', $this->mTarget
);
1173 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
1174 $misc .= Xml
::closeElement( 'form' );
1175 $wgOut->addHTML( $misc );
1181 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1182 global $wgUser, $wgLang;
1184 $rev = Revision
::newFromArchiveRow( $row,
1185 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1187 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1188 // Build checkboxen...
1189 if( $this->mAllowed
) {
1190 if( $this->mInvert
) {
1191 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1192 $checkBox = Xml
::check( "ts$ts");
1194 $checkBox = Xml
::check( "ts$ts", true );
1197 $checkBox = Xml
::check( "ts$ts" );
1202 // Build page & diff links...
1203 if( $this->mCanView
) {
1204 $titleObj = $this->getTitle();
1206 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1207 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1208 $last = wfMsgHtml('diff');
1209 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1210 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1211 $last = $sk->linkKnown(
1216 'target' => $this->mTargetObj
->getPrefixedText(),
1222 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1223 $last = wfMsgHtml('diff');
1226 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1227 $last = wfMsgHtml('diff');
1230 $userLink = $sk->revUserTools( $rev );
1231 // Revision text size
1232 if( !is_null($size = $row->ar_len
) ) {
1233 $stxt = $sk->formatRevisionSize( $size );
1236 $comment = $sk->revComment( $rev );
1237 // Revision delete links
1238 $canHide = $wgUser->isAllowed( 'deleterevision' );
1239 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1240 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1241 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1244 'type' => 'archive',
1245 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1248 $revdlink = $sk->revDeleteLink( $query,
1249 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
1254 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1257 private function formatFileRow( $row, $sk ) {
1258 global $wgUser, $wgLang;
1260 $file = ArchivedFile
::newFromRow( $row );
1262 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1263 if( $this->mAllowed
&& $row->fa_storage_key
) {
1264 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1265 $key = urlencode( $row->fa_storage_key
);
1266 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1269 $pageLink = $wgLang->timeanddate( $ts, true );
1271 $userLink = $this->getFileUser( $file, $sk );
1273 wfMsg( 'widthheight',
1274 $wgLang->formatNum( $row->fa_width
),
1275 $wgLang->formatNum( $row->fa_height
) ) .
1277 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1279 $data = htmlspecialchars( $data );
1280 $comment = $this->getFileComment( $file, $sk );
1281 // Add show/hide deletion links if available
1282 $canHide = $wgUser->isAllowed( 'deleterevision' );
1283 if( $canHide ||
($file->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1284 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1285 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1288 'type' => 'filearchive',
1289 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1290 'ids' => $row->fa_id
1292 $revdlink = $sk->revDeleteLink( $query,
1293 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1298 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1302 * Fetch revision text link if it's available to all users
1305 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1308 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1310 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1311 return '<span class="history-deleted">' . $time . '</span>';
1313 $link = $sk->linkKnown(
1318 'target' => $this->mTargetObj
->getPrefixedText(),
1322 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1323 $link = '<span class="history-deleted">' . $link . '</span>';
1329 * Fetch image view link if it's available to all users
1331 * @return String: HTML fragment
1333 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1334 global $wgLang, $wgUser;
1336 if( !$file->userCan(File
::DELETED_FILE
) ) {
1337 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1339 $link = $sk->linkKnown(
1341 $wgLang->timeanddate( $ts, true ),
1344 'target' => $this->mTargetObj
->getPrefixedText(),
1346 'token' => $wgUser->editToken( $key )
1349 if( $file->isDeleted(File
::DELETED_FILE
) )
1350 $link = '<span class="history-deleted">' . $link . '</span>';
1356 * Fetch file's user id if it's available to this user
1358 * @return String: HTML fragment
1360 function getFileUser( $file, $sk ) {
1361 if( !$file->userCan(File
::DELETED_USER
) ) {
1362 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1364 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1365 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1366 if( $file->isDeleted(File
::DELETED_USER
) )
1367 $link = '<span class="history-deleted">' . $link . '</span>';
1373 * Fetch file upload comment if it's available to this user
1375 * @return String: HTML fragment
1377 function getFileComment( $file, $sk ) {
1378 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1379 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1381 $link = $sk->commentBlock( $file->getRawDescription() );
1382 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1383 $link = '<span class="history-deleted">' . $link . '</span>';
1388 function undelete() {
1389 global $wgOut, $wgUser;
1390 if ( wfReadOnly() ) {
1391 $wgOut->readOnlyPage();
1394 if( !is_null( $this->mTargetObj
) ) {
1395 $archive = new PageArchive( $this->mTargetObj
);
1396 $ok = $archive->undelete(
1397 $this->mTargetTimestamp
,
1399 $this->mFileVersions
,
1400 $this->mUnsuppress
);
1402 if( is_array($ok) ) {
1403 if ( $ok[1] ) // Undeleted file count
1404 wfRunHooks( 'FileUndeleteComplete', array(
1405 $this->mTargetObj
, $this->mFileVersions
,
1406 $wgUser, $this->mComment
) );
1408 $skin = $wgUser->getSkin();
1409 $link = $skin->linkKnown( $this->mTargetObj
);
1410 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1412 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1413 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1416 // Show file deletion warnings and errors
1417 $status = $archive->getFileStatus();
1418 if( $status && !$status->isGood() ) {
1419 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1422 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );