#!/usr/bin/perl #find.cgi #検索。しかし重そうではある。 require './jcodeLE.pl'; require './prei.cgi'; &winit;&jikan; &init;&decode; if($searchmode eq 'box'){ &search_box($keyword,$searchtype); exit; } &html; exit; ################################################## sub init{ $title = 'アンケート検索'; $retlink = "検索メニューへ"; #エラーから戻る時のリンク } ################################################## sub html{ #検索メニュー画面 print "Content-type: text/html\n\n"; print <<"_HTML_"; $title $metacode $bodys

$title


箱サーチ

_HTML_ &print_form; print "
\n"; print $original;#この行だけは消さないでっ!! }#html END ################################################## sub print_form{ #検索フォーム print <<"_HTML_";

検索キーワード

ANDOR

箱・項に含まれるキーワードをスペースで区切って入力して下さい。

_HTML_ }#html END ################################################## sub search_box{ my($title,$word,$type,@words,$room,@rooms,@result,$btitle,$line,$found); my($rcount,$bcount,$hcount); ($word,$type)=@_; $title="\"$word\"に対する検索結果"; print "Content-type: text/html\n\n"; print <<"_HTML_"; $title $metacode $bodys _HTML_ print "

$title


\n"; @words = split(/\s/,$word); @rooms=&get_rooms(); $rcount=@rooms; $bcount=$hcount=0; foreach $room(@rooms){ ($btitle,$found,@result)=&box_is_match($room,$type,@words); if($found){ $room=~s/log\.(.*)\.cgi/$1/; $link = "./$cgi_log?room=$room"; print "$btitle"; print "($room) $found HIT
\n"; &link_vote($room,@result) if(@result); print "
\n"; $hcount+=$found; $bcount++; } } print "${rcount}個の箱を検索して、${bcount}個の箱に${hcount}HITしました。
\n"; print "

再検索

\n"; &print_form; print "
\n"; print $original;#この行だけは消さないでっ!! }#search_box END ################################################## #指定されたログファイルを読み、キーワードに一致する行があれば #箱タイトルと一致した行のコメント等を返す。 #無ければ戻り値は無し。 sub box_is_match{ my($file,$type,$word,@words,@lines,$line,@result,$key); my($title,$found); my($v1,$v2,$v3,$v4,$v5,$hst,$v7,$v8); ($file,$type,@words)=@_; open(FD, "$logdir/$file") || &err("ログファイル${file}が開けませんでした。"); @lines = ; close(FD); ($title)=split(/:#/,$lines[0]); #箱名 #OR検索用の正規表現を作る。ただしANDでも2回目のふるい分けで使う。 #正規表現は$key="(word1|word2...|wordn)"の形になる。 $key=''; foreach $word(@words){ if($key eq ''){ $key="(\Q$word\E"; }else{ $key="$key|\Q$word\E"; } } $key="$key)"; #一回目のふるい分け if($type eq 'and'){ foreach $word(@words){ #ANDでふるい分け @lines=grep(/\Q$word\E/,@lines); } } else{ #ORでふるい分け @lines=grep(/$key/,@lines); } #たまたま箱作・投票者とも6番目の値がホストなので共通の処理にできる。 #最初からこれをやると大変そうなので、この前にふるい分けしている。 $found=0; foreach $line(@lines){ ($v1,$v2,$v3,$v4,$v5,$hst,$v7,$v8)=split(/:#/,$line); $line="$v1:#$v2:#$v3:#$v4:#$v5"; #ホスト以降の値を捨てる if($line=~/$key/){ #なお一致すれば結果に加える $found++; if($v1 ne $title){ #最初の行(箱題など)は無視する @result=(@result,"${v3}:#${v4}"); #項題・票コメント } } } return ($title,$found,@result) if($found); }#box_is_match END ################################################## #項目へのリンクを貼る sub link_vote{ my($room,@lines,$line,$vote,$comment,$vt); ($room,@lines)=@_; print "
\n"; foreach $line(@lines){ ($vote,$comment)=split(/:#/,$line); $vt = $vote; $vt =~ s/([^0-9A-Za-z_ ])/'%'.unpack('H2',$1)/ge; #英数字でない所をエンコードする $vt =~ s/\s/+/g; $link = "$cgi_log?detail=$vt&room=$room"; print "
$vote
\n"; print "
$comment
\n"; } print "
\n"; }#link_vote END ################################################## sub decode{ #一般的なデコード&変数への代入 my($buffer); if($ENV{'REQUEST_METHOD'} eq "POST"){ read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }else{ $buffer = $ENV{'QUERY_STRING'}; } &decodearg($buffer); $searchmode = $FORM{'mode'}; $keyword = $FORM{'word'}; $searchtype = $FORM{'type'}; }#decode END