diff options
Diffstat (limited to '_site/cgi-bin/find.cgi')
| -rw-r--r-- | _site/cgi-bin/find.cgi | 250 |
1 files changed, 184 insertions, 66 deletions
diff --git a/_site/cgi-bin/find.cgi b/_site/cgi-bin/find.cgi index fc1d4af..5f95e3a 100644 --- a/_site/cgi-bin/find.cgi +++ b/_site/cgi-bin/find.cgi @@ -1,86 +1,202 @@ #!/usr/bin/perl -use Encode qw(decode_utf8); +use strict; +use warnings; +use Storable qw(retrieve); +use Encode qw(decode_utf8 encode_utf8); +use URI::Escape qw(uri_unescape); use HTML::Escape qw(escape_html); +# --- Configuration --- +my $max_parallel = 50; # Max parallel search requests +my $lock_timeout = 30; # Seconds before dropping stale locks +my $max_results = 20; # Max search results to display +my $sa_file = 'sa.bin'; # Suffix Array index +my $cp_file = 'corpus.bin'; # Raw text corpus +my $map_file = 'file_map.dat'; # File metadata +my $lock_dir = '/tmp/search_locks'; # Semaphore directory + +# --- Concurrency Control --- +mkdir $lock_dir, 0777 unless -d $lock_dir; +my $active_count = 0; +my $now = time(); + +opendir(my $dh, $lock_dir); +while (my $file = readdir($dh)) { + next unless $file =~ /\.lock$/; + my $path = "$lock_dir/$file"; + my $mtime = (stat($path))[9] || 0; + ($now - $mtime > $lock_timeout) ? unlink($path) : $active_count++; +} +closedir($dh); + +# Template variables +my $year = (localtime)[5] + 1900; my $search_text = ''; -if ($ENV{QUERY_STRING} =~ /^q=([^&]*)/) { - $search_text = decode_utf8($1 // ""); - $search_text =~ s/\P{Print}//g; # toss any non-printable utf-8 characters +# Busy check +if ($active_count >= $max_parallel) { + print "Content-Type: text/html\n\n"; + render_html("<p>Server busy. Please try again in a few seconds.</p>", "", $year); + exit; +} + +# Create semaphore lock +my $lock_file = "$lock_dir/$$.lock"; +open(my $fh_lock, '>', $lock_file); + +# --- Query Decoding --- +if (($ENV{QUERY_STRING} || '') =~ /^q=([^&]*)/) { + my $raw_q = $1; + $raw_q =~ tr/+/ /; + $search_text = uri_unescape($raw_q); + $search_text = decode_utf8($search_text // ""); + $search_text =~ s/\P{Print}//g; $search_text = substr($search_text, 0, 64); $search_text =~ s/^\s+|\s+$//g; } -my @results; - -# Search only index.html files inside the first level of subdirectories -my $start_dir = '../log'; -my @files = glob("$start_dir/*/index.html"); - -foreach my $path (@files) { - # Skip if the path is a symlink or not a file - next if -l $path || ! -f $path; - - next unless open(my $fh, '<:utf8', $path); - my $html = do { local $/; <$fh> }; - close($fh); - - my ($text) = $html =~ m|<main>(.*?)</main>|is; - $text =~ s|<[^>]+>| |g; - $text =~ s|\s+| |g; - - next unless $text =~ /(.{0,40})(\Q$search_text\E)(.{0,40})/is; - my ($before, $actual, $after) = ($1, $2, $3); - - # Trim if we cut into the middle of a sentence - $after =~ s/\s\S*$// if length($after) > 25; - $before =~ s/^.*?\s// if length($before) > 25; - - if ($before =~ /\S/) { # If before has non-whitespace characters - $before = ucfirst($before); - } else { - $before = ""; # Clear any stray spaces - $actual = ucfirst($actual); - } +my $safe_search_text = escape_html($search_text); - my $safe_before = escape_html($before); - my $safe_actual = escape_html($actual); - my $safe_after = escape_html($after); - my $snippet = "${safe_before}<b>${safe_actual}</b>${safe_after}..."; +print "Content-Type: text/html\n\n"; - my ($title) = $html =~ m|<title>(.*?)</title>|is; - my $safe_title = escape_html($title); +if ($search_text eq '') { + final_output("<p>Please enter a search term above.</p>"); +} - $path =~ s|^\.\./||; +# --- Binary Search Logic --- +my @results; +my $query = encode_utf8(lc($search_text)); +my $query_len = length($query); + +if (-f $sa_file && -f $cp_file) { + open(my $fh_sa, '<', $sa_file) or die $!; + open(my $fh_cp, '<', $cp_file) or die $!; + binmode($fh_sa); + binmode($fh_cp); + + my $file_map = retrieve($map_file); + my $total_suffixes = (-s $sa_file) / 4; + + # Find left boundary + my ($low, $high) = (0, $total_suffixes - 1); + my $first_hit = -1; + + while ($low <= $high) { + my $mid = int(($low + $high) / 2); + seek($fh_sa, $mid * 4, 0); + read($fh_sa, my $bin_off, 4); + my $off = unpack("L", $bin_off); + seek($fh_cp, $off, 0); + read($fh_cp, my $text, $query_len); + + my $cmp = $text cmp $query; + if ($cmp >= 0) { + $first_hit = $mid if $cmp == 0; + $high = $mid - 1; + } else { + $low = $mid + 1; + } + } - push @results, { - path => $path, - title => $safe_title, - snippet => $snippet - }; + # Collect results if found + if ($first_hit != -1) { + my $last_hit = $first_hit; + ($low, $high) = ($first_hit, $total_suffixes - 1); + + # Find right boundary + while ($low <= $high) { + my $mid = int(($low + $high) / 2); + seek($fh_sa, $mid * 4, 0); + read($fh_sa, my $bin_off, 4); + my $off = unpack("L", $bin_off); + seek($fh_cp, $off, 0); + read($fh_cp, my $text, $query_len); + + if (($text cmp $query) <= 0) { + $last_hit = $mid if $text eq $query; + $low = $mid + 1; + } else { + $high = $mid - 1; + } + } + + my %seen; + for my $i ($first_hit .. $last_hit) { + seek($fh_sa, $i * 4, 0); + read($fh_sa, my $bin_off, 4); + my $offset = unpack("L", $bin_off); + + foreach my $m (@$file_map) { + if ($offset >= $m->{start} && $offset < $m->{end}) { + if (!$seen{$m->{path}}++) { + # 1. Capture slightly more than 50 chars for trimming + my $snip_start = ($offset - 30 < $m->{start}) ? $m->{start} : $offset - 30; + seek($fh_cp, $snip_start, 0); + read($fh_cp, my $raw_snip, 120); + + my $snippet = decode_utf8($raw_snip, Encode::FB_QUIET) // $raw_snip; + $snippet =~ s/\s+/ /g; # Normalize whitespace + + # 2. Trim Start: Partial word removal + if ($snip_start > $m->{start}) { + $snippet =~ s/^[^\s]*\s//; + } + + # 3. Trim End: Length limit and partial word removal + my $has_more = 0; + if (length($snippet) > 50) { + $snippet = substr($snippet, 0, 50); + $has_more = 1 if $snippet =~ s/\s+[^\s]*$//; + } + + # 4. Cleanup & Capitalize + $snippet = ucfirst($snippet); + my $clean_path = $m->{path}; + $clean_path =~ s|^\.\./_site/||; + + # 5. Build Final Snippet + my $display_snippet = escape_html($snippet) . ($has_more ? "..." : ""); + + push @results, { + path => $clean_path, + title => (split('/', $m->{path}))[-2], + snippet => $display_snippet + }; + } + last; + } + } + last if scalar @results >= $max_results; + } + } + close($fh_sa); + close($fh_cp); } -print "Content-Type: text/html\n\n"; - -my $list; -if ($search_text eq '') { - $list = "<p>Please enter a search term above.</p>"; -} elsif (@results == 0) { - $list = "<p>No results found for \"<b>$search_text</b>\".</p>"; +# --- Formatting & Output --- +my $list_html = ""; +if (@results == 0) { + $list_html = "<p>No results found for \"<b>$safe_search_text</b>\".</p>"; } else { - $list = "<ul>"; - foreach my $res (@results) { - my $url = $res->{path}; - $list .= "<li><a href=\"/$url\">$res->{title}</a><br><small>$res->{snippet}</small></li>"; - } - $list .= "</ul>"; + $list_html = "<ul>" . join('', map { + "<li><a href=\"/$_->{path}\">$_->{title}</a><br><small>$_->{snippet}</small></li>" + } @results) . "</ul>"; } -my $safe_search_text = escape_html($search_text); -my $year = (localtime)[5] + 1900; +final_output($list_html); + +# --- Helpers --- +sub final_output { + my ($content) = @_; + render_html($content, $safe_search_text, $year); + if ($fh_lock) { close($fh_lock); unlink($lock_file); } + exit; +} -print <<"HTML"; +sub render_html { + my ($content, $q_val, $yr) = @_; + print <<"HTML"; <!DOCTYPE html> <html lang="en-us"> <head> @@ -105,19 +221,21 @@ print <<"HTML"; <div class="container"> <h2>Search</h2> <form action="" method="GET"> - <input id="search-box" type="text" name="q" value="$safe_search_text"> + <input id="search-box" type="text" name="q" value="$q_val"> <input id="search-btn" type="submit" value="Search"> </form> - $list + $content </div> </main> <div class="footer"> <div class="container"> <div class="twelve columns right container-2"> - <p id="footer-text">© ASCIIMX - $year</p> + <p id="footer-text">© ASCIIMX - $yr</p> </div> </div> </div> </body> </html> HTML +} + |
