Solr/XML.pm

filter: Statementstmt 44.6% 71 / 159 Branchbran 48.0% 25 / 52 Conditioncond 7.5% 3 / 40 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 51.8% 14 / 27 Podpod 71.4% 5 / 7 totaltotal 38.3% 118 / 308 CC 54 SCAR 34.8
CC
54
Cov
39%
simpleDocument
110.0
_doc2xml
72.0
_add
30.0
CRAP 32.3
« Solr.pm ↑ Summary Solr/Result.pm »
1 # This code is part of Perl distribution Apache-Solr version 1.12.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2012-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Apache::Solr::XML;{
13 5 our $VERSION = '1.12';
14 }
15  
16 5 ▶use base 'Apache::Solr';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 ▶use Log::Report qw/solr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 5 ▶use Apache::Solr::Result ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use XML::LibXML::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use HTTP::Message ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 $Data::Dumper::Indent = 1;
31 5 $Data::Dumper::Quotekeys = 0;
32  
33 # See the XML::LibXML::Simple manual page
34 5 my @xml_decode_config = (
35 ForceArray => [],
36 ContentKey => '_',
37 KeyAttr => [],
38 );
39  
40 #--------------------
41  
42 sub init($)
43 4 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
44 4 ▶ $args->{format} ||= 'XML';
Condition50%1/2
$args->{"format"} ||= "XML"
l!l
04
MC/DC0%0/1
$args->{"format"} ||= "XML"
$args->{"format"}
Truth table50%1/2
A
A= $args->{"format"}
Aresult
11
00
45  
46 4 $self->SUPER::init($args);
47  
48 4 $self->{ASX_simple} = XML::LibXML::Simple->new(@xml_decode_config);
49 4 $self;
50 }
51  
52 #--------------------
53  
54 10 ▶sub xmlsimple() { $_[0]->{ASX_simple} }
Subroutine xmlsimple: called CC=1 SCAR=0.0
Pod documented
55  
56 #--------------------
57  
58 sub _select($$)
59 0 ▶{ my ($self, $args, $params) = @_;
Subroutine _select: not called CC=1 SCAR=6.9
60 0 my $endpoint = $self->endpoint('select', params => $params);
61 0 my $result = Apache::Solr::Result->new(%$args, params => $params, endpoint => $endpoint, core => $self);
62 0 $self->request($endpoint, $result);
63 0 $result;
64 }
65  
66 sub _extract($$$)
67 0 ▶{ my ($self, $params, $data, $ct) = @_;
Subroutine _extract: not called CC=1 SCAR=6.9
68 0 my $endpoint = $self->endpoint('update/extract', params => $params);
69 0 my $result = Apache::Solr::Result->new(params => $params, endpoint => $endpoint, core => $self);
70 0 $self->request($endpoint, $result, $data, $ct);
71 0 $result;
72 }
73  
74 sub _add($$$)
75 0 ▶{ my ($self, $docs, $attrs, $params) = @_;
Subroutine _add: not called CC=5 SCAR=34.0
76 0 ▶ $attrs ||= {};
Condition0%0/2
$attrs ||= {}
l!l
00
MC/DC0%0/1
$attrs ||= {}
$attrs
Truth table0%0/2
A
A= $attrs
Aresult
11
00
77 0 ▶ $params ||= [];
Condition0%0/2
$params ||= []
l!l
00
MC/DC0%0/1
$params ||= []
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
78  
79 0 my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
80 0 my $add = $doc->createElement('add');
81 0 $add->setAttribute($_ => $attrs->{$_}) for sort keys %$attrs;
82  
83 $add->addChild($self->_doc2xml($doc, $_))
84 0 for @$docs;
85  
86 0 $doc->setDocumentElement($add);
87  
88 0 my $endpoint = $self->endpoint('update', params => $params);
89 0 my $result = Apache::Solr::Result->new(params => $params, endpoint => $endpoint, core => $self);
90 0 $self->request($endpoint, $result, $doc);
91 0 $result;
92 }
93  
94 sub _doc2xml($$$)
95 0 ▶{ my ($self, $doc, $this) = @_;
Subroutine _doc2xml: not called CC=8 SCAR=42.8
96  
97 0 my $node = $doc->createElement('doc');
98 0 ▶ my $boost = $this->boost || 1.0;
Condition0%0/2
$this->boost || 1.0
l!l
00
MC/DC0%0/1
$this->boost || 1.0
$this->boost
Truth table0%0/2
A
A= $this->boost
Aresult
11
00
99 0 ▶ $node->setAttribute(boost => $boost) if $boost != 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $boost != 1 0 0 0
100  
101 0 foreach my $field ($this->fields)
102 0 { my $fnode = $doc->createElement('field');
103 0 $fnode->setAttribute(name => $field->{name});
104  
105 0 ▶ my $boost = $field->{boost} || 1.0;
Condition0%0/2
$field->{'boost'} || 1.0
l!l
00
MC/DC0%0/1
$field->{'boost'} || 1.0
$field->{'boost'}
Truth table0%0/2
A
A= $field->{'boost'}
Aresult
11
00
106 0 ▶ $fnode->setAttribute(boost => $boost)
Branch: 1 branch
BranchTrueFalseTotal
if $boost < "0.9999" or $boost > "1.0001" 0 0 0
Condition0%0/3
$boost < "0.9999" or $boost > "1.0001"
l!l&&r!l&&!r
000
MC/DC0%0/2
$boost < "0.9999" or $boost > "1.0001"
$boost < "0.9999" $boost > "1.0001"
Truth table0%0/3
A or B
A= $boost < "0.9999"B= $boost > "1.0001"
ABresult
1X1
011
000
107 if $boost < 0.9999 || $boost > 1.0001;
108  
109 $fnode->setAttribute(update => $field->{update})
110 0 ▶ if defined $field->{update};
Branch: 1 branch
BranchTrueFalseTotal
if defined $field->{"update"} 0 0 0
111  
112 0 $fnode->appendText($field->{content});
113 0 $node->addChild($fnode);
114 }
115 0 $node;
116 }
117  
118 0 ▶sub _commit($) { my ($s, $attr) = @_; $s->simpleUpdate(commit => $attr) }
Subroutine _commit: not called CC=1 SCAR=6.9
119 0 ▶sub _optimize($) { my ($s, $attr) = @_; $s->simpleUpdate(optimize => $attr) }
Subroutine _optimize: not called CC=1 SCAR=6.9
120 0 ▶sub _delete($$) { my $self = shift; $self->simpleUpdate(delete => @_) }
Subroutine _delete: not called CC=1 SCAR=6.9
121 0 ▶sub _rollback() { $_[0]->simpleUpdate('rollback') }
Subroutine _rollback: not called CC=1 SCAR=6.9
122  
123 sub _terms($)
124 0 ▶{ my ($self, $terms) = @_;
Subroutine _terms: not called CC=3 SCAR=24.8
125 0 my $endpoint = $self->endpoint('terms', params => $terms);
126 0 my $result = Apache::Solr::Result->new(params => $terms, endpoint => $endpoint, core => $self);
127  
128 0 $self->request($endpoint, $result);
129  
130 0 ▶ my $table = $result->decoded->{terms} || {};
Condition0%0/2
$result->decoded->{'terms'} || {}
l!l
00
MC/DC0%0/1
$result->decoded->{'terms'} || {}
$result->decoded->{'terms'}
Truth table0%0/2
A
A= $result->decoded->{'terms'}
Aresult
11
00
131 0 ▶ while(my ($field, $terms) = each %$table)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$table) 0 0 0
132 { my @terms = map [ $_ => $terms->{$_} ],
133 0 sort {$terms->{$b} <=> $terms->{$a}} keys %$terms;
134 0 $result->terms($field => \@terms);
135 }
136  
137 0 $result;
138 }
139  
140 #--------------------
141  
142 sub request($$;$$)
143 0 ▶{ my ($self, $url, $result, $body, $body_ct) = @_;
Subroutine request: not called CC=4 SCAR=30.0
Pod documented
144  
145 0 ▶ if(blessed $body && $body->isa('XML::LibXML::Document'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($body) and $body->isa("XML::LibXML::Document")) 0 0 0
Condition0%0/3
builtin::blessed($body) and $body->isa("XML::LibXML::Document")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($body) and $body->isa("XML::LibXML::Document")
builtin::blessed($body) $body->isa("XML::LibXML::Document")
Truth table0%0/3
A and B
A= builtin::blessed($body)B= $body->isa("XML::LibXML::Document")
ABresult
0X0
100
111
146 0 ▶ { $body_ct ||= 'text/xml; charset=utf-8';
Condition0%0/2
$body_ct ||= "text/xml; charset=utf-8"
l!l
00
MC/DC0%0/1
$body_ct ||= "text/xml; charset=utf-8"
$body_ct
Truth table0%0/2
A
A= $body_ct
Aresult
11
00
147 0 $body = \$body->toString;
148 }
149  
150 0 $self->SUPER::request($url, $result, $body, $body_ct);
151 }
152  
153 sub _cleanup_parsed($);
154 sub decodeResponse($)
155 0 ▶{ my ($self, $resp) = @_;
Subroutine decodeResponse: not called CC=3 SCAR=24.8
Pod undocumented
156  
157 0 ▶ $resp->content_type =~ m/xml/i
Branch: 1 branch
BranchTrueFalseTotal
$resp->content_type =~ /xml/i or return undef 0 0 0
158 or return undef;
159  
160 0 ▶ my $dec = $self->xmlsimple->XMLin(
Condition0%0/3
$resp->decoded_content || $resp->content
l!l&&r!l&&!r
000
MC/DC0%0/2
$resp->decoded_content || $resp->content
$resp->decoded_content $resp->content
Truth table0%0/3
A || B
A= $resp->decoded_contentB= $resp->content
ABresult
1X1
011
000
161 $resp->decoded_content || $resp->content,
162 parseropts => { huge => 1 },
163 );
164  
165 0 _cleanup_parsed $dec;
166 }
167  
168 sub _cleanup_parsed($)
169 92 ▶{ my $data = shift;
Subroutine _cleanup_parsed: called CC=22 SCAR=32.2
170  
171 92 ▶ if(!ref $data) { return $data }
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $data) { } 17 75 92
elsif (ref $data eq 'HASH') { } 68 7 75
elsif (ref $data eq 'ARRAY') { } 7 0 7
elsif (ref $data eq 'DateTime') { } 0 0 0
172 elsif(ref $data eq 'HASH')
173 68 { my %d = %$data; # start with shallow copy
174  
175 # Hash
176 68 ▶ if(my $lst = delete $d{lst})
Branch: 1 branch
BranchTrueFalseTotal
if (my $lst = delete $d{"lst"}) 23 45 68
177 23 ▶ { foreach (ref $lst eq 'ARRAY' ? @$lst : $lst)
Branch: 1 branch
BranchTrueFalseTotal
ref $lst eq 'ARRAY' ? : 10 13 23
178 34 { my $name = delete $_->{name};
179 34 $d{$name} = $_;
180 }
181 }
182  
183 # Array
184 68 ▶ if(my $arr = delete $d{arr})
Branch: 1 branch
BranchTrueFalseTotal
if (my $arr = delete $d{"arr"}) 3 65 68
185 3 ▶ { foreach (ref $arr eq 'ARRAY' ? @$arr : $arr)
Branch: 1 branch
BranchTrueFalseTotal
ref $arr eq 'ARRAY' ? : 3 0 3
186 6 { my $name = delete $_->{name};
187 6 my ($type, $values) = %$_;
188 6 ▶ $values = [$values] if ref $values ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $values ne "ARRAY" 1 5 6
189 6 $d{$name} = $values;
190 }
191 }
192  
193 # XXX haven't found a clear list of what can be expected here
194 68 foreach my $type (qw/int long float double bool date str text/)
195 544 ▶ { my $items = delete $d{$type} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $items = delete $d{$type} or next 30 514 544
196 30 ▶ foreach (ref $items eq 'ARRAY' ? @$items : $items)
Branch: 1 branch
BranchTrueFalseTotal
ref $items eq 'ARRAY' ? : 18 12 30
197 82 ▶ { my ($name, $value) = ref $_ eq 'HASH' ? ($_->{name}, $_->{_}) : ('', $_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 81 1 82
198  
199 82 ▶ $value = $value eq 'true' || $_->{_} eq 1
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 2 80 82
Condition33%1/3
$value eq 'true' || $_->{'_'} eq 1
l!l&&r!l&&!r
200
MC/DC0%0/2
$value eq 'true' || $_->{'_'} eq 1
$value eq 'true' $_->{'_'} eq 1
Truth table33%1/3
A || B
A= $value eq 'true'B= $_->{'_'} eq 1
ABresult
1X1
011
000
200 if $type eq 'bool';
201  
202 82 $d{$name} = $value;
203 }
204 }
205  
206 68 foreach my $key (keys %d)
207 186 ▶ { $d{$key} = _cleanup_parsed($d{$key}) if ref $d{$key};
Branch: 1 branch
BranchTrueFalseTotal
if ref $d{$key} 63 123 186
208 }
209 68 return \%d;
210 }
211 elsif(ref $data eq 'ARRAY')
212 7 { return [ map _cleanup_parsed($_), @$data ];
213 }
214 elsif(ref $data eq 'DateTime')
215 0 { return $data;
216 }
217 0 ▶ else {panic ref $data || $data}
Condition0%0/3
ref $data || $data
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $data || $data
ref $data $data
Truth table0%0/3
A || B
A= ref $dataB= $data
ABresult
1X1
011
000
218 }
219  
220  
221 sub simpleUpdate($$;$)
222 0 ▶{ my ($self, $command, $attrs, $content) = @_;
Subroutine simpleUpdate: not called CC=2 SCAR=17.9
Pod documented
223 0 ▶ $attrs ||= {};
Condition0%0/2
$attrs ||= {}
l!l
00
MC/DC0%0/1
$attrs ||= {}
$attrs
Truth table0%0/2
A
A= $attrs
Aresult
11
00
224 0 my $params = [ commit => delete $attrs->{commit} ];
225 0 my $endpoint = $self->endpoint('update', params => $params);
226 0 my $result = Apache::Solr::Result->new(params => $params, endpoint => $endpoint, core => $self);
227 0 my $doc = $self->simpleDocument($command, $attrs, $content);
228 0 $self->request($endpoint, $result, $doc);
229 0 $result;
230 }
231  
232  
233 sub simpleDocument($;$$)
234 0 ▶{ my ($self, $command, $attrs, $content) = @_;
Subroutine simpleDocument: not called CC=10 SCAR=47.0
Pod documented
235 0 my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
236 0 my $top = $doc->createElement($command);
237 0 $doc->setDocumentElement($top);
238  
239 0 ▶ $attrs ||= {};
Condition0%0/2
$attrs ||= {}
l!l
00
MC/DC0%0/1
$attrs ||= {}
$attrs
Truth table0%0/2
A
A= $attrs
Aresult
11
00
240 0 $top->setAttribute($_ => $attrs->{$_}) for sort keys %$attrs;
241  
242 0 ▶ if(!defined $content) {}
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $content) { } 0 0 0
elsif (ref $content eq 'HASH' or ref $content eq 'ARRAY') { } 0 0 0
243 ▶ elsif(ref $content eq 'HASH' || ref $content eq 'ARRAY')
Condition0%0/3
ref $content eq "HASH" or ref $content eq "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $content eq "HASH" or ref $content eq "ARRAY"
ref $content eq "HASH" ref $content eq "ARRAY"
Truth table0%0/3
A or B
A= ref $content eq "HASH"B= ref $content eq "ARRAY"
ABresult
1X1
011
000
244 0 ▶ { my @c = ref $content eq 'HASH' ? %$content : @$content;
Branch: 1 branch
BranchTrueFalseTotal
ref $content eq 'HASH' ? : 0 0 0
245 0 ▶ while(@c)
Branch: 1 branch
BranchTrueFalseTotal
if (@c) 0 0 0
246 0 { my ($name, $values) = (shift @c, shift @c);
247 0 ▶ foreach my $value (ref $values eq 'ARRAY' ? @$values : $values)
Branch: 1 branch
BranchTrueFalseTotal
ref $values eq 'ARRAY' ? : 0 0 0
248 0 { my $node = $doc->createElement($name);
249 0 $node->appendText($value);
250 0 $top->addChild($node);
251 }
252 }
253 }
254 else
255 0 { $top->appendText($content);
256 }
257 0 $doc;
258 }
259  
260 sub endpoint($@)
261 1 ▶{ my ($self, $action, %args) = @_;
Subroutine endpoint: called CC=4 SCAR=16.4
Pod documented
262 1 ▶ my $params = $args{params} ||= [];
Condition50%1/2
$args{"params"} ||= []
l!l
10
MC/DC0%0/1
$args{"params"} ||= []
$args{"params"}
Truth table50%1/2
A
A= $args{"params"}
Aresult
11
00
263  
264 0 ▶ if(ref $params eq 'HASH') { $params->{wt} ||= 'xml' }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq 'HASH') { } 0 1 1
Condition0%0/2
$params->{"wt"} ||= "xml"
l!l
00
MC/DC0%0/1
$params->{"wt"} ||= "xml"
$params->{"wt"}
Truth table0%0/2
A
A= $params->{"wt"}
Aresult
11
00
265 1 else { $args{params} = [ wt => 'xml', @$params ] }
266  
267 1 $self->SUPER::endpoint($action => %args);
268 }
269  
270 5 1;
« Solr.pm ↑ Summary Solr/Result.pm »