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]: w@suika.fam.cx |
16 |
@Date.RCS: |
17 |
$Date: 2004/03/30 04:04:44 $ |
18 |
@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 |
@Parameter: |
165 |
@@Name: fragment |
166 |
@@Type: |
167 |
URI:fragment |
168 |
@@Default: (none) |
169 |
@@Description: |
170 |
@@@@: Fragment identifier of destination resource |
171 |
@@@lang: en |
172 |
@Formatting: |
173 |
my $result = $o->{var}->{search__result}; |
174 |
return if $result->{min} == $result->{start} and |
175 |
$result->{max} == $result->{end}; |
176 |
my $number = $result->{number}; # $result->{end} - $result->{start} + 1; |
177 |
my @range; |
178 |
my $first = ($result->{start} - $result->{min}) % $number; |
179 |
if ($first) { |
180 |
push @range, {start => $result->{min}, end => $first - 1, |
181 |
number => $first, |
182 |
dir => 'prev', seq => 0}; |
183 |
} |
184 |
my $dir; |
185 |
use integer; |
186 |
for my $i (0..(($result->{max} - $result->{min} + 1 - $first) / $number - 1)) { |
187 |
my $start = $result->{min} + $first + $number * $i; |
188 |
push @range, {start => $start, |
189 |
end => $start + $number - 1, |
190 |
number => $number, |
191 |
dir => $dir || (($result->{start} == $start) ? |
192 |
($dir = 'next' and 'current') : 'prev'), |
193 |
seq => scalar @range}; |
194 |
} |
195 |
if ($range[-1]->{end} < $result->{max}) { |
196 |
push @range, {start => $range[-1]->{end} + 1, |
197 |
end => $result->{max}, |
198 |
dir => $dir || 'current', |
199 |
number => $number, |
200 |
seq => scalar @range}; |
201 |
} |
202 |
my %template; |
203 |
for (@range) { |
204 |
$template{$_->{dir}} ||= $WIKIRESOURCE->get_text |
205 |
(name => 'Search:Navigation:'.$_->{dir}, |
206 |
o => $o, wiki => $o->{wiki}); |
207 |
$WIKILINKING->to_wikipage_in_html ({ |
208 |
label => $template{$_->{dir}}, |
209 |
} => { |
210 |
base => $o->{wiki}->{var}->{page}, |
211 |
page_name_relative => $o->{wiki}->{var}->{page}, |
212 |
param => { |
213 |
$result->{param_prefix}.'__query' => $result->{query}, |
214 |
$result->{param_prefix}.'__range_start' => $_->{start}, |
215 |
$result->{param_prefix}.'__range_number' => $_->{number}, |
216 |
}, |
217 |
search__range => $_, |
218 |
page_anchor_name => $p->{fragment}, |
219 |
wiki_mode => $o->{wiki}->{var}->{mode}, |
220 |
}, { |
221 |
o => $o, |
222 |
parent => $p->{-parent}, |
223 |
}); |
224 |
} |
225 |
|
226 |
FormattingRule: |
227 |
@Category[list]: |
228 |
page-link |
229 |
@Name: search--result-navigation-seq |
230 |
@Formatting: |
231 |
$p->{-parent}->append_text ($o->{link}->{dest}->{search__range}->{seq} + 1); |
232 |
|
233 |
FormattingRule: |
234 |
@Category[list]: |
235 |
page-link |
236 |
@Name: search--result-query |
237 |
@Formatting: |
238 |
$p->{-parent}->append_text ($o->{link}->{dest}->{param}->{search__query}); |
239 |
|
240 |
FormattingRule: |
241 |
@Category[list]: |
242 |
page-link |
243 |
@Name: search--result-start |
244 |
@Formatting: |
245 |
$p->{-parent}->append_text ($o->{link}->{dest}->{search__range}->{start} + 1); |
246 |
|
247 |
FormattingRule: |
248 |
@Category[list]: |
249 |
page-link |
250 |
@Name: search--result-end |
251 |
@Formatting: |
252 |
$p->{-parent}->append_text ($o->{link}->{dest}->{search__range}->{end} + 1); |
253 |
|
254 |
ViewFragment: |
255 |
@Name: ws--post-content |
256 |
@Description: |
257 |
@@@: After content body -- Backward links |
258 |
@@lang:en |
259 |
@Order: 101 |
260 |
@Formatting: |
261 |
%section ( |
262 |
id => see-also, |
263 |
title => {%res(name=>SeeAlso);}p, heading, |
264 |
content => {%search-result ( |
265 |
post-list => { |
266 |
%search--result-navigation ( |
267 |
fragment => see-also, |
268 |
); |
269 |
}p, |
270 |
);}p, |
271 |
); |
272 |
|
273 |
Resource: |
274 |
@Link:toWikiPage:SourceLabelLong:SearchResult: |
275 |
{%m--link-weight;} |
276 |
%link-to-it( |
277 |
label=>{%page-title(relative);}p, |
278 |
description=>{%page-name(absolute);}p, |
279 |
); |
280 |
%span(class=>headline,content=>{%page-headline;}p); |
281 |
@Search:Navigation:prev: |
282 |
[%link-to-it ( |
283 |
label => {%search--result-navigation-seq;}p, |
284 |
description => {%search--result-query; [%search--result-start;-%search--result-end;]}p, |
285 |
);] |
286 |
@Search:Navigation:current: |
287 |
[%search--result-navigation-seq;] |
288 |
@Search:Navigation:next: |
289 |
[%link-to-it ( |
290 |
label => {%search--result-navigation-seq;}p, |
291 |
description => {%search--result-query; [%search--result-start;-%search--result-end;]}p, |
292 |
);] |
293 |
|