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/atom+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/atom+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/rss+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/rss+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->{type} = 'application/xml';
518 $self->{holdings_xpath} = '/*[local-name()="record"]';
527 if ($type eq 'opac') {
529 '*[local-name()="record"]',
530 'http://www.w3.org/1999/xhtml',
533 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
536 } elsif ($type eq 'unapi-id') {
538 '*[local-name()="record"]',
539 'http://www.w3.org/1999/xhtml',
542 { title => $id, class => "unapi-id" }
549 #----------------------------------------------------------
551 package OpenILS::WWW::SuperCat::Feed::html;
552 use base 'OpenILS::WWW::SuperCat::Feed::atom';
556 my $self = $class->SUPER::new;
557 $self->type('text/html');
561 our ($_parser, $_xslt, $xslt_file);
565 my $base = $self->base || '';
566 my $root = $self->root || '';
567 my $search = $self->search || '';
568 my $class = $self->class || '';
569 my $lib = $self->lib || '-';
573 $_parser ||= new XML::LibXML;
574 $_xslt ||= new XML::LibXSLT;
577 OpenSRF::Utils::SettingsClient
579 ->config_value( dirs => 'xsl' ).
582 # parse the MODS xslt ...
583 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
585 my $new_doc = $atom2html_xslt->transform(
587 base_dir => "'$root'",
589 searchTerms => "'$search'",
590 searchClass => "'$class'",
593 return $new_doc->toString(1);
597 package OpenILS::WWW::SuperCat::Feed::html::item;
598 use base 'OpenILS::WWW::SuperCat::Feed::atom::item';
600 #----------------------------------------------------------
602 package OpenILS::WWW::SuperCat::Feed::htmlcard;
603 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
607 my $self = $class->SUPER::new;
608 $self->type('text/html');
609 $self->{xsl} = "/MARC21slim2HTMLCard.xsl";
613 our ($_parser, $_xslt, $xslt_file);
617 my $base = $self->base || '';
618 my $root = $self->root || '';
619 my $search = $self->search || '';
620 my $sort = $self->Sort || '';
621 my $sort_dir = $self->SortDir || '';
622 my $lang = $self->lang || '';
623 my $lib = $self->lib || '-';
627 $_parser ||= new XML::LibXML;
628 $_xslt ||= new XML::LibXSLT;
631 OpenSRF::Utils::SettingsClient
633 ->config_value( dirs => 'xsl' ).$self->{xsl};
635 # parse the MODS xslt ...
636 my $atom2html_xslt = $_xslt->parse_stylesheet( $_parser->parse_file($xslt_file) );
638 my $new_doc = $atom2html_xslt->transform(
640 base_dir => "'$root'",
642 searchTerms => "'$search'",
643 searchSort => "'$sort'",
644 searchSortDir => "'$sort_dir'",
645 searchLang => "'$lang'",
648 return $new_doc->toString(1);
651 package OpenILS::WWW::SuperCat::Feed::htmlcard::item;
652 use base 'OpenILS::WWW::SuperCat::Feed::marcxml::item';
654 package OpenILS::WWW::SuperCat::Feed::htmlholdings;
655 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard';
659 my $self = $class->SUPER::new;
660 $self->{xsl} = "/MARC21slim2HTMLCard-holdings.xsl";
664 package OpenILS::WWW::SuperCat::Feed::htmlholdings::item;
665 use base 'OpenILS::WWW::SuperCat::Feed::htmlcard::item';