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