Ladsgroup has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/384096 )

Change subject: Use ContentHandler to get proper MIME type in RawAction
......................................................................

Use ContentHandler to get proper MIME type in RawAction

Bug: T178060
Change-Id: Ief135af917b58ea119b1ed1ee21acb187170ba5e
---
M includes/actions/RawAction.php
M includes/content/ContentHandler.php
M includes/content/CssContentHandler.php
M includes/content/JavaScriptContentHandler.php
M includes/content/JsonContentHandler.php
5 files changed, 41 insertions(+), 19 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/96/384096/1

diff --git a/includes/actions/RawAction.php b/includes/actions/RawAction.php
index d8c8bc3..f058570 100644
--- a/includes/actions/RawAction.php
+++ b/includes/actions/RawAction.php
@@ -33,6 +33,9 @@
  * @ingroup Actions
  */
 class RawAction extends FormlessAction {
+
+       private $contentType;
+
        public function getName() {
                return 'raw';
        }
@@ -64,12 +67,16 @@
                        $this->gen = true;
                }
 
-               $contentType = $this->getContentType();
+               $text = $this->getRawText();
+               // Sanity check
+               if ( $this->contentType === null ) {
+                       $this->contentType = 'text/x-wiki';
+               }
 
                $maxage = $request->getInt( 'maxage', $config->get( 
'SquidMaxage' ) );
                $smaxage = $request->getIntOrNull( 'smaxage' );
                if ( $smaxage === null ) {
-                       if ( $contentType == 'text/css' || $contentType == 
'text/javascript' ) {
+                       if ( $this->contentType == 'text/css' || 
$this->contentType == 'text/javascript' ) {
                                // CSS/JS raw content has its own CDN max age 
configuration.
                                // Note: Title::getCdnUrls() includes 
action=raw for css/js pages,
                                // so if using the canonical url, this will get 
HTCP purges.
@@ -86,7 +93,7 @@
                        $response->header( $this->getOutput()->getKeyHeader() );
                }
 
-               $response->header( 'Content-type: ' . $contentType . '; 
charset=UTF-8' );
+               $response->header( 'Content-type: ' . $this->contentType . '; 
charset=UTF-8' );
                // Output may contain user-specific data;
                // vary generated content for open sessions on private wikis
                $privateCache = !User::isEveryoneAllowed( 'read' ) &&
@@ -98,13 +105,11 @@
                        'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . 
', max-age=' . $maxage
                );
 
-               $text = $this->getRawText();
-
                // Don't return a 404 response for CSS or JavaScript;
                // 404s aren't generally cached and it would create
                // extra hits when user CSS/JS are on and the user doesn't
                // have the pages.
-               if ( $text === false && $contentType == 'text/x-wiki' ) {
+               if ( $text === false && $this->contentType == 'text/x-wiki' ) {
                        $response->statusHeader( 404 );
                }
 
@@ -161,6 +166,7 @@
                                } else {
                                        // want a section?
                                        $section = $request->getIntOrNull( 
'section' );
+                                       $this->updateContentType( $content );
                                        if ( $section !== null ) {
                                                $content = 
$content->getSection( $section );
                                        }
@@ -219,28 +225,25 @@
                return $oldid;
        }
 
-       /**
-        * Get the content type to use for the response
-        *
-        * @return string
-        */
-       public function getContentType() {
-               $ctype = $this->getRequest()->getVal( 'ctype' );
+       private function updateContentType( Content $content ) {
+               $cType = $this->getRequest()->getVal( 'ctype' );
 
-               if ( $ctype == '' ) {
+               if ( $cType == '' ) {
                        $gen = $this->getRequest()->getVal( 'gen' );
                        if ( $gen == 'js' ) {
-                               $ctype = 'text/javascript';
+                               $cType = 'text/javascript';
                        } elseif ( $gen == 'css' ) {
-                               $ctype = 'text/css';
+                               $cType = 'text/css';
                        }
                }
 
                $allowedCTypes = [ 'text/x-wiki', 'text/javascript', 
'text/css', 'application/x-zope-edit' ];
-               if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
-                       $ctype = 'text/x-wiki';
+               if ( $cType == '' || !in_array( $cType, $allowedCTypes ) ) {
+                       $contentHandler = ContentHandler::getForContent( 
$content );
+                       $cType = $contentHandler->getMIMEType();
                }
 
-               return $ctype;
+               $this->contentType = $cType;
        }
+
 }
diff --git a/includes/content/ContentHandler.php 
b/includes/content/ContentHandler.php
index 0509e29..e21f107 100644
--- a/includes/content/ContentHandler.php
+++ b/includes/content/ContentHandler.php
@@ -47,6 +47,12 @@
  * @ingroup Content
  */
 abstract class ContentHandler {
+
+       /**
+        * @var string
+        */
+       protected $MIMEType = 'text/x-wiki';
+
        /**
         * Convenience function for getting flat text from a Content object. 
This
         * should only be used in the context of backwards compatibility with 
code
@@ -1210,4 +1216,11 @@
                return $parserOutput;
        }
 
+       /**
+        * @return string
+        */
+       public function getMIMEType() {
+               return $this->MIMEType;
+       }
+
 }
diff --git a/includes/content/CssContentHandler.php 
b/includes/content/CssContentHandler.php
index 9c11035..9abd529 100644
--- a/includes/content/CssContentHandler.php
+++ b/includes/content/CssContentHandler.php
@@ -29,6 +29,8 @@
  */
 class CssContentHandler extends CodeContentHandler {
 
+       protected $MIMEType = 'text/css';
+
        /**
         * @param string $modelId
         */
diff --git a/includes/content/JavaScriptContentHandler.php 
b/includes/content/JavaScriptContentHandler.php
index 9abad3e..b92e659 100644
--- a/includes/content/JavaScriptContentHandler.php
+++ b/includes/content/JavaScriptContentHandler.php
@@ -28,6 +28,8 @@
  */
 class JavaScriptContentHandler extends CodeContentHandler {
 
+       protected $MIMEType = 'text/javascript';
+
        /**
         * @param string $modelId
         */
diff --git a/includes/content/JsonContentHandler.php 
b/includes/content/JsonContentHandler.php
index edb21f6..a81a44e 100644
--- a/includes/content/JsonContentHandler.php
+++ b/includes/content/JsonContentHandler.php
@@ -29,6 +29,8 @@
  */
 class JsonContentHandler extends CodeContentHandler {
 
+       protected $MIMEType = 'application/json';
+
        public function __construct( $modelId = CONTENT_MODEL_JSON ) {
                parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] );
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/384096
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ief135af917b58ea119b1ed1ee21acb187170ba5e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to