commandline.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /*
  3. * StatusNet - a distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. // -*- mode: php -*-
  20. # Abort if called from a web server
  21. if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
  22. print "This script must be run from the command line\n";
  23. exit();
  24. }
  25. define('GNUSOCIAL', true);
  26. define('STATUSNET', true); //compatibility
  27. define('GNUSOCIAL_CLI', true); // to know we're in a CLI environment
  28. // Set various flags so we don't time out on long-running processes
  29. ini_set("max_execution_time", "0");
  30. ini_set("max_input_time", "0");
  31. set_time_limit(0);
  32. mb_internal_encoding('UTF-8');
  33. error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
  34. // Add extlib to our path so we can get Console_Getopt
  35. $_extra_path = array(INSTALLDIR.'/extlib/');
  36. set_include_path(implode(PATH_SEPARATOR, $_extra_path) . PATH_SEPARATOR . get_include_path());
  37. require_once 'Console/Getopt.php';
  38. // Note: $shortoptions and $longoptions should be pre-defined!
  39. $_default_shortoptions = 'qvhc:s:p:';
  40. $_default_longoptions = array('quiet', 'verbose', 'help', 'conf=', 'server=', 'path=');
  41. if (isset($shortoptions)) {
  42. $shortoptions .= $_default_shortoptions;
  43. } else {
  44. $shortoptions = $_default_shortoptions;
  45. }
  46. if (isset($longoptions)) {
  47. $longoptions = array_merge($longoptions, $_default_longoptions);
  48. } else {
  49. $longoptions = $_default_longoptions;
  50. }
  51. $parser = new Console_Getopt();
  52. $result = $parser->getopt($argv, $shortoptions, $longoptions);
  53. if (PEAR::isError($result)) {
  54. print $result->getMessage()."\n";
  55. exit(1);
  56. } else {
  57. list($options, $args) = $result;
  58. }
  59. function show_help()
  60. {
  61. global $helptext;
  62. $_default_help_text = <<<END_OF_DEFAULT
  63. General options:
  64. -q --quiet Quiet (little output)
  65. -v --verbose Verbose (lots of output)
  66. -c --conf=<filename> Use <filename> as config file
  67. -s --server=<name> Use <name> as server name
  68. -p --path=<path> Use <path> as path name
  69. -h --help Show this message and quit.
  70. END_OF_DEFAULT;
  71. if (isset($helptext)) {
  72. print $helptext;
  73. }
  74. print $_default_help_text;
  75. exit(0);
  76. }
  77. foreach ($options as $option) {
  78. switch ($option[0]) {
  79. case '--server':
  80. case 's':
  81. $server = $option[1];
  82. break;
  83. case '--path':
  84. case 'p':
  85. $path = $option[1];
  86. break;
  87. case '--conf':
  88. case 'c':
  89. $conffile = $option[1];
  90. break;
  91. case '--help':
  92. case 'h':
  93. show_help();
  94. }
  95. }
  96. require_once INSTALLDIR . '/lib/common.php';
  97. set_error_handler('common_error_handler');
  98. // Set up the language infrastructure so we can localize anything that
  99. // needs to be sent out to users, such as mail notifications.
  100. common_init_language();
  101. function _make_matches($opt, $alt)
  102. {
  103. $matches = array();
  104. if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) {
  105. $matches[] = '--'.$opt;
  106. } else {
  107. $matches[] = $opt;
  108. }
  109. if (!empty($alt)) {
  110. if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) {
  111. $matches[] = '--'.$alt;
  112. } else {
  113. $matches[] = $alt;
  114. }
  115. }
  116. return $matches;
  117. }
  118. function have_option($opt, $alt=null)
  119. {
  120. global $options;
  121. $matches = _make_matches($opt, $alt);
  122. foreach ($options as $option) {
  123. if (in_array($option[0], $matches)) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. function get_option_value($opt, $alt=null)
  130. {
  131. global $options;
  132. $matches = _make_matches($opt, $alt);
  133. foreach ($options as $option) {
  134. if (in_array($option[0], $matches)) {
  135. return $option[1];
  136. }
  137. }
  138. return null;
  139. }
  140. class NoUserArgumentException extends Exception
  141. {
  142. }
  143. function getUser()
  144. {
  145. $user = null;
  146. if (have_option('i', 'id')) {
  147. $id = get_option_value('i', 'id');
  148. $user = User::getKV('id', $id);
  149. if (empty($user)) {
  150. throw new Exception("Can't find user with id '$id'.");
  151. }
  152. } else if (have_option('n', 'nickname')) {
  153. $nickname = get_option_value('n', 'nickname');
  154. $user = User::getKV('nickname', $nickname);
  155. if (empty($user)) {
  156. throw new Exception("Can't find user with nickname '$nickname'");
  157. }
  158. } else {
  159. throw new NoUserArgumentException("No user argument specified.");
  160. }
  161. return $user;
  162. }
  163. /** "Printf not quiet" */
  164. function printfnq()
  165. {
  166. if (have_option('q', 'quiet')) {
  167. return null;
  168. }
  169. $cargs = func_num_args();
  170. if ($cargs == 0) {
  171. return 0;
  172. }
  173. $args = func_get_args();
  174. $format = array_shift($args);
  175. return vprintf($format, $args);
  176. }
  177. /** "Print when verbose" */
  178. function printfv()
  179. {
  180. if (!have_option('v', 'verbose')) {
  181. return null;
  182. }
  183. $cargs = func_num_args();
  184. if ($cargs == 0) {
  185. return 0;
  186. }
  187. $args = func_get_args();
  188. $format = array_shift($args);
  189. return vprintf($format, $args);
  190. }