#!/usr/bin/perl use strict; #use warnings; use utf8; use POSIX qw.locale_h.; use OpenDict; use OpenDict::WebUtils; use DBI; setlocale(LC_CTYPE, "pt_PT.utf8"); setlocale(LC_COLLATE, "pt_PT.utf8"); use locale; use CGI qw.:standard.; my $pathinfo = $ENV{PATH_INFO}; our $ROOT = $ENV{DOCUMENT_ROOT}; if ($pathinfo) { for ($pathinfo) { s!^\/!!; s!/.*$!!; } } binmode(STDOUT, ":utf8"); xml_search($pathinfo); sub go404 { print header(-status => '404 Not Found'); exit; } sub xml_search { my $pathinfo = shift; Encode::_utf8_on($pathinfo); my $list = 0; my $dict = OpenDict->connect( db => "$ROOT/xml.db"); if (param("prefix") || param("suffix")) { my $query = '%'; $query = param("prefix").$query if param("prefix"); $query = $query.param("suffix") if param("suffix"); my $sth = $dict->dbh->prepare(sprintf('SELECT word FROM entry WHERE word LIKE "%s" ORDER BY normalized', $query)); $sth->execute; my @row; my $i = 0; print header(-type => "text/xml", -charset => "utf-8"); print "\n"; print "\n"; while ($i < 10 && (@row = $sth->fetchrow_array)) { $i++; print "$row[0]\n"; } print "\n"; } elsif (param("like")) { my $combinacoes = $dict->generateNearMisses(param("like"), 1); print header(-type => "text/xml", -charset => "utf-8"); print "\n"; print "\n"; for my $word (sort {$a <=> $b} @$combinacoes) { print "$word\n"; } print "\n"; } elsif ($pathinfo) { OpenDict::WebUtils::log_query($ROOT, $pathinfo); my $sth = $dict->dbh->prepare("SELECT xml FROM entry WHERE word = ?"); $sth->execute($pathinfo); my @row; my @xmls; while (@row = $sth->fetchrow_array) { push @xmls, $row[0] } if (@xmls) { print header(-type => "text/xml", -charset => "utf-8"); print "\n"; print "\n"; if ($#xmls) { print "\n"; print join("\n", @xmls); print "\n"; } else { print $xmls[0]; } print "\n"; } else { go404; } } else { go404; } }