Index: commands/core/sitealias.drush.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush/commands/core/sitealias.drush.inc,v
retrieving revision 1.4
diff -u -p -r1.4 sitealias.drush.inc
--- commands/core/sitealias.drush.inc	5 Nov 2009 01:46:23 -0000	1.4
+++ commands/core/sitealias.drush.inc	7 Nov 2009 14:44:36 -0000
@@ -48,12 +48,7 @@ function sitealias_drush_command() {
  * is the site specification for the given alias.
  */
 function _drush_sitealias_alias_list() {
-  $site_list = array();
-  $all_site_aliases = drush_get_option('site-aliases', array());
-  foreach ($all_site_aliases as $alias => $alias_record) {
-    $site_list[$alias] = drush_sitealias_get_site_specification($alias);
-  }
-  return $site_list;
+  return drush_get_option('site-aliases', array());
 }
 
 /**
@@ -68,10 +63,9 @@ function _drush_sitealias_site_list() {
   $files = drush_scan_directory($base_path, '/settings\.php/', array('.', '..', 'CVS', 'all'));
   foreach ($files as $filename => $info) {
     if ($info->basename == 'settings.php') {
-      $path = explode('/', $filename);
-      array_pop($path);
-      $site = array_pop($path);
-      $site_list[$site] = $root . '#' . $site;
+      $alias_record = drush_sitealias_build_record_from_settings_file($filename);
+      $uri = drush_sitealias_site_dir_from_filename($filename);
+      $site_list[$uri] = $alias_record;
     }
   }
   return $site_list;
@@ -89,37 +83,30 @@ function _drush_sitealias_all_list() {
  * user specified on the command line.  If none were specified,
  * then all are returned.
  */
-function _drush_sitealias_user_specified_list() {
+function _drush_sitealias_user_specified_list($db_settings_needed = false) {
   $command = drush_get_command();
   $specifications = $command['arguments'];
+  $site_list = array();
   
-  // First we'll get a list of all aliases and local sites
-  $site_list = _drush_sitealias_all_list();
-  
-  // If the user provided no args, then we will return everything. 
+  // Iterate over the arguments and convert them to alias records 
   if (!empty($specifications)) {
-    // Next we'll remove any item from the list that
-    // is not in the list the user specified
-    foreach ($site_list as $site => $sitespec) {
-      if (!in_array($site, $specifications) && !in_array($sitespec, $specifications)) {
-        unset($site_list[$site]);
-      }
-    }
-    
-    // Finally, we'll look for special values that
-    // the user may have specified on the command line
-    // and put those in as well.
     foreach ($specifications as $site) {
-      if ($site == "local") {
-        $site_list = array_merge($site_list, _drush_sitealias_site_list());
-      }
-      elseif ($site == "default") {
-        $root = drush_get_context('DRUSH_DRUPAL_ROOT');
-        $uri = drush_get_option(array('l', 'uri'));
-        $site_list[$uri] = $root . '#' . $uri;
+      $alias_record = drush_sitealias_get_record($site, $db_settings_needed);
+      if (!empty($alias_record)) {
+        // If the alias record has a better name for itself than the
+	// one that was used to search for it, then it will put it
+	// into the 'name' element.
+        if (array_key_exists('name', $alias_record)) {
+          $site = $alias_record['name'];
+	}
+        $site_list[drush_sitealias_uri_to_site_dir($site)] = $alias_record;
       }
     }
   }
+  // If the user provided no args, then we will return everything. 
+  else {
+    $site_list = _drush_sitealias_all_list();
+  }
   
   return $site_list;
 }
@@ -131,12 +118,16 @@ function _drush_sitealias_user_specified
 function drush_sitealias_print() {
   $full_output = drush_get_option('full');
   $long_output = drush_get_option('long');
-  foreach (_drush_sitealias_user_specified_list() as $site => $sitespec) {
+  $with_db = (drush_get_option('with-db') != null) || (drush_get_option('with-db-url') != null);
+  $site_list = _drush_sitealias_user_specified_list($with_db);
+    
+  foreach ($site_list as $site => $alias_record) {
     if (isset($full_output)) {
-      _drush_sitealias_print_record($site);
+      _drush_sitealias_print_record($alias_record, $site);
     }
     elseif (isset($long_output)) {
-      drush_print($sitespec);
+      $site_spec = drush_sitealias_alias_record_to_spec($alias_record, $with_db);
+      drush_print($site_spec);
     }
     else {
       drush_print($site);
@@ -145,50 +136,52 @@ function drush_sitealias_print() {
 }
 
 /**
- * Compose a site specification for a named site alias.
+ * Convert from an alias record to a site specification
+ *
+ * @param alias_record
+ *   The full alias record to convert
  *
- * @param site_alias
- *   The name of the site alias to create a site specification for
+ * @param with_db
+ *   True if the site specification should include a ?db-url term
+ *
+ * @return string
+ *   The site specification
  */
-function drush_sitealias_get_site_specification($site_alias) {
-  $all_site_aliases = drush_get_option('site-aliases', array());
-  $result = '#' . $site_alias;
-  if (array_key_exists($site_alias, $all_site_aliases)) {
-    $site_alias_settings = $all_site_aliases[$site_alias];
-    if (array_key_exists('uri', $site_alias_settings)) {
-      $result = '#' . $site_alias_settings['uri'];
+function drush_sitealias_alias_record_to_spec($alias_record, $with_db = false) {
+    $result = '';
+    if (array_key_exists('uri', $alias_record)) {
+      $result = '#' . str_replace('http://', '', $alias_record['uri']);
     }
-    if (array_key_exists('path-aliases', $site_alias_settings)) {
-      $path_aliases = $site_alias_settings['path-aliases'];
+    if (array_key_exists('path-aliases', $alias_record)) {
+      $path_aliases = $alias_record['path-aliases'];
       if (array_key_exists('!root', $path_aliases)) {
         $result = $path_aliases['!root'] . $result;
       }
     }
-    if (array_key_exists('remote-host', $site_alias_settings)) {
-      $result = $site_alias_settings['remote-host'] . $result;
+    if (array_key_exists('remote-host', $alias_record)) {
+      $result = $alias_record['remote-host'] . $result;
     }
-  }
-  return $result;
+    
+    // add the database info to the specification if desired
+    if ($with_db) {
+      $result = $result . '?db-url=' . urlencode($alias_record['db-url']);
+    }
+    
+    return $result;
 }
 
 /**
  * Given a site alias name, print out a php-syntax
  * representation of it.
  *
- * @param site_alias
+ * @param alias_record
  *   The name of the site alias to print
  */
-function _drush_sitealias_print_record($site_alias) {
+function _drush_sitealias_print_record($alias_record, $site_alias = '') {
   $output_db = drush_get_option('with-db');
   $output_db_url = drush_get_option('with-db-url');
   $output_optional_items = drush_get_option('with-optional');
-  
-  // Get the alias record for the specified site alias.
-  // If the user wants database information included in
-  // the record, then try to fetch that info from the
-  // remote site.
-  $alias_record = drush_sitealias_get_record($site_alias, isset($output_db) | isset($output_db_url));
-  
+    
   // Include the optional items, if requested
   if ($output_optional_items) {
     _drush_sitealias_add_transient_defaults($alias_record);
@@ -219,8 +212,51 @@ function _drush_sitealias_print_record($
   $alias_name = drush_get_option('alias-name');
   if (!isset($alias_name)) {
     $alias_name = $site_alias;
+    if (empty($alias_name)) {
+      $alias_name = drush_sitealias_uri_to_site_dir($alias_record['uri']);
+    }
   }
+  
+  // We don't want the name to go into the output
+  unset($alias_record['name']);
+
   $exported_alias = var_export($alias_record, TRUE);
-  drush_print('$options[\'site-aliases\'][\'' . $alias_name . '\'] = ' . $exported_alias . ',');
+  drush_print('$options[\'site-aliases\'][\'' . $alias_name . '\'] = ' . $exported_alias . ';');
 }
 
+/**
+ * Use heuristics to attempt to convert from a site directory to a URI.
+ * This function should only be used when the URI really is unknown, as
+ * the mapping is not perfect.
+ *
+ * @param site_dir
+ *   A directory, such as domain.com.8080.drupal
+ *
+ * @return string
+ *   A uri, such as http://domain.com:8080/drupal
+ */
+function _drush_sitealias_site_dir_to_uri($site_dir) {
+
+  // Convert .[0-9]+. into :[0-9]+/
+  $uri = preg_replace("/\.([0-9]+)\./", ":$1/", $site_dir);
+  // Convert .[0-9]$ into :[0-9]
+  $uri = preg_replace("/\.([0-9]+)$/", ":$1", $uri);
+  // Convert .(com|net|org|info). into .(com|net|org|info)/
+  $uri = str_replace(array('.com.', '.net.', '.org.', '.info.'), array('.com/', '.net/', '.org/', '.info/'), $uri);
+  
+  // If there is a / then convert every . after the / to /
+  // Then again, if we did this we would break if the path contained a "."
+  // I hope that the path would never contain a "."...
+  $pos = strpos($uri, '/');
+  if ($pos !== false) {
+    $uri = substr($uri, 0, $pos + 1) . str_replace('.', '/', substr($uri, $pos + 1));
+  }
+  
+  // n.b. this heuristic works all the time if there is a port,
+  // it also works all the time if there is a port and no path,
+  // but it does not work for domains such as .co.jp with no path,
+  // and it can fail horribly if someone makes a domain like "info.org".
+  // Still, I think this is the best we can do short of consulting DNS.
+  
+  return 'http://' . $uri;
+}
Index: includes/sitealias.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush/includes/sitealias.inc,v
retrieving revision 1.4
diff -u -p -r1.4 sitealias.inc
--- includes/sitealias.inc	4 Nov 2009 22:29:52 -0000	1.4
+++ includes/sitealias.inc	7 Nov 2009 14:44:37 -0000
@@ -91,7 +91,7 @@ function drush_sitealias_get_record($ali
           }
           // Cache our result so we don't need to do IPC for successive calls to this function
           drush_set_option('databases-' . $alias, $alias_record['databases'], 'sitealias-cache');
-  }
+        }
       }
     }
     // If the parameter is not an alias, then it is some form of
@@ -204,6 +204,14 @@ function _drush_sitealias_add_transient_
  */
 function _drush_sitealias_find_record_for_local_site($alias, $db_settings_needed = false) {
   $alias_record = array();
+  $use_name_from_alias = false;
+  
+  // The alias "current" is a synonym for whatever site
+  // has been specified via -l or -uri
+  if ($alias == "current") {
+    $alias = drush_get_option(array('l', 'uri'));
+    $use_name_from_alias = true;
+  }
   
   // Clip off the leading '#' if it is there
   if (substr($alias,0,1) == '#') {
@@ -238,6 +246,12 @@ function _drush_sitealias_find_record_fo
     $alias_record = array_merge(_drush_sitealias_build_record_from_settings($alias, $drupal_root), $alias_record);
   }
   
+  // If the alias was looked up via the name "current", then
+  // plug in the actual alias name that was used to find it.
+  if (!empty($alias_record) && $use_name_from_alias) {
+    $alias_record['name'] = $alias;
+  }
+  
   return $alias_record;
 }
 
@@ -263,30 +277,82 @@ function _drush_sitealias_build_record_f
     $drupal_root = drush_get_option(array('r', 'root'), drush_locate_root());
   }
   
-  // If we did not find an alias record that matches,
-  // then build one.
-  if (empty($alias_record)) {
-    $site_settings_file = $drupal_root . '/sites/' . $alias . '/settings.php';
-    if (isset($drupal_root) && file_exists($site_settings_file)) {
+  if (isset($drupal_root)) {
+    $alias_dir = drush_sitealias_uri_to_site_dir($alias);
+    $site_settings_file = $drupal_root . '/sites/' . $alias_dir . '/settings.php';
+    $alias_record = drush_sitealias_build_record_from_settings_file($site_settings_file, $alias, $drupal_root);
+  }
+  
+  return $alias_record;
+}
+
+function drush_sitealias_build_record_from_settings_file($site_settings_file, $alias = null, $drupal_root = null) {
+  $alias_record = array();
+
+  if (!isset($drupal_root)) {
+    //$drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
+    $drupal_root = drush_get_option(array('r', 'root'), drush_locate_root());
+  }
+
+  if (file_exists($site_settings_file)) {
+    global $databases, $db_url, $base_url;
+    $base_url = null;
+    require $site_settings_file;
+    if (isset($db_url)) {
+      $alias_record['db-url'] = $db_url;
+    }
+    else {
+      $alias_record['databases'] = $databases;
+    }
+    $alias_record['path-aliases'] = array(
+      '!root' => $drupal_root,
+      /*'!files' => '/path/to/file/storage', */
+      /*'!dump' => '/path/to/sql/dumpfile', */ );
+    
+    if (isset($alias)) {
       $alias_record['uri'] = $alias;
-      global $databases, $db_url;
-      require $site_settings_file;
-      if (isset($db_url)) {
-	$alias_record['db-url'] = $db_url;
-      }
-      else {
-	$alias_record['databases'] = $databases;
-      }
-      $alias_record['path-aliases'] = array(
-	'!root' => $drupal_root,
-	/*'!files' => '/path/to/file/storage', */
-	/*'!dump' => '/path/to/sql/dumpfile', */ );
+    }
+    elseif ($base_url != null) {
+      $alias_record['uri'] = $base_url;
+    }
+    else {
+      $alias_record['uri'] = _drush_sitealias_site_dir_to_uri(drush_sitealias_site_dir_from_filename($site_settings_file));
     }
   }
+
   return $alias_record;
 }
 
 /**
+ * Pull the site directory from the path to settings.php
+ *
+ * @param site_settings_file
+ *   path to settings.php
+ *
+ * @return string
+ *   the site directory component of the path to settings.php
+ */
+function drush_sitealias_site_dir_from_filename($site_settings_file) {
+  $path = explode('/', $site_settings_file);
+  array_pop($path); // pop off 'settings.php'
+  $site_dir = array_pop($path); // get the directory name that settings.php is in
+  
+  return $site_dir;
+}
+
+/**
+ * Convert from a URI to a site directory.
+ *
+ * @param uri
+ *   A uri, such as http://domain.com:8080/drupal
+ * @return string
+ *   A directory, such as domain.com.8080.drupal
+ */
+function drush_sitealias_uri_to_site_dir($uri) {
+  return str_replace(array('http://', '/', ':'), array('', '.', '.'), $uri);
+}
+
+/**
  * Convert from an old-style database URL to an array of database settings
  *
  * @param db_url
