1 package WebPAC::Output::SWISH;
8 use base qw/WebPAC::Common Class::Accessor/;
9 __PACKAGE__->mk_accessors(qw(
17 use File::Path qw/mkpath/;
18 use Data::Dump qw/dump/;
25 WebPAC::Output::SWISH - Create swish-e full text index
29 our $VERSION = '0.01';
33 Create full text index using swish-e indexer from data with
40 my $out = new WebPAC::Output::SWISH({
50 name of database from which data comes
54 Name of database will be used to form URI of documents in index.
58 our $dir = 'var/swish';
63 my $log = $self->_get_logger;
65 my $database = $self->database || $log->logdie("need database");
67 mkpath $dir if ! -e $dir;
69 my $path = "$dir/$database.conf";
71 open(my $conf, '>', $path) || die "can't open $path: $!";
73 print $conf <<"DEFAULT_SWISH_CONF";
74 # swish-e config file for $database
78 # input file definition
82 MetaNames xml swishdocpath
85 #XMLClassAttributes type
86 UndefinedMetaTags auto
87 UndefinedXMLAttributes auto
89 IndexFile $dir/$database
91 # Croatian ISO-8859-2 characters to unaccented equivalents
92 #TranslateCharacters ¹©ðÐèÈæƾ® ssddcccczz
94 # store data into index
103 close($conf) || die "can't write config $path: $!";
105 $self->index_path( "$dir/$database" );
107 my $swish = "swish-e -S prog -c $path";
108 open( $self->{_swish_fh}, '|-', $swish ) || die "can't open pipe to $swish: $!";
110 $log->info( "created $path ", -s $path, " bytes for ", $self->index_path );
114 $self ? return $self : return undef;
119 my $path = $out->index_path;
123 $out->add( 42, $ds );
127 my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
128 my $escape_re = join '|' => keys %escape;
131 my ($self,$id,$ds) = @_;
133 die "need input" unless $self->input;
135 my $log = $self->_get_logger;
136 $log->debug("id: $id ds = ",sub { dump($ds) });
138 my $database = $self->database || $log->logconfess('no database in $self');
140 my $uri = $self->database . '/' . $self->input . "/$id";
141 $log->debug("creating $uri");
143 # filter all tags which have type defined
144 my $type = $self->type || 'search';
146 ref($ds->{$_}) eq 'HASH' && defined( $ds->{$_}->{$type} )
149 $log->debug("tags = ", join(",", @tags));
151 return unless (@tags);
156 foreach ( 'database', 'input' ) {
157 $xml .= "<$_>" . $self->$_ . "</$_>";
158 $data->{$_} = $self->$_;
161 foreach my $tag (@tags) {
163 my $r = ref $ds->{$tag}->{$type};
164 die "tag $tag type $type not ARRAY but '$r' = ",dump( $ds->{$tag}->{$type} ) unless $r eq 'ARRAY';
166 my $vals = join(" ", @{ $ds->{$tag}->{$type} });
170 $vals =~ s/($escape_re)/$escape{$1}/gs;
171 # BW & EW are our markers for tag boundry
172 $xml .= qq{<$tag><![CDATA[BW $vals EW]]></$tag>};
174 $self->{stats}->{attr}->{$tag}++;
175 $self->{stats}->{input}->{ $self->input }->{$tag}++;
177 $data->{$tag} = $vals;
180 # serialize to JSON instead of YAML because we will loose whitespace
181 $data = to_json($data);
182 $xml .= qq{<data><![CDATA[$data]]></data>};
184 $xml .= qq{</all>\n};
186 my $len = length($xml);
188 my $fh = $self->{_swish_fh} || die "_swish_fh missing";
190 print $fh "Path-Name: $uri\nContent-Length: $len\nDocument-Type: XML\n\n$xml" or
191 die "can't add $uri: $@\n$xml";
200 Dump attributes used on disk
206 my $log = $self->_get_logger();
208 my $path = $dir . '/' . $self->{database} . '.yaml';
209 YAML::DumpFile( $path, $self->{stats} );
210 $log->info("created $path ", -s $path, " bytes");
211 $log->debug( dump( $self->{stats} ) );
213 close( $self->{_swish_fh} ) || die "can't close index ", $self->index_path, ": $!";
218 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
220 =head1 COPYRIGHT & LICENSE
222 Copyright 2004-2009 Dobrica Pavlinusic, All Rights Reserved.
224 This program is free software; you can redistribute it and/or modify it
225 under the same terms as Perl itself.