Code coverage for /20080809/includes/path.inc

Line #Times calledCode
1
<?php
2
// $Id: path.inc,v 1.24 2008/06/24 22:12:15 dries 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
 */
162027
function drupal_init_path() {
172027
  if (!empty($_GET['q'])) {
181809
    $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
191809
  }
20
  else {
21218
    $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage',
'node'));
22
  }
232027
}
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
 */
462027
function drupal_lookup_path($action, $path = '', $path_language = '') {
472081
  global $language;
48
  // $map is an array with language keys, holding arrays of Drupal paths to
alias relations
492081
  static $map = array(), $no_src = array(), $count;
50
512081
  $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
542081
  if (!isset($count)) {
552027
    $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));
562027
  }
57
582081
  if ($action == 'wipe') {
599
    $map = array();
609
    $no_src = array();
619
  }
622081
  elseif ($count > 0 && $path != '') {
6328
    if ($action == 'alias') {
6428
      if (isset($map[$path_language][$path])) {
6523
        return $map[$path_language][$path];
660
      }
67
      // Get the most fitting result falling back with alias without
language
6828
      $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src =
'%s' AND language IN('%s', '') ORDER BY language DESC", $path,
$path_language));
6928
      $map[$path_language][$path] = $alias;
7028
      return $alias;
710
    }
72
    // Check $no_src for this $path in case we've already determined that
there
73
    // isn't a path that has this alias
7428
    elseif ($action == 'source' && !isset($no_src[$path_language][$path]))
{
75
      // Look for the value $path within the cached $map
7628
      $src = '';
7728
      if (!isset($map[$path_language]) || !($src = array_search($path,
$map[$path_language]))) {
78
        // Get the most fitting result falling back with alias without
language
7928
        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))) {
805
          $map[$path_language][$src] = $path;
815
        }
82
        else {
83
          // We can't record anything into $map because we do not have a
valid
84
          // index and there is no need because we have not learned
anything
85
          // about any Drupal path. Thus cache to $no_src.
8628
          $no_src[$path_language][$path] = TRUE;
87
        }
8828
      }
8928
      return $src;
900
    }
9122
  }
92
932080
  return FALSE;
940
}
95
96
/**
97
 * Given an internal Drupal path, return the alias set by the
administrator.
98
 *
99
 * @param $path
100
 *   An internal Drupal path.
101
 * @param $path_language
102
 *   An optional language code to look up the path in.
103
 *
104
 * @return
105
 *   An aliased path if one was found, or the original path if no alias
was
106
 *   found.
107
 */
1082027
function drupal_get_path_alias($path, $path_language = '') {
1091976
  $result = $path;
1101976
  if ($alias = drupal_lookup_path('alias', $path, $path_language)) {
11111
    $result = $alias;
11211
  }
1131976
  return $result;
1140
}
115
116
/**
117
 * Given a path alias, return the internal path it represents.
118
 *
119
 * @param $path
120
 *   A Drupal path alias.
121
 * @param $path_language
122
 *   An optional language code to look up the path in.
123
 *
124
 * @return
125
 *   The internal path represented by the alias, or the original alias if
no
126
 *   internal path was found.
127
 */
1282027
function drupal_get_normal_path($path, $path_language = '') {
1292027
  $result = $path;
1302027
  if ($src = drupal_lookup_path('source', $path, $path_language)) {
1315
    $result = $src;
1325
  }
1332027
  if (function_exists('custom_url_rewrite_inbound')) {
134
    // Modules may alter the inbound request path by reference.
1350
    custom_url_rewrite_inbound($result, $path, $path_language);
1360
  }
1372027
  return $result;
1380
}
139
140
/**
141
 * Return a component of the current Drupal path.
142
 *
143
 * When viewing a page at the path "admin/build/types", for example,
arg(0)
144
 * would return "admin", arg(1) would return "content", and arg(2) would
return
145
 * "types".
146
 *
147
 * Avoid use of this function where possible, as resulting code is hard to
read.
148
 * Instead, attempt to use named arguments in menu callback functions. See
the
149
 * explanation in menu.inc for how to construct callbacks that take
arguments.
150
 *
151
 * @param $index
152
 *   The index of the component, where each component is separated by a
'/'
153
 *   (forward-slash), and where the first component has an index of 0
(zero).
154
 *
155
 * @return
156
 *   The component specified by $index, or NULL if the specified component
was
157
 *   not found.
158
 */
1592027
function arg($index = NULL, $path = NULL) {
1602027
  static $arguments;
161
1622027
  if (!isset($path)) {
1632027
    $path = $_GET['q'];
1642027
  }
1652027
  if (!isset($arguments[$path])) {
1662027
    $arguments[$path] = explode('/', $path);
1672027
  }
1682027
  if (!isset($index)) {
1692010
    return $arguments[$path];
1700
  }
1712027
  if (isset($arguments[$path][$index])) {
1722027
    return $arguments[$path][$index];
1730
  }
1741502
}
175
176
/**
177
 * Get the title of the current page, for display on the page and in the
title bar.
178
 *
179
 * @return
180
 *   The current page's title.
181
 */
1822027
function drupal_get_title() {
1831492
  $title = drupal_set_title();
184
185
  // during a bootstrap, menu.inc is not included and thus we cannot
provide a title
1861492
  if (!isset($title) && function_exists('menu_get_active_title')) {
187645
    $title = check_plain(menu_get_active_title());
188645
  }
189
1901492
  return $title;
1910
}
192
193
/**
194
 * Set the title of the current page, for display on the page and in the
title bar.
195
 *
196
 * @param $title
197
 *   Optional string value to assign to the page title; or if set to NULL
198
 *   (default), leaves the current title unchanged.
199
 *
200
 * @return
201
 *   The updated title of the current page.
202
 */
2032027
function drupal_set_title($title = NULL) {
2041638
  static $stored_title;
205
2061638
  if (isset($title)) {
207993
    $stored_title = $title;
208993
  }
2091638
  return $stored_title;
2100
}
211
212
/**
213
 * Check if the current page is the front page.
214
 *
215
 * @return
216
 *   Boolean value: TRUE if the current page is the front page; FALSE if
otherwise.
217
 */
2182027
function drupal_is_front_page() {
219
  // As drupal_init_path updates $_GET['q'] with the 'site_frontpage'
path,
220
  // we can check it against the 'site_frontpage' variable.
2211524
  return $_GET['q'] ==
drupal_get_normal_path(variable_get('site_frontpage', 'node'));
2220
}
223
224
/**
225
 * Check if a path matches any pattern in a set of patterns.
226
 *
227
 * @param $path
228
 *   The path to match.
229
 * @param $patterns
230
 *   String containing a set of patterns separated by \n, \r or \r\n.
231
 *
232
 * @return
233
 *   Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
234
 */
2352027
function drupal_match_path($path, $patterns) {
2360
  static $regexps;
237
2380
  if (!isset($regexps[$patterns])) {
2390
    $regexps[$patterns] = '/^(' . preg_replace(array('/(\r\n?|\n)/',
'/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1' .
preg_quote(variable_get('site_frontpage', 'node'), '/') . '\2'),
preg_quote($patterns, '/')) . ')$/';
2400
  }
2410
  return (bool)preg_match($regexps[$patterns], $path);
2420
}
2432027