OMatic/SearchMod.pm untested

filter: Statementstmt 0.0% 0 / 155 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 240 CC 23 SCAR 28.7
CC
23
Cov
0%
openWordDB
56.0
getMatchesForSet
20.0
getIndices
12.0
CRAP 17.6
1 ##############################################################################
2 # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. #
3 # #
4 # This program is free software; you can redistribute it and/or #
5 # modify it under the terms of the GNU General Public License #
6 # as published by the Free Software Foundation; either version 2 #
7 # of the License, or (at your option) any later version. #
8 # #
9 # This program is distributed in the hope that it will be useful, #
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
12 # GNU General Public License for more details. #
13 # #
14 # You should have received a copy of the GNU General Public License #
15 # along with this program; if not, write to the Free Software #
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#
17 # #
18 # Jon Howell can be contacted at: #
19 # 6211 Sudikoff Lab, Dartmouth College #
20 # Hanover, NH 03755-3510 #
21 # jonh@cs.dartmouth.edu #
22 # #
23 # An electronic copy of the GPL is available at: #
24 # http://www.gnu.org/copyleft/gpl.html #
25 # #
26 ##############################################################################
27  
28 use strict;
29  
30 ### SearchMod.pm
31 ###
32 ### Support for search functions
33 ###
34  
35 package FAQ::OMatic::SearchMod;
36  
37 use FAQ::OMatic::Item;
38 use FAQ::OMatic::I18N;
39 use FAQ::OMatic::Words;
40 use FAQ::OMatic;
41  
42 sub openWordDB {
43 return if (defined FAQ::OMatic::getLocal('wordDB'));
44 my $wordDBtoken;
45 if (FAQ::OMatic::usedbm()) {
46 my %wordDB;
47 if (not dbmopen (%wordDB, "$FAQ::OMatic::Config::metaDir/search", 0400)) {
48 FAQ::OMatic::gripe('abort', "Can't open dbm search database. "
49 ."Have you run buildSearchDB? (Should I?)");
50 }
51 $wordDBtoken = \%wordDB;
52 } else {
53 if (not open(OFFSETFILE, "<$FAQ::OMatic::Config::metaDir/search.offset")) {
54 FAQ::OMatic::gripe('abort', "Can't open search.offset. "
55 ."Have you run buildSearchDB? (Should I?)");
56 }
57 $wordDBtoken = "files_are_open_yahoo!";
58 }
59 if (not open(WORDSFILE, "<$FAQ::OMatic::Config::metaDir/search.words")) {
60 FAQ::OMatic::gripe('abort', "Can't open search.words. "
61 ."Have you run buildSearchDB? (Should I?)");
62 }
63 if (not open(INDEXFILE, "<$FAQ::OMatic::Config::metaDir/search.index")) {
64 FAQ::OMatic::gripe('abort', "Can't open search.index. "
65 ."Have you run buildSearchDB? (Should I?)");
66 }
67 FAQ::OMatic::setLocal('wordDB', $wordDBtoken);
68 }
69  
70 sub closeWordDB {
71 my $wordDB = FAQ::OMatic::getLocal('wordDB');
72 if (FAQ::OMatic::usedbm()) {
73 dbmclose %{$wordDB};
74 undef %{$wordDB};
75 } else {
76 close OFFSETFILE;
77 }
78 close WORDSFILE;
79 close INDEXFILE;
80 }
81  
82 # linear scan of .offset file, looking for $word. Pretty slow,
83 # unless your dbm implementation is somehow very slow and broken,
84 # as on sourceforge.
85 sub scanOffsets {
86 my $word = shift;
87 seek (OFFSETFILE, 0, 0);
88 my $line;
89 # THANKS to Gary.Frost@ubsw.com for reporting the "Value of
90 # <HANDLE> construct can be "0"" error that occurs on his version
91 # of perl; fixed here and elsewhere.
92 while (defined($line = <OFFSETFILE>)) {
93 chomp $line;
94 my ($fileWord, $pair) = split(' ', $line, 2);
95 if ($fileWord eq $word) {
96 #FAQ::OMatic::gripe('debug', "found pair: $line");
97 return $pair;
98 }
99 }
100 return undef;
101 }
102  
103 sub getIndices {
104 my $word = shift;
105 my $pair;
106 if (FAQ::OMatic::usedbm()) {
107 my $wordDB = FAQ::OMatic::getLocal('wordDB');
108 $pair = $wordDB->{$word};
109 } else {
110 $pair = scanOffsets($word);
111 }
112  
113 # returns indexseek,wordseek pair
114 # THANKS to Vicki Brown <vlb@cfcl.com> and jon * <jon@clearink.com>
115 # for reporting unitialized value errors in this code.
116 return defined($pair)
117 ? split(' ', $pair)
118 : (undef,undef);
119 }
120  
121 sub getWordClass {
122 my $word = shift;
123 my @wordclass = ();
124  
125 openWordDB();
126
127 my ($indexseek, $wordseek) = getIndices($word);
128 #FAQ::OMatic::gripe('debug', "got seeks $indexseek and $wordseek for $word");
129  
130 if (defined $indexseek) {
131 #grab all words in wordsfile with $word as a prefix
132 seek WORDSFILE, $wordseek, 0;
133 while (defined($_ = <WORDSFILE>)) {
134 chomp;
135 if (m/^$word/) {
136 push @wordclass, $_;
137 } else {
138 last;
139 }
140 }
141 }
142  
143 return \@wordclass;
144 }
145  
146 sub getMatchesForClass {
147 my $classref = shift; # array ref for a class of "identical" words
148 my %files;
149  
150 my $word;
151 foreach $word (@{$classref}) {
152 my ($indexseek,$wordseek) = getIndices($word);
153 next if (not defined $indexseek);
154 seek INDEXFILE, $indexseek, 0;
155 while (defined($_ = <INDEXFILE>)) {
156 chomp;
157 last if (m/^END$/);
158 $files{$_}=1;
159 }
160 }
161  
162 my @matches = sort keys %files;
163 return \@matches;
164 }
165  
166 sub getMatchesForSet {
167 my $params = shift;
168 my $setref = $params->{'_searchArray'};
169 # array ref for complete set of user words to search
170  
171 $params->{'_minMatches'} = 'all' if ($params->{'_minMatches'} eq '');
172 my $minhits = $params->{'_minMatches'};
173 # we return only files with at least this many hits
174 if ($minhits eq 'all') { # convert symbolic hits to a number
175 $minhits = scalar(@{$setref});
176 }
177 $params->{'_actualMatches'} = $minhits;
178  
179 my %accumulator=();
180 my @hitfiles=();
181  
182 my ($word, $file);
183 foreach $word (@{$setref}) {
184 my $classref = getWordClass($word);
185 my $matches = getMatchesForClass($classref);
186 foreach $file (@{$matches}) {
187 $accumulator{$file}++;
188 }
189 }
190  
191 foreach $file (sort keys %accumulator) {
192 if ($accumulator{$file} >= $minhits) {
193 push @hitfiles, $file;
194 }
195 }
196  
197 return \@hitfiles;
198 }
199  
200 sub convertSearchParams {
201 my $params = shift;
202 my $pattern;
203 my $encode_lang = FAQ::OMatic::I18N::language();
204  
205 # given a user-input search string, we break it into "legal" words
206 # and store it in another parameter.
207
208 $pattern = $params->{'_search'};
209 if($encode_lang eq "ja_JP.EUC") {
210 require NKF; import NKF;
211 $pattern = nkf('-e', $pattern);
212 }
213 my @patternwords = FAQ::OMatic::Words::getWords( $pattern );
214  
215 $params->{'_searchArray'} = \@patternwords;
216 }
217  
218 sub addNewFiles {
219 my $wordset = shift; #ary ref
220 my $fileset = shift; # hash ref -- where to add results
221 my $words = {};
222  
223 # Get the list of files touched since last searchDB build
224 if (not open HINTS, "<$FAQ::OMatic::Config::metaDir/searchHints") {
225 # sorry, can't help ya
226 return;
227 }
228 my @touchedFiles = <HINTS>;
229 close HINTS;
230  
231 # index each item
232 my $filename;
233 my $item;
234 foreach $filename (@touchedFiles) {
235 chomp $filename;
236 $item = new FAQ::OMatic::Item($filename);
237 $item->extractWords($words);
238 }
239  
240 # for every word in the wordset, add all the files it appears
241 # in to the fileset passed to us.
242 my $word;
243 foreach $word (@{$wordset}) {
244 if ($words->{$word}) {
245 foreach $filename (keys %{$words->{$word}}) {
246 $fileset->{$filename} = 1;
247 }
248 }
249 }
250  
251 # notice that if there were suffixes of the user's requested words
252 # in the new content that weren't in the system anywhere when the
253 # searchDB was built, then those suffixes won't be in the wordset,
254 # and the search will miss them. Hey, wah, this is better than
255 # missing ALL the new content, okay? :v)
256  
257 # this also screws up the counts of how many matches this file
258 # had (since it could contribute matches from the searchDB lookup
259 # AND the newFiles lookup), so I'm going to leave it turned off
260 # for now. Rats.
261 }
262  
263 sub getRecentSet {
264 my $params = shift;
265 my $recentList = [];
266  
267 my $durationdays = $params->{'_duration'};
268 # used directly to compare against perl's floating-point -M file test
269 my $then = time() - $durationdays*24*60*60;
270 # Used to compare against LastModifiedSecs field.
271 # By 'days' we mean 24-hour periods, not calendar days.
272 # (In the US, for example, there is a 23-hour calendar day in
273 # April and a 25-hour one in the fall, what, in October? for daylight
274 # savings time.)
275  
276 my $filei;
277 foreach $filei (FAQ::OMatic::getAllItemNames()) {
278 # use file time as a hint for which items we even need to open up.
279 next if (-M "$FAQ::OMatic::Config::itemDir/$filei" >= $durationdays);
280 # ...but only trust LastModifiedSecs field for final say on mod time.
281 my $item = new FAQ::OMatic::Item($filei);
282 my $lm = $item->{'LastModifiedSecs'} || 0;
283 if ($lm > $then) {
284 push @{$recentList}, $filei;
285 }
286 }
287  
288 return $recentList;
289 }
290  
291 # reasonable text for 'n' days
292 my %dayMap = (
293 0 => gettext("zero days"),
294 1 => gettext("day"),
295 2 => gettext("two days"),
296 7 => gettext("week"),
297 14 => gettext("fortnight"),
298 31 => gettext("month"), # (31? a month, give or take. :v)
299 92 => gettext("three months"),
300 184 => gettext("six months"),
301 366 => gettext("year")
302 );
303  
304 sub getRecentMap {
305 # get a copy of the day map (except for 0)
306 # for use in creating the recent form
307 my %recentMap = %dayMap;
308 delete $recentMap{0};
309 return \%recentMap;
310 }
311  
312 sub textDays {
313 my $duration = shift || 0;
314 my $textDayStr = $dayMap{$duration} || $duration." ".gettext("days");
315 return $textDayStr;
316 }
317  
318 1;