-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.inc
896 lines (789 loc) · 29.6 KB
/
scanner.inc
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
<?php
/**
* @file
* Shared functions for the Scanner module.
*/
/**
* Attempt to stretch the amount of time available for processing.
*
* This way timeouts won't interrupt search and replace actions. This only works
* in hosting environments where changing PHP and Apache settings on the fly is
* allowed.
*
* @param string $setting
* The name of the PHP setting to change.
* @param string $value
* The new value to assign.
* @param bool $verbose
* If set to TRUE, an extra message will be displayed indicating the status of
* the execution.
*
* @return bool
* Indicates whether the setting is changed.
*/
function _scanner_change_env($setting, $value, $verbose = FALSE)
{
$old_value = ini_get($setting);
if ($old_value != $value && $old_value != 0) {
if (ini_set($setting, $value)) {
if ($verbose) {
backdrop_set_message(t('%setting changed from %old_value to %value.', array(
'%setting' => $setting,
'%old_value' => $old_value,
'%value' => $value,
)));
}
return TRUE;
} else {
if ($verbose) {
backdrop_set_message(t('%setting could not be changed from %old_value to %value.', array(
'%setting' => $setting,
'%old_value' => $old_value,
'%value' => $value,
)), 'error');
}
return FALSE;
}
}
return FALSE;
}
/**
* Comparison function for sorting fields by table/field label.
*
* @param array $left
* One field.
* @param array $right
* The other field.
*
* @return int
* Comparison value determining which order these two fields should be sorted
* in relation to each other based on field label.
*/
function _scanner_compare_fields_by_label(array $left, array $right)
{
$cmp = strcmp($left['type'], $right['type']);
if ($cmp != 0) {
return $cmp;
}
return strcmp($left['field_label'], $right['field_label']);
}
/**
* Comparison function for sorting fields by table/field name.
*
* @param array $left
* One field.
* @param array $right
* The other field.
*
* @return int
* Comparison value determining which order these two fields should be sorted
* in relation to each other based on field name.
*/
function _scanner_compare_fields_by_name(array $left, array $right)
{
$cmp = strcmp($left['type'], $right['type']);
if ($cmp != 0) {
return $cmp;
}
return strcmp($left['field'], $right['field']);
}
/**
* Handles the actual search and replace.
*
* @param string $searchtype
* The type of search to perform, defaults to 'search'.
*
* @return string
* The themed results.
*/
function _scanner_execute($searchtype = 'search')
{
global $user;
// Variables to monitor possible timeout.
$max_execution_time = ini_get('max_execution_time');
$start_time = REQUEST_TIME;
$expanded = FALSE;
// Get process and undo data if saved from timeout.
$config = config('scanner.settings');
$processed = $_SESSION['scanner_partially_processed'] ?? [];
$undo_data = $_SESSION['scanner_partial_undo'] ?? [];
// Get the field collection field to use when joining revisions.
$fc_revision_field = db_table_exists('field_collection_item_revision') ? 'revision_id' : 'value';
unset($_SESSION['scanner_status']);
$search = $_SESSION['scanner_search'];
$replace = $_SESSION['scanner_replace'];
$preceded = $_SESSION['scanner_preceded'];
$followed = $_SESSION['scanner_followed'];
$mode = $_SESSION['scanner_mode'];
// Case sensitivity flag for use in php preg_search and preg_replace.
$flag = $mode ? NULL : 'i';
$wholeword = $_SESSION['scanner_wholeword'];
$regex = $_SESSION['scanner_regex'];
$published = $_SESSION['scanner_published'];
$pathauto = $_SESSION['scanner_pathauto'];
$terms = $_SESSION['scanner_terms'] ?? NULL;
$results = NULL;
//dpm($_SESSION, 'Session Data');
//dpm($flag);
if ($searchtype == 'search') {
backdrop_set_message(t('Searching for: [%search] ...', ['%search' => $search]));
}
// Searchtype == 'replace'.
else {
backdrop_set_message(t('Replacing [%search] with [%replace] ...', [
'%search' => $search,
'%replace' => $replace,
]));
}
$preceded_php = '';
if (!empty($preceded)) {
if (!$regex) {
$preceded = addcslashes($preceded, SCANNER_REGEX_CHARS);
}
$preceded_php = '(?<=' . $preceded . ')';
}
$followed_php = '';
if (!empty($followed)) {
if (!$regex) {
$followed = addcslashes($followed, SCANNER_REGEX_CHARS);
}
$followed_php = '(?=' . $followed . ')';
}
// Case 1.
if ($wholeword && $regex) {
$where = "[[:<:]]" . $preceded . $search . $followed . "[[:>:]]";
$search_php = '\b' . $preceded_php . $search . $followed_php . '\b';
}
// Case 2.
elseif ($wholeword && !$regex) {
$where = "[[:<:]]" . $preceded . addcslashes($search, SCANNER_REGEX_CHARS) . $followed . "[[:>:]]";
$search_php = '\b' . $preceded_php . addcslashes($search, SCANNER_REGEX_CHARS) . $followed_php . '\b';
}
// Case 3.
elseif (!$wholeword && $regex) {
$where = $preceded . $search . $followed;
$search_php = $preceded_php . $search . $followed_php;
}
// Case 4.
// !wholeword and !regex:
else {
$where = $preceded . addcslashes($search, SCANNER_REGEX_CHARS) . $followed;
$search_php = $preceded_php . addcslashes($search, SCANNER_REGEX_CHARS) . $followed_php;
}
// If terms selected, then put together extra join and where clause.
$join = '';
if (is_array($terms) && count($terms)) {
$terms_where = [];
$terms_params = [];
foreach ($terms as $term) {
$terms_where[] = 'tn.tid = %d';
$terms_params[] = $term;
}
$join = 'INNER JOIN {taxonomy_term_node} tn ON t.nid = tn.nid';
$where .= ' AND (' . implode(' OR ', $terms_where) . ')';
}
$table_map = _scanner_get_selected_tables_map();
//dpm($table_map);
usort($table_map, '_scanner_compare_fields_by_name');
// Examine each field instance as chosen in settings.
foreach ($table_map as $map) {
$table = $map['table'];
$field = $map['field'];
$field_label = $map['field_label'];
$type = $map['type'];
$module = $map['module'] ?? NULL;
$field_collection_parents = $map['field_collection_parents'] ?? NULL;
// Allow the table suffix to be altered.
$suffix = 'value';
// Trigger hook_scanner_field_suffix_alter().
backdrop_alter('scanner_field_suffix', $suffix, $map);
$query = db_select($table, 't');
if ($table == 'node_revision') {
$vid = 'vid';
} else {
$field = $field . '_' . $suffix;
$field_label = $field_label . '_' . $suffix;
$vid = 'revision_id';
}
if (!empty($field_collection_parents)) {
$cnt_fc = count($field_collection_parents);
// Loop through the parents backwards, so that the joins can all be
// created.
for ($i = $cnt_fc; $i > 0; $i--) {
$fc_this = $field_collection_parents[$i - 1];
$fc_alias = 'fc' . ($i - 1);
$fc_table = 'field_revision_' . $fc_this;
$prev_alias = ($i == $cnt_fc) ? 't' : ('fc' . $i);
$query->join(
$fc_table,
$fc_alias,
format_string(
'!PREV_ALIAS.entity_id = !FC_ALIAS.!FC_THIS_value AND !PREV_ALIAS.revision_id = !FC_ALIAS.!FC_THIS_!FC_REV',
[
'!FC_ALIAS' => $fc_alias,
'!FC_THIS' => $fc_this,
'!PREV_ALIAS' => $prev_alias,
'!FC_REV' => $fc_revision_field,
]
)
);
if ($i == 1) {
$query->join(
'node',
'n',
format_string('!FC_ALIAS.entity_id = n.nid AND !FC_ALIAS.revision_id = n.vid', ['!FC_ALIAS' => $fc_alias])
);
}
}
} else {
// Only add the node join if the field is not paragraph-specific.
if (strpos($type, 'paragraph:') === FALSE) {
$query->join('node', 'n', 't.' . $vid . ' = n.vid');
}
}
if (is_array($terms) && !empty($terms)) {
$terms_or = db_or();
$query->join('taxonomy_index', 'tx', 'n.nid = tx.nid');
foreach ($terms as $term) {
$terms_or->condition('tx.tid', $term);
}
$query->condition($terms_or);
}
// Add correct suffix for paragraph fields.
if (strpos($map['type'], 'paragraph:') !== FALSE) {
$query->distinct();
// Ensure '_value' is only added if not already present.
if (!str_ends_with($field, '_value')) {
$field .= '_value';
}
// Add fields to SELECT for paragraphs.
$query->addField('t', $field, 'content');
$query->addField('t', 'bundle', 'bundle');
$query->fields('t', ['delta']);
$query->addField('pi', 'item_id', 'entity_id'); // Add entity_id for paragraph replacement.
// Join with paragraphs_item table.
$query->join('paragraphs_item', 'pi', 'pi.item_id = t.entity_id AND pi.revision_id = t.revision_id');
// Dynamically determine the field_name for the paragraphs reference.
$field_name = db_query("
SELECT DISTINCT field_name
FROM {paragraphs_item}
WHERE bundle = :bundle
LIMIT 1
", [':bundle' => str_replace('paragraph:', '', $map['type'])])->fetchField();
if (!$field_name) {
throw new Exception('Could not determine the field name for paragraphs.');
}
// Construct the paragraphs field table dynamically.
$paragraphs_field_table = 'field_data_' . $field_name;
// Join with the dynamically determined paragraphs field table.
$query->join($paragraphs_field_table, 'fp', "fp.{$field_name}_value = pi.item_id");
// Join with the node table to get node information.
$query->join('node', 'n', 'n.nid = fp.entity_id');
// Add node-specific fields.
$query->addField('n', 'nid', 'nid');
$query->addField('n', 'title', 'title');
// Filter by paragraph bundle.
$query->condition('t.bundle', str_replace('paragraph:', '', $type), '=');
} else {
// Regular node-based fields.
$query->addField('t', $field, 'content');
if ($table != 'node_revision') {
$query->fields('t', ['delta']);
}
// Add node-specific fields.
$query->fields('n', ['nid', 'title']);
$query->condition('n.type', $type, '=');
// Filter by node type.
$query->condition('n.type', $type, '=');
// Retrieve the search term from the session.
$search_term = $_SESSION['scanner_search'] ?? '';
if (empty($search_term)) {
throw new Exception('Search term is required but not provided.');
}
//dpm($search_term, 'Search Term');
// Properly construct the CONVERT statement with enforced spacing.
$charset_conversion = "CONVERT(t.$field USING utf8mb4)";
//dpm($charset_conversion, 'Charset Conversion');
// Build the base where condition.
$query->where("(CONVERT(t.$field USING utf8mb4) REGEXP :search_term)", [':search_term' => $search_term]);
// Check if a summary field exists and add its condition directly.
if (!empty($summary_field)) {
$query->where("(CONVERT(t.$summary_field USING utf8mb4) REGEXP :search_term)", [':search_term' => $search_term]);
}
// Add debugging to ensure conditions are constructed correctly.
//dpm((string) $query, 'Query After Adding Conditions');
if ($published) {
$query->condition('n.status', '1', '=');
}
}
// Triggers hook_scanner_query_alter().
// Allow the query to be modified.
backdrop_alter('scanner_query', $query, $map, $table, $field);
// Debug the constructed SQL query.
//dpm($field_name, 'Dynamic Paragraph Field Name');
//dpm((string) $query, 'Constructed SQL Query');
$result = $query->execute();
$shutting_down = FALSE;
// Perform the search or replace on each hit for the current field instance.
foreach ($result as $row) {
//dpm($row);
// Results of an entity property, e.g. the node title, won't have a
// 'delta' attribute, so make sure there is one.
if (!isset($row->delta)) {
$row->delta = 0;
}
$content = $row->content;
$summary = $row->summary ?? '';
$matches = [];
$text = '';
// If the max_execution_time setting has been set then check for possible
// timeout. If within 5 seconds of timeout, attempt to expand environment.
if ($max_execution_time > 0 && REQUEST_TIME >= ($start_time + $max_execution_time - 5)) {
if (!$expanded) {
if ($user->uid > 0) {
$verbose = TRUE;
} else {
$verbose = FALSE;
}
if (_scanner_change_env('max_execution_time', '600', $verbose)) {
backdrop_set_message(t('Default max_execution_time too small and changed to 10 minutes.'), 'error');
$max_execution_time = 600;
}
$expanded = TRUE;
}
// If expanded environment still running out of time, shutdown process.
else {
$shutting_down = TRUE;
$config->set('scanner_partially_processed_' . $user->uid, $processed);
$config->set('scanner_partial_undo_' . $user->uid, $undo_data);
$config->save();
if ($searchtype == 'search') {
backdrop_set_message(t('Did not have enough time to complete search.'), 'error');
} else {
backdrop_set_message(t('Did not have enough time to complete. Please re-submit replace'), 'error');
}
break 2;
}
}
$node = node_load($row->nid);
//dpm($content, 'Search Target');
//dpm($flag, 'Regex Flag');
// Build the regular expression used later.
$regexstr = "/$search_php/$flag";
//dpm($regexstr);
// Search.
if ($searchtype == 'search') {
$matches = ['0' => []];
$hits = 0;
// Assign matches in the base text field to $matches[0].
// Trigger hook_scanner_preg_match_all().
foreach (module_implements('scanner_preg_match_all') as $module_name) {
$function = $module_name . '_scanner_preg_match_all';
$new_matches = [];
$hits += $function($new_matches, $regexstr, $row);
$matches = array_merge($matches, $new_matches);
}
if ($hits > 0) {
$context_length = 70;
$text .= '<ul>';
foreach ($matches as $key => $item) {
$string = $key == 0 ? $content : $summary;
foreach ($item as $match) {
//dpm($match, "Individual Match");
$text .= '<li>';
// If ($key == 1) {
// $text .= '<i>Summary:</i> ';
// }.
// Don't want substr to wrap.
$start = $match[1] - $context_length > 0 ? $match[1]
- $context_length : 0;
// If the match is close to the beginning of the string, need
// less context.
$length = $match[1] >= $context_length ? $context_length
: $match[1];
if ($prepend = substr($string, $start, $length)) {
if ($length == $context_length) {
$text .= '...';
}
$text .= htmlentities($prepend, ENT_COMPAT, 'UTF-8');
}
$text .= '<strong>' . htmlentities($match[0], ENT_COMPAT, 'UTF-8')
. '</strong>';
if ($append = substr(
$string,
$match[1] + strlen($match[0]),
$context_length
)) {
$text .= htmlentities($append, ENT_COMPAT, 'UTF-8');
if (
strlen($string) - ($match[1] + strlen($match[0]))
> $context_length
) {
$text .= '...';
}
}
$text .= '</li>';
}
}
$text .= '</ul>';
} else {
$text
= '<div class="messages warning"><h2 class="element-invisible">Warning message</h2>'
. t(
"Can't display search result due to conflict between search term and internal preg_match_all function."
)
. '</div>';
}
if ($hits > 0) {
$results[] = [
'title' => $row->title,
'type' => $type,
'count' => $hits,
'field' => $field,
'field_label' => $field_label,
'nid' => $row->nid,
'text' => $text,
];
//dpm($results, 'Results Updated');
} else {
//dpm('No matches for this field, skipping.', 'Skip Adding to
// Results');
}
}
// Replace (and check to see if already processed).
elseif (!isset($processed[$field][$row->nid][$row->delta])) {
// Check first if pathauto_persist, a newer version of pathauto, or some
// other module has already set $node->path['pathauto']. If not, set it
// to false (to prevent pathauto from touching the node during
// node_save()) if a custom alias exists that doesn't follow pathauto
// rules.
if (!isset($node->path['pathauto']) && module_exists('pathauto') && $pathauto) {
[$id,, $bundle] = entity_extract_ids('node', $node);
if (!empty($id)) {
module_load_include('inc', 'pathauto');
$uri = entity_uri('node', $node);
$path = backdrop_get_path_alias($uri['path']);
$pathauto_alias = pathauto_create_alias('node', 'return', $uri['path'], ['node' => $node], $bundle);
$node->path['pathauto'] = ($path != $uri['path'] && $path == $pathauto_alias);
}
}
$hits = 0;
preg_match('/(.+)_' . $suffix . '$/', $field, $matches);
// Paragraphs.
$processed_paragraphs = []; // Keep track of saved paragraphs.
if (strpos($type, 'paragraph:') !== FALSE) {
$paragraph = paragraphs_item_load($row->entity_id);
//dpm($paragraph);
if (!$paragraph) {
backdrop_set_message(t('Failed to load paragraph with ID %id.', ['%id' => $row->entity_id]), 'error');
continue;
}
preg_match('/(.+)_value$/', $field, $matches);
$base_field_name = $matches[1] ?? $field;
if (!isset($paragraph->{$base_field_name})) {
backdrop_set_message(t('Field %field does not exist on paragraph %id.', [
'%field' => $base_field_name,
'%id' => $row->entity_id,
]), 'warning');
continue;
}
$old_paragraph_vid = $paragraph->revision_id;
//dpm($old_paragraph_vid);
if (!isset($undo_data[$node->nid])) {
$undo_data[$node->nid] = [
'old_vid' => $node->vid,
'paragraphs' => [],
];
}
$undo_data[$node->nid]['paragraphs'][$paragraph->id()] = [
'old_vid' => $old_paragraph_vid,
];
$hits = 0;
$field_data = &$paragraph->{$base_field_name};
foreach ($field_data as $langcode => &$values) {
foreach ($values as &$value) {
if (isset($value['value'])) {
$value['value'] = preg_replace($regexstr, $replace, $value['value'], -1, $hit_count);
$hits += $hit_count;
}
if (isset($value['safe_value'])) {
$value['safe_value'] = preg_replace($regexstr, $replace, $value['safe_value'], -1, $hit_count);
$hits += $hit_count;
}
}
}
if ($hits > 0 && !isset($processed_paragraphs[$paragraph->id()])) {
$paragraph->revision = TRUE;
$paragraph->save();
$new_paragraph_vid = db_query("SELECT revision_id FROM {paragraphs_item} WHERE item_id = :item_id", [
':item_id' => $paragraph->id(),
])->fetchField();
if ($new_paragraph_vid) {
$undo_data[$node->nid]['paragraphs'][$paragraph->id()]['new_vid'] = $new_paragraph_vid;
foreach ($node->{$field_name} as $langcode => &$paragraphs) {
foreach ($paragraphs as &$paragraph_field_item) {
if ($paragraph_field_item['value'] == $paragraph->id()) {
$paragraph_field_item['revision_id'] = $new_paragraph_vid;
}
}
}
}
$processed_paragraphs[$paragraph->id()] = TRUE;
$results[] = [
'title' => $node->title ?? 'Unknown Host',
'nid' => $node->nid ?? 'N/A',
'type' => $type,
'count' => $hits,
'field' => $field,
'field_label' => $field_label,
];
} else {
backdrop_set_message(t('No changes detected for paragraph ID %id.', ['%id' => $paragraph->id()]));
}
}
// Handle field collections (unchanged from your original code).
if (!empty($field_collection_parents)) {
foreach ($node->{$field_collection_parents[0]} as $fc_lang => $fc_data) {
foreach ($fc_data as $key => $fc_item) {
$fc = field_collection_item_load($fc_item['value']);
$fc_changed = FALSE;
foreach ($fc->{$matches[1]}[LANGUAGE_NONE] as $fc_key => $fc_val) {
$fc_hits = 0;
$fc_content = preg_replace($regexstr, $replace, $fc_val[$suffix], -1, $fc_hits);
if ($fc_content != $fc_val['value']) {
$fc_changed = TRUE;
$fc->{$matches[1]}[LANGUAGE_NONE][$fc_key][$suffix] = $fc_content;
}
// Also handle the summary part of text+summary fields.
if (isset($fc_val['summary'])) {
$summary_hits = 0;
$fc_summary = preg_replace($regexstr, $replace, $fc_val['summary'], -1, $summary_hits);
if ($fc_summary != $fc_val['summary']) {
$fc_hits += $summary_hits;
$fc_changed = TRUE;
$fc->{$matches[1]}[LANGUAGE_NONE][$fc_key]['summary'] = $fc_summary;
}
}
if ($fc_hits > 0) {
$results[] = [
'title' => $node->title,
'type' => $node->type,
'count' => $fc_hits,
'field' => $field,
'field_label' => $field_label,
'nid' => $node->nid,
];
}
}
// Update field collection revision ID if necessary.
if ($fc_revision_field == 'revision_id') {
$fc->revision = 1;
}
$fc->save(TRUE);
if ($fc_revision_field == 'revision_id') {
$node->{$field_collection_parents[0]}[$fc_lang][$key]['revision_id'] = $fc->revision_id;
}
}
}
}
// Normal node fields (unchanged from your original code).
else {
if (strpos($type, 'paragraph') !== FALSE) {
continue;
}
$hits = 0;
foreach (module_implements('scanner_preg_replace') as $module_name) {
$function = $module_name . '_scanner_preg_replace';
$hits += $function($node, $field, $matches, $row, $regexstr, $replace);
}
$results[] = [
'title' => $node->title,
'nid' => $node->nid,
'type' => $node->type,
'count' => $hits,
'field' => $field,
'field_label' => $field_label,
];
//dpm($results);
}
//dpm($node);
// A revision only created for the first change of the node. Subsequent
// changes of the same node do not generate additional revisions.
// @todo Need a better way of handling this.
if (!isset($undo_data[$node->nid]['new_vid'])) {
$node->revision = TRUE;
$node->log = t('@name replaced %search with %replace via Scanner Search and Replace module.', [
'@name' => $user->name,
'%search' => $search,
'%replace' => $replace,
]);
$undo_data[$node->nid]['old_vid'] = $node->vid;
}
node_save($node);
// Array to log completed fields in case of shutdown.
$processed[$field][$row->nid][$row->delta] = TRUE;
// Undo data construction.
// Now set to updated vid after node_save().
$undo_data[$node->nid]['new_vid'] = $node->vid;
// Update paragraph revisions in the undo data.
foreach ($node as $field_name => $field_value) {
// Check if this field is a paragraph field.
$field_info = field_info_field($field_name);
if ($field_info && $field_info['type'] === 'paragraphs') {
if (!empty($node->{$field_name}['und'])) {
foreach ($node->{$field_name}['und'] as $paragraph_ref) {
$paragraph = entity_load('paragraphs_item', [$paragraph_ref['value']]);
if (!empty($paragraph)) {
$paragraph = reset($paragraph); // Get the loaded entity.
// Update the paragraph's new_vid after node save.
$undo_data[$node->nid]['paragraphs'][$paragraph->item_id]['new_vid'] = $paragraph->revision_id;
}
}
}
}
}
// Debugging outputs to verify the results.
//dpm($node);
//dpm($undo_data);
}
}
}
// If completed.
if (isset($shutting_down) && !$shutting_down) {
$config->clear('scanner_partially_processed_' . $user->uid);
$config->clear('scanner_partial_undo_' . $user->uid);
$config->save();
}
if ($searchtype == 'search') {
return theme('scanner_results', ['results' => $results]);
}
// Searchtype == 'replace'.
// Searchtype == 'replace'.
else {
// Ensure $undo_data is initialized as an array if it is null or undefined.
$undo_data = $undo_data ?? [];
//dpm($undo_data);
if (count($undo_data) && !$shutting_down) {
db_insert('scanner')
->fields([
'undo_data' => serialize($undo_data),
'undone' => 0,
'searched' => $search,
'replaced' => $replace,
'count' => count($undo_data),
'time' => REQUEST_TIME,
])
->execute();
}
return theme('scanner_replace_results', ['results' => $results]);
}
}
/**
* Get all text fields.
*
* @return array
* List of all fields, each of which is an array containing relevant data
* used for diplaying/querying.
*/
/**
* Get all text fields.
*
* @return array
* List of all fields, each of which is an array containing relevant data
* used for displaying/querying.
*/
/**
* Get all text fields.
*
* @return array
* List of all fields, each of which is an array containing relevant data
* used for displaying/querying.
*/
function _scanner_get_all_tables_map()
{
$tables_map = [];
// Add node fields, including the title.
foreach (node_type_get_types() as $node_type) {
// Add the title field explicitly.
$tables_map[] = [
'type' => $node_type->type,
'field' => 'title',
'field_label' => t('Title'),
'table' => 'node_revision',
];
$field_instances = field_info_instances('node', $node_type->type);
foreach ($field_instances as $field_name => $field_instance) {
$field_info = field_info_field($field_name);
if (in_array($field_info['type'], ['text', 'text_long', 'text_with_summary'])) {
$tables_map[] = [
'type' => $node_type->type,
'field' => $field_name,
'field_label' => $field_instance['label'],
'table' => 'field_revision_' . $field_name,
];
}
}
}
// Add paragraph fields.
$paragraph_bundles = field_info_bundles('paragraphs_item');
foreach ($paragraph_bundles as $bundle_name => $bundle_info) {
$field_instances = field_info_instances('paragraphs_item', $bundle_name);
foreach ($field_instances as $field_name => $instance) {
$field_info = field_info_field($field_name);
if (in_array($field_info['type'], ['text', 'text_long', 'text_with_summary'])) {
$tables_map[] = [
'type' => 'paragraph:' . $bundle_name,
'field' => $field_name,
'field_label' => $instance['label'],
'table' => 'field_revision_' . $field_name,
];
}
}
}
// Trigger hook_scanner_fields_alter().
backdrop_alter('scanner_fields', $tables_map);
//dpm($tables_map);
return $tables_map;
}
/**
* Get the fields that have been selected for scanning.
*
* @return array
* Map of selected fields and tables.
*/
/**
* Get the fields that have been selected for scanning.
*
* @return array
* Map of selected fields and tables.
*/
function _scanner_get_selected_tables_map()
{
$tables_map = _scanner_get_all_tables_map();
//dpm($tables_map);
$config = config('scanner.settings');
/*foreach ($tables_map as $i => $item) {
// Handle node fields.
if (strpos($item['type'], 'paragraph:') === 0) {
// Handle paragraph fields separately.
$bundle_name = str_replace('paragraph:', '', $item['type']);
$key = 'scanner_paragraphs_' . $item['field'] . '_' . $item['table'] . '_' . $bundle_name;
} else {
// Handle node fields.
$key = 'scanner_nodes_' . $item['field'] . '_' . $item['table'] . '_' . $item['type'];
}
// Check if the field is enabled in the configuration.
if (!$config->get($key, TRUE)) {
unset($tables_map[$i]);
}
}*/
foreach ($tables_map as $i => $item) {
$key = 'scanner_' . $item['field'] . '_' . $item['table'] . '_' . $item['type'];
if (!$config->get($key, TRUE)) {
unset($tables_map[$i]);
}
}
//dpm($tables_map);
// Reindex array to maintain order and avoid gaps.
return array_values($tables_map);
}