1############################################################
2#
3# �����ե������ɽ�����ޤ���
4# <pre>
5# {{search}}
6# </pre>
7# �����ɥС���ɽ���������v���ץ�����Ĥ��Ƥ���������
8# <pre>
9# {{search v}}
10# </pre>
11#
12############################################################
13package plugin::search::SearchHandler;
14use strict;
15#===========================================================
16# �����ȥ饯��
17#===========================================================
18sub new {
19	my $class = shift;
20	my $self = {};
21	return bless $self,$class;
22}
23
24#===========================================================
25# ���������μ¹�
26#===========================================================
27sub do_action {
28	my $self = shift;
29	my $wiki = shift;
30	my $cgi = $wiki->get_CGI;
31	my $word = Util::trim($cgi->param("word"));
32
33	my $buf = "";
34
35	$wiki->set_title("����");
36	$buf .= "<form method=\"GET\" action=\"".$wiki->config('script_name')."\">\n".
37	        "������� <input type=\"text\" name=\"word\" size=\"20\" value=\"".$cgi->escapeHTML($word)."\"> ";
38
39	$buf .= "<input type=\"radio\" name=\"t\" id=\"and\" value=\"and\"";
40	$buf .= " checked" if($cgi->param("t") ne "or");
41	$buf .= "><label for=\"and\">AND</label>\n";
42	$buf .= "<input type=\"radio\" name=\"t\" id=\"or\" value=\"or\"";
43	$buf .= " checked" if($cgi->param("t") eq "or");
44	$buf .= "><label for=\"or\">OR</label>\n";
45	$buf .= "<input type=\"checkbox\" id=\"contents\" name=\"c\" value=\"true\"";
46	$buf .= " checked" if($cgi->param("c") eq "true");
47	$buf .= "><label for=\"contents\">�ڡ������Ƥ�ޤ��</label>\n";
48
49	$buf .=  "<input type=\"submit\" value=\" �� �� \">".
50	         "<input type=\"hidden\" name=\"action\" value=\"SEARCH\">".
51	         "</form>\n";
52
53	#---------------------------------------------------------------------------
54	# �����¹�
55	if($word ne ""){
56		my @list = $wiki->get_page_list({-permit=>'show'});
57		my @words = split(/ +|��+/,$word);
58		my $name;
59		$buf = $buf."<ul>\n";
60		foreach $name (@list){
61			# �ڡ���̾�⸡���оݤˤ���
62			my $page  = $name;
63			if($cgi->param("c") eq "true"){
64				$page .= "\n".$wiki->get_page($name);
65			}
66			my $page2 = ($word =~ /[A-Za-z]/) ? Jcode->new($page)->tr('a-z','A-Z') : undef;
67
68			if($cgi->param("t") eq "or"){
69				# OR���� -------------------------------------------------------
70				foreach(@words){
71					if($_ eq ""){ next; }
72					my $index = (defined($page2)) ? index($page2, Jcode->new($_)->tr('a-z','A-Z')) : index($page,$_);
73					if($index!=-1){
74						$buf .= "<li><a href=\"".$wiki->config('script_name').
75						        "?page=".Util::url_encode($name)."\">".
76						        $cgi->escapeHTML($name)."</a> - ".
77						        Util::escapeHTML(&get_match_content($wiki,$name,$page,$index))."</li>\n";
78						last;
79					}
80				}
81			} else {
82				# AND���� ------------------------------------------------------
83				my $flag = 1;
84				my $index;
85				foreach(@words){
86					if($_ eq ""){ next; }
87					$index = (defined($page2)) ? index($page2, Jcode->new($_)->tr('a-z','A-Z')) : index($page,$_);
88					if($index==-1){
89						$flag = 0;
90						last;
91					}
92				}
93				if($flag == 1){
94					$buf .= "<li><a href=\"".$wiki->config('script_name').
95					        "?page=".Util::url_encode($name)."\">".
96					        $cgi->escapeHTML($name)."</a> - ".
97					        Util::escapeHTML(&get_match_content($wiki,$name,$page,$index))."</li>\n";
98				}
99			}
100		}
101		$buf = $buf."</ul>\n";
102	}
103	return $buf;
104}
105
106#===========================================================
107# �����˥ޥå������Ԥ���Ф��ؿ�
108#===========================================================
109sub get_match_content {
110	my $wiki    = shift;
111	my $name    = shift;
112	my $content = shift;
113	my $index   = shift;
114
115	unless($wiki->can_show($name)){
116		return "���ȸ��¤�����ޤ���";
117	}
118	my $pre  = substr($content,0,$index);
119	my $post = substr($content,$index,length($content)-$index);
120
121	my $pre_index  = rindex($pre,"\n");
122	if($pre_index==-1){ $pre_index = 0; }
123
124	my $post_index = index($post,"\n");
125	if($post_index==-1){ $post_index = length($post); }
126	$post_index += $index;
127
128	return substr($content,$pre_index,$post_index-$pre_index);
129}
130
1311;
132