blib/lib/File/Tabular/Web/Attachments/Indexed.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 148 CC 6 SCAR 14.7
CC
6
Cov
0%
before_search
12.0
log_search
6.0
after_add_attachment
6.0
CRAP 4.3
1 package File::Tabular::Web::Attachments::Indexed;
2  
3 use base qw/File::Tabular::Web::Attachments/;
4 use strict;
5 use warnings;
6 no warnings 'uninitialized';
7  
8 use Search::Indexer;
9 use Search::QueryParser 0.92;
10 use locale;
11  
12 #----------------------------------------------------------------------
13 sub app_initialize {
14 #----------------------------------------------------------------------
15 my $self = shift;
16  
17 $self->SUPER::app_initialize;
18  
19 # indexed fields are specified as "[fields]upload field=indexed" in config
20 my $upld_ref = $self->{app}{upload_fields};
21 my @indexed = grep {$upld_ref->{$_} =~ /indexed/} values %$upld_ref;
22  
23 @indexed < 2 or die "currently no support for multiple indexed fields";
24  
25 $self->{app}{indexed_field} = $indexed[0];
26 }
27  
28  
29 #======================================================================
30 # REQUEST HANDLING : SEARCH METHODS #
31 #======================================================================
32  
33 #----------------------------------------------------------------------
34 sub words_queried {
35 #----------------------------------------------------------------------
36 my $self = shift;
37 my $all_search = "$self->{search_string_orig} $self->{search_fulltext}";
38 return ($all_search =~ m([\w/]+)g);
39 }
40  
41  
42 #----------------------------------------------------------------------
43 sub log_search {
44 #----------------------------------------------------------------------
45 my $self = shift;
46 return if not $self->{logger};
47  
48 my $msg = sprintf "[%s][%s] $self->{user}",
49 $self->{search_string_orig},
50 $self->{search_fulltext};
51 $self->{logger}->info($msg);
52 }
53  
54  
55 #----------------------------------------------------------------------
56 sub before_search {
57 #----------------------------------------------------------------------
58 my ($self) = @_;
59  
60 $self->SUPER::before_search;
61  
62 # Cleanup fulltext_result which might be populated from previous query
63 delete $self->{fulltext_result};
64
65 # searches into the fulltext index are passed through param 'SFT'
66 $self->{search_fulltext} = $self->param('SFT') or return;
67  
68 $self->{app}{indexer} ||= Search::Indexer->new(
69 dir => $self->{app}{dir},
70 preMatch => $self->{cfg}->get('preMatch'),
71 postMatch => $self->{cfg}->get('postMatch'),
72 );
73  
74 my $result = $self->{app}{indexer}
75 ->search($self->{search_fulltext}, "implicit_plus");
76  
77 # MAYBE add some logging here (time for fulltext search, #docs found)
78  
79 if ($result) { # nonempty results
80 $self->{results}{killedWords} = join ", ", @{$result->{killedWords}};
81 $self->{results}{regex} = $result->{regex};
82  
83 # HACK : build a regex with all document ids, and add that into
84 # the search string. Not efficient if the result set is large;
85 # will require more clever handling in File::Tabular::compile_query
86 # using some kind of representation for sets of integers (bit vectors
87 # or Set::IntSpan::Fast)
88 # ASSUMES the document number is the record key, stored in first field
89 my $doc_ids = join ",", keys %{$result->{scores}}
90 or return; # no scores, no results
91 $self->{search_string} = "#$doc_ids";
92 $self->{search_string} .= " AND ($self->{search_string_orig})"
93 if $self->{search_string_orig};
94 }
95 $self->{fulltext_result} = $result;
96  
97 return $self;
98 }
99  
100  
101  
102 #----------------------------------------------------------------------
103 sub search { # call parent search(), add fulltext scores into results
104 #----------------------------------------------------------------------
105 my ($self, $search_string) = @_;
106  
107 $self->SUPER::search($search_string);
108  
109 my $fulltext_result = $self->{fulltext_result} or return;
110  
111 # merge scores into results
112 $self->{data}->ht->add('score'); # new field for storing 'score'
113  
114 foreach my $record (@{$self->{results}{records}}) {
115 my $doc_id = $self->key($record);
116 $record->{score} = $fulltext_result->{scores}{$doc_id};
117 }
118 $self->{orderBy} ||= "score : -num"; # default sorting by decreasing scores
119 }
120  
121  
122 #----------------------------------------------------------------------
123 sub sort_and_slice {
124 #----------------------------------------------------------------------
125 my ($self) = @_;
126  
127 $self->SUPER::sort_and_slice;
128 $self->add_excerpts;
129 }
130  
131  
132  
133 #----------------------------------------------------------------------
134 sub add_excerpts { # add text excerpts from attached files
135 #----------------------------------------------------------------------
136 my ($self) = @_;
137  
138 $self->{fulltext_result} or return;
139  
140 # need new field in the Hash::Type to store the excerpts
141 $self->{data}->ht->add('excerpts');
142  
143 # add excerpts into each displayed record
144 my $regex = $self->{results}->{regex};
145 foreach my $record (@{$self->{results}{records}}) {
146 my $buf = $self->indexed_doc_content($record);
147 my $excerpts = $self->{app}{indexer}->excerpts($buf, $regex);
148 $record->{excerpts} = join(' / ', @$excerpts);
149 }
150 }
151  
152  
153  
154 #----------------------------------------------------------------------
155 sub params_for_next_slice {
156 #----------------------------------------------------------------------
157 my ($self, $start) = @_;
158  
159 return ("SFT=$self->{search_fulltext}",
160 $self->SUPER::params_for_next_slice($start));
161 }
162  
163  
164  
165 #======================================================================
166 # HANDLING ATTACHMENTS #
167 #======================================================================
168  
169  
170 #----------------------------------------------------------------------
171 sub after_add_attachment {
172 #----------------------------------------------------------------------
173 my ($self, $record, $field, $path) = @_;
174  
175 if ($field eq $self->{app}{indexed_field}) {
176 my $buf = $self->indexed_doc_content($record);
177 delete $self->{app}{indexer};
178 my $indexer = Search::Indexer->new(dir => $self->{app}{dir},
179 writeMode => 1);
180 $indexer->add($self->key($record), $buf);
181 }
182 }
183  
184 #----------------------------------------------------------------------
185 sub before_delete_attachment {
186 #----------------------------------------------------------------------
187 my ($self, $record, $field, $path) = @_;
188  
189 if ($field eq $self->{app}{indexed_field}) {
190 delete $self->{app}{indexer};
191 my $indexer = Search::Indexer->new(dir => $self->{app}{dir},
192 writeMode => 1);
193 $indexer->remove($self->key($record));
194 }
195 }
196  
197  
198 #----------------------------------------------------------------------
199 sub indexed_doc_content {
200 #----------------------------------------------------------------------
201 my ($self, $record) = @_;
202  
203 # this is the default implementation, MOST PROBABLY INADEQUATE
204 # should be overridden in subclasses to perform appropriate
205 # conversions from Html, Pdf, Word, etc.
206  
207 my $path = $self->upload_fullpath($record, $self->{indexed_field});
208 open my $fh, $path or die "open $path: $!";
209 local $/;
210 my $content = <$fh>; # just return the file content
211 return $content;
212 }
213  
214  
215  
216  
217 1;
218  
219 __END__