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