setconfig.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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', realpath(dirname(__FILE__) . '/..'));
  21. $shortoptions = 'da';
  22. $longoptions = array('delete', 'all');
  23. $helptext = <<<END_OF_SETCONFIG_HELP
  24. setconfig.php [options] [section] [setting] <value>
  25. With three args, set the setting to the value.
  26. With two args, just show the setting.
  27. With -d, delete the setting.
  28. With no args, lists all currently set values.
  29. [section] section to use (required)
  30. [setting] setting to use (required)
  31. <value> value to set (optional)
  32. -d --delete delete the setting (no value)
  33. -a --all list all configuration, not just the database values
  34. END_OF_SETCONFIG_HELP;
  35. require_once INSTALLDIR.'/scripts/commandline.inc';
  36. if (empty($args)) {
  37. if (have_option('a', 'all')) {
  38. foreach ($config as $section => $section_value) {
  39. foreach ($section_value as $setting => $value) {
  40. if (have_option('v', 'verbose') || !is_array($value)) {
  41. // Don't print array's without the verbose flag
  42. printf("%-20s %-20s %s\n", $section, $setting, var_export($value, true));
  43. }
  44. }
  45. }
  46. } else {
  47. $count = 0;
  48. $config = new Config();
  49. $config->find();
  50. while ($config->fetch()) {
  51. $count++;
  52. printf("%-20s %-20s %s\n", $config->section, $config->setting,
  53. var_export($config->value, true));
  54. }
  55. if ($count == 0) {
  56. print "No configuration set in database for this site.\n";
  57. }
  58. }
  59. exit(0);
  60. }
  61. if (count($args) < 2 || count($args) > 3) {
  62. show_help();
  63. exit(1);
  64. }
  65. $section = $args[0];
  66. $setting = $args[1];
  67. if (count($args) == 3) {
  68. $value = $args[2];
  69. } else {
  70. $value = null;
  71. }
  72. try {
  73. if (have_option('d', 'delete')) { // Delete
  74. if (count($args) != 2) {
  75. show_help();
  76. exit(1);
  77. }
  78. if (have_option('v', 'verbose')) {
  79. print "Deleting setting $section/$setting...";
  80. }
  81. $setting = Config::pkeyGet(array('section' => $section,
  82. 'setting' => $setting));
  83. if (empty($setting)) {
  84. print "Not found.\n";
  85. } else {
  86. $result = $setting->delete();
  87. if ($result) {
  88. print "DONE.\n";
  89. } else {
  90. print "ERROR.\n";
  91. }
  92. }
  93. } else if (count($args) == 2) { // show
  94. if (have_option('v', 'verbose')) {
  95. print "$section/$setting = ";
  96. }
  97. $value = common_config($section, $setting);
  98. print "$value\n";
  99. } else { // set
  100. if (have_option('v', 'verbose')) {
  101. print "Setting $section/$setting...";
  102. }
  103. Config::save($section, $setting, $value);
  104. print "DONE.\n";
  105. }
  106. } catch (Exception $e) {
  107. print $e->getMessage() . "\n";
  108. exit(1);
  109. }