| 1 |
|
| ##-*- Mode: CPerl; coding: utf-8; -*- |
| 2 |
|
| |
| 3 |
|
| package DTA::CAB::Client::CGIWrapper; |
| 4 |
|
| |
| 5 |
|
| use DTA::CAB::Client; |
| 6 |
|
| use DTA::CAB::Client::XmlRpc; |
| 7 |
|
| use DTA::CAB::Format; |
| 8 |
|
| use DTA::CAB::Format::Builtin; |
| 9 |
|
| use DTA::CAB::Datum ':all'; |
| 10 |
|
| |
| 11 |
|
| use CGI qw(:standard :cgi-lib); |
| 12 |
|
| #use LWP::UserAgent; |
| 13 |
|
| use HTTP::Status; |
| 14 |
|
| use HTTP::Response; |
| 15 |
|
| |
| 16 |
|
| |
| 17 |
|
| use URI::Escape; |
| 18 |
|
| use Encode qw(encode decode); |
| 19 |
|
| use File::Basename qw(basename); |
| 20 |
|
| use utf8; |
| 21 |
|
| use Data::Dumper; |
| 22 |
|
| use strict; |
| 23 |
|
| |
| 24 |
|
| ##============================================================================== |
| 25 |
|
| ## Globals |
| 26 |
|
| ##============================================================================== |
| 27 |
|
| our @ISA = qw(DTA::CAB::Client::XmlRpc); |
| 28 |
|
| |
| 29 |
|
| ##============================================================================== |
| 30 |
|
| ## Constructors etc. |
| 31 |
|
| ##============================================================================== |
| 32 |
|
| |
| 33 |
|
| ## $obj = $CLASS_OR_OBJECT->new(%args) |
| 34 |
|
| ## + %args, %$obj: |
| 35 |
|
| ## ## |
| 36 |
|
| ## ##-- NEW |
| 37 |
|
| ## sysid => "$id str" ##-- default __PACKAGE__ . " v$DTA::CAB::VERSION" |
| 38 |
|
| ## analyzers => \@analyzers, ##-- supported analyzers (undef to query) |
| 39 |
|
| ## fmts => [{class=>$class,label=>$label,level=>$level},...], ##-- supported output formats |
| 40 |
|
| ## defaultFmt => $name, ##-- default format key |
| 41 |
|
| ## autoClean => $bool, ##-- always set 'doAnalyzeClean=1' upstream analysis option |
| 42 |
|
| ## debug => $bool, ##-- default to debug mode? |
| 43 |
|
| ## ## |
| 44 |
|
| ## ##-- INHERITED from DTA::CAB::Client::XmlRpc |
| 45 |
|
| ## serverURL => $url, ##-- default: localhost:8000 |
| 46 |
|
| ## serverEncoding => $encoding, ##-- default: UTF-8 |
| 47 |
|
| ## timeout => $timeout, ##-- timeout in seconds, default: 300 (5 minutes) |
| 48 |
|
| ## xcli => $xcli, ##-- RPC::XML::Client object |
| 49 |
|
| sub new { |
| 50 |
|
| my $that = shift; |
| 51 |
|
| my $wr = $that->SUPER::new( |
| 52 |
|
| ##-- identity |
| 53 |
|
| sysid => (__PACKAGE__ . " v$DTA::CAB::VERSION"), |
| 54 |
|
| analyzers => [qw(dta.cab.norm dta.cab.expand)], |
| 55 |
|
| autoClean => 0, |
| 56 |
|
| debug => 0, |
| 57 |
|
| |
| 58 |
|
| fmts => [ |
| 59 |
|
| {key=>'csv', class=>'DTA::CAB::Format::CSV', label=>'CSV'}, |
| 60 |
|
| {key=>'json', class=>'DTA::CAB::Format::JSON', label=>'JSON', level=>1}, |
| 61 |
|
| {key=>'perl', class=>'DTA::CAB::Format::Perl', label=>'Perl', level=>2}, |
| 62 |
|
| {key=>'text', class=>'DTA::CAB::Format::Text', label=>'Text'}, |
| 63 |
|
| {key=>'tt', class=>'DTA::CAB::Format::TT', label=>'TT'}, |
| 64 |
|
| {key=>'xml', class=>'DTA::CAB::Format::XmlNative', label=>'XML (Native)', level=>0}, |
| 65 |
|
| {key=>'xmlperl', class=>'DTA::CAB::Format::XmlPerl', label=>'XML (Perl)', level=>0}, |
| 66 |
|
| {key=>'xmlrpc', class=>'DTA::CAB::Format::XmlRpc', label=>'XML-RPC', level=>0}, |
| 67 |
|
| {key=>'yaml', class=>'DTA::CAB::Format::YAML', label=>'YAML', level=>0}, |
| 68 |
|
| ], |
| 69 |
|
| defaultFmt => 'text', |
| 70 |
|
| |
| 71 |
|
| ##-- user args |
| 72 |
|
| @_, |
| 73 |
|
| ); |
| 74 |
|
| |
| 75 |
|
| ##-- update formats |
| 76 |
|
| foreach (@{$wr->{fmts}}) { |
| 77 |
|
| $_->{key} = $_->{class} if (!defined($_->{key})); |
| 78 |
|
| } |
| 79 |
|
| $wr->{fmtsh} = { map {($_->{key}=>$_)} @{$wr->{fmts}} }; |
| 80 |
|
| |
| 81 |
|
| return $wr; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
|
| ##============================================================================== |
| 85 |
|
| ## Methods: top-level |
| 86 |
|
| |
| 87 |
|
| ## undef = $wr->run(\*STDIN) |
| 88 |
|
| ## undef = $wr->run("$cgi_src_str") |
| 89 |
|
| ## undef = $wr->run("$cgi_src_file") |
| 90 |
|
| ## undef = $wr->run(\%cgi_param_hash) |
| 91 |
|
| sub run { |
| 92 |
|
| my $wr = shift; |
| 93 |
|
| my $q = $wr->{q} = CGI->new(@_); |
| 94 |
|
| $q->charset('utf-8'); |
| 95 |
|
| my $qv = $wr->{qv} = $wr->cgiDecode(scalar($q->Vars)); |
| 96 |
|
| |
| 97 |
|
| my ($contentType,$content) = ('text/html',''); ##-- default content type |
| 98 |
|
| my ($rdoc,$ofkey,$ofdat,$of); |
| 99 |
|
| |
| 100 |
|
| ##-- get content |
| 101 |
|
| if (defined($qv->{q}) && $qv->{q} ne '') { |
| 102 |
|
| ##-- query given: fetch the results |
| 103 |
|
| $rdoc = $wr->fetchResults() |
| 104 |
|
| || return $wr->http_error(); |
| 105 |
|
| #print $q->pre(Data::Dumper->Dump([$rdoc],['rdoc'])); ##-- debug |
| 106 |
|
| |
| 107 |
|
| ##-- format |
| 108 |
|
| $ofkey = $qv->{format} || $wr->{defaultFmt}; |
| 109 |
|
| $ofdat = $wr->{fmtsh}{$ofkey}; |
| 110 |
|
| return $wr->http_error("unknown format: $ofkey") if (!$ofdat); |
| 111 |
|
| $of = $ofdat->{class}->new(level=>$ofdat->{level},encoding=>'UTF-8'); |
| 112 |
|
| $content = $of->putDocumentRaw($rdoc)->toString; |
| 113 |
|
| $content = decode('utf8',$content) if (!utf8::is_utf8($content)); |
| 114 |
|
| $contentType = $of->mimeType; |
| 115 |
|
| |
| 116 |
|
| if ($contentType ne 'text/html') { |
| 117 |
|
| print |
| 118 |
|
| ($q->header(-type=>($contentType =~ /\/(?:xml|plain)$/ ? $contentType : 'text/plain'), |
| 119 |
|
| -status=>'200 OK', |
| 120 |
|
| -charset => 'utf-8'), |
| 121 |
|
| $content); |
| 122 |
|
| return $wr->finish(); |
| 123 |
|
| } |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
|
| ##-- no query: just print html form |
| 127 |
|
| print |
| 128 |
|
| ($wr->html_header, |
| 129 |
|
| $wr->html_qform, |
| 130 |
|
| #$wr->html_vars, ##-- debug |
| 131 |
|
| ($content ne '' |
| 132 |
|
| ? $q->div({id=>'section'}, |
| 133 |
|
| $q->h2("Results (", $q->tt($ofdat->{class}), ")"), |
| 134 |
|
| $content |
| 135 |
|
| ) |
| 136 |
|
| : ''), |
| 137 |
|
| $wr->html_footer, |
| 138 |
|
| ); |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
|
| ## undef = $wr->finish() |
| 142 |
|
| ## + cleanup after ourselves |
| 143 |
|
| sub finish { |
| 144 |
|
| my $wr = shift; |
| 145 |
|
| $wr->disconnect; |
| 146 |
|
| delete @$wr{qw(q qv error)}; |
| 147 |
|
| return; |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| ##============================================================================== |
| 151 |
|
| ## Methods: Generic |
| 152 |
|
| |
| 153 |
|
| ## \%vars = $wr->cgiDecode(\%vars) |
| 154 |
|
| ## + decodes cgi params in \%vars |
| 155 |
|
| sub cgiDecode { |
| 156 |
|
| my ($wr,$qv) = @_; |
| 157 |
|
| ##-- auto-decode some vars |
| 158 |
|
| my @u8keys = qw(q); |
| 159 |
|
| my ($vr); |
| 160 |
|
| foreach (grep {exists $qv->{$_}} @u8keys) { |
| 161 |
|
| $vr = \$qv->{$_}; |
| 162 |
|
| foreach (ref($$vr) ? @{$$vr} : $$vr) { |
| 163 |
|
| $_ = decode('utf8',$_) if (!utf8::is_utf8($_)); |
| 164 |
|
| } |
| 165 |
|
| } |
| 166 |
|
| return $qv; |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| ## $rdoc_or_undef = $wr->fetchResults() |
| 170 |
|
| ## $rdoc_or_undef = $wr->fetchResults($analyzer,$qdoc,\%qopts) |
| 171 |
|
| ## + calls Client::XmlRpc::analyzeDocument($analyzer,$qdoc,\%qopts) |
| 172 |
|
| sub fetchResults { |
| 173 |
|
| my ($wr,$qa,$qdoc,$qopts) = @_; |
| 174 |
|
| $qa = $wr->parseQueryAnalyzer() if (!$qa); |
| 175 |
|
| $qdoc = $wr->parseQueryDoc() if (!$qdoc); |
| 176 |
|
| $qopts = $wr->parseQueryOpts() if (!$qopts); |
| 177 |
|
| |
| 178 |
|
| #my $fmt = DTA::CAB::Format::JSON->new; |
| 179 |
|
| my $fmt = DTA::CAB::Format::YAML->new; |
| 180 |
|
| my $qstr = $fmt->putDocument($qdoc)->toString |
| 181 |
|
| or return $wr->set_error(ref($fmt)."::putDocument() failed"); |
| 182 |
|
| $qopts->{reader} = $qopts->{writer} = {class=>ref($fmt)}; |
| 183 |
|
| |
| 184 |
|
| $wr->connect() |
| 185 |
|
| or return $wr->set_error("connect() failed: $!"); |
| 186 |
|
| #my $rdoc = $wr->analyzeDocument($qa,$qdoc,$qopts) or return $wr->set_error("analyzeData() failed: $!"); |
| 187 |
|
| my $rstr = $wr->analyzeData($qa,$qstr,$qopts) |
| 188 |
|
| or return $wr->set_error("analyzeData() failed: $!"); |
| 189 |
|
| $wr->disconnect(); |
| 190 |
|
| |
| 191 |
|
| my $rdoc = $fmt->flush->fromString($rstr)->parseDocument() |
| 192 |
|
| or return $wr->set_error(ref($fmt)."::parseDocument() failed"); |
| 193 |
|
| |
| 194 |
|
| return $rdoc; |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
|
| ## $qopts = $wr->parseQueryAnalyzer() |
| 198 |
|
| ## + currently just returns 'dta.cab.default' |
| 199 |
|
| sub parseQueryAnalyzer { |
| 200 |
|
| return $_[0]{qv}{analyzer} if ($_[0]{qv} && $_[0]{qv}{analyzer}); |
| 201 |
|
| return 'dta.cab.default'; |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
|
| ## $qopts = $wr->parseQueryOpts(\%qvars) |
| 205 |
|
| ## $qopts = $wr->parseQueryOpts() |
| 206 |
|
| ## + currently just returns {} |
| 207 |
|
| sub parseQueryOpts { |
| 208 |
|
| my ($wr,$qv) = @_; |
| 209 |
|
| $qv = $wr->{qv} if (!$qv); |
| 210 |
|
| return { |
| 211 |
|
| ##-- autoClean |
| 212 |
|
| ($wr->{autoClean} || !$qv->{debug} ? (doAnalyzeClean=>1) : qw()), |
| 213 |
|
| |
| 214 |
|
| ##-- format |
| 215 |
|
| } |
| 216 |
|
| } |
| 217 |
|
| |
| 218 |
|
| |
| 219 |
|
| ## $qdoc_or_undef = $wr->parseQueryDoc(\%qvars) |
| 220 |
|
| ## $qdoc_or_undef = $wr->parseQueryDoc() |
| 221 |
|
| ## + on error sets $wr->{error} |
| 222 |
|
| sub parseQueryDoc { |
| 223 |
|
| my ($wr,$qv) = @_; |
| 224 |
|
| $qv = $wr->{qv} if (!$qv); |
| 225 |
|
| |
| 226 |
|
| ##-- parse query string to document |
| 227 |
|
| my $qs = $qv->{q}; |
| 228 |
|
| my ($qdoc); |
| 229 |
|
| |
| 230 |
|
| ##-- parse: as sentence |
| 231 |
|
| $qdoc = toDocument([toSentence([map {toToken($_)} |
| 232 |
|
| grep {defined($_)} |
| 233 |
|
| map {split(/\s+/,$_)} |
| 234 |
|
| (ref($qs) ? @$qs : ($qs)) |
| 235 |
|
| ]) |
| 236 |
|
| ]); |
| 237 |
|
| |
| 238 |
|
| ##-- return |
| 239 |
|
| return $wr->set_error('could not parse query string "$qs"') if (!$qdoc); |
| 240 |
|
| return $qdoc; |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
|
| ## undef = $wr->set_error(@err) |
| 244 |
|
| ## + also logs error |
| 245 |
|
| sub set_error { |
| 246 |
|
| my ($wr,@err) = @_; |
| 247 |
|
| $wr->error(@err); |
| 248 |
|
| $wr->{error} = join('', @err); |
| 249 |
|
| return undef; |
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
|
| ##============================================================================== |
| 253 |
|
| ## Methods: HTML |
| 254 |
|
| |
| 255 |
|
| ## @html = $wr->html_error() |
| 256 |
|
| ## + returns error string for $wr->{error} |
| 257 |
|
| sub html_error { |
| 258 |
|
| my $wr = shift; |
| 259 |
|
| return (span({style=>'font-size: large; color: #ff0000;'}, |
| 260 |
|
| b('Error: '), htmlesc($wr->{error} || '???')), |
| 261 |
|
| br, |
| 262 |
|
| ); |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
|
| ## @http = $wr->http_error() |
| 266 |
|
| ## @http = $wr->http_error(@msg) |
| 267 |
|
| ## + prints HTTP headers & error string for $wr->{error} |
| 268 |
|
| sub http_error { |
| 269 |
|
| my $wr = shift; |
| 270 |
|
| $wr->set_error(@_) if (@_); |
| 271 |
|
| print |
| 272 |
|
| (header(-type=>'text/html', |
| 273 |
|
| -charset=>'utf-8', |
| 274 |
|
| -status=>'500 Internal Server Error'), |
| 275 |
|
| start_html(-title => (__PACKAGE__ . " Error"), |
| 276 |
|
| -style=>{'src'=>'taxi.css'}, |
| 277 |
|
| ), |
| 278 |
|
| $wr->html_error, |
| 279 |
|
| ## |
| 280 |
|
| $wr->html_begin_content(), |
| 281 |
|
| $wr->html_qform, |
| 282 |
|
| $wr->html_footer, |
| 283 |
|
| end_html, |
| 284 |
|
| ); |
| 285 |
|
| return $wr->finish; |
| 286 |
|
| } |
| 287 |
|
| |
| 288 |
|
| |
| 289 |
|
| ## @html = $wr->html_header() |
| 290 |
|
| sub html_header { |
| 291 |
|
| my $wr = shift; |
| 292 |
|
| return |
| 293 |
|
| (header(-type=>'text/html', -charset=>'utf-8'), |
| 294 |
|
| start_html(-title=>'DTA::CAB Client Wrapper', |
| 295 |
|
| -style=>{'src'=>'taxi.css'}, |
| 296 |
|
| ), |
| 297 |
|
| $wr->html_begin_content(), |
| 298 |
|
| ); |
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
|
| ## @html = html_begin_content($h1_title_text) |
| 302 |
|
| sub html_begin_content { |
| 303 |
|
| my $wr = shift; |
| 304 |
|
| return |
| 305 |
|
| (openTag('div',{id=>'outer'}), |
| 306 |
|
| div({id=>'headers'}, h1(@_ ? @_ : 'DTA::CAB Client Wrapper')), |
| 307 |
|
| openTag('div',{id=>'content'}), |
| 308 |
|
| ); |
| 309 |
|
| } |
| 310 |
|
| |
| 311 |
|
| ## @html = $wr->html_footer() |
| 312 |
|
| sub html_footer { |
| 313 |
|
| my $wr = shift; |
| 314 |
|
| my $q = $wr->{q}; |
| 315 |
|
| return |
| 316 |
|
| ( |
| 317 |
|
| closeTag('div',{id=>'content'}), |
| 318 |
|
| '<div id="footers">', |
| 319 |
|
| "<tt>$wr->{sysid}</tt>", $q->br, |
| 320 |
|
| '<address><a href="mailto:moocow@cpan.org">moocow@cpan.org</a></address>', |
| 321 |
|
| '</div>', |
| 322 |
|
| closeTag('div',{id=>'outer'}), |
| 323 |
|
| $q->end_html, |
| 324 |
|
| ); |
| 325 |
|
| } |
| 326 |
|
| |
| 327 |
|
| ## @html = $wr->html_vars() |
| 328 |
|
| sub html_vars { |
| 329 |
|
| my $wr = shift; |
| 330 |
|
| local $Data::Dumper::Indent = 2; |
| 331 |
|
| return $wr->{q}->pre(Data::Dumper->Dump([$wr->{qv}],['vars'])); |
| 332 |
|
| } |
| 333 |
|
| |
| 334 |
|
| ## @html = $wr->html_qform() |
| 335 |
|
| sub html_qform { |
| 336 |
|
| my $wr = shift; |
| 337 |
|
| my $q = $wr->{q}; |
| 338 |
|
| |
| 339 |
|
| ##-- get analyzer list |
| 340 |
|
| if (!defined($wr->{analyzers})) { |
| 341 |
|
| my @a = $wr->analyzers(); |
| 342 |
|
| return $wr->set_error("could get analyzer list: ".$a[0]->value) if (!@a || ref($a[0])); |
| 343 |
|
| $wr->{analyzers} = \@a; |
| 344 |
|
| } |
| 345 |
|
| |
| 346 |
|
| return |
| 347 |
|
| ( |
| 348 |
|
| div({id=>'section'}, |
| 349 |
|
| #h2('Query'), |
| 350 |
|
| start_form(-method=>'GET', -id=>'queryForm'), |
| 351 |
|
| table({class=>'sep'}, |
| 352 |
|
| tbody( |
| 353 |
|
| Tr(td({id=>'searchLabel'}, "Query:"), |
| 354 |
|
| td({colspan=>3}, textfield(-name=>'q',-size=>64,-id=>'searchText')), |
| 355 |
|
| ), |
| 356 |
|
| ## |
| 357 |
|
| Tr(td({id=>'searchLabel'}, "Analyzer:"), |
| 358 |
|
| td(popup_menu({-name=>'analyzer', |
| 359 |
|
| -values=>[ @{$wr->{analyzers}} ], |
| 360 |
|
| -default=>($wr->{analyzers}[0] || 'dta.cab.default') |
| 361 |
|
| }))), |
| 362 |
|
| ## |
| 363 |
|
| Tr( |
| 364 |
|
| td({id=>'searchLabelE'}, "Format:"), |
| 365 |
|
| td(popup_menu({-name=>'format', |
| 366 |
|
| -values=>[ map {$_->{key}} @{$wr->{fmts}} ], |
| 367 |
|
| -default=>$wr->{defaultFmt}, |
| 368 |
|
| -labels => { map {($_->{key}=>$_->{label})} @{$wr->{fmts}} }, |
| 369 |
|
| }))), |
| 370 |
|
| ## |
| 371 |
|
| Tr( |
| 372 |
|
| td({id=>'searchLabelE'}, "Debug:"), |
| 373 |
|
| td(checkbox(-name=>'debug', |
| 374 |
|
| -checked=>($wr->{autoClean} || !$wr->{debug} ? 0 : 1), |
| 375 |
|
| -value=>1, |
| 376 |
|
| -label=>'', |
| 377 |
|
| ($wr->{autoClean} ? (-disabled=>1) : qw()))), |
| 378 |
|
| ), |
| 379 |
|
| ## |
| 380 |
|
| Tr(td(), |
| 381 |
|
| td(submit(-name=>'submit',-value=>'submit')), |
| 382 |
|
| ), |
| 383 |
|
| )), |
| 384 |
|
| end_form, |
| 385 |
|
| ), |
| 386 |
|
| ); |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
|
| ##============================================================================== |
| 390 |
|
| ## Functions: utils |
| 391 |
|
| |
| 392 |
|
| sub htmlesc { |
| 393 |
|
| my $str = shift; |
| 394 |
|
| $str =~ s/\&/&/g; |
| 395 |
|
| $str =~ s/\</</g; |
| 396 |
|
| $str =~ s/\>/>/g; |
| 397 |
|
| $str =~ s/\"/"/g; |
| 398 |
|
| $str =~ s/\'/'/g; |
| 399 |
|
| return $str; |
| 400 |
|
| } |
| 401 |
|
| sub openTag { |
| 402 |
|
| my ($name,$attrs) = @_; |
| 403 |
|
| return "<$name".join('', map {" $_=\"".htmlesc($attrs->{$_})."\""} sort keys %$attrs).">"; |
| 404 |
|
| } |
| 405 |
|
| sub closeTag { |
| 406 |
|
| my $name = shift; |
| 407 |
|
| return "</$name>"; |
| 408 |
|
| } |
| 409 |
|
| sub elt { |
| 410 |
|
| my ($name,$attrs,@content) = @_; |
| 411 |
|
| return (openTag($name,$attrs), @content, closeTag($name)); |
| 412 |
|
| } |
| 413 |
|
| sub mydiv { return elt('div',@_); } |
| 414 |
|
| sub mytable { return elt('table',@_); } |
| 415 |
|
| sub mytbody { return elt('tbody',@_); } |
| 416 |
|
| sub mytr { return elt('tr',@_); } |
| 417 |
|
| sub mytd { return elt('td',@_); } |
| 418 |
|
| sub myth { return elt('th',@_); } |
| 419 |
|
| sub myspan { return elt('span',@_); } |
| 420 |
|
| sub mya { return elt('a',@_); } |
| 421 |
|
| |
| 422 |
|
| 1; ##-- be happy |
| 423 |
|
| |
| 424 |
|
| __END__ |