redirect.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. 'status' => array(
  90. 'type' => 'int',
  91. 'size' => 'small',
  92. 'not null' => TRUE,
  93. 'default' => 1,
  94. 'description' => 'Boolean indicating whether the redirect is enabled (visible to non-administrators).',
  95. ),
  96. ),
  97. 'primary key' => array('rid'),
  98. 'unique keys' => array(
  99. 'hash' => array('hash'),
  100. ),
  101. 'indexes' => array(
  102. 'expires' => array('type', 'access'),
  103. 'status_source_language' => array(
  104. 'status',
  105. 'source',
  106. 'language',
  107. ),
  108. ),
  109. );
  110. return $schema;
  111. }
  112. /**
  113. * Implements hook_install().
  114. */
  115. function redirect_install() {
  116. // If the path redirect table exists, then set the schema to run the
  117. // migration update function.
  118. if (db_table_exists('path_redirect')) {
  119. drupal_set_installed_schema_version('redirect', 6999);
  120. }
  121. }
  122. /**
  123. * Implements hook_uninstall().
  124. */
  125. function redirect_uninstall() {
  126. drupal_load('module', 'redirect');
  127. $variables = array_keys(redirect_variables());
  128. foreach ($variables as $variable) {
  129. variable_del($variable);
  130. }
  131. }
  132. /**
  133. * Add the {redirect}.count field.
  134. */
  135. function redirect_update_1() {
  136. $field = array(
  137. 'type' => 'int',
  138. 'unsigned' => TRUE,
  139. 'not null' => TRUE,
  140. 'default' => 0,
  141. 'description' => 'The number of times the redirect has been used.',
  142. );
  143. db_add_field('redirect', 'count', $field);
  144. }
  145. /**
  146. * Add the {redirect}.uid field.
  147. */
  148. function redirect_update_2() {
  149. $field = array(
  150. 'type' => 'int',
  151. 'unsigned' => TRUE,
  152. 'not null' => TRUE,
  153. 'default' => 0,
  154. 'description' => 'The {users}.uid of the user who created the redirect.',
  155. );
  156. db_add_field('redirect', 'uid', $field);
  157. db_update('redirect')
  158. ->fields(array('uid' => 1))
  159. ->execute();
  160. }
  161. /**
  162. * Enable bootstrap status for the module.
  163. */
  164. function redirect_update_3() {
  165. db_update('system')
  166. ->fields(array('bootstrap' => 1))
  167. ->condition('type', 'module')
  168. ->condition('name', 'redirect')
  169. ->execute();
  170. }
  171. /**
  172. * Change empty redirect types to 'redirect'.
  173. */
  174. function redirect_update_4() {
  175. db_update('redirect')
  176. ->fields(array('type' => 'redirect'))
  177. ->condition('type', '')
  178. ->execute();
  179. }
  180. /**
  181. * Rename {redirect}.last_used to {redirect}.access.
  182. */
  183. function redirect_update_5() {
  184. if (db_field_exists('redirect', 'last_used')) {
  185. db_drop_index('redirect', 'expires');
  186. db_change_field('redirect', 'last_used', 'access', array(
  187. 'type' => 'int',
  188. 'unsigned' => TRUE,
  189. 'not null' => TRUE,
  190. 'default' => 0,
  191. 'description' => 'The timestamp of when the redirect was last accessed.',
  192. ));
  193. db_add_index('redirect', 'expires', array('type', 'access'));
  194. }
  195. }
  196. /**
  197. * Add an index on the source and language columns in the redirect table.
  198. */
  199. function redirect_update_6() {
  200. if (!db_index_exists('redirect', 'source_language')) {
  201. db_add_index('redirect', 'source_language', array('source', 'language'));
  202. }
  203. }
  204. /**
  205. * Migrate data and variables from the Drupal 6 path_redirect module.
  206. */
  207. function redirect_update_7000(&$sandbox) {
  208. if (!isset($sandbox['progress']) && db_table_exists('path_redirect')) {
  209. $sandbox['progress'] = 0;
  210. $sandbox['current_rid'] = 0;
  211. $sandbox['max'] = db_query('SELECT COUNT(rid) FROM {path_redirect}')->fetchField();
  212. $sandbox['skipped'] = array();
  213. }
  214. if (empty($sandbox['max'])) {
  215. $sandbox['#finished'] = 1;
  216. return t('No redirects to migrate.');
  217. }
  218. // Ensure the redirect module is loaded since we need to use its functions.
  219. drupal_load('module', 'redirect');
  220. $query = db_query_range("SELECT * FROM {path_redirect} WHERE rid > :rid ORDER BY rid", 0, 25, array(':rid' => $sandbox['current_rid']));
  221. foreach ($query as $old_redirect) {
  222. $redirect = _redirect_migrate_path_redirect_redirect($old_redirect);
  223. if (empty($redirect->success)) {
  224. $sandbox['skipped'][$old_redirect->rid] = t('RID @rid: @from to @to', array(
  225. '@rid' => $old_redirect->rid,
  226. '@from' => redirect_url($redirect->source, $redirect->source_options),
  227. '@to' => redirect_url($redirect->redirect, $redirect->redirect_options),
  228. ));
  229. }
  230. $sandbox['progress']++;
  231. $sandbox['current_rid'] = $old_redirect->rid;
  232. }
  233. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  234. if ($sandbox['#finished'] >= 1) {
  235. // Once finished, drop the old table.
  236. db_drop_table('path_redirect');
  237. // Migrate variables.
  238. _redirect_migrate_path_redirect_variables();
  239. // Remove the path_redirect entry from the system table.
  240. db_delete('system')
  241. ->condition('name', 'path_redirect')
  242. ->execute();
  243. // Show a message about how many redirects were migrated, and how many
  244. // were skipped.
  245. $skipped = count($sandbox['skipped']);
  246. $migrated = $sandbox['progress'] - $skipped;
  247. // @todo The following strings should be using t().
  248. $return = "Migrated $migrated redirects.";
  249. if (!empty($sandbox['skipped'])) {
  250. $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']));
  251. }
  252. return $return;
  253. }
  254. }
  255. /**
  256. * Rebuild the registry and clear the entity info cache.
  257. */
  258. function redirect_update_7100() {
  259. if (!class_exists('RedirectController')) {
  260. registry_rebuild();
  261. entity_info_cache_clear();
  262. }
  263. }
  264. /**
  265. * Add status field.
  266. */
  267. function redirect_update_7101() {
  268. db_add_field('redirect', 'status', array(
  269. 'type' => 'int',
  270. 'size' => 'small',
  271. 'not null' => TRUE,
  272. 'default' => 1,
  273. 'description' => 'Boolean indicating whether the redirect is enabled (visible to non-administrators).',
  274. ));
  275. db_drop_index('redirect', 'source_language');
  276. db_add_index('redirect', 'status_source_language', array(
  277. 'status',
  278. 'source',
  279. 'language',
  280. ));
  281. }
  282. /**
  283. * Disable redirects that could cause infinite loops.
  284. */
  285. function redirect_update_7102() {
  286. $rids = db_query("SELECT r.rid FROM {redirect} r INNER JOIN {url_alias} u ON r.source = u.alias AND r.redirect = u.source AND r.language = u.language")->fetchCol();
  287. if ($rids) {
  288. // Disable redirects
  289. $count = db_update('redirect')
  290. ->fields(array('status' => 0))
  291. ->condition('rid', $rids)
  292. ->execute();
  293. $disabled_redirects_message = format_plural($count,
  294. '1 circular redirect causing infinite loop was disabled.',
  295. '@count circular redirects causing infinite loop were disabled.');
  296. return $disabled_redirects_message;
  297. }
  298. else {
  299. return t('No circular redirects were found that could cause infinite loops.');
  300. }
  301. }
  302. /**
  303. * Migrate a path redirect redirect to a redirect redirect.
  304. */
  305. function _redirect_migrate_path_redirect_redirect($old_redirect) {
  306. $redirect = new stdClass();
  307. redirect_object_prepare($redirect);
  308. $source_parsed = redirect_parse_url($old_redirect->source);
  309. $redirect->source = $source_parsed['url'];
  310. if (!empty($source_parsed['query'])) {
  311. $redirect->source_options['query'] = $source_parsed['query'];
  312. }
  313. $redirect_parsed = redirect_parse_url($old_redirect->redirect) + array('query' => drupal_get_query_array($old_redirect->query), 'fragment' => $old_redirect->fragment);
  314. $redirect->redirect = $redirect_parsed['url'];
  315. if (!empty($redirect_parsed['query'])) {
  316. $redirect->redirect_options['query'] = $redirect_parsed['query'];
  317. }
  318. if (!empty($redirect_parsed['fragment'])) {
  319. $redirect->redirect_options['fragment'] = $redirect_parsed['fragment'];
  320. }
  321. if (!empty($redirect_parsed['https'])) {
  322. $redirect->redirect_options['https'] = TRUE;
  323. }
  324. // Make sure empty language codes get migrated to use the new constant.
  325. $redirect->language = empty($old_redirect->language) ? LANGUAGE_NONE : $old_redirect->language;
  326. // Default status codes get a value of 0.
  327. if ($old_redirect->type != variable_get('redirect_default_status_code', 301)) {
  328. $redirect->status_code = (int) $old_redirect->type;
  329. }
  330. redirect_hash($redirect);
  331. if (redirect_load_by_hash($redirect->hash)) {
  332. // If a redirect with the same hash already exists, then it needs to be
  333. // skipped.
  334. $redirect->success = FALSE;
  335. }
  336. else {
  337. // Add the redirect to the database.
  338. db_insert('redirect')
  339. ->fields(array(
  340. 'hash' => $redirect->hash,
  341. 'type' => 'redirect',
  342. 'uid' => 1,
  343. 'source' => $redirect->source,
  344. 'source_options' => serialize($redirect->source_options),
  345. 'redirect' => $redirect->redirect,
  346. 'redirect_options' => serialize($redirect->redirect_options),
  347. 'language' => $redirect->language,
  348. 'status_code' => $redirect->status_code,
  349. 'status' => 1,
  350. 'count' => 0,
  351. 'access' => $old_redirect->last_used,
  352. ))
  353. ->execute();
  354. // Migrating this redirect succeeded.
  355. $redirect->success = TRUE;
  356. }
  357. return $redirect;
  358. }
  359. /**
  360. * Migrate path redirect variables to redirect variables.
  361. */
  362. function _redirect_migrate_path_redirect_variables() {
  363. // Port path_redirect variables.
  364. $variables = array(
  365. 'path_redirect_default_status' => 'redirect_default_status_code',
  366. 'path_redirect_purge_inactive' => 'redirect_purge_inactive',
  367. 'path_redirect_retain_query_string' => 'redirect_passthrough_querystring',
  368. 'path_redirect_auto_redirect' => 'redirect_auto_redirect',
  369. 'path_redirect_redirect_warning' => 'redirect_warning',
  370. 'path_redirect_allow_bypass' => NULL,
  371. );
  372. foreach ($variables as $old_variable => $new_variable) {
  373. if (!empty($new_variable)) {
  374. $old_value = variable_get($old_variable);
  375. $new_value = variable_get($new_variable);
  376. // Only if the old variable exists, and the new variable hasn't been set
  377. // yet, do we set the new variable with the old value.
  378. if (isset($old_value) && !isset($new_value)) {
  379. variable_set($new_variable, $old_value);
  380. }
  381. }
  382. // Delete the old path redirect variable.
  383. variable_del($old_variable);
  384. }
  385. }