]> mj.ucw.cz Git - pciids.git/blob - scripts/importil.pl
Import: fix empty note
[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, nodenote, discussion) VALUES (?, ?, FROM_UNIXTIME(?), ?, ?, ?)' );
16 my $mismatch = $dbh->prepare( "INSERT INTO history (location, nodename, nodenote, 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 = "" unless defined $email;
50         $email =~ s/.*<([^<>]*)>.*/$1/;
51         my( $mailCheck ) = emailCheck( $email, undef );
52         if( defined $mailCheck ) {
53                 print "Invalid email $email\n";
54                 return undef;
55         }
56         my $result = $db->query( 'email', [ $email ] );
57         if( scalar @{$result} ) {
58                 return $result->[0]->[0];
59         } else {
60                 $db->command( 'adduser-null', [ $email, '' ] );
61                 return $db->last();
62         }
63 }
64
65 sub addComment( $$$$$ ) {
66         my( $email, $time, $name, $comment, $discussion ) = @_;
67         my $user = getUser( $email );
68         $name = undef if( ( defined $name ) && $name !~ /\S/ );
69         $comment = undef if( ( defined $comment ) && $comment !~ /\S/ );
70         $discussion = undef if( ( defined $discussion ) && $discussion !~ /\S/ );
71         $newcomment->execute( $user, $addr->get(), $time, $name, $comment, $discussion );
72         my $id = $db->last();
73         $comment = "" unless defined $comment;
74         $name = "" unless defined $name;
75         $ids{"$name\t$comment"} = $id;
76         push @toMark, $id;
77         return $id;
78 }
79
80 sub markAllSeen() {
81         $db->markChecked( $_ ) foreach( @toMark );
82         @toMark = ();
83 }
84
85 sub setMain( $ ) {
86         $db->setMainHistory( $addr->get(), shift );
87 }
88
89 print "Importing\n";
90
91 while( defined( $_ = <> ) ) {
92         chomp;
93         if( my( $lid ) = /^### ([0-9a-f]+) ###$/ ) {
94                 %ids = ();
95                 @toMark = ();
96                 $id = translateLoc( $lid );
97                 insertId( $id );
98         } elsif( /^(|#.*)$/ ) {
99                 next;
100         } else {
101                 my( $command, @params ) = split( /\t/ );
102                 checkSub();
103                 if( $command eq "CREATE" ) {
104                         my( $time, $email, $name, $comment, $discussion ) = @params;
105                         my $hid = addComment( $email, $time, $name, $comment, $discussion );
106                 } elsif ( $command eq "APPROVE" ) {
107                         my( $time, $email, $name, $comment ) = @params;
108                         $comment = "" if( !defined $comment || $comment !~ /\S/ );
109                         $name = "" if( !defined $name || $name !~ /\S/ );
110                         my $hid = $ids{"$name\t$comment"};
111                         $hid = addComment( $email, $time, $name, $comment, undef ) unless defined $hid;
112                         markAllSeen();
113                         setMain( $hid );
114                 } elsif ( $command eq "COMMENT" ) {# Comments are from admins -> they mark as seen too
115                         my( $time, $email, $discussion ) = @params;
116                         addComment( $email, $time, undef, undef, $discussion );
117                         markAllSeen();
118                 } elsif ( $command eq "MISMATCH" ) {
119                         my( $name, $comment ) = @params;
120                         $mismatch->execute( $addr->get(), $name, $comment );
121                         setMain( $db->last() );
122                 } else {
123                         die "Unknow command $command\n";
124                 }
125         }
126 }
127 $dbh->commit();