From 45ae4bb83834a513e709b960747f964dba90392b Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 17 May 2015 12:56:51 +0200 Subject: Add filtering to remove blank entries from key/value config retrieval This applies to: - acronyms - entities - interwiki - mime - smileys --- inc/confutils.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'inc/confutils.php') diff --git a/inc/confutils.php b/inc/confutils.php index 8643a056c..3e69b20be 100644 --- a/inc/confutils.php +++ b/inc/confutils.php @@ -49,6 +49,7 @@ function getMimeTypes() { static $mime = null; if ( !$mime ) { $mime = retrieveConfig('mime','confToHash'); + $mime = array_filter($mime); } return $mime; } @@ -62,6 +63,7 @@ function getAcronyms() { static $acronyms = null; if ( !$acronyms ) { $acronyms = retrieveConfig('acronyms','confToHash'); + $acronyms = array_filter($acronyms); } return $acronyms; } @@ -75,6 +77,7 @@ function getSmileys() { static $smileys = null; if ( !$smileys ) { $smileys = retrieveConfig('smileys','confToHash'); + $smileys = array_filter($smileys); } return $smileys; } @@ -88,6 +91,7 @@ function getEntities() { static $entities = null; if ( !$entities ) { $entities = retrieveConfig('entities','confToHash'); + $entities = array_filter($entities); } return $entities; } @@ -101,9 +105,11 @@ function getInterwiki() { static $wikis = null; if ( !$wikis ) { $wikis = retrieveConfig('interwiki','confToHash',array(true)); + $wikis = array_filter($wikis); + + //add sepecial case 'this' + $wikis['this'] = DOKU_URL.'{NAME}'; } - //add sepecial case 'this' - $wikis['this'] = DOKU_URL.'{NAME}'; return $wikis; } -- cgit v1.2.3 From 4c353447a306680af86c38db3ef592e29a0cbe60 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 17 May 2015 13:02:07 +0200 Subject: Support negating of config values include earlier in the cascade To negate a config value, prefix the value with an '!'. E.g. to disable recognition of the gopher scheme !gopher This applies to: - scheme - stopwords --- inc/confutils.php | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) (limited to 'inc/confutils.php') diff --git a/inc/confutils.php b/inc/confutils.php index 3e69b20be..7d7c043fa 100644 --- a/inc/confutils.php +++ b/inc/confutils.php @@ -120,7 +120,7 @@ function getInterwiki() { function getWordblocks() { static $wordblocks = null; if ( !$wordblocks ) { - $wordblocks = retrieveConfig('wordblock','file'); + $wordblocks = retrieveConfig('wordblock','file',null,'array_merge_with_removal'); } return $wordblocks; } @@ -133,11 +133,11 @@ function getWordblocks() { function getSchemes() { static $schemes = null; if ( !$schemes ) { - $schemes = retrieveConfig('scheme','file'); + $schemes = retrieveConfig('scheme','file',null,'array_merge_with_removal'); + $schemes = array_map('trim', $schemes); + $schemes = preg_replace('/^#.*/', '', $schemes); + $schemes = array_filter($schemes); } - $schemes = array_map('trim', $schemes); - $schemes = preg_replace('/^#.*/', '', $schemes); - $schemes = array_filter($schemes); return $schemes; } @@ -202,7 +202,7 @@ function confToHash($file,$lower=false) { * @param array $params optional additional params to pass to the callback * @return array configuration values */ -function retrieveConfig($type,$fn,$params=null) { +function retrieveConfig($type,$fn,$params=null,$combine='array_merge') { global $config_cascade; if(!is_array($params)) $params = array(); @@ -214,7 +214,7 @@ function retrieveConfig($type,$fn,$params=null) { foreach ($config_cascade[$type][$config_group] as $file) { if (file_exists($file)) { $config = call_user_func_array($fn,array_merge(array($file),$params)); - $combined = array_merge($combined, $config); + $combined = $combine($combined, $config); } } } @@ -353,4 +353,27 @@ function conf_decodeString($str) { return $str; } } + +/** + * array combination function to remove negated values (prefixed by !) + * + * @param array $current + * @param array $new + * + * @return array the combined array, numeric keys reset + */ +function array_merge_with_removal($current, $new) { + foreach ($new as $val) { + if (substr($val,0,1) == '!') { + $idx = array_search(substr($val,1),$current); + if ($idx !== false) { + unset($current[$idx]); + } + } else { + $current[] = $val; + } + } + + return array_slice($current,0); +} //Setup VIM: ex: et ts=4 : -- cgit v1.2.3 From f266a91942e3eb7558afe54062c5e6a7bbcdf9ee Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Mon, 18 May 2015 20:31:04 +0100 Subject: Ensure filtering only removes empty string values (not other values which PHP evaluates to false) --- inc/confutils.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'inc/confutils.php') diff --git a/inc/confutils.php b/inc/confutils.php index 7d7c043fa..e3805d74e 100644 --- a/inc/confutils.php +++ b/inc/confutils.php @@ -63,7 +63,7 @@ function getAcronyms() { static $acronyms = null; if ( !$acronyms ) { $acronyms = retrieveConfig('acronyms','confToHash'); - $acronyms = array_filter($acronyms); + $acronyms = array_filter($acronyms, 'strlen'); } return $acronyms; } @@ -77,7 +77,7 @@ function getSmileys() { static $smileys = null; if ( !$smileys ) { $smileys = retrieveConfig('smileys','confToHash'); - $smileys = array_filter($smileys); + $smileys = array_filter($smileys, 'strlen'); } return $smileys; } @@ -91,7 +91,7 @@ function getEntities() { static $entities = null; if ( !$entities ) { $entities = retrieveConfig('entities','confToHash'); - $entities = array_filter($entities); + $entities = array_filter($entities, 'strlen'); } return $entities; } @@ -105,7 +105,7 @@ function getInterwiki() { static $wikis = null; if ( !$wikis ) { $wikis = retrieveConfig('interwiki','confToHash',array(true)); - $wikis = array_filter($wikis); + $wikis = array_filter($wikis, 'strlen'); //add sepecial case 'this' $wikis['this'] = DOKU_URL.'{NAME}'; -- cgit v1.2.3 From 3a7669bd1068f34022db08228baeefae2b27d05b Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Mon, 18 May 2015 20:32:49 +0100 Subject: Ensure single value negation is not affected by white space differences --- inc/confutils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'inc/confutils.php') diff --git a/inc/confutils.php b/inc/confutils.php index e3805d74e..c587bed4d 100644 --- a/inc/confutils.php +++ b/inc/confutils.php @@ -365,12 +365,12 @@ function conf_decodeString($str) { function array_merge_with_removal($current, $new) { foreach ($new as $val) { if (substr($val,0,1) == '!') { - $idx = array_search(substr($val,1),$current); + $idx = array_search(trim(substr($val,1)),$current); if ($idx !== false) { unset($current[$idx]); } } else { - $current[] = $val; + $current[] = trim($val); } } -- cgit v1.2.3 From 10b38f10e6ce99d01c6ca30bcd9992699d6ee049 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Mon, 18 May 2015 20:34:47 +0100 Subject: Define the negation character in a constant --- inc/confutils.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'inc/confutils.php') diff --git a/inc/confutils.php b/inc/confutils.php index c587bed4d..4035d6dd4 100644 --- a/inc/confutils.php +++ b/inc/confutils.php @@ -6,6 +6,12 @@ * @author Harry Fuecks */ +/* + * line prefix used to negate single value config items + * (scheme.conf & stopwords.conf), e.g. + * !gopher + */ +const DOKU_CONF_NEGATION = '!'; /** * Returns the (known) extension and mimetype of a given filename @@ -364,7 +370,7 @@ function conf_decodeString($str) { */ function array_merge_with_removal($current, $new) { foreach ($new as $val) { - if (substr($val,0,1) == '!') { + if (substr($val,0,1) == DOKU_CONF_NEGATION) { $idx = array_search(trim(substr($val,1)),$current); if ($idx !== false) { unset($current[$idx]); -- cgit v1.2.3 From 4286c64e98499b93056c09498c10709f8909befd Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Thu, 28 May 2015 12:18:36 +0100 Subject: update confToHash() inline documentation for new parameter --- inc/confutils.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'inc/confutils.php') diff --git a/inc/confutils.php b/inc/confutils.php index 4035d6dd4..8b61a8d5b 100644 --- a/inc/confutils.php +++ b/inc/confutils.php @@ -206,6 +206,11 @@ function confToHash($file,$lower=false) { * @param string $type the configuration settings to be read, must correspond to a key/array in $config_cascade * @param callback $fn the function used to process the configuration file into an array * @param array $params optional additional params to pass to the callback + * @param callback $combine the function used to combine arrays of values read from different configuration files; + * the function takes two parameters, + * $combined - the already read & merged configuration values + * $new - array of config values from the config cascade file being currently processed + * and returns an array of the merged configuration values. * @return array configuration values */ function retrieveConfig($type,$fn,$params=null,$combine='array_merge') { -- cgit v1.2.3