From: Stanislav Malyshev Date: Tue, 31 Oct 2017 21:01:02 +0000 (-0700) Subject: Expose string->bool conversion as function X-Git-Tag: 1.31.0-rc.0~1476^2 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=194acaa0e02b584e8e3f5fe8434b1fe4cbb801de;p=lhc%2Fweb%2Fwiklou.git 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 --- 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 ) ); + } + } + +}