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 // Set various flags so we don't time out on long-running processes
34 ini_set("max_execution_time", "0");
35 ini_set("max_input_time", "0");
37 mb_internal_encoding('UTF-8');
39 require_once INSTALLDIR . '/extlib/Console/Getopt.php';
41 // Note: $shortoptions and $longoptions should be pre-defined!
43 $_default_shortoptions = 'qvhc:s:p:';
45 $_default_longoptions = array('quiet', 'verbose', 'help', 'conf=', 'server=', 'path=');
47 if (isset($shortoptions)) {
48 $shortoptions .= $_default_shortoptions;
50 $shortoptions = $_default_shortoptions;
53 if (isset($longoptions)) {
54 $longoptions = array_merge($longoptions, $_default_longoptions);
56 $longoptions = $_default_longoptions;
59 $parser = new Console_Getopt();
61 $result = $parser->getopt($argv, $shortoptions, $longoptions);
63 if (PEAR::isError($result)) {
64 print $result->getMessage()."\n";
67 list($options, $args) = $result;
74 $_default_help_text = <<<END_OF_DEFAULT
77 -q --quiet Quiet (little output)
78 -v --verbose Verbose (lots of output)
79 -c --conf=<filename> Use <filename> as config file
80 -s --server=<name> Use <name> as server name
81 -p --path=<path> Use <path> as path name
82 -h --help Show this message and quit.
85 if (isset($helptext)) {
88 print $_default_help_text;
92 foreach ($options as $option) {
107 $conffile = $option[1];
116 require_once INSTALLDIR . '/lib/common.php';
118 set_error_handler('common_error_handler');
120 // Set up the language infrastructure so we can localize anything that
121 // needs to be sent out to users, such as mail notifications.
122 common_init_language();
124 function _make_matches($opt, $alt)
128 if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) {
129 $matches[] = '--'.$opt;
135 if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) {
136 $matches[] = '--'.$alt;
145 function have_option($opt, $alt=null)
149 $matches = _make_matches($opt, $alt);
151 foreach ($options as $option) {
152 if (in_array($option[0], $matches)) {
160 function get_option_value($opt, $alt=null)
164 $matches = _make_matches($opt, $alt);
166 foreach ($options as $option) {
167 if (in_array($option[0], $matches)) {
175 class NoUserArgumentException extends Exception
183 if (have_option('i', 'id')) {
184 $id = get_option_value('i', 'id');
185 $user = User::getKV('id', $id);
187 throw new Exception("Can't find user with id '$id'.");
189 } else if (have_option('n', 'nickname')) {
190 $nickname = get_option_value('n', 'nickname');
191 $user = User::getKV('nickname', $nickname);
193 throw new Exception("Can't find user with nickname '$nickname'");
196 throw new NoUserArgumentException("No user argument specified.");
202 /** "Printf not quiet" */
206 if (have_option('q', 'quiet')) {
210 $cargs = func_num_args();
216 $args = func_get_args();
217 $format = array_shift($args);
219 return vprintf($format, $args);
222 /** "Print when verbose" */
226 if (!have_option('v', 'verbose')) {
230 $cargs = func_num_args();
236 $args = func_get_args();
237 $format = array_shift($args);
239 return vprintf($format, $args);