Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/225709

Change subject: [WIP] Implement redirects in CssContent
......................................................................

[WIP] Implement redirects in CssContent

Not redirecting properly yet?

Bug: T73201
Bug: 33973
Change-Id: I10bae44af4b4923f8797172702974cd45dc25ab4
---
M includes/content/CssContent.php
M includes/content/CssContentHandler.php
A tests/phpunit/includes/content/CssContentHandlerTest.php
M tests/phpunit/includes/content/CssContentTest.php
4 files changed, 114 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/09/225709/1

diff --git a/includes/content/CssContent.php b/includes/content/CssContent.php
index 8290603..b4f5196 100644
--- a/includes/content/CssContent.php
+++ b/includes/content/CssContent.php
@@ -33,6 +33,11 @@
 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 @@
                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;
+       }
+
 }
diff --git a/includes/content/CssContentHandler.php 
b/includes/content/CssContentHandler.php
index b2a8676..c53404f 100644
--- a/includes/content/CssContentHandler.php
+++ b/includes/content/CssContentHandler.php
@@ -39,4 +39,23 @@
        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 url('$url');" );
+       }
+
 }
diff --git a/tests/phpunit/includes/content/CssContentHandlerTest.php 
b/tests/phpunit/includes/content/CssContentHandlerTest.php
new file mode 100644
index 0000000..80571fb
--- /dev/null
+++ b/tests/phpunit/includes/content/CssContentHandlerTest.php
@@ -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');"
 ),
+               );
+       }
+}
diff --git a/tests/phpunit/includes/content/CssContentTest.php 
b/tests/phpunit/includes/content/CssContentTest.php
index 44427ba..298673e 100644
--- a/tests/phpunit/includes/content/CssContentTest.php
+++ b/tests/phpunit/includes/content/CssContentTest.php
@@ -83,11 +83,30 @@
        }
 
        /**
-        * Override this since CssContent does not support redirects yet
+        * @dataProvider provideGetRedirectTarget
+        */
+       public function testGetRedirectTarget( $title, $text ) {
+               $this->setMwGlobals( array(
+                       'wgServer' => '//example.org',
+                       'wgScriptPath' => '/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');"
 ),
                );
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I10bae44af4b4923f8797172702974cd45dc25ab4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>

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

Reply via email to