-
Notifications
You must be signed in to change notification settings - Fork 54
/
create_koc_db.pl
executable file
·421 lines (302 loc) · 10.9 KB
/
create_koc_db.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env perl
# 2008 Kyle Hall <kyle.m.hall@gmail.com>
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
#
=head1 NAME
create_koc_db.pl - create a database file for the offline circulation tool
=head1 SYNOPSIS
create_koc_db.pl
Options:
-help brief help message
-man full documentation
=head1 OPTIONS
=over 8
=item B<--help>
Print a brief help message and exits.
=item B<--man>
Prints the manual page and exits.
=item B<--file>
the filename that we should use for the database file that we produce. Defaults to "borrowers.db"
=item B<--force>
Forcefully overwrite any existing db file. Defaults to false, so
program will terminate prematurely if the file already exists.
=back
=head1 DESCRIPTION
This script generates a sqlite database file full of patron and
holdings data that can be used by an offline circulation tool.
=head1 USAGE EXAMPLES
This program could be run from cron to occasionally refresh the
offline circulation database. For instance:
C<0 0 * * * create_koc_db.pl>
=head1 SEE ALSO
This program was originally created to interact with Kyle Hall's
offline circulation tool, which is available from
L<http://kylehall.info/index.php/projects/koha-tools/koha-offline-circulation/>,
but any similar tool could use the database that is produced.
=cut
use strict;
use warnings;
$|++;
use DBI;
use Getopt::Long;
use Pod::Usage;
use Koha;
use C4::Context;
use English qw(-no_match_vars);
my $verbose = 0;
my $help = 0;
my $man = 0;
my $filename = 'borrowers.db';
my $force = 0;
GetOptions(
'verbose' => \$verbose,
'help' => \$help,
'man' => \$man,
'file=s' => \$filename,
'force' => \$force,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -verbose => 2 ) if $man;
my %wanted_borrowers_columns = map { $_ => 1 } qw/borrowernumber cardnumber surname firstname address city phone dateofbirth/;
my %wanted_issues_columns = map { $_ => 1 } qw/borrowernumber date_due itemcallnumber title itemtype/;
prepare_file_for_writing($filename)
or die "file: '$filename' already exists. Use --force to overwrite\n";
verify_dbd_sqlite();
## Create DB Connections
my $dbh_mysql = C4::Context->dbh;
my $dbh_sqlite = DBI->connect( "dbi:SQLite2:dbname=$filename", "", "" );
$dbh_sqlite->{AutoCommit} = 0;
create_borrowers_table();
populate_borrowers_table();
create_issues_table();
populate_issues_table();
=head1 INTERNAL METHODS
=head2 verify_dbd_sqlite
Since DBD::SQLite is a new prerequisite and an optional one, let's
make sure we have a new enough version of it.
=cut
sub verify_dbd_sqlite {
eval { require DBD::SQLite2; };
if ( $EVAL_ERROR ) {
my $msg = <<'END_MESSAGE';
DBD::SQLite2 is required to generate offline circultion database files, but not found.
Please install the DBD::SQLite2 perl module. It is availalbe from
http://search.cpan.org/dist/DBD-SQLite2/ or through the CPAN module.
END_MESSAGE
die $msg;
}
}
=head2 prepare_file_for_writing
pass in the filename that we're considering using for the SQLite db.
returns true if we can use it.
returns false if we can't. For example, if it alredy exists and we
don't have --force or don't have permissions to unlink it.
=cut
sub prepare_file_for_writing {
my $filename = shift;
if ( -e $filename ) {
# this file exists. remove it if --force.
if ($force) {
return unlink $filename;
} else {
return;
}
}
return $filename;
}
=head2 create_borrowers_table
Create sqlite borrowers table to mirror the koha borrowers table structure
=cut
sub create_borrowers_table {
my %borrowers_info = get_columns_and_types_of_table( 'borrowers' );
my $sqlite_create_sql = "CREATE TABLE borrowers ( \n";
$sqlite_create_sql .= join(',', map{ $_ . ' ' . $borrowers_info{$_} }
grep { exists($wanted_borrowers_columns{$_}) } keys %borrowers_info);
$sqlite_create_sql .= " , \n total_fines decimal(28,6) "; ## Extra field to store the total fines for a borrower in.
$sqlite_create_sql .= " ) ";
my $return = $dbh_sqlite->do($sqlite_create_sql);
unless ( $return ) {
warn 'unable to create borrowers table: ' . $dbh_sqlite->errstr();
}
return $return;
}
=head2 populate_borrowers_table
Import the data from the koha.borrowers table into our sqlite table
=cut
sub populate_borrowers_table {
my @borrower_fields = grep { exists($wanted_borrowers_columns{$_}) } get_columns_of_table( 'borrowers' );
push @borrower_fields, 'total_fines';
my $sql = "INSERT INTO borrowers ( ";
$sql .= join( ',', @borrower_fields );
$sql .= " ) VALUES ( ";
$sql .= join( ',', map { '?' } @borrower_fields );
$sql .= " ) ";
my $sth_sqlite = $dbh_sqlite->prepare($sql);
my $sth_mysql = $dbh_mysql->prepare(<<'END_SQL');
SELECT borrowernumber,
cardnumber,
surname,
firstname,
address,
city,
phone,
dateofbirth,
sum( accountlines.amountoutstanding ) as total_fines
FROM borrowers
LEFT JOIN accountlines USING (borrowernumber)
GROUP BY borrowernumber;
END_SQL
my $fields_count = $sth_mysql->execute();
warn "preparing to insert $fields_count borrowers\n" if $verbose;
my $count;
while ( my $borrower = $sth_mysql->fetchrow_hashref ) {
$count++;
if ( $verbose ) {
print '.' unless ( $count % 10 );
print "$count\n" unless ( $count % 1000 );
}
$sth_sqlite->execute( @$borrower{ @borrower_fields } );
$sth_sqlite->finish();
$dbh_sqlite->commit() if ( 0 == $count % 1000 );
}
$dbh_sqlite->commit();
print "inserted $count borrowers\n" if $verbose;
# add_fines_to_borrowers_table();
}
=head2 add_fines_to_borrowers_table
Import the fines from koha.accountlines into the sqlite db
=cut
sub add_fines_to_borrowers_table {
print "preparing to update borrowers\n" if $verbose;
my $sth_mysql = $dbh_mysql->prepare(
"SELECT DISTINCT borrowernumber, SUM( amountoutstanding ) AS total_fines
FROM accountlines
GROUP BY borrowernumber"
);
$sth_mysql->execute();
my $count;
while ( my $result = $sth_mysql->fetchrow_hashref() ) {
$count++;
if ( $verbose ) {
print '.' unless ( $count % 10 );
print "$count\n" unless ( $count % 1000 );
}
my $borrowernumber = $result->{'borrowernumber'};
my $total_fines = $result->{'total_fines'};
# warn "Fines for Borrower # $borrowernumber are \$ $total_fines \n" if $verbose;
my $sql = "UPDATE borrowers SET total_fines = ? WHERE borrowernumber = ?";
my $sth_sqlite = $dbh_sqlite->prepare($sql);
$sth_sqlite->execute( $total_fines, $borrowernumber );
$sth_sqlite->finish();
}
print "updated $count borrowers\n" if ( $verbose && $count );
}
=head2 create_issue_table
Create sqlite issues table with minimal information needed from koha tables issues, items, biblio, biblioitems
=cut
sub create_issues_table {
my $fields = get_columns_for_issues_table();
my $sqlite_create_sql = "CREATE TABLE issues ( \n";
my $firstField = 1;
foreach my $key ( keys %$fields ) {
my $field = $key;
my $type = $fields->{$key};
if ($firstField) {
$sqlite_create_sql .= " $field $type ";
$firstField = 0;
} else {
$sqlite_create_sql .= ", \n $field $type ";
}
}
$sqlite_create_sql .= " ) ";
my $sth_sqlite = $dbh_sqlite->prepare($sqlite_create_sql);
$sth_sqlite->execute();
$sth_sqlite->finish();
}
=head2 populate_issues_table
Import open issues from the koha database
=cut
sub populate_issues_table {
print "preparing to populate ISSUES table\n" if $verbose;
my $sth_mysql = $dbh_mysql->prepare(
"SELECT issues.borrowernumber,
issues.date_due,
items.itemcallnumber,
biblio.title,
biblioitems.itemtype
FROM issues, items, biblioitems, biblio
WHERE issues.itemnumber = items.itemnumber
AND items.biblionumber = biblioitems.biblionumber
AND items.biblionumber = biblio.biblionumber
AND returndate IS NULL"
);
$sth_mysql->execute();
my $column_names = $sth_mysql->{'NAME'};
my $sql_sqlite = "INSERT INTO issues ( ";
$sql_sqlite .= join( ',', @$column_names );
$sql_sqlite .= " ) VALUES ( ";
$sql_sqlite .= join( ',', map { '?' } @$column_names );
$sql_sqlite .= " ) ";
warn "$sql_sqlite\n" if $verbose;
my $sth_sqlite = $dbh_sqlite->prepare($sql_sqlite);
my $count;
while ( my $result = $sth_mysql->fetchrow_hashref ) {
$count++;
if ( $verbose ) {
print '.' unless ( $count % 10 );
print "$count\n" unless ( $count % 1000 );
}
$sth_sqlite->execute( @$result{ @$column_names } );
$sth_sqlite->finish();
$dbh_sqlite->commit() if ( 0 == $count % 1000 );
}
$dbh_sqlite->commit();
print "inserted $count issues\n" if ( $verbose && $count );
}
=head2 get_columns_of_table
pass in the name of a database table.
returns list of column names in that table.
=cut
sub get_columns_of_table {
my $table_name = shift;
my %column_info = get_columns_and_types_of_table( $table_name );
my @columns = keys %column_info;
return @columns;
}
=head2 get_columns_and_types_of_table
pass in the name of a database table
returns a hash of column names to their types.
=cut
sub get_columns_and_types_of_table {
my $table_name = shift;
my $column_info = $dbh_mysql->selectall_arrayref( "SHOW COLUMNS FROM $table_name" );
my %columns = map{ $_->[0] => $_->[1] } @$column_info;
return %columns;
}
=head2 get_columns_for_issues_table
This sub returns a hashref where the keys are all the fields in the given tables, and the data is the field's type
=cut
sub get_columns_for_issues_table {
my @tables = ( 'issues', 'items', 'biblioitems', 'biblio' );
my %fields;
foreach my $table ( @tables ) {
my %table_info = get_columns_and_types_of_table( $table );
%fields = ( %fields, %table_info );
}
return { map { $_ => $fields{$_} } grep { exists($wanted_issues_columns{$_}) } keys %fields };
}
1;
__END__