1 package WebPAC::Output::SWISH;
8 use base qw/WebPAC::Common Class::Accessor/;
9 __PACKAGE__->mk_accessors(qw(
16 use File::Path qw/make_path/;
17 use Data::Dump qw/dump/;
23 WebPAC::Output::SWISH - Create swish-e full text index
27 our $VERSION = '0.01';
31 Create full text index using swish-e indexer from data with
38 my $out = new WebPAC::Output::SWISH({
48 name of database from which data comes
52 Name of database will be used to form URI of documents in index.
56 our $dir = 'var/swish';
61 my $log = $self->_get_logger;
63 my $database = $self->database || $log->logdie("need database");
65 make_path $dir if ! -e $dir;
67 my $path = "$dir/$database.conf";
69 open(my $conf, '>', $path) || die "can't open $path: $!";
71 print $conf <<"DEFAULT_SWISH_CONF";
72 # swish-e config file for $database
76 # input file definition
80 MetaNames xml swishdocpath
83 #XMLClassAttributes type
84 UndefinedMetaTags auto
85 UndefinedXMLAttributes auto
87 IndexFile $dir/$database
89 # Croatian ISO-8859-2 characters to unaccented equivalents
90 #TranslateCharacters ¹©ðÐèÈæƾ® ssddcccczz
99 close($conf) || die "can't write config $path: $!";
101 $self->index_path( "$dir/$database" );
103 my $swish = "swish-e -S prog -c $path";
104 open( $self->{_swish_fh}, '|-', $swish ) || die "can't open pipe to $swish: $!";
106 $log->info( "created $path ", -s $path, " bytes for ", $self->index_path );
110 $self ? return $self : return undef;
115 my $path = $out->index_path;
119 $out->add( 42, $ds );
123 my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"');
124 my $escape_re = join '|' => keys %escape;
127 my ($self,$id,$ds) = @_;
129 my $log = $self->_get_logger;
130 $log->debug("id: $id ds = ",sub { dump($ds) });
132 my $database = $self->database || $log->logconfess('no database in $self');
134 my $uri = $self->database . "/$id";
135 $log->debug("creating $uri");
137 # filter all tags which have type defined
138 my $type = $self->type || 'search';
140 ref($ds->{$_}) eq 'HASH' && defined( $ds->{$_}->{$type} )
143 $log->debug("tags = ", join(",", @tags));
145 return unless (@tags);
149 foreach my $tag (@tags) {
151 my $r = ref $ds->{$tag}->{$type};
152 die "tag $tag type $type not ARRAY but '$r' = ",dump( $ds->{$tag}->{$type} ) unless $r eq 'ARRAY';
154 my $vals = join(" ", @{ $ds->{$tag}->{$type} });
158 $vals =~ s/($escape_re)/$escape{$1}/gs;
159 $xml .= qq{<$tag><![CDATA[$vals]]></$tag>};
161 $self->{stats}->{attr}->{$tag}++;
164 $xml .= qq{</xml>\n};
166 my $len = length($xml);
168 my $fh = $self->{_swish_fh} || die "_swish_fh missing";
170 print $fh "Path-Name: $uri\nContent-Length: $len\nDocument-Type: XML\n\n$xml" or
171 die "can't add $uri: $@\n$xml";
178 Dump attributes used on disk
184 my $log = $self->_get_logger();
186 my $path = $dir . '/' . $self->{database} . '.yaml';
187 YAML::DumpFile( $path, $self->{stats} );
188 $log->info("created $path ", -s $path, " bytes");
189 $log->debug( dump( $self->{stats} ) );
191 close( $self->{_swish_fh} ) || die "can't close index ", $self->index_path, ": $!";
196 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
198 =head1 COPYRIGHT & LICENSE
200 Copyright 2004-2009 Dobrica Pavlinusic, All Rights Reserved.
202 This program is free software; you can redistribute it and/or modify it
203 under the same terms as Perl itself.