2 // $Id: path.module 144 2007-03-28 07:52:20Z thierry $
6 * Enables users to rename URLs.
10 * Implementation of hook_help().
12 function path_help($section) {
14 case 'admin/help#path':
15 $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>';
16 $output .= t('<p>Some examples of URL aliases are:</p>
18 <li>user/login => login</li>
19 <li>image/tid/16 => store</li>
20 <li>taxonomy/term/7+19+20+21 => store/products/whirlygigs</li>
21 <li>node/3 => contact</li>
24 $output .= '<p>'. t('The path module enables an extra field for aliases in all node input and editing forms (when users have the appropriate permissions). It also provides an interface to view and edit all URL aliases. The two permissions are related to URL aliasing are "administer a list of URL aliases" and "add url aliases". ') .'</p>';
25 $output .= '<p>'. t('This module also comes with 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 these kinds of aliases. ') .'</p>';
26 $output .= t('<p>You can</p>
28 <li>set the path for a post with the path module.</li>
29 <li>add a URL alias: <a href="%admin-path-add">administer >> url aliases >> add alias</a>.</li>
30 <li>administer the list of URL aliases: <a href="%admin-path">administer >> url aliases</a>.</li>
31 <li>read how to <a href="%external-http-drupal-org-node-15365">configure clean URLs</a> for your webserver.
32 <li>enable clean url\'s to remove the =? at <a href="%admin-settings">administer >> settings</a>.</li>
34 ', array('%admin-path-add' => url('admin/path/add'), '%admin-path' => url('admin/path'), '%external-http-drupal-org-node-15365' => 'http://drupal.org/node/15365', '%admin-settings' => url('admin/settings')));
35 $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%path">Path page</a>.', array('%path' => 'http://drupal.org/handbook/modules/path/')) .'</p>';
37 case 'admin/modules#description':
38 return t('Allows users to rename URLs.');
40 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>");
41 case 'admin/path/add':
42 return t('<p>Enter the path you wish to create the alias for, followed by the name of the new alias.</p>');
47 * Implementation of hook_menu().
49 function path_menu($may_cache) {
53 $items[] = array('path' => 'admin/path', 'title' => t('url aliases'),
54 'callback' => 'path_admin',
55 'access' => user_access('administer url aliases'));
56 $items[] = array('path' => 'admin/path/edit', 'title' => t('edit alias'),
57 'callback' => 'path_admin_edit',
58 'access' => user_access('administer url aliases'),
59 'type' => MENU_CALLBACK);
60 $items[] = array('path' => 'admin/path/delete', 'title' => t('delete alias'),
61 'callback' => 'path_admin_delete_confirm',
62 'access' => user_access('administer url aliases'),
63 'type' => MENU_CALLBACK);
64 $items[] = array('path' => 'admin/path/list', 'title' => t('list'),
65 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
66 $items[] = array('path' => 'admin/path/add', 'title' => t('add alias'),
67 'callback' => 'path_admin_edit',
68 'access' => user_access('administer url aliases'),
69 'type' => MENU_LOCAL_TASK);
76 * Menu callback; presents an overview of all URL aliases.
78 function path_admin() {
79 return path_overview();
83 * Menu callback; handles pages for creating and editing URL aliases.
85 function path_admin_edit($pid = 0) {
87 $alias = path_load($pid);
88 drupal_set_title($alias['dst']);
89 $output = path_form(path_load($pid));
92 $output = path_form();
99 * Menu callback; confirms deleting an URL alias
101 function path_admin_delete_confirm($pid) {
102 $path = path_load($pid);
103 if (user_access('administer url aliases')) {
104 $form['pid'] = array('#type' => 'value', '#value' => $pid);
105 $output = confirm_form('path_admin_delete_confirm', $form,
106 t('Are you sure you want to delete path alias %title?', array('%title' => theme('placeholder', $path['dst']))),
107 $_GET['destination'] ? $_GET['destination'] : 'admin/path', t('This action cannot be undone.'),
108 t('Delete'), t('Cancel') );
115 * Execute URL alias deletion
117 function path_admin_delete_confirm_submit($form_id, $form_values) {
118 if ($form_values['confirm']) {
119 path_admin_delete($form_values['pid']);
125 * Post-confirmation; delete an URL alias.
127 function path_admin_delete($pid = 0) {
128 db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid);
129 drupal_set_message(t('The alias has been deleted.'));
135 * Set an aliased path for a given Drupal path, preventing duplicates.
137 function path_set_alias($path = NULL, $alias = NULL, $pid = NULL) {
138 if ($path && !$alias) {
139 db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
140 drupal_clear_path_cache();
142 else if (!$path && $alias) {
143 db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
144 drupal_clear_path_cache();
146 else if ($path && $alias) {
147 $path = urldecode($path);
148 $path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s'", $path));
149 $alias = urldecode($alias);
150 $alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s'", $alias));
152 // We have an insert:
153 if ($path_count == 0 && $alias_count == 0) {
154 db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
155 drupal_clear_path_cache();
157 else if ($path_count >= 1 && $alias_count == 0) {
159 db_query("UPDATE {url_alias} SET dst = '%s', src = '%s' WHERE pid = %d", $alias, $path, $pid);
162 db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
164 drupal_clear_path_cache();
166 else if ($path_count == 0 && $alias_count == 1) {
167 db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
168 drupal_clear_path_cache();
170 else if ($path_count == 1 && $alias_count == 1) {
171 // This will delete the path that alias was originally pointing to:
172 path_set_alias(NULL, $alias);
173 path_set_alias($path);
174 path_set_alias($path, $alias);
180 * Return a form for editing or creating an individual URL alias.
182 function path_form($edit = '') {
184 $form['src'] = array('#type' => 'textfield', '#title' => t('Existing system path'), '#default_value' => $edit['src'], '#maxlength' => 64, '#description' => t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.'));
185 $form['dst'] = array('#type' => 'textfield', '#default_value' => $edit['dst'], '#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.'));
188 $form['pid'] = array('#type' => 'hidden', '#value' => $edit['pid']);
189 $form['submit'] = array('#type' => 'submit', '#value' => t('Update alias'));
192 $form['submit'] = array('#type' => 'submit', '#value' => t('Create new alias'));
195 return drupal_get_form('path_form', $form);
199 * Implementation of hook_nodeapi().
201 * Allows URL aliases for nodes to be specified at node edit time rather
202 * than through the administrative interface.
204 function path_nodeapi(&$node, $op, $arg) {
205 if (user_access('create url aliases') || user_access('administer url aliases')) {
208 $node->path = trim($node->path);
209 if ($node->path && !valid_url($node->path)) {
210 form_set_error('path', t('The path is invalid.'));
212 else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/$node->nid"))) {
213 form_set_error('path', t('The path is already in use.'));
218 $path = "node/$node->nid";
219 // We don't use drupal_get_path_alias() to avoid custom rewrite functions.
220 // We only care about exact aliases.
221 $result = db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", $path);
222 if (db_num_rows($result)) {
223 $node->path = db_result($result);
228 // Don't try to insert if path is NULL. We may have already set
229 // the alias ahead of time.
231 path_set_alias("node/$node->nid", $node->path);
236 path_set_alias("node/$node->nid", $node->path, $node->pid);
240 $path = "node/$node->nid";
241 if (drupal_get_path_alias($path) != $path) {
242 path_set_alias($path);
250 * Implementation of hook_form_alter().
252 function path_form_alter($form_id, &$form) {
253 if (user_access('create url aliases') && isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
254 $path = $form['#node']->path;
255 $form['path'] = array(
256 '#type' => 'fieldset',
257 '#title' => t('URL path settings'),
258 '#collapsible' => TRUE,
259 '#collapsed' => empty($path),
262 $form['path']['path'] = array(
263 '#type' => 'textfield',
264 '#default_value' => $path,
266 '#collapsible' => TRUE,
267 '#collapsed' => TRUE,
268 '#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.'),
271 $form['path']['pid'] = array(
273 '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $path))
281 * Implementation of hook_perm().
283 function path_perm() {
284 return array('create url aliases', 'administer url aliases');
288 * Return a listing of all defined URL aliases.
290 function path_overview() {
291 $sql = 'SELECT * FROM {url_alias}';
293 array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'),
294 array('data' => t('System'), 'field' => 'src'),
295 array('data' => t('Operations'), 'colspan' => '2')
297 $sql .= tablesort_sql($header);
298 $result = pager_query($sql, 50);
300 $destination = drupal_get_destination();
301 while ($data = db_fetch_object($result)) {
302 $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));
306 $rows[] = array(array('data' => t('No URL aliases available.'), 'colspan' => '4'));
309 $output = theme('table', $header, $rows);
310 $output .= theme('pager', NULL, 50, 0);
315 * Fetch a specific URL alias from the database.
317 function path_load($pid) {
318 return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid));
322 * Verify that a new URL alias is valid, and save it to the database.
324 function path_form_submit() {
325 $edit = $GLOBALS['form_values'];
330 if (!valid_url($src)) {
331 form_set_error('src', t('The system path %path is invalid.', array('%path' => theme('placeholder', $src))));
334 if (!valid_url($dst)) {
335 form_set_error('dst', t('The alias %alias is invalid.', array('%alias' => theme('placeholder', $dst))));
338 if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != %d AND dst = '%s'", $pid, $dst))) {
339 form_set_error('dst', t('The alias %alias is already in use.', array('%alias' => theme('placeholder', $dst))));
342 if (form_get_errors()) {
343 return path_form($edit);
346 path_set_alias($src, $dst, $pid);
348 drupal_set_message(t('The alias has been saved.'));