legacy.module

  1. drupal
    1. 4.6 modules/legacy.module
    2. 4.7 modules/legacy.module
    3. 5 modules/legacy/legacy.module

Provides legacy handlers for upgrades from older Drupal installations.

Functions & methods

NameDescription
legacy_blog_feedMenu callback; redirects users to new blog feed paths.
legacy_filterImplementation of hook_filter(). Handles URL upgrades from Drupal 4.1.
legacy_helpImplementation of hook_help().
legacy_menuImplementation of hook_menu().
legacy_taxonomy_feedMenu callback; redirects users to new taxonomy feed paths.
legacy_taxonomy_pageMenu callback; redirects users to new taxonomy page paths.
_legacy_filter_old_urlsRewrite legacy URLs.

File

modules/legacy/legacy.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides legacy handlers for upgrades from older Drupal installations.
  5. */
  6. /**
  7. * Implementation of hook_help().
  8. */
  9. function legacy_help($section) {
  10. switch ($section) {
  11. case 'admin/help#legacy':
  12. $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>';
  13. $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>';
  14. $output .= t('<p>Example Mappings:</p>
  15. <ul>
  16. <li><em>taxonomy/page/or/52,97</em> to <em>taxonomy/term/52+97</em>.</li>
  17. <li><em>taxonomy/feed/or/52,97</em> to <em>taxonomy/term/52+97/0/feed</em>.</li>
  18. <li><em>blog/feed/52</em> to <em>blog/52/feed</em>.</li>
  19. <li><em>node/view/52</em> to <em>node/52</em>.</li>
  20. <li><em>book/view/52</em> to <em>node/52</em>.</li>
  21. <li><em>user/view/52</em> to <em>user/52</em>.</li>
  22. </ul>
  23. ');
  24. $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>';
  25. return $output;
  26. }
  27. }
  28. /**
  29. * Implementation of hook_menu().
  30. *
  31. * Registers menu paths used in earlier Drupal versions.
  32. */
  33. function legacy_menu($may_cache) {
  34. $items = array();
  35. if ($may_cache) {
  36. // Map "taxonomy/page/or/52,97" to "taxonomy/term/52+97".
  37. $items[] = array('path' => 'taxonomy/page', 'title' => t('Taxonomy'),
  38. 'callback' => 'legacy_taxonomy_page',
  39. 'access' => TRUE, 'type' => MENU_CALLBACK);
  40. // Map "taxonomy/feed/or/52,97" to "taxonomy/term/52+97/0/feed".
  41. $items[] = array('path' => 'taxonomy/feed', 'title' => t('Taxonomy'),
  42. 'callback' => 'legacy_taxonomy_feed',
  43. 'access' => TRUE, 'type' => MENU_CALLBACK);
  44. // Map "blog/feed/52" to "blog/52/feed".
  45. $items[] = array('path' => 'blog/feed', 'title' => t('Blog'),
  46. 'callback' => 'legacy_blog_feed',
  47. 'access' => TRUE, 'type' => MENU_CALLBACK);
  48. }
  49. else {
  50. // Map "node/view/52" to "node/52".
  51. $items[] = array('path' => 'node/view', 'title' => t('View'),
  52. 'callback' => 'drupal_goto',
  53. 'callback arguments' => array('node/'. arg(2), NULL, NULL, 301),
  54. 'access' => TRUE, 'type' => MENU_CALLBACK);
  55. // Map "book/view/52" to "node/52".
  56. $items[] = array('path' => 'book/view', 'title' => t('View'),
  57. 'callback' => 'drupal_goto',
  58. 'callback arguments' => array('node/'. arg(2), NULL, NULL, 301),
  59. 'access' => TRUE, 'type' => MENU_CALLBACK);
  60. // Map "user/view/52" to "user/52".
  61. $items[] = array('path' => 'user/view', 'title' => t('View'),
  62. 'callback' => 'drupal_goto',
  63. 'callback arguments' => array('user/'. arg(2), NULL, NULL, 301),
  64. 'access' => TRUE, 'type' => MENU_CALLBACK);
  65. }
  66. return $items;
  67. }
  68. /**
  69. * Menu callback; redirects users to new taxonomy page paths.
  70. */
  71. function legacy_taxonomy_page($operation = 'or', $str_tids = '') {
  72. if ($operation == 'or') {
  73. $str_tids = str_replace(',', '+', $str_tids);
  74. }
  75. drupal_goto('taxonomy/term/'. $str_tids);
  76. }
  77. /**
  78. * Menu callback; redirects users to new taxonomy feed paths.
  79. */
  80. function legacy_taxonomy_feed($operation = 'or', $str_tids = '') {
  81. if ($operation == 'or') {
  82. $str_tids = str_replace(',', '+', $str_tids);
  83. }
  84. drupal_goto('taxonomy/term/'. $str_tids .'/0/feed');
  85. }
  86. /**
  87. * Menu callback; redirects users to new blog feed paths.
  88. */
  89. function legacy_blog_feed($str_uid = '') {
  90. // if URL is of form blog/feed/52 redirect
  91. // if URL is of form blog/feed we have to call blog_feed_last().
  92. if (is_numeric($str_uid)) {
  93. drupal_goto('blog/'. $str_uid .'/feed');
  94. }
  95. else {
  96. module_invoke('blog', 'feed_last');
  97. }
  98. }
  99. /**
  100. * Implementation of hook_filter(). Handles URL upgrades from Drupal 4.1.
  101. */
  102. function legacy_filter($op, $delta = 0, $format = -1, $text = '') {
  103. switch ($op) {
  104. case 'list':
  105. return array(t('Legacy filter'));
  106. case 'description':
  107. return t('Replaces URLs from Drupal 4.1 (and lower) with updated equivalents.');
  108. case 'process':
  109. return _legacy_filter_old_urls($text);
  110. case 'settings':
  111. return;
  112. default:
  113. return $text;
  114. }
  115. }
  116. /**
  117. * Rewrite legacy URLs.
  118. *
  119. * This is a *temporary* filter to rewrite old-style URLs to new-style
  120. * URLs (clean URLs). Currently, URLs are being rewritten dynamically
  121. * (ie. "on output"), however when these rewrite rules have been tested
  122. * enough, we will use them to permanently rewrite the links in node
  123. * and comment bodies.
  124. */
  125. function _legacy_filter_old_urls($text) {
  126. if (!variable_get('rewrite_old_urls', 0)) {
  127. return $text;
  128. }
  129. global $base_url;
  130. $end = substr($base_url, 12);
  131. if (variable_get('clean_url', '0') == '0') {
  132. // Relative URLs:
  133. // rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
  134. $text = eregi_replace("\"(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "\"?q=\\1/view/\\2/\\4", $text);
  135. // rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
  136. $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2/\\4/\\6" , $text);
  137. $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2/\\4", $text);
  138. $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"?q=\\2", $text);
  139. // Absolute URLs:
  140. // rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
  141. $text = eregi_replace("$end/(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "$end/?q=\\1/view/\\2/\\4", $text);
  142. // rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
  143. $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/?q=\\2/\\4/\\6" , $text);
  144. $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/?q=\\2/\\4", $text);
  145. $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"$end/?q=\\2", $text);
  146. }
  147. else {
  148. // Relative URLs:
  149. // Rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
  150. $text = eregi_replace("\"(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "\"\\1/view/\\2/\\4", $text);
  151. // Rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
  152. $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2/\\4/\\6", $text);
  153. $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2/\\4", $text);
  154. $text = ereg_replace("\"module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "\"\\2", $text);
  155. // Absolute URLs:
  156. // Rewrite 'node.php?id=<number>[&cid=<number>]' style URLs:
  157. $text = eregi_replace("$end/(node)\.php\?id=([[:digit:]]+)(&cid=)?([[:digit:]]*)", "$end/\\1/view/\\2/\\4", $text);
  158. // Rewrite 'module.php?mod=<name>{&<op>=<value>}' style URLs:
  159. $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2/\\4/\\6", $text);
  160. $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2/\\4", $text);
  161. $text = ereg_replace("$end/module\.php\?(&?[[:alpha:]]+=([[:alnum:]]+))", "$end/\\2", $text);
  162. }
  163. return $text;
  164. }
Login or register to post comments