setconfig.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', dirname(__DIR__));
  21. define('PUBLICDIR', INSTALLDIR);
  22. $shortoptions = 'da';
  23. $longoptions = array('delete', 'all');
  24. $helptext = <<<END_OF_SETCONFIG_HELP
  25. setconfig.php [options] [section] [setting] <value>
  26. With three args, set the setting to the value.
  27. With two args, just show the setting.
  28. With -d, delete the setting.
  29. With no args, lists all currently set values.
  30. [section] section to use (required)
  31. [setting] setting to use (required)
  32. <value> value to set (optional)
  33. -d --delete delete the setting (no value)
  34. -a --all list all configuration, not just the database values
  35. END_OF_SETCONFIG_HELP;
  36. require_once INSTALLDIR.'/scripts/commandline.inc';
  37. if (empty($args)) {
  38. if (have_option('a', 'all')) {
  39. foreach ($config as $section => $section_value) {
  40. foreach ($section_value as $setting => $value) {
  41. if (have_option('v', 'verbose') || !is_array($value)) {
  42. // Don't print array's without the verbose flag
  43. printf("%-20s %-20s %s\n", $section, $setting, var_export($value, true));
  44. }
  45. }
  46. }
  47. } else {
  48. $count = 0;
  49. $config = new Config();
  50. $config->find();
  51. while ($config->fetch()) {
  52. $count++;
  53. printf("%-20s %-20s %s\n", $config->section, $config->setting,
  54. var_export($config->value, true));
  55. }
  56. if ($count == 0) {
  57. print "No configuration set in database for this site.\n";
  58. }
  59. }
  60. exit(0);
  61. }
  62. if (count($args) < 2 || count($args) > 3) {
  63. show_help();
  64. exit(1);
  65. }
  66. $section = $args[0];
  67. $setting = $args[1];
  68. if (count($args) == 3) {
  69. $value = $args[2];
  70. } else {
  71. $value = null;
  72. }
  73. try {
  74. if (have_option('d', 'delete')) { // Delete
  75. if (count($args) != 2) {
  76. show_help();
  77. exit(1);
  78. }
  79. if (have_option('v', 'verbose')) {
  80. print "Deleting setting $section/$setting...";
  81. }
  82. $setting = Config::pkeyGet(array('section' => $section,
  83. 'setting' => $setting));
  84. if (empty($setting)) {
  85. print "Not found.\n";
  86. } else {
  87. $result = $setting->delete();
  88. if ($result) {
  89. print "DONE.\n";
  90. } else {
  91. print "ERROR.\n";
  92. }
  93. }
  94. } else if (count($args) == 2) { // show
  95. if (have_option('v', 'verbose')) {
  96. print "$section/$setting = ";
  97. }
  98. $value = common_config($section, $setting);
  99. print "$value\n";
  100. } else { // set
  101. if (have_option('v', 'verbose')) {
  102. print "Setting $section/$setting...";
  103. }
  104. Config::save($section, $setting, $value);
  105. print "DONE.\n";
  106. }
  107. } catch (Exception $e) {
  108. print $e->getMessage() . "\n";
  109. exit(1);
  110. }