]> mj.ucw.cz Git - pciids.git/blob - scripts/transfer.pl
Do not fail when jumping from help
[pciids.git] / scripts / transfer.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::Config;
28 use DBI;
29
30 my( $orig ) = @ARGV;
31 my $newdb = connectDb();
32 my( $user, $passwd ) = confList( [ "dbuser", "dbpasswd" ] );
33 my $olddb = DBI->connect( "dbi:mysql:$orig", $user, $passwd, { 'RaiseError' => 1 } );
34
35 print "Submiting ordinary users\n";
36 my $uquery = $olddb->prepare( "SELECT DISTINCT author FROM ids WHERE author like '%@%'" );
37 my $upush = $newdb->prepare( "INSERT INTO users (email, passwd) VALUES (?, '')" );
38 $uquery->execute();
39 my %users = ( '' => undef );
40
41 while( my( $author ) = $uquery->fetchrow_array ) {
42         $upush->execute( $author );
43         $users{$author} = $newdb->last_insert_id( undef, undef, undef, undef );
44 }
45
46 my $clean = $newdb->prepare( "DELETE FROM locations WHERE id like 'PC/%'" );
47 print "Cleaning old PCI devices to make place for new ones\n";
48 $clean->execute();
49 print "Submiting items from database\n";
50
51 my $itemq = $olddb->prepare( "SELECT id, name, comment, author, status, type FROM ids ORDER BY LENGTH(id), id" );
52 my $itemp = $newdb->prepare( "INSERT INTO locations (id, parent) VALUES (?, ?)" );
53 my $comp = $newdb->prepare( "INSERT INTO history (owner, location, nodename, nodenote, seen, time) VALUES (?, ?, ?, ?, ?, '2000-01-01 00:00:00')" );
54 my $setMain = $newdb->prepare( 'UPDATE locations SET
55                                 mainhistory = ?,
56                                 name = ( SELECT nodename FROM history WHERE id = ? ),
57                                 note = ( SELECT nodenote FROM history WHERE id = ? )
58                         WHERE
59                                 id = ?' );
60
61 my %rex = (
62         'v' => sub {
63                 my $i = shift;
64                 "PC/$i";
65         },
66         'd' => sub {
67                 my $i = shift;
68                 $i =~ s/(.{4,4})(.*)/PC\/$1\/$2/;
69                 return $i;
70         },
71         's' => sub {
72                 my $i = shift;
73                 $i =~ s/(.{4,4})(.{4,4})(.*)/PC\/$1\/$2\/$3/;
74                 return $i;
75         }
76 );
77
78 $itemq->execute();
79 while( my( $id, $name, $description, $author, $status, $type ) = $itemq->fetchrow_array ) {
80         $id = &{$rex{$type}}( $id );
81         my $parent = $id;
82         $parent =~ s/\/[^\/]+$//;
83         eval {#Add it if not present yet
84                 $itemp->execute( $id, $parent );
85         };
86         $author = '' unless( defined $author );
87         $comp->execute( $users{$author} ? $users{$author} : undef, $id, $name, $description, !$status );
88         unless( $status ) {
89                 my $last = $newdb->last_insert_id( undef, undef, undef, undef );
90                 $setMain->execute( $last, $last, $last, $id );
91         }
92 }
93
94 $newdb->commit();
95 $newdb->disconnect();