autoload Profiling, ParserCache, WebRequest
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
index 657b4b5..ade8d75 100644 (file)
@@ -1,13 +1,13 @@
 <?php
+
 /**
- * @todo document
+ * Special page allowing users with the appropriate permissions to view
+ * and restore deleted content
+ *
  * @package MediaWiki
- * @subpackage SpecialPage
+ * @subpackage Special pages
  */
 
-/** */
-require_once( 'Revision.php' );
-
 /**
  *
  */
@@ -28,7 +28,7 @@ class PageArchive {
 
        function PageArchive( &$title ) {
                if( is_null( $title ) ) {
-                       wfDebugDieBacktrace( 'Archiver() given a null title.');
+                       throw new MWException( 'Archiver() given a null title.');
                }
                $this->title =& $title;
        }
@@ -91,7 +91,7 @@ class PageArchive {
         */
        function getTextFromRow( $row ) {
                $fname = 'PageArchive::getTextFromRow';
-               
+
                if( is_null( $row->ar_text_id ) ) {
                        // An old row from MediaWiki 1.4 or previous.
                        // Text is embedded in this row in classic compression format.
@@ -106,7 +106,7 @@ class PageArchive {
                        return Revision::getRevisionText( $text );
                }
        }
-               
+
 
        /**
         * Fetch (and decompress if necessary) the stored text of the most
@@ -154,8 +154,8 @@ class PageArchive {
         * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
         * @return bool
         */
-       function undelete( $timestamps ) {
-               global $wgDeferredUpdateList, $wgParser, $wgDBtype;
+       function undelete( $timestamps, $comment = '' ) {
+               global $wgParser, $wgDBtype;
 
                $fname = "doUndeleteArticle";
                $restoreAll = empty( $timestamps );
@@ -181,13 +181,11 @@ class PageArchive {
                        $newid             = 0;
                        $pageId            = $page->page_id;
                        $previousRevId     = $page->page_latest;
-                       $previousTimestamp = $page->rev_timestamp;
                } else {
                        # Have to create a new article...
                        $newid  = $article->insertOn( $dbw );
                        $pageId = $newid;
                        $previousRevId = 0;
-                       $previousTimestamp = 0;
                }
 
                if( $restoreAll ) {
@@ -225,7 +223,7 @@ class PageArchive {
                        );
                $revision = null;
                $newRevId = $previousRevId;
-               
+
                while( $row = $dbw->fetchObject( $result ) ) {
                        if( $row->ar_text_id ) {
                                // Revision was deleted in 1.5+; text is in
@@ -289,9 +287,11 @@ class PageArchive {
                # Touch the log!
                $log = new LogPage( 'delete' );
                if( $restoreAll ) {
-                       $reason = '';
+                       $reason = $comment;
                } else {
                        $reason = wfMsgForContent( 'undeletedrevisions', $restoreRevisions );
+                       if( trim( $comment ) != '' )
+                               $reason .= ": {$comment}";
                }
                $log->addEntry( 'restore', $this->title, $reason );
 
@@ -306,16 +306,20 @@ class PageArchive {
  */
 class UndeleteForm {
        var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
-       var $mTargetTimestamp, $mAllowed;
+       var $mTargetTimestamp, $mAllowed, $mComment;
 
        function UndeleteForm( &$request, $par = "" ) {
                global $wgUser;
                $this->mAction = $request->getText( 'action' );
                $this->mTarget = $request->getText( 'target' );
                $this->mTimestamp = $request->getText( 'timestamp' );
-               $this->mRestore = $request->getCheck( 'restore' ) &&
-                       $request->wasPosted() &&
+               
+               $posted = $request->wasPosted() &&
                        $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
+               $this->mRestore = $request->getCheck( 'restore' ) && $posted;
+               $this->mPreview = $request->getCheck( 'preview' ) && $posted;
+               $this->mComment = $request->getText( 'wpComment' );
+               
                if( $par != "" ) {
                        $this->mTarget = $par;
                }
@@ -344,7 +348,7 @@ class UndeleteForm {
        }
 
        function execute() {
-               
+
                if( is_null( $this->mTargetObj ) ) {
                        return $this->showList();
                }
@@ -382,7 +386,7 @@ class UndeleteForm {
                                htmlspecialchars( $n ), "target=" . urlencode( $n ) );
                        $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
                                $wgLang->formatNum( $row->count ) ) );
-                       $wgOut->addHTML( "<li>$link $revisions</li>\n" );
+                       $wgOut->addHTML( "<li>$link ($revisions)</li>\n" );
                }
                $result->free();
                $wgOut->addHTML( "</ul>\n" );
@@ -401,7 +405,46 @@ class UndeleteForm {
 
                $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
                $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
-                       $wgLang->date( $timestamp ) ) . ")\n<hr />\n" . $text );
+                       $wgLang->date( $timestamp ) ) . ")\n" );
+               
+               if( $this->mPreview ) {
+                       $wgOut->addHtml( "<hr />\n" );
+                       $wgOut->addWikiText( $text );
+               }
+               
+               $self = Title::makeTitle( NS_SPECIAL, "Undelete" );
+               
+               $wgOut->addHtml(
+                       wfElement( 'textarea', array(
+                                       'readonly' => true,
+                                       'cols' => intval( $wgUser->getOption( 'cols' ) ),
+                                       'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
+                               $text . "\n" ) .
+                       wfOpenElement( 'div' ) .
+                       wfOpenElement( 'form', array(
+                               'method' => 'post',
+                               'action' => $self->getLocalURL( "action=submit" ) ) ) .
+                       wfElement( 'input', array(
+                               'type' => 'hidden',
+                               'name' => 'target',
+                               'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
+                       wfElement( 'input', array(
+                               'type' => 'hidden',
+                               'name' => 'timestamp',
+                               'value' => $timestamp ) ) .
+                       wfElement( 'input', array(
+                               'type' => 'hidden',
+                               'name' => 'wpEditToken',
+                               'value' => $wgUser->editToken() ) ) .
+                       wfElement( 'input', array(
+                               'type' => 'hidden',
+                               'name' => 'preview',
+                               'value' => '1' ) ) .
+                       wfElement( 'input', array(
+                               'type' => 'submit',
+                               'value' => wfMsg( 'showpreview' ) ) ) .
+                       wfCloseElement( 'form' ) .
+                       wfCloseElement( 'div' ) );
        }
 
        /* private */ function showHistory() {
@@ -421,27 +464,31 @@ class UndeleteForm {
                        return;
                }
                if ( $this->mAllowed ) {
-                       $wgOut->addWikiText( wfMsg( "undeletehistory" ) . "\n----\n" . $text );
+                       $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
                } else {
                        $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
                }
 
                # List all stored revisions
                $revisions = $archive->listRevisions();
+               
+               # Batch existence check on user and talk pages
+               if( $revisions->numRows() > 0 ) {
+                       $batch = new LinkBatch();
+                       while( $row = $revisions->fetchObject() ) {
+                               $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
+                               $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
+                       }
+                       $batch->execute();
+                       $revisions->seek( 0 );
+               }
 
                if ( $this->mAllowed ) {
                        $titleObj = Title::makeTitle( NS_SPECIAL, "Undelete" );
-                       $action = $titleObj->escapeLocalURL( "action=submit" );
-                       $encTarget = htmlspecialchars( $this->mTarget );
-                       $button = htmlspecialchars( wfMsg("undeletebtn") );
-                       $token = htmlspecialchars( $wgUser->editToken() );
-
-                       $wgOut->addHTML("
-                               <form id=\"undelete\" method=\"post\" action=\"{$action}\">
-                               <input type=\"hidden\" name=\"target\" value=\"{$encTarget}\" />
-                               <input type=\"submit\" name=\"restore\" value=\"{$button}\" />
-                               <input type='hidden' name='wpEditToken' value=\"{$token}\" />
-                               ");
+                       $action = $titleObj->getLocalURL( "action=submit" );
+                       # Start the form here
+                       $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
+                       $wgOut->addHtml( $top );
                }
 
                # Show relevant lines from the deletion log:
@@ -453,9 +500,24 @@ class UndeleteForm {
                                        array( 'page' => $this->mTargetObj->getPrefixedText(),
                                               'type' => 'delete' ) ) ) );
                $logViewer->showList( $wgOut );
+               
+               $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
+               
+               if( $this->mAllowed ) {
+                       # Format the user-visible controls (comment field, submission button)
+                       # in a nice little table
+                       $table = '<fieldset><table><tr>';
+                       $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
+                       $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
+                       $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment ) . '</td>';
+                       $table .= '</tr><tr><td>&nbsp;</td><td>';
+                       $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
+                       $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
+                       $table .= '</td></tr></table></fieldset>';
+                       $wgOut->addHtml( $table );
+               }
 
                # The page's stored (deleted) history:
-               $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
                $wgOut->addHTML("<ul>");
                $target = urlencode( $this->mTarget );
                while( $row = $revisions->fetchObject() ) {
@@ -469,35 +531,32 @@ class UndeleteForm {
                                $checkBox = '';
                                $pageLink = $wgLang->timeanddate( $ts, true );
                        }
-                       $userLink = htmlspecialchars( $row->ar_user_text );
-                       if( $row->ar_user ) {
-                               $userLink = $sk->makeKnownLinkObj(
-                                       Title::makeTitle( NS_USER, $row->ar_user_text ),
-                                       $userLink );
-                       } else {
-                               $userLink = $sk->makeKnownLinkObj(
-                                       Title::makeTitle( NS_SPECIAL, 'Contributions' ),
-                                       $userLink, 'target=' . $row->ar_user_text );
-                       }
+                       $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text );
                        $comment = $sk->commentBlock( $row->ar_comment );
                        $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
 
                }
                $revisions->free();
                $wgOut->addHTML("</ul>");
+               
                if ( $this->mAllowed ) {
-                       $wgOut->addHTML( "\n</form>" );
+                       # Slip in the hidden controls here
+                       $misc  = wfHidden( 'target', $this->mTarget );
+                       $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
+                       $wgOut->addHtml( $misc . '</form>' );
                }
 
                return true;
        }
 
        function undelete() {
-               global $wgOut;
+               global $wgOut, $wgUser;
                if( !is_null( $this->mTargetObj ) ) {
                        $archive = new PageArchive( $this->mTargetObj );
-                       if( $archive->undelete( $this->mTargetTimestamp ) ) {
-                               $wgOut->addWikiText( wfMsg( "undeletedtext", $this->mTarget ) );
+                       if( $archive->undelete( $this->mTargetTimestamp, $this->mComment ) ) {
+                               $skin =& $wgUser->getSkin();
+                               $link = $skin->makeKnownLinkObj( $this->mTargetObj );
+                               $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
 
                                if (NS_IMAGE == $this->mTargetObj->getNamespace()) {
                                        /* refresh image metadata cache */
@@ -507,7 +566,7 @@ class UndeleteForm {
                                return true;
                        }
                }
-               $wgOut->fatalError( wfMsg( "cannotundelete" ) );
+               $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
                return false;
        }
 }