02f1ad1596db9ab73d2d86320260fa959f56c78a
[mir.git] / scripts / search.pl
1 #!/usr/bin/perl
2
3 use CGI qw(:standard);
4 use LWP::UserAgent;
5 use URI::Escape;
6
7 #this program takes, as CGI params, a query string "words"
8 #a syntax string "bool" (and|or), values of Y or N for the 
9 #variables of the form hasVideo, hasAudio, hasImage, etc.
10 #and a variable topic corresponding to the desired topic title
11 #it then munges these variables into an appropriate request to htdig,
12 # does this request and forwards the results to the user 
13
14 $searchhost='http://prod.indymedia.nl/cgi-bin/htsearch';
15
16 $bool=param('bool');
17 $hasVideo=param('hasVideo');
18 $hasAudio=param('hasAudio');
19 $hasImages=param('hasImages');
20 $topic=param('topic');
21 @topic_words=split(/\s+/,$topic);
22 @words=();
23
24 $querywords='';
25 @words=split (/\s+/,param('words')) if param('words');
26 push @words,@topic_words;
27 if (@words){
28
29 if ($bool eq "and"){
30 $querywords=join " AND ", @words;  
31 }
32 else{
33 $querywords=join " OR ", @words;  
34 }
35
36 $querywords =~ s/\(\)/ /g;
37 $querywords="($querywords)";
38 }
39
40 if ($hasVideo eq "Y" || $hasAudio eq "Y" || $hasImages eq "Y"){
41     $first=1;
42     if (@words){
43     $querywords.=" AND ";
44 }
45     $querywords .= "(";
46
47 if ($hasVideo eq "Y"){
48     if (!$first){
49         $querywords.=" OR";
50     }
51     $querywords.=" RealVideo OR Video"; 
52     $first=0;
53
54
55 if ($hasAudio eq "Y"){
56     if (!$first){
57         $querywords.=" OR";
58     }    
59     $querywords.=" RealAudio OR Audio"; 
60     $first=0;
61
62
63 if ($hasImages eq "Y"){
64     if (!$first){
65         $querywords.=" OR";
66     }
67     $querywords.=" ImagesGif OR ImagesJpeg";
68     $first=0;
69 }
70         $querywords.=" )";
71 }
72
73 $querywords=uri_escape($querywords,"^A-Za-z0-9");
74
75 $query="words=$querywords&format=builtin-long&sort=score&method=boolean";
76
77 # propagate the config parameter if it is set - rob
78 $config=param('config');
79 $query.="&config=$config" if $config;
80
81
82
83 $ua = new LWP::UserAgent;
84 $ua->agent("AgentName/0.1 " . $ua->agent);
85
86 # Create a request
87 my $req = new HTTP::Request POST => $searchhost;
88 $req->content_type('application/x-www-form-urlencoded');
89 $req->content($query);
90
91 # Pass request to the user agent and get a response back
92 my $res = $ua->request($req);
93
94 # Check the outcome of the response
95 if ($res->is_success) {
96     print header;
97     print $res->content;
98 } else {
99     print header;
100     print "Search engine temporarily unavailable\n";
101 }