File Coverage

blib/lib/Sman/Swishe.pm
Criterion Covered Total %
statement 15 52 28.8
branch 0 14 0.0
condition 0 11 0.0
subroutine 5 9 55.5
pod 0 2 0.0
total 20 88 22.7


line stmt bran cond sub pod time code
1             package Sman::Swishe;
2              
3             #$Id$
4              
5 1     1   416 use strict;
  1         1  
  1         23  
6 1     1   2 use warnings;
  1         1  
  1         21  
7 1     1   595 use File::Temp qw/ tempfile /;
  1         11818  
  1         52  
8 1     1   361 use fields qw(config tempfilestounlink);
  1         1034  
  1         4  
9 1     1   319 use Sman; # for $Sman::SMAN_DATA_VERSION
  1         2  
  1         373  
10              
11              
12             # this doesn't need SWISH::API because we're stuffing data
13             # in with the Swish-e exe directly.
14              
15             # call like my $smanswishe = new Sman::Swishe($smanconfig);
16             sub new {
17 0     0 0   my $proto = shift;
18 0   0       my $class = ref($proto) || $proto;
19 0           my $self = {};
20 0           bless ($self, $class);
21 0           $self->{config} = shift; #
22 0           return $self;
23             }
24             # writes a config file to a tmp file,
25             # returns the filename
26              
27             sub WriteConfigFile {
28 0     0 0   my $self = shift;
29 0   0       my $tmpdir = $self->{config}->GetConfigData("TMPDIR") || "/tmp";
30 0           my ($fh, $filename) = tempfile( "$tmpdir/sman-swish-conf.XXXXX" );
31             # this is safe. ?
32 0           push(@ {$self->{tempfilestounlink}}, $filename);
  0            
33             # extra work to make sure this file gets deleted.
34 0           my @names = $self->{config}->GetConfigNames();
35 0           for my $n (@names) {
36             #print "Examining $n..\n";
37 0 0         if($n =~ /^SWISHE_(.+)/i) {
38 0           my ($name, $value) = ($1, $self->{config}->GetConfigData($n));
39 0 0         if ($name =~ m/IndexPointer/) {
40 0           $value =~ s/%V/$Sman::Util::SMAN_DATA_VERSION/;
41             }
42 0 0         print "Config: $name -> '$value'\n" if $self->{config}->GetConfigData("VERBOSE");
43 0           print $fh "$name $value\n";
44             }
45             }
46 0           print $fh $self->_expandaliasesforswisheconf("TITLEALIASES");
47 0           print $fh $self->_expandaliasesforswisheconf("SECALIASES");
48 0           print $fh $self->_expandaliasesforswisheconf("DESCALIASES");
49 0           print $fh $self->_expandaliasesforswisheconf("MANPAGEALIASES");
50            
51 0 0         close($fh) || die "Failure closing temp config file $filename: $!";
52 0           return $filename;
53             }
54             sub _expandaliasesforswisheconf {
55 0     0     my ($self, $name) = @_;
56 0           (my $swishname = $name) =~ s/ALIASES//; # strip off ALIASES
57 0           $swishname = lc($swishname);
58 0 0         $swishname = "swishtitle" if (lc($swishname) eq "title"); # patchup.
59             # our config calls the title prop 'title', to Swish-e its swishtitle.
60             # we did this because swishtitle is Swish-e's default 'title' meta & prop
61 0           my $val = $self->{config}->GetConfigData($name);
62 0 0         if ($val) {
63 0           return "MetaNameAlias $swishname $val\n" .
64             "PropertyNameAlias $swishname $val\n";
65             }
66 0           return "";
67             }
68              
69             # this is handled here, so user DOESN'T delete the file themself
70             # doesn't get invoked on a CNTRL-C apparently
71             sub DESTROY {
72 0     0     my $self = shift;
73 0           for (@ {$self->{tempfilestounlink}} ) {
  0            
74 0 0 0       (-e $_) && (-f $_) && unlink($_) || warn "Couldn't unlink $_: $!";
      0        
75             }
76             }
77            
78             1;
79              
80             =head1 NAME
81              
82             Sman::Swishe - Sman backend to build an sman index with Swish-e
83              
84             =head1 SYNOPSIS
85              
86             # Sman::Swishe needs an Sman::Config object to build a
87             # Swish-e config file from.
88             my $smanconfig = new Sman::Config();
89             $smanconfig->ReadDefaultConfigFile();
90            
91             # now, get Sman::Swishe to write a config file for Swish-e
92             my $smanswishe = new Sman::Swishe($smanconfig);
93             $swisheconfigfile = $smanswishe->WriteConfigFile();
94            
95             # use the swisheconfigfile to build an index with
96             # (see sman-update), then delete the config file most likely.
97            
98             =head1 DESCRIPTION
99              
100             This module creates a custom config file for
101             Swish-e to build the sman index with.
102              
103             =head1 AUTHOR
104              
105             Josh Rabinowitz
106              
107             =head1 SEE ALSO
108              
109             L
110              
111             =cut
112