]> mj.ucw.cz Git - pciids.git/blob - scripts/transfer.pl
Remove useless directories
[pciids.git] / scripts / transfer.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 BEGIN {
6         unshift @INC, ".";
7 }
8 use PciIds::Db;
9 use PciIds::Config;
10 use DBI;
11
12 my( $orig ) = @ARGV;
13 my $newdb = connectDb();
14 my( $user, $passwd ) = confList( [ "dbuser", "dbpasswd" ] );
15 my $olddb = DBI->connect( "dbi:mysql:$orig", $user, $passwd, { 'RaiseError' => 1 } );
16
17 print "Submiting ordinary users\n";
18 my $uquery = $olddb->prepare( "SELECT DISTINCT author FROM ids WHERE author like '%@%'" );
19 my $upush = $newdb->prepare( "INSERT INTO users (email, passwd) VALUES (?, '')" );
20 $uquery->execute();
21 my %users = ( '' => undef );
22
23 while( my( $author ) = $uquery->fetchrow_array ) {
24         $upush->execute( $author );
25         $users{$author} = $newdb->last_insert_id( undef, undef, undef, undef );
26 }
27
28 my $clean = $newdb->prepare( "DELETE FROM locations WHERE id like 'PC/%'" );
29 print "Cleaning old PCI devices to make place for new ones\n";
30 $clean->execute();
31 print "Submiting items from database\n";
32
33 my $itemq = $olddb->prepare( "SELECT id, name, comment, author, status, type FROM ids ORDER BY LENGTH(id), id" );
34 my $itemp = $newdb->prepare( "INSERT INTO locations (id, parent) VALUES (?, ?)" );
35 my $comp = $newdb->prepare( "INSERT INTO history (owner, location, nodename, nodedescription, seen, time) VALUES (?, ?, ?, ?, ?, '2000-01-01 00:00:00')" );
36 my $setMain = $newdb->prepare( 'UPDATE locations SET
37                                 maincomment = ?,
38                                 name = ( SELECT nodename FROM history WHERE id = ? ),
39                                 description = ( SELECT nodedescription FROM history WHERE id = ? )
40                         WHERE
41                                 id = ?' );
42
43 my %rex = (
44         'v' => sub {
45                 my $i = shift;
46                 "PC/$i";
47         },
48         'd' => sub {
49                 my $i = shift;
50                 $i =~ s/(.{4,4})(.*)/PC\/$1\/$2/;
51                 return $i;
52         },
53         's' => sub {
54                 my $i = shift;
55                 $i =~ s/(.{4,4})(.{4,4})(.*)/PC\/$1\/$2\/$3/;
56                 return $i;
57         }
58 );
59
60 $itemq->execute();
61 while( my( $id, $name, $description, $author, $status, $type ) = $itemq->fetchrow_array ) {
62         $id = &{$rex{$type}}( $id );
63         my $parent = $id;
64         $parent =~ s/\/[^\/]+$//;
65         eval {#Add it if not present yet
66                 $itemp->execute( $id, $parent );
67         };
68         $author = '' unless( defined $author );
69         $comp->execute( $users{$author} ? $users{$author} : undef, $id, $name, $description, !$status );
70         unless( $status ) {
71                 my $last = $newdb->last_insert_id( undef, undef, undef, undef );
72                 $setMain->execute( $last, $last, $last, $id );
73         }
74 }
75
76 $newdb->commit();
77 $newdb->disconnect();