00001 <?php
00002
00014 function wfSpecialUndelete( $par ) {
00015 global $wgRequest;
00016
00017 $form = new UndeleteForm( $wgRequest, $par );
00018 $form->execute();
00019 }
00020
00025 class PageArchive {
00026 protected $title;
00027 var $fileStatus;
00028
00029 function __construct( $title ) {
00030 if( is_null( $title ) ) {
00031 throw new MWException( 'Archiver() given a null title.');
00032 }
00033 $this->title = $title;
00034 }
00035
00043 public static function listAllPages() {
00044 $dbr = wfGetDB( DB_SLAVE );
00045 return self::listPages( $dbr, '' );
00046 }
00047
00055 public static function listPagesByPrefix( $prefix ) {
00056 $dbr = wfGetDB( DB_SLAVE );
00057
00058 $title = Title::newFromText( $prefix );
00059 if( $title ) {
00060 $ns = $title->getNamespace();
00061 $encPrefix = $dbr->escapeLike( $title->getDBkey() );
00062 } else {
00063
00064
00065 $ns = 0;
00066 $encPrefix = $dbr->escapeLike( $prefix );
00067 }
00068 $conds = array(
00069 'ar_namespace' => $ns,
00070 "ar_title LIKE '$encPrefix%'",
00071 );
00072 return self::listPages( $dbr, $conds );
00073 }
00074
00075 protected static function listPages( $dbr, $condition ) {
00076 return $dbr->resultObject(
00077 $dbr->select(
00078 array( 'archive' ),
00079 array(
00080 'ar_namespace',
00081 'ar_title',
00082 'COUNT(*) AS count'
00083 ),
00084 $condition,
00085 __METHOD__,
00086 array(
00087 'GROUP BY' => 'ar_namespace,ar_title',
00088 'ORDER BY' => 'ar_namespace,ar_title',
00089 'LIMIT' => 100,
00090 )
00091 )
00092 );
00093 }
00094
00101 function listRevisions() {
00102 $dbr = wfGetDB( DB_SLAVE );
00103 $res = $dbr->select( 'archive',
00104 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
00105 array( 'ar_namespace' => $this->title->getNamespace(),
00106 'ar_title' => $this->title->getDBkey() ),
00107 'PageArchive::listRevisions',
00108 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
00109 $ret = $dbr->resultObject( $res );
00110 return $ret;
00111 }
00112
00121 function listFiles() {
00122 if( $this->title->getNamespace() == NS_FILE ) {
00123 $dbr = wfGetDB( DB_SLAVE );
00124 $res = $dbr->select( 'filearchive',
00125 array(
00126 'fa_id',
00127 'fa_name',
00128 'fa_archive_name',
00129 'fa_storage_key',
00130 'fa_storage_group',
00131 'fa_size',
00132 'fa_width',
00133 'fa_height',
00134 'fa_bits',
00135 'fa_metadata',
00136 'fa_media_type',
00137 'fa_major_mime',
00138 'fa_minor_mime',
00139 'fa_description',
00140 'fa_user',
00141 'fa_user_text',
00142 'fa_timestamp',
00143 'fa_deleted' ),
00144 array( 'fa_name' => $this->title->getDBkey() ),
00145 __METHOD__,
00146 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
00147 $ret = $dbr->resultObject( $res );
00148 return $ret;
00149 }
00150 return null;
00151 }
00152
00160 function getRevisionText( $timestamp ) {
00161 $rev = $this->getRevision( $timestamp );
00162 return $rev ? $rev->getText() : null;
00163 }
00164
00171 function getRevision( $timestamp ) {
00172 $dbr = wfGetDB( DB_SLAVE );
00173 $row = $dbr->selectRow( 'archive',
00174 array(
00175 'ar_rev_id',
00176 'ar_text',
00177 'ar_comment',
00178 'ar_user',
00179 'ar_user_text',
00180 'ar_timestamp',
00181 'ar_minor_edit',
00182 'ar_flags',
00183 'ar_text_id',
00184 'ar_deleted',
00185 'ar_len' ),
00186 array( 'ar_namespace' => $this->title->getNamespace(),
00187 'ar_title' => $this->title->getDBkey(),
00188 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
00189 __METHOD__ );
00190 if( $row ) {
00191 return new Revision( array(
00192 'page' => $this->title->getArticleId(),
00193 'id' => $row->ar_rev_id,
00194 'text' => ($row->ar_text_id
00195 ? null
00196 : Revision::getRevisionText( $row, 'ar_' ) ),
00197 'comment' => $row->ar_comment,
00198 'user' => $row->ar_user,
00199 'user_text' => $row->ar_user_text,
00200 'timestamp' => $row->ar_timestamp,
00201 'minor_edit' => $row->ar_minor_edit,
00202 'text_id' => $row->ar_text_id,
00203 'deleted' => $row->ar_deleted,
00204 'len' => $row->ar_len) );
00205 } else {
00206 return null;
00207 }
00208 }
00209
00220 function getPreviousRevision( $timestamp ) {
00221 $dbr = wfGetDB( DB_SLAVE );
00222
00223
00224 $row = $dbr->selectRow( 'archive',
00225 'ar_timestamp',
00226 array( 'ar_namespace' => $this->title->getNamespace(),
00227 'ar_title' => $this->title->getDBkey(),
00228 'ar_timestamp < ' .
00229 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
00230 __METHOD__,
00231 array(
00232 'ORDER BY' => 'ar_timestamp DESC',
00233 'LIMIT' => 1 ) );
00234 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
00235
00236 $row = $dbr->selectRow( array( 'page', 'revision' ),
00237 array( 'rev_id', 'rev_timestamp' ),
00238 array(
00239 'page_namespace' => $this->title->getNamespace(),
00240 'page_title' => $this->title->getDBkey(),
00241 'page_id = rev_page',
00242 'rev_timestamp < ' .
00243 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
00244 __METHOD__,
00245 array(
00246 'ORDER BY' => 'rev_timestamp DESC',
00247 'LIMIT' => 1 ) );
00248 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
00249 $prevLiveId = $row ? intval( $row->rev_id ) : null;
00250
00251 if( $prevLive && $prevLive > $prevDeleted ) {
00252
00253 return Revision::newFromId( $prevLiveId );
00254 } elseif( $prevDeleted ) {
00255
00256 return $this->getRevision( $prevDeleted );
00257 } else {
00258
00259 return null;
00260 }
00261 }
00262
00266 function getTextFromRow( $row ) {
00267 if( is_null( $row->ar_text_id ) ) {
00268
00269
00270 return Revision::getRevisionText( $row, "ar_" );
00271 } else {
00272
00273 $dbr = wfGetDB( DB_SLAVE );
00274 $text = $dbr->selectRow( 'text',
00275 array( 'old_text', 'old_flags' ),
00276 array( 'old_id' => $row->ar_text_id ),
00277 __METHOD__ );
00278 return Revision::getRevisionText( $text );
00279 }
00280 }
00281
00282
00291 function getLastRevisionText() {
00292 $dbr = wfGetDB( DB_SLAVE );
00293 $row = $dbr->selectRow( 'archive',
00294 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
00295 array( 'ar_namespace' => $this->title->getNamespace(),
00296 'ar_title' => $this->title->getDBkey() ),
00297 'PageArchive::getLastRevisionText',
00298 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
00299 if( $row ) {
00300 return $this->getTextFromRow( $row );
00301 } else {
00302 return NULL;
00303 }
00304 }
00305
00310 function isDeleted() {
00311 $dbr = wfGetDB( DB_SLAVE );
00312 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
00313 array( 'ar_namespace' => $this->title->getNamespace(),
00314 'ar_title' => $this->title->getDBkey() ) );
00315 return ($n > 0);
00316 }
00317
00331 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
00332
00333
00334 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
00335
00336 $restoreText = $restoreAll || !empty( $timestamps );
00337 $restoreFiles = $restoreAll || !empty( $fileVersions );
00338
00339 if( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
00340 $img = wfLocalFile( $this->title );
00341 $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
00342 $filesRestored = $this->fileStatus->successCount;
00343 } else {
00344 $filesRestored = 0;
00345 }
00346
00347 if( $restoreText ) {
00348 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress );
00349 if($textRestored === false)
00350 return false;
00351 } else {
00352 $textRestored = 0;
00353 }
00354
00355
00356 global $wgContLang;
00357 $log = new LogPage( 'delete' );
00358
00359 if( $textRestored && $filesRestored ) {
00360 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
00361 $wgContLang->formatNum( $textRestored ),
00362 $wgContLang->formatNum( $filesRestored ) );
00363 } elseif( $textRestored ) {
00364 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
00365 $wgContLang->formatNum( $textRestored ) );
00366 } elseif( $filesRestored ) {
00367 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
00368 $wgContLang->formatNum( $filesRestored ) );
00369 } else {
00370 wfDebug( "Undelete: nothing undeleted...\n" );
00371 return false;
00372 }
00373
00374 if( trim( $comment ) != '' )
00375 $reason .= ": {$comment}";
00376 $log->addEntry( 'restore', $this->title, $reason );
00377
00378 return array($textRestored, $filesRestored, $reason);
00379 }
00380
00393 private function undeleteRevisions( $timestamps, $unsuppress = false ) {
00394 if ( wfReadOnly() )
00395 return false;
00396 $restoreAll = empty( $timestamps );
00397
00398 $dbw = wfGetDB( DB_MASTER );
00399
00400 # Does this page already exist? We'll have to update it...
00401 $article = new Article( $this->title );
00402 $options = 'FOR UPDATE';
00403 $page = $dbw->selectRow( 'page',
00404 array( 'page_id', 'page_latest' ),
00405 array( 'page_namespace' => $this->title->getNamespace(),
00406 'page_title' => $this->title->getDBkey() ),
00407 __METHOD__,
00408 $options );
00409 if( $page ) {
00410 $makepage = false;
00411 # Page already exists. Import the history, and if necessary
00412 # we'll update the latest revision field in the record.
00413 $newid = 0;
00414 $pageId = $page->page_id;
00415 $previousRevId = $page->page_latest;
00416 # Get the time span of this page
00417 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
00418 array( 'rev_id' => $previousRevId ),
00419 __METHOD__ );
00420 if( $previousTimestamp === false ) {
00421 wfDebug( __METHOD__.": existing page refers to a page_latest that does not exist\n" );
00422 return 0;
00423 }
00424 } else {
00425 # Have to create a new article...
00426 $makepage = true;
00427 $previousRevId = 0;
00428 $previousTimestamp = 0;
00429 }
00430
00431 if( $restoreAll ) {
00432 $oldones = '1 = 1'; # All revisions...
00433 } else {
00434 $oldts = implode( ',',
00435 array_map( array( &$dbw, 'addQuotes' ),
00436 array_map( array( &$dbw, 'timestamp' ),
00437 $timestamps ) ) );
00438
00439 $oldones = "ar_timestamp IN ( {$oldts} )";
00440 }
00441
00445 $result = $dbw->select( 'archive',
00446 array(
00447 'ar_rev_id',
00448 'ar_text',
00449 'ar_comment',
00450 'ar_user',
00451 'ar_user_text',
00452 'ar_timestamp',
00453 'ar_minor_edit',
00454 'ar_flags',
00455 'ar_text_id',
00456 'ar_deleted',
00457 'ar_page_id',
00458 'ar_len' ),
00459 array(
00460 'ar_namespace' => $this->title->getNamespace(),
00461 'ar_title' => $this->title->getDBkey(),
00462 $oldones ),
00463 __METHOD__,
00464 array( 'ORDER BY' => 'ar_timestamp' )
00465 );
00466 $ret = $dbw->resultObject( $result );
00467 $rev_count = $dbw->numRows( $result );
00468
00469 if( $makepage ) {
00470 $newid = $article->insertOn( $dbw );
00471 $pageId = $newid;
00472 }
00473
00474 $revision = null;
00475 $restored = 0;
00476
00477 while( $row = $ret->fetchObject() ) {
00478 if( $row->ar_text_id ) {
00479
00480
00481
00482
00483 $revText = null;
00484 } else {
00485
00486
00487
00488 $revText = Revision::getRevisionText( $row, 'ar_' );
00489 }
00490
00491 if( $row->ar_rev_id ) {
00492 $exists = $dbw->selectField( 'revision', '1', array('rev_id' => $row->ar_rev_id), __METHOD__ );
00493 if( $exists ) continue;
00494 }
00495
00496 $revision = new Revision( array(
00497 'page' => $pageId,
00498 'id' => $row->ar_rev_id,
00499 'text' => $revText,
00500 'comment' => $row->ar_comment,
00501 'user' => $row->ar_user,
00502 'user_text' => $row->ar_user_text,
00503 'timestamp' => $row->ar_timestamp,
00504 'minor_edit' => $row->ar_minor_edit,
00505 'text_id' => $row->ar_text_id,
00506 'deleted' => $unsuppress ? 0 : $row->ar_deleted,
00507 'len' => $row->ar_len
00508 ) );
00509 $revision->insertOn( $dbw );
00510 $restored++;
00511
00512 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
00513 }
00514 # Now that it's safely stored, take it out of the archive
00515 $dbw->delete( 'archive',
00516 array(
00517 'ar_namespace' => $this->title->getNamespace(),
00518 'ar_title' => $this->title->getDBkey(),
00519 $oldones ),
00520 __METHOD__ );
00521
00522
00523 if( $restored == 0 )
00524 return 0;
00525
00526 if( $revision ) {
00527
00528 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
00529 if( $newid || $wasnew ) {
00530
00531 $article->createUpdates( $revision );
00532
00533 if( $revision->getVisibility() ) {
00534 $dbw->update( 'revision',
00535 array( 'rev_deleted' => 0 ),
00536 array( 'rev_id' => $revision->getId() ),
00537 __METHOD__
00538 );
00539 }
00540 }
00541
00542 if( $newid ) {
00543 wfRunHooks( 'ArticleUndelete', array( &$this->title, true ) );
00544 Article::onArticleCreate( $this->title );
00545 } else {
00546 wfRunHooks( 'ArticleUndelete', array( &$this->title, false ) );
00547 Article::onArticleEdit( $this->title );
00548 }
00549
00550 if( $this->title->getNamespace() == NS_FILE ) {
00551 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
00552 $update->doUpdate();
00553 }
00554 } else {
00555
00556 return self::UNDELETE_UNKNOWNERR;
00557 }
00558
00559 return $restored;
00560 }
00561
00562 function getFileStatus() { return $this->fileStatus; }
00563 }
00564
00570 class UndeleteForm {
00571 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
00572 var $mTargetTimestamp, $mAllowed, $mComment, $mToken;
00573
00574 function UndeleteForm( $request, $par = "" ) {
00575 global $wgUser;
00576 $this->mAction = $request->getVal( 'action' );
00577 $this->mTarget = $request->getVal( 'target' );
00578 $this->mSearchPrefix = $request->getText( 'prefix' );
00579 $time = $request->getVal( 'timestamp' );
00580 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
00581 $this->mFile = $request->getVal( 'file' );
00582
00583 $posted = $request->wasPosted() &&
00584 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
00585 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
00586 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
00587 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
00588 $this->mDiff = $request->getCheck( 'diff' );
00589 $this->mComment = $request->getText( 'wpComment' );
00590 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
00591 $this->mToken = $request->getVal( 'token' );
00592
00593 if( $par != "" ) {
00594 $this->mTarget = $par;
00595 }
00596 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
00597 $this->mAllowed = true;
00598 } else {
00599 $this->mAllowed = false;
00600 $this->mTimestamp = '';
00601 $this->mRestore = false;
00602 }
00603 if ( $this->mTarget !== "" ) {
00604 $this->mTargetObj = Title::newFromURL( $this->mTarget );
00605 } else {
00606 $this->mTargetObj = NULL;
00607 }
00608 if( $this->mRestore || $this->mInvert ) {
00609 $timestamps = array();
00610 $this->mFileVersions = array();
00611 foreach( $_REQUEST as $key => $val ) {
00612 $matches = array();
00613 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
00614 array_push( $timestamps, $matches[1] );
00615 }
00616
00617 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
00618 $this->mFileVersions[] = intval( $matches[1] );
00619 }
00620 }
00621 rsort( $timestamps );
00622 $this->mTargetTimestamp = $timestamps;
00623 }
00624 }
00625
00626 function execute() {
00627 global $wgOut, $wgUser;
00628 if ( $this->mAllowed ) {
00629 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
00630 } else {
00631 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
00632 }
00633
00634 if( is_null( $this->mTargetObj ) ) {
00635 # Not all users can just browse every deleted page from the list
00636 if( $wgUser->isAllowed( 'browsearchive' ) ) {
00637 $this->showSearchForm();
00638
00639 # List undeletable articles
00640 if( $this->mSearchPrefix ) {
00641 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
00642 $this->showList( $result );
00643 }
00644 } else {
00645 $wgOut->addWikiText( wfMsgHtml( 'undelete-header' ) );
00646 }
00647 return;
00648 }
00649 if( $this->mTimestamp !== '' ) {
00650 return $this->showRevision( $this->mTimestamp );
00651 }
00652 if( $this->mFile !== null ) {
00653 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFile );
00654
00655 if( !$file->userCan( File::DELETED_FILE ) ) {
00656 $wgOut->permissionRequired( 'suppressrevision' );
00657 return false;
00658 } elseif ( !$wgUser->matchEditToken( $this->mToken, $this->mFile ) ) {
00659 $this->showFileConfirmationForm( $this->mFile );
00660 return false;
00661 } else {
00662 return $this->showFile( $this->mFile );
00663 }
00664 }
00665 if( $this->mRestore && $this->mAction == "submit" ) {
00666 return $this->undelete();
00667 }
00668 if( $this->mInvert && $this->mAction == "submit" ) {
00669 return $this->showHistory( );
00670 }
00671 return $this->showHistory();
00672 }
00673
00674 function showSearchForm() {
00675 global $wgOut, $wgScript;
00676 $wgOut->addWikiMsg( 'undelete-header' );
00677
00678 $wgOut->addHTML(
00679 Xml::openElement( 'form', array(
00680 'method' => 'get',
00681 'action' => $wgScript ) ) .
00682 Xml::fieldset( wfMsg( 'undelete-search-box' ) ) .
00683 Xml::hidden( 'title',
00684 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
00685 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
00686 'prefix', 'prefix', 20,
00687 $this->mSearchPrefix ) . ' ' .
00688 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
00689 Xml::closeElement( 'fieldset' ) .
00690 Xml::closeElement( 'form' )
00691 );
00692 }
00693
00694
00695 private function showList( $result ) {
00696 global $wgLang, $wgContLang, $wgUser, $wgOut;
00697
00698 if( $result->numRows() == 0 ) {
00699 $wgOut->addWikiMsg( 'undelete-no-results' );
00700 return;
00701 }
00702
00703 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
00704
00705 $sk = $wgUser->getSkin();
00706 $undelete = SpecialPage::getTitleFor( 'Undelete' );
00707 $wgOut->addHTML( "<ul>\n" );
00708 while( $row = $result->fetchObject() ) {
00709 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
00710 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ),
00711 'target=' . $title->getPrefixedUrl() );
00712 $revs = wfMsgExt( 'undeleterevisions',
00713 array( 'parseinline' ),
00714 $wgLang->formatNum( $row->count ) );
00715 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
00716 }
00717 $result->free();
00718 $wgOut->addHTML( "</ul>\n" );
00719
00720 return true;
00721 }
00722
00723 private function showRevision( $timestamp ) {
00724 global $wgLang, $wgUser, $wgOut;
00725 $self = SpecialPage::getTitleFor( 'Undelete' );
00726 $skin = $wgUser->getSkin();
00727
00728 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
00729
00730 $archive = new PageArchive( $this->mTargetObj );
00731 $rev = $archive->getRevision( $timestamp );
00732
00733 if( !$rev ) {
00734 $wgOut->addWikiMsg( 'undeleterevision-missing' );
00735 return;
00736 }
00737
00738 if( $rev->isDeleted(Revision::DELETED_TEXT) ) {
00739 if( !$rev->userCan(Revision::DELETED_TEXT) ) {
00740 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-permission' );
00741 return;
00742 } else {
00743 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1</div>\n", 'rev-deleted-text-view' );
00744 $wgOut->addHTML( '<br/>' );
00745
00746 }
00747 }
00748
00749 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
00750
00751 $link = $skin->makeKnownLinkObj(
00752 SpecialPage::getTitleFor( 'Undelete', $this->mTargetObj->getPrefixedDBkey() ),
00753 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
00754 );
00755
00756 if( $this->mDiff ) {
00757 $previousRev = $archive->getPreviousRevision( $timestamp );
00758 if( $previousRev ) {
00759 $this->showDiff( $previousRev, $rev );
00760 if( $wgUser->getOption( 'diffonly' ) ) {
00761 return;
00762 } else {
00763 $wgOut->addHTML( '<hr />' );
00764 }
00765 } else {
00766 $wgOut->addHTML( wfMsgHtml( 'undelete-nodiff' ) );
00767 }
00768 }
00769
00770
00771
00772 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
00773 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
00774 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
00775 $user = $skin->revUserTools( $rev );
00776
00777 $wgOut->addHTML( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</p>' );
00778
00779 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
00780
00781 if( $this->mPreview ) {
00782 $wgOut->addHTML( "<hr />\n" );
00783
00784
00785 $popts = $wgOut->parserOptions();
00786 $popts->setEditSection( false );
00787 $wgOut->parserOptions( $popts );
00788 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision::FOR_THIS_USER ), $this->mTargetObj, true );
00789 }
00790
00791 $wgOut->addHTML(
00792 Xml::element( 'textarea', array(
00793 'readonly' => 'readonly',
00794 'cols' => intval( $wgUser->getOption( 'cols' ) ),
00795 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
00796 $rev->getText( Revision::FOR_THIS_USER ) . "\n" ) .
00797 Xml::openElement( 'div' ) .
00798 Xml::openElement( 'form', array(
00799 'method' => 'post',
00800 'action' => $self->getLocalURL( "action=submit" ) ) ) .
00801 Xml::element( 'input', array(
00802 'type' => 'hidden',
00803 'name' => 'target',
00804 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
00805 Xml::element( 'input', array(
00806 'type' => 'hidden',
00807 'name' => 'timestamp',
00808 'value' => $timestamp ) ) .
00809 Xml::element( 'input', array(
00810 'type' => 'hidden',
00811 'name' => 'wpEditToken',
00812 'value' => $wgUser->editToken() ) ) .
00813 Xml::element( 'input', array(
00814 'type' => 'submit',
00815 'name' => 'preview',
00816 'value' => wfMsg( 'showpreview' ) ) ) .
00817 Xml::element( 'input', array(
00818 'name' => 'diff',
00819 'type' => 'submit',
00820 'value' => wfMsg( 'showdiff' ) ) ) .
00821 Xml::closeElement( 'form' ) .
00822 Xml::closeElement( 'div' ) );
00823 }
00824
00832 function showDiff( $previousRev, $currentRev ) {
00833 global $wgOut, $wgUser;
00834
00835 $diffEngine = new DifferenceEngine();
00836 $diffEngine->showDiffStyle();
00837 $wgOut->addHTML(
00838 "<div>" .
00839 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
00840 "<col class='diff-marker' />" .
00841 "<col class='diff-content' />" .
00842 "<col class='diff-marker' />" .
00843 "<col class='diff-content' />" .
00844 "<tr>" .
00845 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
00846 $this->diffHeader( $previousRev, 'o' ) .
00847 "</td>\n" .
00848 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
00849 $this->diffHeader( $currentRev, 'n' ) .
00850 "</td>\n" .
00851 "</tr>" .
00852 $diffEngine->generateDiffBody(
00853 $previousRev->getText(), $currentRev->getText() ) .
00854 "</table>" .
00855 "</div>\n" );
00856
00857 }
00858
00859 private function diffHeader( $rev, $prefix ) {
00860 global $wgUser, $wgLang, $wgLang;
00861 $sk = $wgUser->getSkin();
00862 $isDeleted = !( $rev->getId() && $rev->getTitle() );
00863 if( $isDeleted ) {
00865 $targetPage = SpecialPage::getTitleFor( 'Undelete' );
00866 $targetQuery = 'target=' .
00867 $this->mTargetObj->getPrefixedUrl() .
00868 '×tamp=' .
00869 wfTimestamp( TS_MW, $rev->getTimestamp() );
00870 } else {
00872 $targetPage = $rev->getTitle();
00873 $targetQuery = 'oldid=' . $rev->getId();
00874 }
00875 return
00876 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
00877 $sk->makeLinkObj( $targetPage,
00878 wfMsgHtml( 'revisionasof',
00879 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
00880 $targetQuery ) .
00881 ( $isDeleted ? ' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
00882 '</strong></div>' .
00883 '<div id="mw-diff-'.$prefix.'title2">' .
00884 $sk->revUserTools( $rev ) . '<br/>' .
00885 '</div>' .
00886 '<div id="mw-diff-'.$prefix.'title3">' .
00887 $sk->revComment( $rev ) . '<br/>' .
00888 '</div>';
00889 }
00890
00894 private function showFileConfirmationForm( $key ) {
00895 global $wgOut, $wgUser, $wgLang;
00896 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFile );
00897 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
00898 $this->mTargetObj->getText(),
00899 $wgLang->date( $file->getTimestamp() ),
00900 $wgLang->time( $file->getTimestamp() ) );
00901 $wgOut->addHTML(
00902 Xml::openElement( 'form', array(
00903 'method' => 'POST',
00904 'action' => SpecialPage::getTitleFor( 'Undelete' )->getLocalUrl(
00905 'target=' . urlencode( $this->mTarget ) .
00906 '&file=' . urlencode( $key ) .
00907 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
00908 )
00909 ) .
00910 Xml::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
00911 '</form>'
00912 );
00913 }
00914
00918 private function showFile( $key ) {
00919 global $wgOut, $wgRequest;
00920 $wgOut->disable();
00921
00922 # We mustn't allow the output to be Squid cached, otherwise
00923 # if an admin previews a deleted image, and it's cached, then
00924 # a user without appropriate permissions can toddle off and
00925 # nab the image, and Squid will serve it
00926 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
00927 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
00928 $wgRequest->response()->header( 'Pragma: no-cache' );
00929
00930 $store = FileStore::get( 'deleted' );
00931 $store->stream( $key );
00932 }
00933
00934 private function showHistory( ) {
00935 global $wgLang, $wgUser, $wgOut;
00936
00937 $sk = $wgUser->getSkin();
00938 if( $this->mAllowed ) {
00939 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
00940 } else {
00941 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
00942 }
00943
00944 $wgOut->addWikiText( wfMsgHtml( 'undeletepagetitle', $this->mTargetObj->getPrefixedText()) );
00945
00946 $archive = new PageArchive( $this->mTargetObj );
00947
00948
00949
00950
00951
00952
00953
00954 if ( $this->mAllowed ) {
00955 $wgOut->addWikiMsg( "undeletehistory" );
00956 $wgOut->addWikiMsg( "undeleterevdel" );
00957 } else {
00958 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
00959 }
00960
00961 # List all stored revisions
00962 $revisions = $archive->listRevisions();
00963 $files = $archive->listFiles();
00964
00965 $haveRevisions = $revisions && $revisions->numRows() > 0;
00966 $haveFiles = $files && $files->numRows() > 0;
00967
00968 # Batch existence check on user and talk pages
00969 if( $haveRevisions ) {
00970 $batch = new LinkBatch();
00971 while( $row = $revisions->fetchObject() ) {
00972 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
00973 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
00974 }
00975 $batch->execute();
00976 $revisions->seek( 0 );
00977 }
00978 if( $haveFiles ) {
00979 $batch = new LinkBatch();
00980 while( $row = $files->fetchObject() ) {
00981 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
00982 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
00983 }
00984 $batch->execute();
00985 $files->seek( 0 );
00986 }
00987
00988 if ( $this->mAllowed ) {
00989 $titleObj = SpecialPage::getTitleFor( "Undelete" );
00990 $action = $titleObj->getLocalURL( "action=submit" );
00991 # Start the form here
00992 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
00993 $wgOut->addHTML( $top );
00994 }
00995
00996 # Show relevant lines from the deletion log:
00997 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) . "\n" );
00998 LogEventsList::showLogExtract( $wgOut, 'delete', $this->mTargetObj->getPrefixedText() );
00999 # Show relevant lines from the suppression log:
01000 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
01001 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'suppress' ) ) . "\n" );
01002 LogEventsList::showLogExtract( $wgOut, 'suppress', $this->mTargetObj->getPrefixedText() );
01003 }
01004
01005 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
01006 # Format the user-visible controls (comment field, submission button)
01007 # in a nice little table
01008 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
01009 $unsuppressBox =
01010 "<tr>
01011 <td> </td>
01012 <td class='mw-input'>" .
01013 Xml::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
01014 'mw-undelete-unsuppress', $this->mUnsuppress ).
01015 "</td>
01016 </tr>";
01017 } else {
01018 $unsuppressBox = "";
01019 }
01020 $table =
01021 Xml::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
01022 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
01023 "<tr>
01024 <td colspan='2'>" .
01025 wfMsgWikiHtml( 'undeleteextrahelp' ) .
01026 "</td>
01027 </tr>
01028 <tr>
01029 <td class='mw-label'>" .
01030 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
01031 "</td>
01032 <td class='mw-input'>" .
01033 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
01034 "</td>
01035 </tr>
01036 <tr>
01037 <td> </td>
01038 <td class='mw-submit'>" .
01039 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
01040 Xml::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
01041 Xml::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
01042 "</td>
01043 </tr>" .
01044 $unsuppressBox .
01045 Xml::closeElement( 'table' ) .
01046 Xml::closeElement( 'fieldset' );
01047
01048 $wgOut->addHTML( $table );
01049 }
01050
01051 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
01052
01053 if( $haveRevisions ) {
01054 # The page's stored (deleted) history:
01055 $wgOut->addHTML("<ul>");
01056 $target = urlencode( $this->mTarget );
01057 $remaining = $revisions->numRows();
01058 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
01059
01060 while( $row = $revisions->fetchObject() ) {
01061 $remaining--;
01062 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
01063 }
01064 $revisions->free();
01065 $wgOut->addHTML("</ul>");
01066 } else {
01067 $wgOut->addWikiMsg( "nohistory" );
01068 }
01069
01070 if( $haveFiles ) {
01071 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
01072 $wgOut->addHTML( "<ul>" );
01073 while( $row = $files->fetchObject() ) {
01074 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
01075 }
01076 $files->free();
01077 $wgOut->addHTML( "</ul>" );
01078 }
01079
01080 if ( $this->mAllowed ) {
01081 # Slip in the hidden controls here
01082 $misc = Xml::hidden( 'target', $this->mTarget );
01083 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
01084 $misc .= Xml::closeElement( 'form' );
01085 $wgOut->addHTML( $misc );
01086 }
01087
01088 return true;
01089 }
01090
01091 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
01092 global $wgUser, $wgLang;
01093
01094 $rev = new Revision( array(
01095 'page' => $this->mTargetObj->getArticleId(),
01096 'comment' => $row->ar_comment,
01097 'user' => $row->ar_user,
01098 'user_text' => $row->ar_user_text,
01099 'timestamp' => $row->ar_timestamp,
01100 'minor_edit' => $row->ar_minor_edit,
01101 'deleted' => $row->ar_deleted,
01102 'len' => $row->ar_len ) );
01103
01104 $stxt = '';
01105 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
01106 if( $this->mAllowed ) {
01107 if( $this->mInvert){
01108 if( in_array( $ts, $this->mTargetTimestamp ) ) {
01109 $checkBox = Xml::check( "ts$ts");
01110 } else {
01111 $checkBox = Xml::check( "ts$ts", true );
01112 }
01113 } else {
01114 $checkBox = Xml::check( "ts$ts" );
01115 }
01116 $titleObj = SpecialPage::getTitleFor( "Undelete" );
01117 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
01118 # Last link
01119 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
01120 $last = wfMsgHtml('diff');
01121 } else if( $remaining > 0 || ($earliestLiveTime && $ts > $earliestLiveTime) ) {
01122 $last = $sk->makeKnownLinkObj( $titleObj, wfMsgHtml('diff'),
01123 "target=" . $this->mTargetObj->getPrefixedUrl() . "×tamp=$ts&diff=prev" );
01124 } else {
01125 $last = wfMsgHtml('diff');
01126 }
01127 } else {
01128 $checkBox = '';
01129 $pageLink = $wgLang->timeanddate( $ts, true );
01130 $last = wfMsgHtml('diff');
01131 }
01132 $userLink = $sk->revUserTools( $rev );
01133
01134 if(!is_null($size = $row->ar_len)) {
01135 $stxt = $sk->formatRevisionSize( $size );
01136 }
01137 $comment = $sk->revComment( $rev );
01138 $revdlink = '';
01139 if( $wgUser->isAllowed( 'deleterevision' ) ) {
01140 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
01141
01142 $revdlink = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.wfMsgHtml('rev-delundel').')' );
01143 } else {
01144 $query = array( 'target' => $this->mTargetObj->getPrefixedDBkey(),
01145 'artimestamp[]' => $ts
01146 );
01147 $revdlink = $sk->revDeleteLink( $query, $rev->isDeleted( Revision::DELETED_RESTRICTED ) );
01148 }
01149 }
01150
01151 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
01152 }
01153
01154 private function formatFileRow( $row, $sk ) {
01155 global $wgUser, $wgLang;
01156
01157 $file = ArchivedFile::newFromRow( $row );
01158
01159 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
01160 if( $this->mAllowed && $row->fa_storage_key ) {
01161 $checkBox = Xml::check( "fileid" . $row->fa_id );
01162 $key = urlencode( $row->fa_storage_key );
01163 $target = urlencode( $this->mTarget );
01164 $titleObj = SpecialPage::getTitleFor( "Undelete" );
01165 $pageLink = $this->getFileLink( $file, $titleObj, $ts, $key, $sk );
01166 } else {
01167 $checkBox = '';
01168 $pageLink = $wgLang->timeanddate( $ts, true );
01169 }
01170 $userLink = $this->getFileUser( $file, $sk );
01171 $data =
01172 wfMsg( 'widthheight',
01173 $wgLang->formatNum( $row->fa_width ),
01174 $wgLang->formatNum( $row->fa_height ) ) .
01175 ' (' .
01176 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
01177 ')';
01178 $data = htmlspecialchars( $data );
01179 $comment = $this->getFileComment( $file, $sk );
01180 $revdlink = '';
01181 if( $wgUser->isAllowed( 'deleterevision' ) ) {
01182 if( !$file->userCan(File::DELETED_RESTRICTED ) ) {
01183
01184 $revdlink = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ), '('.wfMsgHtml('rev-delundel').')' );
01185 } else {
01186 $query = array( 'target' => $this->mTargetObj->getPrefixedDBkey(),
01187 'fileid' => $row->fa_id
01188 );
01189 $revdlink = $sk->revDeleteLink( $query, $file->isDeleted( File::DELETED_RESTRICTED ) );
01190 }
01191 }
01192 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
01193 }
01194
01199 function getPageLink( $rev, $titleObj, $ts, $sk ) {
01200 global $wgLang;
01201
01202 if( !$rev->userCan(Revision::DELETED_TEXT) ) {
01203 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
01204 } else {
01205 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
01206 "target=".$this->mTargetObj->getPrefixedUrl()."×tamp=$ts" );
01207 if( $rev->isDeleted(Revision::DELETED_TEXT) )
01208 $link = '<span class="history-deleted">' . $link . '</span>';
01209 return $link;
01210 }
01211 }
01212
01217 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
01218 global $wgLang, $wgUser;
01219
01220 if( !$file->userCan(File::DELETED_FILE) ) {
01221 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
01222 } else {
01223 $link = $sk->makeKnownLinkObj( $titleObj, $wgLang->timeanddate( $ts, true ),
01224 "target=".$this->mTargetObj->getPrefixedUrl().
01225 "&file=$key" .
01226 "&token=" . urlencode( $wgUser->editToken( $key ) ) );
01227 if( $file->isDeleted(File::DELETED_FILE) )
01228 $link = '<span class="history-deleted">' . $link . '</span>';
01229 return $link;
01230 }
01231 }
01232
01237 function getFileUser( $file, $sk ) {
01238 if( !$file->userCan(File::DELETED_USER) ) {
01239 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
01240 } else {
01241 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
01242 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
01243 if( $file->isDeleted(File::DELETED_USER) )
01244 $link = '<span class="history-deleted">' . $link . '</span>';
01245 return $link;
01246 }
01247 }
01248
01253 function getFileComment( $file, $sk ) {
01254 if( !$file->userCan(File::DELETED_COMMENT) ) {
01255 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
01256 } else {
01257 $link = $sk->commentBlock( $file->getRawDescription() );
01258 if( $file->isDeleted(File::DELETED_COMMENT) )
01259 $link = '<span class="history-deleted">' . $link . '</span>';
01260 return $link;
01261 }
01262 }
01263
01264 function undelete() {
01265 global $wgOut, $wgUser;
01266 if ( wfReadOnly() ) {
01267 $wgOut->readOnlyPage();
01268 return;
01269 }
01270 if( !is_null( $this->mTargetObj ) ) {
01271 $archive = new PageArchive( $this->mTargetObj );
01272 $ok = $archive->undelete(
01273 $this->mTargetTimestamp,
01274 $this->mComment,
01275 $this->mFileVersions,
01276 $this->mUnsuppress );
01277
01278 if( is_array($ok) ) {
01279 if ( $ok[1] )
01280 wfRunHooks( 'FileUndeleteComplete', array(
01281 $this->mTargetObj, $this->mFileVersions,
01282 $wgUser, $this->mComment) );
01283
01284 $skin = $wgUser->getSkin();
01285 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
01286 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
01287 } else {
01288 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
01289 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
01290 }
01291
01292
01293 $status = $archive->getFileStatus();
01294 if( $status && !$status->isGood() ) {
01295 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
01296 }
01297 } else {
01298 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
01299 }
01300 return false;
01301 }
01302 }