(bug 41476) Implement Language::isKnownLanguageTag()
authorAmir E. Aharoni <amir.aharoni@mail.huji.ac.il>
Mon, 21 Jan 2013 04:15:32 +0000 (06:15 +0200)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 28 Jan 2013 09:10:11 +0000 (09:10 +0000)
Change-Id: I130d8e0b397323e21058cf46510440da066fa12b

languages/Language.php
tests/phpunit/languages/LanguageTest.php

index 212ca32..dd2fef6 100644 (file)
@@ -361,6 +361,30 @@ class Language {
                return (bool)preg_match( '/^[a-z0-9-]+$/i', $code );
        }
 
+       /**
+        * Returns true if a language code is an IETF tag known to MediaWiki.
+        *
+        * @param $code string
+        *
+        * @since 1.21
+        * @return bool
+        */
+       public static function isKnownLanguageTag( $tag ) {
+               static $coreLanguageNames;
+
+               if ( $coreLanguageNames === null ) {
+                       include( MWInit::compiledPath( 'languages/Names.php' ) );
+               }
+
+               if ( isset( $coreLanguageNames[$tag] )
+                       || self::fetchLanguageName( $tag, $tag ) !== ''
+               ) {
+                       return true;
+               }
+
+               return false;
+       }
+
        /**
         * @param $code
         * @return String Name of the language class
index 5594670..ec7f9db 100644 (file)
@@ -449,6 +449,56 @@ class LanguageTest extends LanguageClassesTestCase {
                );
        }
 
+       /**
+        * Test Language::isKnownLanguageTag()
+        * @dataProvider provideKnownLanguageTags
+        */
+       function testKnownLanguageTag( $code, $message = '' ) {
+               $this->assertTrue(
+                       (bool) Language::isKnownLanguageTag( $code ),
+                       "validating code $code - $message"
+               );
+       }
+
+       function provideKnownLanguageTags() {
+               return array(
+                       array( 'fr', 'simple code' ),
+                       array( 'bat-smg', 'an MW legacy tag' ),
+                       array( 'sgs', 'an internal standard MW name, for which a legacy tag is used externally' ),
+               );
+       }
+
+       /**
+        * Test Language::isKnownLanguageTag()
+        */
+       function testKnownCldrLanguageTag() {
+               if ( !class_exists( 'LanguageNames' ) ) {
+                       $this->markTestSkipped( 'reason' );
+               }
+
+               $this->assertTrue(
+                       (bool) Language::isKnownLanguageTag( 'pal' ),
+                       'validating code "pal" an ancient language, which probably will not appear in Names.php, but appears in CLDR in English'
+               );
+       }
+
+       /**
+        * Negative tests for Language::isKnownLanguageTag()
+        * @dataProvider provideUnKnownLanguageTags
+        */
+       function testUnknownLanguageTag( $code, $message = '' ) {
+               $this->assertFalse(
+                       (bool) Language::isKnownLanguageTag( $code ),
+                       "checking that code $code is invalid - $message"
+               );
+       }
+
+       function provideUnknownLanguageTags() {
+               return array(
+                       array( 'mw', 'non-existent two-letter code' ),
+               );
+       }
+
        /**
         * @dataProvider provideSprintfDateSamples
         */