From af0dcffdc5f46d7a1f5215d33875927dd435b9a1 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Wed, 31 Oct 2007 11:26:10 +0000 Subject: [PATCH] r1399@llin: dpavlin | 2007-10-31 11:19:39 +0100 Webpacus output to transfer searchable fields git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@932 07558da8-63fa-0310-ba24-9fe276d99e06 --- conf/llin.yml | 5 +- conf/log.conf | 2 + lib/WebPAC/Output/Webpacus.pm | 159 ++++++++++++++++++++++++++++++++++ t/5-output-webpacus.t | 55 ++++++++++++ 4 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 lib/WebPAC/Output/Webpacus.pm create mode 100755 t/5-output-webpacus.t diff --git a/conf/llin.yml b/conf/llin.yml index 44fd73b..945c27e 100644 --- a/conf/llin.yml +++ b/conf/llin.yml @@ -338,7 +338,8 @@ databases: input: name: 'foobar' type: marc - path: 'out/marc/ffsf-peri.marc' +# path: 'out/marc/ffsf-peri.marc' + path: 'out/marc/ffkk-libri.marc' encoding: 'cp852' normalize: path: 'conf/normalize/webpacus.pl' @@ -347,4 +348,6 @@ databases: path: 'var/kinosearch' - module: 'Sorted' path: 'var/sorted' + - module: 'Webpacus' + path: '/data/Webpacus2' diff --git a/conf/log.conf b/conf/log.conf index ebd082c..65da489 100644 --- a/conf/log.conf +++ b/conf/log.conf @@ -49,6 +49,8 @@ log4perl.rootLogger=INFO, LOG, SCREEN #log4perl.logger.WebPAC.Output.TT=DEBUG #log4perl.logger.WebPAC.Output.Estraier=DEBUG #log4perl.logger.WebPAC.Output.JSON=DEBUG +#log4perl.logger.WebPAC.Output.KinoSearch=DEBUG +#log4perl.logger.WebPAC.Output.Webpacus=DEBUG #log4perl.logger.WebPAC.Search.Estraier=DEBUG diff --git a/lib/WebPAC/Output/Webpacus.pm b/lib/WebPAC/Output/Webpacus.pm new file mode 100644 index 0000000..5a3d54a --- /dev/null +++ b/lib/WebPAC/Output/Webpacus.pm @@ -0,0 +1,159 @@ +package WebPAC::Output::Webpacus; + +use warnings; +use strict; + +use base qw/WebPAC::Common WebPAC::Output Class::Accessor/; +__PACKAGE__->mk_accessors(qw( + path + database +)); + +use File::Path; +use Data::Dump qw/dump/; +use WebPAC::Common qw/force_array/; +use Carp qw/confess/; +use Cwd; + +use Jifty; + +=head1 NAME + +WebPAC::Output::Webpacus - integrate WebPAC front-end with Jifty back-end + +=head1 VERSION + +Version 0.01 + +=cut + +our $VERSION = '0.01'; + +=head1 SYNOPSIS + +Does black magic to sync data between WebPAC and Webpacus, web front-end +implement in Jifty + +=head1 FUNCTIONS + +=head2 new + + my $output = new WebPAC::Output::Webpacus({ + path => '/path/to/Webpacus', + database => 'demo', + }); + +=head2 init + + $output->init; + +=cut + +sub init { + my $self = shift; + + my $log = $self->_get_logger; + + foreach my $p (qw/path database/) { + $log->logdie("need $p") unless ($self->$p); + } + + my $path = $self->path; + + $log->logdie("Webpacus path $path not found: $!") unless -d $path; + + my $config_path = "$path/etc/config.yml"; + + $log->logdie("expected Webpacus config at $config_path: $!") unless -e $config_path; + + $self->{fields} = {}; + +} + + +=head2 add + +Adds one entry + + $est->add( 42, $ds ); + +=cut + +sub add { + my $self = shift; + + my ( $id, $ds ) = @_; + + my $log = $self->_get_logger; + $log->logdie("need id") unless defined $id; + $log->logdie("need ds") unless $ds; + + $log->debug("id: $id ds = ",sub { dump($ds) }); + + my $hash = $self->ds_to_hash( $ds, 'sorted' ) || return; + + foreach my $f ( keys %$hash ) { + $self->{fields}->{$f}++; + } + + return 1; +} + +=head2 finish + +Close index + + $index->finish; + +=cut + +sub finish { + my $self = shift; + + my $log = $self->_get_logger(); + + $log->info("syncing search fields"); + + my $fields = $self->{fields} || confess "no fields?"; + my $path = $self->path || confess "no path?"; + + $log->debug("fields = ", sub { dump $fields }); + + $log->info("using Webpacus installation: $path"); + + my $webpac_dir = getcwd(); + + chdir $path || $log->logdie("can't chdir($path) $!"); + +# push @INC, $path; + Jifty->new(); + my $system_user = Webpacus::CurrentUser->superuser; + my $o = Webpacus::Model::Search->new(current_user => $system_user); + + my $count = 0; + + foreach my $field ( keys %$fields ) { + $log->debug("adding search field: $field"); + $o->create( name => $field ); # || $log->logdie("can't add $field"); + $count++; + } + + return $count; + +} + + +=head1 AUTHOR + +Dobrica Pavlinusic, C<< >> + +=head1 COPYRIGHT & LICENSE + +Copyright 2007 Dobrica Pavlinusic, All Rights Reserved. + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=cut + +1; diff --git a/t/5-output-webpacus.t b/t/5-output-webpacus.t new file mode 100755 index 0000000..11c6a12 --- /dev/null +++ b/t/5-output-webpacus.t @@ -0,0 +1,55 @@ +#!/usr/bin/perl -w + +use Test::More tests => 10; +use Test::Exception; +use Cwd qw/abs_path/; +use Data::Dump qw/dump/; +use blib; +use strict; + +BEGIN { +use_ok( 'WebPAC::Output::Webpacus' ); +} + +my $debug = shift @ARGV; + +ok(my $abs_path = abs_path($0), "abs_path"); +$abs_path =~ s#/[^/]*$#/#; # +diag "abs_path: $abs_path"; +my $path = "$abs_path/sorted/"; + +ok(my $out = new WebPAC::Output::Webpacus({ + path => '/data/Webpacus2/', + database => 'test', + clean => 1, + debug => $debug, +}), "new"); + +ok( $out->init, 'init' ); + +my $ds = { + 'Source' => { + 'name' => 'Izvor: ', + 'sorted' => [ 'foo' ] + }, + 'ID' => { + 'sorted' => 'id', + }, + 'Array' => { + 'sorted' => [ qw/a1 a2 s3 a4 a5/ ], + }, +}; + +throws_ok { $out->add( ) } qr/need id/, 'add without params'; +throws_ok { $out->add( 42 ) } qr/need ds/, 'add without ds'; + +ok( $out->add( 42, $ds ), 'add 42' ); + +ok( $out->add( 99, { foo => { sorted => 'bar' } } ), 'add 99' ); + +ok( $out->add( 100, { foo => { sorted => [ qw/foo bar baz/ ] } } ), 'add 100' ); + +diag "path: ",$out->path; + +ok( $out->finish, 'finish' ); + -- 2.20.1