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

Contents of /suikawiki/script/wiki.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.47 - (hide annotations) (download)
Fri Jan 3 03:27:30 2003 UTC (21 years, 10 months ago) by w
Branch: MAIN
Changes since 1.46: +36 -68 lines
YukiWikiDBMeta support

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24