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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download)
Thu Apr 3 01:09:07 2003 UTC (22 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.2: +44 -31 lines
File MIME type: text/plain
SuikaWiki::View support

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24