OSDN Git Service

[BugTrack-plugin/396]searchプラグインの高速化。
authortakezoe <takezoe@users.osdn.me>
Sun, 25 Jan 2009 15:33:21 +0000 (15:33 +0000)
committertakezoe <takezoe@users.osdn.me>
Sun, 25 Jan 2009 15:33:21 +0000 (15:33 +0000)
plugin/search/SearchHandler.pm

index 0b7ef1f..9445a62 100644 (file)
@@ -1,14 +1,9 @@
 ############################################################
-# 
-# ¸¡º÷¥Õ¥©¡¼¥à¤òɽ¼¨¤·¤Þ¤¹¡£
-# <pre>
-# {{search}}
-# </pre>
-# ¥µ¥¤¥É¥Ð¡¼¤Ëɽ¼¨¤¹¤ë¾ì¹ç¤Ïv¥ª¥×¥·¥ç¥ó¤ò¤Ä¤±¤Æ¤¯¤À¤µ¤¤¡£
-# <pre>
-# {{search v}}
-# </pre>
-# 
+#
+# ¸¡º÷¤ò¼Â¹Ô¤·¤Æ·ë²Ì¤òɽ¼¨¤¹¤ë¥¢¥¯¥·¥ç¥ó¥×¥é¥°¥¤¥ó
+# BugTrack-plugin/396
+# 2009-01-09 ÈÇ
+#
 ############################################################
 package plugin::search::SearchHandler;
 use strict;
@@ -29,79 +24,72 @@ sub do_action {
        my $wiki = shift;
        my $cgi = $wiki->get_CGI;
        my $word = Util::trim($cgi->param("word"));
-       
-       my $buf = "";
-       
+
+       my $buf          = "";
+       my $or_search    = $cgi->param('t') eq 'or';
+       my $with_content = $cgi->param('c') eq 'true';
+
        $wiki->set_title("¸¡º÷");
        $buf .= "<form method=\"GET\" action=\"".$wiki->create_url()."\">\n".
                "¥­¡¼¥ï¡¼¥É <input type=\"text\" name=\"word\" size=\"20\" value=\"".&Util::escapeHTML($word)."\"> ";
-       
+
        $buf .= "<input type=\"radio\" name=\"t\" id=\"and\" value=\"and\"";
-       $buf .= " checked" if($cgi->param("t") ne "or");
+       $buf .= " checked" if (not $or_search);
        $buf .= "><label for=\"and\">AND</label>\n";
        $buf .= "<input type=\"radio\" name=\"t\" id=\"or\" value=\"or\"";
-       $buf .= " checked" if($cgi->param("t") eq "or");
+       $buf .= " checked" if ($or_search);
        $buf .= "><label for=\"or\">OR</label>\n";
        $buf .= "<input type=\"checkbox\" id=\"contents\" name=\"c\" value=\"true\"";
-       $buf .= " checked" if($cgi->param("c") eq "true");
+       $buf .= " checked" if ($with_content);
        $buf .= "><label for=\"contents\">¥Ú¡¼¥¸ÆâÍƤâ´Þ¤á¤ë</label>\n";
-       
+
        $buf .=  "<input type=\"submit\" value=\" ¸¡ º÷ \">".
                 "<input type=\"hidden\" name=\"action\" value=\"SEARCH\">".
                 "</form>\n";
-       
+
+       my $ignore_case = 1;
+       my $conv_upper_case = ($ignore_case and $word =~ /[A-Za-z]/);
+       $word = uc $word if ($conv_upper_case);
+       my @words = grep { $_ ne '' } split(/ +|¡¡+/, $word);
+       return $buf unless (@words);
        #---------------------------------------------------------------------------
        # ¸¡º÷¼Â¹Ô
-       if($word ne ""){
-               my @list = $wiki->get_page_list({-permit=>'show'});
-               my @words = split(/ +|¡¡+/,$word);
-               my $name;
-               $buf = $buf."<ul>\n";
-               foreach $name (@list){
-                       # ¥Ú¡¼¥¸Ì¾¤â¸¡º÷Âоݤˤ¹¤ë
-                       my $page  = $name;
-                       if($cgi->param("c") eq "true"){
-                               $page .= "\n".$wiki->get_page($name);
+       my @list = $wiki->get_page_list({-permit=>'show'});
+       my $res = '';
+       PAGE:
+       foreach my $name (@list){
+               # ¥Ú¡¼¥¸Ì¾¤â¸¡º÷Âоݤˤ¹¤ë
+               my $page = $name;
+               $page .= "\n".$wiki->get_page($name) if ($with_content);
+               my $pageref = ($conv_upper_case) ? \(my $page2 = uc $page) : \$page;
+               my $index;
+
+               if ($or_search) {
+                       # OR¸¡º÷ -------------------------------------------------------
+                       WORD:
+                       foreach(@words){
+                               next WORD if (($index = index $$pageref, $_) == -1);
+                               $res .= "<li>".
+                                           "<a href=\"".$wiki->create_page_url($name)."\">".Util::escapeHTML($name)."</a>".
+                                               " - ".
+                                               Util::escapeHTML(&get_match_content($wiki, $page, $index)).
+                                               "</li>\n";
+                               next PAGE;
                        }
-                       my $page2 = ($word =~ /[A-Za-z]/) ? Jcode->new($page)->tr('a-z','A-Z') : undef;
-                       
-                       if($cgi->param("t") eq "or"){
-                               # OR¸¡º÷ -------------------------------------------------------
-                               foreach(@words){
-                                       if($_ eq ""){ next; }
-                                       my $index = (defined($page2)) ? index($page2, Jcode->new($_)->tr('a-z','A-Z')) : index($page,$_);
-                                       if($index!=-1){
-                                               $buf .= "<li>".
-                                                       "<a href=\"".$wiki->create_page_url($name)."\">".Util::escapeHTML($name)."</a>".
-                                                       " - ".
-                                                       Util::escapeHTML(&get_match_content($wiki,$name,$page,$index)).
-                                                       "</li>\n";
-                                               last;
-                                       }
-                               }
-                       } else {
-                               # AND¸¡º÷ ------------------------------------------------------
-                               my $flag = 1;
-                               my $index;
-                               foreach(@words){
-                                       if($_ eq ""){ next; }
-                                       $index = (defined($page2)) ? index($page2, Jcode->new($_)->tr('a-z','A-Z')) : index($page,$_);
-                                       if($index==-1){
-                                               $flag = 0;
-                                               last;
-                                       }
-                               }
-                               if($flag == 1){
-                                       $buf .= "<li>".
-                                               "<a href=\"".$wiki->create_page_url($name)."\">".Util::escapeHTML($name)."</a>".
-                                               " - ".
-                                               Util::escapeHTML(&get_match_content($wiki,$name,$page,$index)).
-                                               "</li>\n";
-                               }
+               } else {
+                       # AND¸¡º÷ ------------------------------------------------------
+                       WORD:
+                       foreach(@words){
+                               next PAGE if (($index = index $$pageref, $_) == -1);
                        }
+                       $res .= "<li>".
+                                       "<a href=\"".$wiki->create_page_url($name)."\">".Util::escapeHTML($name)."</a>".
+                                       " - ".
+                                       Util::escapeHTML(&get_match_content($wiki, $page, $index)).
+                                       "</li>\n";
                }
-               $buf = $buf."</ul>\n";
        }
+       return "$buf<ul>\n$res</ul>\n" if ($res ne '');
        return $buf;
 }
 
@@ -110,24 +98,25 @@ sub do_action {
 #===========================================================
 sub get_match_content {
        my $wiki    = shift;
-       my $name    = shift;
        my $content = shift;
        my $index   = shift;
-       
-       unless($wiki->can_show($name)){
-               return "»²¾È¸¢¸Â¤¬¤¢¤ê¤Þ¤»¤ó¡£";
-       }
-       my $pre  = substr($content,0,$index);
-       my $post = substr($content,$index,length($content)-$index);
-       
-       my $pre_index  = rindex($pre,"\n");
-       if($pre_index==-1){ $pre_index = 0; }
-       
-       my $post_index = index($post,"\n");
-       if($post_index==-1){ $post_index = length($post); }
-       $post_index += $index;
-       
-       return substr($content,$pre_index,$post_index-$pre_index);
+
+       # ¸¡º÷¤Ë¥Þ¥Ã¥Á¤·¤¿¹Ô¤ÎÀèƬʸ»ú¤Î°ÌÃÖ¤òµá¤á¤ë¡£
+       # ¡¦$content ¤Î $index ÈÖÌܤÎʸ»ú¤«¤éÀèƬÊý¸þ¤Ë²þ¹Ôʸ»ú¤òõ¤¹¡£
+       # ¡¦$index ¤Î°ÌÃÖ¤ò´Þ¤à¹Ô¤ÎÀèƬʸ»ú¤Î°ÌÃ֤ϲþ¹Ôʸ»ú¤Î¼¡¤Ê¤Î¤Ç +1 ¤¹¤ë¡£
+       # ¡¦ÀèƬÊý¸þ¤Ë²þ¹Ôʸ»ú¤¬Ìµ¤«¤Ã¤¿¤éºÇ½é¤Î¹Ô¤Ê¤Î¤Ç¡¢·ë²Ì¤Ï 0(ÀèƬ)¡£
+       #   (¸«¤Ä¤«¤é¤Ê¤¤¤È rindex() = -1 ¤Ë¤Ê¤ë¤Î¤Ç¡¢+1 ¤·¤Æ¤Á¤ç¤¦¤É 0)
+       my $pre_index = rindex($content, "\n", $index) + 1;
+
+       # ¸¡º÷¤Ë¥Þ¥Ã¥Á¤·¤¿¹Ô¤ÎËöÈøʸ»ú¤Î°ÌÃÖ¤òµá¤á¤ë¡£
+       # ¡¦$content ¤Î $index ÈÖÌܤÎʸ»ú¤«¤éËöÈøÊý¸þ¤Ë²þ¹Ôʸ»ú¤òõ¤¹¡£
+       my $post_index = index($content, "\n", $index);
+
+       # ËöÈøÊý¸þ¤Ë²þ¹Ôʸ»ú¤¬¤Ê¤«¤Ã¤¿¤éºÇ½ª¹Ô¤Ê¤Î¤Ç $pre_index °Ê¹ßÁ´¤Æ¤òÊֵѡ£
+       return substr($content, $pre_index) if ($post_index == -1);
+
+       # ¸«¤Ä¤«¤Ã¤¿²þ¹Ôʸ»ú¤Ë¶´¤Þ¤ì¤¿Ê¸»úÎó¤òÊֵѡ£
+       return substr($content, $pre_index, $post_index - $pre_index);
 }
 
 1;