3 * Implements Special:Undelete
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Used to show archived pages and eventually restore them.
27 * @ingroup SpecialPage
37 function __construct( $title ) {
38 if( is_null( $title ) ) {
39 throw new MWException( __METHOD__
. ' given a null title.' );
41 $this->title
= $title;
45 * List all deleted pages recorded in the archive table. Returns result
46 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
49 * @return ResultWrapper
51 public static function listAllPages() {
52 $dbr = wfGetDB( DB_SLAVE
);
53 return self
::listPages( $dbr, '' );
57 * List deleted pages recorded in the archive table matching the
59 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
61 * @param $prefix String: title prefix
62 * @return ResultWrapper
64 public static function listPagesByPrefix( $prefix ) {
65 $dbr = wfGetDB( DB_SLAVE
);
67 $title = Title
::newFromText( $prefix );
69 $ns = $title->getNamespace();
70 $prefix = $title->getDBkey();
72 // Prolly won't work too good
73 // @todo handle bare namespace names cleanly?
77 'ar_namespace' => $ns,
78 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
80 return self
::listPages( $dbr, $conds );
83 protected static function listPages( $dbr, $condition ) {
84 return $dbr->resultObject(
95 'GROUP BY' => 'ar_namespace,ar_title',
96 'ORDER BY' => 'ar_namespace,ar_title',
104 * List the revisions of the given page. Returns result wrapper with
105 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
107 * @return ResultWrapper
109 function listRevisions() {
110 $dbr = wfGetDB( DB_SLAVE
);
111 $res = $dbr->select( 'archive',
112 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
113 array( 'ar_namespace' => $this->title
->getNamespace(),
114 'ar_title' => $this->title
->getDBkey() ),
115 'PageArchive::listRevisions',
116 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
117 $ret = $dbr->resultObject( $res );
122 * List the deleted file revisions for this page, if it's a file page.
123 * Returns a result wrapper with various filearchive fields, or null
124 * if not a file page.
126 * @return ResultWrapper
127 * @todo Does this belong in Image for fuller encapsulation?
129 function listFiles() {
130 if( $this->title
->getNamespace() == NS_FILE
) {
131 $dbr = wfGetDB( DB_SLAVE
);
132 $res = $dbr->select( 'filearchive',
152 array( 'fa_name' => $this->title
->getDBkey() ),
154 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
155 $ret = $dbr->resultObject( $res );
162 * Fetch (and decompress if necessary) the stored text for the deleted
163 * revision of the page with the given timestamp.
165 * @param $timestamp String
167 * @deprecated Use getRevision() for more flexible information
169 function getRevisionText( $timestamp ) {
170 $rev = $this->getRevision( $timestamp );
171 return $rev ?
$rev->getText() : null;
175 * Return a Revision object containing data for the deleted revision.
176 * Note that the result *may* or *may not* have a null page ID.
178 * @param $timestamp String
181 function getRevision( $timestamp ) {
182 $dbr = wfGetDB( DB_SLAVE
);
183 $row = $dbr->selectRow( 'archive',
196 array( 'ar_namespace' => $this->title
->getNamespace(),
197 'ar_title' => $this->title
->getDBkey(),
198 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
201 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
208 * Return the most-previous revision, either live or deleted, against
209 * the deleted revision given by timestamp.
211 * May produce unexpected results in case of history merges or other
212 * unusual time issues.
214 * @param $timestamp String
215 * @return Revision or null
217 function getPreviousRevision( $timestamp ) {
218 $dbr = wfGetDB( DB_SLAVE
);
220 // Check the previous deleted revision...
221 $row = $dbr->selectRow( 'archive',
223 array( 'ar_namespace' => $this->title
->getNamespace(),
224 'ar_title' => $this->title
->getDBkey(),
226 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
229 'ORDER BY' => 'ar_timestamp DESC',
231 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
233 $row = $dbr->selectRow( array( 'page', 'revision' ),
234 array( 'rev_id', 'rev_timestamp' ),
236 'page_namespace' => $this->title
->getNamespace(),
237 'page_title' => $this->title
->getDBkey(),
238 'page_id = rev_page',
240 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
243 'ORDER BY' => 'rev_timestamp DESC',
245 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
246 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
248 if( $prevLive && $prevLive > $prevDeleted ) {
249 // Most prior revision was live
250 return Revision
::newFromId( $prevLiveId );
251 } elseif( $prevDeleted ) {
252 // Most prior revision was deleted
253 return $this->getRevision( $prevDeleted );
255 // No prior revision on this page.
261 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
263 * @param $row Object: database row
266 function getTextFromRow( $row ) {
267 if( is_null( $row->ar_text_id
) ) {
268 // An old row from MediaWiki 1.4 or previous.
269 // Text is embedded in this row in classic compression format.
270 return Revision
::getRevisionText( $row, "ar_" );
272 // New-style: keyed to the text storage backend.
273 $dbr = wfGetDB( DB_SLAVE
);
274 $text = $dbr->selectRow( 'text',
275 array( 'old_text', 'old_flags' ),
276 array( 'old_id' => $row->ar_text_id
),
278 return Revision
::getRevisionText( $text );
284 * Fetch (and decompress if necessary) the stored text of the most
285 * recently edited deleted revision of the page.
287 * If there are no archived revisions for the page, returns NULL.
291 function getLastRevisionText() {
292 $dbr = wfGetDB( DB_SLAVE
);
293 $row = $dbr->selectRow( 'archive',
294 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
295 array( 'ar_namespace' => $this->title
->getNamespace(),
296 'ar_title' => $this->title
->getDBkey() ),
298 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
300 return $this->getTextFromRow( $row );
307 * Quick check if any archived revisions are present for the page.
311 function isDeleted() {
312 $dbr = wfGetDB( DB_SLAVE
);
313 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
314 array( 'ar_namespace' => $this->title
->getNamespace(),
315 'ar_title' => $this->title
->getDBkey() ) );
320 * Restore the given (or all) text and file revisions for the page.
321 * Once restored, the items will be removed from the archive tables.
322 * The deletion log will be updated with an undeletion notice.
324 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
325 * @param $comment String
326 * @param $fileVersions Array
327 * @param $unsuppress Boolean
329 * @return array(number of file revisions restored, number of image revisions restored, log message)
330 * on success, false on failure
332 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
333 // If both the set of text revisions and file revisions are empty,
334 // restore everything. Otherwise, just restore the requested items.
335 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
337 $restoreText = $restoreAll ||
!empty( $timestamps );
338 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
340 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
341 $img = wfLocalFile( $this->title
);
342 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
343 if ( !$this->fileStatus
->isOk() ) {
346 $filesRestored = $this->fileStatus
->successCount
;
352 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
353 if($textRestored === false) // It must be one of UNDELETE_*
361 $log = new LogPage( 'delete' );
363 if( $textRestored && $filesRestored ) {
364 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
365 $wgContLang->formatNum( $textRestored ),
366 $wgContLang->formatNum( $filesRestored ) );
367 } elseif( $textRestored ) {
368 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
369 $wgContLang->formatNum( $textRestored ) );
370 } elseif( $filesRestored ) {
371 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
372 $wgContLang->formatNum( $filesRestored ) );
374 wfDebug( "Undelete: nothing undeleted...\n" );
378 if( trim( $comment ) != '' )
379 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
380 $log->addEntry( 'restore', $this->title
, $reason );
382 return array($textRestored, $filesRestored, $reason);
386 * This is the meaty bit -- restores archived revisions of the given page
387 * to the cur/old tables. If the page currently exists, all revisions will
388 * be stuffed into old, otherwise the most recent will go into cur.
390 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
391 * @param $comment String
392 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
394 * @return Mixed: number of revisions restored or false on failure
396 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
399 $restoreAll = empty( $timestamps );
401 $dbw = wfGetDB( DB_MASTER
);
403 # Does this page already exist? We'll have to update it...
404 $article = new Article( $this->title
);
405 $options = 'FOR UPDATE'; // lock page
406 $page = $dbw->selectRow( 'page',
407 array( 'page_id', 'page_latest' ),
408 array( 'page_namespace' => $this->title
->getNamespace(),
409 'page_title' => $this->title
->getDBkey() ),
415 # Page already exists. Import the history, and if necessary
416 # we'll update the latest revision field in the record.
418 $pageId = $page->page_id
;
419 $previousRevId = $page->page_latest
;
420 # Get the time span of this page
421 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
422 array( 'rev_id' => $previousRevId ),
424 if( $previousTimestamp === false ) {
425 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
429 # Have to create a new article...
432 $previousTimestamp = 0;
436 $oldones = '1 = 1'; # All revisions...
438 $oldts = implode( ',',
439 array_map( array( &$dbw, 'addQuotes' ),
440 array_map( array( &$dbw, 'timestamp' ),
443 $oldones = "ar_timestamp IN ( {$oldts} )";
447 * Select each archived revision...
449 $result = $dbw->select( 'archive',
464 'ar_namespace' => $this->title
->getNamespace(),
465 'ar_title' => $this->title
->getDBkey(),
468 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
470 $ret = $dbw->resultObject( $result );
471 $rev_count = $dbw->numRows( $result );
473 wfDebug( __METHOD__
.": no revisions to restore\n" );
477 $ret->seek( $rev_count - 1 ); // move to last
478 $row = $ret->fetchObject(); // get newest archived rev
479 $ret->seek( 0 ); // move back
482 // Check the state of the newest to-be version...
483 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
484 return false; // we can't leave the current revision like this!
486 // Safe to insert now...
487 $newid = $article->insertOn( $dbw );
490 // Check if a deleted revision will become the current revision...
491 if( $row->ar_timestamp
> $previousTimestamp ) {
492 // Check the state of the newest to-be version...
493 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
494 return false; // we can't leave the current revision like this!
502 foreach ( $ret as $row ) {
503 // Check for key dupes due to shitty archive integrity.
504 if( $row->ar_rev_id
) {
505 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
506 if( $exists ) continue; // don't throw DB errors
508 // Insert one revision at a time...maintaining deletion status
509 // unless we are specifically removing all restrictions...
510 $revision = Revision
::newFromArchiveRow( $row,
513 'deleted' => $unsuppress ?
0 : $row->ar_deleted
516 $revision->insertOn( $dbw );
519 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
521 # Now that it's safely stored, take it out of the archive
522 $dbw->delete( 'archive',
524 'ar_namespace' => $this->title
->getNamespace(),
525 'ar_title' => $this->title
->getDBkey(),
529 // Was anything restored at all?
534 // Attach the latest revision to the page...
535 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
536 if( $newid ||
$wasnew ) {
537 // Update site stats, link tables, etc
538 $article->createUpdates( $revision );
542 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
543 Article
::onArticleCreate( $this->title
);
545 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
546 Article
::onArticleEdit( $this->title
);
549 if( $this->title
->getNamespace() == NS_FILE
) {
550 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
554 // Revision couldn't be created. This is very weird
555 wfDebug( "Undelete: unknown error...\n" );
565 function getFileStatus() { return $this->fileStatus
; }
569 * Special page allowing users with the appropriate permissions to view
570 * and restore deleted content.
572 * @ingroup SpecialPage
574 class SpecialUndelete
extends SpecialPage
{
575 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
576 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
578 function __construct( $request = null ) {
579 parent
::__construct( 'Undelete', 'deletedhistory' );
581 if ( $request === null ) {
583 $this->mRequest
= $wgRequest;
585 $this->mRequest
= $request;
589 function loadRequest() {
591 $this->mAction
= $this->mRequest
->getVal( 'action' );
592 $this->mTarget
= $this->mRequest
->getVal( 'target' );
593 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
594 $time = $this->mRequest
->getVal( 'timestamp' );
595 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
596 $this->mFile
= $this->mRequest
->getVal( 'file' );
598 $posted = $this->mRequest
->wasPosted() &&
599 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
600 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
601 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
602 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
603 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
604 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
605 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
606 $this->mToken
= $this->mRequest
->getVal( 'token' );
608 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
609 $this->mAllowed
= true; // user can restore
610 $this->mCanView
= true; // user can view content
611 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
612 $this->mAllowed
= false; // user cannot restore
613 $this->mCanView
= true; // user can view content
614 } else { // user can only view the list of revisions
615 $this->mAllowed
= false;
616 $this->mCanView
= false;
617 $this->mTimestamp
= '';
618 $this->mRestore
= false;
621 if( $this->mRestore ||
$this->mInvert
) {
622 $timestamps = array();
623 $this->mFileVersions
= array();
624 foreach( $_REQUEST as $key => $val ) {
626 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
627 array_push( $timestamps, $matches[1] );
630 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
631 $this->mFileVersions
[] = intval( $matches[1] );
634 rsort( $timestamps );
635 $this->mTargetTimestamp
= $timestamps;
639 function execute( $par ) {
640 global $wgOut, $wgUser;
643 if ( !$this->userCanExecute( $wgUser ) ) {
644 $this->displayRestrictionError();
647 $this->outputHeader();
649 $this->loadRequest();
651 if ( $this->mAllowed
) {
652 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
654 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
658 $this->mTarget
= $par;
660 if ( $this->mTarget
!== '' ) {
661 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
662 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj
);
664 $this->mTargetObj
= null;
667 if( is_null( $this->mTargetObj
) ) {
668 # Not all users can just browse every deleted page from the list
669 if( $wgUser->isAllowed( 'browsearchive' ) ) {
670 $this->showSearchForm();
672 # List undeletable articles
673 if( $this->mSearchPrefix
) {
674 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
675 $this->showList( $result );
678 $wgOut->addWikiMsg( 'undelete-header' );
682 if( $this->mTimestamp
!== '' ) {
683 return $this->showRevision( $this->mTimestamp
);
685 if( $this->mFile
!== null ) {
686 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
687 // Check if user is allowed to see this file
688 if ( !$file->exists() ) {
689 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile
);
691 } else if( !$file->userCan( File
::DELETED_FILE
) ) {
692 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
693 $wgOut->permissionRequired( 'suppressrevision' );
695 $wgOut->permissionRequired( 'deletedtext' );
698 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
699 $this->showFileConfirmationForm( $this->mFile
);
702 return $this->showFile( $this->mFile
);
705 if( $this->mRestore
&& $this->mAction
== "submit" ) {
706 global $wgUploadMaintenance;
707 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
708 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
711 return $this->undelete();
713 if( $this->mInvert
&& $this->mAction
== "submit" ) {
714 return $this->showHistory( );
716 return $this->showHistory();
719 function showSearchForm() {
720 global $wgOut, $wgScript;
721 $wgOut->addWikiMsg( 'undelete-header' );
724 Xml
::openElement( 'form', array(
726 'action' => $wgScript ) ) .
727 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
728 Html
::hidden( 'title',
729 $this->getTitle()->getPrefixedDbKey() ) .
730 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
731 'prefix', 'prefix', 20,
732 $this->mSearchPrefix
) . ' ' .
733 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
734 Xml
::closeElement( 'fieldset' ) .
735 Xml
::closeElement( 'form' )
739 // Generic list of deleted pages
740 private function showList( $result ) {
741 global $wgLang, $wgUser, $wgOut;
743 if( $result->numRows() == 0 ) {
744 $wgOut->addWikiMsg( 'undelete-no-results' );
748 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
750 $sk = $wgUser->getSkin();
751 $undelete = $this->getTitle();
752 $wgOut->addHTML( "<ul>\n" );
753 foreach ( $result as $row ) {
754 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
755 $link = $sk->linkKnown(
757 htmlspecialchars( $title->getPrefixedText() ),
759 array( 'target' => $title->getPrefixedText() )
761 $revs = wfMsgExt( 'undeleterevisions',
762 array( 'parseinline' ),
763 $wgLang->formatNum( $row->count
) );
764 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
767 $wgOut->addHTML( "</ul>\n" );
772 private function showRevision( $timestamp ) {
773 global $wgLang, $wgUser, $wgOut;
775 $skin = $wgUser->getSkin();
777 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
779 $archive = new PageArchive( $this->mTargetObj
);
780 $rev = $archive->getRevision( $timestamp );
783 $wgOut->addWikiMsg( 'undeleterevision-missing' );
787 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
788 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
789 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
792 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
793 $wgOut->addHTML( '<br />' );
794 // and we are allowed to see...
798 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
800 $link = $skin->linkKnown(
801 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
802 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
806 $previousRev = $archive->getPreviousRevision( $timestamp );
808 $this->showDiff( $previousRev, $rev );
809 if( $wgUser->getOption( 'diffonly' ) ) {
812 $wgOut->addHTML( '<hr />' );
815 $wgOut->addWikiMsg( 'undelete-nodiff' );
819 // date and time are separate parameters to facilitate localisation.
820 // $time is kept for backward compat reasons.
821 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
822 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
823 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
824 $user = $skin->revUserTools( $rev );
826 if( $this->mPreview
) {
827 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
829 $openDiv = '<div id="mw-undelete-revision">';
831 $wgOut->addHTML( $openDiv );
833 // Revision delete links
834 $canHide = $wgUser->isAllowed( 'deleterevision' );
835 if ( !$this->mDiff
&& ( $canHide ||
( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) ) {
836 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
837 $wgOut->addHTML( $skin->revDeleteLinkDisabled( $canHide ) ); // revision was hidden from sysops
841 'target' => $this->mTargetObj
->getPrefixedDBkey(),
842 'ids' => $rev->getTimestamp()
844 $wgOut->addHTML( $skin->revDeleteLink( $query,
845 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide ) );
849 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
850 $wgOut->addHTML( '</div>' );
851 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
853 if( $this->mPreview
) {
855 $popts = $wgOut->parserOptions();
856 $popts->setEditSection( false );
857 $wgOut->parserOptions( $popts );
858 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
862 Xml
::element( 'textarea', array(
863 'readonly' => 'readonly',
864 'cols' => intval( $wgUser->getOption( 'cols' ) ),
865 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
866 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
867 Xml
::openElement( 'div' ) .
868 Xml
::openElement( 'form', array(
870 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
871 Xml
::element( 'input', array(
874 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
875 Xml
::element( 'input', array(
877 'name' => 'timestamp',
878 'value' => $timestamp ) ) .
879 Xml
::element( 'input', array(
881 'name' => 'wpEditToken',
882 'value' => $wgUser->editToken() ) ) .
883 Xml
::element( 'input', array(
886 'value' => wfMsg( 'showpreview' ) ) ) .
887 Xml
::element( 'input', array(
890 'value' => wfMsg( 'showdiff' ) ) ) .
891 Xml
::closeElement( 'form' ) .
892 Xml
::closeElement( 'div' ) );
896 * Build a diff display between this and the previous either deleted
897 * or non-deleted edit.
899 * @param $previousRev Revision
900 * @param $currentRev Revision
901 * @return String: HTML
903 function showDiff( $previousRev, $currentRev ) {
906 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
907 $diffEngine->showDiffStyle();
910 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
911 "<col class='diff-marker' />" .
912 "<col class='diff-content' />" .
913 "<col class='diff-marker' />" .
914 "<col class='diff-content' />" .
916 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
917 $this->diffHeader( $previousRev, 'o' ) .
919 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
920 $this->diffHeader( $currentRev, 'n' ) .
923 $diffEngine->generateDiffBody(
924 $previousRev->getText(), $currentRev->getText() ) .
931 * @param $rev Revision
935 private function diffHeader( $rev, $prefix ) {
936 global $wgUser, $wgLang;
937 $sk = $wgUser->getSkin();
938 $isDeleted = !( $rev->getId() && $rev->getTitle() );
940 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
941 $targetPage = $this->getTitle();
942 $targetQuery = array(
943 'target' => $this->mTargetObj
->getPrefixedText(),
944 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
947 /// @todo Fixme getId() may return non-zero for deleted revs...
948 $targetPage = $rev->getTitle();
949 $targetQuery = array( 'oldid' => $rev->getId() );
951 // Add show/hide deletion links if available
952 $canHide = $wgUser->isAllowed( 'deleterevision' );
953 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
955 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
956 $del .= $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
960 'target' => $this->mTargetObj
->getPrefixedDbkey(),
961 'ids' => $rev->getTimestamp()
963 $del .= $sk->revDeleteLink( $query,
964 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
970 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
975 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
976 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
977 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
983 '<div id="mw-diff-'.$prefix.'title2">' .
984 $sk->revUserTools( $rev ) . '<br />' .
986 '<div id="mw-diff-'.$prefix.'title3">' .
987 $sk->revComment( $rev ) . $del . '<br />' .
992 * Show a form confirming whether a tokenless user really wants to see a file
994 private function showFileConfirmationForm( $key ) {
995 global $wgOut, $wgUser, $wgLang;
996 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
997 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
998 $this->mTargetObj
->getText(),
999 $wgLang->date( $file->getTimestamp() ),
1000 $wgLang->time( $file->getTimestamp() ) );
1002 Xml
::openElement( 'form', array(
1004 'action' => $this->getTitle()->getLocalUrl(
1005 'target=' . urlencode( $this->mTarget
) .
1006 '&file=' . urlencode( $key ) .
1007 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
1010 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1016 * Show a deleted file version requested by the visitor.
1018 private function showFile( $key ) {
1019 global $wgOut, $wgRequest;
1022 # We mustn't allow the output to be Squid cached, otherwise
1023 # if an admin previews a deleted image, and it's cached, then
1024 # a user without appropriate permissions can toddle off and
1025 # nab the image, and Squid will serve it
1026 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1027 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1028 $wgRequest->response()->header( 'Pragma: no-cache' );
1031 require_once( "$IP/includes/StreamFile.php" );
1032 $repo = RepoGroup
::singleton()->getLocalRepo();
1033 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1034 wfStreamFile( $path );
1037 private function showHistory( ) {
1038 global $wgUser, $wgOut;
1040 $sk = $wgUser->getSkin();
1041 if( $this->mAllowed
) {
1042 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
1044 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
1047 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n", array ( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() ) );
1049 $archive = new PageArchive( $this->mTargetObj
);
1051 $text = $archive->getLastRevisionText();
1052 if( is_null( $text ) ) {
1053 $wgOut->addWikiMsg( "nohistory" );
1057 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1058 if ( $this->mAllowed
) {
1059 $wgOut->addWikiMsg( "undeletehistory" );
1060 $wgOut->addWikiMsg( "undeleterevdel" );
1062 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
1064 $wgOut->addHTML( '</div>' );
1066 # List all stored revisions
1067 $revisions = $archive->listRevisions();
1068 $files = $archive->listFiles();
1070 $haveRevisions = $revisions && $revisions->numRows() > 0;
1071 $haveFiles = $files && $files->numRows() > 0;
1073 # Batch existence check on user and talk pages
1074 if( $haveRevisions ) {
1075 $batch = new LinkBatch();
1076 foreach ( $revisions as $row ) {
1077 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1078 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1081 $revisions->seek( 0 );
1084 $batch = new LinkBatch();
1085 foreach ( $files as $row ) {
1086 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1087 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1093 if ( $this->mAllowed
) {
1094 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1095 # Start the form here
1096 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1097 $wgOut->addHTML( $top );
1100 # Show relevant lines from the deletion log:
1101 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1102 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1103 # Show relevant lines from the suppression log:
1104 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1105 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1106 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1109 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1110 # Format the user-visible controls (comment field, submission button)
1111 # in a nice little table
1112 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1116 <td class='mw-input'>" .
1117 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1118 'mw-undelete-unsuppress', $this->mUnsuppress
).
1122 $unsuppressBox = "";
1125 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1126 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1128 <td colspan='2' class='mw-undelete-extrahelp'>" .
1129 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1133 <td class='mw-label'>" .
1134 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1136 <td class='mw-input'>" .
1137 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1142 <td class='mw-submit'>" .
1143 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1144 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1145 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1149 Xml
::closeElement( 'table' ) .
1150 Xml
::closeElement( 'fieldset' );
1152 $wgOut->addHTML( $table );
1155 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1157 if( $haveRevisions ) {
1158 # The page's stored (deleted) history:
1159 $wgOut->addHTML("<ul>");
1160 $remaining = $revisions->numRows();
1161 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1163 foreach ( $revisions as $row ) {
1165 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1168 $wgOut->addHTML("</ul>");
1170 $wgOut->addWikiMsg( "nohistory" );
1174 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1175 $wgOut->addHTML( "<ul>" );
1176 foreach ( $files as $row ) {
1177 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1180 $wgOut->addHTML( "</ul>" );
1183 if ( $this->mAllowed
) {
1184 # Slip in the hidden controls here
1185 $misc = Html
::hidden( 'target', $this->mTarget
);
1186 $misc .= Html
::hidden( 'wpEditToken', $wgUser->editToken() );
1187 $misc .= Xml
::closeElement( 'form' );
1188 $wgOut->addHTML( $misc );
1194 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1195 global $wgUser, $wgLang;
1197 $rev = Revision
::newFromArchiveRow( $row,
1198 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1200 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1201 // Build checkboxen...
1202 if( $this->mAllowed
) {
1203 if( $this->mInvert
) {
1204 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1205 $checkBox = Xml
::check( "ts$ts");
1207 $checkBox = Xml
::check( "ts$ts", true );
1210 $checkBox = Xml
::check( "ts$ts" );
1215 // Build page & diff links...
1216 if( $this->mCanView
) {
1217 $titleObj = $this->getTitle();
1219 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1220 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1221 $last = wfMsgHtml('diff');
1222 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1223 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1224 $last = $sk->linkKnown(
1229 'target' => $this->mTargetObj
->getPrefixedText(),
1235 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1236 $last = wfMsgHtml('diff');
1239 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1240 $last = wfMsgHtml('diff');
1243 $userLink = $sk->revUserTools( $rev );
1244 // Revision text size
1245 if( !is_null($size = $row->ar_len
) ) {
1246 $stxt = $sk->formatRevisionSize( $size );
1249 $comment = $sk->revComment( $rev );
1250 // Revision delete links
1251 $canHide = $wgUser->isAllowed( 'deleterevision' );
1252 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1253 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1254 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1257 'type' => 'archive',
1258 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1261 $revdlink = $sk->revDeleteLink( $query,
1262 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
1267 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1270 private function formatFileRow( $row, $sk ) {
1271 global $wgUser, $wgLang;
1273 $file = ArchivedFile
::newFromRow( $row );
1275 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1276 if( $this->mAllowed
&& $row->fa_storage_key
) {
1277 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1278 $key = urlencode( $row->fa_storage_key
);
1279 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1282 $pageLink = $wgLang->timeanddate( $ts, true );
1284 $userLink = $this->getFileUser( $file, $sk );
1286 wfMsg( 'widthheight',
1287 $wgLang->formatNum( $row->fa_width
),
1288 $wgLang->formatNum( $row->fa_height
) ) .
1290 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1292 $data = htmlspecialchars( $data );
1293 $comment = $this->getFileComment( $file, $sk );
1294 // Add show/hide deletion links if available
1295 $canHide = $wgUser->isAllowed( 'deleterevision' );
1296 if( $canHide ||
($file->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1297 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1298 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1301 'type' => 'filearchive',
1302 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1303 'ids' => $row->fa_id
1305 $revdlink = $sk->revDeleteLink( $query,
1306 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1311 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1315 * Fetch revision text link if it's available to all users
1317 * @param $rev Revision
1321 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1324 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1326 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1327 return '<span class="history-deleted">' . $time . '</span>';
1329 $link = $sk->linkKnown(
1334 'target' => $this->mTargetObj
->getPrefixedText(),
1338 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1339 $link = '<span class="history-deleted">' . $link . '</span>';
1345 * Fetch image view link if it's available to all users
1349 * @return String: HTML fragment
1351 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1352 global $wgLang, $wgUser;
1354 if( !$file->userCan(File
::DELETED_FILE
) ) {
1355 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1357 $link = $sk->linkKnown(
1359 $wgLang->timeanddate( $ts, true ),
1362 'target' => $this->mTargetObj
->getPrefixedText(),
1364 'token' => $wgUser->editToken( $key )
1367 if( $file->isDeleted(File
::DELETED_FILE
) )
1368 $link = '<span class="history-deleted">' . $link . '</span>';
1374 * Fetch file's user id if it's available to this user
1378 * @return String: HTML fragment
1380 function getFileUser( $file, $sk ) {
1381 if( !$file->userCan(File
::DELETED_USER
) ) {
1382 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1384 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1385 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1386 if( $file->isDeleted(File
::DELETED_USER
) )
1387 $link = '<span class="history-deleted">' . $link . '</span>';
1393 * Fetch file upload comment if it's available to this user
1397 * @return String: HTML fragment
1399 function getFileComment( $file, $sk ) {
1400 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1401 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1403 $link = $sk->commentBlock( $file->getRawDescription() );
1404 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1405 $link = '<span class="history-deleted">' . $link . '</span>';
1410 function undelete() {
1411 global $wgOut, $wgUser;
1412 if ( wfReadOnly() ) {
1413 $wgOut->readOnlyPage();
1416 if( !is_null( $this->mTargetObj
) ) {
1417 $archive = new PageArchive( $this->mTargetObj
);
1418 $ok = $archive->undelete(
1419 $this->mTargetTimestamp
,
1421 $this->mFileVersions
,
1422 $this->mUnsuppress
);
1424 if( is_array($ok) ) {
1425 if ( $ok[1] ) // Undeleted file count
1426 wfRunHooks( 'FileUndeleteComplete', array(
1427 $this->mTargetObj
, $this->mFileVersions
,
1428 $wgUser, $this->mComment
) );
1430 $skin = $wgUser->getSkin();
1431 $link = $skin->linkKnown( $this->mTargetObj
);
1432 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1434 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1435 $wgOut->addWikiMsg( 'undeleterevdel' );
1438 // Show file deletion warnings and errors
1439 $status = $archive->getFileStatus();
1440 if( $status && !$status->isGood() ) {
1441 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1444 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );