Repository navigation
Expand file tree
/
Copy pathwiki.cgi
More file actions
167 lines (140 loc) · 4.7 KB
/
Copy pathwiki.cgi
File metadata and controls
167 lines (140 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl
################################################################################
#
# FSWiki Lite
#
################################################################################
require "./lib/common.pl";
#===============================================================================
# 処理の振り分け
#===============================================================================
&ReadParse();
if($in{"p"} eq ""){
$in{"p"} = "FrontPage";
}
if($in{"a"} eq "list"){
&list_page();
} elsif($in{"a"} eq "search"){
&search_page();
} else {
&show_page();
}
#-------------------------------------------------------------------------------
# ページの一覧
#-------------------------------------------------------------------------------
sub list_page {
my @pages = &Wiki::get_page_list();
&print_header("一覧");
print "<ul>\n";
foreach my $page (@pages){
print "<li><a href=\"".&Wiki::create_page_url($page->{NAME})."\">".
&Util::escapeHTML($page->{NAME})."</a>".
" - ".&Util::format_date($page->{TIME})."</li>\n";
}
print "</ul>\n";
&print_footer();
}
#-------------------------------------------------------------------------------
# ページを表示
#-------------------------------------------------------------------------------
sub show_page {
unless(&Wiki::page_exists($in{"p"})){
undef %in;
$in{"a"} = "edit";
require $EDIT_SCRIPT;
return;
}
my $source = &Wiki::get_page($in{"p"});
my $html = &Wiki::process_wiki($source,1);
&print_header($in{"p"},1);
if(&Wiki::page_exists("Header")){
print "<div class=\"header\">\n";
print &Wiki::process_wiki(&Wiki::get_page("Header"));
print "</div>\n";
}
print "<div class=\"day body section\">\n";
print $html;
print "</div>\n";
if(&Wiki::page_exists("Footer")){
print "<div class=\"comment\">\n";
print &Wiki::process_wiki(&Wiki::get_page("Footer"));
print "</div>\n";
}
&print_footer();
}
#-------------------------------------------------------------------------------
# ページの検索
#-------------------------------------------------------------------------------
sub search_page {
&print_header("検索");
print &Wiki::Plugin::search();
my $buf = "";
my $or_search = $in{'t'} eq 'or';
my $with_content = $in{'c'} eq 'true';
my $word = &Util::trim($in{'w'});
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);
if (@words) {
#---------------------------------------------------------------------------
# 検索実行
my @list = &Wiki::get_page_list();
my $res = '';
PAGE:
foreach my $page (@list){
my $name = $page->{NAME};
# ページ名も検索対象にする
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($page, $index)).
"</li>\n";
next PAGE;
}
} 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($page, $index)).
"</li>\n";
}
}
print "$buf<ul>\n$res</ul>\n" if ($res ne '');
}
&print_footer();
}
#-------------------------------------------------------------------------------
# 検索にマッチした行を取り出す関数
#-------------------------------------------------------------------------------
sub get_match_content {
my $content = shift;
my $index = shift;
# 検索にマッチした行の先頭文字の位置を求める。
# ・$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);
}