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

Contents of /suikawiki/script/wiki.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.48 - (hide annotations) (download)
Fri Jan 3 08:25:31 2003 UTC (21 years, 10 months ago) by w
Branch: MAIN
Changes since 1.47: +74 -218 lines
Many builtin functions are removed and implemented as plugin modules

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24