Projet

Général

Profil

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

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

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
  uuid_sync_all();
50
}
51

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

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

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

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

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

    
111
  return $ret;
112
}
113

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

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

    
126
  return $ret;
127
}
128

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

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

    
137
  return $ret;
138
}
139

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

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

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

    
157
  return $ret;
158
}
159

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

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

    
187
  return $ret;
188
}
189

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

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

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

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