]> mj.ucw.cz Git - pciids.git/blob - scripts/importlog.pl
Revert "Allow strange urls"
[pciids.git] / scripts / importlog.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 BEGIN {
23         unshift @INC, ".";
24 };
25 use PciIds::Db;
26
27 my %tracked;
28
29 sub translateLoc( $ ) {
30         my $loc = shift;
31         $loc =~ s/(.{8})(.+)/$1\/$2/;
32         $loc =~ s/(.{4})(.+)/$1\/$2/;
33         return "PC/$loc";
34 }
35
36 my $dbh = connectDb();
37 my $clearHist = $dbh->prepare( 'DELETE FROM history WHERE location = ?' );
38 my %coms;
39 my $com = $dbh->prepare( 'INSERT INTO history (owner, location, time, nodename, nodenote) VALUES (?, ?, FROM_UNIXTIME(?), ?, ?)' );
40 my $user = $dbh->prepare( 'SELECT id FROM users WHERE email = ?' );
41 my $delHis = $dbh->prepare( 'DELETE FROM history WHERE id = ?' );
42 my $markMain = $dbh->prepare( 'UPDATE locations SET
43                                 mainhistory = ?,
44                                 name = ( SELECT nodename FROM history WHERE id = ? ),
45                                 note = ( SELECT nodenote FROM history WHERE id = ? )
46                         WHERE
47                                 id = ?' );
48 my $markSeen = $dbh->prepare( "UPDATE history SET seen = '1' WHERE id = ?" );
49
50 sub getUser( $ ) {
51         $user->execute( shift );
52         if( my( $id ) = $user->fetchrow_array ) {
53                 return $id;
54         } else {
55                 return undef;
56         }
57 }
58
59 my $accept = 0;
60 my $reject = 0;
61 my $del = 0;
62 my $appr = 0;
63
64 print "Parsing and importing log\n";
65
66 foreach( <> ) {
67         my( $time, $who, $ip, $command, $id, $location, $name, $description, $email );
68         if( ( $time, $who, $ip, $command, $id, $location, $name, $description, $email ) = /^(\d+) (\S+) ([0-9.]+) (Create|Batch submit:) (\d+) ([0-9a-f]+) '(.*)(?<!\\)' '(.*)(?<!\\)' '(.*)(?<!\\)'/ ) {
69                 my $translated = translateLoc( $location );
70                 unless( $tracked{$location} ) {#From now on, it is restored from the log
71                         $tracked{$location} = 1;
72                         $clearHist->execute( $translated );
73                 }
74                 $name =~ s/\\(.)/$1/g;
75                 $description =~ s/\\(.)/$1/g;
76                 $name = undef if( $name eq '' );
77                 $description = undef if( $description eq '' );
78                 eval {#If the item is not here at all, it was deleted -> no need to add it here
79                         $com->execute( getUser( $email ), $translated, $time, $name, $description );
80                         $coms{$id} = $dbh->last_insert_id( undef, undef, undef, undef );
81                         $accept ++;
82                 };
83                 if( $@ ) {
84                         $reject ++;
85                 }
86         } elsif( ( $time, $who, $ip, $command, $id, $location, $description, $email ) = /^(\d+) (\S+) ([0-9.]+) (Approve|Delete|Overriden) (\d+) ([0-9a-f]+) '(.*)(?<!\\)' '(.*)(?<!\\)'/ ) {
87                 next unless( defined( $coms{$id} ) );#This one not tracked yet
88                 if( $command eq 'Approve' ) {
89                         my $i = $coms{$id};
90                         $markMain->execute( $i, $i, $i, translateLoc( $location ) );
91                         $markSeen->execute( $i );
92                         $appr ++;
93                 } elsif( $command eq 'Delete' ) {
94                         $delHis->execute( $coms{$id} );
95                         $del ++;
96                 } else {
97                         $markSeen->execute( $coms{$id} );
98                 }
99         } else {
100                 print "Unparsed line: $_";
101         }
102 }
103
104 $dbh->commit();
105 $dbh->disconnect();