6 use CGI::Carp qw(fatalsToBrowser);
8 use WebService::Solr::Query;
15 use HTML::Template::Pro;
18 # Libre10 web based pdf search engine
19 # Copyright (C) 2013 yukikaze
21 # This program is free software; you can redistribute it and/or modify it under
22 # the terms of the GNU General Public License as published by the Free Software
23 # Foundation; either version 3 of the License.
25 # This program is distributed in the hope that it will be useful, but WITHOUT
26 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
27 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License along with
30 # this program. If not, see <http://www.gnu.org/licenses/>.
33 my $confdata = YAML::XS::LoadFile(
34 -e 'libre10.conf' ? 'libre10.conf' : '/etc/libre10.conf' );
36 my $sqlpath = "/tmp/libre10.db";
37 $sqlpath = $confdata->{dburl};
39 DBI->connect( "dbi:SQLite:dbname=$sqlpath", "", "",
40 { RaiseError => 1, AutoCommit => 1 } );
43 my $search_text = $q->param("search_text");
44 $mode = $q->param("mode");
45 $width = $q->param("width");
46 $height = $q->param("height");
47 $start = $q->param("start");
48 $rows = $q->param("rows");
49 $fq_title_row = $q->param("fq_title");
50 $fq_genre_row = $q->param("fq_genre");
51 $fq_title = uri_unescape($fq_title_row);
53 #$fq_title = decode('utf-8',$fq_title);
54 $fq_genre = decode( 'utf-8', uri_unescape($fq_genre_row) );
55 if ( $start eq "" ) { $start = "0"; }
56 if ( $rows eq "" ) { $rows = "10"; }
58 my $url = $confdata->{solrurl};
59 my $solr = WebService::Solr->new($url);
61 print $q->header( -type => "text/html", -charset => "utf-8" );
63 if ( length $search_text > 0 ) {
64 my $query = WebService::Solr::Query->new(
65 { text => decode( 'utf-8', $search_text ) } );
66 my %response_query = (
69 'group.limit' => '30',
70 'group.field' => 'title_group',
74 'hl.simple.pre' => "<font color=red><em>",
75 'hl.simple.post' => "</em></font>",
76 'hl.fragsize' => '30',
77 'hl.fragmenter' => 'gap',
78 'hl.alternateField' => 'text',
79 'hl.maxAlternateFieldLength' => '30',
80 'hl.useFastVectorHighlighter' => true,
82 my $fq_title_solr = "";
83 if ( ( length $fq_title > 0 ) or ( length $fq_genre > 0 ) ) {
86 WHERE title_id = '$fq_title'
87 ORDER BY part DESC LIMIT 1";
88 $dbs = $sqldbi->prepare($sql);
90 $dbh = $dbs->fetchrow_hashref;
91 $fq_title_solr = decode( 'utf-8', $dbh->{'title'} );
96 #if ((length $fq_title > 0) and (length $fq_genre > 0)){
97 # $response_query{'facet'} = "true";
98 # @fqs = ('title_group:$fq_title_solr','genre:$fq_genre');
99 # $response_query{'fq'} = [@fqs];
100 if ( length $fq_title > 0 ) {
101 $response_query{'fq'} = "title_group:\"$fq_title_solr\"";
103 if ( length $fq_genre > 0 ) {
104 $response_query{'facet'} = "true";
105 @fqs = ( 'genre:' . $fq_genre, );
106 $response_query{'fq'} = [@fqs];
109 #print Dumper %response_query;
110 my $response = $solr->search( $query, \%response_query );
112 #print Dumper $response;
114 my $hits = $response->content;
116 my $response_header = $hits{grouped}{title_group}{groups};
117 my @response_header = @$response_header;
118 my @print_group_arrays = ();
119 foreach my $group (@response_header) {
120 my $textarray = $group->{doclist}{docs};
121 my @textarray = @$textarray;
122 my %print_group_array = &printGroupResults(
123 $group->{doclist}{numFound},
124 $textarray[0]->{title_group},
125 $search_text, $textarray, $response->content->{highlighting}
127 push( @print_group_arrays, \%print_group_array );
130 serverName => "kobe.rec10.org",
132 results => \@print_group_arrays
134 print XMLout( \%xml_group_array, RootName => "root" );
137 if ( $mode eq "speedtest" ) {
138 for ( $count = 0 ; $count < 10000 ; $count++ ) {
139 print "libre10 speedtest\n";
144 sub printGroupResults() {
145 ( $hitsnum, $title, $search_text, $hits, $hits_hl ) = @_;
147 my @print_page_arrays = ();
149 #$template->param( show_result => 1 );
150 foreach $doc (@$hits) {
151 ( $pagenum, $path, $linkpath ) =
152 ( $doc->{page}, $doc->{path_id}, $doc->{path_id} );
154 "SELECT startpage,title_id from pdffile WHERE id = '$path';";
155 my $sth = $sqldbi->prepare($findPageSQL);
157 $dbarray = $sth->fetchrow_arrayref;
158 $showpagenum = $pagenum + $dbarray->[0];
160 "htmlserver.pl?title="
161 . uri_escape( $dbarray->[1] )
162 . "&pagenum=$showpagenum&mode=$mode&width=$width&height=$height";
163 my %print_page_array = (
164 page => $showpagenum,
165 hl_text => $hits_hl->{ $doc->{id} }{text}[0],
168 @print_page_array = ($print_page_array);
169 push( @print_page_arrays, \%print_page_array );
171 %print_group_array = (
173 search_num => $hitsnum,
174 search_result => \@print_page_arrays
176 return %print_group_array;
179 #print $template->output();
180 $sqldbi . disconnect;