webmaster@1
|
1 <?php |
webmaster@15
|
2 // $Id: path.module,v 1.138.2.3 2008/11/22 10:49:15 dries Exp $ |
webmaster@1
|
3 |
webmaster@1
|
4 /** |
webmaster@1
|
5 * @file |
webmaster@1
|
6 * Enables users to rename URLs. |
webmaster@1
|
7 */ |
webmaster@1
|
8 |
webmaster@1
|
9 /** |
webmaster@1
|
10 * Implementation of hook_help(). |
webmaster@1
|
11 */ |
webmaster@1
|
12 function path_help($path, $arg) { |
webmaster@1
|
13 switch ($path) { |
webmaster@1
|
14 case 'admin/help#path': |
webmaster@1
|
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>'; |
webmaster@1
|
16 $output .= t('<p>Some examples of URL aliases are:</p> |
webmaster@1
|
17 <ul> |
webmaster@1
|
18 <li>user/login => login</li> |
webmaster@1
|
19 <li>image/tid/16 => store</li> |
webmaster@1
|
20 <li>taxonomy/term/7+19+20+21 => store/products/whirlygigs</li> |
webmaster@1
|
21 <li>node/3 => contact</li> |
webmaster@1
|
22 </ul> |
webmaster@1
|
23 '); |
webmaster@1
|
24 $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>'; |
webmaster@1
|
25 $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>'; |
webmaster@1
|
26 $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>'; |
webmaster@1
|
27 return $output; |
webmaster@1
|
28 case 'admin/build/path': |
webmaster@1
|
29 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>'; |
webmaster@1
|
30 case 'admin/build/path/add': |
webmaster@1
|
31 return '<p>'. t('Enter the path you wish to create the alias for, followed by the name of the new alias.') .'</p>'; |
webmaster@1
|
32 } |
webmaster@1
|
33 } |
webmaster@1
|
34 |
webmaster@1
|
35 /** |
webmaster@1
|
36 * Implementation of hook_menu(). |
webmaster@1
|
37 */ |
webmaster@1
|
38 function path_menu() { |
webmaster@1
|
39 $items['admin/build/path'] = array( |
webmaster@1
|
40 'title' => 'URL aliases', |
webmaster@1
|
41 'description' => "Change your site's URL paths by aliasing them.", |
webmaster@1
|
42 'page callback' => 'path_admin_overview', |
webmaster@1
|
43 'access arguments' => array('administer url aliases'), |
webmaster@1
|
44 'file' => 'path.admin.inc', |
webmaster@1
|
45 ); |
webmaster@1
|
46 $items['admin/build/path/edit'] = array( |
webmaster@1
|
47 'title' => 'Edit alias', |
webmaster@1
|
48 'page callback' => 'path_admin_edit', |
webmaster@5
|
49 'access arguments' => array('administer url aliases'), |
webmaster@1
|
50 'type' => MENU_CALLBACK, |
webmaster@1
|
51 'file' => 'path.admin.inc', |
webmaster@1
|
52 ); |
webmaster@1
|
53 $items['admin/build/path/delete'] = array( |
webmaster@1
|
54 'title' => 'Delete alias', |
webmaster@1
|
55 'page callback' => 'drupal_get_form', |
webmaster@1
|
56 'page arguments' => array('path_admin_delete_confirm'), |
webmaster@5
|
57 'access arguments' => array('administer url aliases'), |
webmaster@1
|
58 'type' => MENU_CALLBACK, |
webmaster@1
|
59 'file' => 'path.admin.inc', |
webmaster@1
|
60 ); |
webmaster@1
|
61 $items['admin/build/path/list'] = array( |
webmaster@1
|
62 'title' => 'List', |
webmaster@1
|
63 'type' => MENU_DEFAULT_LOCAL_TASK, |
webmaster@1
|
64 'weight' => -10, |
webmaster@1
|
65 ); |
webmaster@1
|
66 $items['admin/build/path/add'] = array( |
webmaster@1
|
67 'title' => 'Add alias', |
webmaster@1
|
68 'page callback' => 'path_admin_edit', |
webmaster@1
|
69 'access arguments' => array('administer url aliases'), |
webmaster@1
|
70 'type' => MENU_LOCAL_TASK, |
webmaster@1
|
71 'file' => 'path.admin.inc', |
webmaster@1
|
72 ); |
webmaster@1
|
73 |
webmaster@1
|
74 return $items; |
webmaster@1
|
75 } |
webmaster@1
|
76 |
webmaster@1
|
77 /** |
webmaster@1
|
78 * Post-confirmation; delete an URL alias. |
webmaster@1
|
79 */ |
webmaster@1
|
80 function path_admin_delete($pid = 0) { |
webmaster@1
|
81 db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid); |
webmaster@1
|
82 drupal_set_message(t('The alias has been deleted.')); |
webmaster@1
|
83 } |
webmaster@1
|
84 |
webmaster@1
|
85 /** |
webmaster@1
|
86 * Set an aliased path for a given Drupal path, preventing duplicates. |
webmaster@1
|
87 */ |
webmaster@1
|
88 function path_set_alias($path = NULL, $alias = NULL, $pid = NULL, $language = '') { |
webmaster@1
|
89 $path = urldecode($path); |
webmaster@1
|
90 $alias = urldecode($alias); |
webmaster@1
|
91 // First we check if we deal with an existing alias and delete or modify it based on pid. |
webmaster@1
|
92 if ($pid) { |
webmaster@1
|
93 // An existing alias. |
webmaster@1
|
94 if (!$path || !$alias) { |
webmaster@1
|
95 // Delete the alias based on pid. |
webmaster@1
|
96 db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid); |
webmaster@1
|
97 } |
webmaster@1
|
98 else { |
webmaster@1
|
99 // Update the existing alias. |
webmaster@1
|
100 db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE pid = %d", $path, $alias, $language, $pid); |
webmaster@1
|
101 } |
webmaster@1
|
102 } |
webmaster@1
|
103 else if ($path && $alias) { |
webmaster@1
|
104 // Check for existing aliases. |
webmaster@1
|
105 if ($alias == drupal_get_path_alias($path, $language)) { |
webmaster@1
|
106 // There is already such an alias, neutral or in this language. |
webmaster@1
|
107 // Update the alias based on alias; setting the language if not yet done. |
webmaster@1
|
108 db_query("UPDATE {url_alias} SET src = '%s', dst = '%s', language = '%s' WHERE dst = '%s'", $path, $alias, $language, $alias); |
webmaster@1
|
109 } |
webmaster@1
|
110 else { |
webmaster@1
|
111 // A new alias. Add it to the database. |
webmaster@1
|
112 db_query("INSERT INTO {url_alias} (src, dst, language) VALUES ('%s', '%s', '%s')", $path, $alias, $language); |
webmaster@1
|
113 } |
webmaster@1
|
114 } |
webmaster@1
|
115 else { |
webmaster@1
|
116 // Delete the alias. |
webmaster@1
|
117 if ($alias) { |
webmaster@1
|
118 db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias); |
webmaster@1
|
119 } |
webmaster@1
|
120 else { |
webmaster@1
|
121 db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path); |
webmaster@1
|
122 } |
webmaster@1
|
123 } |
webmaster@1
|
124 drupal_clear_path_cache(); |
webmaster@1
|
125 } |
webmaster@1
|
126 |
webmaster@1
|
127 |
webmaster@1
|
128 /** |
webmaster@1
|
129 * Implementation of hook_nodeapi(). |
webmaster@1
|
130 * |
webmaster@1
|
131 * Allows URL aliases for nodes to be specified at node edit time rather |
webmaster@1
|
132 * than through the administrative interface. |
webmaster@1
|
133 */ |
webmaster@1
|
134 function path_nodeapi(&$node, $op, $arg) { |
webmaster@1
|
135 // Permissions are required for everything except node loading. |
webmaster@1
|
136 if (user_access('create url aliases') || user_access('administer url aliases') || ($op == 'load')) { |
webmaster@1
|
137 $language = isset($node->language) ? $node->language : ''; |
webmaster@1
|
138 switch ($op) { |
webmaster@1
|
139 case 'validate': |
webmaster@7
|
140 if (isset($node->path)) { |
webmaster@7
|
141 $node->path = trim($node->path); |
webmaster@7
|
142 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))) { |
webmaster@7
|
143 form_set_error('path', t('The path is already in use.')); |
webmaster@7
|
144 } |
webmaster@1
|
145 } |
webmaster@1
|
146 break; |
webmaster@1
|
147 |
webmaster@1
|
148 case 'load': |
webmaster@1
|
149 $path = 'node/'. $node->nid; |
webmaster@1
|
150 $alias = drupal_get_path_alias($path, $language); |
webmaster@1
|
151 if ($path != $alias) { |
webmaster@1
|
152 $node->path = $alias; |
webmaster@1
|
153 } |
webmaster@1
|
154 break; |
webmaster@1
|
155 |
webmaster@1
|
156 case 'insert': |
webmaster@1
|
157 // Don't try to insert if path is NULL. We may have already set |
webmaster@1
|
158 // the alias ahead of time. |
webmaster@1
|
159 if (isset($node->path)) { |
webmaster@1
|
160 path_set_alias('node/'. $node->nid, $node->path, NULL, $language); |
webmaster@1
|
161 } |
webmaster@1
|
162 break; |
webmaster@1
|
163 |
webmaster@1
|
164 case 'update': |
webmaster@1
|
165 path_set_alias('node/'. $node->nid, isset($node->path) ? $node->path : NULL, isset($node->pid) ? $node->pid : NULL, $language); |
webmaster@1
|
166 break; |
webmaster@1
|
167 |
webmaster@1
|
168 case 'delete': |
webmaster@1
|
169 $path = 'node/'. $node->nid; |
webmaster@1
|
170 if (drupal_get_path_alias($path) != $path) { |
webmaster@1
|
171 path_set_alias($path); |
webmaster@1
|
172 } |
webmaster@1
|
173 break; |
webmaster@1
|
174 } |
webmaster@1
|
175 } |
webmaster@1
|
176 } |
webmaster@1
|
177 |
webmaster@1
|
178 /** |
webmaster@1
|
179 * Implementation of hook_form_alter(). |
webmaster@1
|
180 */ |
webmaster@1
|
181 function path_form_alter(&$form, $form_state, $form_id) { |
webmaster@1
|
182 if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) { |
webmaster@1
|
183 $path = isset($form['#node']->path) ? $form['#node']->path : NULL; |
webmaster@1
|
184 $form['path'] = array( |
webmaster@1
|
185 '#type' => 'fieldset', |
webmaster@1
|
186 '#title' => t('URL path settings'), |
webmaster@1
|
187 '#collapsible' => TRUE, |
webmaster@1
|
188 '#collapsed' => empty($path), |
webmaster@1
|
189 '#access' => user_access('create url aliases'), |
webmaster@1
|
190 '#weight' => 30, |
webmaster@1
|
191 ); |
webmaster@1
|
192 $form['path']['path'] = array( |
webmaster@1
|
193 '#type' => 'textfield', |
webmaster@1
|
194 '#default_value' => $path, |
webmaster@15
|
195 '#maxlength' => 128, |
webmaster@1
|
196 '#collapsible' => TRUE, |
webmaster@1
|
197 '#collapsed' => TRUE, |
webmaster@1
|
198 '#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.'), |
webmaster@1
|
199 ); |
webmaster@1
|
200 if ($path) { |
webmaster@1
|
201 $form['path']['pid'] = array( |
webmaster@1
|
202 '#type' => 'value', |
webmaster@1
|
203 '#value' => db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s' AND language = '%s'", $path, $form['#node']->language)) |
webmaster@1
|
204 ); |
webmaster@1
|
205 } |
webmaster@1
|
206 } |
webmaster@1
|
207 } |
webmaster@1
|
208 |
webmaster@1
|
209 /** |
webmaster@1
|
210 * Implementation of hook_perm(). |
webmaster@1
|
211 */ |
webmaster@1
|
212 function path_perm() { |
webmaster@1
|
213 return array('create url aliases', 'administer url aliases'); |
webmaster@1
|
214 } |
webmaster@1
|
215 |
webmaster@1
|
216 /** |
webmaster@1
|
217 * Fetch a specific URL alias from the database. |
webmaster@1
|
218 */ |
webmaster@1
|
219 function path_load($pid) { |
webmaster@1
|
220 return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid)); |
webmaster@1
|
221 } |