fcb640e8bc3f232c2ef61b0887e299e1dd07f829
[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 my $NEWCFG = "/usr/share/iserv/www/inc/mod_room-reservation/config.inc";
10
11 my @tsbegin;
12 my @tsend;
13 my @tsbeginold;
14 my @tsendold;
15 my @allowedgroups;
16 my @admingroups;
17 my $restrictaccess = 0;
18 my $showweekend = 0;
19 my $showlessons = 1;
20
21 # convert the config file
22 open IN, "<", $OLDCFG or die "ERROR: old config file could not be opened: $!";
23 while(<IN>) {
24 # skip unused variables
25 if(/\$cfgRooms\[(\"LogOnInsert\"|\'LogOnInsert\')\]/) {
26 } elsif(/\$cfgRooms\[(\"OldBookings\"|\'OldBookings\')\]/) {
27 } elsif(/\$cfgRooms\[(\"ShowClassEdit\"|\'ShowClassEdit\')\]/) {
28 } elsif(/\$cfgRooms\[(\"ClassEditText\"|\'ClassEditText\')\]/) {
29
30 # AbsTime is now named ShowLessons
31 } elsif(/\$cfgRooms\[(\"AbsTime\"|\'AbsTime\')\]\s*=\s*(true|false|0|1)\s*/) {
32 print "AbsTime is $2\n";
33 $showlessons = ($2 eq "true" or $2 eq "1") ? 1 : 0;
34
35 # ShowWeekend
36 } elsif(/\$cfgRooms\[(\"ShowWeekend\"|\'ShowWeekend\')\]\s*=\s*(true|false|0|1)/) {
37 print "ShowWeekend is $2\n";
38 $showweekend = ($2 eq "true" or $2 eq "1") ? 1 : 0;
39
40 # timeslice beginnings
41 } elsif(/\$cfgRooms\[(\"TimeslicesBegin\"|\'TimeslicesBegin\')\]\s*=\s*array\s*\(/) {
42 print "processing timeslice beginnings...\n";
43 while(<IN>) {
44 if(/\);/) {
45 last;
46 } else {
47 /\s*(\d)\s*=>\s*[\'\"](\d\d?):(\d\d)[\'\"]\s*/;
48 my $hr = $2;
49 $hr = "0$hr" if length $hr < 2;
50 push @tsbeginold, "$hr:$3:00";
51 push @tsbegin, Time::Local::timelocal 0, $3, $hr, 1, 0, 1970;
52 print " found beginning: $hr:$3\n";
53 }
54 }
55
56 # timeslice endings
57 } elsif(/\$cfgRooms\[(\"TimeslicesEnd\"|\'TimeslicesEnd\')\]\s*=\s*array\s*\(/) {
58 print "processing timeslice endings...\n";
59 while(<IN>) {
60 if(/\);/) {
61 last;
62 } else {
63 /\s*(\d)\s*=>\s*[\'\"](\d\d?):(\d\d)[\'\"]\s*/;
64 my $hr = $2;
65 $hr = "0$hr" if length $hr < 2;
66 push @tsendold, "$hr:$3:00";
67 push @tsend, Time::Local::timelocal 0, $3, $hr, 1, 0, 1970;
68 print " found ending: $hr:$3\n";
69 }
70 }
71
72 # add rooms to the database, if they do not exist yet
73 } elsif(/\$cfgRooms\[(\"Rooms\"|\'Rooms\')\]\s*=\s*array\s*\(/) {
74 print "processing rooms...\n";
75 while(<IN>) {
76 if(/\);/) {
77 last;
78 } else {
79 /\s*\d\s*=>\s*((\'([^\']*)\')|(\"([^\"]*)\"))\s*/;
80 my $sqlval = IServ::DB::Val $3;
81 if(IServ::DB::Rows "SELECT * FROM rooms WHERE name = $sqlval;") {
82 print " room '$3' found in database.\n";
83 } else {
84 print "NOTICE: room '$3' not found in database, adding it.\n";
85 IServ::DB::Put "rooms", { "name" => $3 } or die $!;
86 }
87 }
88 }
89
90 # convert old user rights to privileges
91 } elsif(/\$cfgRooms\[(\"RestrictAccess\"|\'RestrictAccess\')\]\s*=\s*(false|true|0|1)/) {
92 $restrictaccess = ($2 eq "true" or $2 eq "1") ? 1 : 0;
93
94 } elsif(/\$cfgRooms\[(\"AllowedGroups\"|\'AllowedGroups\')\]\s*=\s*array\s*\(/) {
95 print "processing allowed groups...\n";
96 while(<IN>) {
97 if(/\);/) {
98 last;
99 } else {
100 /\s*\d\s*=>\s*((\'([^\']*)\')|(\"([^\"]*)\"))\s*/;
101 my $name = IServ::DB::Val $3;
102 my @act = IServ::DB::GetArr "SELECT act FROM groups WHERE name=$name;";
103 if(@act) {
104 print " found group $name.\n";
105 push @allowedgroups, $act[0]{"act"};
106 } else {
107 print "NOTICE: group $name not found in database, ignoring it.\n";
108 }
109 }
110 }
111
112 } elsif(/\$cfgRooms\[(\"GroupsAdmin\"|\'GroupsAdmin\')\]\s*=\s*array\s*\(/) {
113 print "processing admin groups...\n";
114 while(<IN>) {
115 if(/\);/) {
116 last;
117 } else {
118 /\s*\d\s*=>\s*((\'([^\']*)\')|(\"([^\"]*)\"))\s*/;
119 my $name = IServ::DB::Val $3;
120 my @act = IServ::DB::GetArr "SELECT act FROM groups WHERE name=$name;";
121 if(@act) {
122 print " found group $name.\n";
123 push @admingroups, $act[0]{"act"};
124 } else {
125 print "NOTICE: group $name not found in database, ignoring it.\n";
126 }
127 }
128 }
129 }
130 }
131
132 print "converting old user rights to privileges...\n";
133 # change the privilege names to the right ones
134 if($restrictaccess) {
135 foreach(@allowedgroups) {
136 my $priv = "mod_roomreservation_view";
137 my $act = "$_";
138 my @act = IServ::DB::GetArr "SELECT act FROM privileges_assign ".
139 "WHERE privilege='$priv' AND act='$act';";
140 if(@act) {
141 print " group $_ is already allowed to see the bookings, ignoring it.\n";
142 } else {
143 IServ::DB::Put "privileges_assign", { "act" => $_,
144 "privilege" => $priv } or die $!;
145 print " allowed viewing for group '$_'\n";
146 }
147
148 $priv = "mod_roomreservation_book";
149 @act = "SELECT act FROM privileges_assign ".
150 "WHERE privilege='$priv' AND act='$act';";
151 if(@act) {
152 print " group $_ is already allowed to book, ignoring it.\n";
153 } else {
154 IServ::DB::Put "privileges_assign", { "act" => $_,
155 "privilege" => $priv } or die $!;
156 print " allowed booking for group '$_'\n";
157 }
158 }
159 }
160 foreach(@admingroups) {
161 my $priv = "mod_roomreservation_admin";
162 my $act = "$_";
163 my @act = IServ::DB::GetArr "SELECT act FROM privileges_assign ".
164 "WHERE privilege='$priv' AND act='$act';";
165 if(@act) {
166 print " group $_ has already adminship, ignoring it.\n";
167 } else {
168 IServ::DB::Put "privileges_assign", { "act" => $_,
169 "privilege" => $priv } or die $!;
170 print " allowed administration for group '$_'\n";
171 }
172 }
173
174 print "writing config file...\n";
175 system "touch $NEWCFG";
176 open OUT, ">", $NEWCFG or die "ERROR: new config file could not be opened: $!";
177 print OUT "<?php\n\$this->flushTimeslices();\n";
178 for(my $i = 0; $i <= $#tsbegin; $i++) {
179 print OUT "\$this->addTimeslice(new mod_roomReservationTimeslice($tsbegin[$i],$tsend[$i]));\n";
180 }
181 print OUT "\$this->setShowWeekend($showweekend);\n";
182 print OUT "\$this->setShowLessons($showlessons);\n";
183 print OUT "?>";
184 close OUT;
185 close IN;
186
187 print "\nconverting the database, have a lot of fun...\n";
188 open IN, "cat /old/rooms.sql | iconv -f utf8 -t utf8 |" or die
189 "ERROR: the database dump could not be opened: $!\n";
190
191 # build hashes of the form "timestamp" => "timeslice" ("15:00:00" => "3")
192 my $i = 0;
193 my %tsbeginoldkeys;
194 my %tsendoldkeys;
195 foreach (@tsbeginold) {
196 $tsbeginoldkeys{$_} = $i;
197 $i++;
198 }
199 $i = 0;
200 foreach (@tsendold) {
201 $tsendoldkeys{$_} = $i;
202 $i++;
203 }
204
205 # database conversion
206 # insert old data and then convert them
207 print "importing old bookings...\n";
208 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);";
209 while(<IN>) {
210 # rename the table
211 $_ =~ s/INSERT INTO rooms/INSERT INTO mod_roomreservation_bookings_old/;
212 IServ::DB::Exec($_);
213 }
214
215 print "converting the database...\n";
216 foreach(IServ::DB::GetArr("SELECT * FROM mod_roomreservation_bookings_old;")) {
217 my %row = %{$_};
218 IServ::DB::Put "mod_roomreservation_bookings", { "rrb_room" => $row{"room"},
219 "rrb_date" => $row{"date"}, "rrb_tsfirst" =>
220 $tsbeginoldkeys{$row{"timebegin"}}, "rrb_tslast" =>
221 $tsendoldkeys{$row{"timeend"}}, "rrb_act" => $row{"act"}, "rrb_reason" =>
222 $row{"reason"}, "rrb_interval" => $row{"fixed"} };
223 }
224
225 # delete old table
226 IServ::DB::Exec "DROP TABLE mod_roomreservation_bookings_old;";
227
228 print "done.\n";
229
This page took 0.058425 seconds and 3 git commands to generate.