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 27 Jul 2007 02:06:10 -0000 @@ -55,39 +55,72 @@ function drupal_lookup_path($action, $pa $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}')); } - if ($action == 'wipe') { - $map = array(); - $no_src = array(); - } - elseif ($count > 0 && $path != '') { - if ($action == 'alias') { - if (isset($map[$path_language][$path])) { - return $map[$path_language][$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; + + switch($action) { + case 'wipe': + $map = array(); + $no_src = array(); + $count = 0; + + // Rebuild a white list of top level paths, depending on what + // is stored in the url_alias table for this site. + $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; } - // 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; - return $alias; - } - // Check $no_src for this $path in case we've already determined that there - // isn't a path that has this alias - elseif ($action == 'source' && !isset($no_src[$path_language][$path])) { - // Look for the value $path within the cached $map - $src = ''; - if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) { - // Get the most fitting result falling back with alias without language - if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s' AND language IN('%s', '') ORDER BY language DESC", $path, $path_language))) { - $map[$path_language][$src] = $path; + + variable_set('alias_whitelist', $whitelist); + return; + + case 'alias': + if ($count > 0 && $path != '') { + 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; } - else { - // We can't record anything into $map because we do not have a valid - // index and there is no need because we have not learned anything - // about any Drupal path. Thus cache to $no_src. - $no_src[$path_language][$path] = TRUE; + // 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; + return $alias; + } + return; + + case 'source': + if ($count > 0 && $path != '') { + // Check $no_src for this $path in case we've already determined that there + // isn't a path that has this alias + if (!isset($no_src[$path_language][$path])) { + // Look for the value $path within the cached $map + $src = ''; + if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) { + // Get the most fitting result falling back with alias without language + if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s' AND language IN('%s', '') ORDER BY language DESC", $path, $path_language))) { + $map[$path_language][$src] = $path; + } + else { + // We can't record anything into $map because we do not have a valid + // index and there is no need because we have not learned anything + // about any Drupal path. Thus cache to $no_src. + $no_src[$path_language][$path] = TRUE; + } + } + return $src; } } - return $src; - } + break; } return FALSE; @@ -220,3 +253,4 @@ 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')); } +