-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiversion.install
609 lines (549 loc) · 22.6 KB
/
multiversion.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
<?php
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface;
use Drupal\multiversion\Redirect\RedirectStorageSchema;
use Drupal\multiversion\Entity\Workspace;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Driver\mysql\Connection;
use Drupal\multiversion\MultiversionMigration;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Implements hook_requirements().
*/
function multiversion_requirements($phase) {
$requirements = [];
if ($phase === 'install') {
if (\Drupal::moduleHandler()->moduleExists('workspaces')) {
$requirements['workspaces_incompatibility'] = [
'severity' => REQUIREMENT_ERROR,
'description' => t('Multiversion can not be installed when Workspaces is also installed.'),
];
}
}
return $requirements;
}
/**
* Implements hook_install().
*/
function multiversion_install() {
// Create default workspace.
Workspace::create(['machine_name' => 'live', 'label' => 'Live', 'type' => 'basic'])->save();
/** @var \Drupal\multiversion\MultiversionManagerInterface $manager */
$manager = \Drupal::getContainer()->get('multiversion.manager');
$manager->enableEntityTypes();
_multiversion_add_workspace_field_in_url_alias_table(TRUE);
}
/**
* Implements hook_uninstall().
*/
function multiversion_uninstall() {
\Drupal::state()->delete('multiversion_enabled');
$database = \Drupal::database();
$schema = $database->schema();
$table = 'url_alias';
$field = 'workspace';
if ($schema->fieldExists($table, $field)) {
$schema->dropField($table, $field);
}
}
/**
* Add workspace field to data_field and revision_field tables and migrate data.
*/
function multiversion_update_8001() {
$connection = Database::getConnection();
if ($connection instanceof Connection) {
$schema = $connection->schema();
$entity_manager = \Drupal::service('entity.manager');
$manager = \Drupal::service('multiversion.manager');
// Find all supported entities.
$entities = $manager->getSupportedEntityTypes();
// Field can't be NOT NULL on an table with existing data.
$field = [
'type' => 'int',
'unsigned' => TRUE,
];
// Loop through each one.
foreach ($entities as $entity_type => $entity) {
$entity_keys = $entity->getKeys();
// Get the field names used as keys.
$field_id = $entity_keys['id'];
$field_revision = $entity_keys['revision'];
// Get the tables name used for base table and revision table.
$table_base = ($entity->isTranslatable()) ? $entity->getDataTable() : $entity->getBaseTable();
$table_revision = ($entity->isTranslatable()) ? $entity->getRevisionDataTable() : $entity->getRevisionTable();
// Block content definition doesn't include the revision field table.
// So get it.
$tables = $entity_manager->getStorage($entity_type)
->getTableMapping()
->getTableNames();
if (!$table_revision && in_array($entity_type . '_field_revision', $tables)) {
$table_revision = $entity_type . '_field_revision';
}
$results = [];
// Pull data from the old table.
$old_data_table = $entity_type . '__workspace';
if ($schema->tableExists($old_data_table)){
$results = $connection->select($old_data_table)
->fields($old_data_table, ['entity_id', 'workspace_target_id'])
->execute()->fetchAll();
}
if ($schema->tableExists($table_base)) {
if (!$schema->fieldExists($table_base, 'workspace')) {
// Add new field to the base table.
$schema->addField($table_base, 'workspace', $field);
}
foreach ($results as $result) {
// Add the value to the new column.
$connection->update($table_base)
->fields(['workspace' => $result->workspace_target_id])
->condition($field_id, $result->entity_id, '=')
->execute();
}
}
if ($schema->tableExists($old_data_table)) {
// Drop old table.
$schema->dropTable($old_data_table);
}
$results = [];
// Pull data from old table.
$old_revision_table = $entity_type . '_revision__workspace';
if ($schema->tableExists($old_revision_table)) {
$results = $connection->select($old_revision_table)
->fields($old_revision_table, [
'entity_id,',
'revision_id',
'workspace_target_id'
])
->execute();
}
if ($schema->tableExists($table_revision)) {
if (!$schema->fieldExists($table_revision, 'workspace')) {
// Add new field to the field revision table.
$schema->addField($table_revision, 'workspace', $field);
}
foreach ($results as $result) {
// Add data to the revision table.
$connection->update($table_revision)
->fields(['workspace' => $result->workspace_target_id])
->condition($field_id, $result->entity_id, '=')
->condition($field_revision, $result->revision_id)
->execute();
}
}
if ($schema->tableExists($old_revision_table)) {
// Drop old table.
$schema->dropTable($old_revision_table);
}
}
}
}
/**
* Update user entity type to non-multiversionable.
*/
function multiversion_update_8004() {
$update_manager = \Drupal::entityDefinitionUpdateManager();
$changes = $update_manager->getChangeSummary();
// Check if user entity type has new changes.
if (isset($changes['user'])) {
/** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */
$entity_type_manager = \Drupal::service('entity_type.manager');
$storage = $entity_type_manager->getStorage('user');
$entity_type = $storage->getEntityType();
$database = \Drupal::database();
try {
$entity_type_manager = \Drupal::entityTypeManager();
// Create a new migration and migrate user entities to the temporary storage.
$migration = MultiversionMigration::create(\Drupal::getContainer(), $entity_type_manager);
$migration->installDependencies();
$migration->migrateContentToTemp($entity_type);
// Remove all data from the old storage.
$migration->emptyOldStorage($storage);
// Delete revision tables that after applying updates doesn't get deleted.
$tables_to_delete = ['user_revision__roles', 'user_revision__user_picture'];
foreach ($tables_to_delete as $table_name) {
if ($database->schema()->tableExists($table_name)) {
$database->schema()->dropTable($table_name);
}
}
// Apply new updates.
$migration->applyNewStorage();
// Migrate content from the temporary storage to the new storage.
$migration->migrateContentFromTemp($entity_type);
$migration->cleanupMigration('user__to_tmp');
$migration->cleanupMigration('user__from_tmp');
$migration->uninstallDependencies();
}
catch (\Exception $e) {
\Drupal::logger('multiversion')->error($e->getMessage());
}
}
}
/**
* Necessary updates when making Multiversion opt-in.
*
* To make Multiversion opt-in we introduced a configuration object where we
* keep the information of the enabled entity types. In this update we check
* which entity types have been changed and need updates, if the last installed
* field storage schema for the changed entity type contain the 'workspace'
* field - a field provided by multiversion, then we know that this entity has
* been already enabled as multiversionable and we add it as enabled in the new
* configuration object.
*/
function multiversion_update_8005() {
$update_manager = \Drupal::entityDefinitionUpdateManager();
$changed_entity_types = array_keys($update_manager->getChangeSummary());
$last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
$enabled_entity_types = [];
// Loop through all changes, if it's a change for an entity type and the last
// installed field storage schema contains the 'workspace' field, add this
// entity as enabled.
foreach ($changed_entity_types as $entity_type_id) {
if ($update_manager->getEntityType($entity_type_id)) {
$last_field_storage_definition = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
if (isset($last_field_storage_definition['workspace'])) {
$enabled_entity_types[] = $entity_type_id;
}
}
}
// To the enabled entity types list, add testing and replication log entity
// types.
$enabled_entity_types = array_merge(
$enabled_entity_types,
[
'entity_test',
'entity_test_rev',
'entity_test_mul',
'entity_test_mulrev',
'entity_test_local',
'replication_log',
]
);
\Drupal::configFactory()
->getEditable('multiversion.settings')
->set('enabled_entity_types', $enabled_entity_types)
->save();
}
/**
* Clear caches due to behavior change.
*/
function multiversion_update_8006() {
// Empty update to cause a cache rebuild.
}
/**
* Create missing revisionable fields in the revision table.
*/
function multiversion_update_8007() {
$connection = Database::getConnection();
if ($connection instanceof Connection) {
$schema = $connection->schema();
$entity_type_manager = \Drupal::entityTypeManager();
$manager = \Drupal::service('multiversion.manager');
// Find all supported entities.
$entity_types = $manager->getSupportedEntityTypes();
// Loop through each one.
foreach ($entity_types as $entity_type_id => $entity_type) {
$id_key = $entity_type->getKey('id');
/** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
$storage = $entity_type_manager->getStorage($entity_type_id);
// Get the tables name used for base table and revision table.
$table_base = ($entity_type->isTranslatable()) ? $entity_type->getDataTable() : $entity_type->getBaseTable();
$table_revision = ($entity_type->isTranslatable()) ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
// Block content definition doesn't include the revision field table.
// So get it.
/** @var \Drupal\Core\Entity\Sql\TableMappingInterface $table_mapping */
$table_mapping = $storage->getTableMapping();
$tables = $table_mapping->getTableNames();
if (!$table_revision && in_array($entity_type_id . '_field_revision', $tables)) {
$table_revision = $entity_type_id . '_field_revision';
}
elseif (!$table_revision && in_array($entity_type_id . '_revision', $tables)) {
$table_revision = $entity_type_id . '_revision';
}
if ($schema->tableExists($table_base) && $table_revision) {
// Get data from base table.
$table_base_results = $connection->select($table_base)
->fields($table_base)
->execute()->fetchAll();
// Get data from revision table.
$table_revision_results = $connection->select($table_revision)
->fields($table_revision)
->execute()->fetchAll();
if (in_array($table_revision, $tables)) {
$table_revision_fields = $table_mapping->getFieldNames($table_revision);
$entity_field_manager = \Drupal::service('entity_field.manager');
$fields = $entity_field_manager->getBaseFieldDefinitions($entity_type_id);
$new_field_storage_definitions = [];
// Loop through all the fields, if the field exists in the new
// revision table mapping and it doesn't exist in the database,
// create the new field.
foreach ($fields as $field_name => $field) {
if (in_array($field_name, $table_revision_fields) && !$schema->fieldExists($table_revision, $field_name)) {
$new_field_storage_definitions[] = $field->getFieldStorageDefinition($field->getName(), $entity_type_id);
}
}
if (!empty($new_field_storage_definitions)) {
// Remove all data from revision table before adding new fields.
$connection->truncate($table_revision)->execute();
foreach ($new_field_storage_definitions as $storage_definition) {
\Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definition);
}
}
// If the revision table has been updated (new field has been added),
// complete new fields with data from base table.
if (!empty($new_field_storage_definitions)) {
$table_base_results_keyed = [];
foreach ($table_base_results as $result) {
if (isset($result->{$id_key})) {
$data = (array) $result;
$table_base_results_keyed[$result->{$id_key}] = $data;
// Add data for fields with multiple columns, like link__title,
// link__description, ...
foreach ($data as $field_column_name => $value) {
if ($field_name = strstr($field_column_name, '__', TRUE)) {
if (in_array($field_name, $table_revision_fields) && !in_array($field_column_name, $table_revision_fields)) {
$table_base_results_keyed[$result->{$id_key}][$field_column_name] = $data[$field_column_name];
$table_revision_fields[] = $field_column_name;
}
}
}
}
}
// For the new created revisionable fields take data from base table.
foreach ($table_revision_results as $result) {
$data = (array) $result;
foreach ($table_revision_fields as $field_name) {
if (!isset($data[$field_name]) && isset($table_base_results_keyed[$result->{$id_key}][$field_name])) {
$data[$field_name] = $table_base_results_keyed[$result->{$id_key}][$field_name];
}
}
// Save the information in the revision table.
$connection->insert($table_revision)
->fields($data)
->execute();
}
}
}
}
}
}
}
/**
* Make langcode primary key in base data table and base revision table.
*/
function multiversion_update_8101() {
$schema = Database::getConnection()->schema();
$entity_types = \Drupal::service('multiversion.manager')->getSupportedEntityTypes();
/** @var ContentEntityTypeInterface $entity_type */
foreach ($entity_types as $entity_type) {
if ($entity_type->id() === 'file' || $entity_type->get('local') == TRUE || empty($entity_type->getKey('langcode'))) {
continue;
}
// Get the tables name used for base table and revision table.
$table_base = ($entity_type->isTranslatable()) ? $entity_type->getDataTable() : $entity_type->getBaseTable();
$table_revision = ($entity_type->isTranslatable()) ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
if ($table_base) {
$schema->dropPrimaryKey($table_base);
$schema->addPrimaryKey($table_base, [$entity_type->getKey('id'), 'langcode']);
}
if ($table_revision) {
$schema->dropPrimaryKey($table_revision);
$schema->addPrimaryKey($table_revision, [$entity_type->getKey('revision'), 'langcode']);
}
}
}
/**
* Make _deleted and _rev field non-translatable.
*/
function multiversion_update_8102() {
$entity_types = \Drupal::service('multiversion.manager')->getSupportedEntityTypes();
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
foreach ($entity_types as $entity_type_id => $entity_type) {
$enabled = \Drupal::state()->get('multiversion.migration_done.' . $entity_type_id, FALSE);
if ($enabled) {
foreach (['_deleted', '_rev'] as $field_name) {
$field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition($field_name, $entity_type_id);
if ($field_storage_definition) {
$field_storage_definition->setTranslatable(FALSE);
$entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
}
}
}
}
}
/**
* Enable poll and poll_choices.
*/
function multiversion_update_8103() {
if (\Drupal::moduleHandler()->moduleExists('poll')) {
$manager = \Drupal::entityTypeManager();
$entity_types = [];
foreach (['poll_choice', 'poll'] as $id) {
$entity_type = $manager->getStorage($id)->getEntityType();
if ($entity_type instanceof ContentEntityTypeInterface) {
$entity_types[$id] = $entity_type;
}
}
if (!empty($entity_types)) {
\Drupal::service('multiversion.manager')->enableEntityTypes($entity_types);
}
}
}
/**
* Use two different settings, one for supported and one for enabled entity types.
*/
function multiversion_update_8104() {
$multiversion_settings = \Drupal::configFactory()->getEditable('multiversion.settings');
$supported = [
'node',
'taxonomy_term',
'comment',
'menu_link_content',
'block_content',
'file',
'media',
'shortcut',
'entity_test',
'entity_test_rev',
'entity_test_mul',
'entity_test_mulrev',
'entity_test_local',
'content_moderation_state',
'replication_log',
'paragraph',
'poll',
'poll_choice',
];
$multiversion_settings->set('supported_entity_types', $supported)->save();
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
$enabled_entity_types = [];
foreach ($entity_types as $entity_type_id => $entity_type) {
if (is_subclass_of($entity_type->getStorageClass(), ContentEntityStorageInterface::class)) {
$enabled_entity_types[] = $entity_type_id;
}
}
$multiversion_settings->set('enabled_entity_types', $enabled_entity_types)->save();
}
/**
* Fix the migration status for already enabled entity types.
*/
function multiversion_update_8105() {
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
if (is_subclass_of($entity_type->getStorageClass(), ContentEntityStorageInterface::class)) {
\Drupal::state()->set("multiversion.migration_done.$entity_type_id", TRUE);
}
}
}
/**
* Resolve all conflicts for local entities, these entities have been excluded
* from conflict tracking.
*/
function multiversion_update_8106() {
$entity_type_manager = \Drupal::entityTypeManager();
$conflict_tracker = \Drupal::service('workspace.conflict_tracker');
$revision_index = \Drupal::service('multiversion.entity_index.rev');
$workspaces = $entity_type_manager->getStorage('workspace')->loadMultiple();
// Load conflicts from all workspaces.
foreach ($workspaces as $id => $workspace) {
$conflicts = $conflict_tracker
->useWorkspace($workspace)
->getAll();
foreach ($conflicts as $uuid => $conflict) {
$conflict_keys = array_keys($conflict);
$rev = reset($conflict_keys);
$rev_info = $revision_index
->useWorkspace($id)
->get("$uuid:$rev");
if (!empty($rev_info['entity_type_id'])) {
$entity_type = $entity_type_manager
->getStorage($rev_info['entity_type_id'])
->getEntityType();
// Resolve conflicts for local entity types.
if ($entity_type->get('local') === TRUE) {
$conflict_tracker->resolveAll($uuid);
}
}
}
}
}
/**
* Add a publishing status field for workspace entities.
*/
function multiversion_update_8107() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Add the published entity key to the workspace entity type.
$entity_type = $definition_update_manager->getEntityType('workspace');
$entity_keys = $entity_type->getKeys();
$entity_keys['published'] = 'published';
$entity_type->set('entity_keys', $entity_keys);
$definition_update_manager->updateEntityType($entity_type);
// Add the publishing status field to the workspace entity type.
$status = BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Publishing status'))
->setDescription(new TranslatableMarkup('A boolean indicating the published state.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)
->setInitialValue(TRUE);
$definition_update_manager->installFieldStorageDefinition('published', 'workspace', 'workspace', $status);
}
/**
* Add workspace field in url_alias table.
*/
function multiversion_update_8108() {
_multiversion_add_workspace_field_in_url_alias_table();
}
/**
* Enable redirect entity type.
*/
function multiversion_update_8109() {
if (\Drupal::moduleHandler()->moduleExists('redirect')) {
$entity_type_id = 'redirect';
$update_manager = \Drupal::entityDefinitionUpdateManager();
// Get the current redirect entity type definition, ensure the storage schema
// class is set.
$entity_type = $update_manager->getEntityType($entity_type_id)
->setHandlerClass('storage_schema', RedirectStorageSchema::class);
// Update entity type.
$update_manager->updateEntityType($entity_type);
if (\Drupal::moduleHandler()->moduleExists('redirect')) {
$multiversion_settings = \Drupal::configFactory()->getEditable('multiversion.settings');
$supported_entity_types = $multiversion_settings->get('supported_entity_types') ?: [];
// Check if entity type should be added to the supported entity types list.
if (!in_array($entity_type_id, $supported_entity_types)) {
$supported_entity_types[] = $entity_type_id;
// Add new entity types to the supported entity types list.
$multiversion_settings
->set('supported_entity_types', $supported_entity_types)
->save();
}
$manager = \Drupal::entityTypeManager();
$entity_types = [];
$entity_type = $manager->getStorage($entity_type_id)->getEntityType();
if ($entity_type instanceof ContentEntityTypeInterface) {
$entity_types[$entity_type_id] = $entity_type;
}
}
// Enable new entity type.
if (!empty($entity_types)) {
\Drupal::service('multiversion.manager')->enableEntityTypes($entity_types);
}
}
}
/**
* Add the queued_for_delete field.
*/
function multiversion_update_8110() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Add the queued for delete flag for the workspace entity type.
$queued_for_delete = BaseFieldDefinition::create('boolean')
->setLabel(t('Queued for delete'))
->setDescription(t('A flag that specifies if the entity has been queued for delete on next cron run.'))
->setRevisionable(FALSE)
->setTranslatable(FALSE)
->setRequired(FALSE)
->setDefaultValue(FALSE)
->setInitialValue(FALSE);
$definition_update_manager->installFieldStorageDefinition('queued_for_delete', 'workspace', 'multiversion', $queued_for_delete);
}