You are here

function xmlsitemap_get_path_alias in XML sitemap 7.2

Same name and namespace in other branches
  1. 6.2 xmlsitemap.generate.inc \xmlsitemap_get_path_alias()

Given an internal Drupal path, return the alias for the path.

This is similar to drupal_get_path_alias(), but designed to fetch all alises at once so that only one database query is executed instead of several or possibly thousands during sitemap generation.

Parameters

string $path: An internal Drupal path.

string $language: A language code to use when looking up the paths.

1 call to xmlsitemap_get_path_alias()
xmlsitemap_generate_chunk in ./xmlsitemap.generate.inc
Generate chunk.

File

./xmlsitemap.generate.inc, line 22
Sitemap generation and rebuilding functions for the xmlsitemap module.

Code

function xmlsitemap_get_path_alias($path, $language) {
  static $aliases;
  static $last_language;
  if (!isset($aliases)) {
    $aliases[LANGUAGE_NONE] = db_query("SELECT source, alias FROM {url_alias} WHERE language = :language ORDER BY pid", array(
      ':language' => LANGUAGE_NONE,
    ))
      ->fetchAllKeyed();
  }
  if ($language != LANGUAGE_NONE && $last_language != $language) {
    unset($aliases[$last_language]);
    $aliases[$language] = db_query("SELECT source, alias FROM {url_alias} WHERE language = :language ORDER BY pid", array(
      ':language' => $language,
    ))
      ->fetchAllKeyed();
    $last_language = $language;
  }

  // We need to pass our path through hook_url_outbound_alter(). This fixes
  // clean URLs not working when they don't exist in the {url_alias} table and
  // are created with something like subpathauto.
  $normalized_path = $path;

  // hook_url_outbound_alter() expects defaults in url() options.
  $options = array(
    'fragment' => '',
    'query' => array(),
    'absolute' => FALSE,
    'alias' => FALSE,
    'prefix' => '',
    'external' => FALSE,
  );
  if ($language != LANGUAGE_NONE && isset($aliases[$language][$path])) {
    $normalized_path = $aliases[$language][$path];
    $options['alias'] = TRUE;
  }
  elseif (isset($aliases[LANGUAGE_NONE][$path])) {
    $normalized_path = $aliases[LANGUAGE_NONE][$path];
    $options['alias'] = TRUE;
  }
  $original_path = $normalized_path;
  drupal_alter('url_outbound', $normalized_path, $options, $original_path);
  return $normalized_path;
}