legacy.module 6.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
// $Id$

/**
 * @file
 * Provides legacy handlers for upgrades from older Drupal installations.
 */

/**
 * Implementation of hook_help().
 */
function legacy_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Provides legacy handlers for upgrades from older Drupal installations.');
  }
}

/**
 * Implementation of hook_menu().
 *
 * Registers menu paths used in earlier Drupal versions.
 */
Dries's avatar
   
Dries committed
24
function legacy_menu($may_cache) {
25
26
  $items = array();

Dries's avatar
   
Dries committed
27
  if ($may_cache) {
28
29
30
31
32
33
34
35
36
    // Map "taxonomy/page/or/52,97" to "taxonomy/term/52+97".
    $items[] = array('path' => 'taxonomy/page', 'title' => t('taxonomy'),
      'callback' => 'legacy_taxonomy_page',
      'access' => TRUE, 'type' => MENU_CALLBACK);

    // Map "taxonomy/feed/or/52,97" to "taxonomy/term/52+97/0/feed".
    $items[] = array('path' => 'taxonomy/feed', 'title' => t('taxonomy'),
      'callback' => 'legacy_taxonomy_feed',
      'access' => TRUE, 'type' => MENU_CALLBACK);
37
38
39
40
41

    // Map "blog/feed/52" to "blog/52/feed".
    $items[] = array('path' => 'blog/feed', 'title' => t('blog'),
      'callback' => 'legacy_blog_feed',
      'access' => TRUE, 'type' => MENU_CALLBACK);
42
43
  }
  else {
Dries's avatar
   
Dries committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    // Map "node/view/52" to "node/52".
    $items[] = array('path' => 'node/view', 'title' => t('view'),
      'callback' => 'drupal_goto',
      'callback arguments' => array('node/'. arg(2), NULL, NULL),
      'access' => TRUE, 'type' => MENU_CALLBACK);

    // Map "book/view/52" to "node/52".
    $items[] = array('path' => 'book/view', 'title' => t('view'),
      'callback' => 'drupal_goto',
      'callback arguments' => array('node/'. arg(2), NULL, NULL),
      'access' => TRUE, 'type' => MENU_CALLBACK);

    // Map "user/view/52" to "user/52".
    $items[] = array('path' => 'user/view', 'title' => t('view'),
      'callback' => 'drupal_goto',
      'callback arguments' => array('user/'. arg(2), NULL, NULL),
      'access' => TRUE, 'type' => MENU_CALLBACK);
  }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

  return $items;
}

/**
 * Menu callback; redirects users to new taxonomy page paths.
 */
function legacy_taxonomy_page($operation = 'or', $str_tids = '') {
  if ($operation == 'or') {
    $str_tids = str_replace(',', '+', $str_tids);
  }
  drupal_goto('taxonomy/term/'. $str_tids);
}

/**
 * Menu callback; redirects users to new taxonomy feed paths.
 */
function legacy_taxonomy_feed($operation = 'or', $str_tids = '') {
  if ($operation == 'or') {
    $str_tids = str_replace(',', '+', $str_tids);
  }
  drupal_goto('taxonomy/term/'. $str_tids .'/0/feed');
}

86
87
88
89
90
91
92
93
94
95
96
/**
 * Menu callback; redirects users to new blog feed paths.
 */
function legacy_blog_feed($str_uid = '') {
  // if URL is of form blog/feed/52 redirect
  // if URL is of form blog/feed no need to redirect
  if (is_numeric($str_uid)) {
    drupal_goto('blog/'. $str_uid .'/feed');
  }
}

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
 * Implementation of hook_filter(). Handles URL upgrades from Drupal 4.1.
 */
function legacy_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(t('Legacy filter'));

    case 'description':
      return t('Replaces URLs from Drupal 4.1 (and lower) with updated equivalents.');

    case 'process':
      return _legacy_filter_old_urls($text, $format);

    case 'settings':
      return;

    default:
      return $text;
  }
}

/**
 * Rewrite legacy URLs.
 *
 * This is a *temporary* filter to rewrite old-style URLs to new-style
 * URLs (clean URLs).  Currently, URLs are being rewritten dynamically
 * (ie. "on output"), however when these rewrite rules have been tested
 * enough, we will use them to permanently rewrite the links in node
 * and comment bodies.
 */
function _legacy_filter_old_urls($text) {
  if (!variable_get('rewrite_old_urls', 0)) {
    return $text;
  }

  global $base_url;

  $end = substr($base_url, 12);

  if (variable_get('clean_url', '0') == '0') {
    // Relative URLs:

    // rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
    $text = eregi_replace("\"(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "\"?q=\\1/view/\\2/\\4", $text);

    // rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
    $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2/\\4/\\6" , $text);
    $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2/\\4", $text);
    $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2", $text);

    // Absolute URLs:

    // rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
    $text = eregi_replace("$end/(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "$end/?q=\\1/view/\\2/\\4", $text);

    // rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
    $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/?q=\\2/\\4/\\6" , $text);
    $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/?q=\\2/\\4", $text);
    $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"$end/?q=\\2", $text);
  }
  else {
    // Relative URLs:

    // Rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
    $text = eregi_replace("\"(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "\"\\1/view/\\2/\\4", $text);

    // Rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
    $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2/\\4/\\6", $text);
    $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2/\\4", $text);
    $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2", $text);

    // Absolute URLs:

    // Rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
    $text = eregi_replace("$end/(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "$end/\\1/view/\\2/\\4", $text);

    // Rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
    $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2/\\4/\\6", $text);
    $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2/\\4", $text);
    $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2", $text);
  }

  return $text;
}

?>