[MediaWiki-commits] [Gerrit] New LanguageFallbackChainFactory functions - change (mediawiki...Wikibase)
jenkins-bot has submitted this change and it was merged. Change subject: New LanguageFallbackChainFactory functions .. New LanguageFallbackChainFactory functions newFromContextAndLanguageCode() and -newFromUserAndLanguageCode() are added. Change-Id: I1122e1757e0f5ac3b5982e343ede62dbec3e34bf --- M lib/includes/LanguageFallbackChainFactory.php M lib/tests/phpunit/LanguageFallbackChainFactoryTest.php 2 files changed, 57 insertions(+), 9 deletions(-) Approvals: Denny Vrandecic: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/includes/LanguageFallbackChainFactory.php b/lib/includes/LanguageFallbackChainFactory.php index 475db4e..e077ebf 100644 --- a/lib/includes/LanguageFallbackChainFactory.php +++ b/lib/includes/LanguageFallbackChainFactory.php @@ -1,7 +1,7 @@ ?php namespace Wikibase; -use Language, IContextSource, MWException; +use User, Language, IContextSource, MWException; /** * Object creating LanguageFallbackChain objects in Wikibase. @@ -180,32 +180,56 @@ } /** -* Construct the fallback chain based a context, currently from on data provided by Extension:Babel. +* Construct the fallback chain based on a context. Currently it just uses user and language info in it. * * @param IContextSource $context * * @return LanguageFallbackChain */ public function newFromContext( IContextSource $context ) { + return $this-newFromUserAndLanguageCode( $context-getUser(), $context-getLanguage()-getCode() ); + } + + /** +* Construct the fallback chain based on a context, but ignore the language info in it and use a specified one instead. +* +* @param IContextSource $context +* @param string $languageCode +* +* @return LanguageFallbackChain +*/ + public function newFromContextAndLanguageCode( IContextSource $context, $languageCode ) { + return $this-newFromUserAndLanguageCode( $context-getUser(), $languageCode ); + } + + /** +* Construct the fallback chain based on a user and a language, currently from data provided by Extension:Babel. +* +* @param User $user +* @param string $languageCode +* +* @return LanguageFallbackChain +*/ + public function newFromUserAndLanguageCode( User $user, $languageCode ) { global $wgBabelCategoryNames; wfProfileIn( __METHOD__ ); - $user = $context-getUser(); - if ( !class_exists( 'Babel' ) || $user-isAnon() ) { - $cached = $this-newFromLanguage( $context-getLanguage(), self::FALLBACK_ALL ); + $cached = $this-newFromLanguageCode( $languageCode, self::FALLBACK_ALL ); wfProfileOut( __METHOD__ ); return $cached; } - if ( isset( $this-userLanguageCache[$user-getName()][$context-getLanguage()-getCode()] ) ) { - $cached = $this-userLanguageCache[$user-getName()][$context-getLanguage()-getCode()]; + $languageCode = LanguageWithConversion::validateLanguageCode( $languageCode ); + + if ( isset( $this-userLanguageCache[$user-getName()][$languageCode] ) ) { + $cached = $this-userLanguageCache[$user-getName()][$languageCode]; wfProfileOut( __METHOD__ ); return $cached; } $babel = array(); - $contextLanguage = array( $context-getLanguage()-getCode() ); + $contextLanguage = array( $languageCode ); if ( count( $wgBabelCategoryNames ) ) { // A little redundant but it's the only way to get required information with current Babel API. @@ -226,7 +250,7 @@ $chain = $this-buildFromBabel( $babel ); $languageFallbackChain = new LanguageFallbackChain( $chain ); - $this-userLanguageCache[$user-getName()][$context-getLanguage()-getCode()] = $languageFallbackChain; + $this-userLanguageCache[$user-getName()][$languageCode] = $languageFallbackChain; wfProfileOut( __METHOD__ ); return $languageFallbackChain; diff --git a/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php b/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php index ca40695..d9ced64 100644 --- a/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php +++ b/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php @@ -190,6 +190,30 @@ /** * @group WikibaseLib +*/ + public function testNewFromContextAndLanguageCode() { + $factory = new LanguageFallbackChainFactory(); + $languageFallbackChain =
[MediaWiki-commits] [Gerrit] New LanguageFallbackChainFactory functions - change (mediawiki...Wikibase)
Liangent has uploaded a new change for review. https://gerrit.wikimedia.org/r/74653 Change subject: New LanguageFallbackChainFactory functions .. New LanguageFallbackChainFactory functions -newFromContextAndLanguage() and -newFromUserAndLanguage() are added. Change-Id: I1122e1757e0f5ac3b5982e343ede62dbec3e34bf --- M lib/includes/LanguageFallbackChainFactory.php M lib/tests/phpunit/LanguageFallbackChainFactoryTest.php 2 files changed, 55 insertions(+), 9 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/53/74653/1 diff --git a/lib/includes/LanguageFallbackChainFactory.php b/lib/includes/LanguageFallbackChainFactory.php index 475db4e..7b7a23c 100644 --- a/lib/includes/LanguageFallbackChainFactory.php +++ b/lib/includes/LanguageFallbackChainFactory.php @@ -1,7 +1,7 @@ ?php namespace Wikibase; -use Language, IContextSource, MWException; +use User, Language, IContextSource, MWException; /** * Object creating LanguageFallbackChain objects in Wikibase. @@ -180,32 +180,54 @@ } /** -* Construct the fallback chain based a context, currently from on data provided by Extension:Babel. +* Construct the fallback chain based on a context. Currently it just uses user and language info in it. * * @param IContextSource $context * * @return LanguageFallbackChain */ public function newFromContext( IContextSource $context ) { + return $this-newFromUserAndLanguage( $context-getUser(), $context-getLanguage() ); + } + + /** +* Construct the fallback chain based on a context, but ignore the language info in it and use a specified one instead. +* +* @param IContextSource $context +* @param Language $language +* +* @return LanguageFallbackChain +*/ + public function newFromContextAndLanguage( IContextSource $context, Language $language ) { + return $this-newFromUserAndLanguage( $context-getUser(), $language ); + } + + /** +* Construct the fallback chain based on a user and a language, currently from data provided by Extension:Babel. +* +* @param User $user +* @param Language $language +* +* @return LanguageFallbackChain +*/ + public function newFromUserAndLanguage( User $user, Language $language ) { global $wgBabelCategoryNames; wfProfileIn( __METHOD__ ); - $user = $context-getUser(); - if ( !class_exists( 'Babel' ) || $user-isAnon() ) { - $cached = $this-newFromLanguage( $context-getLanguage(), self::FALLBACK_ALL ); + $cached = $this-newFromLanguage( $language, self::FALLBACK_ALL ); wfProfileOut( __METHOD__ ); return $cached; } - if ( isset( $this-userLanguageCache[$user-getName()][$context-getLanguage()-getCode()] ) ) { - $cached = $this-userLanguageCache[$user-getName()][$context-getLanguage()-getCode()]; + if ( isset( $this-userLanguageCache[$user-getName()][$language-getCode()] ) ) { + $cached = $this-userLanguageCache[$user-getName()][$language-getCode()]; wfProfileOut( __METHOD__ ); return $cached; } $babel = array(); - $contextLanguage = array( $context-getLanguage()-getCode() ); + $contextLanguage = array( $language-getCode() ); if ( count( $wgBabelCategoryNames ) ) { // A little redundant but it's the only way to get required information with current Babel API. @@ -226,7 +248,7 @@ $chain = $this-buildFromBabel( $babel ); $languageFallbackChain = new LanguageFallbackChain( $chain ); - $this-userLanguageCache[$user-getName()][$context-getLanguage()-getCode()] = $languageFallbackChain; + $this-userLanguageCache[$user-getName()][$language-getCode()] = $languageFallbackChain; wfProfileOut( __METHOD__ ); return $languageFallbackChain; diff --git a/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php b/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php index ca40695..ed30e1b 100644 --- a/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php +++ b/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php @@ -190,6 +190,30 @@ /** * @group WikibaseLib +*/ + public function testNewFromContextAndLanguage() { + $factory = new LanguageFallbackChainFactory(); + $languageFallbackChain = $factory-newFromContext( \RequestContext::getMain(), \Language::factory( 'en' ) ); +