OMatic/buildSearchDB.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 96 CC 7 SCAR 35.9
CC
7
Cov
0%
build
56.0
localhprint
2.0
BEGIN
2.0
CRAP 36.4
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 package FAQ::OMatic::buildSearchDB;
31  
32 use FAQ::OMatic::Item;
33 use FAQ::OMatic;
34  
35 sub build {
36 my $words = {};
37  
38 # Notice we don't bother doing this recursively -- faqs
39 # can be in a state where not everybody shares a common root.
40 # (This does search the trash, however, which is not necessarily
41 # what you want.)
42 # Mental note: If I switch back to recursive search from a common
43 # root, I should remember to turn the recursion back on in
44 # FAQ::OMatic::Item::extractWords.
45 my @allItems = FAQ::OMatic::getAllItemNames();
46 my $filename;
47 my $item;
48 localhprint("beginning scan");
49 foreach $filename (@allItems) {
50 localhprint("scanning $filename");
51 $item = new FAQ::OMatic::Item($filename);
52 if ($item->isEmptyStub()) {
53 # pass over it.
54 } elsif ($item->isBroken()) {
55 FAQ::OMatic::gripe('debug', 'item/'.$filename.' is broken.');
56 } else {
57 $item->extractWords($words);
58 }
59 }
60  
61 my @wordlist = sort keys %{$words};
62  
63 ## Build the files in temporaries, so searching still works during the
64 ## re-extraction. In fact, a running search process will continue to
65 ## use the old ones (and not get confused when we mv in the new ones)
66 ## because of Unix' cool file semantics.
67  
68 my %wordsdb;
69 my $searchFileName = 'search.'.FAQ::OMatic::nonce();
70 my $searchFilePath = $FAQ::OMatic::Config::metaDir.'/'.$searchFileName;
71 my $usedbm = FAQ::OMatic::usedbm();
72 if ($usedbm) {
73 dbmopen (%wordsdb, $searchFilePath, 0600);
74 # perl sure is touchy about its octal literals
75 } else {
76 open OFFSETFILE, ">${searchFilePath}.offset"
77 || die "${searchFilePath}.offset $!";
78 }
79 open(INDEXFILE, ">${searchFilePath}.index")
80 || die "${searchFilePath}.index $!";
81 open(WORDSFILE, ">${searchFilePath}.words")
82 || die "${searchFilePath}.words $!";
83  
84 localhprint("squirting words into ${searchFilePath}");
85 foreach my $i (@wordlist) {
86 ## Write down in the hash the file pointer where we can find
87 ## this entry:
88 if ($usedbm) {
89 $wordsdb{$i} = (tell INDEXFILE)." ".(tell WORDSFILE);
90 } else {
91 # a very slow replacement for a direct-access database. Pfft.
92 print OFFSETFILE "${i} ".(tell INDEXFILE)." ".(tell WORDSFILE)."\n";
93 }
94 ## Then dump out all the items with this word in them:
95 my @list = sort keys %{ $words->{$i} };
96 foreach my $j (@list) {
97 print INDEXFILE "$j\n";
98 }
99 ## And append the word to the words file
100 print WORDSFILE "$i\n";
101 ## terminate the list
102 print INDEXFILE "END\n";
103 }
104 localhprint("squirting complete");
105  
106 if ($usedbm) {
107 dbmclose %wordsdb;
108 } else {
109 close OFFSETFILE;
110 }
111 close INDEXFILE;
112 close WORDSFILE;
113  
114 ## make sure the files are readable
115 # Using wildcards lets us remain ignorant of dbm's extension(s), which
116 # vary machine to machine.
117 my @searchfiles = FAQ::OMatic::safeGlob($FAQ::OMatic::Config::metaDir,
118 "^${searchFileName}");
119  
120 foreach my $i (@searchfiles) {
121 chmod 0644, $i;
122 }
123  
124 # move temp files into place as the official search database
125 foreach my $from (@searchfiles) {
126 $from =~ m#${searchFileName}([^/]*)$#;
127 my $suffix = $1;
128 if (not defined $suffix) {
129 die "Could not find suffix: $from !~ $searchFileName";
130 }
131 my $to = "${FAQ::OMatic::Config::metaDir}/search${suffix}";
132 rename($from,$to) ||
133 FAQ::OMatic::gripe('debug', "rename($from,$to) failed");
134 }
135  
136 # create a freshSearchDBHint to let me know I don't need to do
137 # this again any time soon.
138 open SEARCHHINT, ">$FAQ::OMatic::Config::metaDir/freshSearchDBHint";
139 close SEARCHHINT;
140 }
141  
142 sub localhprint {
143 my $msg = shift;
144 FAQ::OMatic::maintenance::hprint("${msg}<br>\n");
145 FAQ::OMatic::maintenance::hflush();
146 }
147  
148 1;