3 * StatusNet - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
22 # Abort if called from a web server
24 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
25 print "This script must be run from the command line\n";
29 define('GNUSOCIAL', true);
30 define('STATUSNET', true); //compatibility
32 define('GNUSOCIAL_CLI', true); // to know we're in a CLI environment
34 // Set various flags so we don't time out on long-running processes
36 ini_set("max_execution_time", "0");
37 ini_set("max_input_time", "0");
39 mb_internal_encoding('UTF-8');
41 # DEBUG: error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
43 // Add extlib to our path so we can get Console_Getopt
45 $_extra_path = array(INSTALLDIR.'/extlib/');
47 set_include_path(implode(PATH_SEPARATOR, $_extra_path) . PATH_SEPARATOR . get_include_path());
49 require_once 'Console/Getopt.php';
51 // Note: $shortoptions and $longoptions should be pre-defined!
53 $_default_shortoptions = 'qvhc:s:p:';
55 $_default_longoptions = array('quiet', 'verbose', 'help', 'conf=', 'server=', 'path=');
57 if (isset($shortoptions)) {
58 $shortoptions .= $_default_shortoptions;
60 $shortoptions = $_default_shortoptions;
63 if (isset($longoptions)) {
64 $longoptions = array_merge($longoptions, $_default_longoptions);
66 $longoptions = $_default_longoptions;
69 $parser = new Console_Getopt();
71 $result = $parser->getopt($argv, $shortoptions, $longoptions);
73 if (PEAR::isError($result)) {
74 print $result->getMessage()."\n";
77 list($options, $args) = $result;
84 $_default_help_text = <<<END_OF_DEFAULT
87 -q --quiet Quiet (little output)
88 -v --verbose Verbose (lots of output)
89 -c --conf=<filename> Use <filename> as config file
90 -s --server=<name> Use <name> as server name
91 -p --path=<path> Use <path> as path name
92 -h --help Show this message and quit.
95 if (isset($helptext)) {
98 print $_default_help_text;
102 foreach ($options as $option) {
104 switch ($option[0]) {
107 $server = $option[1];
117 $conffile = $option[1];
126 require_once INSTALLDIR . '/lib/common.php';
128 set_error_handler('common_error_handler');
130 // Set up the language infrastructure so we can localize anything that
131 // needs to be sent out to users, such as mail notifications.
132 common_init_language();
134 function _make_matches($opt, $alt)
138 if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) {
139 $matches[] = '--'.$opt;
145 if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) {
146 $matches[] = '--'.$alt;
155 function have_option($opt, $alt=null)
159 $matches = _make_matches($opt, $alt);
161 foreach ($options as $option) {
162 if (in_array($option[0], $matches)) {
170 function get_option_value($opt, $alt=null)
174 $matches = _make_matches($opt, $alt);
176 foreach ($options as $option) {
177 if (in_array($option[0], $matches)) {
185 class NoUserArgumentException extends Exception
193 if (have_option('i', 'id')) {
194 $id = get_option_value('i', 'id');
195 $user = User::getKV('id', $id);
197 throw new Exception("Can't find user with id '$id'.");
199 } else if (have_option('n', 'nickname')) {
200 $nickname = get_option_value('n', 'nickname');
201 $user = User::getKV('nickname', $nickname);
203 throw new Exception("Can't find user with nickname '$nickname'");
206 throw new NoUserArgumentException("No user argument specified.");
212 /** "Printf not quiet" */
216 if (have_option('q', 'quiet')) {
220 $cargs = func_num_args();
226 $args = func_get_args();
227 $format = array_shift($args);
229 return vprintf($format, $args);
232 /** "Print when verbose" */
236 if (!have_option('v', 'verbose')) {
240 $cargs = func_num_args();
246 $args = func_get_args();
247 $format = array_shift($args);
249 return vprintf($format, $args);