From 194acaa0e02b584e8e3f5fe8434b1fe4cbb801de Mon Sep 17 00:00:00 2001 From: Stanislav Malyshev Date: Tue, 31 Oct 2017 14:01:02 -0700 Subject: [PATCH] Expose string->bool conversion as function There is code in several places in extensions which converts setting or parameter string (such as "true", "yes", "false", "no") to boolean. Since we already have the code that does in global functions in wfStringToBool(), it makes sense to expose this code and reuse it. Change-Id: I88d98b012ff4bf14fd64a05a9135a6e75cf2d4e7 --- includes/GlobalFunctions.php | 18 ++++++- .../GlobalFunctions/wfStringToBoolTest.php | 51 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/includes/GlobalFunctions/wfStringToBoolTest.php diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 404d115280..bb1951d528 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -2225,7 +2225,23 @@ function wfPercent( $nr, $acc = 2, $round = true ) { * @return bool */ function wfIniGetBool( $setting ) { - $val = strtolower( ini_get( $setting ) ); + return wfStringToBool( ini_get( $setting ) ); +} + +/** + * Convert string value to boolean, when the following are interpreted as true: + * - on + * - true + * - yes + * - Any number, except 0 + * All other strings are interpreted as false. + * + * @param string $val + * @return bool + * @since 1.31 + */ +function wfStringToBool( $val ) { + $val = strtolower( $val ); // 'on' and 'true' can't have whitespace around them, but '1' can. return $val == 'on' || $val == 'true' diff --git a/tests/phpunit/includes/GlobalFunctions/wfStringToBoolTest.php b/tests/phpunit/includes/GlobalFunctions/wfStringToBoolTest.php new file mode 100644 index 0000000000..7f56b60529 --- /dev/null +++ b/tests/phpunit/includes/GlobalFunctions/wfStringToBoolTest.php @@ -0,0 +1,51 @@ +assertTrue( wfStringToBool( $str ) ); + } else { + $this->assertFalse( wfStringToBool( $str ) ); + } + } + +} -- 2.20.1