redirect.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the redirect module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function redirect_schema() {
  10. $schema['redirect'] = array(
  11. 'description' => 'Stores information on redirects.',
  12. 'fields' => array(
  13. 'rid' => array(
  14. 'type' => 'serial',
  15. 'not null' => TRUE,
  16. 'description' => 'Primary Key: Unique redirect ID.',
  17. ),
  18. 'hash' => array(
  19. 'type' => 'varchar',
  20. 'length' => 64,
  21. 'not null' => TRUE,
  22. 'description' => 'A unique hash based on source, source_options, and language.',
  23. ),
  24. 'type' => array(
  25. 'type' => 'varchar',
  26. 'length' => 64,
  27. 'not null' => TRUE,
  28. 'default' => '',
  29. 'description' => "The redirect type; if value is 'redirect' it is a normal redirect handled by the module.",
  30. ),
  31. 'uid' => array(
  32. 'type' => 'int',
  33. 'unsigned' => TRUE,
  34. 'not null' => TRUE,
  35. 'default' => 0,
  36. 'description' => 'The {users}.uid of the user who created the redirect.',
  37. ),
  38. 'source' => array(
  39. 'type' => 'varchar',
  40. 'length' => 255,
  41. 'not null' => TRUE,
  42. 'description' => 'The source path to redirect from.',
  43. ),
  44. 'source_options' => array(
  45. 'type' => 'text',
  46. 'not null' => TRUE,
  47. 'serialize' => TRUE,
  48. 'description' => 'A serialized array of source options.',
  49. ),
  50. 'redirect' => array(
  51. 'type' => 'varchar',
  52. 'length' => 255,
  53. 'not null' => TRUE,
  54. 'description' => 'The destination path to redirect to.',
  55. ),
  56. 'redirect_options' => array(
  57. 'type' => 'text',
  58. 'not null' => TRUE,
  59. 'serialize' => TRUE,
  60. 'description' => 'A serialized array of redirect options.',
  61. ),
  62. 'language' => array(
  63. 'description' => 'The language this redirect is for; if blank, the alias will be used for unknown languages.',
  64. 'type' => 'varchar',
  65. 'length' => 12,
  66. 'not null' => TRUE,
  67. 'default' => 'und',
  68. ),
  69. 'status_code' => array(
  70. 'type' => 'int',
  71. 'size' => 'small',
  72. 'not null' => TRUE,
  73. 'description' => 'The HTTP status code to use for the redirect.',
  74. ),
  75. 'count' => array(
  76. 'type' => 'int',
  77. 'unsigned' => TRUE,
  78. 'not null' => TRUE,
  79. 'default' => 0,
  80. 'description' => 'The number of times the redirect has been used.',
  81. ),
  82. 'access' => array(
  83. 'type' => 'int',
  84. 'unsigned' => TRUE,
  85. 'not null' => TRUE,
  86. 'default' => 0,
  87. 'description' => 'The timestamp of when the redirect was last accessed.'
  88. ),
  89. ),
  90. 'primary key' => array('rid'),
  91. 'unique keys' => array(
  92. 'hash' => array('hash'),
  93. ),
  94. 'indexes' => array(
  95. 'expires' => array('type', 'access'),
  96. 'source_language' => array('source', 'language'),
  97. ),
  98. );
  99. return $schema;
  100. }
  101. /**
  102. * Implements hook_install().
  103. */
  104. function redirect_install() {
  105. // If the path redirect table exists, then set the schema to run the
  106. // migration update function.
  107. if (db_table_exists('path_redirect')) {
  108. drupal_set_installed_schema_version('redirect', 6999);
  109. }
  110. }
  111. /**
  112. * Implements hook_uninstall().
  113. */
  114. function redirect_uninstall() {
  115. drupal_load('module', 'redirect');
  116. $variables = array_keys(redirect_variables());
  117. foreach ($variables as $variable) {
  118. variable_del($variable);
  119. }
  120. }
  121. /**
  122. * Add the {redirect}.count field.
  123. */
  124. function redirect_update_1() {
  125. $field = array(
  126. 'type' => 'int',
  127. 'unsigned' => TRUE,
  128. 'not null' => TRUE,
  129. 'default' => 0,
  130. 'description' => 'The number of times the redirect has been used.',
  131. );
  132. db_add_field('redirect', 'count', $field);
  133. }
  134. /**
  135. * Add the {redirect}.uid field.
  136. */
  137. function redirect_update_2() {
  138. $field = array(
  139. 'type' => 'int',
  140. 'unsigned' => TRUE,
  141. 'not null' => TRUE,
  142. 'default' => 0,
  143. 'description' => 'The {users}.uid of the user who created the redirect.',
  144. );
  145. db_add_field('redirect', 'uid', $field);
  146. db_update('redirect')
  147. ->fields(array('uid' => 1))
  148. ->execute();
  149. }
  150. /**
  151. * Enable bootstrap status for the module.
  152. */
  153. function redirect_update_3() {
  154. db_update('system')
  155. ->fields(array('bootstrap' => 1))
  156. ->condition('type', 'module')
  157. ->condition('name', 'redirect')
  158. ->execute();
  159. }
  160. /**
  161. * Change empty redirect types to 'redirect'.
  162. */
  163. function redirect_update_4() {
  164. db_update('redirect')
  165. ->fields(array('type' => 'redirect'))
  166. ->condition('type', '')
  167. ->execute();
  168. }
  169. /**
  170. * Rename {redirect}.last_used to {redirect}.access.
  171. */
  172. function redirect_update_5() {
  173. if (db_field_exists('redirect', 'last_used')) {
  174. db_drop_index('redirect', 'expires');
  175. db_change_field('redirect', 'last_used', 'access', array(
  176. 'type' => 'int',
  177. 'unsigned' => TRUE,
  178. 'not null' => TRUE,
  179. 'default' => 0,
  180. 'description' => 'The timestamp of when the redirect was last accessed.',
  181. ));
  182. db_add_index('redirect', 'expires', array('type', 'access'));
  183. }
  184. }
  185. /**
  186. * Add an index on the source and language columns in the redirect table.
  187. */
  188. function redirect_update_6() {
  189. if (!db_index_exists('redirect', 'source_language')) {
  190. db_add_index('redirect', 'source_language', array('source', 'language'));
  191. }
  192. }
  193. /**
  194. * Migrate data and variables from the Drupal 6 path_redirect module.
  195. */
  196. function redirect_update_7000(&$sandbox) {
  197. if (!isset($sandbox['progress']) && db_table_exists('path_redirect')) {
  198. $sandbox['progress'] = 0;
  199. $sandbox['current_rid'] = 0;
  200. $sandbox['max'] = db_query('SELECT COUNT(rid) FROM {path_redirect}')->fetchField();
  201. $sandbox['skipped'] = array();
  202. }
  203. if (empty($sandbox['max'])) {
  204. $sandbox['#finished'] = 1;
  205. return t('No redirects to migrate.');
  206. }
  207. // Ensure the redirect module is loaded since we need to use its functions.
  208. drupal_load('module', 'redirect');
  209. $query = db_query_range("SELECT * FROM {path_redirect} WHERE rid > :rid ORDER BY rid", 0, 25, array(':rid' => $sandbox['current_rid']));
  210. foreach ($query as $old_redirect) {
  211. $redirect = _redirect_migrate_path_redirect_redirect($old_redirect);
  212. if (empty($redirect->success)) {
  213. $sandbox['skipped'][$old_redirect->rid] = t('RID @rid: @from to @to', array(
  214. '@rid' => $old_redirect->rid,
  215. '@from' => redirect_url($redirect->source, $redirect->source_options),
  216. '@to' => redirect_url($redirect->redirect, $redirect->redirect_options),
  217. ));
  218. }
  219. $sandbox['progress']++;
  220. $sandbox['current_rid'] = $old_redirect->rid;
  221. }
  222. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  223. if ($sandbox['#finished'] >= 1) {
  224. // Once finished, drop the old table.
  225. db_drop_table('path_redirect');
  226. // Migrate variables.
  227. _redirect_migrate_path_redirect_variables();
  228. // Remove the path_redirect entry from the system table.
  229. db_delete('system')
  230. ->condition('name', 'path_redirect')
  231. ->execute();
  232. // Show a message about how many redirects were migrated, and how many
  233. // were skipped.
  234. $skipped = count($sandbox['skipped']);
  235. $migrated = $sandbox['progress'] - $skipped;
  236. // @todo The following strings should be using t().
  237. $return = "Migrated $migrated redirects.";
  238. if (!empty($sandbox['skipped'])) {
  239. $return .= " The following $skipped redirects were not migrated since there were already existing redirects for the path and language combination:" . theme('item_list', array('items' => $sandbox['skipped']));
  240. }
  241. return $return;
  242. }
  243. }
  244. /**
  245. * Migrate a path redirect redirect to a redirect redirect.
  246. */
  247. function _redirect_migrate_path_redirect_redirect($old_redirect) {
  248. $redirect = new stdClass();
  249. redirect_object_prepare($redirect);
  250. $source_parsed = redirect_parse_url($old_redirect->source);
  251. $redirect->source = $source_parsed['url'];
  252. if (!empty($source_parsed['query'])) {
  253. $redirect->source_options['query'] = $source_parsed['query'];
  254. }
  255. $redirect_parsed = redirect_parse_url($old_redirect->redirect) + array('query' => drupal_get_query_array($old_redirect->query), 'fragment' => $old_redirect->fragment);
  256. $redirect->redirect = $redirect_parsed['url'];
  257. if (!empty($redirect_parsed['query'])) {
  258. $redirect->redirect_options['query'] = $redirect_parsed['query'];
  259. }
  260. if (!empty($redirect_parsed['fragment'])) {
  261. $redirect->redirect_options['fragment'] = $redirect_parsed['fragment'];
  262. }
  263. if (!empty($redirect_parsed['https'])) {
  264. $redirect->redirect_options['https'] = TRUE;
  265. }
  266. // Make sure empty language codes get migrated to use the new constant.
  267. $redirect->language = empty($old_redirect->language) ? LANGUAGE_NONE : $old_redirect->language;
  268. // Default status codes get a value of 0.
  269. if ($old_redirect->type != variable_get('redirect_default_status_code', 301)) {
  270. $redirect->status_code = (int) $old_redirect->type;
  271. }
  272. redirect_hash($redirect);
  273. if (redirect_load_by_hash($redirect->hash)) {
  274. // If a redirect with the same hash already exists, then it needs to be
  275. // skipped.
  276. $redirect->success = FALSE;
  277. }
  278. else {
  279. // Add the redirect to the database.
  280. db_insert('redirect')
  281. ->fields(array(
  282. 'hash' => $redirect->hash,
  283. 'type' => 'redirect',
  284. 'uid' => 1,
  285. 'source' => $redirect->source,
  286. 'source_options' => serialize($redirect->source_options),
  287. 'redirect' => $redirect->redirect,
  288. 'redirect_options' => serialize($redirect->redirect_options),
  289. 'language' => $redirect->language,
  290. 'status_code' => $redirect->status_code,
  291. 'count' => 0,
  292. 'access' => $old_redirect->last_used,
  293. ))
  294. ->execute();
  295. // Migrating this redirect succeeded.
  296. $redirect->success = TRUE;
  297. }
  298. return $redirect;
  299. }
  300. /**
  301. * Migrate path redirect variables to redirect variables.
  302. */
  303. function _redirect_migrate_path_redirect_variables() {
  304. // Port path_redirect variables.
  305. $variables = array(
  306. 'path_redirect_default_status' => 'redirect_default_status_code',
  307. 'path_redirect_purge_inactive' => 'redirect_purge_inactive',
  308. 'path_redirect_retain_query_string' => 'redirect_passthrough_querystring',
  309. 'path_redirect_auto_redirect' => 'redirect_auto_redirect',
  310. 'path_redirect_redirect_warning' => 'redirect_warning',
  311. 'path_redirect_allow_bypass' => NULL,
  312. );
  313. foreach ($variables as $old_variable => $new_variable) {
  314. if (!empty($new_variable)) {
  315. $old_value = variable_get($old_variable);
  316. $new_value = variable_get($new_variable);
  317. // Only if the old variable exists, and the new variable hasn't been set
  318. // yet, do we set the new variable with the old value.
  319. if (isset($old_value) && !isset($new_value)) {
  320. variable_set($new_variable, $old_value);
  321. }
  322. }
  323. // Delete the old path redirect variable.
  324. variable_del($old_variable);
  325. }
  326. }