| 1 | package OurNet::Site; | ||||||||||||||||||||
| 2 | 1 | require 5.005; | |||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 1 | $OurNet::Site::VERSION = '1.52'; | |||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | OurNet::Site - Extract web pages via templates | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | use LWP::Simple; | ||||||||||||||||||||
| 15 | use OurNet::Site; | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | my ($query, $hits) = ('autrijus', 10); | ||||||||||||||||||||
| 18 | my $found; | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Create a bot | ||||||||||||||||||||
| 21 | $bot = OurNet::Site->new('google'); | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # Parse the result got from LWP::Simple | ||||||||||||||||||||
| 24 | $bot->callme($self, 0, get($bot->geturl($query, $hits)), \&callmeback); | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | print '*** ' . ($found ? $found : 'No') . ' match(es) found.'; | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # Callback routine | ||||||||||||||||||||
| 29 | sub callmeback { | ||||||||||||||||||||
| 30 | my ($self, $himself) = @_; | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | foreach my $entry (@{$himself->{response}}) { | ||||||||||||||||||||
| 33 | if ($entry->{url}) { | ||||||||||||||||||||
| 34 | print "*** [$entry->{title}]" . | ||||||||||||||||||||
| 35 | " ($entry->{score})" . | ||||||||||||||||||||
| 36 | " - [$entry->{id}]\n" . | ||||||||||||||||||||
| 37 | " URL: [$entry->{url}]\n" . | ||||||||||||||||||||
| 38 | " $entry->{preview}\n"; | ||||||||||||||||||||
| 39 | $found++; | ||||||||||||||||||||
| 40 | delete($entry->{url}); | ||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | This module emulates a typical search engine by reading a XML script | ||||||||||||||||||||
| 48 | defining its aspects, and parses results on-the-fly accordingly. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | Note that it also takes Inforia Quest .fmt scripts, available at | ||||||||||||||||||||
| 51 | http://www.inforian.com/. The author of course cannot support this | ||||||||||||||||||||
| 52 | usage. | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | As per v1.52, Site.pm also accepts Template Toolkit format templates | ||||||||||||||||||||
| 55 | with extention '.tt2' as site descriptors, provided that it contains | ||||||||||||||||||||
| 56 | at least one C<[% FOREACH entry %]> block, and C<[% SET url.start %]> | ||||||||||||||||||||
| 57 | accordingly. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | Note that tt2 support is *highly* experimental and should not be | ||||||||||||||||||||
| 60 | relied upon until a more stable release comes. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =head1 BUGS | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | Probably lots. Most notably the 'More' facilities is lacking. Also | ||||||||||||||||||||
| 65 | there is no template-generating abilities. This is a must, but I | ||||||||||||||||||||
| 66 | couldn't find enough motivation to do it. Maybe you could. | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Currently, tt2 does not (quite) support incremental parsing in | ||||||||||||||||||||
| 69 | conjunction with L<OurNet::Query>. | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | # --------------- | ||||||||||||||||||||
| 74 | # Variable Fields | ||||||||||||||||||||
| 75 | # --------------- | ||||||||||||||||||||
| 76 | 1 | ▶ | use vars qw/$Myself/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | ▶ | use fields qw/id charset proc expression template tempdata | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | name info url var response category score | ||||||||||||||||||||
| 80 | 1 | allow_partial allow_tags tmplobj/; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # ----------------- | ||||||||||||||||||||
| 83 | # Package Constants | ||||||||||||||||||||
| 84 | # ----------------- | ||||||||||||||||||||
| 85 | 1 | ▶ | use constant PATH_SITE => join('/', ('', split('::', __PACKAGE__), '')); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 86 | 1 | ▶ | use constant ERROR_SITE_NEEDED => __PACKAGE__ . ' needs a file'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 87 | 1 | ▶ | use constant ERROR_FILE_NEEDED => __PACKAGE__ . ' cannot find definition for '; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 88 | 1 | ▶ | use constant ERROR_FORMAT => __PACKAGE__ . ' cannot determine file format for'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 89 | 1 | ▶ | use constant CHARSET_MAP => {'JIS' => 'ja-jp.jis', 'EUC' => 'ja-jp.euc', | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 90 | 1 | 'BIG5' => 'zh-tw', 'GB' => 'zh-cn'}; | |||||||||||||||||||
| 91 | 1 | ▶ | use constant ENTITY_STRIP => '<.*?>|^[\015\012\s]+|[\015\012\s]+$|\t'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 92 | 1 | ▶ | use constant ENTITY_MAP => {'nbsp' => ' ', 'quot' => '"', 'amp' => '&', | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 93 | 1 | 'gt' => '>', 'lt' => '<', 'copy' => '(c)'}; | |||||||||||||||||||
| 94 | 1 | ▶ | use constant ENTITY_LIST => '&('.join('|', keys(%{ENTITY_MAP()})).');'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # --------------------- | ||||||||||||||||||||
| 97 | # Subroutine new($site) | ||||||||||||||||||||
| 98 | # --------------------- | ||||||||||||||||||||
| 99 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=14 SCAR=43.8
Pod
undocumented
| |||||||||||||||||||||
| 100 | 1 | my $class = shift; | |||||||||||||||||||
| 101 | my $self = ($] > 5.00562) ? fields::new($class) | ||||||||||||||||||||
| 102 | 1 | ▶ | : do { no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | bless [\%{"$class\::FIELDS"}], $class }; | |||||||||||||||||||
| 104 | 1 | ▶ | my $file = $_[0] or (warn(ERROR_SITE_NEEDED), return); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | ▶ | (%{$self} = %{$file}, return $self) if UNIVERSAL::isa($file, 'HASH'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 1 | ▶ | unless (-e $file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 0 | ▶ | if (-e "$_[0].xml") { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 110 | 0 | $file = "$_[0].xml"; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | elsif (-e "$_[0].fmt") { | ||||||||||||||||||||
| 113 | 0 | $file = "$_[0].fmt"; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | elsif (-e "$_[0].tt2") { | ||||||||||||||||||||
| 116 | 0 | $file = "$_[0].tt2"; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||
| 119 | 0 | foreach my $inc (@INC) { | |||||||||||||||||||
| 120 | 0 | ▶ | last if -e ($file = $inc . PATH_SITE . $_[0]); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | ▶ | last if -e ($file = $inc . PATH_SITE . "$_[0].xml"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 0 | ▶ | last if -e ($file = $inc . PATH_SITE . "$_[0].fmt"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 0 | ▶ | last if -e ($file = $inc . PATH_SITE . "$_[0].tt2"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 1 | ▶ | die(ERROR_FILE_NEEDED . $file) if !(-e $file); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 1 | $self->parse($file); | |||||||||||||||||||
| 131 | 0 | $self->{tempdata} = ''; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | return $self; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | # --------------------------------------- | ||||||||||||||||||||
| 137 | # Subroutine geturl($self, $query, $hits) | ||||||||||||||||||||
| 138 | # --------------------------------------- | ||||||||||||||||||||
| 139 | 0 | ▶ | sub geturl { | ||||||||||||||||||
|
Subroutine
geturl: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 140 | 0 | my $self = shift; | |||||||||||||||||||
| 141 | 0 | my $url = $self->{url}{start}; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 0 | $url =~ s|_QUERY_|$_[0]|g; | |||||||||||||||||||
| 144 | 0 | $url =~ s|_HITS_|$_[1]|g; | |||||||||||||||||||
| 145 | 0 | $url =~ s|\${ query }|$_[0]|g; | |||||||||||||||||||
| 146 | 0 | $url =~ s|\${ hits }|$_[1]|g; | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 0 | return $url; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | # ------------------------------ | ||||||||||||||||||||
| 152 | # Subroutine parse($self, $file) | ||||||||||||||||||||
| 153 | # ------------------------------ | ||||||||||||||||||||
| 154 | 1 | ▶ | sub parse { | ||||||||||||||||||
|
Subroutine
parse: called CC=27 SCAR=63.6
Pod
undocumented
| |||||||||||||||||||||
| 155 | 1 | my $self = shift; | |||||||||||||||||||
| 156 | 1 | open(local *SITEFILE, $_[0]); | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 1 | ▶ | if ($_[0] =~ m|\.tt2$|i) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 159 | 1 | local $/; | |||||||||||||||||||
| 160 | 1 | my $content = <SITEFILE>; | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 1 | require OurNet::Template; | |||||||||||||||||||
| 163 | 1 | $self->{tmplobj} = OurNet::Template->new(); | |||||||||||||||||||
| 164 | 1 | $self->{tmplobj}->extract($content, undef, $self); | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | elsif ($_[0] =~ m|\.xml$|i) { | ||||||||||||||||||||
| 167 | 0 | local $/; | |||||||||||||||||||
| 168 | 0 | my $content = <SITEFILE>; | |||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 0 | my $xml_cdata_re = '(<!\[CDATA\[)?\015?\012?(.*?)\015?\012?(]]>)?'; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 0 | ▶ | $self->{id} = $1 if $content =~ m|<site id="(.*?)">|i; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 0 | foreach my $tag ('charset', 'score', 'expression', 'template', 'proc') { | |||||||||||||||||||
| 175 | 0 | ▶ | $self->{$tag} = $2 if $content =~ m|<$tag>$xml_cdata_re</$tag>|is; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 0 | foreach my $tag ('url', 'var', 'name', 'info') { | |||||||||||||||||||
| 179 | 0 | ▶ | $self->{$tag}{lc($1)} = $3 while | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 180 | $content =~ s|<$tag \w+="(.*?)">$xml_cdata_re</$tag>||is; | ||||||||||||||||||||
| 181 | } | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 0 | ▶ | if ($content =~ m|<category>(.*?)</category>|i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | 0 | $self->{category} = [ split(',', $1) ]; | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | elsif ($_[0] =~ m|(?:.*[/\\])?(.*?)(?:\.fmt)?$|i) { | ||||||||||||||||||||
| 188 | 0 | $self->{id} = $1; | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 0 | chomp($self->{name}{'en-us'} = <SITEFILE>); | |||||||||||||||||||
| 191 | 0 | ▶ | if ($self->{name}{'en-us'} =~ s|\((.+)\)||) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | 0 | $self->{info}{'en-us'} = $1; | |||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 0 | chomp($self->{url}{start} = <SITEFILE>); | |||||||||||||||||||
| 196 | 0 | ▶ | if ($self->{url}{start} =~ m|_START_\d+_\d+_|) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 197 | 0 | $self->{url}{more} = $self->{url}{start}; | |||||||||||||||||||
| 198 | 0 | $self->{url}{start} =~ s|_START_\d+_(\d+)_|$1|; | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 0 | ▶ | while (chomp($_ = <SITEFILE>)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 202 | (m|^---|) ? do { | ||||||||||||||||||||
| 203 | 0 | ▶ | last; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 204 | } : | ||||||||||||||||||||
| 205 | (m|^\w+://|) ? do { | ||||||||||||||||||||
| 206 | 0 | ▶ | $self->{url}{backup} = $_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 207 | } : | ||||||||||||||||||||
| 208 | (m|^MORE\t(.+)|) ? do { | ||||||||||||||||||||
| 209 | 0 | ▶ | $self->{url}{more} = $1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 210 | } : | ||||||||||||||||||||
| 211 | (m|^PROC\t(.+)|) ? do { | ||||||||||||||||||||
| 212 | 0 | ▶ | $self->{proc} = $1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 213 | } : | ||||||||||||||||||||
| 214 | (m|^VAR\t(.+)|) ? do { | ||||||||||||||||||||
| 215 | 0 | $self->{var}{$1} = <SITEFILE> . $1 . <SITEFILE>; | |||||||||||||||||||
| 216 | 0 | ▶ | $self->{var}{$1} =~ s|[\t\015\012]||g; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | } : | ||||||||||||||||||||
| 218 | (m|^SCORE\t(.+)|) ? do { | ||||||||||||||||||||
| 219 | 0 | $self->{score} = $1; | |||||||||||||||||||
| 220 | 0 | $self->{score} =~ s|\bx\b|_SCORE_|ig; | |||||||||||||||||||
| 221 | 0 | ▶ | $self->{score} =~ s|\by\b|_RANK_|ig; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 222 | } : | ||||||||||||||||||||
| 223 | (m|^CHARSET\t(.+)|) ? do { | ||||||||||||||||||||
| 224 | 0 | ▶ | $self->{charset} = CHARSET_MAP->{uc($1)}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | } : | ||||||||||||||||||||
| 226 | (m|^CHT\t(.+)|) ? do { | ||||||||||||||||||||
| 227 | 0 | $self->{name}{'zh-tw'} = $1; | |||||||||||||||||||
| 228 | 0 | ▶ | $self->{info}{'zh-tw'} = $self->{info}{'en-us'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 229 | } : | ||||||||||||||||||||
| 230 | (m|^CHS\t(.+)|) ? do { | ||||||||||||||||||||
| 231 | 0 | $self->{name}{'zh-cn'} = $1; | |||||||||||||||||||
| 232 | 0 | ▶ | $self->{info}{'zh-cn'} = $self->{info}{'en-us'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 233 | } : | ||||||||||||||||||||
| 234 | (m|^EXPR\t(.+)|) ? do { | ||||||||||||||||||||
| 235 | 0 | ▶ | $self->{expression} = $1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 236 | } : | ||||||||||||||||||||
| 237 | 0 | ▶ | (m|^TYPE\t(.+)|) ? do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 238 | 0 | $self->{category} = $1; | |||||||||||||||||||
| 239 | } : undef; | ||||||||||||||||||||
| 240 | } | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | 0 | chomp($self->{url}{home} = <SITEFILE>); | |||||||||||||||||||
| 243 | 0 | chomp($self->{template} = <SITEFILE>); | |||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | 0 | ▶ | while (chomp($_ = <SITEFILE>)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 246 | 0 | ▶ | next unless m|^[A-Z_]*$|; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | 0 | ▶ | $self->{template} .= $_ ? "_${_}_" : '___'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 249 | 0 | chomp($self->{template} .= <SITEFILE>); | |||||||||||||||||||
| 250 | } | ||||||||||||||||||||
| 251 | } | ||||||||||||||||||||
| 252 | else { | ||||||||||||||||||||
| 253 | 0 | die(ERROR_FORMAT . $_[0]); | |||||||||||||||||||
| 254 | }; | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | 0 | close(SITEFILE); | |||||||||||||||||||
| 257 | } | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | # --------------------------------------- | ||||||||||||||||||||
| 260 | # Subroutine contemplate($self, $content) | ||||||||||||||||||||
| 261 | # --------------------------------------- | ||||||||||||||||||||
| 262 | 0 | ▶ | sub contemplate { | ||||||||||||||||||
|
Subroutine
contemplate: not called CC=18 SCAR=58.3
Pod
undocumented
| |||||||||||||||||||||
| 263 | 0 | my ($self, $content) = @_; | |||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | 0 | ▶ | if ($self->{tmplobj}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 266 | # tt2 support goes here | ||||||||||||||||||||
| 267 | # XXX macros, etc incomplete | ||||||||||||||||||||
| 268 | 0 | my $result = $self->{tmplobj}->extract(undef, $content); | |||||||||||||||||||
| 269 | 0 | push @{$self->{response}}, @{$result->{entry}}; | |||||||||||||||||||
| 270 | 0 | return $self; | |||||||||||||||||||
| 271 | } | ||||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | 0 | my $template = _quote($self->{template}); | |||||||||||||||||||
| 274 | 0 | my @vars = map {lc($_)} ($template =~ m|_(\w+?)_|g); # slurp! | |||||||||||||||||||
| 275 | 0 | my $length = length($content); | |||||||||||||||||||
| 276 | 0 | $template =~ s|\015?\012?_\w+?_\015?\012?|(.*?)|g; | |||||||||||||||||||
| 277 | |||||||||||||||||||||
| 278 | 0 | ▶ | while (my @vals = ($content =~ m|$template|is)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 279 | 0 | $content =~ s|$template||is; | |||||||||||||||||||
| 280 | 0 | ▶ | last if $length == length($content); # infinite loop | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 281 | 0 | $length = length($content); | |||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | 0 | my $rank = ($#{$self->{response}} + 2); # begins with 1 | |||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | 0 | push(@{$self->{response}}, {'rank' => $rank}); | |||||||||||||||||||
| 286 | 0 | my $entry = $self->{response}[$rank - 1]; | |||||||||||||||||||
| 287 | 0 | $entry->{id} = $self->{id}; | |||||||||||||||||||
| 288 | |||||||||||||||||||||
| 289 | 0 | foreach my $idx (0 .. $#vars) { | |||||||||||||||||||
| 290 | 0 | my ($var, $val) = ($vars[$idx], $vals[$idx]); | |||||||||||||||||||
| 291 | |||||||||||||||||||||
| 292 | # Null variable ___ | ||||||||||||||||||||
| 293 | 0 | ▶ | next if $var eq '_'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | # Expand HTML entities | ||||||||||||||||||||
| 296 | 0 | ▶ | if (!$self->{allow_tags}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 297 | 0 | $val =~ s|@{[ENTITY_STRIP]}||gs; | |||||||||||||||||||
| 298 | 0 | $val =~ s|@{[ENTITY_LIST]}|ENTITY_MAP->{$1}|ge; | |||||||||||||||||||
| 299 | } | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | 0 | ▶ | if ($var eq 'sizek') { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 302 | 0 | $entry->{size} = $val * 1024; | |||||||||||||||||||
| 303 | } | ||||||||||||||||||||
| 304 | elsif ($var eq 'score') { | ||||||||||||||||||||
| 305 | 0 | my $proc = $self->{score}; | |||||||||||||||||||
| 306 | |||||||||||||||||||||
| 307 | 0 | $proc =~ s|_RANK_|$rank|ig; | |||||||||||||||||||
| 308 | 0 | $proc =~ s|_SCORE_|$val|ig; | |||||||||||||||||||
| 309 | |||||||||||||||||||||
| 310 | 0 | ▶ | if ($proc =~ m|^\d*|) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 311 | 0 | $entry->{$var} = $proc; | |||||||||||||||||||
| 312 | } | ||||||||||||||||||||
| 313 | else { | ||||||||||||||||||||
| 314 | 0 | require Safe; | |||||||||||||||||||
| 315 | |||||||||||||||||||||
| 316 | 0 | my $compartment = Safe->new(); | |||||||||||||||||||
| 317 | 0 | $compartment->permit_only(qw/:base_core :base_mem/); | |||||||||||||||||||
| 318 | 0 | $compartment->share(qw/$rank $val $self/); | |||||||||||||||||||
| 319 | 0 | $entry->{$var} = $compartment->reval($proc); | |||||||||||||||||||
| 320 | } | ||||||||||||||||||||
| 321 | } | ||||||||||||||||||||
| 322 | elsif ($var eq 'url') { | ||||||||||||||||||||
| 323 | 0 | $entry->{$var} = $val; | |||||||||||||||||||
| 324 | |||||||||||||||||||||
| 325 | 0 | ▶ | if ($entry->{$var} !~ m|^\w+://|) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 326 | 0 | ▶ | if ($self->{url}{home}) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 327 | 0 | $entry->{$var} = $self->{url}{home} . $entry->{$var}; | |||||||||||||||||||
| 328 | } | ||||||||||||||||||||
| 329 | ▶ | elsif (!$self->{allow_partial} and | |||||||||||||||||||
|
Condition0%0/3
not $self->{"allow_partial"} and $self->{"url"}{"start"} =~ m[^(\w+://.*?)/]
MC/DC0%0/2
not $self->{"allow_partial"} and $self->{"url"}{"start"} =~ m[^(\w+://.*?)/]
not $self->{"allow_partial"}
$self->{"url"}{"start"} =~ m[^(\w+://.*?)/]
Truth table0%0/3
not(A) and B
A= not $self->{"allow_partial"}B= $self->{"url"}{"start"} =~ m[^(\w+://.*?)/]
| |||||||||||||||||||||
| 330 | $self->{url}{start} =~ m|^(\w+://.*?)/|) { | ||||||||||||||||||||
| 331 | 0 | $entry->{$var} = $1 . $entry->{$var}; | |||||||||||||||||||
| 332 | } | ||||||||||||||||||||
| 333 | } | ||||||||||||||||||||
| 334 | } | ||||||||||||||||||||
| 335 | else { | ||||||||||||||||||||
| 336 | 0 | $entry->{$var} = $val; | |||||||||||||||||||
| 337 | }; | ||||||||||||||||||||
| 338 | } | ||||||||||||||||||||
| 339 | |||||||||||||||||||||
| 340 | 0 | ▶ | if (!$entry->{score}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 341 | 0 | my $proc = $self->{score}; | |||||||||||||||||||
| 342 | 0 | $proc =~ s|_RANK_|\$rank|ig; | |||||||||||||||||||
| 343 | 0 | $entry->{score} = eval($proc); | |||||||||||||||||||
| 344 | } | ||||||||||||||||||||
| 345 | |||||||||||||||||||||
| 346 | 0 | ▶ | if (my $proc = $self->{proc}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 347 | 0 | require Safe; | |||||||||||||||||||
| 348 | 0 | ▶ | $Myself ||= $self; | ||||||||||||||||||
|
Condition0%0/3
$OurNet::Site::Myself ||= $self
MC/DC0%0/2
$OurNet::Site::Myself ||= $self
$OurNet::Site::Myself
$self
Truth table0%0/3
A ||= B
A= $OurNet::Site::MyselfB= $self
| |||||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | 0 | my $compartment = Safe->new(); | |||||||||||||||||||
| 351 | 0 | $compartment->share(qw/$Myself/); | |||||||||||||||||||
| 352 | 0 | $compartment->permit_only(qw/:base_core :base_mem pushre regcmaybe regcreset regcomp/); | |||||||||||||||||||
| 353 | |||||||||||||||||||||
| 354 | 0 | $proc =~ s|_(\w+)_|\$Myself->{response}[$rank - 1]{lc('$1')}|ig; | |||||||||||||||||||
| 355 | 0 | $compartment->reval($proc); | |||||||||||||||||||
| 356 | } | ||||||||||||||||||||
| 357 | } | ||||||||||||||||||||
| 358 | |||||||||||||||||||||
| 359 | 0 | undef $Myself; | |||||||||||||||||||
| 360 | 0 | return $self; | |||||||||||||||||||
| 361 | } | ||||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | # ---------------------------------------------------------- | ||||||||||||||||||||
| 364 | # Subroutine callme($self, $herself, $id, $data, \&callback) | ||||||||||||||||||||
| 365 | # ---------------------------------------------------------- | ||||||||||||||||||||
| 366 | 0 | ▶ | sub callme { | ||||||||||||||||||
|
Subroutine
callme: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 367 | 0 | my ($self, $herself, $id, $data, $callback) = @_; | |||||||||||||||||||
| 368 | 0 | my $template = _quote($self->{template}); | |||||||||||||||||||
| 369 | 0 | my $count = $#{$self->{response}}; | |||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | # Append old ones | ||||||||||||||||||||
| 372 | 0 | $self->{tempdata} = $data = $self->{tempdata} . $data; | |||||||||||||||||||
| 373 | |||||||||||||||||||||
| 374 | 0 | ▶ | unless ($self->{tmplobj}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 375 | # Deep magic here | ||||||||||||||||||||
| 376 | 0 | $template =~ s|\015?\012?_\w+?_\015?\012?|(.*?)|g; # Find variables | |||||||||||||||||||
| 377 | |||||||||||||||||||||
| 378 | 0 | $template = '^.*' . $template; | |||||||||||||||||||
| 379 | |||||||||||||||||||||
| 380 | 0 | $self->{tempdata} =~ s|$template||is; | |||||||||||||||||||
| 381 | } | ||||||||||||||||||||
| 382 | |||||||||||||||||||||
| 383 | 0 | ▶ | if (defined $callback) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 384 | 0 | return &$callback($herself, $self->contemplate($data)); | |||||||||||||||||||
| 385 | } | ||||||||||||||||||||
| 386 | else { | ||||||||||||||||||||
| 387 | 0 | return $self->contemplate($data); | |||||||||||||||||||
| 388 | } | ||||||||||||||||||||
| 389 | } | ||||||||||||||||||||
| 390 | |||||||||||||||||||||
| 391 | 0 | ▶ | sub _quote { | ||||||||||||||||||
|
Subroutine
_quote: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 392 | 0 | my $quoted; | |||||||||||||||||||
| 393 | |||||||||||||||||||||
| 394 | 0 | ▶ | foreach my $chunk (split(/({{.*?}})/, $_[0] || '')) { | ||||||||||||||||||
|
Condition0%0/2
$_[0] || ''
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
| |||||||||||||||||||||
| 395 | 0 | ▶ | if ($chunk =~ m|{{(.*?)}}|) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 396 | 0 | $quoted .= $1; | |||||||||||||||||||
| 397 | } | ||||||||||||||||||||
| 398 | else { | ||||||||||||||||||||
| 399 | 0 | $quoted .= quotemeta($chunk); | |||||||||||||||||||
| 400 | } | ||||||||||||||||||||
| 401 | } | ||||||||||||||||||||
| 402 | |||||||||||||||||||||
| 403 | 0 | return $quoted; | |||||||||||||||||||
| 404 | } | ||||||||||||||||||||
| 405 | |||||||||||||||||||||
| 406 | 1 | 1; | |||||||||||||||||||
| 407 | |||||||||||||||||||||
| 408 | =head1 SEE ALSO | ||||||||||||||||||||
| 409 | |||||||||||||||||||||
| 410 | L<OurNet::Query> | ||||||||||||||||||||
| 411 | |||||||||||||||||||||
| 412 | =head1 AUTHORS | ||||||||||||||||||||
| 413 | |||||||||||||||||||||
| 414 | Autrijus Tang E<lt>autrijus@autrijus.org> | ||||||||||||||||||||
| 415 | |||||||||||||||||||||
| 416 | =head1 COPYRIGHT | ||||||||||||||||||||
| 417 | |||||||||||||||||||||
| 418 | Copyright 2001 by Autrijus Tang E<lt>autrijus@autrijus.org>. | ||||||||||||||||||||
| 419 | |||||||||||||||||||||
| 420 | All rights reserved. You can redistribute and/or modify | ||||||||||||||||||||
| 421 | this module under the same terms as Perl itself. | ||||||||||||||||||||
| 422 | |||||||||||||||||||||
| 423 | =cut | ||||||||||||||||||||