4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 * Used to show archived pages and eventually restore them.
22 * @ingroup SpecialPage
28 function __construct( $title ) {
29 if( is_null( $title ) ) {
30 throw new MWException( __METHOD__
. ' given a null title.' );
32 $this->title
= $title;
36 * List all deleted pages recorded in the archive table. Returns result
37 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
40 * @return ResultWrapper
42 public static function listAllPages() {
43 $dbr = wfGetDB( DB_SLAVE
);
44 return self
::listPages( $dbr, '' );
48 * List deleted pages recorded in the archive table matching the
50 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
52 * @param $prefix String: title prefix
53 * @return ResultWrapper
55 public static function listPagesByPrefix( $prefix ) {
56 $dbr = wfGetDB( DB_SLAVE
);
58 $title = Title
::newFromText( $prefix );
60 $ns = $title->getNamespace();
61 $prefix = $title->getDBkey();
63 // Prolly won't work too good
64 // @todo handle bare namespace names cleanly?
68 'ar_namespace' => $ns,
69 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
71 return self
::listPages( $dbr, $conds );
74 protected static function listPages( $dbr, $condition ) {
75 return $dbr->resultObject(
86 'GROUP BY' => 'ar_namespace,ar_title',
87 'ORDER BY' => 'ar_namespace,ar_title',
95 * List the revisions of the given page. Returns result wrapper with
96 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
98 * @return ResultWrapper
100 function listRevisions() {
101 $dbr = wfGetDB( DB_SLAVE
);
102 $res = $dbr->select( 'archive',
103 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
104 array( 'ar_namespace' => $this->title
->getNamespace(),
105 'ar_title' => $this->title
->getDBkey() ),
106 'PageArchive::listRevisions',
107 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
108 $ret = $dbr->resultObject( $res );
113 * List the deleted file revisions for this page, if it's a file page.
114 * Returns a result wrapper with various filearchive fields, or null
115 * if not a file page.
117 * @return ResultWrapper
118 * @todo Does this belong in Image for fuller encapsulation?
120 function listFiles() {
121 if( $this->title
->getNamespace() == NS_FILE
) {
122 $dbr = wfGetDB( DB_SLAVE
);
123 $res = $dbr->select( 'filearchive',
143 array( 'fa_name' => $this->title
->getDBkey() ),
145 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
146 $ret = $dbr->resultObject( $res );
153 * Fetch (and decompress if necessary) the stored text for the deleted
154 * revision of the page with the given timestamp.
156 * @param $timestamp String
158 * @deprecated Use getRevision() for more flexible information
160 function getRevisionText( $timestamp ) {
161 $rev = $this->getRevision( $timestamp );
162 return $rev ?
$rev->getText() : null;
166 * Return a Revision object containing data for the deleted revision.
167 * Note that the result *may* or *may not* have a null page ID.
169 * @param $timestamp String
172 function getRevision( $timestamp ) {
173 $dbr = wfGetDB( DB_SLAVE
);
174 $row = $dbr->selectRow( 'archive',
187 array( 'ar_namespace' => $this->title
->getNamespace(),
188 'ar_title' => $this->title
->getDBkey(),
189 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
192 return Revision
::newFromArchiveRow( $row, array( 'page' => $this->title
->getArticleId() ) );
199 * Return the most-previous revision, either live or deleted, against
200 * the deleted revision given by timestamp.
202 * May produce unexpected results in case of history merges or other
203 * unusual time issues.
205 * @param $timestamp String
206 * @return Revision or null
208 function getPreviousRevision( $timestamp ) {
209 $dbr = wfGetDB( DB_SLAVE
);
211 // Check the previous deleted revision...
212 $row = $dbr->selectRow( 'archive',
214 array( 'ar_namespace' => $this->title
->getNamespace(),
215 'ar_title' => $this->title
->getDBkey(),
217 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
220 'ORDER BY' => 'ar_timestamp DESC',
222 $prevDeleted = $row ?
wfTimestamp( TS_MW
, $row->ar_timestamp
) : false;
224 $row = $dbr->selectRow( array( 'page', 'revision' ),
225 array( 'rev_id', 'rev_timestamp' ),
227 'page_namespace' => $this->title
->getNamespace(),
228 'page_title' => $this->title
->getDBkey(),
229 'page_id = rev_page',
231 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
234 'ORDER BY' => 'rev_timestamp DESC',
236 $prevLive = $row ?
wfTimestamp( TS_MW
, $row->rev_timestamp
) : false;
237 $prevLiveId = $row ?
intval( $row->rev_id
) : null;
239 if( $prevLive && $prevLive > $prevDeleted ) {
240 // Most prior revision was live
241 return Revision
::newFromId( $prevLiveId );
242 } elseif( $prevDeleted ) {
243 // Most prior revision was deleted
244 return $this->getRevision( $prevDeleted );
246 // No prior revision on this page.
252 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
254 * @param $row Object: database row
257 function getTextFromRow( $row ) {
258 if( is_null( $row->ar_text_id
) ) {
259 // An old row from MediaWiki 1.4 or previous.
260 // Text is embedded in this row in classic compression format.
261 return Revision
::getRevisionText( $row, "ar_" );
263 // New-style: keyed to the text storage backend.
264 $dbr = wfGetDB( DB_SLAVE
);
265 $text = $dbr->selectRow( 'text',
266 array( 'old_text', 'old_flags' ),
267 array( 'old_id' => $row->ar_text_id
),
269 return Revision
::getRevisionText( $text );
275 * Fetch (and decompress if necessary) the stored text of the most
276 * recently edited deleted revision of the page.
278 * If there are no archived revisions for the page, returns NULL.
282 function getLastRevisionText() {
283 $dbr = wfGetDB( DB_SLAVE
);
284 $row = $dbr->selectRow( 'archive',
285 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
286 array( 'ar_namespace' => $this->title
->getNamespace(),
287 'ar_title' => $this->title
->getDBkey() ),
289 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
291 return $this->getTextFromRow( $row );
298 * Quick check if any archived revisions are present for the page.
302 function isDeleted() {
303 $dbr = wfGetDB( DB_SLAVE
);
304 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
305 array( 'ar_namespace' => $this->title
->getNamespace(),
306 'ar_title' => $this->title
->getDBkey() ) );
311 * Restore the given (or all) text and file revisions for the page.
312 * Once restored, the items will be removed from the archive tables.
313 * The deletion log will be updated with an undeletion notice.
315 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
316 * @param $comment String
317 * @param $fileVersions Array
318 * @param $unsuppress Boolean
320 * @return array(number of file revisions restored, number of image revisions restored, log message)
321 * on success, false on failure
323 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
324 // If both the set of text revisions and file revisions are empty,
325 // restore everything. Otherwise, just restore the requested items.
326 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
328 $restoreText = $restoreAll ||
!empty( $timestamps );
329 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
331 if( $restoreFiles && $this->title
->getNamespace() == NS_FILE
) {
332 $img = wfLocalFile( $this->title
);
333 $this->fileStatus
= $img->restore( $fileVersions, $unsuppress );
334 $filesRestored = $this->fileStatus
->successCount
;
340 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
341 if($textRestored === false) // It must be one of UNDELETE_*
349 $log = new LogPage( 'delete' );
351 if( $textRestored && $filesRestored ) {
352 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
353 $wgContLang->formatNum( $textRestored ),
354 $wgContLang->formatNum( $filesRestored ) );
355 } elseif( $textRestored ) {
356 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
357 $wgContLang->formatNum( $textRestored ) );
358 } elseif( $filesRestored ) {
359 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
360 $wgContLang->formatNum( $filesRestored ) );
362 wfDebug( "Undelete: nothing undeleted...\n" );
366 if( trim( $comment ) != '' )
367 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
368 $log->addEntry( 'restore', $this->title
, $reason );
370 return array($textRestored, $filesRestored, $reason);
374 * This is the meaty bit -- restores archived revisions of the given page
375 * to the cur/old tables. If the page currently exists, all revisions will
376 * be stuffed into old, otherwise the most recent will go into cur.
378 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
379 * @param $comment String
380 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
382 * @return Mixed: number of revisions restored or false on failure
384 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
387 $restoreAll = empty( $timestamps );
389 $dbw = wfGetDB( DB_MASTER
);
391 # Does this page already exist? We'll have to update it...
392 $article = new Article( $this->title
);
393 $options = 'FOR UPDATE'; // lock page
394 $page = $dbw->selectRow( 'page',
395 array( 'page_id', 'page_latest' ),
396 array( 'page_namespace' => $this->title
->getNamespace(),
397 'page_title' => $this->title
->getDBkey() ),
403 # Page already exists. Import the history, and if necessary
404 # we'll update the latest revision field in the record.
406 $pageId = $page->page_id
;
407 $previousRevId = $page->page_latest
;
408 # Get the time span of this page
409 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
410 array( 'rev_id' => $previousRevId ),
412 if( $previousTimestamp === false ) {
413 wfDebug( __METHOD__
.": existing page refers to a page_latest that does not exist\n" );
417 # Have to create a new article...
420 $previousTimestamp = 0;
424 $oldones = '1 = 1'; # All revisions...
426 $oldts = implode( ',',
427 array_map( array( &$dbw, 'addQuotes' ),
428 array_map( array( &$dbw, 'timestamp' ),
431 $oldones = "ar_timestamp IN ( {$oldts} )";
435 * Select each archived revision...
437 $result = $dbw->select( 'archive',
452 'ar_namespace' => $this->title
->getNamespace(),
453 'ar_title' => $this->title
->getDBkey(),
456 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
458 $ret = $dbw->resultObject( $result );
459 $rev_count = $dbw->numRows( $result );
461 wfDebug( __METHOD__
.": no revisions to restore\n" );
465 $ret->seek( $rev_count - 1 ); // move to last
466 $row = $ret->fetchObject(); // get newest archived rev
467 $ret->seek( 0 ); // move back
470 // Check the state of the newest to-be version...
471 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
472 return false; // we can't leave the current revision like this!
474 // Safe to insert now...
475 $newid = $article->insertOn( $dbw );
478 // Check if a deleted revision will become the current revision...
479 if( $row->ar_timestamp
> $previousTimestamp ) {
480 // Check the state of the newest to-be version...
481 if( !$unsuppress && ($row->ar_deleted
& Revision
::DELETED_TEXT
) ) {
482 return false; // we can't leave the current revision like this!
490 while( $row = $ret->fetchObject() ) {
491 // Check for key dupes due to shitty archive integrity.
492 if( $row->ar_rev_id
) {
493 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id
), __METHOD__
);
494 if( $exists ) continue; // don't throw DB errors
496 // Insert one revision at a time...maintaining deletion status
497 // unless we are specifically removing all restrictions...
498 $revision = Revision
::newFromArchiveRow( $row,
501 'deleted' => $unsuppress ?
0 : $row->ar_deleted
504 $revision->insertOn( $dbw );
507 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title
, $revision, $row->ar_page_id
) );
509 # Now that it's safely stored, take it out of the archive
510 $dbw->delete( 'archive',
512 'ar_namespace' => $this->title
->getNamespace(),
513 'ar_title' => $this->title
->getDBkey(),
517 // Was anything restored at all?
522 // Attach the latest revision to the page...
523 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
524 if( $newid ||
$wasnew ) {
525 // Update site stats, link tables, etc
526 $article->createUpdates( $revision );
530 wfRunHooks( 'ArticleUndelete', array( &$this->title
, true, $comment ) );
531 Article
::onArticleCreate( $this->title
);
533 wfRunHooks( 'ArticleUndelete', array( &$this->title
, false, $comment ) );
534 Article
::onArticleEdit( $this->title
);
537 if( $this->title
->getNamespace() == NS_FILE
) {
538 $update = new HTMLCacheUpdate( $this->title
, 'imagelinks' );
542 // Revision couldn't be created. This is very weird
543 wfDebug( "Undelete: unknown error...\n" );
550 function getFileStatus() { return $this->fileStatus
; }
554 * Special page allowing users with the appropriate permissions to view
555 * and restore deleted content.
557 * @ingroup SpecialPage
559 class UndeleteForm
extends SpecialPage
{
560 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
561 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
563 function __construct( $request = null ) {
564 parent
::__construct( 'Undelete', 'deletedhistory' );
566 if ( $request === null ) {
568 $this->mRequest
= $wgRequest;
570 $this->mRequest
= $request;
574 function loadRequest() {
576 $this->mAction
= $this->mRequest
->getVal( 'action' );
577 $this->mTarget
= $this->mRequest
->getVal( 'target' );
578 $this->mSearchPrefix
= $this->mRequest
->getText( 'prefix' );
579 $time = $this->mRequest
->getVal( 'timestamp' );
580 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
581 $this->mFile
= $this->mRequest
->getVal( 'file' );
583 $posted = $this->mRequest
->wasPosted() &&
584 $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ) );
585 $this->mRestore
= $this->mRequest
->getCheck( 'restore' ) && $posted;
586 $this->mInvert
= $this->mRequest
->getCheck( 'invert' ) && $posted;
587 $this->mPreview
= $this->mRequest
->getCheck( 'preview' ) && $posted;
588 $this->mDiff
= $this->mRequest
->getCheck( 'diff' );
589 $this->mComment
= $this->mRequest
->getText( 'wpComment' );
590 $this->mUnsuppress
= $this->mRequest
->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
591 $this->mToken
= $this->mRequest
->getVal( 'token' );
593 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
594 $this->mAllowed
= true; // user can restore
595 $this->mCanView
= true; // user can view content
596 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
597 $this->mAllowed
= false; // user cannot restore
598 $this->mCanView
= true; // user can view content
599 } else { // user can only view the list of revisions
600 $this->mAllowed
= false;
601 $this->mCanView
= false;
602 $this->mTimestamp
= '';
603 $this->mRestore
= false;
606 if( $this->mRestore ||
$this->mInvert
) {
607 $timestamps = array();
608 $this->mFileVersions
= array();
609 foreach( $_REQUEST as $key => $val ) {
611 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
612 array_push( $timestamps, $matches[1] );
615 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
616 $this->mFileVersions
[] = intval( $matches[1] );
619 rsort( $timestamps );
620 $this->mTargetTimestamp
= $timestamps;
624 function execute( $par ) {
625 global $wgOut, $wgUser;
628 if ( !$this->userCanExecute( $wgUser ) ) {
629 $this->displayRestrictionError();
632 $this->outputHeader();
634 $this->loadRequest();
636 if ( $this->mAllowed
) {
637 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
639 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
643 $this->mTarget
= $par;
645 if ( $this->mTarget
!== '' ) {
646 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
648 $this->mTargetObj
= null;
651 if( is_null( $this->mTargetObj
) ) {
652 # Not all users can just browse every deleted page from the list
653 if( $wgUser->isAllowed( 'browsearchive' ) ) {
654 $this->showSearchForm();
656 # List undeletable articles
657 if( $this->mSearchPrefix
) {
658 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
659 $this->showList( $result );
662 $wgOut->addWikiMsg( 'undelete-header' );
666 if( $this->mTimestamp
!== '' ) {
667 return $this->showRevision( $this->mTimestamp
);
669 if( $this->mFile
!== null ) {
670 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
671 // Check if user is allowed to see this file
672 if ( !$file->exists() ) {
673 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFile
);
675 } else if( !$file->userCan( File
::DELETED_FILE
) ) {
676 if( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
677 $wgOut->permissionRequired( 'suppressrevision' );
679 $wgOut->permissionRequired( 'deletedtext' );
682 } elseif ( !$wgUser->matchEditToken( $this->mToken
, $this->mFile
) ) {
683 $this->showFileConfirmationForm( $this->mFile
);
686 return $this->showFile( $this->mFile
);
689 if( $this->mRestore
&& $this->mAction
== "submit" ) {
690 global $wgUploadMaintenance;
691 if( $wgUploadMaintenance && $this->mTargetObj
&& $this->mTargetObj
->getNamespace() == NS_FILE
) {
692 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
695 return $this->undelete();
697 if( $this->mInvert
&& $this->mAction
== "submit" ) {
698 return $this->showHistory( );
700 return $this->showHistory();
703 function showSearchForm() {
704 global $wgOut, $wgScript;
705 $wgOut->addWikiMsg( 'undelete-header' );
708 Xml
::openElement( 'form', array(
710 'action' => $wgScript ) ) .
711 Xml
::fieldset( wfMsg( 'undelete-search-box' ) ) .
712 Xml
::hidden( 'title',
713 $this->getTitle()->getPrefixedDbKey() ) .
714 Xml
::inputLabel( wfMsg( 'undelete-search-prefix' ),
715 'prefix', 'prefix', 20,
716 $this->mSearchPrefix
) . ' ' .
717 Xml
::submitButton( wfMsg( 'undelete-search-submit' ) ) .
718 Xml
::closeElement( 'fieldset' ) .
719 Xml
::closeElement( 'form' )
723 // Generic list of deleted pages
724 private function showList( $result ) {
725 global $wgLang, $wgUser, $wgOut;
727 if( $result->numRows() == 0 ) {
728 $wgOut->addWikiMsg( 'undelete-no-results' );
732 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
734 $sk = $wgUser->getSkin();
735 $undelete = $this->getTitle();
736 $wgOut->addHTML( "<ul>\n" );
737 while( $row = $result->fetchObject() ) {
738 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
739 $link = $sk->linkKnown(
741 htmlspecialchars( $title->getPrefixedText() ),
743 array( 'target' => $title->getPrefixedText() )
745 $revs = wfMsgExt( 'undeleterevisions',
746 array( 'parseinline' ),
747 $wgLang->formatNum( $row->count
) );
748 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
751 $wgOut->addHTML( "</ul>\n" );
756 private function showRevision( $timestamp ) {
757 global $wgLang, $wgUser, $wgOut;
759 $skin = $wgUser->getSkin();
761 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
763 $archive = new PageArchive( $this->mTargetObj
);
764 $rev = $archive->getRevision( $timestamp );
767 $wgOut->addWikiMsg( 'undeleterevision-missing' );
771 if( $rev->isDeleted(Revision
::DELETED_TEXT
) ) {
772 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
773 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
776 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
777 $wgOut->addHTML( '<br />' );
778 // and we are allowed to see...
782 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
784 $link = $skin->linkKnown(
785 $this->getTitle( $this->mTargetObj
->getPrefixedDBkey() ),
786 htmlspecialchars( $this->mTargetObj
->getPrefixedText() )
790 $previousRev = $archive->getPreviousRevision( $timestamp );
792 $this->showDiff( $previousRev, $rev );
793 if( $wgUser->getOption( 'diffonly' ) ) {
796 $wgOut->addHTML( '<hr />' );
799 $wgOut->addWikiMsg( 'undelete-nodiff' );
803 // date and time are separate parameters to facilitate localisation.
804 // $time is kept for backward compat reasons.
805 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
806 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
807 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
808 $user = $skin->revUserTools( $rev );
810 if( $this->mPreview
) {
811 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
813 $openDiv = '<div id="mw-undelete-revision">';
816 // Revision delete links
817 $canHide = $wgUser->isAllowed( 'deleterevision' );
819 $revdlink = ''; // diffs already have revision delete links
820 } else if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
821 if( !$rev->userCan(Revision
::DELETED_RESTRICTED
) ) {
822 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
826 'target' => $this->mTargetObj
->getPrefixedDBkey(),
827 'ids' => $rev->getTimestamp()
829 $revdlink = $skin->revDeleteLink( $query,
830 $rev->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
836 $wgOut->addHTML( $openDiv . $revdlink . wfMsgWikiHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</div>' );
837 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
839 if( $this->mPreview
) {
841 $popts = $wgOut->parserOptions();
842 $popts->setEditSection( false );
843 $wgOut->parserOptions( $popts );
844 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision
::FOR_THIS_USER
), $this->mTargetObj
, true );
848 Xml
::element( 'textarea', array(
849 'readonly' => 'readonly',
850 'cols' => intval( $wgUser->getOption( 'cols' ) ),
851 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
852 $rev->getText( Revision
::FOR_THIS_USER
) . "\n" ) .
853 Xml
::openElement( 'div' ) .
854 Xml
::openElement( 'form', array(
856 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
857 Xml
::element( 'input', array(
860 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
861 Xml
::element( 'input', array(
863 'name' => 'timestamp',
864 'value' => $timestamp ) ) .
865 Xml
::element( 'input', array(
867 'name' => 'wpEditToken',
868 'value' => $wgUser->editToken() ) ) .
869 Xml
::element( 'input', array(
872 'value' => wfMsg( 'showpreview' ) ) ) .
873 Xml
::element( 'input', array(
876 'value' => wfMsg( 'showdiff' ) ) ) .
877 Xml
::closeElement( 'form' ) .
878 Xml
::closeElement( 'div' ) );
882 * Build a diff display between this and the previous either deleted
883 * or non-deleted edit.
885 * @param $previousRev Revision
886 * @param $currentRev Revision
887 * @return String: HTML
889 function showDiff( $previousRev, $currentRev ) {
892 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
893 $diffEngine->showDiffStyle();
896 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
897 "<col class='diff-marker' />" .
898 "<col class='diff-content' />" .
899 "<col class='diff-marker' />" .
900 "<col class='diff-content' />" .
902 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
903 $this->diffHeader( $previousRev, 'o' ) .
905 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
906 $this->diffHeader( $currentRev, 'n' ) .
909 $diffEngine->generateDiffBody(
910 $previousRev->getText(), $currentRev->getText() ) .
916 private function diffHeader( $rev, $prefix ) {
917 global $wgUser, $wgLang;
918 $sk = $wgUser->getSkin();
919 $isDeleted = !( $rev->getId() && $rev->getTitle() );
921 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
922 $targetPage = $this->getTitle();
923 $targetQuery = array(
924 'target' => $this->mTargetObj
->getPrefixedText(),
925 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
928 /// @todo Fixme getId() may return non-zero for deleted revs...
929 $targetPage = $rev->getTitle();
930 $targetQuery = array( 'oldid' => $rev->getId() );
932 // Add show/hide deletion links if available
933 $canHide = $wgUser->isAllowed( 'deleterevision' );
934 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
936 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
937 $del .= $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
941 'target' => $this->mTargetObj
->getPrefixedDbkey(),
942 'ids' => $rev->getTimestamp()
944 $del .= $sk->revDeleteLink( $query,
945 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
951 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
956 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
957 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
958 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
964 '<div id="mw-diff-'.$prefix.'title2">' .
965 $sk->revUserTools( $rev ) . '<br />' .
967 '<div id="mw-diff-'.$prefix.'title3">' .
968 $sk->revComment( $rev ) . $del . '<br />' .
973 * Show a form confirming whether a tokenless user really wants to see a file
975 private function showFileConfirmationForm( $key ) {
976 global $wgOut, $wgUser, $wgLang;
977 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFile
);
978 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
979 $this->mTargetObj
->getText(),
980 $wgLang->date( $file->getTimestamp() ),
981 $wgLang->time( $file->getTimestamp() ) );
983 Xml
::openElement( 'form', array(
985 'action' => $this->getTitle()->getLocalUrl(
986 'target=' . urlencode( $this->mTarget
) .
987 '&file=' . urlencode( $key ) .
988 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
991 Xml
::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
997 * Show a deleted file version requested by the visitor.
999 private function showFile( $key ) {
1000 global $wgOut, $wgRequest;
1003 # We mustn't allow the output to be Squid cached, otherwise
1004 # if an admin previews a deleted image, and it's cached, then
1005 # a user without appropriate permissions can toddle off and
1006 # nab the image, and Squid will serve it
1007 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1008 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1009 $wgRequest->response()->header( 'Pragma: no-cache' );
1012 require_once( "$IP/includes/StreamFile.php" );
1013 $repo = RepoGroup
::singleton()->getLocalRepo();
1014 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1015 wfStreamFile( $path );
1018 private function showHistory( ) {
1019 global $wgUser, $wgOut;
1021 $sk = $wgUser->getSkin();
1022 if( $this->mAllowed
) {
1023 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
1025 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
1028 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n", array ( 'undeletepagetitle', $this->mTargetObj
->getPrefixedText() ) );
1030 $archive = new PageArchive( $this->mTargetObj
);
1032 $text = $archive->getLastRevisionText();
1033 if( is_null( $text ) ) {
1034 $wgOut->addWikiMsg( "nohistory" );
1038 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1039 if ( $this->mAllowed
) {
1040 $wgOut->addWikiMsg( "undeletehistory" );
1041 $wgOut->addWikiMsg( "undeleterevdel" );
1043 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
1045 $wgOut->addHTML( '</div>' );
1047 # List all stored revisions
1048 $revisions = $archive->listRevisions();
1049 $files = $archive->listFiles();
1051 $haveRevisions = $revisions && $revisions->numRows() > 0;
1052 $haveFiles = $files && $files->numRows() > 0;
1054 # Batch existence check on user and talk pages
1055 if( $haveRevisions ) {
1056 $batch = new LinkBatch();
1057 while( $row = $revisions->fetchObject() ) {
1058 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
1059 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
1062 $revisions->seek( 0 );
1065 $batch = new LinkBatch();
1066 while( $row = $files->fetchObject() ) {
1067 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
1068 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
1074 if ( $this->mAllowed
) {
1075 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1076 # Start the form here
1077 $top = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1078 $wgOut->addHTML( $top );
1081 # Show relevant lines from the deletion log:
1082 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'delete' ) ) . "\n" );
1083 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->mTargetObj
->getPrefixedText() );
1084 # Show relevant lines from the suppression log:
1085 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1086 $wgOut->addHTML( Xml
::element( 'h2', null, LogPage
::logName( 'suppress' ) ) . "\n" );
1087 LogEventsList
::showLogExtract( $wgOut, 'suppress', $this->mTargetObj
->getPrefixedText() );
1090 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
1091 # Format the user-visible controls (comment field, submission button)
1092 # in a nice little table
1093 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1097 <td class='mw-input'>" .
1098 Xml
::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1099 'mw-undelete-unsuppress', $this->mUnsuppress
).
1103 $unsuppressBox = "";
1106 Xml
::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1107 Xml
::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1109 <td colspan='2' class='mw-undelete-extrahelp'>" .
1110 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1114 <td class='mw-label'>" .
1115 Xml
::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1117 <td class='mw-input'>" .
1118 Xml
::input( 'wpComment', 50, $this->mComment
, array( 'id' => 'wpComment' ) ) .
1123 <td class='mw-submit'>" .
1124 Xml
::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1125 Xml
::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1126 Xml
::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1130 Xml
::closeElement( 'table' ) .
1131 Xml
::closeElement( 'fieldset' );
1133 $wgOut->addHTML( $table );
1136 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1138 if( $haveRevisions ) {
1139 # The page's stored (deleted) history:
1140 $wgOut->addHTML("<ul>");
1141 $target = urlencode( $this->mTarget
);
1142 $remaining = $revisions->numRows();
1143 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
1145 while( $row = $revisions->fetchObject() ) {
1147 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1150 $wgOut->addHTML("</ul>");
1152 $wgOut->addWikiMsg( "nohistory" );
1156 $wgOut->addHTML( Xml
::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1157 $wgOut->addHTML( "<ul>" );
1158 while( $row = $files->fetchObject() ) {
1159 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1162 $wgOut->addHTML( "</ul>" );
1165 if ( $this->mAllowed
) {
1166 # Slip in the hidden controls here
1167 $misc = Xml
::hidden( 'target', $this->mTarget
);
1168 $misc .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() );
1169 $misc .= Xml
::closeElement( 'form' );
1170 $wgOut->addHTML( $misc );
1176 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1177 global $wgUser, $wgLang;
1179 $rev = Revision
::newFromArchiveRow( $row,
1180 array( 'page' => $this->mTargetObj
->getArticleId() ) );
1182 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
1183 // Build checkboxen...
1184 if( $this->mAllowed
) {
1185 if( $this->mInvert
) {
1186 if( in_array( $ts, $this->mTargetTimestamp
) ) {
1187 $checkBox = Xml
::check( "ts$ts");
1189 $checkBox = Xml
::check( "ts$ts", true );
1192 $checkBox = Xml
::check( "ts$ts" );
1197 // Build page & diff links...
1198 if( $this->mCanView
) {
1199 $titleObj = $this->getTitle();
1201 if( !$rev->userCan( Revision
::DELETED_TEXT
) ) {
1202 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1203 $last = wfMsgHtml('diff');
1204 } else if( $remaining > 0 ||
($earliestLiveTime && $ts > $earliestLiveTime) ) {
1205 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1206 $last = $sk->linkKnown(
1211 'target' => $this->mTargetObj
->getPrefixedText(),
1217 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1218 $last = wfMsgHtml('diff');
1221 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1222 $last = wfMsgHtml('diff');
1225 $userLink = $sk->revUserTools( $rev );
1226 // Revision text size
1227 if( !is_null($size = $row->ar_len
) ) {
1228 $stxt = $sk->formatRevisionSize( $size );
1231 $comment = $sk->revComment( $rev );
1232 // Revision delete links
1233 $canHide = $wgUser->isAllowed( 'deleterevision' );
1234 if( $canHide ||
($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1235 if( !$rev->userCan( Revision
::DELETED_RESTRICTED
) ) {
1236 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1239 'type' => 'archive',
1240 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1243 $revdlink = $sk->revDeleteLink( $query,
1244 $rev->isDeleted( Revision
::DELETED_RESTRICTED
), $canHide );
1249 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1252 private function formatFileRow( $row, $sk ) {
1253 global $wgUser, $wgLang;
1255 $file = ArchivedFile
::newFromRow( $row );
1257 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
1258 if( $this->mAllowed
&& $row->fa_storage_key
) {
1259 $checkBox = Xml
::check( "fileid" . $row->fa_id
);
1260 $key = urlencode( $row->fa_storage_key
);
1261 $target = urlencode( $this->mTarget
);
1262 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1265 $pageLink = $wgLang->timeanddate( $ts, true );
1267 $userLink = $this->getFileUser( $file, $sk );
1269 wfMsg( 'widthheight',
1270 $wgLang->formatNum( $row->fa_width
),
1271 $wgLang->formatNum( $row->fa_height
) ) .
1273 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
1275 $data = htmlspecialchars( $data );
1276 $comment = $this->getFileComment( $file, $sk );
1277 // Add show/hide deletion links if available
1278 $canHide = $wgUser->isAllowed( 'deleterevision' );
1279 if( $canHide ||
($file->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1280 if( !$file->userCan(File
::DELETED_RESTRICTED
) ) {
1281 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1284 'type' => 'filearchive',
1285 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1286 'ids' => $row->fa_id
1288 $revdlink = $sk->revDeleteLink( $query,
1289 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1294 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1298 * Fetch revision text link if it's available to all users
1301 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1304 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1306 if( !$rev->userCan(Revision
::DELETED_TEXT
) ) {
1307 return '<span class="history-deleted">' . $time . '</span>';
1309 $link = $sk->linkKnown(
1314 'target' => $this->mTargetObj
->getPrefixedText(),
1318 if( $rev->isDeleted(Revision
::DELETED_TEXT
) )
1319 $link = '<span class="history-deleted">' . $link . '</span>';
1325 * Fetch image view link if it's available to all users
1327 * @return String: HTML fragment
1329 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1330 global $wgLang, $wgUser;
1332 if( !$file->userCan(File
::DELETED_FILE
) ) {
1333 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1335 $link = $sk->linkKnown(
1337 $wgLang->timeanddate( $ts, true ),
1340 'target' => $this->mTargetObj
->getPrefixedText(),
1342 'token' => $wgUser->editToken( $key )
1345 if( $file->isDeleted(File
::DELETED_FILE
) )
1346 $link = '<span class="history-deleted">' . $link . '</span>';
1352 * Fetch file's user id if it's available to this user
1354 * @return String: HTML fragment
1356 function getFileUser( $file, $sk ) {
1357 if( !$file->userCan(File
::DELETED_USER
) ) {
1358 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1360 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1361 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1362 if( $file->isDeleted(File
::DELETED_USER
) )
1363 $link = '<span class="history-deleted">' . $link . '</span>';
1369 * Fetch file upload comment if it's available to this user
1371 * @return String: HTML fragment
1373 function getFileComment( $file, $sk ) {
1374 if( !$file->userCan(File
::DELETED_COMMENT
) ) {
1375 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1377 $link = $sk->commentBlock( $file->getRawDescription() );
1378 if( $file->isDeleted(File
::DELETED_COMMENT
) )
1379 $link = '<span class="history-deleted">' . $link . '</span>';
1384 function undelete() {
1385 global $wgOut, $wgUser;
1386 if ( wfReadOnly() ) {
1387 $wgOut->readOnlyPage();
1390 if( !is_null( $this->mTargetObj
) ) {
1391 $archive = new PageArchive( $this->mTargetObj
);
1392 $ok = $archive->undelete(
1393 $this->mTargetTimestamp
,
1395 $this->mFileVersions
,
1396 $this->mUnsuppress
);
1398 if( is_array($ok) ) {
1399 if ( $ok[1] ) // Undeleted file count
1400 wfRunHooks( 'FileUndeleteComplete', array(
1401 $this->mTargetObj
, $this->mFileVersions
,
1402 $wgUser, $this->mComment
) );
1404 $skin = $wgUser->getSkin();
1405 $link = $skin->linkKnown( $this->mTargetObj
);
1406 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1408 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1409 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1412 // Show file deletion warnings and errors
1413 $status = $archive->getFileStatus();
1414 if( $status && !$status->isGood() ) {
1415 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1418 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );