Skip to content

Instantly share code, notes, and snippets.

@totten
Last active March 30, 2023 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totten/fa35e711eaa8f413a524c7036c67523e to your computer and use it in GitHub Desktop.
Save totten/fa35e711eaa8f413a524c7036c67523e to your computer and use it in GitHub Desktop.
Settings metadata summary circa 5.61

The civicrm settings/ folder provides metadata about the, well, settings. But what metadata is provided?

This report is based on scanning and counting the various keys. It has major sections for:

  • keys(setting) (keys which appear directly in the setting record)
  • keys(setting.html_attributes) (keys which appear nested underneath the html_attributes property)
  • keys(setting.attributes) (keys which appear nested underneath the attributes property)

For each section, we have a list of COMMON keys (>20% records), UNCOMMON keys (<20%), and extremely rare OUTLIERS (1 or 2 instances).

For each key, we have a total quantity and a relative percentage.

Let's read an example:

    "keys(setting)": {
        "COUNT": 223,
        "COMMON": {
            "help_text": "197 (88.34%)",

After scanning 223 setting records, the help_text appears 197 times. Thus, 88% of settings have a declaration for help_text. This makes it a COMMON key.

{
"keys(setting)": {
"COUNT": 223,
"COMMON": {
"add": "223 (100.00%)",
"default": "220 (98.65%)",
"description": "167 (74.89%)",
"group": "216 (96.86%)",
"group_name": "216 (96.86%)",
"help_text": "197 (88.34%)",
"html_attributes": "80 (35.87%)",
"html_type": "184 (82.51%)",
"is_contact": "223 (100.00%)",
"is_domain": "223 (100.00%)",
"name": "223 (100.00%)",
"pseudoconstant": "62 (27.80%)",
"quick_form_type": "159 (71.30%)",
"title": "214 (95.96%)",
"type": "223 (100.00%)"
},
"UNCOMMON": {
"bootstrap_comment": "13 (5.83%)",
"on_change": "10 (4.48%)",
"options": "4 (1.79%)",
"serialize": "7 (3.14%)",
"settings_pages": "34 (15.25%)",
"validate_callback": "10 (4.48%)"
},
"OUTLIER": {
"attributes": "1 (0.45%)",
"chain_select_settings": "1 (0.45%)",
"config_key": "1 (0.45%)",
"documentation_link": "2 (0.90%)",
"entity_reference_options": "1 (0.45%)",
"help": "1 (0.45%)",
"is_required": "1 (0.45%)",
"legacy_key": "2 (0.90%)",
"sortable": "1 (0.45%)",
"weight": "1 (0.45%)"
}
},
"keys(setting.html_attributes)": {
"COUNT": 80,
"COMMON": {
"class": "35 (43.75%)",
"maxlength": "37 (46.25%)",
"size": "42 (52.50%)"
},
"UNCOMMON": {
"multiple": "13 (16.25%)",
"style": "3 (3.75%)"
},
"OUTLIER": []
},
"keys(setting.attributes)": {
"COUNT": 1,
"COMMON": [],
"UNCOMMON": [],
"OUTLIER": {
"cols": "1 (100.00%)",
"rows": "1 (100.00%)"
}
}
}
<?php
use Civi\Core\SettingsMetadata;
function jp($ar) {
echo json_encode($ar, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
echo "\n";
}
function keyCount(array $md) {
$counts = [];
foreach ($md as $item) {
foreach (array_keys($item) as $key) {
$counts[$key] = 1 + ($counts[$key] ?? 0);
}
}
ksort($counts);
return $counts;
}
function keySummary(array $md) {
$items = count($md);
$threshold = round($items / 4);
$counts = keyCount($md);
$buckets = ['COUNT' => $items, 'COMMON' => [], 'UNCOMMON' => [], 'OUTLIER' => []];
foreach ($counts as $key => $count) {
if ($count < 3) {
$bucket = 'OUTLIER';
}
elseif ($count < $threshold) {
$bucket = 'UNCOMMON';
}
else {
$bucket = 'COMMON';
}
$buckets[$bucket][$key] = sprintf('%d (%.2f%%)', $count, 100 * $count / $items);
}
return $buckets;
}
function collectKeys(array $md) {
$keys = array_reduce($md, function($carry, $item){
return array_unique(array_merge(
$carry,
array_keys($item)
));
}, []);
kort($keys);
return $keys;
}
$md = SettingsMetadata::getMetadata();
jp([
'keys(setting)' => keySummary($md),
'keys(setting.html_attributes)' => keySummary(array_column($md, 'html_attributes')),
'keys(setting.attributes)' => keySummary(array_column($md, 'attributes'))
]);
// $tgts = [
// 'attributes',
// 'html_attributes',
// // 'options',
// // 'quick_form_type'
// ];
// foreach ($md as $name => $setting) {
// $setting = CRM_Utils_Array::subset($setting, $tgts);
// foreach ($tgts as $tgt) {
// if (empty($setting[$tgt])) {
// unset($setting[$tgt]);
// }
// }
// if (!empty($setting)) {
// jp($setting);
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment