initial import from onelab svn codebase
[plewww.git] / includes / path.inc
1 <?php
2 // $Id: path.inc 144 2007-03-28 07:52:20Z thierry $
3
4 /**
5  * @file
6  * Functions to handle paths in Drupal, including path aliasing.
7  *
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);".
11  */
12
13 /**
14  * Initialize the $_GET['q'] variable to the proper normal path.
15  */
16 function drupal_init_path() {
17   if (!empty($_GET['q'])) {
18     $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
19   }
20   else {
21     $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
22   }
23 }
24
25 /**
26  * Given an alias, return its Drupal system URL if one exists. Given a Drupal
27  * system URL return its alias if one exists.
28  *
29  * @param $action
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).
34  * @param $path
35  *   The path to investigate for corresponding aliases or system URLs.
36  *
37  * @return
38  *   Either a Drupal system path, an aliased path, or FALSE if no path was
39  *   found.
40  */
41 function drupal_lookup_path($action, $path = '') {
42   static $map = array();
43   static $count = NULL;
44
45   if ($count === NULL) {
46     $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));
47   }
48
49   if ($action == 'wipe') {
50     $map = array();
51   }
52   elseif ($count > 0 && $path != '') {
53     if ($action == 'alias') {
54       if (isset($map[$path])) {
55         return $map[$path];
56       }
57       if ($alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path))) {
58         $map[$path] = $alias;
59         return $alias;
60       }
61       else {
62         $map[$path] = $path;
63       }
64     }
65     elseif ($action == 'source') {
66       if ($alias = array_search($path, $map)) {
67         return $alias;
68       }
69       if (!isset($map[$path])) {
70         if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) {
71           $map[$src] = $path;
72           return $src;
73         }
74       }
75     }
76   }
77
78   return FALSE;
79 }
80
81 /**
82  * Given an internal Drupal path, return the alias set by the administrator.
83  *
84  * @param $path
85  *   An internal Drupal path.
86  *
87  * @return
88  *   An aliased path if one was found, or the original path if no alias was
89  *   found.
90  */
91 function drupal_get_path_alias($path) {
92   $result = $path;
93   if ($alias = drupal_lookup_path('alias', $path)) {
94     $result = $alias;
95   }
96   if (function_exists('custom_url_rewrite')) {
97     $result = custom_url_rewrite('alias', $result, $path);
98   }
99   return $result;
100 }
101
102 /**
103  * Given a path alias, return the internal path it represents.
104  *
105  * @param $path
106  *   A Drupal path alias.
107  *
108  * @return
109  *   The internal path represented by the alias, or the original alias if no
110  *   internal path was found.
111  */
112 function drupal_get_normal_path($path) {
113   $result = $path;
114   if ($src = drupal_lookup_path('source', $path)) {
115     $result = $src;
116   }
117   if (function_exists('custom_url_rewrite')) {
118     $result = custom_url_rewrite('source', $result, $path);
119   }
120   return $result;
121 }
122
123 /**
124  * Return a component of the current Drupal path.
125  *
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
128  * "configure".
129  *
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.
133  *
134  * @param $index
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).
137  *
138  * @return
139  *   The component specified by $index, or FALSE if the specified component was
140  *   not found.
141  */
142 function arg($index) {
143   static $arguments, $q;
144
145   if (empty($arguments) || $q != $_GET['q']) {
146     $arguments = explode('/', $_GET['q']);
147     $q = $_GET['q'];
148   }
149
150   if (isset($arguments[$index])) {
151     return $arguments[$index];
152   }
153 }
154
155 /**
156  * Get the title of the current page, for display on the page and in the title bar.
157  *
158  * @return
159  *   The current page's title.
160  */
161 function drupal_get_title() {
162   $title = drupal_set_title();
163
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());
167   }
168
169   return $title;
170 }
171
172 /**
173  * Set the title of the current page, for display on the page and in the title bar.
174  *
175  * @param $title
176  *   Optional string value to assign to the page title; or if set to NULL
177  *   (default), leaves the current title unchanged.
178  *
179  * @return
180  *   The updated title of the current page.
181  */
182 function drupal_set_title($title = NULL) {
183   static $stored_title;
184
185   if (isset($title)) {
186     $stored_title = $title;
187   }
188   return $stored_title;
189 }
190
191 /**
192  * Check if the current page is the front page.
193  *
194  * @return
195  *   Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
196  */
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');
201 }