| 1 | package Solr::HTTPUpdateHandler; | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use XML::Simple; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::Slurp qw(slurp); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 1 | require Exporter; | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||
| 16 | 1 | our @EXPORT_OK = | |||||||||||||||||||||||||||||
| 17 | qw(add _fixXmlEndTag delete_by_id delete_by_query commit optimize _postRequest add_by_file _logAddDeletes _logPost post_file); | ||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||
| 19 | 1 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 22 | 0 | my $class = shift; | |||||||||||||||||||||||||||||
| 23 | 0 | my (%params) = @_; | |||||||||||||||||||||||||||||
| 24 | 0 | my $self = \%params; | |||||||||||||||||||||||||||||
| 25 | 0 | bless( $self, $class ); | |||||||||||||||||||||||||||||
| 26 | 0 | $self->_init; | |||||||||||||||||||||||||||||
| 27 | 0 | return $self; | |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub _init { | ||||||||||||||||||||||||||||
|
Subroutine
_init: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 31 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | unless ( $self->{schema}->{update_post_url} ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 33 | 0 | die "$! No update_post_url supplied to Solr::HTTPUpdateHandler\n"; | |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||
| 35 | 0 | return $self; | |||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub add { | ||||||||||||||||||||||||||||
|
Subroutine
add: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||
| 40 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||
| 42 | 0 | my ($hash, $timeout) = @_; | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||
| 44 | 0 | my $addHash; | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||
| 46 | 0 | foreach my $key ( keys %{$hash} ) { | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||
| 48 | # flush out hash ref to create solrXML format | ||||||||||||||||||||||||||||||
| 49 | 0 | $addHash->{"field name=\"$key\""} = $hash->{$key}; | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||
| 53 | # make solrXml | ||||||||||||||||||||||||||||||
| 54 | 0 | my $xml = "<add>\n"; | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||
| 56 | 0 | $xml .= XMLout( | |||||||||||||||||||||||||||||
| 57 | $addHash, | ||||||||||||||||||||||||||||||
| 58 | NumericEscape => 2, | ||||||||||||||||||||||||||||||
| 59 | ContentKey => 'name', | ||||||||||||||||||||||||||||||
| 60 | NoAttr => 1, | ||||||||||||||||||||||||||||||
| 61 | RootName => 'doc', | ||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 0 | $xml .= "</add>\n"; | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | # remove extra part of end string to fit solrXML format | ||||||||||||||||||||||||||||||
| 67 | 0 | $xml = $self->_fixXmlEndTag($xml); | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | 0 | $self->_postRequest($xml, "add", $timeout); | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | 0 | return $self; | |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub add_by_file { | ||||||||||||||||||||||||||||
|
Subroutine
add_by_file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 75 | # add_by_file and post_file differ in logging. | ||||||||||||||||||||||||||||||
| 76 | # add_by_file will parse out the uniqueID element and log the addition of each | ||||||||||||||||||||||||||||||
| 77 | # uniqueID added to the index. post_file, simply logs the filename of the posted file. | ||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 0 | my ($file, $timeout) = @_; | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( -f $file ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | 0 | my $content = slurp($file); | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||
| 88 | 0 | $self->_postRequest($content, "add_by_file", $timeout); | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | else { | ||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | 0 | ERROR("$! file not found, $file"); | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | 0 | return $self; | |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub post_file { | ||||||||||||||||||||||||||||
|
Subroutine
post_file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 102 | # add_by_file and post_file differ in logging. | ||||||||||||||||||||||||||||||
| 103 | # add_by_file will parse out the uniqueID element and log the addition of each | ||||||||||||||||||||||||||||||
| 104 | # uniqueID added to the index. post_file, simply logs the filename of the posted file. | ||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | 0 | my ($file, $timeout) = @_; | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ( -f $file ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||
| 113 | 0 | my $content = slurp($file); | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||
| 115 | 0 | $self->_postRequest($content, "post_file $file", $timeout); | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||
| 121 | 0 | ERROR("$! file not found, $file"); | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||
| 125 | 0 | return $self; | |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub delete_by_id { | ||||||||||||||||||||||||||||
|
Subroutine
delete_by_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 129 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 130 | 0 | my ($id, $timeout) = @_; | |||||||||||||||||||||||||||||
| 131 | 0 | $self->_postRequest("<delete><id>$id</id></delete>", "delete_by_id", $timeout); | |||||||||||||||||||||||||||||
| 132 | 0 | return $self; | |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub delete_by_query { | ||||||||||||||||||||||||||||
|
Subroutine
delete_by_query: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 136 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 137 | 0 | my ($query, $timeout) = @_; | |||||||||||||||||||||||||||||
| 138 | 0 | $self->_postRequest("<delete><query>$query</query></delete>", "delete_by_query", $timeout); | |||||||||||||||||||||||||||||
| 139 | 0 | return $self; | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub commit { | ||||||||||||||||||||||||||||
|
Subroutine
commit: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 143 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 144 | 0 | $self->_postRequest("<commit/>","commit", 600); | |||||||||||||||||||||||||||||
| 145 | 0 | return $self; | |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | sub optimize { | ||||||||||||||||||||||||||||
|
Subroutine
optimize: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 149 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 150 | 0 | my ($timeout) = @_; | |||||||||||||||||||||||||||||
| 151 | 0 | $self->_postRequest("<optimize/>","optimize", $timeout); | |||||||||||||||||||||||||||||
| 152 | 0 | return $self; | |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | sub _postRequest { | ||||||||||||||||||||||||||||
|
Subroutine
_postRequest: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||
| 156 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 157 | 0 | my ($content, $type, $timeout) = @_; | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | $timeout ||= 600; | ||||||||||||||||||||||||||||
|
Condition0%0/2
$timeout ||= 600
MC/DC0%0/1
$timeout ||= 600
$timeout
Truth table0%0/2
A
A= $timeout
| |||||||||||||||||||||||||||||||
| 159 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||||
| 160 | 0 | $ua->timeout($timeout); | |||||||||||||||||||||||||||||
| 161 | 0 | $ua->agent("SolrHTTPUpdateHandlerAgent"); | |||||||||||||||||||||||||||||
| 162 | 0 | my $req = HTTP::Request->new( POST => $self->{schema}->{update_post_url} ); | |||||||||||||||||||||||||||||
| 163 | 0 | $req->content_type('Content-type:text/xml; charset=utf-8'); | |||||||||||||||||||||||||||||
| 164 | 0 | $req->content($content); | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | # Pass request to the user agent and get a response back | ||||||||||||||||||||||||||||||
| 167 | 0 | my $res = $ua->request($req); | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||
| 169 | # Check the outcome of the response | ||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if ( $res->is_success ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if ($res->content eq '<result status="0"></result>') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 172 | # log the uniqueKey's in the posted content | ||||||||||||||||||||||||||||||
| 173 | 0 | $self->_logPost($content, $type); | |||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||||
| 176 | 0 | ERROR("Post Type: $type Error: \"$content\""); | |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||
| 179 | else { | ||||||||||||||||||||||||||||||
| 180 | 0 | ERROR($content); | |||||||||||||||||||||||||||||
| 181 | 0 | ERROR( $res->status_line ); | |||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||
| 183 | 0 | return $self; | |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub _fixXmlEndTag { | ||||||||||||||||||||||||||||
|
Subroutine
_fixXmlEndTag: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||
| 187 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 188 | 0 | my ($xml) = @_; | |||||||||||||||||||||||||||||
| 189 | 0 | my $rv; | |||||||||||||||||||||||||||||
| 190 | 0 | my @lines = split /\n/, $xml; | |||||||||||||||||||||||||||||
| 191 | 0 | foreach my $line (@lines) { | |||||||||||||||||||||||||||||
| 192 | 0 | $line =~ s/<\/field name="\w+">/<\/field>/g; | |||||||||||||||||||||||||||||
| 193 | 0 | $rv .= $line; | |||||||||||||||||||||||||||||
| 194 | 0 | $rv .= "\n"; | |||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||
| 196 | 0 | return $rv; | |||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | sub _logPost { | ||||||||||||||||||||||||||||
|
Subroutine
_logPost: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||
| 200 | # intended to take content and type of successful posts and parse into log file info | ||||||||||||||||||||||||||||||
| 201 | 0 | my $self = shift; | |||||||||||||||||||||||||||||
| 202 | 0 | my ($content, $type) = @_; | |||||||||||||||||||||||||||||
| 203 | 0 | ▶ | if ($type eq "commit") { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 204 | 0 | INFO("Commit Successfully posted."); | |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||
| 206 | elsif ($type eq "delete_by_id") { | ||||||||||||||||||||||||||||||
| 207 | 0 | $content =~ m/<delete><id>(\w+)<\/id><\/delete>/; | |||||||||||||||||||||||||||||
| 208 | 0 | INFO("$self->{schema}->{uniqueKey} $1 successfully removed from index."); | |||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||
| 210 | elsif ($type eq "delete_by_query") { | ||||||||||||||||||||||||||||||
| 211 | # this is less than perfect. Mark for future fix. | ||||||||||||||||||||||||||||||
| 212 | 0 | my $logString = $content; | |||||||||||||||||||||||||||||
| 213 | 0 | chomp $logString; | |||||||||||||||||||||||||||||
| 214 | 0 | $logString =~s/^\s+<delete><query>//g; | |||||||||||||||||||||||||||||
| 215 | 0 | $logString =~s/<\/query><\/delete>//g; | |||||||||||||||||||||||||||||
| 216 | 0 | INFO("Delete by query: \"$logString\" successfully processed"); | |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||
| 218 | ▶ | elsif (($type eq "add") || ($type eq "add_by_file")) { | |||||||||||||||||||||||||||||
|
Condition0%0/3
$type eq "add" or $type eq "add_by_file"
MC/DC0%0/2
$type eq "add" or $type eq "add_by_file"
$type eq "add"
$type eq "add_by_file"
Truth table0%0/3
A or B
A= $type eq "add"B= $type eq "add_by_file"
| |||||||||||||||||||||||||||||||
| 219 | 0 | my @content = split /\n/, $content; | |||||||||||||||||||||||||||||
| 220 | 0 | my @uniqueKeyStrings = grep /$self->{schema}->{uniqueKey}/, @content; | |||||||||||||||||||||||||||||
| 221 | 0 | my $addString =''; | |||||||||||||||||||||||||||||
| 222 | 0 | foreach my $key (@uniqueKeyStrings) { | |||||||||||||||||||||||||||||
| 223 | 0 | chomp $key; | |||||||||||||||||||||||||||||
| 224 | 0 | $key =~ s/^\s+//g; | |||||||||||||||||||||||||||||
| 225 | 0 | $key =~ s/<field name=\"$self->{schema}->{uniqueKey}\">//g; | |||||||||||||||||||||||||||||
| 226 | 0 | $key =~ s/<\/field>$//g; | |||||||||||||||||||||||||||||
| 227 | # log it | ||||||||||||||||||||||||||||||
| 228 | 0 | $addString .= "$self->{schema}->{uniqueKey} $key added to solr index\n"; | |||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||
| 230 | 0 | INFO($addString); | |||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||
| 232 | elsif ($type eq "optimize") { | ||||||||||||||||||||||||||||||
| 233 | 0 | INFO("Solr Index Optimized\n"); | |||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||
| 235 | elsif ($type =~ m/^post_file/g) { | ||||||||||||||||||||||||||||||
| 236 | 0 | my ($garbage, $filename) = split / /, $type; | |||||||||||||||||||||||||||||
| 237 | 0 | INFO("$filename posted"); | |||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||
| 239 | else { | ||||||||||||||||||||||||||||||
| 240 | 0 | INFO("The following content succesfully posted to solr Index: \"$content\""); | |||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||
| 243 | 0 | return $self; | |||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||
| 249 | 1 | 1; | |||||||||||||||||||||||||||||
| 250 | __END__ | ||||||||||||||||||||||||||||||