1 package WebPAC::Output::SWISH;
8 use base qw/WebPAC::Common Class::Accessor/;
9 __PACKAGE__->mk_accessors(qw(
16 use File::Path qw/mkpath/;
17 use Data::Dump qw/dump/;
24 WebPAC::Output::SWISH - Create swish-e full text index
28 our $VERSION = '0.01';
32 Create full text index using swish-e indexer from data with
39 my $out = new WebPAC::Output::SWISH({
49 name of database from which data comes
53 Name of database will be used to form URI of documents in index.
57 our $dir = 'var/swish';
62 my $log = $self->_get_logger;
64 my $database = $self->database || $log->logdie("need database");
66 mkpath $dir if ! -e $dir;
68 my $path = "$dir/$database.conf";
70 open(my $conf, '>', $path) || die "can't open $path: $!";
72 print $conf <<"DEFAULT_SWISH_CONF";
73 # swish-e config file for $database
77 # input file definition
81 MetaNames xml swishdocpath
84 #XMLClassAttributes type
85 UndefinedMetaTags auto
86 UndefinedXMLAttributes auto
88 IndexFile $dir/$database
90 # Croatian ISO-8859-2 characters to unaccented equivalents
91 #TranslateCharacters ¹©ðÐèÈæƾ® ssddcccczz
93 # store data into index
102 close($conf) || die "can't write config $path: $!";
104 $self->index_path( "$dir/$database" );
106 my $swish = "swish-e -S prog -c $path";
107 open( $self->{_swish_fh}, '|-', $swish ) || die "can't open pipe to $swish: $!";
109 $log->info( "created $path ", -s $path, " bytes for ", $self->index_path );
113 $self ? return $self : return undef;
118 my $path = $out->index_path;
122 $out->add( 42, $ds );
126 my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
127 my $escape_re = join '|' => keys %escape;
130 my ($self,$id,$ds) = @_;
132 my $log = $self->_get_logger;
133 $log->debug("id: $id ds = ",sub { dump($ds) });
135 my $database = $self->database || $log->logconfess('no database in $self');
137 my $uri = $self->database . "/$id";
138 $log->debug("creating $uri");
140 # filter all tags which have type defined
141 my $type = $self->type || 'search';
143 ref($ds->{$_}) eq 'HASH' && defined( $ds->{$_}->{$type} )
146 $log->debug("tags = ", join(",", @tags));
148 return unless (@tags);
154 foreach my $tag (@tags) {
156 my $r = ref $ds->{$tag}->{$type};
157 die "tag $tag type $type not ARRAY but '$r' = ",dump( $ds->{$tag}->{$type} ) unless $r eq 'ARRAY';
159 my $vals = join(" ", @{ $ds->{$tag}->{$type} });
163 $vals =~ s/($escape_re)/$escape{$1}/gs;
164 $xml .= qq{<$tag><![CDATA[$vals]]></$tag>};
166 $self->{stats}->{attr}->{$tag}++;
168 $data->{$tag} = $vals;
171 # serialize to JSON instead of YAML because we will loose whitespace
172 $data = to_json($data);
173 $xml .= qq{<data><![CDATA[$data]]></data>};
175 $xml .= qq{</xml>\n};
177 my $len = length($xml);
179 my $fh = $self->{_swish_fh} || die "_swish_fh missing";
181 print $fh "Path-Name: $uri\nContent-Length: $len\nDocument-Type: XML\n\n$xml" or
182 die "can't add $uri: $@\n$xml";
191 Dump attributes used on disk
197 my $log = $self->_get_logger();
199 my $path = $dir . '/' . $self->{database} . '.yaml';
200 YAML::DumpFile( $path, $self->{stats} );
201 $log->info("created $path ", -s $path, " bytes");
202 $log->debug( dump( $self->{stats} ) );
204 close( $self->{_swish_fh} ) || die "can't close index ", $self->index_path, ": $!";
209 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
211 =head1 COPYRIGHT & LICENSE
213 Copyright 2004-2009 Dobrica Pavlinusic, All Rights Reserved.
215 This program is free software; you can redistribute it and/or modify it
216 under the same terms as Perl itself.