]> mj.ucw.cz Git - pciids.git/blob - scripts/importil.pl
New terminology, part 2 -- DBQ function names
[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 =~ 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, $discussion ) = @_;
66         my $user = getUser( $email );
67         $name = undef if( ( defined $name ) && $name !~ /\S/ );
68         $comment = undef if( ( defined $comment ) && $comment !~ /\S/ );
69         $discussion = undef if( ( defined $discussion ) && $discussion !~ /\S/ );
70         $newcomment->execute( $user, $addr->get(), $time, $name, $comment, $discussion );
71         my $id = $db->last();
72         $comment = "" unless defined $comment;
73         $name = "" unless defined $name;
74         $ids{"$name\t$comment"} = $id;
75         push @toMark, $id;
76         return $id;
77 }
78
79 sub markAllSeen() {
80         $db->markChecked( $_ ) foreach( @toMark );
81         @toMark = ();
82 }
83
84 sub setMain( $ ) {
85         $db->setMainHistory( $addr->get(), shift );
86 }
87
88 print "Importing\n";
89
90 while( defined( $_ = <> ) ) {
91         if( my( $lid ) = /^### ([0-9a-f]+) ###$/ ) {
92                 %ids = ();
93                 @toMark = ();
94                 $id = translateLoc( $lid );
95                 insertId( $id );
96         } elsif( /^(|#.*)$/ ) {
97                 next;
98         } else {
99                 my( $command, @params ) = split( /\t/ );
100                 checkSub();
101                 if( $command eq "CREATE" ) {
102                         my( $time, $email, $name, $comment, $discussion ) = @params;
103                         my $hid = addComment( $email, $time, $name, $comment, $discussion );
104                 } elsif ( $command eq "APPROVE" ) {
105                         my( $time, $email, $name, $comment ) = @params;
106                         $comment = "" if( !defined $comment || $comment !~ /\S/ );
107                         $name = "" if( !defined $name || $name !~ /\S/ );
108                         my $hid = $ids{"$name\t$comment"};
109                         $hid = addComment( $email, $time, $name, $comment, undef ) unless defined $hid;
110                         markAllSeen();
111                         setMain( $hid );
112                 } elsif ( $command eq "COMMENT" ) {# Comments are from admins -> they mark as seen too
113                         my( $time, $email, $discussion ) = @params;
114                         addComment( $email, $time, undef, undef, $discussion );
115                         markAllSeen();
116                 } elsif ( $command eq "MISMATCH" ) {
117                         my( $name, $comment ) = @params;
118                         $mismatch->execute( $addr->get(), $name, $comment );
119                         setMain( $db->last() );
120                 } else {
121                         die "Unknow command $command\n";
122                 }
123         }
124 }
125 $dbh->commit();