]> mj.ucw.cz Git - pciids.git/blob - scripts/importlog.pl
Import PCI ids from intermediate conversion language
[pciids.git] / scripts / importlog.pl
1 #!/usr/bin/perl
2 use strict;
3 BEGIN {
4         unshift @INC, ".";
5 };
6 use PciIds::Db;
7
8 my %tracked;
9
10 sub translateLoc( $ ) {
11         my $loc = shift;
12         $loc =~ s/(.{8})(.+)/$1\/$2/;
13         $loc =~ s/(.{4})(.+)/$1\/$2/;
14         return "PC/$loc";
15 }
16
17 my $dbh = connectDb();
18 my $clearHist = $dbh->prepare( 'DELETE FROM history WHERE location = ?' );
19 my %coms;
20 my $com = $dbh->prepare( 'INSERT INTO history (owner, location, time, nodename, nodedescription) VALUES (?, ?, FROM_UNIXTIME(?), ?, ?)' );
21 my $user = $dbh->prepare( 'SELECT id FROM users WHERE email = ?' );
22 my $delHis = $dbh->prepare( 'DELETE FROM history WHERE id = ?' );
23 my $markMain = $dbh->prepare( 'UPDATE locations SET
24                                 maincomment = ?,
25                                 name = ( SELECT nodename FROM history WHERE id = ? ),
26                                 description = ( SELECT nodedescription FROM history WHERE id = ? )
27                         WHERE
28                                 id = ?' );
29 my $markSeen = $dbh->prepare( "UPDATE history SET seen = '1' WHERE id = ?" );
30
31 sub getUser( $ ) {
32         $user->execute( shift );
33         if( my( $id ) = $user->fetchrow_array ) {
34                 return $id;
35         } else {
36                 return undef;
37         }
38 }
39
40 my $accept = 0;
41 my $reject = 0;
42 my $del = 0;
43 my $appr = 0;
44
45 print "Parsing and importing log\n";
46
47 foreach( <> ) {
48         my( $time, $who, $ip, $command, $id, $location, $name, $description, $email );
49         if( ( $time, $who, $ip, $command, $id, $location, $name, $description, $email ) = /^(\d+) (\S+) ([0-9.]+) (Create|Batch submit:) (\d+) ([0-9a-f]+) '(.*)(?<!\\)' '(.*)(?<!\\)' '(.*)(?<!\\)'/ ) {
50                 my $translated = translateLoc( $location );
51                 unless( $tracked{$location} ) {#From now on, it is restored from the log
52                         $tracked{$location} = 1;
53                         $clearHist->execute( $translated );
54                 }
55                 $name =~ s/\\(.)/$1/g;
56                 $description =~ s/\\(.)/$1/g;
57                 $name = undef if( $name eq '' );
58                 $description = undef if( $description eq '' );
59                 eval {#If the item is not here at all, it was deleted -> no need to add it here
60                         $com->execute( getUser( $email ), $translated, $time, $name, $description );
61                         $coms{$id} = $dbh->last_insert_id( undef, undef, undef, undef );
62                         $accept ++;
63                 };
64                 if( $@ ) {
65                         $reject ++;
66                 }
67         } elsif( ( $time, $who, $ip, $command, $id, $location, $description, $email ) = /^(\d+) (\S+) ([0-9.]+) (Approve|Delete|Overriden) (\d+) ([0-9a-f]+) '(.*)(?<!\\)' '(.*)(?<!\\)'/ ) {
68                 next unless( defined( $coms{$id} ) );#This one not tracked yet
69                 if( $command eq 'Approve' ) {
70                         my $i = $coms{$id};
71                         $markMain->execute( $i, $i, $i, translateLoc( $location ) );
72                         $markSeen->execute( $i );
73                         $appr ++;
74                 } elsif( $command eq 'Delete' ) {
75                         $delHis->execute( $coms{$id} );
76                         $del ++;
77                 } else {
78                         $markSeen->execute( $coms{$id} );
79                 }
80         } else {
81                 print "Unparsed line: $_";
82         }
83 }
84
85 $dbh->commit();
86 $dbh->disconnect();