OSDN Git Service

tablet support.
[libre10/libre10.git] / www / ipdfsearch.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 use XML::Simple;
17
18 # Libre10 web based pdf search engine
19 # Copyright (C) 2013 yukikaze
20 #
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.
24 #
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.
28 #
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/>.
31 #
32
33 my $confdata = YAML::XS::LoadFile(
34         -e 'libre10.conf' ? 'libre10.conf' : '/etc/libre10.conf' );
35
36 my $sqlpath = "/tmp/libre10.db";
37 $sqlpath = $confdata->{dburl};
38 $sqldbi =
39   DBI->connect( "dbi:SQLite:dbname=$sqlpath", "", "",
40         { RaiseError => 1, AutoCommit => 1 } );
41 my $q           = new CGI;
42 my $mode        = "";
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);
52
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"; }
57
58 my $url  = $confdata->{solrurl};
59 my $solr = WebService::Solr->new($url);
60
61 print $q->header( -type => "text/html", -charset => "utf-8" );
62
63 if ( length $search_text > 0 ) {
64         my $query = WebService::Solr::Query->new(
65                 { text => decode( 'utf-8', $search_text ) } );
66         my %response_query = (
67                 'rows'                        => $rows,
68                 'start'                       => $start,
69                 'group.limit'                 => '30',
70                 'group.field'                 => 'title_group',
71                 'group'                       => true,
72                 'hl'                          => true,
73                 'hl.fl'                       => 'text',
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,
81         );
82         my $fq_title_solr = "";
83         if ( ( length $fq_title > 0 ) or ( length $fq_genre > 0 ) ) {
84                 $sql = "SELECT title
85                         FROM pdffile 
86                         WHERE title_id = '$fq_title' 
87                         ORDER BY part DESC LIMIT 1";
88                 $dbs = $sqldbi->prepare($sql);
89                 $dbs->execute();
90                 $dbh = $dbs->fetchrow_hashref;
91                 $fq_title_solr = decode( 'utf-8', $dbh->{'title'} );
92
93                 #print Dumper $dbh;
94         }
95
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\"";
102         }
103         if ( length $fq_genre > 0 ) {
104                 $response_query{'facet'} = "true";
105                 @fqs                     = ( 'genre:' . $fq_genre, );
106                 $response_query{'fq'}    = [@fqs];
107         }
108
109         #print Dumper %response_query;
110         my $response = $solr->search( $query, \%response_query );
111
112         #print Dumper $response;
113
114         my $hits               = $response->content;
115         my %hits               = %$hits;
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}
126                 );
127                 push( @print_group_arrays, \%print_group_array );
128         }
129         %xml_group_array = (
130                 serverName => "kobe.rec10.org",
131                 time       => "time",
132                 results    => \@print_group_arrays
133         );
134         print XMLout( \%xml_group_array, RootName => "root" );
135 }
136 else {
137         if ( $mode eq "speedtest" ) {
138                 for ( $count = 0 ; $count < 10000 ; $count++ ) {
139                         print "libre10 speedtest\n";
140                 }
141         }
142 }
143
144 sub printGroupResults() {
145         ( $hitsnum, $title, $search_text, $hits, $hits_hl ) = @_;
146         my $result_group;
147         my @print_page_arrays = ();
148
149         #$template->param( show_result => 1 );
150         foreach $doc (@$hits) {
151                 ( $pagenum, $path, $linkpath ) =
152                   ( $doc->{page}, $doc->{path_id}, $doc->{path_id} );
153                 my $findPageSQL =
154                   "SELECT startpage,title_id from pdffile WHERE id = '$path';";
155                 my $sth = $sqldbi->prepare($findPageSQL);
156                 $sth->execute;
157                 $dbarray     = $sth->fetchrow_arrayref;
158                 $showpagenum = $pagenum + $dbarray->[0];
159                 $ref =
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],
166                         refaddr => $ref
167                 );
168                 @print_page_array = ($print_page_array);
169                 push( @print_page_arrays, \%print_page_array );
170         }
171         %print_group_array = (
172                 text          => $title,
173                 search_num    => $hitsnum,
174                 search_result => \@print_page_arrays
175         );
176         return %print_group_array;
177 }
178
179 #print $template->output();
180 $sqldbi . disconnect;