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