1: <?php
2:
3: namespace cli_import;
4:
5: require_once SHARED . 'classes/CLI_Command.php';
6:
7: interface Importer {
8:
9: 10: 11: 12: 13: 14: 15: 16:
17: static function import($options);
18: }
19:
20: define('ERR_ILLEGAL_FILE_FORMAT', 'Unsupported file format. Please recheck');
21:
22: 23: 24: 25:
26: abstract class AbstractImporter implements \CLI_Command, Importer {
27:
28: 29: 30: 31: 32: 33: 34: 35: 36:
37: public static function CLI_getCommand(\Console_CommandLine $parser) {
38: $command = $parser->addCommand(call_user_func(array(get_called_class(), 'CLI_commandName')), array(
39: 'description' => call_user_func(array(get_called_class(), 'CLI_commandDescription'))
40: ));
41:
42: $command->add_help_option = false;
43:
44: $command->addOption('help', array(
45: 'short_name' => '-h',
46: 'long_name' => '--help',
47: 'action' => 'Help',
48: 'action_params' => array('class' => get_called_class()),
49: 'description' => 'show this help message and exit'
50: ));
51:
52: $command->addOption('organism_id', array(
53: 'short_name' => '-o',
54: 'long_name' => '--organism_id',
55: 'description' => 'id of the organism this import is for'
56: ));
57: $command->addOption('release', array(
58: 'short_name' => '-r',
59: 'long_name' => '--release',
60: 'description' => 'this will be used as prefix for all uniquenames and displayed in the "dataset" dropdown'
61: ));
62:
63: $command->addArgument('files', array(
64: 'multiple' => true,
65: 'description' => 'files to be imported'
66: ));
67:
68: return $command;
69: }
70:
71: 72: 73: 74: 75:
76: public static function CLI_checkRequiredOpts(\Console_CommandLine_Result $command) {
77: $options = $command->options;
78:
79: self::dieOnMissingArg($options, 'organism_id');
80: self::dieOnMissingArg($options, 'release');
81: }
82:
83: 84: 85: 86: 87: 88:
89: static function CLI_execute(\Console_CommandLine_Result $command, \Console_CommandLine $parser) {
90:
91: define('LINES_IMPORTED', 'datasets_imported');
92: define('DB_ORGANISM_ID', $command->options['organism_id']);
93: define('IMPORT_PREFIX', $command->options['release']);
94:
95: $command_name = call_user_func(array(get_called_class(), 'CLI_commandName'));
96: $command_options = $command->options;
97: $command_args = $command->args;
98:
99:
100: foreach ($command_args['files'] as $filename) {
101:
102: printf("importing %s as %s\n", $filename, $command_name);
103:
104: $ret_table = call_user_func(array(get_called_class(), 'import'), array_merge($command_options, array('file' => $filename)));
105:
106: $tbl = new Console_Table();
107: foreach ($ret_table as $key => $value)
108: $tbl->addRow(array($key, $value));
109: echo $tbl->getTable();
110: }
111:
112: echo "\nupdating materialized views...";
113: global $db;
114: $db->query('SELECT update_materialized_views()');
115: echo " done!\n";
116: }
117:
118: static function preCommitMsg() {
119: echo "\ncommiting changes to database. this may take a moment.\n";
120: }
121:
122: 123: 124: 125:
126: public static $log;
127:
128: 129: 130: 131:
132: private static $bar;
133:
134: 135: 136: 137:
138: private static $announce_steps = 100;
139:
140: 141: 142: 143:
144: private static $barstr = '[%bar%] %fraction%(%percent%), elapsed: %elapsed% , remaining est.: %estimate%';
145:
146: 147: 148: 149:
150: protected static function setLineCount($count) {
151: $width_exec = exec('tput cols 2>&1');
152: $width = is_int($width_exec) && $width_exec > 0 ? $width_exec : 120;
153:
154: if (self::$bar == null) {
155: self::$bar = new \Console_ProgressBar(self::$barstr, '=>', ' ', $width, $count);
156: } else {
157: self::$bar->reset(self::$barstr, '=>', ' ', $width, $count);
158: }
159: }
160:
161: 162: 163: 164: 165:
166: protected static function updateProgress($current_count) {
167: if ($current_count % self::$announce_steps != 0)
168: return;
169: self::$bar->update($current_count);
170: }
171:
172: 173: 174: 175: 176: 177:
178: protected static function dieOnMissingArg($options, $argname) {
179: if (!isset($options[$argname]))
180: throw new \Exception(sprintf('option --%s has to be set', $argname));
181: }
182:
183: }
184:
185:
186: AbstractImporter::$log = \Log::factory('console', '', 'Importer');
187: ?>
188: