SpecialWatchlist: JS enhancements to namespace selector (like RC)
[lhc/web/wiklou.git] / includes / EditPage.php
index c0e7ed2..7115eab 100644 (file)
@@ -319,6 +319,18 @@ class EditPage {
                }
        }
 
+       /**
+        * Returns if the given content model is editable.
+        *
+        * @param string $modelId The ID of the content model to test. Use CONTENT_MODEL_XXX constants.
+        * @return bool
+        * @throws MWException if $modelId has no known handler
+        */
+       public function isSupportedContentModel( $modelId ) {
+               return $this->allowNonTextContent ||
+                       ContentHandler::getForModelID( $modelId ) instanceof TextContentHandler;
+       }
+
        function submit() {
                $this->edit();
        }
@@ -613,6 +625,7 @@ class EditPage {
        /**
         * This function collects the form data and uses it to populate various member variables.
         * @param $request WebRequest
+        * @throws ErrorPageError
         */
        function importFormData( &$request ) {
                global $wgContLang, $wgUser;
@@ -777,12 +790,17 @@ class EditPage {
                $this->bot = $request->getBool( 'bot', true );
                $this->nosummary = $request->getBool( 'nosummary' );
 
-               $content_handler = ContentHandler::getForTitle( $this->mTitle );
-               $this->contentModel = $request->getText( 'model', $content_handler->getModelID() ); #may be overridden by revision
-               $this->contentFormat = $request->getText( 'format', $content_handler->getDefaultFormat() ); #may be overridden by revision
+               $this->contentModel = $request->getText( 'model', $this->contentModel ); #may be overridden by revision
+               $this->contentFormat = $request->getText( 'format', $this->contentFormat ); #may be overridden by revision
 
+               if ( !ContentHandler::getForModelID( $this->contentModel )->isSupportedFormat( $this->contentFormat ) ) {
+                       throw new ErrorPageError(
+                               'editpage-notsupportedcontentformat-title',
+                               'editpage-notsupportedcontentformat-text',
+                               array( $this->contentFormat, ContentHandler::getLocalizedName( $this->contentModel ) )
+                       );
+               }
                #TODO: check if the desired model is allowed in this namespace, and if a transition from the page's current model to the new model is allowed
-               #TODO: check if the desired content model supports the given content format!
 
                $this->live = $request->getCheck( 'live' );
                $this->editintro = $request->getText( 'editintro',
@@ -1913,12 +1931,10 @@ class EditPage {
        function getBaseRevision() {
                if ( !$this->mBaseRevision ) {
                        $db = wfGetDB( DB_MASTER );
-                       $baseRevision = Revision::loadFromTimestamp(
+                       $this->mBaseRevision = Revision::loadFromTimestamp(
                                $db, $this->mTitle, $this->edittime );
-                       return $this->mBaseRevision = $baseRevision;
-               } else {
-                       return $this->mBaseRevision;
                }
+               return $this->mBaseRevision;
        }
 
        /**
@@ -2128,9 +2144,9 @@ class EditPage {
                        return $content;
                }
 
-               if ( !$this->allowNonTextContent && !( $content instanceof TextContent ) ) {
-                       throw new MWException( "This content model can not be edited as text: "
-                                                               . ContentHandler::getLocalizedName( $content->getModel() ) );
+               if ( !$this->isSupportedContentModel( $content->getModel() ) ) {
+                       throw new MWException( 'This content model is not supported: '
+                               . ContentHandler::getLocalizedName( $content->getModel() ) );
                }
 
                return $content->serialize( $this->contentFormat );
@@ -2158,8 +2174,8 @@ class EditPage {
                $content = ContentHandler::makeContent( $text, $this->getTitle(),
                        $this->contentModel, $this->contentFormat );
 
-               if ( !$this->allowNonTextContent && !( $content instanceof TextContent ) ) {
-                       throw new MWException( "This content model can not be edited as text: "
+               if ( !$this->isSupportedContentModel( $content->getModel() ) ) {
+                       throw new MWException( 'This content model is not supported: '
                                . ContentHandler::getLocalizedName( $content->getModel() ) );
                }
 
@@ -2476,7 +2492,9 @@ class EditPage {
                        }
                }
 
-               if ( $this->mTitle->getNamespace() != NS_MEDIAWIKI && $this->mTitle->isProtected( 'edit' ) ) {
+               if ( $this->mTitle->isProtected( 'edit' ) &&
+                       MWNamespace::getRestrictionLevels( $this->mTitle->getNamespace() ) !== array( '' )
+               ) {
                        # Is the title semi-protected?
                        if ( $this->mTitle->isSemiProtected() ) {
                                $noticeMsg = 'semiprotectedpagewarning';
@@ -2678,7 +2696,9 @@ HTML
                        $attribs = array( 'style' => 'display:none;' );
                } else {
                        $classes = array(); // Textarea CSS
-                       if ( $this->mTitle->getNamespace() != NS_MEDIAWIKI && $this->mTitle->isProtected( 'edit' ) ) {
+                       if ( $this->mTitle->isProtected( 'edit' ) &&
+                               MWNamespace::getRestrictionLevels( $this->mTitle->getNamespace() ) !== array( '' )
+                       ) {
                                # Is the title semi-protected?
                                if ( $this->mTitle->isSemiProtected() ) {
                                        $classes[] = 'mw-textarea-sprotected';
@@ -3220,36 +3240,30 @@ HTML
                                }
                        }
 
-                       $rt = $content->getRedirectChain();
-                       if ( $rt ) {
-                               $previewHTML = $this->mArticle->viewRedirect( $rt, false );
-                       } else {
-
-                               # If we're adding a comment, we need to show the
-                               # summary as the headline
-                               if ( $this->section === "new" && $this->summary !== "" ) {
-                                       $content = $content->addSectionHeader( $this->summary );
-                               }
+                       # If we're adding a comment, we need to show the
+                       # summary as the headline
+                       if ( $this->section === "new" && $this->summary !== "" ) {
+                               $content = $content->addSectionHeader( $this->summary );
+                       }
 
-                               $hook_args = array( $this, &$content );
-                               ContentHandler::runLegacyHooks( 'EditPageGetPreviewText', $hook_args );
-                               wfRunHooks( 'EditPageGetPreviewContent', $hook_args );
+                       $hook_args = array( $this, &$content );
+                       ContentHandler::runLegacyHooks( 'EditPageGetPreviewText', $hook_args );
+                       wfRunHooks( 'EditPageGetPreviewContent', $hook_args );
 
-                               $parserOptions->enableLimitReport();
+                       $parserOptions->enableLimitReport();
 
-                               # For CSS/JS pages, we should have called the ShowRawCssJs hook here.
-                               # But it's now deprecated, so never mind
+                       # For CSS/JS pages, we should have called the ShowRawCssJs hook here.
+                       # But it's now deprecated, so never mind
 
-                               $content = $content->preSaveTransform( $this->mTitle, $wgUser, $parserOptions );
-                               $parserOutput = $content->getParserOutput( $this->getArticle()->getTitle(), null, $parserOptions );
+                       $content = $content->preSaveTransform( $this->mTitle, $wgUser, $parserOptions );
+                       $parserOutput = $content->getParserOutput( $this->getArticle()->getTitle(), null, $parserOptions );
 
-                               $previewHTML = $parserOutput->getText();
-                               $this->mParserOutput = $parserOutput;
-                               $wgOut->addParserOutputNoText( $parserOutput );
+                       $previewHTML = $parserOutput->getText();
+                       $this->mParserOutput = $parserOutput;
+                       $wgOut->addParserOutputNoText( $parserOutput );
 
-                               if ( count( $parserOutput->getWarnings() ) ) {
-                                       $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() );
-                               }
+                       if ( count( $parserOutput->getWarnings() ) ) {
+                               $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() );
                        }
                } catch ( MWContentSerializationException $ex ) {
                        $m = wfMessage( 'content-failed-to-parse', $this->contentModel, $this->contentFormat, $ex->getMessage() );