View source
<?php
function demo_drush_help($command) {
if ($command == 'drush:demo-status') {
return dt('Print default dump settings and last reset time.');
}
elseif ($command == 'drush:demo-create') {
return dt('Create a new snapshow with the given file name and description. Dump filename can have alphanumeric characters, dots, dashes, and underscores only. Other characters, including blanks (spaces), are not allowed.');
}
elseif ($command == 'drush:demo-delete') {
return dt('Delete a specified snapshot.');
}
elseif ($command == 'drush:demo-reset') {
return dt('Reset your site using a specified snapshot. DO NOT USE THIS COMMAND ON A PRODUCTION SERVER.');
}
}
function demo_drush_command() {
$items['demo-status'] = array(
'callback' => 'demo_drush_status',
'description' => 'Print default dump settings and last reset time.',
'aliases' => array(
'demo-st',
),
);
$items['demo-create'] = array(
'callback' => 'demo_drush_create_snapshot',
'description' => dt('Create a 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"',
),
'aliases' => array(
'demo-mk',
),
);
$items['demo-delete'] = array(
'callback' => 'demo_drush_delete_snapshot',
'description' => dt('Delete 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',
),
'aliases' => array(
'demo-rm',
),
);
$items['demo-snapshots'] = array(
'callback' => 'demo_drush_snapshots',
'description' => 'Get a list of available snapshots.',
'arguments' => array(
'filename' => 'Optional. Name of the snapshot.',
),
'examples' => array(
'To get the list' => 'drush demo-snapshots',
'To get description about individual snapshot' => 'drush demo-snapshot <snapshot-name>',
),
'aliases' => array(
'demo-ls',
),
);
$items['demo-reset'] = array(
'callback' => 'demo_drush_reset',
'description' => 'Reset your site using a specified snapshot. DO NOT USE THIS COMMAND ON A PRODUCTION SERVER',
'arguments' => array(
'filename' => 'Required. SQL dump file name.',
),
'examples' => array(
'To reset use' => 'drush demo-reset snapshot-v1.0',
),
'aliases' => array(
'demo-rs',
),
);
return $items;
}
function demo_drush_status() {
$reset_date = variable_get('demo_reset_last', 0) ? format_date(variable_get('demo_reset_last', 0)) : dt('Never');
drush_print('Last reset: ' . $reset_date);
if (module_exists('demo_reset')) {
drush_print('Default snapshot to use for reset on cron run: ' . variable_get('demo_dump_cron', dt('None')));
}
drush_print('Current dump path: ' . variable_get('demo_dump_path', dt('n/a')));
}
function demo_drush_create_snapshot($filename = NULL, $description = NULL) {
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;
}
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');
}
}
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;
}
if (!_demo_drush_is_file_exists($filename)) {
drush_set_error('File does not exists. Use "drush demo-snapshots" to get the list of available snapshots.');
return;
}
drush_log('This command will delete the snapshot and can not be undone', 'warning');
if (!drush_confirm('Are you sure you want to continue?', $indent = 0)) {
drush_log('Command Aborted.', 'ok');
return;
}
module_load_include('inc', 'demo', 'demo.admin');
$file = demo_get_fileconfig($filename);
unlink($file['sqlfile']);
unlink($file['infofile']);
drush_log("Deleted snapshot {$filename}", 'success');
}
function demo_drush_snapshots($filename = NULL) {
module_load_include('inc', 'demo', 'demo.admin');
if (!isset($filename)) {
$rows = array();
$fileconfig = demo_get_fileconfig();
$files = file_scan_directory($fileconfig['dumppath'], '.info$');
$rows[time()] = array(
'File name',
'Date',
'Size',
'Description',
);
foreach ($files as $filename => $file) {
$files[$filename] = (array) $file;
$info = demo_get_info($filename);
$m_time = filemtime($filename);
$rows[$m_time] = array(
'name' => check_plain($info['filename']),
'date' => format_date($m_time, 'small'),
'size' => format_size(filesize(substr($filename, 0, -4) . 'sql')),
'desc' => !empty($info['description']) ? $info['description'] : dt('n/a'),
);
}
krsort($rows);
drush_print_table($rows, TRUE);
}
else {
if (!_demo_drush_is_file_exists($filename)) {
drush_set_error('File does not exist. Use "drush demo-snapshots" to get the list of available snapshots.');
return;
}
$file = demo_get_fileconfig($filename);
$info = demo_get_info($file['infofile']);
if (!empty($info['description'])) {
drush_print('Description: ' . $info['description']);
}
$modules = array_diff($info['modules'], array(
'block',
'filter',
'node',
'system',
'user',
'demo',
));
sort($info['modules']);
drush_print('Modules: ' . implode(', ', $info['modules']));
}
}
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;
}
if (!_demo_drush_is_file_exists($filename)) {
drush_set_error('File does not exist. Use "drush demo-snapshots" to get the list of available snapshots.');
return;
}
drush_log('DO NOT USE THIS COMMAND ON A PRODUCTION SERVER. THIS ACTION WILL RESET YOUR DATABASE AND CAN NOT BE UNDONE. WE RECOMMAND YOU HAVE A BACKUP OF YOUR DATABASE BEFORE YOU PROCEED WIHT THIS COMMAND.', 'warning');
if (!drush_confirm('Are you sure you want to reset the site?')) {
drush_log('Command Aborted.', 'ok');
return;
}
drush_print('This command will take a while. Please wait...');
if (_demo_reset($filename)) {
drush_log('Successfully restored database from snapshot ' . $filename, 'ok');
}
else {
drush_set_error('Failed to restore from snapshot.');
}
}
function _demo_drush_is_file_exists($filename) {
module_load_include('inc', 'demo', 'demo.admin');
$files = demo_get_fileconfig($filename);
return file_exists($files['infofile']);
}