webmaster@1: language; webmaster@1: webmaster@1: // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases webmaster@1: if (!isset($count)) { webmaster@1: $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}')); webmaster@1: } webmaster@1: webmaster@1: if ($action == 'wipe') { webmaster@1: $map = array(); webmaster@1: $no_src = array(); webmaster@13: $count = NULL; webmaster@1: } webmaster@1: elseif ($count > 0 && $path != '') { webmaster@1: if ($action == 'alias') { webmaster@1: if (isset($map[$path_language][$path])) { webmaster@1: return $map[$path_language][$path]; webmaster@1: } webmaster@1: // Get the most fitting result falling back with alias without language webmaster@1: $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC", $path, $path_language)); webmaster@1: $map[$path_language][$path] = $alias; webmaster@1: return $alias; webmaster@1: } webmaster@1: // Check $no_src for this $path in case we've already determined that there webmaster@1: // isn't a path that has this alias webmaster@1: elseif ($action == 'source' && !isset($no_src[$path_language][$path])) { webmaster@1: // Look for the value $path within the cached $map webmaster@1: $src = ''; webmaster@1: if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) { webmaster@1: // Get the most fitting result falling back with alias without language webmaster@1: 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))) { webmaster@1: $map[$path_language][$src] = $path; webmaster@1: } webmaster@1: else { webmaster@1: // We can't record anything into $map because we do not have a valid webmaster@1: // index and there is no need because we have not learned anything webmaster@1: // about any Drupal path. Thus cache to $no_src. webmaster@1: $no_src[$path_language][$path] = TRUE; webmaster@1: } webmaster@1: } webmaster@1: return $src; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: return FALSE; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Given an internal Drupal path, return the alias set by the administrator. webmaster@1: * webmaster@1: * @param $path webmaster@1: * An internal Drupal path. webmaster@1: * @param $path_language webmaster@1: * An optional language code to look up the path in. webmaster@1: * webmaster@1: * @return webmaster@1: * An aliased path if one was found, or the original path if no alias was webmaster@1: * found. webmaster@1: */ webmaster@1: function drupal_get_path_alias($path, $path_language = '') { webmaster@1: $result = $path; webmaster@1: if ($alias = drupal_lookup_path('alias', $path, $path_language)) { webmaster@1: $result = $alias; webmaster@1: } webmaster@1: return $result; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Given a path alias, return the internal path it represents. webmaster@1: * webmaster@1: * @param $path webmaster@1: * A Drupal path alias. webmaster@1: * @param $path_language webmaster@1: * An optional language code to look up the path in. webmaster@1: * webmaster@1: * @return webmaster@1: * The internal path represented by the alias, or the original alias if no webmaster@1: * internal path was found. webmaster@1: */ webmaster@1: function drupal_get_normal_path($path, $path_language = '') { webmaster@1: $result = $path; webmaster@1: if ($src = drupal_lookup_path('source', $path, $path_language)) { webmaster@1: $result = $src; webmaster@1: } webmaster@1: if (function_exists('custom_url_rewrite_inbound')) { webmaster@1: // Modules may alter the inbound request path by reference. webmaster@1: custom_url_rewrite_inbound($result, $path, $path_language); webmaster@1: } webmaster@1: return $result; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Return a component of the current Drupal path. webmaster@1: * webmaster@1: * When viewing a page at the path "admin/content/types", for example, arg(0) webmaster@1: * would return "admin", arg(1) would return "content", and arg(2) would return webmaster@1: * "types". webmaster@1: * webmaster@1: * Avoid use of this function where possible, as resulting code is hard to read. webmaster@1: * Instead, attempt to use named arguments in menu callback functions. See the webmaster@1: * explanation in menu.inc for how to construct callbacks that take arguments. webmaster@1: * webmaster@1: * @param $index webmaster@1: * The index of the component, where each component is separated by a '/' webmaster@1: * (forward-slash), and where the first component has an index of 0 (zero). webmaster@1: * webmaster@1: * @return webmaster@1: * The component specified by $index, or NULL if the specified component was webmaster@1: * not found. webmaster@1: */ webmaster@1: function arg($index = NULL, $path = NULL) { webmaster@1: static $arguments; webmaster@1: webmaster@1: if (!isset($path)) { webmaster@1: $path = $_GET['q']; webmaster@1: } webmaster@1: if (!isset($arguments[$path])) { webmaster@1: $arguments[$path] = explode('/', $path); webmaster@1: } webmaster@1: if (!isset($index)) { webmaster@1: return $arguments[$path]; webmaster@1: } webmaster@1: if (isset($arguments[$path][$index])) { webmaster@1: return $arguments[$path][$index]; webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Get the title of the current page, for display on the page and in the title bar. webmaster@1: * webmaster@1: * @return webmaster@1: * The current page's title. webmaster@1: */ webmaster@1: function drupal_get_title() { webmaster@1: $title = drupal_set_title(); webmaster@1: webmaster@1: // during a bootstrap, menu.inc is not included and thus we cannot provide a title webmaster@1: if (!isset($title) && function_exists('menu_get_active_title')) { webmaster@1: $title = check_plain(menu_get_active_title()); webmaster@1: } webmaster@1: webmaster@1: return $title; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Set the title of the current page, for display on the page and in the title bar. webmaster@1: * webmaster@1: * @param $title webmaster@1: * Optional string value to assign to the page title; or if set to NULL webmaster@1: * (default), leaves the current title unchanged. webmaster@1: * webmaster@1: * @return webmaster@1: * The updated title of the current page. webmaster@1: */ webmaster@1: function drupal_set_title($title = NULL) { webmaster@1: static $stored_title; webmaster@1: webmaster@1: if (isset($title)) { webmaster@1: $stored_title = $title; webmaster@1: } webmaster@1: return $stored_title; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Check if the current page is the front page. webmaster@1: * webmaster@1: * @return webmaster@1: * Boolean value: TRUE if the current page is the front page; FALSE if otherwise. webmaster@1: */ webmaster@1: function drupal_is_front_page() { webmaster@1: // As drupal_init_path updates $_GET['q'] with the 'site_frontpage' path, webmaster@1: // we can check it against the 'site_frontpage' variable. webmaster@1: return $_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node')); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Check if a path matches any pattern in a set of patterns. webmaster@1: * webmaster@1: * @param $path webmaster@1: * The path to match. webmaster@1: * @param $patterns webmaster@1: * String containing a set of patterns separated by \n, \r or \r\n. webmaster@1: * webmaster@1: * @return webmaster@1: * Boolean value: TRUE if the path matches a pattern, FALSE otherwise. webmaster@1: */ webmaster@1: function drupal_match_path($path, $patterns) { webmaster@1: static $regexps; webmaster@1: webmaster@1: if (!isset($regexps[$patterns])) { webmaster@1: $regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/'; webmaster@1: } webmaster@1: return preg_match($regexps[$patterns], $path); webmaster@1: }