2 // $Id: path.inc 144 2007-03-28 07:52:20Z thierry $
6 * Functions to handle paths in Drupal, including path aliasing.
8 * These functions are not loaded for cached pages, but modules that need
9 * to use them in hook_init() or hook exit() can make them available, by
10 * executing "drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);".
14 * Initialize the $_GET['q'] variable to the proper normal path.
16 function drupal_init_path() {
17 if (!empty($_GET['q'])) {
18 $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
21 $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
26 * Given an alias, return its Drupal system URL if one exists. Given a Drupal
27 * system URL return its alias if one exists.
30 * One of the following values:
31 * - wipe: delete the alias cache.
32 * - alias: return an alias for a given Drupal system path (if one exists).
33 * - source: return the Drupal system URL for a path alias (if one exists).
35 * The path to investigate for corresponding aliases or system URLs.
38 * Either a Drupal system path, an aliased path, or FALSE if no path was
41 function drupal_lookup_path($action, $path = '') {
42 static $map = array();
45 if ($count === NULL) {
46 $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));
49 if ($action == 'wipe') {
52 elseif ($count > 0 && $path != '') {
53 if ($action == 'alias') {
54 if (isset($map[$path])) {
57 if ($alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path))) {
65 elseif ($action == 'source') {
66 if ($alias = array_search($path, $map)) {
69 if (!isset($map[$path])) {
70 if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) {
82 * Given an internal Drupal path, return the alias set by the administrator.
85 * An internal Drupal path.
88 * An aliased path if one was found, or the original path if no alias was
91 function drupal_get_path_alias($path) {
93 if ($alias = drupal_lookup_path('alias', $path)) {
96 if (function_exists('custom_url_rewrite')) {
97 $result = custom_url_rewrite('alias', $result, $path);
103 * Given a path alias, return the internal path it represents.
106 * A Drupal path alias.
109 * The internal path represented by the alias, or the original alias if no
110 * internal path was found.
112 function drupal_get_normal_path($path) {
114 if ($src = drupal_lookup_path('source', $path)) {
117 if (function_exists('custom_url_rewrite')) {
118 $result = custom_url_rewrite('source', $result, $path);
124 * Return a component of the current Drupal path.
126 * When viewing a page at the path "admin/node/configure", for example, arg(0)
127 * would return "admin", arg(1) would return "node", and arg(2) would return
130 * Avoid use of this function where possible, as resulting code is hard to read.
131 * Instead, attempt to use named arguments in menu callback functions. See the
132 * explanation in menu.inc for how to construct callbacks that take arguments.
135 * The index of the component, where each component is separated by a '/'
136 * (forward-slash), and where the first component has an index of 0 (zero).
139 * The component specified by $index, or FALSE if the specified component was
142 function arg($index) {
143 static $arguments, $q;
145 if (empty($arguments) || $q != $_GET['q']) {
146 $arguments = explode('/', $_GET['q']);
150 if (isset($arguments[$index])) {
151 return $arguments[$index];
156 * Get the title of the current page, for display on the page and in the title bar.
159 * The current page's title.
161 function drupal_get_title() {
162 $title = drupal_set_title();
164 // during a bootstrap, menu.inc is not included and thus we cannot provide a title
165 if (!isset($title) && function_exists('menu_get_active_title')) {
166 $title = check_plain(menu_get_active_title());
173 * Set the title of the current page, for display on the page and in the title bar.
176 * Optional string value to assign to the page title; or if set to NULL
177 * (default), leaves the current title unchanged.
180 * The updated title of the current page.
182 function drupal_set_title($title = NULL) {
183 static $stored_title;
186 $stored_title = $title;
188 return $stored_title;
192 * Check if the current page is the front page.
195 * Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
197 function drupal_is_front_page() {
198 // As drupal_init_path updates $_GET['q'] with the 'site_frontpage' path,
199 // we can check it against the 'site_frontpage' variable.
200 return $_GET['q'] == variable_get('site_frontpage', 'node');