| 1 | package SWISH::Prog::Utils; | ||||||||||||||||||||||||||||
| 2 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use Data::Dump qw( dump ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 17 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use Search::Tools::XML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 17 | our $VERSION = '0.75'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =pod | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | SWISH::Prog::Utils - utility variables and methods | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | use SWISH::Prog::Utils; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # use the utils | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | This class provides commonly used variables and methods | ||||||||||||||||||||||||||||
| 26 | shared by many classes in the SWISH::Prog project. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 VARIABLES | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =over | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =item $ExtRE | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Regular expression of common file type extensions. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =item %ParserTypes | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Hash of MIME types to their equivalent parser. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =back | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 17 | our $ExtRE = qr{\.(\w+)(\.gz)?$}io; | |||||||||||||||||||||||||||
| 45 | 17 | our %ParserTypes = ( | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # mime parser type | ||||||||||||||||||||||||||||
| 48 | 'text/html' => 'HTML*', | ||||||||||||||||||||||||||||
| 49 | 'text/xml' => 'XML*', | ||||||||||||||||||||||||||||
| 50 | 'application/xml' => 'XML*', | ||||||||||||||||||||||||||||
| 51 | 'text/plain' => 'TXT*', | ||||||||||||||||||||||||||||
| 52 | 'application/pdf' => 'HTML*', | ||||||||||||||||||||||||||||
| 53 | 'application/msword' => 'HTML*', | ||||||||||||||||||||||||||||
| 54 | 'audio/mpeg' => 'XML*', | ||||||||||||||||||||||||||||
| 55 | 'default' => 'HTML*', | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 17 | our $DefaultExtension = 'html'; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # cache to avoid hitting MIME::Type each time | ||||||||||||||||||||||||||||
| 61 | 17 | my %ext2mime = ( | |||||||||||||||||||||||||||
| 62 | doc => 'application/msword', | ||||||||||||||||||||||||||||
| 63 | pdf => 'application/pdf', | ||||||||||||||||||||||||||||
| 64 | ppt => 'application/vnd.ms-powerpoint', | ||||||||||||||||||||||||||||
| 65 | html => 'text/html', | ||||||||||||||||||||||||||||
| 66 | htm => 'text/html', | ||||||||||||||||||||||||||||
| 67 | txt => 'text/plain', | ||||||||||||||||||||||||||||
| 68 | text => 'text/plain', | ||||||||||||||||||||||||||||
| 69 | xml => 'application/xml', | ||||||||||||||||||||||||||||
| 70 | mp3 => 'audio/mpeg', | ||||||||||||||||||||||||||||
| 71 | gz => 'application/x-gzip', | ||||||||||||||||||||||||||||
| 72 | xls => 'application/vnd.ms-excel', | ||||||||||||||||||||||||||||
| 73 | zip => 'application/zip', | ||||||||||||||||||||||||||||
| 74 | json => 'application/json', | ||||||||||||||||||||||||||||
| 75 | yml => 'application/x-yaml', | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # prime the cache with some typical defaults that MIME::Type won't match. | ||||||||||||||||||||||||||||
| 80 | 17 | $ext2mime{'php'} = 'text/html'; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 17 | eval { require MIME::Types }; | |||||||||||||||||||||||||||
| 83 | 17 | my $mime_types; | |||||||||||||||||||||||||||
| 84 | 17 | ▶ | if ( !$@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 17 | $mime_types = MIME::Types->new; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 17 | my $XML = Search::Tools::XML->new; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 METHODS | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head2 mime_type( I<url> [, I<ext> ] ) | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Returns MIME type for I<url>. If I<ext> is used, that is checked against | ||||||||||||||||||||||||||||
| 94 | MIME::Types. Otherwise the I<url> is parsed for an extension using | ||||||||||||||||||||||||||||
| 95 | path_parts() and then fed to MIME::Types. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 25 | ▶ | sub mime_type { | ||||||||||||||||||||||||||
|
Subroutine
mime_type: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 101 | 25 | ▶ | my $url = shift or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 25 | ▶ | my $ext = lc( shift || ( $self->path_parts($url) )[2] ); | ||||||||||||||||||||||||||
|
Condition100%3/3
shift() || ($self->path_parts($url))[2]
MC/DC100%2/2
shift() || ($self->path_parts($url))[2]
shift()
($self->path_parts($url))[2]
Truth table100%3/3
A || B
A= shift()B= ($self->path_parts($url))[2]
| |||||||||||||||||||||||||||||
| 103 | 25 | $ext =~ s/^\.//; | |||||||||||||||||||||||||||
| 104 | 25 | ▶ | $ext ||= $DefaultExtension; | ||||||||||||||||||||||||||
|
Condition66%2/3
$ext ||= $SWISH::Prog::Utils::DefaultExtension
MC/DC0%0/2
$ext ||= $SWISH::Prog::Utils::DefaultExtension
$ext
$SWISH::Prog::Utils::DefaultExtension
Truth table66%2/3
A ||= B
A= $extB= $SWISH::Prog::Utils::DefaultExtension
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #warn "$url => $ext"; | ||||||||||||||||||||||||||||
| 107 | 25 | ▶ | if ( !exists $ext2mime{$ext} and $mime_types ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not exists $ext2mime{$ext} and $mime_types
MC/DC50%1/2
not exists $ext2mime{$ext} and $mime_types
not exists $ext2mime{$ext}
$mime_types
Truth table66%2/3
not(A) and B
A= not exists $ext2mime{$ext}B= $mime_types
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # cache the mime type as a string | ||||||||||||||||||||||||||||
| 110 | # to avoid the MIME::Type::type() stringification | ||||||||||||||||||||||||||||
| 111 | 2 | ▶ | my $mime = $mime_types->mimeTypeOf($url) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 1 | $ext2mime{$ext} = $mime . ""; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 24 | return $ext2mime{$ext}; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head2 parser_for( I<url> ) | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Returns the SWISH parser type for I<url>. This can be | ||||||||||||||||||||||||||||
| 120 | configured via the C<%ParserTypes> class variable. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 2 | ▶ | sub parser_for { | ||||||||||||||||||||||||||
|
Subroutine
parser_for: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | 2 | ▶ | my $url = shift or croak "url required"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 2 | my $mime = $self->mime_type($url); | |||||||||||||||||||||||||||
| 128 | 2 | ▶ | my $parser = $ParserTypes{$mime} || $ParserTypes{'default'}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$SWISH::Prog::Utils::ParserTypes{$mime} || $SWISH::Prog::Utils::ParserTypes{'default'}
MC/DC0%0/2
$SWISH::Prog::Utils::ParserTypes{$mime} || $SWISH::Prog::Utils::ParserTypes{'default'}
$SWISH::Prog::Utils::ParserTypes{$mime}
$SWISH::Prog::Utils::ParserTypes{'default'}
Truth table66%2/3
A || B
A= $SWISH::Prog::Utils::ParserTypes{$mime}B= $SWISH::Prog::Utils::ParserTypes{'default'}
| |||||||||||||||||||||||||||||
| 129 | 2 | return $parser; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head2 path_parts( I<url> [, I<regex> ] ) | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Returns array of I<path>, I<file> and I<extension> using the | ||||||||||||||||||||||||||||
| 135 | File::Basename module. If I<regex> is missing or false, | ||||||||||||||||||||||||||||
| 136 | uses $ExtRE. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 19 | ▶ | sub path_parts { | ||||||||||||||||||||||||||
|
Subroutine
path_parts: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | 19 | my $url = shift; | |||||||||||||||||||||||||||
| 143 | 19 | ▶ | my $re = shift || $ExtRE; | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $SWISH::Prog::Utils::ExtRE
MC/DC0%0/2
shift() || $SWISH::Prog::Utils::ExtRE
shift()
$SWISH::Prog::Utils::ExtRE
Truth table33%1/3
A || B
A= shift()B= $SWISH::Prog::Utils::ExtRE
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # TODO build regex from ->config | ||||||||||||||||||||||||||||
| 146 | 19 | my ( $file, $path, $ext ) = fileparse( $url, $re ); | |||||||||||||||||||||||||||
| 147 | 19 | return ( $path, $file, $ext ); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 perl_to_xml( I<ref>, I<root_element> [, I<strip_plural> ] ) | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Similar to the XML::Simple XMLout() feature, perl_to_xml() | ||||||||||||||||||||||||||||
| 153 | will take a Perl data structure I<ref> and convert it to XML, | ||||||||||||||||||||||||||||
| 154 | using I<root_element> as the top-level element. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | As of version 0.38 this method is now part of Search::Tools | ||||||||||||||||||||||||||||
| 157 | and included here simply as a backcompat feature. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | sub perl_to_xml { | ||||||||||||||||||||||||||
|
Subroutine
perl_to_xml: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | 0 | return $XML->perl_to_xml(@_); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 write_log( I<args> ) | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Logging method. By default writes to stderr via warn(). | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | I<args> is a key/value pair hash, with keys B<uri> and B<msg>. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1 | ▶ | sub write_log { | ||||||||||||||||||||||||||
|
Subroutine
write_log: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 176 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 177 | 1 | ▶ | my $uri = delete $args{uri} or croak "uri required"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 1 | ▶ | my $msg = delete $args{msg} or croak "msg required"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 1 | warn sprintf( "[%s][%s] %s [%s]\n", scalar localtime(), $$, $uri, $msg ); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 write_log_line([I<char>, I<width>]) | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Writes I<char> x I<width> to stderr, to provide some visual separation when viewing logs. | ||||||||||||||||||||||||||||
| 185 | I<char> defaults to C<-> and I<width> to C<80>. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub write_log_line { | ||||||||||||||||||||||||||
|
Subroutine
write_log_line: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | my $char = shift || '-'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || '-'
MC/DC0%0/1
shift() || '-'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | my $width = shift || 80; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 80
MC/DC0%0/1
shift() || 80
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 193 | 0 | warn $char x $width, "\n"; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 17 | 1; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | __END__ | ||||||||||||||||||||||||||||