pathauto.install 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for Pathauto.
  5. *
  6. * @ingroup pathauto
  7. */
  8. /**
  9. * Implements hook_schema().
  10. */
  11. function pathauto_schema() {
  12. $schema['pathauto_state'] = array(
  13. 'description' => 'The status of each entity alias (whether it was automatically generated or not).',
  14. 'fields' => array(
  15. 'entity_type' => array(
  16. 'type' => 'varchar',
  17. 'length' => 32,
  18. 'not null' => TRUE,
  19. 'description' => 'An entity type.',
  20. ),
  21. 'entity_id' => array(
  22. 'type' => 'int',
  23. 'unsigned' => TRUE,
  24. 'not null' => TRUE,
  25. 'description' => 'An entity ID.',
  26. ),
  27. 'pathauto' => array(
  28. 'type' => 'int',
  29. 'size' => 'tiny',
  30. 'not null' => TRUE,
  31. 'default' => 0,
  32. 'description' => 'The automatic alias status of the entity.',
  33. ),
  34. ),
  35. 'primary key' => array('entity_type', 'entity_id'),
  36. );
  37. return $schema;
  38. }
  39. /**
  40. * Implements hook_install().
  41. */
  42. function pathauto_install() {
  43. // Set some default variables necessary for the module to perform.
  44. $defaults = array(
  45. 'pathauto_node_pattern' => 'content/[node:title]',
  46. 'pathauto_taxonomy_term_pattern' => '[term:vocabulary]/[term:name]',
  47. 'pathauto_forum_pattern' => '[term:vocabulary]/[term:name]',
  48. 'pathauto_user_pattern' => 'users/[user:name]',
  49. 'pathauto_blog_pattern' => 'blogs/[user:name]',
  50. // Set hyphen character to replace instead of remove.
  51. 'pathauto_punctuation_hyphen' => 1,
  52. );
  53. foreach ($defaults as $variable => $default) {
  54. if (variable_get($variable) === NULL) {
  55. variable_set($variable, $default);
  56. }
  57. }
  58. // Set the weight to 1
  59. db_update('system')
  60. ->fields(array('weight' => 1))
  61. ->condition('type', 'module')
  62. ->condition('name', 'pathauto')
  63. ->execute();
  64. }
  65. /**
  66. * Implements hook_uninstall().
  67. */
  68. function pathauto_uninstall() {
  69. // Delete all the pathauto variables and then clear the variable cache.
  70. db_query("DELETE FROM {variable} WHERE name LIKE 'pathauto_%'");
  71. cache_clear_all('variables', 'cache');
  72. }
  73. /**
  74. * Implements hook_requirements().
  75. */
  76. function pathauto_requirements($phase) {
  77. $requirements = array();
  78. $t = get_t();
  79. if ($phase == 'runtime' && module_exists('pathauto_persist')) {
  80. $requirements['pathauto'] = array(
  81. 'title' => $t('Pathauto Persist'),
  82. 'value' => $t('Enabled'),
  83. 'description' => $t('Pathauto Persist is installed and enabled. As Pathauto Persist has been merged into Pathauto, the Pathauto Persist module can be safely disabled and removed. All Pathauto Persist settings have been migrated to the Pathauto implementation.'),
  84. 'severity' => REQUIREMENT_INFO,
  85. );
  86. }
  87. return $requirements;
  88. }
  89. /**
  90. * Remove the unsupported user/%/contact and user/%/tracker pattern variables.
  91. */
  92. function pathauto_update_6200() {
  93. variable_del('pathauto_contact_bulkupdate');
  94. variable_del('pathauto_contact_pattern');
  95. variable_del('pathauto_contact_supportsfeeds');
  96. variable_del('pathauto_contact_applytofeeds');
  97. variable_del('pathauto_tracker_bulkupdate');
  98. variable_del('pathauto_tracker_pattern');
  99. variable_del('pathauto_tracker_supportsfeeds');
  100. variable_del('pathauto_tracker_applytofeeds');
  101. }
  102. /**
  103. * Empty update since it is handled by pathauto_update_7000().
  104. */
  105. function pathauto_update_6201() {
  106. }
  107. /**
  108. * Empty update since it is handled by pathauto_update_7004().
  109. */
  110. function pathauto_update_6202() {
  111. }
  112. /**
  113. * Remove obsolete variables since batch API is now used.
  114. */
  115. function pathauto_update_7000() {
  116. variable_del('pathauto_max_bulk_update');
  117. variable_del('pathauto_node_bulkupdate');
  118. variable_del('pathauto_taxonomy_bulkupdate');
  119. variable_del('pathauto_forum_bulkupdate');
  120. variable_del('pathauto_user_bulkupdate');
  121. variable_del('pathauto_blog_bulkupdate');
  122. variable_del('pathauto_modulelist');
  123. variable_del('pathauto_indexaliases');
  124. variable_del('pathauto_indexaliases_bulkupdate');
  125. }
  126. /**
  127. * Empty update since feed paths are no longer supported.
  128. */
  129. function pathauto_update_7001() {
  130. }
  131. /**
  132. * Update pathauto_taxonomy_[vid]_pattern variables to pathauto_taxonomy_[machinename]_pattern.
  133. */
  134. function pathauto_update_7002() {
  135. if (module_exists('taxonomy')) {
  136. $vocabularies = taxonomy_get_vocabularies();
  137. foreach ($vocabularies as $vid => $vocabulary) {
  138. if ($vid == variable_get('forum_nav_vocabulary', '')) {
  139. // Skip the forum vocabulary.
  140. continue;
  141. }
  142. if ($pattern = variable_get('pathauto_taxonomy_' . $vid . '_pattern', '')) {
  143. variable_set('pathauto_taxonomy_' . $vocabulary->machine_name . '_pattern', $pattern);
  144. }
  145. variable_del('pathauto_taxonomy_' . $vid . '_pattern');
  146. }
  147. }
  148. }
  149. /**
  150. * Rename 'taxonomy' variables to use the entity type 'taxonomy_term'.
  151. */
  152. function pathauto_update_7003() {
  153. $variables = db_select('variable', 'v')
  154. ->fields('v', array('name'))
  155. ->condition(db_and()
  156. ->condition('name', db_like("pathauto_taxonomy_") . '%', 'LIKE')
  157. ->condition('name', db_like("pathauto_taxonomy_term_") . '%', 'NOT LIKE')
  158. )
  159. ->execute()
  160. ->fetchCol();
  161. foreach ($variables as $variable) {
  162. $value = variable_get($variable);
  163. variable_del($variable);
  164. $variable = strtr($variable, array('pathauto_taxonomy_' => 'pathauto_taxonomy_term_'));
  165. variable_set($variable, $value);
  166. }
  167. }
  168. /**
  169. * Remove obsolete variables for removed feed handling.
  170. */
  171. function pathauto_update_7004() {
  172. variable_del('pathauto_node_supportsfeeds');
  173. variable_del('pathauto_node_applytofeeds');
  174. variable_del('pathauto_taxonomy_supportsfeeds');
  175. variable_del('pathauto_taxonomy_applytofeeds');
  176. variable_del('pathauto_forum_supportsfeeds');
  177. variable_del('pathauto_forum_applytofeeds');
  178. variable_del('pathauto_user_supportsfeeds');
  179. variable_del('pathauto_user_applytofeeds');
  180. variable_del('pathauto_blog_supportsfeeds');
  181. variable_del('pathauto_blog_applytofeeds');
  182. }
  183. /**
  184. * Fix original incorrect tokens in taxonomy and forum patterns.
  185. */
  186. function pathauto_update_7005() {
  187. $replacements = array(
  188. '[vocabulary:name]' => '[term:vocabulary]',
  189. '[vocabulary:' => '[term:vocabulary:',
  190. '[term:catpath]' => '[term:name]',
  191. '[term:path]' => '[term:name]',
  192. );
  193. $variables = db_select('variable', 'v')
  194. ->fields('v', array('name'))
  195. ->condition(db_or()
  196. ->condition('name', db_like("pathauto_taxonomy_term_") . '%' . db_like('pattern'), 'LIKE')
  197. ->condition('name', db_like("pathauto_forum_") . '%' . db_like('pattern'), 'LIKE')
  198. )
  199. ->execute()
  200. ->fetchCol();
  201. foreach ($variables as $variable) {
  202. if ($pattern = variable_get($variable)) {
  203. $pattern = strtr($pattern, $replacements);
  204. variable_set($variable, $pattern);
  205. }
  206. }
  207. return 'Your Pathauto taxonomy and forum patterns have been corrected. You may wish to regenerate your taxonomy and forum term URL aliases.';
  208. }
  209. /**
  210. * Create pathauto_state table, using data from pathauto_persist if it exists.
  211. */
  212. function pathauto_update_7006() {
  213. if (!db_table_exists('pathauto_state')) {
  214. $schema['pathauto_state'] = array(
  215. 'description' => 'The status of each entity alias (whether it was automatically generated or not).',
  216. 'fields' => array(
  217. 'entity_type' => array(
  218. 'type' => 'varchar',
  219. 'length' => 32,
  220. 'not null' => TRUE,
  221. 'description' => 'The entity type.',
  222. ),
  223. 'entity_id' => array(
  224. 'type' => 'int',
  225. 'unsigned' => TRUE,
  226. 'not null' => TRUE,
  227. 'description' => 'The entity ID.',
  228. ),
  229. 'pathauto' => array(
  230. 'type' => 'int',
  231. 'size' => 'tiny',
  232. 'not null' => TRUE,
  233. 'default' => 0,
  234. 'description' => 'The automatic alias status of the entity.',
  235. ),
  236. ),
  237. 'primary key' => array('entity_type', 'entity_id'),
  238. );
  239. if (db_table_exists('pathauto_persist')) {
  240. // Rename pathauto_persist's table, then create a new empty one just so
  241. // that we can cleanly disable that module.
  242. db_rename_table('pathauto_persist', 'pathauto_state');
  243. db_create_table('pathauto_persist', $schema['pathauto_state']);
  244. // Disable the module and inform the user.
  245. if (module_exists('pathauto_persist')) {
  246. module_disable(array('pathauto_persist'));
  247. }
  248. return t('The Pathauto Persist module and all of its data has been merged into Pathauto. The Pathauto Persist module has been disabled and can be safely uninstalled.');
  249. }
  250. else {
  251. db_create_table('pathauto_state', $schema['pathauto_state']);
  252. }
  253. }
  254. }
  255. /**
  256. * Build a list of Drupal 6 tokens and their Drupal 7 token names.
  257. */
  258. function _pathauto_upgrade_token_list() {
  259. $tokens = array(
  260. //'catpath' => 'node:term-lowest:parent:path][node:term-lowest',
  261. //'catalias' => 'node:term-lowest:path',
  262. //'termpath' => 'term:parent:path][term:name',
  263. //'termalias' => 'term:url:alias',
  264. //'bookpathalias' => 'node:book:parent:path',
  265. );
  266. }