| 1 |
wakaba |
1.1 |
#?SuikaWikiConfig/2.0 |
| 2 |
|
|
|
| 3 |
|
|
Plugin: |
| 4 |
|
|
@Name: Search |
| 5 |
|
|
@Description: |
| 6 |
|
|
@@@: |
| 7 |
|
|
Wiki internal full-text search |
| 8 |
|
|
@@lang:en |
| 9 |
|
|
@License: %%Perl%% |
| 10 |
|
|
@Author: |
| 11 |
|
|
@@Name: |
| 12 |
|
|
@@@@: Wakaba |
| 13 |
|
|
@@@lang:ja |
| 14 |
|
|
@@@script:Latn |
| 15 |
|
|
@@Mail[list]: [email protected] |
| 16 |
|
|
@Date.RCS: |
| 17 |
wakaba |
1.2 |
$Date: 2004/03/19 03:42:06 $ |
| 18 |
wakaba |
1.1 |
@RequiredPlugin[list]: |
| 19 |
|
|
WikiLinking |
| 20 |
|
|
@Use: |
| 21 |
|
|
use Message::Util::Error; |
| 22 |
|
|
my $WIKILINKING; |
| 23 |
|
|
my $WIKIRESOURCE; |
| 24 |
|
|
|
| 25 |
|
|
PluginConst: |
| 26 |
|
|
@NS_XHTML1: |
| 27 |
|
|
http://www.w3.org/1999/xhtml |
| 28 |
|
|
@WIKILINKING: |
| 29 |
|
|
{($WIKILINKING ||= SuikaWiki::Plugin->module_package ('WikiLinking'))} |
| 30 |
|
|
@WIKIRESOURCE: |
| 31 |
|
|
{($WIKIRESOURCE ||= SuikaWiki::Plugin->module_package ('WikiResource'))} |
| 32 |
|
|
|
| 33 |
|
|
FormattingRule: |
| 34 |
|
|
@Category[list]: |
| 35 |
|
|
form-input |
| 36 |
|
|
view |
| 37 |
|
|
view-resource |
| 38 |
|
|
@Name: search-result |
| 39 |
|
|
@Description: |
| 40 |
|
|
@@@: |
| 41 |
|
|
Full-text search in wiki with given query string. |
| 42 |
|
|
\ |
| 43 |
|
|
This rule seek all (*1) wiki page contents whenever cache is not |
| 44 |
|
|
available. This rule should not be used within wiki that has |
| 45 |
|
|
lots of wiki pages. (*1 Number of wiki pages that is to be target |
| 46 |
|
|
of search is limited to C<$max_target> defined in this rule for |
| 47 |
|
|
safety.) |
| 48 |
|
|
\ |
| 49 |
|
|
This rule does not support multiple wiki page content formats. |
| 50 |
|
|
It handles contents as a plain text whatever its actual media type is. |
| 51 |
|
|
\ |
| 52 |
|
|
This rule requires C<m__search_result> WikiDB property to be enabled |
| 53 |
|
|
for caching. (See C<suikawiki-config.ph>.) |
| 54 |
|
|
\ |
| 55 |
|
|
Namazu WikiPlugin module provides more clever search engine. |
| 56 |
|
|
@@lang: en |
| 57 |
|
|
@Parameter: |
| 58 |
|
|
@@Name: case-sensitive |
| 59 |
|
|
@@Type: boolean |
| 60 |
|
|
@@Default: "0" |
| 61 |
|
|
@@Description: |
| 62 |
|
|
@@@@: Case sensitivility of search. |
| 63 |
|
|
@@@lang: en |
| 64 |
|
|
@Parameter: |
| 65 |
|
|
@@Name: number |
| 66 |
|
|
@@Type: <<1*DIGIT>> |
| 67 |
|
|
@@Default: (auto) |
| 68 |
|
|
@@Description: |
| 69 |
|
|
@@@@: (Reserved) |
| 70 |
|
|
@@@lang: en |
| 71 |
|
|
@Parameter: |
| 72 |
|
|
@@Name: query |
| 73 |
|
|
@@Type: string |
| 74 |
|
|
@@Default: "" |
| 75 |
|
|
@@Description: |
| 76 |
|
|
@@@@: Query. |
| 77 |
|
|
@@@lang: en |
| 78 |
|
|
@Parameter: |
| 79 |
|
|
@@Name: start |
| 80 |
|
|
@@Type: <<1*DIGIT>> |
| 81 |
|
|
@@Default: "0" |
| 82 |
|
|
@@Descuription: |
| 83 |
|
|
@@@@: (Reserved) |
| 84 |
|
|
@@@lang: en |
| 85 |
|
|
@Formatting: |
| 86 |
|
|
__ATTRTEXT:%query__; |
| 87 |
|
|
$p->{query} ||= ($o->{wiki}->{input} ? |
| 88 |
|
|
$o->{wiki}->{input}->parameter ('search__query') : undef) |
| 89 |
|
|
|| $o->{wiki}->{var}->{page}->stringify (wiki => $o->{wiki}); |
| 90 |
|
|
__ATTRTEXT:%case_sensitive__; |
| 91 |
|
|
my $word = $p->{case_sensitive} ? $p->{query} : lc $p->{query}; |
| 92 |
|
|
my $max_target = 1023; |
| 93 |
|
|
my @r; |
| 94 |
|
|
my $sr = $o->{wiki}->{db}->get (m__search_result => [$word]); |
| 95 |
|
|
unless ($sr) { |
| 96 |
|
|
my $n = 0; |
| 97 |
|
|
for my $page ($o->{wiki}->{db}->keys ('content')) { |
| 98 |
|
|
last if $n++ == $max_target; |
| 99 |
|
|
my $content; |
| 100 |
|
|
try { |
| 101 |
|
|
$content = $o->{wiki}->{db}->get (content => $page); |
| 102 |
|
|
} catch SuikaWiki::DB::Util::Error with { |
| 103 |
|
|
my $err = shift; |
| 104 |
|
|
$err->throw if $err->{-type} eq 'ERROR_REPORTED'; |
| 105 |
|
|
$content = undef; |
| 106 |
|
|
}; |
| 107 |
|
|
$content = $p->{case_sensitive} ? $content : lc $content; |
| 108 |
|
|
$content =~ s/^[^\x0A\x0D]+[\x0D\x0A]+//s; |
| 109 |
|
|
$page = $o->{wiki}->name ($page); |
| 110 |
|
|
my $n_page = $page->stringify (wiki => $o->{wiki}); |
| 111 |
|
|
$n_page = lc $n_page unless $p->{case_sensible}; |
| 112 |
|
|
if (index ($n_page, $word) > -1) { |
| 113 |
|
|
my $c = $content =~ s/\Q$word\E//g; |
| 114 |
|
|
push @r, [$page, $c+20]; |
| 115 |
|
|
} elsif (index ($word, $n_page) > -1) { |
| 116 |
|
|
my $c = $content =~ s/\Q$word\E//g; |
| 117 |
|
|
push @r, [$page, $c+10]; |
| 118 |
|
|
} elsif (my $c = $content =~ s/\Q$word\E//g) { |
| 119 |
|
|
push @r, [$page, $c]; |
| 120 |
|
|
} |
| 121 |
|
|
} |
| 122 |
|
|
@r = sort {$b->[1] <=> $a->[1] || $a->[0] cmp $b->[0]} @r; |
| 123 |
|
|
$sr = join "\x1E", |
| 124 |
|
|
map {$_->[0]->stringify (wiki => $o->{wiki}, delimiter => "\x1D") |
| 125 |
|
|
."\x1F".$_->[1]} @r; |
| 126 |
|
|
$o->{wiki}->{db}->set (m__search_result => [$word] => $sr); |
| 127 |
|
|
## TODO: Cache & case-sensitivility |
| 128 |
|
|
} else { |
| 129 |
|
|
@r = map {[$o->{wiki}->name ($_->[0], delimiter_reg => qr/\x1D/), $_->[1]]} |
| 130 |
|
|
map {[split /\x1F/, $_, 2]} split /\x1E/, $sr; |
| 131 |
|
|
} |
| 132 |
|
|
return unless @r; |
| 133 |
|
|
my $list = $p->{-parent}->append_new_node |
| 134 |
|
|
(type => '#element', |
| 135 |
|
|
namespace_uri => $NS_XHTML1, |
| 136 |
|
|
local_name => 'ol'); |
| 137 |
|
|
__ATTRTEXT:%template__; |
| 138 |
|
|
$p->{template} ||= $WIKIRESOURCE->get |
| 139 |
|
|
(name => 'Link:toWikiPage:SourceLabelLong:SearchResult', |
| 140 |
|
|
wiki => $o->{wiki}, o => $o); |
| 141 |
|
|
for (@r) { |
| 142 |
|
|
$WIKILINKING->to_wikipage_in_html ({ |
| 143 |
|
|
label => $p->{template}, |
| 144 |
|
|
} => { |
| 145 |
|
|
base => $o->{wiki}->{var}->{page}, |
| 146 |
|
|
page_name_relative => $_->[0], |
| 147 |
|
|
wiki_mode => $p->{mode}, |
| 148 |
|
|
}, { |
| 149 |
|
|
o => $o, |
| 150 |
|
|
parent => $list->append_new_node |
| 151 |
|
|
(type => '#element', |
| 152 |
|
|
namespace_uri => $NS_XHTML1, |
| 153 |
|
|
local_name => 'li'), |
| 154 |
|
|
-m__weight => $_->[1], |
| 155 |
|
|
}); |
| 156 |
|
|
} |
| 157 |
|
|
|
| 158 |
|
|
FormattingRule: |
| 159 |
|
|
@Category[list]: |
| 160 |
|
|
view |
| 161 |
|
|
view-resource |
| 162 |
|
|
form-input |
| 163 |
|
|
@Name: search--result-navigation |
| 164 |
|
|
@Formatting: |
| 165 |
|
|
my $result = $o->{var}->{search__result}; |
| 166 |
|
|
return if $result->{min} == $result->{start} and |
| 167 |
|
|
$result->{max} == $result->{end}; |
| 168 |
|
|
my $number = $result->{number}; # $result->{end} - $result->{start} + 1; |
| 169 |
|
|
my @range; |
| 170 |
|
|
my $first = ($result->{start} - $result->{min}) % $number; |
| 171 |
|
|
if ($first) { |
| 172 |
|
|
push @range, {start => $result->{min}, end => $first - 1, |
| 173 |
|
|
number => $first, |
| 174 |
|
|
dir => 'prev', seq => 0}; |
| 175 |
|
|
} |
| 176 |
|
|
my $dir; |
| 177 |
|
|
use integer; |
| 178 |
|
|
for my $i (0..(($result->{max} - $result->{min} + 1 - $first) / $number - 1)) { |
| 179 |
|
|
my $start = $result->{min} + $first + $number * $i; |
| 180 |
|
|
push @range, {start => $start, |
| 181 |
|
|
end => $start + $number - 1, |
| 182 |
|
|
number => $number, |
| 183 |
|
|
dir => $dir || (($result->{start} == $start) ? |
| 184 |
wakaba |
1.2 |
($dir = 'next' and 'current') : 'prev'), |
| 185 |
wakaba |
1.1 |
seq => scalar @range}; |
| 186 |
|
|
} |
| 187 |
|
|
if ($range[-1]->{end} < $result->{max}) { |
| 188 |
|
|
push @range, {start => $range[-1]->{end} + 1, |
| 189 |
|
|
end => $result->{max}, |
| 190 |
|
|
dir => $dir || 'current', |
| 191 |
|
|
number => $number, |
| 192 |
|
|
seq => scalar @range}; |
| 193 |
|
|
} |
| 194 |
|
|
my %template; |
| 195 |
|
|
for (@range) { |
| 196 |
|
|
$template{$_->{dir}} ||= $WIKIRESOURCE->get_text |
| 197 |
|
|
(name => 'Search:Navigation:'.$_->{dir}, |
| 198 |
|
|
o => $o, wiki => $o->{wiki}); |
| 199 |
|
|
$WIKILINKING->to_wikipage_in_html ({ |
| 200 |
|
|
label => $template{$_->{dir}}, |
| 201 |
|
|
} => { |
| 202 |
|
|
base => $o->{wiki}->{var}->{page}, |
| 203 |
|
|
page_name_relative => $o->{wiki}->{var}->{page}, |
| 204 |
|
|
param => { |
| 205 |
|
|
search__query => $result->{query}, |
| 206 |
|
|
search__range_start => $_->{start}, |
| 207 |
|
|
search__range_number => $_->{number}, |
| 208 |
|
|
}, |
| 209 |
|
|
search__range => $_, |
| 210 |
|
|
page_anchor_name => q<see-also>, |
| 211 |
|
|
}, { |
| 212 |
|
|
o => $o, |
| 213 |
|
|
parent => $p->{-parent}, |
| 214 |
|
|
}); |
| 215 |
|
|
} |
| 216 |
|
|
|
| 217 |
|
|
FormattingRule: |
| 218 |
|
|
@Category[list]: |
| 219 |
|
|
page-link |
| 220 |
|
|
@Name: search--result-navigation-seq |
| 221 |
|
|
@Formatting: |
| 222 |
|
|
$p->{-parent}->append_text ($o->{link}->{dest}->{search__range}->{seq} + 1); |
| 223 |
|
|
|
| 224 |
|
|
FormattingRule: |
| 225 |
|
|
@Category[list]: |
| 226 |
|
|
page-link |
| 227 |
|
|
@Name: search--result-query |
| 228 |
|
|
@Formatting: |
| 229 |
|
|
$p->{-parent}->append_text ($o->{link}->{dest}->{param}->{search__query}); |
| 230 |
|
|
|
| 231 |
|
|
FormattingRule: |
| 232 |
|
|
@Category[list]: |
| 233 |
|
|
page-link |
| 234 |
|
|
@Name: search--result-start |
| 235 |
|
|
@Formatting: |
| 236 |
|
|
$p->{-parent}->append_text ($o->{link}->{dest}->{search__range}->{start} + 1); |
| 237 |
|
|
|
| 238 |
|
|
FormattingRule: |
| 239 |
|
|
@Category[list]: |
| 240 |
|
|
page-link |
| 241 |
|
|
@Name: search--result-end |
| 242 |
|
|
@Formatting: |
| 243 |
|
|
$p->{-parent}->append_text ($o->{link}->{dest}->{search__range}->{end} + 1); |
| 244 |
|
|
|
| 245 |
|
|
ViewFragment: |
| 246 |
|
|
@Name: ws--post-content |
| 247 |
|
|
@Description: |
| 248 |
|
|
@@@: After content body -- Backward links |
| 249 |
|
|
@@lang:en |
| 250 |
|
|
@Order: 101 |
| 251 |
|
|
@Formatting: |
| 252 |
|
|
%section ( |
| 253 |
|
|
id => see-also, |
| 254 |
|
|
title => {%res(name=>SeeAlso);}p, heading, |
| 255 |
|
|
content => {%search-result ( |
| 256 |
|
|
post-list => { |
| 257 |
|
|
%search--result-navigation; |
| 258 |
|
|
}p, |
| 259 |
|
|
);}p, |
| 260 |
|
|
); |
| 261 |
|
|
|
| 262 |
|
|
Resource: |
| 263 |
|
|
@Link:toWikiPage:SourceLabelLong:SearchResult: |
| 264 |
|
|
{%m--link-weight;} |
| 265 |
|
|
%link-to-it( |
| 266 |
|
|
label=>{%page-title(relative);}p, |
| 267 |
|
|
description=>{%page-name(absolute);}p, |
| 268 |
|
|
); |
| 269 |
|
|
%span(class=>headline,content=>{%page-headline;}p); |
| 270 |
|
|
@Search:Navigation:prev: |
| 271 |
|
|
[%link-to-it ( |
| 272 |
|
|
label => {%search--result-navigation-seq;}p, |
| 273 |
|
|
description => {%search--result-query; [%search--result-start;-%search--result-end;]}p, |
| 274 |
|
|
);] |
| 275 |
|
|
@Search:Navigation:current: |
| 276 |
|
|
[%search--result-navigation-seq;] |
| 277 |
|
|
@Search:Navigation:next: |
| 278 |
|
|
[%link-to-it ( |
| 279 |
|
|
label => {%search--result-navigation-seq;}p, |
| 280 |
|
|
description => {%search--result-query; [%search--result-start;-%search--result-end;]}p, |
| 281 |
|
|
);] |
| 282 |
|
|
|