f582475534a0a87e07389fb1d6b6bd629339e03f
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">';
824 $wgOut->addHTML( $openDiv );
826 // Revision delete links
827 $canHide = $wgUser->isAllowed( 'deleterevision' );
828 if ( !$this->mDiff
&& ( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) ) {
829 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
830 $wgOut->addHTML( $skin->revDeleteLinkDisabled( $canHide ) ); // revision was hidden from sysops
834 'target' => $this->mTargetObj
->getPrefixedDBkey(),
835 'ids' => $rev->getTimestamp()
837 $wgOut->addHTML( $skin->revDeleteLink( $query,
838 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide ) );
842 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
843 $wgOut->addHTML( '</div>' );
844 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
846 if( $this->mPreview
) {
848 $popts = $wgOut->parserOptions();
849 $popts->setEditSection( false );
850 $wgOut->parserOptions( $popts );
851 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
855 Xml
::element( 'textarea', array(
856 'readonly' => 'readonly',
857 'cols' => intval( $wgUser->getOption( 'cols' ) ),
858 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
859 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
860 Xml
::openElement( 'div' ) .
861 Xml
::openElement( 'form', array(
863 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
864 Xml
::element( 'input', array(
867 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
868 Xml
::element( 'input', array(
870 'name' => 'timestamp',
871 'value' => $timestamp ) ) .
872 Xml
::element( 'input', array(
874 'name' => 'wpEditToken',
875 'value' => $wgUser->editToken() ) ) .
876 Xml
::element( 'input', array(
879 'value' => wfMsg( 'showpreview' ) ) ) .
880 Xml
::element( 'input', array(
883 'value' => wfMsg( 'showdiff' ) ) ) .
884 Xml
::closeElement( 'form' ) .
885 Xml
::closeElement( 'div' ) );
889 * Build a diff display between this and the previous either deleted
890 * or non-deleted edit.
892 * @param $previousRev Revision
893 * @param $currentRev Revision
894 * @return String: HTML
896 function showDiff( $previousRev, $currentRev ) {
899 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
900 $diffEngine->showDiffStyle();
903 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
904 "<col class='diff-marker' />" .
905 "<col class='diff-content' />" .
906 "<col class='diff-marker' />" .
907 "<col class='diff-content' />" .
909 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
910 $this->diffHeader( $previousRev, 'o' ) .
912 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
913 $this->diffHeader( $currentRev, 'n' ) .
916 $diffEngine->generateDiffBody(
917 $previousRev->getText(), $currentRev->getText() ) .
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
1306 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1309 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1311 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1312 return '<span class="history-deleted">' . $time . '</span>';
1314 $link = $sk->linkKnown(
1319 'target' => $this->mTargetObj
->getPrefixedText(),
1323 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1324 $link = '<span class="history-deleted">' . $link . '</span>';
1330 * Fetch image view link if it's available to all users
1332 * @return String: HTML fragment
1334 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1335 global $wgLang, $wgUser;
1337 if( !$file->userCan(File
::DELETED_FILE
) ) {
1338 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1340 $link = $sk->linkKnown(
1342 $wgLang->timeanddate( $ts, true ),
1345 'target' => $this->mTargetObj
->getPrefixedText(),
1347 'token' => $wgUser->editToken( $key )
1350 if( $file->isDeleted(File
::DELETED_FILE
) )
1351 $link = '<span class="history-deleted">' . $link . '</span>';
1357 * Fetch file's user id if it's available to this user
1359 * @return String: HTML fragment
1361 function getFileUser( $file, $sk ) {
1362 if( !$file->userCan(File
::DELETED_USER
) ) {
1363 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1365 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1366 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1367 if( $file->isDeleted(File
::DELETED_USER
) )
1368 $link = '<span class="history-deleted">' . $link . '</span>';
1374 * Fetch file upload comment if it's available to this user
1376 * @return String: HTML fragment
1378 function getFileComment( $file, $sk ) {
1379 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1380 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1382 $link = $sk->commentBlock( $file->getRawDescription() );
1383 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1384 $link = '<span class="history-deleted">' . $link . '</span>';
1389 function undelete() {
1390 global $wgOut, $wgUser;
1391 if ( wfReadOnly() ) {
1392 $wgOut->readOnlyPage();
1395 if( !is_null( $this->mTargetObj
) ) {
1396 $archive = new PageArchive( $this->mTargetObj
);
1397 $ok = $archive->undelete(
1398 $this->mTargetTimestamp
,
1400 $this->mFileVersions
,
1401 $this->mUnsuppress
);
1403 if( is_array($ok) ) {
1404 if ( $ok[1] ) // Undeleted file count
1405 wfRunHooks( 'FileUndeleteComplete', array(
1406 $this->mTargetObj
, $this->mFileVersions
,
1407 $wgUser, $this->mComment
) );
1409 $skin = $wgUser->getSkin();
1410 $link = $skin->linkKnown( $this->mTargetObj
);
1411 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1413 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1414 $wgOut->addWikiMsg( 'undeleterevdel' );
1417 // Show file deletion warnings and errors
1418 $status = $archive->getFileStatus();
1419 if( $status && !$status->isGood() ) {
1420 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1423 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );