#!/usr/bin/perl # wiki.cgi - This is YukiWiki, yet another Wiki clone. # # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. use strict; use lib qw(./lib); use CGI::Carp qw(fatalsToBrowser); require 'wikidata/suikawiki-config.ph'; use Yuki::DiffText qw(difftext); use Fcntl; ############################## my %fmt; ## formatter objects my %embed_command = ( searched => '^\[\[#searched:([^\]]+)\]\]$', form => qr/\[\[\#form(?:\(([A-Za-z0-9-]+)\))?:'((?:[^'\\]|\\.)*)':'((?:[^'\\]|\\.)*)'(?::'((?:[^'\\]|\\.)*)')?\]\]/, ); our ($modifier_dbtype,$url_cgi,%uri,%PathTo,$use_exists); our (%PageName,$kanjicode,$lang,%FixedPage); ############################## my $info_LastModified = 'LastModified'; my $info_IsFrozen = 'IsFrozen'; ############################## my %form; my %database; my %infobase; my %diffbase; my %interwiki; ############################## my %page_command = ( $PageName{IndexPage} => 'index', $PageName{RssPage} => 'rss', ); my %command_do = ( read => \&do_read, TEXT_CSS => \&do_output_css, edit => \&do_edit, adminedit => \&do_adminedit, adminchangepassword => \&do_adminchangepassword, write => \&do_write, index => \&do_index, searchform => \&do_searchform, comment => \&do_comment, RandomJump => \&do_random_jump, rss => \&do_rss, diff => \&do_diff, wikiform => \&do_wikiform, map => \&do_map, ); my $UA = ''; ## User agent name $| = 1; ############################## sub main { $UA = $main::ENV{HTTP_USER_AGENT}; &open_db; &init_form; if ($command_do{$form{mycmd}}) { &{$command_do{$form{mycmd}}}; } else { &{$command_do{read}}; } &close_db; } sub do_read { my $content = $database{$form{mypage}}; #print "content-type:text/plain;charset=euc-jp\n\n".gmtime."Get Lastmodified\n"; my $lm = &get_info($form{mypage}, $info_LastModified); wiki::referer::add ($form{mypage}, $ENV{HTTP_REFERER}); wiki::useragent::add ($ENV{HTTP_USER_AGENT}); #print gmtime."Search...\n"; my ($r, $c) = get_search_result ($form{mypage}); my $rl = wiki::referer::list_html ($form{mypage}); my @toc; push @toc, qq(-@{[&Resource('SeeAlso',escape=>1)]}) if $c; push @toc, qq(-@{[&Resource('Referers',escape=>1)]}) if $rl; my $cf = 'SuikaWiki/0.9'; ## Should be support at least: ## - 'SuikaWiki/0.9' CRLF ## - 'H2H/' ("0.9" / "1.0" / "1.1") CRLF ## - "/*" WSP* 'W3C-CSS/' ("1.0" / "2.0") "*/" CRLF $cf = $1 if $content =~ s#^(?:/\*\s*|[\#<]\?)?([A-Z][A-Za-z0-9-]+/[0-9.]+(?:[^0-9.\x0D\x0A][^\x0D\x0A]*)?)[\x0D\x0A]+##s; if ($cf =~ m!^(?:\#\?)?SuikaWiki/0.9(?:$|\s)!) { #print gmtime."Header...\n"; &print_header ($form{mypage}, -last_modified => $lm, -expires => time + 120, -content_format => $cf, -noindex => ($cf =~ /obsoleted="yes"/ ? 1 : 0)); #print "\n". gmtime."Body...\n"; &print_content ($content, content_format => $cf, last_modified => $lm, -toc => \@toc); print &text_to_html (q([[#comment]])) if $cf !~ /obsoleted="yes"/ && !$FixedPage{$form{mypage}}; } else { &print_header($form{mypage}, -expires => time + 120, -last_modified => $lm); print "
@{[&escape($content)]}";
}
if ($c) {
print qq{$msg
); &print_footer($PageName{ErrorPage}); exit(0); } sub print_header ($;%) { my ($page, %option) = @_; my @head; $option{body_class} = &is_frozen($page) ? 'frozen' : 'normal'; $option{body_class} .= " wiki-page-obsoleted" if $option{-content_format} =~ /obsoleted="yes"/; if ($option{-goto}) { if ($UA =~ m#Opera|MSIE 2\.#) { ## WARNING: This code may output unsafe HTML document if ## $option{-goto} is not clean. $option{-goto} =~ tr/;/&/ if $UA =~ m#Opera#; print qq{Refresh: 0; url=$option{-goto}\n}; push @head, qq(); } else { $option{-goto} =~ tr/;/&/ if $UA =~ m#Mozilla/[1-4]\.#; print qq{Refresh: 0; url="$option{-goto}"\n}; push @head, qq(); } } print qq{Last-Modified: @{[scalar gmtime $option{-last_modified}]}\n} if $option{-last_modified}; if ($option{-expires}) { print qq{Expires: @{[scalar gmtime $option{-expires}]}\n}; } if ($UA =~ m#Mozilla/2#) { my $ct = qq{text/html; charset=@{[&get_charset_name($kanjicode,compatible=>1)]}}; print qq{Content-Type: $ct\n}; push @head, qq{}; } elsif ($UA =~ m#Infomosaic#) { print qq{Content-Type: text/html\n}; } else { print qq{Content-Type: text/html; charset=@{[&get_charset_name($kanjicode)]}\n}; } push @head, qq("); foreach (@txt) { chomp; if (/^\*\*\*\*\*([^\x0D\x0A]*)/) { push(@toc, qq(----- @{[&escape($1)||$tocnum]}\n)); push(@result, splice(@saved), qq(
"; push(@result, &inline($2, const => \%const)); unshift @saved, "
"; } elsif (/^\s*$/) { push(@result, splice(@saved)); push(@result, ""); unshift(@saved, "
"); } elsif (/^(\s+.*)$/) { &back_push('pre', 1, \@saved, \@result); push(@result, &inline($1, const => \%const)); } elsif (/^\,(.*?)[\x0D\x0A]*$/) { &back_push('table', 1, \@saved, \@result); ####### # This part is taken from Mr. Ohzaki's Perl Memo and Makio Tsukamoto's WalWiki. # XXXXX my $tmp = "$1,"; my @value = map {/^"(.*)"$/ ? scalar($_ = $1, s/""/"/g, $_) : $_} ($tmp =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g); my @align = map {(s/^\s+//) ? ((s/\s+$//) ? ' align="center"' : ' align="right"') : ''} @value; my @colspan = map {($_ eq '==') ? 0 : 1} @value; for (my $i = 0; $i < @value; $i++) { if ($colspan[$i]) { while ($i + $colspan[$i] < @value and $value[$i + $colspan[$i]] eq '==') { $colspan[$i]++; } $colspan[$i] = ($colspan[$i] > 1) ? sprintf(' colspan="%d"', $colspan[$i]) : ''; $value[$i] = sprintf('"; } elsif (/^\](INS|DEL|PRE)\]\s*$/) { push @result, splice (@saved), ''.lc($1).'>'; } elsif (/^\[([0-9]+)\](.*)$/ && !$main::_EMBEDED) { my $num = 0+$1; push @result, qq([$num]); push @result, &inline ($2, const => \%const); } else { push(@result, &inline($_, const => \%const)); } } push(@result, splice(@saved)); my $toc = ''; if ($option{toc}) { # Convert @toc (table of contents) to HTML. # This part is taken from Makio Tsukamoto's WalWiki. my (@tocsaved, @tocresult); foreach (@toc,@toc2) { if (/^(-{1,6})(.*)$/) { &back_push('ul', length($1), \@tocsaved, \@tocresult); push(@tocresult, '
\n
##g; $toc =~ s#[\x0D\x0A]+##g; $toc =~ s#\n##g;
$toc;
}
sub back_push {
my ($tag, $level, $savedref, $resultref, $attr) = @_;
while (@$savedref > $level) {
push(@$resultref, shift(@$savedref));
}
if ($savedref->[0] ne "$tag>") {
push(@$resultref, splice(@$savedref));
}
while (@$savedref < $level) {
unshift(@$savedref, "$tag>");
push(@$resultref, "<$tag$attr>");
}
}
sub inline ($;%) {
my ($line, %option) = @_;
$line = &escape($line);
$line =~ s{$embed_command{form}}{&make_custom_form ($1, $2, $3, $4)}ge;
$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;
$line =~ s:\[(WEAK)\[(.+?)\]\]:$2:g;
$line =~ s:\[ABBR\[([^]]+)\] \[([^]]+)\]\]:$1:g;
$line =~ s:\[RUBYB\[([^]]+)\] \[([^]]+)\] \[([^]]+)\]\]:$1 ($3) :g;
$line =~ s:\[RUBY\[([^]]+)\] \[([^]]+)\]\]:$1 :g;
$line =~ s:\[RUBYB\[([^]]+)\] \[([^]]+)\]\]:$1 ($2) :g;
$line =~ s%\[Q\[([^]]+)\](?: \[<([\x21-\x5A\x5E-\x7E]+)>\])?\]%¡Ö$1
¡×%g;
$line =~ s|'''([^']+)'''|$1|g;
$line =~ s|''([^']+)''|$1|g;
$line =~ s{
(\[\[(\#\S+?)\]\])
|\[\[([^[]+?)](?:>>([0-9]+))?]
|>>([0-9]+)
|<([A-Za-z0-9%]+:(?:(?!>).)+)>
}{
my ($l, $page,$anchor, $anum, $uri) = ($1, $3,$4, 0+$5, $6);
if ($l) {
return &embedded_to_html($1);
} elsif (defined $page) {
&make_wikilink ($page, anchor => 0+$anchor);
} elsif ($anum) {
qq(>>$anum);
} elsif ($uri) {
&make_urilink ($uri);
}
}gex;
return $line;
}
sub make_wikilink ($%) {
my ($ename, %option) = @_;
my $name = &unescape ($ename);
if ($database{$name}) {
my $subject = &escape (&get_subjectline ($name, delimiter => ''));
if ($option{anchor}) {
return qq($ename>>$option{anchor});
} else {
return qq($ename);
}
} else {
return qq($ename@{[&Resource('JumpAndEditWikiPageMark',escape=>1)]});
}
}
sub make_urilink ($;%) {
require URI;
my $uri = shift;
if ($uri =~ s/^IW://) { ## InterWiki (not URI)
$uri = &unescape ($uri);
if ($uri =~ /^([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+"):([^\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+|"(?:\\.|[^"\\])+")$/) {
my ($site, $name) = ($1, $2);
for ($site, $name) {
if (s/^"//) { s/"$//; s/\\(.)/$1/g }
}
&init_InterWikiName () unless $interwiki{'[[]]'};
if ($interwiki{$site}) {
&load_formatter ('interwiki');
my $uri = &escape ($fmt{interwiki}->replace ($interwiki{$site} => {site => $site, name => $name}));
$site = &escape ($site); $name = &escape ($name);
qq(<$site:$name>);
} else {
qq(<@{[&Resource('Error:UnknownInterWikiName=',escape=>1)]}@{[&escape ($site)]}>);
}
} else {
qq(<@{[&Resource('Error:InvalidInterWiki=',escape=>1)]}@{[&escape($uri)]}>);
}
} elsif ($uri =~ /^urn:/) { ## URN
my $uri2 = &escape (URI->new ('/uri-res/N2L?'.&unescape ($uri), 'http')->canonical);
qq(<$uri>);
} elsif ($uri =~ s/^MAIL://) { ## mail address (not URI)
my $uri2 = &escape (URI->new ('mailto:'.&unescape ($uri))->canonical);
qq(<$uri>);
} elsif ($uri =~ s/^IMG(?:\([^)]+\))?://) { ## image (not URI itself)
my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
qq(
);
} else { ## misc. URI
CGI::Carp::warningsToBrowser (0);
my $uri2 = &escape (URI->new (&unescape ($uri))->canonical);
CGI::Carp::warningsToBrowser (1);
qq(<$uri>);
}
}
{my $FormIndex = 0;
sub make_custom_form ($$$$) {
my ($wfname, $definition, $template, $option) = @_;
## $template and $option is currently not used in this procedure.
unless ($main::_EMBEDED) {
$FormIndex++;
if (length $definition) {
my $param = bless {}, 'SuikaWiki::Plugin';
my $lastmodified = &get_info($form{mypage}, $info_LastModified);
&load_formatter (qw/form_input form_option/);
$definition = &unescape ($definition);
$definition =~ s/\\(.)/$1/g;
$option = &unescape ($option);
$option =~ s/\\(.)/$1/g;
$fmt{form_option}->replace ($option, $param);
$param->{output}->{form} = 1 unless defined $param->{output}->{form};
$definition .= ' %submit;' if $definition !~ /%submit/ && !$param->{output}->{nosubmit} && $param->{output}->{form};
my $target_page = $param->{output}->{page} || $form{mypage};
$param->{form_disabled} = 1 if $FixedPage{$target_page};
my $target_form = $param->{output}->{id};
my $r = '';
$r = <{output}->{form};
\n" if $param->{output}->{form};
$r;
} else { ## No input-interface WikiForm
qq();
}
} else {
qq(@{[&Resource('Error:WikiForm:EmbedIsNotSupported',escape=>1)]});
}
}}
sub print_message {
my ($msg) = @_;
print qq(@{[&escape($msg)]}
);
}
sub init_form {
## TODO: Support multipart/form-data
my $query = '';
if (uc $main::ENV{REQUEST_METHOD} eq 'POST') {
read STDIN, $query, $main::ENV{CONTENT_LENGTH};
}
$query .= ($query ? ';' : '') . $main::ENV{QUERY_STRING};
if ($main::ENV{REQUEST_METHOD} ne 'POST' && $main::ENV{QUERY_STRING} && $main::ENV{QUERY_STRING} !~ /[&;=]/) {
my $query = &decode($main::ENV{QUERY_STRING});
$query = &code_convert(\$query, $kanjicode);
if ($page_command{$query}) {
$form{mycmd} = $page_command{$query};
$form{mypage} = $query;
} else {
$form{mypage} = $query;
$form{mycmd} = $database{$form{mypage}} ? 'read' : 'edit';
}
} else {
for (split /[;&]/, $query) {
if (my ($n, $v) = split /=/, $_, 2) {
for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge};
$form{$n} = $v;
}
}
unless (defined $form{mypage}) {
$form{mypage} = $form{epage};
$form{mypage} =~ s/([0-9A-F]{2})/ord hex $1/g;
}
if ($page_command{$form{mypage}} && $form{mycmd} eq 'read') {
$form{mypage} = &code_convert(\$form{mypage}, $kanjicode);
$form{mycmd} = $page_command{$form{mypage}};
}
}
$form{mypage} ||= 'HomePage';
$form{mycmd} ||= 'read';
# mypreview_edit -> do_edit, with preview.
# mypreview_adminedit -> do_adminedit, with preview.
# mypreview_write -> do_write, without preview.
foreach (keys %form) {
if (/^mypreview_(.*)$/) {
$form{mycmd} = $1;
$form{mypreview} = 1;
}
}
#
# $form{mycmd} is frozen here.
#
for (grep /^wikiform__/, keys %form) {
$form{$_} = &code_convert (\$form{$_}, $kanjicode);
}
$form{mymsg} = &code_convert(\$form{mymsg}, $kanjicode);
$form{myname} = &code_convert(\$form{myname}, $kanjicode);
}
sub update_recent_changes {
my $update = "- @{[&get_now]} [[$form{mypage}]] @{[&get_subjectline($form{mypage})]}";
my @oldupdates = split(/\x0D?\x0A/, $database{RecentChanges});
shift @oldupdates; ## '#?' magic line
my @updates;
foreach (@oldupdates) {
/^\- \d\d\d\d\-\d\d\-\d\d \d\d:\d\d \[\[([^]]+)\]\]/;
my $name = $1;
if ($name ne $form{mypage}) {
push @updates, $_;
}
}
if (&is_exist_page($form{mypage})) {
unshift @updates, $update;
}
splice @updates, (&Resource ('RecentChanges:Max') || 50) + 1;
$database{RecentChanges} = "#?SuikaWiki/0.9\n" . join("\n", @updates);
if ($PathTo{TouchFile}) {
open(FILE, "> ".$PathTo{TouchFile});
print FILE localtime() . "\n";
close(FILE);
}
}
{my %SubjectLine;
sub get_subjectline {
my ($page, %option) = @_;
unless (defined $SubjectLine{$page}) {
if (not &is_editable($page)) {
$SubjectLine{$page} = "";
} else {
$SubjectLine{$page} = $database{$page};
$SubjectLine{$page} =~ s!^\#\?[^\x0A\x0D]+[\x0A\x0D]*!!s;
$SubjectLine{$page} =~ s/\x0D?\x0A.*//s;
}
}
if (length $SubjectLine{$page}) {
$option{delimiter} = defined $option{delimiter} ? $option{delimiter} : &Resource('Title-Summary Delimiter');
$option{delimiter}.$SubjectLine{$page}.$option{tail};
} else {
'';
}
}}
sub open_db {
if ($modifier_dbtype eq 'dbmopen') {
dbmopen(%database, $PathTo{WikiDataBase}, 0666) or &print_error("(dbmopen) $PathTo{WikiDataBase}");
dbmopen(%infobase, $PathTo{WikiInfoBase}, 0666) or &print_error("(dbmopen) $PathTo{WikiInfoBase}");
} elsif ($modifier_dbtype eq 'AnyDBM_File') {
eval q{use AnyDBM_File};
tie(%database, "AnyDBM_File", $PathTo{WikiDataBase}, O_RDWR|O_CREAT, 0666) or &print_error("(tie AnyDBM_File) $PathTo{WikiDataBase}");
tie(%infobase, "AnyDBM_File", $PathTo{WikiInfoBase}, O_RDWR|O_CREAT, 0666) or &print_error("(tie AnyDBM_File) $PathTo{WikiInfoBase}");
} else {
eval q{use Yuki::YukiWikiDB};
tie(%database, "Yuki::YukiWikiDB", $PathTo{WikiDataBase}) or &print_error("(tie Yuki::YukiWikiDB) $PathTo{WikiDataBase}");
tie(%infobase, "Yuki::YukiWikiDB", $PathTo{WikiInfoBase}) or &print_error("(tie Yuki::YukiWikiDB) $PathTo{WikiInfoBase}");
}
}
sub close_db {
if ($modifier_dbtype eq 'dbmopen') {
dbmclose(%database);
dbmclose(%infobase);
} elsif ($modifier_dbtype eq 'AnyDBM_File') {
untie(%database);
untie(%infobase);
} else {
untie(%database);
untie(%infobase);
}
}
sub open_diff {
if ($modifier_dbtype eq 'dbmopen') {
dbmopen(%diffbase, $PathTo{WikiDiffBase}, 0666) or &print_error("(dbmopen) $PathTo{WikiDiffBase}");
} elsif ($modifier_dbtype eq 'AnyDBM_File') {
tie(%diffbase, "AnyDBM_File", $PathTo{WikiDiffBase}, O_RDWR|O_CREAT, 0666) or &print_error("(tie AnyDBM_File) $PathTo{WikiDiffBase}");
} else {
tie(%diffbase, "Yuki::YukiWikiDB", $PathTo{WikiDiffBase}) or &print_error("(tie Yuki::YukiWikiDB) $PathTo{WikiDiffBase}");
}
}
sub close_diff {
if ($modifier_dbtype eq 'dbmopen') {
dbmclose(%diffbase);
} elsif ($modifier_dbtype eq 'AnyDBM_File') {
untie(%diffbase);
} else {
untie(%diffbase);
}
}
sub print_editform {
my ($mymsg, $lastmodified, %mode) = @_;
my $frozen = &is_frozen($form{mypage});
if ($form{mypreview}) {
if ($form{mymsg}) {
unless ($mode{conflict}) {
print qq(@{[&Resource('Preview:Title',escape=>1)]}
\n);
print qq(@{[&Resource('Preview:Notice',escape=>1)]}
\n);
print qq(\n);
&print_content($form{mymsg});
print qq(\n);
}
} else {
print @{[&Resource('Preview:Empty',escape=>1)]};
}
$mymsg = &escape($form{mymsg});
} else {
$mymsg = &escape($mymsg || $database{NewPageTemplate});
}
my $magic = '';
$magic = $1 if $mymsg =~ m/^([^\x0A\x0D]+)/s;
my $edit = $mode{admin} ? 'adminedit' : 'edit';
my $selected = 'read';
if ($form{after_edit_cmd}) {
$selected = $form{after_edit_cmd};
} elsif ($magic =~ /Const|Config|CSS/) {
$selected = 'edit';
}
my $afteredit = <
EOH
print <<"EOD";
EOD
unless ($mode{conflict}) {
## Show help text
my $help = $database{WikiEditHelp};
$help =~ s!^\#\?([A-Z][A-Za-z0-9-]+/[0-9.]+(?:[^0-9.\x0D\x0A][^\x0D\x0A]*)?)[\x0D\x0A]+!!s;
print &text_to_html ($help, toc => 0);
}
}
sub is_editable {
my ($page) = @_;
if ($FixedPage{$page} || $page =~ /\s/ || $page =~ /^\#/) {
return 0;
} else {
return 1;
}
}
# armor_name:
# WikiName -> WikiName
# not_wiki_name -> [[not_wiki_name]]
sub armor_name { qq([[$_[0]]]) }
# unarmor_name:
# [[bracket_name]] -> bracket_name
# WikiName -> WikiName
sub unarmor_name {
my ($name) = @_;
if ($name =~ /^\[\[(\S+?)\]\]$/) {
return $1;
} else {
return $name;
}
}
sub decode {
my ($s) = @_;
$s =~ tr/+/ /;
$s =~ s/%([A-Fa-f0-9][A-Fa-f0-9])/pack("C", hex($1))/eg;
return $s;
}
sub encode {
my ($s) = @_;
my $encoded = '';
foreach my $ch (split(//, $s)) {
if ($ch =~ /[A-Za-z0-9_]/) {
$encoded .= $ch;
} else {
$encoded .= '%' . sprintf("%02X", ord($ch));
}
}
return $encoded;
}
sub conflict {
my ($page, $rawmsg) = @_;
if ($form{myLastModified} eq &get_info($page, $info_LastModified)) {
return 0;
}
&print_header($page, -noindex => 1);
&print_content(&Resource('Error:Conflict'));
&print_editform($rawmsg, $form{myLastModified}, frozen=>0, conflict=>1);
&print_footer($page);
return 1;
}
sub get_now {
my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);
$year += 1900;
$mon++;
$mon = "0$mon" if $mon < 10;
$day = "0$day" if $day < 10;
$hour = "0$hour" if $hour < 10;
$min = "0$min" if $min < 10;
#$sec = "0$sec" if $sec < 10;
return "$year-$mon-$day $hour:$min";
}
sub init_InterWikiName {
my @content = split /\n/, $database{InterWikiName};
for (@content) {
if (/^([^#]\S*)\s+(\S[^\x0A\x0D]+)/) {
$interwiki{$1} = $2;
}
}
$interwiki{'[[]]'} = 1; ## dummy
}
sub get_info {
my ($page, $key) = @_;
my %info = map { split(/=/, $_, 2) } split(/\n/, $infobase{$page});
return $info{$key};
}
sub set_info {
my ($page, $key, $value) = @_;
my %info = map { split(/=/, $_, 2) } split(/\n/, $infobase{$page});
$info{$key} = $value;
my $s = '';
for (keys %info) {
$s .= "$_=$info{$_}\n";
}
$infobase{$page} = $s;
}
sub frozen_reject {
my ($isfrozen) = &get_info($form{mypage}, $info_IsFrozen);
my ($willbefrozen) = $form{myfrozen};
if (not $isfrozen and not $willbefrozen) {
# You need no check.
return 0;
} elsif (valid_password($form{mypassword})) {
# You are admin.
return 0;
} else {
&print_error(&Resource('Error:PasswordIsIncorrect'));
return 1;
}
}
sub is_frozen {
my ($page) = @_;
if (&get_info($page, $info_IsFrozen)) {
return 1;
} else {
return 0;
}
}
sub do_comment {
my ($content) = $database{$form{mypage}};
my $default_name; ## this code does not strict.
$default_name = $1 if $content =~ /default-name="([^"]+)"/;
my $datestr = '[WEAK['.&get_now.']]';
my $namestr = $form{myname} || $default_name || &Resource('WikiForm:WikiComment:DefaultName');
($namestr = '', $datestr = '') if $form{myname} eq 'nodate';
if ($namestr =~ /^(?:>>)?[0-9]/) {
$namestr = qq( ''$namestr'': );
} elsif (length $namestr) {
$namestr = qq( ''[[$namestr]]'': );
}
my $anchor = &get_new_anchor_index ($content);
my $i = 1; my $o = 0;
$content =~ s{(\[\[\#r?comment\]\])}{
my $embed = $1;
if ($i == $form{comment_index}) {
if ($embed ne '[[#rcomment]]') {
$embed = "- [$anchor] $datestr$namestr$form{mymsg}\n$embed"; $o = 1;
} else {
$embed .= "\n- [$anchor] $datestr$namestr$form{mymsg}"; $o = 1;
}
}
$i++; $embed;
}ge;
unless ($o) {
$content = "#?SuikaWiki/0.9\n\n" unless $content;
$content .= "\n" unless $content =~ /\n$/s;
$content .= "- [$anchor] $datestr$namestr$form{mymsg}\n";
}
$form{__comment_anchor_index} = $anchor;
if ($form{mymsg} || $form{myname}) {
$form{mymsg} = $content;
$form{mytouch} = 'on';
&do_write;
} else {
$form{mycmd} = 'read';
&do_read;
}
}
sub get_new_anchor_index ($) {
my $content = shift;
my $anchor = 0;
$content =~ s/^(?:[-=]+\s*)?\[([0-9]+)\]/$anchor = $1 if $1 > $anchor; $&/mge;
$anchor + 1;
}
my $CommentIndex = 0;
sub embedded_to_html {
my ($embedded) = @_;
if ($embedded eq '[[#comment]]' or $embedded eq '[[#rcomment]]') {
unless ($main::_EMBEDED) {
my $lastmodified = &get_info($form{mypage}, $info_LastModified);
return <<"EOD";
EOD
} else {
return <<"EOD";
EOD
}
} elsif ($embedded =~ /$embed_command{searched}/) {
return get_search_result ($1, -match_myself => 1);
} elsif ($embedded =~ /^\[\[\#embed:(.+)\]\]$/) {
my ($name, $r) = ($1, '');
if ($main::_EMBEDED != 1) {
my ($content, $cf) = ($database{$name}, 'SuikaWiki/0.9');
$cf = $1 if $content =~ s!^(?:[\#<]\?|/\*\s*)?([A-Z][A-Za-z0-9-]+/[0-9.]+(?:[^0-9.][^\x0D\x0A]*)?)[\x0D\x0A]+!!s;
if ($cf =~ m!^(?:\#\?)?SuikaWiki/0.9(?:$|\s)!) {
$main::_EMBEDED = 1;
$r = &text_to_html ($content, content_format => $cf);
$main::_EMBEDED = 0;
} elsif (length $content) {
$r = "@{[&escape ($content)]}";
} else {
$r = &text_to_html ("[INS[\n[[$name]]: @{[&Resource('Embed:PageNotFound')]}\n]INS]\n", content_format => 'SuikaWiki/0.9');
}
} else { ## nested #EMBED
$r = &text_to_html ("[INS[\n[[$name]]: @{[&Resource('Embed:Nested',escape=>1)]}\n]INS]\n", content_format => 'SuikaWiki/0.9');
}
return qq($r
);
} elsif ($embedded =~ /^\[\[\#randomlink:(.+)\]\]$/) {
return qq($1);
} else {
return $embedded;
}
}
sub load_formatter (@) {
for my $t (@_) {
unless ($fmt{$t}) {
require Message::Util::Formatter;
$fmt{$t} = Message::Util::Formatter->new;
for (@{$SuikaWiki::Plugin::List{'wiki'.$t}||[]}) {
$_->load_formatter ($fmt{$t}, type => 'wiki'.$t);
}
}
}
}
sub do_wikiform {
my $content = $database{$form{mypage}};
my $anchor = &get_new_anchor_index ($content);
&load_formatter (qw/form_template form_option/);
my $write = 0;
my $i = 1;
$content =~ s{$embed_command{form}}{
my ($embed, $wfname, $template, $option) = ($&, $1, $3, $4);
if (($wfname && $wfname eq $form{wikiform_targetform})
|| $i == $form{wikiform_index}) {
$template =~ s/\\(.)/$1/g;
$option =~ s/\\(.)/$1/g;
my $param = bless {}, 'SuikaWiki::Plugin';
$param->{page} = $form{mypage};
$param->{form_index} = $i;
$param->{form_name} = $wfname;
$param->{anchor_index} = $anchor;
$param->{argv} = \%form;
$param->{default_name} = $1 if $content =~ /default-name="([^"]+)"/;
$param->{default_name} ||= &Resource('WikiForm:WikiComment:DefaultName');
$fmt{form_option}->replace ($option, $param);
my $t = 1;
for (@{$param->{require}||[]}) {
(undef $t, last) unless length $param->{argv}->{'wikiform__'.$_};
}
$t = $fmt{form_template}->replace ($template, $param) if $t;
if (length $t) {
if ($param->{output}->{reverse}) {
$embed .= "\n" . $t;
} else {
$embed = $t . "\n" . $embed;
}
$write = 1;
$form{__comment_anchor_index} = $anchor
if $param->{anchor_index_}; ## $anchor is used!
}
$form{__wikiform_anchor_index} = $i;
undef $form{wikiform_targetform}; ## make sure never to match
undef $form{wikiform_index}; ## with WikiForm in rest of page
}
$i++; $embed;
}ge;
unless ($write) {
#$content = "#?SuikaWiki/0.9\n\n" unless $content;
#$content .= "\n" unless $content =~ /\n$/s;
#
}
if ($write) {
$form{mymsg} = $content;
$form{mytouch} = 'on';
&do_write;
} else {
$form{mycmd} = 'read';
&do_read;
}
}
sub code_convert {
require Jcode;
my ($contentref, $code) = (shift, shift || $kanjicode);
$code = 'jis' if $code =~ /iso/;
$code = 'euc' if $code =~ /euc/;
$code = 'sjis' if $code =~ /shift/;
$code = 'utf8' if $code =~ /utf/;
#&Jcode::convert($contentref, $code); # for Jcode.pm
# &jcode::convert($contentref, $code); # for jcode.pl
#&Jcode::tr ($contentref, "\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&,.:;?!`^_/|()[]{}+$%#*@='"~-><\ )) if $code eq 'euc';
$$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;
return $$contentref;
}
sub do_diff {
if (not &is_editable($form{mypage})) {
&do_read;
return;
}
&open_diff;
my $title = $form{mypage};
&print_header($title, -noindex => 1);
$_ = &escape($diffbase{$form{mypage}});
&close_diff;
print qq(@{[&Resource('Diff:Title',escape=>1)]}
);
print qq(@{[&Resource('Diff:Notice',escape=>1)]}
);
print qq();
foreach (split(/\n/, $_)) {
if (/^\+(.*)/) {
print qq($1\n);
} elsif (/^\-(.*)/) {
print qq($1\n);
} elsif (/^\=(.*)/) {
print qq($1\n);
} else {
print qq|??? $_\n|;
}
}
print qq();
&print_footer($title);
}
sub do_rss {
eval q{use Yuki::RSS};
my $rss = new Yuki::RSS(
version => '1.0',
encoding => &get_charset_name ($kanjicode),
);
my $scheme = 'http';
$scheme = lc $1 if $main::ENV{SERVER_PROTOCOL} =~ m#([A-Za-z0-9+.%-]+)#;
my $myuri = "$scheme://$main::ENV{SERVER_NAME}:$main::ENV{SERVER_PORT}$url_cgi";
$rss->stylesheet (
href => $myuri . "?mycmd=TEXT_CSS;mypage=WikiStyle:RSS",
type => 'text/css',
);
$rss->channel(
title => &Resource ('RSS:WikiTitle'),
link => $myuri,
description => &Resource ('RSS:WikiDescription'),
'dc:language' => $lang,
);
my $recentchanges = $database{RecentChanges};
my $count = 0;
foreach (split(/\n/, $recentchanges)) {
last if ($count >= 15);
if (/\[\[([^]]+)\]\]/) {
my $title = $1;
$rss->add_item (
title => &escape($title),
link => $myuri . '?' . &encode($title),
description => &escape(&get_subjectline($title,delimiter=>'')),
'dc:date' => &get_info ($title, $info_LastModified),
);
$count++;
}
}
# print RSS information (as XML).
print <<"EOD"
Content-type: application/xml; charset=@{[&get_charset_name ($kanjicode)]}
@{[$rss->as_string]}
EOD
}
sub _rfc3339_date ($) {
my @time = gmtime (shift);
sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
}
sub is_exist_page {
my ($name) = @_;
if ($use_exists) {
return exists($database{$name});
} else {
return $database{$name};
}
}
sub __get_database ($) { $database{ $_[0] } }
sub __set_database ($$) { $database{ $_[0] } = $_[1] }
sub do_map {
my $page = $form{mypage};
&print_header ($page);
wiki::referer::add ($form{mypage}, $ENV{HTTP_REFERER});
wiki::useragent::add ($ENV{HTTP_USER_AGENT});
my ($r, $c) = get_search_result ($form{mypage});
my $rl = wiki::referer::list_html ($form{mypage});
print "@{[&Resource('Map:Title',escape=>1)]}
\n@{[&Resource('Map:Description',escape=>1)]}
\n";
my %option = (level => 0+&Resource('Map:Depth'), weight_list => {}, not_exist => {},
map_from_here => &Resource('Map:FromHere'),
map_from_here_description => &Resource('Map:FromHereLong'));
&wiki::map::make_list ($page, %option);
print &wiki::map::list_to_html ($page, $option{weight_list}, %option);
if ($c) {
print qq{@{[&Resource('SeeAlso',escape=>1)]}
};
print $r;
}
if ($rl) {
print qq(@{[&Resource('Referers',escape=>1)]}
\n$rl\n);
}
&print_footer ($page);
}
my %_Resource;
sub Resource ($;%) {
my ($s, %o) = @_;
unless (defined $_Resource{$s}) {
$_Resource{$_[0]} = &wiki::resource::get ($s, $_Resource{__option});
}
$o{escape} ? &escape ($_Resource{$s}) : $_Resource{$s};
}
package wiki::referer;
sub add ($$) {
my $page = shift;
my $uri = shift;
unless (ref $uri) {
require URI;
$uri = URI->new ($uri);
## Some schemes do not have query part.
eval q{ $uri->query (undef) if $uri->query =~ /^[0-9]{6,8}$/ };
$uri->fragment (undef);
}
$uri = $uri->canonical;
return unless $uri;
for my $regex (&get_dont_record) {
return if $uri =~ /$regex/;
}
my %list = get ($page);
$list{ $uri }++;
set ($page, \%list);
}
sub get ($) {
my $page = shift;
split /"/, main::get_info ($page, 'Referer');
}
sub set ($%) {
my $page = shift;
my $list = shift;
main::set_info ($page, Referer => join '"', %$list);
}
sub get_dont_record () {
map {s/\$/\\\$/g; s/\@/\\\@/g; $_}
grep !/^#/,
split /[\x0D\x0A]+/, &main::__get_database ('RefererDontRecord');
}
sub get_site_name () {
my @lines = grep /[^#]/, split /[\x0D\x0A]+/, &main::__get_database('RefererSiteName');
my @item;
for (@lines) {
next if /^#/;
my ($uri, $name) = split /\s+/, $_, 2;
$uri =~ s/\$/\\\$/g; $uri =~ s/\@/\\\@/g; $uri =~ s/\//\\\//g;
$name =~ s!([()/\\])!\\$1!g; $name =~ s/\$([0-9]+)/).__decode (\${$1}).q(/g;
push @item, [$uri, qq(q($name))];
}
@item;
}
sub list_html ($) {
my $page = shift;
my %list = get ($page);
my $r = '';
my @name = get_site_name;
for my $uri (sort keys %list) {
my $title;
for my $item (@name) {
if ($uri =~ /$item->[0]/) {
$title = $uri;
eval qq{\$title =~ s/^.*$item->[0].*\$/$item->[1]/e}
or die $@ ;#. qq{\$title =~ s/^.*$item->[0].*\$/$item->[1]/e};
last;
}
}
my $euri = main::escape ($uri);
if ($title) {
$r .= qq([$list{$uri}] @{[main::escape ($title)]} \n);
} else {
$r .= qq([$list{$uri}] <$euri> \n);
}
}
$r ? qq($r
\n) : '';
}
sub __decode ($) {
my $s = shift;
$s =~ tr/+/ /;
$s =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge;
main::code_convert (\$s);
}
package wiki::useragent;
our $UseLog;
sub add ($) {
my $s = shift;
return unless length $s;
return unless $UseLog;
$s =~ s/([\x00-\x08\x0A-\x1F\x25\x7F-\xFF])/sprintf '%%%02X', unpack 'C', $1/ge;
my %ua;
for (split /\n/, &main::__get_database($main::PageName{UserAgentList})) {
if (/^-\[(\d+)\] (.+)$/) {
my ($t, $n) = ($1, $2);
$n =~ tr/\x0A\x0D//d;
$ua{$n} = $t;
}
}
$ua{$s}++;
my $s = qq(#?SuikaWiki/0.9\n);
for (sort {$ua{$a} <=> $ua{$b}} keys %ua) {
$s .= sprintf qq(-[%d] %s\n), $ua{$_}, $_;
}
&main::__set_database ($main::PageName{UserAgentList} => $s);
}
package wiki::suikawikiconst;
sub to_hash ($;$) {
my $page = shift;
my $h = shift || {};
my $val;
for my $line (split /\n/, $page) {
$line =~ tr/\x0A\x0D//d;
if ($val && $line =~ s/^\s+//) {
$h->{$val} .= length $h->{$val} ? "\n" . $line : $line;
} elsif ($line =~ /^(.+):/) {
$val = $1; $h->{$val} = '';
}
}
$h;
}
package SuikaWiki::Plugin;
our $plugin_directory; # defined in top of this file.
our %List;
sub escape ($$) { main::escape ($_[1]) }
sub unescape ($$) { main::unescape ($_[1]) }
sub encode ($$) { main::encode ($_[1]) }
sub decode ($$) { main::decode ($_[1]) }
sub __get_datetime ($) { main::get_now () }
sub resource ($$;%) { shift; &main::Resource (@_) }
sub uri ($$) { $main::uri{$_[1]} }
sub regist ($@) {
my $pack = shift;
for (@_) {
push @{$List{$_}}, $pack;
}
}
sub import_plugins () {
opendir PDIR, $plugin_directory;
my @plugin = grep {s/\.pm$//} readdir (PDIR);
closedir PDIR;
for (@plugin) {
eval qq{ use SuikaWiki::Plugin::$_ } unless /[^A-Za-z0-9_]/;
push @{$List{_all}}, qq(SuikaWiki::Plugin::$_);
}
}
&import_plugins ();
package wiki::conneg;
## BUG: this parser isn't strict.
sub get_accept_lang (;$) {
my $alang = shift || $main::ENV{HTTP_ACCEPT_LANGUAGE};
my %alang = (ja => 0.0002, en => 0.0001);
my $i = 0.1;
for (split /\s*,\s*/, $alang) {
tr/\x09\x0A\x0D\x20//d;
if (/((?:(?!;q=).)+)(?:;q="?([0-9.]+)"?)?/) {
my $l = lc $1; $l =~ tr/\x22\x5C//d;
$alang{$l} = (defined $2 ? $2 : 1.000)*1000;
$alang{$l} += $i unless $alang{$l} == 0;
$i -= 0.001;
}
}
\%alang;
}
package wiki::resource;
sub get ($;\%) {
my ($resname, $option) = @_;
$option->{accept_language} ||= &wiki::conneg::get_accept_lang ();
$option->{resource} ||= {};
my $v;
for my $lang (sort {$option->{accept_language}->{$b} <=> $option->{accept_language}->{$a}} grep {$option->{accept_language}->{$_}!=0} keys %{$option->{accept_language}}) {
while (length $lang) {
unless ($option->{accept_language}->{defined $option->{accept_language}->{$lang} ? $lang : '*'} == 0) {
$option->{resource}->{$lang} ||= &wiki::suikawikiconst::to_hash (&main::__get_database('WikiResource:'.$lang));
$v = $option->{resource}->{$lang}->{$resname};
last if defined $v;
}
$lang =~ s/[^+-]*$//; $lang =~ s/[+-]$//;
}
last if defined $v;
}
defined $v ? $v : $resname;
}
package wiki::map;
sub make_list ($;%) {
my ($page, %option) = @_;
$option{level} ||= 3;
my %weight;
my $content = &main::__get_database ($page);
$content =~ s{^\#\?([^\x0A\x0D]+)}{
if ($1 =~ /import="([^"]+)"/) {
for (split /\s*,\s*/, $1) {
$weight{$_} += 2;
}
}
$&;
}ges;
## Bug: this code does not support content type.
$content =~ s{\[\[((?!\#)[^]]+)\](?:>>\d+)?\]}{
$weight{$1}++; $&;
}ge;
delete $weight{$page}; ## Delete myself
for my $page (keys %weight) {
my $w = ($content =~ s/\Q$page\E/$&/g);
$weight{$page} += $w + $weight{$page}; ## Weight of [[name]] is x2.
($weight{$page} *= 0.1, $option{not_exist}->{$page} = 1) unless &main::is_exist_page ($page);
}
$option{weight_list}->{$page} = \%weight;
if (--$option{level}) {
for my $page (keys %weight) {
&make_list ($page, %option) unless $option{weight_list}->{$page};
}
}
$option{weight_list};
}
sub list_to_html ($$;%) {
my ($Page, $wlist, %option) = @_;
my $r = '';
$option{outputed}->{$Page} = 1;
for my $page (sort {$wlist->{$Page}->{$b} <=> $wlist->{$Page}->{$a}} keys %{$wlist->{$Page}}) {
$r .= qq([@{[0+$wlist->{$Page}->{$page}]}] @{[&main::escape ($page).($option{not_exist}->{$page}?qq(@{[&main::Resource('JumpAndEditWikiPageMark',escape=>1)]}):'')]} @{[&main::escape($option{map_from_here})]} @{[&main::escape(&main::get_subjectline($page))]});
unless ($option{outputed}->{$page}) {
$r .= &list_to_html ($page, $wlist, %option);
}
$r .= " \n";
}
$r ? qq($r
) : '';
}
package main;
&main;
exit 0;
1;
__END__
=head1 NAME
wiki.cgi - This is YukiWiki, yet another Wiki clone.
walwiki.cgi based on yukiwiki.cgi - Yet another WikiWikiWeb clone.
=head1 DESCRIPTION
YukiWiki is yet another Wiki clone.
YukiWiki can treat Japanese WikiNames (enclosed with [[ and ]]).
YukiWiki provides 'InterWiki' feature, RDF Site Summary (RSS),
and some embedded commands (such as [[#comment]] to add comments).
Read F (English) or F (Japanese) in more detail.
=head1 AUTHOR
Hiroshi Yuki http://www.hyuki.com/yukiwiki/
=head1 LICENSE
Copyright (C) 2000-2002 by Hiroshi Yuki.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut