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');
330 $self->{acnpClass} = Fieldmapper::class_for_hint('acnp');
331 $self->{acnsClass} = Fieldmapper::class_for_hint('acns');
333 # Make an arrayref of shortname ids if the library option was
336 if ($Marque::config->option_value('library')) {
337 # This is done not only for speed, but to prevent SQL injection.
338 my $sth = $self->{handle}->prepare('select id from actor.org_unit where shortname=any(?::text[])');
339 if ($sth->execute($Marque::config->option_value('library'))) {
340 my $r = $sth->fetchall_arrayref();
341 my @ids = map {$_->[0]} @{$r};
342 $self->{libs} = \@ids;
346 # Ditto for descendants. We don't worry about redundancy, the db can deal with it.
347 if ($Marque::config->option_value('descendants')) {
348 # Unlike the above, we're looping to make this simpler in the database.
349 my $sth = $self->{handle}->prepare(
350 'select id from actor.org_unit_descendants((select id from actor.org_unit where shortname=?))');
351 foreach my $shortname (@{$Marque::config->option_value('descendants')}) {
352 if ($sth->execute($shortname)) {
353 my $r = $sth->fetchall_arrayref();
354 my @ids = map {$_->[0]} @{$r};
355 push(@{$self->{libs}}, @ids);
368 # Get the field names and tables for our classes. We add the fully
369 # qualified table names to the fields so that the joins will work.
370 my $breTable = $self->{breClass}->Table();
371 my @breFields = map {$breTable . '.' . $_} $self->{breClass}->real_fields();
372 my $acnTable = $self->{acnClass}->Table();
373 my $acpTable = $self->{acpClass}->Table();
374 my $acnpTable = $self->{acnpClass}->Table();
375 my $acnsTable = $self->{acnsClass}->Table();
377 # Now we build the query in pieces:
379 # We always select the bre fields:
380 my $select = 'select distinct ' . join(',', @breFields);
381 # We always use the bre table.
382 my $from = "from $breTable";
384 # If have the libraries or items options, we need to join the
385 # asset.call_number table. If we have both, this variable checks
386 # that it has already been joined so we don't create an invalid
389 # Join to the acn table as needed for the library option.
390 if (@{$self->{libs}}) {
394 join $acnTable on $acnTable.record = $breTable.id
395 and $acnTable.owning_lib in (
397 $from .= join(',', @{$self->{libs}}) . ")";
398 $from .= "\nand $acnTable.deleted = 'f'" unless ($Marque::config->option_value('since'));
401 if ($Marque::config->option_value('items')) {
402 unless ($acn_joined) {
403 $from .= "\njoin $acnTable on $acnTable.record = $breTable.id";
404 $from .= "\nand $acnTable.deleted = 'f'" unless ($Marque::config->option_value('since'));
406 $from .= "\njoin $acpTable on $acpTable.call_number = $acnTable.id";
407 $from .= "\nand $acpTable.deleted = 'f'" unless ($Marque::config->option_value('since'));
408 $from .= "\nleft outer join $acnpTable on $acnTable.prefix = $acnpTable.id";
409 $from .= "\nleft outer join $acnsTable on $acnTable.suffix = $acnsTable.id";
412 # The where really depends on a few options:
413 my $where = "where $breTable.id > 0 and ";
414 # We fill in the where as necessary.
415 if ($self->{idlist} && @{$self->{idlist}}) {
416 $where .= "$breTable.id in (" . join(',', @{$self->{idlist}}) . ')';
417 } elsif ($Marque::config->option_value('since')) {
418 my $since_str = Marque::Connector::db_date($self->{since_date});
419 $where .= "($breTable.edit_date > '$since_str'";
420 $where .= " or $breTable.create_date > '$since_str')";
422 # We want all non-deleted records.
423 $where .= "$breTable.deleted = 'f'";
426 $self->{query} = $select . "\n" . $from . "\n" . $where;
431 $self->build_query() unless ($self->{query});
432 $self->{sth} = $self->{handle}->prepare($self->{query});
433 return $self->{sth}->execute;
440 # $r holds the record object, either sre or bre. $marc holds the
441 # current record's MARC, either sre.marc or bre.marc
443 # If we have the mfhd option and we've previously retrieved some
444 # sres, then we output one of the retrieved sres for each call
445 # until we run out. These sres "go with" the previous bib record.
446 if ($Marque::config->option_value('mfhd') && $self->{mfhds} && @{$self->{mfhds}}) {
447 $r = shift(@{$self->{mfhds}});
449 local $SIG{__WARN__} = sub {
450 my $message = "Warning from serial record " . $r->id() . ": "
454 $marc = MARC::Record->new_from_xml($r->marc(),
455 $Marque::config->option_value('encoding'),
456 $Marque::config->option_value('format'));
459 warn("Error in serial record " . $r->id() . ": $@");
460 import MARC::File::XML; # Reset SAX Parser.
461 return $self->next();
464 my $data = $self->{sth}->fetchrow_hashref;
466 $r = $self->{breClass}->from_bare_hash($data);
467 if ($Marque::config->option_value('format') eq 'BRE') {
468 $output = OpenSRF::Utils::JSON->perl2JSON($r);
471 local $SIG{__WARN__} = sub {
472 my $message = "Warning from bibliographic record " . $r->id() . ": "
476 $marc = MARC::Record->new_from_xml($r->marc(),
477 $Marque::config->option_value('encoding'),
478 $Marque::config->option_value('format'));
481 warn("Error in bibliographic record " . $r->id() . ": $@");
482 import MARC::File::XML; # Reset SAX Parser.
483 return $self->next();
485 if ($Marque::config->option_value('replace_001')) {
486 my $tcn = $marc->field('001');
488 $tcn->update($r->id());
490 $tcn = MARC::Field->new('001', $r->id());
491 $marc->insert_fields_ordered($tcn);
494 if ($Marque::config->option_value('items')) {
495 my @acps = $self->acps_for_bre($r);
496 foreach my $acp (@acps) {
498 my $location = $Marque::config->option_value('location');
499 my $price = ($acp->price() ? $Marque::config->option_value('money').$acp->price() : '');
500 my $prefix = $acp->call_number()->prefix()->label();
501 my $suffix = $acp->call_number()->suffix()->label();
503 local $SIG{__WARN__} = sub {
504 my $message = "Warning from bibliographic record " . $r->id() . ": "
508 $marc->insert_grouped_field(
511 ($location ? ('a' => $location) : ()),
512 b => Encode::decode_utf8($acp->call_number()->owning_lib()->shortname()),
513 b => Encode::decode_utf8($acp->circ_lib()->shortname()),
514 c => Encode::decode_utf8($acp->location()->name()),
515 j => Encode::decode_utf8($acp->call_number()->label()),
516 ($prefix ? (k => Encode::decode_utf8($prefix)) : ()),
517 ($suffix ? (m => Encode::decode_utf8($suffix)) : ()),
518 ($acp->circ_modifier() ? (g => Encode::decode_utf8($acp->circ_modifier())) : ()),
519 p => Encode::decode_utf8($acp->barcode()),
520 ($price ? (y => Encode::decode_utf8($price)) : ()),
521 ($acp->copy_number() ? (t => $acp->copy_number()) : ()),
522 ($U->is_true($acp->ref()) ? (x => 'reference') : ()),
523 (!$U->is_true($acp->holdable()) ? (x => 'unholdable') : ()),
524 (!$U->is_true($acp->circulate()) ? (x => 'noncirculating') : ()),
525 (!$U->is_true($acp->opac_visible()) ? (x => 'hidden') : ())
529 warn("Error in bibliographic record " . $r->id() . ": $@");
530 import MARC::File::XML; # Reset SAX Parser.
531 return $self->next();
535 if ($Marque::config->option_value('mfhd')) {
536 $self->{mfhds} = [$self->sres_for_bre($r)];
541 # Common stuff that doesn't depend on record type.
543 if ($Marque::config->option_value('since')) {
544 my $leader = $marc->leader();
545 if ($U->is_true($r->deleted())) {
546 substr($leader, 5, 1) = 'd';
547 $marc->leader($leader);
549 my $create_date = Date::Manip::Date->new;
550 $create_date->parse($r->create_date());
551 my $edit_date = Date::Manip::Date->new;
552 $edit_date->parse($r->edit_date());
553 if ($self->{since_date}->cmp($create_date) < 0) {
554 substr($leader, 5, 1) = 'n';
555 $marc->leader($leader);
556 } elsif ($self->{since_date}->cmp($edit_date) < 0) {
557 substr($leader, 5, 1) = 'c';
558 $marc->leader($leader);
562 if ($Marque::config->option_value('format') eq 'XML') {
564 local $SIG{__WARN__} = sub {
565 my $message = "Warning from bibliographic record " . $r->id() . ": "
569 $output = $marc->as_xml_record;
570 $output =~ s/^<\?.+?\?>$//mo;
573 warn("Error in bibliographic record " . $r->id() . ": $@");
574 return $self->next();
578 local $SIG{__WARN__} = sub {
579 my $message = "Warning from bibliographic record " . $r->id() . ": "
583 $output = $marc->as_usmarc;
586 warn("Error in bibliographic record " . $r->id() . ": $@");
587 return $self->next();
594 # Returns a list of aou objects in an array.
597 unless ($self->{orgs} && @{$self->{orgs}}) {
598 my $fmClass = Fieldmapper::class_for_hint('aou');
599 my @classFields = $fmClass->real_fields();
600 my $classTable = $fmClass->Table();
601 my $query = 'select ' . join(',', @classFields);
602 $query .= "\nfrom $classTable";
603 my $sth = $self->{handle}->prepare($query);
604 if ($sth->execute()) {
605 my $result = $sth->fetchall_arrayref({});
606 my @orgs = map {$fmClass->from_bare_hash($_)} @{$result};
607 $self->{orgs} = \@orgs;
612 return @{$self->{orgs}};
615 # Returns an array of acpl objects.
619 unless ($self->{shelves} && @{$self->{shelves}}) {
620 my $fmClass = Fieldmapper::class_for_hint('acpl');
621 my @classFields = $fmClass->real_fields();
622 my $classTable = $fmClass->Table();
623 my $query = 'select ' . join(',', @classFields);
624 $query .= "\nfrom $classTable";
625 my $result = $self->{handle}->selectall_arrayref($query, {Slice=>{}});
626 my @shelves = map {$fmClass->from_bare_hash($_)} @{$result};
627 $self->{shelves} = \@shelves;
630 return @{$self->{shelves}};
633 # Returns an array of acnp objects.
637 unless ($self->{prefixes} && @{$self->{prefixes}}) {
638 my $fmClass = Fieldmapper::class_for_hint('acnp');
639 my @classFields = $fmClass->real_fields();
640 my $classTable = $fmClass->Table();
641 my $query = 'select ' . join(',', @classFields);
642 $query .= "\nfrom $classTable";
643 my $result = $self->{handle}->selectall_arrayref($query, {Slice=>{}});
644 my @prefixes = map {$fmClass->from_bare_hash($_)} @{$result};
645 $self->{prefixes} = \@prefixes;
648 return @{$self->{prefixes}};
651 # Returns an array of acns objects.
655 unless ($self->{suffixes} && @{$self->{suffixes}}) {
656 my $fmClass = Fieldmapper::class_for_hint('acns');
657 my @classFields = $fmClass->real_fields();
658 my $classTable = $fmClass->Table();
659 my $query = 'select ' . join(',', @classFields);
660 $query .= "\nfrom $classTable";
661 my $result = $self->{handle}->selectall_arrayref($query, {Slice=>{}});
662 my @suffixes = map {$fmClass->from_bare_hash($_)} @{$result};
663 $self->{suffixes} = \@suffixes;
666 return @{$self->{suffixes}};
669 # Returns an array of acn objects for a given bre object or id.
673 $bre = $bre->id() if (ref($bre));
675 unless ($self->{acnHandle}) {
676 my $query = "select " . join(',', $self->{acnClass}->real_fields());
677 $query .= "\nfrom " . $self->{acnClass}->Table();
678 $query .= "\nwhere record = ?";
679 if (@{$self->{libs}}) {
680 $query .= "\nand owning_lib in (";
681 $query .= join(',', @{$self->{libs}}) . ")";
683 $query .= "\nand deleted = 'f'" unless($Marque::config->option_value('since'));
684 $self->{acnHandle} = $self->{handle}->prepare($query);
687 if ($self->{acnHandle}->execute($bre)) {
688 my $result = $self->{acnHandle}->fetchall_arrayref({});
689 return map {$self->{acnClass}->from_bare_hash($_)} @{$result};
692 # If for some reason, we don't find anything.
696 # Returns an array of acp objects for a given bre object or id.
700 $bre = $bre->id() if (ref($bre));
702 my @orgs = $self->orgs();
703 my @locations = $self->shelves();
704 my @prefixes = $self->prefixes();
705 my @suffixes = $self->suffixes();
707 my @acns = $self->acns_for_bre($bre);
709 my $query = 'select ' . join(',', $self->{acpClass}->real_fields());
710 $query .= "\nfrom " . $self->{acpClass}->Table();
711 $query .= "\nwhere call_number in (";
712 $query .= join(',', map {$_->id()} @acns);
714 $query .= "\nand deleted = 'f'" unless ($Marque::config->option_value('since'));
715 my $result = $self->{handle}->selectall_arrayref($query, {Slice=>{}});
716 if ($result && @{$result}) {
717 my @acps = map {$self->{acpClass}->from_bare_hash($_)} @{$result};
719 my $cn = $_->call_number();
720 my $clib = $_->circ_lib();
721 my $loc = $_->location();
722 my ($org) = grep {$_->id() == $clib} @orgs;
723 my ($acn) = grep {$_->id() == $cn} @acns;
724 my ($location) = grep {$_->id() == $loc} @locations;
725 my $olib = $acn->owning_lib();
726 my $pre = $acn->prefix();
727 my $suf = $acn->suffix();
728 my ($acnp) = grep {$_->id() == $pre} @prefixes;
729 my ($acns) = grep {$_->id() == $suf} @suffixes;
730 my ($owner) = grep {$_->id() == $olib} @orgs;
731 $acn->owning_lib($owner);
732 $_->call_number($acn);
734 $_->location($location);
735 $_->call_number->prefix($acnp);
736 $_->call_number->suffix($acns);
742 # If for some reason, we don't find anything.
746 # Retreive an array for sre objects when the --mfhd option is used.
750 $bre = $bre->id() if (ref($bre));
752 # Build a query to retrieve SREs when the MFHD option is passed.
753 if ($Marque::config->option_value('mfhd')) {
754 # Create a persistent handle as needed.
755 unless ($self->{sreSth}) {
756 my $query = "select " . join(',', $self->{sreClass}->real_fields());
757 $query .= "\nfrom " . $self->{sreClass}->Table();
758 $query .= "\nwhere record = ?";
759 $query .= "\nand deleted = 'f'" unless ($Marque::config->option_value('since'));
760 $self->{sreSth} = $self->{handle}->prepare($query);
762 if ($self->{sreSth}->execute($bre)) {
763 while (my $data = $self->{sreSth}->fetchrow_hashref) {
764 push @sres, $self->{sreClass}->from_bare_hash($data);
766 $self->{sreSth}->finish; # Sometimes DBI complains.
773 # Get authority records from the database.
774 package Marque::Authority;
779 my $self = {idlist => $idlist};
780 $self->{handle} = Marque::Connector::connect(
781 $Marque::config->database_settings);
782 $self->{fmClass} = Fieldmapper::class_for_hint('are');
783 $self->{since_date} = Date::Manip::Date->new;
784 $self->{since_date}->parse($Marque::config->option_value('since'));
792 # Get the information for are object from the Fieldmapper:
793 my @fields = $self->{fmClass}->real_fields();
794 my $table = $self->{fmClass}->Table();
796 # Build the actual query.
797 my $select = "select " . join(',', @fields);
798 my $from = "from $table";
799 my $where = 'where ';
801 # If we have an idlist, we pretty much ignore anything else.
802 if ($self->{idlist} && @{$self->{idlist}}) {
803 $where .= 'id in (' . join(',', @{$self->{idlist}}) . ')';
804 } elsif ($Marque::config->option_value('since')) {
805 my $since_str = Marque::Connector::db_date($self->{since_date});
806 $where .= "edit_date > '$since_str'";
807 $where .= " or create_date > '$since_str'";
809 # We want all non-deleted records.
810 $where .= "deleted = 'f'";
813 $self->{query} = $select . "\n" . $from . "\n" . $where;
818 $self->build_query() unless ($self->{query});
819 $self->{sth} = $self->{handle}->prepare($self->{query});
820 return $self->{sth}->execute;
826 my $data = $self->{sth}->fetchrow_hashref;
829 my $format = $Marque::config->option_value('format');
830 my $r = $self->{fmClass}->from_bare_hash($data);
831 if ($format eq 'ARE') {
832 $output = OpenSRF::Utils::JSON->perl2JSON($r);
836 local $SIG{__WARN__} = sub {
837 my $message = "Warning from authority record " . $r->id() . ": "
841 $marc = MARC::Record->new_from_xml($r->marc(),
842 $Marque::config->option_value('encoding'),
843 $Marque::config->option_value('format'));
846 warn("Error in authority record " . $r->id() . ": $@");
847 import MARC::File::XML; # Reset SAX Parser.
848 return $self->next();
850 if ($Marque::config->option_value('replace_001')) {
851 my $tcn = $marc->field('001');
853 $tcn->update($r->id());
855 $tcn = MARC::Field->new('001', $r->id());
856 $marc->insert_fields_ordered($tcn);
859 if ($Marque::config->option_value('since')) {
860 my $leader = $marc->leader();
861 if ($U->is_true($r->deleted())) {
862 substr($leader, 5, 1) = 'd';
863 $marc->leader($leader);
865 my $create_date = Date::Manip::Date->new;
866 $create_date->parse($r->create_date());
867 my $edit_date = Date::Manip::Date->new;
868 $edit_date->parse($r->edit_date());
869 if ($self->{since_date}->cmp($create_date) < 0) {
870 substr($leader, 5, 1) = 'n';
871 $marc->leader($leader);
872 } elsif ($self->{since_date}->cmp($edit_date) < 0) {
873 substr($leader, 5, 1) = 'c';
874 $marc->leader($leader);
878 if ($Marque::config->option_value('format') eq 'XML') {
880 local $SIG{__WARN__} = sub {
881 my $message = "Warning from authority record " . $r->id() . ": "
885 $output = $marc->as_xml_record;
886 $output =~ s/^<\?.+?\?>$//mo;
889 warn("Error in authority record " . $r->id() . ": $@");
890 return $self->next();
894 local $SIG{__WARN__} = sub {
895 my $message = "Warning from authority record " . $r->id() . ": "
899 $output = $marc->as_usmarc;
902 warn("Error in authority record " . $r->id() . ": $@");
903 return $self->next();
912 # ------------------------------------------------------------------
913 # Since the ultimate output is largely independent of the type of the
914 # records, we use a single subpackage to group our output routines.
915 package Marque::Output;
918 my $extractor = shift;
919 if ($extractor->execute_query) {
920 if ($Marque::config->option_value('encoding') eq 'UTF-8') {
921 binmode(STDOUT, ':utf8');
923 binmode(STDOUT, ':raw');
927 while (my $output = $extractor->next()) {
932 print STDERR $extractor->{query} if ($Marque::config->option_value('debug'));
933 die "Database query failed!";
938 if ($Marque::config->option_value('format') eq 'XML') {
939 my $encoding = $Marque::config->option_value('encoding');
941 <?xml version="1.0" encoding="$encoding"?>
942 <collection xmlns='http://www.loc.gov/MARC21/slim'>
948 if ($Marque::config->option_value('format') eq 'XML') {
949 print "</collection>\n";