| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Interchange::Search::Solr::UpdateIndex; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 642 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 4 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 5 | use Moo; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 7 | 1 |  |  | 1 |  | 331 | use Sub::Quote; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 66 |  | 
| 8 | 1 |  |  | 1 |  | 7 | use LWP::UserAgent; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 407 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | =head1 NAME | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | Interchange::Search::Solr::UpdateIndex | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | Updates your Solr index. | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | =head2 ACCESSORS | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head3 url | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | Solr URL for your collection, e.g. | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | http://localhost:9059/solr/collection1 | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =cut | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | has url => ( | 
| 29 |  |  |  |  |  |  | is => 'rw', | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =head3 agent | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | HTTP user agent object, defaults to a LWP::UserAgent instance. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =cut | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | has agent => ( | 
| 40 |  |  |  |  |  |  | is => 'rw', | 
| 41 |  |  |  |  |  |  | lazy => 1, | 
| 42 |  |  |  |  |  |  | default => sub {LWP::UserAgent->new;}, | 
| 43 |  |  |  |  |  |  | ); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | =head3 status_line | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | HTTP status line. | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =cut | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | has status_line => ( | 
| 52 |  |  |  |  |  |  | is => 'rwp', | 
| 53 |  |  |  |  |  |  | lazy => 1, | 
| 54 |  |  |  |  |  |  | default => quote_sub q{return '';}, | 
| 55 |  |  |  |  |  |  | ); | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =head2 METHODS | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =head3 update | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | Updates index. Returns 1 on success. | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | Requires C<$mode> parameter which is either I<full> | 
| 64 |  |  |  |  |  |  | or I<delta>. | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | =cut | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub update { | 
| 69 | 0 |  |  | 0 | 1 |  | my ($self, $mode) = @_; | 
| 70 | 0 |  |  |  |  |  | my ($update_url, $command, $request, $response); | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | # reset status line | 
| 73 | 0 |  |  |  |  |  | $self->_set_status_line(''); | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | # construct url and request | 
| 76 | 0 |  |  |  |  |  | $update_url = $self->_construct_url($mode); | 
| 77 | 0 |  |  |  |  |  | $response = $self->agent->get($update_url); | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # save status line | 
| 80 | 0 |  |  |  |  |  | $self->_set_status_line($response->status_line); | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 | 0 |  |  |  |  | unless ($response->is_success) { | 
| 83 | 0 |  |  |  |  |  | return; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | return $response; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head3 query | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | Queries index. | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =cut | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub query { | 
| 96 | 0 |  |  | 0 | 1 |  | my ($self, $query) = @_; | 
| 97 | 0 |  |  |  |  |  | my $url = $self->url . '/select?wt=json&'.$query; | 
| 98 | 0 |  |  |  |  |  | my $response = $self->agent->get($url); | 
| 99 | 0 |  |  |  |  |  | print $url."\n\r---------------\n\r"; | 
| 100 | 0 |  |  |  |  |  | return $response; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub _construct_url { | 
| 104 | 0 |  |  | 0 |  |  | my ($self, $mode) = @_; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 0 |  |  |  |  |  | my $update_url; | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 0 | 0 |  |  |  |  | if ($mode eq 'clear') { | 
| 110 | 0 |  |  |  |  |  | return $self->url . '/update?stream.body=<delete><query>*:*</query></delete>&commit=true'; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 0 |  |  |  |  |  | $update_url = $self->url . '/dataimport?command='; | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 0 | 0 |  |  |  |  | if ($mode eq 'full') { | 
|  |  | 0 |  |  |  |  |  | 
| 116 | 0 |  |  |  |  |  | $update_url .= 'full-import'; | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  | elsif ($mode eq 'delta') { | 
| 119 | 0 |  |  |  |  |  | $update_url .= 'delta-import'; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  | #$update_url .= '&commit=true'; | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =head1 AUTHOR | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | Stefan Hornburg (Racke), <racke@linuxia.de> | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | Copyright 2013-2019 Stefan Hornburg (Racke) <racke@linuxia.de>. | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 133 |  |  |  |  |  |  | under the terms of either: the GNU General Public License as published | 
| 134 |  |  |  |  |  |  | by the Free Software Foundation; or the Artistic License. | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | See http://dev.perl.org/licenses/ for more information. | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =cut | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | 1; |