48ef0c00f7b8a4aeba3eb12605be482df3f820cf
[spider.git] / perl / convert-users-v3-to-v4.pl
1 #!/usr/bin/env perl
2 #
3 # Convert users.v2 or .v3 to JSON .v4 format
4 #
5 # It is believed that this can be run at any time...
6 #
7 # Copyright (c) 2020 Dirk Koopman G1TLH
8 #
9 #
10
11
12 # make sure that modules are searched in the order local then perl
13
14 BEGIN {
15         # root of directory tree for this system
16         $root = "/spider"; 
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";
20 }
21
22 use strict;
23
24 use SysVar;
25 use DXUser;
26 use DXUtil;
27 use JSON;
28 use Data::Structure::Util qw(unbless);
29 use Time::HiRes qw(gettimeofday tv_interval);
30 use IO::File;
31 use File::Copy;
32 use Carp;
33 use DB_File;
34
35 use 5.10.1;
36
37 my $ufn;
38 my $fn = "users";
39
40 my $json = JSON->new()->canonical(1);
41 my $ofn = localdata("$fn.v4");
42 my $convert;
43
44 eval {
45         require Storable;
46 };
47
48 if ($@) {
49         if ( ! -e localdata("$fn.v3") && -e localdata("$fn.v2") ) {
50                 $convert = 2;
51         }
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");
55 }
56 else {
57         import Storable qw(nfreeze thaw);
58         $convert = 3 if -e localdata("users.v3") && !-e $ufn;
59 }
60
61 die "need to have a $fn.v2 or (preferably) a $fn.v3 file in /spider/data or /spider/local_data\n" unless $convert;
62
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";
66         $ofn = $nfn;
67 } else {
68         $ofn = "$ofn.n";
69         say "using $ofn.n for output";
70 }
71
72
73 # do a conversion if required
74 if ($convert) {
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";
78                 
79         my %oldu;
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) {
84                 my $ref;
85                 if ($convert == 3) {
86                         eval { $ref = storable_decode($val) };
87                 }
88                 else {
89                         eval { $ref = asc_decode($val) };
90                 }
91                 unless ($@) {
92                         if ($ref) {
93                                 unbless $ref;
94                                 $ofh->print("$ref->{call}\t" . $json->encode($ref) . "\n");
95                                 $count++;
96                         }
97                         else {
98                                 $err++
99                         }
100                 }
101                 else {
102                         Log('err', "DXUser: error decoding $@");
103                 }
104         } 
105         undef $odbm;
106         untie %oldu;
107         my $t = _diffms($ta);
108         LogDbg('',"Conversion from users.v$convert to $ofn completed $count records $err errors $t mS");
109         $ofh->close;
110 }
111
112 exit 0;
113
114 sub asc_decode
115 {
116         my $s = shift;
117         my $ref;
118         $s =~ s/\%([0-9A-F][0-9A-F])/chr(hex($1))/eg;
119         eval '$ref = ' . $s;
120         if ($@) {
121                 LogDbg('err', "DXUser::asc_decode: on '$s' $@");
122                 $ref = undef;
123         }
124         return $ref;
125 }
126
127 sub storable_decode
128 {
129         my $ref;
130         $ref = thaw(shift);
131         return $ref;
132 }
133
134 sub LogDbg
135 {
136         my (undef, $s) = @_;
137         say $s;
138 }
139
140 sub Log
141 {
142         say shift;
143 }