From 810ff1543f88471aa7271fb44d3c9f118100edf9 Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 19 Jun 2012 14:45:25 +0200 Subject: [PATCH] support symbolic name for content models Change-Id: I4eaed3db7fb10069f73d84514abd2f6957019153 --- includes/ContentHandler.php | 51 +++++++++++++--- languages/messages/MessagesEn.php | 10 +-- languages/messages/MessagesQqq.php | 10 +-- tests/phpunit/includes/ContentHandlerTest.php | 61 +++++++++++++++++-- tests/phpunit/suite.xml | 4 +- 5 files changed, 110 insertions(+), 26 deletions(-) diff --git a/includes/ContentHandler.php b/includes/ContentHandler.php index 63fa47df48..7bf33c5d99 100644 --- a/includes/ContentHandler.php +++ b/includes/ContentHandler.php @@ -360,28 +360,46 @@ abstract class ContentHandler { } /** - * Returns the localized name for a given content model, - * or null if no MIME type is known. + * Returns the symbolic name for a given content model. + * + * @param $id int The content model ID, as given by a CONTENT_MODEL_XXX + * constant or returned by Revision::getContentModel(). + * + * @return string The content model's symbolic name. + * @throws MWException if the model id isn't known. + */ + public static function getContentModelName( $id ) { + $handler = self::getForModelID( $id ); + return $handler->getModelName(); + } + + + /** + * Returns the localized name for a given content model. * * Model names are localized using system messages. Message keys - * have the form content-model-$id. + * have the form content-model-$name, where $name is getContentModelName( $id ). * * @static * @param $id int The content model ID, as given by a CONTENT_MODEL_XXX * constant or returned by Revision::getContentModel(). + * @todo also accept a symbolic name instead of a numeric id * - * @return string|null The content format's MIME type. + * @return string The content format's localized name. + * @throws MWException if the model id isn't known. */ - public static function getContentModelName( $id ) { - $key = "content-model-$id"; + public static function getLocalizedName( $id ) { + $name = self::getContentModelName( $id ); + $key = "content-model-$name"; - if ( wfEmptyMsg( $key ) ) return null; + if ( wfEmptyMsg( $key ) ) return $name; else return wfMsg( $key ); } // ------------------------------------------------------------------------ protected $mModelID; + protected $mModelName; protected $mSupportedFormats; /** @@ -396,8 +414,11 @@ abstract class ContentHandler { public function __construct( $modelId, $formats ) { $this->mModelID = $modelId; $this->mSupportedFormats = $formats; - } + $this->mModelName = preg_replace( '/(Content)?Handler$/', '', get_class( $this ) ); + $this->mModelName = preg_replace( '/[_\\\\]/', '', $this->mModelName ); + $this->mModelName = strtolower( $this->mModelName ); + } /** * Serializes a Content object of the type supported by this ContentHandler. @@ -445,6 +466,20 @@ abstract class ContentHandler { return $this->mModelID; } + /** + * Returns the content model's symbolic name. + * + * The symbolic name is is this object's class name in lower case with the trailing "ContentHandler" + * and and special characters removed. + * + * @since WD.1 + * + * @return String The content model's name + */ + public function getModelName() { + return $this->mModelName; + } + /** * Throws an MWException if $model_id is not the ID of the content model * supported by this ContentHandler. diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 8b40898011..20ba009898 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -4876,10 +4876,10 @@ Otherwise, you can use the easy form below. Your comment will be added to the pa 'duration-centuries' => '$1 {{PLURAL:$1|century|centuries}}', 'duration-millennia' => '$1 {{PLURAL:$1|millennium|millennia}}', -# Content model IDs for the ContentHandler facility; used by ContentHander::getContentModel() -'content-model-1' => 'wikitext', -'content-model-2' => 'JavaScript', -'content-model-3' => 'CSS', -'content-model-4' => 'plain text', +# Content model IDs for the ContentHandler facility; used by ContentHandler::getContentModel() +'content-model-wikitext' => 'wikitext', +'content-model-javascript' => 'JavaScript', +'content-model-css' => 'CSS', +'content-model-text' => 'plain text', ); diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index 79a687a871..496940509e 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -4738,10 +4738,10 @@ $4 is the gender of the target user.', 'api-error-uploaddisabled' => 'API error message that can be used for client side localisation of API errors.', 'api-error-verification-error' => 'The word "extension" refers to the part behind the last dot in a file name, that by convention gives a hint about the kind of data format which a files contents are in.', -# Content model IDs for the ContentHandler facility; used by ContentHander::getContentModel() -'content-model-1' => 'Name for the wikitext content model, used when decribing what type of content a page contains.', -'content-model-2' => 'Name for the JavaScript content model, used when decribing what type of content a page contains.', -'content-model-3' => 'Name for the CSS content model, used when decribing what type of content a page contains.', -'content-model-4' => 'Name for the plain text content model, used when decribing what type of content a page contains.', +# Content model IDs for the ContentHandler facility; used by ContentHandler::getContentModel() +'content-model-wikitext' => 'Name for the wikitext content model, used when decribing what type of content a page contains.', +'content-model-javascript' => 'Name for the JavaScript content model, used when decribing what type of content a page contains.', +'content-model-css' => 'Name for the CSS content model, used when decribing what type of content a page contains.', +'content-model-text' => 'Name for the plain text content model, used when decribing what type of content a page contains.', ); diff --git a/tests/phpunit/includes/ContentHandlerTest.php b/tests/phpunit/includes/ContentHandlerTest.php index 155d4f7e9b..6c06049f3e 100644 --- a/tests/phpunit/includes/ContentHandlerTest.php +++ b/tests/phpunit/includes/ContentHandlerTest.php @@ -120,7 +120,7 @@ class ContentHandlerTest extends MediaWikiTestCase { $this->assertEquals( $expectedId, $id ); } - public function dataGetContentModelName() { + public function dataGetLocalizedNameName() { return array( array( 0, null ), array( null, null ), @@ -130,17 +130,62 @@ class ContentHandlerTest extends MediaWikiTestCase { ); } + /** + * @dataProvider dataGetLocalizedNameName + */ + public function testGetLocalizedName( $id, $expected ) { + try{ + $name = ContentHandler::getLocalizedName( $id ); + + if ( !$expected ) $this->fail("should not have a name for content id #$id"); + + $this->assertNotNull( $name, "no name found for content model #$id" ); + $this->assertTrue( preg_match( $expected, $name ) > 0 , "content model name for #$id did not match pattern $expected" ); + } catch (MWException $e) { + if ( $expected ) $this->fail("failed to get name for content id #$id"); + } + } + + public function dataGetContentModelName() { + return array( + array( 0, null ), + array( null, null ), + array( 99887766, null ), + + array( CONTENT_MODEL_JAVASCRIPT, 'javascript' ), + ); + } + /** * @dataProvider dataGetContentModelName */ public function testGetContentModelName( $id, $expected ) { - $name = ContentHandler::getContentModelName( $id ); + try { + $name = ContentHandler::getContentModelName( $id ); + + if ( !$expected ) $this->fail("should not have a name for content id #$id"); - if ( $expected === null ) { - $this->assertNull( $name, "content model name for #$id was expected to be null" ); - } else { $this->assertNotNull( $name, "no name found for content model #$id" ); - $this->assertTrue( preg_match( $expected, $name ) > 0 , "content model name for #$id did not match pattern $expected" ); + $this->assertEquals( $expected, $name); + } catch (MWException $e) { + if ( $expected ) $this->fail("failed to get name for content id #$id"); + } + } + + /** + * @dataProvider dataGetContentModelName + */ + public function testGetModelName( $id, $expected ) { + try { + $handler = ContentHandler::getForModelID( $id ); + $name = $handler->getModelName(); + + if ( !$expected ) $this->fail("should not have a name for content id #$id"); + + $this->assertNotNull( $name, "no name found for content model #$id" ); + $this->assertEquals( $expected, $name); + } catch (MWException $e) { + if ( $expected ) $this->fail("failed to get name for content id #$id"); } } @@ -354,6 +399,10 @@ class ContentHandlerTest extends MediaWikiTestCase { } } } + + public function testSupportsSections() { + $this->markTestIncomplete( "not yet implemented" ); + } } class DummyContentHandlerForTesting extends ContentHandler { diff --git a/tests/phpunit/suite.xml b/tests/phpunit/suite.xml index ddce5c5db9..f4ee50093b 100644 --- a/tests/phpunit/suite.xml +++ b/tests/phpunit/suite.xml @@ -11,8 +11,8 @@ timeoutForSmallTests="2" timeoutForMediumTests="10" timeoutForLargeTests="60" - strict="false" - verbose="true"> + strict="false" + verbose="false"> includes -- 2.20.1