File Coverage

blib/lib/Plucene/SearchEngine/Index/URL.pm
Criterion Covered Total %
statement 33 69 47.8
branch 0 14 0.0
condition n/a
subroutine 11 12 91.6
pod 0 1 0.0
total 44 96 45.8


line stmt bran cond sub pod time code
1             package Plucene::SearchEngine::Index::URL;
2 1     1   1026 use base "Plucene::SearchEngine::Index::Base";
  1         2  
  1         68  
3              
4 1     1   5 use strict;
  1         1  
  1         30  
5 1     1   4 use Carp;
  1         1  
  1         56  
6 1     1   4 use Time::Piece;
  1         2  
  1         8  
7 1     1   76 use File::Temp;
  1         2  
  1         100  
8 1     1   3415 use URI;
  1         12529  
  1         21  
9 1     1   11339 use Date::Parse;
  1         9787  
  1         171  
10 1     1   1345 use HTTP::Request;
  1         28026  
  1         15  
11 1     1   3917 use LWP::UserAgent;
  1         26549  
  1         15  
12 1     1   43 use File::Basename;
  1         2  
  1         100  
13 1     1   6 use File::Temp qw(tempfile);
  1         2  
  1         450  
14              
15             =head1 NAME
16              
17             Plucene::SearchEngine::Index::URL - File reader for web URLs
18              
19             =head1 DESCRIPTION
20              
21             This frontend module takes a URL, downloads its content, extracts its metadata
22             and passes the file onto a backend. The frontend registers the following
23             Plucene fields:
24              
25             =over 3
26              
27             =item mimetype
28              
29             The MIME type of the data.
30              
31             =item filename
32              
33             The basename of the URL's filename.
34              
35             =item id
36              
37             The URL given.
38              
39             =item modified
40              
41             A Plucene date field representing the last modified date of the file
42              
43             =item language
44              
45             The ISO language identifier of the content
46              
47             =item encoding
48              
49             The original character set. (before conversion to UTF-8)
50              
51             =back
52              
53             =head2 METHODS
54              
55             Plucene::SearchEngine::Index::URL->examine($url);
56              
57             This downloads and examines a file on the filesystem for the above metadata,
58             before handling it to a backend.
59              
60             =cut
61              
62             sub examine {
63 0     0 0   my ($class, $url) = @_;
64 0           my $ua = LWP::UserAgent->new;
65 0           my $response = $ua->get($url);
66 0 0         return unless $response->is_success;
67              
68 0           my $encoding = "";
69 0           my $filename = basename($url); # Hack, hack
70 0           my $mime = $response->header("Content-Type");
71 0           $mime =~ s/;\s+(.*)//;
72 0           my $rest = $1;
73 0           my $self = $class->handler_for($filename, $mime)->new();
74 0           my $lm = $response->header("Last-Modified");
75 0 0         if ($rest =~ /charset=([\w\-]+)/) { $encoding = $1; }
  0            
76              
77 0 0         if (my $language =$response->header("Content-Language")) {
78 0           $self->add_data("language", "Keyword", $language);
79             }
80              
81 0           $self->add_data("mimetype", "Keyword", $mime);
82 0           $self->add_data("id", "Keyword", $url);
83 0           $self->add_data("filename", "Keyword", $filename);
84 0           $self->add_data("modified", "Date", Time::Piece->new(str2time($lm)));
85              
86 0           my ($fh, $tmpfile) = tempfile();
87 0 0         if ($encoding) {
88 0           require Encode;
89 0           binmode $fh, ":utf8";
90 0           print $fh Encode::decode($encoding, $response->content);
91             } else {
92 0           print $fh $response->content;
93             }
94 0           close $fh;
95 0           my @docs = $self->gather_data_from_file($tmpfile);
96 0 0         if (@docs <2) { @docs = ($self) }
  0            
97 0 0         if ($encoding) {
98 0           $_->add_data("encoding", "Text", $encoding) for @docs;
99             }
100 0           unlink $tmpfile;
101 0 0         if (wantarray) { return @docs }
  0            
102             else {
103 0           carp "Using ->examine in scalar context is deprecated";
104 0           return $docs[0];
105             }
106             }
107              
108             1;