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

Contents of /suikawiki/script/wiki.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.46 - (show annotations) (download)
Thu Jan 2 12:14:15 2003 UTC (21 years, 4 months ago) by w
Branch: MAIN
Changes since 1.45: +25 -123 lines
do_index: Removed.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24