OSDN Git Service

conf loader as one-liner
[libre10/libre10.git] / pdfsearch.pl
1 #!/usr/bin/perl
2 use utf8;
3 use Encode 'decode';
4 use Encode 'encode';
5 use CGI;
6 use CGI::Carp qw(fatalsToBrowser);
7 use WebService::Solr;
8 use WebService::Solr::Query;
9 use warnings;
10 use Data::Dumper;
11 use YAML::XS;
12 use DBD::SQLite;
13 use DBI;
14 use URI::Escape;
15 use HTML::Template::Pro;
16
17 #
18 #   Libre10
19 #
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
24 #
25 #       http://www.apache.org/licenses/LICENSE-2.0
26 #
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.
32 #
33 my $confdata = YAML::XS::LoadFile( -e 'libre10.conf' ? 'libre10.conf' : '/etc/libre10.conf' );
34
35 my $sqlpath = "/tmp/libre10.db";
36 $sqlpath = $confdata->{dburl};
37 $sqldbi =
38   DBI->connect( "dbi:SQLite:dbname=$sqlpath", "", "",
39         { RaiseError => 1, AutoCommit => 1 } );
40 my $q           = new CGI;
41 my $mode        = "";
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"; }
50
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
56 );
57 print $q->header( -type => "text/html", -charset => "utf-8" );
58
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 );
64
65 $bstart = int($start) - int($rows);
66 $nstart = int($start) + int($rows);
67 if ( $bstart < 0 ) {
68         $bstart = 0;
69 }
70 $blink =
71 "pdfsearch.pl?mode=$mode&search_text=$search_text&width=$width&height=$height&start=$bstart&rows=$rows";
72 $nlink =
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(
83                 $query,
84                 {
85                         'rows'                        => $rows,
86                         'start'                       => $start,
87                         'group.limit'                 => '30',
88                         'group.field'                 => 'title_group',
89                         'group'                       => true,
90                         'hl'                          => true,
91                         'hl.fl'                       => 'text',
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,
99                 }
100         );
101
102         my $hits               = $response->content;
103         my %hits               = %$hits;
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}
114                 );
115                 push( @print_group_arrays, \%print_group_array );
116         }
117         $template->param( group => \@print_group_arrays );
118 }
119 else {
120         $template->param( search_top => true );
121 }
122
123 sub printGroupResults() {
124         ( $hitsnum, $title, $search_text, $hits, $hits_hl ) = @_;
125         my $result_group;
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} );
131                 my $findPageSQL =
132                   "SELECT startpage,title_id from pdffile WHERE id = '$path';";
133                 my $sth = $sqldbi->prepare($findPageSQL);
134                 $sth->execute;
135                 $dbarray     = $sth->fetchrow_arrayref;
136                 $showpagenum = $pagenum + $dbarray->[0];
137                 $ref =
138                     "title="
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],
144                         refaddr => $ref
145                 );
146                 @print_page_array = ($print_page_array);
147                 push( @print_page_arrays, \%print_page_array );
148         }
149         %print_group_array = (
150                 text          => $title,
151                 search_num    => $hitsnum,
152                 search_result => \@print_page_arrays
153         );
154         return %print_group_array;
155 }
156 print $template->output();
157 $sqldbi . disconnect;