annotate includes/path.inc @ 4:d94886ac61a0

Added tag 6.1 for changeset 165d43f946a8
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:29:21 +0100
parents c1f4ac30525a
children 8b6c45761e01
rev   line source
webmaster@1 1 <?php
webmaster@1 2 // $Id: path.inc,v 1.19 2007/11/04 16:42:45 goba Exp $
webmaster@1 3
webmaster@1 4 /**
webmaster@1 5 * @file
webmaster@1 6 * Functions to handle paths in Drupal, including path aliasing.
webmaster@1 7 *
webmaster@1 8 * These functions are not loaded for cached pages, but modules that need
webmaster@1 9 * to use them in hook_init() or hook exit() can make them available, by
webmaster@1 10 * executing "drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);".
webmaster@1 11 */
webmaster@1 12
webmaster@1 13 /**
webmaster@1 14 * Initialize the $_GET['q'] variable to the proper normal path.
webmaster@1 15 */
webmaster@1 16 function drupal_init_path() {
webmaster@1 17 if (!empty($_GET['q'])) {
webmaster@1 18 $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
webmaster@1 19 }
webmaster@1 20 else {
webmaster@1 21 $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
webmaster@1 22 }
webmaster@1 23 }
webmaster@1 24
webmaster@1 25 /**
webmaster@1 26 * Given an alias, return its Drupal system URL if one exists. Given a Drupal
webmaster@1 27 * system URL return one of its aliases if such a one exists. Otherwise,
webmaster@1 28 * return FALSE.
webmaster@1 29 *
webmaster@1 30 * @param $action
webmaster@1 31 * One of the following values:
webmaster@1 32 * - wipe: delete the alias cache.
webmaster@1 33 * - alias: return an alias for a given Drupal system path (if one exists).
webmaster@1 34 * - source: return the Drupal system URL for a path alias (if one exists).
webmaster@1 35 * @param $path
webmaster@1 36 * The path to investigate for corresponding aliases or system URLs.
webmaster@1 37 * @param $path_language
webmaster@1 38 * Optional language code to search the path with. Defaults to the page language.
webmaster@1 39 * If there's no path defined for that language it will search paths without
webmaster@1 40 * language.
webmaster@1 41 *
webmaster@1 42 * @return
webmaster@1 43 * Either a Drupal system path, an aliased path, or FALSE if no path was
webmaster@1 44 * found.
webmaster@1 45 */
webmaster@1 46 function drupal_lookup_path($action, $path = '', $path_language = '') {
webmaster@1 47 global $language;
webmaster@1 48 // $map is an array with language keys, holding arrays of Drupal paths to alias relations
webmaster@1 49 static $map = array(), $no_src = array(), $count;
webmaster@1 50
webmaster@1 51 $path_language = $path_language ? $path_language : $language->language;
webmaster@1 52
webmaster@1 53 // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases
webmaster@1 54 if (!isset($count)) {
webmaster@1 55 $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}'));
webmaster@1 56 }
webmaster@1 57
webmaster@1 58 if ($action == 'wipe') {
webmaster@1 59 $map = array();
webmaster@1 60 $no_src = array();
webmaster@1 61 }
webmaster@1 62 elseif ($count > 0 && $path != '') {
webmaster@1 63 if ($action == 'alias') {
webmaster@1 64 if (isset($map[$path_language][$path])) {
webmaster@1 65 return $map[$path_language][$path];
webmaster@1 66 }
webmaster@1 67 // Get the most fitting result falling back with alias without language
webmaster@1 68 $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC", $path, $path_language));
webmaster@1 69 $map[$path_language][$path] = $alias;
webmaster@1 70 return $alias;
webmaster@1 71 }
webmaster@1 72 // Check $no_src for this $path in case we've already determined that there
webmaster@1 73 // isn't a path that has this alias
webmaster@1 74 elseif ($action == 'source' && !isset($no_src[$path_language][$path])) {
webmaster@1 75 // Look for the value $path within the cached $map
webmaster@1 76 $src = '';
webmaster@1 77 if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) {
webmaster@1 78 // Get the most fitting result falling back with alias without language
webmaster@1 79 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))) {
webmaster@1 80 $map[$path_language][$src] = $path;
webmaster@1 81 }
webmaster@1 82 else {
webmaster@1 83 // We can't record anything into $map because we do not have a valid
webmaster@1 84 // index and there is no need because we have not learned anything
webmaster@1 85 // about any Drupal path. Thus cache to $no_src.
webmaster@1 86 $no_src[$path_language][$path] = TRUE;
webmaster@1 87 }
webmaster@1 88 }
webmaster@1 89 return $src;
webmaster@1 90 }
webmaster@1 91 }
webmaster@1 92
webmaster@1 93 return FALSE;
webmaster@1 94 }
webmaster@1 95
webmaster@1 96 /**
webmaster@1 97 * Given an internal Drupal path, return the alias set by the administrator.
webmaster@1 98 *
webmaster@1 99 * @param $path
webmaster@1 100 * An internal Drupal path.
webmaster@1 101 * @param $path_language
webmaster@1 102 * An optional language code to look up the path in.
webmaster@1 103 *
webmaster@1 104 * @return
webmaster@1 105 * An aliased path if one was found, or the original path if no alias was
webmaster@1 106 * found.
webmaster@1 107 */
webmaster@1 108 function drupal_get_path_alias($path, $path_language = '') {
webmaster@1 109 $result = $path;
webmaster@1 110 if ($alias = drupal_lookup_path('alias', $path, $path_language)) {
webmaster@1 111 $result = $alias;
webmaster@1 112 }
webmaster@1 113 return $result;
webmaster@1 114 }
webmaster@1 115
webmaster@1 116 /**
webmaster@1 117 * Given a path alias, return the internal path it represents.
webmaster@1 118 *
webmaster@1 119 * @param $path
webmaster@1 120 * A Drupal path alias.
webmaster@1 121 * @param $path_language
webmaster@1 122 * An optional language code to look up the path in.
webmaster@1 123 *
webmaster@1 124 * @return
webmaster@1 125 * The internal path represented by the alias, or the original alias if no
webmaster@1 126 * internal path was found.
webmaster@1 127 */
webmaster@1 128 function drupal_get_normal_path($path, $path_language = '') {
webmaster@1 129 $result = $path;
webmaster@1 130 if ($src = drupal_lookup_path('source', $path, $path_language)) {
webmaster@1 131 $result = $src;
webmaster@1 132 }
webmaster@1 133 if (function_exists('custom_url_rewrite_inbound')) {
webmaster@1 134 // Modules may alter the inbound request path by reference.
webmaster@1 135 custom_url_rewrite_inbound($result, $path, $path_language);
webmaster@1 136 }
webmaster@1 137 return $result;
webmaster@1 138 }
webmaster@1 139
webmaster@1 140 /**
webmaster@1 141 * Return a component of the current Drupal path.
webmaster@1 142 *
webmaster@1 143 * When viewing a page at the path "admin/content/types", for example, arg(0)
webmaster@1 144 * would return "admin", arg(1) would return "content", and arg(2) would return
webmaster@1 145 * "types".
webmaster@1 146 *
webmaster@1 147 * Avoid use of this function where possible, as resulting code is hard to read.
webmaster@1 148 * Instead, attempt to use named arguments in menu callback functions. See the
webmaster@1 149 * explanation in menu.inc for how to construct callbacks that take arguments.
webmaster@1 150 *
webmaster@1 151 * @param $index
webmaster@1 152 * The index of the component, where each component is separated by a '/'
webmaster@1 153 * (forward-slash), and where the first component has an index of 0 (zero).
webmaster@1 154 *
webmaster@1 155 * @return
webmaster@1 156 * The component specified by $index, or NULL if the specified component was
webmaster@1 157 * not found.
webmaster@1 158 */
webmaster@1 159 function arg($index = NULL, $path = NULL) {
webmaster@1 160 static $arguments;
webmaster@1 161
webmaster@1 162 if (!isset($path)) {
webmaster@1 163 $path = $_GET['q'];
webmaster@1 164 }
webmaster@1 165 if (!isset($arguments[$path])) {
webmaster@1 166 $arguments[$path] = explode('/', $path);
webmaster@1 167 }
webmaster@1 168 if (!isset($index)) {
webmaster@1 169 return $arguments[$path];
webmaster@1 170 }
webmaster@1 171 if (isset($arguments[$path][$index])) {
webmaster@1 172 return $arguments[$path][$index];
webmaster@1 173 }
webmaster@1 174 }
webmaster@1 175
webmaster@1 176 /**
webmaster@1 177 * Get the title of the current page, for display on the page and in the title bar.
webmaster@1 178 *
webmaster@1 179 * @return
webmaster@1 180 * The current page's title.
webmaster@1 181 */
webmaster@1 182 function drupal_get_title() {
webmaster@1 183 $title = drupal_set_title();
webmaster@1 184
webmaster@1 185 // during a bootstrap, menu.inc is not included and thus we cannot provide a title
webmaster@1 186 if (!isset($title) && function_exists('menu_get_active_title')) {
webmaster@1 187 $title = check_plain(menu_get_active_title());
webmaster@1 188 }
webmaster@1 189
webmaster@1 190 return $title;
webmaster@1 191 }
webmaster@1 192
webmaster@1 193 /**
webmaster@1 194 * Set the title of the current page, for display on the page and in the title bar.
webmaster@1 195 *
webmaster@1 196 * @param $title
webmaster@1 197 * Optional string value to assign to the page title; or if set to NULL
webmaster@1 198 * (default), leaves the current title unchanged.
webmaster@1 199 *
webmaster@1 200 * @return
webmaster@1 201 * The updated title of the current page.
webmaster@1 202 */
webmaster@1 203 function drupal_set_title($title = NULL) {
webmaster@1 204 static $stored_title;
webmaster@1 205
webmaster@1 206 if (isset($title)) {
webmaster@1 207 $stored_title = $title;
webmaster@1 208 }
webmaster@1 209 return $stored_title;
webmaster@1 210 }
webmaster@1 211
webmaster@1 212 /**
webmaster@1 213 * Check if the current page is the front page.
webmaster@1 214 *
webmaster@1 215 * @return
webmaster@1 216 * Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
webmaster@1 217 */
webmaster@1 218 function drupal_is_front_page() {
webmaster@1 219 // As drupal_init_path updates $_GET['q'] with the 'site_frontpage' path,
webmaster@1 220 // we can check it against the 'site_frontpage' variable.
webmaster@1 221 return $_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node'));
webmaster@1 222 }
webmaster@1 223
webmaster@1 224 /**
webmaster@1 225 * Check if a path matches any pattern in a set of patterns.
webmaster@1 226 *
webmaster@1 227 * @param $path
webmaster@1 228 * The path to match.
webmaster@1 229 * @param $patterns
webmaster@1 230 * String containing a set of patterns separated by \n, \r or \r\n.
webmaster@1 231 *
webmaster@1 232 * @return
webmaster@1 233 * Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
webmaster@1 234 */
webmaster@1 235 function drupal_match_path($path, $patterns) {
webmaster@1 236 static $regexps;
webmaster@1 237
webmaster@1 238 if (!isset($regexps[$patterns])) {
webmaster@1 239 $regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/';
webmaster@1 240 }
webmaster@1 241 return preg_match($regexps[$patterns], $path);
webmaster@1 242 }