| 1 | # Copyright 2005-2013, Paul Johnson (paul@pjcj.net) | ||
| 2 | |||
| 3 | require 5.006; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | our $AUTOLOAD; | ||
| 9 | our $VERSION = "1.20"; | ||
| 10 | |||
| 11 | package Gedcom::WebServices; | ||
| 12 | |||
| 13 | use Gedcom 1.20; | ||
| 14 | |||
| 15 | use Apache::Constants qw( OK DECLINED ); | ||
| 16 | use Apache::Request; | ||
| 17 | use Apache::URI; | ||
| 18 | |||
| 19 | sub _new { | ||
| 20 | my $class = shift; | ||
| 21 | my $r = shift; | ||
| 22 | |||
| 23 | my $self = { | ||
| 24 | r => $r ? Apache::Request->instance($r) : $r, | ||
| 25 | @_ | ||
| 26 | }; | ||
| 27 | bless $self, $class | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _set_handlers { | ||
| 31 | my ($handlers) = @_; | ||
| 32 | |||
| 33 | my $handler_text; | ||
| 34 | |||
| 35 | for my $h (@{$handlers}) { | ||
| 36 | # print STDERR "creating $type handler for $h\n"; | ||
| 37 | $handler_text .= _create_handler($h); | ||
| 38 | } | ||
| 39 | |||
| 40 | # print STDERR "handler_text is [$handler_text]\n"; | ||
| 41 | $handler_text | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _create_handler { | ||
| 45 | my ($h) = @_; | ||
| 46 | my $handler = "___$h"; | ||
| 47 | unless (defined &$handler) { | ||
| 48 | no strict "refs"; | ||
| 49 | *$handler = sub ($$) { | ||
| 50 | my ($class, $r) = @_; | ||
| 51 | my $self; | ||
| 52 | my $vars; | ||
| 53 | eval { | ||
| 54 | my $hn = "__$h"; | ||
| 55 | $self = $class->_new($r, handler => $hn); | ||
| 56 | $self->$hn; | ||
| 57 | }; | ||
| 58 | # die Template::Exception->new("Retemps.perl", $@); | ||
| 59 | # die $@ if $@; | ||
| 60 | $self->_error($@) if $@; | ||
| 61 | exists $self->{_status} ? $self->{_status} : OK | ||
| 62 | }; | ||
| 63 | } | ||
| 64 | my $l = <<"EOE"; | ||
| 65 | \$Location{"/$h"} = { | ||
| 66 | SetHandler => "perl-script", | ||
| 67 | PerlHandler => "Gedcom::WebServices->___$h", | ||
| 68 | }; | ||
| 69 | EOE | ||
| 70 | $l | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _parse_uri { | ||
| 74 | my $r = shift; | ||
| 75 | my $uri = $r->parsed_uri; | ||
| 76 | my $path = $uri->path; | ||
| 77 | # print STDERR "parse path $path\n"; | ||
| 78 | if ($path =~ s!^/ws/(plain|json|xml)/!!) { | ||
| 79 | $r->notes(PATH => $path); | ||
| 80 | # print STDERR "parse new path [$1]\n"; | ||
| 81 | $r->uri("/$1"); | ||
| 82 | } | ||
| 83 | DECLINED | ||
| 84 | } | ||
| 85 | |||
| 86 | sub _error { | ||
| 87 | my $self = shift; | ||
| 88 | my ($msg) = @_; | ||
| 89 | print $msg; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _process { | ||
| 93 | my $self = shift; | ||
| 94 | my ($type) = @_; | ||
| 95 | |||
| 96 | my $path = $self->{r}->notes("path"); | ||
| 97 | # print STDERR "$type [$path]\n"; | ||
| 98 | |||
| 99 | my @params = split "/", $path; | ||
| 100 | |||
| 101 | my $file = shift @params or die "No GEDCOM file specified\n"; | ||
| 102 | my $gedcom_file = "$Gedcom::ROOT/$file.ged"; | ||
| 103 | # print STDERR "gedcom_file $gedcom_file\n"; | ||
| 104 | |||
| 105 | my $ged = $self->{ged} = Gedcom->new(gedcom_file => $gedcom_file, | ||
| 106 | read_only => 1); | ||
| 107 | die "Can't open gedcom file [$gedcom_file]\n" unless $ged; | ||
| 108 | |||
| 109 | # print STDERR "params @params\n"; | ||
| 110 | |||
| 111 | my @ret; | ||
| 112 | if (@params) { | ||
| 113 | my $xref = shift @params; | ||
| 114 | my $rec = $ged->resolve_xref($xref) || $ged->resolve_xref(uc $xref) || | ||
| 115 | die "Can't get record [$xref]\n"; | ||
| 116 | |||
| 117 | if (@params) { | ||
| 118 | my ($action, @parms) = @params; | ||
| 119 | die "Invalid action [$action]\n" unless $rec->can($action); | ||
| 120 | |||
| 121 | if ($Gedcom::Funcs{lc $action} && @parms) { | ||
| 122 | # print STDERR "Calling get_value(@params)\n"; | ||
| 123 | @ret = $rec->get_value(@params); | ||
| 124 | } else { | ||
| 125 | # print STDERR "Calling $action(@params)\n"; | ||
| 126 | @ret = $rec->$action(@parms); | ||
| 127 | } | ||
| 128 | } else { | ||
| 129 | if ($type eq "plain") { | ||
| 130 | $rec->write(\*STDOUT); | ||
| 131 | } elsif ($type eq "xml") { | ||
| 132 | my $r = $rec->hash; | ||
| 133 | $rec->write_xml(\*STDOUT); | ||
| 134 | } elsif ($type eq "json") { | ||
| 135 | my $r = $rec->hash; | ||
| 136 | # use DDS; print STDERR Dump $r; | ||
| 137 | print JSON->new->objToJson({ rec => $r }); | ||
| 138 | } else { | ||
| 139 | die "unrecognised type: $type"; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } elsif (my $search = $self->{r}->param("search")) { | ||
| 143 | @ret = $ged->get_individual($search); | ||
| 144 | } else { | ||
| 145 | die "No xref or parameters specified\n"; | ||
| 146 | } | ||
| 147 | |||
| 148 | # print @ret . "\n"; | ||
| 149 | # use Data::Dumper; print Dumper \@ret; | ||
| 150 | for (@ret) { | ||
| 151 | if (ref) { | ||
| 152 | if (defined $_->{xref}) { | ||
| 153 | print "/ws/$type/$file/", $_->xref, "\n"; | ||
| 154 | } else { | ||
| 155 | if ($type eq "plain") { | ||
| 156 | $_->write(\*STDOUT, scalar @params); | ||
| 157 | } elsif ($type eq "xml") { | ||
| 158 | $_->write_xml(\*STDOUT); | ||
| 159 | } elsif ($type eq "json") { | ||
| 160 | my $r = $_->hash; | ||
| 161 | # use DDS; print STDERR Dump $r; | ||
| 162 | print JSON->new->objToJson($r); | ||
| 163 | } else { | ||
| 164 | die "unrecognised type: $type"; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } else { | ||
| 168 | my $result = @params ? $params[-1] : "result"; | ||
| 169 | if ($type eq "plain") { | ||
| 170 | print "$_\n"; | ||
| 171 | } elsif ($type eq "xml") { | ||
| 172 | $result = uc $result; | ||
| 173 | print "<$result>$_</$result>\n"; | ||
| 174 | } elsif ($type eq "json") { | ||
| 175 | print JSON->new->objToJson({ $result => $_ }); | ||
| 176 | } else { | ||
| 177 | die "unrecognised type: $type"; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | print "\n" unless @ret; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub __plain { | ||
| 185 | my $self = shift; | ||
| 186 | $self->_process("plain"); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub __xml { | ||
| 190 | my $self = shift; | ||
| 191 | $self->_process("xml"); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub __json { | ||
| 195 | my $self = shift; | ||
| 196 | require JSON; | ||
| 197 | $self->_process("json"); | ||
| 198 | } | ||
| 199 | |||
| 200 | 1; | ||
| 201 | |||
| 202 | __END__ |