This repository was archived by the owner on Jul 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_d2d.module
More file actions
executable file
·813 lines (709 loc) · 26.8 KB
/
Copy pathdeploy_d2d.module
File metadata and controls
executable file
·813 lines (709 loc) · 26.8 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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
<?php
/**
* Actency custom module to take nodes from a source and deploy them into a destination
*
* @author Manuel Grosrenaud
* @author Pierre Lesbazeilles
*
* @todo
* - vbo implementation (enrich node selection mechanism befor export)
* - manage taxonomy voc
* - extend uuid to others entities than only nodes
* - implement some logs
*/
/**
* Implements hook_help().
*
* Show some help on each form provided by this module.
*/
function deploy_d2d_help($path) {
$output = '';
switch ($path) {
case 'deploy_d2d/import-export/import':
$output = t('Generate files in public://import');
break;
case 'deploy_d2d/import-export/export':
$output = t('Load file in public://export and saves nodes ');
break;
}
return $output;
}
/**
* Implements hook_menu()
*/
function deploy_d2d_menu() {
$items['admin/deploy_d2d/import-export'] = array(
'title' => 'deploy_d2d',
'description' => 'deploy_d2d.',
'page callback' => 'deploy_d2d_deploy_page',
'access arguments' => array('access administration pages'),
);
$items['admin/deploy_d2d/import-export/import'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('deploy_d2d_import_form'),
'access arguments' => array('access content'),
'title' => 'import',
);
$items['admin/deploy_d2d/import-export/export'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('deploy_d2d_export_form'),
'access arguments' => array('access content'),
'title' => 'export',
);
return $items;
}
/**
* Menu callbacks
*/
function deploy_d2d_deploy_page() {
$output = l('import', 'admin/deploy_d2d/import-export/import');
$output .= '<br/>' . l('export', 'admin/deploy_d2d/import-export/export');
return $output;
}
function deploy_d2d_export_form($form, &$form_state) {
if (module_exists('domain')) {
$domains = domain_domains();
$options = array('' => '');
foreach ($domains as $domain) {
$options[$domain['domain_id']] = $domain['sitename'];
}
$form['domain'] = array(
'#type' => 'select',
'#title' => t('Domain'),
'#options' => $options,
'#required' => TRUE,
);
}
$form['submit'] = array('#type' => 'submit', '#value' => t('Export'));
return $form;
}
function deploy_d2d_import_form($form, &$form_state) {
$form['delete'] = array(
'#type' => 'fieldset',
'#title' => t('Delete'),
'#weight' => -5,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
if (module_exists('domain')) {
$domains = domain_domains();
$options = array('' => '');
$options = array('0' => 'ALL');
foreach ($domains as $domain) {
$options[$domain['domain_id']] = $domain['sitename'];
}
$form['delete']['domain'] = array(
'#type' => 'select',
'#title' => t('Domain'),
'#options' => $options,
'#required' => TRUE,
);
}
$form['delete']['delete'] = array(
'#type' => 'checkbox',
'#title' => t('Delete ALL nodes before import.'),
'#return_value' => 1,
'#default_value' => 0,
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Import'));
return $form;
}
function deploy_d2d_export_form_submit($form, &$form_state) {
$nodes = $taxos = $taxo_order = $vocabs = $users = $menu_links = array();
$file = DRUPAL_ROOT . '/includes/utility.inc';
if (is_file($file)) {
require_once $file;
}
// Taxonomies
$vocabs = taxonomy_get_vocabularies(NULL);
foreach ($vocabs as $vocab_object) {
$taxos = array_merge($taxos, taxonomy_get_tree($vocab_object->vid));
}
foreach ($taxos as $taxo) {
$taxo_order[$taxo->tid] = $taxo;
}
file_put_contents('public://export/taxonomies.export', drupal_var_export($taxo_order));
// Users
$users = entity_load('user');
file_put_contents('public://export/users.export', drupal_var_export(user_load_multiple(array_keys($users))));
// Nodes
$nids = deploy_d2d_get_nodes($form_state['values']['domain']);
$nodes = node_load_multiple($nids);
file_put_contents('public://export/nodes.export', drupal_var_export($nodes));
// Menus links
$mlids = deploy_d2d_get_menu_links($nids);
foreach ($mlids as $mlid) {
$menu_links[$mlid] = menu_link_load($mlid);
}
file_put_contents('public://export/menu_links.export', drupal_var_export($menu_links));
$files = deploy_d2d_get_files($nids);
file_put_contents('public://export/files.export', drupal_var_export($files));
drupal_set_message(t('Export: succeeded (files are in public://export/)'));
}
function deploy_d2d_import_form_submit($form, &$form_state) {
set_time_limit(0);
if ($form_state['values']['delete'] == 1) {
$nids = array();
if (isset($form_state['values']['domain']) && (int) ($form_state['values']['domain']) > 0) {
$result = db_query("SELECT da.nid FROM {domain_access} da where da.gid=:gid and realm='domain_id' ", array(':gid' => $form_state['values']['domain']));
foreach ($result as $row) {
$nids[] = $row->nid;
}
}
else {
$result = db_query('SELECT n.nid FROM {node} n');
foreach ($result as $row) {
$nids[$row->nid] = $row->nid;
}
}
node_delete_multiple($nids);
drupal_set_message(t('Delete @countnode nodes', array('@countnode' => count($nids))));
}
$nodes = array();
$taxos = array();
$users = array();
$menu_links = array();
$nodes_import = array();
$nodes_matches = array();
$taxo_import = array();
$users_import = array();
$mouchard = 0;
// Get data
$files_file_import = file_get_contents('public://import/files.export');
$files_file = deploy_d2d_drupal_import($files_file_import);
$nodes_file_import = file_get_contents('public://import/nodes.export');
$nodes_import = deploy_d2d_drupal_import($nodes_file_import);
$taxo_file_import = file_get_contents('public://import/taxonomies.export');
$taxo_import = deploy_d2d_drupal_import($taxo_file_import);
$users_file_import = file_get_contents('public://import/users.export');
$users_import = deploy_d2d_drupal_import($users_file_import);
$menu_links_file_import = file_get_contents('public://import/menu_links.export');
$menu_links_import = deploy_d2d_drupal_import($menu_links_file_import);
$i = 0;
// Import
//users :
$users = deploy_d2d_process_users_import($users_import);
// nodes
foreach ($nodes_import as $current_node) {
if (!array_key_exists($current_node->nid, $nodes)) {
$nodes[$current_node->nid] = deploy_d2d_process_node_import($current_node, $nodes_import, $taxo_import, $users_import, $nodes, $taxos, $users, $files_file, $i);
}
}
drupal_set_message(t('Import @countnode nodes', array('@countnode' => count($nodes))));
// Import menus links
foreach ($menu_links_import as $menu_link) {
if (!array_key_exists($menu_link['mlid'], $menu_links)) {
$menu_links[$menu_link['mlid']] = deploy_d2d_process_menu_link_import($menu_link, $menu_links_import, $nodes, $menu_links);
}
}
drupal_set_message(t('Import @countnode menu links', array('@countnode' => count($menu_link))));
drupal_set_message(t('Import: succeeded'));
$file = DRUPAL_ROOT . '/includes/utility.inc';
if (is_file($file)) {
require_once $file;
}
file_put_contents('public://import/nodes-report.import', drupal_var_export($nodes));
file_put_contents('public://import/taxos-report.import', drupal_var_export($taxos));
file_put_contents('public://import/users-report.import', drupal_var_export($users));
file_put_contents('public://import/menus-links-report.import', drupal_var_export($menu_links));
}
/**
* Import callback.
*/
function deploy_d2d_drupal_import($code_string) {
if (substr(ltrim($code_string), 0, 6) == "array(") {
$nodes = eval('return ' . $code_string . ';');
if (is_array($nodes)) {
return deploy_d2d_drupal_decode_objects($nodes);
}
}
}
/**
* Recursively convert arrays back to objects.
*
* This is only for backwards compatibility with the deprecated node_code format.
*/
function deploy_d2d_drupal_decode_objects($array) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$array[$k] = deploy_d2d_drupal_decode_objects($v);
}
}
if (isset($array['#_export_node_encode_object'])) {
unset($array['#_export_node_encode_object']);
$array = (object) $array;
}
return $array;
}
function deploy_d2d_get_nodes($domain) {
if (module_exists('domain')) {
$query = db_select('domain_access', 'da');
$query
->condition('da.gid', $domain)
->fields('da', array('nid'));
}
else {
$query = db_select('node', 'n');
$query
->fields('n', array('nid'));
}
$result = $query->execute()->fetchCol();
return $result;
}
function deploy_d2d_get_menu_links($nids) {
$query = db_query("SELECT mlid FROM {menu_links} WHERE REPLACE(link_path, 'node/', '') IN (:nids) ORDER BY mlid ASC", array(
':nids' => $nids,
));
return $query->fetchCol();
}
function deploy_d2d_get_files($nids = NULL) {
$files = array();
$result = db_query('SELECT m.fid FROM {file_managed} m');
foreach ($result as $row) {
if ($file = file_load($row->fid)) {
$files[$file->fid] = (array) $file;
$files[$file->fid]['url'] = file_create_url($file->uri);
}
}
return $files;
}
function deploy_d2d_process_menu_link_import($menu_link, $menu_links_import, $nodes, &$menu_links) {
$mlid = $menu_link['mlid'];
$menu_link_destination = $menu_link;
unset($menu_link_destination['mlid']);
unset($menu_link_destination['plid']);
if (isset($menu_link['link_path']) && substr($menu_link['link_path'], 0, 5) == 'node/') {
$ref_nid = (int) substr($menu_link['link_path'], 5);
$menu_link_destination['link_path'] = 'node/' . $nodes[$ref_nid]->nid;
$menu_link_destination['href'] = 'node/' . $nodes[$ref_nid]->nid;
if ($menu_link['plid'] != 0 && !array_key_exists($menu_link['plid'], $menu_links)) {
if (array_key_exists($menu_link['plid'], $menu_links_import)) {
$menu_links[$menu_link['plid']] = deploy_d2d_process_menu_link_import($menu_links_import[$menu_link['plid']], $menu_links_import, $nodes, $menu_links);
$menu_link_destination['plid'] = $menu_links[$menu_link['plid']]['mlid'];
}
else {
unset($menu_link_destination['plid']);
}
}
else {
$menu_link_destination['plid'] = $menu_links[$menu_link['plid']]['mlid'];
}
menu_link_save($menu_link_destination);
$menu_links[$mlid] = $menu_link_destination;
return $menu_link_destination;
}
return FALSE;
}
/**
* Import a node.
*/
function deploy_d2d_process_node_import($node, $nodes_import, $taxo_import, $users_import, &$nodes, &$taxos, &$users, &$files, &$i) {
$node_destination = FALSE;
$nid = $node->nid;
// Check if node was not already imported.
if (!array_key_exists($node->nid, $nodes)) {
// Clone the old node into a new one.
$node_destination = clone $node;
// Try to keep the UUID data to avoid duplicate nodes.
if (module_exists('uuid') && function_exists('entity_uuid_load') && $node->uuid && $node_destinationuuuid = entity_uuid_load('node', array($node->uuid))) {
$node_destinationuuuid = $node_destinationuuuid[key($node_destinationuuuid)];
$node_destination->uid = $node->uid;
$node_destination->revision_uid = $node->revision_uid;
$node_destination->vid = $node_destinationuuuid->vid;
$node_destination->nid = $node_destinationuuuid->nid;
$node_destination->tnid = $node_destinationuuuid->tnid;
$node_destination->vuuid = $node_destinationuuuid->vuuid;
$node_destination->uuid = $node_destinationuuuid->uuid;
}
else {
unset($node_destination->vid);
unset($node_destination->nid);
unset($node_destination->tnid);
}
// Assign the real user because uid might have changed.
$user = $users[$node->uid];
if ($user->uid) {
$node_destination->uid = $user->uid;
}
// Extract fields data and process it later.
$fields = array();
foreach ($node_destination as $field_name => $field) {
if (substr($field_name, 0, 5) == 'field') {
if (is_array($node_destination->{$field_name}) && count($node_destination->{$field_name}) == 1) {
$langcode = array_keys($node_destination->{$field_name});
$langcode = array_shift($langcode);
$fields[$field_name]['data'] = $node_destination->{$field_name}[$langcode];
$fields[$field_name]['langcode'] = $langcode;
}
}
}
// Prepare node for saving.
node_object_prepare($node_destination);
$node_destination = node_submit($node_destination);
// Get a NID of this new node but without saving his fields at first.
try {
node_save($node_destination);
} catch (Exception $e) {
// Error while trying to save node data. Log the error.
drupal_set_message(t('Error when saving node %nid% : %error%. Please check this node manually.', array('%nid%' => $node_destination->nid, '%error%' => $e->getMessage())), 'warning');
return FALSE;
}
// Mark the node as already imported.
$nodes[$nid] = $node_destination;
// Iterate over fields and process them.
foreach ($fields as $field_name => $field) {
$field_info = field_info_field($field_name);
$langcode = $field['langcode'];
$field_data = $field['data'];
switch ($field_info['type']) {
case 'text_long':
// Search for things like this in the HTML :
// [[{"type":"media","view_mode":"media_link","fid":"1188","attributes":{"class":"media-image","title":"immatriculation d'un commerçant individuel"}}]]
$regex = "/\\[\\[{.*\"fid\":\"(\\d*)\".*}\\]\\]/U";
foreach ($field_data as $index => $txt_data) {
if (preg_match_all($regex, $txt_data['value'], $matches) > 0) {
foreach ($matches[0] as $match_index => $match) {
$old_fid = $matches[1][$match_index];
$new_file = deploy_d2d_check_file($old_fid, $files);
if (!$new_file) {
// There was an error, display a message.
drupal_set_message(t("Error when importing file embedded '%embed%', please check the node %nid%.", array(
'%embed%' => $match,
'%nid%' => $node_destination->nid
)), 'warning');
}
else {
// Replace the fid in the text.
$new_match = str_replace($old_fid, $new_file->fid, $match);
// Replace the match in the HTML.
$node_destination->{$field_name}[$langcode][$index]['value'] = str_replace($match, $new_match, $node_destination->{$field_name}[$langcode][$index]['value']);
$node_destination->{$field_name}[$langcode][$index]['safe_value'] = str_replace($match, $new_match, $node_destination->{$field_name}[$langcode][$index]['safe_value']);
}
}
}
}
break;
case 'link_field':
foreach ($field_data as $index => $link_data) {
if (isset($link_data['url']) && substr($link_data['url'], 0, 5) == 'node/') {
$ref_nid = (int) substr($link_data['url'], 5);
// Check if the node was already imported.
if (!array_key_exists($ref_nid, $nodes) && $ref_nid != $nid) {
// Check if the node is in the export.
if (array_key_exists($ref_nid, $nodes_import)) {
// The node is in the export and was not already imported.
// Import it and its data.
$new_node = deploy_d2d_process_node_import($nodes_import[$ref_nid], $nodes_import, $taxo_import, $users_import, $nodes, $taxos, $users, $files, $i);
$node_destination->{$field_name}[$langcode][$index]['url'] = 'node/' . $new_node->nid;
// Mark the node as imported.
$nodes[$ref_nid] = $new_node;
}
else {
// The node is not in the export... unset it ?
// @TODO make sure it's the proper behavior.
unset($node_destination->{$field_name}[$langcode][$index]);
}
}
else {
// The node was already imported. Only set its real new nid.
$node_destination->{$field_name}[$langcode][$index]['url'] = 'node/' . $nodes[$ref_nid]->nid;
}
}
}
break;
case 'image':
foreach ($field_data as $index => $image_data) {
if (isset($image_data['fid'])) {
$new_file = deploy_d2d_check_file($image_data['fid'], $files);
if (!$new_file) {
// There was an error, display a message.
drupal_set_message(t("Error when importing image %url%, please check the node %nid%.", array(
'%url%' => $image_data['filename'],
'%nid%' => $node_destination->nid
)), 'warning');
}
else {
// Add the file to the node.
$node_destination->{$field_name}[$langcode][$index] = (array) $new_file;
}
}
}
break;
case 'entityreference':
foreach ($field_data as $index => $target_id) {
if (isset($target_id['target_id'])) {
$ref_nid = (int) $target_id['target_id'];
// Check if the node was already imported.
if (!array_key_exists($ref_nid, $nodes)) {
// Check if the node is in the export.
if (array_key_exists($ref_nid, $nodes_import)) {
// The node is in the export and was not already imported.
// Import it and its data.
$new_node = deploy_d2d_process_node_import($nodes_import[$ref_nid], $nodes_import, $taxo_import, $users_import, $nodes, $taxos, $users, $files, $i);
$node_destination->{$field_name}[$langcode][$index]['target_id'] = $new_node->nid;
// Mark the node as imported.
$nodes[$ref_nid] = $new_node;
}
else {
// The node is not in the export... unset it ?
// @TODO make sure it's the proper behavior.
unset($node_destination->{$field_name}[$langcode][$index]);
}
}
else {
// The node was already imported. Only set its real new nid.
$node_destination->{$field_name}[$langcode][$index]['target_id'] = $nodes[$ref_nid]->nid;
}
}
}
break;
case 'taxonomy_term_reference':
foreach ($field_data as $index => $term_data) {
if (isset($term_data['tid'])) {
$tid = (int) $term_data['tid'];
// Check if the term was already imported.
if (!array_key_exists($tid, $taxos)) {
// Check if the term is in the export.
if (array_key_exists($tid, $taxo_import)) {
// The term is in the export and was not already imported.
// Import it and its data.
$new_term = deploy_d2d_check_taxo($tid, 1, $taxo_import);
$node_destination->{$field_name}[$langcode][$index]['tid'] = $new_term->tid;
// Mark the term as imported.
$taxos[$tid] = $new_term;
}
else {
// The term is not in the export... unset it ?
// @TODO make sure it's the proper behavior.
unset($node_destination->{$field_name}[$langcode][$index]);
}
}
else {
// The term was already imported. Only set its real new tid.
$node_destination->{$field_name}[$langcode][$index]['tid'] = $taxos[$tid]->tid;
}
}
}
break;
default:
break;
}
}
try {
// Save the node with all its final data.
$i++;
node_save($node_destination);
} catch (Exception $e) {
// Error while trying to save node data. Log the error.
drupal_set_message(t('Error when saving node %nid% : %error%.', array('%nid%' => $node_destination->nid, '%error%' => $e->getMessage())), 'error');
return FALSE;
}
}
return $node_destination;
}
function deploy_d2d_process_users_import($users_import) {
$users_destination = array();
foreach ($users_import as $user) {
$uid = $user->uid;
if ($uid > 1) {
// Try to load by email.
$user_destination = ($user->mail) ? user_load_by_mail($user->mail) : FALSE;
// Try to load by name.
if (!$user_destination || $user_destination->uid == 0) {
$user_destination = user_load_by_name($user->name);
}
// Try to load by UUID.
if (!$user_destination || $user_destination->uid == 0) {
if (module_exists('uuid') && function_exists('entity_uuid_load') && $user->uuid && $user_destinationuuuid = entity_uuid_load('user', array($user->uuid))) {
unset($user->uid);
$user->uid = $user_destinationuuuid->uid;
$user->uuid = $user_destinationuuuid->uuid;
$user_destination = user_save($user);
}
else {
unset($user->uid);
$user_destination = user_save($user);
}
}
else {
$user->uid = $user_destination->uid;
$user_destination = user_save($user);
}
}
else {
$user_destination = $user;
}
$users[$uid] = $user_destination;
}
return $users;
}
/**
* Check if taxonomy term already exists.
*
* @param int $tid
* The term id.
* @param int $vid
* The version id.
* @param array $exported
* All exported terms to import.
*
* @return \stdClass
*/
function deploy_d2d_check_taxo($tid, $vid, array $exported) {
$term = null;
// Get the exported term data.
$old_term = $exported[$tid];
// Try to get an existing term by name.
$term = taxonomy_get_term_by_name($old_term->name, $vid);
// Try to get an existing term by uuid.
if (!$term && module_exists('uuid') && function_exists('entity_uuid_load') && $old_term->uuid) {
$term = entity_uuid_load('taxonomy_term', array($old_term->uuid));
if (is_array($term)) {
$term = array_shift($term);
}
}
if (!$term) {
// Unable to find an existing term. Import the term as a new one.
$term = deploy_d2d_create_taxonomy_term($old_term);
}
return $term;
}
/**
* Create a taxonomy term and return the tid.
*
* @param object $old_term
* The old term that is imported.
*
* @return object
* The new imported term.
*/
function deploy_d2d_create_taxonomy_term($old_term) {
$term = new stdClass();
$term->name = $old_term->name;
$term->vid = $old_term->vid;
$term->parents = $old_term->parents; // @TODO for now, doesn't support hierarch. Adapt if necessary.
if (module_exists('uuid') && isset($old_term->uuid)) {
// Save the UUID for future imports.
$term->uuid = $old_term->uuid;
}
// Save the new term.
taxonomy_term_save($term);
return $term;
}
/**
* Check if user aready exists
*/
function deploy_d2d_check_user($uid, $users_import_order) {
$user = clone $users_import_order[$uid];
if (user_load($user->uid) !== FALSE) {
$user_local = user_load($user->uid);
if ($user_local->name == $user->name) {
return $user;
}
}
return deploy_d2d_create_user($user);
}
/**
* Create a user and return the uid.
*/
function deploy_d2d_create_user($user) {
unset($user->uid);
user_save($user);
return $user;
}
/**
* Check if a file already exists.
*
* @param int $fid
* The file fid.
* @param array $files
* All exported files.
*
* @return \stdClass
*/
function deploy_d2d_check_file($fid, array $files) {
$file = null;
// Get the exported file data.
$old_file = $files[$fid];
// Try to get an existing file by URI.
$file = file_load_multiple(array(), array('uri' => $old_file['uri']));
if (is_array($file) && count($file) == 1) {
$file = array_shift($file);
}
// Try to get an existing file by UUID.
if (!$file && module_exists('uuid') && function_exists('entity_uuid_load') && isset($old_file['uuid'])) {
$file = entity_uuid_load('file', array($old_file['uuid']));
if (is_array($file)) {
$file = array_shift($file);
}
}
if (!$file) {
// Unable to find an existing file. Import the file as a new one.
$file = download_external_file($old_file['url'], $old_file['uri'], FILE_EXISTS_RENAME, TRUE);
if ($file) {
$file->uuid = $old_file['uuid'];
file_save($file);
}
}
return $file;
}
/**
* Download an external file when importing files.
*
* @see http://www.patrickjwaters.com/blog/2013-08-22/drupal-7-how-programmatically-download-files-optionally-save-them-managed-files-save
*
* @param string $url
* Full url to file to download
* @param string $uri
* Drupal uri of where to save file public://archive/test.pdf
* @param int $save_mode
* File save mode from drupal core, ex FILE_EXISTS_REPLACE
* @param bool $manage_file
* Manage file by drupal ? Defaults to TRUE.
*
* @return object|bool
* The file or FALSE if an error occurred.
*/
function download_external_file($url, $uri, $save_mode = FILE_EXISTS_RENAME, $manage_file = TRUE) {
$url_info = parse_url($url);
$url_path_info = pathinfo($url_info['path']);
// This helps with filename with spaces.
$url = $url_info['scheme'] . '://' . $url_info['host'] . $url_path_info['dirname'] . '/' . rawurlencode($url_path_info['basename']);
// Need to remove the filename from the uri.
$uri_target = file_uri_target($uri);
$uri_scheme = file_uri_scheme($uri);
$uri_path_info = pathinfo($uri_target);
$directory = file_stream_wrapper_uri_normalize($uri_scheme . "://" . $uri_path_info['dirname']);
// Create the directory.
if (file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
// HTTP request to get the image.
$drupal_result = drupal_http_request($url);
if (!empty($drupal_result->data)) {
$path = file_stream_wrapper_uri_normalize($uri);
if ($manage_file) {
$new_file = file_save_data($drupal_result->data, $path, $save_mode);
if (!$new_file) {
// Error while saving the file. Log the error.
watchdog('deploy_d2d', 'Error saving file %file%.', array('%file%' => $url), WATCHDOG_WARNING);
return FALSE;
}
}
else {
return file_unmanaged_save_data($drupal_result->data, $path, $save_mode);
}
}
else {
// Error while getting the file. Log the error.
watchdog('deploy_d2d', 'Error download file %file% - %error%.', array('%file%' => $url, '%error%' => $drupal_result->error), WATCHDOG_WARNING);
return FALSE;
}
$new_file->display = 1;
$new_file->status = FILE_STATUS_PERMANENT;
return $new_file;
}
else {
// Error while creating directory. Log the error.
watchdog('deploy_d2d', 'Could not create directory %dir%.', array('%dir%' => $directory), WATCHDOG_WARNING);
return FALSE;
}
}