From: daniel Date: Tue, 26 Jun 2012 14:37:42 +0000 (+0200) Subject: determine getPageLanguage via ContentHandler X-Git-Tag: 1.31.0-rc.0~22097^2^2~80^2~2 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/cotisations/rappels.php?a=commitdiff_plain;h=1ff6a778b3da1190125a28d6e465832b62f07c9a;p=lhc%2Fweb%2Fwiklou.git determine getPageLanguage via ContentHandler Change-Id: I73760d4b4412aef416ee3b55d85e9fa257703063 --- diff --git a/includes/ContentHandler.php b/includes/ContentHandler.php index 36abb77e67..99468e02cf 100644 --- a/includes/ContentHandler.php +++ b/includes/ContentHandler.php @@ -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 .= "
\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 = "";
diff --git a/includes/Title.php b/includes/Title.php
index 07be7063f5..f80a74de84 100644
--- a/includes/Title.php
+++ b/includes/Title.php
@@ -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 );
diff --git a/tests/phpunit/includes/ContentHandlerTest.php b/tests/phpunit/includes/ContentHandlerTest.php
index a45274c6af..611c09d82b 100644
--- a/tests/phpunit/includes/ContentHandlerTest.php
+++ b/tests/phpunit/includes/ContentHandlerTest.php
@@ -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;