commandline.inc 5.44 KB
Newer Older
1 2
<?php
/*
3 4
 * StatusNet - a distributed open-source microblogging tool
 * Copyright (C) 2008, 2009, StatusNet, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// -*- mode: php -*-

# Abort if called from a web server

if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
    print "This script must be run from the command line\n";
    exit();
}

29
define('STATUSNET', true);
30
define('LACONICA', true); // compatibility
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

// Set various flags so we don't time out on long-running processes

ini_set("max_execution_time", "0");
ini_set("max_input_time", "0");
set_time_limit(0);
mb_internal_encoding('UTF-8');

// Add extlib to our path so we can get Console_Getopt

$_extra_path = array(INSTALLDIR.'/extlib/');

set_include_path(implode(PATH_SEPARATOR, $_extra_path) . PATH_SEPARATOR . get_include_path());

require_once 'Console/Getopt.php';

// Note: $shortoptions and $longoptions should be pre-defined!

$_default_shortoptions = 'qvhc:s:p:';

$_default_longoptions = array('quiet', 'verbose', 'help', 'conf=', 'server=', 'path=');

if (isset($shortoptions)) {
    $shortoptions .= $_default_shortoptions;
} else {
    $shortoptions = $_default_shortoptions;
}

if (isset($longoptions)) {
    $longoptions = array_merge($longoptions, $_default_longoptions);
} else {
    $longoptions = $_default_longoptions;
}

$parser = new Console_Getopt();

67 68 69
$result = $parser->getopt($argv, $shortoptions, $longoptions);

if (PEAR::isError($result)) {
Evan Prodromou's avatar
Evan Prodromou committed
70 71
    print $result->getMessage()."\n";
    exit(1);
72
} else {
Evan Prodromou's avatar
Evan Prodromou committed
73
    list($options, $args) = $result;
74
}
75

Evan Prodromou's avatar
Evan Prodromou committed
76 77 78 79 80
function show_help()
{
    global $helptext;

    $_default_help_text = <<<END_OF_DEFAULT
Evan Prodromou's avatar
Evan Prodromou committed
81
      General options:
Evan Prodromou's avatar
Evan Prodromou committed
82 83 84 85 86 87 88 89 90

    -q --quiet           Quiet (little output)
    -v --verbose         Verbose (lots of output)
    -c --conf=<filename> Use <filename> as config file
    -s --server=<name>   Use <name> as server name
    -p --path=<path>     Use <path> as path name
    -h --help            Show this message and quit.

END_OF_DEFAULT;
Evan Prodromou's avatar
Evan Prodromou committed
91 92 93 94 95
    if (isset($helptext)) {
        print $helptext;
    }
    print $_default_help_text;
    exit(0);
Evan Prodromou's avatar
Evan Prodromou committed
96 97
}

98 99 100 101
foreach ($options as $option) {

    switch ($option[0]) {
     case '--server':
102
     case 's':
103 104 105 106
        $server = $option[1];
        break;

     case '--path':
107
     case 'p':
108 109 110 111
        $path = $option[1];
        break;

     case '--conf':
112
     case 'c':
113 114 115 116
        $conffile = $option[1];
        break;

     case '--help':
117
     case 'h':
Evan Prodromou's avatar
Evan Prodromou committed
118
        show_help();
119 120 121 122
    }
}

require_once INSTALLDIR . '/lib/common.php';
Evan Prodromou's avatar
Evan Prodromou committed
123 124 125

set_error_handler('common_error_handler');

126 127 128 129
// Set up the language infrastructure so we can localize anything that
// needs to be sent out to users, such as mail notifications.
common_init_language();

130
function _make_matches($opt, $alt)
Evan Prodromou's avatar
Evan Prodromou committed
131
{
Evan Prodromou's avatar
Evan Prodromou committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    $matches = array();

    if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) {
        $matches[] = '--'.$opt;
    } else {
        $matches[] = $opt;
    }

    if (!empty($alt)) {
        if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) {
            $matches[] = '--'.$alt;
        } else {
            $matches[] = $alt;
        }
    }

148 149 150 151 152 153 154 155 156
    return $matches;
}

function have_option($opt, $alt=null)
{
    global $options;

    $matches = _make_matches($opt, $alt);

Evan Prodromou's avatar
Evan Prodromou committed
157 158 159 160 161 162 163
    foreach ($options as $option) {
        if (in_array($option[0], $matches)) {
            return true;
        }
    }

    return false;
Evan Prodromou's avatar
Evan Prodromou committed
164 165
}

166
function get_option_value($opt, $alt=null)
Evan Prodromou's avatar
Evan Prodromou committed
167
{
Evan Prodromou's avatar
Evan Prodromou committed
168 169
    global $options;

170
    $matches = _make_matches($opt, $alt);
Evan Prodromou's avatar
Evan Prodromou committed
171 172 173 174 175 176 177 178 179

    foreach ($options as $option) {
        if (in_array($option[0], $matches)) {
            return $option[1];
        }
    }

    return null;
}
180

181 182 183 184
class NoUserArgumentException extends Exception
{
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
function getUser()
{
    $user = null;

    if (have_option('i', 'id')) {
        $id = get_option_value('i', 'id');
        $user = User::staticGet('id', $id);
        if (empty($user)) {
            throw new Exception("Can't find user with id '$id'.");
        }
    } else if (have_option('n', 'nickname')) {
        $nickname = get_option_value('n', 'nickname');
        $user = User::staticGet('nickname', $nickname);
        if (empty($user)) {
            throw new Exception("Can't find user with nickname '$nickname'");
        }
    } else {
202
        throw new NoUserArgumentException("No user argument specified.");
203 204 205 206
    }

    return $user;
}
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

/** "Printf not quiet" */

function printfnq()
{
    if (have_option('q', 'quiet')) {
        return null;
    }

    $cargs  = func_num_args();

    if ($cargs == 0) {
        return 0;
    }

    $args   = func_get_args();
    $format = array_shift($args);

    return vprintf($format, $args);
}

/** "Print when verbose" */

function printfv()
{
    if (!have_option('v', 'verbose')) {
        return null;
    }

    $cargs  = func_num_args();

    if ($cargs == 0) {
        return 0;
    }

    $args   = func_get_args();
    $format = array_shift($args);

    return vprintf($format, $args);
}