uuid.install 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the uuid module.
  5. */
  6. define('UUID_UPGRADE_VAR', 'uuid_upgrade_in_progress');
  7. /**
  8. * Include some helper functions for the Entity API.
  9. */
  10. module_load_include('inc', 'uuid', 'uuid.entity');
  11. /**
  12. * Helper function that returns a schema field definition for UUID fields.
  13. *
  14. * @see uuid_schema_alter()
  15. * @see uuid_install()
  16. */
  17. function uuid_schema_field_definition() {
  18. return array(
  19. 'type' => 'char',
  20. 'length' => 36,
  21. 'not null' => TRUE,
  22. 'default' => '',
  23. 'description' => 'The Universally Unique Identifier.',
  24. );
  25. }
  26. /**
  27. * Implements hook_schema_alter().
  28. */
  29. function uuid_schema_alter(array &$schema) {
  30. $field_info = uuid_schema_field_definition();
  31. $key_names = array(
  32. 'base table' => 'uuid',
  33. 'revision table' => 'revision uuid',
  34. );
  35. foreach (uuid_get_core_entity_info() as $entity_info) {
  36. foreach ($key_names as $table_type => $key_name) {
  37. if (isset($entity_info[$table_type], $entity_info['entity keys'][$key_name])) {
  38. $field_name = $entity_info['entity keys'][$key_name];
  39. $properties = array(
  40. 'fields' => $field_info,
  41. 'indexes' => array($field_name),
  42. );
  43. foreach ($properties as $property => $value) {
  44. $schema[$entity_info[$table_type]][$property][$field_name] = $value;
  45. }
  46. }
  47. }
  48. }
  49. }
  50. /**
  51. * Implements hook_install().
  52. */
  53. function uuid_install() {
  54. _uuid_install_uuid_fields();
  55. module_load_include('inc', 'uuid');
  56. uuid_sync_all();
  57. }
  58. /**
  59. * Install the uuid and vuuid fields for Drupal core entity tables where needed.
  60. *
  61. * IMPORTANT: This function is called both at install and update time. If this
  62. * method is modified to add additional fields in the future, the update
  63. * strategy must be considered. See the comment in uuid_update_7102.
  64. */
  65. function _uuid_install_uuid_fields() {
  66. $field = uuid_schema_field_definition();
  67. foreach (uuid_get_core_entity_info() as $info) {
  68. if (!db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
  69. db_add_field($info['base table'], $info['entity keys']['uuid'], $field);
  70. db_add_index($info['base table'], $info['entity keys']['uuid'], array($info['entity keys']['uuid']));
  71. }
  72. if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
  73. if (!db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
  74. db_add_field($info['revision table'], $info['entity keys']['revision uuid'], $field);
  75. db_add_index($info['revision table'], $info['entity keys']['revision uuid'], array($info['entity keys']['revision uuid']));
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Implements hook_uninstall().
  82. */
  83. function uuid_uninstall() {
  84. foreach (uuid_get_core_entity_info() as $info) {
  85. if (db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
  86. db_drop_field($info['base table'], $info['entity keys']['uuid']);
  87. db_drop_index($info['base table'], $info['entity keys']['uuid']);
  88. }
  89. if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
  90. if (db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
  91. db_drop_field($info['revision table'], $info['entity keys']['revision uuid']);
  92. db_drop_index($info['revision table'], $info['entity keys']['revision uuid']);
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * Implements hook_modules_installed().
  99. */
  100. function uuid_modules_installed($modules) {
  101. // Run the installation hook. This makes sure that the schema for all
  102. // supported core entity types is set correct.
  103. uuid_install();
  104. }
  105. /**
  106. * Create uuid_vocabulary and uuid_term_data tables.
  107. */
  108. function uuid_update_6001() {
  109. $ret = array();
  110. db_create_table($ret, 'uuid_vocabulary', uuid_table_schema('vocabulary', 'vid'));
  111. db_create_table($ret, 'uuid_term_data', uuid_table_schema('term_data', 'tid'));
  112. return $ret;
  113. }
  114. /**
  115. * Make all uuid columns unique keys instead of indexes.
  116. */
  117. function uuid_update_6002() {
  118. $ret = array();
  119. foreach (uuid_schema() as $table => $schema) {
  120. db_drop_index($ret, $table, $table . '_uuid_idx');
  121. db_add_unique_key($ret, $table, $table . '_uuid_key', array('uuid'));
  122. }
  123. return $ret;
  124. }
  125. /**
  126. * Create uuid_comment table.
  127. */
  128. function uuid_update_6003() {
  129. $ret = array();
  130. db_create_table($ret, 'uuid_comments', uuid_table_schema('comments', 'cid'));
  131. return $ret;
  132. }
  133. /**
  134. * Change column definitions for uuid columns to more efficient char spec.
  135. */
  136. function uuid_update_6004() {
  137. $ret = array();
  138. // Use what's in uuid_table_schema in order to be consistent.
  139. $tables = uuid_schema();
  140. $spec = $tables['uuid_node']['fields']['uuid'];
  141. foreach ($tables as $tablename => $schema) {
  142. if (db_table_exists($tablename)) {
  143. db_change_field($ret, $tablename, 'uuid', 'uuid', $spec);
  144. }
  145. }
  146. return $ret;
  147. }
  148. /**
  149. * Support deleting node revision.
  150. *
  151. * Modify existing uuid_node_revisions table to support revision deletion, and
  152. * add in as much legacy data as possible.
  153. */
  154. function uuid_update_6005() {
  155. $ret = array();
  156. if (db_table_exists('uuid_node_revisions')) {
  157. // Use what's already defined in uuid schema in order to be consistent.
  158. $schema = uuid_schema();
  159. $spec = $schema['uuid_node_revisions']['fields']['nid'];
  160. db_add_field($ret, 'uuid_node_revisions', 'nid', $spec);
  161. // Add node ids to the new column, for revisions that exist, but now have a
  162. // default value of 0 in uuid_node_revisions.
  163. $result = db_query('SELECT nr.nid, nr.vid FROM {node_revisions} AS nr LEFT JOIN {uuid_node_revisions} AS unr ON unr.vid=nr.vid WHERE unr.nid=%d', 0);
  164. while ($item = db_fetch_object($result)) {
  165. $ret[] = update_sql('UPDATE {uuid_node_revisions} SET nid=' . (int) $item->nid . ' WHERE vid=' . (int) $item->vid);
  166. }
  167. // Add uuid_node_revision rows for rows that don't exist, but should.
  168. $result = db_query('SELECT nr.nid, nr.vid FROM {node_revisions} AS nr LEFT JOIN {uuid_node_revisions} AS unr ON unr.vid=nr.vid WHERE unr.nid IS NULL');
  169. while ($item = db_fetch_object($result)) {
  170. $ret[] = update_sql(sprintf("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES(%d, '%s', %d)", $item->vid, uuid_uuid(), $item->nid));
  171. }
  172. // Delete any orphaned revision vid, uuid pairs.
  173. $ret[] = update_sql('DELETE FROM {uuid_node_revisions} WHERE nid=0');
  174. }
  175. return $ret;
  176. }
  177. /**
  178. * Remove old variables.
  179. */
  180. function uuid_update_7100() {
  181. $types = array(
  182. 'nodes',
  183. 'users',
  184. 'taxonomy',
  185. 'comments',
  186. );
  187. foreach ($types as $type) {
  188. variable_del('uuid_automatic_for_' . $type);
  189. }
  190. return array();
  191. }
  192. /**
  193. * Clear cache for installations that used alpha1.
  194. *
  195. * Modules previously enabled in uuid_update_7100() don't exist any more. We
  196. * need to clear the cache so Drupal detects this change.
  197. */
  198. function uuid_update_7101() {
  199. drupal_flush_all_caches();
  200. }
  201. /**
  202. * Ensure that the uuid and vuuid fields are added where needed.
  203. *
  204. * Note that update 7102 calls _uuid_install_uuid_fields(), which is an
  205. * idempotent function. If _uuid_install_uuid_fields() is changed at some
  206. * point in the future (but remains idempotent), then some uuid users
  207. * will have run update 7102, and some will not. A new uuid_update_7103()
  208. * function would would therefore be necessary to update all users to
  209. * the latest schema. At the same time, uuid_update_7102() could become
  210. * an empty function, as it would not be necessary to call
  211. * _uuid_install_uuid_fields() twice.
  212. */
  213. function uuid_update_7102() {
  214. // If the user have disabled the UUID module during upgrade (as UPGRADE.txt
  215. // instructs), some functions will be missing. So include the module file.
  216. module_load_include('module', 'uuid', 'uuid');
  217. _uuid_install_uuid_fields();
  218. uuid_sync_all();
  219. }
  220. /**
  221. * Clean up entities created by uuid_default_entities_example module.
  222. *
  223. * Modify the labels of all example entities created by the now removed
  224. * uuid_default_entities_example.module to make it clear they're examples. Also
  225. * remove the administrator role of any example user.
  226. */
  227. function uuid_update_7103() {
  228. // These are UUIDs of all the example entities that might exist after having
  229. // installed uuid_default_entities_example.module.
  230. $info = entity_get_info();
  231. $uuids = array(
  232. 'node' => array(
  233. 'b0558664-c94b-3674-d9df-3e1696b2e471',
  234. '5e3d8bbe-a1f2-f2d4-fdc0-71e6c23aa837',
  235. ),
  236. 'user' => array(
  237. '7cf875e6-dc15-4404-f190-5a7c3e91d14c',
  238. ),
  239. );
  240. // We can't assume taxonomy is enabled.
  241. if (isset($info['taxonomy_term'])) {
  242. $uuids['taxonomy_term'] = array(
  243. 'bcb92ce8-2236-e264-65c8-0c163ae716d1',
  244. '4293a15c-531a-6164-7d1b-668ed019a6bd',
  245. 'af738a46-f278-cf84-d94d-9e03879fd71e',
  246. );
  247. }
  248. foreach (array_keys($uuids) as $entity_type) {
  249. $info = entity_get_info($entity_type);
  250. $entity_ids = entity_get_id_by_uuid($entity_type, $uuids[$entity_type]);
  251. $entities = entity_load($entity_type, $entity_ids);
  252. foreach ($entities as $entity) {
  253. // Update the label to make it clear this is example content.
  254. $entity->{$info['entity keys']['label']} = $entity->{$info['entity keys']['label']} . ' (UUID example)';
  255. // Remove the administrator role from any user.
  256. if ($entity_type == 'user' && $rid = array_search('administrator', $entity->roles)) {
  257. unset($entity->roles[$rid]);
  258. }
  259. entity_save($entity_type, $entity);
  260. }
  261. }
  262. }