--- loncom/interface/spellcheck.pm 2012/08/21 10:31:52 1.1 +++ loncom/interface/spellcheck.pm 2012/08/27 11:09:56 1.2 @@ -1,7 +1,7 @@ # The LearningOnline Network # Printout # -# $Id: spellcheck.pm,v 1.1 2012/08/21 10:31:52 foxr Exp $ +# $Id: spellcheck.pm,v 1.2 2012/08/27 11:09:56 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -26,6 +26,8 @@ # # package Apache::spellcheck; +use CGI qw(:standard); +use Apache::Constants qw(:common); use strict; use Text::Aspell; @@ -79,7 +81,7 @@ sub spell_check { my @mis_spelled; foreach my $word (@word_list) { if (!$checker->check($word)) { - push (@mis_spelled $word); + push (@mis_spelled, $word); } } return &array_to_json(\@mis_spelled); @@ -98,7 +100,7 @@ sub suggest_spellings { my $checker = Text::Aspell->new; $checker->set_option('lang', $lang); - @suggestions = $checker->suggest($word); + my @suggestions = $checker->suggest($word); return &array_to_json(\@suggestions); } @@ -115,16 +117,25 @@ sub suggest_spellings { sub handler { my $r = shift; + my $raw_params; + + if ($r->method eq 'POST') { + $raw_params = $r->content(); + } else { + $raw_params = $r->args(); + } + my $query = CGI->new($raw_params); # Figure out the language defaulting to english. my $language = "en-US"; - if ($r->param{'lang'}) { - $language = $r->param{'lang'}; + if ($query->param('lang')) { + $language = $query->param('lang'); } # Regardless, response Content type: is application/json: - $h->header_out('Content-Type', 'application/json'); + $r->content_type( 'application/json'); + $r->send_http_header; # Whether we are suggesting or spell checking # depends on which of the suggest or text args are present: @@ -132,13 +143,17 @@ sub handler { my $data; - if ($h->args{'text'}) { - $data = &spell_check($h->args{'text'}, $language); - } eslif ($h->args{'suggest'}) { - $data = &suggest_spellings($h->args{'suggest'}, $language); + if ($query->param('text')) { + $data = &spell_check($query->param('text'), $language); + } elsif ($query->param('suggest')) { + $data = &suggest_spellings($query->param('suggest'), $language); } else { die "Invalid request"; } $r->print($data); + return OK; } + + 1; +__END__