--- db/prgsrc/db.cgi 2001/11/26 10:31:49 1.37 +++ db/prgsrc/db.cgi 2001/12/03 13:57:20 1.40 @@ -1,4 +1,4 @@ -#!/usr/bin/perl -w +#!/perl/bin/perl -w use DBI; use CGI ':all'; @@ -7,12 +7,17 @@ use strict; use Time::Local; use POSIX qw(locale_h); use locale; -#open STDERR, ">errors"; +#open STDERR, ">errors1"; my $printqueries=0; my %forbidden=(); my $debug=0; #added by R7 my %fieldname= (0,'Question', 1, 'Answer', 2, 'Comments', 3, 'Authors', 4, 'Sources'); my %searchin; +my $rl=qr/[ÊÃÕËÅÎÇÛÝÚÈßÆÙ×ÁÐÒÏÌÄÖÜÑÞÓÍÉÔØÂÀ£]/; +my $RL=qr/[êãõëåîçûýúèÿüöäìïòðá÷ùæñþóíéôøâà³]/; +my $RLrl=qr/(?:(?:${rl})|(?:${RL}))+/; +my $l=qr/(?:(?:${RLrl})|(?:[\w\-]))+/; +my $Ll=qr/(?:[A-Z])|(?:${RL})/; @@ -21,7 +26,7 @@ my $thislocale; $searchin{'question'}=param('Question'); $searchin{'answer'}=param('Answer'); -$searchin{'comment'}=param('Comment'); +$searchin{'comments'}=param('Comments'); $searchin{'authors'}=param('Authors'); $searchin{'sources'}=param('Sources'); my $all=param('all'); @@ -122,6 +127,62 @@ sub GetTours { return @Tours; } +sub count +{ + my ($dbh,$word)=@_; +print "timeb=".time.br if $debug; + $word=$dbh->quote(uc $word); + my $query="SELECT number from nests,nf where $word=w1 AND w2=nf.id"; + my $sth=$dbh->prepare($query); + $sth->execute; + my @a=$sth->fetchrow; +print "timee0=".time.br if $debug; + $a[0]||0; +} + + +sub proxy +{ +#print "time0=".time.br if $debug; + my ($dbh,$ptext,$allnf)=@_; + my $text=$$ptext; + $text=~tr/£³/Åå/; + $text=~s/(${RLrl})p(${RLrl})/$1p$2/gom; + $text=~s/p(${RLrl})/Ò$1/gom; + $text=~s/(${RLrl})p/$1Ò/gom; + $text=~s/\s+/ /gmo; + $text=~s/[^ÊÃÕËÅÎÇÛÝÚÈßÆÙ×ÁÐÒÏÌÄÖÜÑÞÓÍÉÔØÂÀêãõëåîçûýúèÿæù÷áðòïìäöüñþóíéôøâàQWERTYUIOPASDFGHJKLZXCVBNM]/ /g; + $text=uc $text; + my @list= $text=~m/(?:(?:${RLrl})+)|(?:[A-Za-z0-9]+)/gom; + my (%c, %good,$sstr); + foreach (@list) + { + $c{$_}=count($dbh,$_)||10000; + } + my @words=sort {$c{$a}<=> $c{$b}} @list; + +# $good{$words[$_]}=1 foreach 0..4; + + foreach (@words) + { + $good{$_}=1 if $c{$_}<200; + } + +# foreach (@list) +# { +# if ($good{$_}) +# { +# $good{$_}=0; +# $sstr.=" $_"; +# } +# } + $sstr.=" $_" foreach grep {$good{$_}} @list; +print "time05=".time.br if $debug; + $$ptext=$sstr; + return russearch($dbh,$sstr,0,$allnf); +} + + sub russearch { my ($dbh, $sstr, $all,$allnf)=@_; my (@qw,@w,@tasks,$qw,@arr,$nf,$sth,@nf,$w,$where,$e,@where,%good,$i,%where,$from); @@ -161,7 +222,7 @@ print "$query",br if $printqueries; } - my @bad=grep {!$nf[$_]} 0..$#w; # @bad -- ÎÏÍÅÒÁ ÓÌÏ×ÏÆÏÒÍ, + my @bad=grep {!@{$nf[$_]}} 0..$#w; # @bad -- ÎÏÍÅÒÁ ÓÌÏ×ÏÆÏÒÍ, # ËÏÔÏÒÙÈ ÎÅÔ × ÓÌÏ×ÁÒÅ if (@bad) #ÅÓÔØ ÎÅÏÐÏÚÎÁÎÎÙÅ ÓÌÏ×ÏÆÏÒÍÙ @@ -192,9 +253,9 @@ print "$query",br if $printqueries; $_= " word2question.word=".$_. ' ' foreach @arr; $_= " nf.id=".$_. ' ' foreach @arr1; - +# @arr=(0) unless @arr; $query="select questions from word2question where". (join ' OR ', @arr); -print "$query\n",br if $printqueries; +print STDERR "!$query\n",br if $printqueries; $sth=$dbh -> prepare($query); $sth->execute; @@ -330,17 +391,35 @@ if $$words{$first}; # Returns list of QuestionId's, that have the search string in them. sub Search { - my ($dbh, $sstr,$metod,$all,$allnf) = @_; + my ($dbh, $s,$metod,$all,$allnf) = @_; + my $sstr=$$s; my (@arr, @Questions, @fields); - my (@sar, $i, $sth,$where); - -# push @fields, 'Question'; + my (@sar, $i, $sth,$where,$query); + my $ip=$ENV{'REMOTE_ADDR'}; + $ip=$dbh->quote($ip); + $query= + "INSERT into queries (query,metod,searchin,ip) + values (". $dbh->quote($sstr).', '. + $dbh->quote($metod) . ', ' . + $dbh->quote(join ' ', grep $searchin{$_}, keys %searchin) . + ", $ip)"; +print $query if $printqueries; + $dbh -> do ($query); if ($metod eq 'rus') { my @tasks=russearch($dbh,$sstr,$all,$allnf); return @tasks } + elsif ($metod eq 'proxy') + { +# $searchin{'question'}=1; +# $searchin{'answer'}=1; + my @task=proxy($dbh,$s,$allnf); +# $$s=$sstr; + return @task + } + ###Simple and advanced query processing. Added by R7 @@ -362,7 +441,7 @@ sub Search { -fields_searched => $fields); $where= $$q{'matchexp'}; - my $query= "SELECT Questionid FROM Questions + $query= "SELECT Questionid FROM Questions WHERE $where"; print br."Query is: $query".br if $debug; @@ -395,7 +474,6 @@ sub Search { print $query if $printqueries; $sth = $dbh->prepare($query) - } #else -- processing old-style query (R7) $sth->execute; @@ -403,6 +481,7 @@ print $query if $printqueries; push @Questions, $arr[0] unless $forbidden{$arr[0]}; } +print "@Questions" if $printqueries; return @Questions; } @@ -421,14 +500,55 @@ sub NoCase { } } +sub PrintList { + my ($dbh,$Questions,$shablon)=@_; + + my $first=param('first') ||1; + my $kvo=param('kvo') ||30; + + $first=$first-($first-1)%$kvo; + my $last=$first+$kvo-1; + $last=scalar @$Questions if scalar @$Questions <$last; + my($f,$l); + my $nav=''; + + for($f=1; $f<=@$Questions; $f+=$kvo) + { + $l=$f+$kvo-1; + $l=$#$Questions+1 if $l>$#$Questions+1; + if ($f==$first) {$nav.="[$f-$l] ";} + else { + my $qs=query_string; + $qs=~s/\;/\&/g; + $qs=~s/\&first\=[^\&]+//g; + $qs.="\&first=$f"; + + $nav.= "[".a({href=>(url."?".$qs)},"$f-$l")."] ";} + } + print "$nav".br."\n"; + for (my $i = $first; $i <= $last; $i++) { + my $output = &PrintQuestion($dbh, $$Questions[$i-1], 1, $i, 1); + if (param('metod') eq 'rus' || param('metod') eq 'proxy') + { + $output=~s/\b($shablon)\b/\$1\<\/strong\>/gi; + } else { + $output=~s/($shablon)/\$1\<\/strong\>/gi; + } + print $output; + } + print "$nav".br."\n"; + +} + sub PrintSearch { my ($dbh, $sstr, $metod) = @_; + my @allnf; - my (@Questions) = &Search($dbh, $sstr,$metod,$all,\@allnf); + my (@Questions) = &Search($dbh, \$sstr,$metod,$all,\@allnf); my ($output, $i, $suffix, $hits) = ('', 0, '', $#Questions + 1); my $shablon; - + $metod='rus' if $metod eq 'proxy'; if ($metod eq 'rus') { my $where='0'; @@ -469,18 +589,13 @@ print "$query" if $printqueries; $sstr =~ s/(.)/&NoCase($1)/ge; - my(@sar) = split(' ', $sstr); - for ($i = 0; $i <= $#Questions; $i++) { - $output = &PrintQuestion($dbh, $Questions[$i], 1, $i + 1, 1); - if (param('metod') eq 'rus') - { - $output=~s/\b($shablon)\b/\$1\<\/strong\>/gi; - } else { - foreach (@sar) { - $output =~ s/$_/$&<\/strong>/gs; - }} - print $output; + my @sar; + if ($metod ne 'rus') + { + (@sar) = split(' ', $sstr); + $shablon=join "|",@sar; } + PrintList($dbh,\@Questions,$shablon); } sub PrintRandom { @@ -714,7 +829,6 @@ sub PrintField { sub PrintQuestion { my ($dbh, $Id, $answer, $qnum, $title, $text) = @_; my ($output, $titles) = ('', ''); - my (%Question) = &GetQuestion($dbh, $Id); if (!$text) { $output .= hr({width=>"50%"}); @@ -752,7 +866,6 @@ sub PrintQuestion { while ((($AuthorId,$Name, $Surname,$Nicks)=$sth->fetchrow),$AuthorId) { my ($firstletter)=$Name=~m/^./g; -# $other.=a({href=>url."?qofauthor=$AuthorId"},"$Name $Surname").". "; $Name=~s/\./\\\./g; my $sha="(?:$Name\\s+$Surname)|(?:$Surname\\s+$Name)|(?:$firstletter\\.\\s*$Surname)|(?:$Surname\\s+$firstletter\\.)|(?:$Surname)|(?:$Name)"; if ($Nicks) @@ -784,6 +897,8 @@ sub PrintQuestion { $output .= &PrintField("ëÏÍÍÅÎÔÁÒÉÉ", $Question{'Comments'}, $text); } } + $output.=br.a({href=> url."?metod=proxy&qid=$Id"}, 'âÌÉÚËÉÅ ×ÏÐÒÏÓÙ').p + if $answer; return $output; } @@ -941,10 +1056,11 @@ sub PrintQOfAuthor . " : $hits ÐÏÐÁÄÁÎÉ$suffix."); - for ($i = 0; $i <= $#Questions; $i++) { - $output = &PrintQuestion($dbh, $Questions[$i], 1, $i + 1, 1); - print $output; - } +# for ($i = 0; $i <= $#Questions; $i++) { +# $output = &PrintQuestion($dbh, $Questions[$i], 1, $i + 1, 1); +# print $output; +# } + PrintList($dbh,\@Questions,'gdfgdfgdfgdfg'); } @@ -1084,7 +1200,22 @@ EOT } elsif (param('sstr')) { &PrintSearch($dbh, param('sstr'), param('metod')); - } elsif (param('all')) { + } + elsif (param('qid')) { + my $qid=param('qid'); + my $query="SELECT Question, Answer from Questions where QuestionId=$qid"; +print $query if $printqueries; + my $sth=$dbh->prepare($query); + $sth->execute; + my $sstr= join ' ',$sth->fetchrow; + $searchin{'question'}=1; + $searchin{'answer'}=1; +$sstr=~s/[^ÊÃÕËÅÎÇÛÝÚÈßÆÙ×ÁÐÒÏÌÄÖÜÑÞÓÍÉÔØÂÀêãõëåîçûýúèÿæù÷áðòïìäöüñþóíéôøâàa-zA-Z0-9]/ /gi; +# print &PrintQuestion($dbh,$qid, 1, '!'); + &PrintSearch($dbh, $sstr, 'proxy'); + } + + elsif (param('all')) { print &PrintAll($dbh, 0); } elsif (param('from_year') && param('to_year')) { print &PrintDates($dbh);