You are here

function path_set_alias in Drupal 4

Same name and namespace in other branches
  1. 5 modules/path/path.module \path_set_alias()
  2. 6 modules/path/path.module \path_set_alias()

Set an aliased path for a given Drupal path, preventing duplicates.

2 calls to path_set_alias()
path_form_submit in modules/path.module
Save URL alias to the database.
path_nodeapi in modules/path.module
Implementation of hook_nodeapi().

File

modules/path.module, line 136
Enables users to rename URLs.

Code

function path_set_alias($path = NULL, $alias = NULL, $pid = NULL) {
  if ($path && !$alias) {
    db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
    drupal_clear_path_cache();
  }
  else {
    if (!$path && $alias) {
      db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
      drupal_clear_path_cache();
    }
    else {
      if ($path && $alias) {
        $path = urldecode($path);
        $path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s'", $path));
        $alias = urldecode($alias);
        $alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s'", $alias));

        // We have an insert:
        if ($path_count == 0 && $alias_count == 0) {
          db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
          drupal_clear_path_cache();
        }
        else {
          if ($path_count >= 1 && $alias_count == 0) {
            if ($pid) {
              db_query("UPDATE {url_alias} SET dst = '%s', src = '%s' WHERE pid = %d", $alias, $path, $pid);
            }
            else {
              db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
            }
            drupal_clear_path_cache();
          }
          else {
            if ($path_count == 0 && $alias_count == 1) {
              db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
              drupal_clear_path_cache();
            }
            else {
              if ($path_count == 1 && $alias_count == 1) {

                // This will delete the path that alias was originally pointing to:
                path_set_alias(NULL, $alias);
                path_set_alias($path);
                path_set_alias($path, $alias);
              }
            }
          }
        }
      }
    }
  }
}