3 # Convert users.v2 or .v3 to JSON .v4 format
5 # It is believed that this can be run at any time...
7 # Copyright (c) 2020 Dirk Koopman G1TLH
12 # make sure that modules are searched in the order local then perl
15 # root of directory tree for this system
17 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
18 unshift @INC, "$root/perl"; # this IS the right way round!
19 unshift @INC, "$root/local";
28 use Data::Structure::Util qw(unbless);
29 use Time::HiRes qw(gettimeofday tv_interval);
40 my $json = JSON->new()->canonical(1);
41 my $ofn = localdata("$fn.v4");
49 if ( ! -e localdata("$fn.v3") && -e localdata("$fn.v2") ) {
52 LogDbg('',"the module Storable appears to be missing!!");
53 LogDbg('',"trying to continue in compatibility mode (this may fail)");
54 LogDbg('',"please install Storable from CPAN as soon as possible");
57 import Storable qw(nfreeze thaw);
58 $convert = 3 if -e localdata("users.v3") && !-e $ufn;
61 die "need to have a $fn.v2 or (preferably) a $fn.v3 file in /spider/data or /spider/local_data\n" unless $convert;
63 if (-e $ofn || -e "$ofn.n") {
64 my $nfn = localdata("$fn.v4.json");
65 say "You appear to have (or are using) $ofn, creating $nfn instead";
69 say "using $ofn.n for output";
73 # do a conversion if required
75 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
76 my $ta = [gettimeofday];
77 my $ofh = IO::File->new(">$ofn") or die "cannot open $ofn ($!)\n";
80 LogDbg('',"Converting the User File from V$convert to $fn.v4 ");
81 LogDbg('',"This will take a while, maybe as much as 10 secs");
82 my $odbm = tie (%oldu, 'DB_File', localdata("users.v$convert"), O_RDONLY, 0666, $DB_BTREE) or confess "can't open user file: $fn.v$convert ($!) [rebuild it from user_asc?]";
83 for ($action = R_FIRST; !$odbm->seq($key, $val, $action); $action = R_NEXT) {
86 eval { $ref = storable_decode($val) };
89 eval { $ref = asc_decode($val) };
94 $ofh->print("$ref->{call}\t" . $json->encode($ref) . "\n");
102 Log('err', "DXUser: error decoding $@");
107 my $t = _diffms($ta);
108 LogDbg('',"Conversion from users.v$convert to $ofn completed $count records $err errors $t mS");
118 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
121 LogDbg('err', "DXUser::asc_decode: on '$s' $@");