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->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
234 $self->{type} = 'application/xml';
235 $self->{item_xpath} = '/atom:feed';
242 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','title', $text);
248 $self->_create_node($self->{item_xpath},'http://www.w3.org/2005/Atom','updated', $text);
254 $self->_create_node('/atom:feed','http://www.w3.org/2005/Atom','author');
255 $self->_create_node('/atom:feed/atom:author', 'http://www.w3.org/2005/Atom','name', $text);
262 my $mime = shift || "application/x-$type+xml";
265 $type = 'self' if ($type eq 'atom');
269 'http://www.w3.org/2005/Atom',
284 $self->_create_node( '/atom:feed', 'http://www.w3.org/2005/Atom', 'id', $id );
287 package OpenILS::WWW::SuperCat::Feed::atom::item;
288 use base 'OpenILS::WWW::SuperCat::Feed::atom';
293 my $self = $class->SUPER::build($xml);
294 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', undef);
295 $self->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom');
296 $self->{item_xpath} = '/atom:entry';
297 $self->{holdings_xpath} = '/atom:entry';
298 $self->{type} = 'application/xml';
303 #----------------------------------------------------------
305 package OpenILS::WWW::SuperCat::Feed::rss2;
306 use base 'OpenILS::WWW::SuperCat::Feed';
310 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
311 $self->{type} = 'application/xml';
312 $self->{item_xpath} = '/rss/channel';
319 $self->_create_node('/rss/channel',undef,'title', $text);
325 $self->_create_node($self->{item_xpath},undef,'lastBuildDate', $text);
331 $self->_create_node('/rss/channel', undef,'generator', $text);
338 my $mime = shift || "application/x-$type+xml";
340 $type = 'self' if ($type eq 'rss2');
353 package OpenILS::WWW::SuperCat::Feed::rss2::item;
354 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
359 my $self = $class->SUPER::build($xml);
360 $self->{type} = 'application/xml';
361 $self->{item_xpath} = '/item';
362 $self->{holdings_xpath} = '/item';
369 $self->_create_node($self->{item_xpath},undef,'pubDate', $text);
374 #----------------------------------------------------------
376 package OpenILS::WWW::SuperCat::Feed::mods;
377 use base 'OpenILS::WWW::SuperCat::Feed';
381 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
382 $self->{type} = 'application/xml';
383 $self->{item_xpath} = '/mods:modsCollection';
387 package OpenILS::WWW::SuperCat::Feed::mods::item;
388 use base 'OpenILS::WWW::SuperCat::Feed::mods';
393 my $self = $class->SUPER::build($xml);
394 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', undef);
395 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
396 $self->{type} = 'application/xml';
397 $self->{holdings_xpath} = '/mods:mods';
408 if ($type eq 'unapi' || $type eq 'opac') {
411 'http://www.loc.gov/mods/',
414 { type => 'otherFormat', id => 'link-'.$linkid }
417 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
418 'http://www.loc.gov/mods/',
419 'mods:recordIdentifier',
427 #----------------------------------------------------------
429 package OpenILS::WWW::SuperCat::Feed::mods3;
430 use base 'OpenILS::WWW::SuperCat::Feed::mods';
434 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
435 $self->{type} = 'application/xml';
436 $self->{item_xpath} = '/mods:modsCollection';
440 package OpenILS::WWW::SuperCat::Feed::mods3::item;
441 use base 'OpenILS::WWW::SuperCat::Feed::mods::item';
446 my $self = $class->SUPER::build($xml);
447 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', undef);
448 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
449 $self->{type} = 'application/xml';
450 $self->{holdings_xpath} = '/mods:mods';
459 if ($type eq 'unapi' || $type eq 'opac') {
462 'http://www.loc.gov/mods/v3',
465 { type => 'otherFormat', id => 'link-'.$linkid }
468 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
469 'http://www.loc.gov/mods/v3',
470 'mods:recordIdentifier',
478 #----------------------------------------------------------
480 package OpenILS::WWW::SuperCat::Feed::marcxml;
481 use base 'OpenILS::WWW::SuperCat::Feed';
485 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
486 $self->{type} = 'application/xml';
487 $self->{item_xpath} = '/marc:collection';
495 if ($type eq 'unapi') {
498 'http://www.w3.org/1999/xhtml',
501 { rel => 'unapi-server', href => $id, title => "unapi" }
508 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
509 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
514 my $self = $class->SUPER::build($xml);
515 return undef unless $self;
516 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', undef);
517 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
518 $self->{type} = 'application/xml';
519 $self->{holdings_xpath} = '/marc:record';
528 if ($type eq 'opac') {
531 'http://www.w3.org/1999/xhtml',
534 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
537 } elsif ($type eq 'unapi-id') {
540 'http://www.w3.org/1999/xhtml',
543 { title => $id, class => "unapi-id" }
550 #----------------------------------------------------------
552 package OpenILS::WWW::SuperCat::Feed::html;
553 use base 'OpenILS::WWW::SuperCat::Feed::atom';
557 my $self = $class->SUPER::new;
558 $self->type('text/html');
562 our ($_parser, $_xslt, $xslt_file);
566 my $base = $self->base || '';
567 my $root = $self->root || '';
568 my $search = $self->search || '';
569 my $class = $self->class || '';
570 my $lib = $self->lib || '-';
574 $_parser ||= new XML::LibXML;
575 $_xslt ||= new XML::LibXSLT;
578 OpenSRF::Utils::SettingsClient
580 ->config_value( dirs => 'xsl' ).
583 # parse the MODS xslt ...
584 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
586 my $new_doc = $atom2html_xslt->transform(
588 base_dir => "'$root'",
590 searchTerms => "'$search'",
591 searchClass => "'$class'",
594 return $new_doc->toString(1);
598 package OpenILS::WWW::SuperCat::Feed::html::item;
599 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
601 #----------------------------------------------------------
603 package OpenILS::WWW::SuperCat::Feed::htmlcard;
604 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
608 my $self = $class->SUPER::new;
609 $self->type('text/html');
610 $self->{xsl} = "/MARC21slim2HTMLCard.xsl";
614 our ($_parser, $_xslt, $xslt_file);
618 my $base = $self->base || '';
619 my $root = $self->root || '';
620 my $search = $self->search || '';
621 my $sort = $self->Sort || '';
622 my $sort_dir = $self->SortDir || '';
623 my $lang = $self->lang || '';
624 my $lib = $self->lib || '-';
628 $_parser ||= new XML::LibXML;
629 $_xslt ||= new XML::LibXSLT;
632 OpenSRF::Utils::SettingsClient
634 ->config_value( dirs => 'xsl' ).$self->{xsl};
636 # parse the MODS xslt ...
637 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
639 my $new_doc = $atom2html_xslt->transform(
641 base_dir => "'$root'",
643 searchTerms => "'$search'",
644 searchSort => "'$sort'",
645 searchSortDir => "'$sort_dir'",
646 searchLang => "'$lang'",
649 return $new_doc->toString(1);
652 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
653 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
655 package OpenILS::WWW::SuperCat::Feed::htmlholdings;
656 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard';
660 my $self = $class->SUPER::new;
661 $self->{xsl} = "/MARC21slim2HTMLCard-holdings.xsl";
665 package OpenILS::WWW::SuperCat::Feed::htmlholdings::item;
666 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard::item';