Projet

Général

Profil

Paste
Télécharger (8,92 ko) Statistiques
| Branche: | Révision:

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

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(&$schema = array()) {
35
  $field = uuid_schema_field_definition();
36
  foreach (uuid_get_core_entity_info() as $entity_type => $info) {
37
    $schema[$info['base table']]['fields'][$info['entity keys']['uuid']] = $field;
38
    if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
39
      $schema[$info['revision table']]['fields'][$info['entity keys']['revision uuid']] = $field;
40
    }
41
  }
42
}
43

    
44
/**
45
 * Implements hook_install().
46
 */
47
function uuid_install() {
48
  _uuid_install_uuid_fields();
49
  module_load_include('inc', 'uuid');
50
  uuid_sync_all();
51
}
52

    
53
/**
54
 * Install the 'uuid' and 'vuuid' fields into Drupal core entity tables where needed.
55
 *
56
 * IMPORTANT:  This function is called both at install and update time.  If this method
57
 * is modified to add additional fields in the future, the update strategy must be
58
 * considered.  See the comment in uuid_update_7102.
59
 */
60
function _uuid_install_uuid_fields() {
61
  $field = uuid_schema_field_definition();
62
  foreach (uuid_get_core_entity_info() as $entity_type => $info) {
63
    if (!db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
64
      db_add_field($info['base table'], $info['entity keys']['uuid'], $field);
65
      db_add_index($info['base table'], $info['entity keys']['uuid'], array($info['entity keys']['uuid']));
66
    }
67
    if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
68
      if (!db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
69
        db_add_field($info['revision table'], $info['entity keys']['revision uuid'], $field);
70
        db_add_index($info['revision table'], $info['entity keys']['revision uuid'], array($info['entity keys']['revision uuid']));
71
      }
72
    }
73
  }
74
}
75

    
76
/**
77
 * Implements hook_uninstall().
78
 */
79
function uuid_uninstall() {
80
  foreach (uuid_get_core_entity_info() as $entity_type => $info) {
81
    if (db_field_exists($info['base table'], $info['entity keys']['uuid'])) {
82
      db_drop_field($info['base table'], $info['entity keys']['uuid']);
83
      db_drop_index($info['base table'], $info['entity keys']['uuid']);
84
    }
85
    if (!empty($info['revision table']) && !empty($info['entity keys']['revision uuid'])) {
86
      if (db_field_exists($info['revision table'], $info['entity keys']['revision uuid'])) {
87
        db_drop_field($info['revision table'], $info['entity keys']['revision uuid']);
88
        db_drop_index($info['revision table'], $info['entity keys']['revision uuid']);
89
      }
90
    }
91
  }
92
}
93

    
94
/**
95
 * Implements hook_modules_installed().
96
 */
97
function uuid_modules_installed($modules) {
98
  // Run the installation hook. This makes sure that the schema for all
99
  // supported core entity types is set correct.
100
  uuid_install();
101
}
102

    
103
/**
104
 * Create uuid_vocabulary and uuid_term_data tables.
105
 */
106
function uuid_update_6001() {
107
  $ret = array();
108

    
109
  db_create_table($ret, 'uuid_vocabulary', uuid_table_schema('vocabulary', 'vid'));
110
  db_create_table($ret, 'uuid_term_data', uuid_table_schema('term_data', 'tid'));
111

    
112
  return $ret;
113
}
114

    
115
/**
116
 * For each of out tables, drop the indexe on the UUID column and add a unique
117
 * key on that column.
118
 */
119
function uuid_update_6002() {
120
  $ret = array();
121

    
122
  foreach (uuid_schema() as $table => $schema) {
123
    db_drop_index($ret, $table, $table . '_uuid_idx');
124
    db_add_unique_key($ret, $table, $table . '_uuid_key', array('uuid'));
125
  }
126

    
127
  return $ret;
128
}
129

    
130
/**
131
 * Create uuid_comment table.
132
 */
133
function uuid_update_6003() {
134
  $ret = array();
135

    
136
  db_create_table($ret, 'uuid_comments', uuid_table_schema('comments', 'cid'));
137

    
138
  return $ret;
139
}
140

    
141
/**
142
 * Fix the column definitions for uuid columns in all tables
143
 * to use the more efficient char spec.
144
 */
145
function uuid_update_6004() {
146
  $ret = array();
147

    
148
  // Use what's in uuid_table_schema in order to be consistent.
149
  $tables = uuid_schema();
150
  $spec = $tables['uuid_node']['fields']['uuid'];
151

    
152
  foreach ($tables as $tablename => $schema) {
153
    if (db_table_exists($tablename)) {
154
      db_change_field($ret, $tablename, 'uuid', 'uuid', $spec);
155
    }
156
  }
157

    
158
  return $ret;
159
}
160

    
161
/**
162
 * Modify existing uuid_node_revisions table to support revision deletion, and
163
 * add in as much legacy data as possible.
164
 */
165
function uuid_update_6005() {
166
  $ret = array();
167

    
168
  if (db_table_exists('uuid_node_revisions')) {
169
    // Use what's already defined in uuid schema in order to be consistent.
170
    $schema = uuid_schema();
171
    $spec = $schema['uuid_node_revisions']['fields']['nid'];
172
    db_add_field($ret, 'uuid_node_revisions', 'nid', $spec);
173
    // Add node ids to the new column, for revisions that exist, but now have a
174
    // default value of 0 in uuid_node_revisions.
175
    $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);
176
    while ($item = db_fetch_object($result)) {
177
      $ret[] = update_sql('UPDATE {uuid_node_revisions} SET nid=' . (int) $item->nid . ' WHERE vid=' . (int) $item->vid);
178
    }
179
    // Add uuid_node_revision rows for rows that don't exist, but should.
180
    $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');
181
    while ($item = db_fetch_object($result)) {
182
      $ret[] = update_sql(sprintf("INSERT INTO {uuid_node_revisions} (vid, uuid, nid) VALUES(%d, '%s', %d)", $item->vid, uuid_uuid(), $item->nid));
183
    }
184
    // Delete any orphaned revision vid, uuid pairs.
185
    $ret[] = update_sql('DELETE FROM {uuid_node_revisions} WHERE nid=0');
186
  }
187

    
188
  return $ret;
189
}
190

    
191
/**
192
 * Remove old variables.
193
 */
194
function uuid_update_7100() {
195
  $types = array(
196
    'nodes',
197
    'users',
198
    'taxonomy',
199
    'comments',
200
  );
201
  foreach ($types as $type) {
202
    variable_del('uuid_automatic_for_' . $type);
203
  }
204
  return array();
205
}
206

    
207
/**
208
 * Clear cache for installations that used alpha1. Modules that previously was
209
 * enabled in uuid_update_7100() doesn't exist anymore.
210
 */
211
function uuid_update_7101() {
212
  drupal_flush_all_caches();
213
}
214

    
215
/**
216
 * Insure that the uuid and vuuid fields are added where needed.
217
 *
218
 * Note that update 7102 calls _uuid_install_uuid_fields(), which is an
219
 * idempotent function.  If _uuid_install_uuid_fields() is changed at some
220
 * point in the future (but remains idempotent), then some uuid users
221
 * will have run update 7102, and some will not.  A new uuid_update_7103()
222
 * function would would therefore be necessary to update all users to
223
 * the latest schema.  At the same time, uuid_update_7102() could become
224
 * an empty function, as it would not be necessary to call _uuid_install_uuid_fields()
225
 * twice.
226
 */
227
function uuid_update_7102() {
228
  // If the user have disabled the UUID module during upgrade (as UPGRADE.txt
229
  // instructs), some functions will be missing. So include the module file.
230
  module_load_include('module', 'uuid', 'uuid');
231
  _uuid_install_uuid_fields();
232
  uuid_sync_all();
233
}
234

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