forked from discoverygarden/islandora_drush_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathislandora_drush_utils.module
124 lines (115 loc) · 3.52 KB
/
islandora_drush_utils.module
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
<?php
/**
* @file
* General hook implementations.
*/
/**
* Implements hook_batch_alter().
*/
function islandora_drush_utils_batch_alter(&$batch) {
// Better preservation messsaging.
foreach ($batch['sets'] as &$set) {
if (!isset($set['operations'])) {
continue;
}
foreach ($set['operations'] as $operation_structure) {
$operation = reset($operation_structure);
// This can be either an array, or a string like
// '\Drupal\migrate_tools\MigrateBatchExecutable::batchProcessImport'.
if (is_array($operation)) {
$operator = reset($operation);
}
else {
$operator = $operation;
}
// @XXX: this is the most identifying part of this batch.
// XXX: Spell out the classname to avoid creating a strong dependency on
// "content_sync_helper".
if (is_a($operator, "\Drupal\content_sync_helper\Utility\Exporter")) {
$set['title'] = t('Updating the repository');
}
}
}
// XXX: Batches performed under Drush 9 do not inherit the user of the
// caller, and Drush 9's "process.manager" service is not easily
// overridable so as to be able to override... SO: Let's wrap all batch
// operations passing through the CLI if they have been submitted as an
// authenticated user, to make them run as said authenticated user.
// Somewhat associated with Drush 9's whole dropping-support-for-the-"--user"
// -argument business...
// @see https://github.com/drush-ops/drush/issues/3396
if (PHP_SAPI !== 'cli') {
return;
}
$logger = \Drupal::logger('islandora_drush_utils');
$user = \Drupal::currentUser();
if (!$user->isAuthenticated()) {
$logger->debug('Non-authenticated batch; skipping @wrapper wrapping...', [
'@wrapper' => '_islandora_drush_utils_user_wrapped_batch_op',
]);
return;
}
$namer = function ($callable) {
// Adapted from https://stackoverflow.com/a/42954492
if (is_string($callable)) {
return trim($callable);
}
elseif (is_array($callable)) {
if (is_object($callable[0])) {
return sprintf("(%s instance)::%s", get_class($callable[0]),
trim($callable[1]));
}
else {
return sprintf("%s::%s", trim($callable[0]), trim($callable[1]));
}
}
elseif ($callable instanceof Closure) {
return 'closure';
}
else {
return 'unknown';
}
};
$wrap_op = function ($op) use ($user, $logger, $namer) {
$func = reset($op);
if ($func !== '_islandora_drush_utils_user_wrapped_batch_op') {
$logger->debug('Wrapping @func with @wrapper to maintain the user (@uid).',
[
'@func' => $namer($func),
'@wrapper' => '_islandora_drush_utils_user_wrapped_batch_op',
'@uid' => $user->id(),
]);
return [
'_islandora_drush_utils_user_wrapped_batch_op',
[
$user->id(),
$op,
],
];
}
else {
return $op;
}
};
foreach ($batch['sets'] as &$set) {
$set['operations'] = array_map($wrap_op, $set['operations']);
}
// XXX: Clean up the reference.
unset($set);
}
/**
* Wrap batch op with user.
*/
function _islandora_drush_utils_user_wrapped_batch_op($id, $info, &$context) {
$switcher = \Drupal::service('account_switcher');
try {
$user = \Drupal::service('entity_type.manager')
->getStorage('user')
->load($id);
$switcher->switchTo($user);
[$op, $args] = $info;
return call_user_func_array($op, array_merge($args, [&$context]));
} finally {
$switcher->switchBack();
}
}