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