Code coverage for /20081101/includes/path.inc

Line #Times calledCode
1
<?php
2
// $Id: path.inc,v 1.28 2008/10/14 11:01:08 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
 */
162366
function drupal_init_path() {
172366
  if (!empty($_GET['q'])) {
182106
    $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
192106
  }
20
  else {
21260
    $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage',
'node'));
22
  }
232366
}
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
 */
462366
function drupal_lookup_path($action, $path = '', $path_language = '') {
472442
  global $language;
48
  // $map is an array with language keys, holding arrays of Drupal paths to
alias relations
492442
  static $map = array(), $no_src = array(), $count;
50
512442
  $path_language = $path_language ? $path_language : $language->language;
52
53
  // Use $count to avoid looking up paths in subsequent calls if there
simply are no aliases
542442
  if (!isset($count)) {
552367
    $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));
562367
  }
57
582442
  if ($action == 'wipe') {
5912
    $map = array();
6012
    $no_src = array();
6112
    $count = NULL;
6212
  }
632442
  elseif ($count > 0 && $path != '') {
6439
    if ($action == 'alias') {
6539
      if (isset($map[$path_language][$path])) {
6632
        return $map[$path_language][$path];
670
      }
68
      // Get the most fitting result falling back with alias without
language
6939
      $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src =
'%s' AND language IN('%s', '') ORDER BY language DESC", $path,
$path_language));
7039
      $map[$path_language][$path] = $alias;
7139
      return $alias;
720
    }
73
    // Check $no_src for this $path in case we've already determined that
there
74
    // isn't a path that has this alias
7535
    elseif ($action == 'source' && !isset($no_src[$path_language][$path]))
{
76
      // Look for the value $path within the cached $map
7735
      $src = '';
7835
      if (!isset($map[$path_language]) || !($src = array_search($path,
$map[$path_language]))) {
79
        // Get the most fitting result falling back with alias without
language
8035
        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))) {
819
          $map[$path_language][$src] = $path;
829
        }
83
        else {
84
          // We can't record anything into $map because we do not have a
valid
85
          // index and there is no need because we have not learned
anything
86
          // about any Drupal path. Thus cache to $no_src.
8735
          $no_src[$path_language][$path] = TRUE;
88
        }
8935
      }
9035
      return $src;
910
    }
9228
  }
93
942441
  return FALSE;
950
}
96
97
/**
98
 * Given an internal Drupal path, return the alias set by the
administrator.
99
 *
100
 * @param $path
101
 *   An internal Drupal path.
102
 * @param $path_language
103
 *   An optional language code to look up the path in.
104
 *
105
 * @return
106
 *   An aliased path if one was found, or the original path if no alias was
107
 *   found.
108
 */
1092366
function drupal_get_path_alias($path, $path_language = '') {
1102317
  $result = $path;
1112317
  if ($alias = drupal_lookup_path('alias', $path, $path_language)) {
11220
    $result = $alias;
11320
  }
1142317
  return $result;
1150
}
116
117
/**
118
 * Given a path alias, return the internal path it represents.
119
 *
120
 * @param $path
121
 *   A Drupal path alias.
122
 * @param $path_language
123
 *   An optional language code to look up the path in.
124
 *
125
 * @return
126
 *   The internal path represented by the alias, or the original alias if
no
127
 *   internal path was found.
128
 */
1292366
function drupal_get_normal_path($path, $path_language = '') {
1302367
  $result = $path;
1312367
  if ($src = drupal_lookup_path('source', $path, $path_language)) {
1329
    $result = $src;
1339
  }
1342367
  if (function_exists('custom_url_rewrite_inbound')) {
135
    // Modules may alter the inbound request path by reference.
1360
    custom_url_rewrite_inbound($result, $path, $path_language);
1370
  }
1382367
  return $result;
1390
}
140
141
/**
142
 * Return a component of the current Drupal path.
143
 *
144
 * When viewing a page at the path "admin/build/types", for example, arg(0)
145
 * would return "admin", arg(1) would return "content", and arg(2) would
return
146
 * "types".
147
 *
148
 * Avoid use of this function where possible, as resulting code is hard to
read.
149
 * Instead, attempt to use named arguments in menu callback functions. See
the
150
 * explanation in menu.inc for how to construct callbacks that take
arguments.
151
 *
152
 * @param $index
153
 *   The index of the component, where each component is separated by a '/'
154
 *   (forward-slash), and where the first component has an index of 0
(zero).
155
 *
156
 * @return
157
 *   The component specified by $index, or NULL if the specified component
was
158
 *   not found.
159
 */
1602366
function arg($index = NULL, $path = NULL) {
1612366
  static $arguments;
162
1632366
  if (!isset($path)) {
1642366
    $path = $_GET['q'];
1652366
  }
1662366
  if (!isset($arguments[$path])) {
1672366
    $arguments[$path] = explode('/', $path);
1682366
  }
1692366
  if (!isset($index)) {
1702346
    return $arguments[$path];
1710
  }
1722366
  if (isset($arguments[$path][$index])) {
1732366
    return $arguments[$path][$index];
1740
  }
1751753
}
176
177
/**
178
 * Get the title of the current page, for display on the page and in the
title bar.
179
 *
180
 * @return
181
 *   The current page's title.
182
 */
1832366
function drupal_get_title() {
1841744
  $title = drupal_set_title();
185
186
  // during a bootstrap, menu.inc is not included and thus we cannot
provide a title
1871744
  if (!isset($title) && function_exists('menu_get_active_title')) {
188779
    $title = check_plain(menu_get_active_title());
189779
  }
190
1911744
  return $title;
1920
}
193
194
/**
195
 * Set the title of the current page, for display on the page and in the
title bar.
196
 *
197
 * @param $title
198
 *   Optional string value to assign to the page title; or if set to NULL
199
 *   (default), leaves the current title unchanged.
200
 * @param $output
201
 *   Optional flag - normally should be left as CHECK_PLAIN. Only set to
202
 *   PASS_THROUGH if you have already removed any possibly dangerous code
203
 *   from $title using a function like check_plain() or filter_xss(). With
this
204
 *   flag the string will be passed through unchanged.
205
 *
206
 * @return
207
 *   The updated title of the current page.
208
 */
2092366
function drupal_set_title($title = NULL, $output = CHECK_PLAIN) {
2101898
  static $stored_title;
211
2121898
  if (isset($title)) {
2131120
    $stored_title = ($output == PASS_THROUGH) ? $title :
check_plain($title);
2141120
  }
2151898
  return $stored_title;
2160
}
217
218
/**
219
 * Check if the current page is the front page.
220
 *
221
 * @return
222
 *   Boolean value: TRUE if the current page is the front page; FALSE if
otherwise.
223
 */
2242366
function drupal_is_front_page() {
225
  // As drupal_init_path updates $_GET['q'] with the 'site_frontpage' path,
226
  // we can check it against the 'site_frontpage' variable.
2271780
  return $_GET['q'] ==
drupal_get_normal_path(variable_get('site_frontpage', 'node'));
2280
}
229
230
/**
231
 * Check if a path matches any pattern in a set of patterns.
232
 *
233
 * @param $path
234
 *   The path to match.
235
 * @param $patterns
236
 *   String containing a set of patterns separated by \n, \r or \r\n.
237
 *
238
 * @return
239
 *   Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
240
 */
2412366
function drupal_match_path($path, $patterns) {
2420
  static $regexps;
243
2440
  if (!isset($regexps[$patterns])) {
2450
    $regexps[$patterns] = '/^(' . preg_replace(array('/(\r\n?|\n)/',
'/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1' .
preg_quote(variable_get('site_frontpage', 'node'), '/') . '\2'),
preg_quote($patterns, '/')) . ')$/';
2460
  }
2470
  return (bool)preg_match($regexps[$patterns], $path);
2480
}
2492366