support symbolic name for content models
authordaniel <daniel.kinzler@wikimedia.de>
Tue, 19 Jun 2012 12:45:25 +0000 (14:45 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Tue, 19 Jun 2012 12:45:25 +0000 (14:45 +0200)
Change-Id: I4eaed3db7fb10069f73d84514abd2f6957019153

includes/ContentHandler.php
languages/messages/MessagesEn.php
languages/messages/MessagesQqq.php
tests/phpunit/includes/ContentHandlerTest.php
tests/phpunit/suite.xml

index 63fa47d..7bf33c5 100644 (file)
@@ -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.
index 8b40898..20ba009 100644 (file)
@@ -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',
 
 );
index 79a687a..4969405 100644 (file)
@@ -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.',
 
 );
index 155d4f7..6c06049 100644 (file)
@@ -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 {
index ddce5c5..f4ee500 100644 (file)
@@ -11,8 +11,8 @@
                 timeoutForSmallTests="2"
                 timeoutForMediumTests="10"
                 timeoutForLargeTests="60"
-         strict="false"
-                verbose="true">
+                strict="false"
+                verbose="false">
        <testsuites>
                <testsuite name="includes">
                        <directory>includes</directory>