/[pub]/suikawiki/script/wiki.cgi
Suika

Contents of /suikawiki/script/wiki.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.46 - (hide annotations) (download)
Thu Jan 2 12:14:15 2003 UTC (21 years, 10 months ago) by w
Branch: MAIN
Changes since 1.45: +25 -123 lines
do_index: Removed.

1 wakaba 1.9 #!/usr/bin/perl
2 wakaba 1.16 # wiki.cgi - This is YukiWiki, yet another Wiki clone.
3 wakaba 1.9 #
4 wakaba 1.16 # This program is free software; you can redistribute it and/or
5     # modify it under the same terms as Perl itself.
6    
7     use strict;
8 w 1.44 use lib qw(./lib);
9 wakaba 1.16 use CGI::Carp qw(fatalsToBrowser);
10 w 1.46 our $VERSION = do{my @r=(q$Revision: 1.26 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
11 w 1.43
12     require 'wikidata/suikawiki-config.ph';
13 wakaba 1.16 use Fcntl;
14     ##############################
15 wakaba 1.31 my %fmt; ## formatter objects
16 wakaba 1.22 my %embed_command = (
17     searched => '^\[\[#searched:([^\]]+)\]\]$',
18 w 1.43 form => qr/\[\[\#form(?:\(([A-Za-z0-9-]+)\))?:'((?:[^'\\]|\\.)*)':'((?:[^'\\]|\\.)*)'(?::'((?:[^'\\]|\\.)*)')?\]\]/,
19 wakaba 1.22 );
20 w 1.43 our ($modifier_dbtype,$url_cgi,%uri,%PathTo,$use_exists);
21 w 1.45 our (%PageName,$kanjicode,$lang,%FixedPage);
22 w 1.43
23 wakaba 1.16 ##############################
24     my $info_LastModified = 'LastModified';
25     my $info_IsFrozen = 'IsFrozen';
26     ##############################
27     my %form;
28 w 1.46 our %database;
29     our $database;
30 wakaba 1.16 my %infobase;
31     my %interwiki;
32     ##############################
33     my %page_command = (
34 w 1.43 $PageName{RssPage} => 'rss',
35 wakaba 1.16 );
36     my %command_do = (
37     read => \&do_read,
38 wakaba 1.25 TEXT_CSS => \&do_output_css,
39 wakaba 1.16 edit => \&do_edit,
40     adminedit => \&do_adminedit,
41     adminchangepassword => \&do_adminchangepassword,
42     write => \&do_write,
43     searchform => \&do_searchform,
44     comment => \&do_comment,
45 wakaba 1.28 RandomJump => \&do_random_jump,
46 wakaba 1.16 rss => \&do_rss,
47     diff => \&do_diff,
48 wakaba 1.41 wikiform => \&do_wikiform,
49     map => \&do_map,
50 wakaba 1.16 );
51 wakaba 1.33 my $UA = ''; ## User agent name
52 wakaba 1.41 $| = 1;
53 wakaba 1.16 ##############################
54 wakaba 1.9
55     sub main {
56 wakaba 1.32 $UA = $main::ENV{HTTP_USER_AGENT};
57 wakaba 1.16 &open_db;
58     &init_form;
59     if ($command_do{$form{mycmd}}) {
60     &{$command_do{$form{mycmd}}};
61 wakaba 1.9 } else {
62 wakaba 1.42 &{$command_do{read}};
63 wakaba 1.9 }
64 wakaba 1.16 &close_db;
65 wakaba 1.9 }
66    
67     sub do_read {
68 wakaba 1.26 my $content = $database{$form{mypage}};
69 wakaba 1.41 #print "content-type:text/plain;charset=euc-jp\n\n".gmtime."Get Lastmodified\n";
70 wakaba 1.27 my $lm = &get_info($form{mypage}, $info_LastModified);
71     wiki::referer::add ($form{mypage}, $ENV{HTTP_REFERER});
72 wakaba 1.32 wiki::useragent::add ($ENV{HTTP_USER_AGENT});
73 wakaba 1.41 #print gmtime."Search...\n";
74 wakaba 1.29 my ($r, $c) = get_search_result ($form{mypage});
75     my $rl = wiki::referer::list_html ($form{mypage});
76     my @toc;
77 wakaba 1.41 push @toc, qq(-<a href="#wikipage-see-also">@{[&Resource('SeeAlso',escape=>1)]}</a>) if $c;
78     push @toc, qq(-<a href="#wikipage-referer">@{[&Resource('Referers',escape=>1)]}</a>) if $rl;
79 wakaba 1.26 my $cf = 'SuikaWiki/0.9';
80 wakaba 1.27 ## Should be support at least:
81     ## - 'SuikaWiki/0.9' CRLF
82     ## - 'H2H/' ("0.9" / "1.0" / "1.1") CRLF
83     ## - "/*" WSP* 'W3C-CSS/' ("1.0" / "2.0") "*/" CRLF
84 wakaba 1.32 $cf = $1 if $content =~ s#^(?:/\*\s*|[\#<]\?)?([A-Z][A-Za-z0-9-]+/[0-9.]+(?:[^0-9.\x0D\x0A][^\x0D\x0A]*)?)[\x0D\x0A]+##s;
85 wakaba 1.28 if ($cf =~ m!^(?:\#\?)?SuikaWiki/0.9(?:$|\s)!) {
86 wakaba 1.41 #print gmtime."Header...\n";
87 wakaba 1.42 &print_header ($form{mypage}, -last_modified => $lm, -expires => time + 120,
88     -content_format => $cf, -noindex => ($cf =~ /obsoleted="yes"/ ? 1 : 0));
89 wakaba 1.41 #print "\n". gmtime."Body...\n";
90 wakaba 1.29 &print_content ($content, content_format => $cf, last_modified => $lm,
91     -toc => \@toc);
92 w 1.45 print &text_to_html (q([[#comment]])) if $cf !~ /obsoleted="yes"/ && !$FixedPage{$form{mypage}};
93 wakaba 1.26 } else {
94 wakaba 1.42 &print_header($form{mypage}, -expires => time + 120, -last_modified => $lm);
95 wakaba 1.26 print "<pre>@{[&escape($content)]}</pre>";
96     }
97 wakaba 1.22 if ($c) {
98 wakaba 1.41 print qq{<h2 @{[&id_and_name('wikipage-see-also')]}>@{[&Resource('SeeAlso',escape=>1)]}</h2>};
99 wakaba 1.22 print $r;
100     }
101 wakaba 1.29 if ($rl) {
102 wakaba 1.41 print qq(<div @{[&id_and_name('wikipage-referer')]}><h2>@{[&Resource('Referers',escape=>1)]}</h2>\n$rl</div>\n);
103 wakaba 1.27 }
104 wakaba 1.41 #print "\n". gmtime."Footer...\n";
105 wakaba 1.27 &print_footer($form{mypage}, $lm);
106 wakaba 1.41 #print "\n". gmtime."Fin...\n";
107 wakaba 1.9 }
108    
109 wakaba 1.25 sub do_output_css {
110 wakaba 1.37 wiki::referer::add ($form{mypage}, $ENV{HTTP_REFERER});
111     wiki::useragent::add ($ENV{HTTP_USER_AGENT});
112 wakaba 1.25 my $content = $database{$form{mypage}};
113     if ($content =~ m#^\s*/\*\s*W3C-CSS#) {
114 wakaba 1.41 my $lm = gmtime &get_info($form{mypage}, $info_LastModified);
115     print "Content-Type: text/css; charset=@{[&get_charset_name($kanjicode)]}\n";
116 wakaba 1.29 print "Last-Modified: $lm\n";
117 wakaba 1.42 print "Expires: @{[scalar gmtime time+3600]}\n"; ## TODO: don't use asctime
118 wakaba 1.25 print "\n";
119     print $content;
120     } else {
121     print "Status: 406 Unsupported Media Type\n";
122 wakaba 1.29 &print_header('WikiPageIsNotCSS', -noindex => 1);
123 wakaba 1.25 &print_content($database{WikiPageIsNotCSS});
124     &print_footer('WikiPageIsNotCSS');
125     }
126     }
127    
128 wakaba 1.32 sub id_and_name ($) {
129     my $name = shift;
130 wakaba 1.36 if ($UA =~ m#Mozilla/[12]\.|Microsoft Internet Explorer#) {
131 wakaba 1.32 qq{id="$name"><a name="$name"></a};
132     } else {
133     qq{id="$name"};
134     }
135     }
136    
137 wakaba 1.9 sub do_edit {
138 wakaba 1.16 my ($page) = &unarmor_name(&armor_name($form{mypage}));
139     if (not &is_editable($page)) {
140 w 1.45 &print_header($page, -noindex => 1, -expires => time+60);
141 wakaba 1.41 &print_message(&Resource('Error:ThisPageIsUneditable'));
142 wakaba 1.16 } elsif (&is_frozen($page)) {
143 w 1.45 &print_header($page, -noindex => 1, -expires => time+60);
144 wakaba 1.41 &print_message(&Resource('Error:ThisPageIsUneditable'));
145 wakaba 1.16 } else {
146 wakaba 1.42 &print_header($page, -noindex => 1, -expires => time+60);
147 wakaba 1.16 &print_editform($database{$page}, &get_info($page, $info_LastModified), admin=>0);
148 wakaba 1.9 }
149 wakaba 1.30 wiki::referer::add ($form{mypage}, $ENV{HTTP_REFERER});
150 wakaba 1.32 wiki::useragent::add ($ENV{HTTP_USER_AGENT});
151 wakaba 1.30 my ($r, $c) = get_search_result ($form{mypage});
152     my $rl = wiki::referer::list_html ($form{mypage});
153     if ($c) {
154 wakaba 1.41 print qq{<h2 id="wikipage-see-also">@{[&Resource('SeeAlso',escape=>1)]}</h2>};
155 wakaba 1.30 print $r;
156     }
157     if ($rl) {
158 wakaba 1.41 print qq(<div id="wikipage-referer"><h2>@{[&Resource('Referers',escape=>1)]}</h2>\n$rl</div>\n);
159 wakaba 1.30 }
160 wakaba 1.16 &print_footer($page);
161 wakaba 1.9 }
162    
163 wakaba 1.16 sub do_adminedit {
164     my ($page) = &unarmor_name(&armor_name($form{mypage}));
165 w 1.45 &print_header($page, -noindex => 1, -expires => time+60);
166 wakaba 1.16 if (not &is_editable($page)) {
167 wakaba 1.41 &print_message(&Resource('Error:ThisPageIsUneditable'));
168 wakaba 1.9 } else {
169 wakaba 1.41 &print_message(&Resource('Error:PasswordIsNotSpecified'));
170 wakaba 1.16 &print_editform($database{$page}, &get_info($page, $info_LastModified), admin=>1);
171 wakaba 1.9 }
172 wakaba 1.16 &print_footer($page);
173 wakaba 1.9 }
174    
175 wakaba 1.16 sub do_adminchangepassword {
176     if ($form{mynewpassword} ne $form{mynewpassword2}) {
177 wakaba 1.41 &print_error(&Resource('Error:PasswordMismatch'));
178 wakaba 1.16 }
179 w 1.43 my ($validpassword_crypt) = &get_info($PageName{AdminSpecialPage}, 'AdminPassword');
180 wakaba 1.16 if ($validpassword_crypt) {
181     if (not &valid_password($form{myoldpassword})) {
182 wakaba 1.41 &print_error(&Resource('Error:PasswordIsIncorrect'));
183 wakaba 1.9 }
184 wakaba 1.16 }
185     my ($sec, $min, $hour, $day, $mon, $year, $weekday) = localtime(time);
186     my (@token) = ('0'..'9', 'A'..'Z', 'a'..'z');
187     my $salt1 = $token[(time | $$) % scalar(@token)];
188     my $salt2 = $token[($sec + $min*60 + $hour*60*60) % scalar(@token)];
189     my $crypted = crypt($form{mynewpassword}, "$salt1$salt2");
190 w 1.43 &set_info($PageName{AdminSpecialPage}, 'AdminPassword', $crypted);
191 wakaba 1.16
192 wakaba 1.41 &print_header('CompletedSuccessfully', -noindex => 1);
193     &print_message(&Resource('Error:PasswordIsChanged'));
194     &print_footer('CompletedSuccessfully');
195 wakaba 1.16 }
196    
197 w 1.45 sub valid_password ($) {
198     my ($validpassword_crypt) = &get_info($PageName{AdminSpecialPage}, 'AdminPassword');
199     return crypt (shift, $validpassword_crypt) eq $validpassword_crypt ? 1 : 0;
200     }
201    
202 wakaba 1.16 sub do_write {
203     if (&frozen_reject()) {
204     return;
205 wakaba 1.9 }
206    
207     if (not &is_editable($form{mypage})) {
208 wakaba 1.29 &print_header($form{mypage}, -noindex => 1);
209 wakaba 1.41 &print_message(&Resource('Error:ThisPageIsUneditable'));
210 wakaba 1.16 &print_footer($form{mypage});
211 wakaba 1.9 return;
212     }
213    
214 wakaba 1.16 if (&conflict($form{mypage}, $form{mymsg})) {
215 wakaba 1.9 return;
216     }
217    
218     if ($form{mymsg}) {
219 w 1.46 if ($form{mytouch} || !ref $database) {
220     $database{$form{mypage}} = $form{mymsg};
221     } else {
222     $database->STORE ($form{mypage} => $form{mymsg}, -touch => 0);
223 wakaba 1.16 }
224     &set_info($form{mypage}, $info_IsFrozen, 0 + $form{myfrozen});
225 wakaba 1.36 my $fragment = '';
226     $fragment .= qq(;after_edit_cmd=@{[&encode($form{after_edit_cmd})]}) if $form{after_edit_cmd};
227 wakaba 1.33 if ($form{__comment_anchor_index}) {
228 wakaba 1.36 $fragment .= qq(#anchor-$form{__comment_anchor_index});
229 wakaba 1.33 } elsif ($form{__wikiform_anchor_index}) {
230 wakaba 1.36 $fragment .= qq(#wikiform-$form{__wikiform_anchor_index});
231 wakaba 1.33 }
232 wakaba 1.41 &print_header('CompletedSuccessfully', -noindex => 1, -goto => $url_cgi.'?mycmd='.&encode($form{after_edit_cmd}||'read').';mypage='.&encode($form{mypage}).qq(;x-param=@{[time.[0..9]->[rand 10]]}$fragment));
233     &print_message(&Resource('Error:SavedSuccessfully'));
234     &print_content(&Resource('Error:ContinueReading')." @{[&armor_name($form{mypage})]}");
235     &print_footer('CompletedSuccessfully');
236 wakaba 1.16 } else {
237     delete $database{$form{mypage}};
238     delete $infobase{$form{mypage}};
239 wakaba 1.29 &print_header($form{mypage}, -noindex => 1);
240 wakaba 1.41 &print_message(&Resource('Error:PageIsDeletedSuccessfully'));
241 wakaba 1.16 &print_footer($form{mypage});
242 wakaba 1.9 }
243     }
244    
245 wakaba 1.22 sub get_search_result ($;%) {
246 wakaba 1.32 my $word = lc shift;
247 wakaba 1.22 my %option = @_;
248 wakaba 1.30 my @r;
249     foreach my $page (keys %database) {
250 wakaba 1.41 next if !$option{-match_myself} && ($page eq $word);
251     my $content = lc $database{$page};
252 wakaba 1.32 if (index (lc $page, $word) > -1) {
253 wakaba 1.41 my $c = $content =~ s/\Q$word\E//g;
254 wakaba 1.30 push @r, [$page, $c+20];
255 wakaba 1.32 } elsif (index ($word, lc $page) > -1) {
256 wakaba 1.41 my $c = $content =~ s/\Q$word\E//g;
257 wakaba 1.30 push @r, [$page, $c+10];
258 wakaba 1.41 } elsif (my $c = $content =~ s/\Q$word\E//g) {
259 wakaba 1.30 push @r, [$page, $c];
260 wakaba 1.22 }
261     }
262 wakaba 1.41 #my $em = sub { my $s = shift; $s =~ s#(\Q$word\E)#<em>$1</em>#gi; $s };
263     my $r = join "\n", map {qq(<li>[$_->[1]] <a href ="$url_cgi?@{[&encode($_->[0])]}" class="wiki">@{[&escape($_->[0])]}</a> <span class="wikipage-summary">@{[&escape(&get_subjectline($_->[0]))]}</span></li>)} sort {$b->[1] <=> $a->[1] || $a->[0] cmp $b->[0]} @r;
264 wakaba 1.30 $r = qq|<ul class="search-result">$r</ul>| if $r;
265     wantarray? ($r, scalar @r): $r;
266 wakaba 1.22 }
267    
268 wakaba 1.28 sub do_random_jump {
269     my @list = keys %database;
270     my $name = &encode ($list[rand @list]);
271 wakaba 1.41 my $scheme = 'http';
272     $scheme = lc $1 if $main::ENV{SERVER_PROTOCOL} =~ m#([A-Za-z0-9+.%-]+)#;
273 wakaba 1.29 print "Location: $scheme://$main::ENV{SERVER_NAME}:$main::ENV{SERVER_PORT}$url_cgi?$name\n";
274 wakaba 1.28 print "\n";
275     }
276    
277 wakaba 1.9 sub print_error {
278     my ($msg) = @_;
279 w 1.43 &print_header($PageName{ErrorPage}, -noindex => 1);
280 wakaba 1.16 print qq(<p><strong class="error">$msg</strong></p>);
281 w 1.43 &print_footer($PageName{ErrorPage});
282 wakaba 1.9 exit(0);
283     }
284    
285 wakaba 1.27 sub print_header ($;%) {
286     my ($page, %option) = @_;
287 wakaba 1.41 my @head;
288     $option{body_class} = &is_frozen($page) ? 'frozen' : 'normal';
289     $option{body_class} .= " wiki-page-obsoleted" if $option{-content_format} =~ /obsoleted="yes"/;
290 wakaba 1.32 if ($option{-goto}) {
291 wakaba 1.36 if ($UA =~ m#Opera|MSIE 2\.#) {
292 wakaba 1.41 ## WARNING: This code may output unsafe HTML document if
293     ## $option{-goto} is not clean.
294 wakaba 1.36 $option{-goto} =~ tr/;/&/ if $UA =~ m#Opera#;
295 wakaba 1.33 print qq{Refresh: 0; url=$option{-goto}\n};
296 wakaba 1.41 push @head, qq(<meta http-equiv="refresh" content="0; url=$option{-goto}">);
297 wakaba 1.32 } else {
298 wakaba 1.36 $option{-goto} =~ tr/;/&/ if $UA =~ m#Mozilla/[1-4]\.#;
299 wakaba 1.33 print qq{Refresh: 0; url="$option{-goto}"\n};
300 wakaba 1.41 push @head, qq(<meta http-equiv="refresh" content="0; url=&quot;@{[&escape($option{-goto})]}&quot;">);
301 wakaba 1.32 }
302     }
303 wakaba 1.41 print qq{Last-Modified: @{[scalar gmtime $option{-last_modified}]}\n} if $option{-last_modified};
304 wakaba 1.42 if ($option{-expires}) {
305     print qq{Expires: @{[scalar gmtime $option{-expires}]}\n};
306     }
307 wakaba 1.32 if ($UA =~ m#Mozilla/2#) {
308 wakaba 1.41 my $ct = qq{text/html; charset=@{[&get_charset_name($kanjicode,compatible=>1)]}};
309     print qq{Content-Type: $ct\n};
310     push @head, qq{<meta http-equiv="content-type" content="$ct">};
311 wakaba 1.36 } elsif ($UA =~ m#Infomosaic#) {
312     print qq{Content-Type: text/html\n};
313 wakaba 1.32 } else {
314 wakaba 1.41 print qq{Content-Type: text/html; charset=@{[&get_charset_name($kanjicode)]}\n};
315 wakaba 1.32 }
316 wakaba 1.41 push @head, qq(<title>@{[&escape($page)]}</title>);
317 wakaba 1.42 if ($UA !~ m#Mozilla/[1-4]\.# || $UA =~ m#MSIE (?:[4-9]\.|\d\d)#) {
318     push @head, qq(<link rel="stylesheet" type="text/css" href="@{[&escape($uri{wiki}.'?mycmd=TEXT_CSS;mypage='.&encode($PageName{DefaultStyleForHTML}).';x-lm='.&get_info($PageName{DefaultStyleForHTML}, $info_LastModified))]}");
319     }
320 wakaba 1.41 push @head, q(<meta name="ROBOTS" content="NOINDEX">) if $option{-noindex};
321 wakaba 1.36 my ($Links, $links) = &make_navigate_links ($page);
322 wakaba 1.41 #print $Links; ## Link: fields
323     $links = join "\n", (@head, $links);
324 wakaba 1.9 print <<"EOD";
325 wakaba 1.18 Content-Language: $lang
326     Content-Style-Type: text/css
327 wakaba 1.9
328 wakaba 1.41 <!-- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
329     "http://www.w3.org/TR/html4/loose.dtd"> + RUBY -->
330     <html lang="$lang" class="$option{body_class}">
331 wakaba 1.36 <head profile="http://suika.fam.cx/~wakaba/-temp/wiki/wiki?WikiHTMLMetaProfile">
332     $links
333 wakaba 1.9 </head>
334 wakaba 1.41 <body class="$option{body_class}">
335 wakaba 1.22 EOD
336 wakaba 1.41 &print_navigate_links ($page);
337     print <<EOD;
338 wakaba 1.29 <h1 class="header">@{[&escape($page)]}</h1>
339 wakaba 1.22 EOD
340     }
341    
342 wakaba 1.41 sub get_charset_name ($;%) {
343     my ($charset, %option) = (lc shift, @_);
344     if ($charset =~ 'euc') {
345     $charset = $option{compatible} ? 'x-euc-jp' : 'euc-jp';
346     } elsif ($charset =~ 'sjis' || $charset =~ 'shift') {
347     $charset = $option{compatible} ? 'x-sjis' : 'shift_jis';
348     } elsif ($charset =~ 'jis') {
349     $charset = 'iso-2022-jp';
350 wakaba 1.32 }
351     $charset;
352     }
353    
354 wakaba 1.22 sub print_navigate_links (@) {
355     my ($page) = @_;
356 wakaba 1.41 my $editable = (&is_editable($page) && !&is_frozen($page)) ? 1 : 0;
357     my $cookedpage = &encode($page);
358 wakaba 1.22 print <<EOH;
359 wakaba 1.16 <div class="tools">
360     @{[ $editable
361 wakaba 1.41 ? qq(<a title="@{[&Resource('EditThisPageLong',escape=>1)]}" href="$url_cgi?mycmd=edit;mypage=$cookedpage" accesskey="E" class="wiki-cmd">@{[&Resource('EditThisPage',escape=>1)]}</a> | )
362 wakaba 1.16 : qq()
363     ]}
364 wakaba 1.41 <a href="$url_cgi?mycmd=read;mypage=$cookedpage;x-param=@{[time.[0..9]->[rand 10]]}" class="wiki-cmd" title="@{[&Resource('ViewThisPageLong',escape=>1)]}">@{[&Resource('ViewThisPage',escape=>1)]}</a> |
365     <a href="$url_cgi?mycmd=map;mypage=$cookedpage" class="wiki-cmd" title="@{[&Resource('ShowMapOfThisPageLong',escape=>1)]}">@{[&Resource('ShowMapOfThisPage',escape=>1)]}</a> |
366 w 1.43 <a href="$url_cgi?$PageName{CreatePage}" class="wiki" title="@{[&Resource('GoToCreatePageLong',escape=>1)]}">@{[&Resource('GoToCreatePage',escape=>1)]}</a> |
367     <a href="$url_cgi?$PageName{IndexPage}" class="wiki" title="@{[&Resource('GoToIndexPageLong',escape=>1)]}">@{[&Resource('GoToIndexPage',escape=>1)]}</a> |
368     <a href="$url_cgi?$PageName{FrontPage}" class="wiki" title="@{[&Resource('GoToHomePageLong',escape=>1)]}">@{[&Resource('GoToHomePage',escape=>1)]}</a> |
369     <a href="$url_cgi?$PageName{SearchPage}" class="wiki" title="@{[&Resource('GoToSearchPageLong',escape=>1)]}">@{[&Resource('GoToSearchPage',escape=>1)]}</a> |
370 wakaba 1.41 <a href="$url_cgi?mycmd=RandomJump;x-param=@{[time.[0..9]->[rand 10]]}" class="wiki randomlink" title="@{[&Resource('GoSomewhereLong',escape=>1)]}">@{[&Resource('GoSomewhere',escape=>1)]}</a> |
371 w 1.46 <a href="$url_cgi?$PageName{RecentChanges}" class="wiki" title="@{[&Resource('GoToRecentChangesLong',escape=>1)]}">@{[&Resource('GoToRecentChanges',escape=>1)]}</a>
372 wakaba 1.29 </div>
373     EOH
374 wakaba 1.36 }
375    
376     sub make_navigate_links ($) {
377     my $page = shift;
378     my @link;
379 wakaba 1.41 push @link, {rel=>'edit', href=>"$url_cgi?mycmd=edit;mypage=@{[&encode($page)]}", class=>"wiki-command", title=>&Resource('EditThisPageLink')} if &is_editable ($page) && !&is_frozen ($page);
380     push @link, {rel=>'edit', href=>"$url_cgi?mycmd=adminedit;mypage=@{[&encode($page)]}", class=>"wiki-command", title=>&Resource('AdminEditThisPageLink')} if &is_editable ($page) || &is_frozen ($page);
381     push @link, {rel=>'view', href=>"$url_cgi?mycmd=read;mypage=@{[&encode($page)]};x-p=@{[time.[0..9]->[rand 10]]}", class=>'wiki-command', title=>&Resource('ViewThisPageLink')};
382     push @link, {rel=>'myself', href=>"$url_cgi?@{[&encode($page)]}", class=>'wiki', title=>&Resource('GoToMyselfLink')};
383 w 1.43 push @link, {rel=>'index', href=>"$url_cgi?$PageName{IndexPage}", class=>'wiki', title=>&Resource('GoToIndexPageLink')};
384     push @link, {rel=>'home', href=>"$url_cgi?$PageName{FrontPage}", class=>'wiki', title=>&Resource('GoToHomePageLink')};
385 wakaba 1.41 push @link, {rel=>'News', href=>"$url_cgi?WikiNews", class=>'wiki', title=>&Resource('GoToWikiNewsLink')};
386 w 1.46 push @link, {rel=>'News', href=>"$url_cgi?$PageName{RecentChanges}", class=>"wiki", title=>&Resource('GoToRecentChangesLink')};
387 w 1.43 push @link, {rel=>'News', href=>"$url_cgi?$PageName{RssPage}", class=>"wiki", title=>&Resource('GoToRssPageLink'), type=>'application/xml'};
388     push @link, {rel=>'search', href=>"$url_cgi?$PageName{SearchPage}", class=>'wiki', title=>&Resource('GoToSearchPageLink')};
389 wakaba 1.41 push @link, {rel=>'help', href=>"$url_cgi?WikiHelp", class=>'wiki', title=>&Resource('GoToWikiHelpLink')};
390     push @link, {rel=>'copyright', href=>"$url_cgi?WikiPageLicense", class=>'wiki', title=>&Resource('GoToWikiPageLicenseLink')};
391     push @link, {rel=>'jump', href=>qq(javascript:var%20WikiName=prompt('Please%20input%20the%20WikiName:','','Jump%20to%20SuikaWiki');if(WikiName)%7B_content.location.href='$url_cgi%3F'+encodeURIComponent(WikiName)%7D), class=>'wiki-cmd', title=>&Resource('JumpToLink')};
392     push @link, {rel=>'jump', href=>qq(javascript:var%20WikiName=prompt('Please%20input%20the%20WikiName:','','Jump%20to%20SuikaWiki');if(WikiName)%7B_content.location.href='$url_cgi%3Fmycmd=edit;mypage='+encodeURIComponent(WikiName)%7D), class=>'wiki-cmd', title=>&Resource('JumpToEditLink')};
393     push @link, {rel=>'lucky', href=>"$url_cgi?mycmd=RandomJump;x-param=@{[time.[0..9]->[rand 10]]}", class=>'wiki randomlink', title=>&Resource('GoSomewhereLink')};
394     push @link, {rel=>'history', href=>$uri{cvs_wikipage}.do{my $s=$page;$s=~s/(.)/sprintf '%02X', ord $1/ges;$s}.'.txt', title=>&Resource('ViewHistoryOfThisPageLink'),hreflang=>'en'} if $uri{cvs_wikipage};
395 w 1.46 push @link, {rel=>'history', href=>"$url_cgi?mycmd=diff;mypage=@{[&encode($page)]}", title=>&Resource('ViewDiffOfThisPageLink'), class=>'wiki-command'} if $wiki::diff::UseDiff;
396 wakaba 1.41 push @link, {rel=>'contents', href=>"$url_cgi?mycmd=map;mypage=@{[&encode($page)]}", title=>&Resource('ShowMapOfThisPageLink'), class=>'wiki-command'};
397 wakaba 1.36 my ($Links, $links) = ('', '');
398     for my $e (@link) {
399     $links .= qq(<link);
400     $Links .= qq(Link: <$e->{href}>);
401     for my $attr (qw/rel rev href title class type hreflang charset/) {
402     $links .= qq( $attr="@{[&escape($e->{$attr})]}") if $e->{$attr};
403     }
404     for my $attr (qw/rel rev title/) {
405     $Links .= qq(; $attr="@{[do{$e->{$attr} =~ s/([\\\"])/\\$1/g; $e->{$attr}}]}") if $e->{$attr};
406     }
407     $links .= qq(>\n);
408     $Links .= qq(\n);
409     }
410     wantarray ? ($Links, $links) : $Links;
411 wakaba 1.9 }
412    
413 wakaba 1.16 sub print_footer {
414 wakaba 1.27 my ($page, $lm) = @_;
415 wakaba 1.33 my $epage = &encode ($page);
416 w 1.45 my $cvslog1 = q$Revision: 1.44 $;
417     my $cvslog2 = q$Date: 2003/01/01 12:30:24 $;
418 wakaba 1.22 print_navigate_links ($page);
419     print <<"EOD";
420 wakaba 1.41 @{[ $lm ? qq(<div id="wikipage-last-modified">@{[&Resource('LastModified=',escape=>1)]}@{[&_rfc3339_date ($lm)]}</div>) : '' ]}
421 wakaba 1.16 <div class="footer">
422 wakaba 1.41 <a href="http://www.hyuki.com/yukiwiki/" title="YukiWiki 2.0.beta1.2002-05-29 &copy; 2000-2002 by Hiroshi Yuki">@{[&Resource('About:Name:YukiWiki',escape=>1)]}</a> <a href="http://digit.que.ne.jp/work/" title="WalWiki 2.0.beta1.wal.1 &copy; 2000-2002 by Makio Tsukamoto">@{[&Resource('About:Name:WalWiki',escape=>1)]}</a>
423     <a href="/gate/cvs/wakaba/wiki/" title="@{[&Resource('About:SuikaWiki:JumpToCVS',escape=>1)]} ($cvslog2)">@{[&Resource('About:Name:SuikaWiki',escape=>1)]} $cvslog1</a>
424 wakaba 1.18 </div>
425 wakaba 1.16 </body>
426     </html>
427 wakaba 1.12 EOD
428 wakaba 1.9 }
429    
430 wakaba 1.16 sub escape {
431     my $s = shift;
432     $s =~ s|\r\n|\n|g;
433 wakaba 1.23 $s =~ s|&|&amp;|g;
434 wakaba 1.16 $s =~ s|<|&lt;|g;
435     $s =~ s|>|&gt;|g;
436     $s =~ s|"|&quot;|g;
437     return $s;
438     }
439    
440     sub unescape {
441     my $s = shift;
442     # $s =~ s|\n|\r\n|g;
443 wakaba 1.23 $s =~ s|&lt;|<|g;
444     $s =~ s|&gt;|>|g;
445     $s =~ s|&quot;|"|g;
446     $s =~ s|&amp;|&|g;
447 wakaba 1.16 return $s;
448     }
449    
450 wakaba 1.26 sub print_content ($;$) {
451 wakaba 1.27 my ($rawcontent, %option) = @_;
452 wakaba 1.29 print &text_to_html($rawcontent, toc=>1, %option);
453 wakaba 1.9 }
454    
455 wakaba 1.16 sub text_to_html {
456     my ($txt, %option) = @_;
457 wakaba 1.29 my @toc;
458     my @toc2 = @{$option{-toc}||[]};
459 wakaba 1.16 my $tocnum = 0;
460 wakaba 1.41
461     ## Load constants
462     my %const;
463     if ($option{content_format} =~ /import="([^"]+)"/) {
464     for (split /\s*,\s*/, $1) {
465     my $wp = $database{$_};
466     if ($wp =~ m!^\#\?SuikaWikiConst/1.0!) {
467     wiki::suikawikiconst::to_hash ($wp => \%const);
468     }
469     }
470     }
471    
472     $txt =~ s{__&&([^&]+)&&__}{defined $const{$1}?$const{$1}:qq(__&&$1&&__)}ge;
473     my (@txt) = split(/\n/, $txt);
474 wakaba 1.16 my (@saved, @result);
475     unshift(@saved, "</p>");
476     push(@result, "<p>");
477 wakaba 1.9 foreach (@txt) {
478     chomp;
479 wakaba 1.25 if (/^\*\*\*\*\*([^\x0D\x0A]*)/) {
480     push(@toc, qq(----- <a href="#i$tocnum">@{[&escape($1)||$tocnum]}</a>\n));
481 wakaba 1.41 push(@result, splice(@saved), qq(<h6 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h6>');
482 wakaba 1.18 $tocnum++;
483 wakaba 1.25 } elsif (/^\*\*\*\*([^\x0D\x0A]*)/) {
484     push(@toc, qq(---- <a href="#i$tocnum">@{[&escape($1)||$tocnum]}</a>\n));
485 wakaba 1.41 push(@result, splice(@saved), qq(<h5 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h5>');
486 wakaba 1.18 $tocnum++;
487 wakaba 1.25 } elsif (/^\*\*\*([^\x0D\x0A]*)/) {
488     push(@toc, qq(--- <a href="#i$tocnum">@{[&escape($1)||$tocnum]}</a>\n));
489 wakaba 1.41 push(@result, splice(@saved), qq(<h4 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h4>');
490 wakaba 1.18 $tocnum++;
491 wakaba 1.25 } elsif (/^\*\*([^\x0D\x0A]*)/) {
492 wakaba 1.16 # if (/^\*\*(.*)/) {
493     # Walrus mod (6) end
494 wakaba 1.25 push(@toc, qq(-- <a href="#i$tocnum">@{[&escape($1)||$tocnum]}</a>\n));
495 wakaba 1.41 push(@result, splice(@saved), qq(<h3 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h3>');
496 wakaba 1.16 $tocnum++;
497 wakaba 1.25 } elsif (/^\*([^\x0D\x0A]*)/) {
498     push(@toc, qq(- <a href="#i$tocnum">@{[&escape($1)||$tocnum]}</a>\n));
499 wakaba 1.41 push(@result, splice(@saved), qq(<h2 @{[&id_and_name("i$tocnum")]}>) . &inline($1, const => \%const) . '</h2>');
500 wakaba 1.16 $tocnum++;
501 wakaba 1.25 } elsif (/^(={1,6})(.*)/) {
502 wakaba 1.18 &back_push('ol', length($1), \@saved, \@result);
503 wakaba 1.41 push(@result, '<li>' . &inline($2, const => \%const) . '</li>');
504 wakaba 1.25 } elsif (/^(-{1,6})(.*)/) {
505 wakaba 1.28 &back_push('ul', length($1), \@saved, \@result);
506     my ($pf, $l) = ('', $2);
507     if (!$main::_EMBEDED && $l =~ s/^\s*\[([0-9]+)\]//) {
508     my $num = 0+$1;
509     $pf = qq(<a name="anchor-$num" id="anchor-$num" class="anchor">[$num]</a>);
510     }
511 wakaba 1.41 push(@result, '<li>' . $pf . &inline ($l, const => \%const) . '</li>');
512 wakaba 1.9 } elsif (/^:([^:]+):(.*)/) {
513 wakaba 1.16 &back_push('dl', 1, \@saved, \@result);
514 wakaba 1.41 push(@result, '<dt>' . &inline($1, const => \%const) . '</dt>', '<dd>' . &inline($2, const => \%const) . '</dd>');
515 wakaba 1.30 } elsif (/^(?!>>\d)(>{1,5})(.*)/) {
516 wakaba 1.16 &back_push('blockquote', length($1), \@saved, \@result);
517 wakaba 1.30 push @result, "<p>";
518 wakaba 1.41 push(@result, &inline($2, const => \%const));
519 wakaba 1.30 unshift @saved, "</p>";
520 wakaba 1.9 } elsif (/^\s*$/) {
521     push(@result, splice(@saved));
522 wakaba 1.30 push(@result, "<p>");
523 wakaba 1.9 unshift(@saved, "</p>");
524     } elsif (/^(\s+.*)$/) {
525 wakaba 1.16 &back_push('pre', 1, \@saved, \@result);
526 wakaba 1.41 push(@result, &inline($1, const => \%const));
527     } elsif (/^\,(.*?)[\x0D\x0A]*$/) {
528     &back_push('table', 1, \@saved, \@result);
529 wakaba 1.16 #######
530     # This part is taken from Mr. Ohzaki's Perl Memo and Makio Tsukamoto's WalWiki.
531     # XXXXX
532     my $tmp = "$1,";
533     my @value = map {/^"(.*)"$/ ? scalar($_ = $1, s/""/"/g, $_) : $_} ($tmp =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g);
534     my @align = map {(s/^\s+//) ? ((s/\s+$//) ? ' align="center"' : ' align="right"') : ''} @value;
535     my @colspan = map {($_ eq '==') ? 0 : 1} @value;
536     for (my $i = 0; $i < @value; $i++) {
537     if ($colspan[$i]) {
538     while ($i + $colspan[$i] < @value and $value[$i + $colspan[$i]] eq '==') {
539     $colspan[$i]++;
540     }
541     $colspan[$i] = ($colspan[$i] > 1) ? sprintf(' colspan="%d"', $colspan[$i]) : '';
542 wakaba 1.41 $value[$i] = sprintf('<td%s%s>%s</td>', $align[$i], $colspan[$i], &inline($value[$i], const => \%const));
543 wakaba 1.16 } else {
544     $value[$i] = '';
545     }
546     }
547     push(@result, join('', '<tr>', @value, '</tr>'));
548     # XXXXX
549     #######
550 wakaba 1.29 } elsif (/^\[(INS|DEL|PRE)\[\s*$/) {
551 wakaba 1.28 push @result, splice (@saved), '<'.lc($1).'>';
552     unshift @saved, "</p>";
553     push @result, "<p>";
554 wakaba 1.29 } elsif (/^\](INS|DEL|PRE)\]\s*$/) {
555 wakaba 1.28 push @result, splice (@saved), '</'.lc($1).'>';
556     } elsif (/^\[([0-9]+)\](.*)$/ && !$main::_EMBEDED) {
557     my $num = 0+$1;
558     push @result, qq(<a name="anchor-$num" id="anchor-$num" class="anchor">[$num]</a>);
559 wakaba 1.41 push @result, &inline ($2, const => \%const);
560 wakaba 1.9 } else {
561 wakaba 1.41 push(@result, &inline($_, const => \%const));
562 wakaba 1.9 }
563     }
564     push(@result, splice(@saved));
565 wakaba 1.27
566     my $toc = '';
567 wakaba 1.16 if ($option{toc}) {
568     # Convert @toc (table of contents) to HTML.
569     # This part is taken from Makio Tsukamoto's WalWiki.
570     my (@tocsaved, @tocresult);
571 wakaba 1.29 foreach (@toc,@toc2) {
572 wakaba 1.25 if (/^(-{1,6})(.*)$/) {
573 wakaba 1.16 &back_push('ul', length($1), \@tocsaved, \@tocresult);
574     push(@tocresult, '<li>' . $2 . '</li>');
575     }
576     }
577     push(@tocresult, splice(@tocsaved));
578 wakaba 1.27 $toc = join("\n", @tocresult);
579     $toc = $toc ? qq(<div id="wikipage-toc">$toc</div>) : '';
580 wakaba 1.16 }
581 wakaba 1.30 $toc .= join("\n", @result);
582     $toc =~ s#<p>\n</p>##g;
583     $toc =~ s#[\x0D\x0A]+</#</#g;
584     $toc =~ s#<pre>\n#<pre>#g;
585     $toc;
586 wakaba 1.9 }
587    
588     sub back_push {
589 wakaba 1.16 my ($tag, $level, $savedref, $resultref, $attr) = @_;
590     while (@$savedref > $level) {
591     push(@$resultref, shift(@$savedref));
592     }
593     if ($savedref->[0] ne "</$tag>") {
594     push(@$resultref, splice(@$savedref));
595     }
596     while (@$savedref < $level) {
597     unshift(@$savedref, "</$tag>");
598     push(@$resultref, "<$tag$attr>");
599 wakaba 1.9 }
600     }
601    
602 wakaba 1.41 sub inline ($;%) {
603     my ($line, %option) = @_;
604 wakaba 1.16 $line = &escape($line);
605 wakaba 1.34 $line =~ s{$embed_command{form}}{&make_custom_form ($1, $2, $3, $4)}ge;
606 wakaba 1.32 $line =~ s{\[(INS|DEL|SUP|SUB|VAR|CODE|KBD|SAMP|DFN)(?:\(([A-Za-z0-9\x20-]+)\))?\[(.+?)\]\]}{<@{[lc $1]}@{[$2 ? qq( class="$2") : '']}>$3</@{[lc $1]}>}g;
607 wakaba 1.30 $line =~ s:\[(WEAK)\[(.+?)\]\]:<span class="@{[lc $1]}">$2</span>:g;
608     $line =~ s:\[ABBR\[([^]]+)\] \[([^]]+)\]\]:<acronym title="$2">$1</acronym>:g;
609     $line =~ s:\[RUBYB\[([^]]+)\] \[([^]]+)\] \[([^]]+)\]\]:<span class="ruby"><ruby class="rb"><rb>$1</rb><rp>(</rp><rt>$2</rt><rp>)</rp></ruby><span class="rp"> (</span><span class="rt-below">$3</span><span class="rp">) </span></span>:g;
610     $line =~ s:\[RUBY\[([^]]+)\] \[([^]]+)\]\]:<ruby><rb>$1</rb><rp>(</rp><rt>$2</rt><rp>)</rp></ruby>:g;
611     $line =~ s:\[RUBYB\[([^]]+)\] \[([^]]+)\]\]:<span class="ruby"><span class="rb">$1</span><span class="rp"> (</span><span class="rt-below">$2</span><span class="rp">) </span></span>:g;
612     $line =~ s|'''([^']+)'''|<strong>$1</strong>|g;
613     $line =~ s|''([^']+)''|<em>$1</em>|g;
614 wakaba 1.31 $line =~ s{
615 wakaba 1.41 (\[\[(\#\S+?)\]\])
616     |\[\[([^[]+?)](?:&gt;&gt;([0-9]+))?]
617 wakaba 1.28 |&gt;&gt;([0-9]+)
618 wakaba 1.31 |&lt;([A-Za-z0-9%]+:(?:(?!&gt;).)+)&gt;
619     }{
620     my ($l, $page,$anchor, $anum, $uri) = ($1, $3,$4, 0+$5, $6);
621 wakaba 1.28 if ($l) {
622 w 1.46 &embedded_to_html($1);
623 wakaba 1.28 } elsif (defined $page) {
624     &make_wikilink ($page, anchor => 0+$anchor);
625     } elsif ($anum) {
626     qq(<a href="#anchor-$anum" class="wiki-anchor">&gt;&gt;$anum</a>);
627 wakaba 1.31 } elsif ($uri) {
628     &make_urilink ($uri);
629 wakaba 1.28 }
630 wakaba 1.31 }gex;
631 wakaba 1.16 return $line;
632     }
633    
634 wakaba 1.28 sub make_wikilink ($%) {
635     my ($ename, %option) = @_;
636     my $name = &unescape ($ename);
637     if ($database{$name}) {
638     my $subject = &escape (&get_subjectline ($name, delimiter => ''));
639     if ($option{anchor}) {
640     return qq(<a title="$subject" href="$url_cgi?@{[&encode($name)]}#anchor-$option{anchor}" class="wiki">$ename&gt;&gt;$option{anchor}</a>);
641     } else {
642     return qq(<a title="$subject" href="$url_cgi?@{[&encode($name)]}" class="wiki">$ename</a>);
643     }
644     } else {
645 wakaba 1.42 return qq(<a title="@{[&Resource('JumpAndEditWikiPage',escape=>1)]}" href="$url_cgi?@{[&escape($name)]}" class="wiki not-exist">$ename<span class="mark">@{[&Resource('JumpAndEditWikiPageMark',escape=>1)]}</span></a>);
646 wakaba 1.28 }
647     }
648    
649 wakaba 1.31 sub make_urilink ($;%) {
650     require URI;
651     my $uri = shift;
652     if ($uri =~ s/^IW://) { ## InterWiki (not URI)
653     $uri = &unescape ($uri);
654     if ($uri =~ /^([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+"):([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+")$/) {
655     my ($site, $name) = ($1, $2);
656     for ($site, $name) {
657     if (s/^"//) { s/"$//; s/\\(.)/$1/g }
658     }
659 wakaba 1.41 &init_InterWikiName () unless $interwiki{'[[]]'};
660 wakaba 1.31 if ($interwiki{$site}) {
661 wakaba 1.41 &load_formatter ('interwiki');
662 wakaba 1.31 my $uri = &escape ($fmt{interwiki}->replace ($interwiki{$site} => {site => $site, name => $name}));
663     $site = &escape ($site); $name = &escape ($name);
664 wakaba 1.32 qq(&lt;<a href="$uri" class="out-of-wiki interwiki" title="$name ($site); URI: &lt;$uri&gt;"><span class="interwiki-site">$site:</span><span class="interwiki-name">$name</span></a>&gt;);
665 wakaba 1.31 } else {
666 wakaba 1.41 qq(&lt;@{[&Resource('Error:UnknownInterWikiName=',escape=>1)]}@{[&escape ($site)]}&gt;);
667 wakaba 1.31 }
668     } else {
669 wakaba 1.41 qq(&lt;@{[&Resource('Error:InvalidInterWiki=',escape=>1)]}@{[&escape($uri)]}&gt;);
670 wakaba 1.31 }
671     } elsif ($uri =~ /^urn:/) { ## URN
672     my $uri2 = &escape (URI->new ('/uri-res/N2L?'.&unescape ($uri), 'http')->canonical);
673     qq(&lt;<a href="$uri2" title="URI: &lt;$uri&gt; (via &lt;$uri2&gt;)" class="out-of-wiki urn">$uri</a>&gt;);
674     } elsif ($uri =~ s/^MAIL://) { ## mail address (not URI)
675     my $uri2 = &escape (URI->new ('mailto:'.&unescape ($uri))->canonical);
676     qq(&lt;<a href="$uri2" class="out-of-wiki mail">$uri</a>&gt;);
677     } elsif ($uri =~ s/^IMG(?:\([^)]+\))?://) { ## image (not URI itself)
678     my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
679     qq(<img src="$uri2" alt="" title="URI: &lt;$uri2&gt;" class="out-of-wiki">);
680     } else { ## misc. URI
681     CGI::Carp::warningsToBrowser (0);
682     my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
683     CGI::Carp::warningsToBrowser (1);
684     qq(&lt;<a href="$uri2" title="URI: &lt;$uri2&gt;" class="out-of-wiki">$uri</a>&gt;);
685     }
686     }
687    
688 wakaba 1.41 {my $FormIndex = 0;
689 wakaba 1.35 sub make_custom_form ($$$$) {
690 wakaba 1.34 my ($wfname, $definition, $template, $option) = @_;
691 wakaba 1.33 ## $template and $option is currently not used in this procedure.
692     unless ($main::_EMBEDED) {
693     $FormIndex++;
694 wakaba 1.34 if (length $definition) {
695     my $param = bless {}, 'SuikaWiki::Plugin';
696     my $lastmodified = &get_info($form{mypage}, $info_LastModified);
697 wakaba 1.35 &load_formatter (qw/form_input form_option/);
698     $definition = &unescape ($definition);
699 wakaba 1.34 $definition =~ s/\\(.)/$1/g;
700     $option = &unescape ($option);
701     $option =~ s/\\(.)/$1/g;
702     $fmt{form_option}->replace ($option, $param);
703 wakaba 1.42 $param->{output}->{form} = 1 unless defined $param->{output}->{form};
704     $definition .= ' %submit;' if $definition !~ /%submit/ && !$param->{output}->{nosubmit} && $param->{output}->{form};
705 wakaba 1.35 my $target_page = $param->{output}->{page} || $form{mypage};
706 w 1.45 $param->{form_disabled} = 1 if $FixedPage{$target_page};
707 wakaba 1.34 my $target_form = $param->{output}->{id};
708 wakaba 1.42 my $r = '';
709     $r = <<EOH if $param->{output}->{form};
710 wakaba 1.33 <form method="post" action="$url_cgi" id="wikiform-$FormIndex" class="wikiform">
711 wakaba 1.35 <input type="hidden" name="mycmd" value="@{[$param->{form_disabled}?'read':'wikiform']}">
712     <input type="hidden" name="mypage" value="@{[&escape($target_page)]}">
713 wakaba 1.33 <input type="hidden" name="myLastModified" value="$lastmodified">
714     <input type="hidden" name="mytouch" value="on">
715 wakaba 1.34 <input type="hidden" name="@{[$target_form? qq(wikiform_targetform" value="@{[&escape($target_form)]}) : qq(wikiform_index" value="$FormIndex)]}">
716 wakaba 1.33 EOH
717 wakaba 1.34 $r .= qq(<a name="wikiform-$FormIndex"></a>) if $UA =~ m#Mozilla/[12]\.#;
718 wakaba 1.35 $r .= $fmt{form_input}->replace ($definition, $param);
719 wakaba 1.42 $r .= "</form>\n" if $param->{output}->{form};
720 wakaba 1.34 $r;
721     } else { ## No input-interface WikiForm
722     qq(<a id="wikiform-$FormIndex" name="wikiform-$FormIndex"><!-- #form --></a>);
723     }
724 wakaba 1.33 } else {
725 wakaba 1.41 qq(<ins class="wiki-error">@{[&Resource('Error:WikiForm:EmbedIsNotSupported',escape=>1)]}</ins>);
726 wakaba 1.9 }
727 wakaba 1.41 }}
728 wakaba 1.9
729 wakaba 1.16 sub print_message {
730     my ($msg) = @_;
731 wakaba 1.41 print qq(<p><strong>@{[&escape($msg)]}</strong></p>);
732 wakaba 1.22 }
733    
734 wakaba 1.16 sub init_form {
735 wakaba 1.41 ## TODO: Support multipart/form-data
736     my $query = '';
737     if (uc $main::ENV{REQUEST_METHOD} eq 'POST') {
738     read STDIN, $query, $main::ENV{CONTENT_LENGTH};
739 wakaba 1.16 }
740 wakaba 1.41 $query .= ($query ? ';' : '') . $main::ENV{QUERY_STRING};
741 wakaba 1.42 if ($main::ENV{REQUEST_METHOD} ne 'POST' && $main::ENV{QUERY_STRING} && $main::ENV{QUERY_STRING} !~ /[&;=]/) {
742 wakaba 1.30 my $query = &decode($main::ENV{QUERY_STRING});
743 wakaba 1.32 $query = &code_convert(\$query, $kanjicode);
744 wakaba 1.30 if ($page_command{$query}) {
745 wakaba 1.16 $form{mycmd} = $page_command{$query};
746     $form{mypage} = $query;
747 wakaba 1.30 } else {
748 wakaba 1.16 $form{mypage} = $query;
749 wakaba 1.30 $form{mycmd} = $database{$form{mypage}} ? 'read' : 'edit';
750     }
751 wakaba 1.41 } else {
752     for (split /[;&]/, $query) {
753     if (my ($n, $v) = split /=/, $_, 2) {
754     for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge};
755     $form{$n} = $v;
756     }
757     }
758 w 1.43 unless (defined $form{mypage}) {
759     $form{mypage} = $form{epage};
760     $form{mypage} =~ s/([0-9A-F]{2})/ord hex $1/g;
761     }
762 wakaba 1.41 if ($page_command{$form{mypage}} && $form{mycmd} eq 'read') {
763     $form{mypage} = &code_convert(\$form{mypage}, $kanjicode);
764     $form{mycmd} = $page_command{$form{mypage}};
765     }
766 wakaba 1.16 }
767 wakaba 1.30 $form{mypage} ||= 'HomePage';
768 wakaba 1.32 $form{mycmd} ||= 'read';
769 wakaba 1.16
770     # mypreview_edit -> do_edit, with preview.
771     # mypreview_adminedit -> do_adminedit, with preview.
772     # mypreview_write -> do_write, without preview.
773     foreach (keys %form) {
774     if (/^mypreview_(.*)$/) {
775     $form{mycmd} = $1;
776     $form{mypreview} = 1;
777     }
778     }
779 wakaba 1.9
780 wakaba 1.30 #
781 wakaba 1.16 # $form{mycmd} is frozen here.
782 wakaba 1.30 #
783 wakaba 1.16
784 wakaba 1.33 for (grep /^wikiform__/, keys %form) {
785     $form{$_} = &code_convert (\$form{$_}, $kanjicode);
786     }
787 wakaba 1.16 $form{mymsg} = &code_convert(\$form{mymsg}, $kanjicode);
788     $form{myname} = &code_convert(\$form{myname}, $kanjicode);
789     }
790    
791 wakaba 1.41 {my %SubjectLine;
792 wakaba 1.16 sub get_subjectline {
793     my ($page, %option) = @_;
794 wakaba 1.41 unless (defined $SubjectLine{$page}) {
795     if (not &is_editable($page)) {
796     $SubjectLine{$page} = "";
797     } else {
798     $SubjectLine{$page} = $database{$page};
799     $SubjectLine{$page} =~ s!^\#\?[^\x0A\x0D]+[\x0A\x0D]*!!s;
800     $SubjectLine{$page} =~ s/\x0D?\x0A.*//s;
801     }
802     }
803     if (length $SubjectLine{$page}) {
804     $option{delimiter} = defined $option{delimiter} ? $option{delimiter} : &Resource('Title-Summary Delimiter');
805     $option{delimiter}.$SubjectLine{$page}.$option{tail};
806 wakaba 1.16 } else {
807 wakaba 1.41 '';
808 wakaba 1.16 }
809 wakaba 1.41 }}
810 wakaba 1.9
811 wakaba 1.16 sub open_db {
812     if ($modifier_dbtype eq 'dbmopen') {
813 w 1.43 dbmopen(%database, $PathTo{WikiDataBase}, 0666) or &print_error("(dbmopen) $PathTo{WikiDataBase}");
814     dbmopen(%infobase, $PathTo{WikiInfoBase}, 0666) or &print_error("(dbmopen) $PathTo{WikiInfoBase}");
815 wakaba 1.16 } elsif ($modifier_dbtype eq 'AnyDBM_File') {
816 wakaba 1.41 eval q{use AnyDBM_File};
817 w 1.43 tie(%database, "AnyDBM_File", $PathTo{WikiDataBase}, O_RDWR|O_CREAT, 0666) or &print_error("(tie AnyDBM_File) $PathTo{WikiDataBase}");
818     tie(%infobase, "AnyDBM_File", $PathTo{WikiInfoBase}, O_RDWR|O_CREAT, 0666) or &print_error("(tie AnyDBM_File) $PathTo{WikiInfoBase}");
819 w 1.46 } elsif ($modifier_dbtype eq 'YukiWikiDB') {
820 wakaba 1.41 eval q{use Yuki::YukiWikiDB};
821 w 1.43 tie(%database, "Yuki::YukiWikiDB", $PathTo{WikiDataBase}) or &print_error("(tie Yuki::YukiWikiDB) $PathTo{WikiDataBase}");
822     tie(%infobase, "Yuki::YukiWikiDB", $PathTo{WikiInfoBase}) or &print_error("(tie Yuki::YukiWikiDB) $PathTo{WikiInfoBase}");
823 w 1.46 } else {
824     eval qq{use $modifier_dbtype};
825     $database = tie(%database, $modifier_dbtype => $PathTo{WikiDataBase}, -lock => 2, -backup => $wiki::diff::UseDiff) or &print_error("(tie $modifier_dbtype) $PathTo{WikiDataBase}");
826     tie(%infobase, $modifier_dbtype => $PathTo{WikiInfoBase}, -lock => 2) or &print_error("(tie $modifier_dbtype) $PathTo{WikiInfoBase}");
827 wakaba 1.9 }
828     }
829    
830 wakaba 1.16 sub close_db {
831     if ($modifier_dbtype eq 'dbmopen') {
832     dbmclose(%database);
833     dbmclose(%infobase);
834     } else {
835     untie(%database);
836     untie(%infobase);
837     }
838     }
839 wakaba 1.9
840 wakaba 1.16 sub print_editform {
841     my ($mymsg, $lastmodified, %mode) = @_;
842     my $frozen = &is_frozen($form{mypage});
843    
844     if ($form{mypreview}) {
845     if ($form{mymsg}) {
846     unless ($mode{conflict}) {
847 wakaba 1.41 print qq(<h3>@{[&Resource('Preview:Title',escape=>1)]}</h3>\n);
848     print qq(<p>@{[&Resource('Preview:Notice',escape=>1)]}</p>\n);
849 wakaba 1.16 print qq(<div class="preview">\n);
850     &print_content($form{mymsg});
851     print qq(</div>\n);
852     }
853     } else {
854 wakaba 1.41 print @{[&Resource('Preview:Empty',escape=>1)]};
855 wakaba 1.16 }
856     $mymsg = &escape($form{mymsg});
857 wakaba 1.9 } else {
858 wakaba 1.25 $mymsg = &escape($mymsg || $database{NewPageTemplate});
859 wakaba 1.9 }
860 wakaba 1.36 my $magic = '';
861     $magic = $1 if $mymsg =~ m/^([^\x0A\x0D]+)/s;
862 wakaba 1.9
863 wakaba 1.16 my $edit = $mode{admin} ? 'adminedit' : 'edit';
864 wakaba 1.36 my $selected = 'read';
865     if ($form{after_edit_cmd}) {
866     $selected = $form{after_edit_cmd};
867 wakaba 1.41 } elsif ($magic =~ /Const|Config|CSS/) {
868 wakaba 1.36 $selected = 'edit';
869     }
870     my $afteredit = <<EOH;
871     <select name="after_edit_cmd">
872 wakaba 1.41 <option value="read" label="@{[&Resource('Edit:SaveAndView',escape=>1)]}"@{[$selected eq 'read' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndView',escape=>1)]}</option>
873     <option value="edit" label="@{[&Resource('Edit:SaveAndEdit',escape=>1)]}"@{[$selected eq 'edit' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndEdit',escape=>1)]}</option>
874 wakaba 1.36 </select>
875     EOH
876 wakaba 1.16
877     print <<"EOD";
878     <form action="$url_cgi" method="post">
879 wakaba 1.41 <h2>@{[&Resource('Edit:Title',escape=>1)]}</h2>
880     @{[ $mode{conflict} ? '' : qq(<input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}"><kbd>S</kbd>) ]}
881 w 1.45 @{[ $mode{admin} ? qq(<label>@{[&Resource('Edit:Password=',escape=>1)]}<input type="password" name="mypassword" value="" size="10"></label>) : "" ]} [@{[do {my $n = 0;
882 wakaba 1.36 $mymsg =~ s/(?:-+\s)?\[([0-9]+)\]/$n = $1 if $1 > $n; $&/mge;
883     ++$n}]}]<br>
884 wakaba 1.16 <input type="hidden" name="myLastModified" value="$lastmodified">
885 w 1.45 <input type="hidden" name="mypage" value="@{[&escape($form{mypage})]}">
886 wakaba 1.41 <textarea cols="@{[&Resource('Edit:Form:Cols')+0||80]}" rows="@{[&Resource('Edit:Form:Rows')+0||20]}" name="mymsg" tabindex="1">$mymsg</textarea><br>
887 wakaba 1.16 @{[
888     $mode{admin} ?
889     qq(
890 wakaba 1.41 <label><input type="radio" name="myfrozen" value="1" @{[$frozen ? qq(checked="checked") : ""]}>@{[&Resource('Edit:Freeze',escape=>1)]}</label>
891     <label><input type="radio" name="myfrozen" value="0" @{[$frozen ? "" : qq(checked="checked")]}>@{[&Resource('Edit:DontFreeze',escape=>1)]}</label><br>)
892 wakaba 1.16 : ""
893     ]}
894     @{[
895     $mode{conflict} ? "" :
896     qq(
897 w 1.45 <label><input type="checkbox" name="mytouch" value="on" checked="checked">@{[&Resource('Edit:UpdateTimeStamp',escape=>1)]}</label><br>
898 wakaba 1.41 <input type="submit" name="mypreview_$edit" value="@{[&Resource('Edit:Preview',escape=>1)]}">
899 w 1.45 <label><input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}" accesskey="S"><kbd>S</kbd></label>
900 wakaba 1.36 $afteredit
901 wakaba 1.16 )
902     ]}
903     </form>
904     EOD
905     unless ($mode{conflict}) {
906 wakaba 1.41 ## Show help text
907     my $help = $database{WikiEditHelp};
908 wakaba 1.30 $help =~ s!^\#\?([A-Z][A-Za-z0-9-]+/[0-9.]+(?:[^0-9.\x0D\x0A][^\x0D\x0A]*)?)[\x0D\x0A]+!!s;
909     print &text_to_html ($help, toc => 0);
910 wakaba 1.9 }
911     }
912    
913 wakaba 1.16 sub is_editable {
914     my ($page) = @_;
915 w 1.45 if ($FixedPage{$page} || $page =~ /\s/ || $page =~ /^\#/) {
916 wakaba 1.16 return 0;
917     } else {
918     return 1;
919     }
920 wakaba 1.9 }
921    
922 wakaba 1.16 # armor_name:
923     # WikiName -> WikiName
924     # not_wiki_name -> [[not_wiki_name]]
925 wakaba 1.31 sub armor_name { qq([[$_[0]]]) }
926 wakaba 1.9
927 wakaba 1.16 # unarmor_name:
928     # [[bracket_name]] -> bracket_name
929     # WikiName -> WikiName
930     sub unarmor_name {
931     my ($name) = @_;
932 wakaba 1.41 if ($name =~ /^\[\[(\S+?)\]\]$/) {
933 wakaba 1.16 return $1;
934     } else {
935     return $name;
936     }
937 wakaba 1.9 }
938    
939 wakaba 1.16 sub decode {
940     my ($s) = @_;
941     $s =~ tr/+/ /;
942     $s =~ s/%([A-Fa-f0-9][A-Fa-f0-9])/pack("C", hex($1))/eg;
943     return $s;
944     }
945 wakaba 1.9
946 wakaba 1.16 sub encode {
947     my ($s) = @_;
948     my $encoded = '';
949     foreach my $ch (split(//, $s)) {
950     if ($ch =~ /[A-Za-z0-9_]/) {
951     $encoded .= $ch;
952     } else {
953     $encoded .= '%' . sprintf("%02X", ord($ch));
954     }
955     }
956     return $encoded;
957     }
958 wakaba 1.9
959 wakaba 1.16 sub conflict {
960     my ($page, $rawmsg) = @_;
961     if ($form{myLastModified} eq &get_info($page, $info_LastModified)) {
962     return 0;
963     }
964 wakaba 1.29 &print_header($page, -noindex => 1);
965 wakaba 1.41 &print_content(&Resource('Error:Conflict'));
966 wakaba 1.16 &print_editform($rawmsg, $form{myLastModified}, frozen=>0, conflict=>1);
967     &print_footer($page);
968     return 1;
969     }
970    
971     sub get_now {
972 wakaba 1.41 my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);
973 wakaba 1.16 $year += 1900;
974     $mon++;
975     $mon = "0$mon" if $mon < 10;
976     $day = "0$day" if $day < 10;
977     $hour = "0$hour" if $hour < 10;
978     $min = "0$min" if $min < 10;
979 wakaba 1.28 #$sec = "0$sec" if $sec < 10;
980 wakaba 1.41 return "$year-$mon-$day $hour:$min";
981 wakaba 1.16 }
982    
983     sub init_InterWikiName {
984 wakaba 1.41 my @content = split /\n/, $database{InterWikiName};
985 wakaba 1.31 for (@content) {
986     if (/^([^#]\S*)\s+(\S[^\x0A\x0D]+)/) {
987     $interwiki{$1} = $2;
988 wakaba 1.16 }
989 wakaba 1.31 }
990 wakaba 1.41 $interwiki{'[[]]'} = 1; ## dummy
991 wakaba 1.16 }
992 wakaba 1.9
993    
994 wakaba 1.16 sub get_info {
995     my ($page, $key) = @_;
996     my %info = map { split(/=/, $_, 2) } split(/\n/, $infobase{$page});
997     return $info{$key};
998     }
999 wakaba 1.9
1000 wakaba 1.16 sub set_info {
1001     my ($page, $key, $value) = @_;
1002     my %info = map { split(/=/, $_, 2) } split(/\n/, $infobase{$page});
1003     $info{$key} = $value;
1004     my $s = '';
1005     for (keys %info) {
1006     $s .= "$_=$info{$_}\n";
1007     }
1008     $infobase{$page} = $s;
1009     }
1010 wakaba 1.9
1011 wakaba 1.16 sub frozen_reject {
1012     my ($isfrozen) = &get_info($form{mypage}, $info_IsFrozen);
1013     my ($willbefrozen) = $form{myfrozen};
1014     if (not $isfrozen and not $willbefrozen) {
1015     # You need no check.
1016     return 0;
1017     } elsif (valid_password($form{mypassword})) {
1018     # You are admin.
1019     return 0;
1020     } else {
1021 wakaba 1.41 &print_error(&Resource('Error:PasswordIsIncorrect'));
1022 wakaba 1.16 return 1;
1023     }
1024     }
1025 wakaba 1.9
1026 wakaba 1.16 sub is_frozen {
1027     my ($page) = @_;
1028     if (&get_info($page, $info_IsFrozen)) {
1029     return 1;
1030     } else {
1031     return 0;
1032     }
1033     }
1034 wakaba 1.9
1035 wakaba 1.16 sub do_comment {
1036     my ($content) = $database{$form{mypage}};
1037 wakaba 1.30 my $default_name; ## this code does not strict.
1038     $default_name = $1 if $content =~ /default-name="([^"]+)"/;
1039     my $datestr = '[WEAK['.&get_now.']]';
1040 wakaba 1.41 my $namestr = $form{myname} || $default_name || &Resource('WikiForm:WikiComment:DefaultName');
1041 wakaba 1.30 ($namestr = '', $datestr = '') if $form{myname} eq 'nodate';
1042 wakaba 1.29 if ($namestr =~ /^(?:>>)?[0-9]/) {
1043     $namestr = qq( ''$namestr'': );
1044 wakaba 1.30 } elsif (length $namestr) {
1045 wakaba 1.29 $namestr = qq( ''[[$namestr]]'': );
1046     }
1047 wakaba 1.33 my $anchor = &get_new_anchor_index ($content);
1048 wakaba 1.27 my $i = 1; my $o = 0;
1049 wakaba 1.41 $content =~ s{(\[\[\#r?comment\]\])}{
1050 wakaba 1.25 my $embed = $1;
1051     if ($i == $form{comment_index}) {
1052 wakaba 1.41 if ($embed ne '[[#rcomment]]') {
1053 wakaba 1.28 $embed = "- [$anchor] $datestr$namestr$form{mymsg}\n$embed"; $o = 1;
1054 wakaba 1.25 } else {
1055 wakaba 1.28 $embed .= "\n- [$anchor] $datestr$namestr$form{mymsg}"; $o = 1;
1056 wakaba 1.25 }
1057     }
1058     $i++; $embed;
1059     }ge;
1060     unless ($o) {
1061 wakaba 1.30 $content = "#?SuikaWiki/0.9\n\n" unless $content;
1062     $content .= "\n" unless $content =~ /\n$/s;
1063 wakaba 1.28 $content .= "- [$anchor] $datestr$namestr$form{mymsg}\n";
1064 wakaba 1.16 }
1065 wakaba 1.29 $form{__comment_anchor_index} = $anchor;
1066 wakaba 1.30 if ($form{mymsg} || $form{myname}) {
1067 wakaba 1.16 $form{mymsg} = $content;
1068     $form{mytouch} = 'on';
1069     &do_write;
1070     } else {
1071     $form{mycmd} = 'read';
1072     &do_read;
1073     }
1074     }
1075 wakaba 1.9
1076 wakaba 1.33 sub get_new_anchor_index ($) {
1077     my $content = shift;
1078     my $anchor = 0;
1079     $content =~ s/^(?:[-=]+\s*)?\[([0-9]+)\]/$anchor = $1 if $1 > $anchor; $&/mge;
1080     $anchor + 1;
1081     }
1082    
1083 wakaba 1.27 my $CommentIndex = 0;
1084 wakaba 1.16 sub embedded_to_html {
1085     my ($embedded) = @_;
1086 wakaba 1.41 if ($embedded eq '[[#comment]]' or $embedded eq '[[#rcomment]]') {
1087 wakaba 1.28 unless ($main::_EMBEDED) {
1088 wakaba 1.16 my $lastmodified = &get_info($form{mypage}, $info_LastModified);
1089     return <<"EOD";
1090 wakaba 1.30 <form action="$url_cgi" method="post" id="x-comment-@{[++$CommentIndex]}" class="comment"><p>
1091 wakaba 1.16 <input type="hidden" name="mycmd" value="comment">
1092     <input type="hidden" name="mypage" value="$form{mypage}">
1093     <input type="hidden" name="myLastModified" value="$lastmodified">
1094     <input type="hidden" name="mytouch" value="on">
1095 wakaba 1.27 <input type="hidden" name="comment_index" value="$CommentIndex">
1096 wakaba 1.41 @{[&Resource('WikiForm:WikiComment:Name=',escape=>1)]}
1097 wakaba 1.30 <input type="text" name="myname" value="" size="10" class="comment-name">
1098     <input type="text" name="mymsg" value="" size="60" class="comment-msg">
1099 wakaba 1.41 <input type="submit" value="@{[&Resource('WikiForm:Add',escape=>1)]}" title="@{[&Resource('WikiForm:AddLong',escape=>1)]}" class="comment-submit">
1100 wakaba 1.30 </p></form>
1101 wakaba 1.16 EOD
1102 wakaba 1.28 } else {
1103     return <<"EOD";
1104     <del><form action="$url_cgi" method="get">
1105     <input type="hidden" name="mycmd" value="read">
1106     <input type="hidden" name="mypage" value="$form{mypage}">
1107 wakaba 1.41 @{[&Resource('WikiForm:WikiComment:Name=',escape=>1)]}
1108 wakaba 1.28 <input type="text" name="myname" value="" size="10" disabled="disabled">
1109     <input type="text" name="mymsg" value="" size="60" disabled="disabled">
1110     </form></del>
1111     EOD
1112     }
1113 wakaba 1.22 } elsif ($embedded =~ /$embed_command{searched}/) {
1114 wakaba 1.30 return get_search_result ($1, -match_myself => 1);
1115 wakaba 1.28 } elsif ($embedded =~ /^\[\[\#embed:(.+)\]\]$/) {
1116     my ($name, $r) = ($1, '');
1117     if ($main::_EMBEDED != 1) {
1118     my ($content, $cf) = ($database{$name}, 'SuikaWiki/0.9');
1119     $cf = $1 if $content =~ s!^(?:[\#<]\?|/\*\s*)?([A-Z][A-Za-z0-9-]+/[0-9.]+(?:[^0-9.][^\x0D\x0A]*)?)[\x0D\x0A]+!!s;
1120     if ($cf =~ m!^(?:\#\?)?SuikaWiki/0.9(?:$|\s)!) {
1121     $main::_EMBEDED = 1;
1122     $r = &text_to_html ($content, content_format => $cf);
1123     $main::_EMBEDED = 0;
1124     } elsif (length $content) {
1125     $r = "<pre>@{[&escape ($content)]}</pre>";
1126     } else {
1127 wakaba 1.41 $r = &text_to_html ("[INS[\n[[$name]]: @{[&Resource('Embed:PageNotFound')]}\n]INS]\n", content_format => 'SuikaWiki/0.9');
1128 wakaba 1.28 }
1129     } else { ## nested #EMBED
1130 wakaba 1.41 $r = &text_to_html ("[INS[\n[[$name]]: @{[&Resource('Embed:Nested',escape=>1)]}\n]INS]\n", content_format => 'SuikaWiki/0.9');
1131 wakaba 1.16 }
1132 wakaba 1.31 return qq(<blockquote title="@{[&escape($name)]}" class="wiki-embed">$r<div class="cite-note">¡Ø<cite><a href="$url_cgi?@{[&encode($name)]}" class="wiki">@{[&escape($name)]}</a></cite>¡Ù</div></blockquote>);
1133 wakaba 1.28 } elsif ($embedded =~ /^\[\[\#randomlink:(.+)\]\]$/) {
1134     return qq(<a href="$url_cgi?mycmd=RandomJump;x-param=@{[time.[0..9]->[rand 10]]}" class="wiki randomlink">$1</a>);
1135     } else {
1136     return $embedded;
1137     }
1138 wakaba 1.16 }
1139 wakaba 1.9
1140 wakaba 1.35 sub load_formatter (@) {
1141     for my $t (@_) {
1142 wakaba 1.33 unless ($fmt{$t}) {
1143 wakaba 1.41 require Message::Util::Formatter;
1144 wakaba 1.33 $fmt{$t} = Message::Util::Formatter->new;
1145     for (@{$SuikaWiki::Plugin::List{'wiki'.$t}||[]}) {
1146     $_->load_formatter ($fmt{$t}, type => 'wiki'.$t);
1147     }
1148     }
1149     }
1150 wakaba 1.35 }
1151    
1152     sub do_wikiform {
1153     my $content = $database{$form{mypage}};
1154     my $anchor = &get_new_anchor_index ($content);
1155     &load_formatter (qw/form_template form_option/);
1156 wakaba 1.33 my $write = 0;
1157     my $i = 1;
1158     $content =~ s{$embed_command{form}}{
1159 wakaba 1.34 my ($embed, $wfname, $template, $option) = ($&, $1, $3, $4);
1160     if (($wfname && $wfname eq $form{wikiform_targetform})
1161     || $i == $form{wikiform_index}) {
1162     $template =~ s/\\(.)/$1/g;
1163     $option =~ s/\\(.)/$1/g;
1164 wakaba 1.33 my $param = bless {}, 'SuikaWiki::Plugin';
1165     $param->{page} = $form{mypage};
1166     $param->{form_index} = $i;
1167 wakaba 1.34 $param->{form_name} = $wfname;
1168 wakaba 1.33 $param->{anchor_index} = $anchor;
1169     $param->{argv} = \%form;
1170     $param->{default_name} = $1 if $content =~ /default-name="([^"]+)"/;
1171 wakaba 1.41 $param->{default_name} ||= &Resource('WikiForm:WikiComment:DefaultName');
1172 wakaba 1.33 $fmt{form_option}->replace ($option, $param);
1173 wakaba 1.34 my $t = 1;
1174     for (@{$param->{require}||[]}) {
1175 wakaba 1.36 (undef $t, last) unless length $param->{argv}->{'wikiform__'.$_};
1176 wakaba 1.34 }
1177     $t = $fmt{form_template}->replace ($template, $param) if $t;
1178 wakaba 1.33 if (length $t) {
1179     if ($param->{output}->{reverse}) {
1180 wakaba 1.34 $embed .= "\n" . $t;
1181     } else {
1182 wakaba 1.33 $embed = $t . "\n" . $embed;
1183     }
1184     $write = 1;
1185     $form{__comment_anchor_index} = $anchor
1186 wakaba 1.34 if $param->{anchor_index_}; ## $anchor is used!
1187 wakaba 1.33 }
1188 wakaba 1.34 $form{__wikiform_anchor_index} = $i;
1189     undef $form{wikiform_targetform}; ## make sure never to match
1190     undef $form{wikiform_index}; ## with WikiForm in rest of page
1191 wakaba 1.33 }
1192     $i++; $embed;
1193     }ge;
1194     unless ($write) {
1195     #$content = "#?SuikaWiki/0.9\n\n" unless $content;
1196     #$content .= "\n" unless $content =~ /\n$/s;
1197     #
1198     }
1199     if ($write) {
1200     $form{mymsg} = $content;
1201     $form{mytouch} = 'on';
1202     &do_write;
1203     } else {
1204     $form{mycmd} = 'read';
1205     &do_read;
1206     }
1207     }
1208    
1209 wakaba 1.16 sub code_convert {
1210 wakaba 1.41 require Jcode;
1211 wakaba 1.27 my ($contentref, $code) = (shift, shift || $kanjicode);
1212 wakaba 1.31 $code = 'jis' if $code =~ /iso/;
1213     $code = 'euc' if $code =~ /euc/;
1214     $code = 'sjis' if $code =~ /shift/;
1215     $code = 'utf8' if $code =~ /utf/;
1216 w 1.43 $$contentref = Jcode->new ($contentref)->tr ("\xA3\xB0-\xA3\xB9\xA3\xC1-\xA3\xDA\xA3\xE1-\xA3\xFA\xA1\xF5\xA1\xA4\xA1\xA5\xA1\xA7\xA1\xA8\xA1\xA9\xA1\xAA\xA1\xAE\xA1\xB0\xA1\xB2\xA1\xBF\xA1\xC3\xA1\xCA\xA1\xCB\xA1\xCE\xA1\xCF\xA1\xD0\xA1\xD1\xA1\xDC\xA1\xF0\xA1\xF3\xA1\xF4\xA1\xF6\xA1\xF7\xA1\xE1\xA2\xAF\xA2\xB0\xA2\xB2\xA2\xB1\xA1\xE4\xA1\xE3\xA1\xC0\xA1\xA1" => q(0-9A-Za-z&,.:;?!`^_/|()[]{}+$%#*@='"~-><\ ))->$code;
1217 wakaba 1.16 return $$contentref;
1218     }
1219    
1220     sub do_diff {
1221     my $title = $form{mypage};
1222 wakaba 1.29 &print_header($title, -noindex => 1);
1223 w 1.46 print qq(<h2>@{[&Resource('Diff:Title',escape=>1)]}</h2>);
1224 wakaba 1.41 print qq(<p>@{[&Resource('Diff:Notice',escape=>1)]}</p>);
1225 wakaba 1.16 print qq(<pre class="diff">);
1226 w 1.46 for (split(/\n/, &escape ($database->traverse_diff ($form{mypage})))) {
1227 wakaba 1.16 if (/^\+(.*)/) {
1228 wakaba 1.41 print qq(<ins class="added">$1</ins>\n);
1229 wakaba 1.16 } elsif (/^\-(.*)/) {
1230 wakaba 1.41 print qq(<del class="deleted">$1</del>\n);
1231 wakaba 1.16 } elsif (/^\=(.*)/) {
1232     print qq(<span class="same">$1</span>\n);
1233     } else {
1234     print qq|??? $_\n|;
1235     }
1236     }
1237     print qq(</pre>);
1238     &print_footer($title);
1239     }
1240 wakaba 1.9
1241 wakaba 1.16 sub do_rss {
1242 wakaba 1.41 eval q{use Yuki::RSS};
1243 wakaba 1.16 my $rss = new Yuki::RSS(
1244     version => '1.0',
1245 wakaba 1.41 encoding => &get_charset_name ($kanjicode),
1246     );
1247     my $scheme = 'http';
1248     $scheme = lc $1 if $main::ENV{SERVER_PROTOCOL} =~ m#([A-Za-z0-9+.%-]+)#;
1249     my $myuri = "$scheme://$main::ENV{SERVER_NAME}:$main::ENV{SERVER_PORT}$url_cgi";
1250     $rss->stylesheet (
1251     href => $myuri . "?mycmd=TEXT_CSS;mypage=WikiStyle:RSS",
1252     type => 'text/css',
1253 wakaba 1.16 );
1254     $rss->channel(
1255 wakaba 1.41 title => &Resource ('RSS:WikiTitle'),
1256     link => $myuri,
1257     description => &Resource ('RSS:WikiDescription'),
1258     'dc:language' => $lang,
1259 wakaba 1.16 );
1260 wakaba 1.41 my $recentchanges = $database{RecentChanges};
1261 wakaba 1.16 my $count = 0;
1262     foreach (split(/\n/, $recentchanges)) {
1263     last if ($count >= 15);
1264 wakaba 1.41 if (/\[\[([^]]+)\]\]/) {
1265     my $title = $1;
1266     $rss->add_item (
1267     title => &escape($title),
1268     link => $myuri . '?' . &encode($title),
1269     description => &escape(&get_subjectline($title,delimiter=>'')),
1270     'dc:date' => &get_info ($title, $info_LastModified),
1271     );
1272     $count++;
1273     }
1274 wakaba 1.16 }
1275     # print RSS information (as XML).
1276     print <<"EOD"
1277 wakaba 1.41 Content-type: application/xml; charset=@{[&get_charset_name ($kanjicode)]}
1278 wakaba 1.9
1279 wakaba 1.16 @{[$rss->as_string]}
1280     EOD
1281     }
1282 wakaba 1.9
1283 wakaba 1.41 sub _rfc3339_date ($) {
1284     my @time = gmtime (shift);
1285     sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
1286     }
1287    
1288 wakaba 1.16 sub is_exist_page {
1289     my ($name) = @_;
1290     if ($use_exists) {
1291     return exists($database{$name});
1292     } else {
1293     return $database{$name};
1294     }
1295 wakaba 1.27 }
1296    
1297     sub __get_database ($) { $database{ $_[0] } }
1298 wakaba 1.32 sub __set_database ($$) { $database{ $_[0] } = $_[1] }
1299 wakaba 1.27
1300 wakaba 1.41 sub do_map {
1301     my $page = $form{mypage};
1302     &print_header ($page);
1303     wiki::referer::add ($form{mypage}, $ENV{HTTP_REFERER});
1304     wiki::useragent::add ($ENV{HTTP_USER_AGENT});
1305     my ($r, $c) = get_search_result ($form{mypage});
1306     my $rl = wiki::referer::list_html ($form{mypage});
1307     print "<h2>@{[&Resource('Map:Title',escape=>1)]}</h2>\n<p>@{[&Resource('Map:Description',escape=>1)]}</p>\n";
1308     my %option = (level => 0+&Resource('Map:Depth'), weight_list => {}, not_exist => {},
1309     map_from_here => &Resource('Map:FromHere'),
1310     map_from_here_description => &Resource('Map:FromHereLong'));
1311     &wiki::map::make_list ($page, %option);
1312     print &wiki::map::list_to_html ($page, $option{weight_list}, %option);
1313     if ($c) {
1314     print qq{<h2 id="wikipage-see-also">@{[&Resource('SeeAlso',escape=>1)]}</h2>};
1315     print $r;
1316     }
1317     if ($rl) {
1318     print qq(<div id="wikipage-referer"><h2>@{[&Resource('Referers',escape=>1)]}</h2>\n$rl</div>\n);
1319     }
1320     &print_footer ($page);
1321     }
1322    
1323     my %_Resource;
1324     sub Resource ($;%) {
1325     my ($s, %o) = @_;
1326     unless (defined $_Resource{$s}) {
1327     $_Resource{$_[0]} = &wiki::resource::get ($s, $_Resource{__option});
1328     }
1329     $o{escape} ? &escape ($_Resource{$s}) : $_Resource{$s};
1330     }
1331    
1332 wakaba 1.27 package wiki::referer;
1333     sub add ($$) {
1334     my $page = shift;
1335     my $uri = shift;
1336     unless (ref $uri) {
1337     require URI;
1338     $uri = URI->new ($uri);
1339     ## Some schemes do not have query part.
1340     eval q{ $uri->query (undef) if $uri->query =~ /^[0-9]{6,8}$/ };
1341     $uri->fragment (undef);
1342     }
1343     $uri = $uri->canonical;
1344     return unless $uri;
1345     for my $regex (&get_dont_record) {
1346     return if $uri =~ /$regex/;
1347     }
1348     my %list = get ($page);
1349     $list{ $uri }++;
1350     set ($page, \%list);
1351     }
1352     sub get ($) {
1353     my $page = shift;
1354     split /"/, main::get_info ($page, 'Referer');
1355     }
1356     sub set ($%) {
1357     my $page = shift;
1358     my $list = shift;
1359     main::set_info ($page, Referer => join '"', %$list);
1360     }
1361    
1362     sub get_dont_record () {
1363     map {s/\$/\\\$/g; s/\@/\\\@/g; $_}
1364     grep !/^#/,
1365     split /[\x0D\x0A]+/, &main::__get_database ('RefererDontRecord');
1366     }
1367     sub get_site_name () {
1368     my @lines = grep /[^#]/, split /[\x0D\x0A]+/, &main::__get_database('RefererSiteName');
1369 wakaba 1.28 my @item;
1370 wakaba 1.27 for (@lines) {
1371     next if /^#/;
1372     my ($uri, $name) = split /\s+/, $_, 2;
1373     $uri =~ s/\$/\\\$/g; $uri =~ s/\@/\\\@/g; $uri =~ s/\//\\\//g;
1374 wakaba 1.28 $name =~ s!([()/\\])!\\$1!g; $name =~ s/\$([0-9]+)/).__decode (\${$1}).q(/g;
1375     push @item, [$uri, qq(q($name))];
1376 wakaba 1.27 }
1377 wakaba 1.28 @item;
1378 wakaba 1.27 }
1379    
1380     sub list_html ($) {
1381     my $page = shift;
1382     my %list = get ($page);
1383     my $r = '';
1384 wakaba 1.28 my @name = get_site_name;
1385 wakaba 1.27 for my $uri (sort keys %list) {
1386     my $title;
1387 wakaba 1.28 for my $item (@name) {
1388     if ($uri =~ /$item->[0]/) {
1389 wakaba 1.27 $title = $uri;
1390 wakaba 1.28 eval qq{\$title =~ s/^.*$item->[0].*\$/$item->[1]/e}
1391     or die $@ ;#. qq{\$title =~ s/^.*$item->[0].*\$/$item->[1]/e};
1392 wakaba 1.27 last;
1393     }
1394     }
1395     my $euri = main::escape ($uri);
1396     if ($title) {
1397 wakaba 1.36 $r .= qq(<li>[$list{$uri}] <a href="$euri" title="URI: &lt;$euri&gt;">@{[main::escape ($title)]}</a></li>\n);
1398 wakaba 1.27 } else {
1399     $r .= qq(<li>[$list{$uri}] &lt;<a href="$euri">$euri</a>&gt;</li>\n);
1400     }
1401     }
1402     $r ? qq(<ul>$r</ul>\n) : '';
1403     }
1404    
1405     sub __decode ($) {
1406     my $s = shift;
1407     $s =~ tr/+/ /;
1408     $s =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge;
1409     main::code_convert (\$s);
1410 wakaba 1.32 }
1411    
1412     package wiki::useragent;
1413 w 1.45 our $UseLog;
1414 wakaba 1.32
1415     sub add ($) {
1416     my $s = shift;
1417     return unless length $s;
1418 w 1.45 return unless $UseLog;
1419 wakaba 1.39 $s =~ s/([\x00-\x08\x0A-\x1F\x25\x7F-\xFF])/sprintf '%%%02X', unpack 'C', $1/ge;
1420 wakaba 1.32 my %ua;
1421 w 1.45 for (split /\n/, &main::__get_database($main::PageName{UserAgentList})) {
1422 wakaba 1.32 if (/^-\[(\d+)\] (.+)$/) {
1423     my ($t, $n) = ($1, $2);
1424     $n =~ tr/\x0A\x0D//d;
1425     $ua{$n} = $t;
1426     }
1427     }
1428     $ua{$s}++;
1429     my $s = qq(#?SuikaWiki/0.9\n);
1430     for (sort {$ua{$a} <=> $ua{$b}} keys %ua) {
1431     $s .= sprintf qq(-[%d] %s\n), $ua{$_}, $_;
1432     }
1433 w 1.46 $main::database->STORE ($main::PageName{UserAgentList} => $s, -touch => 0);
1434 wakaba 1.16 }
1435 wakaba 1.33
1436 wakaba 1.41 package wiki::suikawikiconst;
1437    
1438     sub to_hash ($;$) {
1439     my $page = shift;
1440     my $h = shift || {};
1441     my $val;
1442     for my $line (split /\n/, $page) {
1443     $line =~ tr/\x0A\x0D//d;
1444     if ($val && $line =~ s/^\s+//) {
1445     $h->{$val} .= length $h->{$val} ? "\n" . $line : $line;
1446     } elsif ($line =~ /^(.+):/) {
1447     $val = $1; $h->{$val} = '';
1448     }
1449     }
1450     $h;
1451     }
1452    
1453 wakaba 1.33 package SuikaWiki::Plugin;
1454 wakaba 1.35 our $plugin_directory; # defined in top of this file.
1455 wakaba 1.33 our %List;
1456    
1457     sub escape ($$) { main::escape ($_[1]) }
1458     sub unescape ($$) { main::unescape ($_[1]) }
1459     sub encode ($$) { main::encode ($_[1]) }
1460     sub decode ($$) { main::decode ($_[1]) }
1461     sub __get_datetime ($) { main::get_now () }
1462 wakaba 1.42 sub resource ($$;%) { shift; &main::Resource (@_) }
1463     sub uri ($$) { $main::uri{$_[1]} }
1464 wakaba 1.33
1465     sub regist ($@) {
1466     my $pack = shift;
1467     for (@_) {
1468     push @{$List{$_}}, $pack;
1469     }
1470     }
1471    
1472 wakaba 1.35 sub import_plugins () {
1473     opendir PDIR, $plugin_directory;
1474     my @plugin = grep {s/\.pm$//} readdir (PDIR);
1475     closedir PDIR;
1476     for (@plugin) {
1477     eval qq{ use SuikaWiki::Plugin::$_ } unless /[^A-Za-z0-9_]/;
1478     push @{$List{_all}}, qq(SuikaWiki::Plugin::$_);
1479     }
1480     }
1481    
1482     &import_plugins ();
1483    
1484 wakaba 1.41 package wiki::conneg;
1485    
1486     ## BUG: this parser isn't strict.
1487     sub get_accept_lang (;$) {
1488     my $alang = shift || $main::ENV{HTTP_ACCEPT_LANGUAGE};
1489     my %alang = (ja => 0.0002, en => 0.0001);
1490     my $i = 0.1;
1491     for (split /\s*,\s*/, $alang) {
1492     tr/\x09\x0A\x0D\x20//d;
1493     if (/((?:(?!;q=).)+)(?:;q="?([0-9.]+)"?)?/) {
1494     my $l = lc $1; $l =~ tr/\x22\x5C//d;
1495     $alang{$l} = (defined $2 ? $2 : 1.000)*1000;
1496     $alang{$l} += $i unless $alang{$l} == 0;
1497     $i -= 0.001;
1498     }
1499     }
1500     \%alang;
1501     }
1502    
1503     package wiki::resource;
1504    
1505     sub get ($;\%) {
1506     my ($resname, $option) = @_;
1507     $option->{accept_language} ||= &wiki::conneg::get_accept_lang ();
1508     $option->{resource} ||= {};
1509     my $v;
1510     for my $lang (sort {$option->{accept_language}->{$b} <=> $option->{accept_language}->{$a}} grep {$option->{accept_language}->{$_}!=0} keys %{$option->{accept_language}}) {
1511     while (length $lang) {
1512     unless ($option->{accept_language}->{defined $option->{accept_language}->{$lang} ? $lang : '*'} == 0) {
1513     $option->{resource}->{$lang} ||= &wiki::suikawikiconst::to_hash (&main::__get_database('WikiResource:'.$lang));
1514     $v = $option->{resource}->{$lang}->{$resname};
1515     last if defined $v;
1516     }
1517     $lang =~ s/[^+-]*$//; $lang =~ s/[+-]$//;
1518     }
1519     last if defined $v;
1520     }
1521     defined $v ? $v : $resname;
1522     }
1523    
1524     package wiki::map;
1525    
1526     sub make_list ($;%) {
1527     my ($page, %option) = @_;
1528     $option{level} ||= 3;
1529     my %weight;
1530     my $content = &main::__get_database ($page);
1531     $content =~ s{^\#\?([^\x0A\x0D]+)}{
1532     if ($1 =~ /import="([^"]+)"/) {
1533     for (split /\s*,\s*/, $1) {
1534     $weight{$_} += 2;
1535     }
1536     }
1537     $&;
1538     }ges;
1539     ## Bug: this code does not support content type.
1540     $content =~ s{\[\[((?!\#)[^]]+)\](?:>>\d+)?\]}{
1541     $weight{$1}++; $&;
1542     }ge;
1543     delete $weight{$page}; ## Delete myself
1544     for my $page (keys %weight) {
1545     my $w = ($content =~ s/\Q$page\E/$&/g);
1546     $weight{$page} += $w + $weight{$page}; ## Weight of [[name]] is x2.
1547     ($weight{$page} *= 0.1, $option{not_exist}->{$page} = 1) unless &main::is_exist_page ($page);
1548     }
1549     $option{weight_list}->{$page} = \%weight;
1550     if (--$option{level}) {
1551     for my $page (keys %weight) {
1552     &make_list ($page, %option) unless $option{weight_list}->{$page};
1553     }
1554     }
1555     $option{weight_list};
1556     }
1557    
1558     sub list_to_html ($$;%) {
1559     my ($Page, $wlist, %option) = @_;
1560     my $r = '';
1561     $option{outputed}->{$Page} = 1;
1562     for my $page (sort {$wlist->{$Page}->{$b} <=> $wlist->{$Page}->{$a}} keys %{$wlist->{$Page}}) {
1563     $r .= qq(<li><span class="weight">[@{[0+$wlist->{$Page}->{$page}]}]</span> <a href="$main::url_cgi?@{[&main::encode($page)]}" class="wiki@{[$option{not_exist}->{$page}?' not-exist':'']}">@{[&main::escape ($page).($option{not_exist}->{$page}?qq(<span class="mark">@{[&main::Resource('JumpAndEditWikiPageMark',escape=>1)]}</span>):'')]}</a> <a href="$main::url_cgi?mycmd=map;mypage=@{[&main::encode($page)]}" class="wiki-cmd map-from-here" title="@{[&main::escape($option{map_from_here_description})]}">@{[&main::escape($option{map_from_here})]}</a> <span class="summary">@{[&main::escape(&main::get_subjectline($page))]}</span>);
1564     unless ($option{outputed}->{$page}) {
1565     $r .= &list_to_html ($page, $wlist, %option);
1566     }
1567     $r .= "</li>\n";
1568     }
1569     $r ? qq(<ul class="map">$r</ul>) : '';
1570     }
1571    
1572 wakaba 1.35 package main;
1573     &main;
1574     exit 0;
1575 wakaba 1.9
1576 wakaba 1.16 1;
1577     __END__
1578     =head1 NAME
1579 wakaba 1.9
1580 w 1.46 wiki.cgi --- SuikaWiki: Yet yet another Wiki engine
1581 wakaba 1.9
1582 w 1.46 =head1 AUTHORS
1583 wakaba 1.9
1584 w 1.46 Hiroshi Yuki <hyuki@hyuki.com> <http://www.hyuki.com/yukiwiki/>
1585 wakaba 1.9
1586 w 1.46 Makio Tsukamoto <http://digit.que.ne.jp/>
1587 wakaba 1.9
1588 w 1.46 Wakaba <w@suika.fam.cx>
1589 wakaba 1.9
1590 wakaba 1.16 =head1 LICENSE
1591 wakaba 1.9
1592 w 1.46 Copyright (C) 2000-2003 AUTHORS
1593 wakaba 1.9
1594 wakaba 1.16 This program is free software; you can redistribute it and/or
1595     modify it under the same terms as Perl itself.
1596 wakaba 1.9
1597     =cut

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24