mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-24 00:05:00 +00:00
Avoid try…catch where if…else will do when using LanguageFactory
Needed for I869af06896b9757af18488b916211c5a41a8c563, where I am trying to change LanguageFactory in MediaWiki core not to use MWException. Language::isSupportedLanguage() does not actually throw, so this sigh can be one of relief. Change-Id: I3079d8e18d88a4a26c2f2b09dccd4beea06678ee
This commit is contained in:
parent
e415ed564c
commit
46bb5b0ac4
|
@ -7,7 +7,6 @@ use DateTimeZone;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Language;
|
use Language;
|
||||||
use MediaWiki\MediaWikiServices;
|
use MediaWiki\MediaWikiServices;
|
||||||
use MWException;
|
|
||||||
use MWTimestamp;
|
use MWTimestamp;
|
||||||
use Title;
|
use Title;
|
||||||
use User;
|
use User;
|
||||||
|
@ -82,12 +81,7 @@ class LanguageLibrary extends LibraryBase {
|
||||||
*/
|
*/
|
||||||
public function isSupportedLanguage( $code ) {
|
public function isSupportedLanguage( $code ) {
|
||||||
$this->checkType( 'isSupportedLanguage', 1, $code, 'string' );
|
$this->checkType( 'isSupportedLanguage', 1, $code, 'string' );
|
||||||
try {
|
|
||||||
// There's no good reason this should throw, but it does. Sigh.
|
|
||||||
return [ Language::isSupportedLanguage( $code ) ];
|
return [ Language::isSupportedLanguage( $code ) ];
|
||||||
} catch ( MWException $ex ) {
|
|
||||||
return [ false ];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -180,9 +174,9 @@ class LanguageLibrary extends LibraryBase {
|
||||||
if ( count( $this->langCache ) > $this->maxLangCacheSize ) {
|
if ( count( $this->langCache ) > $this->maxLangCacheSize ) {
|
||||||
throw new LuaError( 'too many language codes requested' );
|
throw new LuaError( 'too many language codes requested' );
|
||||||
}
|
}
|
||||||
try {
|
if ( Language::isValidCode( $code ) ) {
|
||||||
$this->langCache[$code] = Language::factory( $code );
|
$this->langCache[$code] = Language::factory( $code );
|
||||||
} catch ( MWException $ex ) {
|
} else {
|
||||||
throw new LuaError( "language code '$code' is invalid" );
|
throw new LuaError( "language code '$code' is invalid" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
|
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
|
||||||
|
|
||||||
|
use Language;
|
||||||
use MediaWiki\MediaWikiServices;
|
use MediaWiki\MediaWikiServices;
|
||||||
use Message;
|
use Message;
|
||||||
use MWException;
|
|
||||||
use RawMessage;
|
use RawMessage;
|
||||||
|
|
||||||
class MessageLibrary extends LibraryBase {
|
class MessageLibrary extends LibraryBase {
|
||||||
|
@ -43,8 +43,12 @@ class MessageLibrary extends LibraryBase {
|
||||||
} else {
|
} else {
|
||||||
$msg = Message::newFallbackSequence( $data['keys'] );
|
$msg = Message::newFallbackSequence( $data['keys'] );
|
||||||
}
|
}
|
||||||
$msg->inLanguage( $data['lang'] )
|
if ( is_string( $data['lang'] ) && !Language::isValidCode( $data['lang'] ) ) {
|
||||||
->useDatabase( $data['useDB'] );
|
throw new LuaError( "language code '{$data['lang']}' is invalid" );
|
||||||
|
} else {
|
||||||
|
$msg->inLanguage( $data['lang'] );
|
||||||
|
}
|
||||||
|
$msg->useDatabase( $data['useDB'] );
|
||||||
if ( $setParams ) {
|
if ( $setParams ) {
|
||||||
$msg->params( array_values( $data['params'] ) );
|
$msg->params( array_values( $data['params'] ) );
|
||||||
}
|
}
|
||||||
|
@ -58,12 +62,8 @@ class MessageLibrary extends LibraryBase {
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
public function messagePlain( $data ) {
|
public function messagePlain( $data ) {
|
||||||
try {
|
|
||||||
$msg = $this->makeMessage( $data, true );
|
$msg = $this->makeMessage( $data, true );
|
||||||
return [ $msg->plain() ];
|
return [ $msg->plain() ];
|
||||||
} catch ( MWException $ex ) {
|
|
||||||
throw new LuaError( "msg:plain() failed (" . $ex->getMessage() . ")" );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,11 +78,7 @@ class MessageLibrary extends LibraryBase {
|
||||||
throw new LuaError( "invalid what for 'messageCheck'" );
|
throw new LuaError( "invalid what for 'messageCheck'" );
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
$msg = $this->makeMessage( $data, false );
|
$msg = $this->makeMessage( $data, false );
|
||||||
return [ call_user_func( [ $msg, $what ] ) ];
|
return [ call_user_func( [ $msg, $what ] ) ];
|
||||||
} catch ( MWException $ex ) {
|
|
||||||
throw new LuaError( "msg:$what() failed (" . $ex->getMessage() . ")" );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue