#!/usr/bin/perl # c4lj2doaj.cgi - query code4lib journal (wordpress) database and create doajaxml # Eric Lease Morgan # June 26, 2008 - tweaked user interface for code4lib site # May 28, 2008 - added startPage and publisherRecordId to XML template # May 24, 2008 - first cut # require use lib './lib'; use C4LJ::Article; use CGI; use CGI::Carp qw(fatalsToBrowser); use strict; # define use constant HOME => './etc/home.txt'; # initialize my $records = ''; my $cgi = CGI->new; my $issue = $cgi->param( 'issue' ); # (weak) sanity check if ( $issue !~ /\d+/ ) { print $cgi->header; print &slurp( HOME ); exit; } # process each article foreach my $article ( C4LJ::Article->get_articles( issue => $issue )) { # slurp up the template and process the macros my $record = &template; $record =~ s/##ID##/&escape_entities($article->id)/ge; $record =~ s/##TITLE##/&escape_entities($article->title)/e; $record =~ s/##ABSTRACT##/&escape_entities($article->abstract)/e; $record =~ s/##AUTHOR##/&escape_entities($article->author)/e; $record =~ s/##URL##/&escape_entities($article->url)/e; $record =~ s/##DATE##/&escape_entities($article->date)/e; $record =~ s/##ISSUE##/&escape_entities($article->issue)/e; # add the records to the list of records $records .= $record; } # done print $cgi->header(-type=>'text/xml', -charset => 'UTF-8'); print < $records XML exit; sub template { return < eng Code4Lib Code4Lib Journal 19405758 ##DATE## ##ISSUE## article ##ID## ##ID## ##TITLE## ##AUTHOR## ##ABSTRACT## ##URL## EOT } sub escape_entities { # get the input my $s = shift; # escape $s =~ s/&/&/g; $s =~ s//>/g; $s =~ s/"/"/g; $s =~ s/'/'/g; # done return $s; } sub slurp { # open a file named by the input and return its contents my $f = @_[0]; my $r; open (F, "< $f"); while () { $r .= $_ } close F; return $r; }