'drush_sm_list', 'description' => t('List the available system items from : @l', array( '@l' => _drush_sm_service_strings())), ); $items['sm export'] = array( 'callback' => 'drush_sm_export', 'description' => t('Export a system aspect like : @l', array( '@l' => _drush_sm_service_strings())), ); $items['sm import'] = array( 'callback' => 'drush_sm_import', 'description' => t('Import a system aspect like : @l', array( '@l' => _drush_sm_service_strings())), ); $items['sm report'] = array( 'callback' => 'drush_sm_report', 'description' => t('Report a system aspect like : @l', array( '@l' => _drush_sm_service_strings())), ); /* $items['sm create db'] = array( 'callback' => 'drush_sm_create_db', 'description' => t('Create a new database.'), ); */ return $items; } function _drush_sm_services() { return array('cck','views', 'variables'); } function _drush_sm_service_strings() { return '[' . implode( "|" , _drush_sm_services()) . ']'; } function _drush_sm_command() { return array( 'list', 'export', 'report', 'import'); } function _drush_sm_command_strings() { return '[' . implode( "|" , _drush_sm_command()) . ']'; } function _drush_sm_usage() { return '[' . _drush_sm_command() . "] [" . _drush_sm_services() . ']'; } function drush_sm_list() { $args = func_get_args(); if ($args) { $command=array_shift($args); if (!in_array($command, _drush_sm_services())) { drush_die('Unknown list argument ' . $command); } else { array_unshift($args, 'list'); call_user_func_array('_drush_sm_' . $command, $args); } } else { drush_die('No args given. Try : ' . _drush_sm_service_strings()); } } function drush_sm_export() { $args = func_get_args(); if ($args) { $command=array_shift($args); array_unshift($args, 'export'); if (!in_array($command, _drush_sm_services())) { drush_die('Unknown list argument ' . $command); } else { call_user_func_array('_drush_sm_' . $command, $args); } } else { drush_die('No args given. Try : ' . implode(', ', _drush_sm_services())); } } function drush_sm_import() { $args = func_get_args(); if ($args) { $command=array_shift($args); array_unshift($args, 'import'); if (!in_array($command, _drush_sm_services())) { drush_die('Unknown list argument ' . $command); } else { call_user_func_array('_drush_sm_' . $command, $args); } } else { drush_die('No args given. Try : ' . implode(', ', _drush_sm_services())); } } function drush_sm_report() { $args = func_get_args(); if ($args) { $command=array_shift($args); if (!in_array($command, _drush_sm_services())) { drush_die('Unknown list argument ' . $command); } else { array_unshift($args, 'report'); $function= '_drush_sm' . $command; call_user_func_array($function, $args); } } else { drush_die('No args given. Try : ' . _drush_sm_service_strings()); } } function _drush_sm_variables() { $args = func_get_args(); $command=array_shift($args); if ($command=='list') { echo "// listing ... \n"; $args = func_get_args(); $command=array_shift($args); $report=_drush_sm_variables_sql(); ksort($report); foreach ($report as $name => $value) { echo "$name\n"; } } else if ($command=='import') { $dir=array_shift($args); $file= $dir . '/variables.txt'; drush_verbose( "Using $file"); if (is_file($file)) { /* import fails due to stdObject::__setState call for language settings :( $o=(object) array('a'=>'b'); $o->__setState(array('b'=>'c')); */ $result = eval( 'return ' . file_get_contents($file)); if (is_array($result)) { drush_verbose( "Setting variables"); foreach( $result as $key => $value) { $old=variable_get($key); drush_verbose( (isset($old) || is_null($value) ? 'overriding ' : 'adding ') . $key); if (!DRUSH_SIMULATE) { variable_set( $key, $value); } } } else { drush_die( "Unexpected result from contents of $file"); } } else { drush_die( "$file does not exists."); } } else if ($command=='export') { $dir=array_shift($args); $report=_drush_sm_variables_sql(); $code='$drush_sm_var_import=' . var_export($report, TRUE) . ';'; if ($dir) { if (is_dir($dir)) { $file = $dir . "/" . 'variables.txt'; file_put_contents($file, $code); } else { drush_die( "$dir does not exists."); } } else { echo '$drush_sm_var_import=' . var_export($report, TRUE); } } else if ($command=='report') { echo "// Reporting ... \n"; $report=_drush_sm_variables_sql(); foreach ($report as $name => $value) { $var_get=variable_get($name); if (!isset($var_get) || $var_get !== $value) { echo $name . " overridden by " . var_export($var_get, TRUE) ."\n"; } } } else { drush_die('Unknown variables command ' . $command); } } function _drush_sm_variables_sql() { $sql= "select * from {variable} order by name"; $result=db_query($sql); $return=array(); while ($record = db_fetch_array($result)) { $name = $record['name']; $value=unserialize($record['value']); $return[$name]=$value; } return $return; } function _drush_sm_views() { if (function_exists('views_get_all_views')) { $args = func_get_args(); $command=array_shift($args); if ($command=='list') { _drush_sm_views_list(); } else if ($command=='export') { $result=call_user_func_array('_drush_sm_views_export', $args); } else { drush_die('Unknown views command ' . $command); } } else { drush_die('views not installed'); } } function _drush_sm_views_list() { $views=views_get_all_views(); ksort($views); foreach ($views as $name => $view) { echo "$name " . ($view->disabled ? 'disabled': 'enabled') . "\n"; } } function _drush_sm_views_export() { $args=func_get_args(); $dir=array_shift($args); $views=views_get_all_views(); foreach ($views as $name => $view) { $code = views_views_exportables('export', array($name => TRUE), $name); if ($dir) { $file=$dir . 'views_' . $name . '.txt'; if (DRUSH_SIMULATE) { drush_verbose( "Should write to file $file"); } else { drush_verbose( "Writing file $file"); file_put_contents( $file, $code); } } else { echo "$name\n"; if (!DRUSH_SIMULATE) { echo $code; } } } } function _drush_sm_views_report() { echo "Not yet implemented ... what could we report?"; } function _drush_sm_cck() { $args = func_get_args(); if (function_exists('content_copy_types')) { $command=array_shift($args); if ($command=='list') { $content_types=content_copy_types(); foreach ($content_types as $content_type => $item) { echo "$content_type\n"; $fields = content_copy_fields($content_type); foreach ($fields as $field) { echo " $field\n"; } } } else if ($command=='export') { $dir=array_shift($args); require_once './includes/common.inc'; module_load_include('inc', 'node', 'content_types'); $content_types=content_copy_types(); ksort($content_types); foreach ($content_types as $type_name => $item) { $node_state = array('values' => array('type_name' => $type_name)); drupal_execute('node_type_form', $node_state, node_get_types('type', $type_name)); $code= "\n" . '$content_types["' . $type_name .'"]='; $code .= "\n" . var_export($node_state, TRUE) . ";"; if ($dir) { // patch for ad module with has names like ad/image :( $type_name = str_replace("/", "_", $type_name); $file=$dir . '/' . $type_name . '.txt'; if (DRUSH_SIMULATE) { drush_verbose( "Should write to file $file"); } else { drush_verbose( "Writing file $file"); $result=file_put_contents( $file, $code . "\n"); if ($result===FALSE) { drush_die('Cannot write to ' . $file); } } } else { echo $code; } } module_load_include('inc', 'content', 'includes/content.admin'); module_load_include('inc', 'content', 'includes/content.crud'); $content_fields=content_field_instance_read(); foreach ($content_fields as $def) { $field_name= 'cck_' . $def['type_name'] . '_' . $def['field_name']; $code='$content_fields["' . $def['type_name'] . '"]["'. $def['field_name'] . '"]=' . "\n"; $code .= var_export( $def, TRUE) . ";\n"; if ($dir) { $file=$dir . '/' . $field_name . '.txt'; if (DRUSH_SIMULATE) { drush_verbose( "Should write to file $file"); } else { drush_verbose( "Writing file $file"); $result=file_put_contents( $file, $code . "\n"); if ($result===FALSE) { drush_die('nop'); } } } else { echo $code; } } } else { drush_die('Unknown cck command ' . $command); } } else { drush_die('cck not installed'); } } /** * // http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=value&q2=test#top // @see http://stevenlevithan.com/demo/parseuri/cf/ * @param uri */ function drush_sm_create_db() { $args = func_get_args(); $uri = $args[0]; // parse_url() # $url = 'mysql://drush_mm:drush_mm@localhost/drush_mm'; $HOSTNAME = 'test'; $protocol = 'mysql'; $user = $HOSTNAME; $password = $HOSTNAME; $host = 'localhost'; $resource = $HOSTNAME; $template = "create database $resource; \n"; $template.= "grant all on $resource.* to '$user'@'$host' identified by '$password'; \n"; db_query( $template); }