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);
39 my $json = JSON->new()->canonical(1);
40 my $ofn = localdata("$fn.v4");
48 if ( ! -e localdata("$fn.v3") && -e localdata("$fn.v2") ) {
51 LogDbg('',"the module Storable appears to be missing!!");
52 LogDbg('',"trying to continue in compatibility mode (this may fail)");
53 LogDbg('',"please install Storable from CPAN as soon as possible");
56 import Storable qw(nfreeze thaw);
57 $convert = 3 if -e localdata("users.v3") && !-e $ufn;
60 die "need to have a $fn.v2 or (preferably) a $fn.v3 file in /spider/data or /spider/local_data\n" unless $convert;
62 if (-e $ofn || -e "$ofn.n") {
63 my $nfn = localdata("$fn.v4.json");
64 say "You appear to have (or are using) $ofn, creating $nfn instead";
67 say "using $ofn for output";
71 # do a conversion if required
73 my ($key, $val, $action, $count, $err) = ('','',0,0,0);
74 my $ta = [gettimeofday];
75 my $ofh = IO::File->new(">$ofn") or die "cannot open $ofn ($!)\n";
78 LogDbg('',"Converting the User File from V$convert to $fn.v4 ");
79 LogDbg('',"This will take a while, maybe as much as 10 secs");
80 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?]";
81 for ($action = R_FIRST; !$odbm->seq($key, $val, $action); $action = R_NEXT) {
84 eval { $ref = storable_decode($val) };
87 eval { $ref = asc_decode($val) };
92 $ofh->print($json->encode($ref) . "\n");
100 Log('err', "DXUser: error decoding $@");
105 my $t = _diffms($ta);
106 LogDbg('',"Conversion from users.v$convert to $ofn completed $count records $err errors $t mS");
116 $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
119 LogDbg('err', "DXUser::asc_decode: on '$s' $@");