DBBrowser/GetContent/Filter/SearchAndReplace.pm untested

filter: Statementstmt 0.0% 0 / 433 Branchbran 0.0% 0 / 126 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 666 CC 64 SCAR 53.6
CC
64
Cov
0%
search_and_replace
552.0
__execute_substitutions
240.0
__saved_search_and_replace
90.0
CRAP 213.0
1 package # hide from PAUSE
2 App::DBBrowser::GetContent::Filter::SearchAndReplace;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use List::MoreUtils qw( any );
9 use String::Substitution qw( sub_modify gsub_modify );
10  
11 use Term::Choose qw();
12 use Term::Choose::Util qw();
13 use Term::Form qw();
14  
15 use App::DBBrowser::Auxil;
16 use App::DBBrowser::GetContent::Filter;
17  
18  
19 sub new {
20 my ( $class, $info, $options, $d ) = @_;
21 my $sf = {
22 i => $info,
23 o => $options,
24 d => $d
25 };
26 bless $sf, $class;
27 return $sf;
28 }
29  
30  
31 sub search_and_replace {
32 my ( $sf, $sql, $bu_insert_args, $filter_str ) = @_;
33 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
34 my $tf = Term::Form->new( $sf->{i}{tf_default} );
35 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
36 my $cf = App::DBBrowser::GetContent::Filter->new( $sf->{i}, $sf->{o}, $sf->{d} );
37 my $aoa = $sql->{insert_args};
38 my $is_empty = $cf->__search_empty_cols( $aoa );
39 my $header = $cf->__prepare_header( $aoa, $is_empty );
40 my $saved = $ax->read_json( $sf->{i}{f_search_and_replace} ) // {};
41 my $all_sr_groups = [];
42 my $used_names = [];
43 my @bu;
44 my ( $hidden, $add ) = ( 'Your choice:', ' * New *' );
45 my $available = [ sort { $a cmp $b } keys %$saved ];
46 my $old_idx = 1;
47  
48 ADD_SEARCH_AND_REPLACE: while ( 1 ) {
49 my @tmp_info = ( $filter_str );
50 for my $sr_group ( @$all_sr_groups ) {
51 push @tmp_info, map { ' ' . $_ } _stringified_sr_group( $sr_group );
52 }
53 push @tmp_info, '' if @$all_sr_groups;
54 my @pre = ( $hidden, undef, $sf->{i}{_confirm}, $add );
55 my $prefixed_available = [];
56 for my $name ( @$available ) {
57 if ( any { $_ eq $name } @$used_names ) {
58 push @$prefixed_available, '- ' . $name . ' (used)';
59 }
60 else {
61 push @$prefixed_available, '- ' . $name;
62 }
63 }
64 my $menu = [ @pre, @$prefixed_available ];
65 my $info = $cf->__get_filter_info( $sql, join( "\n", @tmp_info ) );
66 # Choose
67 my $idx = $tc->choose(
68 $menu,
69 { %{$sf->{i}{lyt_v}}, info => $info, prompt => '', default => $old_idx, index => 1, undef => $sf->{i}{_back} }
70 );
71 if ( ! defined $idx || ! defined $menu->[$idx] ) {
72 if ( @bu ) {
73 ( $used_names, $all_sr_groups ) = @{pop @bu};
74 next ADD_SEARCH_AND_REPLACE;
75 }
76 return;
77 }
78 if ( $sf->{o}{G}{menu_memory} ) {
79 if ( $old_idx == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
80 $old_idx = 1;
81 next ADD_SEARCH_AND_REPLACE;
82 }
83 $old_idx = $idx;
84 }
85 my $choice = $menu->[$idx];
86 if ( $choice eq $hidden ) {
87 $sf->__saved_search_and_replace();
88 $saved = $ax->read_json( $sf->{i}{f_search_and_replace} ) // {};
89 $available = [ sort { $a cmp $b } keys %$saved ];
90 next ADD_SEARCH_AND_REPLACE;
91 }
92 elsif ( $choice eq $sf->{i}{_confirm} ) {
93 if ( ! @$all_sr_groups ) {
94 return;
95 }
96 $info = $cf->__get_filter_info( $sql, join( "\n", @tmp_info ) );
97 my $col_idxs = $sf->__choose_column_indexes( $header, $info, 'Apply to: ' );
98 if ( ! defined $col_idxs ) {
99 next ADD_SEARCH_AND_REPLACE;
100 }
101 if ( ! eval {
102 $sf->__execute_substitutions( $aoa, $col_idxs, $all_sr_groups ); # modifies $aoa
103 1 }
104 ) {
105 $ax->print_error_message( $@ );
106 next ADD_SEARCH_AND_REPLACE;
107 }
108 $sql->{insert_args} = $aoa;
109 my $header_changed = 0;
110 if ( $sf->{d}{stmt_types}[0] eq 'Create_Table' ) {
111 for my $i ( @$col_idxs ) {
112 if ( ! defined $sql->{insert_args}[0][$i] ) {
113 next;
114 }
115 if ( $header->[$i] ne $sql->{insert_args}[0][$i] ) {
116 $header_changed = 1;
117 last;
118 }
119 }
120 }
121 if ( $header_changed ) {
122 my ( $yes, $no ) = ( '- YES', '- NO' );
123 my $menu = [ undef, $yes, $no ];
124 my @tmp_info_addition = ( 'Header: ' . join( ', ', map { $_ // '' } @{$sql->{insert_args}[0]} ), ' ' );
125 $info = $cf->__get_filter_info( $sql, join( "\n", @tmp_info, @tmp_info_addition ) );
126 # Choose
127 my $idx = $tc->choose(
128 $menu,
129 { %{$sf->{i}{lyt_v}}, info => $info, prompt => 'Restore header?', index => 1, undef => $sf->{i}{s_back} }
130 );
131 if ( ! defined $idx || ! defined $menu->[$idx] ) {
132 if ( @$aoa * @$col_idxs > 500_000 ) {
133 $cf->__print_busy_string( 'Working ...' );
134 }
135 $sql->{insert_args} = [ map { [ @$_ ] } @{$bu_insert_args} ];
136 return;
137 }
138 my $choice = $menu->[$idx];
139 if ( $choice eq $yes ) {
140 $sql->{insert_args}[0] = $header;
141 }
142 }
143 return 1;
144 }
145 elsif ( $choice eq $add ) {
146 my $prompt = 'Build s///;';
147 my $skip = ' ';
148 my $fields = [];
149 for my $nr ( 1 .. 5 ) {
150 push @$fields,
151 [ $skip ],
152 [ $nr . ' Pattern', ],
153 [ $nr . ' Replacement', ],
154 [ $nr . ' Modifiers', ];
155 }
156 my $back = $sf->{i}{back} . ' ';
157  
158 SUBSTITUTION: while ( 1 ) {
159 $info = $cf->__get_filter_info( $sql, join( "\n", @tmp_info ) );
160 # Fill_form
161 my $form = $tf->fill_form(
162 $fields,
163 { info => $info, prompt => $prompt, confirm => $sf->{i}{confirm}, back => $back }
164 );
165 if ( ! defined $form ) {
166 next ADD_SEARCH_AND_REPLACE;
167 }
168 my $sr_group = [ $sf->__from_form_to_sr_group_data( $form ) ];
169 if ( ! @$sr_group ) {
170 next ADD_SEARCH_AND_REPLACE;
171 }
172 if ( ! eval {
173 $sf->__execute_substitutions( [ [ 'test_string' ] ], [ 0 ], [ $sr_group ] );
174 1 }
175 ) {
176 $ax->print_error_message( $@ );
177 $fields = $form;
178 next SUBSTITUTION;
179 }
180 push @bu, [ [ @$used_names ], [ @$all_sr_groups ] ];
181 push @$all_sr_groups, $sr_group;
182 last SUBSTITUTION;
183 }
184 }
185 else {
186 my $name = $available->[$idx-@pre];
187 my $sr_group = $saved->{$name};
188 push @bu, [ [ @$used_names ], [ @$all_sr_groups ] ];
189 push @$used_names, $name;
190 push @$all_sr_groups, $sr_group;
191 }
192 }
193 }
194  
195  
196 sub __from_form_to_sr_group_data {
197 my ( $sf, $form ) = @_;
198 my @sr_group_data;
199 my @copy = @$form;
200 while ( @copy ) {
201 my ( $section_separator, $pattern, $replacement, $modifiers ) = map { $_->[1] // '' } splice @copy, 0, 4;
202 if ( length $pattern ) {
203 push @sr_group_data, { pattern => $pattern, replacement => $replacement, modifiers => $modifiers };
204 }
205 }
206 return @sr_group_data;
207 }
208  
209  
210 sub __choose_column_indexes {
211 my ( $sf, $columns, $info, $prompt ) = @_;
212 my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
213 # Choose
214 my $col_idxs = $tu->choose_a_subset(
215 $columns,
216 { cs_label => $prompt, info => $info, layout => 0, all_by_default => 1, index => 1, keep_chosen => 0 }
217 );
218 if ( ! defined $col_idxs ) {
219 return;
220 }
221 return $col_idxs;
222 }
223  
224  
225 sub __execute_substitutions {
226 my ( $sf, $aoa, $col_idxs, $all_sr_groups ) = @_;
227 my $cf = App::DBBrowser::GetContent::Filter->new( $sf->{i}, $sf->{o}, $sf->{d} );
228 my $busy_string = 'Search and replace: ';
229 my $col_count = @$col_idxs;
230 my $cell_count = @$aoa * $col_count;
231 my $threshold_busy = 25_000;
232 if ( $cell_count > $threshold_busy ) {
233 $cf->__print_busy_string( $busy_string . '...' );
234 }
235 my $threshold_progress = 500_000;
236 my ( $show_progress, $step, $total, $fmt );
237 if ( $cell_count > $threshold_progress ) {
238 $show_progress = $cell_count > ( $threshold_progress * 3 ) ? 2 : 1;
239 $step = 1_000;
240 $total = int $cell_count / $step;
241 $fmt = $busy_string . $total . '/%' . length( $total ) . 'd';
242 }
243 else {
244 $show_progress = 0;
245 }
246 my $c;
247  
248 for my $sr_group ( @$all_sr_groups ) {
249 for my $sr_single ( @$sr_group ) {
250 my ( $pattern, $replacement_str, $modifiers ) = @$sr_single{qw(pattern replacement modifiers)};
251 my $global = $modifiers =~ tr/g//;
252 my $count_e = $modifiers =~ tr/e//;
253 my $replacement;
254 if ( $count_e ) {
255 my $replacement_code = sub { $replacement_str };
256 for ( 1 .. $count_e ) {
257 my $recurse = $replacement_code;
258 $replacement_code = sub { eval $recurse->() }; # execute (e) substitution
259 }
260 $replacement = $replacement_code;
261 }
262 else {
263 # with no `e`: the replacement has to be passed as a string
264 $replacement = $replacement_str;
265 }
266 $modifiers =~ tr/imnsxa//dc if length $modifiers; # tr/imnsxadlup//dc
267 $pattern = "(?${modifiers}:${pattern})" if length $modifiers;
268 if ( $count_e || $replacement_str =~ tr/$// ) {
269 for my $row ( 0 .. $#$aoa ) {
270 for my $col ( @$col_idxs ) {
271 $c = 0; ##
272 if ( ! defined $aoa->[$row][$col] ) {
273 next;
274 }
275 elsif ( $global ) {
276 gsub_modify( $aoa->[$row][$col], $pattern, $replacement ); # modifies $aoa
277 }
278 else {
279 sub_modify( $aoa->[$row][$col], $pattern, $replacement ); # modifies $aoa
280 }
281 }
282 if ( $show_progress && ! ( $row * $col_count % $step ) ) {
283 $cf->__print_busy_string( sprintf $fmt, $row * $col_count / $step );
284 }
285 }
286 }
287 else {
288 if ( $show_progress == 1 ) {
289 $cf->__print_busy_string( $busy_string . '...' );
290 }
291 for my $row ( 0 .. $#$aoa ) {
292 for my $col ( @$col_idxs ) {
293 $c = 0;
294 if ( ! defined $aoa->[$row][$col] ) {
295 next;
296 }
297 elsif ( $global ) {
298 $aoa->[$row][$col] =~ s/$pattern/$replacement/g; # modifies $aoa
299 }
300 else {
301 $aoa->[$row][$col] =~ s/$pattern/$replacement/; # modifies $aoa
302 }
303 }
304 if ( $show_progress == 2 && ! ( $row * $col_count % $step ) ) {
305 $cf->__print_busy_string( sprintf $fmt, $row * $col_count / $step );
306 }
307 }
308 }
309 }
310 }
311 }
312  
313  
314 sub _stringified_sr_group {
315 my ( $sr_group ) = @_;
316 return map { sprintf 's/%s/%s/%s;', @$_{qw(pattern replacement modifiers)} } @$sr_group;
317 }
318  
319  
320 sub __saved_search_and_replace {
321 my ( $sf ) = @_;
322 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
323 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
324 my $saved = $ax->read_json( $sf->{i}{f_search_and_replace} ) // {};
325 my $save_data = 0;
326 my $old_idx_history = 0;
327  
328 HISTORY: while ( 1 ) {
329 my $info = 'Saved "search & replace":';
330 $info = join "\n", $info, map( ' ' . $_, sort { $a cmp $b } keys %$saved ), ' ';
331 my ( $add, $edit, $remove ) = ( '- Add ', '- Edit', '- Remove' );
332 my $menu = [ undef, $add, $edit, $remove ];
333 # Choose
334 my $idx = $tc->choose(
335 $menu,
336 { %{$sf->{i}{lyt_v}}, info => $info, undef => ' <=', index => 1, default => $old_idx_history }
337 );
338 if ( ! defined $idx || ! defined $menu->[$idx] ) {
339 if ( $save_data ) {
340 $ax->write_json( $sf->{i}{f_search_and_replace}, $saved );
341 }
342 return;
343 }
344 if ( $sf->{o}{G}{menu_memory} ) {
345 if ( $old_idx_history == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
346 $old_idx_history = 0;
347 next HISTORY;
348 }
349 $old_idx_history = $idx;
350 }
351 my $choice = $menu->[$idx];
352 my $changed;
353 if ( $choice eq $add ) {
354 $changed = $sf->__add_saved( $saved );
355 }
356 elsif ( $choice eq $edit ) {
357 $changed = $sf->__edit_saved( $saved );
358 }
359 elsif ( $choice eq $remove ) {
360 $changed = $sf->__remove_saved( $saved );
361 }
362 if ( $changed && ! $save_data ) {
363 $save_data = 1;
364 }
365 }
366 }
367  
368  
369 sub __add_saved {
370 my ( $sf, $saved ) = @_;
371 my $tf = Term::Form->new( $sf->{i}{tf_default} );
372 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
373 my $skip = ' ';
374 my $fields = [];
375 for my $nr ( 1 .. 9 ) {
376 push @$fields,
377 [ $skip ],
378 [ $nr . ' Pattern', ],
379 [ $nr . ' Replacement', ],
380 [ $nr . ' Modifiers', ];
381 }
382 my $prompt = 'Add "search & replace":';
383  
384 ADD_CODE: while ( 1 ) {
385 # Fill_form
386 my $form = $tf->fill_form(
387 $fields,
388 { prompt => $prompt, confirm => $sf->{i}{_confirm}, back => $sf->{i}{_back} . ' ' }
389 );
390 if ( ! defined $form ) {
391 return;
392 }
393 my $sr_group = [ $sf->__from_form_to_sr_group_data( $form ) ];
394 if ( ! @$sr_group ) {
395 return;
396 }
397 if ( ! eval {
398 $sf->__execute_substitutions( [ [ 'test_string' ] ], [ 0 ], [ $sr_group ] );
399 1 }
400 ) {
401 $ax->print_error_message( $@ );
402 $fields = $form;
403 next ADD_CODE;
404 }
405 my @code = _stringified_sr_group( $sr_group );
406 my $info = join "\n", map { ( ' ' x 6 ) . $_ } @code;
407 $info =~ s/^\s{5}/Code:/;
408 $info = $prompt . "\n\n" . $info;
409 my $name = $sf->__get_new_name( $info, 'Name: ', $saved, $sr_group );
410 if ( ! length $name ) {
411 $fields = $form;
412 next ADD_CODE;
413 }
414 $saved->{$name} = [ @$sr_group ];
415 return 1;
416 }
417 }
418  
419  
420 sub __get_new_name {
421 my ( $sf, $info, $prompt, $saved, $sr_group ) = @_;
422 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
423 my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
424 my $name_default = '';
425 if ( @$sr_group == 1 ) {
426 $name_default = ( _stringified_sr_group( $sr_group ) )[0];
427 }
428 my $count = 1;
429  
430 NAME: while ( 1 ) {
431 # Readline
432 my $new_name = $tr->readline(
433 $prompt,
434 { info => $info, default => $name_default, history => [] }
435 );
436 if ( ! length $new_name ) {
437 return;
438 }
439 if ( any { $new_name eq $_ } keys %$saved ) {
440 my $prompt = "\"$new_name\" already exists.";
441 my $choice = $tc->choose(
442 [ undef, ' New name' ],
443 { %{$sf->{i}{lyt_v}}, prompt => $prompt, info => $info }
444 );
445 if ( ! defined $choice ) {
446 return;
447 }
448 if ( $count > 1 ) {
449 $new_name = undef;
450 }
451 $name_default = $new_name;
452 $count++;
453 next NAME;
454 }
455 return $new_name;
456 }
457 }
458  
459  
460 sub __edit_saved {
461 my ( $sf, $saved ) = @_;
462 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
463 my $tf = Term::Form->new( $sf->{i}{tf_default} );
464 my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
465 my @pre = ( undef );
466 my $menu = [ @pre, map( '- ' . $_, sort { $a cmp $b } keys %$saved ) ];
467 my $prompt = 'Edit "search & replace":';
468 # Choose
469 my $idx = $tc->choose(
470 $menu,
471 { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, undef => ' <=' }
472 );
473 if ( ! defined $idx || ! defined $menu->[$idx] ) {
474 return;
475 }
476 my $name = $menu->[$idx] =~ s/^- //r;
477 my $skip = ' ';
478 my $sr_group = $saved->{$name};
479 my $fields = [
480 [ $skip ],
481 [ ' Pattern', ],
482 [ ' Replacement', ],
483 [ ' Modifiers', ]
484 ];
485 my $c = 0;
486 for my $sr_single ( @$sr_group ) {
487 $c++;
488 push @$fields,
489 [ $skip ],
490 [ $c . ' Pattern', $sr_single->{pattern} ],
491 [ $c . ' Replacement', $sr_single->{replacement} ],
492 [ $c . ' Modifiers', $sr_single->{modifiers} ],
493 [ $skip ],
494 [ ' Pattern', ],
495 [ ' Replacement', ],
496 [ ' Modifiers', ];
497 }
498 my $info_fmt = "$prompt\n\nName: \"%s\"\nCode: %s\n";
499 my $code_str = join "\n" . ( ' ' x 6 ), _stringified_sr_group( $sr_group );
500 my $info = sprintf $info_fmt, $name, $code_str;
501 # Readline
502 my $new_name = $tr->readline(
503 'Confirm name: ',
504 { info => $info, default => $name, history => [] }
505 );
506 if ( ! length $new_name ) {
507 return;
508 }
509 my $old_code_str = join "\n" . ( ' ' x 6 ), _stringified_sr_group( $sr_group );
510 $info = sprintf $info_fmt, $new_name, $old_code_str;
511 # Fill_form
512 my $form = $tf->fill_form(
513 $fields,
514 { prompt => "Edit \"$new_name\":", info => $info, confirm => $sf->{i}{_confirm},
515 back => $sf->{i}{_back} . ' ' }
516 );
517 if ( ! defined $form ) {
518 return;
519 }
520 my $new_sr_group = [ $sf->__from_form_to_sr_group_data( $form ) ];
521 if ( ! @$new_sr_group ) {
522 return;
523 }
524 if ( $new_name ne $name ) {
525 delete $saved->{$name};
526 }
527 $saved->{$new_name} = [ @$new_sr_group ];
528 return 1;
529 }
530  
531  
532 sub __remove_saved {
533 my ( $sf, $saved ) = @_;
534 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
535 my @pre = ( undef );
536 my $prompt = 'Remove "search & replace":';
537 my $menu = [ @pre, map { '- ' . $_ } sort { $a cmp $b } keys %$saved ];
538 # Choose
539 my $idx = $tc->choose(
540 $menu,
541 { %{$sf->{i}{lyt_v}}, index => 1, undef => ' <=', prompt => $prompt }
542 );
543 if ( ! defined $idx || ! defined $menu->[$idx] ) {
544 return;
545 }
546 my $name = $menu->[$idx] =~ s/^- //r;
547 my $sr_group = $saved->{$name};
548 my $code_str = join "\n" . ( ' ' x 6 ), _stringified_sr_group( $sr_group );
549 my $info_fmt = "$prompt\n\nName: \"%s\"\nCode: %s\n";
550 my $info = sprintf $info_fmt, $name, $code_str;
551 my ( $no, $yes ) = ( '- NO', '- YES' );
552 $menu = [ undef, $no, $yes ];
553 # Choose
554 $idx = $tc->choose(
555 $menu,
556 { %{$sf->{i}{lyt_v}}, prompt => "Remove \"$name\"?", index => 1, undef => ' <=', info => $info }
557 );
558 if ( ! defined $idx || ! defined $menu->[$idx] ) {
559 return;
560 }
561 elsif ( $menu->[$idx] eq $yes ) {
562 delete $saved->{$name};
563 return 1;
564 }
565 else {
566 return;
567 }
568 }
569  
570  
571  
572  
573  
574 1;
575  
576  
577 __END__