2 // $Id: legacy.module,v 1.15 2006/11/21 20:14:18 dries Exp $
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('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>';
33 * Implementation of hook_menu().
35 * Registers menu paths used in earlier Drupal versions.
37 function legacy_menu($may_cache) {
41 // Map "taxonomy/page/or/52,97" to "taxonomy/term/52+97".
42 $items[] = array('path' => 'taxonomy/page', 'title' => t('Taxonomy'),
43 'callback' => 'legacy_taxonomy_page',
44 'access' => TRUE, 'type' => MENU_CALLBACK);
46 // Map "taxonomy/feed/or/52,97" to "taxonomy/term/52+97/0/feed".
47 $items[] = array('path' => 'taxonomy/feed', 'title' => t('Taxonomy'),
48 'callback' => 'legacy_taxonomy_feed',
49 'access' => TRUE, 'type' => MENU_CALLBACK);
51 // Map "blog/feed/52" to "blog/52/feed".
52 $items[] = array('path' => 'blog/feed', 'title' => t('Blog'),
53 'callback' => 'legacy_blog_feed',
54 'access' => TRUE, 'type' => MENU_CALLBACK);
57 // Map "node/view/52" to "node/52".
58 $items[] = array('path' => 'node/view', 'title' => t('View'),
59 'callback' => 'drupal_goto',
60 'callback arguments' => array('node/'. arg(2), NULL, NULL),
61 'access' => TRUE, 'type' => MENU_CALLBACK);
63 // Map "book/view/52" to "node/52".
64 $items[] = array('path' => 'book/view', 'title' => t('View'),
65 'callback' => 'drupal_goto',
66 'callback arguments' => array('node/'. arg(2), NULL, NULL),
67 'access' => TRUE, 'type' => MENU_CALLBACK);
69 // Map "user/view/52" to "user/52".
70 $items[] = array('path' => 'user/view', 'title' => t('View'),
71 'callback' => 'drupal_goto',
72 'callback arguments' => array('user/'. arg(2), NULL, NULL),
73 'access' => TRUE, 'type' => MENU_CALLBACK);
80 * Menu callback; redirects users to new taxonomy page paths.
82 function legacy_taxonomy_page($operation = 'or', $str_tids = '') {
83 if ($operation == 'or') {
84 $str_tids = str_replace(',', '+', $str_tids);
86 drupal_goto('taxonomy/term/'. $str_tids);
90 * Menu callback; redirects users to new taxonomy feed paths.
92 function legacy_taxonomy_feed($operation = 'or', $str_tids = '') {
93 if ($operation == 'or') {
94 $str_tids = str_replace(',', '+', $str_tids);
96 drupal_goto('taxonomy/term/'. $str_tids .'/0/feed');
100 * Menu callback; redirects users to new blog feed paths.
102 function legacy_blog_feed($str_uid = '') {
103 // if URL is of form blog/feed/52 redirect
104 // if URL is of form blog/feed we have to call blog_feed_last().
105 if (is_numeric($str_uid)) {
106 drupal_goto('blog/'. $str_uid .'/feed');
109 module_invoke('blog', 'feed_last');
114 * Implementation of hook_filter(). Handles URL upgrades from Drupal 4.1.
116 function legacy_filter($op, $delta = 0, $format = -1, $text = '') {
119 return array(t('Legacy filter'));
122 return t('Replaces URLs from Drupal 4.1 (and lower) with updated equivalents.');
125 return _legacy_filter_old_urls($text);
136 * Rewrite legacy URLs.
138 * This is a *temporary* filter to rewrite old-style URLs to new-style
139 * URLs (clean URLs). Currently, URLs are being rewritten dynamically
140 * (ie. "on output"), however when these rewrite rules have been tested
141 * enough, we will use them to permanently rewrite the links in node
142 * and comment bodies.
144 function _legacy_filter_old_urls($text) {
145 if (!variable_get('rewrite_old_urls', 0)) {
151 $end = substr($base_url, 12);
153 if (variable_get('clean_url', '0') == '0') {
156 // rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
157 $text = eregi_replace("\"(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "\"?q=\\1/view/\\2/\\4", $text);
159 // rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
160 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2/\\4/\\6" , $text);
161 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2/\\4", $text);
162 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2", $text);
166 // rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
167 $text = eregi_replace("$end/(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "$end/?q=\\1/view/\\2/\\4", $text);
169 // rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
170 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/?q=\\2/\\4/\\6" , $text);
171 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/?q=\\2/\\4", $text);
172 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"$end/?q=\\2", $text);
177 // Rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
178 $text = eregi_replace("\"(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "\"\\1/view/\\2/\\4", $text);
180 // Rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
181 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2/\\4/\\6", $text);
182 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2/\\4", $text);
183 $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2", $text);
187 // Rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
188 $text = eregi_replace("$end/(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "$end/\\1/view/\\2/\\4", $text);
190 // Rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
191 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2/\\4/\\6", $text);
192 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2/\\4", $text);
193 $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2", $text);