File: clsearch.cgi

package info (click to toggle)
chalow 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 324 kB
  • ctags: 53
  • sloc: perl: 1,534; makefile: 52
file content (238 lines) | stat: -rwxr-xr-x 6,185 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/perl
# $Id: clsearch.cgi,v 1.1 2007-10-19 22:08:03+09 tatsuoyamashita Exp tatsuoyamashita $
# clsearch.cgi - chalow ˤ HTML 줿 ChangeLog 򸡺 CGI
use strict;

### User Setting from here
# ߤˤ碌ѤƲ

my $numnum = 10;		# ٤ɽǤ
my $css_file = "diary.css";

# for simple mode
my $simple_template = << "_TEMPLATE"
<div class="section">%%CNT%%. %%DATE%%  %%CONT%%</div>
_TEMPLATE
    ;

# for item mode
my $item_template = << "_TEMPLATE"
%%CONT%%
_TEMPLATE
    ;

# for list mode
my $list_template = << "_TEMPLATE"
%%DATE%%  %%CONT%%<br/>
_TEMPLATE
    ;

# simple mode ǥޥåʸϤॿ
my ($open_tag, $close_tag) = 
    (qq(<em style="background-color:yellow">), "</em>");

### to here

use CGI;
my $q = new CGI;

my $myself = $q->url();		# CGIURL
my $key = $q->param('key');
my $from = $q->param('from') || 0;
my $clen = $q->param('context_length') || 200;
my $mode = $q->param('mode');	# 0:simple mode, 1:item mode, 2:list mode

if (defined $q->param('date')) {
    $mode = 2;
    $key = "date:".$q->param('date');
}

if ($mode == 2) {		# ꥹȥ⡼ɤΤȤ쵤˽Ф
    $numnum = 100000000;
    $from = 0;
}

if (defined $q->param('cat')) {
    $mode = 1;
    $key = "cat:".$q->param('cat');
    $key =~ s/^(.+)$/"$1"/ if ($key =~ / /);
}


#  HTML head  
print "Content-type: text/html; charset=euc-jp\n\n";


#   
my $outstr = "";
my $cnt = 0;


sub clean {
    local ($_) = @_;
#    s/^"(.+)"$/$1/;
    s/(.)/'\x'.unpack("H2", $1)/gie;
    return $_;
}

if (defined $key and $key !~ /^\s*$/) {
    $key =~ s/\xa1\xa1/ /g;	# ad hoc
    $key =~ s/\s+$//;
    $key =~ s/^\s+//;

    my @keys = ($key =~ /(".+?"|\S+)/g);
#    @keys = map {s/^"(.+)"$/$1/; s/(.)/'\x'.unpack("H2", $1)/gie; $_;} @keys;
    @keys = map {s/^"(.+)"$/$1/; $_;} @keys;

    my $fn = "cl.itemlist";
    open(F, "< $fn") or die "Can't open $fn : $!\n";
    binmode(F);
    while (<F>) {
	my ($date, $c) = (/^(.+?)\t(.+)$/);
	my @regular_keys;

	my $match_num = 0;
	foreach my $k (@keys) {	# Τ̵̡Ȥľ٤
	    if ($k =~ /^date:(.+)$/) {
		my $tmp = clean($1);
		$match_num++ if ($date =~ /\[$tmp/);
	    } elsif ($k =~ /^cat:(.+)$/) {
		my $tmp = clean($1);
		$match_num++ if ($c =~ /^.+\[$tmp\].*\t.*$/);
 	    } else {
		my $tmp = clean($k);
		$match_num++ if ($c =~ m|$tmp|i);
		push @regular_keys, $tmp;
	    }
	}
	my $pkey = $regular_keys[0] if (@regular_keys > 0); # ɽ

#print @keys,"<br>\n";
	
	if ($match_num == @keys) {
#	if ($c =~ m|$pkey|i) {
	    $cnt++;
	    #next if ($cnt < $from + 1);last if ($cnt >= $from + 1 + $numnum);
	    next if ($cnt < $from + 1 or $cnt >= $from + 1 + $numnum);
	    # ®;

	    my $tmp_tmpl = $simple_template;
	    if ($mode == 0) { # ץ⡼
		if (defined $pkey and 
		    $c =~ m|^.*?(.{0,$clen})($pkey)(.{0,$clen}).*?$|i) {
		    my ($pre, $k, $pos) = ($1, $2, $3);
		    # 80-ff ä 1 ХȺ (ץ֥å up)
		    $pre =~ s!^[\x80-\xff](([\x80-\xff]{2})*[\x00-\x7f])!$1!;
		    $pre =~ s!^[\x80-\xff](([\x80-\xff]{2})*)$!$1!;
		    $pos =~ s!^(.*?[\x00-\x7f]([\x80-\xff]{2})*?)[\x80-\xff]$!$1!;
		    $pos =~ s!^(([\x80-\xff]{2})*?)[\x80-\xff]$!$1!;
		    $c = qq($pre$k$pos);
		    my $p = join('|', @regular_keys);
		    $c =~ s!($p)!$open_tag$1$close_tag!gi;
		}
	    } elsif ($mode == 1) { # ƥ⡼
		my ($file, $id) = ($date =~ /href="(.*?.html).*?">\[(.+?)\]/);
		$c = get_item($file, $id);
		$tmp_tmpl = $item_template;
	    } elsif ($mode == 2) { # ꥹȥ⡼
		$c =~ s/\t.*$//;
		$tmp_tmpl = $list_template;
	    }
	    
	    $tmp_tmpl =~ s/%%CNT%%/$cnt/g;
	    $tmp_tmpl =~ s/%%DATE%%/$date/g;
	    $tmp_tmpl =~ s/%%CONT%%/$c/g;
	    $outstr .= $tmp_tmpl;
	}
    }
    close(F);
}



#  ɽΤ 
my $page_max = int(($cnt - 1) / $numnum);
my ($qkey) = ($q->query_string =~ /(key=[^&]+)/);
($qkey) = ($q->query_string =~ /(cat=[^&]+)/) if ($qkey eq "");

my $bar = "";
my ($navip, $navin);
if ($page_max != 0) {		# 1ڡΤߤΤȤʤ
    for (my $i = 0; $i <= $page_max; $i++) {
	if ($from / $numnum == $i) {
	    $bar .= "<strong>".($i + 1).'</strong>';
	} else {
	    $bar .= $q->a({-href => "$myself?from=".($i * $numnum)."&".$qkey},
			  $i + 1);
	}
	$bar .= " ";

	if ($from / $numnum == $i - 1) {
	    $navin = $q->a({-href => "$myself?from=".($i * $numnum).
				"&".$qkey}, "[  ]");
	} elsif ($from / $numnum == $i + 1) {
	    $navip = $q->a({-href => "$myself?from=".($i * $numnum).
				"&".$qkey}, "[  ]");
	}

    }
}

if ($cnt == 0) {
    print "<p>ĤޤǤ</p>\n";
} else {
    print "<p>$cnt  Ĥޤ</p>\n";
}


my $page_template = << "__TEMPLE"

<html><head><title>CHALOW Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<link rel=stylesheet href="$css_file" media=all>
</head><body><a href="index.html">ChangeLog INDEX</a>

<form method="get" action="clsearch.cgi" 
    enctype="application/x-www-form-urlencoded">
<input type="text" name="key" value="@{[$q->escapeHTML($key)]}" />
<input type="checkbox" name="mode" value="1" @{[($mode)? "checked":""]}/>
<!--
<input type="radio" name="mode" value="0" @{[($mode==0)? "checked":""]}/>
<input type="radio" name="mode" value="1" @{[($mode==1)? "checked":""]}/>
<input type="radio" name="mode" value="2" @{[($mode==2)? "checked":""]}/>
-->
<input type="submit">
</form>

<p>$navip $bar $navin</p>
<div class="body">$outstr</div>
<p>$navip $bar $navin</p>
<a href="./">ChangeLog INDEX</a>
<div style="text-align:right">Powered by 
<a href="http://chalow.org/"><strong>chalow</strong></a></div>
</body></html>
__TEMPLE

    ;

#print $q->Dump();
print $page_template;

exit;


### ե뤫顢IDˤꤵ줿item
my %file_cache;
sub get_item {
    my ($file, $id) = @_;
    my $all;
    if (not defined $file_cache{$file}) {
	open(F2, $file) or die "can't open $file : $!";
	$file_cache{$file} = join('', <F2>);
	close F2;
    }
    my $start = "<!-- start:$id -->";
    my $end = "<!-- end:$id -->";
    my ($item) = ($file_cache{$file} =~ /($start.+$end)/sm);
    return $item;
}