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($command = NULL) { if (empty($command)) { drush_set_error(dt('Usage: drush multisite [options...], where is a drush command.')); return; } // Accept a variable number of arguments. $args = func_get_args(); array_shift($args); // Verify that the command is defined. $commands = drush_get_commands(); if (!isset($command[$commands])) { drush_set_error('DRUSH_COMMAND_NOT_FOUND', dt('Invalid command @command.', array('@command' => $command))); return; } // Enumerate sites and construct --uri arguments to target them. 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(basename($dir_path)); } } if (empty($sites)) { drush_set_error(dt('No sites found to run command on.')); return; } if (!drush_get_context('DRUSH_SIMULATE') && !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; } // Pass options along with the command. $data = drush_get_context('options'); unset($data['sites']); // Execute the command with drush_backend_invoke(). foreach ($sites as $uri) { $data['uri'] = $uri; $cmd = ($args) ? $command . ' ' . implode(' ', $args) : $command; drush_print(dt("Running '@command' on @uri...", array('@command' => $cmd, '@uri' => $uri))); if (!drush_get_context('DRUSH_SIMULATE')) { drush_backend_invoke_args($command, $args, $data); } } }