commandline.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // Autoload composer dependencies
  35. require_once INSTALLDIR . '/vendor/autoload.php';
  36. // Add extlib to our path
  37. $_extra_path = [INSTALLDIR.'/extlib/'];
  38. set_include_path(implode(PATH_SEPARATOR, $_extra_path) . PATH_SEPARATOR . get_include_path());
  39. // Note: $shortoptions and $longoptions should be pre-defined!
  40. $_default_shortoptions = 'qvhc:s:p:';
  41. $_default_longoptions = array('quiet', 'verbose', 'help', 'conf=', 'server=', 'path=');
  42. if (isset($shortoptions)) {
  43. $shortoptions .= $_default_shortoptions;
  44. } else {
  45. $shortoptions = $_default_shortoptions;
  46. }
  47. if (isset($longoptions)) {
  48. $longoptions = array_merge($longoptions, $_default_longoptions);
  49. } else {
  50. $longoptions = $_default_longoptions;
  51. }
  52. $parser = new Console_Getopt();
  53. $result = $parser->getopt($argv, $shortoptions, $longoptions);
  54. if (PEAR::isError($result)) {
  55. print $result->getMessage()."\n";
  56. exit(1);
  57. } else {
  58. list($options, $args) = $result;
  59. }
  60. function show_help()
  61. {
  62. global $helptext;
  63. $_default_help_text = <<<END_OF_DEFAULT
  64. General options:
  65. -q --quiet Quiet (little output)
  66. -v --verbose Verbose (lots of output)
  67. -c --conf=<filename> Use <filename> as config file
  68. -s --server=<name> Use <name> as server name
  69. -p --path=<path> Use <path> as path name
  70. -h --help Show this message and quit.
  71. END_OF_DEFAULT;
  72. if (isset($helptext)) {
  73. print $helptext;
  74. }
  75. print $_default_help_text;
  76. exit(0);
  77. }
  78. foreach ($options as $option) {
  79. switch ($option[0]) {
  80. case '--server':
  81. case 's':
  82. $server = $option[1];
  83. break;
  84. case '--path':
  85. case 'p':
  86. $path = $option[1];
  87. break;
  88. case '--conf':
  89. case 'c':
  90. $conffile = $option[1];
  91. break;
  92. case '--help':
  93. case 'h':
  94. show_help();
  95. }
  96. }
  97. require_once INSTALLDIR . '/lib/util/common.php';
  98. set_error_handler('common_error_handler');
  99. // Set up the language infrastructure so we can localize anything that
  100. // needs to be sent out to users, such as mail notifications.
  101. common_init_language();
  102. function _make_matches($opt, $alt)
  103. {
  104. $matches = array();
  105. if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) {
  106. $matches[] = '--'.$opt;
  107. } else {
  108. $matches[] = $opt;
  109. }
  110. if (!empty($alt)) {
  111. if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) {
  112. $matches[] = '--'.$alt;
  113. } else {
  114. $matches[] = $alt;
  115. }
  116. }
  117. return $matches;
  118. }
  119. function have_option($opt, $alt=null)
  120. {
  121. global $options;
  122. $matches = _make_matches($opt, $alt);
  123. foreach ($options as $option) {
  124. if (in_array($option[0], $matches)) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. function get_option_value($opt, $alt=null)
  131. {
  132. global $options;
  133. $matches = _make_matches($opt, $alt);
  134. foreach ($options as $option) {
  135. if (in_array($option[0], $matches)) {
  136. return $option[1];
  137. }
  138. }
  139. return null;
  140. }
  141. class NoUserArgumentException extends Exception
  142. {
  143. }
  144. function getUser()
  145. {
  146. $user = null;
  147. if (have_option('i', 'id')) {
  148. $id = get_option_value('i', 'id');
  149. $user = User::getKV('id', $id);
  150. if (empty($user)) {
  151. throw new Exception("Can't find user with id '$id'.");
  152. }
  153. } else if (have_option('n', 'nickname')) {
  154. $nickname = get_option_value('n', 'nickname');
  155. $user = User::getKV('nickname', $nickname);
  156. if (empty($user)) {
  157. throw new Exception("Can't find user with nickname '$nickname'");
  158. }
  159. } else {
  160. throw new NoUserArgumentException("No user argument specified.");
  161. }
  162. return $user;
  163. }
  164. /** "Printf not quiet" */
  165. function printfnq()
  166. {
  167. if (have_option('q', 'quiet')) {
  168. return null;
  169. }
  170. $cargs = func_num_args();
  171. if ($cargs == 0) {
  172. return 0;
  173. }
  174. $args = func_get_args();
  175. $format = array_shift($args);
  176. return vprintf($format, $args);
  177. }
  178. /** "Print when verbose" */
  179. function printfv()
  180. {
  181. if (!have_option('v', 'verbose')) {
  182. return null;
  183. }
  184. $cargs = func_num_args();
  185. if ($cargs == 0) {
  186. return 0;
  187. }
  188. $args = func_get_args();
  189. $format = array_shift($args);
  190. return vprintf($format, $args);
  191. }