comparison mgtool.php @ 322:2f4e3e458714

Improve configuration handling, and add "string array" configuration item type.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 09 Apr 2020 11:55:08 +0300
parents a4c6937dfaaf
children 9a4297e59a40
comparison
equal deleted inserted replaced
321:f07e3d79c421 322:2f4e3e458714
1332 mgFatal("Failed to purge table '".$tname."'.\n"); 1332 mgFatal("Failed to purge table '".$tname."'.\n");
1333 } 1333 }
1334 } 1334 }
1335 1335
1336 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt); 1336 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt);
1337 }
1338
1339
1340 function mgGetDValStr($key, $mdata, $val, $format, $multi)
1341 {
1342 $vfmt = "%s";
1343
1344 switch ($mdata[0])
1345 {
1346 case MG_STR_ARRAY:
1347 if ($val === FALSE)
1348 $val = [];
1349 else
1350 if (is_array($val) || is_string($val))
1351 {
1352 $vfmt = "\"%s\"";
1353 if (is_string($val))
1354 $val = [ $val ];
1355 }
1356 else
1357 $val = "ERROR1";
1358 break;
1359
1360 case MG_STR:
1361 case MG_STR_LC:
1362 if (is_string($val))
1363 {
1364 $vfmt = "\"%s\"";
1365 if ($mdata[0] == MG_STR_LC)
1366 $val = strtolower($val);
1367 }
1368 else
1369 $val = "ERROR2";
1370 break;
1371
1372 case MG_BOOL:
1373 $val = $val ? "yes" : "no";
1374 break;
1375
1376 case MG_FLAGS:
1377 {
1378 $mstr = [];
1379 foreach ($mdata[2] as $vkey => $vval)
1380 {
1381 if ($val & $vval)
1382 $mstr[] = $vkey;
1383 }
1384
1385 $val = "\"".implode(" | ", $mstr)."\"";
1386 }
1387 break;
1388
1389 case MG_INT:
1390 $val = (string) $val;
1391 break;
1392
1393 default:
1394 $val = "ERROR";
1395 }
1396
1397 if (!is_array($val))
1398 {
1399 if ($val === NULL || $val === FALSE)
1400 $val = [];
1401 else
1402 $val = [ $val ];
1403 }
1404
1405 $res = (count($val) == 0) ? "# " : "";
1406
1407 if ($multi)
1408 {
1409 if (count($val) > 0)
1410 {
1411 foreach ($val as $vv)
1412 $res .= sprintf($format, $key, sprintf($vfmt, $vv));
1413 }
1414 else
1415 $res .= sprintf($format, $key, "");
1416 }
1417 else
1418 {
1419 $res .= sprintf($format, $key,
1420 implode(", ", array_map(function($vv) use ($vfmt) { return sprintf($vfmt, $vv); }, $val)));
1421 }
1422
1423 return $res;
1337 } 1424 }
1338 1425
1339 1426
1340 function mgShowCopyright() 1427 function mgShowCopyright()
1341 { 1428 {
1543 } 1630 }
1544 break; 1631 break;
1545 1632
1546 case "config": 1633 case "config":
1547 case "dump": 1634 case "dump":
1548 1635 foreach ($mgDefaults as $ckey => $cdata)
1549 foreach ($mgDefaults as $key => $dval) 1636 {
1550 { 1637 $sval = mgGetSetting($ckey);
1551 $sval = mgGetSetting($key); 1638
1552 if ($cmd == "dump") 1639 if ($cmd == "dump")
1553 { 1640 {
1554 printf("%-20s = %s\n", 1641 echo mgGetDValStr($ckey, $cdata, $sval, "%1\$-15s = %2\$s\n", FALSE);
1555 $key,
1556 mgGetDValStr($dval, $sval));
1557 } 1642 }
1558 else 1643 else
1559 { 1644 {
1560 printf("%-20s = %s%s\n", 1645 echo mgGetDValStr($ckey, $cdata, $sval, "%1\$-15s = %2\$s", TRUE).
1561 $key, 1646 (($cdata[1] !== NULL && $sval !== $cdata[1]) ?
1562 mgGetDValStr($dval, $sval), 1647 " (default: ".mgGetDValStr($ckey, $cdata, $cdata[1], "%2\$s", FALSE).")" : "")."\n";
1563 ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval, $dval[1]).")" : "");
1564 } 1648 }
1565 } 1649 }
1566 break; 1650 break;
1567 1651
1568 default: 1652 default: