Implement redirects in CssContent
authorKunal Mehta <legoktm@gmail.com>
Sun, 19 Jul 2015 16:16:16 +0000 (11:16 -0500)
committerOri.livneh <ori@wikimedia.org>
Tue, 28 Jul 2015 17:53:47 +0000 (17:53 +0000)
Just like ad9f14d662f959 which was for JavaScript. The redirect will be
of the form "/* #REDIRECT */@import url(...);".

Bug: T73201
Bug: T35973
Change-Id: I10bae44af4b4923f8797172702974cd45dc25ab4

includes/content/CssContent.php
includes/content/CssContentHandler.php
tests/phpunit/includes/content/CssContentHandlerTest.php [new file with mode: 0644]
tests/phpunit/includes/content/CssContentTest.php

index 8290603..b4f5196 100644 (file)
  */
 class CssContent extends TextContent {
 
+       /**
+        * @var bool|Title|null
+        */
+       private $redirectTarget = false;
+
        /**
         * @param string $text CSS code.
         * @param string $modelId the content content model
@@ -74,4 +79,43 @@ class CssContent extends TextContent {
                return $html;
        }
 
+       /**
+        * @param Title $target
+        * @return CssContent
+        */
+       public function updateRedirect( Title $target ) {
+               if ( !$this->isRedirect() ) {
+                       return $this;
+               }
+
+               return $this->getContentHandler()->makeRedirectContent( $target );
+       }
+
+       /**
+        * @return Title|null
+        */
+       public function getRedirectTarget() {
+               if ( $this->redirectTarget !== false ) {
+                       return $this->redirectTarget;
+               }
+               $this->redirectTarget = null;
+               $text = $this->getNativeData();
+               if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
+                       // Extract the title from the url
+                       preg_match( '/title=(.*?)&action=raw/', $text, $matches );
+                       if ( isset( $matches[1] ) ) {
+                               $title = Title::newFromText( $matches[1] );
+                               if ( $title ) {
+                                       // Have a title, check that the current content equals what
+                                       // the redirect content should be
+                                       if ( $this->equals( $this->getContentHandler()->makeRedirectContent( $title ) ) ) {
+                                               $this->redirectTarget = $title;
+                                       }
+                               }
+                       }
+               }
+
+               return $this->redirectTarget;
+       }
+
 }
index b2a8676..dfa01e2 100644 (file)
@@ -39,4 +39,23 @@ class CssContentHandler extends CodeContentHandler {
        protected function getContentClass() {
                return 'CssContent';
        }
+
+       public function supportsRedirects() {
+               return true;
+       }
+
+       /**
+        * Create a redirect that is also valid CSS
+        *
+        * @param Title $destination
+        * @param string $text ignored
+        * @return JavaScriptContent
+        */
+       public function makeRedirectContent( Title $destination, $text = '' ) {
+               // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
+               $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
+               $class = $this->getContentClass();
+               return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
+       }
+
 }
diff --git a/tests/phpunit/includes/content/CssContentHandlerTest.php b/tests/phpunit/includes/content/CssContentHandlerTest.php
new file mode 100644 (file)
index 0000000..e1785a9
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+class CssContentHandlerTest extends MediaWikiTestCase {
+
+       /**
+        * @dataProvider provideMakeRedirectContent
+        * @covers CssContentHandler::makeRedirectContent
+        */
+       public function testMakeRedirectContent( $title, $expected ) {
+               $this->setMwGlobals( array(
+                       'wgServer' => '//example.org',
+                       'wgScript' => '/w/index.php',
+               ) );
+               $ch = new CssContentHandler();
+               $content = $ch->makeRedirectContent( Title::newFromText( $title ) );
+               $this->assertInstanceOf( 'CssContent', $content );
+               $this->assertEquals( $expected, $content->serialize( CONTENT_FORMAT_CSS ) );
+       }
+
+       /**
+        * Keep this in sync with CssContentTest::provideGetRedirectTarget()
+        */
+       public static function provideMakeRedirectContent() {
+               return array(
+                       array( 'MediaWiki:MonoBook.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ),
+                       array( 'User:FooBar/common.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ),
+                       array( 'Gadget:FooBaz.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ),
+               );
+       }
+}
index 44427ba..c4d87c2 100644 (file)
@@ -83,11 +83,31 @@ class CssContentTest extends JavaScriptContentTest {
        }
 
        /**
-        * Override this since CssContent does not support redirects yet
+        * @dataProvider provideGetRedirectTarget
+        */
+       public function testGetRedirectTarget( $title, $text ) {
+               $this->setMwGlobals( array(
+                       'wgServer' => '//example.org',
+                       'wgScriptPath' => '/w',
+                       'wgScript' => '/w/index.php',
+               ) );
+               $content = new CssContent( $text );
+               $target = $content->getRedirectTarget();
+               $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
+       }
+
+       /**
+        * Keep this in sync with CssContentHandlerTest::provideMakeRedirectContent()
         */
        public static function provideGetRedirectTarget() {
                return array(
-                       array( null, '' ),
+                       array( 'MediaWiki:MonoBook.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ),
+                       array( 'User:FooBar/common.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ),
+                       array( 'Gadget:FooBaz.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ),
+                       # No #REDIRECT comment
+                       array( null, "@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ),
+                       # Wrong domain
+                       array( null, "/* #REDIRECT */@import url(//example.com/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ),
                );
        }