2 // $Id: legacy.module 144 2007-03-28 07:52:20Z thierry $
6 * Provides legacy handlers for upgrades from older Drupal installations.
10 * Implementation of hook_help().
12 function legacy_help($section) {
14 case 'admin/help#legacy':
15 $output = '<p>'. t('The legacy module provides legacy handlers for upgrades from older installations. These handlers help automatically redirect references to pages from old installations and prevent <em>page not found</em> errors for your site.') .'</p>';
16 $output .= '<p>'. t('The legacy module handles legacy style taxonomy page, taxonomy feed, and blog feed paths. It also handles URL upgrades from Drupal 4.1. It rewrites old-style URLs to new-style URLs (clean URLs). ') .'</p>';
17 $output .= t('<p>Example Mappings:</p>
19 <li><em>taxonomy/page/or/52,97</em> to <em>taxonomy/term/52+97</em>.</li>
20 <li><em>taxonomy/feed/or/52,97</em> to <em>taxonomy/term/52+97/0/feed</em>.</li>
21 <li><em>blog/feed/52</em> to <em>blog/52/feed</em>.</li>
22 <li><em>node/view/52</em> to <em>node/52</em>.</li>
23 <li><em>book/view/52</em> to <em>node/52</em>.</li>
24 <li><em>user/view/52</em> to <em>user/52</em>.</li>
27 $output .= '<p>'. t('Legacy module has no configurable options.') .'</p>';
28 $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%legacy">Legacy page</a>.', array('%legacy' => 'http://drupal.org/handbook/modules/legacy/')) .'</p>';
30 case 'admin/modules#description':
31 return t('Provides legacy handlers for upgrades from older Drupal installations.');
36 * Implementation of hook_menu().
38 * Registers menu paths used in earlier Drupal versions.
40 function legacy_menu($may_cache) {
44 // Map "taxonomy/page/or/52,97" to "taxonomy/term/52+97".
45 $items[] = array('path' => 'taxonomy/page', 'title' => t('taxonomy'),
46 'callback' => 'legacy_taxonomy_page',
47 'access' => TRUE, 'type' => MENU_CALLBACK);
49 // Map "taxonomy/feed/or/52,97" to "taxonomy/term/52+97/0/feed".
50 $items[] = array('path' => 'taxonomy/feed', 'title' => t('taxonomy'),
51 'callback' => 'legacy_taxonomy_feed',
52 'access' => TRUE, 'type' => MENU_CALLBACK);
54 // Map "blog/feed/52" to "blog/52/feed".
55 $items[] = array('path' => 'blog/feed', 'title' => t('blog'),
56 'callback' => 'legacy_blog_feed',
57 'access' => TRUE, 'type' => MENU_CALLBACK);
60 // Map "node/view/52" to "node/52".
61 $items[] = array('path' => 'node/view', 'title' => t('view'),
62 'callback' => 'drupal_goto',
63 'callback arguments' => array('node/'. arg(2), NULL, NULL),
64 'access' => TRUE, 'type' => MENU_CALLBACK);
66 // Map "book/view/52" to "node/52".
67 $items[] = array('path' => 'book/view', 'title' => t('view'),
68 'callback' => 'drupal_goto',
69 'callback arguments' => array('node/'. arg(2), NULL, NULL),
70 'access' => TRUE, 'type' => MENU_CALLBACK);
72 // Map "user/view/52" to "user/52".
73 $items[] = array('path' => 'user/view', 'title' => t('view'),
74 'callback' => 'drupal_goto',
75 'callback arguments' => array('user/'. arg(2), NULL, NULL),
76 'access' => TRUE, 'type' => MENU_CALLBACK);
83 * Menu callback; redirects users to new taxonomy page paths.
85 function legacy_taxonomy_page($operation = 'or', $str_tids = '') {
86 if ($operation == 'or') {
87 $str_tids = str_replace(',', '+', $str_tids);
89 drupal_goto('taxonomy/term/'. $str_tids);
93 * Menu callback; redirects users to new taxonomy feed paths.
95 function legacy_taxonomy_feed($operation = 'or', $str_tids = '') {
96 if ($operation == 'or') {
97 $str_tids = str_replace(',', '+', $str_tids);
99 drupal_goto('taxonomy/term/'. $str_tids .'/0/feed');
103 * Menu callback; redirects users to new blog feed paths.
105 function legacy_blog_feed($str_uid = '') {
106 // if URL is of form blog/feed/52 redirect
107 // if URL is of form blog/feed we have to call blog_feed_last().
108 if (is_numeric($str_uid)) {
109 drupal_goto('blog/'. $str_uid .'/feed');
112 module_invoke('blog', 'feed_last');
117 * Implementation of hook_filter(). Handles URL upgrades from Drupal 4.1.
119 function legacy_filter($op, $delta = 0, $format = -1, $text = '') {
122 return array(t('Legacy filter'));
125 return t('Replaces URLs from Drupal 4.1 (and lower) with updated equivalents.');
128 return _legacy_filter_old_urls($text);
139 * Rewrite legacy URLs.
141 * This is a *temporary* filter to rewrite old-style URLs to new-style
142 * URLs (clean URLs). Currently, URLs are being rewritten dynamically
143 * (ie. "on output"), however when these rewrite rules have been tested
144 * enough, we will use them to permanently rewrite the links in node
145 * and comment bodies.
147 function _legacy_filter_old_urls($text) {
148 if (!variable_get('rewrite_old_urls', 0)) {
154 $end = substr($base_url, 12);
156 if (variable_get('clean_url', '0') == '0') {
159 // rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
160 $text = eregi_replace("\"(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "\"?q=\\1/view/\\2/\\4", $text);
162 // rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
163 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2/\\4/\\6" , $text);
164 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2/\\4", $text);
165 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2", $text);
169 // rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
170 $text = eregi_replace("$end/(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "$end/?q=\\1/view/\\2/\\4", $text);
172 // rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
173 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/?q=\\2/\\4/\\6" , $text);
174 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/?q=\\2/\\4", $text);
175 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"$end/?q=\\2", $text);
180 // Rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
181 $text = eregi_replace("\"(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "\"\\1/view/\\2/\\4", $text);
183 // Rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
184 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2/\\4/\\6", $text);
185 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2/\\4", $text);
186 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2", $text);
190 // Rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
191 $text = eregi_replace("$end/(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "$end/\\1/view/\\2/\\4", $text);
193 // Rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
194 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2/\\4/\\6", $text);
195 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2/\\4", $text);
196 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2", $text);