From 70145dbd74d60900328ac299606eaae17aaf663e Mon Sep 17 00:00:00 2001 From: Michal Vaner Date: Fri, 29 Aug 2008 14:26:27 +0200 Subject: [PATCH] Import PCI ids from intermediate conversion language --- PciIds/Users.pm | 2 +- scripts/importil.pl | 122 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100755 scripts/importil.pl diff --git a/PciIds/Users.pm b/PciIds/Users.pm index eb49ba3..1bb26f9 100644 --- a/PciIds/Users.pm +++ b/PciIds/Users.pm @@ -18,7 +18,7 @@ our @EXPORT = qw(&addUser &emailConfirm &checkConfirmHash &saltedPasswd &genAuth sub emailCheck( $$ ) { my( $email, $tables ) = @_; my $newmail; - return 'Does not look like an email address' unless ( ( $newmail ) = ( $email =~ /^([^,? "'`;]+@[^@,?\/ "'`;]+)$/ ) );#make sure the mail is not only reasonable looking, but safe to work with too + return 'Does not look like an email address' unless ( ( $newmail ) = ( $email =~ /^([^,? "'`;<>]+@[^@,?\/ "'`;<>]+)$/ ) );#make sure the mail is not only reasonable looking, but safe to work with too return 'Email too long' if length $newmail > 255; return 'An account for this email address already exists' if( ( defined $tables ) && $tables->hasEmail( $newmail ) ); return ( undef, $newmail ); diff --git a/scripts/importil.pl b/scripts/importil.pl new file mode 100755 index 0000000..21da83f --- /dev/null +++ b/scripts/importil.pl @@ -0,0 +1,122 @@ +#!/usr/bin/perl +use strict; +use warnings; +BEGIN { + unshift @INC, "."; +}; +use PciIds::Db; +use PciIds::DBQ; +use PciIds::Address; +use PciIds::Users; + +my $dbh = connectDb(); +print "Deleting all PCI devices\n"; +$dbh->prepare( "DELETE FROM locations WHERE id like 'PC/%'" )->execute(); +my $newcomment = $dbh->prepare( 'INSERT INTO history (owner, location, time, nodename, nodedescription, text) VALUES (?, ?, FROM_UNIXTIME(?), ?, ?, ?)' ); +my $mismatch = $dbh->prepare( "INSERT INTO history (location, nodename, nodedescription, seen) VALUES(?, ?, ?, '1')" ); +my $db = PciIds::DBQ::new( $dbh ); +my @toMark; +my %ids; +my $id; +my $addr; +my $submitted = 0; + +sub translateLoc( $ ) { + my $loc = shift; + $loc =~ s/(.{8})(.+)/$1\/$2/; + $loc =~ s/(.{4})(.+)/$1\/$2/; + return "PC/$loc"; +} + +my $cnt = 0; + +sub checkSub() { + if( !$submitted ) { + $db->command( 'newitem', [ $addr->get(), $addr->parent()->get() ] ); + $submitted = 1; + } +} + +sub insertId( $ ) { + my( $id ) = @_; + print "$cnt\n" if( ++ $cnt % 1000 == 0 ); + $addr = PciIds::Address::new( $id ); + $submitted = 0; +} + +sub getUser( $ ) { + my( $email ) = @_; + $email =~ s/.*<([^<>]*)>.*/$1/; + my( $mailCheck ) = emailCheck( $email, undef ); + if( defined $mailCheck ) { + print "Invalid email $email\n"; + return undef; + } + my $result = $db->query( 'email', [ $email ] ); + if( scalar @{$result} ) { + return $result->[0]->[0]; + } else { + $db->command( 'adduser-null', [ $email, '' ] ); + return $db->last(); + } +} + +sub addComment( $$$$$ ) { + my( $email, $time, $name, $comment, $history ) = @_; + my $user = getUser( $email ); + $newcomment->execute( $user, $addr->get(), $time, $name, $comment, $history ); + my $id = $db->last(); + $comment = "" unless defined $comment; + $name = "" unless defined $name; + $ids{"$name\t$comment"} = $id; + push @toMark, $id; + return $id; +} + +sub markAllSeen() { + $db->markChecked( $_ ) foreach( @toMark ); + @toMark = (); +} + +sub setMain( $ ) { + $db->setMainComment( $addr->get(), shift ); +} + +print "Importing\n"; + +while( defined( $_ = <> ) ) { + if( my( $lid ) = /^### ([0-9a-f]+) ###$/ ) { + %ids = (); + @toMark = (); + $id = translateLoc( $lid ); + insertId( $id ); + } elsif( /^(|#.*)$/ ) { + next; + } else { + my( $command, @params ) = split( /\t/ ); + checkSub(); + if( $command eq "CREATE" ) { + my( $time, $email, $name, $comment, $discussion ) = @params; + my $hid = addComment( $email, $time, $name, $comment, $discussion ); + } elsif ( $command eq "APPROVE" ) { + my( $time, $email, $name, $comment ) = @params; + $comment = "" unless defined $comment; + $name = "" unless defined $name; + my $hid = $ids{"$name\t$comment"}; + $hid = addComment( $email, $time, $name, $comment, undef ); + markAllSeen(); + setMain( $hid ); + } elsif ( $command eq "COMMENT" ) {# Comments are from admins -> they mark as seen too + my( $time, $email, $discussion ) = @params; + addComment( $email, $time, undef, undef, $discussion ); + markAllSeen(); + } elsif ( $command eq "MISMATCH" ) { + my( $name, $comment ) = @params; + $mismatch->execute( $addr->get(), $name, $comment ); + setMain( $db->last() ); + } else { + die "Unknow command $command\n"; + } + } +} +$dbh->commit(); -- 2.39.2