1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 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: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 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: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450:
<?php
global $legiscan_options;
function legiscan_getopt($valid_options = array(), $extra = '')
{
global $argv, $argc, $legiscan_options;
if (!empty($valid_options))
{
$parameters['short'] = array(
'h',
);
$parameters['long'] = array_merge($valid_options, array('help','version'));
}
$opt_short = implode('', array_values($parameters['short']));
$opt_long = array_values($parameters['long']);
$legiscan_options = getopt($opt_short, $opt_long);
if (isset($legiscan_options['h']) || isset($legiscan_options['help']) || $legiscan_options === false)
{
legiscan_help($parameters['long'], $extra);
}
if (isset($legiscan_options['version']))
{
echo $argv[0] . ' ' . LegiScan::VERSION . "\n";
exit(0);
}
$pruneargv = array();
foreach ($legiscan_options as $option => $value)
{
if (!$value)
$legiscan_options[$option] = true;
foreach ($argv as $key => $chunk)
{
$regex = '/^'. (isset($option[1]) ? '--' : '-') . $option . '/';
if ($chunk == $value && $argv[$key-1][0] == '-' || preg_match($regex, $chunk))
{
array_push($pruneargv, $key);
$argc--;
}
}
}
while ($key = array_pop($pruneargv))
unset($argv[$key]);
$argv = array_values($argv);
return $legiscan_options;
}
function legiscan_option($option, $default = false)
{
global $legiscan_options;
if (isset($legiscan_options[$option]))
return $legiscan_options[$option];
else
return $default;
}
function legiscan_help($parameters, $extra = '')
{
global $argv;
$help = array(
"monitoredlist" => "--monitoredlist \tRetrieve monitored list for GAITS account",
"sessionlist:" => "--sessionlist STATE \tRetrieve session list info for STATE or ALL",
"masterlist:" => "--masterlist STATE \tRetrieve master list for STATE",
"bill:" => "--bill BILL_ID \tRetrieve bill detail payload for BILL_ID",
"text:" => "--text DOC_ID \tRetrieve text payload for DOC_ID",
"amendment:" => "--amendment AMEND_ID\tRetrieve amendment payload for AMEND_ID",
"supplement:" => "--supplement SUP_ID \tRetrieve supplement payload for SUP_ID",
"vote:" => "--vote ROLL_CALL_ID \tRetrieve roll call detail payload for ROLL_CALL_ID",
"people:" => "--people PEOPLE_ID \tRetrieve people detail payload for PEOPLE_ID",
"search:" => "--search QUERY \tRetrieve search results for QUERY",
"state:" => "--state STATE \tSet STATE for query, defaults to all states",
"score:" => "--score RELEVANCE \tSet RELEVANCE score cutoff (0-100) defaults to 50",
"import:" => "--import ACTION \tTake ACTION on search/masterlist results: New, Changed, All",
"key:" => "--key API_KEY \tOverride legiscan.conf api_key with API_KEY to use for request",
"sync" => "--sync \tSync local monitor/ignore operations to GAITS account",
"monitor:" => "--monitor BILL_ID \tAdd BILL_ID to ls_monitor list",
"unmonitor:" => "--unmonitor BILL_ID \tRemove BILL_ID from ls_monitor list",
"ignore:" => "--ignore BILL_ID \tAdd BILL_ID to ls_ignore list",
"unignore:" => "--unignore BILL_ID \tRemove BILL_ID from ls_ignore list",
"stance:" => "--stance STANCE \tStance of monitored bill (0=watch,1=support,2=oppose)",
"clean" => "--clean \tClean the API cache of stale records",
"bulk" => "--bulk \tRun a bulk update based on config.php settings",
"scan" => "--scan \tScan the available dataset archives",
"import" => "--import \tImport selected bulk dataset archives",
"year:" => "--year YEAR \tSet YEAR for filtering results",
"session:" => "--session SESSION_ID\tFilter for specific SESSION_IDs",
"skip:" => "--skip SESSION_ID \tFilter against specific SESSION_IDs",
"file:" => "--file ZIP \tDataset zip archive to process",
"regular" => "--regular \tFlag only regular session datasets",
"special" => "--special \tFlag only special session datasets",
"reset-db" => "--reset-db \tTruncate and reset database tables back to clean production install",
"dry-run" => "--dry-run \tDon't take action only print out what would have been done",
"yes" => "--yes \tAutomatically answer Yes to any Yes/No prompts",
"verbose" => "--verbose \tBe verbose in general",
"debug" => "--debug \tBe more verbose specifically, maybe dump some JSON payload",
"version" => "--version \tShow the application version number",
"daemon" => "--daemon \tRun infinitely (otherwise update and exit)",
"help" => "--help \tDisplay this help text",
);
echo "Usage: " . basename($argv[0]) . " [OPTIONS]\n\n";
if ($extra)
echo trim($extra) . "\n\n";
foreach ($parameters as $opt)
{
if (isset($help[$opt]))
echo " {$help[$opt]}\n";
else
echo sprintf(" --%-16s\t ***** NO HELP AVAILABLE *****\n", $opt);
}
exit(0);
}
function expect_user($expect)
{
$user = posix_getpwuid(posix_geteuid());
$whoami = $user['name'];
if (strcasecmp($expect, $whoami) == 0)
return true;
else
return false;
}
function yes_no_prompt($default = false)
{
$answer = $default;
if (is_cli())
{
echo 'Are you sure you want to do this? (y/N) ';
$handle = fopen('php://stdin','r');
$line = fgets($handle);
fclose($handle);
$response = substr(strtolower(trim($line)), 0, 1);
if ($response == 'y')
$answer = true;
else
$answer = false;
}
return $answer;
}
function is_cli()
{
static $is_cli;
if (!isset($is_cli))
{
if (php_sapi_name() == "cli")
$is_cli = true;
else
$is_cli = false;
}
return $is_cli;
}
function memory_usage()
{
$m1 = number_format(memory_get_usage() / 1048576, 1) . 'MB';
$m2 = number_format(memory_get_usage(true) / 1048576, 1) . 'MB';
$mp1 = number_format(memory_get_peak_usage() / 1048576, 1) . 'MB';
$mp2 = number_format(memory_get_peak_usage(true) / 1048576, 1) . 'MB';
$str = "Memory: $m1 (Real: $m2) -- Peak: $mp1 (Real: $mp2)";
return $str;
}
function sec2hms($num_secs) {
$str = '';
$hours = intval(intval($num_secs) / 3600);
$str .= sprintf('%02d', $hours) . ':';
$minutes = intval(((intval($num_secs) / 60) % 60));
if ($minutes < 10) $str .= '0';
$str .= $minutes.':';
$seconds = intval(intval(($num_secs % 60)));
if ($seconds < 10) $str .= '0';
$str .= $seconds;
return($str);
}
function prettyPrint($json)
{
$indent_char = ' ';
$result = '';
$level = 0;
$prev_char = '';
$in_quotes = false;
$ends_line_level = null;
$json_length = strlen($json);
for ($i = 0; $i < $json_length; $i++)
{
$char = $json[$i];
$new_line_level = null;
$post = '';
if ($ends_line_level !== null)
{
$new_line_level = $ends_line_level;
$ends_line_level = null;
}
if ($char === '"' && $prev_char != "\\")
{
$in_quotes = !$in_quotes;
}
elseif (!$in_quotes)
{
switch ($char)
{
case '}': case ']':
$level--;
$ends_line_level = null;
$new_line_level = $level;
break;
case '{': case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':':
$post = ' ';
break;
case ' ': case "\t": case "\n": case "\r":
$char = '';
$ends_line_level = $new_line_level;
$new_line_level = null;
break;
}
}
if ($new_line_level !== null)
{
$result .= "\n" . str_repeat($indent_char, $new_line_level);
}
$result .= $char . $post;
$prev_char = $char;
}
return $result;
}
function timer_clear($name)
{
global $timers;
$timers[$name] = array();
}
function timer_read($name)
{
global $timers;
$stop = microtime($true);
$diff = round(($stop - $timers[$name]['start']), 2);
return $timers[$name]['time'] + $diff;
}
function timer_start($name)
{
global $timers;
$timers[$name]['start'] = microtime(true);
$timers[$name]['count'] = isset($timers[$name]['count']) ? ++$timers[$name]['count'] : 1;
}
function timer_stop($name)
{
global $timers;
$stop = microtime(true);
$diff = round(($stop - $timers[$name]['start']) , 2);
$timers[$name]['time'] += $diff;
return $timers[$name];
}