From 3a28ee5acb7d8ccfa5d80766f2bd764d14902f24 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Mon, 15 Sep 2014 04:17:29 -0400 Subject: [PATCH] CSS/JSON/JavaScript ContentHandler refactoring * All content handlers that deal with code/data tend to have English as their page language & pageview language, so moved common code to the abstract CodeContentHandler class. * Renamed JSONContent & JSONContentHandler into JsonContent* Change-Id: I46819a0572ef5becc211d0d82471ff7102edaa3c --- RELEASE-NOTES-1.24 | 2 +- includes/AutoLoader.php | 15 +++-- includes/DefaultSettings.php | 2 +- includes/content/CodeContentHandler.php | 65 +++++++++++++++++++ includes/content/CssContentHandler.php | 31 +-------- includes/content/JSONContentHandler.php | 54 --------------- includes/content/JavaScriptContentHandler.php | 31 +-------- .../{JSONContent.php => JsonContent.php} | 4 +- includes/content/JsonContentHandler.php | 26 ++++++++ includes/content/MessageContent.php | 2 +- ...SONContentTest.php => JsonContentTest.php} | 14 ++-- 11 files changed, 113 insertions(+), 133 deletions(-) create mode 100644 includes/content/CodeContentHandler.php delete mode 100644 includes/content/JSONContentHandler.php rename includes/content/{JSONContent.php => JsonContent.php} (97%) create mode 100644 includes/content/JsonContentHandler.php rename tests/phpunit/includes/content/{JSONContentTest.php => JsonContentTest.php} (90%) diff --git a/RELEASE-NOTES-1.24 b/RELEASE-NOTES-1.24 index 33fe0d0091..c5766d0c07 100644 --- a/RELEASE-NOTES-1.24 +++ b/RELEASE-NOTES-1.24 @@ -174,7 +174,7 @@ production. * (bug 15484) Users will now be redirected to the login page when they need to log in, rather than being shown a page asking them to log in and having to click another link to actually get to the login page. -* A JSONContent and JSONContentHandler were added for extensions to extend. +* A JsonContent and JsonContentHandler were added for extensions to extend. * (bug 35045) Redirects to sections will now update the URL in browser's address bar using the HTML5 History API. When [[Dog]] redirects to [[Animals#Dog]], the user will now see "Animals#Dog" in their browser instead of "Dog#Dog". diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 5242ec0f65..7c5de2ac6f 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -375,20 +375,21 @@ $wgAutoloadLocalClasses = array( # includes/content 'AbstractContent' => 'includes/content/AbstractContent.php', - 'ContentHandler' => 'includes/content/ContentHandler.php', + 'CodeContentHandler' => 'includes/content/CodeContentHandler.php', 'Content' => 'includes/content/Content.php', - 'CssContentHandler' => 'includes/content/CssContentHandler.php', + 'ContentHandler' => 'includes/content/ContentHandler.php', 'CssContent' => 'includes/content/CssContent.php', - 'JavaScriptContentHandler' => 'includes/content/JavaScriptContentHandler.php', + 'CssContentHandler' => 'includes/content/CssContentHandler.php', 'JavaScriptContent' => 'includes/content/JavaScriptContent.php', - 'JSONContentHandler' => 'includes/content/JSONContentHandler.php', - 'JSONContent' => 'includes/content/JSONContent.php', + 'JavaScriptContentHandler' => 'includes/content/JavaScriptContentHandler.php', + 'JsonContent' => 'includes/content/JsonContent.php', + 'JsonContentHandler' => 'includes/content/JsonContentHandler.php', 'MessageContent' => 'includes/content/MessageContent.php', 'MWContentSerializationException' => 'includes/content/ContentHandler.php', - 'TextContentHandler' => 'includes/content/TextContentHandler.php', 'TextContent' => 'includes/content/TextContent.php', - 'WikitextContentHandler' => 'includes/content/WikitextContentHandler.php', + 'TextContentHandler' => 'includes/content/TextContentHandler.php', 'WikitextContent' => 'includes/content/WikitextContent.php', + 'WikitextContentHandler' => 'includes/content/WikitextContentHandler.php', # includes/context 'ContextSource' => 'includes/context/ContextSource.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 73179cf1a1..8b29733706 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -859,7 +859,7 @@ $wgContentHandlers = array( // dumb version, no syntax highlighting CONTENT_MODEL_JAVASCRIPT => 'JavaScriptContentHandler', // simple implementation, for use by extensions, etc. - CONTENT_MODEL_JSON => 'JSONContentHandler', + CONTENT_MODEL_JSON => 'JsonContentHandler', // dumb version, no syntax highlighting CONTENT_MODEL_CSS => 'CssContentHandler', // plain text, for use by extensions, etc. diff --git a/includes/content/CodeContentHandler.php b/includes/content/CodeContentHandler.php new file mode 100644 index 0000000000..447a2a732a --- /dev/null +++ b/includes/content/CodeContentHandler.php @@ -0,0 +1,65 @@ + - * @author Kunal Mehta - */ - -/** - * @since 1.24 - */ -class JSONContentHandler extends TextContentHandler { - - public function __construct( $modelId = CONTENT_MODEL_JSON ) { - parent::__construct( $modelId, array( CONTENT_FORMAT_JSON ) ); - } - - /** - * @return string - */ - protected function getContentClass() { - return 'JSONContent'; - } - - /** - * Returns the english language, because JSON is english, and should be handled as such. - * - * @param Title $title - * @param Content|null $content - * - * @return Language Return of wfGetLangObj( 'en' ) - * - * @see ContentHandler::getPageLanguage() - */ - public function getPageLanguage( Title $title, Content $content = null ) { - return wfGetLangObj( 'en' ); - } - - /** - * Returns the english language, because JSON is english, and should be handled as such. - * - * @param Title $title - * @param Content|null $content - * - * @return Language Return of wfGetLangObj( 'en' ) - * - * @see ContentHandler::getPageLanguage() - */ - public function getPageViewLanguage( Title $title, Content $content = null ) { - return wfGetLangObj( 'en' ); - } -} diff --git a/includes/content/JavaScriptContentHandler.php b/includes/content/JavaScriptContentHandler.php index 8d62e2a3ad..457b83d78a 100644 --- a/includes/content/JavaScriptContentHandler.php +++ b/includes/content/JavaScriptContentHandler.php @@ -27,7 +27,7 @@ * @ingroup Content * @todo make ScriptContentHandler base class, do highlighting stuff there? */ -class JavaScriptContentHandler extends TextContentHandler { +class JavaScriptContentHandler extends CodeContentHandler { /** * @param string $modelId @@ -39,33 +39,4 @@ class JavaScriptContentHandler extends TextContentHandler { protected function getContentClass() { return 'JavaScriptContent'; } - - /** - * Returns the english language, because JS is english, and should be handled as such. - * - * @param Title $title - * @param Content $content - * - * @return Language Return of wfGetLangObj( 'en' ) - * - * @see ContentHandler::getPageLanguage() - */ - public function getPageLanguage( Title $title, Content $content = null ) { - return wfGetLangObj( 'en' ); - } - - /** - * Returns the english language, because JS is english, and should be handled as such. - * - * @param Title $title - * @param Content $content - * - * @return Language Return of wfGetLangObj( 'en' ) - * - * @see ContentHandler::getPageViewLanguage() - */ - public function getPageViewLanguage( Title $title, Content $content = null ) { - return wfGetLangObj( 'en' ); - } - } diff --git a/includes/content/JSONContent.php b/includes/content/JsonContent.php similarity index 97% rename from includes/content/JSONContent.php rename to includes/content/JsonContent.php index e563780159..b36827c507 100644 --- a/includes/content/JSONContent.php +++ b/includes/content/JsonContent.php @@ -12,7 +12,7 @@ * Represents the content of a JSON content. * @since 1.24 */ -class JSONContent extends TextContent { +class JsonContent extends TextContent { public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) { parent::__construct( $text, $modelId ); @@ -52,7 +52,7 @@ class JSONContent extends TextContent { * @param Title $title Title * @param User $user User * @param ParserOptions $popts - * @return JSONContent + * @return JsonContent */ public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { return new static( $this->beautifyJSON() ); diff --git a/includes/content/JsonContentHandler.php b/includes/content/JsonContentHandler.php new file mode 100644 index 0000000000..392ce37beb --- /dev/null +++ b/includes/content/JsonContentHandler.php @@ -0,0 +1,26 @@ + + * @author Kunal Mehta + */ + +/** + * @since 1.24 + */ +class JsonContentHandler extends CodeContentHandler { + + public function __construct( $modelId = CONTENT_MODEL_JSON ) { + parent::__construct( $modelId, array( CONTENT_FORMAT_JSON ) ); + } + + /** + * @return string + */ + protected function getContentClass() { + return 'JsonContent'; + } +} diff --git a/includes/content/MessageContent.php b/includes/content/MessageContent.php index 2240669dfa..5b84657f99 100644 --- a/includes/content/MessageContent.php +++ b/includes/content/MessageContent.php @@ -29,7 +29,7 @@ * Wrapper allowing us to handle a system message as a Content object. * Note that this is generally *not* used to represent content from the * MediaWiki namespace, and that there is no MessageContentHandler. - * MessageContent is just intended as glue for wrapping a message programatically. + * MessageContent is just intended as glue for wrapping a message programmatically. * * @ingroup Content */ diff --git a/tests/phpunit/includes/content/JSONContentTest.php b/tests/phpunit/includes/content/JsonContentTest.php similarity index 90% rename from tests/phpunit/includes/content/JSONContentTest.php rename to tests/phpunit/includes/content/JsonContentTest.php index acfdc0e59f..6c77d1aaf0 100644 --- a/tests/phpunit/includes/content/JSONContentTest.php +++ b/tests/phpunit/includes/content/JsonContentTest.php @@ -2,15 +2,15 @@ /** * @author Adam Shorland - * @covers JSONContent + * @covers JsonContent */ -class JSONContentTest extends MediaWikiLangTestCase { +class JsonContentTest extends MediaWikiLangTestCase { /** * @dataProvider provideValidConstruction */ public function testValidConstruct( $text, $modelId, $isValid, $expected ) { - $obj = new JSONContent( $text, $modelId ); + $obj = new JsonContent( $text, $modelId ); $this->assertEquals( $isValid, $obj->isValid() ); $this->assertEquals( $expected, $obj->getJsonData() ); } @@ -27,7 +27,7 @@ class JSONContentTest extends MediaWikiLangTestCase { * @dataProvider provideDataToEncode */ public function testBeautifyUsesFormatJson( $data ) { - $obj = new JSONContent( FormatJson::encode( $data ) ); + $obj = new JsonContent( FormatJson::encode( $data ) ); $this->assertEquals( FormatJson::encode( $data, true ), $obj->beautifyJSON() ); } @@ -45,9 +45,9 @@ class JSONContentTest extends MediaWikiLangTestCase { * @dataProvider provideDataToEncode */ public function testPreSaveTransform( $data ) { - $obj = new JSONContent( FormatJson::encode( $data ) ); + $obj = new JsonContent( FormatJson::encode( $data ) ); $newObj = $obj->preSaveTransform( $this->getMockTitle(), $this->getMockUser(), $this->getMockParserOptions() ); - $this->assertTrue( $newObj->equals( new JSONContent( FormatJson::encode( $data, true ) ) ) ); + $this->assertTrue( $newObj->equals( new JsonContent( FormatJson::encode( $data, true ) ) ) ); } private function getMockTitle() { @@ -71,7 +71,7 @@ class JSONContentTest extends MediaWikiLangTestCase { * @dataProvider provideDataAndParserText */ public function testFillParserOutput( $data, $expected ) { - $obj = new JSONContent( FormatJson::encode( $data ) ); + $obj = new JsonContent( FormatJson::encode( $data ) ); $parserOutput = $obj->getParserOutput( $this->getMockTitle(), null, null, true ); $this->assertInstanceOf( 'ParserOutput', $parserOutput ); // var_dump( $parserOutput->getText(), "\n" ); -- 2.20.1