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->{doc}->documentElement->setNamespace('http://www.w3.org/2005/Atom', 'atom', 0);
295 $self->{item_xpath} = '/atom:entry';
296 $self->{holdings_xpath} = '/atom:entry';
297 $self->{type} = 'application/xml';
302 #----------------------------------------------------------
304 package OpenILS::WWW::SuperCat::Feed::rss2;
305 use base 'OpenILS::WWW::SuperCat::Feed';
309 my $self = $class->SUPER::build('<rss version="2.0"><channel/></rss>');
310 $self->{type} = 'application/xml';
311 $self->{item_xpath} = '/rss/channel';
318 $self->_create_node('/rss/channel',undef,'title', $text);
324 $self->_create_node($self->{item_xpath},undef,'lastBuildDate', $text);
330 $self->_create_node('/rss/channel', undef,'generator', $text);
337 my $mime = shift || "application/x-$type+xml";
339 $type = 'self' if ($type eq 'rss2');
352 package OpenILS::WWW::SuperCat::Feed::rss2::item;
353 use base 'OpenILS::WWW::SuperCat::Feed::rss2';
358 my $self = $class->SUPER::build($xml);
359 $self->{type} = 'application/xml';
360 $self->{item_xpath} = '/item';
361 $self->{holdings_xpath} = '/item';
368 $self->_create_node($self->{item_xpath},undef,'pubDate', $text);
373 #----------------------------------------------------------
375 package OpenILS::WWW::SuperCat::Feed::mods;
376 use base 'OpenILS::WWW::SuperCat::Feed';
380 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/"/>');
381 $self->{type} = 'application/xml';
382 $self->{item_xpath} = '/mods:modsCollection';
386 package OpenILS::WWW::SuperCat::Feed::mods::item;
387 use base 'OpenILS::WWW::SuperCat::Feed::mods';
392 my $self = $class->SUPER::build($xml);
393 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods');
394 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/', 'mods', 0);
395 $self->{type} = 'application/xml';
396 $self->{holdings_xpath} = '/mods:mods';
407 if ($type eq 'unapi' || $type eq 'opac') {
410 'http://www.loc.gov/mods/',
413 { type => 'otherFormat', id => 'link-'.$linkid }
416 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
417 'http://www.loc.gov/mods/',
418 'mods:recordIdentifier',
426 #----------------------------------------------------------
428 package OpenILS::WWW::SuperCat::Feed::mods3;
429 use base 'OpenILS::WWW::SuperCat::Feed::mods';
433 my $self = $class->SUPER::build('<mods:modsCollection version="3.0" xmlns:mods="http://www.loc.gov/mods/v3"/>');
434 $self->{type} = 'application/xml';
435 $self->{item_xpath} = '/mods:modsCollection';
439 package OpenILS::WWW::SuperCat::Feed::mods3::item;
440 use base 'OpenILS::WWW::SuperCat::Feed::mods::item';
445 my $self = $class->SUPER::build($xml);
446 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods');
447 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/mods/v3', 'mods', 0);
448 $self->{type} = 'application/xml';
449 $self->{holdings_xpath} = '/mods:mods';
458 if ($type eq 'unapi' || $type eq 'opac') {
461 'http://www.loc.gov/mods/v3',
464 { type => 'otherFormat', id => 'link-'.$linkid }
467 "mods:mods/mods:relatedItem[\@id='link-$linkid']",
468 'http://www.loc.gov/mods/v3',
469 'mods:recordIdentifier',
477 #----------------------------------------------------------
479 package OpenILS::WWW::SuperCat::Feed::marcxml;
480 use base 'OpenILS::WWW::SuperCat::Feed';
484 my $self = $class->SUPER::build('<marc:collection xmlns:marc="http://www.loc.gov/MARC21/slim"/>');
485 $self->{type} = 'application/xml';
486 $self->{item_xpath} = '/marc:collection';
494 if ($type eq 'unapi') {
497 'http://www.w3.org/1999/xhtml',
500 { rel => 'unapi-server', href => $id, title => "unapi" }
507 package OpenILS::WWW::SuperCat::Feed::marcxml::item;
508 use base 'OpenILS::WWW::SuperCat::Feed::marcxml';
513 my $self = $class->SUPER::build($xml);
514 return undef unless $self;
515 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc');
516 $self->{doc}->documentElement->setNamespace('http://www.loc.gov/MARC21/slim', 'marc', 0);
517 $self->{type} = 'application/xml';
518 $self->{holdings_xpath} = '/marc:record';
527 if ($type eq 'opac') {
530 'http://www.w3.org/1999/xhtml',
533 { rel => 'otherFormat', href => $id, title => "Dynamic Details" }
536 } elsif ($type eq 'unapi-id') {
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';