WE_Frontend/Indexer/Htdig.pm

filter: Statementstmt 5.1% 8 / 156 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 28.5% 2 / 7 Podpod 25.0% 1 / 4 totaltotal 4.0% 11 / 275 CC 53 SCAR 65.6
CC
53
Cov
3%
search
1122.0
generate_conf
272.0
__ANON__
30.0
CRAP 704.1
« WE_Frontend/Publish/Rsync.pm ↑ Summary WebEditor/OldController/Admin.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: Htdig.pm,v 1.18 2006/12/01 10:12:56 cmuellermeta Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2002 Slaven Rezic. All rights reserved.
8 # This package is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
10 #
11 # Mail: slaven@rezic.de
12 # WWW: http://www.rezic.de/eserte/
13 #
14  
15 package WE_Frontend::Indexer::Htdig;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 $VERSION = sprintf("%d.%02d", q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/);
20  
21 ## XXX Maybe some day ...
22 # sub new {
23 # my $class = shift;
24 # my(%args) = @_;
25 # my $self = {};
26 # if ($args{-searchengine}) {
27 # WEsiteinfo::SearchEngine
28 # }
29 # }
30  
31 0 ▶sub conf {
Subroutine conf: not called CC=1 SCAR=6.9
Pod undocumented
32 0 my($conf, $lang) = @_;
33 0 $conf =~ s/%\{lang\}/$lang/g;
34 0 $conf;
35 }
36  
37 0 ▶sub conf_is_lang_dependent {
Subroutine conf_is_lang_dependent: not called CC=2 SCAR=17.9
Pod undocumented
38 0 my($conf) = @_;
39 0 ▶ $conf =~ /%\{lang\}/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$conf =~ /%\{lang\}/ ? : 0 0 0
40 }
41  
42 0 ▶sub search {
Subroutine search: not called CC=33 SCAR=70.2
Pod documented
43 0 my(%args) = @_;
44 0 ▶ my $words = delete $args{-words} || die "No words specified";
Condition0%0/2
delete $args{'-words'} || die('No words specified')
l!l
00
MC/DC0%0/1
delete $args{'-words'} || die('No words specified')
delete $args{'-words'}
Truth table0%0/2
A
A= delete $args{'-words'}
Aresult
11
00
45 0 my $conf = delete $args{-conf};
46 0 my $lang = delete $args{-lang};
47 0 my $query = delete $args{-query};
48 0 my $debug = delete $args{-debug};
49 0 my $https_hack = delete $args{-httpshack};
50 0 my $transform = delete $args{-transform};
51 0 my $method = delete $args{-method};
52  
53 0 ▶ if (keys %args) { warn "Unknown arguments: " . join(", ", %args) } # XXXdie?
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args) 0 0 0
54  
55 0 local %ENV = %ENV;
56 0 delete $ENV{REQUEST_METHOD}; # security barrier in htsearch
57 # Location of htdig in the standard FreeBSD port, after a normal
58 # unaltered install and a Debian 3.0 install. Sometimes $ENV{PATH} is
59 # empty, so supply additional reasonable defaults.
60 0 local $ENV{PATH} .= ":/usr/local/share/apache/cgi-bin:/opt/www/cgi-bin:/usr/lib/cgi-bin:/usr/bin:/bin";
61  
62 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
63 0 warn "Current path is $ENV{PATH}";
64 }
65  
66 0 require CGI;
67 0 my $q = CGI::new($query);
68 0 $q->param("words", $words);
69 0 $q->param("format", "perl");
70 0 ▶ $q->param("method", $method) if defined $method;
Branch: 1 branch
BranchTrueFalseTotal
if defined $method 0 0 0
71  
72 0 ▶ my $conf_path = conf($conf, $lang) if defined $conf;
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf 0 0 0
73 0 ▶ my @htsearch_cmd = ("htsearch",
Branch: 1 branch
BranchTrueFalseTotal
defined $conf ? : 0 0 0
74 (defined $conf ? ("-c", $conf_path) : ()),
75 $q->query_string);
76 0 ▶ open(HTSEARCH, "-|") or do {
Branch: 1 branch
BranchTrueFalseTotal
open HTSEARCH, "-|" or do { if ($debug) { warn "Execute: @htsearch_cmd"; } ; exec @htsearch_cmd; die qq[Can't execute htsearch command (@htsearch_cmd), PATH is $ENV{"PATH"}: $!] } 0 0 0
77 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
78 0 warn "Execute: @htsearch_cmd";
79 }
80 0 exec @htsearch_cmd;
81 0 die "Can't execute htsearch command (@htsearch_cmd), PATH is $ENV{PATH}: $!";
82 };
83  
84 # overread header
85 0 ▶ while(<HTSEARCH>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline HTSEARCH)) 0 0 0
86 0 chomp;
87 0 ▶ last if /^\r?$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\r?$/ 0 0 0
88 }
89  
90 # slurp generated perl data dump
91 0 local $/ = undef;
92 0 my $perlcode = <HTSEARCH>;
93 #warn $perlcode;
94  
95 0 require Safe;
96 0 my $cpt = Safe->new;
97 0 my $obj = $cpt->reval($perlcode);
98  
99 0 my $nr = 0;
100 die "Error while evaluating perl code result from htsearch call:\n@htsearch_cmd.\n" .
101 0 ▶ "Code: " . join("\n", map { sprintf "%4d %s", ++$nr, $_ }
Branch: 2 branches
BranchTrueFalseTotal
unless $obj 0 0 0
defined $conf ? : 0 0 0
102 split /\n/, $perlcode) . "\n" .
103 "Error: $@\n" .
104 (defined $conf ? "Configuration file is $conf_path\n" : "Using standard configuration file\n")
105 if !$obj;
106  
107 0 ▶ while(my($key,$val) = each %$obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$obj) 0 0 0
108 0 ▶ if ($key =~ /(.*)_urlenc$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ /(.*)_urlenc$/) { } 0 0 0
elsif ($key eq 'list') { } 0 0 0
109 0 my $real_key = $1;
110 0 $obj->{$real_key} = CGI::unescape($val);
111 } elsif ($key eq 'list') {
112 0 for my $obj (@$val) {
113 0 ▶ while(my($key,$val) = each %$obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$obj) 0 0 0
114 0 ▶ if ($key =~ /(.*)_urlenc$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /(.*)_urlenc$/) 0 0 0
115 0 $obj->{$1} = CGI::unescape($val);
116 }
117 }
118 }
119 }
120 }
121  
122 0 ▶ my $parse_href = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
123 0 my $href = shift;
124 0 my($pageurl, $pagenumber);
125 0 ▶ if (my($url, $querystring) = $href =~ /^(.*?)\?(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $href =~ /^(.*?)\?(.*)$/) { } 0 0 0
126 0 my $q = CGI->new($querystring);
127 0 ▶ if (defined $lang && !defined $q->param("lang")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lang and not defined $q->param("lang")) 0 0 0
Condition0%0/3
defined $lang and not defined $q->param("lang")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $lang and not defined $q->param("lang")
defined $lang not defined $q->param("lang")
Truth table0%0/3
A and not(B)
A= defined $langB= not defined $q->param("lang")
ABresult
0X0
100
111
128 0 $q->param("lang", $lang);
129 }
130 0 ▶ if (!defined $q->param("page")) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $q->param('page')) { } 0 0 0
131 0 warn "Can't get page parameter from $href";
132 0 $pagenumber = undef;
133 } else {
134 0 $pagenumber = $q->param("page");
135 }
136 0 my $new_href = "$url?" . $q->query_string;
137 0 $pageurl = $new_href;
138 } else {
139 0 warn "Can't parse URL $href";
140 }
141 0 ($pageurl, $pagenumber);
142 0 };
143  
144 0 ▶ if ($obj->{"pagelist"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"pagelist"}) 0 0 0
145 0 my @pageurllist;
146 my @pagenumberlist;
147 0 my @hrefs = $obj->{"pagelist"} =~ m{href="(.*?)"}g;
148 0 for my $href (@hrefs) {
149 0 my($pageurl, $pagenumber) = $parse_href->($href);
150 0 ▶ if (defined $pageurl) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pageurl) 0 0 0
151 0 push @pageurllist, $pageurl;
152 0 push @pagenumberlist, $pagenumber;
153 }
154 }
155 # Add this page
156 0 ▶ if ($pagenumberlist[0] != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagenumberlist[0] != 1) { } 0 0 0
157 0 unshift @pageurllist, undef; # undef means: this page
158 0 unshift @pagenumberlist, 1;
159 } else {
160 SEARCH: {
161 0 for my $i (0 .. $#pagenumberlist) {
162 0 ▶ if ($i+1 != $pagenumberlist[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 != $pagenumberlist[$i]) 0 0 0
163 # current page is in the middle of the list
164 0 splice @pagenumberlist, $i, 0, $i+1;
165 0 splice @pageurllist, $i, 0, undef;
166 0 last SEARCH;
167 }
168 }
169 # otherwise it's the last page
170 0 push @pageurllist, undef;
171 0 push @pagenumberlist, $#pagenumberlist+2;
172 }
173 }
174 0 $obj->{pageurllist} = \@pageurllist;
175 0 $obj->{pagenumberlist} = \@pagenumberlist;
176 }
177  
178 0 for my $dir (qw(prev next)) {
179 0 ▶ if ($obj->{$dir."page"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$obj{$dir . 'page'}) 0 0 0
180 0 my($href) = $obj->{$dir."page"} =~ m{href="(.*?)"};
181 0 my($pageurl, $pagenumber) = $parse_href->($href);
182 0 ▶ if (defined $pageurl) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pageurl) 0 0 0
183 0 $obj->{$dir."pageurl"} = $pageurl;
184 0 $obj->{$dir."pagenumber"} = $pagenumber;
185 }
186 }
187 }
188  
189 0 ▶ if ($https_hack && $obj->{list}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($https_hack and $obj->{"list"}) 0 0 0
Condition0%0/3
$https_hack and $obj->{"list"}
!ll&&!rl&&r
000
MC/DC0%0/2
$https_hack and $obj->{"list"}
$https_hack $obj->{"list"}
Truth table0%0/3
A and B
A= $https_hackB= $obj->{"list"}
ABresult
0X0
100
111
190 0 for my $hit (@{ $obj->{list} }) {
191 0 $hit->{url} =~ s{^http://}{https://};
192 }
193 }
194  
195 # words is documented, but not available?
196 0 ▶ if (!exists $obj->{words}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $obj->{"words"}) 0 0 0
197 0 $obj->{words} = $words;
198 }
199  
200 0 ▶ if ($transform) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transform) 0 0 0
201 0 $transform->($obj);
202 }
203  
204 0 $obj;
205 }
206  
207 0 ▶sub generate_conf {
Subroutine generate_conf: not called CC=16 SCAR=56.1
Pod undocumented
208 0 my($c, %args) = @_;
209  
210 0 my $debug = $args{-debug};
211  
212 0 my $lang = $args{-lang};
213  
214 0 ▶ my $tpl = $args{-htdigconftemplate} || $c->searchengine->htdigconftemplate;
Condition0%0/3
$args{'-htdigconftemplate'} || $c->searchengine->htdigconftemplate
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'-htdigconftemplate'} || $c->searchengine->htdigconftemplate
$args{'-htdigconftemplate'} $c->searchengine->htdigconftemplate
Truth table0%0/3
A || B
A= $args{'-htdigconftemplate'}B= $c->searchengine->htdigconftemplate
ABresult
1X1
011
000
215 0 ▶ my $conf = $args{-htdigconf} || $c->searchengine->htdigconf;
Condition0%0/3
$args{'-htdigconf'} || $c->searchengine->htdigconf
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'-htdigconf'} || $c->searchengine->htdigconf
$args{'-htdigconf'} $c->searchengine->htdigconf
Truth table0%0/3
A || B
A= $args{'-htdigconf'}B= $c->searchengine->htdigconf
ABresult
1X1
011
000
216 0 my $lang_conf = conf($conf, $lang);
217 0 ▶ if (conf_is_lang_dependent($lang_conf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (conf_is_lang_dependent($lang_conf)) 0 0 0
218 0 die "-lang should be supplied for language independent conf specification $conf";
219 }
220  
221 0 ▶ if (!defined $tpl) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tpl) 0 0 0
222 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
223 0 warn "No template config defined, we're done with $lang_conf.\n";
224 }
225 0 return $lang_conf;
226 }
227  
228 0 my @dependents;
229 0 ▶ if ($args{-dependents}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'-dependents'}) { } 0 0 0
230 0 @dependents = @{ $args{-dependents} };
231 } else {
232 0 (my $pkgfile = __PACKAGE__) =~ s{::}{/}g;
233 # XXX what if module is named WEsiteinfo_project.pm and WEprojectinfo_project.pm?
234 push @dependents,
235 $INC{"WEsiteinfo.pm"}, $INC{"WEprojectinfo.pm"},
236 $tpl,
237 0 $INC{"$pkgfile.pm"};
238 }
239  
240 # is the configuration file current?
241 0 my $conf_is_old = 0;
242 0 ▶ if (!-e $lang_conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $lang_conf) { } 0 0 0
243 0 $conf_is_old = 1;
244 } else {
245 0 for my $dep (grep { defined $_ } @dependents) {
246 0 ▶ if (!-e $lang_conf || -M $dep < -M $lang_conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $lang_conf or -M $dep < -M $lang_conf) 0 0 0
Condition0%0/3
not -e $lang_conf or -M $dep < -M $lang_conf
l!l&&r!l&&!r
000
MC/DC0%0/2
not -e $lang_conf or -M $dep < -M $lang_conf
not -e $lang_conf -M $dep < -M $lang_conf
Truth table0%0/3
not(A) or B
A= not -e $lang_confB= -M $dep < -M $lang_conf
ABresult
1X1
011
000
247 0 $conf_is_old = 1;
248 0 last;
249 }
250 }
251 }
252  
253 0 ▶ if (!$conf_is_old) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf_is_old) 0 0 0
254 0 warn "htdig config file $lang_conf is current, we're done.\n";
255 0 return $lang_conf;
256 }
257  
258 0 my $long_lang;
259 0 ▶ if (defined $lang) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lang) 0 0 0
260 $long_lang = {en => "english",
261 de => "german",
262 it => "italian",
263 fr => "french",
264 kr => "korean",
265 ru => "russian",
266 es => "spanish",
267 pt => "portugese",
268 hu => "hungarian",
269 0 }->{$lang};
270 0 ▶ warn "long_lang is not defined for $lang"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $long_lang 0 0 0
271 if !defined $long_lang;
272 }
273  
274 # regenerate conf file
275 0 require Template;
276 # XXX Don't duplicate this --- already found in we_search.cgi and
277 # we_redisys.cgi
278 0 my $t = Template->new
279 (ABSOLUTE => 1,
280 POST_CHOMP => 0,
281 INCLUDE_PATH => [$c->paths->site_templatebase,
282 $c->paths->we_templatebase,
283 ],
284 EVAL_PERL => 1,
285 PLUGIN_BASE => ["WE_" . $c->project->name . "::Plugin",
286 "WE_Frontend::Plugin"]
287 );
288 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
289 0 warn "Create config file $lang_conf from $tpl.\n";
290 }
291 0 my $conf_header = <<EOF;
292 # DO NOT EDIT THIS FILE!
293 # Generated automatically by:
294 0 # module: @{[ __PACKAGE__ ]}
295 0 # user: @{[ (getpwuid($<))[0] ]}
296 0 # date: @{[ scalar localtime ]}
297 EOF
298 $t->process
299 ($tpl,
300 {c => $c,
301 config => $c, # for compatibility
302 lang => $lang,
303 longlang => $long_lang,
304 # strip dash from args keys
305 0 ▶ args => [map { (substr($_, 1) => $args{$_}) } keys(%args)],
Branch: 1 branch
BranchTrueFalseTotal
$t->process($tpl, {"c", $c, "config", $c, "lang", $lang, "longlang", $long_lang, "args", [map({substr($_, 1), $args{$_};} keys %args)], "conf_header", $conf_header}, $lang_conf) or die $t->error 0 0 0
306 conf_header => $conf_header,
307 }, $lang_conf)
308 or die $t->error;
309  
310 0 return $lang_conf;
311 }
312  
313 1 1;
314  
315 __END__
« WE_Frontend/Publish/Rsync.pm ↑ Summary WebEditor/OldController/Admin.pm »