Index: demo.drush.inc =================================================================== RCS file: demo.drush.inc diff -N demo.drush.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ demo.drush.inc 28 Mar 2010 20:53:08 -0000 @@ -0,0 +1,230 @@ + 'demo_drush_status', + 'description' => 'Prints default dump settings and last reset time.', + ); + + /** + * command to create new snapshot. + */ + $items['demo-create'] = array( + 'callback' => 'demo_drush_create_snapshot', + 'description' => dt('Creates new snapshot with the given file name and description.'), + 'arguments' => array( + 'filename' => 'Required. SQL dump file name.', + 'description' => 'Optional. Enter a description for this snapshot.', + ), + 'examples' => array('To create a new snapshot' => 'drush demo-create snapshot-v1.0 "First version"'), + ); + // TODO: For demo-create, use default file name when no dump file name is given (@see http://drupal.org/node/754798) + + /** + * command to delete snapshot. + */ + $items['demo-delete'] = array( + 'callback' => 'demo_drush_delete_snapshot', + 'description' => dt('Deletes a snapshot with the given file name.'), + 'arguments' => array('filename' => 'Required. SQL dump file name.'), + 'examples' => array('To delete a snapshot' => 'drush demo-delete snapshot-v1.0'), + ); + + /** + * command to get a list of available snapshots. + */ + $items['demo-snapshots'] = array( + 'callback' => 'demo_drush_snapshots', + 'description' => 'Gets a list of available snapshots.', + ); + // TODO: demo-snapshot looks very lengthy, need alias or meaningful short name. + + /** + * command to reset your site. + */ + $items['demo-reset'] = array( + 'callback' => 'demo_drush_reset', + 'description' => dt('To reset your site with give snapshot. DO NOT TRY THIS COMMAND ON PRODUCTION SERVER'), + 'arguments' => array('filename' => 'Required. SQL dump file name.'), + 'examples' => array('To reset use' => 'drush demo-reset snapshot-v1.0'), + ); + + // For later implementation + // module_exists is unavailable here, may be we need to use demo_reset.drush.inc + //if (module_exists('demo_reset')) { + /** + * command to set default snapshot. + * This will be used to reset site automatically after some regular interval during cron run. + */ + + /** + $items['demo-set-default'] = array( + 'callback' => 'demo_drush_set_default', + 'description' => dt('Set default snapshot to reset your site after regular interval.'), + 'arguments' => array('filename' => 'Required. SQL dump file name.'), + 'examples' => array('To reset your site with given' => 'drush demo-delete snapshot-v1.0'), + ); + } + */ + return $items; +} + +/** + * Callback for drush command demo-status. + */ +function demo_drush_status() { + // last reset date + $reset_date = (variable_get('demo_reset_last', 0)) ? format_date(variable_get('demo_reset_last', 0)) : dt('Never'); + drush_print('Last reset: ' . $reset_date); + + // default snapshot name + if (module_exists('demo_reset')) { + drush_print('Default snapshot to use for reset on cron run: ' . variable_get('demo_dump_cron', dt('None'))); + } + + // dump path + drush_print('Current dump path: ' . variable_get('demo_dump_path', dt('n/a'))); + + //TODO add default dump file name and appending date format http://drupal.org/node/754798 +} + +/** + * Callback for drush command demo-create. + */ +function demo_drush_create_snapshot($filename = NULL, $description = NULL) { + // filename validation code + if (empty($filename)) { + drush_set_error('Argument file name is not given. See "drush help demo-create" for usage.'); + return; + } + + if (!preg_match('/^[-_\.a-zA-Z0-9]+$/', $filename)) { + drush_set_error(t('Dump filename %title must contain alphanumeric characters, dots, dashes and underscores only. Other characters, including blanks (spaces), are not allowed.', array('%title' => $filename))); + return; + } + + // invoke _demo_dump() to create snapshot + module_load_include('inc', 'demo', 'demo.admin'); + $options = array( + 'filename' => $filename, + 'description' => $description, + 'tables' => demo_enum_tables(), + ); + if ($fileconfig = _demo_dump($options)) { + drush_log('Successfully created snapshot ' . $fileconfig['sqlfile'], 'ok'); + } + +} + +/** + * Callback for drush command demo-delete. + */ +function demo_drush_delete_snapshot($filename = NULL) { + if (empty($filename)) { + drush_set_error('Argument file name is not given. See "drush help demo-delete" for usage.'); + return; + } + + module_load_include('inc', 'demo', 'demo.admin'); + // TODO: module_load_include() is redundant, need to use equivalent of hook_init(). + + $files = demo_get_fileconfig($filename); + if (!file_exists($files['infofile'])) { + drush_set_error('File does not exists. Use "drush demo-snapshots" to get the list of available of snapshots.'); + return; + } + unlink($files['sqlfile']); + unlink($files['infofile']); + drush_log("Deleted snapshot $filename", 'success'); + +} + +/** + * Callback for drush command demo-snapshot. + */ +function demo_drush_snapshots() { + $rows = array(); + + module_load_include('inc', 'demo', 'demo.admin'); + // TODO: module_load_include() is redundant, need to use equivalent of hook_init(). + + $fileconfig = demo_get_fileconfig(); + $files = file_scan_directory($fileconfig['dumppath'], '.info$'); + + $rows[] = array('File name', 'Date', 'Size', 'Description'); + foreach ($files as $filename => $file) { + // Build basic file info + $files[$filename] = (array) $file; + $info = demo_get_info($filename); + $rows[] = array( + 'name' => check_plain($info['filename']), + 'date' => format_date(filemtime($filename), 'small'), + 'size' => format_size(filesize(substr($filename, 0, -4) . 'sql')), + 'desc' => !empty($info['description']) ? $info['description'] : dt('n/a'), + ); + + } + drush_print_table($rows, TRUE); +} + +/** + * Callback for drush command demo-reset. + */ +function demo_drush_reset($filename = NULL) { + if (empty($filename)) { + drush_set_error('Argument file name is not given. See "drush help demo-reset" for usage.'); + return; + } + module_load_include('inc', 'demo', 'demo.admin'); + // TODO: module_load_include() is redundant, need to use equivalent of hook_init(). + + $files = demo_get_fileconfig($filename); + if (!file_exists($files['infofile'])) { + drush_set_error('File does not exists. Use "drush demo-snapshots" to get the list of available of snapshots.'); + return; + } + + drush_print('DO NOT TRY THIS COMMAND ON PRODUCTION SERVER. THIS ACTION WILL RESET YOUR DATABASE AND CAN NOT BE UNDONE. WE RECOMMEND YOU TO HAVE A BACKUP OF YOU DATABASE BEFORE YOU PROCEED WIHT THIS COMMAND.', 'warning'); + + if (!drush_confirm('Are you sure you want to reset the site ?', $indent = 0)) { + return; + } + + if(_demo_reset($filename)) { + drush_log('Successfully restored database from snapshot '. $filename, 'ok'); + } + else { + drush_set_error('Failed to restore from snapshot.'); + } +}