path.module 11.6 KB
Newer Older
Dries's avatar
Dries committed
1 2 3 4
<?php
/* $Id$ */

function path_admin() {
Dries's avatar
 
Dries committed
5
  $op = $_POST["op"];
Dries's avatar
Dries committed
6 7
  $edit = $_POST["edit"];

Dries's avatar
 
Dries committed
8 9 10
  if (empty($op)) {
    $op = arg(2);
  }
Dries's avatar
Dries committed
11

Dries's avatar
 
Dries committed
12 13 14 15
  switch ($op) {
    case "add":
      $output = path_form();
      break;
Dries's avatar
Dries committed
16

Dries's avatar
 
Dries committed
17 18 19
    case "edit":
      $output = path_form(path_load(arg(3)));
      break;
Dries's avatar
Dries committed
20

Dries's avatar
 
Dries committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    case "help":
      $output = path_help();
      break;

    case "delete":
      path_delete(arg(3));
      $output .= path_overview();
      break;

    case t("Create new alias"):
    case t("Update alias"):
      $output = path_save($edit);
      break;

    default:
      $output .= path_overview();
Dries's avatar
Dries committed
37
  }
Dries's avatar
 
Dries committed
38 39

  print theme("page", $output);
Dries's avatar
Dries committed
40 41
}

Dries's avatar
 
Dries committed
42 43
function path_set_alias($path = NULL, $alias = NULL) {
  if ($path && !$alias) {
Dries's avatar
 
Dries committed
44
    db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
Dries's avatar
 
Dries committed
45 46 47
    drupal_rebuild_path_map();
  }
  else if (!$path && $alias) {
Dries's avatar
 
Dries committed
48
    db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
Dries's avatar
 
Dries committed
49 50 51
    drupal_rebuild_path_map();
  }
  else if ($path && $alias) {
Dries's avatar
 
Dries committed
52 53
    $path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s'", $path));
    $alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s'", $alias));
Dries's avatar
 
Dries committed
54 55 56

    // We have an insert:
    if ($path_count == 0 && $alias_count == 0) {
57
      db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
Dries's avatar
 
Dries committed
58 59 60
      drupal_rebuild_path_map();
    }
    else if ($path_count == 1 && $alias_count == 0) {
Dries's avatar
 
Dries committed
61
      db_query("UPDATE {url_alias} SET dst = '%s' WHERE src = '%s'", $alias, $path);
Dries's avatar
 
Dries committed
62 63 64
      drupal_rebuild_path_map();
    }
    else if ($path_count == 0 && $alias_count == 1) {
Dries's avatar
 
Dries committed
65
      db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
Dries's avatar
 
Dries committed
66 67 68 69 70 71 72 73 74
      drupal_rebuild_path_map();
    }
    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);
    }
  }
Dries's avatar
Dries committed
75 76
}

Dries's avatar
 
Dries committed
77
function path_form($edit = "", $error = "") {
Dries's avatar
Dries committed
78

Dries's avatar
 
Dries committed
79 80
  $form .= form_textfield(t("Existing path"), "src", $edit["src"], 50, 64, t("Specify the existing path you wish to alias. For example: node/view/28, forum/1, taxonomy/page/or/1,2."));
  $form .= form_textfield(t("New path alias"), "dst", $edit["dst"], 50, 64, t("Specify an alternative path by which this data 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."));
Dries's avatar
Dries committed
81

Dries's avatar
 
Dries committed
82 83 84 85 86 87 88 89 90
  if ($edit["pid"]) {
    $form .= form_hidden("pid", $edit["pid"]);
    $form .= form_submit(t("Update alias"));
  }
  else {
    $form .= form_submit(t("Create new alias"));
  }

  return form($form);
Dries's avatar
Dries committed
91 92
}

Dries's avatar
 
Dries committed
93
function path_help($section = "admin/help#path") {
Dries's avatar
 
Dries committed
94

Dries's avatar
Dries committed
95
  switch ($section) {
Dries's avatar
 
Dries committed
96 97
    case "admin/system/modules#description":
      $output = t("Enables users to create custom URLs.");
Dries's avatar
Dries committed
98
      break;
Dries's avatar
 
Dries committed
99
    case "admin/path":
Dries's avatar
 
Dries committed
100
      $output = t("Drupal provides users complete control over URLs through aliasing. While the original Drupal URLs are always created and accessible, advanced users have the option to override these normal paths.");
Dries's avatar
 
Dries committed
101 102
      break;
    case "admin/path/add":
Dries's avatar
 
Dries committed
103
      $output = t("Enter the path you wish to create the alias for, followed by the name of the new alias. Each path can be associated with only one alias.");
Dries's avatar
Dries committed
104
      break;
Dries's avatar
 
Dries committed
105
    case "admin/help#path":
Dries's avatar
 
Dries committed
106 107
      $output = t("
<h3>Background</h3>
Dries's avatar
 
Dries committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121
<p>A very powerful feature of Drupal is the ability to have control over all paths. The path module is the tool that provides this functionality and is part of the basic Drupal installation, although it is not enabled by default. Some examples of re-mapping paths are:</p>
<pre>
user/login => login

image/tid/16 => store

taxonomy/page/or/7,19,20,21 => store/products/whirlygigs

node/view/3 => contact
</pre>
<p>This functionality integrates seamlessly into node forms and also provides the administrator an interface to view all aliases that have been created.</p>
<p>Aliases have a 1 to 1 relationship with their original Drupal URLs. In other words you cannot have an alias map to more than one path. Likewise, a Drupal URL can't be mapped to more than one alias.</p>

<h3>Permissions</h3>
122 123
<p>Two permissions are related to URL aliasing: <em>create url aliases</em> and <em>administer url aliases</em>.</p>
<ol><li><strong>create url aliases</strong> - Allows users to create aliases for nodes. Enabling this permission will display a path field to the user in any node form, allowing them to enter an alias for that node. They will be able to edit/delete the alias after it is created using the same form.</li><li><strong>administer url aliases</strong> - Allows users to access the alias administration interface. They must also have the <em>access administration pages</em> permission set as well. This interface displays all aliases and provides a way to create and modify them. This is also the location to build aliases for things other than nodes. For example, you can create an alias for a taxonomy URL or even re-map the admin path (although the original admin path will still be accessible since aliases do not cancel out original paths).</li></ol>
Dries's avatar
 
Dries committed
124 125 126 127 128 129

<h3>Mass URL aliasing</h3>
<p>Drupal also comes with user defined mass URL aliasing capabilities. You might like to see completely different URLs used by Drupal, or even URLs translated to the visitors' native language, in which case this feature is handy. Only an administrator with access to the website source code can set up this kind of aliases. You can define a <code>conf_url_rewrite</code> function in conf.php, following this example:</p>
<pre>
function conf_url_rewrite(\$path, \$mode = 'incoming') {
  if (\$mode == 'incoming') { // URL coming from a client
Dries's avatar
 
Dries committed
130
    return preg_replace('!^display/(\\d+)\$!', 'node/view/\\1', \$path);
Dries's avatar
 
Dries committed
131 132
  }
  else { // URL going out to a client
Dries's avatar
 
Dries committed
133 134
    \$aliased = preg_replace('!^node/view/(\\d+)\$!', 'display/\\1', \$path);
    if (\$aliased != \$path) { return \$aliased; }
Dries's avatar
 
Dries committed
135 136 137 138
  }
}
</pre>
<p>This function will shorten every <code>node/view/\$node_id</code> type of URL to <code>display/\$node_id</code>. Individual URL aliases defined on the browser interface of Drupal take precedence, so if you have the 'contact' page alias from the example above, then the <code>display/3</code> alias will not be effective when outgoing links are created. Incoming URLs however always work with the mass URL aliased variant. Only the 'incoming' and 'outgoing' modes are supposed to be supported by your <code>conf_url_rewrite</code> function.</p>
Dries's avatar
 
Dries committed
139
<p>You cannot only use this feature to shorten the URLs, or to translate them to you own language, but also to add completely new subURLs to an already existing module's URL space, or to compose a bunch of existing stuff together to a common URL space. You can create a <code>news</code> section for example aliasing nodes and taxonomy overview pages falling under a 'news' vocabulary, thus having <code>news/15</code> and <code>news/sections/3</code> instead of <code>node/view/15</code> and <code>taxonomy/view/or/3</code>. You need extensive knowledge of Drupal's inner workings and regular expressions though to make such advanced aliases.</p>");
Dries's avatar
Dries committed
140 141 142
      break;
  }

Dries's avatar
 
Dries committed
143
  return $output;
Dries's avatar
Dries committed
144 145
}

Dries's avatar
 
Dries committed
146 147
function path_link($type, $node = NULL) {
  if ($type == "system" && user_access("administer url aliases")) {
Dries's avatar
 
Dries committed
148 149 150
    menu("admin/path", t("url aliasing"), "path_admin", 4);
    menu("admin/path/add", t("new alias"), "path_admin");
    menu("admin/path/help", t("help"), "path_admin", 9);
Dries's avatar
 
Dries committed
151
  }
Dries's avatar
 
Dries committed
152
}
Dries's avatar
Dries committed
153

Dries's avatar
 
Dries committed
154
function path_nodeapi(&$node, $op, $arg) {
Dries's avatar
 
Dries committed
155
  if (user_access("create url aliases") || user_access("administer url aliases")) {
Dries's avatar
Dries committed
156

Dries's avatar
 
Dries committed
157 158 159 160 161
    switch ($op) {
      case "validate":
        // is_null provides a mechanism for us to determine if this is the first
        // viewing of the form.  If it is the first time, load the alias, if it isn't
        // (i.e., user has clicked preview) let them work with their current form alias.
162 163
        if (is_null($node->path)) {
          $node->path = drupal_get_path_alias("node/view/$node->nid");
Dries's avatar
 
Dries committed
164 165
        }
        else {
166 167 168
          $node->path = trim($node->path);
          if ($node->path && !valid_url($node->path)) {
            $error["path"] = t("The path is invalid.");
Dries's avatar
 
Dries committed
169 170
            return $error;
          }
Dries's avatar
 
Dries committed
171
          else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/view/$node->nid"))) {
172
            $error["path"] = t("The path is already in use.");
Dries's avatar
 
Dries committed
173 174 175 176
            return $error;
          }
        }
        break;
Dries's avatar
Dries committed
177

Dries's avatar
 
Dries committed
178
      case "form pre":
179
        return form_textfield(t("Path alias"), "path", $node->path, 60, 250, 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.") . theme_error($arg["path"]));
Dries's avatar
 
Dries committed
180 181

      case "insert":
Dries's avatar
 
Dries committed
182 183 184 185 186 187 188 189
        /*
        ** Don't try to insert if path is NULL.  We may have already set
        ** the alias ahead of time.
        */
        if ($node->path) {
          path_set_alias("node/view/$node->nid", $node->path);
        }
        break;
Dries's avatar
 
Dries committed
190
      case "update":
191
        path_set_alias("node/view/$node->nid", $node->path);
Dries's avatar
 
Dries committed
192 193 194 195 196 197 198 199
        break;

      case "delete":
        if ($alias = drupal_get_path_alias("node/view/$node->nid")) {
          path_set_alias("node/view/$node->nid");
        }
        break;
    }
Dries's avatar
Dries committed
200 201 202 203
  }
}

function path_perm() {
Dries's avatar
 
Dries committed
204
  return array("create url aliases", "administer url aliases");
Dries's avatar
Dries committed
205 206 207
}

function path_overview() {
Dries's avatar
 
Dries committed
208
  $sql = "SELECT * FROM {url_alias}";
Dries's avatar
Dries committed
209
  $header = array(
210 211 212
    array("data" => t("alias"), "field" => "dst", "sort" => "asc"),
    array("data" => t("normal"), "field" => "src"),
    array("data" => t("operations"), "colspan" => 2)
Dries's avatar
Dries committed
213 214 215 216 217
  );
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);

  while ($data = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
218
    $rows[] = array($data->dst, $data->src, l(t("edit"), "admin/path/edit/$data->pid"), l(t("delete"), "admin/path/delete/$data->pid"));
Dries's avatar
Dries committed
219 220
  }

Dries's avatar
Dries committed
221
  if ($pager = theme("pager", NULL, 50, 0, tablesort_pager())) {
Dries's avatar
 
Dries committed
222
    $rows[] = array(array("data" => $pager, "colspan" => "4"));
Dries's avatar
Dries committed
223 224
  }

Dries's avatar
 
Dries committed
225
  if (!$rows) {
Dries's avatar
 
Dries committed
226
    $rows[] = array(array("data" => t("No URL aliases available."), "colspan" => "4"));
Dries's avatar
 
Dries committed
227 228
  }

Dries's avatar
 
Dries committed
229
  return theme("table", $header, $rows);
Dries's avatar
Dries committed
230 231
}

Dries's avatar
 
Dries committed
232
function path_load($pid) {
Dries's avatar
 
Dries committed
233
  return db_fetch_array(db_query("SELECT * FROM {url_alias} WHERE pid = '%d'", $pid));
Dries's avatar
 
Dries committed
234
}
Dries's avatar
Dries committed
235

Dries's avatar
 
Dries committed
236
function path_delete($pid) {
Dries's avatar
 
Dries committed
237
  db_query("DELETE FROM {url_alias} WHERE pid = '%d'", $pid);
Dries's avatar
 
Dries committed
238
  drupal_set_message(t("the alias has been deleted."));
Dries's avatar
 
Dries committed
239
}
Dries's avatar
Dries committed
240

Dries's avatar
 
Dries committed
241 242 243 244
function path_save($edit) {
  $src = $edit["src"];
  $dst = $edit["dst"];
  $pid = $edit["pid"];
Dries's avatar
Dries committed
245

Dries's avatar
 
Dries committed
246
  if (!valid_url($src)) {
Dries's avatar
 
Dries committed
247
    $error = t("the normal path '%src' is invalid.", array("%src" => $src));
Dries's avatar
Dries committed
248 249
  }

Dries's avatar
 
Dries committed
250
  if (db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE pid != '%d' AND src = '%s'", $pid, $src))) {
Dries's avatar
 
Dries committed
251
    $error = t("the normal path '%src' is already aliased.", array("%src" => $src));
Dries's avatar
Dries committed
252 253
  }

Dries's avatar
 
Dries committed
254
  if (!valid_url($dst)) {
Dries's avatar
 
Dries committed
255
    $error = t("the alias '%dst' is invalid.", array("%dst" => $dst));
Dries's avatar
Dries committed
256 257
  }

Dries's avatar
 
Dries committed
258
  if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != '%d' AND dst = '%s'", $pid, $dst))) {
Dries's avatar
 
Dries committed
259
    $error = t("the alias '%dst' is already in use.", array("%dst" => $dst));
Dries's avatar
 
Dries committed
260
  }
Dries's avatar
Dries committed
261

Dries's avatar
 
Dries committed
262
  if ($error) {
Dries's avatar
 
Dries committed
263
    drupal_set_message($error, 'error');
Dries's avatar
 
Dries committed
264
    return path_form($edit, $error);
Dries's avatar
Dries committed
265
  }
Dries's avatar
 
Dries committed
266 267 268 269 270 271 272 273
  else {
    /*
    ** Normally, you would use path_set_alias to update the paths table,
    ** but this is a special case.  We want to modify a specific row and the only
    ** way to do that is with pid.
    */

    if ($pid) {
Dries's avatar
 
Dries committed
274
      db_query("UPDATE {url_alias} SET src = '%s', dst = '%s' WHERE pid = '%d'", $src, $dst, $pid);
Dries's avatar
 
Dries committed
275 276 277 278
    }
    else {
      path_set_alias($src, $dst);
    }
Dries's avatar
Dries committed
279 280
  }

Dries's avatar
 
Dries committed
281 282
  drupal_set_message(t('the alias has been saved.'));
  return  path_overview();
Dries's avatar
Dries committed
283 284 285
}

?>