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->{sort} = $search if ($search);
78 $self->{sort_dir} = $search if ($search);
79 return $self->{sort_dir};
85 $self->{lang} = $search if ($search);
92 $self->{lib} = $lib if ($lib);
99 $self->{base} = $base if ($base);
100 return $self->{base};
106 $self->{root} = $root if ($root);
107 return $self->{root};
113 $self->{unapi} = $unapi if ($unapi);
114 return $self->{unapi};
119 $self->{count} += scalar(@_);
120 push @{ $self->{items} }, @_;
125 return @{ $self->{items} } if (wantarray);
126 return $self->{items};
135 for my $node ($self->{doc}->findnodes($xpath)) {
136 $node->appendChild($new);
150 for my $node ($self->{doc}->findnodes($xpath)) {
151 my $new = $self->{doc}->createElement($name) if (!$ns);
152 $new = $self->{doc}->createElementNS($ns,$name) if ($ns);
154 $new->appendChild( $self->{doc}->createTextNode( $text ) )
158 for my $key (keys %$attrs) {
159 next unless $$attrs{$key};
160 $new->setAttribute( $key => $$attrs{$key} );
164 $node->appendChild( $new );
172 my $class = ref($self) || $self;
175 my $item_xml = shift;
176 my $entry = $class->new($item_xml);
177 return undef unless $entry;
179 $entry->base($self->base);
180 $entry->unapi($self->unapi);
182 $self->push_item($entry);
188 my $holdings_xml = shift;
190 return $self unless ($holdings_xml);
192 $parser = new XML::LibXML if (!$parser);
193 my $new_doc = $parser->parse_string($holdings_xml);
195 for my $root ( $self->{doc}->findnodes($self->{holdings_xpath}) ) {
196 $root->appendChild($new_doc->documentElement);
204 for my $root ( $self->{doc}->findnodes($self->{item_xpath}) ) {
205 for my $item ( $self->items ) {
206 $root->appendChild( $item->{doc}->documentElement );
215 return $self->{doc}->toString(1);
224 #----------------------------------------------------------
226 package OpenILS::WWW::SuperCat::Feed::atom;
227 use base 'OpenILS::WWW::SuperCat::Feed';
231 my $self = $class->SUPER::build('<feed xmlns:atom="http://www.w3.org/2005/Atom"/>');
232 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', undef);
233 $self->{type} = 'application/xml';
234 $self->{item_xpath} = '/atom:feed';
241 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','title', $text);
247 $self->_create_node($self->{item_xpath},'http://www.w3.org/2005/Atom','updated', $text);
253 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','author');
254 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','name', $text);
261 my $mime = shift || "application/x-$type+xml";
264 $type = 'self' if ($type eq 'atom');
268 'http://www.w3.org/2005/Atom',
283 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'id', $id );
286 package OpenILS::WWW::SuperCat::Feed::atom::item;
287 use base 'OpenILS::WWW::SuperCat::Feed::atom';
292 my $self = $class->SUPER::build($xml);
293 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
294 $self->{item_xpath} = '/atom:entry';
295 $self->{holdings_xpath} = '/atom:entry';
296 $self->{type} = 'application/xml';
301 #----------------------------------------------------------
303 package OpenILS::WWW::SuperCat::Feed::rss2;
304 use base 'OpenILS::WWW::SuperCat::Feed';
308 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
309 $self->{type} = 'application/xml';
310 $self->{item_xpath} = '/rss/channel';
317 $self->_create_node('/rss/channel',undef,'title', $text);
323 $self->_create_node($self->{item_xpath},undef,'lastBuildDate', $text);
329 $self->_create_node('/rss/channel', undef,'generator', $text);
336 my $mime = shift || "application/x-$type+xml";
338 $type = 'self' if ($type eq 'rss2');
351 package OpenILS::WWW::SuperCat::Feed::rss2::item;
352 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
357 my $self = $class->SUPER::build($xml);
358 $self->{type} = 'application/xml';
359 $self->{item_xpath} = '/item';
360 $self->{holdings_xpath} = '/item';
367 $self->_create_node($self->{item_xpath},undef,'pubDate', $text);
372 #----------------------------------------------------------
374 package OpenILS::WWW::SuperCat::Feed::mods;
375 use base 'OpenILS::WWW::SuperCat::Feed';
379 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
380 $self->{type} = 'application/xml';
381 $self->{item_xpath} = '/mods:modsCollection';
385 package OpenILS::WWW::SuperCat::Feed::mods::item;
386 use base 'OpenILS::WWW::SuperCat::Feed::mods';
391 my $self = $class->SUPER::build($xml);
392 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
393 $self->{type} = 'application/xml';
394 $self->{holdings_xpath} = '/mods:mods';
405 if ($type eq 'unapi' || $type eq 'opac') {
408 'http://www.loc.gov/mods/',
411 { type => 'otherFormat', id => 'link-'.$linkid }
414 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
415 'http://www.loc.gov/mods/',
416 'mods:recordIdentifier',
424 #----------------------------------------------------------
426 package OpenILS::WWW::SuperCat::Feed::mods3;
427 use base 'OpenILS::WWW::SuperCat::Feed::mods';
431 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
432 $self->{type} = 'application/xml';
433 $self->{item_xpath} = '/mods:modsCollection';
437 package OpenILS::WWW::SuperCat::Feed::mods3::item;
438 use base 'OpenILS::WWW::SuperCat::Feed::mods::item';
443 my $self = $class->SUPER::build($xml);
444 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
445 $self->{type} = 'application/xml';
446 $self->{holdings_xpath} = '/mods:mods';
455 if ($type eq 'unapi' || $type eq 'opac') {
458 'http://www.loc.gov/mods/v3',
461 { type => 'otherFormat', id => 'link-'.$linkid }
464 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
465 'http://www.loc.gov/mods/v3',
466 'mods:recordIdentifier',
474 #----------------------------------------------------------
476 package OpenILS::WWW::SuperCat::Feed::marcxml;
477 use base 'OpenILS::WWW::SuperCat::Feed';
481 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
482 $self->{type} = 'application/xml';
483 $self->{item_xpath} = '/marc:collection';
491 if ($type eq 'unapi') {
494 'http://www.w3.org/1999/xhtml',
497 { rel => 'unapi-server', href => $id, title => "unapi" }
504 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
505 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
510 my $self = $class->SUPER::build($xml);
511 return undef unless $self;
512 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
513 $self->{type} = 'application/xml';
514 $self->{holdings_xpath} = '/marc:record';
523 if ($type eq 'opac') {
526 'http://www.w3.org/1999/xhtml',
529 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
532 } elsif ($type eq 'unapi-id') {
535 'http://www.w3.org/1999/xhtml',
538 { title => $id, class => "unapi-id" }
545 #----------------------------------------------------------
547 package OpenILS::WWW::SuperCat::Feed::html;
548 use base 'OpenILS::WWW::SuperCat::Feed::atom';
552 my $self = $class->SUPER::new;
553 $self->type('text/html');
557 our ($_parser, $_xslt, $xslt_file);
561 my $base = $self->base || '';
562 my $root = $self->root || '';
563 my $search = $self->search || '';
564 my $class = $self->class || '';
565 my $lib = $self->lib || '-';
569 $_parser ||= new XML::LibXML;
570 $_xslt ||= new XML::LibXSLT;
573 OpenSRF::Utils::SettingsClient
575 ->config_value( dirs => 'xsl' ).
578 # parse the MODS xslt ...
579 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
581 my $new_doc = $atom2html_xslt->transform(
583 base_dir => "'$root'",
585 searchTerms => "'$search'",
586 searchClass => "'$class'",
589 return $new_doc->toString(1);
593 package OpenILS::WWW::SuperCat::Feed::html::item;
594 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
596 #----------------------------------------------------------
598 package OpenILS::WWW::SuperCat::Feed::htmlcard;
599 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
603 my $self = $class->SUPER::new;
604 $self->type('text/html');
605 $self->{xsl} = "/MARC21slim2HTMLCard.xsl";
609 our ($_parser, $_xslt, $xslt_file);
613 my $base = $self->base || '';
614 my $root = $self->root || '';
615 my $search = $self->search || '';
616 my $sort = $self->Sort || '';
617 my $sort_dir = $self->SortDir || '';
618 my $lang = $self->lang || '';
619 my $lib = $self->lib || '-';
623 $_parser ||= new XML::LibXML;
624 $_xslt ||= new XML::LibXSLT;
627 OpenSRF::Utils::SettingsClient
629 ->config_value( dirs => 'xsl' ).$self->{xsl};
631 # parse the MODS xslt ...
632 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
634 my $new_doc = $atom2html_xslt->transform(
636 base_dir => "'$root'",
638 searchTerms => "'$search'",
639 searchSort => "'$sort'",
640 searchSortDir => "'$sort_dir'",
641 searchLang => "'$lang'",
644 return $new_doc->toString(1);
647 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
648 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
650 package OpenILS::WWW::SuperCat::Feed::htmlholdings;
651 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard';
655 my $self = $class->SUPER::new;
656 $self->{xsl} = "/MARC21slim2HTMLCard-holdings.xsl";
660 package OpenILS::WWW::SuperCat::Feed::htmlholdings::item;
661 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard::item';