1 |
#!/opt/bin/perl |
2 |
|
3 |
# cfmap2idx - inverted index for deliantra maps |
4 |
# Copyright (C) 2005,2007,2008 Marc Lehmann <cfmaps@schmorp.de> |
5 |
# |
6 |
# CFMAPIDX is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, |
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
# GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with cfmaps; if not, write to the Free Software |
18 |
# Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 |
|
20 |
our $VERSION = '0.913'; |
21 |
|
22 |
use Deliantra; |
23 |
use Deliantra::Map; |
24 |
use DB_File; |
25 |
|
26 |
sub escape_html($) { |
27 |
local $_ = shift; |
28 |
s/([<>&])/sprintf "&#%d;", ord $1/ge; |
29 |
$_ |
30 |
} |
31 |
|
32 |
sub search { |
33 |
my (@kw) = @_; |
34 |
|
35 |
tie %idx, DB_File, ".index.dat", O_RDONLY, 0, $DB_HASH |
36 |
or die ".index.dat: $!"; |
37 |
|
38 |
my $cnt; |
39 |
my %res; |
40 |
my @found; |
41 |
|
42 |
for (map lc, @kw) { |
43 |
if (exists $idx{$_}) { |
44 |
$cnt++; |
45 |
$res{$_}++ for unpack "n*", $idx{$_}; |
46 |
push @found, $_; |
47 |
#warn "$_ found\n";#d# |
48 |
} else { |
49 |
#warn "$_ not found\n";#d# |
50 |
} |
51 |
} |
52 |
|
53 |
my @paths; |
54 |
|
55 |
while (my ($k, $v) = each %res) { |
56 |
next unless $v == $cnt; |
57 |
push @paths, $idx{"D" . pack "n", $k}; |
58 |
} |
59 |
|
60 |
binmode STDOUT, ":utf8"; |
61 |
|
62 |
print "<?xml version='1.0' encoding='utf-8'?>", |
63 |
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">', |
64 |
"<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>", |
65 |
"<head>", |
66 |
"<title>Deliantra Keyword Search</title>", |
67 |
"<link rel='stylesheet' type='text/css' media='all' href='/common.css'/>\n", |
68 |
"</head>", |
69 |
"<body>"; |
70 |
|
71 |
print "<h1>Search results for '", (join "' & '", map escape_html $_, @found), "'</h1>", |
72 |
"<p class='searchcount'>", (scalar @paths), " results found, up to 200 results shown.</p>", |
73 |
"<p class='searchresult'><ul>"; |
74 |
|
75 |
pop @paths while @paths > 200; |
76 |
|
77 |
for (sort @paths) { |
78 |
print "<li><a href='$_.xhtml'>$_</a></li>"; |
79 |
} |
80 |
|
81 |
print "</ul></p><p class='footer'>created by <a href='http://software.schmorp.de/pkg/cfmaps'>cfmapidx</a> version $VERSION</p>", |
82 |
"</body></html>"; |
83 |
} |
84 |
|
85 |
if ($ARGV[0] eq "-a") { |
86 |
shift; |
87 |
|
88 |
tie %idx, DB_File, ".index.dat~", O_RDWR|O_CREAT, 0644, $DB_HASH |
89 |
or die ".index.dat~: $!"; |
90 |
|
91 |
my %idx2; |
92 |
|
93 |
for my $path (@ARGV) { |
94 |
(my $base = $path) =~ s/\.map$//; |
95 |
my $docnum = pack "n", ++$idx{Vdocnum}; |
96 |
$idx{"D$docnum"} = $base; |
97 |
my $meta = Deliantra::Map->new_from_file ("$base.map"); |
98 |
|
99 |
for my $x (0 .. $meta->{width} - 1) { |
100 |
for my $y (0 .. $meta->{height} - 1) { |
101 |
for my $a (@{ $meta->{map}[$x][$y] }) { |
102 |
for my $v (values %$a) { |
103 |
$v = lc $v; |
104 |
$v =~ y/a-zA-Z0-9_\-.\// /c; |
105 |
for (split /\s+/, $v) { |
106 |
$idx2{$_} .= $docnum; |
107 |
|
108 |
if (/[_\-\.\/]/) { |
109 |
$idx2{$_} .= $docnum for (split /[_\-\.\/]/) |
110 |
} |
111 |
} |
112 |
} |
113 |
} |
114 |
} |
115 |
} |
116 |
} |
117 |
|
118 |
while (my ($k, $v) = each %idx2) { |
119 |
$idx{$k} = pack "n*", keys %{ { map +($_ => 1), unpack "n*", $idx{$k}.$v } }; |
120 |
} |
121 |
|
122 |
} elsif ($ARGV[0] eq "-s") { |
123 |
shift; |
124 |
|
125 |
search @ARGV; |
126 |
} else { |
127 |
# assume CGI |
128 |
chdir "/var/www/maps.deliantra.net" or exit 69; |
129 |
|
130 |
print <<EOF; |
131 |
Content-Type: application/xhtml+xml |
132 |
|
133 |
EOF |
134 |
$ENV{QUERY_STRING} =~ s/^k=//; |
135 |
search split /\s+|\++|(?:%20)+/, $ENV{QUERY_STRING}; |
136 |
} |
137 |
|