| 1 | package Sphinx::XMLpipe2; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use XML::Hash::XS; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 2 | our $VERSION = "0.05"; | |||||||||||||
| 6 | |||||||||||||||
| 7 | 4 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 8 | 4 | my ($class, %args) = @_; | |||||||||||||
| 9 | 4 | ▶ | return undef unless %args; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 3 | my $self = \%args; | |||||||||||||
| 12 | 3 | $self->{'data'} = {}; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 3 | $self->{'xmlprocessor'} = XML::Hash::XS->new( | |||||||||||||
| 15 | xml_decl => 0, | ||||||||||||||
| 16 | indent => 4, | ||||||||||||||
| 17 | use_attr => 1, | ||||||||||||||
| 18 | content => 'content', | ||||||||||||||
| 19 | encoding => 'utf-8', | ||||||||||||||
| 20 | utf8 => 0, | ||||||||||||||
| 21 | ); | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 3 | bless $self, $class; | |||||||||||||
| 24 | 3 | return $self | |||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | sub fetch { | ||||||||||||
|
Subroutine
fetch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 28 | 1 | my ($self, ) = @_; | |||||||||||||
| 29 | |||||||||||||||
| 30 | 1 | my $content1 = $self->_fetch_header(); | |||||||||||||
| 31 | 1 | my $content2 = $self->_fetch_data(); | |||||||||||||
| 32 | 1 | return qq~<?xml version="1.0" encoding="utf-8"?>\n<sphinx:docset>$content1$content2</sphinx:docset>~; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | 2 | ▶ | sub add_data { | ||||||||||||
|
Subroutine
add_data: called CC=7 SCAR=22.0
Pod
documented
| |||||||||||||||
| 36 | 2 | my ($self, $data) = @_; | |||||||||||||
| 37 | 2 | ▶ | return undef unless exists $data->{'id'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | |||||||||||||||
| 39 | $self->{'data'}->{'sphinx:document'} = [] | ||||||||||||||
| 40 | 1 | ▶ | unless exists $self->{'data'}->{'sphinx:document'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | |||||||||||||||
| 42 | 1 | my %params = (); | |||||||||||||
| 43 | 1 | my @keys = (); | |||||||||||||
| 44 | |||||||||||||||
| 45 | 1 | ▶ | if (ref($self->{'attrs'}) eq 'HASH') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 46 | 1 | @keys = (@{$self->{'fields'}}, keys %{$self->{'attrs'}}); | |||||||||||||
| 47 | 1 | map { $params{$_} = [$data->{$_}] } @keys; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | elsif (ref($self->{'attrs'}) eq 'ARRAY') { | ||||||||||||||
| 50 | 0 | my @def = (); | |||||||||||||
| 51 | 0 | for my $definition (@{$self->{'attrs'}}) { | |||||||||||||
| 52 | 0 | push @def, $definition->{'name'}; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | 0 | @keys = (@{$self->{'fields'}}, @def); | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | 1 | ▶ | map { $params{$_} = [$data->{$_}] if exists $data->{$_}; } @keys; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | |||||||||||||||
| 58 | 1 | push @{$self->{'data'}->{'sphinx:document'}}, { | |||||||||||||
| 59 | 1 | id => $data->{'id'}, | |||||||||||||
| 60 | %params | ||||||||||||||
| 61 | }; | ||||||||||||||
| 62 | 1 | return $self; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 2 | ▶ | sub remove_data { | ||||||||||||
|
Subroutine
remove_data: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 66 | 2 | my ($self, $data) = @_; | |||||||||||||
| 67 | 2 | ▶ | return undef unless exists $data->{'id'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | |||||||||||||||
| 69 | $self->{'data'}->{'sphinx:killlist'} = {'id' => []} | ||||||||||||||
| 70 | 1 | ▶ | unless exists $self->{'data'}->{'sphinx:killlist'}->{'id'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | |||||||||||||||
| 72 | 1 | push @{$self->{'data'}->{'sphinx:killlist'}->{'id'}}, [$data->{'id'}]; | |||||||||||||
| 73 | 1 | return $self; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | |||||||||||||||
| 77 | 1 | ▶ | sub _fetch_header { | ||||||||||||
|
Subroutine
_fetch_header: called CC=7 SCAR=24.3
| |||||||||||||||
| 78 | 1 | my ($self, ) = @_; | |||||||||||||
| 79 | 1 | my $header = { | |||||||||||||
| 80 | 'sphinx:schema' => { | ||||||||||||||
| 81 | 'sphinx:field' => [], | ||||||||||||||
| 82 | 'sphinx:attr' => [], | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | }; | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 1 | for my $field (@{$self->{'fields'}}) { | |||||||||||||
| 87 | 3 | push @{$header->{'sphinx:schema'}->{'sphinx:field'}}, {'name' => $field}; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | 1 | ▶ | if (ref($self->{'attrs'}) eq 'HASH') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 91 | 1 | for my $attr (keys %{$self->{'attrs'}}) { | |||||||||||||
| 92 | 1 | push @{$header->{'sphinx:schema'}->{'sphinx:attr'}}, {'name' => $attr, 'type' => $self->{'attrs'}->{$attr}}; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | } | ||||||||||||||
| 95 | elsif (ref($self->{'attrs'}) eq 'ARRAY') { | ||||||||||||||
| 96 | 0 | for my $definition (@{$self->{'attrs'}}) { | |||||||||||||
| 97 | 0 | my $node = {}; | |||||||||||||
| 98 | 0 | ▶ | map {$node->{$_} = $definition->{$_} if exists $definition->{$_};} qw(name type bits default); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | 0 | push @{$header->{'sphinx:schema'}->{'sphinx:attr'}}, $node; | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | } | ||||||||||||||
| 102 | 0 | @{$header->{'sphinx:schema'}->{'sphinx:attr'}} = sort {$a->{name} cmp $b->{name}} @{$header->{'sphinx:schema'}->{'sphinx:attr'}}; | |||||||||||||
| 103 | |||||||||||||||
| 104 | 1 | return _pruning_xml($self->{'xmlprocessor'}->hash2xml($header)); | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 1 | ▶ | sub _fetch_data { | ||||||||||||
|
Subroutine
_fetch_data: called CC=1 SCAR=0.0
| |||||||||||||||
| 108 | 1 | my ($self, ) = @_; | |||||||||||||
| 109 | 1 | return _pruning_xml($self->{'xmlprocessor'}->hash2xml($self->{'data'})); | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 2 | ▶ | sub _pruning_xml { | ||||||||||||
|
Subroutine
_pruning_xml: called CC=1 SCAR=0.0
| |||||||||||||||
| 113 | 2 | my ($xml) = @_; | |||||||||||||
| 114 | 2 | $xml =~ s/^\s*<root>//is; | |||||||||||||
| 115 | 2 | $xml =~ s/<\/root>\s*$//is; | |||||||||||||
| 116 | 2 | return $xml; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 2 | 1; | |||||||||||||
| 120 | __END__ | ||||||||||||||