[ Index ]
 

Code source de Drupal 5.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/includes/ -> path.inc (source)

   1  <?php
   2  // $Id: path.inc,v 1.13 2006/12/23 22:04:52 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   */
  16  function drupal_init_path() {
  17    if (!empty($_GET['q'])) {
  18      $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
  19    }
  20    else {
  21      $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  22    }
  23  }
  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   *
  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 = '') {
  43    // $map keys are Drupal paths and the values are the corresponding aliases
  44    static $map = array(), $no_src = array();
  45    static $count;
  46  
  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  
  52    if ($action == 'wipe') {
  53      $map = array();
  54      $no_src = array();
  55    }
  56    elseif ($count > 0 && $path != '') {
  57      if ($action == 'alias') {
  58        if (isset($map[$path])) {
  59          return $map[$path];
  60        }
  61        $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path));
  62        $map[$path] = $alias;
  63        return $alias;
  64      }
  65      // Check $no_src for this $path in case we've already determined that there
  66      // isn't a path that has this alias
  67      elseif ($action == 'source' && !isset($no_src[$path])) {
  68        // Look for the value $path within the cached $map
  69        if (!$src = array_search($path, $map)) {
  70          if ($src = db_result(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $path))) {
  71            $map[$src] = $path;
  72          }
  73          else {
  74            // We can't record anything into $map because we do not have a valid
  75            // index and there is no need because we have not learned anything
  76            // about any Drupal path. Thus cache to $no_src.
  77            $no_src[$path] = TRUE;
  78          }
  79        }
  80        return $src;
  81      }
  82    }
  83  
  84    return FALSE;
  85  }
  86  
  87  /**
  88   * Given an internal Drupal path, return the alias set by the administrator.
  89   *
  90   * @param $path
  91   *   An internal Drupal path.
  92   *
  93   * @return
  94   *   An aliased path if one was found, or the original path if no alias was
  95   *   found.
  96   */
  97  function drupal_get_path_alias($path) {
  98    $result = $path;
  99    if ($alias = drupal_lookup_path('alias', $path)) {
 100      $result = $alias;
 101    }
 102    if (function_exists('custom_url_rewrite')) {
 103      $result = custom_url_rewrite('alias', $result, $path);
 104    }
 105    return $result;
 106  }
 107  
 108  /**
 109   * Given a path alias, return the internal path it represents.
 110   *
 111   * @param $path
 112   *   A Drupal path alias.
 113   *
 114   * @return
 115   *   The internal path represented by the alias, or the original alias if no
 116   *   internal path was found.
 117   */
 118  function drupal_get_normal_path($path) {
 119    $result = $path;
 120    if ($src = drupal_lookup_path('source', $path)) {
 121      $result = $src;
 122    }
 123    if (function_exists('custom_url_rewrite')) {
 124      $result = custom_url_rewrite('source', $result, $path);
 125    }
 126    return $result;
 127  }
 128  
 129  /**
 130   * Return a component of the current Drupal path.
 131   *
 132   * When viewing a page at the path "admin/content/types", for example, arg(0)
 133   * would return "admin", arg(1) would return "content", and arg(2) would return
 134   * "types".
 135   *
 136   * Avoid use of this function where possible, as resulting code is hard to read.
 137   * Instead, attempt to use named arguments in menu callback functions. See the
 138   * explanation in menu.inc for how to construct callbacks that take arguments.
 139   *
 140   * @param $index
 141   *   The index of the component, where each component is separated by a '/'
 142   *   (forward-slash), and where the first component has an index of 0 (zero).
 143   *
 144   * @return
 145   *   The component specified by $index, or FALSE if the specified component was
 146   *   not found.
 147   */
 148  function arg($index) {
 149    static $arguments, $q;
 150  
 151    if (empty($arguments) || $q != $_GET['q']) {
 152      $arguments = explode('/', $_GET['q']);
 153      $q = $_GET['q'];
 154    }
 155  
 156    if (isset($arguments[$index])) {
 157      return $arguments[$index];
 158    }
 159  }
 160  
 161  /**
 162   * Get the title of the current page, for display on the page and in the title bar.
 163   *
 164   * @return
 165   *   The current page's title.
 166   */
 167  function drupal_get_title() {
 168    $title = drupal_set_title();
 169  
 170    // during a bootstrap, menu.inc is not included and thus we cannot provide a title
 171    if (!isset($title) && function_exists('menu_get_active_title')) {
 172      $title = check_plain(menu_get_active_title());
 173    }
 174  
 175    return $title;
 176  }
 177  
 178  /**
 179   * Set the title of the current page, for display on the page and in the title bar.
 180   *
 181   * @param $title
 182   *   Optional string value to assign to the page title; or if set to NULL
 183   *   (default), leaves the current title unchanged.
 184   *
 185   * @return
 186   *   The updated title of the current page.
 187   */
 188  function drupal_set_title($title = NULL) {
 189    static $stored_title;
 190  
 191    if (isset($title)) {
 192      $stored_title = $title;
 193    }
 194    return $stored_title;
 195  }
 196  
 197  /**
 198   * Check if the current page is the front page.
 199   *
 200   * @return
 201   *   Boolean value: TRUE if the current page is the front page; FALSE if otherwise.
 202   */
 203  function drupal_is_front_page() {
 204    // As drupal_init_path updates $_GET['q'] with the 'site_frontpage' path,
 205    // we can check it against the 'site_frontpage' variable.
 206    return $_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node'));
 207  }


Généré le : Fri Nov 30 16:20:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics