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
20
// -*- mode: php -*-
21
22
# Abort if called from a web server
23
24
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
25
    print "This script must be run from the command line\n";
26
    exit();
27
}
28
29
define('STATUSNET', true);
30
define('LACONICA', true); // compatibility
31
32
// Set various flags so we don't time out on long-running processes
33
34
ini_set("max_execution_time", "0");
35
ini_set("max_input_time", "0");
36
set_time_limit(0);
37
mb_internal_encoding('UTF-8');
38
39
// Add extlib to our path so we can get Console_Getopt
40
41
$_extra_path = array(INSTALLDIR.'/extlib/');
42
43
set_include_path(implode(PATH_SEPARATOR, $_extra_path) . PATH_SEPARATOR . get_include_path());
44
45
require_once 'Console/Getopt.php';
46
47
// Note: $shortoptions and $longoptions should be pre-defined!
48
49
$_default_shortoptions = 'qvhc:s:p:';
50
51
$_default_longoptions = array('quiet', 'verbose', 'help', 'conf=', 'server=', 'path=');
52
53
if (isset($shortoptions)) {
54
    $shortoptions .= $_default_shortoptions;
55
} else {
56
    $shortoptions = $_default_shortoptions;
57
}
58
59
if (isset($longoptions)) {
60
    $longoptions = array_merge($longoptions, $_default_longoptions);
61
} else {
62
    $longoptions = $_default_longoptions;
63
}
64
65
$parser = new Console_Getopt();
66
67
$result = $parser->getopt($argv, $shortoptions, $longoptions);
68
69
if (PEAR::isError($result)) {
70
    print $result->getMessage()."\n";
71
    exit(1);
72
} else {
73
    list($options, $args) = $result;
74
}
75
76
function show_help()
77
{
78
    global $helptext;
79
80
    $_default_help_text = <<<END_OF_DEFAULT
81
      General options:
82
83
    -q --quiet           Quiet (little output)
84
    -v --verbose         Verbose (lots of output)
85
    -c --conf=<filename> Use <filename> as config file
86
    -s --server=<name>   Use <name> as server name
87
    -p --path=<path>     Use <path> as path name
88
    -h --help            Show this message and quit.
89
90
END_OF_DEFAULT;
91
    if (isset($helptext)) {
92
        print $helptext;
93
    }
94
    print $_default_help_text;
95
    exit(0);
96
}
97
98
foreach ($options as $option) {
99
100
    switch ($option[0]) {
101
     case '--server':
102
     case 's':
103
        $server = $option[1];
104
        break;
105
106
     case '--path':
107
     case 'p':
108
        $path = $option[1];
109
        break;
110
111
     case '--conf':
112
     case 'c':
113
        $conffile = $option[1];
114
        break;
115
116
     case '--help':
117
     case 'h':
118
        show_help();
119
    }
120
}
121
122
require_once INSTALLDIR . '/lib/common.php';
123
124
set_error_handler('common_error_handler');
125
126
// Set up the language infrastructure so we can localize anything that
127
// needs to be sent out to users, such as mail notifications.
128
common_init_language();
129
130
function _make_matches($opt, $alt)
131
{
132
    $matches = array();
133
134
    if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) {
135
        $matches[] = '--'.$opt;
136
    } else {
137
        $matches[] = $opt;
138
    }
139
140
    if (!empty($alt)) {
141
        if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) {
142
            $matches[] = '--'.$alt;
143
        } else {
144
            $matches[] = $alt;
145
        }
146
    }
147
148
    return $matches;
149
}
150
151
function have_option($opt, $alt=null)
152
{
153
    global $options;
154
155
    $matches = _make_matches($opt, $alt);
156
157
    foreach ($options as $option) {
158
        if (in_array($option[0], $matches)) {
159
            return true;
160
        }
161
    }
162
163
    return false;
164
}
165
166
function get_option_value($opt, $alt=null)
167
{
168
    global $options;
169
170
    $matches = _make_matches($opt, $alt);
171
172
    foreach ($options as $option) {
173
        if (in_array($option[0], $matches)) {
174
            return $option[1];
175
        }
176
    }
177
178
    return null;
179
}
180
181
class NoUserArgumentException extends Exception
182
{
183
}
184
185
function getUser()
186
{
187
    $user = null;
188
189
    if (have_option('i', 'id')) {
190
        $id = get_option_value('i', 'id');
191
        $user = User::staticGet('id', $id);
192
        if (empty($user)) {
193
            throw new Exception("Can't find user with id '$id'.");
194
        }
195
    } else if (have_option('n', 'nickname')) {
196
        $nickname = get_option_value('n', 'nickname');
197
        $user = User::staticGet('nickname', $nickname);
198
        if (empty($user)) {
199
            throw new Exception("Can't find user with nickname '$nickname'");
200
        }
201
    } else {
202
        throw new NoUserArgumentException("No user argument specified.");
203
    }
204
205
    return $user;
206
}
207
208
/** "Printf not quiet" */
209
210
function printfnq()
211
{
212
    if (have_option('q', 'quiet')) {
213
        return null;
214
    }
215
216
    $cargs  = func_num_args();
217
218
    if ($cargs == 0) {
219
        return 0;
220
    }
221
222
    $args   = func_get_args();
223
    $format = array_shift($args);
224
225
    return vprintf($format, $args);
226
}
227
228
/** "Print when verbose" */
229
230
function printfv()
231
{
232
    if (!have_option('v', 'verbose')) {
233
        return null;
234
    }
235
236
    $cargs  = func_num_args();
237
238
    if ($cargs == 0) {
239
        return 0;
240
    }
241
242
    $args   = func_get_args();
243
    $format = array_shift($args);
244
245
    return vprintf($format, $args);
246
}