Smalyshev has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/387683 )
Change subject: Expose string->bool conversion as function
......................................................................
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
---
M includes/GlobalFunctions.php
A tests/phpunit/includes/GlobalFunctions/wfStringToBoolTest.php
2 files changed, 71 insertions(+), 5 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/83/387683/1
diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index 1cff881..2a62812 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -2225,12 +2225,27 @@
* @return bool
*/
function wfIniGetBool( $setting ) {
- $val = strtolower( ini_get( $setting ) );
- // 'on' and 'true' can't have whitespace around them, but '1' can.
+ 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 );
return $val == 'on'
- || $val == 'true'
- || $val == 'yes'
- || preg_match( "/^\s*[+-]?0*[1-9]/", $val ); // approx C atoi()
function
+ || $val == 'true'
+ || $val == 'yes'
+ || preg_match( "/^\s*[+-]?0*[1-9]/", $val ); // approx C atoi()
function
}
/**
diff --git a/tests/phpunit/includes/GlobalFunctions/wfStringToBoolTest.php
b/tests/phpunit/includes/GlobalFunctions/wfStringToBoolTest.php
new file mode 100644
index 0000000..e037ccd
--- /dev/null
+++ b/tests/phpunit/includes/GlobalFunctions/wfStringToBoolTest.php
@@ -0,0 +1,51 @@
+<?php
+
+/**
+ * @group GlobalFunctions
+ * @covers ::wfStringToBool
+ */
+class WfStringToBoolTest extends MediaWikiTestCase {
+
+ public function getTestCases() {
+ return [
+ [ 'true', true ],
+ [ 'on', true ],
+ [ 'yes', true ],
+ [ 'TRUE', true ],
+ [ 'YeS', true ],
+ [ 'On', true ],
+ [ '1', true ],
+ [ '+1', true ],
+ [ '01', true ],
+ [ '-001', true ],
+ [ ' 1', true ],
+ [ '-1 ', true ],
+ [ '', false ],
+ [ '0', false ],
+ [ 'false', false ],
+ [ 'NO', false ],
+ [ 'NOT', false ],
+ [ 'never', false ],
+ [ '!&', false ],
+ [ '-0', false ],
+ [ '+0', false ],
+ [ 'forget about it', false ],
+ [ ' on', false ],
+ [ 'true ', false ],
+ ];
+ }
+
+ /**
+ * @dataProvider getTestCases
+ * @param string $str
+ * @param bool $bool
+ */
+ public function testStr2Bool( $str, $bool ) {
+ if ( $bool ) {
+ $this->assertTrue( wfStringToBool( $str ) );
+ } else {
+ $this->assertFalse( wfStringToBool( $str ) );
+ }
+
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/387683
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I88d98b012ff4bf14fd64a05a9135a6e75cf2d4e7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Smalyshev <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits