1 package WebPAC::Output::Webpacus;
6 use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
7 __PACKAGE__->mk_accessors(qw(
14 use Data::Dump qw/dump/;
15 use WebPAC::Common qw/force_array/;
24 WebPAC::Output::Webpacus - integrate WebPAC front-end with Jifty back-end
28 our $VERSION = '0.03';
32 Does black magic to sync data between WebPAC and Webpacus, web front-end
39 my $output = new WebPAC::Output::Webpacus({
40 path => '/path/to/Webpacus',
53 my $log = $self->_get_logger;
55 foreach my $p (qw/path database/) {
56 $log->logdie("need $p") unless ($self->$p);
59 my $path = $self->path;
61 $log->logdie("Webpacus path $path not found: $!") unless -d $path;
63 my $config_path = "$path/etc/config.yml";
65 $log->logdie("expected Webpacus config at $config_path: $!") unless -e $config_path;
85 my $log = $self->_get_logger;
86 $log->logdie("need id") unless defined $id;
87 $log->logdie("need ds") unless $ds;
89 $log->debug("id: $id ds = ",sub { dump($ds) });
93 foreach my $type ( $self->consume_outputs ) {
95 my $hash = $self->ds_to_hash( $ds, $type ) || next;
97 $log->debug("$type has following data: ", sub { dump( $hash ) });
99 foreach my $f ( keys %$hash ) {
100 $self->{fields}->{$type}->{$f}++;
101 $stat->{$type}->{$f}++;
105 $log->debug("this record added following fields: ", sub { dump( $stat ) });
114 my $affected = $index->finish;
116 Returns of records saved in total
123 my $log = $self->_get_logger();
125 my $fields = $self->{fields} || confess "no fields?";
127 $log->debug("fields = ", sub { dump $fields });
129 $log->info("init Jifty");
130 my $path = $self->path || confess "no path?";
131 my $webpac_dir = getcwd();
132 chdir $path || $log->logdie("can't chdir($path) $!");
137 foreach my $type ( $self->consume_outputs ) {
138 next unless defined $fields->{$type};
139 $affected += $self->_sync_field(
140 $self->database, $type, $fields->{$type}
146 my $glue_path = "$path/lib/Webpacus/Webpac.pm";
148 $log->debug("creating clue class Webpacus::Webpac at $glue_path");
150 my $glue = <<"_END_OF_GLUE_";
151 package Webpacus::Webpac;
155 Webpacus::Webpac - configuration exported from WebPAC
162 sub index_path { '/data/webpac2/var/kinosearch/' };
167 $log->debug("glue source:\n$glue");
169 write_file( $glue_path, $glue ) || $log->logdie("can't create $glue_path: $!");
177 my ( $database, $type, $field_hash ) = @_;
179 my $path = $self->path || confess "no path?";
181 my $log = $self->_get_logger();
183 my $model = 'Webpacus::Model::' . ucfirst($type);
184 $log->info("sync $model");
186 $log->debug("field_hash = ",sub { dump($field_hash) });
188 my @field_names = keys %$field_hash;
190 if ( ! @field_names ) {
191 $log->warn("normalization rules don't produce any data for search!");
195 $log->info("syncing $database $type fields: ", join(", ", @field_names));
197 my $system_user = Webpacus::CurrentUser->superuser;
198 my $o = $model->new(current_user => $system_user);
200 my ( $count, $new, $updated ) = ( 0, 0, 0 );
202 foreach my $field ( @field_names ) {
203 my $items = $field_hash->{$field} || confess "no field?";
205 my ( $id, $msg ) = $o->load_by_cols(
207 from_database => $database,
211 $o->set_items( $items );
212 $log->debug("updated $database $type field: $field [$items] ID: $id $msg");
215 $log->debug("adding $database $type field: $field [$items] $msg");
218 from_database => $database,
227 $log->info("synced $count fields (",join(", ", @field_names),") from $database with Webpacus ($new new/$updated updated) at $path");
233 =head2 consume_outputs
235 Returns array with names of supported output types for this module
239 sub consume_outputs {
240 return qw/search sorted/;
245 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
247 =head1 COPYRIGHT & LICENSE
249 Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
251 This program is free software; you can redistribute it and/or modify it
252 under the same terms as Perl itself.