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

Contents of /suikawiki/script/wiki.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.49 - (hide annotations) (download)
Sat Jan 4 03:32:55 2003 UTC (21 years, 4 months ago) by w
Branch: MAIN
Changes since 1.48: +63 -138 lines
Fix to more customizable

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24