Simpletest Coverage - includes/path.inc

1 <?php
2 // $Id: path.inc,v 1.44 2009/08/02 06:48:24 webchick Exp $
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_path_initialize() {
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 one of its aliases if such a one exists. Otherwise,
28 * return FALSE.
29 *
30 * @param $action
31 * One of the following values:
32 * - wipe: delete the alias cache.
33 * - alias: return an alias for a given Drupal system path (if one exists).
34 * - source: return the Drupal system URL for a path alias (if one exists).
35 * @param $path
36 * The path to investigate for corresponding aliases or system URLs.
37 * @param $path_language
38 * Optional language code to search the path with. Defaults to the page language.
39 * If there's no path defined for that language it will search paths without
40 * language.
41 *
42 * @return
43 * Either a Drupal system path, an aliased path, or FALSE if no path was
44 * found.
45 */
46 function drupal_lookup_path($action, $path = '', $path_language = '') {
47 global $language;
48 $cache = &drupal_static(__FUNCTION__, array(
49 'map' => array(),
50 'no_src' => array(),
51 'whitelist' => NULL,
52 'system_paths' => array(),
53 'no_aliases' => array(),
54 'first_call' => TRUE,
55 ));
56
57 // Retrieve the path alias whitelist.
58 if (!isset($cache['whitelist'])) {
59 $cache['whitelist'] = variable_get('path_alias_whitelist', NULL);
60 if (!isset($cache['whitelist'])) {
61 $cache['whitelist'] = drupal_path_alias_whitelist_rebuild();
62 }
63 }
64
65 $path_language = $path_language ? $path_language : $language->language;
66
67 if ($action == 'wipe') {
68 $cache = array();
69 $cache['whitelist'] = drupal_path_alias_whitelist_rebuild();
70 }
71 elseif ($cache['whitelist'] && $path != '') {
72 if ($action == 'alias') {
73 // During the first call to drupal_lookup_path() per language, load the
74 // expected system paths for the page from cache.
75 if (!empty($cache['first_call'])) {
76 $cache['first_call'] = FALSE;
77
78 $cache['map'][$path_language] = array();
79 // Load system paths from cache.
80 $cid = current_path();
81 if ($cached = cache_get($cid, 'cache_path')) {
82 $cache['system_paths'] = $cached->data;
83 // Now fetch the aliases corresponding to these system paths.
84 // We order by ASC and overwrite array keys to ensure the correct
85 // alias is used when there are multiple aliases per path.
86 $cache['map'][$path_language] = db_query("SELECT src, dst FROM {url_alias} WHERE src IN (:system) AND language IN (:language, '') ORDER BY language ASC, pid ASC", array(
87 ':system' => $cache['system_paths'],
88 ':language' => $path_language
89 ))->fetchAllKeyed();
90 // Keep a record of paths with no alias to avoid querying twice.
91 $cache['no_aliases'][$path_language] = array_flip(array_diff_key($cache['system_paths'], array_keys($cache['map'][$path_language])));
92 }
93 }
94 // If the alias has already been loaded, return it.
95 if (isset($cache['map'][$path_language][$path])) {
96 return $cache['map'][$path_language][$path];
97 }
98 // Check the path whitelist, if the top_level part before the first /
99 // is not in the list, then there is no need to do anything further,
100 // it is not in the database.
101 elseif (!isset($cache['whitelist'][strtok($path, '/')])) {
102 return FALSE;
103 }
104 // For system paths which were not cached, query aliases individually.
105 else if (!isset($cache['no_aliases'][$path_language][$path])) {
106 // Get the most fitting result falling back with alias without language
107 $alias = db_query("SELECT dst FROM {url_alias} WHERE src = :src AND language IN (:language, '') ORDER BY language DESC, pid DESC", array(
108 ':src' => $path,
109 ':language' => $path_language
110 ))->fetchField();
111 $cache['map'][$path_language][$path] = $alias;
112 return $alias;
113 }
114 }
115 // Check $no_src for this $path in case we've already determined that there
116 // isn't a path that has this alias
117 elseif ($action == 'source' && !isset($cache['no_src'][$path_language][$path])) {
118 // Look for the value $path within the cached $map
119 $src = '';
120 if (!isset($cache['map'][$path_language]) || !($src = array_search($path, $cache['map'][$path_language]))) {
121 // Get the most fitting result falling back with alias without language
122 if ($src = db_query("SELECT src FROM {url_alias} WHERE dst = :dst AND language IN (:language, '') ORDER BY language DESC, pid DESC", array(
123 ':dst' => $path,
124 ':language' => $path_language))
125 ->fetchField()) {
126 $cache['map'][$path_language][$src] = $path;
127 }
128 else {
129 // We can't record anything into $map because we do not have a valid
130 // index and there is no need because we have not learned anything
131 // about any Drupal path. Thus cache to $no_src.
132 $cache['no_src'][$path_language][$path] = TRUE;
133 }
134 }
135 return $src;
136 }
137 }
138
139 return FALSE;
140 }
141
142 /**
143 * Cache system paths for a page.
144 *
145 * Cache an array of the system paths available on each page. We assume
146 * that aiases will be needed for the majority of these paths during
147 * subsequent requests, and load them in a single query during
148 * drupal_lookup_path().
149 */
150 function drupal_cache_system_paths() {
151 // Check if the system paths for this page were loaded from cache in this
152 // request to avoid writing to cache on every request.
153 $cache = &drupal_static('drupal_lookup_path', array());
154 if (!$cache['system_paths']) {
155 // Generate a cache ID (cid) specifically for this page.
156 $cid = current_path();
157 // The static $map array used by drupal_lookup_path() includes all
158 // system paths for the page request.
159 if ($paths = current($cache['map'])) {
160 $data = array_keys($paths);
161 $expire = REQUEST_TIME + (60 * 60 * 24);
162 cache_set($cid, $data, 'cache_path', $expire);
163 }
164 }
165 }
166
167 /**
168 * Given an internal Drupal path, return the alias set by the administrator.
169 *
170 * If no path is provided, the function will return the alias of the current
171 * page.
172 *
173 * @param $path
174 * An internal Drupal path.
175 * @param $path_language
176 * An optional language code to look up the path in.
177 *
178 * @return
179 * An aliased path if one was found, or the original path if no alias was
180 * found.
181 */
182 function drupal_get_path_alias($path = NULL, $path_language = '') {
183 // If no path is specified, use the current page's path.
184 if ($path == NULL) {
185 $path = $_GET['q'];
186 }
187 $result = $path;
188 if ($alias = drupal_lookup_path('alias', $path, $path_language)) {
189 $result = $alias;
190 }
191 return $result;
192 }
193
194 /**
195 * Given a path alias, return the internal path it represents.
196 *
197 * @param $path
198 * A Drupal path alias.
199 * @param $path_language
200 * An optional language code to look up the path in.
201 *
202 * @return
203 * The internal path represented by the alias, or the original alias if no
204 * internal path was found.
205 */
206 function drupal_get_normal_path($path, $path_language = '') {
207 $result = $path;
208 if ($src = drupal_lookup_path('source', $path, $path_language)) {
209 $result = $src;
210 }
211 if (function_exists('custom_url_rewrite_inbound')) {
212 // Modules may alter the inbound request path by reference.
213 custom_url_rewrite_inbound($result, $path, $path_language);
214 }
215 return $result;
216 }
217
218 /**
219 * Return a component of the current Drupal path.
220 *
221 * When viewing a page at the path "admin/structure/types", for example, arg(0)
222 * returns "admin", arg(1) returns "content", and arg(2) returns "types".
223 *
224 * Avoid use of this function where possible, as resulting code is hard to read.
225 * In menu callback functions, attempt to use named arguments. See the explanation
226 * in menu.inc for how to construct callbacks that take arguments. When attempting
227 * to use this function to load an element from the current path, e.g. loading the
228 * node on a node page, please use menu_get_object() instead.
229 *
230 * @param $index
231 * The index of the component, where each component is separated by a '/'
232 * (forward-slash), and where the first component has an index of 0 (zero).
233 * @param $path
234 * A path to break into components. Defaults to the path of the current page.
235 *
236 * @return
237 * The component specified by $index, or NULL if the specified component was
238 * not found.
239 */
240 function arg($index = NULL, $path = NULL) {
241 $arguments = &drupal_static(__FUNCTION__);
242
243 if (!isset($path)) {
244 $path = $_GET['q'];
245 }
246 if (!isset($arguments[$path])) {
247 $arguments[$path] = explode('/', $path);
248 }
249 if (!isset($index)) {
250 return $arguments[$path];
251 }
252 if (isset($arguments[$path][$index])) {
253 return $arguments[$path][$index];
254 }
255 }
256
257 /**
258 * Get the title of the current page, for display on the page and in the title bar.
259 *
260 * @return
261 * The current page's title.
262 */
263 function drupal_get_title() {
264 $title = drupal_set_title();
265
266 // During a bootstrap, menu.inc is not included and thus we cannot provide a title.
267 if (!isset($title) && function_exists('menu_get_active_title')) {
268 $title = check_plain(menu_get_active_title());
269 }
270
271 return $title;
272 }
273
274 /**
275 * Set the title of the current page, for display on the page and in the title bar.
276 *
277 * @param $title
278 * Optional string value to assign to the page title; or if set to NULL
279 * (default), leaves the current title unchanged.
280 * @param $output
281 * Optional flag - normally should be left as CHECK_PLAIN. Only set to
282 * PASS_THROUGH if you have already removed any possibly dangerous code
283 * from $title using a function like check_plain() or filter_xss(). With this
284 * flag the string will be passed through unchanged.
285 *
286 * @return
287 * The updated title of the current page.
288 */
289 function drupal_set_title($title = NULL, $output = CHECK_PLAIN) {
290 $stored_title = &drupal_static(__FUNCTION__);
291
292 if (isset($title)) {
293 $stored_title = ($output == PASS_THROUGH) ? $title : check_plain($title);
294 }
295
296 return $stored_title;
297 }
298
299 /**
300 * Check if the current page is the front page.
301 *
302 * @return
303 * Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
304 */
305 function drupal_is_front_page() {
306 $is_front_page = &drupal_static(__FUNCTION__);
307
308 if (!isset($is_front_page)) {
309 // As drupal_path_initialize updates $_GET['q'] with the 'site_frontpage' path,
310 // we can check it against the 'site_frontpage' variable.
311 $is_front_page = ($_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node')));
312 }
313
314 return $is_front_page;
315 }
316
317 /**
318 * Check if a path matches any pattern in a set of patterns.
319 *
320 * @param $path
321 * The path to match.
322 * @param $patterns
323 * String containing a set of patterns separated by \n, \r or \r\n.
324 *
325 * @return
326 * Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
327 */
328 function drupal_match_path($path, $patterns) {
329 $regexps = &drupal_static(__FUNCTION__);
330
331 if (!isset($regexps[$patterns])) {
332 $regexps[$patterns] = '/^(' . preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\2'), preg_quote($patterns, '/')) . ')$/';
333 }
334 return (bool)preg_match($regexps[$patterns], $path);
335 }
336
337 /**
338 * Return the current URL path of the page being viewed.
339 *
340 * Examples:
341 * - http://example.com/node/306 returns "node/306".
342 * - http://example.com/drupalfolder/node/306 returns "node/306" while
343 * base_path() returns "/drupalfolder/".
344 * - http://example.com/path/alias (which is a path alias for node/306) returns
345 * "node/306" as opposed to the path alias.
346 *
347 * This function is not available in hook_boot() so use $_GET['q'] instead.
348 * However, be careful when doing that because in the case of Example #3
349 * $_GET['q'] will contain "path/alias". If "node/306" is needed, calling
350 * drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH) makes this function available.
351 *
352 * @return
353 * The current Drupal URL path.
354 */
355 function current_path() {
356 return $_GET['q'];
357 }
358
359 /**
360 * Rebuild the path alias white list.
361 *
362 * @return
363 * An array containing a white list of path aliases.
364 */
365 function drupal_path_alias_whitelist_rebuild() {
366 // For each alias in the database, get the top level component of the system
367 // path it corresponds to. This is the portion of the path before the first /
368 // if present, otherwise the whole path itself.
369 $whitelist = array();
370 $result = db_query("SELECT SUBSTRING_INDEX(src, '/', 1) AS path FROM {url_alias} GROUP BY path");
371 foreach ($result as $row) {
372 $whitelist[$row->path] = TRUE;
373 }
374 variable_set('path_alias_whitelist', $whitelist);
375 return $whitelist;
376 }
377

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.