Bug 23204: Adjust copyright and license
[koha.git] / misc / export_borrowers.pl
blob4733c3cbc1683f18aed8f92f16ebe099f7f113a3
1 #!/usr/bin/perl
3 # Copyright 2011 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 # Script to export borrowers
22 use Modern::Perl;
23 use Text::CSV;
24 use Getopt::Long qw(:config no_ignore_case);
26 use Koha::Script;
27 use C4::Context;
28 use Koha::Patrons;
30 binmode STDOUT, ":encoding(UTF-8)";
32 sub print_usage {
33 ( my $basename = $0 ) =~ s|.*/||;
34 print <<USAGE;
36 $basename
37 Export patron informations in CSV format.
38 It prints to standard output. Use redirection to save CSV in a file.
40 Usage:
41 $0 [--field=FIELD [--field=FIELD [...]]] [--separator=CHAR] [--show-header] [--where=CONDITION]
42 $0 -h
44 -f, --field=FIELD Field to export. It is repeatable and has to match
45 column names of the borrower table (also as 'description' and 'category_type'
46 If no field is specified, then all fields will be
47 exported.
48 -s, --separator=CHAR This character will be used to separate fields.
49 Some characters like | or ; will need to be escaped
50 in the parameter setting, like -s=\\| or -s=\\;
51 If no separator is specified, the delimiter pref
52 will be used (or a comma, if the pref is empty)
53 -H, --show-header Print field names on first row
54 -w, --where=CONDITION Condition to filter borrowers to export
55 (SQL where clause).
56 CONDITION must be enclosed by double quotes
57 You can use single quotes around a field value
58 within the condition like:
59 --where "surname='De Lattre'"
60 -h, --help Show this help
62 USAGE
65 # Getting parameters
66 my @fields;
67 my $separator;
68 my $show_header;
69 my $where;
70 my $help;
72 GetOptions(
73 'field|f=s' => \@fields,
74 'separator|s=s' => \$separator,
75 'show-header|H' => \$show_header,
76 'where|w=s' => \$where,
77 'help|h' => \$help
78 ) or print_usage, exit 1;
80 if ($help) {
81 print_usage;
82 exit;
85 # Getting borrowers
86 my $dbh = C4::Context->dbh;
87 my $query = "SELECT borrowernumber FROM borrowers";
88 $query .= " WHERE $where" if ($where);
89 $query .= " ORDER BY borrowernumber";
90 my $sth = $dbh->prepare($query);
91 $sth->execute;
93 unless ( $separator ) {
94 $separator = C4::Context->preference('delimiter') || ',';
95 $separator = "\t" if ($separator eq 'tabulation');
98 my $csv = Text::CSV->new( { sep_char => $separator, binary => 1 } );
100 # If the user did not specify any field to export, we assume they want them all
101 # We retrieve the first borrower informations to get field names
102 my ($borrowernumber) = $sth->fetchrow_array or die "No borrower to export";
103 my $patron = Koha::Patrons->find( $borrowernumber ); # FIXME Now is_expired is no longer available
104 # We will have to use Koha::Patron and allow method calls
105 my $category = $patron->category;
106 my $member = $patron->unblessed;
107 $member->{description} = $category->description;
108 $member->{category_type} = $category->category_type;
110 @fields = keys %$member unless (@fields);
112 if ($show_header) {
113 $csv->combine(@fields);
114 print $csv->string . "\n";
117 $csv->combine(
118 map {
119 ( defined $member->{$_} and !ref $member->{$_} )
120 ? $member->{$_}
121 : ''
122 } @fields
124 die "Invalid character at borrower $borrowernumber: ["
125 . $csv->error_input . "]\n"
126 if ( !defined( $csv->string ) );
127 print $csv->string . "\n";
129 while ( my $borrowernumber = $sth->fetchrow_array ) {
130 my $patron = Koha::Patrons->find( $borrowernumber );
131 my $category = $patron->category;
132 my $member = $patron->unblessed;
133 $member->{description} = $category->description;
134 $member->{category_type} = $category->category_type;
135 $csv->combine(
136 map {
137 ( defined $member->{$_} and !ref $member->{$_} )
138 ? $member->{$_}
139 : ''
140 } @fields
142 die "Invalid character at borrower $borrowernumber: ["
143 . $csv->error_input . "]\n"
144 if ( !defined( $csv->string ) );
145 print $csv->string . "\n";