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