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('<mods:modsCollection version="3.0" 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/', undef);
443 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
444 $self->{type} = 'application/xml';
445 $self->{holdings_xpath} = '/mods:mods';
456 if ($type eq 'unapi' || $type eq 'opac') {
459 'http://www.loc.gov/mods/',
462 { type => 'otherFormat', id => 'link-'.$linkid }
465 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
466 'http://www.loc.gov/mods/',
467 'mods:recordIdentifier',
475 #----------------------------------------------------------
477 package OpenILS::WWW::SuperCat::Feed::mods3;
478 use base 'OpenILS::WWW::SuperCat::Feed::mods';
482 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
483 $self->{type} = 'application/xml';
484 $self->{item_xpath} = '/mods:modsCollection';
488 package OpenILS::WWW::SuperCat::Feed::mods3::item;
489 use base 'OpenILS::WWW::SuperCat::Feed::mods::item';
494 my $self = $class->SUPER::build($xml);
495 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', undef);
496 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
497 $self->{type} = 'application/xml';
498 $self->{holdings_xpath} = '/mods:mods';
507 if ($type eq 'unapi' || $type eq 'opac') {
510 'http://www.loc.gov/mods/v3',
513 { type => 'otherFormat', id => 'link-'.$linkid }
516 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
517 'http://www.loc.gov/mods/v3',
518 'mods:recordIdentifier',
526 #----------------------------------------------------------
528 package OpenILS::WWW::SuperCat::Feed::marcxml;
529 use base 'OpenILS::WWW::SuperCat::Feed';
533 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
534 $self->{type} = 'application/xml';
535 $self->{item_xpath} = '/marc:collection';
543 if ($type eq 'unapi') {
546 'http://www.w3.org/1999/xhtml',
549 { rel => 'unapi-server', href => $id, title => "unapi" }
556 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
557 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
562 my $self = $class->SUPER::build($xml);
563 return undef unless $self;
564 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', undef);
565 $self->{type} = 'application/xml';
566 $self->{holdings_xpath} = '/*[local-name()="record"]';
575 if ($type eq 'opac') {
577 '*[local-name()="record"]',
578 'http://www.w3.org/1999/xhtml',
581 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
584 } elsif ($type eq 'unapi-id') {
586 '*[local-name()="record"]',
587 'http://www.w3.org/1999/xhtml',
590 { title => $id, class => "unapi-id" }
597 #----------------------------------------------------------
599 package OpenILS::WWW::SuperCat::Feed::html;
600 use base 'OpenILS::WWW::SuperCat::Feed::atom';
604 my $self = $class->SUPER::new;
605 $self->type('text/html');
609 our ($_parser, $_xslt, $xslt_file);
613 my $base = $self->base || '';
614 my $root = $self->root || '';
615 my $search = $self->search || '';
616 my $class = $self->class || '';
617 my $lib = $self->lib || '-';
621 $_parser ||= new XML::LibXML;
622 $_xslt ||= new XML::LibXSLT;
625 OpenSRF::Utils::SettingsClient
627 ->config_value( dirs => 'xsl' ).
630 # parse the MODS xslt ...
631 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
633 my $new_doc = $atom2html_xslt->transform(
635 base_dir => "'$root'",
637 searchTerms => "'$search'",
638 searchClass => "'$class'",
641 return $new_doc->toString(1);
645 package OpenILS::WWW::SuperCat::Feed::html::item;
646 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
648 #----------------------------------------------------------
650 package OpenILS::WWW::SuperCat::Feed::htmlcard;
651 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
655 my $self = $class->SUPER::new;
656 $self->type('text/html');
657 $self->{xsl} = "/MARC21slim2HTMLCard.xsl";
661 our ($_parser, $_xslt, $xslt_file);
665 my $base = $self->base || '';
666 my $root = $self->root || '';
667 my $search = $self->search || '';
668 my $sort = $self->Sort || '';
669 my $sort_dir = $self->SortDir || '';
670 my $lang = $self->lang || '';
671 my $lib = $self->lib || '-';
675 $_parser ||= new XML::LibXML;
676 $_xslt ||= new XML::LibXSLT;
679 OpenSRF::Utils::SettingsClient
681 ->config_value( dirs => 'xsl' ).$self->{xsl};
683 # parse the MODS xslt ...
684 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
686 my $new_doc = $atom2html_xslt->transform(
688 base_dir => "'$root'",
690 searchTerms => "'$search'",
691 searchSort => "'$sort'",
692 searchSortDir => "'$sort_dir'",
693 searchLang => "'$lang'",
696 return $new_doc->toString(1);
699 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
700 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
702 package OpenILS::WWW::SuperCat::Feed::htmlholdings;
703 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard';
707 my $self = $class->SUPER::new;
708 $self->{xsl} = "/MARC21slim2HTMLCard-holdings.xsl";
712 package OpenILS::WWW::SuperCat::Feed::htmlholdings::item;
713 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard::item';