#!/home/eric/bin/perl # mylibrary2oai.pl - export the contents of a MyLibrary database to sets of OAIXML files # Eric Lease Morgan # June 24, 2007 - added full dublin core to the output # June 23, 2007 - first investigations; based on CRRI use constant DIRECTORY => '/home/eric/apache/htdocs/oai/data/'; use MyLibrary::Core; use strict; # get all facets and loop through them my @facets = MyLibrary::Facet->get_facets( sort => 'name' ); foreach my $facet ( @facets ) { # get all terms associated with this facet, and loop through them foreach my $term_id ( $facet->related_terms( sort => 'name' )) { # create a term directory my $term = MyLibrary::Term->new( id => $term_id ); my $term_directory = DIRECTORY . &directify( $term->term_name ) . '/'; print $term->term_name . " ($term_directory)\n"; if ( ! -e $term_directory ) { mkdir $term_directory or die "Can't create $term_directory ($!)\n" } # get all the resources with this term, and loop through them foreach my $resource_id ( $term->related_resources( sort => 'name' )) { # get the required data my $resource = MyLibrary::Resource->new( id => $resource_id ); my $content; if ( $resource->contributor ) { $content .= '' . &escape( $resource->contributor ) . '' } if ( $resource->coverage ) { $content .= '' . &escape( $resource->coverage ) . '' } if ( $resource->creator ) { $content .= '' . &escape( $resource->creator ) . '' } if ( $resource->date ) { $content .= '' . &escape( $resource->date ) . '' } if ( $resource->note ) { $content .= '' . &escape( $resource->note ) . '' } if ( $resource->format ) { $content .= '' . &escape( $resource->format ) . '' } if ( $resource->fkey ) { $content .= '' . &escape( $resource->fkey ) . '' } foreach my $location ( $resource->resource_locations ) { $content .= '' . &escape( $location->location ) . '' } if ( $resource->language ) { $content .= '' . &escape( $resource->language ) . '' } if ( $resource->publisher ) { $content .= '' . &escape( $resource->publisher ) . '' } if ( $resource->relation ) { $content .= '' . &escape( $resource->relation ) . '' } if ( $resource->rights ) { $content .= '' . &escape( $resource->rights ) . '' } if ( $resource->source ) { $content .= '' . &escape( $resource->source ) . '' } if ( $resource->rights ) { $content .= '' . &escape( $resource->rights ) . '' } if ( $resource->subject ) { $content .= '' . &escape( $resource->subject ) . '' } if ( $resource->name ) { $content .= '' . &escape( $resource->name ) . '' } if ( $resource->type ) { $content .= '' . &escape( $resource->type ) . '' } my $filename = 'mylibrary-' . $resource->id . '.xml'; print "\t\t" . $resource->name . " ($filename)\n"; # open, write, and close an oaixml file open OAI, " > $term_directory$filename" or die "Can't open $filename ($!)\n"; print OAI < $content EOR close OAI; } } } sub directify { my $s = shift; $s = lc( $s ); while ( $s =~ / / ) { $s =~ s/ /_/g } return $s; } sub escape { my $s = shift; $s =~ s/&/&/g; $s =~ s//>/g; return $s; }