Overview

Namespaces

  • cli_db
    • propel
      • map
      • om
  • cli_import
  • LoggedPDO
  • None
  • PHP
  • webservices
    • cart
    • combisearch
    • details
      • annotations
        • feature
    • graphs
      • barplot
      • genome
    • listing
    • queue

Classes

  • AbstractTable
  • Acquisition
  • Analysis
  • Assay
  • Biomaterial
  • Contact
  • Feature
  • Organism
  • Protocol
  • Publication
  • Quantification

Interfaces

  • Table
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace cli_db;
  4: 
  5: require_once SHARED . 'classes/CLI_Command.php';
  6: 
  7: interface Table {
  8: 
  9:     /**
 10:      * returns associative array of command parameters.
 11:      * key is command name, value is an array describing paramteters consisting of
 12:      * .  action: array of subcommands this parameter is available for. array key is subcommand name, value is one of optional|required
 13:      * .  description: description for help text
 14:      * .  colname: name of associated Propel column. Ucfirst. can be unset, but then this parameter will not be evaluated automatically
 15:      * .  all other keys will be passed to $submcd->addOption
 16:      */
 17:     static function getKeys();
 18: 
 19:     /**
 20:      * array of available subcommands
 21:      */
 22:     static function getSubCommands();
 23: 
 24:     /**
 25:      * namespace and class name of propel class this table is based on
 26:      */
 27:     static function getPropelClass();
 28: }
 29: 
 30: /**
 31:  * abstract class as parent for tbro-db commands.
 32:  * implements standard behavior for insert, update, delete, list and display subcommands
 33:  */
 34: abstract class AbstractTable implements \CLI_Command, Table {
 35: 
 36:     /**
 37:      * create subcommand for $command and add parameters
 38:      * @param \Console_CommandLine $command 
 39:      * @param string $subcommand_name subcommand name.
 40:      * @param array $keys returnvalue of Table::getKeys()
 41:      */
 42:     private static function processSubCommand($command, $subcommand_name, $keys) {
 43:         //adds a subcommand
 44:         $submcd = $command->addCommand($subcommand_name);
 45: 
 46:         //merge passed keys with standard values. standard values will be overwritten
 47:         $keys = array_merge(array(
 48:             'short' => array(
 49:                 'actions' => array(
 50:                     'insert' => 'optional',
 51:                 ),
 52:                 'description' => 'if set, will just output the ID of newly inserted line on success',
 53:                 'action' => 'StoreTrue'
 54:             ),
 55:             'noconfirm' => array(
 56:                 'actions' => array(
 57:                     'delete' => 'optional',
 58:                 ),
 59:                 'description' => 'if set, will not ask for confirmation on delete',
 60:                 'action' => 'StoreTrue'
 61:             ),
 62:                 ), $keys);
 63: 
 64:         // for all possible parameters
 65:         foreach ($keys as $key => $data) {
 66:             // if parameter is required or optional for this subcommand
 67:             if (isset($data['actions'][$subcommand_name]) && ($data['actions'][$subcommand_name] == 'optional' || $data['actions'][$subcommand_name] == 'required')) {
 68:                 //set standard options for $submcd->addOption
 69:                 $stdopts = array(
 70:                     'long_name' => '--' . $key,
 71:                     'help_name' => $key
 72:                 );
 73:                 //add extra options. ignore $data['actions'] and $data['colname'], these are not of interest to $submcd->addOption
 74:                 $extraopts = array_diff_key($data, array('actions' => null, 'colname' => null));
 75:                 $options = array_merge($stdopts, $extraopts);
 76:                 //add prefix (required) or (optional) to description
 77:                 $options['description'] = sprintf('(%2$s) %1$s', $data['description'], $data['actions'][$subcommand_name]);
 78:                 //add option to subcommand
 79:                 $option = $submcd->addOption($key, $options);
 80:             }
 81:         }
 82:     }
 83: 
 84:     /**
 85:      * adds a command representing this class to $parser
 86:      * @param \Console_CommandLine $parser
 87:      * @return \Console_CommandLine_Command
 88:      */
 89:     public static function CLI_getCommand(\Console_CommandLine $parser) {
 90:         //create command
 91:         $command = $parser->addCommand(call_user_func(array(get_called_class(), 'CLI_commandName')), array(
 92:             'description' => call_user_func(array(get_called_class(), 'CLI_commandDescription'))
 93:         ));
 94: 
 95:         //essentially self::getKeys(), but calling the overwriting getKeys function and not AbstractTable::getKeys()
 96:         $keys = call_user_func(array(get_called_class(), 'getKeys'));
 97:         //same with self::getSubCommands()
 98:         $subcommands = call_user_func(array(get_called_class(), 'getSubCommands'));
 99: 
100:         //add all subcommands
101:         foreach ($subcommands as $cmd) {
102:             self::processSubCommand($command, $cmd, $keys);
103:         }
104:         
105:         return $command;
106:     }
107: 
108:     /**
109:      * check if all required options have been set by the user
110:      * @param \Console_CommandLine_Result $command
111:      * @return nothing
112:      * @throws \Exception if an option is missing
113:      */
114:     public static function CLI_checkRequiredOpts(\Console_CommandLine_Result $command) {
115:         //if we are called without subcommand, skip
116:         if (!is_object($command->command))
117:             return;
118: 
119:         $subcommand_name = $command->command_name;
120:         $subcommand_options = $command->command->options;
121: 
122:         //self::getKeys()
123:         $keys = call_user_func(array(get_called_class(), 'getKeys'));
124:         //check for all keys if it is required for currently called subcommand
125:         foreach ($keys as $key => $data) {
126:             if (isset($data['actions'][$subcommand_name]) && $data['actions'][$subcommand_name] == 'required')
127:                 if (!isset($subcommand_options[$key]))
128:                     throw new \Exception(sprintf('option --%s has to be set', $key));
129:         }
130:     }
131: 
132:     /**
133:      * execute this command
134:      * @param \Console_CommandLine_Result $command the command as created by self::CLI_getCommand
135:      * @param \Console_CommandLine $parser
136:      * @return false if subcommand is unknown
137:      */
138:     public static function CLI_execute(\Console_CommandLine_Result $command, \Console_CommandLine $parser) {
139:         // we are called without subcommand. just display help.
140:         if (!is_object($command->command))
141:         //this command will die
142:             $parser->commands[call_user_func(array(get_called_class(), 'CLI_commandName'))]->displayUsage();
143: 
144:         $subcommand_name = $command->command_name;
145:         $subcommand_options = $command->command->options;
146:         //self::getKeys()
147:         $keys = call_user_func(array(get_called_class(), 'getKeys'));
148:         $subcommands = call_user_func(array(get_called_class(), 'getSubCommands'));
149:         //if invalid subcommand, cancel
150:         if (!in_array($subcommand_name, $subcommands))
151:             return false;
152: 
153:         //execute the command, i.e. a function self::command_<subcommand_name>
154:         call_user_func(array(get_called_class(), 'command_' . $subcommand_name), $subcommand_options, $keys);
155:     }
156: 
157:     /**
158:      * prepare a propel result for displaying with printTable
159:      * @param type $res PropelObjectCollection|Array[propel\BaseObject] 
160:      * @return type Array[Array[String]]
161:      */
162:     public static function prepareQueryResult($res) {
163:         $keys = call_user_func(array(get_called_class(), 'getKeys'));
164:         //identify all parameters with "colname" set
165:         $column_keys = array();
166:         foreach ($keys as $key => $val) {
167:             if (isset($val['colname']) && $val['colname'] != null)
168:                 $column_keys[$key] = $val['colname'];
169:         }
170: 
171:         //for all items
172:         $ret = array();
173:         foreach ($res as $row) {
174:             $ret_row = array();
175:             //for the identified parameters
176:             foreach ($column_keys as $key => $val)
177:             //execute $row->get<ParameterColname>
178:                 $ret_row[$key] = call_user_func(array($row, "get" . $val));
179:             $ret[] = $ret_row;
180:         }
181:         return $ret;
182:     }
183: 
184:     /**
185:      * print a table
186:      * @param Array[String] $headers
187:      * @param Array[Array[String]] $data 
188:      */
189:     public static function printTable($headers, $data) {
190:         $tbl = new \Console_Table();
191:         $tbl->setHeaders($headers);
192:         $tbl->addData($data);
193:         echo $tbl->getTable();
194:     }
195: 
196:     //<editor-fold defaultstate="collapsed" desc="Table manipulation commands">
197:     /**
198:      * can be overwritten. is called after filling Propel item, before calling $item->save
199:      * can be used to satisfy non-null-constraints by setting default values
200:      * @param \BaseObject $item
201:      */
202:     protected static function command_insert_set_defaults(\BaseObject $item) {
203:         
204:     }
205: 
206:     /**
207:      * parsing $keys for paramters, set values from $options in $propelitem. 
208:      ** @param Array $options user-specified command line parameters
209:      * @param Array $keys result from self::getKeys()
210:      * @param String $cmdname command name to scan keys for
211:      * @param \BaseObject $propelitem propel item to set values
212:      */
213:     protected static function setKeys($options, $keys, $cmdname, \BaseObject $propelitem) {
214:         foreach ($keys as $key => $data) {
215:             if (!isset($data['colname']) || !isset($data['actions']) || !isset($data['actions'][$cmdname]))
216:                 continue;
217:             if ($data['actions'][$cmdname] == 'required' || $data['actions'][$cmdname] == 'internal')
218:                 $propelitem->{"set" . $data['colname']}($options[$key]);
219:             else if ($data['actions'][$cmdname] == 'optional' && isset($options[$key]))
220:                 $propelitem->{"set" . $data['colname']}($options[$key]);
221:         }
222:     }
223: 
224:     /**
225:      * default insert row command
226:      * @param Array $options user-specified command line parameters
227:      * @param Array $keys result from self::getKeys()
228:      */
229:     protected static function command_insert($options, $keys) {
230:         $propel_class = call_user_func(array(get_called_class(), 'getPropelClass'));
231:         $item = new $propel_class();
232:         self::setKeys($options, $keys, 'insert', $item);
233: 
234:         call_user_func(array(get_called_class(), 'command_insert_set_defaults'), $item);
235: 
236:         $lines = $item->save();
237:         if (isset($options['short']) && $options['short'])
238:             print $item->getPrimaryKey();
239:         else {
240:             printf("%d line(s) inserted.\nNew item ID is %d.\n", $lines, $item->getPrimaryKey());
241:         }
242:     }
243: 
244:     /**
245:      * default update row command
246:      * @param Array $options user-specified command line parameters
247:      * @param Array $keys result from self::getKeys()
248:      */
249:     protected static function command_update($options, $keys) {
250:         $propel_class = call_user_func(array(get_called_class(), 'getPropelClass')) . 'Query';
251:         $q = new $propel_class;
252: 
253:         $item = $q->findOneBy($keys['id']['colname'], $options['id']);
254:         if ($item == null) {
255:             trigger_error(sprintf("No contact found for id %d.\n", $options['id']), E_USER_ERROR);
256:         }
257: 
258:         foreach ($keys as $key => $data) {
259:             if ($key != 'id' && isset($data['colname']) && isset($options[$key]))
260:                 $item->{"set" . $data['colname']}($options[$key]);
261:         }
262: 
263:         $lines = $item->save();
264:         printf("%d line(s) udpated.\n", $lines);
265:     }
266: 
267:     /**
268:      * prompts the user to confirm deletion of a row if $option['noconfirm'] is not set
269:      * @param Array $options user-specified command line parameters
270:      * @param type $message message to promt the user
271:      * @return boolean delete or not
272:      */
273:     public static function command_delete_confirm($options, $message = "This will delete a row from the database.\n") {
274:         if (isset($options['noconfirm']) && $options['noconfirm'])
275:             return true;
276: 
277:         echo $message;
278:         echo "Coninue (yes/no)\n> ";
279:         while (!in_array($line = trim(fgets(STDIN)), array('yes', 'no'))) {
280: 
281:             echo "enter one of (yes/no):\n> ";
282:         }
283:         return $line == 'yes';
284:     }
285: 
286:     /**
287:      * default delete row command
288:      * @param Array $options user-specified command line parameters
289:      * @param Array $keys result from self::getKeys()
290:      */
291:     protected static function command_delete($options, $keys) {
292: 
293:         $propel_class = call_user_func(array(get_called_class(), 'getPropelClass')) . 'Query';
294:         $q = new $propel_class;
295: 
296:         $item = $q->findOneBy($keys['id']['colname'], $options['id']);
297: 
298:         $cmdname = call_user_func(array(get_called_class(), 'CLI_commandName'));
299: 
300:         if ($item == null) {
301:             trigger_error(sprintf("No $cmdname found for id %d.\n", $options['id']), E_USER_ERROR);
302:         }
303:         if (self::command_delete_confirm($options)) {
304:             $item->delete();
305:             printf("$cmdname with id %d deleted successfully.\n", $options['id']);
306:         }
307:     }
308: 
309:     /**
310:      * default show details for row command
311:      * @param Array $options user-specified command line parameters
312:      * @param Array $keys result from self::getKeys()
313:      */
314:     protected static function command_details($options, $keys) {
315:         $propel_class = call_user_func(array(get_called_class(), 'getPropelClass')) . 'Query';
316:         $q = new $propel_class;
317: 
318:         $item = $q->findOneBy($keys['id']['colname'], $options['id']);
319:         if ($item == null) {
320:             $cmdname = call_user_func(array(get_called_class(), 'CLI_commandName'));
321:             trigger_error(sprintf("No $cmdname found for id %d.\n", $options['id']), E_USER_ERROR);
322:         }
323: 
324:         $table_keys = array_keys(array_filter($keys, function($val) {
325:                             return isset($val['colname']);
326:                         }));
327:         $results = self::prepareQueryResult(array($item));
328:         self::printTable($table_keys, $results);
329:     }
330: 
331:     /**
332:      * default list rows command
333:      * @param Array $options user-specified command line parameters
334:      * @param Array $keys result from self::getKeys()
335:      */
336:     protected static function command_list($options, $keys) {
337: 
338:         $propel_class = call_user_func(array(get_called_class(), 'getPropelClass')) . 'Query';
339:         $q = new $propel_class;
340: 
341:         $table_keys = array_keys(array_filter($keys, function($val) {
342:                             return isset($val['colname']);
343:                         }));
344:         $results = self::prepareQueryResult($q->find());
345:         self::printTable($table_keys, $results);
346:     }
347: 
348:     //</editor-fold>
349: }
350: 
351: ?>
352: 
tbro API documentation generated by ApiGen 2.8.0