/[webpac2]/trunk/lib/WebPAC/Output/Webpacus.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/WebPAC/Output/Webpacus.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 993 - (hide annotations)
Sun Nov 4 14:43:47 2007 UTC (16 years, 6 months ago) by dpavlin
File size: 4482 byte(s)
 r1525@llin:  dpavlin | 2007-11-04 15:34:13 +0100
 added consume_outputs which returns all output types this plugin supports.
 This will eventually come in handy for main loop to decide which outputs to
 call...

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

  ViewVC Help
Powered by ViewVC 1.1.26