#!/usr/bin/perl # ============================================================================== # Gossamer Threads Remote Search for Links SQL # http://www.gossamer-threads.com/ # Copyright (c) 2002 Gossamer Threads Inc. All Rights Reserved. # ============================================================================== # # Server Requirements: # - You must have LWP module installed, and the XML_Results plugin installed on # the Links SQL installation. # # Installation Instructions: # - Edit the $url below to point to the server you wish to get your results from. # - Edit the search_results.htm template to format it the way you like. NOTE: This # is NOT parsed by GT::Template, and you can use only normal template tags, there # is no support for if's, functions, etc. # - Upload this file, and search_results.htm in ASCII mode, and place them # in a directory that can run cgi's. # - chmod 755 remote_search.cgi. # use strict; use vars qw/$TEMPLATE $URL/; use CGI; $TEMPLATE = "search_results.htm"; $URL = "http://gossamer-threads.com/perl/links-sql2/search.cgi?xml_feed=1"; main(); sub main { # ----------------------------------------------------------------------------- my $q = new CGI; my $query = $q->param('query'); $query =~ s/([^\w.-])/sprintf("%%%02X",ord($1))/ge; print $q->header; $q->param or return print "No search term entered."; eval { require LWP::Simple; import LWP::Simple; }; if ($@) { return print "You must have LWP::Simple installed in order to use this script."; } my $content = get("$URL&query=$query") or return print "Unable to load $URL. Reason: $!"; my $item_count = 0; my $link_results_loop; while ($content =~ m,(.*?),sg) { my $item = $1; my ($title,$link,$description) = $item =~ m, (.*?)\s* (.*?)\s* (.*?) ,sx; $item_count++; push @$link_results_loop => {title => $title, link => $link, description => $description}; } $item_count or return print "No links were found containing $query !"; open (TMP, $TEMPLATE) or return print "Unable to open template file: $TEMPLATE ($!)"; read (TMP, my $data, -s TMP); close TMP; print parse_template($data, $link_results_loop); } sub parse_template { #------------------------------------------------------------------------- # Simply parse the template and replace the link loop # my ($TEMPLATE,$loop) = @_; (ref $loop eq 'ARRAY') || return; my ($loop_content) = $TEMPLATE =~ /<%link_loop%>(.*?)<%end_loop%>/sm; my $result; foreach (@$loop) { my $tmp = $loop_content; $tmp =~ s/<%\s*(.*?)\s*%>/(exists $_->{$1})?$_->{$1}:'';/seg; $result .= $tmp; } $TEMPLATE =~ s/(<%link_loop%>.*?<%end_loop%>)/$result/sm; return $TEMPLATE; }