Index: includes/path.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/path.inc,v
retrieving revision 1.16
diff -u -F^f -r1.16 path.inc
--- includes/path.inc	18 Jun 2007 06:59:11 -0000	1.16
+++ includes/path.inc	26 Jul 2007 15:15:44 -0000
@@ -58,12 +58,25 @@ function drupal_lookup_path($action, $pa
   if ($action == 'wipe') {
     $map = array();
     $no_src = array();
+    drupal_rebuild_whitelist();
   }
   elseif ($count > 0 && $path != '') {
+    // Load the whitelist
+    $whitelist = variable_get('alias_whitelist', array());
+    // And derive the top level component of the path
+    $pos = strpos($path, '/');
+    $top_level = ($pos) ? substr($path, 0, $pos) : $path;
+
+
     if ($action == 'alias') {
       if (isset($map[$path_language][$path])) {
         return $map[$path_language][$path];
       }
+      // Check the whitelist, if the top_level is not in it, then
+      // no need to do anything further, it is not in the database
+      if (!isset($whitelist[$top_level])) {
+        return FALSE;
+      }
       // Get the most fitting result falling back with alias without language
       $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC", $path, $path_language));
       $map[$path_language][$path] = $alias;
@@ -220,3 +233,21 @@ function drupal_is_front_page() {
   // we can check it against the 'site_frontpage' variable.
   return $_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node'));
 }
+
+/**
+ * Rebuild a white list of top level paths, depending on what
+ * is stored in the url_alias table for this site.
+ *
+ */
+function drupal_rebuild_whitelist() {
+  $whitelist = array();
+
+  // For each alias in the database, get the top level (i.e. the portion before the first /).
+  // Using GROUP BY is faster than DISTINCT, at least for MyISAM.
+  $result = db_query("SELECT SUBSTRING_INDEX(src, '/', 1) AS path FROM {url_alias} GROUP BY path");
+  while ($row = db_fetch_object($result)) {
+    $whitelist[$row->path] = TRUE;
+  }
+
+  variable_set('alias_whitelist', $whitelist);
+}
