/[pub]/suikawiki/script/lib/suikawiki.pl
Suika

Contents of /suikawiki/script/lib/suikawiki.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Thu Mar 27 04:28:41 2003 UTC (21 years, 8 months ago) by wakaba
Branch: MAIN
File MIME type: text/plain
New.

1 wakaba 1.1 # -*- perl -*-
2     use strict;
3    
4     package main;
5     binmode STDOUT; binmode STDIN;
6     require Yuki::YukiWikiCache;
7     use Fcntl;
8     require SuikaWiki::Plugin;
9     our %fmt; ## formatter objects
10     our %embed_command = (
11     searched => '^\[\[#searched:([^\]]+)\]\]$',
12     form => qr/\[\[\#form(?:\(([A-Za-z0-9-]+)\))?:'((?:[^'\\]|\\.)*)':'((?:[^'\\]|\\.)*)'(?::'((?:[^'\\]|\\.)*)')?\]\]/,
13     );
14     our ($modifier_dbtype,$url_cgi,%uri,%PathTo);
15     our (%PageName,$kanjicode,$lang,%ViewDefinition);
16    
17     my %form;
18     our %database;
19     our $database = bless {}, 'wiki::dummy';
20     my %interwiki;
21     my %command_do = (
22     default => \&do_view,
23     adminchangepassword => \&do_adminchangepassword,
24     write => \&do_write,
25     searchform => \&do_searchform,
26     comment => \&do_comment,
27     RandomJump => \&do_random_jump,
28     wikiform => \&do_wikiform,
29     );
30     our $UA = ''; ## User agent name
31     $| = 1;
32    
33     sub main {
34     $UA = $main::ENV{HTTP_USER_AGENT};
35     &open_db;
36     &init_form;
37     if ($command_do{$form{mycmd}}) {
38     &{$command_do{$form{mycmd}}};
39     } else {
40     &{$command_do{default}};
41     }
42     &close_db;
43     }
44    
45     sub do_view {
46     my $content = $database{$form{mypage}};
47     my $lm = $database->mtime ($form{mypage});
48     wiki::referer::add ($form{mypage}, $ENV{HTTP_REFERER});
49     wiki::useragent::add ($ENV{HTTP_USER_AGENT});
50     &load_formatter ('view');
51     my $view = $form{mycmd};
52     if ($view eq 'edit') {
53     $view = 'adminedit' if $form{admin};
54     } elsif ($view =~ /[^0-9A-Za-z_]/) {
55     $view = 'default'
56     }
57     if ($view eq 'default' || !$view) {
58     ## BUG: this code is not strict
59     if ($main::ENV{HTTP_COOKIE} =~ /SelectedMode=([0-9A-Za-z]+)/) {
60     $view = $1;
61     } else {
62     $view = 'read';
63     }
64     }
65     my ($magic, $content) = &SuikaWiki::Plugin::magic_and_content (undef, $content);
66     $magic ||= '#?SuikaWiki/0.9';
67     my $o = bless {param => \%form, page => $form{mypage}, toc => [],
68     magic => $magic, content => $content,
69     formatter => $fmt{view}, &_compatible_options ()}, 'SuikaWiki::Plugin';
70     if (!ref $ViewDefinition{$view} || !&{$ViewDefinition{$view}->{check}} ($o)) {
71     print "Status: 406 Unsupported Media Type\n";
72     $view = '-UnsupportedMediaType';
73     }
74     my $media = $ViewDefinition{$view}->{media};
75     if ($ViewDefinition{$view}->{xmedia} && $UA =~ /Gecko/) {
76     $media = $ViewDefinition{$view}->{xmedia};
77     $o->{media} = $media;
78     } elsif ($UA =~ m#Mozilla/0\..+Windows#) {
79     $kanjicode = 'shift_jis';
80     }
81     if ($magic =~ m!^\#\?SuikaWiki/0.9!) {
82     &print_header ($form{mypage}, -last_modified => ($magic =~ /interactive="yes"/ ? time : $lm),
83     -expires => ($magic =~ /interactive="yes"/ ? 1 : undef), o => $o,
84     -media => $media, -magic => $magic, content => $content);
85     } else {
86     &print_header($form{mypage}, -media => $media,
87     -magic => $magic, -last_modified => $lm, o => $o);
88     }
89     if ($kanjicode ne 'euc') {
90     my $s = $fmt{view}->replace ($ViewDefinition{$view}->{template} => $o);
91     print &code_convert (\$s => $kanjicode);
92     } else {
93     print $fmt{view}->replace ($ViewDefinition{$view}->{template} => $o);
94     }
95     }
96    
97     sub _do_view_msg (%) {
98     my %option = @_;
99     &load_formatter ('view');
100     my $o = bless {param => \%form, page => $option{-page}, toc => [], condition => \%option,
101     formatter => $fmt{view}, &_compatible_options ()}, 'SuikaWiki::Plugin';
102     unless (&{$ViewDefinition{$option{-view}}->{check}} ($o)) {
103     print "Status: 406 Unsupported Media Type\n";
104     $option{-view} = '-UnsupportedMediaType';
105     }
106     my $media = $ViewDefinition{$option{-view}}->{media};
107     if ($ViewDefinition{$option{-view}}->{xmedia} && $UA =~ /Gecko/) {
108     $media = $ViewDefinition{$option{-view}}->{xmedia};
109     $o->{media} = $media;
110     }
111     &print_header($option{-page}, -media => $media, o => $o, -goto => $option{-goto});
112     print $fmt{view}->replace ($ViewDefinition{$option{-view}}->{template} => $o);
113     }
114    
115     sub id_and_name ($) {
116     my $name = shift;
117     if ($UA =~ m#Mozilla/[12]\.|Microsoft Internet Explorer#) {
118     qq{id="$name"><a name="$name"></a};
119     } else {
120     qq{id="$name"};
121     }
122     }
123    
124     sub do_adminchangepassword {
125     if ($form{mynewpassword} ne $form{mynewpassword2}) {
126     &_do_view_msg (-view => '-error', -page => $form{mypage},
127     error_message => &Resource ('Error:PasswordMismatch'));
128     return;
129     }
130     my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage});
131     if ($validpassword_crypt) {
132     if (not &valid_password($form{myoldpassword})) {
133     &_do_view_msg (-view => '-error', -page => $form{mypage},
134     error_message => &Resource ('Error:PasswordIsIncorrect'));
135     return;
136     }
137     }
138     my ($sec, $min, $hour, $day, $mon, $year, $weekday) = localtime(time);
139     my (@token) = ('0'..'9', 'A'..'Z', 'a'..'z');
140     my $salt1 = $token[(time | $$) % scalar(@token)];
141     my $salt2 = $token[($sec + $min*60 + $hour*60*60) % scalar(@token)];
142     my $crypted = crypt($form{mynewpassword}, "$salt1$salt2");
143     $database->meta (AdminPassword => $PageName{AdminSpecialPage} => $crypted);
144    
145     &_do_view_msg (-view => '-wrote', -page => $form{mypage});
146     }
147    
148     sub valid_password ($) {
149     my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage});
150     return crypt (shift, $validpassword_crypt) eq $validpassword_crypt ? 1 : 0;
151     }
152    
153     sub do_write {
154     if (&frozen_reject()) {
155     return;
156     }
157    
158     if (not &is_editable($form{mypage})) {
159     &_do_view_msg (-view => '-error', -page => $form{mypage},
160     error_message => &Resource ('Error:ThisPageIsUneditable'));
161     return;
162     }
163    
164     ## Check confliction
165     if ($form{myLastModified} ne $database->mtime ($form{mypage})) {
166     &_do_view_msg (-view => '-conflict', -page => $form{mypage});
167     return;
168     }
169    
170     if ($form{mymsg}) {
171     if ($form{mytouch} || !ref $database) {
172     $database{$form{mypage}} = $form{mymsg};
173     } else {
174     $database->STORE ($form{mypage} => $form{mymsg}, -touch => 0);
175     }
176     $database->meta (IsFrozen => $form{mypage} => 0 + $form{myfrozen});
177     my $fragment = '';
178     $fragment .= qq(;after_edit_cmd=@{[&encode($form{after_edit_cmd})]}) if $form{after_edit_cmd};
179     if ($form{__comment_anchor_index}) {
180     $fragment .= qq(#anchor-$form{__comment_anchor_index});
181     } elsif ($form{__wikiform_anchor_index}) {
182     $fragment .= qq(#wikiform-$form{__wikiform_anchor_index});
183     }
184     &_do_view_msg (-view => '-wrote', -page => $form{mypage}, -goto => $url_cgi.'?mycmd='.&encode($form{after_edit_cmd}||'default').';mypage='.&encode($form{mypage}).qq(;x-param=@{[time.[0..9]->[rand 10]]}$fragment));
185     } else {
186     delete $database{$form{mypage}};
187     &_do_view_msg (-view => '-deleted', -page => $form{mypage});
188     }
189     }
190    
191     sub _compatible_options () {
192     (use_anchor_name => ($UA =~ m#Mozilla/[12]\.|Microsoft Internet Explorer# ? 1 : 0));
193     }
194    
195     sub get_search_result ($;%) {
196     my $word = lc shift;
197     my $SearchResult = SuikaWiki::Plugin->cache ('search');
198     my %option = @_;
199     my @r;
200     unless (defined $SearchResult->{$word}) {
201     for my $page (keys %database) {
202     next if !$option{-match_myself} && ($page eq $word);
203     my $content = lc $database{$page};
204     $content =~ s/^[^\x0A\x0D]+[\x0D\x0A]+//s;
205     if (index (lc $page, $word) > -1) {
206     my $c = $content =~ s/\Q$word\E//g;
207     push @r, [$page, $c+20];
208     } elsif (index ($word, lc $page) > -1) {
209     my $c = $content =~ s/\Q$word\E//g;
210     push @r, [$page, $c+10];
211     } elsif (my $c = $content =~ s/\Q$word\E//g) {
212     push @r, [$page, $c];
213     }
214     }
215     @r = sort {$b->[1] <=> $a->[1] || $a->[0] cmp $b->[0]} @r;
216     $SearchResult->{$word} = join "\x1E", map {$_->[0]."\x1F".$_->[1]} @r;
217     } else {
218     @r = map {[split /\x1F/, $_, 2]} split /\x1E/, $SearchResult->{$word};
219     }
220     #my $em = sub { my $s = shift; $s =~ s#(\Q$word\E)#<em>$1</em>#gi; $s };
221     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>)} @r;
222     $r = qq|<ul class="search-result">$r</ul>| if $r;
223     wantarray? ($r, scalar @r): $r;
224     }
225    
226     sub do_random_jump {
227     my @list = keys %database;
228     my $name = &encode ($list[rand @list]);
229     print "Location: $uri{wiki}?$name\n";
230     print "\n";
231     }
232    
233     sub print_header ($;%) {
234     my ($page, %option) = @_;
235     my @head;
236     $option{o}->{-header}->{class} = &is_frozen($page) ? 'frozen' : '';
237     $option{o}->{-header}->{class} .= " wiki-page-obsoleted" if $option{-magic} =~ /obsoleted="yes"/;
238     if ($option{-goto}) {
239     if ($UA =~ m#Opera|MSIE 2\.#) {
240     ## WARNING: This code may output unsafe HTML document if
241     ## $option{-goto} is not clean.
242     $option{-goto} =~ tr/;/&/ if $UA =~ m#Opera#;
243     print qq{Refresh: 0; url=$option{-goto}\n};
244     push @head, qq(<meta http-equiv="refresh" content="0; url=$option{-goto}">);
245     } elsif ($UA =~ /Gecko/) {
246     print qq{Refresh: 0; url="$option{-goto}"\n};
247     push @head, qq(<meta http-equiv="refresh" content="0; url=&quot;@{[&escape($option{-goto})]}&quot;" />);
248     } else {
249     $option{-goto} =~ tr/;/&/ if $UA =~ m#Mozilla/[1-4]\.#;
250     print qq{Refresh: 0; url="$option{-goto}"\n};
251     push @head, qq(<meta http-equiv="refresh" content="0; url=&quot;@{[&escape($option{-goto})]}&quot;">);
252     }
253     }
254     print qq{Last-Modified: @{[scalar gmtime $option{-last_modified}]}\n} if $option{-last_modified};
255     if ($option{-expires} != -1) {
256     if (defined $option{-expires}) { ## TODO: Don't use asctime
257     print qq{Expires: @{[scalar gmtime (time + $option{-expires})]}\n};
258     } elsif ($option{-media}->{expires} != -1) {
259     print qq{Expires: @{[scalar gmtime (time + $option{-media}->{expires})]}\n};
260     }
261     }
262     if ($option{-media}->{charset} && $UA =~ m#Mozilla/[12]\.#) {
263     my $ct = qq{$option{-media}->{type}; charset=@{[&get_charset_name($kanjicode,compatible=>1)]}};
264     print qq{Content-Type: $ct\n};
265     $option{o}->{-header}->{meta_ct} = qq{<meta http-equiv="content-type" content="$ct">\n};
266     } elsif (!$option{-media}->{charset} || $UA =~ m#Infomosaic|Mozilla/0\.#) {
267     print qq{Content-Type: $option{-media}->{type}\n};
268     $option{o}->{-header}->{meta_ct} = qq{<meta http-equiv="content-type" content="$option{-media}->{type}; charset=@{[&get_charset_name($kanjicode,compatible=>1)]}">\n};
269     } else {
270     my $type = $option{-media}->{type};
271     $type = 'application/xml' if $type eq 'application/rss+xml';
272     print qq{Content-Type: $type; charset=@{[&get_charset_name($kanjicode)]}\n};
273     }
274     print <<"EOD"; ## TODO:
275     Content-Language: $lang
276     Content-Style-Type: text/css
277    
278     EOD
279     $option{o}->{-header}->{links} = join "\n", (@head);
280     }
281    
282     sub get_charset_name ($;%) {
283     my ($charset, %option) = (lc shift, @_);
284     if ($charset =~ 'euc') {
285     $charset = $option{compatible} ? 'x-euc-jp' : 'euc-jp';
286     } elsif ($charset =~ 'sjis' || $charset =~ 'shift') {
287     $charset = $option{compatible} ? 'x-sjis' : 'shift_jis';
288     } elsif ($charset =~ 'jis') {
289     $charset = 'iso-2022-jp';
290     }
291     $charset;
292     }
293    
294     sub escape {
295     my $s = shift;
296     $s =~ s|\x0D\x0A|\x0A|g;
297     $s =~ s|&|&amp;|g;
298     $s =~ s|<|&lt;|g;
299     $s =~ s|>|&gt;|g;
300     $s =~ s|"|&quot;|g;
301     return $s;
302     }
303    
304     sub unescape {
305     my $s = shift;
306     # $s =~ s|\n|\r\n|g;
307     $s =~ s|&lt;|<|g;
308     $s =~ s|&gt;|>|g;
309     $s =~ s|&quot;|"|g;
310     $s =~ s|&amp;|&|g;
311     return $s;
312     }
313    
314     sub convert_format ($$$;%) {
315     my ($content, $d => $t, %option) = @_;
316     my $f = SuikaWiki::Plugin->format_converter ($d => $t);
317     if (ref $f) {
318     $option{content} = $content;
319     $option{from} = $d;
320     $option{to} = $t;
321     &$f ({}, bless (\%option, 'SuikaWiki::Plugin'));
322     } elsif ($option{-error_no_return}) {
323     return undef;
324     } elsif ($t =~ /HTML|xml/) {
325     length $content ? '<pre>'.&escape($content).'</pre>' : '';
326     } else {
327     $content;
328     }
329     }
330    
331     sub text_to_html {
332     my ($txt, %option) = @_;
333     my $toc = $option{-toc} || (ref $option{toc} ? $option{toc} : []);
334     my $tocnum = 0;
335    
336     ## Load constants
337     my %const;
338     if ($option{magic} =~ /import="([^"]+)"/) {
339     for (split /\s*,\s*/, $1) {
340     my $wp = $database{$_};
341     if ($wp =~ m!^\#\?SuikaWikiConst/(?:0.9|1.0)!) {
342     wiki::suikawikiconst::to_hash ($wp => \%const);
343     }
344     }
345     }
346    
347     $txt =~ s{__&&([^&]+)&&__}{defined $const{$1}?$const{$1}:qq(__&&$1&&__)}ge;
348     my (@txt) = split(/\n/, $txt);
349     my (@saved, @result);
350     unshift(@saved, "</p>");
351     push(@result, "<p>");
352     foreach (@txt) {
353     chomp;
354     if (/^\*\*\*\*\*([^\x0D\x0A]*)/) {
355     push @$toc, [5, "i$tocnum" => ($1 || $tocnum)];
356     push(@result, splice(@saved), qq(<h6 @{[&id_and_name("i$tocnum")]}>) . &inline($1, %option, const => \%const) . '</h6>');
357     $tocnum++;
358     } elsif (/^\*\*\*\*([^\x0D\x0A]*)/) {
359     push @$toc, [4, "i$tocnum" => ($1 || $tocnum)];
360     push(@result, splice(@saved), qq(<h5 @{[&id_and_name("i$tocnum")]}>) . &inline($1, %option, const => \%const) . '</h5>');
361     $tocnum++;
362     } elsif (/^\*\*\*([^\x0D\x0A]*)/) {
363     push @$toc, [3, "i$tocnum" => ($1 || $tocnum)];
364     push(@result, splice(@saved), qq(<h4 @{[&id_and_name("i$tocnum")]}>) . &inline($1, %option, const => \%const) . '</h4>');
365     $tocnum++;
366     } elsif (/^\*\*([^\x0D\x0A]*)/) {
367     # if (/^\*\*(.*)/) {
368     # Walrus mod (6) end
369     push @$toc, [2, "i$tocnum" => ($1 || $tocnum)];
370     push(@result, splice(@saved), qq(<h3 @{[&id_and_name("i$tocnum")]}>) . &inline($1, %option, const => \%const) . '</h3>');
371     $tocnum++;
372     } elsif (/^\*([^\x0D\x0A]*)/) {
373     push @$toc, [1, "i$tocnum" => ($1 || $tocnum)];
374     push(@result, splice(@saved), qq(<h2 @{[&id_and_name("i$tocnum")]}>) . &inline($1, %option, const => \%const) . '</h2>');
375     $tocnum++;
376     } elsif (/^(={1,6})(.*)/) {
377     &back_push('ol', length($1), \@saved, \@result);
378     push(@result, '<li>' . &inline($2, %option, const => \%const) . '</li>');
379     } elsif (/^(-{1,6})(.*)/) {
380     &back_push('ul', length($1), \@saved, \@result);
381     my ($pf, $l) = ('', $2);
382     if (!$main::_EMBEDED && $l =~ s/^\s*\[([0-9]+)\]//) {
383     my $num = 0+$1;
384     $pf = qq(<a name="anchor-$num" id="anchor-$num" class="anchor">[$num]</a>);
385     }
386     push(@result, '<li>' . $pf . &inline ($l, %option, const => \%const) . '</li>');
387     } elsif (/^:([^:]+):(.*)/) {
388     &back_push('dl', 1, \@saved, \@result);
389     push(@result, '<dt>' . &inline($1, %option, const => \%const) . '</dt>', '<dd>' . &inline($2, %option, const => \%const) . '</dd>');
390     } elsif (/^(?!>>\d)(>{1,5})(.*)/) {
391     &back_push('blockquote', length($1), \@saved, \@result);
392     push @result, "<p>";
393     push(@result, &inline($2, %option, const => \%const));
394     unshift @saved, "</p>";
395     } elsif (/^\s*$/) {
396     push(@result, splice(@saved));
397     push(@result, "<p>");
398     unshift(@saved, "</p>");
399     } elsif (/^(\s+.*)$/) {
400     &back_push('pre', 1, \@saved, \@result);
401     push(@result, &inline($1, %option, const => \%const));
402     } elsif (/^\,(.*?)[\x0D\x0A]*$/) {
403     &back_push('table', 1, \@saved, \@result);
404     #######
405     # This part is taken from Mr. Ohzaki's Perl Memo and Makio Tsukamoto's WalWiki.
406     # XXXXX
407     my $tmp = "$1,";
408     my @value = map {/^"(.*)"$/ ? scalar($_ = $1, s/""/"/g, $_) : $_} ($tmp =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g);
409     my @align = map {(s/^\s+//) ? ((s/\s+$//) ? ' align="center"' : ' align="right"') : ''} @value;
410     my @colspan = map {($_ eq '==') ? 0 : 1} @value;
411     for (my $i = 0; $i < @value; $i++) {
412     if ($colspan[$i]) {
413     while ($i + $colspan[$i] < @value and $value[$i + $colspan[$i]] eq '==') {
414     $colspan[$i]++;
415     }
416     $colspan[$i] = ($colspan[$i] > 1) ? sprintf(' colspan="%d"', $colspan[$i]) : '';
417     $value[$i] = sprintf('<td%s%s>%s</td>', $align[$i], $colspan[$i], &inline($value[$i], %option, const => \%const));
418     } else {
419     $value[$i] = '';
420     }
421     }
422     push(@result, join('', '<tr>', @value, '</tr>'));
423     # XXXXX
424     #######
425     } elsif (/^\[(INS|DEL|PRE)\[\s*$/) {
426     push @result, splice (@saved), '<'.lc($1).'>';
427     unshift @saved, "</p>";
428     push @result, "<p>";
429     } elsif (/^\](INS|DEL|PRE)\]\s*$/) {
430     push @result, splice (@saved), '</'.lc($1).'>';
431     } elsif (/^\[([0-9]+)\](.*)$/ && !$main::_EMBEDED) {
432     my $num = 0+$1;
433     push @result, qq(<a name="anchor-$num" id="anchor-$num" class="anchor">[$num]</a>);
434     push @result, &inline ($2, %option, const => \%const);
435     } else {
436     push(@result, &inline($_, %option, const => \%const));
437     }
438     }
439     push(@result, splice(@saved));
440    
441     my $r = join("\n", @result);
442     $r =~ s#<p>\x0D?\x0A</p>##g;
443     $r =~ s#[\x0D\x0A]+</#</#g;
444     $r =~ s#<pre>\x0D?\x0A#<pre>#g;
445     $r;
446     }
447    
448     sub back_push {
449     my ($tag, $level, $savedref, $resultref, $attr) = @_;
450     while (@$savedref > $level) {
451     push(@$resultref, shift(@$savedref));
452     }
453     if ($savedref->[0] ne "</$tag>") {
454     push(@$resultref, splice(@$savedref));
455     }
456     while (@$savedref < $level) {
457     unshift(@$savedref, "</$tag>");
458     push(@$resultref, "<$tag$attr>");
459     }
460     }
461    
462     sub inline ($;%) {
463     my ($line, %option) = @_;
464     $line = &escape($line);
465     $line =~ s{$embed_command{form}}{&make_custom_form ($1, $2, $3, $4, \%option)}ge;
466     $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;
467     $line =~ s:\[(WEAK)\[(.+?)\]\]:<span class="@{[lc $1]}">$2</span>:g;
468     $line =~ s:\[ABBR\[([^]]+)\] \[([^]]+)\]\]:<acronym title="$2">$1</acronym>:g;
469     $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;
470     $line =~ s:\[RUBY\[([^]]+)\] \[([^]]+)\]\]:<ruby><rb>$1</rb><rp>(</rp><rt>$2</rt><rp>)</rp></ruby>:g;
471     $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;
472     $line =~ s|'''([^']+)'''|<strong>$1</strong>|g;
473     $line =~ s|''([^']+)''|<em>$1</em>|g;
474     $line =~ s{
475     (\[\[(\#\S+?)\]\])
476     |\[\[([^[]+?)](?:&gt;&gt;([0-9]+))?]
477     |&gt;&gt;([0-9]+)
478     |&lt;([A-Za-z0-9%]+:(?:(?!&gt;).)+)&gt;
479     }{
480     my ($l, $page,$anchor, $anum, $uri) = ($1, $3,$4, 0+$5, $6);
481     if ($l) {
482     &embedded_to_html($1);
483     } elsif (defined $page) {
484     &make_wikilink (&unescape ($page), anchor => 0+$anchor);
485     } elsif ($anum) {
486     qq(<a href="#anchor-$anum" class="wiki-anchor">&gt;&gt;$anum</a>);
487     } elsif ($uri) {
488     &make_urilink ($uri);
489     }
490     }gex;
491     return $line;
492     }
493    
494     sub make_wikilink ($%) {
495     my ($name, %option) = @_;
496     my $ename = &escape (length $option{label} ? $option{label} : $name);
497     $option{latest} = $option{latest} ? qq(mycmd=default;x-param=@{[time.[0..9]->[rand 10]]};mypage=) : '';
498     if ($database{$name}) {
499     my $subject = &escape (&get_subjectline ($name, delimiter => ''));
500     if ($option{anchor}) {
501     return qq(<a title="$subject" href="$uri{wiki}?$option{latest}@{[&encode($name)]}#anchor-$option{anchor}" class="wiki">$ename&gt;&gt;$option{anchor}</a>);
502     } else {
503     return qq(<a title="$subject" href="$uri{wiki}?$option{latest}@{[&encode($name)]}" class="wiki">$ename</a>);
504     }
505     } else {
506     return qq(<a title="@{[&Resource('JumpAndEditWikiPage',escape=>1)]}" href="$uri{wiki}?$option{latest}@{[&encode($name)]}" class="wiki not-exist">$ename<span class="mark">@{[&Resource('JumpAndEditWikiPageMark',escape=>1)]}</span></a>);
507     }
508     }
509    
510     sub make_urilink ($;%) {
511     require URI;
512     my $uri = shift;
513     if ($uri =~ s/^IW://) { ## InterWiki (not URI)
514     $uri = &unescape ($uri);
515     if ($uri =~ /^([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+"):([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+")$/) {
516     my ($site, $name) = ($1, $2);
517     for ($site, $name) {
518     if (s/^"//) { s/"$//; s/\\(.)/$1/g }
519     }
520     &init_InterWikiName () unless $interwiki{'[[]]'};
521     if ($interwiki{$site}) {
522     &load_formatter ('interwiki');
523     my $uri = &escape ($fmt{interwiki}->replace ($interwiki{$site} => {site => $site, name => $name}));
524     $site = &escape ($site); $name = &escape ($name);
525     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;);
526     } else {
527     qq(&lt;@{[&Resource('Error:UnknownInterWikiName=',escape=>1)]}@{[&escape ($site)]}&gt;);
528     }
529     } else {
530     qq(&lt;@{[&Resource('Error:InvalidInterWiki=',escape=>1)]}@{[&escape($uri)]}&gt;);
531     }
532     } elsif ($uri =~ /^urn:/) { ## URN
533     my $uri2 = &escape (URI->new ('/uri-res/N2L?'.&unescape ($uri), 'http')->canonical);
534     qq(&lt;<a href="$uri2" title="URI: &lt;$uri&gt; (via &lt;$uri2&gt;)" class="out-of-wiki urn">$uri</a>&gt;);
535     } elsif ($uri =~ s/^MAIL://) { ## mail address (not URI)
536     my $uri2 = &escape (URI->new ('mailto:'.&unescape ($uri))->canonical);
537     qq(&lt;<a href="$uri2" class="out-of-wiki mail">$uri</a>&gt;);
538     } elsif ($uri =~ s/^IMG(?:\([^)]+\))?://) { ## image (not URI itself)
539     my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
540     qq(<img src="$uri2" alt="" title="URI: &lt;$uri2&gt;" class="out-of-wiki">);
541     } else { ## misc. URI
542     CGI::Carp::warningsToBrowser (0);
543     my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
544     CGI::Carp::warningsToBrowser (1);
545     qq(&lt;<a href="$uri2" title="URI: &lt;$uri2&gt;" class="out-of-wiki">$uri</a>&gt;);
546     }
547     }
548    
549     {my %FormIndex;
550     sub make_custom_form ($$$$%) {
551     my ($wfname, $definition, $template, $foption, $option) = @_;
552     ## $template is currently not used in this procedure.
553     #unless ($main::_EMBEDED) {
554     $FormIndex{$option->{page}}++;
555     if (length $definition) {
556     my $param = bless {depth=>10}, 'SuikaWiki::Plugin';
557     my $lastmodified = $database->mtime ($option->{page});
558     &load_formatter (qw/form_input form_option/);
559     $definition = &unescape ($definition);
560     $definition =~ s/\\(['\\])/$1/g;
561     $foption = &unescape ($foption);
562     $foption =~ s/\\(['\\])/$1/g;
563     $fmt{form_option}->replace ($foption, $param);
564     $param->{output}->{form} = 1 unless defined $param->{output}->{form};
565     $param->{output}->{form} = 0 if $main::_EMBEDED;
566     $definition .= ' %submit;' if $definition !~ /%submit/ && !$param->{output}->{nosubmit} && $param->{output}->{form};
567     $param->{output}->{page} ||= $option->{page};
568     $param->{form_disabled} = 1 if $database->meta (IsFrozen => $option->{page});
569     my $target_form = $param->{output}->{id};
570     my $r = '';
571     $r = <<EOH if $param->{output}->{form};
572     <form method="post" action="$url_cgi" id="wikiform-$FormIndex{$option->{page}}" class="wikiform">
573     <input type="hidden" name="mycmd" value="@{[$param->{form_disabled}?'default':'wikiform']}" />
574     <input type="hidden" name="mypage" value="@{[&escape($param->{output}->{page})]}" />
575     <input type="hidden" name="myLastModified" value="$lastmodified" />
576     <input type="hidden" name="mytouch" value="on" />
577     <input type="hidden" name="@{[$target_form? qq(wikiform_targetform" value="@{[&escape($target_form)]}) : qq(wikiform_index" value="$FormIndex{$option->{page}})]}" />
578     EOH
579     $r .= qq(<a name="wikiform-$FormIndex{$option->{page}}"></a>) if $UA =~ m#Mozilla/[12]\.#;
580     $r .= $fmt{form_input}->replace ($definition, $param);
581     $r .= "</form>\n" if $param->{output}->{form};
582     $r;
583     } else { ## No input-interface WikiForm
584     qq(<a id="wikiform-$FormIndex{$option->{page}}" name="wikiform-$FormIndex{$option->{page}}"><!-- #form --></a>);
585     }
586     #} else {
587     # qq(<ins class="wiki-error">@{[&Resource('Error:WikiForm:EmbedIsNotSupported',escape=>1)]}</ins>);
588     #}
589     }}
590    
591     sub init_form {
592     ## TODO: Support multipart/form-data
593     my $query = '';
594     if (uc $main::ENV{REQUEST_METHOD} eq 'POST') {
595     read STDIN, $query, $main::ENV{CONTENT_LENGTH};
596     }
597     $query .= ($query ? ';' : '') . $main::ENV{QUERY_STRING};
598     if ($main::ENV{REQUEST_METHOD} ne 'POST' && $main::ENV{QUERY_STRING} && $main::ENV{QUERY_STRING} !~ /[&;=]/) {
599     my $query = &decode($main::ENV{QUERY_STRING});
600     $query = &code_convert(\$query, $kanjicode);
601     $form{mypage} = $query;
602     $form{mycmd} = 'default';
603     } else {
604     for (split /[;&]/, $query) {
605     if (my ($n, $v) = split /=/, $_, 2) {
606     for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge};
607     $form{$n} = $v;
608     }
609     }
610     unless (defined $form{mypage}) {
611     $form{mypage} = $form{epage};
612     $form{mypage} =~ s/([0-9A-F]{2})/ord hex $1/g;
613     }
614     $form{mypage} = &code_convert (\$form{mypage}, $kanjicode);
615     }
616     $form{mypage} ||= $PageName{FrontPage};
617     $form{mypage} =~ tr/\x00-\x1F\x7F//d;
618     $form{mycmd} ||= 'default';
619    
620     # mypreview_edit -> do_edit, with preview.
621     # mypreview_adminedit -> do_adminedit, with preview.
622     # mypreview_write -> do_write, without preview.
623     foreach (keys %form) {
624     if (/^mypreview_(.*)$/) {
625     $form{mycmd} = $1;
626     $form{mypreview} = 1;
627     }
628     }
629    
630     #
631     # $form{mycmd} is frozen here.
632     #
633    
634     for (grep /^wikiform__/, keys %form) {
635     $form{$_} = &code_convert (\$form{$_}, $kanjicode);
636     }
637     $form{mymsg} = &code_convert(\$form{mymsg}, $kanjicode);
638     $form{myname} = &code_convert(\$form{myname}, $kanjicode);
639     }
640    
641     sub get_subjectline {
642     my ($page, %option) = @_;
643     my $SubjectLine = SuikaWiki::Plugin->cache ('headline');
644     unless (defined $SubjectLine->{$page}) {
645     if (not &is_editable($page)) {
646     $SubjectLine->{$page} = "";
647     } else {
648     $SubjectLine->{$page} = do {
649     my $s=$database{$page};
650     $s =~ s!^\#\?[^\x0A\x0D]+[\x0A\x0D]*!!s;
651     $s =~ s/\x0D?\x0A.*//s;
652     $s =~ s/^[-=]*\s*\[\d+\]\s*//;
653     $s =~ s/'''?//g;
654     $s =~ s/\[[A-Z]+(?:\([^)]+\))?\[([^]]+)\](?:\s\[([^]]+)\])?\]/$1$2/g;
655     $s =~ s/\[\[([^]]+)\]\]/$1/g;
656     $s};
657     }
658     }
659     if (length $SubjectLine->{$page}) {
660     $option{delimiter} = defined $option{delimiter} ? $option{delimiter} : &Resource('Title-Summary Delimiter');
661     $option{delimiter}.$SubjectLine->{$page}.$option{tail};
662     } else {
663     '';
664     }
665     }
666    
667     sub open_db {
668     if ($modifier_dbtype eq 'dbmopen') {
669     dbmopen(%database, $PathTo{WikiDataBase}, 0666) or die "(dbmopen) $PathTo{WikiDataBase}";
670     } elsif ($modifier_dbtype eq 'AnyDBM_File') {
671     eval q{use AnyDBM_File};
672     tie(%database, "AnyDBM_File", $PathTo{WikiDataBase}, O_RDWR|O_CREAT, 0666) or die ("(tie AnyDBM_File) $PathTo{WikiDataBase}");
673     } elsif ($modifier_dbtype eq 'Yuki::YukiWikiDB') {
674     eval q{use Yuki::YukiWikiDB};
675     tie(%database, "Yuki::YukiWikiDB", $PathTo{WikiDataBase}) or die ("(tie Yuki::YukiWikiDB) $PathTo{WikiDataBase}");
676     } else { ## Yuki::YukiWikiDB || Yuki::YukiWikiDBMeta
677     eval qq{use $modifier_dbtype};
678     $database = tie(%database, $modifier_dbtype => $PathTo{WikiDataBase}, -lock => 2, -backup => $wiki::diff::UseDiff) or die ("(tie $modifier_dbtype) $PathTo{WikiDataBase}");
679     }
680     }
681    
682     sub close_db {
683     if ($modifier_dbtype eq 'dbmopen') {
684     dbmclose(%database);
685     } else {
686     untie(%database);
687     }
688     }
689    
690     sub editform (@) {
691     my %option = @_;
692     my $frozen = &is_frozen ($option{page});
693     $option{content} = $database{$option{page}} unless defined $option{content};
694     $option{content} = $database{NewPageTemplate} unless length $option{content};
695     $option{last_modified} = $database->mtime ($option{page}) unless defined $option{last_modified};
696     my $f = '';
697     my $magic = '';
698     $magic = $1 if $option{content} =~ m/^([^\x0A\x0D]+)/s;
699    
700     my $selected = 'default';
701     if ($form{after_edit_cmd}) {
702     $selected = $form{after_edit_cmd};
703     } elsif ($magic =~ /Const|Config|CSS/) {
704     $selected = 'edit';
705     }
706     my $afteredit = <<EOH;
707     <select name="after_edit_cmd">
708     <option value="default" label="@{[&Resource('Edit:SaveAndDefault',escape=>1)]}"@{[$selected eq 'default' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndDefault',escape=>1)]}</option>
709     <option value="read" label="@{[&Resource('Edit:SaveAndView',escape=>1)]}"@{[$selected eq 'read' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndView',escape=>1)]}</option>
710     <option value="edit" label="@{[&Resource('Edit:SaveAndEdit',escape=>1)]}"@{[$selected eq 'edit' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndEdit',escape=>1)]}</option>
711     </select>
712     EOH
713     $f .= <<"EOD";
714     <form action="$uri{wiki}" method="post">
715     @{[ $option{conflict} ? '' : qq(<label><input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}" /><kbd>S</kbd></label>) ]}
716     @{[ $option{admin} ? qq(<label>@{[&Resource('Edit:Password=',escape=>1)]}<input type="password" name="mypassword" value="" size="10" /></label>) : "" ]} [@{[&get_new_anchor_index($option{content})]}]<br />
717     <input type="hidden" name="myLastModified" value="$option{last_modified}" />
718     <input type="hidden" name="mypage" value="@{[&escape($form{mypage})]}" />
719     <textarea cols="@{[&Resource('Edit:Form:Cols')+0||80]}" rows="@{[&Resource('Edit:Form:Rows')+0||20]}" name="mymsg" tabindex="1">@{[&escape($option{content})]}</textarea><br />
720     @{[
721     $option{admin} ?
722     qq(
723     <label><input type="radio" name="myfrozen" value="1" @{[$frozen ? qq(checked="checked") : ""]} />@{[&Resource('Edit:Freeze',escape=>1)]}</label>
724     <label><input type="radio" name="myfrozen" value="0" @{[$frozen ? "" : qq(checked="checked")]} />@{[&Resource('Edit:DontFreeze',escape=>1)]}</label><br />)
725     : ""
726     ]}
727     @{[
728     $option{conflict} ? "" :
729     qq(
730     <label><input type="checkbox" name="mytouch" value="on" checked="checked" />@{[&Resource('Edit:UpdateTimeStamp',escape=>1)]}</label><br />
731     <label><input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}" accesskey="S" /><kbd>S</kbd></label>
732     $afteredit
733     )
734     ]}
735     </form>
736     EOD
737     $f;
738     }
739    
740     sub is_editable {
741     my ($page) = @_;
742     $page =~ /[\x00-\x20\x7F]/ ? 0 : 1;
743     }
744    
745     sub decode {
746     my ($s) = @_;
747     $s =~ tr/+/ /;
748     $s =~ s/%([A-Fa-f0-9][A-Fa-f0-9])/pack("C", hex($1))/eg;
749     return $s;
750     }
751    
752     sub encode {
753     my $s = shift;
754     $s =~ s/([^0-9A-Za-z_-])/sprintf '%%%02X', ord $1/ge;
755     $s;
756     }
757    
758     sub get_now {
759     my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);
760     $year += 1900;
761     $mon++;
762     $mon = "0$mon" if $mon < 10;
763     $day = "0$day" if $day < 10;
764     $hour = "0$hour" if $hour < 10;
765     $min = "0$min" if $min < 10;
766     #$sec = "0$sec" if $sec < 10;
767     return "$year-$mon-$day $hour:$min";
768     }
769    
770     sub init_InterWikiName {
771     my @content = split /\n/, $database{InterWikiName};
772     for (@content) {
773     if (/^([^#]\S*)\s+(\S[^\x0A\x0D]+)/) {
774     $interwiki{$1} = $2;
775     }
776     }
777     $interwiki{'[[]]'} = 1; ## dummy
778     }
779    
780     sub frozen_reject {
781     my ($isfrozen) = $database->meta (IsFrozen => $form{mypage});
782     my ($willbefrozen) = $form{myfrozen};
783     if (not $isfrozen and not $willbefrozen) {
784     # You need no check.
785     return 0;
786     } elsif (valid_password($form{mypassword})) {
787     # You are admin.
788     return 0;
789     } else {
790     &_do_view_msg (-view => '-error', -page => $form{mypage},
791     error_message => &Resource ('Error:PasswordIsIncorrect'));
792     exit;
793     }
794     }
795    
796     sub is_frozen ($) { $database->meta (IsFrozen => $_[0]) ? 1 : 0 }
797    
798     sub do_comment {
799     my ($content) = $database{$form{mypage}};
800     my $default_name; ## this code is not strict.
801     $default_name = $1 if $content =~ /default-name="([^"]+)"/;
802     my $datestr = '[WEAK['.&get_now.']]';
803     my $namestr = $form{myname} || $default_name || &Resource('WikiForm:WikiComment:DefaultName');
804     ($namestr = '', $datestr = '') if $form{myname} eq 'nodate';
805     if ($namestr =~ /^(?:>>)?[0-9]/) {
806     $namestr = qq( ''$namestr'': );
807     } elsif (length $namestr) {
808     $namestr = qq( ''[[$namestr]]'': );
809     }
810     my $anchor = &get_new_anchor_index ($content);
811     my $i = 1; my $o = 0;
812     $content =~ s{(\[\[\#r?comment\]\])}{
813     my $embed = $1;
814     if ($i == $form{comment_index}) {
815     if ($embed ne '[[#rcomment]]') {
816     $embed = "- [$anchor] $datestr$namestr$form{mymsg}\n$embed"; $o = 1;
817     } else {
818     $embed .= "\n- [$anchor] $datestr$namestr$form{mymsg}"; $o = 1;
819     }
820     }
821     $i++; $embed;
822     }ge;
823     unless ($o) {
824     $content = "#?SuikaWiki/0.9\n\n" unless $content;
825     $content .= "\n" unless $content =~ /\n$/s;
826     $content .= "- [$anchor] $datestr$namestr$form{mymsg}\n";
827     }
828     $form{__comment_anchor_index} = $anchor;
829     if ($form{mymsg} || $form{myname}) {
830     $form{mymsg} = $content;
831     $form{mytouch} = 'on';
832     &do_write;
833     } else { ## Don't write
834     $form{mycmd} = 'default';
835     &do_view;
836     }
837     }
838    
839     sub get_new_anchor_index ($) {
840     my $content = shift;
841     my $anchor = 0;
842     $content =~ s/^(?:[-=]+\s*)?\[([0-9]+)\]/$anchor = $1 if $1 > $anchor; $&/mge;
843     $anchor + 1;
844     }
845    
846     my $CommentIndex = 0;
847     sub embedded_to_html {
848     my ($embedded) = @_;
849     if ($embedded eq '[[#comment]]' or $embedded eq '[[#rcomment]]') {
850     unless ($main::_EMBEDED) {
851     my $lastmodified = $database->mtime ($form{mypage});
852     return <<"EOD";
853     <form action="$url_cgi" method="post" id="x-comment-@{[++$CommentIndex]}" class="comment"><p>
854     <input type="hidden" name="mycmd" value="comment" />
855     <input type="hidden" name="mypage" value="$form{mypage}" />
856     <input type="hidden" name="myLastModified" value="$lastmodified" />
857     <input type="hidden" name="mytouch" value="on" />
858     <input type="hidden" name="comment_index" value="$CommentIndex" />
859     @{[&Resource('WikiForm:WikiComment:Name=',escape=>1)]}
860     <input type="text" name="myname" value="" size="10" class="comment-name" />
861     <input type="text" name="mymsg" value="" size="60" class="comment-msg" />
862     <input type="submit" value="@{[&Resource('WikiForm:Add',escape=>1)]}" title="@{[&Resource('WikiForm:AddLong',escape=>1)]}" class="comment-submit" />
863     </p></form>
864     EOD
865     } else {
866     return <<"EOD";
867     <del><form action="$url_cgi" method="get">
868     <input type="hidden" name="mycmd" value="default" />
869     <input type="hidden" name="mypage" value="$form{mypage}" />
870     @{[&Resource('WikiForm:WikiComment:Name=',escape=>1)]}
871     <input type="text" name="myname" value="" size="10" disabled="disabled" />
872     <input type="text" name="mymsg" value="" size="60" disabled="disabled" />
873     </form></del>
874     EOD
875     }
876     } elsif ($embedded =~ /$embed_command{searched}/) {
877     return get_search_result ($1, -match_myself => 1);
878     } elsif ($embedded =~ /^\[\[\#embed:(.+)\]\]$/) {
879     my ($name, $r) = ($1, '');
880     if ($name =~ s/^IMG://) {
881     my ($magic, $content) = SuikaWiki::Plugin->magic_and_content ($database{$name});
882     $r = &convert_format ($content, $magic => 'HTML_fragment', page => $name, magic => $magic, -error_no_return => 1);
883     if (defined $r) {
884     return $r;
885     } else {
886     return '<span class="error-embed-image">'.&Resource ('Embed:ImageNotSupported', escape=>1).'</span>';
887     }
888     } else {
889     if ($main::_EMBEDED != 1) {
890     my ($cf, $content) = SuikaWiki::Plugin->magic_and_content ($database{$name});
891     $cf ||= '#?SuikaWiki/0.9';
892     if ($cf =~ m!^#\?SuikaWiki/0.9(?:$|\s)!) {
893     $main::_EMBEDED = 1;
894     $r = &text_to_html ($content, magic => $cf, page => $name);
895     $main::_EMBEDED = 0;
896     } elsif (length $content) {
897     $r = "<pre>@{[&escape ($content)]}</pre>";
898     } else {
899     $r = &text_to_html ("[INS[\n[[$name]]: @{[&Resource('Embed:PageNotFound')]}\n]INS]\n", magic => '#?SuikaWiki/0.9');
900     }
901     } else { ## nested #EMBED
902     $r = &text_to_html ("[INS[\n[[$name]]: @{[&Resource('Embed:Nested',escape=>1)]}\n]INS]\n", magic => '#?SuikaWiki/0.9');
903     }
904     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>);
905     }
906     } else {
907     return $embedded;
908     }
909     }
910    
911     sub load_formatter (@) {
912     for my $t (@_) {
913     unless ($fmt{$t}) {
914     require Message::Util::Formatter;
915     $fmt{$t} = Message::Util::Formatter->new;
916     for (@{$SuikaWiki::Plugin::List{'wiki'.$t}||[]}) {
917     $_->load_formatter ($fmt{$t}, type => 'wiki'.$t);
918     }
919     }
920     }
921     }
922    
923     sub do_wikiform {
924     my $content = $database{$form{mypage}};
925     my $anchor = &get_new_anchor_index ($content);
926     &load_formatter (qw/form_template form_option/);
927     my $write = 0;
928     my $i = 1;
929     $content =~ s{$embed_command{form}}{
930     my ($embed, $wfname, $template, $option) = ($&, $1, $3, $4);
931     if (($wfname && $wfname eq $form{wikiform_targetform})
932     || $i == $form{wikiform_index}) {
933     $template =~ s/\\([\\'])/$1/g;
934     $option =~ s/\\([\\'])/$1/g;
935     my $param = bless {depth=>10}, 'SuikaWiki::Plugin';
936     $param->{page} = $form{mypage};
937     $param->{form_index} = $i;
938     $param->{form_name} = $wfname;
939     $param->{anchor_index} = $anchor;
940     $param->{argv} = \%form;
941     $param->{default_name} = $1 if $content =~ /default-name="([^"]+)"/;
942     $param->{default_name} ||= &Resource('WikiForm:WikiComment:DefaultName');
943     $fmt{form_option}->replace ($option, $param);
944     my $t = 1;
945     for (keys %{$param->{require}||{}}) {
946     (undef $t, last) unless length $param->{argv}->{'wikiform__'.$_};
947     }
948     $t = $fmt{form_template}->replace ($template, $param) if $t;
949     if (length $t) {
950     if ($param->{output}->{reverse}) {
951     $embed .= "\n" . $t;
952     } else {
953     $embed = $t . "\n" . $embed;
954     }
955     $write = 1;
956     $form{__comment_anchor_index} = $anchor
957     if $param->{anchor_index_}; ## $anchor is used!
958     }
959     $form{__wikiform_anchor_index} = $i;
960     undef $form{wikiform_targetform}; ## Make sure never to match
961     undef $form{wikiform_index}; ## with WikiForm in rest of page!
962     }
963     $i++; $embed;
964     }ge;
965     unless ($write) {
966     #$content = "#?SuikaWiki/0.9\n\n" unless $content;
967     #$content .= "\n" unless $content =~ /\n$/s;
968     #
969     }
970     if ($write) {
971     $form{mymsg} = $content;
972     $form{mytouch} = 'on';
973     &do_write;
974     } else { ## Don't write!
975     $form{mycmd} = 'default';
976     &do_view;
977     }
978     }
979    
980     sub code_convert {
981     require Jcode;
982     my ($contentref, $code) = (shift, shift || $kanjicode);
983     if ($code =~ /euc/) { $code = 'euc' }
984     elsif ($code =~ /iso/) { $code = 'jis' }
985     elsif ($code =~ /shi/) { $code = 'sjis' }
986     elsif ($code =~ /utf/) { $code = 'utf8' }
987     $$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;
988     return $$contentref;
989     }
990    
991     sub _rfc3339_date ($) {
992     my @time = gmtime (shift);
993     sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
994     }
995    
996     my %_Resource;
997     sub Resource ($;%) {
998     my ($s, %o) = @_;
999     unless (defined $_Resource{$s}) {
1000     $_Resource{$_[0]} = &wiki::resource::get ($s, $_Resource{__option});
1001     }
1002     $o{escape} ? &escape ($_Resource{$s}) : $_Resource{$s};
1003     }
1004    
1005     package wiki::referer;
1006     sub add ($$) {
1007     my $page = shift;
1008     my $uri = shift;
1009     unless (ref $uri) {
1010     require URI;
1011     $uri = URI->new ($uri);
1012     ## Some schemes do not have query part.
1013     eval q{ $uri->query (undef) if $uri->query =~ /^[0-9]{6,8}$/ };
1014     $uri->fragment (undef);
1015     }
1016     $uri = $uri->canonical;
1017     return unless $uri;
1018     for my $regex (&get_dont_record) {
1019     return if $uri =~ /$regex/;
1020     }
1021     my %list = get ($page);
1022     $list{ $uri }++;
1023     set ($page, \%list);
1024     }
1025     sub get ($) { split /"/, $main::database->meta (Referer => $_[0]) }
1026     sub set ($%) {
1027     my $page = shift;
1028     my $list = shift;
1029     $main::database->meta (Referer => $page => join '"', %$list);
1030     }
1031    
1032     sub get_dont_record () {
1033     map {s/\$/\\\$/g; s/\@/\\\@/g; $_}
1034     grep !/^#/,
1035     split /[\x0D\x0A]+/, $main::database{RefererDontRecord};
1036     }
1037     sub get_site_name () {
1038     my @lines = grep /[^#]/, split /[\x0D\x0A]+/, $main::database{RefererSiteName};
1039     my @item;
1040     for (@lines) {
1041     next if /^#/;
1042     my ($uri, $name) = split /\s+/, $_, 2;
1043     $uri =~ s/\$/\\\$/g; $uri =~ s/\@/\\\@/g; $uri =~ s/\//\\\//g;
1044     $name =~ s!([()/\\])!\\$1!g; $name =~ s/\$([0-9]+)/).__decode (\${$1}).q(/g;
1045     push @item, [$uri, qq(q($name))];
1046     }
1047     @item;
1048     }
1049    
1050     sub list_html ($) {
1051     my $page = shift;
1052     my %list = get ($page);
1053     my $r = '';
1054     my @name = get_site_name ();
1055     for my $uri (sort {$list{$b}<=>$list{$a}||$a cmp $b} keys %list) {
1056     my $title;
1057     for my $item (@name) {
1058     if ($uri =~ /$item->[0]/) {
1059     $title = $uri;
1060     eval qq{\$title =~ s/^.*$item->[0].*\$/$item->[1]/e}
1061     or die $@ ;#. qq{\$title =~ s/^.*$item->[0].*\$/$item->[1]/e};
1062     last;
1063     }
1064     }
1065     my $euri = main::escape ($uri);
1066     if ($title) {
1067     $r .= qq(<li>{$list{$uri}} <a href="$euri" title="URI: &lt;$euri&gt;">@{[main::escape ($title)]}</a></li>\n);
1068     } else {
1069     $r .= qq(<li>{$list{$uri}} &lt;<a href="$euri">$euri</a>&gt;</li>\n);
1070     }
1071     }
1072     $r ? qq(<ul>$r</ul>\n) : '';
1073     }
1074    
1075     sub __decode ($) {
1076     my $s = shift;
1077     $s =~ tr/+/ /;
1078     $s =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge;
1079     main::code_convert (\$s);
1080     }
1081    
1082     package wiki::useragent;
1083     our $UseLog;
1084    
1085     sub add ($) {
1086     my $s = shift;
1087     return unless length $s;
1088     return unless $UseLog;
1089     $s =~ s/([\x00-\x08\x0A-\x1F\x25\x7F-\xFF])/sprintf '%%%02X', unpack 'C', $1/ge;
1090     my %ua;
1091     for (split /\n/, $main::database{$main::PageName{UserAgentList}}) {
1092     if (/^-\[(\d+)\] (.+)$/) {
1093     my ($t, $n) = ($1, $2);
1094     $n =~ tr/\x0A\x0D//d;
1095     $ua{$n} = $t;
1096     }
1097     }
1098     $ua{$s}++;
1099     my $s = qq(#?SuikaWiki/0.9\n);
1100     for (sort {$ua{$a} <=> $ua{$b}} keys %ua) {
1101     $s .= sprintf qq(-[%d] %s\n), $ua{$_}, $_;
1102     }
1103     $main::database->STORE ($main::PageName{UserAgentList} => $s, -touch => 0);
1104     }
1105    
1106     package wiki::suikawikiconst;
1107    
1108     sub to_hash ($;$) {
1109     my $page = shift;
1110     my $h = shift || {};
1111     my $val;
1112     for my $line (split /\n/, $page) {
1113     next if $line =~ /^#/;
1114     $line =~ tr/\x0A\x0D//d;
1115     if ($val && $line =~ s/^\s+\\?//) {
1116     $h->{$val} .= length $h->{$val} ? "\n" . $line : $line;
1117     } elsif ($line =~ /^(.+):/) {
1118     $val = $1; $h->{$val} = '';
1119     }
1120     }
1121     $h;
1122     }
1123    
1124     package wiki::dummy;
1125     sub mtime (@) {undef}
1126     sub meta (@) {undef}
1127     sub Yuki::YukiWikiDB2::meta (@) {undef}
1128    
1129     package SuikaWiki::Plugin;
1130     sub escape ($$) { main::escape ($_[1]) }
1131     sub unescape ($$) { main::unescape ($_[1]) }
1132     sub encode ($$) { main::encode ($_[1]) }
1133     sub decode ($$) { main::decode ($_[1]) }
1134     sub __get_datetime ($) { main::get_now () }
1135     sub resource ($$;%) { shift; &main::Resource (@_) }
1136     sub uri ($$) { $main::uri{$_[1]} }
1137     sub user_agent_names ($) { $main::UA }
1138     sub formatter ($$) {
1139     &main::load_formatter ($_[1]);
1140     $main::fmt{$_[1]};
1141     }
1142     sub format_converter ($$$) {
1143     &main::load_formatter ('format');
1144     $main::fmt{format}->{($_[1]=~/([A-Za-z0-9]\S+)/?$1:'SuikaWiki/0.9').'_to_'.$_[2]}
1145     || $main::fmt{format}->{($_[1]=~/([A-Za-z0-9](?:(?!\/)\S)+)/?$1:'SuikaWiki').'_to_'.$_[2]};
1146     }
1147     sub cache ($$) {
1148     our %Cache;
1149     my (undef, $name, %option) = @_;
1150     unless (ref $Cache{$name}) {
1151     my %cache;
1152     tie (%cache, 'Yuki::YukiWikiCache', -file => $main::PathTo{CachePrefix}.$name, %option);
1153     $Cache{$name} = \%cache;
1154     }
1155     $Cache{$name};
1156     }
1157     sub _database ($) { $main::database }
1158     sub _database_exist ($$) { exists $main::database{$_[1]} }
1159     sub _html_wikilink ($$%) { shift; &main::make_wikilink (@_) }
1160     sub _uri_wiki_page ($$%) {
1161     my (undef, $page, %option) = @_;
1162     $option{mode} ||= 'read';
1163     length $page ? undef : ($page = $main::PageName{FrontPage});
1164     $option{href} = $main::uri{wiki}.'?';
1165     if ($option{up_to_date} || $option{mode} ne 'read' || $option{add_param}) {
1166     $option{href} .= qq(mypage=@{[&main::encode($page)]};mycmd=@{[&main::encode($option{mode})]});
1167     $option{href} .= ';'.$option{add_param} if $option{add_param};
1168     $option{href} .= ';x-d='.time if $option{up_to_date};
1169     $option{href} .= ';x-lm='.($main::database->mtime ($page)||0) if $option{with_lm};
1170     } else {
1171     $option{href} .= &main::encode ($page);
1172     }
1173     $option{href};
1174     }
1175    
1176    
1177     package wiki::conneg;
1178    
1179     ## BUG: this parser isn't strict.
1180     sub get_accept_lang (;$) {
1181     my $alang = shift || $main::ENV{HTTP_ACCEPT_LANGUAGE};
1182     my %alang = (ja => 0.0002, en => 0.0001);
1183     if ($main::UA =~ m#Mozilla/0\.#) {
1184     $alang{ja} = 0.00001;
1185     }
1186     my $i = 0.1;
1187     for (split /\s*,\s*/, $alang) {
1188     tr/\x09\x0A\x0D\x20//d;
1189     if (/((?:(?!;q=).)+)(?:;q="?([0-9.]+)"?)?/) {
1190     my $l = lc $1; $l =~ tr/\x22\x5C//d;
1191     $alang{$l} = (defined $2 ? $2 : 1.000)*1000;
1192     $alang{$l} += $i unless $alang{$l} == 0;
1193     $i -= 0.001;
1194     }
1195     }
1196     \%alang;
1197     }
1198    
1199     package wiki::resource;
1200    
1201     sub get ($;\%) {
1202     my ($resname, $option) = @_;
1203     $option->{accept_language} ||= &wiki::conneg::get_accept_lang ();
1204     $option->{resource} ||= {};
1205     my $v;
1206     for my $lang (sort {$option->{accept_language}->{$b} <=> $option->{accept_language}->{$a}} grep {$option->{accept_language}->{$_}!=0} keys %{$option->{accept_language}}) {
1207     while (length $lang) {
1208     unless ($option->{accept_language}->{defined $option->{accept_language}->{$lang} ? $lang : '*'} == 0) {
1209     $option->{resource}->{$lang} ||= &wiki::suikawikiconst::to_hash ($main::database{'WikiResource:'.$lang});
1210     $v = $option->{resource}->{$lang}->{$resname};
1211     last if defined $v;
1212     }
1213     $lang =~ s/[^+-]*$//; $lang =~ s/[+-]$//;
1214     }
1215     last if defined $v;
1216     }
1217     defined $v ? $v : $resname;
1218     }
1219    
1220     package main;
1221     &SuikaWiki::Plugin::import_plugins ();
1222     &main ();
1223    
1224     =head1 NAME
1225    
1226     lib/suikawiki.pl --- SuikaWiki
1227    
1228     =head1 AUTHOR
1229    
1230     Hiroshi Yuki <hyuki@hyuki.com> <http://www.hyuki.com/yukiwiki/>
1231    
1232     Makio Tsukamoto <http://digit.que.ne.jp/>
1233    
1234     Wakaba <w@suika.fam.cx>
1235    
1236     =head1 LICENSE
1237    
1238     Copyright (C) 2000-2003 AUTHORS
1239    
1240     This program is free software; you can redistribute it and/or
1241     modify it under the same terms as Perl itself.
1242    
1243     =cut
1244    
1245     1; # $Date: $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24