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