determine getPageLanguage via ContentHandler
authordaniel <daniel.kinzler@wikimedia.de>
Tue, 26 Jun 2012 14:37:42 +0000 (16:37 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Tue, 26 Jun 2012 14:37:42 +0000 (16:37 +0200)
Change-Id: I73760d4b4412aef416ee3b55d85e9fa257703063

includes/ContentHandler.php
includes/Title.php
tests/phpunit/includes/ContentHandlerTest.php

index 36abb77..99468e0 100644 (file)
@@ -557,6 +557,29 @@ abstract class ContentHandler {
                return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
        }
 
+       /**
+        * Get the language in which the content of the given page is written.
+        *
+        * This default implementation returns $wgContLang->getCode().
+        *
+        * Note that a page's language must be permanent and cacheable, that is, it must not depend
+        * on user preferences, request parameters or session state.
+        *
+        * Also note that the page language may or may not depend on the actual content of the page,
+        * that is, this method may load the content in order to determine the language.
+        *
+        * @since 1.WD
+        *
+        * @param Title        $title the page to determine the language for.
+        * @param Content|null $content the page's content, if you have it handy, to avoid reloading it.
+        *
+        * @return Language the page's language code
+        */
+       public function getPageLanguage( Title $title, Content $content = null ) {
+               global $wgContLang;
+               return $wgContLang;
+       }
+
        /**
         * Returns the name of the diff engine to use.
         *
@@ -1126,6 +1149,17 @@ class JavaScriptContentHandler extends TextContentHandler {
                return new JavaScriptContent( '' );
        }
 
+       /**
+        * Returns the english language, because JS is english, and should be handled as such.
+        *
+        * @return Language wfGetLangObj( 'en' )
+        *
+        * @see ContentHandler::getPageLanguage()
+        */
+       public function getPageLanguage( Title $title, Content $content = null ) {
+               return wfGetLangObj( 'en' );
+       }
+
        protected function getHtml( Content $content ) {
                $html = "";
                $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
@@ -1155,6 +1189,16 @@ class CssContentHandler extends TextContentHandler {
                return new CssContent( '' );
        }
 
+       /**
+        * Returns the english language, because CSS is english, and should be handled as such.
+        *
+        * @return Language wfGetLangObj( 'en' )
+        *
+        * @see ContentHandler::getPageLanguage()
+        */
+       public function getPageLanguage( Title $title, Content $content = null ) {
+               return wfGetLangObj( 'en' );
+       }
 
        protected function getHtml( Content $content ) {
                $html = "";
index 07be706..f80a74d 100644 (file)
@@ -4548,17 +4548,17 @@ class Title {
                if ( $this->isSpecialPage() ) {
                        // special pages are in the user language
                        return $wgLang;
-               } elseif ( $this->isCssOrJsPage() || $this->isCssJsSubpage() ) {
-                       // css/js should always be LTR and is, in fact, English
-                       return wfGetLangObj( 'en' );
                } elseif ( $this->getNamespace() == NS_MEDIAWIKI ) {
                        // Parse mediawiki messages with correct target language
                        list( /* $unused */, $lang ) = MessageCache::singleton()->figureMessage( $this->getText() );
                        return wfGetLangObj( $lang );
                }
-               global $wgContLang;
-               // If nothing special, it should be in the wiki content language
-               $pageLang = $wgContLang;
+
+               //TODO: use the LinkCache to cache this!
+               //NOTE: ContentHandler::getPageLanguage() may need to load the content to determine the page language!
+               $contentHandler = ContentHandler::getForTitle( $this );
+               $pageLang = $contentHandler->getPageLanguage( $this );
+
                // Hook at the end because we don't want to override the above stuff
                wfRunHooks( 'PageContentLanguage', array( $this, &$pageLang, $wgLang ) );
                return wfGetLangObj( $pageLang );
index a45274c..611c09d 100644 (file)
@@ -121,6 +121,38 @@ class ContentHandlerTest extends MediaWikiTestCase {
                }
        }
 
+       public function dataGetPageLanguage() {
+               global $wgLanguageCode;
+
+               return array(
+                       array( "Main", $wgLanguageCode ),
+                       array( "Dummy:Foo", $wgLanguageCode ),
+                       array( "MediaWiki:common.js", 'en' ),
+                       array( "User:Foo/common.js", 'en' ),
+                       array( "MediaWiki:common.css", 'en' ),
+                       array( "User:Foo/common.css", 'en' ),
+                       array( "User:Foo", $wgLanguageCode ),
+
+                       array( CONTENT_MODEL_JAVASCRIPT, 'javascript' ),
+               );
+       }
+
+       /**
+        * @dataProvider dataGetPageLanguage
+        */
+       public function testGetPageLanguage( $title, $expected ) {
+               if ( is_string( $title ) ) {
+                       $title = Title::newFromText( $title );
+               }
+
+               $expected = wfGetLangObj( $expected );
+
+               $handler = ContentHandler::getForTitle( $title );
+               $lang = $handler->getPageLanguage( $title );
+
+               $this->assertEquals( $expected->getCode(), $lang->getCode() );
+       }
+
        public function testGetContentText_Null( ) {
                global $wgContentHandlerTextFallback;