6 use CGI::Carp qw(fatalsToBrowser);
8 use WebService::Solr::Query;
15 use HTML::Template::Pro;
20 # Copyright 2013 yukikaze
21 # Licensed under the Apache License, Version 2.0 (the "License");
22 # you may not use this file except in compliance with the License.
23 # You may obtain a copy of the License at
25 # http://www.apache.org/licenses/LICENSE-2.0
27 # Unless required by applicable law or agreed to in writing, software
28 # distributed under the License is distributed on an "AS IS" BASIS,
29 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 # See the License for the specific language governing permissions and
31 # limitations under the License.
33 my $confdata = YAML::XS::LoadFile( -e 'libre10.conf' ? 'libre10.conf' : '/etc/libre10.conf' );
35 my $sqlpath = "/tmp/libre10.db";
36 $sqlpath = $confdata->{dburl};
38 DBI->connect( "dbi:SQLite:dbname=$sqlpath", "", "",
39 { RaiseError => 1, AutoCommit => 1 } );
42 my $search_text = $q->param("search_text");
43 $mode = $q->param("mode");
44 $width = $q->param("width");
45 $height = $q->param("height");
46 $start = $q->param("start");
47 $rows = $q->param("rows");
48 if ( $start eq "" ) { $start = "0"; }
49 if ( $rows eq "" ) { $rows = "10"; }
51 my $url = $confdata->{solrurl};
52 my $solr = WebService::Solr->new($url);
53 my $template = HTML::Template::Pro->new(
54 filename => "./template/desktop_top.tmpl",
55 die_on_bad_params => 0
57 print $q->header( -type => "text/html", -charset => "utf-8" );
59 $template->param( mode => $mode );
60 $template->param( width => $width );
61 $template->param( height => $height );
62 $template->param( start => $start );
63 $template->param( rows => $rows );
65 $bstart = int($start) - int($rows);
66 $nstart = int($start) + int($rows);
71 "pdfsearch.pl?mode=$mode&search_text=$search_text&width=$width&height=$height&start=$bstart&rows=$rows";
73 "pdfsearch.pl?mode=$mode&search_text=$search_text&width=$width&height=$height&start=$nstart&rows=$rows";
74 $template->param( blink => $blink );
75 $template->param( nlink => $nlink );
76 if ( length $search_text > 0 ) {
77 $template->param( text => $search_text );
78 $template->param( show_result => "true" );
79 $template->param( search_top => "0" );
80 my $query = WebService::Solr::Query->new(
81 { text => decode( 'utf-8', $search_text ) } );
82 my $response = $solr->search(
87 'group.limit' => '30',
88 'group.field' => 'title_group',
92 'hl.simple.pre' => "<font color=red><em>",
93 'hl.simple.post' => "</em></font>",
94 'hl.fragsize' => '300',
95 'hl.fragmenter' => 'gap',
96 'hl.alternateField' => 'text',
97 'hl.maxAlternateFieldLength' => '300',
98 'hl.useFastVectorHighlighter' => true,
102 my $hits = $response->content;
104 my $response_header = $hits{grouped}{title_group}{groups};
105 my @response_header = @$response_header;
106 my @print_group_arrays = ();
107 foreach my $group (@response_header) {
108 my $textarray = $group->{doclist}{docs};
109 my @textarray = @$textarray;
110 my %print_group_array = &printGroupResults(
111 $group->{doclist}{numFound},
112 $textarray[0]->{title_group},
113 $search_text, $textarray, $response->content->{highlighting}
115 push( @print_group_arrays, \%print_group_array );
117 $template->param( group => \@print_group_arrays );
120 $template->param( search_top => true );
123 sub printGroupResults() {
124 ( $hitsnum, $title, $search_text, $hits, $hits_hl ) = @_;
126 my @print_page_arrays = ();
127 $template->param( show_result => 1 );
128 foreach $doc (@$hits) {
129 ( $pagenum, $path, $linkpath ) =
130 ( $doc->{page}, $doc->{path_id}, $doc->{path_id} );
132 "SELECT startpage,title_id from pdffile WHERE id = '$path';";
133 my $sth = $sqldbi->prepare($findPageSQL);
135 $dbarray = $sth->fetchrow_arrayref;
136 $showpagenum = $pagenum + $dbarray->[0];
139 . uri_escape( $dbarray->[1] )
140 . "&pagenum=$showpagenum&mode=$mode&width=$width&height=$height";
141 my %print_page_array = (
142 page => $showpagenum,
143 hl_text => $hits_hl->{ $doc->{id} }{text}[0],
146 @print_page_array = ($print_page_array);
147 push( @print_page_arrays, \%print_page_array );
149 %print_group_array = (
151 search_num => $hitsnum,
152 search_result => \@print_page_arrays
154 return %print_group_array;
156 print $template->output();
157 $sqldbi . disconnect;