[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/path/ -> path.module (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Enables users to rename URLs.
   6   */
   7  
   8  /**
   9   * Implementation of hook_help().
  10   */
  11  function path_help($path, $arg) {
  12    switch ($path) {
  13      case 'admin/help#path':
  14        $output = '<p>'. t('The path module allows you to specify aliases for Drupal URLs. Such aliases improve readability of URLs for your users and may help internet search engines to index your content more effectively. More than one alias may be created for a given page.') .'</p>';
  15        $output .= t('<p>Some examples of URL aliases are:</p>
  16  <ul>
  17  <li>user/login =&gt; login</li>
  18  <li>image/tid/16 =&gt; store</li>
  19  <li>taxonomy/term/7+19+20+21 =&gt; store/products/whirlygigs</li>
  20  <li>node/3 =&gt; contact</li>
  21  </ul>
  22  ');
  23        $output .= '<p>'. t('The path module enables appropriately permissioned users to specify an optional alias in all node input and editing forms, and provides an interface to view and edit all URL aliases. The two permissions related to URL aliasing are <em>administer url aliases</em> and <em>create url aliases</em>. ') .'</p>';
  24        $output .= '<p>'. t('This module also provides user-defined mass URL aliasing capabilities, which is useful if you wish to uniformly use URLs different from the default. For example, you may want to have your URLs presented in a different language. Access to the Drupal source code on the web server is required to set up mass URL aliasing. ') .'</p>';
  25        $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@path">Path module</a>.', array('@path' => 'http://drupal.org/handbook/modules/path/')) .'</p>';
  26        return $output;
  27      case 'admin/build/path':
  28        return '<p>'. t("Drupal provides complete control over URLs through aliasing, which is often used to make URLs more readable or easy to remember. For example, the alias 'about' may be mapped onto the post at the system path 'node/1', creating a more meaningful URL. Each system path can have multiple aliases.") .'</p>';
  29      case 'admin/build/path/add':
  30        return '<p>'. t('Enter the path you wish to create the alias for, followed by the name of the new alias.') .'</p>';
  31    }
  32  }
  33  
  34  /**
  35   * Implementation of hook_menu().
  36   */
  37  function path_menu() {
  38    $items['admin/build/path'] = array(
  39      'title' => 'URL aliases',
  40      'description' => "Change your site's URL paths by aliasing them.",
  41      'page callback' => 'path_admin_overview',
  42      'access arguments' => array('administer url aliases'),
  43      'file' => 'path.admin.inc',
  44    );
  45    $items['admin/build/path/edit'] = array(
  46      'title' => 'Edit alias',
  47      'page callback' => 'path_admin_edit',
  48      'access arguments' => array('administer url aliases'),
  49      'type' => MENU_CALLBACK,
  50      'file' => 'path.admin.inc',
  51    );
  52    $items['admin/build/path/delete'] = array(
  53      'title' => 'Delete alias',
  54      'page callback' => 'drupal_get_form',
  55      'page arguments' => array('path_admin_delete_confirm'),
  56      'access arguments' => array('administer url aliases'),
  57      'type' => MENU_CALLBACK,
  58      'file' => 'path.admin.inc',
  59    );
  60    $items['admin/build/path/list'] = array(
  61      'title' => 'List',
  62      'type' => MENU_DEFAULT_LOCAL_TASK,
  63      'weight' => -10,
  64    );
  65    $items['admin/build/path/add'] = array(
  66      'title' => 'Add alias',
  67      'page callback' => 'path_admin_edit',
  68      'access arguments' => array('administer url aliases'),
  69      'type' => MENU_LOCAL_TASK,
  70      'file' => 'path.admin.inc',
  71    );
  72  
  73    return $items;
  74  }
  75  
  76  /**
  77   * Post-confirmation; delete an URL alias.
  78   */
  79  function path_admin_delete($pid = 0) {
  80    db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
  81    drupal_set_message(t('The alias has been deleted.'));
  82  }
  83  
  84  /**
  85   * Set an aliased path for a given Drupal path, preventing duplicates.
  86   * 
  87   * @param $path
  88   *   Path URL. Set to NULL to delete alias.
  89   * @param $alias
  90   *   Alias URL. Set to NULL to delete alias.
  91   * @param $pid
  92   *   Path id to update. Set to NULL to create a new alias or to delete a group of aliases.
  93   * @param $language
  94   *   The language this alias is valid for.
  95   */
  96  function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') {
  97    /* This function claimed to prevent duplicate aliases but has not done
  98     * so since the end of 2007.
  99     * The uniqueness of dst+language pairs was enforced on the database level
 100     * until D6.16 (march 2010); trying to insert duplicate aliass would yield a
 101     * database error.
 102     * From D6.16 onwards, duplicates would silently be inserted, and
 103     * drupal_lookup_path() consistently uses those newer aliases.
 104     * While fixing an issue in D6.23, the behavior was reverted to preventing
 105     * duplicates by the below code. Watchdog errors are now logged instead.
 106     */
 107    $path = urldecode($path);
 108    $alias = urldecode($alias);
 109    // First we check if we deal with an existing alias and delete or modify it based on pid.
 110    if ($pid) {
 111      // An existing alias.
 112      if (!$path || !$alias) {
 113        // Delete the alias based on pid.
 114        db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
 115      }
 116      else {
 117        // Update the existing alias.
 118        // Check if the alias exists already.
 119        $existing = db_fetch_array(db_query("SELECT pid, src FROM {url_alias} WHERE dst = '%s' AND language = '%s' ORDER BY pid DESC", $alias, $language));
 120        if (!$existing || ($existing['pid'] == $pid && $existing['src'] != $path)) {
 121          db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid);
 122        }
 123        else {
 124          if ($existing['src'] != $path) {
 125            watchdog('path', "The alias for path '@path' (language '@lang') was not updated to '@alias', because the path '@expath' already has the same alias.",
 126              array('@path' => $path, '@lang' => $language, '@alias' => $alias, '@expath' => $existing['src']),
 127              WATCHDOG_ERROR);
 128          }
 129          // Don't clear cache if we didn't change anything
 130          return;
 131        }
 132      }
 133    }
 134    elseif ($path && $alias) {
 135      // Add this alias to the database, if it's new & doesn't cause conflicts.
 136      $existing = db_fetch_array(db_query("SELECT src, language, pid FROM {url_alias} WHERE dst = '%s' AND language IN('%s', '') ORDER BY language DESC, pid DESC", $alias, $language));
 137      if (!$existing || ($existing['language'] != $language && $existing['src'] != $path)) {
 138        // A new alias. Add it to the database.
 139        db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language);
 140      }
 141      elseif ($existing['language'] != $language) {
 142        // This alias already exists ONLY for 'language neutral': update language.
 143        // (We can only get here if $language != '')
 144        db_query("UPDATE {url_alias} SET language = '%s' WHERE pid = %d", $language, $existing['pid']);
 145      }
 146      else {
 147        if ($existing['src'] != $path) {
 148          watchdog('path', "The alias '@alias' for path '@path' (language '@lang') was not created, because the path '@expath' already has the same alias.",
 149            array('@path' => $path, '@lang' => $language, '@alias' => $alias, '@expath' => $existing['src']),
 150            WATCHDOG_ERROR);
 151        }
 152        // Don't clear cache if we didn't change anything
 153        return;
 154      }
 155    }
 156    else {
 157      // Delete the alias.
 158      if ($alias) {
 159        db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
 160      }
 161      else {
 162        db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
 163      }
 164    }
 165    drupal_clear_path_cache();
 166  }
 167  
 168  
 169  /**
 170   * Implementation of hook_nodeapi().
 171   *
 172   * Allows URL aliases for nodes to be specified at node edit time rather
 173   * than through the administrative interface.
 174   */
 175  function path_nodeapi(&$node, $op, $arg = NULL) {
 176    // Permissions are required for everything except node loading.
 177    if (user_access('create url aliases') || user_access('administer url aliases') || ($op == 'load')) {
 178      $language = isset($node->language) ? $node->language : '';
 179      switch ($op) {
 180        case 'validate':
 181          if (isset($node->path)) {
 182            $node->path = trim($node->path);
 183            if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s' AND language = '%s'", $node->path, "node/$node->nid", $language))) {
 184              form_set_error('path', t('The path is already in use.'));
 185            }
 186          }
 187          break;
 188  
 189        case 'load':
 190          $path = 'node/'. $node->nid;
 191          $alias = drupal_get_path_alias($path, $language);
 192          if ($path != $alias) {
 193            $node->path = $alias;
 194          }
 195          break;
 196  
 197        case 'insert':
 198          // Don't try to insert if path is NULL. We may have already set
 199          // the alias ahead of time.
 200          if (isset($node->path)) {
 201            path_set_alias('node/'. $node->nid, $node->path, NULL, $language);
 202          }
 203          break;
 204  
 205        case 'update':
 206          // $node->pid is usually only set when updating from a node edit form
 207          // (see path_form_alter). If it is not set (e.g. on most node_save()
 208          // commands), we cannot be sure whether a change in $node->path is meant
 209          // to replace an existing alias or add one extra, so we do the latter.
 210          path_set_alias('node/'. $node->nid, isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language);
 211          break;
 212  
 213        case 'delete':
 214          $path = 'node/'. $node->nid;
 215          if (drupal_get_path_alias($path) != $path) {
 216            path_set_alias($path);
 217          }
 218          break;
 219      }
 220    }
 221  }
 222  
 223  /**
 224   * Implementation of hook_form_alter().
 225   */
 226  function path_form_alter(&$form, $form_state, $form_id) {
 227    if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
 228      $path = isset($form['#node']->path) ? $form['#node']->path : NULL;
 229      $form['path'] = array(
 230        '#type' => 'fieldset',
 231        '#title' => t('URL path settings'),
 232        '#collapsible' => TRUE,
 233        '#collapsed' => empty($path),
 234        '#access' => user_access('create url aliases'),
 235        '#weight' => 30,
 236      );
 237      $form['path']['path'] = array(
 238        '#type' => 'textfield',
 239        '#default_value' => $path,
 240        '#maxlength' => 128,
 241        '#collapsible' => TRUE,
 242        '#collapsed' => TRUE,
 243        '#description' => t('Optionally specify an alternative URL by which this node can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'),
 244      );
 245      if ($path) {
 246        $form['path']['pid'] = array(
 247          '#type' => 'value',
 248          '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $path, $form['#node']->language))
 249        );
 250      }
 251    }
 252  }
 253  
 254  /**
 255   * Implementation of hook_perm().
 256   */
 257  function path_perm() {
 258    return array('create url aliases', 'administer url aliases');
 259  }
 260  
 261  /**
 262   * Fetch a specific URL alias from the database.
 263   */
 264  function path_load($pid) {
 265    return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
 266  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7