This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworkspace.drush.inc
More file actions
121 lines (106 loc) · 4.36 KB
/
workspace.drush.inc
File metadata and controls
121 lines (106 loc) · 4.36 KB
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
<?php
use Drupal\Core\Database\Database;
use Drupal\multiversion\Entity\Workspace;
use Psr\Log\LogLevel;
/**
* Implements of hook_drush_command().
*/
function workspace_drush_command() {
$items = [];
$items['workspace-uninstall'] = [
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
'description' => 'Uninstall Workspace.',
'aliases' => ['wun'],
];
return $items;
}
/**
* Implements drush_hook_COMMAND().
*/
function drush_workspace_uninstall() {
$extension = 'workspace';
$uninstall = TRUE;
$extension_info = drush_get_extensions();
$required = drush_drupal_required_modules($extension_info);
if (in_array($extension, $required)) {
$info = $extension_info[$extension]->info;
$explanation = !empty($info['explanation']) ? ' ' . dt('Reason: !explanation.', ['!explanation' => strip_tags($info['explanation'])]) : '';
drush_log(dt('!extension is a required extension and can\'t be uninstalled.', ['!extension' => $extension]) . $explanation, LogLevel::INFO);
$uninstall = FALSE;
}
elseif (!$extension_info[$extension]->status) {
drush_log(dt('!extension is already uninstalled.', ['!extension' => $extension]), LogLevel::INFO);
$uninstall = FALSE;
}
elseif (drush_extension_get_type($extension_info[$extension]) == 'module') {
$dependents = [];
foreach (drush_module_dependents([$extension], $extension_info) as $dependent) {
if (!in_array($dependent, $required) && ($extension_info[$dependent]->status)) {
$dependents[] = $dependent;
}
}
if (count($dependents)) {
drush_log(dt('To uninstall !extension, the following extensions must be uninstalled first: !required', ['!extension' => $extension, '!required' => implode(', ', $dependents)]), LogLevel::ERROR);
$uninstall = FALSE;
}
}
if ($uninstall) {
drush_print(dt('Workspace will be uninstalled.'));
if(!drush_confirm(dt('Do you really want to continue?'))) {
return drush_user_abort();
}
try {
$entity_type_manager = \Drupal::entityTypeManager();
$default_workspace_id = \Drupal::getContainer()->getParameter('workspace.default');
$default_workspace = Workspace::load($default_workspace_id);
\Drupal::service('workspace.manager')->setActiveWorkspace($default_workspace);
$database = \Drupal::database();
$database
->delete('key_value_expire')
->condition('collection', 'user.private_tempstore.workspace.negotiator.session')
->execute();
// Delete the 'workspace_replication' queue before deleting non-default
// workspaces.
\Drupal::queue('workspace_replication')->deleteQueue();
// Delete all workspaces excluding the default workspace, also delete all
// content from deleted workspaces.
$workspaces = Workspace::loadMultiple();
foreach ($workspaces as $workspace) {
if (!$workspace->isDefaultWorkspace()) {
$workspace->delete();
}
}
\Drupal::service('cron')->run();
// Delete all workspace_pointer entities.
$storage = \Drupal::entityTypeManager()->getStorage('workspace_pointer');
$entities = $storage->loadMultiple();
$storage->delete($entities);
// Delete all replication entities.
$storage = \Drupal::entityTypeManager()->getStorage('replication');
$entities = $storage->loadMultiple();
$storage->delete($entities);
// Set values for all fields provided by Workspace to NULL in the database
// (for workspace entity type), so the module can be uninstalled.
$entity_field_manager = \Drupal::service('entity_field.manager');
$storage = $entity_type_manager->getStorage('workspace');
$fields = [];
foreach ($entity_field_manager->getFieldStorageDefinitions('workspace') as $storage_definition) {
if ($storage_definition->getProvider() === 'workspace') {
$fields[$storage_definition->getName()] = NULL;
}
}
if (!empty($fields)) {
$connection = Database::getConnection();
$connection->update($storage->getEntityType()->getBaseTable())
->fields($fields)
->execute();
}
drush_module_uninstall([$extension]);
}
catch (Exception $e) {
drush_log($e->getMessage(), LogLevel::ERROR);
}
// Inform the user of final status.
drush_log(dt('!extension was successfully uninstalled.', ['!extension' => $extension]), LogLevel::INFO);
}
}