]> mj.ucw.cz Git - pciids.git/blob - scripts/importil.pl
TODO
[pciids.git] / scripts / importil.pl
1 #!/usr/bin/perl
2
3 #       PciIds web database
4 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
5 #
6 #       This program is free software; you can redistribute it and/or modify
7 #       it under the terms of the GNU General Public License as published by
8 #       he Free Software Foundation; either version 2 of the License, or
9 #       (at your option) any later version.
10 #
11 #       This program is distributed in the hope that it will be useful,
12 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #
15 #       GNU General Public License for more details.
16 #
17 #       You should have received a copy of the GNU General Public License
18 #       along with this program; if not, write to the Free Software
19 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 use strict;
22 use warnings;
23 BEGIN {
24         unshift @INC, ".";
25 };
26 use PciIds::Db;
27 use PciIds::DBQ;
28 use PciIds::Address;
29 use PciIds::Users;
30
31 my $dbh = connectDb();
32 print "Deleting all PCI devices\n";
33 $dbh->prepare( "DELETE FROM locations WHERE id like 'PC/%'" )->execute();
34 my $newcomment = $dbh->prepare( 'INSERT INTO history (owner, location, time, nodename, nodenote, discussion) VALUES (?, ?, FROM_UNIXTIME(?), ?, ?, ?)' );
35 my $mismatch = $dbh->prepare( "INSERT INTO history (location, nodename, nodenote, seen) VALUES(?, ?, ?, '1')" );
36 my $db = PciIds::DBQ::new( $dbh );
37 my @toMark;
38 my %ids;
39 my $id;
40 my $addr;
41 my $submitted = 0;
42
43 sub translateLoc( $ ) {
44         my $loc = shift;
45         $loc =~ s/(.{8})(.+)/$1\/$2/;
46         $loc =~ s/(.{4})(.+)/$1\/$2/;
47         return "PC/$loc";
48 }
49
50 my $cnt = 0;
51
52 sub checkSub() {
53         if( !$submitted ) {
54                 $db->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] );
55                 $submitted = 1;
56         }
57 }
58
59 sub insertId( $ ) {
60         my( $id ) = @_;
61         print "$cnt\n" if( ++ $cnt % 1000 == 0 );
62         $addr = PciIds::Address::new( $id );
63         $submitted = 0;
64 }
65
66 sub getUser( $ ) {
67         my( $email ) = @_;
68         $email = "" unless defined $email;
69         $email =~ s/.*<([^<>]*)>.*/$1/;
70         my( $mailCheck ) = emailCheck( $email, undef );
71         if( defined $mailCheck ) {
72                 print "Invalid email $email\n";
73                 return undef;
74         }
75         my $result = $db->query( 'email', [ $email ] );
76         if( scalar @{$result} ) {
77                 return $result->[0]->[0];
78         } else {
79                 $db->command( 'adduser-null', [ $email, '' ] );
80                 return $db->last();
81         }
82 }
83
84 sub addComment( $$$$$ ) {
85         my( $email, $time, $name, $comment, $discussion ) = @_;
86         my $user = getUser( $email );
87         $name = undef if( ( defined $name ) && $name !~ /\S/ && $name ne '' );
88         $comment = undef if( ( defined $comment ) && $comment !~ /\S/ );
89         $discussion = undef if( ( defined $discussion ) && $discussion !~ /\S/ );
90         $newcomment->execute( $user, $addr->get(), $time, $name, $comment, $discussion );
91         my $id = $db->last();
92         $comment = "" unless defined $comment;
93         $name = "" unless defined $name;
94         $ids{"$name\t$comment"} = $id;
95         push @toMark, $id;
96         return $id;
97 }
98
99 sub markAllSeen() {
100         $db->markChecked( $_ ) foreach( @toMark );
101         @toMark = ();
102 }
103
104 sub setMain( $ ) {
105         $db->setMainHistory( $addr->get(), shift );
106 }
107
108 print "Importing\n";
109
110 while( defined( $_ = <> ) ) {
111         chomp;
112         if( my( $lid ) = /^### ([0-9a-f]+) ###$/ ) {
113                 %ids = ();
114                 @toMark = ();
115                 $id = translateLoc( $lid );
116                 insertId( $id );
117         } elsif( /^(|#.*)$/ ) {
118                 next;
119         } else {
120                 my( $command, @params ) = split( /\t/ );
121                 checkSub();
122                 if( $command eq "CREATE" ) {
123                         my( $time, $email, $name, $comment, $discussion ) = @params;
124                         my $hid = addComment( $email, $time, $name, $comment, $discussion );
125                 } elsif ( $command eq "APPROVE" ) {
126                         my( $time, $email, $name, $comment ) = @params;
127                         $comment = "" if( !defined $comment || $comment !~ /\S/ );
128                         $name = "" if( !defined $name || $name !~ /\S/ );
129                         my $hid = $ids{"$name\t$comment"};
130                         $hid = addComment( $email, $time, $name, $comment, undef ) unless defined $hid;
131                         markAllSeen();
132                         setMain( $hid );
133                 } elsif ( $command eq "COMMENT" ) {# Comments are from admins -> they mark as seen too
134                         my( $time, $email, $discussion ) = @params;
135                         addComment( $email, $time, undef, undef, $discussion );
136                         markAllSeen();
137                 } elsif ( $command eq "MISMATCH" ) {
138                         my( $name, $comment ) = @params;
139                         $mismatch->execute( $addr->get(), $name, $comment );
140                         setMain( $db->last() );
141                 } else {
142                         die "Unknow command $command\n";
143                 }
144         }
145 }
146 $dbh->commit();