1 package OpenILS::WWW::SuperCat::Feed;
2 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
7 use OpenSRF::Utils::SettingsClient;
10 use DateTime::Format::Mail;
17 return 1 if UNIVERSAL::can("OpenILS::WWW::SuperCat::Feed::$type" => 'new');
28 throw OpenSRF::EX::ERROR ("I need a feed type!") ;
34 return undef unless $xml;
36 $parser = new XML::LibXML if (!$parser);
38 my $self = { doc => $parser->parse_string($xml), items => [] };
40 $self = bless $self => $class;
48 $self->{type} = $type if ($type);
54 return $self->{count};
60 $self->{search} = $search if ($search);
61 return $self->{search};
67 $self->{class} = $search if ($search);
68 return $self->{class};
74 $self->{sort} = $search if ($search);
81 $self->{sort_dir} = $search if ($search);
82 return $self->{sort_dir};
88 $self->{lang} = $search if ($search);
95 $self->{lib} = $lib if ($lib);
102 $self->{base} = $base if ($base);
103 return $self->{base};
109 $self->{root} = $root if ($root);
110 return $self->{root};
116 $self->{unapi} = $unapi if ($unapi);
117 return $self->{unapi};
122 $self->{count} += scalar(@_);
123 push @{ $self->{items} }, @_;
128 return @{ $self->{items} } if (wantarray);
129 return $self->{items};
138 for my $node ($self->{doc}->findnodes($xpath)) {
139 $node->appendChild($new);
153 for my $node ($self->{doc}->findnodes($xpath)) {
154 my $new = $self->{doc}->createElement($name) if (!$ns);
155 $new = $self->{doc}->createElementNS($ns,$name) if ($ns);
157 $new->appendChild( $self->{doc}->createTextNode( $text ) )
161 for my $key (keys %$attrs) {
162 next unless $$attrs{$key};
163 $new->setAttribute( $key => $$attrs{$key} );
167 $node->appendChild( $new );
175 my $class = ref($self) || $self;
178 my $item_xml = shift;
179 my $entry = $class->new($item_xml);
180 return undef unless $entry;
182 $entry->base($self->base);
183 $entry->unapi($self->unapi);
185 $self->push_item($entry);
191 my $holdings_xml = shift;
193 return $self unless ($holdings_xml);
195 $parser = new XML::LibXML if (!$parser);
196 my $new_doc = $parser->parse_string($holdings_xml);
198 for my $root ( $self->{doc}->findnodes($self->{holdings_xpath}) ) {
199 $root->appendChild($new_doc->documentElement);
207 for my $root ( $self->{doc}->findnodes($self->{item_xpath}) ) {
208 for my $item ( $self->items ) {
209 $root->appendChild( $item->{doc}->documentElement );
218 return $self->{doc}->toString(1);
227 #----------------------------------------------------------
229 package OpenILS::WWW::SuperCat::Feed::atom;
230 use base 'OpenILS::WWW::SuperCat::Feed';
231 use OpenSRF::Utils qw/:datetime/;
235 my $self = $class->SUPER::build('<feed xmlns:atom="http://www.w3.org/2005/Atom"/>');
236 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', undef);
237 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
238 $self->{type} = 'application/atom+xml';
239 $self->{item_xpath} = '/atom:feed';
246 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','title', $text);
251 # ATOM demands RFC-3339 compliant datetime formats
252 my $text = shift || gmtime_ISO8601();
253 $self->_create_node($self->{item_xpath},'http://www.w3.org/2005/Atom','updated', $text);
259 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','author');
260 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','name', $text);
267 my $mime = shift || "application/x-$type+xml";
270 $type = 'self' if ($type eq 'atom');
274 'http://www.w3.org/2005/Atom',
289 $self->_create_node( $self->{item_xpath}, 'http://www.w3.org/2005/Atom', 'id', $id );
292 package OpenILS::WWW::SuperCat::Feed::atom::item;
293 use base 'OpenILS::WWW::SuperCat::Feed::atom';
298 my $self = $class->SUPER::build($xml);
299 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', undef);
300 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
301 $self->{item_xpath} = '/atom:entry';
302 $self->{holdings_xpath} = '/atom:entry';
303 $self->{type} = 'application/atom+xml';
308 #----------------------------------------------------------
310 package OpenILS::WWW::SuperCat::Feed::rss2;
311 use base 'OpenILS::WWW::SuperCat::Feed';
315 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
316 $self->{type} = 'application/rss+xml';
317 $self->{item_xpath} = '/rss/channel';
324 $self->_create_node('/rss/channel',undef,'title', $text);
325 # RSS2 demands a /channel/description element; just dupe title until we give
326 # users the ability to provide a description for their bookbags
327 $self->_create_node('/rss/channel',undef,'description', $text);
332 # RSS2 demands RFC-822 compliant datetime formats
333 my $text = shift || DateTime::Format::Mail->format_datetime(DateTime->now());
334 $self->_create_node($self->{item_xpath},undef,'lastBuildDate', $text);
340 $self->_create_node('/rss/channel', undef,'generator', $text);
347 my $mime = shift || "application/x-$type+xml";
349 if ($type eq 'rss2' or $type eq 'alternate') {
350 # Just link to ourself using standard RSS2 link element
359 # Alternate link: use XHTML link element
362 'http://www.w3.org/1999/xhtml',
376 $self->_create_node($self->{item_xpath}, undef,'guid', $id);
379 package OpenILS::WWW::SuperCat::Feed::rss2::item;
380 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
385 my $self = $class->SUPER::build($xml);
386 $self->{type} = 'application/rss+xml';
387 $self->{item_xpath} = '/item';
388 $self->{holdings_xpath} = '/item';
394 # RSS2 demands RFC-822 compliant datetime formats
397 # No date passed in, default to now
398 $text = DateTime::Format::Mail->format_datetime(DateTime->now());
399 } elsif ($text =~ m/^\s*(\d{4})\.?\s*$/o) {
400 # Publication date is just a year, convert accordingly
401 my $year = DateTime->new(year=>$1);
402 $text = DateTime::Format::Mail->format_datetime($year);
404 $self->_create_node($self->{item_xpath},undef,'pubDate', $text);
422 #----------------------------------------------------------
424 package OpenILS::WWW::SuperCat::Feed::mods;
425 use base 'OpenILS::WWW::SuperCat::Feed';
429 my $self = $class->SUPER::build('<modsCollection version="3.0" xmlns="http://www.loc.gov/mods/" xmlns:mods="http://www.loc.gov/mods/"/>');
430 $self->{type} = 'application/xml';
431 $self->{item_xpath} = '/mods:modsCollection';
435 package OpenILS::WWW::SuperCat::Feed::mods::item;
436 use base 'OpenILS::WWW::SuperCat::Feed::mods';
441 my $self = $class->SUPER::build($xml);
442 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
443 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', undef, 1);
444 $self->{type} = 'application/xml';
445 $self->{holdings_xpath} = '/mods:mods';
456 if ($type eq 'unapi' || $type eq 'opac') {
462 { type => 'otherFormat', id => 'link-'.$linkid }
465 "mods:mods/relatedItem[\@id='link-$linkid']",
474 #----------------------------------------------------------
476 package OpenILS::WWW::SuperCat::Feed::mods3;
477 use base 'OpenILS::WWW::SuperCat::Feed::mods';
481 my $self = $class->SUPER::build('<modsCollection version="3.0" xmlns="http://www.loc.gov/mods/v3" xmlns:mods="http://www.loc.gov/mods/v3"/>');
482 $self->{type} = 'application/xml';
483 $self->{item_xpath} = '/mods:modsCollection';
487 package OpenILS::WWW::SuperCat::Feed::mods3::item;
488 use base 'OpenILS::WWW::SuperCat::Feed::mods::item';
493 my $self = $class->SUPER::build($xml);
494 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
495 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', undef, 1);
496 $self->{type} = 'application/xml';
497 $self->{holdings_xpath} = '/mods:mods';
506 if ($type eq 'unapi' || $type eq 'opac') {
512 { type => 'otherFormat', id => 'link-'.$linkid }
515 "mods:mods/relatedItem[\@id='link-$linkid']",
525 #----------------------------------------------------------
527 package OpenILS::WWW::SuperCat::Feed::mods32;
528 use base 'OpenILS::WWW::SuperCat::Feed::mods3';
532 my $self = $class->SUPER::build('<modsCollection version="3.2" xmlns="http://www.loc.gov/mods/v3" xmlns:mods="http://www.loc.gov/mods/v3"/>');
533 $self->{type} = 'application/xml';
534 $self->{item_xpath} = '/mods:modsCollection';
538 package OpenILS::WWW::SuperCat::Feed::mods32::item;
539 use base 'OpenILS::WWW::SuperCat::Feed::mods3::item';
541 #----------------------------------------------------------
543 package OpenILS::WWW::SuperCat::Feed::mods33;
544 use base 'OpenILS::WWW::SuperCat::Feed::mods3';
548 my $self = $class->SUPER::build('<modsCollection version="3.3" xmlns="http://www.loc.gov/mods/v3" xmlns:mods="http://www.loc.gov/mods/v3"/>');
549 $self->{type} = 'application/xml';
550 $self->{item_xpath} = '/mods:modsCollection';
554 package OpenILS::WWW::SuperCat::Feed::mods33::item;
555 use base 'OpenILS::WWW::SuperCat::Feed::mods3::item';
558 #----------------------------------------------------------
560 package OpenILS::WWW::SuperCat::Feed::marcxml;
561 use base 'OpenILS::WWW::SuperCat::Feed';
565 my $self = $class->SUPER::build('<collection xmlns="http://www.loc.gov/MARC21/slim" xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
566 $self->{type} = 'application/xml';
567 $self->{item_xpath} = '/marc:collection';
575 if ($type eq 'unapi') {
578 'http://www.w3.org/1999/xhtml',
581 { rel => 'unapi-server', href => $id, title => "unapi" }
588 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
589 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
594 my $self = $class->SUPER::build($xml);
595 return undef unless $self;
596 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', undef);
597 $self->{type} = 'application/xml';
598 $self->{holdings_xpath} = '/*[local-name()="record"]';
607 if ($type eq 'opac') {
609 '*[local-name()="record"]',
610 'http://www.w3.org/1999/xhtml',
613 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
616 } elsif ($type eq 'unapi-id') {
618 '*[local-name()="record"]',
619 'http://www.w3.org/1999/xhtml',
622 { title => $id, class => "unapi-id" }
629 #----------------------------------------------------------
631 package OpenILS::WWW::SuperCat::Feed::html;
632 use base 'OpenILS::WWW::SuperCat::Feed::atom';
636 my $self = $class->SUPER::new;
637 $self->type('text/html');
641 our ($_parser, $_xslt, $xslt_file);
645 my $base = $self->base || '';
646 my $root = $self->root || '';
647 my $search = $self->search || '';
648 my $class = $self->class || '';
649 my $lib = $self->lib || '-';
653 $_parser ||= new XML::LibXML;
654 $_xslt ||= new XML::LibXSLT;
657 OpenSRF::Utils::SettingsClient
659 ->config_value( dirs => 'xsl' ).
662 # parse the MODS xslt ...
663 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
665 my $new_doc = $atom2html_xslt->transform(
667 base_dir => "'$root'",
669 searchTerms => "'$search'",
670 searchClass => "'$class'",
673 return $new_doc->toString(1);
677 package OpenILS::WWW::SuperCat::Feed::html::item;
678 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
680 #----------------------------------------------------------
682 package OpenILS::WWW::SuperCat::Feed::htmlcard;
683 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
687 my $self = $class->SUPER::new;
688 $self->type('text/html');
689 $self->{xsl} = "/MARC21slim2HTMLCard.xsl";
693 our ($_parser, $_xslt, $xslt_file);
697 my $base = $self->base || '';
698 my $root = $self->root || '';
699 my $search = $self->search || '';
700 my $sort = $self->Sort || '';
701 my $sort_dir = $self->SortDir || '';
702 my $lang = $self->lang || '';
703 my $lib = $self->lib || '-';
707 $_parser ||= new XML::LibXML;
708 $_xslt ||= new XML::LibXSLT;
711 OpenSRF::Utils::SettingsClient
713 ->config_value( dirs => 'xsl' ).$self->{xsl};
715 # parse the MODS xslt ...
716 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
718 my $new_doc = $atom2html_xslt->transform(
720 base_dir => "'$root'",
722 searchTerms => "'$search'",
723 searchSort => "'$sort'",
724 searchSortDir => "'$sort_dir'",
725 searchLang => "'$lang'",
728 return $new_doc->toString(1);
731 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
732 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
734 package OpenILS::WWW::SuperCat::Feed::htmlholdings;
735 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard';
739 my $self = $class->SUPER::new;
740 $self->{xsl} = "/MARC21slim2HTMLCard-holdings.xsl";
744 package OpenILS::WWW::SuperCat::Feed::htmlholdings::item;
745 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard::item';
748 package OpenILS::WWW::SuperCat::Feed::marctxt;
749 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
753 my $self = $class->SUPER::new;
754 $self->{type} = 'text/plain';
755 $self->{xsl} = "/MARC21slim2MARCtxt.xsl";
760 our ($_parser, $_xslt, $xslt_file);
764 my $base = $self->base || '';
765 my $root = $self->root || '';
766 my $search = $self->search || '';
767 my $class = $self->class || '';
768 my $lib = $self->lib || '-';
772 $_parser ||= new XML::LibXML;
773 $_xslt ||= new XML::LibXSLT;
776 OpenSRF::Utils::SettingsClient
778 ->config_value( dirs => 'xsl' ).
781 # parse the MARC text xslt ...
782 my $marctxt_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
784 my $new_doc = $marctxt_xslt->transform(
786 base_dir => "'$root'",
788 searchTerms => "'$search'",
789 searchClass => "'$class'",
792 return $marctxt_xslt->output_string($new_doc);
796 package OpenILS::WWW::SuperCat::Feed::marctxt::item;
797 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
800 package OpenILS::WWW::SuperCat::Feed::ris;
801 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
805 my $self = $class->SUPER::new;
806 $self->{type} = 'text/plain';
807 $self->{xsl} = "/MARC21slim2RIS.xsl";
812 our ($_parser, $_xslt, $xslt_file);
816 my $base = $self->base || '';
817 my $root = $self->root || '';
818 my $search = $self->search || '';
819 my $class = $self->class || '';
820 my $lib = $self->lib || '-';
824 $_parser ||= new XML::LibXML;
825 $_xslt ||= new XML::LibXSLT;
828 OpenSRF::Utils::SettingsClient
830 ->config_value( dirs => 'xsl' ).
833 # parse the MARC text xslt ...
834 my $ris_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
836 my $new_doc = $ris_xslt->transform(
838 base_dir => "'$root'",
840 searchTerms => "'$search'",
841 searchClass => "'$class'",
844 return $ris_xslt->output_string($new_doc);
848 package OpenILS::WWW::SuperCat::Feed::ris::item;
849 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';