Index: includes/path.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/path.inc,v retrieving revision 1.13 diff -u -r1.13 path.inc --- includes/path.inc 23 Dec 2006 22:04:52 -0000 1.13 +++ includes/path.inc 30 Sep 2008 05:17:50 -0000 @@ -46,42 +46,73 @@ // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases if (!isset($count)) { - $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}')); + $count = (bool)db_result(db_query_range('SELECT pid FROM {url_alias}', 0, 1)); } - - if ($action == 'wipe') { - $map = array(); - $no_src = array(); - } - elseif ($count > 0 && $path != '') { - if ($action == 'alias') { - if (isset($map[$path])) { - return $map[$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; } - $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path)); - $map[$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])) { - // Look for the value $path within the cached $map - if (!$src = array_search($path, $map)) { - if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) { - $map[$src] = $path; + variable_set('alias_whitelist', $whitelist); + return; + case 'alias': + if ($count && $path != '') { + if (isset($map[$path])) { + return $map[$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] = TRUE; + // 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; } + $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path)); + return $alias; } - return $src; - } - } - - return FALSE; + return; + case 'source': + if ($count && $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])) { + // Look for the value $path within the cached $map + if (!$src = array_search($path, $map)) { + if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) { + $map[$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] = TRUE; + } + } + return $src; + } + } + break; + } + return FALSE; } /**