merged master some more
[lhc/web/wiklou.git] / includes / specials / SpecialUndelete.php
index 6dde6b9..09d5ce1 100644 (file)
@@ -32,7 +32,16 @@ class PageArchive {
         * @var Title
         */
        protected $title;
-       var $fileStatus;
+
+       /**
+        * @var Status
+        */
+       protected $fileStatus;
+
+       /**
+        * @var Status
+        */
+       protected $revisionStatus;
 
        function __construct( $title ) {
                if( is_null( $title ) ) {
@@ -112,12 +121,22 @@ class PageArchive {
         * @return ResultWrapper
         */
        function listRevisions() {
+               global $wgContentHandlerNoDB;
+
                $dbr = wfGetDB( DB_SLAVE );
+
+               $fields = array(
+                       'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
+                       'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
+               );
+
+               if ( !$wgContentHandlerNoDB ) {
+                       $fields[] = 'ar_content_format';
+                       $fields[] = 'ar_content_model';
+               }
+
                $res = $dbr->select( 'archive',
-                       array(
-                               'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
-                               'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1'
-                       ),
+                       $fields,
                        array( 'ar_namespace' => $this->title->getNamespace(),
                                   'ar_title' => $this->title->getDBkey() ),
                        __METHOD__,
@@ -174,22 +193,32 @@ class PageArchive {
         * @return Revision
         */
        function getRevision( $timestamp ) {
+               global $wgContentHandlerNoDB;
+
                $dbr = wfGetDB( DB_SLAVE );
+
+               $fields = array(
+                       'ar_rev_id',
+                       'ar_text',
+                       'ar_comment',
+                       'ar_user',
+                       'ar_user_text',
+                       'ar_timestamp',
+                       'ar_minor_edit',
+                       'ar_flags',
+                       'ar_text_id',
+                       'ar_deleted',
+                       'ar_len',
+                       'ar_sha1',
+               );
+
+               if ( !$wgContentHandlerNoDB ) {
+                       $fields[] = 'ar_content_format';
+                       $fields[] = 'ar_content_model';
+               }
+
                $row = $dbr->selectRow( 'archive',
-                       array(
-                               'ar_rev_id',
-                               'ar_text',
-                               'ar_comment',
-                               'ar_user',
-                               'ar_user_text',
-                               'ar_timestamp',
-                               'ar_minor_edit',
-                               'ar_flags',
-                               'ar_text_id',
-                               'ar_deleted',
-                               'ar_len',
-                               'ar_sha1',
-                       ),
+                       $fields,
                        array( 'ar_namespace' => $this->title->getNamespace(),
                                        'ar_title' => $this->title->getDBkey(),
                                        'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
@@ -341,7 +370,7 @@ class PageArchive {
                if( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
                        $img = wfLocalFile( $this->title );
                        $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
-                       if ( !$this->fileStatus->isOk() ) {
+                       if ( !$this->fileStatus->isOK() ) {
                                return false;
                        }
                        $filesRestored = $this->fileStatus->successCount;
@@ -350,10 +379,12 @@ class PageArchive {
                }
 
                if( $restoreText ) {
-                       $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
-                       if( $textRestored === false ) { // It must be one of UNDELETE_*
+                       $this->revisionStatus = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
+                       if( !$this->revisionStatus->isOK() ) {
                                return false;
                        }
+
+                       $textRestored = $this->revisionStatus->getValue();
                } else {
                        $textRestored = 0;
                }
@@ -401,11 +432,13 @@ class PageArchive {
         * @param $comment String
         * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
         *
-        * @return Mixed: number of revisions restored or false on failure
+        * @return Status, containing the number of revisions restored on success
         */
        private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
+               global $wgContentHandlerNoDB;
+
                if ( wfReadOnly() ) {
-                       return false;
+                       throw new ReadOnlyError();
                }
                $restoreAll = empty( $timestamps );
 
@@ -435,9 +468,14 @@ class PageArchive {
                        $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
                                array( 'rev_id' => $previousRevId ),
                                __METHOD__ );
+
                        if( $previousTimestamp === false ) {
                                wfDebug( __METHOD__.": existing page refers to a page_latest that does not exist\n" );
-                               return 0;
+
+                               $status = Status::newGood( 0 );
+                               $status->warning( 'undeleterevision-missing' );
+
+                               return $status;
                        }
                } else {
                        # Have to create a new article...
@@ -457,24 +495,31 @@ class PageArchive {
                        $oldones = "ar_timestamp IN ( {$oldts} )";
                }
 
+               $fields = array(
+                       'ar_rev_id',
+                       'ar_text',
+                       'ar_comment',
+                       'ar_user',
+                       'ar_user_text',
+                       'ar_timestamp',
+                       'ar_minor_edit',
+                       'ar_flags',
+                       'ar_text_id',
+                       'ar_deleted',
+                       'ar_page_id',
+                       'ar_len',
+                       'ar_sha1');
+
+               if ( !$wgContentHandlerNoDB ) {
+                       $fields[] = 'ar_content_format';
+                       $fields[] = 'ar_content_model';
+               }
+
                /**
                 * Select each archived revision...
                 */
                $result = $dbw->select( 'archive',
-                       /* fields */ array(
-                               'ar_rev_id',
-                               'ar_text',
-                               'ar_comment',
-                               'ar_user',
-                               'ar_user_text',
-                               'ar_timestamp',
-                               'ar_minor_edit',
-                               'ar_flags',
-                               'ar_text_id',
-                               'ar_deleted',
-                               'ar_page_id',
-                               'ar_len',
-                               'ar_sha1' ),
+                       $fields,
                        /* WHERE */ array(
                                'ar_namespace' => $this->title->getNamespace(),
                                'ar_title'     => $this->title->getDBkey(),
@@ -486,17 +531,38 @@ class PageArchive {
                $rev_count = $dbw->numRows( $result );
                if( !$rev_count ) {
                        wfDebug( __METHOD__ . ": no revisions to restore\n" );
-                       return false; // ???
+
+                       $status = Status::newGood( 0 );
+                       $status->warning( "undelete-no-results" );
+                       return $status;
                }
 
                $ret->seek( $rev_count - 1 ); // move to last
                $row = $ret->fetchObject(); // get newest archived rev
                $ret->seek( 0 ); // move back
 
+               // grab the content to check consistency with global state before restoring the page.
+               $revision = Revision::newFromArchiveRow( $row,
+                       array(
+                               'title' => $article->getTitle(), // used to derive default content model
+                       ) );
+
+               $m = $revision->getContentModel();
+
+               $user = User::newFromName( $revision->getRawUserText(), false );
+               $content = $revision->getContent( Revision::RAW );
+
+               //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
+               $status = $content->prepareSave( $article, 0, -1, $user );
+
+               if ( !$status->isOK() ) {
+                       return $status;
+               }
+
                if( $makepage ) {
                        // Check the state of the newest to-be version...
                        if( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
-                               return false; // we can't leave the current revision like this!
+                               return Status::newFatal( "undeleterevdel" );
                        }
                        // Safe to insert now...
                        $newid  = $article->insertOn( $dbw );
@@ -506,7 +572,7 @@ class PageArchive {
                        if( $row->ar_timestamp > $previousTimestamp ) {
                                // Check the state of the newest to-be version...
                                if( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
-                                       return false; // we can't leave the current revision like this!
+                                       return Status::newFatal( "undeleterevdel" );
                                }
                        }
                }
@@ -546,7 +612,7 @@ class PageArchive {
 
                // Was anything restored at all?
                if ( $restored == 0 ) {
-                       return 0;
+                       return Status::newGood( 0 );
                }
 
                $created = (bool)$newid;
@@ -566,13 +632,18 @@ class PageArchive {
                        $update->doUpdate();
                }
 
-               return $restored;
+               return Status::newGood( $restored );
        }
 
        /**
         * @return Status
         */
        function getFileStatus() { return $this->fileStatus; }
+
+       /**
+        * @return Status
+        */
+       function getRevisionStatus() { return $this->revisionStatus; }
 }
 
 /**
@@ -855,12 +926,15 @@ class SpecialUndelete extends SpecialPage {
 
                if( $this->mPreview ) {
                        // Hide [edit]s
+                       //FIXME: ContentHandler will have to provide some specialized magic to do this
                        $popts = $out->parserOptions();
                        $popts->setEditSection( false );
                        $out->parserOptions( $popts );
                        $out->addWikiTextTitleTidy( $rev->getText( Revision::FOR_THIS_USER, $user ), $this->mTargetObj, true );
                }
 
+               //FIXME: ContentHandler will have to provide some specialized magic for reviewing content before undeletion
+
                $out->addHTML(
                        Xml::element( 'textarea', array(
                                        'readonly' => 'readonly',
@@ -904,7 +978,7 @@ class SpecialUndelete extends SpecialPage {
         * @return String: HTML
         */
        function showDiff( $previousRev, $currentRev ) {
-               $diffEngine = new DifferenceEngine( $this->getContext() );
+               $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $this->getContext() );
                $diffEngine->showDiffStyle();
                $this->getOutput()->addHTML(
                        "<div>" .
@@ -921,9 +995,9 @@ class SpecialUndelete extends SpecialPage {
                                $this->diffHeader( $currentRev, 'n' ) .
                                "</td>\n" .
                        "</tr>" .
-                       $diffEngine->generateDiffBody(
-                               $previousRev->getText( Revision::FOR_THIS_USER, $this->getUser() ),
-                               $currentRev->getText( Revision::FOR_THIS_USER, $this->getUser() ) ) .
+                       $diffEngine->generateContentDiffBody(
+                               $previousRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ),
+                               $currentRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ) ) .
                        "</table>" .
                        "</div>\n"
                );
@@ -1417,11 +1491,15 @@ class SpecialUndelete extends SpecialPage {
                        $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
                } else {
                        $out->setPageTitle( $this->msg( 'undelete-error' ) );
-                       $out->addWikiMsg( 'cannotundelete' );
-                       $out->addWikiMsg( 'undeleterevdel' );
                }
 
-               // Show file deletion warnings and errors
+               // Show revision undeletion warnings and errors
+               $status = $archive->getRevisionStatus();
+               if( $status && !$status->isGood() ) {
+                       $out->addWikiText( '<div class="error">' . $status->getWikiText( 'cannotundelete', 'cannotundelete' ) . '</div>' );
+               }
+
+               // Show file undeletion warnings and errors
                $status = $archive->getFileStatus();
                if( $status && !$status->isGood() ) {
                        $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );