2 # ---------------------------------------------------------------
3 # Copyright © 2013 Merrimack Valley Library Consortium
4 # Jason Stephenson <jstephenson@mvlc.org>
6 # This program is part of Evergreen; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 # ---------------------------------------------------------------
18 use OpenILS::Utils::Fieldmapper;
19 use OpenILS::Application::AppUtils;
20 use OpenSRF::Utils::JSON;
23 use MARC::File::XML (BinaryEncoding => 'UTF-8');
24 use Date::Manip::Date;
26 my $U = 'OpenILS::Application::AppUtils';
28 binmode(STDERR, ':utf8');
32 our $config = Marque::Config->new();
33 Fieldmapper->import(IDL => $config->option_value('xml-idl'));
35 # Look for passed in ids:
37 if ($config->need_ids()) {
38 print STDERR "Waiting for input\n" if (-t);
40 push @ids, $i if ($i =~ /^\s*[0-9]+\s*$/);
45 if ($config->option_value('type') eq 'authority') {
46 $exporter = Marque::Authority->new(\@ids);
48 $exporter = Marque::Biblio->new(\@ids);
51 Marque::Output::output($exporter);
53 # ------------------------------------------------------------------
54 package Marque::Config;
57 use List::MoreUtils qw(none);
59 use OpenSRF::Utils::SettingsClient;
61 use constant FORMATS => qw(USMARC UNIMARC XML BRE ARE);
62 use constant STORES => qw(reporter cstore storage);
63 use constant TYPES => qw(authority biblio);
71 # For command line options.
74 # set some default values
75 $opts{'format'} = 'USMARC';
76 $opts{'encoding'} = 'MARC8';
77 $opts{'type'} = 'biblio';
80 $opts{'config'} = '@sysconfdir@/opensrf_core.xml';
81 $opts{'store'} = 'reporter';
105 This script exports MARC authority, bibliographic, and serial holdings
106 records from an Evergreen database.
108 Input to this script can consist of a list of record IDs, with one record ID
109 per line, corresponding to the record ID in the Evergreen database table of
110 your requested record type.
112 Alternately, passing the --all option will attempt to export all records of
113 the specified type from the Evergreen database. The --all option starts at
114 record ID 1 and increments the ID by 1 until the largest ID in the database
115 is retrieved. This may not be very efficient for databases with large gaps
116 in their ID sequences.
119 --help or -h This screen.
120 --config or -c Configuration file [@sysconfdir@/opensrf_core.xml]
121 --format or -f Output format (USMARC, UNIMARC, XML, BRE, ARE) [USMARC]
122 --encoding or -e Output encoding (UTF-8, ISO-8859-?, MARC8) [MARC8]
123 --xml-idl or -x Location of the IDL XML
124 --timeout Remains for backward compatibility. No longer used.
125 --type or -t Record type (BIBLIO, AUTHORITY) [BIBLIO]
126 --all or -a Export all records; ignores input list
127 --replace_001 Replace the 001 field value with the record ID
128 --store Use the given storage backend to connect to the database.
129 Choices are (reporter, cstore, storage) [reporter]
130 --since Export records modified since a certain date and time.
132 Additional options for type = 'BIBLIO':
133 --items or -i Include items (holdings) in the output
134 --money Currency symbol to use in item price field [\$]
135 --mfhd Export serial MFHD records for associated bib records
136 Not compatible with --format=BRE
137 --location or -l MARC Location Code for holdings from
138 http://www.loc.gov/marc/organizations/orgshome.html
139 --library Export the bibliographic records that have attached
140 holdings for the listed library or libraries as
141 identified by shortname
142 --descendants Like the --library option, but org. tree aware. It
143 exports records that have attached holdings for the
144 specified org. unit and all of its descendants in
149 To export a set of USMARC records in a file named "output_file" based on the
150 IDs contained in a file named "list_of_ids":
151 cat list_of_ids | $0 > output_file
153 To export a set of MARC21XML authority records in a file named "output.xml"
154 for all authority records in the database:
155 $0 --format XML --type AUTHORITY --all > output.xml
157 To export a set of USMARC bibliographic records encoded in UTF-8 in a file
158 named "sys1_bibs.mrc" based on records which have attached callnumbers for the
159 libraries with the short names "BR1" and "BR2":
161 $0 --library BR1 --library BR2 --encoding UTF-8 > sys1_bibs.mrc
167 OpenSRF::System->bootstrap_client( config_file => $opts{config} );
168 my $sclient = OpenSRF::Utils::SettingsClient->new();
169 unless ($opts{'xml-idl'}) {
170 $opts{'xml-idl'} = $sclient->config_value('IDL');
173 # Validate some of the settings.
174 if ($opts{all} && ($opts{library} || $opts{descendants})) {
175 die('Incompatible arguments: you cannot combine a request for all ' .
176 'records with a request for records by library');
178 if ($opts{all} && $opts{since}) {
179 die('Incompatible arguments: you cannot combine a request for all ' .
180 'records with a request for records added or changed since a certain date');
182 $opts{type} = lc($opts{type});
183 if (none {$_ eq $opts{type}} (TYPES)) {
184 die "Please select a supported type. ".
185 "Right now that means one of [".
186 join('|',(FORMATS)). "]\n";
188 $opts{format} = uc($opts{format});
189 if (none {$_ eq $opts{format}} (FORMATS)) {
190 die "Please select a supported format. ".
191 "Right now that means one of [".
192 join('|',(FORMATS)). "]\n";
195 if ($opts{format} eq 'ARE' && $opts{type} ne 'authority') {
196 die "Format ARE is not compatible with type " . $opts{type};
198 if ($opts{format} eq 'BRE' && $opts{type} ne 'biblio') {
199 die "Format BRE is not compatible with type " . $opts{type};
201 if ($opts{format} eq 'BRE' && $opts{items}) {
202 die "Format BRE is not compatible with exporting holdings."
206 if ($opts{type} ne 'biblio') {
207 die "MFHD export only works with bibliographic records.";
208 } elsif ($opts{format} eq 'BRE') {
209 die "MFHD export incompatible with format BRE.";
213 $opts{store} = lc($opts{store});
214 if (none {$_ eq $opts{store}} (STORES)) {
215 die "Please select a supported store. ".
216 "Right now that means one of [".
217 join('|',(STORES)). "]\n";
220 if ($opts{store} eq 'reporter') {
221 $app = 'open-ils.reporter-store';
223 $app = 'open-ils.' . $opts{store};
225 if ($app eq 'open-ils.storage') {
226 $self->{dbsettings} = $sclient->config_value(
227 apps => $app => app_settings => databases => 'database');
229 $self->{dbsettings} = $sclient->config_value(
230 apps => $app => app_settings => 'database');
233 $opts{encoding} = uc($opts{encoding});
235 $self->{'options'} = \%opts;
241 my ($self, $option) = @_;
242 return $self->{options}->{$option};
245 sub database_settings {
247 return $self->{dbsettings};
254 $rv = 0 if ($self->{options}->{all});
255 $rv = 0 if ($self->{options}->{since});
256 $rv = 0 if ($self->{options}->{library});
257 $rv = 0 if ($self->{options}->{descendants});
262 # ------------------------------------------------------------------
263 # This package exists to get a connection to the database. Since
264 # we'll need one for both biblio records and authorities, we've made a
265 # single subpackage with a function so that we don't have to duplicate
267 package Marque::Connector;
271 # Pass a Marque::Config object's database_settings return value into
272 # this to get a DBI connection.
274 # my $db = Marque::Connector::connect($config->database_settings);
278 # Build a connect string from the args.
279 my $connect_string = 'DBI:Pg:';
280 $connect_string .= 'dbname=' . $args->{db} . ';';
281 $connect_string .= 'host=' . $args->{host} . ';';
282 $connect_string .= 'port=' . $args->{port};
284 my $db_handle = DBI->connect($connect_string,
285 $args->{user}, $args->{pw});
289 # A function to get the date into a format better for PostgreSQL.
293 if (ref($input) eq 'Date::Manip::Date') {
296 $date = Date::Manip::Date->new();
297 if ($date->parse($input)) {
298 die "Can't parse date $input";
301 return $date->printf("%Y-%m-%dT%H:%M:%S%z");
304 # ------------------------------------------------------------------
305 # You would typically have the next two packages inherit from a common
306 # superclass, but ineritance doesn't seem to work when all packages
307 # are in single file, so we have some duplicated code between these
310 # Get bibliographic records from the database.
311 package Marque::Biblio;
316 my $self = {idlist => $idlist};
317 $self->{handle} = Marque::Connector::connect(
318 $Marque::config->database_settings);
319 $self->{since_date} = Date::Manip::Date->new;
320 $self->{since_date}->parse($Marque::config->option_value('since'));
322 # We need multiple fieldmapper classes depending on our
323 # options. We'll just get the information that we'll need for them
324 # all right here instead of only fetching the information when
326 $self->{breClass} = Fieldmapper::class_for_hint('bre');
327 $self->{acnClass} = Fieldmapper::class_for_hint('acn');
328 $self->{acpClass} = Fieldmapper::class_for_hint('acp');
329 $self->{sreClass} = Fieldmapper::class_for_hint('sre');
331 # Make an arrayref of shortname ids if the library option was
334 if ($Marque::config->option_value('library')) {
335 # This is done not only for speed, but to prevent SQL injection.
336 my $sth = $self->{handle}->prepare('select id from actor.org_unit where shortname=any(?::text[])');
337 if ($sth->execute($Marque::config->option_value('library'))) {
338 my $r = $sth->fetchall_arrayref();
339 my @ids = map {$_->[0]} @{$r};
340 $self->{libs} = \@ids;
344 # Ditto for descendants. We don't worry about redundancy, the db can deal with it.
345 if ($Marque::config->option_value('descendants')) {
346 # Unlike the above, we're looping to make this simpler in the database.
347 my $sth = $self->{handle}->prepare(
348 'select id from actor.org_unit_descendants((select id from actor.org_unit where shortname=?))');
349 foreach my $shortname (@{$Marque::config->option_value('descendants')}) {
350 if ($sth->execute($shortname)) {
351 my $r = $sth->fetchall_arrayref();
352 my @ids = map {$_->[0]} @{$r};
353 push(@{$self->{libs}}, @ids);
366 # Get the field names and tables for our classes. We add the fully
367 # qualified table names to the fields so that the joins will work.
368 my $breTable = $self->{breClass}->Table();
369 my @breFields = map {$breTable . '.' . $_} $self->{breClass}->real_fields();
370 my $acnTable = $self->{acnClass}->Table();
371 my $acpTable = $self->{acpClass}->Table();
373 # Now we build the query in pieces:
375 # We always select the bre fields:
376 my $select = 'select distinct ' . join(',', @breFields);
377 # We always use the bre table.
378 my $from = "from $breTable";
380 # If have the libraries or items options, we need to join the
381 # asset.call_number table. If we have both, this variable checks
382 # that it has already been joined so we don't create an invalid
385 # Join to the acn table as needed for the library option.
386 if (@{$self->{libs}}) {
390 join $acnTable on $acnTable.record = $breTable.id
391 and $acnTable.owning_lib in (
393 $from .= join(',', @{$self->{libs}}) . ")";
394 $from .= "\nand $acnTable.deleted = 'f'" unless ($Marque::config->option_value('since'));
397 if ($Marque::config->option_value('items')) {
398 unless ($acn_joined) {
399 $from .= "\njoin $acnTable on $acnTable.record = $breTable.id";
400 $from .= "\nand $acnTable.deleted = 'f'" unless ($Marque::config->option_value('since'));
402 $from .= "\njoin $acpTable on $acpTable.call_number = $acnTable.id";
403 $from .= "\nand $acpTable.deleted = 'f'" unless ($Marque::config->option_value('since'));
406 # The where really depends on a few options:
407 my $where = "where $breTable.id > 0 and ";
408 # We fill in the where as necessary.
409 if ($self->{idlist} && @{$self->{idlist}}) {
410 $where .= "$breTable.id in (" . join(',', @{$self->{idlist}}) . ')';
411 } elsif ($Marque::config->option_value('since')) {
412 my $since_str = Marque::Connector::db_date($self->{since_date});
413 $where .= "($breTable.edit_date > '$since_str'";
414 $where .= " or $breTable.create_date > '$since_str')";
416 # We want all non-deleted records.
417 $where .= "$breTable.deleted = 'f'";
420 $self->{query} = $select . "\n" . $from . "\n" . $where;
425 $self->build_query() unless ($self->{query});
426 $self->{sth} = $self->{handle}->prepare($self->{query});
427 return $self->{sth}->execute;
434 # $r holds the record object, either sre or bre. $marc holds the
435 # current record's MARC, either sre.marc or bre.marc
437 # If we have the mfhd option and we've previously retrieved some
438 # sres, then we output one of the retrieved sres for each call
439 # until we run out. These sres "go with" the previous bib record.
440 if ($Marque::config->option_value('mfhd') && $self->{mfhds} && @{$self->{mfhds}}) {
441 $r = shift(@{$self->{mfhds}});
443 local $SIG{__WARN__} = sub {
444 my $message = "Warning from serial record " . $r->id() . ": "
448 $marc = MARC::Record->new_from_xml($r->marc(),
449 $Marque::config->option_value('encoding'),
450 $Marque::config->option_value('format'));
453 warn("Error in serial record " . $r->id() . ": $@");
454 import MARC::File::XML; # Reset SAX Parser.
455 return $self->next();
458 my $data = $self->{sth}->fetchrow_hashref;
460 $r = $self->{breClass}->from_bare_hash($data);
461 if ($Marque::config->option_value('format') eq 'BRE') {
462 $output = OpenSRF::Utils::JSON->perl2JSON($r);
465 local $SIG{__WARN__} = sub {
466 my $message = "Warning from bibliographic record " . $r->id() . ": "
470 $marc = MARC::Record->new_from_xml($r->marc(),
471 $Marque::config->option_value('encoding'),
472 $Marque::config->option_value('format'));
475 warn("Error in bibliographic record " . $r->id() . ": $@");
476 import MARC::File::XML; # Reset SAX Parser.
477 return $self->next();
479 if ($Marque::config->option_value('replace_001')) {
480 my $tcn = $marc->field('001');
482 $tcn->update($r->id());
484 $tcn = MARC::Field->new('001', $r->id());
485 $marc->insert_fields_ordered($tcn);
488 if ($Marque::config->option_value('items')) {
489 my @acps = $self->acps_for_bre($r);
490 foreach my $acp (@acps) {
492 my $location = $Marque::config->option_value('location');
493 my $price = ($acp->price() ? $Marque::config->option_value('money').$acp->price() : '');
495 local $SIG{__WARN__} = sub {
496 my $message = "Warning from bibliographic record " . $r->id() . ": "
500 $marc->insert_grouped_field(
503 ($location ? ('a' => $location) : ()),
504 b => Encode::decode_utf8($acp->call_number()->owning_lib()->shortname()),
505 b => Encode::decode_utf8($acp->circ_lib()->shortname()),
506 c => Encode::decode_utf8($acp->location()->name()),
507 j => Encode::decode_utf8($acp->call_number()->label()),
508 ($acp->circ_modifier() ? (g => Encode::decode_utf8($acp->circ_modifier())) : ()),
509 p => Encode::decode_utf8($acp->barcode()),
510 ($price ? (y => Encode::decode_utf8($price)) : ()),
511 ($acp->copy_number() ? (t => $acp->copy_number()) : ()),
512 ($U->is_true($acp->ref()) ? (x => 'reference') : ()),
513 (!$U->is_true($acp->holdable()) ? (x => 'unholdable') : ()),
514 (!$U->is_true($acp->circulate()) ? (x => 'noncirculating') : ()),
515 (!$U->is_true($acp->opac_visible()) ? (x => 'hidden') : ())
519 warn("Error in bibliographic record " . $r->id() . ": $@");
520 import MARC::File::XML; # Reset SAX Parser.
521 return $self->next();
525 if ($Marque::config->option_value('mfhd')) {
526 $self->{mfhds} = [$self->sres_for_bre($r)];
531 # Common stuff that doesn't depend on record type.
533 if ($Marque::config->option_value('since')) {
534 my $leader = $marc->leader();
535 if ($U->is_true($r->deleted())) {
536 substr($leader, 5, 1) = 'd';
537 $marc->leader($leader);
539 my $create_date = Date::Manip::Date->new;
540 $create_date->parse($r->create_date());
541 my $edit_date = Date::Manip::Date->new;
542 $edit_date->parse($r->edit_date());
543 if ($self->{since_date}->cmp($create_date) < 0) {
544 substr($leader, 5, 1) = 'n';
545 $marc->leader($leader);
546 } elsif ($self->{since_date}->cmp($edit_date) < 0) {
547 substr($leader, 5, 1) = 'c';
548 $marc->leader($leader);
552 if ($Marque::config->option_value('format') eq 'XML') {
554 local $SIG{__WARN__} = sub {
555 my $message = "Warning from bibliographic record " . $r->id() . ": "
559 $output = $marc->as_xml_record;
560 $output =~ s/^<\?.+?\?>$//mo;
563 warn("Error in bibliographic record " . $r->id() . ": $@");
564 return $self->next();
568 local $SIG{__WARN__} = sub {
569 my $message = "Warning from bibliographic record " . $r->id() . ": "
573 $output = $marc->as_usmarc;
576 warn("Error in bibliographic record " . $r->id() . ": $@");
577 return $self->next();
584 # Returns a list of aou objects in an array.
587 unless ($self->{orgs} && @{$self->{orgs}}) {
588 my $fmClass = Fieldmapper::class_for_hint('aou');
589 my @classFields = $fmClass->real_fields();
590 my $classTable = $fmClass->Table();
591 my $query = 'select ' . join(',', @classFields);
592 $query .= "\nfrom $classTable";
593 my $sth = $self->{handle}->prepare($query);
594 if ($sth->execute()) {
595 my $result = $sth->fetchall_arrayref({});
596 my @orgs = map {$fmClass->from_bare_hash($_)} @{$result};
597 $self->{orgs} = \@orgs;
602 return @{$self->{orgs}};
605 # Returns an array of acpl objects.
609 unless ($self->{shelves} && @{$self->{shelves}}) {
610 my $fmClass = Fieldmapper::class_for_hint('acpl');
611 my @classFields = $fmClass->real_fields();
612 my $classTable = $fmClass->Table();
613 my $query = 'select ' . join(',', @classFields);
614 $query .= "\nfrom $classTable";
615 my $result = $self->{handle}->selectall_arrayref($query, {Slice=>{}});
616 my @shelves = map {$fmClass->from_bare_hash($_)} @{$result};
617 $self->{shelves} = \@shelves;
620 return @{$self->{shelves}};
623 # Returns an array of acn objects for a given bre object or id.
627 $bre = $bre->id() if (ref($bre));
629 unless ($self->{acnHandle}) {
630 my $query = "select " . join(',', $self->{acnClass}->real_fields());
631 $query .= "\nfrom " . $self->{acnClass}->Table();
632 $query .= "\nwhere record = ?";
633 if (@{$self->{libs}}) {
634 $query .= "\nand owning_lib in (";
635 $query .= join(',', @{$self->{libs}}) . ")";
637 $query .= "\nand deleted = 'f'" unless($Marque::config->option_value('since'));
638 $self->{acnHandle} = $self->{handle}->prepare($query);
641 if ($self->{acnHandle}->execute($bre)) {
642 my $result = $self->{acnHandle}->fetchall_arrayref({});
643 return map {$self->{acnClass}->from_bare_hash($_)} @{$result};
646 # If for some reason, we don't find anything.
650 # Returns an array of acp objects for a given bre object or id.
654 $bre = $bre->id() if (ref($bre));
656 my @orgs = $self->orgs();
657 my @locations = $self->shelves();
659 my @acns = $self->acns_for_bre($bre);
661 my $query = 'select ' . join(',', $self->{acpClass}->real_fields());
662 $query .= "\nfrom " . $self->{acpClass}->Table();
663 $query .= "\nwhere call_number in (";
664 $query .= join(',', map {$_->id()} @acns);
666 $query .= "\nand deleted = 'f'" unless ($Marque::config->option_value('since'));
667 my $result = $self->{handle}->selectall_arrayref($query, {Slice=>{}});
668 if ($result && @{$result}) {
669 my @acps = map {$self->{acpClass}->from_bare_hash($_)} @{$result};
671 my $cn = $_->call_number();
672 my $clib = $_->circ_lib();
673 my $loc = $_->location();
674 my ($org) = grep {$_->id() == $clib} @orgs;
675 my ($acn) = grep {$_->id() == $cn} @acns;
676 my ($location) = grep {$_->id() == $loc} @locations;
677 my $olib = $acn->owning_lib();
678 my ($owner) = grep {$_->id() == $olib} @orgs;
679 $acn->owning_lib($owner);
680 $_->call_number($acn);
682 $_->location($location);
688 # If for some reason, we don't find anything.
692 # Retreive an array for sre objects when the --mfhd option is used.
696 $bre = $bre->id() if (ref($bre));
698 # Build a query to retrieve SREs when the MFHD option is passed.
699 if ($Marque::config->option_value('mfhd')) {
700 # Create a persistent handle as needed.
701 unless ($self->{sreSth}) {
702 my $query = "select " . join(',', $self->{sreClass}->real_fields());
703 $query .= "\nfrom " . $self->{sreClass}->Table();
704 $query .= "\nwhere record = ?";
705 $query .= "\nand deleted = 'f'" unless ($Marque::config->option_value('since'));
706 $self->{sreSth} = $self->{handle}->prepare($query);
708 if ($self->{sreSth}->execute($bre)) {
709 while (my $data = $self->{sreSth}->fetchrow_hashref) {
710 push @sres, $self->{sreClass}->from_bare_hash($data);
712 $self->{sreSth}->finish; # Sometimes DBI complains.
719 # Get authority records from the database.
720 package Marque::Authority;
725 my $self = {idlist => $idlist};
726 $self->{handle} = Marque::Connector::connect(
727 $Marque::config->database_settings);
728 $self->{fmClass} = Fieldmapper::class_for_hint('are');
729 $self->{since_date} = Date::Manip::Date->new;
730 $self->{since_date}->parse($Marque::config->option_value('since'));
738 # Get the information for are object from the Fieldmapper:
739 my @fields = $self->{fmClass}->real_fields();
740 my $table = $self->{fmClass}->Table();
742 # Build the actual query.
743 my $select = "select " . join(',', @fields);
744 my $from = "from $table";
745 my $where = 'where ';
747 # If we have an idlist, we pretty much ignore anything else.
748 if ($self->{idlist} && @{$self->{idlist}}) {
749 $where .= 'id in (' . join(',', @{$self->{idlist}}) . ')';
750 } elsif ($Marque::config->option_value('since')) {
751 my $since_str = Marque::Connector::db_date($self->{since_date});
752 $where .= "edit_date > '$since_str'";
753 $where .= " or create_date > '$since_str'";
755 # We want all non-deleted records.
756 $where .= "deleted = 'f'";
759 $self->{query} = $select . "\n" . $from . "\n" . $where;
764 $self->build_query() unless ($self->{query});
765 $self->{sth} = $self->{handle}->prepare($self->{query});
766 return $self->{sth}->execute;
772 my $data = $self->{sth}->fetchrow_hashref;
775 my $format = $Marque::config->option_value('format');
776 my $r = $self->{fmClass}->from_bare_hash($data);
777 if ($format eq 'ARE') {
778 $output = OpenSRF::Utils::JSON->perl2JSON($r);
782 local $SIG{__WARN__} = sub {
783 my $message = "Warning from authority record " . $r->id() . ": "
787 $marc = MARC::Record->new_from_xml($r->marc(),
788 $Marque::config->option_value('encoding'),
789 $Marque::config->option_value('format'));
792 warn("Error in authority record " . $r->id() . ": $@");
793 import MARC::File::XML; # Reset SAX Parser.
794 return $self->next();
796 if ($Marque::config->option_value('replace_001')) {
797 my $tcn = $marc->field('001');
799 $tcn->update($r->id());
801 $tcn = MARC::Field->new('001', $r->id());
802 $marc->insert_fields_ordered($tcn);
805 if ($Marque::config->option_value('since')) {
806 my $leader = $marc->leader();
807 if ($U->is_true($r->deleted())) {
808 substr($leader, 5, 1) = 'd';
809 $marc->leader($leader);
811 my $create_date = Date::Manip::Date->new;
812 $create_date->parse($r->create_date());
813 my $edit_date = Date::Manip::Date->new;
814 $edit_date->parse($r->edit_date());
815 if ($self->{since_date}->cmp($create_date) < 0) {
816 substr($leader, 5, 1) = 'n';
817 $marc->leader($leader);
818 } elsif ($self->{since_date}->cmp($edit_date) < 0) {
819 substr($leader, 5, 1) = 'c';
820 $marc->leader($leader);
824 if ($Marque::config->option_value('format') eq 'XML') {
826 local $SIG{__WARN__} = sub {
827 my $message = "Warning from authority record " . $r->id() . ": "
831 $output = $marc->as_xml_record;
832 $output =~ s/^<\?.+?\?>$//mo;
835 warn("Error in authority record " . $r->id() . ": $@");
836 return $self->next();
840 local $SIG{__WARN__} = sub {
841 my $message = "Warning from authority record " . $r->id() . ": "
845 $output = $marc->as_usmarc;
848 warn("Error in authority record " . $r->id() . ": $@");
849 return $self->next();
858 # ------------------------------------------------------------------
859 # Since the ultimate output is largely independent of the type of the
860 # records, we use a single subpackage to group our output routines.
861 package Marque::Output;
864 my $extractor = shift;
865 if ($extractor->execute_query) {
866 if ($Marque::config->option_value('encoding') eq 'UTF-8') {
867 binmode(STDOUT, ':utf8');
869 binmode(STDOUT, ':raw');
873 while (my $output = $extractor->next()) {
878 print STDERR $extractor->{query} if ($Marque::config->option_value('debug'));
879 die "Database query failed!";
884 if ($Marque::config->option_value('format') eq 'XML') {
885 my $encoding = $Marque::config->option_value('encoding');
887 <?xml version="1.0" encoding="$encoding"?>
888 <collection xmlns='http://www.loc.gov/MARC21/slim'>
894 if ($Marque::config->option_value('format') eq 'XML') {
895 print "</collection>\n";