2003-09-30 17:03:29 +00:00
<?php
2005-08-11 13:02:08 +00:00
// $Id$
2003-09-30 17:03:29 +00:00
2004-08-21 06:42:38 +00:00
/**
* @file
* Enables users to rename URLs.
*/
2004-05-18 19:15:14 +00:00
/**
* Implementation of hook_help().
*/
function path_help($section) {
switch ($section) {
2004-06-18 15:04:37 +00:00
case 'admin/modules#description':
2005-04-01 15:55:02 +00:00
return t('Allows users to rename URLs.');
2004-05-18 19:15:14 +00:00
case 'admin/path':
2004-11-23 22:20:41 +00:00
return t("<p>Drupal provides users complete control over URLs through aliasing. This feature is typically used to make URLs human-readable or easy to remember. For example, one could map the relative URL 'node/1' onto 'about'. Each system path can have multiple aliases.</p>");
2004-05-18 19:15:14 +00:00
case 'admin/path/add':
2004-11-23 22:20:41 +00:00
return t('<p>Enter the path you wish to create the alias for, followed by the name of the new alias.</p>');
2004-05-18 19:15:14 +00:00
case 'admin/help#path':
2005-09-23 21:21:03 +00:00
return t('
2004-05-18 19:15:14 +00:00
<h3>Background</h3>
<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
2003-09-30 17:03:29 +00:00
2004-05-18 19:15:14 +00:00
image/tid/16 => store
2003-09-30 17:03:29 +00:00
- Patch #6760 by JonBob: refactored the taxonomy module URLs to be nicer, improved the code/Doxygen comments.
As discussed before, the path "taxonomy/page/or/1,2" becomes "taxonomy/term/1+2" and the path "taxonomy/page/and/1,2" becomes "taxonomy/term/1,2". The most common case of listing nodes attached to a single term becomes simpler, since it doesn't require a meaningless "or" or "and". A depth of "0" is assumed, but a positive integer or "all" can be used. Feeds are available at "taxonomy/term/1+2/all/feed" and the like.
This iteration of the patch also changes the structure of taxonomy_select_nodes(), since it was not following Drupal conventions. A handful of contrib modules call this function, and will need to be updated. Instead of passing in a $taxonomy object containing parameters for the function, the parameters are passed independently. This simplifies the code quite a bit. The queries were changed to only return node IDs for speed; all results from this function are passed through node_load() anyway, so the extra information returned was discarded. The AND query was also changed to avoid the strange trick and remove an extra query, at the expense of a table join per root term in the AND. This cleans up the code substantially while at the same time enabling the use of AND with a depth parameter.
TODO: update contribution modules.
2004-08-07 19:45:54 +00:00
taxonomy/term/7+19+20+21 => store/products/whirlygigs
2003-09-30 17:03:29 +00:00
2004-06-18 15:04:37 +00:00
node/3 => contact
2004-05-18 19:15:14 +00:00
</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>
2004-07-11 06:53:39 +00:00
<p>Aliases have a many to one relationship with their original Drupal URLs. In other words you can have many different aliases map to a single path. An example of where a multiple aliases come in handy is creating a standard RSS feed URL:</p>
<pre>
node/feed => rss.xml
node/feed => index.rdf
</pre>
<p>When Drupal generates links for a path with multiple aliases it will choose the first alias created per system URL. So in our above example, Drupal would use rss.xml as the default alias rather than index.rdf. To change this behavior, delete the aliases for node/feed and create the index.rdf alias before rss.xml.</p>
2003-09-30 17:03:29 +00:00
2004-05-18 19:15:14 +00:00
<h3>Permissions</h3>
<p>Two permissions are related to URL aliasing: <em>create url aliases</em> and <em>administer url aliases</em>.</p>
2004-10-23 13:47:11 +00:00
<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. 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>
2004-02-15 20:09:46 +00:00
2004-05-18 19:15:14 +00:00
<h3>Mass URL aliasing</h3>
2005-09-23 21:21:03 +00:00
<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. You need to have a working PHP programming knowledge to make use of this feature however. Read on in the <a href="%mass-alias-doc">Drupal Handbook about mass url aliasing.</a></p>', array("%mass-alias-doc" => "http://drupal.org/node/23708"));
2004-05-18 19:15:14 +00:00
}
}
2004-02-15 20:09:46 +00:00
2004-05-18 19:15:14 +00:00
/**
2004-06-18 15:04:37 +00:00
* Implementation of hook_menu().
2004-05-18 19:15:14 +00:00
*/
2004-09-16 07:17:56 +00:00
function path_menu($may_cache) {
2004-06-18 15:04:37 +00:00
$items = array();
2004-09-16 07:17:56 +00:00
if ($may_cache) {
$items[] = array('path' => 'admin/path', 'title' => t('url aliases'),
'callback' => 'path_admin',
'access' => user_access('administer url aliases'));
$items[] = array('path' => 'admin/path/edit', 'title' => t('edit alias'),
'callback' => 'path_admin_edit',
'access' => user_access('administer url aliases'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/path/delete', 'title' => t('delete alias'),
'callback' => 'path_admin_delete',
'access' => user_access('administer url aliases'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/path/list', 'title' => t('list'),
'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
2004-11-15 11:16:39 +00:00
$items[] = array('path' => 'admin/path/add', 'title' => t('add alias'),
2004-09-16 07:17:56 +00:00
'callback' => 'path_admin_edit',
'access' => user_access('administer url aliases'),
'type' => MENU_LOCAL_TASK);
}
2004-06-18 15:04:37 +00:00
return $items;
2004-05-18 19:15:14 +00:00
}
2004-02-15 20:09:46 +00:00
2004-05-18 19:15:14 +00:00
/**
* Menu callback; presents an overview of all URL aliases.
*/
function path_admin() {
2005-04-24 16:34:36 +00:00
return path_overview();
2004-05-18 19:15:14 +00:00
}
/**
* Menu callback; handles pages for creating and editing URL aliases.
*/
function path_admin_edit($pid = 0) {
2005-10-07 06:11:12 +00:00
if ($pid) {
2004-07-11 06:53:39 +00:00
$alias = path_load($pid);
2004-12-15 21:19:42 +00:00
drupal_set_title($alias['dst']);
2004-07-11 06:53:39 +00:00
$output = path_form(path_load($pid));
2003-09-30 17:03:29 +00:00
}
2004-05-18 19:15:14 +00:00
else {
2004-07-11 06:53:39 +00:00
$output = path_form();
2004-05-18 19:15:14 +00:00
}
2004-07-11 06:53:39 +00:00
2005-04-24 16:34:36 +00:00
return $output;
2004-05-18 19:15:14 +00:00
}
2004-02-15 20:09:46 +00:00
2004-05-18 19:15:14 +00:00
/**
* Menu callback; handles deletion of an URL alias.
*/
function path_admin_delete($pid = 0) {
2004-07-13 07:10:56 +00:00
db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
2004-08-16 18:02:48 +00:00
drupal_set_message(t('The alias has been deleted.'));
2004-07-13 07:10:56 +00:00
drupal_goto('admin/path');
2004-05-18 19:15:14 +00:00
}
/**
* Set an aliased path for a given Drupal path, preventing duplicates.
*/
2004-07-11 06:53:39 +00:00
function path_set_alias($path = NULL, $alias = NULL, $pid = NULL) {
2003-10-03 14:10:05 +00:00
if ($path && !$alias) {
2003-10-22 20:20:35 +00:00
db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
2005-05-14 09:23:47 +00:00
drupal_clear_path_cache();
2003-10-03 14:10:05 +00:00
}
else if (!$path && $alias) {
2003-10-22 20:20:35 +00:00
db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
2005-05-14 09:23:47 +00:00
drupal_clear_path_cache();
2003-10-03 14:10:05 +00:00
}
else if ($path && $alias) {
2003-10-22 20:20:35 +00:00
$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));
2003-10-03 14:10:05 +00:00
// We have an insert:
if ($path_count == 0 && $alias_count == 0) {
2003-11-27 13:24:58 +00:00
db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
2005-05-14 09:23:47 +00:00
drupal_clear_path_cache();
2003-10-03 14:10:05 +00:00
}
2004-07-11 06:53:39 +00:00
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);
}
2005-05-14 09:23:47 +00:00
drupal_clear_path_cache();
2003-10-03 14:10:05 +00:00
}
else if ($path_count == 0 && $alias_count == 1) {
2003-10-22 20:20:35 +00:00
db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
2005-05-14 09:23:47 +00:00
drupal_clear_path_cache();
2003-10-03 14:10:05 +00:00
}
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);
}
}
2003-09-30 17:03:29 +00:00
}
2004-05-18 19:15:14 +00:00
/**
* Return a form for editing or creating an individual URL alias.
*/
function path_form($edit = '') {
2003-09-30 17:03:29 +00:00
2005-10-11 19:44:35 +00:00
$form['src'] = array('#type' => 'textfield', '#title' => t('Existing system path'), '#default_value' => $edit['src'], '#size' => 60, '#maxlength' => 64, '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'));
$form['dst'] = array('#type' => 'textfield', '#default_value' => $edit['dst'], '#size' => 60, '#maxlength' => 64, '#description' => 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.'));
2003-09-30 17:03:29 +00:00
2004-05-18 19:15:14 +00:00
if ($edit['pid']) {
2005-10-11 19:44:35 +00:00
$form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
$form['submit'] = array('#type' => 'submit', '#value' => t('Update alias'));
2003-10-03 14:10:05 +00:00
}
else {
2005-10-11 19:44:35 +00:00
$form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias'));
2003-10-03 14:10:05 +00:00
}
2005-10-07 06:11:12 +00:00
return drupal_get_form('path_form', $form);
2003-09-30 17:03:29 +00:00
}
2004-04-21 13:56:38 +00:00
/**
2004-05-18 19:15:14 +00:00
* Implementation of hook_nodeapi().
*
* Allows URL aliases for nodes to be specified at node edit time rather
* than through the administrative interface.
2004-04-21 13:56:38 +00:00
*/
2003-10-17 16:00:44 +00:00
function path_nodeapi(&$node, $op, $arg) {
2004-05-18 19:15:14 +00:00
if (user_access('create url aliases') || user_access('administer url aliases')) {
2003-10-03 14:10:05 +00:00
switch ($op) {
2004-05-18 19:15:14 +00:00
case 'validate':
2004-09-16 14:32:51 +00:00
$node->path = trim($node->path);
if ($node->path && !valid_url($node->path)) {
form_set_error('path', t('The path is invalid.'));
2003-10-03 14:10:05 +00:00
}
2004-09-16 14:32:51 +00:00
else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/$node->nid"))) {
form_set_error('path', t('The path is already in use.'));
2003-10-03 14:10:05 +00:00
}
break;
2003-09-30 17:03:29 +00:00
2005-10-07 06:11:12 +00:00
case 'form':
2005-10-28 14:04:20 +00:00
$form['path'] = array('#type' => 'fieldset', '#title' => t('URL path settings'), '#collapsible' => TRUE, '#collapsed' => TRUE);
2005-11-01 10:11:36 +00:00
$form['path']['path'] = array('#type' => 'textfield', '#default_value' => $node->path, '#size' => 60, '#maxlength' => 250, '#collapsible' => TRUE, '#collapsed' => TRUE, '#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.'));
2004-07-11 06:53:39 +00:00
if ($node->path) {
2005-10-28 14:04:20 +00:00
$form['path']['pid'] = array('#type' => 'hidden', '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $node->path)));
2004-07-11 06:53:39 +00:00
}
2005-10-07 06:11:12 +00:00
return $form;
2003-10-03 14:10:05 +00:00
2004-09-16 14:32:51 +00:00
case 'load':
$path = "node/$node->nid";
$alias = drupal_get_path_alias($path);
if ($alias != $path) {
$node->path = $alias;
}
break;
2004-05-18 19:15:14 +00:00
case 'insert':
// Don't try to insert if path is NULL. We may have already set
// the alias ahead of time.
2003-10-17 16:00:44 +00:00
if ($node->path) {
2004-06-18 15:04:37 +00:00
path_set_alias("node/$node->nid", $node->path);
2003-10-17 16:00:44 +00:00
}
break;
2004-09-16 14:32:51 +00:00
2004-05-18 19:15:14 +00:00
case 'update':
2004-07-11 06:53:39 +00:00
path_set_alias("node/$node->nid", $node->path, $node->pid);
2003-10-03 14:10:05 +00:00
break;
2004-05-18 19:15:14 +00:00
case 'delete':
2004-06-18 15:04:37 +00:00
$path = "node/$node->nid";
2004-06-01 21:58:46 +00:00
if (drupal_get_path_alias($path) != $path) {
path_set_alias($path);
2003-10-03 14:10:05 +00:00
}
break;
}
2003-09-30 17:03:29 +00:00
}
}
2004-05-18 19:15:14 +00:00
/**
* Implementation of hook_perm().
*/
2003-09-30 17:03:29 +00:00
function path_perm() {
2004-05-18 19:15:14 +00:00
return array('create url aliases', 'administer url aliases');
2003-09-30 17:03:29 +00:00
}
2004-05-18 19:15:14 +00:00
/**
* Return a listing of all defined URL aliases.
*/
2003-09-30 17:03:29 +00:00
function path_overview() {
2004-05-18 19:15:14 +00:00
$sql = 'SELECT * FROM {url_alias}';
2003-09-30 17:03:29 +00:00
$header = array(
2004-08-19 15:41:57 +00:00
array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
array('data' => t('System'), 'field' => 'src'),
2004-11-15 11:16:39 +00:00
array('data' => t('Operations'), 'colspan' => '2')
2003-09-30 17:03:29 +00:00
);
$sql .= tablesort_sql($header);
$result = pager_query($sql, 50);
2005-02-01 19:45:58 +00:00
$destination = drupal_get_destination();
2003-09-30 17:03:29 +00:00
while ($data = db_fetch_object($result)) {
2005-02-10 19:30:08 +00:00
$rows[] = array($data->dst, $data->src, l(t('edit'), "admin/path/edit/$data->pid", array(), $destination), l(t('delete'), "admin/path/delete/$data->pid", array(), $destination));
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
if (!$rows) {
2004-05-18 19:15:14 +00:00
$rows[] = array(array('data' => t('No URL aliases available.'), 'colspan' => '4'));
2003-10-03 14:10:05 +00:00
}
2005-09-24 07:53:26 +00:00
$output = theme('table', $header, $rows);
$output .= theme('pager', NULL, 50, 0, tablesort_pager());
return $output;
2003-09-30 17:03:29 +00:00
}
2004-05-18 19:15:14 +00:00
/**
* Fetch a specific URL alias from the database.
*/
2003-10-03 14:10:05 +00:00
function path_load($pid) {
2004-05-18 19:15:14 +00:00
return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
2003-10-03 14:10:05 +00:00
}
2003-09-30 17:03:29 +00:00
2004-05-18 19:15:14 +00:00
/**
* Verify that a new URL alias is valid, and save it to the database.
*/
2005-10-07 06:11:12 +00:00
function path_form_execute() {
$edit = $GLOBALS['form_values'];
2004-05-18 19:15:14 +00:00
$src = $edit['src'];
$dst = $edit['dst'];
$pid = $edit['pid'];
2003-09-30 17:03:29 +00:00
2003-10-03 14:10:05 +00:00
if (!valid_url($src)) {
2005-03-31 09:25:33 +00:00
form_set_error('src', t('The system path %path is invalid.', array('%path' => theme('placeholder', $src))));
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
if (!valid_url($dst)) {
2005-03-31 09:25:33 +00:00
form_set_error('dst', t('The alias %alias is invalid.', array('%alias' => theme('placeholder', $dst))));
2003-09-30 17:03:29 +00:00
}
2004-05-18 19:15:14 +00:00
if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != %d AND dst = '%s'", $pid, $dst))) {
2005-03-31 09:25:33 +00:00
form_set_error('dst', t('The alias %alias is already in use.', array('%alias' => theme('placeholder', $dst))));
2003-10-03 14:10:05 +00:00
}
2003-09-30 17:03:29 +00:00
2004-07-04 16:50:02 +00:00
if (form_get_errors()) {
return path_form($edit);
2003-09-30 17:03:29 +00:00
}
2003-10-03 14:10:05 +00:00
else {
2004-07-11 06:53:39 +00:00
path_set_alias($src, $dst, $pid);
2003-09-30 17:03:29 +00:00
2004-08-16 18:02:48 +00:00
drupal_set_message(t('The alias has been saved.'));
2004-06-22 18:26:24 +00:00
drupal_goto('admin/path');
2004-05-31 11:51:21 +00:00
}
2003-09-30 17:03:29 +00:00
}
2005-08-25 21:14:17 +00:00