1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Adding a search engine to your Hypermail archives</title>
<meta http-equiv="Content-Type" content="text/html;CHARSET=iso-8859-1" />
<meta name="description" content="hypermail documentation: adding search engine" />
<meta name="keywords" content="search, hypermail" />
<meta name="Author" content="Bob Crispen" />
<style type="text/css">
body, html {
color: #000;
background: #fff;
padding: 0.2em 2em;
}
pre {
color: #000;
background: #eee;
border: 1px solid #888;
padding-top: 1em;
margin-top: 0em;
white-space: pre;
}
h1 {
text-align: center;
font-family: sans-serif;
font-weight: bold;
background: #deedf8;
}
h4 {
border-top: 1px solid #888;
border-left: 1px solid #888;
border-right: 1px solid #888;
text-align: center;
font-family: sans-serif;
font-weight: bold;
background: #deedf8;
padding-bottom: 0em;
margin-bottom: 0em;
}
</style>
</head>
<body>
<h1>Adding a search engine<br />to your Hypermail archives</h1>
<p>One of the feature requests we hear most often is to incorporate
a search engine in Hypermail. But because everyone has his or her
own favorite search engine, and since it's hard to imagine a search engine
that may not turn out to be unusable
for some archives, we haven't built a search engine into hypermail, and
we probably won't.</p>
<p>But hypermail's page
customizations make it easy to integrate your own search engine into your
hypermail archives.</p>
<p>For our example, we're going to put a form box on the top and bottom of
every index page, and we'll use the <a href="http://swish-e.org/">swish-e</a>
search engine. We'll show a typical <a href="http://www.php.net/">PHP</a>
script and a typical <a href="http://www.cpan.org/">Perl</a> script that can
function as a glue layer between the web and the search engine.
There ought to be enough information here to get you
started regardless of what search engine and scripting language you choose
for your site.</p>
<p>Let's begin by modifying the header and footer hypermail puts on each
index file.</p>
<p>(1) Create a file called indexheader.hyp containing the following:</p>
<h4>indexheader.hyp</h4>
<pre>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>%l: %s</title>
<meta http-equiv="Content-Type" content="text/html;CHARSET=iso-8859-1" />
<meta name="generator" content="%p %v, see %h" />
<meta name="Subject" content="%s" />
<meta name="Date" content="(nil)" />
<link rev="made" href="mailto:%m" />
<style type="text/css">
body {color: black; background: #ffffff}
h1.center {text-align: center}
div.center {text-align: center}
.quotelev1 {color : #990099}
.quotelev2 {color : #ff7700}
.quotelev3 {color : #007799}
.quotelev4 {color : #95c500}
.headers {background : #e0e0d0}
.links {background : #f8f8e0}
</style>
</head>
<body>
<h1 class="center">%l<br />%s</h1>
<!-- Your custom code goes below this line -->
<div class="center">
<form action="http://url/of/search.php" method="post"
enctype="application/x-www-form-urlencoded">
<div>
<input type="hidden" name="db" value="name.of.index" />
<p class="headers">
<input type="text" name="str" size="20" />
<input type="submit" value="Search" />
</p>
</div>
</form>
</div>
</pre>
<p>Notice the HTML comment about 2/3 of the way down. Everything
above that line is standard, and ought to be very similar to the default
header. The custom code (below the comment) adds the features you
want to add. In this case, it's a form for a search.</p>
<p>You will, of course, substitute your own URL for search.php for the form's ACTION
and the name of your own search index for "name.of.index". See the bottom of this page for
an example of creating a search index file.</p>
<p>If you like the general appearance of the web pages Hypermail creates and
don't want to spend a lot of time playing with the HTML, you might want to
look at the source of "index.html" in the directory for which you're installing
a search engine and just copy the appropriate lines (<i>e.g.</i>, the lines
above the first <hr />) to indexheader.hyp.</p>
<p>(2) Create a file called indexfooter.hyp containing the following:</p>
<h4>indexfooter.hyp</h4>
<pre>
<div class="center">
<form action="/url/of/search.php" method="post"
enctype="application/x-www-form-urlencoded">
<div>
<input type="hidden" name="db" value="name.of.index" />
<p class="headers">
<input type="text" name="str" size="20" />
<input type"submit" value="Search" />
</p>
</div>
</form>
</div>
<!-- Your custom code goes above this line -->
<hr />
<p><small><em>
This archive was generated by <a href="%h">%p %v</a> : %g
</em></small></p>
</body>
</html>
</pre>
<p>Since this is a footer file, the common code will be at the end, and
you'll put your custom code above the comment.</p>
<p>You may have noted what seem to be superfluous <div> elements in
the HTML in the header and footer file.
The <a href="http://validator.w3.org/">W3C validator</a> likes them, and
they do no harm.</p>
<p>(3) Modify the .hmrc file to point to your custom header and footer:</p>
<h4>.hmrc (excerpt)</h4>
<pre>
# ihtmlheaderfile = [ path to index header template file | NONE ]
#
# Set this to the path to the Index header template file containing
# valid HTML statements and substitution cookies for runtime expansion.
# This will be included at the top of every index page.
ihtmlheaderfile = /path/to/indexheader.hyp
# ihtmlfooterfile = [ path to index footer template file | NONE ]
#
# Set this to the path to the Index footer template file containing
# valid HTML statements and substitution cookies for runtime expansion.
# This will be included at the bottom of every index page.
ihtmlfooterfile = /path/to/indexfooter.hyp
</pre>
<p>You'll call hypermail using this .hmrc file to create your archive
or add a message:</p>
<pre>
hypermail -c /path/to/.hmrc [...]
</pre>
<p>Here's a sample PHP script that performs the minimum functionality
required for "search.php":</p>
<h4>search.php</h4>
<pre>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Search results</title>
<meta http-equiv="Content-Type" content="text/html;CHARSET=iso-8859-1" />
</head>
<body>
<div>
<?
// Very simple search using swish-e (see http://swish-e.org/)
// copyright 2003 by Bob Crispen <http://www.crispen.org/>
// May be distributed without restriction for any purpose.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Set these values to the appropriate ones for your system
$db_dir = "/path/to/swish-e-databases/"; // Directory where
// you keep the databases and indexes that swish-e generates
$swishe = "/usr/local/bin/swish-e"; // swish-e executable
// Get arguments from call
// "str" -- String to search for
// "db" -- swish-e Database name (e.g., "site.index")
$str = $_POST["str"];
$db = $_POST["db"];
// This script is probably called from a form similar to the one
// below. If you call it without arguments, all it'll do is print
// the form.
if (($str != "") && ($db != "")) {
// Prevent shell execution exploit
$searchfor = escapeshellarg($str);
$database = escapeshellarg($db_dir . $db);
// Do the search
$result = `$swishe -H 0 -d '\t' -w $searchfor -f $database`;
// Turn the results into an array
$results = split("\n", $result);
// See how many results we have. Ignore the final blank line.
$count = count($results)-1;
if ($count > 0) {
print("Search results for <strong>$searchfor</strong>:\n");
print("<ul>\n");
for ($i=0; $i<$count; $i++) {
list($score, $url, $title, $len) = split("\t", $results[$i], 4);
print("<li> <a href=\"$url\">$title</a> [$score]</li>\n");
}
print("</ul>\n");
} else {
print("Sorry, <strong>$searchfor</strong> not found\n");
}
}
// Print a new form so they can continue searching
print <<<EOT
<form action="$PHP_SELF" method="post"
enctype="application/x-www-form-urlencoded">
<div>
<input type="hidden" name="db" value="$db" />
<input type="text" name="str" size="20" />
<input type="submit" value="Search" />
</div>
</form>
</div>
EOT;
?>
</body>
</html>
</pre>
<p>If you prefer Perl, or if your web server doesn't offer PHP, then you could
modify these lines in indexheader.hyp and indexfooter.hyp:</p>
<pre>
<form action="/url/of/search.php" method="post"
enctype="application/x-www-form-urlencoded">
</pre>
<p>to</p>
<pre>
<form action="/url/of/search.pl" method="post"
enctype="application/x-www-form-urlencoded">
</pre>
<p>Here's a sample script contributed by Perl maven Greg Bacon that
performs the minimum functionality required for "search.pl":</p>
<h4>search.pl</h4>
<pre>
#! /usr/local/bin/perl -T
# Perl implementation of Bob Crispen's PHP swish-e search available
# at <URL:http://www.crispen.org/doc/hypermail/archive_search.html>
# Copyright 2003 Greg Bacon.
# Very simple search using swish-e (see http://swish-e.org/)
# copyright 2003 by Bob Crispen <http://www.crispen.org/>
# May be distributed without restriction for any purpose.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
use warnings;
use strict;
my @warnings;
BEGIN {
$SIG{__WARN__} = sub { push @warnings, @_ };
print <<'EOHeader';
Content-type: text/html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Search results</title>
<meta http-equiv="Content-Type" content="text/html;CHARSET=iso-8859-1" />
</head>
<body>
<div>
EOHeader
$SIG{__DIE__} = sub {
print "<h1>Error!</h1>\n",
map { "<pre>$_</pre>\n" } @_;
};
}
use CGI qw/ :standard /;
use HTML::Entities;
# keep the taint checker happy
$ENV{PATH} = "/bin:/usr/bin:/usr/local/bin";
# ----------------------- Configuration ------------------------
# Directory where you keep the databases and
# indexes that swish-e generates
my $db_dir = "/path/to/index/";
# swish-e executable
my $swishe = "/path/to/bin/swish-e";
# -------------------- End of Configuration --------------------
sub drop_privs {
my @temp = ($>, $));
my $orig_uid = $<;
my $orig_gid = $(;
# set effective user and group id to real
$> = $<;
$) = $(;
# Drop privileges
$< = $orig_uid;
$( = $orig_gid;
# Make sure privs are really gone
($>, $)) = @temp;
die "FATAL: can't drop privileges" unless $< == $> && $( eq $);
}
sub search {
my $str = shift;
my $db = shift;
return unless $str && $db;
my $database = $db_dir . $db;
my $pid = open SWISHE, "-|";
unless (defined $pid) {
warn "failed fork: $!";
return;
}
my @results;
if ($pid) {
# parent
while (<SWISHE>) {
chomp;
# swish-e output is four TAB-separated fields, so
# we assume lines with three TABs are from swish-e
# and all other non-blank lines are warnings
if (/\t.*\t.*\t/) {
push @results, [split /\t/, $_, 4];
}
else {
push @warnings, $_ if /\S/;
}
}
close SWISHE
or warn $! ? "Error closing $swishe pipe: $!"
: "Exit status $? from $swishe";
}
else {
# child
local $SIG{__WARN__} = sub { print @_ };
local $SIG{__DIE__} = sub { print @_; exit 1 };
# 2>&1
open STDERR, ">&STDOUT" or warn "WARNING: dup STDOUT: $!";
drop_privs;
# Do the search
no warnings;
exec $swishe, '-H', 0, '-d', '\t', '-w', $str, '-f', $database
or die "FATAL: exec $swishe: $!";
exit 1;
}
my $searchfor = encode_entities $str;
if (@results) {
print "Search results for <strong>$searchfor</strong>:\n",
"<ul>\n";
for (@results) {
my($score,$url,$title,$len) = @$_;
print qq{ <li> <a href="$url">$title</a> [$score]</li>\n};
}
print "</ul>\n";
}
else {
print("Sorry, <strong>$searchfor</strong> not found\n");
}
}
## main
# URL of this page
my $me = url -full => 1;
# Get arguments from call
# "str" -- String to search for
# "db" -- swish-e Database name (e.g., "site.index")
my $str = param "str";
my $db = param "db";
# This script is probably called from a form similar to the one
# below. If you call it without arguments, all it'll do is print
# the form.
my @results = search $str, $db;
# Print a new form so they can continue searching
print startform,
"<div>\n",
hidden(db => $db),
textfield(-name => 'str', -size => 20),
submit('Search'),
"</div>\n",
end_form, "\n";
if (@warnings) {
my $messages = @warnings == 1 ? "message" : "messages";
print <<EOWarningsHead;
<p><hr>
<h1>Warnings</h1>
<em>Warning $messages:</em>
<ul>
EOWarningsHead
for (@warnings) {
print " <li><code>$_</code></li>\n";
}
print "</ul>\n";
}
print <<EOFooter;
</div>
</body>
</html>
EOFooter
</pre>
<p>Swish-e, like many other search engines, requires you to generate
a search index before you perform any searches. Here's a .conf file for
swish-e that might generate a reasonable
search index for a hypermail archive of messages about model railroading:</p>
<h4>model-rr.conf</h4>
<pre>
IndexDir /path/to/model-rr-archive
IndexFile /path/to/model-rr.index
IndexReport 3
IndexOnly .html
ReplaceRules replace "/path/to/document_root/" "http://www.yoursite.com/"
FileRules filename is attachment.html
FileRules filename is author.html
FileRules filename is date.html
FileRules filename is index.html
FileRules filename is subject.html
FileRules filename is thread.html
</pre>
<p>This tells swish-e to collect data from all the HTML files in your
model railroading archive <em>except</em> the index pages that hypermail
generates. That way, all the search results will point directly to the
messages themselves.</p>
<p>When you want to build your search index, you'll call swish-e something like this:</p>
<pre>
swish-e -v 3 -c /path/to/model-rr.conf > /path/to/model-rr.report 2>&1
</pre>
<p>Many people do this either in a cron job during off-peak hours or
through a CGI script that they call whenever they update their archive.</p>
<p>You might want to add search logging, page control (so you only
print, say, 20 results at a time), some nice CSS, and all sorts of other
things to your script. And if you'd prefer to write your script in
Python, sh, or Ada, you can do that too. From here on, it's up to you.</p>
<div>
--
<address>Bob Crispen</address>
<address>Thursday, June 26, 2003</address>
</div>
</body>
</html>
|