Project

General

Profile

Paste
Download (9.26 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / uuid / uuid.install @ bf6fb0ee

1
<?php
2

    
3
/**
4
 * @file
5
 * Install, update and uninstall functions for the uuid module.
6
 */
7

    
8
define('UUID_UPGRADE_VAR', 'uuid_upgrade_in_progress');
9

    
10
/**
11
 * Include some helper functions for the Entity API.
12
 */
13
module_load_include('inc', 'uuid', 'uuid.entity');
14

    
15
/**
16
 * Helper function that returns a schema field definition for UUID fields.
17
 *
18
 * @see uuid_schema_alter()
19
 * @see uuid_install()
20
 */
21
function uuid_schema_field_definition() {
22
  return array(
23
    'type' => 'char',
24
    'length' => 36,
25
    'not null' => TRUE,
26
    'default' => '',
27
    'description' => 'The Universally Unique Identifier.',
28
  );
29
}
30

    
31
/**
32
 * Implements hook_schema_alter().
33
 */
34
function uuid_schema_alter(array &$schema) {
35
  $field_info = uuid_schema_field_definition();
36
  $key_names = array(
37
    'base table' => 'uuid',
38
    'revision table' => 'revision uuid',
39
  );
40

    
41
  foreach (uuid_get_core_entity_info() as $entity_info) {
42
    foreach ($key_names as $table_type => $key_name) {
43
      if (isset($entity_info[$table_type], $entity_info['entity keys'][$key_name])) {
44
        $field_name = $entity_info['entity keys'][$key_name];
45
        $properties = array(
46
          'fields' => $field_info,
47
          'indexes' => array($field_name),
48
        );
49

    
50
        foreach ($properties as $property => $value) {
51
          $schema[$entity_info[$table_type]][$property][$field_name] = $value;
52
        }
53
      }
54
    }
55
  }
56
}
57

    
58
/**
59
 * Implements hook_install().
60
 */
61
function uuid_install() {
62
  _uuid_install_uuid_fields();
63
  module_load_include('inc', 'uuid');
64
  uuid_sync_all();
65
}
66

    
67
/**
68
 * Install the uuid and vuuid fields for Drupal core entity tables where needed.
69
 *
70
 * IMPORTANT:  This function is called both at install and update time.  If this
71
 * method is modified to add additional fields in the future, the update
72
 * strategy must be considered.  See the comment in uuid_update_7102.
73
 */
74
function _uuid_install_uuid_fields() {
75
  $field = uuid_schema_field_definition();
76
  foreach (uuid_get_core_entity_info() as $info) {
77
    if (!db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
78
      db_add_field($info['base table'], $info['entity keys']['uuid'], $field);
79
      db_add_index($info['base table'], $info['entity keys']['uuid'], array($info['entity keys']['uuid']));
80
    }
81
    if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
82
      if (!db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
83
        db_add_field($info['revision table'], $info['entity keys']['revision uuid'], $field);
84
        db_add_index($info['revision table'], $info['entity keys']['revision uuid'], array($info['entity keys']['revision uuid']));
85
      }
86
    }
87
  }
88
}
89

    
90
/**
91
 * Implements hook_uninstall().
92
 */
93
function uuid_uninstall() {
94
  foreach (uuid_get_core_entity_info() as $info) {
95
    if (db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
96
      db_drop_field($info['base table'], $info['entity keys']['uuid']);
97
      db_drop_index($info['base table'], $info['entity keys']['uuid']);
98
    }
99
    if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
100
      if (db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
101
        db_drop_field($info['revision table'], $info['entity keys']['revision uuid']);
102
        db_drop_index($info['revision table'], $info['entity keys']['revision uuid']);
103
      }
104
    }
105
  }
106
}
107

    
108
/**
109
 * Implements hook_modules_installed().
110
 */
111
function uuid_modules_installed($modules) {
112
  // Run the installation hook. This makes sure that the schema for all
113
  // supported core entity types is set correct.
114
  uuid_install();
115
}
116

    
117
/**
118
 * Create uuid_vocabulary and uuid_term_data tables.
119
 */
120
function uuid_update_6001() {
121
  $ret = array();
122

    
123
  db_create_table($ret, 'uuid_vocabulary', uuid_table_schema('vocabulary', 'vid'));
124
  db_create_table($ret, 'uuid_term_data', uuid_table_schema('term_data', 'tid'));
125

    
126
  return $ret;
127
}
128

    
129
/**
130
 * Make all uuid columns unique keys instead of indexes.
131
 */
132
function uuid_update_6002() {
133
  $ret = array();
134

    
135
  foreach (uuid_schema() as $table => $schema) {
136
    db_drop_index($ret, $table, $table . '_uuid_idx');
137
    db_add_unique_key($ret, $table, $table . '_uuid_key', array('uuid'));
138
  }
139

    
140
  return $ret;
141
}
142

    
143
/**
144
 * Create uuid_comment table.
145
 */
146
function uuid_update_6003() {
147
  $ret = array();
148

    
149
  db_create_table($ret, 'uuid_comments', uuid_table_schema('comments', 'cid'));
150

    
151
  return $ret;
152
}
153

    
154
/**
155
 * Change column definitions for uuid columns to more efficient char spec.
156
 */
157
function uuid_update_6004() {
158
  $ret = array();
159

    
160
  // Use what's in uuid_table_schema in order to be consistent.
161
  $tables = uuid_schema();
162
  $spec = $tables['uuid_node']['fields']['uuid'];
163

    
164
  foreach ($tables as $tablename => $schema) {
165
    if (db_table_exists($tablename)) {
166
      db_change_field($ret, $tablename, 'uuid', 'uuid', $spec);
167
    }
168
  }
169

    
170
  return $ret;
171
}
172

    
173
/**
174
 * Support deleting node revision.
175
 *
176
 * Modify existing uuid_node_revisions table to support revision deletion, and
177
 * add in as much legacy data as possible.
178
 */
179
function uuid_update_6005() {
180
  $ret = array();
181

    
182
  if (db_table_exists('uuid_node_revisions')) {
183
    // Use what's already defined in uuid schema in order to be consistent.
184
    $schema = uuid_schema();
185
    $spec = $schema['uuid_node_revisions']['fields']['nid'];
186
    db_add_field($ret, 'uuid_node_revisions', 'nid', $spec);
187
    // Add node ids to the new column, for revisions that exist, but now have a
188
    // default value of 0 in uuid_node_revisions.
189
    $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);
190
    while ($item = db_fetch_object($result)) {
191
      $ret[] = update_sql('UPDATE {uuid_node_revisions} SET nid=' . (int) $item->nid . ' WHERE vid=' . (int) $item->vid);
192
    }
193
    // Add uuid_node_revision rows for rows that don't exist, but should.
194
    $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');
195
    while ($item = db_fetch_object($result)) {
196
      $ret[] = update_sql(sprintf("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES(%d, '%s', %d)", $item->vid, uuid_uuid(), $item->nid));
197
    }
198
    // Delete any orphaned revision vid, uuid pairs.
199
    $ret[] = update_sql('DELETE FROM {uuid_node_revisions} WHERE nid=0');
200
  }
201

    
202
  return $ret;
203
}
204

    
205
/**
206
 * Remove old variables.
207
 */
208
function uuid_update_7100() {
209
  $types = array(
210
    'nodes',
211
    'users',
212
    'taxonomy',
213
    'comments',
214
  );
215
  foreach ($types as $type) {
216
    variable_del('uuid_automatic_for_' . $type);
217
  }
218
  return array();
219
}
220

    
221
/**
222
 * Clear cache for installations that used alpha1.
223
 *
224
 * Modules previously enabled in uuid_update_7100() don't exist any more. We
225
 * need to clear the cache so Drupal detects this change.
226
 */
227
function uuid_update_7101() {
228
  drupal_flush_all_caches();
229
}
230

    
231
/**
232
 * Ensure that the uuid and vuuid fields are added where needed.
233
 *
234
 * Note that update 7102 calls _uuid_install_uuid_fields(), which is an
235
 * idempotent function.  If _uuid_install_uuid_fields() is changed at some
236
 * point in the future (but remains idempotent), then some uuid users
237
 * will have run update 7102, and some will not.  A new uuid_update_7103()
238
 * function would would therefore be necessary to update all users to
239
 * the latest schema.  At the same time, uuid_update_7102() could become
240
 * an empty function, as it would not be necessary to call
241
 * _uuid_install_uuid_fields() twice.
242
 */
243
function uuid_update_7102() {
244
  // If the user have disabled the UUID module during upgrade (as UPGRADE.txt
245
  // instructs), some functions will be missing. So include the module file.
246
  module_load_include('module', 'uuid', 'uuid');
247
  _uuid_install_uuid_fields();
248
  uuid_sync_all();
249
}
250

    
251
/**
252
 * Clean up entities created by uuid_default_entities_example module.
253
 *
254
 * Modify the labels of all example entities created by the now removed
255
 * uuid_default_entities_example.module to make it clear they're examples. Also
256
 * remove the administrator role of any example user.
257
 */
258
function uuid_update_7103() {
259
  // These are UUIDs of all the example entities that might exist after having
260
  // installed uuid_default_entities_example.module.
261
  $info = entity_get_info();
262
  $uuids = array(
263
    'node' => array(
264
      'b0558664-c94b-3674-d9df-3e1696b2e471',
265
      '5e3d8bbe-a1f2-f2d4-fdc0-71e6c23aa837',
266
    ),
267
    'user' => array(
268
      '7cf875e6-dc15-4404-f190-5a7c3e91d14c',
269
    ),
270
  );
271
  // We can't assume taxonomy is enabled.
272
  if (isset($info['taxonomy_term'])) {
273
    $uuids['taxonomy_term'] = array(
274
      'bcb92ce8-2236-e264-65c8-0c163ae716d1',
275
      '4293a15c-531a-6164-7d1b-668ed019a6bd',
276
      'af738a46-f278-cf84-d94d-9e03879fd71e',
277
    );
278
  }
279
  foreach (array_keys($uuids) as $entity_type) {
280
    $info = entity_get_info($entity_type);
281
    $entity_ids = entity_get_id_by_uuid($entity_type, $uuids[$entity_type]);
282
    $entities = entity_load($entity_type, $entity_ids);
283
    foreach ($entities as $entity) {
284
      // Update the label to make it clear this is example content.
285
      $entity->{$info['entity keys']['label']} = $entity->{$info['entity keys']['label']} . ' (UUID example)';
286
      // Remove the administrator role from any user.
287
      if ($entity_type == 'user' && $rid = array_search('administrator', $entity->roles)) {
288
        unset($entity->roles[$rid]);
289
      }
290
      entity_save($entity_type, $entity);
291
    }
292
  }
293
}