-
Notifications
You must be signed in to change notification settings - Fork 1
/
helfi_api_base.install
354 lines (309 loc) · 8.93 KB
/
helfi_api_base.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
<?php
/**
* @file
* Contains installation procedure for API base module.
*/
declare(strict_types=1);
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\TranslatableInterface;
use Drupal\helfi_api_base\Features\FeatureManager;
use Drupal\rest\Entity\RestResourceConfig;
use Drupal\user\Entity\Role;
/**
* Implements hook_install().
*/
function helfi_api_base_install(bool $is_syncing = FALSE) : void {
if ($is_syncing) {
return;
}
if (Drupal::moduleHandler()->moduleExists('filter')) {
$formatters = Drupal::entityTypeManager()
->getStorage('filter_format')
->loadMultiple();
/** @var \Drupal\filter\Entity\FilterFormat $format */
foreach ($formatters as $format) {
$format->setFilterConfig('helfi_link_converter', [
'status' => 1,
])
->save();
}
}
if (Drupal::moduleHandler()->moduleExists('raven')) {
Drupal::configFactory()->getEditable('raven.settings')
->set('drush_error_handler', TRUE)
->set('request_tracing', TRUE)
->set('traces_sample_rate', 0.2)
->set('browser_traces_sample_rate', 0.2)
->set('database_tracing', TRUE)
->set('twig_tracing', TRUE)
->set('log_levels', [
'emergency' => TRUE,
'alert' => TRUE,
'critical' => TRUE,
'error' => TRUE,
'warning' => FALSE,
'notice' => FALSE,
'info' => FALSE,
'debug' => FALSE,
])
->set('fatal_error_handler', TRUE)
->save();
}
}
/**
* Enable 'helfi_link_converter' filter format.
*/
function helfi_api_base_update_9001() : void {
helfi_api_base_install();
}
/**
* Install the package version rest config.
*/
function helfi_api_base_update_9003() : void {
if (!Drupal::moduleHandler()->moduleExists('rest')) {
return;
}
$config_path = Drupal::service('extension.list.module')
->getPath('helfi_api_base') . '/config/optional';
$source = new FileStorage($config_path);
$config_storage = Drupal::service('config.storage');
$configs = [
'rest.resource.helfi_debug_package_version',
];
foreach ($configs as $config) {
$config_storage->write($config, $source->read($config));
}
}
/**
* Enable health check.
*/
function helfi_api_base_update_9007() : void {
if (!Drupal::moduleHandler()->moduleExists('health_check')) {
Drupal::service('module_installer')->install([
'health_check',
]);
}
}
/**
* Enable basic_auth authentication method for debug endpoints.
*/
function helfi_api_base_update_9009() : void {
if (Drupal::moduleHandler()->moduleExists('rest')) {
Drupal::service('module_installer')->install([
'basic_auth',
]);
$endpoints = RestResourceConfig::loadMultiple([
'helfi_debug_package_version',
]);
foreach ($endpoints as $endpoint) {
$config = $endpoint->get('configuration');
if (!in_array('basic_auth', $config['authentication'])) {
$config['authentication'][] = 'basic_auth';
$endpoint->set('configuration', $config);
$endpoint->save();
}
}
}
}
/**
* Create 'debug_api' role for hel.fi projects.
*/
function helfi_api_base_update_9013() : void {
helfi_api_base_install();
}
/**
* Enable 'delete old revisions' feature.
*/
function helfi_api_base_update_9014() : void {
\Drupal::configFactory()->getEditable('helfi_api_base.delete_revisions')
->set('entity_types', [
'node',
'paragraph',
'tpr_unit',
'tpr_service',
'tpr_errand_service',
])->save();
}
/**
* Enable needed features.
*/
function helfi_api_base_update_9015() : void {
/** @var \Drupal\helfi_api_base\Features\FeatureManager $service */
$service = \Drupal::service(FeatureManager::class);
$service->enableFeature(FeatureManager::DISABLE_USER_PASSWORD);
}
/**
* Install raven module.
*/
function helfi_api_base_update_9016() : void {
Drupal::service('module_installer')->install([
'raven',
]);
Drupal::configFactory()->getEditable('raven.settings')
->set('drush_error_handler', TRUE)
->set('request_tracing', TRUE)
->set('traces_sample_rate', 0.2)
->set('database_tracing', TRUE)
->set('twig_tracing', TRUE)
->save();
}
/**
* Enable 'monolog' module.
*/
function helfi_api_base_update_9017(): void {
Drupal::service('module_installer')->install([
'monolog',
]);
}
/**
* Enable new features.
*/
function helfi_api_base_update_9018(): void {
/** @var \Drupal\helfi_api_base\Features\FeatureManager $service */
$service = \Drupal::service(FeatureManager::class);
$service->enableFeature(FeatureManager::USER_EXPIRE);
$service->enableFeature(FeatureManager::DISABLE_EMAIL_SENDING);
}
/**
* Remove debug_api role and debug api functionality.
*/
function helfi_api_base_update_9019(): void {
if ($account = user_load_by_name('helfi-debug-data')) {
$account->delete();
}
if ($role = Role::load('debug_api')) {
$role->delete();
}
// Delete rest endpoint configuration.
\Drupal::configFactory()->getEditable('rest.resource.helfi_debug_data')
->delete();
}
/**
* UHF-9982: Configure sentry browser tracking.
*/
function helfi_api_base_update_9020(): void {
if (Drupal::moduleHandler()->moduleExists('raven')) {
Drupal::configFactory()->getEditable('raven.settings')
->set('browser_traces_sample_rate', 0.2)
->save();
}
}
/**
* UHF-10880: Update the Raven settings.
*/
function helfi_api_base_update_9021() : void {
helfi_api_base_install();
}
/**
* UHF-10969 Fix URLs with spaces.
*/
function helfi_api_base_update_9022(): void {
helfi_api_base_sanitize_links();
}
/**
* Sanitize links from text fields.
*/
function helfi_api_base_sanitize_links(): void {
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
$entity_field_manager = Drupal::service('entity_field.manager');
$entity_type_manager = \Drupal::entityTypeManager();
$field_types = [
'text_with_summary',
'text',
'text_long',
];
$count = 0;
foreach ($field_types as $field_type) {
$field_map = $entity_field_manager->getFieldMapByFieldType($field_type);
foreach ($field_map as $entity_type => $fields) {
foreach ($fields as $name => $field) {
$query = $entity_type_manager
->getStorage($entity_type)
->getQuery();
$condition_group = $query->orConditionGroup();
$conditions = [
// Matches spaces immediately after href=".
'<a href=" +[^"]+',
// Matches URLs starting with %20.
'<a href="%20[^"]+',
// Matches URLs ending with %20.
'<a href="[^"]*%20"',
// Matches URLs ending with a literal or non-breaking space.
'<a href="[^"]*[ \ ]"',
];
foreach ($conditions as $condition) {
$condition_group->condition($name, $condition, 'REGEXP');
}
$query->exists($name)->condition($condition_group);
$query->accessCheck(FALSE);
$ids = $query->execute();
foreach ($ids as $id) {
$entity = $entity_type_manager->getStorage($entity_type)->load($id);
assert($entity instanceof TranslatableInterface);
foreach ($entity->getTranslationLanguages() as $language) {
_helfi_api_base_process_links(
$entity->getTranslation($language->getId()),
$name,
$count
);
}
}
}
}
}
\Drupal::logger('helfi_api_base')
->notice(sprintf('Fixed %s links with extra spaces.', $count));
}
/**
* Sanitize filenames inside text fields.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity translation to process.
* @param string $field_name
* The field name.
* @param int $count
* The number of links fixed.
*/
function _helfi_api_base_process_links(ContentEntityInterface $entity, string $field_name, int &$count = 0) : void {
if (!$value = $entity->get($field_name)->value) {
return;
}
$hasChanges = FALSE;
$dom = Html::load($value);
/** @var \DOMElement $node */
foreach ($dom->getElementsByTagName('a') as $node) {
// Nothing to do if link has no href.
if (!$href = $node->getAttribute('href')) {
continue;
}
// Remove non-breaking spaces, any leading or trailing `%20`
// and trim the href.
$newHref = preg_replace('/^( )+|( )$/u', '', $href);
$newHref = preg_replace('/^(%20)|(%20)$/u', '', $newHref);
$newHref = trim($newHref);
if ($newHref === $href) {
continue;
}
$hasChanges = TRUE;
$count++;
$node->setAttribute('href', $newHref);
}
if ($hasChanges) {
$entity->get($field_name)->value = Html::serialize($dom);
$entity->save();
}
}
/**
* UHF-10713 Remove VersionChecker.
*/
function helfi_api_base_update_9023(): void {
if (!\Drupal::moduleHandler()->moduleExists('rest')) {
return;
}
\Drupal::entityTypeManager()
->getStorage('rest_resource_config')
->load('helfi_debug_package_version')
?->delete();
}