array('multi', 'ms'), 'description' => 'Run a command on multiple Drupal sites.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, 'examples' => array( 'drush [command] ' => 'Run on all sites using .', 'drush [command] --sites=http://default,http://subsite.mysite.com ' => 'Run on sites/default and sites/subsite.mysite.com.', ), ); return $items; } /** * Implementation of hook_drush_help(). */ function multisite_drush_help($section) { switch ($section) { case 'drush:multisite': return dt('Perform a command on multiple sites.'); } } /** * Drush callback to run a command on multiple Drupal sites. */ function drush_multisite() { $args = func_get_args(); // Escape arguments that contain spaces. foreach ($args as $i => $arg) { if (strstr($arg, ' ')) { $args[$i] = escapeshellarg($arg); } } $command = implode(' ', $args); if (empty($command)) { drush_set_error(dt('Usage: drush multisite [options...], where is a drush command.')); return; } if ($sites_option = drush_get_option('sites')) { $sites = explode(',', $sites_option); } else { // Track symlink targets, so sites aren't hit twice. $targets = array(); $sites = array(); $result = array_keys(drush_scan_directory(drush_get_context('DRUSH_DRUPAL_ROOT') . '/sites', '/^settings\.php$/')); foreach($result as $path) { $dir_path = dirname($path); $target = realpath($dir_path); if (isset($targets[$target])) { continue; } $targets[$target] = TRUE; $sites[] = 'http://' . strtolower(end(explode('/', $dir_path))); } } $options = drush_get_context('options'); if (empty($sites)) { drush_set_error(dt('No sites found to run command on.')); return; } $commands = drush_get_commands(); $command_found = FALSE; for ($i = count($args); $i != 0; $i--) { if (isset($commands[implode(' ', array_slice($args, 0, $i))])) { $command_found = TRUE; break; } } if (!$command_found) { drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt('Invalid command @command.', array('@command' => $command))); return; } if (!drush_confirm(dt("Are you sure you want to run '@command' on @num site(s)?", array('@command' => $command, '@num' => count($sites))))) { drush_set_error(dt('Aborting.')); return; } foreach ($sites as $uri) { $options['uri'] = $uri; drush_print(dt("Running '@command' on: @uri...", array('@command' => $command, '@uri' => $uri))); drush_backend_invoke($command, $options); } }