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

Contents of /suikawiki/script/wiki.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.50 - (show annotations) (download)
Sun Jan 12 09:15:06 2003 UTC (21 years, 4 months ago) by w
Branch: MAIN
Changes since 1.49: +131 -261 lines
Most of do_* are integred

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24