1 package OpenILS::WWW::SuperCat::Feed;
2 use strict; use warnings;
4 use OpenSRF::EX qw(:try);
7 use OpenSRF::Utils::SettingsClient;
14 return 1 if UNIVERSAL::can("OpenILS::WWW::SuperCat::Feed::$type" => 'new');
25 throw OpenSRF::EX::ERROR ("I need a feed type!") ;
31 return undef unless $xml;
33 $parser = new XML::LibXML if (!$parser);
35 my $self = { doc => $parser->parse_string($xml), items => [] };
37 $self = bless $self => $class;
45 $self->{type} = $type if ($type);
51 return $self->{count};
57 $self->{search} = $search if ($search);
58 return $self->{search};
64 $self->{class} = $search if ($search);
65 return $self->{class};
71 $self->{lib} = $lib if ($lib);
78 $self->{base} = $base if ($base);
85 $self->{root} = $root if ($root);
92 $self->{unapi} = $unapi if ($unapi);
93 return $self->{unapi};
98 $self->{count} += scalar(@_);
99 push @{ $self->{items} }, @_;
104 return @{ $self->{items} } if (wantarray);
105 return $self->{items};
114 for my $node ($self->{doc}->findnodes($xpath)) {
115 $node->appendChild($new);
129 for my $node ($self->{doc}->findnodes($xpath)) {
130 my $new = $self->{doc}->createElement($name) if (!$ns);
131 $new = $self->{doc}->createElementNS($ns,$name) if ($ns);
133 $new->appendChild( $self->{doc}->createTextNode( $text ) )
137 for my $key (keys %$attrs) {
138 next unless $$attrs{$key};
139 $new->setAttribute( $key => $$attrs{$key} );
143 $node->appendChild( $new );
151 my $class = ref($self) || $self;
154 my $item_xml = shift;
155 my $entry = $class->new($item_xml);
156 return undef unless $entry;
158 $entry->base($self->base);
159 $entry->unapi($self->unapi);
161 $self->push_item($entry);
167 my $holdings_xml = shift;
169 $parser = new XML::LibXML if (!$parser);
170 my $new_doc = $parser->parse_string($holdings_xml);
172 for my $root ( $self->{doc}->findnodes($self->{holdings_xpath}) ) {
173 $root->appendChild($new_doc->documentElement);
181 for my $root ( $self->{doc}->findnodes($self->{item_xpath}) ) {
182 for my $item ( $self->items ) {
183 $root->appendChild( $item->{doc}->documentElement );
192 return $self->{doc}->toString(1);
201 #----------------------------------------------------------
203 package OpenILS::WWW::SuperCat::Feed::atom;
204 use base 'OpenILS::WWW::SuperCat::Feed';
208 my $self = $class->SUPER::build('<feed xmlns:atom="http://www.w3.org/2005/Atom"/>');
209 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', undef);
210 $self->{type} = 'application/xml';
211 $self->{item_xpath} = '/atom:feed';
218 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','title', $text);
224 $self->_create_node($self->{item_xpath},'http://www.w3.org/2005/Atom','updated', $text);
230 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','author');
231 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','name', $text);
238 my $mime = shift || "application/x-$type+xml";
241 $type = 'self' if ($type eq 'atom');
245 'http://www.w3.org/2005/Atom',
260 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'id', $id );
263 package OpenILS::WWW::SuperCat::Feed::atom::item;
264 use base 'OpenILS::WWW::SuperCat::Feed::atom';
269 my $self = $class->SUPER::build($xml);
270 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
271 $self->{item_xpath} = '/atom:entry';
272 $self->{holdings_xpath} = '/atom:entry';
273 $self->{type} = 'application/xml';
278 #----------------------------------------------------------
280 package OpenILS::WWW::SuperCat::Feed::rss2;
281 use base 'OpenILS::WWW::SuperCat::Feed';
285 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
286 $self->{type} = 'application/xml';
287 $self->{item_xpath} = '/rss/channel';
294 $self->_create_node('/rss/channel',undef,'title', $text);
300 $self->_create_node($self->{item_xpath},undef,'lastBuildDate', $text);
306 $self->_create_node('/rss/channel', undef,'generator', $text);
313 my $mime = shift || "application/x-$type+xml";
315 $type = 'self' if ($type eq 'rss2');
328 package OpenILS::WWW::SuperCat::Feed::rss2::item;
329 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
334 my $self = $class->SUPER::build($xml);
335 $self->{type} = 'application/xml';
336 $self->{item_xpath} = '/item';
337 $self->{holdings_xpath} = '/item';
344 $self->_create_node($self->{item_xpath},undef,'pubDate', $text);
349 #----------------------------------------------------------
351 package OpenILS::WWW::SuperCat::Feed::mods;
352 use base 'OpenILS::WWW::SuperCat::Feed';
356 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
357 $self->{type} = 'application/xml';
358 $self->{item_xpath} = '/mods:modsCollection';
362 package OpenILS::WWW::SuperCat::Feed::mods::item;
363 use base 'OpenILS::WWW::SuperCat::Feed::mods';
368 my $self = $class->SUPER::build($xml);
369 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
370 $self->{type} = 'application/xml';
371 $self->{holdings_xpath} = '/mods:mods';
382 if ($type eq 'unapi' || $type eq 'opac') {
385 'http://www.loc.gov/mods/',
388 { type => 'otherFormat', id => 'link-'.$linkid }
391 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
392 'http://www.loc.gov/mods/',
393 'mods:recordIdentifier',
401 #----------------------------------------------------------
403 package OpenILS::WWW::SuperCat::Feed::mods3;
404 use base 'OpenILS::WWW::SuperCat::Feed::mods';
408 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
409 $self->{type} = 'application/xml';
410 $self->{item_xpath} = '/mods:modsCollection';
414 package OpenILS::WWW::SuperCat::Feed::mods3::item;
415 use base 'OpenILS::WWW::SuperCat::Feed::mods::item';
420 my $self = $class->SUPER::build($xml);
421 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
422 $self->{type} = 'application/xml';
423 $self->{holdings_xpath} = '/mods:mods';
432 if ($type eq 'unapi' || $type eq 'opac') {
435 'http://www.loc.gov/mods/v3',
438 { type => 'otherFormat', id => 'link-'.$linkid }
441 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
442 'http://www.loc.gov/mods/v3',
443 'mods:recordIdentifier',
451 #----------------------------------------------------------
453 package OpenILS::WWW::SuperCat::Feed::marcxml;
454 use base 'OpenILS::WWW::SuperCat::Feed';
458 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
459 $self->{type} = 'application/xml';
460 $self->{item_xpath} = '/marc:collection';
468 if ($type eq 'unapi') {
471 'http://www.w3.org/1999/xhtml',
474 { rel => 'unapi-server', href => $id, title => "unapi" }
481 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
482 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
487 my $self = $class->SUPER::build($xml);
488 return undef unless $self;
489 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
490 $self->{type} = 'application/xml';
491 $self->{holdings_xpath} = '/marc:record';
500 if ($type eq 'opac') {
503 'http://www.w3.org/1999/xhtml',
506 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
509 } elsif ($type eq 'unapi-id') {
512 'http://www.w3.org/1999/xhtml',
515 { title => $id, class => "unapi-id" }
522 #----------------------------------------------------------
524 package OpenILS::WWW::SuperCat::Feed::html;
525 use base 'OpenILS::WWW::SuperCat::Feed::atom';
529 my $self = $class->SUPER::new;
530 $self->type('text/html');
534 our ($_parser, $_xslt, $xslt_file);
538 my $base = $self->base || '';
539 my $root = $self->root || '';
540 my $search = $self->search || '';
541 my $class = $self->class || '';
542 my $lib = $self->lib || '-';
546 $_parser ||= new XML::LibXML;
547 $_xslt ||= new XML::LibXSLT;
550 OpenSRF::Utils::SettingsClient
552 ->config_value( dirs => 'xsl' ).
555 # parse the MODS xslt ...
556 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
558 my $new_doc = $atom2html_xslt->transform(
560 base_dir => "'$root'",
562 searchTerms => "'$search'",
563 searchClass => "'$class'",
566 return $new_doc->toString(1);
570 package OpenILS::WWW::SuperCat::Feed::html::item;
571 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
573 #----------------------------------------------------------
575 package OpenILS::WWW::SuperCat::Feed::htmlcard;
576 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
580 my $self = $class->SUPER::new;
581 $self->type('text/html');
582 $self->{xsl} = "/MARC21slim2HTMLCard.xsl";
586 our ($_parser, $_xslt, $xslt_file);
590 my $base = $self->base || '';
591 my $root = $self->root || '';
592 my $search = $self->search || '';
593 my $lib = $self->lib || '-';
597 $_parser ||= new XML::LibXML;
598 $_xslt ||= new XML::LibXSLT;
601 OpenSRF::Utils::SettingsClient
603 ->config_value( dirs => 'xsl' ).$self->{xsl};
605 # parse the MODS xslt ...
606 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
608 my $new_doc = $atom2html_xslt->transform(
610 base_dir => "'$root'",
612 searchTerms => "'$search'",
615 return $new_doc->toString(1);
618 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
619 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
621 package OpenILS::WWW::SuperCat::Feed::htmlholdings;
622 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard';
626 my $self = $class->SUPER::new;
627 $self->{xsl} = "/MARC21slim2HTMLCard-holdings.xsl";
631 package OpenILS::WWW::SuperCat::Feed::htmlholdings::item;
632 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard::item';