path.inc

  1. 7.x drupal-7.x/includes/path.inc
  2. 6.x drupal-6.x/includes/path.inc

Functions to handle paths in Drupal, including path aliasing.

These functions are not loaded for cached pages, but modules that need to use them in hook_init() or hook exit() can make them available, by executing "drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);".

File

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