3105f770e83a81363442aad990ef0a90f40c0a44
[iserv-mod-room-reservation.git] / update-iserv1-iserv2.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5 use IServ::DB;
6 use Time::Local;
7
8 my $OLDCFG = "/old/opt/iserv/idesk/rooms/admin/config.inc.rpmsave";
9 # FIXME insert real path
10 my $NEWCFG = "test.inc";
11
12 my @tsbegin;
13 my @tsend;
14 my @tsbeginold;
15 my @tsendold;
16 my @allowedgroups;
17 my @admingroups;
18 my $restrictaccess = 0;
19 my $showweekend = 0;
20 my $showlessons = 1;
21
22 # convert the config file
23 open IN, "<", $OLDCFG or die "ERROR: old config file could not be opened: $!";
24 while(<IN>) {
25 # skip unused variables
26 if(/\$cfgRooms\[(\"LogOnInsert\"|\'LogOnInsert\')\]/) {
27 #print "NOTICE: config variable \"LogOnInsert\" is no longer used.\n";
28 } elsif(/\$cfgRooms\[(\"OldBookings\"|\'OldBookings\')\]/) {
29 #print "NOTICE: config variable \"OldBookings\" is no longer used.\n";
30 } elsif(/\$cfgRooms\[(\"ShowClassEdit\"|\'ShowClassEdit\')\]/) {
31 #print "NOTICE: config variable \"ShowClassEdit\" is no longer used.\n";
32 } elsif(/\$cfgRooms\[(\"ClassEditText\"|\'ClassEditText\')\]/) {
33 #print "NOTICE: config variable \"ClassEditText\" is no longer used.\n";
34
35 # AbsTime is now named ShowLessons
36 } elsif(/\$cfgRooms\[(\"AbsTime\"|\'AbsTime\')\]\s*=\s*(true|false|0|1)\s*/) {
37 print "AbsTime is $2\n";
38 $showlessons = ($2 eq "true" or $2 eq "1") ? 1 : 0;
39
40 # ShowWeekend
41 } elsif(/\$cfgRooms\[(\"ShowWeekend\"|\'ShowWeekend\')\]\s*=\s*(true|false|0|1)/) {
42 print "ShowWeekend is $2\n";
43 $showweekend = ($2 eq "true" or $2 eq "1") ? 1 : 0;
44
45 # timeslice beginnings
46 } elsif(/\$cfgRooms\[(\"TimeslicesBegin\"|\'TimeslicesBegin\')\]\s*=\s*array\s*\(/) {
47 print "processing timeslice beginnings...\n";
48 while(<IN>) {
49 if(/\);/) {
50 last;
51 } else {
52 /\s*(\d)\s*=>\s*[\'\"](\d\d?):(\d\d)[\'\"]\s*/;
53 my $hr = $2;
54 $hr = "0$hr" if length $hr < 2;
55 push @tsbeginold, "$hr:$3:00";
56 push @tsbegin, Time::Local::timelocal 0, $3, $hr, 1, 0, 1970;
57 print " found beginning: $hr:$3\n";
58 }
59 }
60
61 # timeslice endings
62 } elsif(/\$cfgRooms\[(\"TimeslicesEnd\"|\'TimeslicesEnd\')\]\s*=\s*array\s*\(/) {
63 print "processing timeslice endings...\n";
64 while(<IN>) {
65 if(/\);/) {
66 last;
67 } else {
68 /\s*(\d)\s*=>\s*[\'\"](\d\d?):(\d\d)[\'\"]\s*/;
69 my $hr = $2;
70 $hr = "0$hr" if length $hr < 2;
71 push @tsendold, "$hr:$3:00";
72 push @tsend, Time::Local::timelocal 0, $3, $hr, 1, 0, 1970;
73 print " found ending: $hr:$3\n";
74 }
75 }
76
77 # add rooms to the database, if they do not exist yet
78 } elsif(/\$cfgRooms\[(\"Rooms\"|\'Rooms\')\]\s*=\s*array\s*\(/) {
79 print "processing rooms...\n";
80 while(<IN>) {
81 if(/\);/) {
82 last;
83 } else {
84 /\s*\d\s*=>\s*((\'([^\']*)\')|(\"([^\"]*)\"))\s*/;
85 my $sqlval = IServ::DB::Val $3;
86 if(IServ::DB::Rows "SELECT * FROM rooms WHERE name = $sqlval;") {
87 print " room '$3' found in database.\n";
88 } else {
89 print "NOTICE: room '$3' not found in database, adding it.\n";
90 IServ::DB::Put "rooms", { "name" => $3 } or die $!;
91 }
92 }
93 }
94
95 # convert old user rights to privileges
96 } elsif(/\$cfgRooms\[(\"RestrictAccess\"|\'RestrictAccess\')\]\s*=\s*(false|true|0|1)/) {
97 #print "config variable \"RestrictAccess\" is no longer used.\n";
98 $restrictaccess = ($2 eq "true" or $2 eq "1") ? 1 : 0;
99
100 } elsif(/\$cfgRooms\[(\"AllowedGroups\"|\'AllowedGroups\')\]\s*=\s*array\s*\(/) {
101 print "processing allowed groups...\n";
102 while(<IN>) {
103 if(/\);/) {
104 last;
105 } else {
106 /\s*\d\s*=>\s*((\'([^\']*)\')|(\"([^\"]*)\"))\s*/;
107 my $name = IServ::DB::Val $3;
108 my @act = IServ::DB::GetArr "SELECT act FROM groups WHERE name=$name;";
109 if(@act) {
110 print " found group $name.\n";
111 push @allowedgroups, $act[0]{"act"};
112 } else {
113 print "NOTICE: group $name not found in database, ignoring it.\n";
114 }
115 }
116 }
117
118 } elsif(/\$cfgRooms\[(\"GroupsAdmin\"|\'GroupsAdmin\')\]\s*=\s*array\s*\(/) {
119 #print "config variable AdminGroups is not longer used\n"
120 print "processing admin groups...\n";
121 while(<IN>) {
122 if(/\);/) {
123 last;
124 } else {
125 /\s*\d\s*=>\s*((\'([^\']*)\')|(\"([^\"]*)\"))\s*/;
126 my $name = IServ::DB::Val $3;
127 my @act = IServ::DB::GetArr "SELECT act FROM groups WHERE name=$name;";
128 if(@act) {
129 print " found group $name.\n";
130 push @admingroups, $act[0]{"act"};
131 } else {
132 print "NOTICE: group $name not found in database, ignoring it.\n";
133 }
134 }
135 }
136 }
137 }
138
139 print "converting old user rights to privileges...\n";
140 # change the privilege names to the right ones
141 if($restrictaccess) {
142 foreach(@allowedgroups) {
143 my $priv = "mod_roomreservation_view";
144 my $act = "$_";
145 my @act = IServ::DB::GetArr "SELECT act FROM privileges_assign ".
146 "WHERE privilege='$priv' AND act='$act';";
147 if(@act) {
148 print " group $_ is already allowed to see the bookings, ignoring it.\n";
149 } else {
150 IServ::DB::Put "privileges_assign", { "act" => $_,
151 "privilege" => $priv } or die $!;
152 print " allowed viewing for group '$_'\n";
153 }
154
155 $priv = "mod_roomreservation_book";
156 @act = "SELECT act FROM privileges_assign ".
157 "WHERE privilege='$priv' AND act='$act';";
158 if(@act) {
159 print " group $_ is already allowed to book, ignoring it.\n";
160 } else {
161 IServ::DB::Put "privileges_assign", { "act" => $_,
162 "privilege" => $priv } or die $!;
163 print " allowed booking for group '$_'\n";
164 }
165 }
166 }
167 foreach(@admingroups) {
168 my $priv = "mod_roomreservation_admin";
169 my $act = "$_";
170 my @act = IServ::DB::GetArr "SELECT act FROM privileges_assign ".
171 "WHERE privilege='$priv' AND act='$act';";
172 if(@act) {
173 print " group $_ has already adminship, ignoring it.\n";
174 } else {
175 IServ::DB::Put "privileges_assign", { "act" => $_,
176 "privilege" => $priv } or die $!;
177 print " allowed administration for group '$_'\n";
178 }
179 }
180
181 print "writing config file...\n";
182 system "touch $NEWCFG";
183 open OUT, ">", $NEWCFG or die "ERROR: new config file could not be opened: $!";
184 print OUT "<?php\n\$this->flushTimeslices();\n";
185 for(my $i = 0; $i <= $#tsbegin; $i++) {
186 print OUT "\$this->addTimeslice(new rsTimeslice($tsbegin[$i],$tsend[$i]));\n";
187 }
188 print OUT "\$this->setShowWeekend($showweekend);\n";
189 print OUT "\$this->setShowLessons($showlessons);\n";
190 print OUT "?>";
191 close OUT;
192 close IN;
193
194 print "\nconverting the database. Have a lot of fun...\n";
195 open IN, "cat /old/rooms.sql | iconv -f utf8 -t utf8 |" or die
196 #open IN, "</old/pgdump.sql" or die
197 "ERROR: the database dump could not be opened: $!\n";
198
199 # build hashes of the form "timestamp" => "timeslice" ("15:00:00" => "3")
200 my $i = 0;
201 my %tsbeginoldkeys;
202 my %tsendoldkeys;
203 foreach (@tsbeginold) {
204 $tsbeginoldkeys{$_} = $i;
205 $i++;
206 }
207 $i = 0;
208 foreach (@tsendold) {
209 $tsendoldkeys{$_} = $i;
210 $i++;
211 }
212
213 #my ($key, $val);
214 #while(($key, $val) = each(%tsbeginoldkeys)) {
215 # print "$key => $val\n";
216 #}
217 #while(($key, $val) = each(%tsendoldkeys)) {
218 # print "$ke:y => $val\n";
219 #}
220
221 # FIXME database conversion
222 print "Importing old bookings...\n";
223 IServ::DB::Exec "CREATE TABLE mod_roomreservation_bookings_old (id INT NOT NULL PRIMARY KEY, room TEXT NOT NULL, date DATE NOT NULL, timebegin TIME NOT NULL, timeend TIME NOT NULL, act TEXT REFERENCES users(Act) ON DELETE CASCADE ON UPDATE CASCADE NOT NULL, class TEXT, reason TEXT NOT NULL, fixed BOOL);";
224 while(<IN>) {
225 $_ =~ s/INSERT INTO rooms/INSERT INTO mod_roomreservation_bookings_old/;
226 #print "$_";
227 IServ::DB::Exec($_);
228 }
229 #IServ::DB::Exec "INSERT INTO mod_roomreservation_bookings";
230 IServ::DB::Exec "DROP TABLE mod_roomreservation_bookings_old;";
231
232 die 333;
233 $i = 0;
234
235 my $maxid = 0;
236 while(<IN>) {
237 if(/COPY \"rooms\" FROM stdin;/) {
238 while(<IN>) {
239 if(/\\\./) {
240 last;
241 } elsif(/(\d+)\t([^\t]*)\t(\d{4}-\d\d-\d\d)\t(\d\d:\d\d:\d\d)\t(\d\d:\d\d:\d\d)\t([a-zA-Z0-9\.-]+)\t([^\t]*)\t([^\t]*)\t(t|f)/) {
242 $i++;
243 # FIXME convert the timeslices!!!
244 # FIXME insert the right table name and rs_weekly
245 # ignore uid, it is serial, also class is not used anymore
246 # print $tsbeginoldkeys{$4}; print "\n";
247 IServ::DB::Put "roomschedule", { "rs_room" => $2, "rs_date" => $3,
248 "rs_tsfirst" => $tsbeginoldkeys{$4}, "rs_tslast" => $tsendoldkeys{$5},
249 "rs_act" => $6, "rs_reason" => $8, "rs_weekly" => $9 };
250 }
251 }
252 }
253 }
254 print "$i datasets converted.\ndone!\n";
This page took 0.077769 seconds and 3 git commands to generate.