-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminisite.install
380 lines (333 loc) · 12.2 KB
/
minisite.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
<?php
/**
* @file
* Contains install and update functions for Minisite.
*/
use Drupal\Core\Database\Database;
use Drupal\Core\File\FileSystemInterface;
use Drupal\minisite\Minisite;
use Symfony\Component\HttpFoundation\Request;
use Drupal\minisite\LegacyWrapper;
/**
* Implements hook_uninstall().
*/
function minisite_uninstall() {
// Remove the minisite directory and generated files.
if (file_exists(Minisite::getCommonArchiveDir())) {
\Drupal::service('file_system')->deleteRecursive(Minisite::getCommonArchiveDir());
}
if (file_exists(Minisite::getCommonAssetDir())) {
\Drupal::service('file_system')->deleteRecursive(Minisite::getCommonAssetDir());
}
}
/**
* Implements hook_requirements().
*/
function minisite_requirements($phase) {
if ($phase != 'runtime') {
return [];
}
$requirements = [];
if ($phase == 'runtime') {
$path = Minisite::getCommonArchiveDir();
$requirements['minisite_archive'] = [
'title' => t('Minisite archive files upload directory'),
'severity' => REQUIREMENT_OK,
'value' => t('Exists (%path)', ['%path' => $path]),
];
if (!\Drupal::service('file_system')->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
$requirements['minisite_archive']['description'] = t('The Minisite archive upload directory %path could not be created due to a misconfiguration of files directory. Please ensure that the files directory is correctly configured and that the webserver has permission to create directories.', [
'%path' => LegacyWrapper::getTarget($path),
]);
$requirements['minisite_archive']['severity'] = REQUIREMENT_ERROR;
$requirements['minisite_archive']['value'] = t('Unable to create');
}
$path = Minisite::getCommonAssetDir();
$requirements['minisite_asset'] = [
'title' => t('Minisite asset files directory'),
'severity' => REQUIREMENT_OK,
'value' => t('Exists (%path)', ['%path' => $path]),
];
if (!\Drupal::service('file_system')->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
$requirements['minisite_asset']['description'] = t('The Minisite asset files directory %path could not be created due to a misconfiguration of files directory. Please ensure that the files directory is correctly configured and that the webserver has permission to create directories.', [
'%path' => LegacyWrapper::getTarget($path),
]);
$requirements['minisite_asset']['severity'] = REQUIREMENT_ERROR;
$requirements['minisite_asset']['value'] = t('Unable to create');
}
$requirements['minisite_archiver'] = [
'title' => t('Minisite archiver library'),
'severity' => REQUIREMENT_OK,
'value' => t('Present'),
];
if (!class_exists(\ZipArchive::class)) {
$requirements['minisite_archiver']['description'] = t('ZipArchive is required to extract Minisite assets from uploaded archives. Please install php-zip extension.');
$requirements['minisite_asset']['severity'] = REQUIREMENT_ERROR;
$requirements['minisite_asset']['value'] = t('Absent');
}
}
return $requirements;
}
/**
* Implements hook_schema().
*/
function minisite_schema() {
$schema['minisite_asset'] = [
'description' => 'Asset information for minisite.',
'fields' => [
'id' => [
'description' => 'The primary identifier for a minisite asset',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'entity_type' => [
'description' => 'The entity type of that entity.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
],
'entity_bundle' => [
'description' => 'The type of this entity.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
],
'entity_id' => [
'description' => 'The entity id this data is attached to',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'entity_language' => [
'description' => 'The {language}.langcode of this entity.',
'type' => 'varchar_ascii',
'length' => 12,
'not null' => TRUE,
'default' => '',
],
'field_name' => [
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
],
'source' => [
'description' => 'The URI of the source asset file.',
'type' => 'varchar',
'length' => 2048,
'not null' => TRUE,
'default' => '',
],
'alias' => [
'description' => 'The alias for the asset path.',
'type' => 'varchar',
'length' => 2048,
'not null' => FALSE,
'default' => '',
],
'filemime' => [
'description' => 'The file MIME of the asset.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => NULL,
],
'filesize' => [
'description' => 'The file size of the asset.',
'type' => 'int',
'size' => 'big',
'length' => 20,
'unsigned' => TRUE,
'not null' => FALSE,
'default' => NULL,
],
],
'primary key' => [
'id',
],
'unique keys' => [
'id' => [
'id',
'entity_type',
'entity_bundle',
'entity_id',
'entity_language',
'field_name',
],
],
'indexes' => [
'source' => [['source', 170]],
'alias' => [['alias', 170]],
],
];
return $schema;
}
/**
* Adds 'filemime' and 'filesize' and removes `entity_rid` columns.
*/
function minisite_update_8001() {
$schema = Database::getConnection()->schema();
$spec = [
'description' => 'The file MIME of the asset.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'default' => NULL,
];
$schema->addField('minisite_asset', 'filemime', $spec);
$spec = [
'description' => 'The file size of the asset.',
'type' => 'int',
'size' => 'big',
'length' => 20,
'unsigned' => TRUE,
'not null' => FALSE,
'default' => NULL,
];
$schema->addField('minisite_asset', 'filesize', $spec);
$schema->dropField('minisite_asset', 'entity_rid');
}
/**
* Re-saves all Minisite instances to populate the database with asset links.
*/
function minisite_update_8002(&$sandbox) {
// Number of Minisite instances to process in a single batch.
// trying to keep this as low as possible as some minisites may have
// a lot of assets that will take time and resources to process.
$batch_size = getenv('MINISITE_UPDATE_BATCH_SIZE') ?: 1;
if (!isset($sandbox['info'])) {
module_load_include('module', 'minisite');
$info = minisite_get_info_all(TRUE);
if (empty($info)) {
$sandbox['#finished'] = 1;
return t('There are no Minisite fields with content in any available entity types.');
}
$sandbox['info'] = $info;
$sandbox['max'] = count($info);
$sandbox['progress'] = 0;
$sandbox['updated'] = 0;
}
$current_batch_info = array_slice($sandbox['info'], $sandbox['progress'], $batch_size);
$stage_file_proxy_is_enabled = \Drupal::service('module_handler')->moduleExists('stage_file_proxy');
$messages = [];
foreach ($current_batch_info as $info) {
$sandbox['progress']++;
list($entity_type, $field_name, $entity_id) = explode('__', $info);
$messages[] = t('Processing Minisite for field @field_name attached to @entity_type with ID @entity_id.', [
'@entity_type' => $entity_type,
'@field_name' => $field_name,
'@entity_id' => $entity_id,
]);
$host_entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id);
$field_item_list = $host_entity->get($field_name);
// Before proceeding with minisite instantiation, we need to check that
// the archive file is available in this environment and do our best effort
// to fetch files from origin location (works only on the environments with
// stage_file_proxy enabled).
/** @var \Drupal\file\Entity\File $archive_file */
$archive_file = $field_item_list->entity;
$archive_file_uri = $archive_file->getFileUri();
$archive_file_absolute_url = $archive_file->createFileUrl(FALSE);
if (!is_readable($archive_file_uri)) {
if (!$stage_file_proxy_is_enabled) {
$messages[] = ' ' . t('SKIPPED: Archive file is missing in this environment and stage_file_proxy module is not enabled.');
continue;
}
_minisite_install_stage_file_proxy_fetch($archive_file_absolute_url);
if (!is_readable($archive_file_uri)) {
$messages[] = ' ' . t('SKIPPED: Unable to fetch archive file @uri.', [
'@uri' => $archive_file_uri,
]);
continue;
}
$messages[] = ' ' . t('Fetched archive file @uri.', [
'@uri' => $archive_file_uri,
]);
}
$minisite = Minisite::createInstance($field_item_list);
if ($minisite) {
$minisite->save();
$sandbox['updated']++;
$messages[] = ' ' . t('SUCCESS: Updated Ministe.');
}
else {
$messages[] = ' ' . t('SKIPPED: Unable to process Ministe.');
}
}
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
return t('Processed @processed of @total and updated @updated Minisite instances: @messages', [
'@total' => $sandbox['max'],
'@processed' => $sandbox['progress'],
'@updated' => $sandbox['updated'],
'@messages' => PHP_EOL . implode(PHP_EOL, $messages),
]);
}
/**
* Fetch file from the specified URI using stage_file_proxy fetcher.
*
* This is a shortened version of
* \Drupal\stage_file_proxy\EventSubscriber\ProxySubscriber::checkFileOrigin.
*
* Unfortunately, it is not possible to use functionality of stage_file_proxy
* transparently within update hooks (send request from the update hook to the
* same server) as they can be ran via CLI, in which case it is possible that
* the request may not reach the website (for example, if PHP runs in a Docker
* container separate to the web-server and the internal name of the web-server
* is not the same as site's external URI).
*
* It is also not possible to simply craft a stub request and pass it to the
* stage_file_proxy's ProxySubscriber::checkFileOrigin method, since the method
* uses 'exit' to perform a redirect (the logic is not separated from the
* request stack handling), which will terminate the process itself.
*
* The only viable solution is to use stage_file_proxy's fetcher manager with
* additionally added logic taken from ProxySubscriber::checkFileOrigin method.
*
* @param string $url
* Absolute URL to the file to download. Absolute is required in order to
* check that this is not an origin server.
*
* @return bool
* TRUE if file was downloaded, FALSE otherwise.
*
* @see \Drupal\stage_file_proxy\EventSubscriber\ProxySubscriber::checkFileOrigin
*/
function _minisite_install_stage_file_proxy_fetch($url) {
$config = \Drupal::configFactory()->get('stage_file_proxy.settings');
// Get the origin server.
$server = $config->get('origin');
// Quit if no origin given.
if (!$server) {
return FALSE;
}
$request = Request::create($url);
// Quit if we are the origin, ignore http(s).
$current_host = $request->getHost();
if (preg_replace('#^[a-z]*://#u', '', $server) === $current_host) {
return FALSE;
}
$fetch_manager = \Drupal::getContainer()->get('stage_file_proxy.fetch_manager');
$file_dir = $fetch_manager->filePublicPath();
$request_path = $request->getPathInfo();
$request_path = mb_substr($request_path, 1);
if (strpos($request_path, '' . $file_dir) !== 0) {
return FALSE;
}
// Note if the origin server files location is different. This
// must be the exact path for the remote site's public file
// system path, and defaults to the local public file system path.
$remote_file_dir = trim($config->get('origin_dir'));
if (!$remote_file_dir) {
$remote_file_dir = $file_dir;
}
$request_path = rawurldecode($request_path);
$relative_path = mb_substr($request_path, mb_strlen($file_dir) + 1);
$options = [
'verify' => $config->get('verify'),
];
return $fetch_manager->fetch($server, $remote_file_dir, $relative_path, $options);
}