You are here

function drupal_lookup_path in Redis 7.2

Same name and namespace in other branches
  1. 7.3 redis.path.inc \drupal_lookup_path()

Given an alias, return its Drupal system URL if one exists. Given a Drupal system URL return one of its aliases if such a one exists. Otherwise, return FALSE.

Parameters

$action: One of the following values:

  • wipe: delete the alias cache.
  • alias: return an alias for a given Drupal system path (if one exists).
  • source: return the Drupal system URL for a path alias (if one exists).

$path: The path to investigate for corresponding aliases or system URLs.

$path_language: Optional language code to search the path with. Defaults to the page language. If there's no path defined for that language it will search paths without language.

Return value

Either a Drupal system path, an aliased path, or FALSE if no path was found.

2 calls to drupal_lookup_path()
drupal_get_normal_path in ./redis.path.inc
Given a path alias, return the internal path it represents.
drupal_get_path_alias in ./redis.path.inc
Given an internal Drupal path, return the alias set by the administrator.
2 string references to 'drupal_lookup_path'
drupal_cache_system_paths in ./redis.path.inc
Cache system paths for a page.
drupal_clear_path_cache in ./redis.path.inc
Clear the path cache.

File

./redis.path.inc, line 63
Drupal default includes/path.inc file copy which only differs in:

Code

function drupal_lookup_path($action, $path = '', $path_language = NULL) {
  global $language_url;
  static $cache, $denyAdmin;
  if (null === $cache) {
    $cache = array(
      'whitelist' => variable_get('path_alias_whitelist'),
    );
    if (null === $cache['whitelist']) {
      $cache['whitelist'] = drupal_path_alias_whitelist_rebuild();
    }
    $denyAdmin = (bool) variable_get('path_alias_admin_blacklist', true);
  }

  // If no language is explicitly specified we default to the current URL
  // language. If we used a language different from the one conveyed by the
  // requested URL, we might end up being unable to check if there is a path
  // alias matching the URL path.
  if (!($path_language = $path_language ? $path_language : $language_url->language)) {
    $path_language = LANGUAGE_NONE;
  }
  if (!empty($path) && isset($cache[$path_language][$action][$path])) {
    return $cache[$path_language][$action][$path];
  }
  $ret = null;
  $hashLookup = redis_path_backend_get();
  switch ($action) {
    case 'wipe':
      $cache = array();
      $cache['whitelist'] = drupal_path_alias_whitelist_rebuild();
      break;
    case 'alias':
      if (empty($path)) {
        return false;
      }

      // Check the path whitelist, if the top_level part before the first /
      // is not in the list, then there is no need to do anything further,
      // it is not in the database.
      if (!isset($cache['whitelist'][strtok($path, '/')])) {
        return false;
      }

      // Deny admin paths.
      if ($denyAdmin && path_is_admin($path)) {
        return false;
      }
      $ret = $hashLookup
        ->lookupAlias($path, $path_language);
      if (null === $ret) {

        // Original Drupal algorithm.
        // This will also update the $path_language variable so Redis will store
        // the right language (keeps track of LANGUAGE_NONE or specific language
        // so that default fallback behavior is the same that core).
        if ($path_language == LANGUAGE_NONE) {
          list($ret, $path_language) = db_query("SELECT alias, language FROM {url_alias} WHERE source = :source AND language = :language ORDER BY pid DESC LIMIT 1", array(
            ':source' => $path,
            ':language' => $path_language,
          ))
            ->fetch(PDO::FETCH_NUM);
        }
        else {
          if ($path_language > LANGUAGE_NONE) {
            list($ret, $path_language) = db_query("SELECT alias, language FROM {url_alias} WHERE source = :source AND language IN (:language) ORDER BY language DESC, pid DESC LIMIT 1", array(
              ':source' => $path,
              ':language' => array(
                $path_language,
                LANGUAGE_NONE,
              ),
            ))
              ->fetch(PDO::FETCH_NUM);
          }
          else {
            list($ret, $path_language) = db_query("SELECT alias, language FROM {url_alias} WHERE source = :source AND language IN (:language) ORDER BY language ASC, pid DESC LIMIT 1", array(
              ':source' => $path,
              ':language' => array(
                $path_language,
                LANGUAGE_NONE,
              ),
            ))
              ->fetch(PDO::FETCH_NUM);
          }
        }

        // Getting here with a value means we need to cache it
        if (empty($ret)) {
          $ret = false;
        }
        $hashLookup
          ->saveAlias($path, $ret, $path_language);
      }
      $cache[$path_language]['alias'][$path] = $ret;
      $cache[$path_language]['source'][$ret] = $path;
      break;
    case 'source':
      if (empty($path)) {
        return false;
      }

      // Even thought given entry is an alias, if it conflicts with an
      // existing admin path just deny any lookup.
      if ($denyAdmin && path_is_admin($path)) {
        return false;
      }
      $ret = $hashLookup
        ->lookupSource($path, $path_language);
      if (null === $ret) {

        // Original Drupal algorithm.
        // This will also update the $path_language variable so Redis will store
        // the right language (keeps track of LANGUAGE_NONE or specific language
        // so that default fallback behavior is the same that core).
        if ($path_language == LANGUAGE_NONE) {
          list($ret, $path_language) = db_query("SELECT source, language FROM {url_alias} WHERE alias = :alias AND language = :language ORDER BY pid DESC LIMIT 1", array(
            ':alias' => $path,
            ':language' => LANGUAGE_NONE,
          ))
            ->fetch(PDO::FETCH_NUM);
        }
        else {
          if ($path_language > LANGUAGE_NONE) {
            list($ret, $path_language) = db_query("SELECT source, language FROM {url_alias} WHERE alias = :alias AND language IN (:language) ORDER BY language DESC, pid DESC LIMIT 1", array(
              ':alias' => $path,
              ':language' => array(
                $path_language,
                LANGUAGE_NONE,
              ),
            ))
              ->fetch(PDO::FETCH_NUM);
          }
          else {
            list($ret, $path_language) = db_query("SELECT source, language FROM {url_alias} WHERE alias = :alias AND language IN (:language) ORDER BY language ASC, pid DESC LIMIT 1", array(
              ':alias' => $path,
              ':language' => array(
                $path_language,
                LANGUAGE_NONE,
              ),
            ))
              ->fetch(PDO::FETCH_NUM);
          }
        }

        // Getting here with a value means we need to cache it
        if (empty($ret)) {
          $ret = false;
        }
        $hashLookup
          ->saveAlias($ret, $path, $path_language);
      }
      $cache[$path_language]['alias'][$ret] = $path;
      $cache[$path_language]['source'][$path] = $ret;
      break;
  }
  return $ret;
}