? domainpath.patch
Index: API.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/API.php,v
retrieving revision 1.40
diff -u -p -r1.40 API.php
--- API.php	10 Oct 2008 20:19:27 -0000	1.40
+++ API.php	18 Oct 2008 19:31:20 -0000
@@ -579,3 +579,26 @@ function hook_domain_bootstrap_full($dom
   // Our test module sets the default language to Spanish.
   $conf['language'] = 'es';
 }
+
+/**
+ * Allows modules to alter path when rewriting URLs.
+ *
+ * This hook will fire for all paths and may be resource-intensive.
+ * Look at Domain Prefix for best practices implementation. In Domain
+ * Prefix, we only include this function if we know it is necessary.
+ *
+ * @see domain_prefix_init()
+ *
+ * @param $domain_id
+ *  The domain_id taken from {domain}.
+ * @param $path
+ *  The internal drupal path to the node.
+ * @param $path_language
+ *  Language code to look up the path in.
+ *
+ * @ingroup domain_hooks
+ */
+function hook_domainpath($domain_id, &$path, $path_language = '') {
+  // Give a normal path alias
+  $path = drupal_get_path_alias($path);
+}
Index: domain.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain.module,v
retrieving revision 1.79
diff -u -p -r1.79 domain.module
--- domain.module	18 Oct 2008 16:49:18 -0000	1.79
+++ domain.module	18 Oct 2008 19:31:22 -0000
@@ -638,7 +638,6 @@ function domain_get_user_domains($accoun
         }
       }
     }
-   # print_r($domains[$uid]);
   }
   return $domains[$uid];
 }
@@ -712,7 +711,21 @@ function domain_get_path($domain) {
  * The absolute url to the current page on the requested domain.
  */
 function domain_get_uri($domain) {
-  $path = $domain['scheme'] .'://'. $domain['subdomain'] . request_uri();
+  $request_uri = request_uri();
+
+  $modules = _domain_path_modules();
+  if (!empty($modules)) {
+    // If needed, let modules modify the path alias.
+    $string = substr($request_uri, strlen(base_path()));
+    $path = drupal_get_normal_path($string);
+    $alias = domain_path($domain['domain_id'], $path);
+    if ($string == $alias) {
+      $alias = drupal_get_path_alias($path);
+    }
+    $request_uri = base_path() . $alias;
+  }
+
+  $path = $domain['scheme'] .'://'. $domain['subdomain'] . $request_uri;
   return $path;
 }
 
@@ -1607,3 +1620,38 @@ function domain_simpletest() {
   $tests = file_scan_directory($dir, '\.test$');
   return array_keys($tests);
 }
+
+/**
+ * Helper function for passing hook_domainpath() by reference.
+ *
+ * @param $domain_id
+ * The domain_id taken from {domain}.
+ * @param $path
+ * The internal drupal path to the node.
+ * @param $path_language
+ * Optional language code to look up the path in.
+ * @return
+ * The $path, modified by reference by hook_domainpath() implementations.
+ */
+function domain_path($domain_id, $path, $path_language = '') {
+  $modules = _domain_path_modules();
+  if (!empty($modules)) {
+    foreach ($modules as $module) {
+      // Cannot use module_invoke_all() since these are passed by reference.
+      $function = $module .'_domainpath';
+      $function($domain_id, $path, $path_language);
+    }
+  }
+  return $path;
+}
+
+/**
+ * Helper function for domain_path() checks.
+ */
+function _domain_path_modules() {
+  static $modules;
+  if (!isset($modules)) {
+    $modules = module_implements('domainpath');
+  }
+  return $modules;
+}
Index: settings_custom_url.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/settings_custom_url.inc,v
retrieving revision 1.11
diff -u -p -r1.11 settings_custom_url.inc
--- settings_custom_url.inc	6 Oct 2008 15:24:46 -0000	1.11
+++ settings_custom_url.inc	18 Oct 2008 19:31:22 -0000
@@ -23,7 +23,7 @@ function custom_url_rewrite_outbound(&$p
   }
 
   // Set static variables for the node lookups, to remove redundant queries.
-  static $domain_site, $domain, $nodepaths;
+  static $domain_site, $domain, $nodepaths, $path_rewrite;
 
   // This routine only needs to be run from certain urls or if we want to
   // force all links to go to a single domain for SEO.
@@ -99,4 +99,12 @@ function custom_url_rewrite_outbound(&$p
       }
     }
   }
+  // We may have to implement hook_domainpath().
+  if (!isset($path_rewrite)) {
+    $path_rewrite = count(_domain_path_modules());
+  }
+  // Allow path changes, if needed.
+  if ($path_rewrite > 0) {
+    $path = domain_path($domain[$nid]['domain_id'], $original_path, isset($options['language']) ? $options['language']->language : '');
+  }
 }
Index: domain_prefix/domain_prefix.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_prefix/domain_prefix.admin.inc,v
retrieving revision 1.2
diff -u -p -r1.2 domain_prefix.admin.inc
--- domain_prefix/domain_prefix.admin.inc	6 Jul 2008 21:16:40 -0000	1.2
+++ domain_prefix/domain_prefix.admin.inc	18 Oct 2008 19:31:23 -0000
@@ -9,24 +9,6 @@
  */
 
 /**
- * Check for existing table prefixing.
- */
-function domain_prefix_get_prefix() {
-  global $db_prefix;
-  // Check for existing table prefixing.
-  $prefix = NULL;
-  if (!empty($db_prefix)) {
-    if (is_array($db_prefix)) {
-      $prefix = $db_prefix['default'];
-    }
-    else {
-      $prefix = $db_prefix;
-    }
-  }
-  return $prefix;
-}
-
-/**
  * Get the tables with the active prefix
  *
  * @param $prefix
@@ -360,18 +342,6 @@ function domain_prefix_form($form_state,
 }
 
 /**
- * Does a table exist -- we use this to bypass Drupal's default table prefixing check.
- *
- * @param $prefix
- *   The table prefix used with this domain. Optional.
- * @param $table
- *   The name of the table being acted upon.
- */
-function domain_prefix_table_exists($prefix, $table) {
-  return db_table_exists(db_escape_table($prefix . $table));
-}
-
-/**
  * FormsAPI for domain_prefix_form.
  */
 function domain_prefix_form_submit($form, &$form_state) {
@@ -556,14 +526,3 @@ function domain_prefix_drop_records($dom
   }
   db_query("DELETE FROM {domain_prefix} WHERE domain_id = %d", $domain_id);
 }
-
-/**
- * Uniform prefix string
- *
- * @param $domain_id
- *  The domain_id taken from {domain}.
- */
-function domain_prefix_string($domain_id) {
-  return 'domain_'. $domain_id .'_';
-}
-
Index: domain_prefix/domain_prefix.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/domain/domain_prefix/domain_prefix.module,v
retrieving revision 1.26
diff -u -p -r1.26 domain_prefix.module
--- domain_prefix/domain_prefix.module	10 Oct 2008 20:19:28 -0000	1.26
+++ domain_prefix/domain_prefix.module	18 Oct 2008 19:31:23 -0000
@@ -49,6 +49,18 @@ function domain_prefix_domain_bootstrap_
 }
 
 /**
+ * Implements hook_init().
+ *
+ * Check for prefixes of the url_alias table, and if present add hook_domainprefix().
+ */
+function domain_prefix_init() {
+  $count = db_result(db_query("SELECT COUNT(*) FROM {domain_prefix} WHERE status > 1 AND tablename = 'url_alias'"));
+  if (!empty($count)) {
+    include 'domain_prefix.path.inc';
+  }
+}
+
+/**
  * Implements hook_menu()
  */
 function domain_prefix_menu() {
@@ -116,6 +128,46 @@ function domain_prefix_disable() {
 }
 
 /**
+ * Uniform prefix string
+ *
+ * @param $domain_id
+ *  The domain_id taken from {domain}.
+ */
+function domain_prefix_string($domain_id) {
+  return 'domain_'. $domain_id .'_';
+}
+
+/**
+ * Check for existing table prefixing.
+ */
+function domain_prefix_get_prefix() {
+  global $db_prefix;
+  // Check for existing table prefixing.
+  $prefix = NULL;
+  if (!empty($db_prefix)) {
+    if (is_array($db_prefix)) {
+      $prefix = $db_prefix['default'];
+    }
+    else {
+      $prefix = $db_prefix;
+    }
+  }
+  return $prefix;
+}
+
+/**
+ * Does a table exist -- we use this to bypass Drupal's default table prefixing check.
+ *
+ * @param $prefix
+ *   The table prefix used with this domain. Optional.
+ * @param $table
+ *   The name of the table being acted upon.
+ */
+function domain_prefix_table_exists($prefix, $table) {
+  return db_table_exists(db_escape_table($prefix . $table));
+}
+
+/**
  * Implements hook_domainlinks()
  *
  * @param $domain
@@ -154,4 +206,3 @@ function domain_prefix_domainupdate($op,
       break;
   }
 }
-
Index: domain_prefix/domain_prefix.path.inc
===================================================================
RCS file: domain_prefix/domain_prefix.path.inc
diff -N domain_prefix/domain_prefix.path.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ domain_prefix/domain_prefix.path.inc	18 Oct 2008 19:31:23 -0000
@@ -0,0 +1,51 @@
+<?php
+// $Id:$
+
+/**
+ * @file
+ * Implements hook_domainpath().
+ * This is a resource-intensive function, so we only include it
+ * if the {url_alias} table is present.
+ */
+
+/**
+ * Implements hook_domainpath().
+ */
+function domain_prefix_domainpath($domain_id, &$path, $path_language = NULL) {
+  global $language, $_domain;
+
+  // $map[$domain_id] keys are Drupal paths and the values are the corresponding aliases
+  static $map = array(), $count = array(), $status = array(), $tablename = array();
+
+  $path_language = (!empty($path_language)) ? $path_language : $language->language;
+
+  // The default domain does not use a prefix.
+  if ($domain_id == 0) {
+    $status[0] = db_table_exists('url_alias');
+    $tablename[0] = 'url_alias';
+  }
+
+  // Check to see if table prefixing has been done for the {url_alias} table.
+  if (!isset($status[$domain_id])) {
+    $prefix = domain_prefix_string($domain_id) . domain_prefix_get_prefix();
+    $status[$domain_id] = domain_prefix_table_exists($prefix, 'url_alias');
+    $tablename[$domain_id] = $prefix .'url_alias';
+  }
+
+  if (!$status[$domain_id]) {
+    return;
+  }
+
+  // Use $count[$domain_id] to avoid looking up paths in subsequent calls if there are no aliases
+  if (!isset($count[$domain_id])) {
+    $count[$domain_id] = db_result(db_query('SELECT COUNT(pid) FROM {%s}', $tablename[$domain_id]));
+  }
+  
+  if ($count[$domain_id] > 0) {
+    if (!isset($map[$domain_id][$path_language][$path])) {
+      $alias = db_result(db_query("SELECT dst FROM {%s} WHERE src = '%s'", $tablename[$domain_id], $path));
+      $map[$domain_id][$path_language][$path] = $alias ? $alias : $path;
+    }
+    $path = $map[$domain_id][$path_language][$path];
+  }
+}
