Prog/Config.pm

filter: Statementstmt 82.2% 277 / 337 Branchbran 71.8% 102 / 142 Conditioncond 34.0% 15 / 44 MC/DCmcdc 3.7% 1 / 27 Subroutinesub 76.6% 23 / 30 Podpod 100% 10 / 10 totaltotal 72.5% 428 / 590 CC 140 SCAR 42.6
CC
140
Cov
75%
ver2_to_ver3
125.2
all_metanames
30.0
get_stemmer_lang
30.0
CRAP 70.6
« Prog/Headers.pm ↑ Summary Prog/Aggregator/Object.pm »
1 package SWISH::Prog::Config;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Config::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Search::Tools::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Search::Tools::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use SWISH::Prog::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use overload(
Subroutine BEGIN: called CC=1 SCAR=0.2
14 '""' => \&stringify,
15 0 ▶ bool => sub {1},
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 16 fallback => 1,
17 16 );
18  
19 16 our $VERSION = '0.75';
20  
21 16 my $XML = Search::Tools::XML->new;
22  
23 16 ▶use base qw( SWISH::Prog::Class );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 16 my %unique = map { $_ => 1 } qw(
26 MetaNames
27 PropertyNames
28 PropertyNamesNoStripChars
29 IncludeConfigFile
30 );
31  
32 16 my %takes_single_value = map { $_ => 1 } qw(
33 IndexFile
34 FuzzyIndexingMode
35 );
36  
37 16 my @Opts = qw(
38 AbsoluteLinks
39 BeginCharacters
40 BumpPositionCounterCharacters
41 Buzzwords
42 CascadeMetaContext
43 ConvertHTMLEntities
44 DefaultContents
45 Delay
46 DontBumpPositionOnEndTags
47 DontBumpPositionOnStartTags
48 EnableAltSearchSyntax
49 EndCharacters
50 EquivalentServer
51 ExtractPath
52 FileFilter
53 FileFilterMatch
54 FileInfoCompression
55 FileMatch
56 FileRules
57 FollowSymLinks
58 FollowXInclude
59 FuzzyIndexingMode
60 HTMLLinksMetaName
61 IgnoreFirstChar
62 IgnoreLastChar
63 IgnoreLimit
64 IgnoreMetaTags
65 IgnoreNumberChars
66 IgnoreTotalWordCountWhenRanking
67 IgnoreWords
68 ImageLinksMetaName
69 IncludeConfigFile
70 IndexAdmin
71 IndexAltTagMetaName
72 IndexComments
73 IndexContents
74 IndexDescription
75 IndexDir
76 IndexFile
77 IndexName
78 IndexOnly
79 IndexPointer
80 IndexReport
81 MaxDepth
82 MaxWordLimit
83 MetaNameAlias
84 MetaNames
85 MetaNamesRank
86 MinWordLimit
87 NoContents
88 obeyRobotsNoIndex
89 ParserWarnLevel
90 PreSortedIndex
91 PropCompressionLevel
92 PropertyNameAlias
93 PropertyNames
94 PropertyNamesCompareCase
95 PropertyNamesDate
96 PropertyNamesIgnoreCase
97 PropertyNamesMaxLength
98 PropertyNamesNoStripChars
99 PropertyNamesNumeric
100 PropertyNamesSortKeyLength
101 RecursionDepth
102 ReplaceRules
103 ResultExtFormatName
104 SpiderDirectory
105 StoreDescription
106 SwishProgParameters
107 SwishSearchDefaultRule
108 SwishSearchOperators
109 TagAlias
110 TmpDir
111 TranslateCharacters
112 TruncateDocSize
113 UndefinedMetaTags
114 UndefinedMetaNames
115 UndefinedXMLAttributes
116 UseSoundex
117 UseStemming
118 UseWords
119 WordCharacters
120 Words
121 XMLClassAttributes
122 );
123 16 my %Opts = map { $_ => $_ } @Opts;
124  
125 16 __PACKAGE__->mk_accessors(qw( file debug verbose ));
126  
127 =head1 NAME
128  
129 SWISH::Prog::Config - read/write Swish-e config files
130  
131 =head1 SYNOPSIS
132  
133 use SWISH::Prog::Config;
134
135 my $config = SWISH::Prog::Config->new;
136 $config->write2();
137 $config->read2('path/to/file.conf');
138 $config->write3();
139
140
141 =head1 DESCRIPTION
142  
143 The SWISH::Prog::Config class is intended to be accessed via SWISH::Prog->new().
144  
145 See the Swish-e documentation for a list of configuration parameters.
146 Each parameter has an accessor/mutator method as part of the Config object.
147 Some preliminary compatability is offered for SWISH::3::Config
148 with XML format config files.
149  
150 B<NOTE:> Every config parameter can take either a scalar or an array ref as a value.
151 In addition, you may append config values to any existing values by passing an additional
152 true argument. The return value of any 'get' is always an array ref.
153  
154 Example:
155  
156 $config->MetaNameAlias( ['foo bar', 'one two', 'red yellow'] );
157 $config->MetaNameAlias( 'green blue', 1 );
158 print join("\n", @{ $config->MetaNameAlias }), " \n";
159 # would print:
160 # foo bar
161 # one two
162 # red yellow
163 # green blue
164
165  
166 =head1 METHODS
167  
168 =head2 new( I<params> )
169  
170 Instantiate a new Config object.
171 Takes a hash of key/value pairs, where each key
172 may be a Swish-e configuration parameter.
173  
174 Example:
175  
176 my $config = SWISH::Prog::Config->new( DefaultContents => 'HTML*' );
177
178 print "DefaultContents is ", $config->DefaultContents, "\n";
179
180 =cut
181  
182 20 ▶sub new {
Subroutine new: called CC=7 SCAR=23.7
Pod documented
183 20 my $class = shift;
184 20 my ( %args, $file2read );
185 20 ▶ if ( @_ == 1 && !ref $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 0 20 20
elsif (not ref $_[0]) { } 20 0 20
Condition33%1/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
2000
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
186 0 $file2read = shift;
187 }
188 elsif ( !ref $_[0] ) {
189 20 %args = @_;
190 }
191 else {
192 0 %args = %{ $_[0] };
193 }
194  
195 # do our own init here because we rely on AUTOLOAD magic
196 20 my $self = bless {}, $class;
197 20 for my $k ( keys %args ) {
198 0 $self->$k( $args{$k} );
199 }
200  
201 # set some defaults
202 20 $self->{'_start'} = time;
203 20 ▶ $self->IgnoreTotalWordCountWhenRanking(0)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->IgnoreTotalWordCountWhenRanking 20 0 20
204 unless defined $self->IgnoreTotalWordCountWhenRanking;
205  
206 20 ▶ if ($file2read) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file2read) 0 20 20
207 0 $self->read2($file2read);
208 }
209  
210 20 return $self;
211 }
212  
213 =head2 debug
214  
215 Get/set the debug level. Default is 0.
216  
217 =head2 verbose
218  
219 Get/set flags affecting the verbosity of the program.
220  
221 =cut
222  
223 =head2 get_opt_names
224  
225 Class method.
226  
227 Returns array ref of all the option (method) names supported.
228  
229 =cut
230  
231 0 ▶sub get_opt_names {
Subroutine get_opt_names: not called CC=1 SCAR=6.9
Pod documented
232 0 return [@Opts];
233 }
234  
235 134 ▶sub _set {
Subroutine _set: called CC=9 SCAR=23.1
236 134 my $self = shift;
237 134 my ( $key, $val, $append ) = @_;
238  
239 134 ▶ if ( $key eq 'file' or $key eq 'debug' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'file' or $key eq 'debug') { } 0 134 134
elsif (exists $unique{$key}) { } 7 127 134
Condition33%1/3
$key eq "file" or $key eq "debug"
l!l&&r!l&&!r
00134
MC/DC0%0/2
$key eq "file" or $key eq "debug"
$key eq "file" $key eq "debug"
Truth table33%1/3
A or B
A= $key eq "file"B= $key eq "debug"
ABresult
1X1
011
000
240 0 return $self->{$key} = $val;
241 }
242 elsif ( exists $unique{$key} ) {
243 7 return $self->_name_hash( $key, $val );
244 }
245  
246 127 ▶ $self->{$key} = [] unless defined $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$key} 126 1 127
247  
248 # save everything as an array ref regardless of input
249 127 ▶ if ( ref $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 7 120 127
250 7 ▶ if ( ref($val) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 7 0 7
251 7 ▶ $self->{$key} = $append ? [ @{ $self->{$key} }, @$val ] : $val;
Branch: 1 branch
BranchTrueFalseTotal
$append ? : 7 0 7
252 }
253 else {
254 0 croak "$key cannot accept a " . ref($val) . " ref as a value";
255 }
256 }
257 else {
258 120 ▶ $self->{$key} = $append ? [ @{ $self->{$key} }, $val ] : [$val];
Branch: 1 branch
BranchTrueFalseTotal
$append ? : 98 22 120
259 }
260  
261 }
262  
263 107 ▶sub _get {
Subroutine _get: called CC=3 SCAR=11.0
264 107 my $self = shift;
265 107 my $key = shift;
266  
267 107 ▶ if ( exists $unique{$key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $unique{$key}) { } 4 103 107
elsif (exists $takes_single_value{$key}) { } 2 101 103
268 4 return $self->_name_hash($key);
269 }
270 elsif ( exists $takes_single_value{$key} ) {
271 2 return $self->{$key}->[0];
272 }
273 else {
274 101 return $self->{$key};
275 }
276 }
277  
278 11 ▶sub _name_hash {
Subroutine _name_hash: called CC=5 SCAR=16.1
279 11 my $self = shift;
280 11 my $name = shift;
281  
282 11 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 7 4 11
283  
284 #carp "setting $name => " . join(', ', @_);
285 7 for my $v (@_) {
286 7 ▶ my @v = ref $v ? @$v : ($v);
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 0 7 7
287 7 $self->{$name}->{ lc($_) } = 1 for @v;
288 }
289 }
290 else {
291  
292 #carp "getting $name -> " . join(', ', sort keys %{$self->{$name}});
293  
294 }
295  
296 11 return [ sort keys %{ $self->{$name} } ];
297 }
298  
299 =head2 read2( I<path/file> )
300  
301 Reads version 2 compatible config file and stores in current object.
302 Returns parsed config file as a hashref or undef on failure to parse.
303  
304 Example:
305  
306 use SWISH::Prog::Config;
307 my $config = SWISH::Prog::Config->new();
308 my $parsed = $config->read2( 'my/file.cfg' );
309
310 # should print same thing
311 print $config->WordCharacters->[0], "\n";
312 print $parsed->{WordCharacters}, "\n";
313
314
315 =cut
316  
317 11 ▶sub read2 {
Subroutine read2: called CC=4 SCAR=14.0
Pod documented
318 11 my $self = shift;
319 11 ▶ my $file = shift or croak "version2 type file required";
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or croak("version2 type file required") 11 0 11
320  
321 # stringify $file in case it is an object
322 11 my $buf = read_file("$file");
323  
324 # filter include syntax to work with Config::General's
325 11 $buf =~ s,IncludeConfigFile (.+?)\n,Include $1\n,g;
326  
327 11 my ( $volume, $dir, $filename ) = File::Spec->splitpath($file);
328  
329 11 ▶ my $c = Config::General->new(
Branch: 1 branch
BranchTrueFalseTotal
my $c = "Config::General"->new("-String", $buf, "-IncludeRelative", 1, "-ConfigPath", [$dir], "-ApacheCompatible", 1) or return 11 0 11
330 -String => $buf,
331 -IncludeRelative => 1,
332 -ConfigPath => [$dir],
333 -ApacheCompatible => 1,
334 ) or return;
335  
336 11 my %conf = $c->getall;
337  
338 11 for ( keys %conf ) {
339 111 my $v = $conf{$_};
340 111 $self->$_( $v, 1 );
341 }
342  
343 11 return \%conf;
344 }
345  
346 =head2 write2( I<path/file> [,I<prog_mode>] )
347  
348 Writes version 2 compatible config file.
349  
350 If I<path/file> is omitted, a temp file will be
351 written using File::Temp.
352  
353 If I<prog_mode> is true all config directives
354 inappropriate for the -S prog mode in the Native::Indexer
355 are skipped. The default is false.
356  
357 Returns full path to file.
358  
359 Full path is also available via file() method.
360  
361 =head2 file
362  
363 Returns name of the file written by write2().
364  
365 =cut
366  
367 1 ▶sub write2 {
Subroutine write2: called CC=5 SCAR=17.1
Pod documented
368 1 my $self = shift;
369 1 ▶ my $file = shift || File::Temp->new();
Condition33%1/3
shift() || 'File::Temp'->new
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || 'File::Temp'->new
shift() 'File::Temp'->new
Truth table33%1/3
A || B
A= shift()B= 'File::Temp'->new
ABresult
1X1
011
000
370 1 ▶ my $prog_mode = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
371  
372 # stringify both
373 1 write_file( "$file", $self->stringify($prog_mode) );
374  
375 #warn "$self";
376  
377 1 ▶ warn "wrote config file $file" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
378  
379 # remember file. this especially crucial for File::Temp
380 # since we want it to hang around till $self is DESTROYed
381 1 ▶ if ( ref $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) 1 0 1
382 1 $self->{__tmpfile} = $file;
383 }
384 1 $self->file("$file");
385  
386 1 return $self->file;
387 }
388  
389 =head2 write3( I<path/file> )
390  
391 Write config object to file in SWISH::3::Config XML format.
392  
393 =cut
394  
395 0 ▶sub write3 {
Subroutine write3: not called CC=3 SCAR=24.8
Pod documented
396 0 my $self = shift;
397 0 ▶ my $file = shift or croak "file required";
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or croak("file required") 0 0 0
398  
399 0 write_file( "$file", $self->ver2_to_ver3 );
400  
401 0 ▶ warn "wrote config file $file" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
402  
403 0 return $self;
404 }
405  
406 =head2 as_hash
407  
408 Returns current Config object as a hash ref.
409  
410 =cut
411  
412 0 ▶sub as_hash {
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
413 0 my $self = shift;
414 0 my $c = Config::General->new( -String => $self->stringify );
415 0 return { $c->getall };
416 }
417  
418 =head2 all_metanames
419  
420 Returns array ref of all MetaNames, regardless of whether they
421 are declared as MetaNames, MetaNamesRank or MetaNameAlias config
422 options.
423  
424 =cut
425  
426 0 ▶sub all_metanames {
Subroutine all_metanames: not called CC=5 SCAR=34.0
Pod documented
427 0 my $self = shift;
428 0 my @meta = @{ $self->MetaNames };
429 0 ▶ for my $line ( @{ $self->MetaNamesRank || [] } ) {
Condition0%0/2
$self->MetaNamesRank || []
l!l
00
MC/DC0%0/1
$self->MetaNamesRank || []
$self->MetaNamesRank
Truth table0%0/2
A
A= $self->MetaNamesRank
Aresult
11
00
430 0 my ( $bias, @list ) = split( m/\ +/, $line );
431 0 push( @meta, @list );
432 }
433 0 ▶ for my $line ( @{ $self->MetaNameAlias || [] } ) {
Condition0%0/2
$self->MetaNameAlias || []
l!l
00
MC/DC0%0/1
$self->MetaNameAlias || []
$self->MetaNameAlias
Truth table0%0/2
A
A= $self->MetaNameAlias
Aresult
11
00
434 0 my ( $orig, @alias ) = split( m/\ +/, $line );
435 0 push( @meta, @alias );
436 }
437 0 return \@meta;
438 }
439  
440 =head2 stringify([I<prog_mode>])
441  
442 Returns object as version 2 formatted scalar.
443  
444 If I<prog_mode> is true skips inappropriate directives for
445 running the Native::Indexer. Default is false. See write2().
446  
447 This method is used to overload the object for printing, so these are
448 equivalent:
449  
450 print $config->stringify;
451 print $config;
452  
453 =cut
454  
455 1 ▶sub stringify {
Subroutine stringify: called CC=12 SCAR=26.1
Pod documented
456 1 my $self = shift;
457 1 ▶ my $prog_mode = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
458 1 my @config;
459  
460 # must pass metanames and properties first, since others may depend on them
461 # in swish config parsing.
462 1 for my $method ( keys %unique ) {
463 4 my $v = $self->$method;
464  
465 4 ▶ next unless scalar(@$v);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$v 3 1 4
466  
467 #carp "adding $method to config";
468 1 push( @config, "$method " . join( ' ', @$v ) );
469 }
470  
471 1 for my $name (@Opts) {
472 84 ▶ next if exists $unique{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $unique{$name} 4 80 84
473 80 ▶ if ( $prog_mode && $name =~ m/^File/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prog_mode and $name =~ /^File/) 0 80 80
Condition33%1/3
$prog_mode and $name =~ /^File/
!ll&&!rl&&r
8000
MC/DC0%0/2
$prog_mode and $name =~ /^File/
$prog_mode $name =~ /^File/
Truth table33%1/3
A and B
A= $prog_modeB= $name =~ /^File/
ABresult
0X0
100
111
474 0 next;
475 }
476  
477 80 my $v = $self->$name;
478 80 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 77 3 80
479 3 ▶ if ( ref $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 3 0 3
480 3 push( @config, "$name $_" ) for @$v;
481 }
482 else {
483 0 push( @config, "$name $v" );
484 }
485 }
486  
487 1 my $buf = join( "\n", @config ) . "\n";
488  
489 1 ▶ print STDERR $buf if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
490  
491 1 return $buf;
492 }
493  
494 0 ▶sub _write_utf8 {
Subroutine _write_utf8: not called CC=1 SCAR=6.9
495 0 my ( $self, $file, $buf ) = @_;
496 0 binmode $file, ':utf8';
497 0 print {$file} $buf;
498 }
499  
500 =head2 ver2_to_ver3( I<file> )
501  
502 Utility method for converting Swish-e version 2 style config files
503 to SWISH::3::Config XML style.
504  
505 Converts I<file> to XML format and returns as XML string.
506  
507 my $xmlconf = $config->ver2_to_ver3( 'my/file.config' );
508  
509 If I<file> is omitted, uses the current values in the calling object.
510  
511 =cut
512  
513 10 ▶sub ver2_to_ver3 {
Subroutine ver2_to_ver3: called CC=89 SCAR=48.3
Pod documented
514 10 my $self = shift;
515 10 my $file = shift;
516 10 ▶ my $no_timestamp = shift || 0;
Condition50%1/2
shift() || 0
l!l
100
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
517  
518 # list of config directives that take arguments to the opt value
519 # i.e. the directive has 3 or more parts
520 10 my %takes_arg = map { $_ => 1 } qw(
521 DefaultContents
522 ExtractPath
523 FileFilter
524 FileRules
525 IgnoreWords
526 IndexContents
527 MetaNameAlias
528 MetaNamesRank
529 PropertyNameAlias
530 PropertyNamesMaxLength
531 PropertyNamesSortKeyLength
532 ReplaceRules
533 StoreDescription
534 TagAlias
535 Words
536 );
537  
538 10 my %parser_map = (
539 'XML' => 'application/xml',
540 'HTML' => 'text/html',
541 'TXT' => 'text/plain',
542 );
543  
544 10 my %remap = (
545 'IndexDir' => 'SourceDir',
546 'IndexOnly' => 'SourceOnly',
547 'IndexReport' => 'Verbosity',
548 );
549  
550 10 my %unsupported = map { $_ => 1 } qw(
551 BeginCharacters
552 Delay
553 EndCharacters
554 EquivalentServer
555 FileFilter
556 FileFilterMatch
557 FileMatch
558 FileRules
559 FileRules
560 IgnoreFirstChar
561 IgnoreLastChar
562 MaxDepth
563 SpiderDirectory
564 SwishProgParameters
565 TmpDir
566 WordCharacters
567 );
568 10 my $disclaimer = "<!-- WARNING: CONFIG ignored by Swish3 -->\n ";
569  
570 10 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
571 10 ▶ my $config = $file ? $class->new->read2($file) : $self->as_hash;
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 10 0 10
572 10 ▶ my $time = $no_timestamp ? '' : localtime();
Branch: 1 branch
BranchTrueFalseTotal
$no_timestamp ? : 10 0 10
573  
574 # if we were not passed a file name, all the config resolution
575 # has already been done, so do not perpetuate.
576 10 ▶ if ( !$file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file) 0 10 10
577 0 delete $config->{IncludeConfigFile};
578 }
579  
580 10 my $xml = <<EOF;
581 <?xml version="1.0" encoding="UTF-8"?>
582 <!-- converted with SWISH::Prog::Config ver2_to_ver3() $time -->
583 <swish>
584 EOF
585  
586 10 ▶ my $debug = ref($self) ? $self->debug : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 10 10
587 10 ▶ $debug and warn dump $config;
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn SWISH::Prog::Config::dump($config) 0 10 10
588  
589 # first convert the $config ver2 hash into a ver3 hash
590 10 my %conf3 = (
591 MetaNames => {},
592 PropertyNames => {},
593 Index => { Format => ['Native'], },
594 MIME => {},
595 Parsers => {},
596 TagAlias => {},
597 );
598  
599 #warn dump $config;
600  
601 10 KEY: for my $k ( sort keys %$config ) {
602 107 ▶ my @args = ref $config->{$k} ? @{ $config->{$k} } : ( $config->{$k} );
Branch: 1 branch
BranchTrueFalseTotal
ref $config->{$k} ? : 7 100 107
603  
604 107 ▶ $debug and warn "$k => " . dump( \@args );
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn "$k => " . SWISH::Prog::Config::dump(\@args) 0 107 107
605  
606 107 ▶ if ( $k eq 'MetaNames' ) {
Branch: 19 branches
BranchTrueFalseTotal
if ($k eq 'MetaNames') { } 3 104 107
elsif ($k eq 'MetaNamesRank') { } 1 103 104
elsif ($k eq 'TagAlias') { } 1 102 103
elsif ($k eq 'MetaNameAlias') { } 1 101 102
elsif ($k eq 'PropertyNames') { } 1 100 101
elsif ($k eq 'PropertyNamesCompareCase') { } 1 99 100
elsif ($k eq 'PropertyNamesIgnoreCase') { } 1 98 99
elsif ($k eq 'PropertyNamesNoStripChars') { } 1 97 98
elsif ($k eq 'PropertyNamesNumeric') { } 1 96 97
elsif ($k eq 'PropertyNamesDate') { } 1 95 96
elsif ($k eq 'PropertyNameAlias') { } 1 94 95
elsif ($k eq 'PropertyNamesMaxLength') { } 1 93 94
elsif ($k eq 'PropertyNamesSortKeyLength') { } 1 92 93
elsif ($k eq 'PreSortedIndex') { } 0 92 92
elsif ($k eq 'StoreDescription') { } 0 92 92
elsif ($k eq 'IndexContents') { } 2 90 92
elsif ($k eq 'DefaultContents') { } 1 89 90
elsif (exists $remap{$k}) { } 18 71 89
elsif ($k =~ /^Index(\w+)/) { } 7 64 71
607 3 for my $line (@args) {
608 3 for my $metaname ( split( m/\ +/, $line ) ) {
609 5 ▶ $conf3{'MetaNames'}->{$metaname} ||= {};
Condition50%1/2
$conf3{"MetaNames"}{$metaname} ||= {}
l!l
05
MC/DC0%0/1
$conf3{"MetaNames"}{$metaname} ||= {}
$conf3{"MetaNames"}{$metaname}
Truth table50%1/2
A
A= $conf3{"MetaNames"}{$metaname}
Aresult
11
00
610 }
611 }
612 }
613 elsif ( $k eq 'MetaNamesRank' ) {
614 1 for my $pair (@args) {
615 2 my ( $bias, $names ) = ( $pair =~ m/^([\-\d]+) +(.+)$/ );
616 2 for my $name ( split( m/\ +/, $names ) ) {
617 2 $conf3{'MetaNames'}->{$name}->{bias} = $bias;
618 }
619 }
620 }
621 elsif ( $k eq 'TagAlias' ) {
622 1 for my $pair (@args) {
623 1 my ( $name, $aliases ) = ( $pair =~ m/^(\S+) +(.+)$/ );
624 1 for my $alias ( split( m/\ +/, $aliases ) ) {
625 1 $conf3{'TagAlias'}->{$alias} = $name;
626 }
627 }
628 }
629 elsif ( $k eq 'MetaNameAlias' ) {
630 1 for my $pair (@args) {
631 2 my ( $name, $aliases ) = ( $pair =~ m/^(\S+) +(.+)$/ );
632 2 for my $alias ( split( m/\ +/, $aliases ) ) {
633 2 $conf3{'MetaNames'}->{$alias}->{alias_for} = $name;
634 }
635 }
636 }
637 elsif ( $k eq 'PropertyNames' ) {
638 1 for my $line (@args) {
639 1 for my $name ( split( m/\ +/, $line ) ) {
640 1 ▶ $conf3{'PropertyNames'}->{$name} ||= {};
Condition50%1/2
$conf3{"PropertyNames"}{$name} ||= {}
l!l
01
MC/DC0%0/1
$conf3{"PropertyNames"}{$name} ||= {}
$conf3{"PropertyNames"}{$name}
Truth table50%1/2
A
A= $conf3{"PropertyNames"}{$name}
Aresult
11
00
641 }
642 }
643 }
644 elsif ( $k eq 'PropertyNamesCompareCase' ) {
645 1 for my $line (@args) {
646 1 for my $name ( split( m/\ +/, $line ) ) {
647 1 $conf3{'PropertyNames'}->{$name}->{ignore_case} = 0;
648 }
649 }
650 }
651 elsif ( $k eq 'PropertyNamesIgnoreCase' ) {
652 1 for my $line (@args) {
653 1 for my $name ( split( m/\ +/, $line ) ) {
654 1 $conf3{'PropertyNames'}->{$name}->{ignore_case} = 1;
655 }
656 }
657 }
658 elsif ( $k eq 'PropertyNamesNoStripChars' ) {
659 1 for my $line (@args) {
660 1 for my $name ( split( m/\ +/, $line ) ) {
661 1 $conf3{'PropertyNames'}->{$name}->{verbatim} = 1;
662 }
663 }
664 }
665 elsif ( $k eq 'PropertyNamesNumeric' ) {
666 1 for my $line (@args) {
667 1 for my $name ( split( m/\ +/, $line ) ) {
668 1 $conf3{'PropertyNames'}->{$name}->{type} = 'int';
669 }
670 }
671 }
672 elsif ( $k eq 'PropertyNamesDate' ) {
673 1 for my $line (@args) {
674 1 for my $name ( split( m/\ +/, $line ) ) {
675 1 $conf3{'PropertyNames'}->{$name}->{type} = 'date';
676 }
677 }
678 }
679 elsif ( $k eq 'PropertyNameAlias' ) {
680 1 for my $pair (@args) {
681 2 my ( $name, $aliases ) = ( $pair =~ m/^(\S+) +(.+)$/ );
682 2 for my $alias ( split( m/\ +/, $aliases ) ) {
683 2 $conf3{'PropertyNames'}->{$alias}->{alias_for} = $name;
684 }
685 }
686 }
687 elsif ( $k eq 'PropertyNamesMaxLength' ) {
688 1 for my $pair (@args) {
689 1 my ( $max, $names ) = ( $pair =~ m/^([\d]+) +(.+)$/ );
690 1 for my $name ( split( m/\ +/, $names ) ) {
691 2 $conf3{'PropertyNames'}->{$name}->{max} = $max;
692 }
693 }
694 }
695 elsif ( $k eq 'PropertyNamesSortKeyLength' ) {
696 1 for my $pair (@args) {
697 1 my ( $len, $names ) = ( $pair =~ m/^([\d]+) +(.+)$/ );
698 1 for my $name ( split( m/\ +/, $names ) ) {
699 2 $conf3{'PropertyNames'}->{$name}->{sort_length} = $len;
700 }
701 }
702 }
703 elsif ( $k eq 'PreSortedIndex' ) {
704 0 for my $line (@args) {
705 0 for my $name ( split( m/\ +/, $line ) ) {
706 0 $conf3{'PropertyNames'}->{$name}->{sort} = 1;
707 }
708 }
709 }
710 elsif ( $k eq 'StoreDescription' ) {
711 0 for my $line (@args) {
712 0 my ( $parser_type, $tag, $len )
713 = ( $line =~ m/^(XML|HTML|TXT)[2\*]? +<(.+?)> ?(\d*)$/ );
714 0 ▶ if ( !$tag ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag) 0 0 0
715 0 warn "unparsed config2 line for StoreDescription: $line";
716 0 next;
717 }
718 $conf3{'PropertyNames'}->{$tag}->{alias_for}
719 0 = 'swishdescription';
720 }
721 }
722  
723 elsif ( $k eq 'IndexContents' ) {
724 2 for my $line (@args) {
725 4 my ( $parser_type, $file_ext )
726 = ( $line =~ m/^(XML|HTML|TXT)[2\*]? +(.+)$/ );
727  
728 4 ▶ if ( !exists $parser_map{$parser_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $parser_map{$parser_type}) 0 4 4
729 0 warn "Unsupported Parser type: $parser_type\n";
730 0 next;
731 }
732  
733 4 for my $ext ( split( m/\ +/, $file_ext ) ) {
734 6 $ext =~ s/^\.//;
735 my $mime
736 = SWISH::Prog::Utils->mime_type( "null.$ext", $ext )
737 6 ▶ || $parser_map{$parser_type};
Condition66%2/3
'SWISH::Prog::Utils'->mime_type("null.$ext", $ext) || $parser_map{$parser_type}
l!l&&r!l&&!r
510
MC/DC0%0/2
'SWISH::Prog::Utils'->mime_type("null.$ext", $ext) || $parser_map{$parser_type}
'SWISH::Prog::Utils'->mime_type("null.$ext", $ext) $parser_map{$parser_type}
Truth table66%2/3
A || B
A= 'SWISH::Prog::Utils'->mime_type("null.$ext", $ext)B= $parser_map{$parser_type}
ABresult
1X1
011
000
738 6 ▶ if ( exists $conf3{Parsers}->{$parser_type}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf3{"Parsers"}{$parser_type} and exists $conf3{"Parsers"}{$parser_type}{$mime}) 0 6 6
Condition33%1/3
exists $conf3{"Parsers"}{$parser_type} and exists $conf3{"Parsers"}{$parser_type}{$mime}
!ll&&!rl&&r
600
MC/DC0%0/2
exists $conf3{"Parsers"}{$parser_type} and exists $conf3{"Parsers"}{$parser_type}{$mime}
exists $conf3{"Parsers"}{$parser_type} exists $conf3{"Parsers"}{$parser_type}{$mime}
Truth table33%1/3
A and B
A= exists $conf3{"Parsers"}{$parser_type}B= exists $conf3{"Parsers"}{$parser_type}{$mime}
ABresult
0X0
100
111
739 and exists $conf3{Parsers}->{$parser_type}->{$mime} )
740 {
741 0 warn
742 "parser type $parser_type already defined for $mime\n";
743 0 next;
744 }
745 6 ▶ if ( exists $parser_map{$parser_type}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parser_map{$parser_type} and $parser_map{$parser_type} eq $mime) 5 1 6
Condition66%2/3
exists $parser_map{$parser_type} and $parser_map{$parser_type} eq $mime
!ll&&!rl&&r
015
MC/DC50%1/2
exists $parser_map{$parser_type} and $parser_map{$parser_type} eq $mime
exists $parser_map{$parser_type} $parser_map{$parser_type} eq $mime
Truth table66%2/3
A and B
A= exists $parser_map{$parser_type}B= $parser_map{$parser_type} eq $mime
ABresult
0X0
100
111
746 and $parser_map{$parser_type} eq $mime )
747 {
748  
749 # already a default
750 5 next;
751 }
752 1 $conf3{Parsers}->{$parser_type}->{$mime} = $ext;
753 1 ▶ if ( exists $conf3{MIME}->{$ext} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf3{"MIME"}{$ext}) 0 1 1
754 0 warn "file extension '$ext' already defined\n";
755 0 next;
756 }
757 1 $conf3{MIME}->{$ext} = $mime;
758 }
759 }
760 }
761 elsif ( $k eq 'DefaultContents' ) {
762 1 my $parser = $args[0];
763 1 $conf3{Parsers}->{default}->{$parser} = $parser;
764 }
765 elsif ( exists $remap{$k} ) {
766 18 push( @{ $conf3{ $remap{$k} } }, @args );
767 }
768 elsif ( $k =~ m/^Index(\w+)/ ) {
769 7 my $tag = $1;
770 7 push( @{ $conf3{'Index'}->{$tag} }, join( ' ', @args ) );
771 }
772  
773 else {
774 64 push( @{ $conf3{$k} }, @args );
775 }
776  
777 }
778  
779 # now convert %conf3 to XML
780  
781 # deal with these special cases separately
782 10 my $metas = delete $conf3{'MetaNames'};
783 10 my $props = delete $conf3{'PropertyNames'};
784 10 my $index = delete $conf3{'Index'};
785 10 my $mimes = delete $conf3{'MIME'};
786 10 my $parsers = delete $conf3{'Parsers'};
787 10 my $tag_alias = delete $conf3{'TagAlias'};
788  
789 10 for my $k ( sort keys %conf3 ) {
790 82 my $key = to_utf8($k);
791 82 for my $v ( @{ $conf3{$k} } ) {
792 84 my $val = $XML->escape( to_utf8($v) );
793 84 my $note = '';
794  
795 # $key fails to register in exists() below under 5.10
796 84 ▶ if ( exists $unsupported{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $unsupported{$k}) 40 44 84
797 40 $note = $disclaimer;
798 40 $note =~ s/CONFIG/$key/;
799 }
800 84 $xml .= " $note<$key>$val</$key>\n";
801 }
802 }
803  
804 10 ▶ if ( keys %$metas ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$metas) 3 7 10
805 3 $xml .= " <MetaNames>\n";
806 3 for my $name ( sort keys %$metas ) {
807 8 my $uname = to_utf8($name);
808 $xml .= sprintf( " <%s />\n",
809 8 $self->_make_tag( $uname, $metas->{$name} ) );
810 }
811 3 $xml .= " </MetaNames>\n";
812 }
813 10 ▶ if ( keys %$props ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$props) 1 9 10
814 1 $xml .= " <PropertyNames>\n";
815 1 for my $name ( sort keys %$props ) {
816 10 my $uname = to_utf8($name);
817 $xml .= sprintf( " <%s />\n",
818 10 $self->_make_tag( $uname, $props->{$name} ) );
819 }
820 1 $xml .= " </PropertyNames>\n";
821 }
822  
823 10 $xml .= " <Index>\n";
824 10 for my $tag ( sort keys %$index ) {
825 17 for my $val ( @{ $index->{$tag} } ) {
826 17 $xml .= sprintf( " <%s>%s</%s>\n", $tag, $XML->escape($val),
827 $tag );
828 }
829 }
830 10 ▶ if ( $conf3{FuzzyIndexingMode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf3{"FuzzyIndexingMode"}) 0 10 10
831 0 ▶ $debug
Branch: 1 branch
BranchTrueFalseTotal
$debug and warn qq[got FuzzyIndexingMode: $conf3{"FuzzyIndexingMode"}[0]] 0 0 0
832 and warn "got FuzzyIndexingMode: $conf3{FuzzyIndexingMode}->[0]";
833 $xml .= sprintf(
834 " <%s>%s</%s>\n",
835 "Stemmer",
836 $XML->escape(
837 0 $self->get_stemmer_lang( $conf3{FuzzyIndexingMode}->[0] )
838 ),
839 "Stemmer"
840 );
841 }
842 10 $xml .= " </Index>\n";
843  
844 10 ▶ if ( keys %$mimes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$mimes) 1 9 10
845 1 $xml .= " <MIME>\n";
846 1 for my $ext ( sort keys %$mimes ) {
847 1 my $mime = $mimes->{$ext};
848 1 $xml .= sprintf( " <%s>%s</%s>\n",
849 $XML->tag_safe($ext), $XML->escape($mime),
850 $XML->tag_safe($ext) );
851 }
852 1 $xml .= " </MIME>\n";
853 }
854  
855 10 ▶ if ( keys %$parsers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$parsers) 2 8 10
856 2 $xml .= " <Parsers>\n";
857 2 for my $parser ( sort keys %$parsers ) {
858 2 for my $mime ( sort keys %{ $parsers->{$parser} } ) {
859 2 $xml .= sprintf( " <%s>%s</%s>\n",
860 $XML->tag_safe($parser),
861 $XML->escape($mime), $XML->tag_safe($parser) );
862 }
863 }
864 2 $xml .= " </Parsers>\n";
865 }
866  
867 10 ▶ if ( keys %$tag_alias ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$tag_alias) 1 9 10
868 1 $xml .= " <TagAlias>\n";
869 1 for my $alias ( sort keys %$tag_alias ) {
870 1 my $name = $tag_alias->{$alias};
871 1 $xml .= sprintf( " <%s>%s</%s>\n",
872 $XML->tag_safe($alias),
873 $XML->escape($name), $XML->tag_safe($alias) );
874 }
875 1 $xml .= " </TagAlias>\n";
876 }
877  
878 10 $xml .= "</swish>\n";
879  
880 10 return $xml;
881  
882 }
883  
884 18 ▶sub _make_tag {
Subroutine _make_tag: called CC=1 SCAR=0.0
885 18 my ( $self, $tag, $attrs ) = @_;
886 18 return $XML->tag_safe($tag) . $XML->attr_safe($attrs);
887 }
888  
889 =head2 get_stemmer_lang([ I<fuzzymode> ])
890  
891 Returns the 2-letter language code for the Snowball stemmer
892 corresponding to I<fuzzymode>. If I<fuzzymode> is not defined,
893 calls FuzzyIndexingMode() method on the config object.
894  
895 =cut
896  
897 0 ▶sub get_stemmer_lang {
Subroutine get_stemmer_lang: not called CC=5 SCAR=34.0
Pod documented
898 0 my $self = shift;
899 0 ▶ my $lang = shift || $self->FuzzyIndexingMode;
Condition0%0/3
shift() || $self->FuzzyIndexingMode
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->FuzzyIndexingMode
shift() $self->FuzzyIndexingMode
Truth table0%0/3
A || B
A= shift()B= $self->FuzzyIndexingMode
ABresult
1X1
011
000
900 0 ▶ $self->debug and warn "get_stemmer_lang for '$lang'";
Branch: 1 branch
BranchTrueFalseTotal
$self->debug and warn "get_stemmer_lang for '${lang}'" 0 0 0
901 0 ▶ if ( $lang and $lang =~ m/^Stemming_(\w\w)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and $lang =~ /^Stemming_(\w\w)/) 0 0 0
Condition0%0/3
$lang and $lang =~ /^Stemming_(\w\w)/
!ll&&!rl&&r
000
MC/DC0%0/2
$lang and $lang =~ /^Stemming_(\w\w)/
$lang $lang =~ /^Stemming_(\w\w)/
Truth table0%0/3
A and B
A= $langB= $lang =~ /^Stemming_(\w\w)/
ABresult
0X0
100
111
902 0 return $1;
903 }
904 0 return 'none';
905 }
906  
907 261 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=14.0
908 261 my $self = shift;
909 261 my $method = our $AUTOLOAD;
910 261 $method =~ s/.*://;
911 261 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 20 241 261
912 241 ▶ if ( !exists $Opts{$method} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Opts{$method}) 0 241 241
913 0 croak("method '$method' not implemented in $self");
914 }
915 241 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 134 107 241
916 134 return $self->_set( $method, @_ );
917 }
918 else {
919 107 return $self->_get($method);
920 }
921 }
922  
923 16 1;
924  
925 __END__
« Prog/Headers.pm ↑ Summary Prog/Aggregator/Object.pm »