bdf5b1be28045dcde979329479b6d644a71d5f30
[webpac2] / lib / WebPAC / Output / Webpacus.pm
1 package WebPAC::Output::Webpacus;
2
3 use warnings;
4 use strict;
5
6 use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
7 __PACKAGE__->mk_accessors(qw(
8         path
9         database
10         input
11 ));
12
13 use File::Path;
14 use Data::Dump qw/dump/;
15 use WebPAC::Common qw/force_array/;
16 use Carp qw/confess/;
17 use Cwd;
18 use File::Slurp;
19
20 use Jifty;
21
22 =head1 NAME
23
24 WebPAC::Output::Webpacus - integrate WebPAC front-end with Jifty back-end
25
26 =head1 VERSION
27
28 Version 0.01
29
30 =cut
31
32 our $VERSION = '0.01';
33
34 =head1 SYNOPSIS
35
36 Does black magic to sync data between WebPAC and Webpacus, web front-end
37 implement in Jifty
38
39 =head1 FUNCTIONS
40
41 =head2 new
42
43  my $output = new WebPAC::Output::Webpacus({
44         path => '/path/to/Webpacus',
45         database => 'demo',
46  });
47
48 =head2 init
49
50  $output->init;
51
52 =cut
53
54 sub init {
55         my $self = shift;
56
57         my $log = $self->_get_logger;
58
59         foreach my $p (qw/path database/) {
60                 $log->logdie("need $p") unless ($self->$p);
61         }
62
63         my $path = $self->path;
64
65         $log->logdie("Webpacus path $path not found: $!") unless -d $path;
66
67         my $config_path = "$path/etc/config.yml";
68
69         $log->logdie("expected Webpacus config at $config_path: $!") unless -e $config_path;
70
71         $self->{fields} = {};
72
73 }
74
75
76 =head2 add
77
78 Adds one entry
79
80   $est->add( 42, $ds );
81
82 =cut
83
84 sub add {
85         my $self = shift;
86
87         my ( $id, $ds ) = @_;
88
89         my $log = $self->_get_logger;
90         $log->logdie("need id") unless defined $id;
91         $log->logdie("need ds") unless $ds;
92
93         $log->debug("id: $id ds = ",sub { dump($ds) });
94
95         my $hash = $self->ds_to_hash( $ds, 'sorted' ) || return;
96
97         foreach my $f ( keys %$hash ) {
98                 $self->{fields}->{$f}++;
99         }
100
101         return 1;
102 }
103
104 =head2 finish
105
106 Close index
107
108  $index->finish;
109
110 =cut
111
112 sub finish {
113         my $self = shift;
114
115         my $log = $self->_get_logger();
116
117         my $fields = $self->{fields} || confess "no fields?";
118         my $path = $self->path || confess "no path?";
119
120         my @field_names = %$fields;
121
122         if ( ! @field_names ) {
123                 $log->warn("normalization rules don't produce any data for search!");
124                 return;
125         }
126
127         $log->info("syncing search fields: ", join(", ", @field_names));
128
129         $log->debug("fields = ", sub { dump $fields });
130
131         my $webpac_dir = getcwd();
132
133         chdir $path || $log->logdie("can't chdir($path) $!");
134
135 #       push @INC, $path;
136         Jifty->new();
137         my $system_user = Webpacus::CurrentUser->superuser;
138         my $o = Webpacus::Model::Search->new(current_user => $system_user);
139
140         my ( $count, $new, $updated ) = ( 0, 0, 0 );
141
142         foreach my $field ( @field_names ) {
143                 my $items = $fields->{$field} || confess "no field?";
144
145                 my ( $id, $msg ) = $o->load_by_cols( name => $field );
146
147                 if ( $id ) {
148                         $o->set_items( $items );
149                         $log->debug("updated search field: $field [$items] ID: $id $msg");
150                         $updated++;
151                 } else {
152                         $log->debug("adding search field: $field [$items] $msg");
153                         $o->create(
154                                 name => $field,
155                                 items => $items,
156                         );
157                         $new++;
158                 }
159
160                 $count++;
161         }
162
163         $log->info("synced $count search fields with Webpacus ($new new/$updated updated) at $path");
164
165         my $glue_path = "$path/lib/Webpacus/Webpac.pm";
166
167         $log->debug("creating clue class Webpacus::Webpac at $glue_path");
168
169         my $glue = <<"_END_OF_GLUE_";
170 package Webpacus::Webpac;
171
172 =head1 NAME
173
174 Webpacus::Webpac - configuration exported from WebPAC
175
176 =cut
177
178 use strict;
179 use warnings;
180
181 sub index_path { '/data/webpac2/var/kinosearch/webpacus' };
182
183 1;
184 _END_OF_GLUE_
185
186         $log->debug("glue source:\n$glue");
187
188         write_file( $glue_path, $glue ) || $log->logdie("can't create $glue_path: $!");
189
190         return $count;
191
192 }
193
194
195 =head1 AUTHOR
196
197 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
198
199 =head1 COPYRIGHT & LICENSE
200
201 Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
202
203 This program is free software; you can redistribute it and/or modify it
204 under the same terms as Perl itself.
205
206 =cut
207
208 1;