more robust versions of kmig import and export
[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","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 'smsproviders') {
81         my $timestamp = most_recent_single($MIGGITDIR,'smsproviders');
82         if ($timestamp) { $restorefile = $MIGGITDIR . 'smsproviders' . '.' . $timestamp . '.xml'; }
83         print "Restoring from $restorefile ... \n";
84         if ($restorefile) { restore_smsproviders($dbh,$restorefile); }
85     }
86 }
87
88 print "Done.\n";
89
90
91 sub most_recent_single {
92     my $MGIGITDIR = shift;
93     my $str = shift;
94     my @files;
95     my @times;
96     opendir (DIR, $MIGGITDIR) or abort("could not open $MIGGITDIR");;
97     while (my $file = readdir(DIR)) {
98         if ($file =~ m/$str/) { push @files, $file; }
99     }
100     foreach my $file (@files) {
101         my @f = split /\./, $file;
102         push @times, $f[1];
103     }
104     closedir(DIR);
105     my $max = max @times; 
106     return $max;
107 }
108
109 sub restore_authorisedvalues {
110     my $dbh = shift;
111     my $restore_file = shift;
112     my $parser = XML::LibXML->new();
113     my $dom = $parser->parse_file($restore_file);
114
115     my $query = "DELETE FROM authorised_values WHERE category != 'ITEMTYPECAT'";
116     my $sth = $dbh->prepare($query);
117     $sth->execute();
118
119     foreach my $node ($dom->findnodes('//value')) {
120         my $category = sql_str($node->findvalue('./category'));
121         my $authorised_value = sql_str($node->findvalue('./authorised_value'));
122         my $lib = sql_str($node->findvalue('./lib'));
123         my $lib_opac = sql_str($node->findvalue('./lib_opac'));
124         my $imageurl = sql_str($node->findvalue('./imageurl'));
125         $query = "INSERT INTO authorised_values (category,authorised_value,lib,lib_opac,imageurl) VALUES ($category,$authorised_value,$lib,$lib_opac,$imageurl)";
126         $sth = $dbh->prepare($query);
127         $sth->execute();
128     }
129     return;
130 }
131
132 sub restore_calendar {
133     my $dbh = shift;
134     my $restore_file = shift;
135     my $parser = XML::LibXML->new();
136     my $dom = $parser->parse_file($restore_file);
137
138     my $query = "DELETE FROM repeatable_holidays WHERE 1 = 1";
139     my $sth = $dbh->prepare($query);
140     $sth->execute();
141
142     $query = "DELETE FROM special_holidays WHERE 1 = 1";
143     $sth = $dbh->prepare($query);
144     $sth->execute();
145
146     foreach my $node ($dom->findnodes('/holidays/repeatable')) {
147         my $branchcode = sql_str($node->findvalue('./branchcode'));
148         my $weekday = sql_num($node->findvalue('./weekday'));
149         my $day = sql_num($node->findvalue('./day'));
150         my $month = sql_num($node->findvalue('./month'));
151         my $title = sql_str($node->findvalue('./title'));
152         my $description = sql_str($node->findvalue('./description'));
153         $query = "INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description) VALUES ($branchcode,$weekday,$day,$month,$title,$description)";
154         $sth = $dbh->prepare($query);
155         $sth->execute();
156     }
157
158     foreach my $node ($dom->findnodes('/holidays/special')) {
159         my $branchcode = sql_str($node->findvalue('./branchcode'));
160         my $day = sql_num($node->findvalue('./day'));
161         my $month = sql_num($node->findvalue('./month'));
162         my $year = sql_num($node->findvalue('./year'));
163         my $isexception = sql_num($node->findvalue('./isexception'));
164         my $title = sql_str($node->findvalue('./title'));
165         my $description = sql_str($node->findvalue('./description'));
166         $query = "INSERT INTO special_holidays (branchcode,day,month,year,isexception,title,description) VALUES ($branchcode,$day,$month,$year,$isexception,$title,$description)";
167         $sth = $dbh->prepare($query);
168         $sth->execute();
169     }
170
171     return;
172 }
173
174 sub restore_circrules {
175     my $dbh = shift;
176     my $restore_file = shift;
177     my $parser = XML::LibXML->new();
178     my $dom = $parser->parse_file($restore_file);
179  
180     #order of operations - clean out circ_rules, then issuingrules; restore issuingrules then circ_rules 
181
182     my $query = "DELETE FROM circulation_rules WHERE 1 = 1";
183     my $sth = $dbh->prepare($query);
184     $sth->execute();
185
186     $query = "DELETE FROM issuingrules WHERE 1 = 1";
187     $sth = $dbh->prepare($query);
188     $sth->execute();
189
190     foreach my $node ($dom->findnodes('//issuing/rule')) {
191         my $categorycode = sql_str($node->findvalue('./categorycode'));
192         my $itemtype = sql_str($node->findvalue('./itemtype'));
193         my $restrictedtype = sql_num($node->findvalue('./restrictedtype'));
194         my $rentaldiscount = sql_num($node->findvalue('./rentaldiscount'));
195         my $reservecharge = sql_num($node->findvalue('./reservecharge'));
196         my $fine = sql_num($node->findvalue('./fine'));
197         my $finedays = sql_num($node->findvalue('./finedays'));
198         my $maxsuspensiondays = sql_num($node->findvalue('./maxsuspensiondays'));
199         my $suspension_chargeperiod = sql_num($node->findvalue('./suspension_chargeperiod'));
200         my $firstremind = sql_num($node->findvalue('./firstremind'));
201         my $chargeperiod = sql_num($node->findvalue('./chargeperiod'));
202         my $chargeperiod_charge_at = sql_num($node->findvalue('./chargeperiod_charge_at'));
203         my $accountsent = sql_num($node->findvalue('./accountsent'));
204         my $issuelength = sql_num($node->findvalue('./issuelength'));
205         my $lengthunit = sql_str($node->findvalue('./lengthunit'));
206         my $hardduedate = sql_str($node->findvalue('./hardduedate'));
207         my $hardduedatecompare = sql_num($node->findvalue('./hardduedatecompare'));
208         my $renewalsallowed = sql_num($node->findvalue('./renewalsallowed'));
209         my $renewalperiod = sql_num($node->findvalue('./renewalperiod'));
210         my $norenewalbefore = sql_num($node->findvalue('./norenewalbefore'));
211         my $auto_renew = sql_num($node->findvalue('./auto_renew'));
212         my $no_auto_renewal_after = sql_num($node->findvalue('./no_auto_renewal_after'));
213         my $no_auto_renewal_after_hard_limit = sql_str($node->findvalue('./no_auto_renewal_after_hard_limit'));
214         my $reservesallowed = sql_num($node->findvalue('./reservesallowed'));
215         my $holds_per_record = sql_num($node->findvalue('./holds_per_record'));
216         my $holds_per_day = sql_num($node->findvalue('./holds_per_day'));
217         my $branchcode = sql_str($node->findvalue('./branchcode'));
218         my $overduefinescap = sql_num($node->findvalue('./overduefinescap'));
219         my $cap_fine_to_replacement_price = sql_num($node->findvalue('./cap_fine_to_replacement_price'));
220         my $onshelfholds = sql_num($node->findvalue('./onshelfholds'));
221         my $opacitemholds = sql_str($node->findvalue('./opacitemholds'));
222         my $article_requests = sql_str($node->findvalue('./article_requests'));
223         my $note = sql_str($node->findvalue('./note'));
224         $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)";
225         $sth = $dbh->prepare($query);
226         $sth->execute();
227     }
228
229     foreach my $node ($dom->findnodes('//circ/rule')) {
230         my $branchcode = sql_str($node->findvalue('./branchcode'));
231         my $categorycode = sql_str($node->findvalue('./categorycode'));
232         my $itemtype = sql_str($node->findvalue('./itemtype'));
233         my $rule_name = sql_str($node->findvalue('./rule_name'));
234         my $rule_value = sql_str($node->findvalue('./rule_value'));
235         $query = "INSERT INTO circulation_rules (branchcode,categorycode,itemtype,rule_name,rule_value) VALUES ($branchcode,$categorycode,$itemtype,$rule_name,$rule_value)";
236         $sth = $dbh->prepare($query);
237         $sth->execute();
238     }
239
240     return;
241 }
242
243 sub restore_itemtypes {
244     my $dbh = shift;
245     my $restorefile = shift;
246     my $parser = XML::LibXML->new();
247     my $dom = $parser->parse_file($restorefile);
248
249     my $query = "UPDATE itemtypes SET searchcategory = NULL";
250     my $sth = $dbh->prepare($query);
251     $sth->execute();
252
253     $query = "DELETE FROM authorised_values WHERE category = 'ITEMTYPECAT'";
254     $sth = $dbh->prepare($query);
255     $sth->execute();
256
257     $query = "DELETE FROM localization WHERE entity = 'itemtypes'";
258     $sth = $dbh->prepare($query);
259     $sth->execute();
260
261     foreach my $node ($dom->findnodes('/authorised_values/value')) {
262         my $category = sql_str($node->findvalue('./category'));
263         my $authvalue = sql_str($node->findvalue('./authvalue'));
264         my $lib = sql_str($node->findvalue('./lib'));
265         my $lib_opac = sql_str($node->findvalue('./lib_opac'));
266         my $image_url = sql_str($node->findvalue('./image_url'));
267         my $query = "INSERT INTO authorised_values (category,authvalue,lib,lib_opac,image_url) VALUES ($category,$authvalue,$lib,$lib_opac,$image_url)";
268         $sth = $dbh->prepare($query);
269         $sth->execute();
270     }
271
272     foreach my $node ($dom->findnodes('/itemtypes/value')) {
273         my $itemtype = sql_str($node->findvalue('./itemtype'));
274         my $description = sql_str($node->findvalue('./description'));
275         my $rentalcharge = sql_str($node->findvalue('./rentalcharge'));
276         my $rentalcharge_daily = sql_str($node->findvalue('./rentalcharge_daily'));
277         my $rentalcharge_hourly = sql_str($node->findvalue('./rentalcharge_hourly'));
278         my $defaultreplacecost = sql_str($node->findvalue('./defaultreplacecost'));
279         my $processfee = sql_str($node->findvalue('./processfee'));
280         my $notforloan = sql_str($node->findvalue('./notforloan'));
281         my $imageurl = sql_str($node->findvalue('./imageurl'));
282         my $summary = sql_str($node->findvalue('./summary'));
283         my $checkinmsg = sql_str($node->findvalue('./checkinmsg'));
284         my $checkinmsgtype = sql_str($node->findvalue('./checkinmsgtype'));
285         my $sip_media_type = sql_str($node->findvalue('./sip_media_type'));
286         my $hideinopac = sql_str($node->findvalue('./hideinopac'));
287         my $searchcategory = sql_str($node->findvalue('./searchcategory'));
288         $query = "SELECT itemtype FROM itemtypes WHERE itemtype = $itemtype";
289         $sth = $dbh->prepare($query);
290         $sth->execute();
291         my $answer;
292         while (my @row = $sth->fetchrow_array) { $answer = sql_str($row[0]); }
293         if (!defined $answer or $answer ne $itemtype) {
294             $query = "INSERT INTO itemtypes (itemtype,description,rentalcharge,rentalcharge_daily,rentalcharge_hourly,defaultreplacecost,processfee,notforloan,imageurl,summary,checkinmsg,sip_media_type,hideinopac,searchcategory) 
295             VALUES ($itemtype,$description,$rentalcharge,$rentalcharge_daily,$rentalcharge_hourly,$defaultreplacecost,$processfee,$notforloan,$imageurl,$summary,$checkinmsg,$sip_media_type,$hideinopac,$searchcategory)";
296             $sth = $dbh->prepare($query);
297             $sth->execute();
298         } else {
299             $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 
300             WHERE itemtype = $itemtype";
301             $sth = $dbh->prepare($query);
302             $sth->execute();
303         }
304     }
305
306     foreach my $node ($dom->findnodes('//localizations/value')) {
307         my $entity = sql_str($node->findvalue('./entity'));
308         my $code = sql_str($node->findvalue('./code'));
309         my $lang = sql_str($node->findvalue('./lang'));
310         my $translation = sql_str($node->findvalue('./translation'));
311         my $query = "INSERT INTO localization (entity,code,lang,translation) VALUES ($entity,$code,$lang,$translation)";
312         $sth = $dbh->prepare($query);
313         print "$query\n";
314         $sth->execute();
315     }
316
317     return;
318 }
319
320 sub restore_libraries {
321     my $dbh = shift;
322     my $restorefile = shift;
323     my $parser = XML::LibXML->new();
324     my $dom = $parser->parse_file($restorefile);
325
326     my $query = "TRUNCATE library_groups";
327     my $sth = $dbh->prepare($query);
328     $sth->execute();
329
330     foreach my $node ($dom->findnodes('//library')) {
331         my $branchcode = sql_str($node->findvalue('./branchcode'));
332         my $branchname = sql_str($node->findvalue('./branchname'));
333         my $branchaddress1 = sql_str($node->findvalue('./branchaddress1'));
334         my $branchaddress2 = sql_str($node->findvalue('./branchaddress2'));
335         my $branchaddress3 = sql_str($node->findvalue('./branchaddress3'));
336         my $branchzip = sql_str($node->findvalue('./branchzip'));
337         my $branchcity = sql_str($node->findvalue('./branchcity'));
338         my $branchstate = sql_str($node->findvalue('./branchstate'));
339         my $branchcountry = sql_str($node->findvalue('./branchcountry'));
340         my $branchphone = sql_str($node->findvalue('./branchphone'));
341         my $branchfax = sql_str($node->findvalue('./branchfax'));
342         my $branchemail = sql_str($node->findvalue('./branchemail'));
343         my $branchreplyto = sql_str($node->findvalue('./branchreplyto'));
344         my $branchreturnpath = sql_str($node->findvalue('./branchreturnpath'));
345         my $branchurl = sql_str($node->findvalue('./branchurl'));
346         my $issuing = sql_str($node->findvalue('./issuing'));
347         my $branchip = sql_str($node->findvalue('./branchip'));
348         my $branchprinter = sql_str($node->findvalue('./branchprinter'));
349         my $branchnotes = sql_str($node->findvalue('./branchnotes'));
350         my $opac_info = sql_str($node->findvalue('./opac_info'));
351         my $geolocation = sql_str($node->findvalue('./geolocation'));
352         my $marcorgcode = sql_str($node->findvalue('./marcorgcode'));
353         my $pickup_location = $node->findvalue('./pickup_location');
354         $query = "SELECT branchcode FROM branches WHERE branchcode = $branchcode";
355         $sth = $dbh->prepare($query);
356         $sth->execute();
357         my $answer;
358         while (my @row = $sth->fetchrow_array) { $answer = sql_str($row[0]); }
359         if (!defined $answer or $answer ne $branchcode) { 
360             $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)";
361             $sth = $dbh->prepare($query);
362             $sth->execute();
363         } else {
364             $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";
365             $sth = $dbh->prepare($query);
366             $sth->execute();  
367         }
368     }
369
370     #haven't taken into account them needing to go in, in a specific order so I could check parent ids first 
371     foreach my $node ($dom->findnodes('//library_group')) {
372         my $id = $node->findvalue('./id');
373         my $parent_id = sql_num($node->findvalue('./parent_id'));
374         my $branchcode = sql_str($node->findvalue('./branchcode'));
375         my $title = sql_str($node->findvalue('./title'));
376         my $descr = sql_str($node->findvalue('./description'));
377         my $ft1 = sql_num($node->findvalue('./ft_hide_patron_info'));
378         my $ft2 = sql_num($node->findvalue('./ft_search_groups_opac'));
379         my $ft3 = sql_num($node->findvalue('./ft_search_groups_staff'));
380         $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)";
381         $sth = $dbh->prepare($query);
382         $sth->execute();
383     }
384     return;
385 }
386
387 sub restore_patrontypes {
388     my $dbh = shift;
389     my $restorefile = shift;
390     my $parser = XML::LibXML->new();
391     my $dom = $parser->parse_file($restorefile);
392
393     my $query;
394     my $sth;
395
396     foreach my $node ($dom->findnodes('//category')) {
397         my $categorycode = sql_str($node->findvalue('./categorycode'));
398         my $description = sql_str($node->findvalue('./description'));
399         my $enrolmentperiod = sql_num($node->findvalue('./enrolmentperiod'));
400         my $enrolmentperioddate = sql_str($node->findvalue('./enrolmentperioddate'));
401         my $upperagelimit = sql_num($node->findvalue('./upperagelimit'));
402         my $dateofbirthrequired = sql_num($node->findvalue('./dateofbirthrequired'));
403         my $finetype = sql_str($node->findvalue('./finetype'));
404         my $bulk = sql_num($node->findvalue('./bulk'));
405         my $enrolmentfee = sql_num($node->findvalue('./enrolmentfee'));
406         my $overduenoticerequired = sql_num($node->findvalue('./overduenoticerequired'));
407         my $issuelimit = sql_num($node->findvalue('./issuelimit'));
408         my $reservefee = sql_num($node->findvalue('./reservefee'));
409         my $hidelostitems = sql_num($node->findvalue('./hidelostitems'));
410         my $category_type = sql_str($node->findvalue('./category_type'));
411         my $BlockExpiredPatronOpacActions = sql_num($node->findvalue('./BlockExpiredPatronOpacActions'));
412         my $default_privacy = sql_str($node->findvalue('./default_privacy'));
413         my $checkprevcheckout = sql_str($node->findvalue('./checkprevcheckout'));
414         my $reset_password = sql_num($node->findvalue('./reset_password'));
415         my $change_password = sql_num($node->findvalue('./change_password'));
416         $query = "SELECT categorycode FROM categories WHERE categorycode = $categorycode";
417         $sth = $dbh->prepare($query);
418         $sth->execute();
419         my $answer;
420         while (my @row = $sth->fetchrow_array) { $answer = sql_str($row[0]); }
421         if (!defined $answer or $answer ne $categorycode) {
422             $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)";
423             $sth = $dbh->prepare($query);
424             $sth->execute();
425         } else {
426             $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";
427             $sth = $dbh->prepare($query);
428             $sth->execute();
429         }
430     }
431    return;
432 }
433
434 sub restore_preferences {
435     my $dbh = shift;
436     my $restore_file = shift;
437     my $parser = XML::LibXML->new();
438     my $dom = $parser->parse_file($restore_file);
439
440     foreach my $node ($dom->findnodes('//pref')) {
441         my $variable = sql_str($node->findvalue('./variable'));
442         my $value = sql_str($node->findvalue('./value'));
443         my $query = "UPDATE systempreferences SET value = $value WHERE variable = $variable";
444         my $sth = $dbh->prepare($query);
445         $sth->execute();
446     }
447     return;
448 }
449
450 sub restore_smsproviders {
451     my $dbh = shift;
452     my $restore_file = shift;
453     my $parser = XML::LibXML->new();
454     my $dom = $parser->parse_file($restore_file);
455
456     my $query = "DELETE FROM sms_providers WHERE 1 = 1";
457     my $sth = $dbh->prepare($query);
458     $sth->execute();
459
460     foreach my $node ($dom->findnodes('//provider')) {
461         my $name = sql_str($node->findvalue('./name'));
462         my $domain = sql_str($node->findvalue('./domain'));
463         $query = "INSERT INTO sms_providers (name,domain) VALUES ($name,$domain)";
464         $sth = $dbh->prepare($query);
465         $sth->execute();
466     }
467     return;
468 }
469
470 sub sql_str {
471     my $str = shift;
472     if (!defined $str or $str eq '') { return 'NULL'; }
473     $str =~ s/'/''/g;
474     $str = '\'' . $str . '\'';
475     return $str;
476 }
477
478 sub sql_num {
479     my $str = shift;
480     if (!defined $str or $str eq '') { return 'NULL'; } else { return $str; }
481 }
482
483 sub abort {
484     my $msg = shift;
485     print STDERR "$0: $msg", "\n";
486     print_usage();
487     exit 1;
488 }
489
490 sub print_usage {
491     print <<_USAGE_;
492
493     mig import foo_a foo_b foo_c
494
495 _USAGE_
496 }
497