Bug 14585: Fixing up online help on main page
[koha.git] / misc / export_borrowers.pl
bloba9ecb3dc0a613fac4c726eb5bec088f5dcd0026f
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 C4::Context;
27 use C4::Members;
29 binmode STDOUT, ":encoding(UTF-8)";
31 sub print_usage {
32 ( my $basename = $0 ) =~ s|.*/||;
33 print <<USAGE;
35 $basename
36 Export patron informations in CSV format.
37 It prints to standard output. Use redirection to save CSV in a file.
39 Usage:
40 $0 [--field=FIELD [--field=FIELD [...]]] [--separator=CHAR] [--show-header] [--where=CONDITION]
41 $0 -h
43 -f, --field=FIELD Field to export. It is repeatable and has to match
44 keys returned by the GetMemberDetails function.
45 If no field is specified, then all fields will be
46 exported.
47 -s, --separator=CHAR This character will be used to separate fields.
48 Some characters like | or ; will need to be escaped
49 in the parameter setting, like -s=\\| or -s=\\;
50 If no separator is specifield, a comma will be used.
51 -H, --show-header Print field names on first row
52 -w, --where=CONDITION Condition to filter borrowers to export
53 (SQL where clause).
54 CONDITION must be enclosed by double quotes and
55 if needed, where value by single quotes.
56 example : --where "surname='De Lattre'"
57 -h, --help Show this help
59 USAGE
62 # Getting parameters
63 my @fields;
64 my $separator;
65 my $show_header;
66 my $where;
67 my $help;
69 GetOptions(
70 'field|f=s' => \@fields,
71 'separator|s=s' => \$separator,
72 'show-header|H' => \$show_header,
73 'where|w=s' => \$where,
74 'help|h' => \$help
75 ) or print_usage, exit 1;
77 if ($help) {
78 print_usage;
79 exit;
82 # Getting borrowers
83 my $dbh = C4::Context->dbh;
84 my $query = "SELECT borrowernumber FROM borrowers";
85 $query .= " WHERE $where" if ($where);
86 $query .= " ORDER BY borrowernumber";
87 my $sth = $dbh->prepare($query);
88 $sth->execute;
90 my $csv = Text::CSV->new( { sep_char => $separator, binary => 1 } );
92 # If the user did not specify any field to export, we assume he wants them all
93 # We retrieve the first borrower informations to get field names
94 my ($borrowernumber) = $sth->fetchrow_array or die "No borrower to export";
95 my $member = GetMemberDetails($borrowernumber);
96 @fields = keys %$member unless (@fields);
98 if ($show_header) {
99 $csv->combine(@fields);
100 print $csv->string . "\n";
103 $csv->combine(
104 map {
105 ( defined $member->{$_} and !ref $member->{$_} )
106 ? $member->{$_}
107 : ''
108 } @fields
110 die "Invalid character at borrower $borrowernumber: ["
111 . $csv->error_input . "]\n"
112 if ( !defined( $csv->string ) );
113 print $csv->string . "\n";
115 while ( my $borrowernumber = $sth->fetchrow_array ) {
116 $member = GetMemberDetails($borrowernumber);
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";