42354593fab636896cdaea9b925838ba7e44f449
[migration-tools.git] / kmig.d / bin / mig-import
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use DBI;
7 use Data::Dumper;
8 use Env qw(
9     HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
10         MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
11 );
12 use open ':encoding(utf8)';
13 use Cwd 'abs_path';
14 use Cwd qw(getcwd);
15 use FindBin;
16 use XML::LibXML;
17 use open ':std', ':encoding(UTF-8)';
18 use Encode qw( decode_utf8 );
19 use List::Util qw( min max );
20 my $mig_bin = "$FindBin::Bin/";
21 use lib "$FindBin::Bin/";
22 use KMig;
23
24 my $dbh = KMig::db_connect();
25 #$dbh->{mysql_enable_utf8mb4} = 1;
26 $dbh->do('SET NAMES utf8mb4');
27
28 #to do check for array passed and if not present then use tags 
29
30 my @taglist = @ARGV;
31 my $arg_list_length = scalar @taglist;
32 if($arg_list_length < 1) { @taglist = ("authorisedvalues","calendar","circrules","itemtypes","libraries","patrontypes","preferences","reports","smsproviders"); } 
33 $MIGGITDIR =~ s/\/\//\//;
34
35 foreach my $restore (@taglist) {
36     my $restorefile;
37     if ($restore eq 'authorisedvalues') {
38         my $timestamp = most_recent_single($MIGGITDIR,'authorisedvalues');
39         if ($timestamp) { $restorefile = $MIGGITDIR . 'authorisedvalues' . '.' . $timestamp . '.xml'; }
40         print "Restoring from $restorefile ... \n";
41         if ($restorefile) { restore_authorisedvalues($dbh,$restorefile); }
42     }
43     if ($restore eq 'calendar') {
44         my $timestamp = most_recent_single($MIGGITDIR,'calendar');
45         if ($timestamp) { $restorefile = $MIGGITDIR . 'calendar' . '.' . $timestamp . '.xml'; }
46         print "Restoring from $restorefile ... \n";
47         if ($restorefile) { restore_calendar($dbh,$restorefile); }
48     }
49     if ($restore eq 'circrules') {
50         my $timestamp = most_recent_single($MIGGITDIR,'circrules');
51         if ($timestamp) { $restorefile = $MIGGITDIR . 'circrules' . '.' . $timestamp . '.xml'; }
52         print "Restoring from $restorefile ... \n";
53         if ($restorefile) { restore_circrules($dbh,$restorefile); }
54     }
55     if ($restore eq 'itemtypes') {
56         my $timestamp = most_recent_single($MIGGITDIR,'itemtypes');
57         if ($timestamp) { $restorefile = $MIGGITDIR . 'itemtypes' . '.' . $timestamp . '.xml'; }
58         print "Restoring from $restorefile ... \n";
59         if ($restorefile) { restore_itemtypes($dbh,$restorefile); }
60     }
61     if ($restore eq 'libraries') {
62         my $timestamp = most_recent_single($MIGGITDIR,'libraries');
63         if ($timestamp) { $restorefile = $MIGGITDIR . 'libraries' . '.' . $timestamp . '.xml'; }
64         print "Restoring from $restorefile ... \n";
65         if ($restorefile) { restore_libraries($dbh,$restorefile); }
66     }
67     if ($restore eq 'patrontypes') {
68         my $timestamp = most_recent_single($MIGGITDIR,'patrontypes');
69         if ($timestamp) { $restorefile = $MIGGITDIR . 'patrontypes' . '.' . $timestamp . '.xml'; }
70         print "Restoring from $restorefile ... \n";
71         if ($restorefile) { restore_patrontypes($dbh,$restorefile); }
72     }
73     if ($restore eq 'preferences') {
74         my $timestamp = most_recent_single($MIGGITDIR,'systempreferences');
75         if ($timestamp) { $restorefile = $MIGGITDIR . 'systempreferences' . '.' . $timestamp . '.xml'; }
76         print "Restoring from $restorefile ... \n";
77         if ($restorefile) { restore_preferences($dbh,$restorefile); }
78         print "IMPORTANT : if you are changing system preferences you may need to run 'sudo systemctl restart apache2 memcached'\n\n";
79     }
80     if ($restore eq 'reports') {
81         my $timestamp = most_recent_single($MIGGITDIR,'reports');
82         if ($timestamp) { $restorefile = $MIGGITDIR . 'reports' . '.' . $timestamp . '.xml'; }
83         print "Restoring from $restorefile ... \n";
84         if ($restorefile) { restore_reports($dbh,$restorefile); }
85     }
86     if ($restore eq 'smsproviders') {
87         my $timestamp = most_recent_single($MIGGITDIR,'smsproviders');
88         if ($timestamp) { $restorefile = $MIGGITDIR . 'smsproviders' . '.' . $timestamp . '.xml'; }
89         print "Restoring from $restorefile ... \n";
90         if ($restorefile) { restore_smsproviders($dbh,$restorefile); }
91     }
92 }
93
94 print "Done.\n";
95
96
97 sub most_recent_single {
98     my $MGIGITDIR = shift;
99     my $str = shift;
100     my @files;
101     my @times;
102     opendir (DIR, $MIGGITDIR) or abort("could not open $MIGGITDIR");;
103     while (my $file = readdir(DIR)) {
104         if ($file =~ m/$str/) { push @files, $file; }
105     }
106     foreach my $file (@files) {
107         my @f = split /\./, $file;
108         push @times, $f[1];
109     }
110     closedir(DIR);
111     my $max = max @times; 
112     return $max;
113 }
114
115 sub restore_authorisedvalues {
116     my $dbh = shift;
117     my $restore_file = shift;
118     my $parser = XML::LibXML->new();
119     my $dom = $parser->parse_file($restore_file);
120
121     my $query = "DELETE FROM authorised_values WHERE category != 'ITEMTYPECAT'";
122     my $sth = $dbh->prepare($query);
123     $sth->execute();
124
125     foreach my $node ($dom->findnodes('//value')) {
126         my $category = sql_str($node->findvalue('./category'));
127         my $authorised_value = sql_str($node->findvalue('./authorised_value'));
128         my $lib = sql_str($node->findvalue('./lib'));
129         my $lib_opac = sql_str($node->findvalue('./lib_opac'));
130         my $imageurl = sql_str($node->findvalue('./imageurl'));
131         $query = "INSERT INTO authorised_values (category,authorised_value,lib,lib_opac,imageurl) VALUES ($category,$authorised_value,$lib,$lib_opac,$imageurl)";
132         $sth = $dbh->prepare($query);
133         $sth->execute();
134     }
135     return;
136 }
137
138 sub restore_calendar {
139     my $dbh = shift;
140     my $restore_file = shift;
141     my $parser = XML::LibXML->new();
142     my $dom = $parser->parse_file($restore_file);
143
144     my $query = "DELETE FROM repeatable_holidays WHERE 1 = 1";
145     my $sth = $dbh->prepare($query);
146     $sth->execute();
147
148     $query = "DELETE FROM special_holidays WHERE 1 = 1";
149     $sth = $dbh->prepare($query);
150     $sth->execute();
151
152     foreach my $node ($dom->findnodes('/holidays/repeatable')) {
153         my $branchcode = sql_str($node->findvalue('./branchcode'));
154         my $weekday = sql_num($node->findvalue('./weekday'));
155         my $day = sql_num($node->findvalue('./day'));
156         my $month = sql_num($node->findvalue('./month'));
157         my $title = sql_str($node->findvalue('./title'));
158         my $description = sql_str($node->findvalue('./description'));
159         $query = "INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description) VALUES ($branchcode,$weekday,$day,$month,$title,$description)";
160         $sth = $dbh->prepare($query);
161         $sth->execute();
162     }
163
164     foreach my $node ($dom->findnodes('/holidays/special')) {
165         my $branchcode = sql_str($node->findvalue('./branchcode'));
166         my $day = sql_num($node->findvalue('./day'));
167         my $month = sql_num($node->findvalue('./month'));
168         my $year = sql_num($node->findvalue('./year'));
169         my $isexception = sql_num($node->findvalue('./isexception'));
170         my $title = sql_str($node->findvalue('./title'));
171         my $description = sql_str($node->findvalue('./description'));
172         $query = "INSERT INTO special_holidays (branchcode,day,month,year,isexception,title,description) VALUES ($branchcode,$day,$month,$year,$isexception,$title,$description)";
173         $sth = $dbh->prepare($query);
174         $sth->execute();
175     }
176
177     return;
178 }
179
180 sub restore_circrules {
181     my $dbh = shift;
182     my $restore_file = shift;
183     my $parser = XML::LibXML->new();
184     my $dom = $parser->parse_file($restore_file);
185  
186     #order of operations - clean out circ_rules, then issuingrules; restore issuingrules then circ_rules 
187
188     my $query = "DELETE FROM circulation_rules WHERE 1 = 1";
189     my $sth = $dbh->prepare($query);
190     $sth->execute();
191
192     $query = "DELETE FROM issuingrules WHERE 1 = 1";
193     $sth = $dbh->prepare($query);
194     $sth->execute();
195
196     foreach my $node ($dom->findnodes('//issuing/rule')) {
197         my $categorycode = sql_str($node->findvalue('./categorycode'));
198         my $itemtype = sql_str($node->findvalue('./itemtype'));
199         my $restrictedtype = sql_num($node->findvalue('./restrictedtype'));
200         my $rentaldiscount = sql_num($node->findvalue('./rentaldiscount'));
201         my $reservecharge = sql_num($node->findvalue('./reservecharge'));
202         my $fine = sql_num($node->findvalue('./fine'));
203         my $finedays = sql_num($node->findvalue('./finedays'));
204         my $maxsuspensiondays = sql_num($node->findvalue('./maxsuspensiondays'));
205         my $suspension_chargeperiod = sql_num($node->findvalue('./suspension_chargeperiod'));
206         my $firstremind = sql_num($node->findvalue('./firstremind'));
207         my $chargeperiod = sql_num($node->findvalue('./chargeperiod'));
208         my $chargeperiod_charge_at = sql_num($node->findvalue('./chargeperiod_charge_at'));
209         my $accountsent = sql_num($node->findvalue('./accountsent'));
210         my $issuelength = sql_num($node->findvalue('./issuelength'));
211         my $lengthunit = sql_str($node->findvalue('./lengthunit'));
212         my $hardduedate = sql_str($node->findvalue('./hardduedate'));
213         my $hardduedatecompare = sql_num($node->findvalue('./hardduedatecompare'));
214         my $renewalsallowed = sql_num($node->findvalue('./renewalsallowed'));
215         my $renewalperiod = sql_num($node->findvalue('./renewalperiod'));
216         my $norenewalbefore = sql_num($node->findvalue('./norenewalbefore'));
217         my $auto_renew = sql_num($node->findvalue('./auto_renew'));
218         my $no_auto_renewal_after = sql_num($node->findvalue('./no_auto_renewal_after'));
219         my $no_auto_renewal_after_hard_limit = sql_str($node->findvalue('./no_auto_renewal_after_hard_limit'));
220         my $reservesallowed = sql_num($node->findvalue('./reservesallowed'));
221         my $holds_per_record = sql_num($node->findvalue('./holds_per_record'));
222         my $holds_per_day = sql_num($node->findvalue('./holds_per_day'));
223         my $branchcode = sql_str($node->findvalue('./branchcode'));
224         my $overduefinescap = sql_num($node->findvalue('./overduefinescap'));
225         my $cap_fine_to_replacement_price = sql_num($node->findvalue('./cap_fine_to_replacement_price'));
226         my $onshelfholds = sql_num($node->findvalue('./onshelfholds'));
227         my $opacitemholds = sql_str($node->findvalue('./opacitemholds'));
228         my $article_requests = sql_str($node->findvalue('./article_requests'));
229         my $note = sql_str($node->findvalue('./note'));
230         $query = "INSERT INTO issuingrules (categorycode,itemtype,restrictedtype,rentaldiscount,reservecharge,fine,finedays,maxsuspensiondays,suspension_chargeperiod,firstremind,chargeperiod,chargeperiod_charge_at,accountsent,issuelength,lengthunit,hardduedate,hardduedatecompare,renewalsallowed,renewalperiod,norenewalbefore,auto_renew,no_auto_renewal_after,no_auto_renewal_after_hard_limit,reservesallowed,holds_per_record,holds_per_day,branchcode,overduefinescap,cap_fine_to_replacement_price,onshelfholds,opacitemholds,article_requests,note) VALUES ($categorycode,$itemtype,$restrictedtype,$rentaldiscount,$reservecharge,$fine,$finedays,$maxsuspensiondays,$suspension_chargeperiod,$firstremind,$chargeperiod,$chargeperiod_charge_at,$accountsent,$issuelength,$lengthunit,$hardduedate,$hardduedatecompare,$renewalsallowed,$renewalperiod,$norenewalbefore,$auto_renew,$no_auto_renewal_after,$no_auto_renewal_after_hard_limit,$reservesallowed,$holds_per_record,$holds_per_day,$branchcode,$overduefinescap,$cap_fine_to_replacement_price,$onshelfholds,$opacitemholds,$article_requests,$note)";
231         $sth = $dbh->prepare($query);
232         $sth->execute();
233     }
234
235     foreach my $node ($dom->findnodes('//circ/rule')) {
236         my $branchcode = sql_str($node->findvalue('./branchcode'));
237         my $categorycode = sql_str($node->findvalue('./categorycode'));
238         my $itemtype = sql_str($node->findvalue('./itemtype'));
239         my $rule_name = sql_str($node->findvalue('./rule_name'));
240         my $rule_value = sql_str($node->findvalue('./rule_value'));
241         $query = "INSERT INTO circulation_rules (branchcode,categorycode,itemtype,rule_name,rule_value) VALUES ($branchcode,$categorycode,$itemtype,$rule_name,$rule_value)";
242         $sth = $dbh->prepare($query);
243         $sth->execute();
244     }
245
246     return;
247 }
248
249 sub restore_itemtypes {
250     my $dbh = shift;
251     my $restorefile = shift;
252     my $parser = XML::LibXML->new();
253     my $dom = $parser->parse_file($restorefile);
254
255     my $query = "UPDATE itemtypes SET searchcategory = NULL";
256     my $sth = $dbh->prepare($query);
257     $sth->execute();
258
259     $query = "DELETE FROM authorised_values WHERE category = 'ITEMTYPECAT'";
260     $sth = $dbh->prepare($query);
261     $sth->execute();
262
263     $query = "DELETE FROM localization WHERE entity = 'itemtypes'";
264     $sth = $dbh->prepare($query);
265     $sth->execute();
266
267     foreach my $node ($dom->findnodes('/authorised_values/value')) {
268         my $category = sql_str($node->findvalue('./category'));
269         my $authvalue = sql_str($node->findvalue('./authvalue'));
270         my $lib = sql_str($node->findvalue('./lib'));
271         my $lib_opac = sql_str($node->findvalue('./lib_opac'));
272         my $image_url = sql_str($node->findvalue('./image_url'));
273         my $query = "INSERT INTO authorised_values (category,authvalue,lib,lib_opac,image_url) VALUES ($category,$authvalue,$lib,$lib_opac,$image_url)";
274         $sth = $dbh->prepare($query);
275         $sth->execute();
276     }
277
278     foreach my $node ($dom->findnodes('/itemtypes/value')) {
279         my $itemtype = sql_str($node->findvalue('./itemtype'));
280         my $description = sql_str($node->findvalue('./description'));
281         my $rentalcharge = sql_str($node->findvalue('./rentalcharge'));
282         my $rentalcharge_daily = sql_str($node->findvalue('./rentalcharge_daily'));
283         my $rentalcharge_hourly = sql_str($node->findvalue('./rentalcharge_hourly'));
284         my $defaultreplacecost = sql_str($node->findvalue('./defaultreplacecost'));
285         my $processfee = sql_str($node->findvalue('./processfee'));
286         my $notforloan = sql_str($node->findvalue('./notforloan'));
287         my $imageurl = sql_str($node->findvalue('./imageurl'));
288         my $summary = sql_str($node->findvalue('./summary'));
289         my $checkinmsg = sql_str($node->findvalue('./checkinmsg'));
290         my $checkinmsgtype = sql_str($node->findvalue('./checkinmsgtype'));
291         my $sip_media_type = sql_str($node->findvalue('./sip_media_type'));
292         my $hideinopac = sql_str($node->findvalue('./hideinopac'));
293         my $searchcategory = sql_str($node->findvalue('./searchcategory'));
294         $query = "SELECT itemtype FROM itemtypes WHERE itemtype = $itemtype";
295         $sth = $dbh->prepare($query);
296         $sth->execute();
297         my $answer;
298         while (my @row = $sth->fetchrow_array) { $answer = sql_str($row[0]); }
299         if (!defined $answer or $answer ne $itemtype) {
300             $query = "INSERT INTO itemtypes (itemtype,description,rentalcharge,rentalcharge_daily,rentalcharge_hourly,defaultreplacecost,processfee,notforloan,imageurl,summary,checkinmsg,sip_media_type,hideinopac,searchcategory) 
301             VALUES ($itemtype,$description,$rentalcharge,$rentalcharge_daily,$rentalcharge_hourly,$defaultreplacecost,$processfee,$notforloan,$imageurl,$summary,$checkinmsg,$sip_media_type,$hideinopac,$searchcategory)";
302             $sth = $dbh->prepare($query);
303             $sth->execute();
304         } else {
305             $query = "UPDATE itemtypes SET description = $description,rentalcharge = $rentalcharge,rentalcharge_daily = $rentalcharge_daily,rentalcharge_hourly = $rentalcharge_hourly,defaultreplacecost = $defaultreplacecost,processfee = $processfee,notforloan = $notforloan,imageurl = $imageurl,summary = $summary,checkinmsg = $checkinmsg,sip_media_type = $sip_media_type,hideinopac = $hideinopac,searchcategory = $searchcategory 
306             WHERE itemtype = $itemtype";
307             $sth = $dbh->prepare($query);
308             $sth->execute();
309         }
310     }
311
312     foreach my $node ($dom->findnodes('//localizations/value')) {
313         my $entity = sql_str($node->findvalue('./entity'));
314         my $code = sql_str($node->findvalue('./code'));
315         my $lang = sql_str($node->findvalue('./lang'));
316         my $translation = sql_str($node->findvalue('./translation'));
317         my $query = "INSERT INTO localization (entity,code,lang,translation) VALUES ($entity,$code,$lang,$translation)";
318         $sth = $dbh->prepare($query);
319         print "$query\n";
320         $sth->execute();
321     }
322
323     return;
324 }
325
326 sub restore_libraries {
327     my $dbh = shift;
328     my $restorefile = shift;
329     my $parser = XML::LibXML->new();
330     my $dom = $parser->parse_file($restorefile);
331
332     my $query = "TRUNCATE library_groups";
333     my $sth = $dbh->prepare($query);
334     $sth->execute();
335
336     foreach my $node ($dom->findnodes('//library')) {
337         my $branchcode = sql_str($node->findvalue('./branchcode'));
338         my $branchname = sql_str($node->findvalue('./branchname'));
339         my $branchaddress1 = sql_str($node->findvalue('./branchaddress1'));
340         my $branchaddress2 = sql_str($node->findvalue('./branchaddress2'));
341         my $branchaddress3 = sql_str($node->findvalue('./branchaddress3'));
342         my $branchzip = sql_str($node->findvalue('./branchzip'));
343         my $branchcity = sql_str($node->findvalue('./branchcity'));
344         my $branchstate = sql_str($node->findvalue('./branchstate'));
345         my $branchcountry = sql_str($node->findvalue('./branchcountry'));
346         my $branchphone = sql_str($node->findvalue('./branchphone'));
347         my $branchfax = sql_str($node->findvalue('./branchfax'));
348         my $branchemail = sql_str($node->findvalue('./branchemail'));
349         my $branchreplyto = sql_str($node->findvalue('./branchreplyto'));
350         my $branchreturnpath = sql_str($node->findvalue('./branchreturnpath'));
351         my $branchurl = sql_str($node->findvalue('./branchurl'));
352         my $issuing = sql_str($node->findvalue('./issuing'));
353         my $branchip = sql_str($node->findvalue('./branchip'));
354         my $branchprinter = sql_str($node->findvalue('./branchprinter'));
355         my $branchnotes = sql_str($node->findvalue('./branchnotes'));
356         my $opac_info = sql_str($node->findvalue('./opac_info'));
357         my $geolocation = sql_str($node->findvalue('./geolocation'));
358         my $marcorgcode = sql_str($node->findvalue('./marcorgcode'));
359         my $pickup_location = $node->findvalue('./pickup_location');
360         $query = "SELECT branchcode FROM branches WHERE branchcode = $branchcode";
361         $sth = $dbh->prepare($query);
362         $sth->execute();
363         my $answer;
364         while (my @row = $sth->fetchrow_array) { $answer = sql_str($row[0]); }
365         if (!defined $answer or $answer ne $branchcode) { 
366             $query = "INSERT INTO branches (branchcode,branchname,branchaddress1,branchaddress2,branchaddress3,branchzip,branchcity,branchstate,branchcountry,branchphone,branchfax,branchemail,branchreplyto,branchreturnpath,branchurl,issuing,branchip,branchprinter,branchnotes,opac_info,geolocation,marcorgcode,pickup_location) VALUES ($branchcode,$branchname,$branchaddress1,$branchaddress2,$branchaddress3,$branchzip,$branchcity,$branchstate,$branchcountry,$branchphone,$branchfax,$branchemail,$branchreplyto,$branchreturnpath,$branchurl,$issuing,$branchip,$branchprinter,$branchnotes,$opac_info,$geolocation,$marcorgcode,$pickup_location)";
367             $sth = $dbh->prepare($query);
368             $sth->execute();
369         } else {
370             $query = "UPDATE branches SET branchname = $branchname,branchaddress1 = $branchaddress1,branchaddress2 = $branchaddress2,branchaddress3 = $branchaddress3,branchzip = $branchzip,branchcity = $branchcity,branchstate = $branchstate,branchcountry = $branchcountry,branchphone = $branchphone,branchfax = $branchfax,branchemail = $branchemail,branchreplyto = $branchreplyto,branchreturnpath = $branchreturnpath,branchurl = $branchurl,issuing = $issuing,branchip = $branchip,branchprinter = $branchprinter,branchnotes = $branchnotes,opac_info = $opac_info,geolocation = $geolocation,marcorgcode = $marcorgcode,pickup_location = $pickup_location WHERE branchcode = $branchcode";
371             $sth = $dbh->prepare($query);
372             $sth->execute();  
373         }
374     }
375
376     #haven't taken into account them needing to go in, in a specific order so I could check parent ids first 
377     foreach my $node ($dom->findnodes('//library_group')) {
378         my $id = $node->findvalue('./id');
379         my $parent_id = sql_num($node->findvalue('./parent_id'));
380         my $branchcode = sql_str($node->findvalue('./branchcode'));
381         my $title = sql_str($node->findvalue('./title'));
382         my $descr = sql_str($node->findvalue('./description'));
383         my $ft1 = sql_num($node->findvalue('./ft_hide_patron_info'));
384         my $ft2 = sql_num($node->findvalue('./ft_search_groups_opac'));
385         my $ft3 = sql_num($node->findvalue('./ft_search_groups_staff'));
386         $query = "INSERT INTO library_groups (id,parent_id,branchcode,title,description,ft_hide_patron_info,ft_search_groups_opac,ft_search_groups_staff) VALUES ($id,$parent_id,$branchcode,$title,$descr,$ft1,$ft2,$ft3)";
387         $sth = $dbh->prepare($query);
388         $sth->execute();
389     }
390     return;
391 }
392
393 sub restore_patrontypes {
394     my $dbh = shift;
395     my $restorefile = shift;
396     my $parser = XML::LibXML->new();
397     my $dom = $parser->parse_file($restorefile);
398
399     my $query;
400     my $sth;
401
402     foreach my $node ($dom->findnodes('//category')) {
403         my $categorycode = sql_str($node->findvalue('./categorycode'));
404         my $description = sql_str($node->findvalue('./description'));
405         my $enrolmentperiod = sql_num($node->findvalue('./enrolmentperiod'));
406         my $enrolmentperioddate = sql_str($node->findvalue('./enrolmentperioddate'));
407         my $upperagelimit = sql_num($node->findvalue('./upperagelimit'));
408         my $dateofbirthrequired = sql_num($node->findvalue('./dateofbirthrequired'));
409         my $finetype = sql_str($node->findvalue('./finetype'));
410         my $bulk = sql_num($node->findvalue('./bulk'));
411         my $enrolmentfee = sql_num($node->findvalue('./enrolmentfee'));
412         my $overduenoticerequired = sql_num($node->findvalue('./overduenoticerequired'));
413         my $issuelimit = sql_num($node->findvalue('./issuelimit'));
414         my $reservefee = sql_num($node->findvalue('./reservefee'));
415         my $hidelostitems = sql_num($node->findvalue('./hidelostitems'));
416         my $category_type = sql_str($node->findvalue('./category_type'));
417         my $BlockExpiredPatronOpacActions = sql_num($node->findvalue('./BlockExpiredPatronOpacActions'));
418         my $default_privacy = sql_str($node->findvalue('./default_privacy'));
419         my $checkprevcheckout = sql_str($node->findvalue('./checkprevcheckout'));
420         my $reset_password = sql_num($node->findvalue('./reset_password'));
421         my $change_password = sql_num($node->findvalue('./change_password'));
422         $query = "SELECT categorycode FROM categories WHERE categorycode = $categorycode";
423         $sth = $dbh->prepare($query);
424         $sth->execute();
425         my $answer;
426         while (my @row = $sth->fetchrow_array) { $answer = sql_str($row[0]); }
427         if (!defined $answer or $answer ne $categorycode) {
428             $query = "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,upperagelimit,dateofbirthrequired,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit,reservefee,hidelostitems,category_type,BlockExpiredPatronOpacActions,default_privacy,checkprevcheckout,reset_password,change_password) VALUES ($categorycode,$description,$enrolmentperiod,$enrolmentperioddate,$upperagelimit,$dateofbirthrequired,$finetype,$bulk,$enrolmentfee,$overduenoticerequired,$issuelimit,$reservefee,$hidelostitems,$category_type,$BlockExpiredPatronOpacActions,$default_privacy,$checkprevcheckout,$reset_password,$change_password)";
429             $sth = $dbh->prepare($query);
430             $sth->execute();
431         } else {
432             $query = "UPDATE categories SET description = $description,enrolmentperiod = $enrolmentperiod,enrolmentperioddate = $enrolmentperioddate,upperagelimit = $upperagelimit,dateofbirthrequired = $dateofbirthrequired,finetype = $finetype,bulk = $bulk,enrolmentfee = $enrolmentfee,overduenoticerequired = $overduenoticerequired,issuelimit = $issuelimit,reservefee = $reservefee,hidelostitems = $hidelostitems,category_type = $category_type,BlockExpiredPatronOpacActions = $BlockExpiredPatronOpacActions,default_privacy = $default_privacy,checkprevcheckout = $checkprevcheckout,reset_password = $reset_password,change_password = $change_password WHERE categorycode = $categorycode";
433             $sth = $dbh->prepare($query);
434             $sth->execute();
435         }
436     }
437    return;
438 }
439
440 sub restore_preferences {
441     my $dbh = shift;
442     my $restore_file = shift;
443     my $parser = XML::LibXML->new();
444     my $dom = $parser->parse_file($restore_file);
445
446     foreach my $node ($dom->findnodes('//pref')) {
447         my $variable = domain_shift(sql_str($node->findvalue('./variable')));
448         my $value = domain_shift(sql_str($node->findvalue('./value')));
449         my $query = "UPDATE systempreferences SET value = $value WHERE variable = $variable";
450         my $sth = $dbh->prepare($query);
451         $sth->execute();
452     }
453     return;
454 }
455
456 sub restore_reports {
457     my $dbh = shift;
458     my $restore_file = shift;
459     my $parser = XML::LibXML->new();
460     my $dom = $parser->parse_file($restore_file);
461
462     my $query = "DELETE FROM saved_sql WHERE 1 = 1";
463     my $sth = $dbh->prepare($query);
464     $sth->execute();
465
466     foreach my $node ($dom->findnodes('//sqlreport')) {
467         my $date_created = sql_str($node->findvalue('./date_created'));
468         my $last_modified = sql_str($node->findvalue('./last_modified'));
469         my $savedsql = domain_shift(sql_str($node->findvalue('./savedsql')));
470         my $report_name = sql_str($node->findvalue('./report_name'));
471         my $type = sql_str($node->findvalue('./type'));
472         my $notes = sql_str($node->findvalue('./notes'));
473         my $cache_expiry = sql_num($node->findvalue('./cache_expiry'));
474         my $public = sql_str($node->findvalue('./public'));
475         my $report_area = sql_str($node->findvalue('./report_area'));
476         my $report_group = sql_str($node->findvalue('./report_group'));
477         my $report_subgroup = sql_str($node->findvalue('./report_subgroup'));
478         my $userid = sql_str($node->findvalue('./userid'));
479
480         $query = "SELECT borrowernumber FROM borrowers WHERE userid = $userid";
481         $sth = $dbh->prepare($query);
482         $sth->execute();
483         my @row = $sth->fetchrow_array;
484         my $borrowernumber = sql_num($row[0]);
485
486         $query = "INSERT INTO saved_sql (date_created,last_modified,savedsql,report_name,type,notes,cache_expiry,public,report_area,report_group,report_subgroup,borrowernumber) VALUES ($date_created,$last_modified,$savedsql,$report_name,$type,$notes,$cache_expiry,$public,$report_area,$report_group,$report_subgroup,$borrowernumber)";
487         $sth = $dbh->prepare($query);
488         $sth->execute();
489     }
490     return;
491 }
492
493 sub restore_smsproviders {
494     my $dbh = shift;
495     my $restore_file = shift;
496     my $parser = XML::LibXML->new();
497     my $dom = $parser->parse_file($restore_file);
498
499     my $query = "DELETE FROM sms_providers WHERE 1 = 1";
500     my $sth = $dbh->prepare($query);
501     $sth->execute();
502
503     foreach my $node ($dom->findnodes('//provider')) {
504         my $name = sql_str($node->findvalue('./name'));
505         my $domain = sql_str($node->findvalue('./domain'));
506         $query = "INSERT INTO sms_providers (name,domain) VALUES ($name,$domain)";
507         $sth = $dbh->prepare($query);
508         $sth->execute();
509     }
510     return;
511 }
512
513 sub domain_shift {
514         my $str = shift;
515         $str =~ s/-test.kohacatalog.com/.kohacatalog.com/g;
516         $str =~ s/-test-staff.kohacatalog.com/-staff.kohacatalog.com/g;
517         return $str;
518 }
519
520 sub sql_str {
521     my $str = shift;
522     if (!defined $str or $str eq '') { return 'NULL'; }
523     $str =~ s/'/''/g;
524     $str = '\'' . $str . '\'';
525     return $str;
526 }
527
528 sub sql_num {
529     my $str = shift;
530     if (!defined $str or $str eq '') { return 'NULL'; } else { return $str; }
531 }
532
533 sub abort {
534     my $msg = shift;
535     print STDERR "$0: $msg", "\n";
536     print_usage();
537     exit 1;
538 }
539
540 sub print_usage {
541     print <<_USAGE_;
542
543     mig import foo_a foo_b foo_c
544
545 _USAGE_
546 }
547