initial import from onelab svn codebase
[plewww.git] / modules / legacy.module
1 <?php
2 // $Id: legacy.module 144 2007-03-28 07:52:20Z thierry $
3
4 /**
5  * @file
6  * Provides legacy handlers for upgrades from older Drupal installations.
7  */
8
9 /**
10  * Implementation of hook_help().
11  */
12 function legacy_help($section) {
13   switch ($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>
18 <ul>
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>
25 </ul>
26 ');
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>';
29       return $output;
30     case 'admin/modules#description':
31       return t('Provides legacy handlers for upgrades from older Drupal installations.');
32   }
33 }
34
35 /**
36  * Implementation of hook_menu().
37  *
38  * Registers menu paths used in earlier Drupal versions.
39  */
40 function legacy_menu($may_cache) {
41   $items = array();
42
43   if ($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);
48
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);
53
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);
58   }
59   else {
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);
65
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);
71
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);
77   }
78
79   return $items;
80 }
81
82 /**
83  * Menu callback; redirects users to new taxonomy page paths.
84  */
85 function legacy_taxonomy_page($operation = 'or', $str_tids = '') {
86   if ($operation == 'or') {
87     $str_tids = str_replace(',', '+', $str_tids);
88   }
89   drupal_goto('taxonomy/term/'. $str_tids);
90 }
91
92 /**
93  * Menu callback; redirects users to new taxonomy feed paths.
94  */
95 function legacy_taxonomy_feed($operation = 'or', $str_tids = '') {
96   if ($operation == 'or') {
97     $str_tids = str_replace(',', '+', $str_tids);
98   }
99   drupal_goto('taxonomy/term/'. $str_tids .'/0/feed');
100 }
101
102 /**
103  * Menu callback; redirects users to new blog feed paths.
104  */
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');
110   }
111   else {
112     module_invoke('blog', 'feed_last');
113   }
114 }
115
116 /**
117  * Implementation of hook_filter(). Handles URL upgrades from Drupal 4.1.
118  */
119 function legacy_filter($op, $delta = 0, $format = -1, $text = '') {
120   switch ($op) {
121     case 'list':
122       return array(t('Legacy filter'));
123
124     case 'description':
125       return t('Replaces URLs from Drupal 4.1 (and lower) with updated equivalents.');
126
127     case 'process':
128       return _legacy_filter_old_urls($text);
129
130     case 'settings':
131       return;
132
133     default:
134       return $text;
135   }
136 }
137
138 /**
139  * Rewrite legacy URLs.
140  *
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.
146  */
147 function _legacy_filter_old_urls($text) {
148   if (!variable_get('rewrite_old_urls', 0)) {
149     return $text;
150   }
151
152   global $base_url;
153
154   $end = substr($base_url, 12);
155
156   if (variable_get('clean_url', '0') == '0') {
157     // Relative URLs:
158
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);
161
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);
166
167     // Absolute URLs:
168
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);
171
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);
176   }
177   else {
178     // Relative URLs:
179
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);
182
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);
187
188     // Absolute URLs:
189
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);
192
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);
197   }
198
199   return $text;
200 }
201
202