Lines 16-41
Link Here
|
16 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
16 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
17 |
# Suite 330, Boston, MA 02111-1307 USA |
17 |
# Suite 330, Boston, MA 02111-1307 USA |
18 |
|
18 |
|
19 |
|
19 |
use Modern::Perl; |
20 |
use strict; |
20 |
use List::MoreUtils qw(uniq); |
21 |
use warnings; |
21 |
use Getopt::Long; |
|
|
22 |
use CGI; |
22 |
use C4::Auth; |
23 |
use C4::Auth; |
|
|
24 |
use C4::AuthoritiesMarc; # GetAuthority |
25 |
use C4::Biblio; # GetMarcBiblio GetXmlBiblio |
26 |
use C4::Branch; # GetBranches |
27 |
use C4::Csv; |
28 |
use C4::Koha; # GetItemTypes |
23 |
use C4::Output; |
29 |
use C4::Output; |
24 |
use C4::Biblio; # GetMarcBiblio GetXmlBiblio |
|
|
25 |
use C4::AuthoritiesMarc; # GetAuthority |
26 |
use CGI; |
27 |
use C4::Koha; # GetItemTypes |
28 |
use C4::Branch; # GetBranches |
29 |
use C4::Record; |
30 |
use C4::Record; |
30 |
use Getopt::Long; |
|
|
31 |
|
31 |
|
32 |
my $query = new CGI; |
32 |
my $query = new CGI; |
33 |
|
33 |
|
34 |
my $commandline = 0; |
34 |
my $commandline = 0; |
35 |
my $op; |
|
|
36 |
my $filename; |
37 |
my $dbh = C4::Context->dbh; |
38 |
my $marcflavour = C4::Context->preference("marcflavour"); |
39 |
my $clean; |
35 |
my $clean; |
40 |
my $output_format; |
36 |
my $output_format; |
41 |
my $dont_export_items; |
37 |
my $dont_export_items; |
Lines 43-48
my $deleted_barcodes;
Link Here
|
43 |
my $timestamp; |
39 |
my $timestamp; |
44 |
my $record_type; |
40 |
my $record_type; |
45 |
my $help; |
41 |
my $help; |
|
|
42 |
my $op = $query->param("op") || ''; |
43 |
my $filename = $query->param("filename") || 'koha.mrc'; |
44 |
my $dbh = C4::Context->dbh; |
45 |
my $marcflavour = C4::Context->preference("marcflavour"); |
46 |
my $format = $query->param("format") || 'iso2709'; |
46 |
|
47 |
|
47 |
# Checks if the script is called from commandline |
48 |
# Checks if the script is called from commandline |
48 |
if ( scalar @ARGV > 0 ) { |
49 |
if ( scalar @ARGV > 0 ) { |
Lines 50-67
if ( scalar @ARGV > 0 ) {
Link Here
|
50 |
# Getting parameters |
51 |
# Getting parameters |
51 |
$op = 'export'; |
52 |
$op = 'export'; |
52 |
GetOptions( |
53 |
GetOptions( |
53 |
'format=s' => \$output_format, |
54 |
'format=s' => \$output_format, |
54 |
'date=s' => \$timestamp, |
55 |
'date=s' => \$timestamp, |
55 |
'dont_export_items' => \$dont_export_items, |
56 |
'dont_export_items' => \$dont_export_items, |
56 |
'deleted_barcodes' => \$deleted_barcodes, |
57 |
'deleted_barcodes' => \$deleted_barcodes, |
57 |
'clean' => \$clean, |
58 |
'clean' => \$clean, |
58 |
'filename=s' => \$filename, |
59 |
'filename=s' => \$filename, |
59 |
'record-type=s' => \$record_type, |
60 |
'record-type=s' => \$record_type, |
60 |
'help|?' => \$help |
61 |
'help|?' => \$help |
61 |
); |
62 |
); |
62 |
$commandline = 1; |
63 |
$commandline = 1; |
63 |
|
64 |
|
64 |
|
|
|
65 |
if ($help) { |
65 |
if ($help) { |
66 |
print <<_USAGE_; |
66 |
print <<_USAGE_; |
67 |
export.pl [--format=format] [--date=date] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] --filename=outputfile |
67 |
export.pl [--format=format] [--date=date] [--record-type=TYPE] [--dont_export_items] [--deleted_barcodes] [--clean] --filename=outputfile |
Lines 96-409
_USAGE_
Link Here
|
96 |
# Redirect stdout |
96 |
# Redirect stdout |
97 |
open STDOUT, ">$filename" if $filename; |
97 |
open STDOUT, ">$filename" if $filename; |
98 |
|
98 |
|
99 |
} else { |
99 |
} |
|
|
100 |
else { |
100 |
|
101 |
|
101 |
$op = $query->param("op") || ''; |
102 |
$op = $query->param("op") || ''; |
102 |
$filename = $query->param("filename") || 'koha.mrc'; |
103 |
$filename = $query->param("filename") || 'koha.mrc'; |
103 |
$filename =~ s/(\r|\n)//; |
104 |
$filename =~ s/(\r|\n)//; |
104 |
|
105 |
|
105 |
} |
106 |
} |
106 |
|
107 |
|
107 |
my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( |
108 |
my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( |
108 |
{ |
109 |
{ |
109 |
template_name => "tools/export.tmpl", |
110 |
template_name => "tools/export.tmpl", |
110 |
query => $query, |
111 |
query => $query, |
111 |
type => "intranet", |
112 |
type => "intranet", |
112 |
authnotrequired => $commandline, |
113 |
authnotrequired => $commandline, |
113 |
flagsrequired => {tools => 'export_catalog'}, |
114 |
flagsrequired => { tools => 'export_catalog' }, |
114 |
debug => 1, |
115 |
debug => 1, |
115 |
} |
116 |
} |
116 |
); |
117 |
); |
117 |
|
118 |
|
118 |
my $limit_ind_branch = ( |
119 |
my $limit_ind_branch = |
119 |
C4::Context->preference('IndependantBranches') && |
120 |
( C4::Context->preference('IndependantBranches') |
120 |
C4::Context->userenv && |
121 |
&& C4::Context->userenv |
121 |
!(C4::Context->userenv->{flags} & 1) && |
122 |
&& !( C4::Context->userenv->{flags} & 1 ) |
122 |
C4::Context->userenv->{branch} |
123 |
&& C4::Context->userenv->{branch} ) ? 1 : 0; |
123 |
) ? 1 : 0; |
|
|
124 |
|
124 |
|
125 |
my $branch = $query->param("branch") || ''; |
125 |
my $branch = $query->param("branch") || ''; |
126 |
if ( C4::Context->preference("IndependantBranches") && |
126 |
if ( C4::Context->preference("IndependantBranches") |
127 |
C4::Context->userenv && |
127 |
&& C4::Context->userenv |
128 |
!(C4::Context->userenv->{flags} & 1) ) { |
128 |
&& !( C4::Context->userenv->{flags} & 1 ) ) |
|
|
129 |
{ |
129 |
$branch = C4::Context->userenv->{'branch'}; |
130 |
$branch = C4::Context->userenv->{'branch'}; |
130 |
} |
131 |
} |
131 |
|
132 |
|
132 |
my $backupdir = C4::Context->config('backupdir'); |
133 |
my $backupdir = C4::Context->config('backupdir'); |
133 |
warn $backupdir; |
134 |
|
134 |
|
135 |
if ( $op eq "export" ) { |
135 |
if ($op eq "export") { |
136 |
if ( $format eq "iso2709" or $format eq "xml" ) { |
136 |
my $charset = 'utf-8'; |
137 |
my $charset = 'utf-8'; |
137 |
my $mimetype = 'application/octet-stream'; |
138 |
my $mimetype = 'application/octet-stream'; |
138 |
binmode STDOUT, ':encoding(UTF-8)'; |
139 |
binmode STDOUT, ':encoding(UTF-8)'; |
139 |
if ( $filename =~ m/\.gz$/ ) { |
140 |
if ( $filename =~ m/\.gz$/ ) { |
140 |
$mimetype = 'application/x-gzip'; |
141 |
$mimetype = 'application/x-gzip'; |
141 |
$charset = ''; |
142 |
$charset = ''; |
142 |
binmode STDOUT; |
143 |
binmode STDOUT; |
143 |
} elsif ( $filename =~ m/\.bz2$/ ) { |
144 |
} |
144 |
$mimetype = 'application/x-bzip2'; |
145 |
elsif ( $filename =~ m/\.bz2$/ ) { |
145 |
binmode STDOUT; |
146 |
$mimetype = 'application/x-bzip2'; |
146 |
$charset = ''; |
147 |
binmode STDOUT; |
147 |
} |
148 |
$charset = ''; |
148 |
print $query->header( |
149 |
} |
149 |
-type => $mimetype, |
150 |
print $query->header( |
150 |
-charset => $charset, |
151 |
-type => $mimetype, |
151 |
-attachment => $filename |
152 |
-charset => $charset, |
152 |
) unless ($commandline); |
153 |
-attachment => $filename |
153 |
|
154 |
) unless ($commandline); |
154 |
$record_type = $query->param("record_type") unless ($commandline); |
155 |
|
155 |
$output_format = $query->param("output_format") || 'marc' unless ($commandline); |
156 |
$record_type = $query->param("record_type") unless ($commandline); |
156 |
my $dont_export_fields = $query->param("dont_export_fields"); |
157 |
$output_format = $query->param("output_format") || 'marc' |
157 |
my @sql_params; |
158 |
unless ($commandline); |
158 |
my $sql_query; |
159 |
my $dont_export_fields = $query->param("dont_export_fields"); |
159 |
|
160 |
my @biblionumbers = $query->param("biblionumbers"); |
160 |
my $StartingBiblionumber = $query->param("StartingBiblionumber"); |
161 |
my @itemnumbers = $query->param("itemnumbers"); |
161 |
my $EndingBiblionumber = $query->param("EndingBiblionumber"); |
162 |
my @sql_params; |
162 |
my $itemtype = $query->param("itemtype"); |
163 |
my $sql_query; |
163 |
my $start_callnumber = $query->param("start_callnumber"); |
164 |
my @recordids; |
164 |
my $end_callnumber = $query->param("end_callnumber"); |
165 |
|
165 |
$timestamp = ($timestamp) ? C4::Dates->new($timestamp) : '' if ($commandline); |
166 |
my $StartingBiblionumber = $query->param("StartingBiblionumber"); |
166 |
my $start_accession = |
167 |
my $EndingBiblionumber = $query->param("EndingBiblionumber"); |
167 |
( $query->param("start_accession") ) |
168 |
my $itemtype = $query->param("itemtype"); |
168 |
? C4::Dates->new( $query->param("start_accession") ) |
169 |
my $start_callnumber = $query->param("start_callnumber"); |
169 |
: ''; |
170 |
my $end_callnumber = $query->param("end_callnumber"); |
170 |
my $end_accession = |
171 |
$timestamp = ($timestamp) ? C4::Dates->new($timestamp) : '' |
171 |
( $query->param("end_accession") ) |
172 |
if ($commandline); |
172 |
? C4::Dates->new( $query->param("end_accession") ) |
173 |
my $start_accession = |
173 |
: ''; |
174 |
( $query->param("start_accession") ) |
174 |
$dont_export_items = $query->param("dont_export_item") unless ($commandline); |
175 |
? C4::Dates->new( $query->param("start_accession") ) |
175 |
my $strip_nonlocal_items = $query->param("strip_nonlocal_items"); |
176 |
: ''; |
176 |
|
177 |
my $end_accession = |
177 |
my $biblioitemstable = ($commandline and $deleted_barcodes) |
178 |
( $query->param("end_accession") ) |
178 |
? 'deletedbiblioitems' |
179 |
? C4::Dates->new( $query->param("end_accession") ) |
179 |
: 'biblioitems'; |
180 |
: ''; |
180 |
my $itemstable = ($commandline and $deleted_barcodes) |
181 |
$dont_export_items = $query->param("dont_export_item") |
181 |
? 'deleteditems' |
182 |
unless ($commandline); |
182 |
: 'items'; |
183 |
|
183 |
|
184 |
my $strip_nonlocal_items = $query->param("strip_nonlocal_items"); |
184 |
my $starting_authid = $query->param('starting_authid'); |
185 |
|
185 |
my $ending_authid = $query->param('ending_authid'); |
186 |
my $biblioitemstable = |
186 |
my $authtype = $query->param('authtype'); |
187 |
( $commandline and $deleted_barcodes ) |
187 |
|
188 |
? 'deletedbiblioitems' |
188 |
if ( $record_type eq 'bibs' ) { |
189 |
: 'biblioitems'; |
189 |
if ($timestamp) { |
190 |
my $itemstable = |
|
|
191 |
( $commandline and $deleted_barcodes ) |
192 |
? 'deleteditems' |
193 |
: 'items'; |
194 |
|
195 |
my $starting_authid = $query->param('starting_authid'); |
196 |
my $ending_authid = $query->param('ending_authid'); |
197 |
my $authtype = $query->param('authtype'); |
198 |
|
199 |
if ( $record_type eq 'bibs' and not @biblionumbers ) { |
200 |
if ($timestamp) { |
201 |
|
190 |
# Specific query when timestamp is used |
202 |
# Specific query when timestamp is used |
191 |
# Actually it's used only with CLI and so all previous filters |
203 |
# Actually it's used only with CLI and so all previous filters |
192 |
# are not used. |
204 |
# are not used. |
193 |
# If one day timestamp is used via the web interface, this part will |
205 |
# If one day timestamp is used via the web interface, this part will |
194 |
# certainly have to be rewrited |
206 |
# certainly have to be rewrited |
195 |
$sql_query = " ( |
207 |
my ( $query, $params ) = construct_query( |
196 |
SELECT biblionumber |
208 |
{ |
197 |
FROM $biblioitemstable |
209 |
recordtype => $record_type, |
198 |
LEFT JOIN items USING(biblionumber) |
210 |
timestamp => $timestamp, |
199 |
WHERE $biblioitemstable.timestamp >= ? |
211 |
biblioitemstable => $biblioitemstable, |
200 |
OR items.timestamp >= ? |
212 |
} |
201 |
) UNION ( |
213 |
); |
202 |
SELECT biblionumber |
214 |
$sql_query = $query; |
203 |
FROM $biblioitemstable |
215 |
@sql_params = @$params; |
204 |
LEFT JOIN deleteditems USING(biblionumber) |
|
|
205 |
WHERE $biblioitemstable.timestamp >= ? |
206 |
OR deleteditems.timestamp >= ? |
207 |
) "; |
208 |
my $ts = $timestamp->output('iso'); |
209 |
@sql_params = ($ts, $ts, $ts, $ts); |
210 |
} else { |
211 |
my $items_filter = |
212 |
$branch || $start_callnumber || $end_callnumber || |
213 |
$start_accession || $timestamp || $end_accession || |
214 |
($itemtype && C4::Context->preference('item-level_itypes')); |
215 |
$sql_query = $items_filter ? |
216 |
"SELECT DISTINCT $biblioitemstable.biblionumber |
217 |
FROM $biblioitemstable JOIN $itemstable |
218 |
USING (biblionumber) WHERE 1" |
219 |
: |
220 |
"SELECT $biblioitemstable.biblionumber FROM $biblioitemstable WHERE biblionumber >0 "; |
221 |
|
222 |
if ( $StartingBiblionumber ) { |
223 |
$sql_query .= " AND $biblioitemstable.biblionumber >= ? "; |
224 |
push @sql_params, $StartingBiblionumber; |
225 |
} |
226 |
|
227 |
if ( $EndingBiblionumber ) { |
228 |
$sql_query .= " AND $biblioitemstable.biblionumber <= ? "; |
229 |
push @sql_params, $EndingBiblionumber; |
230 |
} |
231 |
|
216 |
|
232 |
if ($branch) { |
|
|
233 |
$sql_query .= " AND homebranch = ? "; |
234 |
push @sql_params, $branch; |
235 |
} |
217 |
} |
236 |
|
218 |
else { |
237 |
if ($start_callnumber) { |
219 |
my ( $query, $params ) = construct_query( |
238 |
$sql_query .= " AND itemcallnumber <= ? "; |
220 |
{ |
239 |
push @sql_params, $start_callnumber; |
221 |
recordtype => $record_type, |
|
|
222 |
biblioitemstable => $biblioitemstable, |
223 |
itemstable => $itemstable, |
224 |
StartingBiblionumber => $StartingBiblionumber, |
225 |
EndingBiblionumber => $EndingBiblionumber, |
226 |
branch => $branch, |
227 |
start_callnumber => $start_callnumber, |
228 |
end_callnumber => $end_callnumber, |
229 |
start_accession => $start_accession, |
230 |
end_accession => $end_accession, |
231 |
itemtype => $itemtype, |
232 |
} |
233 |
); |
234 |
$sql_query = $query; |
235 |
@sql_params = @$params; |
240 |
} |
236 |
} |
|
|
237 |
} |
238 |
elsif ( $record_type eq 'auths' ) { |
239 |
my ( $query, $params ) = construct_query( |
240 |
{ |
241 |
recordtype => $record_type, |
242 |
starting_authid => $starting_authid, |
243 |
ending_authid => $ending_authid, |
244 |
authtype => $authtype, |
245 |
} |
246 |
); |
247 |
$sql_query = $query; |
248 |
@sql_params = @$params; |
241 |
|
249 |
|
242 |
if ($end_callnumber) { |
250 |
} |
243 |
$sql_query .= " AND itemcallnumber >= ? "; |
251 |
elsif ( $record_type eq 'db' ) { |
244 |
push @sql_params, $end_callnumber; |
252 |
my $successful_export; |
|
|
253 |
if ( $flags->{superlibrarian} |
254 |
&& C4::Context->config('backup_db_via_tools') ) |
255 |
{ |
256 |
$successful_export = download_backup( |
257 |
{ |
258 |
directory => "$backupdir", |
259 |
extension => 'sql', |
260 |
filename => "$filename" |
261 |
} |
262 |
); |
245 |
} |
263 |
} |
246 |
if ($start_accession) { |
264 |
unless ($successful_export) { |
247 |
$sql_query .= " AND dateaccessioned >= ? "; |
265 |
my $remotehost = $query->remote_host(); |
248 |
push @sql_params, $start_accession->output('iso'); |
266 |
$remotehost =~ s/(\n|\r)//; |
|
|
267 |
warn |
268 |
"A suspicious attempt was made to download the db at '$filename' by someone at " |
269 |
. $remotehost . "\n"; |
249 |
} |
270 |
} |
250 |
|
271 |
exit; |
251 |
if ($end_accession) { |
272 |
} |
252 |
$sql_query .= " AND dateaccessioned <= ? "; |
273 |
elsif ( $record_type eq 'conf' ) { |
253 |
push @sql_params, $end_accession->output('iso'); |
274 |
my $successful_export; |
|
|
275 |
if ( $flags->{superlibrarian} |
276 |
&& C4::Context->config('backup_conf_via_tools') ) |
277 |
{ |
278 |
$successful_export = download_backup( |
279 |
{ |
280 |
directory => "$backupdir", |
281 |
extension => 'tar', |
282 |
filename => "$filename" |
283 |
} |
284 |
); |
254 |
} |
285 |
} |
255 |
|
286 |
unless ($successful_export) { |
256 |
if ( $itemtype ) { |
287 |
my $remotehost = $query->remote_host(); |
257 |
$sql_query .= (C4::Context->preference('item-level_itypes')) ? " AND items.itype = ? " : " AND biblioitems.itemtype = ?"; |
288 |
$remotehost =~ s/(\n|\r)//; |
258 |
push @sql_params, $itemtype; |
289 |
warn |
|
|
290 |
"A suspicious attempt was made to download the configuration at '$filename' by someone at " |
291 |
. $remotehost . "\n"; |
259 |
} |
292 |
} |
|
|
293 |
exit; |
260 |
} |
294 |
} |
261 |
} |
295 |
elsif (@biblionumbers) { |
262 |
elsif ( $record_type eq 'auths' ) { |
296 |
push @recordids, (@biblionumbers); |
263 |
$sql_query = |
|
|
264 |
"SELECT DISTINCT auth_header.authid FROM auth_header WHERE 1"; |
265 |
|
266 |
if ($starting_authid) { |
267 |
$sql_query .= " AND auth_header.authid >= ? "; |
268 |
push @sql_params, $starting_authid; |
269 |
} |
297 |
} |
|
|
298 |
else { |
270 |
|
299 |
|
271 |
if ($ending_authid) { |
300 |
# Someone is trying to mess us up |
272 |
$sql_query .= " AND auth_header.authid <= ? "; |
301 |
exit; |
273 |
push @sql_params, $ending_authid; |
|
|
274 |
} |
302 |
} |
275 |
|
303 |
|
276 |
if ($authtype) { |
304 |
unless (@biblionumbers) { |
277 |
$sql_query .= " AND auth_header.authtypecode = ? "; |
305 |
my $sth = $dbh->prepare($sql_query); |
278 |
push @sql_params, $authtype; |
306 |
$sth->execute(@sql_params); |
279 |
} |
307 |
push @recordids, map { |
280 |
} |
308 |
map { $$_[0] } $_ |
281 |
elsif ( $record_type eq 'db' ) { |
309 |
} @{ $sth->fetchall_arrayref }; |
282 |
my $successful_export; |
|
|
283 |
if ( $flags->{superlibrarian} && C4::Context->config('backup_db_via_tools') ) { |
284 |
$successful_export = download_backup( { directory => "$backupdir", extension => 'sql', filename => "$filename" } ) |
285 |
} |
286 |
unless ( $successful_export ) { |
287 |
my $remotehost = $query->remote_host(); |
288 |
$remotehost =~ s/(\n|\r)//; |
289 |
warn "A suspicious attempt was made to download the db at '$filename' by someone at " . $remotehost . "\n"; |
290 |
} |
291 |
exit; |
292 |
} |
293 |
elsif ( $record_type eq 'conf' ) { |
294 |
my $successful_export; |
295 |
if ( $flags->{superlibrarian} && C4::Context->config('backup_conf_via_tools') ) { |
296 |
$successful_export = download_backup( { directory => "$backupdir", extension => 'tar', filename => "$filename" } ) |
297 |
} |
310 |
} |
298 |
unless ( $successful_export ) { |
|
|
299 |
my $remotehost = $query->remote_host(); |
300 |
$remotehost =~ s/(\n|\r)//; |
301 |
warn "A suspicious attempt was made to download the configuration at '$filename' by someone at " . $remotehost . "\n"; |
302 |
} |
303 |
exit; |
304 |
} |
305 |
else { |
306 |
# Someone is trying to mess us up |
307 |
exit; |
308 |
} |
309 |
|
311 |
|
310 |
my $sth = $dbh->prepare($sql_query); |
312 |
for my $recordid ( uniq @recordids ) { |
311 |
$sth->execute(@sql_params); |
313 |
if ($deleted_barcodes) { |
312 |
|
314 |
my $q = " |
313 |
while ( my ($recordid) = $sth->fetchrow ) { |
315 |
SELECT DISTINCT barcode |
314 |
if ( $deleted_barcodes ) { |
316 |
FROM deleteditems |
315 |
my $q = " |
317 |
WHERE deleteditems.biblionumber = ? |
316 |
SELECT DISTINCT barcode |
318 |
"; |
317 |
FROM deleteditems |
319 |
my $sth = $dbh->prepare($q); |
318 |
WHERE deleteditems.biblionumber = ? |
320 |
$sth->execute($recordid); |
319 |
"; |
321 |
while ( my $row = $sth->fetchrow_array ) { |
320 |
my $sth = $dbh->prepare($q); |
322 |
print "$row\n"; |
321 |
$sth->execute($recordid); |
|
|
322 |
while (my $row = $sth->fetchrow_array) { |
323 |
print "$row\n"; |
324 |
} |
325 |
} else { |
326 |
my $record; |
327 |
if ( $record_type eq 'bibs' ) { |
328 |
$record = eval { GetMarcBiblio($recordid); }; |
329 |
|
330 |
if ($@) { |
331 |
next; |
332 |
} |
323 |
} |
333 |
next if not defined $record; |
324 |
} |
334 |
C4::Biblio::EmbedItemsInMarcBiblio( $record, $recordid ) |
325 |
else { |
335 |
unless $dont_export_items; |
326 |
my $record; |
336 |
if ( $strip_nonlocal_items || $limit_ind_branch ) { |
327 |
if ( $record_type eq 'bibs' ) { |
337 |
my ( $homebranchfield, $homebranchsubfield ) = |
328 |
$record = eval { GetMarcBiblio($recordid); }; |
338 |
GetMarcFromKohaField( 'items.homebranch', '' ); |
329 |
|
339 |
for my $itemfield ( $record->field($homebranchfield) ) { |
330 |
next if $@; |
340 |
|
331 |
next if not defined $record; |
341 |
# if stripping nonlocal items, use loggedinuser's branch if they didn't select one |
332 |
C4::Biblio::EmbedItemsInMarcBiblio( $record, $recordid, |
342 |
$branch = C4::Context->userenv->{'branch'} unless $branch; |
333 |
\@itemnumbers ) |
343 |
$record->delete_field($itemfield) |
334 |
unless $dont_export_items; |
344 |
if ( |
335 |
if ( $strip_nonlocal_items |
345 |
$itemfield->subfield($homebranchsubfield) ne $branch ); |
336 |
|| $limit_ind_branch |
|
|
337 |
|| $dont_export_items ) |
338 |
{ |
339 |
my ( $homebranchfield, $homebranchsubfield ) = |
340 |
GetMarcFromKohaField( 'items.homebranch', '' ); |
341 |
for my $itemfield ( $record->field($homebranchfield) ) { |
342 |
|
343 |
# if stripping nonlocal items, use loggedinuser's branch if they didn't select one |
344 |
$branch = C4::Context->userenv->{'branch'} |
345 |
unless $branch; |
346 |
$record->delete_field($itemfield) |
347 |
if ( $dont_export_items |
348 |
|| $itemfield->subfield($homebranchsubfield) ne |
349 |
$branch ); |
350 |
} |
346 |
} |
351 |
} |
347 |
} |
352 |
} |
348 |
} |
353 |
elsif ( $record_type eq 'auths' ) { |
349 |
elsif ( $record_type eq 'auths' ) { |
354 |
$record = C4::AuthoritiesMarc::GetAuthority($recordid); |
350 |
$record = C4::AuthoritiesMarc::GetAuthority($recordid); |
355 |
next if not defined $record; |
351 |
next if not defined $record; |
356 |
} |
352 |
} |
|
|
353 |
|
357 |
|
354 |
if ( $dont_export_fields ) { |
358 |
if ($dont_export_fields) { |
355 |
my @fields = split " ", $dont_export_fields; |
359 |
my @fields = split " ", $dont_export_fields; |
356 |
foreach ( @fields ) { |
360 |
foreach (@fields) { |
357 |
/^(\d*)(\w)?$/; |
361 |
/^(\d*)(\w)?$/; |
358 |
my $field = $1; |
362 |
my $field = $1; |
359 |
my $subfield = $2; |
363 |
my $subfield = $2; |
360 |
# skip if this record doesn't have this field |
364 |
|
361 |
next if not defined $record->field($field); |
365 |
# skip if this record doesn't have this field |
362 |
if( $subfield ) { |
366 |
next if not defined $record->field($field); |
363 |
$record->field($field)->delete_subfields($subfield); |
367 |
if ($subfield) { |
|
|
368 |
$record->field($field)->delete_subfields($subfield); |
369 |
} |
370 |
else { |
371 |
$record->delete_field( $record->field($field) ); |
372 |
} |
373 |
} |
374 |
} |
375 |
RemoveAllNsb($record) if ($clean); |
376 |
if ( $output_format eq "xml" ) { |
377 |
if ( $marcflavour eq 'UNIMARC' && $record_type eq 'auths' ) |
378 |
{ |
379 |
print $record->as_xml_record('UNIMARCAUTH'); |
364 |
} |
380 |
} |
365 |
else { |
381 |
else { |
366 |
$record->delete_field($record->field($field)); |
382 |
print $record->as_xml_record($marcflavour); |
367 |
} |
383 |
} |
368 |
} |
384 |
} |
369 |
} |
385 |
else { |
370 |
RemoveAllNsb($record) if ($clean); |
386 |
print $record->as_usmarc(); |
371 |
if ( $output_format eq "xml" ) { |
|
|
372 |
if ($marcflavour eq 'UNIMARC' && $record_type eq 'auths') { |
373 |
print $record->as_xml_record('UNIMARCAUTH'); |
374 |
} else { |
375 |
print $record->as_xml_record($marcflavour); |
376 |
} |
387 |
} |
377 |
} |
388 |
} |
378 |
else { |
|
|
379 |
print $record->as_usmarc(); |
380 |
} |
381 |
} |
389 |
} |
|
|
390 |
exit; |
391 |
} |
392 |
elsif ( $format eq "csv" ) { |
393 |
my @biblionumbers = uniq $query->param("biblionumbers"); |
394 |
my @itemnumbers = $query->param("itemnumbers"); |
395 |
my $output = |
396 |
marc2csv( \@biblionumbers, |
397 |
GetCsvProfileId( C4::Context->preference('CsvProfileForExport') ), |
398 |
\@itemnumbers, ); |
399 |
print $query->header( |
400 |
-type => 'application/octet-stream', |
401 |
-'Content-Transfer-Encoding' => 'binary', |
402 |
-attachment => "export.csv" |
403 |
); |
404 |
print $output; |
405 |
exit; |
382 |
} |
406 |
} |
383 |
exit; |
|
|
384 |
|
385 |
} # if export |
407 |
} # if export |
386 |
|
408 |
|
387 |
else { |
409 |
else { |
388 |
|
410 |
|
389 |
my $itemtypes = GetItemTypes; |
411 |
my $itemtypes = GetItemTypes; |
390 |
my @itemtypesloop; |
412 |
my @itemtypesloop; |
391 |
foreach my $thisitemtype (sort keys %$itemtypes) { |
413 |
foreach my $thisitemtype ( sort keys %$itemtypes ) { |
392 |
my %row = |
414 |
my %row = ( |
393 |
( |
415 |
value => $thisitemtype, |
394 |
value => $thisitemtype, |
416 |
description => $itemtypes->{$thisitemtype}->{'description'}, |
395 |
description => $itemtypes->{$thisitemtype}->{'description'}, |
417 |
); |
396 |
); |
418 |
push @itemtypesloop, \%row; |
397 |
push @itemtypesloop, \%row; |
|
|
398 |
} |
419 |
} |
399 |
my $branches = GetBranches($limit_ind_branch); |
420 |
my $branches = GetBranches($limit_ind_branch); |
400 |
my @branchloop; |
421 |
my @branchloop; |
401 |
for my $thisbranch ( |
422 |
for my $thisbranch ( |
402 |
sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } |
423 |
sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } |
403 |
keys %{$branches} |
424 |
keys %{$branches} |
404 |
) { |
425 |
) |
|
|
426 |
{ |
405 |
push @branchloop, |
427 |
push @branchloop, |
406 |
{ value => $thisbranch, |
428 |
{ |
|
|
429 |
value => $thisbranch, |
407 |
selected => $thisbranch eq $branch, |
430 |
selected => $thisbranch eq $branch, |
408 |
branchname => $branches->{$thisbranch}->{'branchname'}, |
431 |
branchname => $branches->{$thisbranch}->{'branchname'}, |
409 |
}; |
432 |
}; |
Lines 420-433
else {
Link Here
|
420 |
push @authtypesloop, \%row; |
443 |
push @authtypesloop, \%row; |
421 |
} |
444 |
} |
422 |
|
445 |
|
423 |
if ( $flags->{superlibrarian} && C4::Context->config('backup_db_via_tools') && $backupdir && -d $backupdir ) { |
446 |
if ( $flags->{superlibrarian} |
|
|
447 |
&& C4::Context->config('backup_db_via_tools') |
448 |
&& $backupdir |
449 |
&& -d $backupdir ) |
450 |
{ |
424 |
$template->{VARS}->{'allow_db_export'} = 1; |
451 |
$template->{VARS}->{'allow_db_export'} = 1; |
425 |
$template->{VARS}->{'dbfiles'} = getbackupfilelist( { directory => "$backupdir", extension => 'sql' } ); |
452 |
$template->{VARS}->{'dbfiles'} = getbackupfilelist( |
|
|
453 |
{ directory => "$backupdir", extension => 'sql' } ); |
426 |
} |
454 |
} |
427 |
|
455 |
|
428 |
if ( $flags->{superlibrarian} && C4::Context->config('backup_conf_via_tools') && $backupdir && -d $backupdir ) { |
456 |
if ( $flags->{superlibrarian} |
|
|
457 |
&& C4::Context->config('backup_conf_via_tools') |
458 |
&& $backupdir |
459 |
&& -d $backupdir ) |
460 |
{ |
429 |
$template->{VARS}->{'allow_conf_export'} = 1; |
461 |
$template->{VARS}->{'allow_conf_export'} = 1; |
430 |
$template->{VARS}->{'conffiles'} = getbackupfilelist( { directory => "$backupdir", extension => 'tar' } ); |
462 |
$template->{VARS}->{'conffiles'} = getbackupfilelist( |
|
|
463 |
{ directory => "$backupdir", extension => 'tar' } ); |
431 |
} |
464 |
} |
432 |
|
465 |
|
433 |
$template->param( |
466 |
$template->param( |
Lines 441-456
else {
Link Here
|
441 |
output_html_with_http_headers $query, $cookie, $template->output; |
474 |
output_html_with_http_headers $query, $cookie, $template->output; |
442 |
} |
475 |
} |
443 |
|
476 |
|
|
|
477 |
sub construct_query { |
478 |
my ($params) = @_; |
479 |
|
480 |
my ( $sql_query, @sql_params ); |
481 |
|
482 |
if ( $params->{recordtype} eq "bibs" ) { |
483 |
if ( $params->{timestamp} ) { |
484 |
my $biblioitemstable = $params->{biblioitemstable}; |
485 |
$sql_query = " ( |
486 |
SELECT biblionumber |
487 |
FROM $biblioitemstable |
488 |
LEFT JOIN items USING(biblionumber) |
489 |
WHERE $biblioitemstable.timestamp >= ? |
490 |
OR items.timestamp >= ? |
491 |
) UNION ( |
492 |
SELECT biblionumber |
493 |
FROM $biblioitemstable |
494 |
LEFT JOIN deleteditems USING(biblionumber) |
495 |
WHERE $biblioitemstable.timestamp >= ? |
496 |
OR deleteditems.timestamp >= ? |
497 |
) "; |
498 |
my $ts = $timestamp->output('iso'); |
499 |
@sql_params = ( $ts, $ts, $ts, $ts ); |
500 |
} |
501 |
else { |
502 |
my $biblioitemstable = $params->{biblioitemstable}; |
503 |
my $itemstable = $params->{itemstable}; |
504 |
my $StartingBiblionumber = $params->{StartingBiblionumber}; |
505 |
my $EndingBiblionumber = $params->{EndingBiblionumber}; |
506 |
my $branch = $params->{branch}; |
507 |
my $start_callnumber = $params->{start_callnumber}; |
508 |
my $end_callnumber = $params->{end_callnumber}; |
509 |
my $start_accession = $params->{star_accession}; |
510 |
my $end_accession = $params->{end_accession}; |
511 |
my $itemtype = $params->{itemtype}; |
512 |
my $items_filter = |
513 |
$branch |
514 |
|| $start_callnumber |
515 |
|| $end_callnumber |
516 |
|| $start_accession |
517 |
|| $end_accession |
518 |
|| ( $itemtype && C4::Context->preference('item-level_itypes') ); |
519 |
$sql_query = $items_filter |
520 |
? "SELECT DISTINCT $biblioitemstable.biblionumber |
521 |
FROM $biblioitemstable JOIN $itemstable |
522 |
USING (biblionumber) WHERE 1" |
523 |
: "SELECT $biblioitemstable.biblionumber FROM $biblioitemstable WHERE biblionumber >0 "; |
524 |
|
525 |
if ($StartingBiblionumber) { |
526 |
$sql_query .= " AND $biblioitemstable.biblionumber >= ? "; |
527 |
push @sql_params, $StartingBiblionumber; |
528 |
} |
529 |
|
530 |
if ($EndingBiblionumber) { |
531 |
$sql_query .= " AND $biblioitemstable.biblionumber <= ? "; |
532 |
push @sql_params, $EndingBiblionumber; |
533 |
} |
534 |
|
535 |
if ($branch) { |
536 |
$sql_query .= " AND homebranch = ? "; |
537 |
push @sql_params, $branch; |
538 |
} |
539 |
|
540 |
if ($start_callnumber) { |
541 |
$sql_query .= " AND itemcallnumber <= ? "; |
542 |
push @sql_params, $start_callnumber; |
543 |
} |
544 |
|
545 |
if ($end_callnumber) { |
546 |
$sql_query .= " AND itemcallnumber >= ? "; |
547 |
push @sql_params, $end_callnumber; |
548 |
} |
549 |
if ($start_accession) { |
550 |
$sql_query .= " AND dateaccessioned >= ? "; |
551 |
push @sql_params, $start_accession->output('iso'); |
552 |
} |
553 |
|
554 |
if ($end_accession) { |
555 |
$sql_query .= " AND dateaccessioned <= ? "; |
556 |
push @sql_params, $end_accession->output('iso'); |
557 |
} |
558 |
|
559 |
if ($itemtype) { |
560 |
$sql_query .= |
561 |
( C4::Context->preference('item-level_itypes') ) |
562 |
? " AND items.itype = ? " |
563 |
: " AND biblioitems.itemtype = ?"; |
564 |
push @sql_params, $itemtype; |
565 |
} |
566 |
} |
567 |
} |
568 |
elsif ( $params->{recordtype} eq "auths" ) { |
569 |
if ( $params->{timestamp} ) { |
570 |
|
571 |
#TODO |
572 |
} |
573 |
else { |
574 |
my $starting_authid = $params->{starting_authid}; |
575 |
my $ending_authid = $params->{ending_authid}; |
576 |
my $authtype = $params->{authtype}; |
577 |
$sql_query = |
578 |
"SELECT DISTINCT auth_header.authid FROM auth_header WHERE 1"; |
579 |
|
580 |
if ($starting_authid) { |
581 |
$sql_query .= " AND auth_header.authid >= ? "; |
582 |
push @sql_params, $starting_authid; |
583 |
} |
584 |
|
585 |
if ($ending_authid) { |
586 |
$sql_query .= " AND auth_header.authid <= ? "; |
587 |
push @sql_params, $ending_authid; |
588 |
} |
589 |
|
590 |
if ($authtype) { |
591 |
$sql_query .= " AND auth_header.authtypecode = ? "; |
592 |
push @sql_params, $authtype; |
593 |
} |
594 |
} |
595 |
} |
596 |
return ( $sql_query, \@sql_params ); |
597 |
} |
598 |
|
444 |
sub getbackupfilelist { |
599 |
sub getbackupfilelist { |
445 |
my $args = shift; |
600 |
my $args = shift; |
446 |
my $directory = $args->{directory}; |
601 |
my $directory = $args->{directory}; |
447 |
my $extension = $args->{extension}; |
602 |
my $extension = $args->{extension}; |
448 |
my @files; |
603 |
my @files; |
449 |
|
604 |
|
450 |
if ( opendir(my $dir, $directory) ) { |
605 |
if ( opendir( my $dir, $directory ) ) { |
451 |
while (my $file = readdir($dir)) { |
606 |
while ( my $file = readdir($dir) ) { |
452 |
next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ ); |
607 |
next unless ( $file =~ m/\.$extension(\.(gz|bz2|xz))?/ ); |
453 |
push @files, $file if ( -f "$backupdir/$file" && -r "$backupdir/$file" ); |
608 |
push @files, $file |
|
|
609 |
if ( -f "$backupdir/$file" && -r "$backupdir/$file" ); |
454 |
} |
610 |
} |
455 |
closedir($dir); |
611 |
closedir($dir); |
456 |
} |
612 |
} |
Lines 458-477
sub getbackupfilelist {
Link Here
|
458 |
} |
614 |
} |
459 |
|
615 |
|
460 |
sub download_backup { |
616 |
sub download_backup { |
461 |
my $args = shift; |
617 |
my $args = shift; |
462 |
my $directory = $args->{directory}; |
618 |
my $directory = $args->{directory}; |
463 |
my $extension = $args->{extension}; |
619 |
my $extension = $args->{extension}; |
464 |
my $filename = $args->{filename}; |
620 |
my $filename = $args->{filename}; |
465 |
|
621 |
|
466 |
return unless ( $directory && -d $directory ); |
622 |
return unless ( $directory && -d $directory ); |
467 |
return unless ( $filename =~ m/$extension(\.(gz|bz2|xz))?$/ && not $filename =~ m#|# ); |
623 |
return |
|
|
624 |
unless ( $filename =~ m/$extension(\.(gz|bz2|xz))?$/ |
625 |
&& not $filename =~ m#|# ); |
468 |
$filename = "$directory/$filename"; |
626 |
$filename = "$directory/$filename"; |
469 |
return unless ( -f $filename && -r $filename ); |
627 |
return unless ( -f $filename && -r $filename ); |
470 |
return unless ( open(my $dump, '<', $filename) ); |
628 |
return unless ( open( my $dump, '<', $filename ) ); |
471 |
binmode $dump; |
629 |
binmode $dump; |
472 |
while (read($dump, my $data, 64 * 1024)) { |
630 |
|
|
|
631 |
while ( read( $dump, my $data, 64 * 1024 ) ) { |
473 |
print $data; |
632 |
print $data; |
474 |
} |
633 |
} |
475 |
close ($dump); |
634 |
close($dump); |
476 |
return 1; |
635 |
return 1; |
477 |
} |
636 |
} |
478 |
- |
|
|