annotate includes/path.inc @ 20:e3d20ebd63d1 tip

Added tag 6.9 for changeset 3edae6ecd6c6
author Franck Deroche <franck@defr.org>
date Thu, 15 Jan 2009 10:16:10 +0100
parents 8b6c45761e01
children
rev   line source
webmaster@1 1 <?php
webmaster@13 2 // $Id: path.inc,v 1.19.2.1 2008/10/13 21:06:41 dries 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@13 61 $count = NULL;
webmaster@1 62 }
webmaster@1 63 elseif ($count > 0 && $path != '') {
webmaster@1 64 if ($action == 'alias') {
webmaster@1 65 if (isset($map[$path_language][$path])) {
webmaster@1 66 return $map[$path_language][$path];
webmaster@1 67 }
webmaster@1 68 // Get the most fitting result falling back with alias without language
webmaster@1 69 $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 70 $map[$path_language][$path] = $alias;
webmaster@1 71 return $alias;
webmaster@1 72 }
webmaster@1 73 // Check $no_src for this $path in case we've already determined that there
webmaster@1 74 // isn't a path that has this alias
webmaster@1 75 elseif ($action == 'source' && !isset($no_src[$path_language][$path])) {
webmaster@1 76 // Look for the value $path within the cached $map
webmaster@1 77 $src = '';
webmaster@1 78 if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) {
webmaster@1 79 // Get the most fitting result falling back with alias without language
webmaster@1 80 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 81 $map[$path_language][$src] = $path;
webmaster@1 82 }
webmaster@1 83 else {
webmaster@1 84 // We can't record anything into $map because we do not have a valid
webmaster@1 85 // index and there is no need because we have not learned anything
webmaster@1 86 // about any Drupal path. Thus cache to $no_src.
webmaster@1 87 $no_src[$path_language][$path] = TRUE;
webmaster@1 88 }
webmaster@1 89 }
webmaster@1 90 return $src;
webmaster@1 91 }
webmaster@1 92 }
webmaster@1 93
webmaster@1 94 return FALSE;
webmaster@1 95 }
webmaster@1 96
webmaster@1 97 /**
webmaster@1 98 * Given an internal Drupal path, return the alias set by the administrator.
webmaster@1 99 *
webmaster@1 100 * @param $path
webmaster@1 101 * An internal Drupal path.
webmaster@1 102 * @param $path_language
webmaster@1 103 * An optional language code to look up the path in.
webmaster@1 104 *
webmaster@1 105 * @return
webmaster@1 106 * An aliased path if one was found, or the original path if no alias was
webmaster@1 107 * found.
webmaster@1 108 */
webmaster@1 109 function drupal_get_path_alias($path, $path_language = '') {
webmaster@1 110 $result = $path;
webmaster@1 111 if ($alias = drupal_lookup_path('alias', $path, $path_language)) {
webmaster@1 112 $result = $alias;
webmaster@1 113 }
webmaster@1 114 return $result;
webmaster@1 115 }
webmaster@1 116
webmaster@1 117 /**
webmaster@1 118 * Given a path alias, return the internal path it represents.
webmaster@1 119 *
webmaster@1 120 * @param $path
webmaster@1 121 * A Drupal path alias.
webmaster@1 122 * @param $path_language
webmaster@1 123 * An optional language code to look up the path in.
webmaster@1 124 *
webmaster@1 125 * @return
webmaster@1 126 * The internal path represented by the alias, or the original alias if no
webmaster@1 127 * internal path was found.
webmaster@1 128 */
webmaster@1 129 function drupal_get_normal_path($path, $path_language = '') {
webmaster@1 130 $result = $path;
webmaster@1 131 if ($src = drupal_lookup_path('source', $path, $path_language)) {
webmaster@1 132 $result = $src;
webmaster@1 133 }
webmaster@1 134 if (function_exists('custom_url_rewrite_inbound')) {
webmaster@1 135 // Modules may alter the inbound request path by reference.
webmaster@1 136 custom_url_rewrite_inbound($result, $path, $path_language);
webmaster@1 137 }
webmaster@1 138 return $result;
webmaster@1 139 }
webmaster@1 140
webmaster@1 141 /**
webmaster@1 142 * Return a component of the current Drupal path.
webmaster@1 143 *
webmaster@1 144 * When viewing a page at the path "admin/content/types", for example, arg(0)
webmaster@1 145 * would return "admin", arg(1) would return "content", and arg(2) would return
webmaster@1 146 * "types".
webmaster@1 147 *
webmaster@1 148 * Avoid use of this function where possible, as resulting code is hard to read.
webmaster@1 149 * Instead, attempt to use named arguments in menu callback functions. See the
webmaster@1 150 * explanation in menu.inc for how to construct callbacks that take arguments.
webmaster@1 151 *
webmaster@1 152 * @param $index
webmaster@1 153 * The index of the component, where each component is separated by a '/'
webmaster@1 154 * (forward-slash), and where the first component has an index of 0 (zero).
webmaster@1 155 *
webmaster@1 156 * @return
webmaster@1 157 * The component specified by $index, or NULL if the specified component was
webmaster@1 158 * not found.
webmaster@1 159 */
webmaster@1 160 function arg($index = NULL, $path = NULL) {
webmaster@1 161 static $arguments;
webmaster@1 162
webmaster@1 163 if (!isset($path)) {
webmaster@1 164 $path = $_GET['q'];
webmaster@1 165 }
webmaster@1 166 if (!isset($arguments[$path])) {
webmaster@1 167 $arguments[$path] = explode('/', $path);
webmaster@1 168 }
webmaster@1 169 if (!isset($index)) {
webmaster@1 170 return $arguments[$path];
webmaster@1 171 }
webmaster@1 172 if (isset($arguments[$path][$index])) {
webmaster@1 173 return $arguments[$path][$index];
webmaster@1 174 }
webmaster@1 175 }
webmaster@1 176
webmaster@1 177 /**
webmaster@1 178 * Get the title of the current page, for display on the page and in the title bar.
webmaster@1 179 *
webmaster@1 180 * @return
webmaster@1 181 * The current page's title.
webmaster@1 182 */
webmaster@1 183 function drupal_get_title() {
webmaster@1 184 $title = drupal_set_title();
webmaster@1 185
webmaster@1 186 // during a bootstrap, menu.inc is not included and thus we cannot provide a title
webmaster@1 187 if (!isset($title) && function_exists('menu_get_active_title')) {
webmaster@1 188 $title = check_plain(menu_get_active_title());
webmaster@1 189 }
webmaster@1 190
webmaster@1 191 return $title;
webmaster@1 192 }
webmaster@1 193
webmaster@1 194 /**
webmaster@1 195 * Set the title of the current page, for display on the page and in the title bar.
webmaster@1 196 *
webmaster@1 197 * @param $title
webmaster@1 198 * Optional string value to assign to the page title; or if set to NULL
webmaster@1 199 * (default), leaves the current title unchanged.
webmaster@1 200 *
webmaster@1 201 * @return
webmaster@1 202 * The updated title of the current page.
webmaster@1 203 */
webmaster@1 204 function drupal_set_title($title = NULL) {
webmaster@1 205 static $stored_title;
webmaster@1 206
webmaster@1 207 if (isset($title)) {
webmaster@1 208 $stored_title = $title;
webmaster@1 209 }
webmaster@1 210 return $stored_title;
webmaster@1 211 }
webmaster@1 212
webmaster@1 213 /**
webmaster@1 214 * Check if the current page is the front page.
webmaster@1 215 *
webmaster@1 216 * @return
webmaster@1 217 * Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
webmaster@1 218 */
webmaster@1 219 function drupal_is_front_page() {
webmaster@1 220 // As drupal_init_path updates $_GET['q'] with the 'site_frontpage' path,
webmaster@1 221 // we can check it against the 'site_frontpage' variable.
webmaster@1 222 return $_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node'));
webmaster@1 223 }
webmaster@1 224
webmaster@1 225 /**
webmaster@1 226 * Check if a path matches any pattern in a set of patterns.
webmaster@1 227 *
webmaster@1 228 * @param $path
webmaster@1 229 * The path to match.
webmaster@1 230 * @param $patterns
webmaster@1 231 * String containing a set of patterns separated by \n, \r or \r\n.
webmaster@1 232 *
webmaster@1 233 * @return
webmaster@1 234 * Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
webmaster@1 235 */
webmaster@1 236 function drupal_match_path($path, $patterns) {
webmaster@1 237 static $regexps;
webmaster@1 238
webmaster@1 239 if (!isset($regexps[$patterns])) {
webmaster@1 240 $regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/';
webmaster@1 241 }
webmaster@1 242 return preg_match($regexps[$patterns], $path);
webmaster@1 243 }