SherlockSearch.pm

filter: Statementstmt 5.3% 20 / 375 Branchbran 0.0% 0 / 152 Conditioncond 0.0% 0 / 49 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 12.8% 5 / 39 Podpod 0.0% 0 / 30 totaltotal 3.7% 25 / 676 CC 108 SCAR 54.8
CC
108
Cov
3%
convertResults
930.0
parseSherlock
156.0
find
156.0
CRAP 238.7
↑ Summary SherlockSearch/Results.pm »
1 # $File: //member/autrijus/WWW-SherlockSearch/lib/WWW/SherlockSearch.pm $ $Author: autrijus $
2 # $Revision: #32 $ $Change: 10623 $ $DateTime: 2004/05/22 08:07:29 $ vim: expandtab shiftwidth=4
3  
4 package WWW::SherlockSearch;
5 1 $WWW::SherlockSearch::VERSION = '0.20';
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use vars qw($ExcerptLength $UAClass);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use WWW::SherlockSearch::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 $ExcerptLength = 100;
15 1 $UAClass = 'LWP::RobotUA';
16  
17 =head1 NAME
18  
19 WWW::SherlockSearch - Parse and execute Apple Sherlock 2 plugins
20  
21 =head1 VERSION
22  
23 This document describes version 0.20 of WWW::SherlockSearch, released
24 May 22, 2004.
25  
26 =head1 SYNOPSIS
27  
28 use WWW::SherlockSearch;
29  
30 my $sherlock = WWW::SherlockSearch->new('google.src');
31  
32 my $text = $sherlock->asString;
33 my $rss = $sherlock->asRssString;
34 my $src = $sherlock->asSherlockString;
35  
36 # fiind 'test' with limit '10'
37 my $results = $sherlock->find("test", 10);
38  
39 my $text = $results->asString;
40 my $rss = $results->asRssString;
41 my $html = $results->asHtmlString;
42  
43 =head1 DESCRIPTION
44  
45 This module parses and executes Apple Sherlock 2 plugin files,
46 and generate a result set that can be expressed in text, HTML
47 or RSS format. It is a repackaged and cleaned-up version of
48 Damian Steer's B<Sherch> service at L<http://www.sherch.com/>.
49  
50 The module differ from other Sherlock implementation in that
51 it can actually follow the individual links and extract the
52 full text within it, delimited by the C<resultContentStart>
53 and C<resultContentEnd> tags. In RSS, they will be expressed
54 via the C<content:encoded> attribute proposed by Aaron.
55  
56 If there is no I<description> but I<content> is available, the
57 C<$WWW::SherlockSearch::ExcerptLength> variable is used to
58 determine how many leading characters to use to generate the
59 description from content (defaults to C<100>). Setting it to
60 C<0> disables this feature.
61  
62 Please see L<http://mycroft.mozdev.org/> for a repository and
63 detailed description of Sherlock 2 plugins.
64  
65 =cut
66  
67 0 ▶sub import {
Subroutine import: not called CC=2 SCAR=17.9
68 0 my $class = shift;
69 0 ▶ $UAClass = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
70 }
71  
72 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
73 0 my $type = shift;
74 0 my $self = {};
75 0 bless($self, $type);
76 0 ▶ $self->loadFile(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
77 0 return $self;
78 }
79  
80 0 ▶sub getChannelUrl {
Subroutine getChannelUrl: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my $self = shift;
82 0 return $self->{channelUrl};
83 }
84  
85 0 ▶sub setChannelUrl {
Subroutine setChannelUrl: not called CC=1 SCAR=6.9
Pod undocumented
86 0 my $self = shift;
87 0 $self->{channelUrl} = shift;
88 0 return $self;
89 }
90  
91 0 ▶sub getQueryAttr {
Subroutine getQueryAttr: not called CC=1 SCAR=6.9
Pod undocumented
92 0 my $self = shift;
93 0 return $self->{queryAttr};
94 }
95  
96 0 ▶sub setQueryAttr {
Subroutine setQueryAttr: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my $self = shift;
98 0 $self->{queryAttr} = shift;
99 0 return $self;
100 }
101  
102 0 ▶sub getPictureUrl {
Subroutine getPictureUrl: not called CC=1 SCAR=6.9
Pod undocumented
103 0 my $self = shift;
104 0 return $self->{pictureUrl};
105 }
106  
107 0 ▶sub setPictureUrl {
Subroutine setPictureUrl: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my $self = shift;
109 0 $self->{pictureUrl} = shift;
110 0 return $self;
111 }
112  
113 0 ▶sub loadFile {
Subroutine loadFile: not called CC=4 SCAR=30.0
Pod undocumented
114 0 my $self = shift;
115 0 ▶ my $filename = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $filename = shift() or return 0 0 0
116  
117 0 ▶ if (UNIVERSAL::isa($filename, 'SCALAR')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($filename, 'SCALAR')) { } 0 0 0
118 0 $self->initialiseSearch($$filename);
119 }
120 else {
121 0 local $/;
122 0 ▶ open(SHERFILE, $filename) or die "Couldn't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open SHERFILE, $filename or die "Couldn't open ${filename}: $!" 0 0 0
123 0 $self->initialiseSearch(<SHERFILE>);
124 0 close SHERFILE;
125 }
126  
127 0 return $self;
128 }
129  
130 0 ▶sub initialiseSearch {
Subroutine initialiseSearch: not called CC=3 SCAR=24.8
Pod undocumented
131 0 my ($self, $content) = @_;
132 0 my ($action, $basehref, $host);
133  
134 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 0 0
135 0 @{$self}{qw{
136 search interpretList inputList
137 prefetch preinputList postfetch postinputList
138 }} = parseSherlock(\$content);
139 }
140  
141 0 ▶ $action = $self->{search}{action} or return;
Branch: 1 branch
BranchTrueFalseTotal
$action = $self->{"search"}{"action"} or return 0 0 0
142  
143 0 ($basehref) = ($action =~ /(.*\/)/);
144 0 ($host) = ($action =~ /(.*\/\/.*?)\//);
145  
146 0 $self->{basehref} = $basehref;
147 0 $self->{host} = $host;
148  
149 0 return $self;
150 }
151  
152 # The following parses sherlock .src files
153  
154 # This takes a sherlock file, strips the comments, then passes the
155 # individual tags for further parsing.
156  
157 1 my %Attr = (
158 search => [qw{
159 name method action update updateCheckDays
160 description bannerImage bannerLink routeType
161 queryEncoding queryCharset queryLimit
162 }],
163 input => [qw{
164 value name user user1 user2 user3 usern prefix suffix mode
165 }],
166 interpret => [qw{
167 bannerStart bannerEnd relevanceStart relevanceEnd
168 resultListStart resultListEnd resultItemStart resultItemEnd
169 priceStart priceEnd availStart availEnd dateStart dateEnd
170 nameStart nameEnd emailStart emailEnd
171 pageNextStart pageNextEnd
172 resultItemFind resultItemReplace
173 resultContentStart resultContentEnd
174 }],
175 );
176  
177 0 ▶sub parseSherlock {
Subroutine parseSherlock: not called CC=12 SCAR=50.5
Pod undocumented
178 0 my $sherfiletoparse = shift;
179  
180 # XXX: inputprev, inputnext?
181 0 my $tags = join('|', qw{
182 search interpret input prefetch postfetch
183 /search /prefetch /postfetch
184 });
185 0 my $searchAttributes = join('|', map lc, @{$Attr{search}});
186 0 my $inputAttributes = join('|', map lc, @{$Attr{input}});
187 0 my $interpretAttributes = join('|', map lc, @{$Attr{interpret}});
188  
189 0 my ($search, $prefetch, $postfetch, $interpret, $input);
190 0 my $interpretList = [];
191 0 my $inputList = [];
192 0 my $preinputList = [];
193 0 my $postinputList = [];
194 0 my ($tag, $current_tag);
195  
196 0 $$sherfiletoparse =~ s/\r/\n/g; # fix line endings
197 0 $$sherfiletoparse =~ s/(?:\s+|^)\#(?:\s+$).*//g; # remove comment lines
198  
199 PARSELOOP:
200 0 ▶ while ($$sherfiletoparse =~ /<\s*($tags)/gcis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$sherfiletoparse =~ /<\s*($tags)/cgis) 0 0 0
201 0 $tag = $1;
202 0 $tag =~ tr/A-Z/a-z/;
203  
204 0 ▶ if ($tag eq 'search') {
Branch: 8 branches
BranchTrueFalseTotal
if ($tag eq 'search') { } 0 0 0
elsif ($tag eq 'prefetch') { } 0 0 0
elsif ($tag eq 'postfetch') { } 0 0 0
elsif ($tag eq 'interpret') { } 0 0 0
elsif ($tag eq 'input') { } 0 0 0
elsif ($tag eq '/prefetch') { } 0 0 0
elsif ($tag eq '/postfetch') { } 0 0 0
elsif ($tag eq '/search') { } 0 0 0
205 0 $search = parseAttValString($sherfiletoparse, $searchAttributes);
206 0 $current_tag = 'search';
207 }
208 elsif ($tag eq 'prefetch') {
209 0 $prefetch =
210 parseAttValString($sherfiletoparse, $searchAttributes);
211 0 $current_tag = 'prefetch';
212 }
213 elsif ($tag eq 'postfetch') {
214 0 $postfetch =
215 parseAttValString($sherfiletoparse, $searchAttributes);
216 0 $current_tag = 'postfetch';
217 }
218 elsif ($tag eq 'interpret') {
219 0 $interpret =
220 parseAttValString($sherfiletoparse, $interpretAttributes);
221 0 push (@{$interpretList}, $interpret);
222 }
223 elsif ($tag eq 'input') {
224 0 $input = parseAttValString($sherfiletoparse, $inputAttributes);
225 0 ▶ if ($current_tag eq "prefetch") {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_tag eq 'prefetch') { } 0 0 0
elsif ($current_tag eq 'postfetch') { } 0 0 0
226 0 push (@{$preinputList}, $input);
227 }
228 elsif ($current_tag eq "postfetch") {
229 0 push (@{$postinputList}, $input);
230 }
231 else {
232 0 push (@{$inputList}, $input);
233 }
234 }
235 elsif ($tag eq '/prefetch') {
236 0 $current_tag = '';
237 }
238 elsif ($tag eq '/postfetch') {
239 0 $current_tag = '';
240 }
241 elsif ($tag eq '/search') {
242 0 last PARSELOOP;
243 }
244 }
245 0 return ($search, $interpretList, $inputList, $prefetch, $preinputList,
246 $postfetch, $postinputList);
247 }
248  
249 # This parses a string containg items of the form attribute = string
250 # or just attribute and returns a hash of attribute=>value.
251 # For lone attributes value = 'true'.
252 # $parse is of the form "attr|attr|attr|...|attr"
253  
254 0 ▶sub parseAttValString {
Subroutine parseAttValString: not called CC=5 SCAR=34.0
Pod undocumented
255 0 my $stringtoparse = shift;
256 0 my $attrList = shift;
257  
258 0 my ($value, $attribute);
259 0 my $returnHash = {};
260  
261 PARSELOOP2:
262 0 ▶ while ($$stringtoparse =~ /($attrList)\s*(=| |>)\s*/gcis) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$stringtoparse =~ /($attrList)\s*(=| |>)\s*/cgis) 0 0 0
263 0 $attribute = $1;
264 0 $attribute =~ tr/A-Z/a-z/; # lowercase to make my life easy
265 0 ▶ if ($2 eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq '=') { } 0 0 0
266 0 $value = parseString($stringtoparse);
267 }
268 0 else { $value = 'true'; }
269 0 $returnHash->{$attribute} = $value;
270 0 ▶ if (($2 eq '>') or ($$stringtoparse =~ /\G\s*>/gcs)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq ">" or $$stringtoparse =~ /\G\s*>/cgs) 0 0 0
Condition0%0/3
$2 eq ">" or $$stringtoparse =~ /\G\s*>/cgs
l!l&&r!l&&!r
000
MC/DC0%0/2
$2 eq ">" or $$stringtoparse =~ /\G\s*>/cgs
$2 eq ">" $$stringtoparse =~ /\G\s*>/cgs
Truth table0%0/3
A or B
A= $2 eq ">"B= $$stringtoparse =~ /\G\s*>/cgs
ABresult
1X1
011
000
271 0 last PARSELOOP2;
272 }
273 }
274 0 return $returnHash;
275 }
276  
277 # This takes a string of the form "....", '.....',
278 # or just ..... (no whitespace)
279 # And returns the ..... bit.
280  
281 0 ▶sub parseString {
Subroutine parseString: not called CC=7 SCAR=40.3
Pod undocumented
282 0 my $string = shift;
283 0 my $content;
284 0 my $skip = 2;
285  
286 0 ▶ if ($$string =~ /\G\"/gcs) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$string =~ /\G\"/cgs) { } 0 0 0
elsif ($$string =~ /\G\'/cgs) { } 0 0 0
287 0 ▶ $content = $1 if ($$string =~ /\G(.*?[^\\])\"/gcs);
Branch: 1 branch
BranchTrueFalseTotal
if $$string =~ /\G(.*?[^\\])\"/cgs 0 0 0
288 0 $content =~ s{\\"}{"}g;
289 }
290 elsif ($$string =~ /\G\'/gcs) {
291 0 ▶ $content = $1 if ($$string =~ /\G(.*?[^\\])\'/gcs);
Branch: 1 branch
BranchTrueFalseTotal
if $$string =~ /\G(.*?[^\\])\'/cgs 0 0 0
292 0 $content =~ s{\\'}{'}g;
293 }
294 else {
295 0 ▶ $content = $1 if ($$string =~ /\G(\S+)/gcs);
Branch: 1 branch
BranchTrueFalseTotal
if $$string =~ /\G(\S+)/cgs 0 0 0
296 0 ▶ if ($content =~ s/>$//) # this removes a closing tag (if present)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ s/>$//) 0 0 0
297 {
298 0 pos($$string)--; # this corrects for that removal
299 }
300 }
301 0 return $content;
302 }
303  
304 0 ▶sub printHash {
Subroutine printHash: not called CC=2 SCAR=17.9
Pod undocumented
305 0 my $hashRef = shift;
306 0 my $tab = shift;
307  
308 0 my $key;
309  
310 0 foreach $key (keys %$hashRef) {
311 0 print "$tab$key := ", $hashRef->{$key}, "\n";
312 }
313 }
314  
315 0 ▶sub find {
Subroutine find: not called CC=12 SCAR=50.5
Pod undocumented
316 0 my ($self, $query, $limit, $skip_href) = @_;
317  
318 0 my $ua_pm = "$UAClass.pm";
319 0 $ua_pm =~ s{::}{/}g;
320 0 require $ua_pm;
321
322 0 my $search = $UAClass->new(
323 'Mozilla/5.0 Gecko/libwww-perl', 'autrijus@cpan.org',
324 );
325 0 my ($result, @post, $get, $rv);
326  
327 0 $search->cookie_jar(HTTP::Cookies->new);
328  
329 0 foreach my $stage (qw/prefetch search postfetch/) {
330 0 ▶ next unless $self->{$stage}{method} and $self->{$stage}{action};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$stage}{"method"} and $self->{$stage}{"action"} 0 0 0
Condition0%0/3
$self->{$stage}{"method"} and $self->{$stage}{"action"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{$stage}{"method"} and $self->{$stage}{"action"}
$self->{$stage}{"method"} $self->{$stage}{"action"}
Truth table0%0/3
A and B
A= $self->{$stage}{"method"}B= $self->{$stage}{"action"}
ABresult
0X0
100
111
331  
332 my $find = _encode(
333 $self->{$stage}{querycharset} || $self->{$stage}{queryencoding},
334 0 ▶ $query,
Condition0%0/3
$self->{$stage}{'querycharset'} || $self->{$stage}{'queryencoding'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$stage}{'querycharset'} || $self->{$stage}{'queryencoding'}
$self->{$stage}{'querycharset'} $self->{$stage}{'queryencoding'}
Truth table0%0/3
A || B
A= $self->{$stage}{'querycharset'}B= $self->{$stage}{'queryencoding'}
ABresult
1X1
011
000
335 );
336  
337 0 ▶ if ($self->{$stage}->{method} =~ /post/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{$stage}{'method'} =~ /post/i) { } 0 0 0
elsif ($self->{$stage}{'method'} =~ /get/i) { } 0 0 0
338 0 @post = $self->getPostData($stage, $find);
339 0 $result = $search->request(POST $self->{$stage}->{action}, \@post);
340 }
341 elsif ($self->{$stage}->{method} =~ /get/i) {
342 0 $get = $self->getGetData($stage, $find);
343 0 ▶ $get = '' if $get eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $get eq "?" 0 0 0
344 0 $result = $search->request(GET $self->{$stage}->{action} . $get);
345 }
346 else {
347 0 die "Unknown method: $stage / $self->{$stage}->{method}";
348 }
349  
350 0 ▶ next unless $stage eq 'search';
Branch: 1 branch
BranchTrueFalseTotal
unless $stage eq "search" 0 0 0
351
352 0 ▶ while ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) 0 0 0
353 0 ▶ if (!$result->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->is_success) 0 0 0
354 0 print "$stage / $self->{$stage}->{method}: Warn: " .
355 $result->code . " " . $result->message;
356 0 last;
357 }
358  
359 $self->{content} = _decode(
360 $self->{$stage}{querycharset} || $self->{$stage}{queryencoding},
361 0 ▶ $result->content,
Condition0%0/3
$self->{$stage}{'querycharset'} || $self->{$stage}{'queryencoding'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$stage}{'querycharset'} || $self->{$stage}{'queryencoding'}
$self->{$stage}{'querycharset'} $self->{$stage}{'queryencoding'}
Truth table0%0/3
A || B
A= $self->{$stage}{'querycharset'}B= $self->{$stage}{'queryencoding'}
ABresult
1X1
011
000
362 );
363  
364 ($rv, $result) = $self->convertResults(
365 0 $self->{content}, $search, $limit, $skip_href, $result->date, $rv
366 );
367 }
368 }
369  
370 0 return $rv;
371 }
372  
373 0 ▶sub _encode {
Subroutine _encode: not called CC=3 SCAR=24.8
374 0 ▶ my $charset = shift || 'utf8';
Condition0%0/2
shift() || 'utf8'
l!l
00
MC/DC0%0/1
shift() || 'utf8'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
375 0 ▶ require Encode::compat if $] < 5.007001; require Encode;
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.007001" 0 0 0
376 0 return Encode::encode($charset, $_[0]);
377 }
378  
379 0 ▶sub _decode {
Subroutine _decode: not called CC=3 SCAR=24.8
380 0 ▶ my $charset = shift || 'utf8';
Condition0%0/2
shift() || 'utf8'
l!l
00
MC/DC0%0/1
shift() || 'utf8'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
381 0 ▶ require Encode::compat if $] < 5.007001; require Encode;
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.007001" 0 0 0
382 0 return Encode::decode($charset, $_[0]);
383 }
384  
385 0 ▶sub getPostData {
Subroutine getPostData: not called CC=6 SCAR=37.4
Pod undocumented
386 0 my ($self, $tag, $find) = @_;
387 0 my (@post, $item);
388 0 my $list;
389  
390 0 ▶ if ($tag eq 'prefetch') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'prefetch') { } 0 0 0
elsif ($tag eq 'postfetch') { } 0 0 0
391 0 $list = 'preinputList';
392 }
393 elsif ($tag eq 'postfetch') {
394 0 $list = 'postinputList';
395 }
396 else {
397 0 $list = 'inputList';
398 }
399  
400 0 foreach $item (@{ $self->{$list} }) {
401 0 ▶ if ($item->{user}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item->{'user'}) { } 0 0 0
elsif ($item->{'mode'} ne 'browser') { } 0 0 0
402 0 push (@post, $item->{name}, $find);
403 }
404 elsif ($item->{mode} ne 'browser') {
405 0 push (@post, $item->{name}, $item->{value});
406 }
407 }
408 0 return @post;
409 }
410  
411 0 ▶sub getGetData {
Subroutine getGetData: not called CC=6 SCAR=37.4
Pod undocumented
412 0 my ($self, $tag, $find) = @_;
413 0 my ($get, $item, $amp);
414 0 my $list;
415  
416 0 ▶ if ($tag eq 'prefetch') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'prefetch') { } 0 0 0
elsif ($tag eq 'postfetch') { } 0 0 0
417 0 $list = 'preinputList';
418 }
419 elsif ($tag eq 'postfetch') {
420 0 $list = 'postinputList';
421 }
422 else {
423 0 $list = 'inputList';
424 }
425  
426 0 $get = "?";
427 0 $amp = "";
428 0 foreach $item (@{ $self->{$list} }) {
429 0 ▶ if ($item->{user}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item->{'user'}) { } 0 0 0
elsif ($item->{'mode'} ne 'browser') { } 0 0 0
430 0 $get .= $amp . $item->{name} . "=" . $find;
431 0 $amp = "&";
432 }
433 elsif ($item->{mode} ne 'browser') {
434 0 $get .= $amp . $item->{name} . "=" . $item->{value};
435 0 $amp = "&";
436 }
437 }
438 0 return $get;
439 }
440  
441 # The following methods are used to interpret results
442  
443 0 ▶sub convertResults {
Subroutine convertResults: not called CC=30 SCAR=68.4
Pod undocumented
444 0 my ($self, $html, $search, $limit, $skip_href, $result_date, $resultStruct) = @_;
445 0 ▶ $limit ||= $self->{search}{querylimit};
Condition0%0/3
$limit ||= $self->{"search"}{"querylimit"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$limit ||= $self->{"search"}{"querylimit"}
$limit $self->{"search"}{"querylimit"}
Truth table0%0/3
A ||= B
A= $limitB= $self->{"search"}{"querylimit"}
ABresult
1X1
011
000
446  
447 # It appears plugins can have more than one interpet tag
448 # I only use the first
449 0 my $interpret = $self->{interpretList}->[0];
450 0 my ($banner, @results, $bannerimageurl, $bannerurl, $pagenexturl);
451  
452 0 ▶ if (!$resultStruct) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resultStruct) 0 0 0
453 0 $resultStruct = WWW::SherlockSearch::Results->new;
454 0 $resultStruct->setServiceName($self->{search}{name});
455 0 $resultStruct->setServiceDescription($self->{search}{description});
456 0 $resultStruct->setBaseHREF($self->{basehref});
457 0 $resultStruct->setHost($self->{host});
458 0 $resultStruct->setPictureUrl($self->getPictureUrl);
459 $resultStruct->setChannelUrl(
460 $self->getChannelUrl ||
461 0 ▶ ($self->{search}{action} . $self->getGetData)
Condition0%0/3
$self->getChannelUrl || $self->{'search'}{'action'} . $self->getGetData
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->getChannelUrl || $self->{'search'}{'action'} . $self->getGetData
$self->getChannelUrl $self->{'search'}{'action'} . $self->getGetData
Truth table0%0/3
A || B
A= $self->getChannelUrlB= $self->{'search'}{'action'} . $self->getGetData
ABresult
1X1
011
000
462 );
463 0 $resultStruct->setQueryAttr($self->getQueryAttr);
464 }
465  
466 # get that banner
467 0 ▶ if ($interpret->{bannerstart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpret->{'bannerstart'}) { } 0 0 0
468 ($banner) = getDelimited(
469 \$html,
470 $interpret->{bannerstart},
471 $interpret->{bannerend}
472 0 );
473 0 ($bannerimageurl) = $self->getIMG($banner);
474 0 ($bannerurl) = $self->getHREF($banner);
475 0 $resultStruct->setBannerImage($bannerimageurl);
476 0 $resultStruct->setBannerLink($bannerurl);
477 }
478 else {
479 $resultStruct->setBannerLink(
480 0 $self->fixRef($self->{search}{bannerlink}));
481 0 $bannerimageurl = $self->getIMG($self->{search}{bannerimage});
482 0 ▶ if (!$bannerimageurl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bannerimageurl) 0 0 0
483 0 $bannerimageurl = $self->fixRef($self->{search}{bannerimage});
484 }
485 0 $resultStruct->setBannerImage($bannerimageurl);
486 }
487  
488 0 ▶ if ($interpret->{pagenextstart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpret->{"pagenextstart"}) 0 0 0
489 ($pagenexturl) = getDelimited(
490 \$html,
491 $interpret->{pagenextstart},
492 $interpret->{pagenextend}
493 0 );
494 0 ($pagenexturl) = $self->getHREF($pagenexturl);
495 }
496  
497 0 ▶ if ($interpret->{resultliststart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpret->{"resultliststart"}) 0 0 0
498 ($html) = getDelimited(
499 \$html,
500 $interpret->{resultliststart},
501 $interpret->{resultlistend}
502 0 );
503 }
504 0 ▶ if ($interpret->{resultitemstart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpret->{'resultitemstart'}) { } 0 0 0
505 @results = getDelimited(
506 \$html,
507 $interpret->{resultitemstart},
508 $interpret->{resultitemend}
509 0 );
510 }
511 else {
512 0 @results = ($html =~ /(<\s*A[^>]+HREF\s*=.*?(?:<\/A>|$))/sgi);
513 }
514  
515 # Find-and-Replace
516 # Thanks for mtve @ #perl for this.
517 0 ▶ if (length $interpret->{resultitemfind}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $interpret->{"resultitemfind"}) 0 0 0
518 0 my $find = $interpret->{resultitemfind};
519 0 $find =~ s|\\Q(.*?)\\E|quotemeta($1)|eg;
520  
521 0 foreach (@results) {
522 0 s{$find}{"qq($interpret->{resultitemreplace})"}ees;
523 }
524 }
525  
526 0 my ($item, $temp, $relev, $itemurl, $content, $rest, $fulltext, $date);
527  
528 0 foreach $item (@results) {
529 0 require HTML::Entities;
530 0 $item =~ s/&nbsp;/ /g; # :-(~~~
531 0 HTML::Entities::decode_entities($item);
532  
533 0 ▶ if ($interpret->{relevancestart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpret->{"relevancestart"}) 0 0 0
534 ($temp) = getDelimited(
535 \$item,
536 $interpret->{relevancestart},
537 $interpret->{relevanceend}
538 0 );
539 0 ($relev) = ($temp =~ /(\d+)/s);
540 }
541 0 ($itemurl, $content, $rest) = $self->getHREF($item);
542  
543 0 ▶ if ($interpret->{namestart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpret->{"namestart"}) 0 0 0
544 ($temp) = getDelimited(\$item, $interpret->{namestart},
545 0 $interpret->{nameend});
546 0 ▶ $content = $temp if $temp;
Branch: 1 branch
BranchTrueFalseTotal
if $temp 0 0 0
547 }
548  
549 0 ▶ if ($interpret->{datestart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpret->{"datestart"}) 0 0 0
550 ($temp) = getDelimited(\$item, $interpret->{datestart},
551 0 $interpret->{dateend});
552 0 ▶ $date = $temp if $temp;
Branch: 1 branch
BranchTrueFalseTotal
if $temp 0 0 0
553 }
554  
555 # The following strips tags, $content, line endings and relevance
556 # in the hope that removing this garbage will leave a nice summary
557  
558 0 stripTags(\$content);
559  
560 0 ▶ next if $content =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $content =~ /^\s*$/ 0 0 0
561  
562 0 ▶ $rest = $content unless ($rest);
Branch: 1 branch
BranchTrueFalseTotal
unless $rest 0 0 0
563  
564 0 stripTags(\$rest);
565 0 ▶ $rest =~ s/$relev\%?//g if $relev;
Branch: 1 branch
BranchTrueFalseTotal
if $relev 0 0 0
566 0 ▶ next if $skip_href and exists $skip_href->{$itemurl};
Branch: 1 branch
BranchTrueFalseTotal
if $skip_href and exists $skip_href->{$itemurl} 0 0 0
Condition0%0/3
$skip_href and exists $skip_href->{$itemurl}
!ll&&!rl&&r
000
MC/DC0%0/2
$skip_href and exists $skip_href->{$itemurl}
$skip_href exists $skip_href->{$itemurl}
Truth table0%0/3
A and B
A= $skip_hrefB= exists $skip_href->{$itemurl}
ABresult
0X0
100
111
567  
568 0 ▶ if ($interpret->{resultcontentstart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpret->{"resultcontentstart"}) 0 0 0
569 0 my $result = $search->request(GET $itemurl);
570  
571 0 ▶ if ($result->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_success) 0 0 0
572 my $item = _decode(
573 $self->{search}{querycharset} || $self->{search}{queryencoding},
574 0 ▶ $result->content,
Condition0%0/3
$self->{'search'}{'querycharset'} || $self->{'search'}{'queryencoding'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'search'}{'querycharset'} || $self->{'search'}{'queryencoding'}
$self->{'search'}{'querycharset'} $self->{'search'}{'queryencoding'}
Truth table0%0/3
A || B
A= $self->{'search'}{'querycharset'}B= $self->{'search'}{'queryencoding'}
ABresult
1X1
011
000
575 );
576  
577 ($fulltext) = getDelimited(
578 \$item,
579 $interpret->{resultcontentstart},
580 $interpret->{resultcontentend}
581 0 );
582  
583 0 require HTML::Entities;
584 0 $fulltext =~ s/<[bB][rR]\b([^>]*)>/\n/gs;
585 0 $fulltext =~ s/&nbsp;/ /g; # :-(~~~
586 0 HTML::Entities::decode_entities($fulltext);
587  
588 0 stripTags(\$fulltext);
589 0 ▶ $date ||= $result->date;
Condition0%0/3
$date ||= $result->date
l!l&&r!l&&!r
000
MC/DC0%0/2
$date ||= $result->date
$date $result->date
Truth table0%0/3
A ||= B
A= $dateB= $result->date
ABresult
1X1
011
000
590 }
591 }
592  
593 $resultStruct->add(
594 0 ▶ $itemurl, $content, $relev, $rest, $fulltext, $date || $result_date
Condition0%0/3
$date || $result_date
l!l&&r!l&&!r
000
MC/DC0%0/2
$date || $result_date
$date $result_date
Truth table0%0/3
A || B
A= $dateB= $result_date
ABresult
1X1
011
000
595 );
596  
597 0 ▶ if ($limit and $resultStruct->getNumResults >= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit and $resultStruct->getNumResults >= $limit) 0 0 0
Condition0%0/3
$limit and $resultStruct->getNumResults >= $limit
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $resultStruct->getNumResults >= $limit
$limit $resultStruct->getNumResults >= $limit
Truth table0%0/3
A and B
A= $limitB= $resultStruct->getNumResults >= $limit
ABresult
0X0
100
111
598 0 $pagenexturl = ''; # no next page, please
599 0 last;
600 }
601 }
602  
603 0 $self->{resultArray} = $resultStruct;
604  
605 return (
606 0 ▶ $resultStruct,
Branch: 1 branch
BranchTrueFalseTotal
$pagenexturl ? : 0 0 0
607 $pagenexturl ? ($search->request(GET $pagenexturl)) : ()
608 );
609 }
610  
611 0 ▶sub stripTags {
Subroutine stripTags: not called CC=1 SCAR=6.9
Pod undocumented
612 0 my $var = shift;
613 0 $$var =~ s/<[bB][rR]\b([^>]*)>/\n/gs;
614 0 $$var =~ s/<([^>]+)(?:>|$)//gs;
615 0 $$var =~ s/^\s*//;
616 0 $$var =~ s/\s*$//;
617 0 $$var =~ s/\s+/ /g;
618 }
619  
620 0 ▶sub getHREF {
Subroutine getHREF: not called CC=1 SCAR=6.9
Pod undocumented
621 0 my ($self, $html) = @_;
622 0 my ($itemurl, $content, $rest) =
623 ($html =~ /<\s*A[^>]+HREF\s*=\s*(.*?)>(.*?)(?:<\/A>|$)(.*)/si);
624 0 ($itemurl) = ($itemurl =~ /^\'?\"?(\S+)/);
625 0 $itemurl =~ s/\'?\"?$//;
626 0 $itemurl = $self->fixRef($itemurl);
627 0 return ($itemurl, $content, $rest);
628 }
629  
630 0 ▶sub getIMG {
Subroutine getIMG: not called CC=1 SCAR=6.9
Pod undocumented
631 0 my ($self, $html) = @_;
632 0 my ($itemurl) = ($html =~ /<\s*IMG\s+SRC\s*=\s*(.*?)>/si);
633 0 ($itemurl) = ($itemurl =~ /^\'?\"?(\S+)/);
634 0 $itemurl =~ s/\'?\"?$//;
635 0 $itemurl = $self->fixRef($itemurl);
636 0 return $itemurl;
637 }
638  
639 0 ▶sub fixRef {
Subroutine fixRef: not called CC=6 SCAR=37.4
Pod undocumented
640 0 my ($self, $url) = @_;
641 0 my ($basehref, $host);
642 0 ▶ if (!$url) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
643 0 $basehref = $self->{basehref};
644 0 $host = $self->{host};
645  
646 # This doesn't work for relative links :-(
647 0 ▶ if ($url !~ m{^(?:\w+:)?//}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ m[^(?:\w+:)?//]) 0 0 0
648 0 ▶ $url = ($url =~ m{^/}) ? $host . $url : $basehref . $url;
Branch: 1 branch
BranchTrueFalseTotal
$url =~ m[^/] ? : 0 0 0
649 }
650 0 ▶ if ($url =~ m{^//} and $basehref =~ m{^(\w+:)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^//] and $basehref =~ /^(\w+:)/) 0 0 0
Condition0%0/3
$url =~ m[^//] and $basehref =~ /^(\w+:)/
!ll&&!rl&&r
000
MC/DC0%0/2
$url =~ m[^//] and $basehref =~ /^(\w+:)/
$url =~ m[^//] $basehref =~ /^(\w+:)/
Truth table0%0/3
A and B
A= $url =~ m[^//]B= $basehref =~ /^(\w+:)/
ABresult
0X0
100
111
651 0 $url = $1 . $url;
652 }
653 0 return $url;
654 }
655  
656 0 ▶sub getResults {
Subroutine getResults: not called CC=1 SCAR=6.9
Pod undocumented
657 0 my $self = shift;
658 0 return $self->{resultArray};
659 }
660  
661 0 ▶sub getDelimited {
Subroutine getDelimited: not called CC=1 SCAR=6.9
Pod undocumented
662 0 my ($list, $left, $right) = @_;
663 0 my @results;
664  
665 0 @results = ($$list =~ /\Q$left\E(.*?)\Q$right\E/gis);
666  
667 0 return @results;
668 }
669  
670 0 ▶sub asString {
Subroutine asString: not called CC=5 SCAR=34.0
Pod undocumented
671 0 my $self = shift;
672 0 my $string = "Search :\n\n";
673  
674 0 $string .= "Base Href := " . $self->{basehref} . "\n";
675 0 $string .= "Host := " . $self->{host} . "\n";
676 0 foreach my $key (keys %{ $self->{search} }) {
677 0 $string .= "$key := " . $self->{search}{$key} . "\n";
678 }
679  
680 0 $string .= "\nInterpret :\n\n";
681  
682 0 foreach my $key (keys %{ $self->{interpretList}->[0] }) {
683 0 $string .= "$key := " . $self->{interpretList}->[0]->{$key} . "\n";
684 }
685  
686 0 foreach my $hash (@{ $self->{inputList} }) {
687 0 $string .= "\nInput :\n";
688 0 foreach my $key (keys %{$hash}) {
689 0 $string .= "\t$key := " . $hash->{$key} . "\n";
690 }
691 }
692 0 return $string;
693 }
694  
695 0 ▶sub _fmt {
Subroutine _fmt: not called CC=7 SCAR=40.3
696 0 my ($tag, $attr) = @_;
697  
698 0 ▶ return '' unless UNIVERSAL::isa($attr, 'HASH') and %$attr;
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($attr, "HASH") and %$attr 0 0 0
Condition0%0/3
UNIVERSAL::isa($attr, "HASH") and %$attr
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($attr, "HASH") and %$attr
UNIVERSAL::isa($attr, "HASH") %$attr
Truth table0%0/3
A and B
A= UNIVERSAL::isa($attr, "HASH")B= %$attr
ABresult
0X0
100
111
699  
700 0 my $rv = "<\U$tag\E\n";
701 0 ▶ $tag = 'search' if $tag =~ /^(?:pre|post)fetch$/;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^(?:pre|post)fetch$/ 0 0 0
702  
703 0 foreach my $key (sort keys %$attr) {
704 0 my $val = $attr->{$key};
705 0 ▶ ($key) = grep {lc($_) eq lc($key)} @{$Attr{$tag}} or next;
Branch: 1 branch
BranchTrueFalseTotal
($key) = grep({lc $_ eq lc $key;} @{$Attr{$tag};}) or next 0 0 0
706 0 $val =~ s/"/\\"/g;
707 0 ▶ $rv .= qq(\t$key="$val"\n) if length $val;
Branch: 1 branch
BranchTrueFalseTotal
if length $val 0 0 0
708 }
709  
710 0 return "$rv>\n";
711 }
712  
713 0 ▶sub asSherlockString {
Subroutine asSherlockString: not called CC=10 SCAR=47.0
Pod undocumented
714 0 my $self = shift;
715 0 my $string = '';
716  
717 0 foreach my $stage ( qw(prefetch postfetch search) ) {
718 $string .= _fmt($stage => $self->{$stage})
719 0 ▶ if length $self->{$stage}{action};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{$stage}{"action"} 0 0 0
720  
721 0 ▶ $stage =~ /^(.*?)(?:fetch)?(?:search)?$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$stage =~ /^(.*?)(?:fetch)?(?:search)?$/ or next 0 0 0
722  
723 0 foreach my $list (@{$self->{"$1inputList"}}) {
724 0 $string .= _fmt(input => $list);
725 }
726  
727 0 ▶ if ($stage eq 'search') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stage eq "search") 0 0 0
728 0 foreach my $list (@{$self->{interpretList}}) {
729 0 $string .= _fmt(interpret => $list);
730 }
731 }
732  
733 $string .= "</\U$stage\E>\n\n"
734 0 if $self->{$stage} and %{$self->{$stage}}
735 0 ▶ and length $self->{$stage}{action};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$stage} and %{$self->{$stage};} and length $self->{$stage}{"action"} 0 0 0
Condition0%0/6
$self->{$stage} and %{$self->{$stage};} and length $self->{$stage}{"action"}
!ll&&!rl&&r
000
$self->{$stage} and %{$self->{$stage};}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{$stage} and %{$self->{$stage};} and length $self->{$stage}{"action"}
$self->{$stage} %{$self->{$stage};} length $self->{$stage}{"action"}
Truth table0%0/4
A and B and C
A= $self->{$stage}B= %{$self->{$stage};}C= length $self->{$stage}{"action"}
ABCresult
0XX0
10X0
1100
1111
736 }
737  
738 0 return $string;
739 }
740  
741 0 ▶sub asRssString {
Subroutine asRssString: not called CC=1 SCAR=6.9
Pod undocumented
742 0 my $self = shift;
743  
744 0 require XML::RSS;
745 0 my $rss = XML::RSS->(version => '1.0');
746  
747 $rss->channel(
748 title => fixEm($self->{search}{name}),
749 link => fixEm($self->getChannelUrl),
750 description => fixEm($self->{search}{description})
751 0 );
752  
753 $rss->image(
754 title => fixEm($self->{search}{name}),
755 url => fixEm($self->getPictureUrl),
756 link => fixEm($self->{host})
757 0 );
758  
759 $rss->textinput(
760 0 title => fixEm($self->{search}{name}),
761 description => "Search this site",
762 name => fixEm($self->getQueryAttr),
763 link => fixEm($self->getChannelUrl)
764 );
765  
766 0 return $rss->as_string;
767 }
768  
769 # This is a cludge to fix xml problems
770 # - bah, thought XML::RSS would do this
771  
772 0 ▶sub fixEm {
Subroutine fixEm: not called CC=1 SCAR=6.9
Pod undocumented
773 0 my $text = shift;
774  
775 0 $text =~ s/&/&amp;/gs;
776 0 $text =~ s/</&lt;/gs;
777 0 $text =~ s/>/&gt;/gs;
778 0 return $text;
779 }
780  
781 0 ▶sub resultsAsRssString {
Subroutine resultsAsRssString: not called CC=1 SCAR=6.9
Pod undocumented
782 0 my $self = shift;
783 0 my $results = $self->getResults;
784 0 return $results->asRssString;
785 }
786  
787 0 ▶sub resultsAsString {
Subroutine resultsAsString: not called CC=1 SCAR=6.9
Pod undocumented
788 0 my $self = shift;
789 0 my $results = $self->getResults;
790 0 return $results->asString;
791 }
792  
793 0 ▶sub resultsAsHtmlString {
Subroutine resultsAsHtmlString: not called CC=1 SCAR=6.9
Pod undocumented
794 0 my $self = shift;
795 0 my $results = $self->getResults;
796 0 return $results->asHtmlString;
797 }
798  
799 1 1;
800  
801 =head1 SEE ALSO
802  
803 L<sherch>
804  
805 L<LWP>, L<XML::RSS>
806  
807 =head1 AUTHORS
808  
809 =over 4
810  
811 =item *
812  
813 Damian Steer E<lt>D.M.Steer@lse.ac.ukE<gt>
814  
815 =item *
816  
817 Kang-min Liu E<lt>gugod@gugod.org<gt>
818  
819 =item *
820  
821 Autrijus Tang E<lt>autrijus@autrijus.orgE<gt>
822  
823 =back
824  
825 =head1 COPYRIGHT
826  
827 Copyright 1999, 2000, 2001 by Damian Steer.
828  
829 Copyright 2002, 2003 by Kang-min Liu.
830  
831 Copyright 2002, 2003, 2004 by Autrijus Tang.
832  
833 This program is free software; you can redistribute it and/or modify it
834 under the same terms as Perl itself.
835  
836 See L<http://www.perl.com/perl/misc/Artistic.html>
837  
838 =cut
↑ Summary SherlockSearch/Results.pm »