]> mj.ucw.cz Git - pciids.git/blob - scripts/importil.pl
Import PCI ids from intermediate conversion language
[pciids.git] / scripts / importil.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 BEGIN {
5         unshift @INC, ".";
6 };
7 use PciIds::Db;
8 use PciIds::DBQ;
9 use PciIds::Address;
10 use PciIds::Users;
11
12 my $dbh = connectDb();
13 print "Deleting all PCI devices\n";
14 $dbh->prepare( "DELETE FROM locations WHERE id like 'PC/%'" )->execute();
15 my $newcomment = $dbh->prepare( 'INSERT INTO history (owner, location, time, nodename, nodedescription, text) VALUES (?, ?, FROM_UNIXTIME(?), ?, ?, ?)' );
16 my $mismatch = $dbh->prepare( "INSERT INTO history (location, nodename, nodedescription, seen) VALUES(?, ?, ?, '1')" );
17 my $db = PciIds::DBQ::new( $dbh );
18 my @toMark;
19 my %ids;
20 my $id;
21 my $addr;
22 my $submitted = 0;
23
24 sub translateLoc( $ ) {
25         my $loc = shift;
26         $loc =~ s/(.{8})(.+)/$1\/$2/;
27         $loc =~ s/(.{4})(.+)/$1\/$2/;
28         return "PC/$loc";
29 }
30
31 my $cnt = 0;
32
33 sub checkSub() {
34         if( !$submitted ) {
35                 $db->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
36                 $submitted = 1;
37         }
38 }
39
40 sub insertId( $ ) {
41         my( $id ) = @_;
42         print "$cnt\n" if( ++ $cnt % 1000 == 0 );
43         $addr = PciIds::Address::new( $id );
44         $submitted = 0;
45 }
46
47 sub getUser( $ ) {
48         my( $email ) = @_;
49         $email =~ s/.*<([^<>]*)>.*/$1/;
50         my( $mailCheck ) = emailCheck( $email, undef );
51         if( defined $mailCheck ) {
52                 print "Invalid email $email\n";
53                 return undef;
54         }
55         my $result = $db->query( 'email', [ $email ] );
56         if( scalar @{$result} ) {
57                 return $result->[0]->[0];
58         } else {
59                 $db->command( 'adduser-null', [ $email, '' ] );
60                 return $db->last();
61         }
62 }
63
64 sub addComment( $$$$$ ) {
65         my( $email, $time, $name, $comment, $history ) = @_;
66         my $user = getUser( $email );
67         $newcomment->execute( $user, $addr->get(), $time, $name, $comment, $history );
68         my $id = $db->last();
69         $comment = "" unless defined $comment;
70         $name = "" unless defined $name;
71         $ids{"$name\t$comment"} = $id;
72         push @toMark, $id;
73         return $id;
74 }
75
76 sub markAllSeen() {
77         $db->markChecked( $_ ) foreach( @toMark );
78         @toMark = ();
79 }
80
81 sub setMain( $ ) {
82         $db->setMainComment( $addr->get(), shift );
83 }
84
85 print "Importing\n";
86
87 while( defined( $_ = <> ) ) {
88         if( my( $lid ) = /^### ([0-9a-f]+) ###$/ ) {
89                 %ids = ();
90                 @toMark = ();
91                 $id = translateLoc( $lid );
92                 insertId( $id );
93         } elsif( /^(|#.*)$/ ) {
94                 next;
95         } else {
96                 my( $command, @params ) = split( /\t/ );
97                 checkSub();
98                 if( $command eq "CREATE" ) {
99                         my( $time, $email, $name, $comment, $discussion ) = @params;
100                         my $hid = addComment( $email, $time, $name, $comment, $discussion );
101                 } elsif ( $command eq "APPROVE" ) {
102                         my( $time, $email, $name, $comment ) = @params;
103                         $comment = "" unless defined $comment;
104                         $name = "" unless defined $name;
105                         my $hid = $ids{"$name\t$comment"};
106                         $hid = addComment( $email, $time, $name, $comment, undef );
107                         markAllSeen();
108                         setMain( $hid );
109                 } elsif ( $command eq "COMMENT" ) {# Comments are from admins -> they mark as seen too
110                         my( $time, $email, $discussion ) = @params;
111                         addComment( $email, $time, undef, undef, $discussion );
112                         markAllSeen();
113                 } elsif ( $command eq "MISMATCH" ) {
114                         my( $name, $comment ) = @params;
115                         $mismatch->execute( $addr->get(), $name, $comment );
116                         setMain( $db->last() );
117                 } else {
118                         die "Unknow command $command\n";
119                 }
120         }
121 }
122 $dbh->commit();