OSDN Git Service

[BugTrack-plugin/396]searchプラグインの高速化。
[fswiki/fswiki.git] / plugin / search / SearchHandler.pm
1 ############################################################
2 #
3 # ¸¡º÷¤ò¼Â¹Ô¤·¤Æ·ë²Ì¤òɽ¼¨¤¹¤ë¥¢¥¯¥·¥ç¥ó¥×¥é¥°¥¤¥ó
4 # BugTrack-plugin/396
5 # 2009-01-09 ÈÇ
6 #
7 ############################################################
8 package plugin::search::SearchHandler;
9 use strict;
10 #===========================================================
11 # ¥³¥ó¥¹¥È¥é¥¯¥¿
12 #===========================================================
13 sub new {
14         my $class = shift;
15         my $self = {};
16         return bless $self,$class;
17 }
18
19 #===========================================================
20 # ¥¢¥¯¥·¥ç¥ó¤Î¼Â¹Ô
21 #===========================================================
22 sub do_action {
23         my $self = shift;
24         my $wiki = shift;
25         my $cgi = $wiki->get_CGI;
26         my $word = Util::trim($cgi->param("word"));
27
28         my $buf          = "";
29         my $or_search    = $cgi->param('t') eq 'or';
30         my $with_content = $cgi->param('c') eq 'true';
31
32         $wiki->set_title("¸¡º÷");
33         $buf .= "<form method=\"GET\" action=\"".$wiki->create_url()."\">\n".
34                 "¥­¡¼¥ï¡¼¥É <input type=\"text\" name=\"word\" size=\"20\" value=\"".&Util::escapeHTML($word)."\"> ";
35
36         $buf .= "<input type=\"radio\" name=\"t\" id=\"and\" value=\"and\"";
37         $buf .= " checked" if (not $or_search);
38         $buf .= "><label for=\"and\">AND</label>\n";
39         $buf .= "<input type=\"radio\" name=\"t\" id=\"or\" value=\"or\"";
40         $buf .= " checked" if ($or_search);
41         $buf .= "><label for=\"or\">OR</label>\n";
42         $buf .= "<input type=\"checkbox\" id=\"contents\" name=\"c\" value=\"true\"";
43         $buf .= " checked" if ($with_content);
44         $buf .= "><label for=\"contents\">¥Ú¡¼¥¸ÆâÍƤâ´Þ¤á¤ë</label>\n";
45
46         $buf .=  "<input type=\"submit\" value=\" ¸¡ º÷ \">".
47                  "<input type=\"hidden\" name=\"action\" value=\"SEARCH\">".
48                  "</form>\n";
49
50         my $ignore_case = 1;
51         my $conv_upper_case = ($ignore_case and $word =~ /[A-Za-z]/);
52         $word = uc $word if ($conv_upper_case);
53         my @words = grep { $_ ne '' } split(/ +|¡¡+/, $word);
54         return $buf unless (@words);
55         #---------------------------------------------------------------------------
56         # ¸¡º÷¼Â¹Ô
57         my @list = $wiki->get_page_list({-permit=>'show'});
58         my $res = '';
59         PAGE:
60         foreach my $name (@list){
61                 # ¥Ú¡¼¥¸Ì¾¤â¸¡º÷Âоݤˤ¹¤ë
62                 my $page = $name;
63                 $page .= "\n".$wiki->get_page($name) if ($with_content);
64                 my $pageref = ($conv_upper_case) ? \(my $page2 = uc $page) : \$page;
65                 my $index;
66
67                 if ($or_search) {
68                         # OR¸¡º÷ -------------------------------------------------------
69                         WORD:
70                         foreach(@words){
71                                 next WORD if (($index = index $$pageref, $_) == -1);
72                                 $res .= "<li>".
73                                             "<a href=\"".$wiki->create_page_url($name)."\">".Util::escapeHTML($name)."</a>".
74                                                 " - ".
75                                                 Util::escapeHTML(&get_match_content($wiki, $page, $index)).
76                                                 "</li>\n";
77                                 next PAGE;
78                         }
79                 } else {
80                         # AND¸¡º÷ ------------------------------------------------------
81                         WORD:
82                         foreach(@words){
83                                 next PAGE if (($index = index $$pageref, $_) == -1);
84                         }
85                         $res .= "<li>".
86                                         "<a href=\"".$wiki->create_page_url($name)."\">".Util::escapeHTML($name)."</a>".
87                                         " - ".
88                                         Util::escapeHTML(&get_match_content($wiki, $page, $index)).
89                                         "</li>\n";
90                 }
91         }
92         return "$buf<ul>\n$res</ul>\n" if ($res ne '');
93         return $buf;
94 }
95
96 #===========================================================
97 # ¸¡º÷¤Ë¥Þ¥Ã¥Á¤·¤¿¹Ô¤ò¼è¤ê½Ð¤¹´Ø¿ô
98 #===========================================================
99 sub get_match_content {
100         my $wiki    = shift;
101         my $content = shift;
102         my $index   = shift;
103
104         # ¸¡º÷¤Ë¥Þ¥Ã¥Á¤·¤¿¹Ô¤ÎÀèƬʸ»ú¤Î°ÌÃÖ¤òµá¤á¤ë¡£
105         # ¡¦$content ¤Î $index ÈÖÌܤÎʸ»ú¤«¤éÀèƬÊý¸þ¤Ë²þ¹Ôʸ»ú¤òõ¤¹¡£
106         # ¡¦$index ¤Î°ÌÃÖ¤ò´Þ¤à¹Ô¤ÎÀèƬʸ»ú¤Î°ÌÃ֤ϲþ¹Ôʸ»ú¤Î¼¡¤Ê¤Î¤Ç +1 ¤¹¤ë¡£
107         # ¡¦ÀèƬÊý¸þ¤Ë²þ¹Ôʸ»ú¤¬Ìµ¤«¤Ã¤¿¤éºÇ½é¤Î¹Ô¤Ê¤Î¤Ç¡¢·ë²Ì¤Ï 0(ÀèƬ)¡£
108         #   (¸«¤Ä¤«¤é¤Ê¤¤¤È rindex() = -1 ¤Ë¤Ê¤ë¤Î¤Ç¡¢+1 ¤·¤Æ¤Á¤ç¤¦¤É 0)
109         my $pre_index = rindex($content, "\n", $index) + 1;
110
111         # ¸¡º÷¤Ë¥Þ¥Ã¥Á¤·¤¿¹Ô¤ÎËöÈøʸ»ú¤Î°ÌÃÖ¤òµá¤á¤ë¡£
112         # ¡¦$content ¤Î $index ÈÖÌܤÎʸ»ú¤«¤éËöÈøÊý¸þ¤Ë²þ¹Ôʸ»ú¤òõ¤¹¡£
113         my $post_index = index($content, "\n", $index);
114
115         # ËöÈøÊý¸þ¤Ë²þ¹Ôʸ»ú¤¬¤Ê¤«¤Ã¤¿¤éºÇ½ª¹Ô¤Ê¤Î¤Ç $pre_index °Ê¹ßÁ´¤Æ¤òÊֵѡ£
116         return substr($content, $pre_index) if ($post_index == -1);
117
118         # ¸«¤Ä¤«¤Ã¤¿²þ¹Ôʸ»ú¤Ë¶´¤Þ¤ì¤¿Ê¸»úÎó¤òÊֵѡ£
119         return substr($content, $pre_index, $post_index - $pre_index);
120 }
121
122 1;