/[pub]/suikawiki/script/lib/suikawiki.pl
Suika

Contents of /suikawiki/script/lib/suikawiki.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download)
Sat May 10 05:58:49 2003 UTC (22 years ago) by wakaba
Branch: MAIN
Changes since 1.5: +6 -106 lines
File MIME type: text/plain
Remove some packages and moved to plugin

1 # -*- perl -*-
2 use strict;
3
4 package main;
5 our $VERSION = '2.'.do{my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
6 binmode STDOUT; binmode STDIN;
7 use Fcntl;
8 require Yuki::YukiWikiCache;
9 require SuikaWiki::Name::Space;
10 require SuikaWiki::View;
11 require SuikaWiki::Plugin;
12 our %fmt; ## formatter objects
13 our %embed_command = (
14 form => qr/\[\[\#form(?:\(([A-Za-z0-9-]+)\))?:'((?:[^'\\]|\\.)*)':'((?:[^'\\]|\\.)*)'(?::'((?:[^'\\]|\\.)*)')?\]\]/,
15 );
16 our ($modifier_dbtype,$url_cgi,%uri,%PathTo);
17 our (%PageName,$kanjicode,$lang);
18
19 our %form;
20 our %database;
21 our $database = bless {}, 'wiki::dummy';
22 our %interwiki;
23 my %command_do = (
24 default => \&do_view,
25 adminchangepassword => \&do_adminchangepassword,
26 write => \&do_write,
27 searchform => \&do_searchform,
28 comment => \&do_comment,
29 RandomJump => \&do_random_jump,
30 wikiform => \&do_wikiform,
31 );
32 our $UA = ''; ## User agent name
33 $| = 1;
34 my $HAS_XML = SuikaWiki::Plugin->feature ('SuikaWiki::Markup::XML');
35 my $NS_XHTML1 = 'http://www.w3.org/1999/xhtml';
36
37 sub main {
38 $UA = $main::ENV{HTTP_USER_AGENT};
39 &open_db;
40 &init_form;
41 for (@{$SuikaWiki::Plugin::On{WikiDatabaseLoaded}||[]}) {
42 &{$_};
43 }
44 if ($command_do{$form{mycmd}}) {
45 &{$command_do{$form{mycmd}}};
46 } else {
47 &{$command_do{default}};
48 }
49 &close_db;
50 }
51
52 sub do_view {
53 my $content = $database{$form{mypage}};
54 my $lm = $database->mtime ($form{mypage});
55 &load_formatter ('view');
56 my $view = $form{mycmd};
57 if ($view eq 'edit') {
58 $view = 'adminedit' if $form{admin};
59 } elsif ($view =~ /[^0-9A-Za-z_]/) {
60 $view = 'default'
61 }
62 if ($view eq 'default' || !$view) {
63 ## BUG: this code is not strict
64 if ($main::ENV{HTTP_COOKIE} =~ /SelectedMode=([0-9A-Za-z]+)/) {
65 $view = $1;
66 } else {
67 $view = 'read';
68 }
69 }
70 my ($magic, $content) = &SuikaWiki::Plugin::magic_and_content (undef, $content);
71 $magic ||= '#?SuikaWiki/0.9';
72 my $o = bless {param => \%form, page => $form{mypage}, toc => [],
73 magic => $magic, content => $content,
74 formatter => $fmt{view}, &_compatible_options ()}, 'SuikaWiki::Plugin';
75 my $view_def = SuikaWiki::View->definition ($view);
76 if (!$view_def->check ($o)) {
77 print "Status: 406 Unsupported Media Type\n";
78 $view = '-UnsupportedMediaType';
79 $view_def = SuikaWiki::View->definition ($view);
80 }
81 my $media = $view_def->properties->{media};
82 if ($view_def->properties->{xmedia} && $UA =~ /Gecko/) {
83 $media = $view_def->properties->{xmedia};
84 $o->{media} = $media;
85 } elsif ($UA =~ m#Mozilla/0\..+Windows#) {
86 $kanjicode = 'shift_jis';
87 }
88 if ($magic =~ m!^\#\?SuikaWiki/0.9!) {
89 &print_header ($form{mypage}, -last_modified => ($magic =~ /interactive="yes"/ ? time : $lm),
90 -expires => ($magic =~ /interactive="yes"/ ? 1 : undef), o => $o,
91 -media => $media, -magic => $magic, content => $content);
92 } else {
93 &print_header($form{mypage}, -media => $media,
94 -magic => $magic, -last_modified => $lm, o => $o);
95 }
96 if ($kanjicode ne 'euc') {
97 my $s = $fmt{view}->replace ($view_def->as_string => $o, {formatter => $fmt{view}});
98 print &code_convert (\$s => $kanjicode);
99 } else {
100 print $fmt{view}->replace ($view_def->as_string => $o, {formatter => $fmt{view}});
101 }
102 }
103
104 sub _do_view_msg (%) {
105 my %option = @_;
106 &load_formatter ('view');
107 my $o = bless {param => \%form, page => $option{-page}, toc => [], condition => \%option,
108 formatter => $fmt{view}, &_compatible_options ()}, 'SuikaWiki::Plugin';
109 my $view_def = SuikaWiki::View->definition ($option{-view});
110 unless ($view_def->check ($o)) {
111 print "Status: 406 Unsupported Media Type\n";
112 $option{-view} = '-UnsupportedMediaType';
113 $view_def = SuikaWiki::View->definition ($option{-view});
114 }
115 my $media = $view_def->properties->{media};
116 if ($view_def->properties->{xmedia} && $UA =~ /Gecko/) {
117 $media = $view_def->properties->{xmedia};
118 $o->{media} = $media;
119 }
120 &print_header($option{-page}, -media => $media, o => $o, -goto => $option{-goto});
121 print $fmt{view}->replace ($view_def->as_string => $o, {formatter => $fmt{view}});
122 }
123
124 sub do_adminchangepassword {
125 if ($form{mynewpassword} ne $form{mynewpassword2}) {
126 &_do_view_msg (-view => '-error', -page => $form{mypage},
127 error_message => &Resource ('Error:PasswordMismatch'));
128 return;
129 }
130 my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage});
131 if ($validpassword_crypt) {
132 if (not &valid_password($form{myoldpassword})) {
133 &_do_view_msg (-view => '-error', -page => $form{mypage},
134 error_message => &Resource ('Error:PasswordIsIncorrect'));
135 return;
136 }
137 }
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 $database->meta (AdminPassword => $PageName{AdminSpecialPage} => $crypted);
144
145 &_do_view_msg (-view => '-wrote', -page => $form{mypage});
146 }
147
148 sub valid_password ($) {
149 my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage});
150 return crypt (shift, $validpassword_crypt) eq $validpassword_crypt ? 1 : 0;
151 }
152
153 sub do_write {
154 if (&frozen_reject()) {
155 return;
156 }
157
158 if (not &is_editable($form{mypage})) {
159 &_do_view_msg (-view => '-error', -page => $form{mypage},
160 error_message => &Resource ('Error:ThisPageIsUneditable'));
161 return;
162 }
163
164 ## Check confliction
165 if ($form{myLastModified} ne $database->mtime ($form{mypage})) {
166 &_do_view_msg (-view => '-conflict', -page => $form{mypage});
167 return;
168 }
169
170 if ($form{mymsg}) {
171 if ($form{mytouch} || !ref $database) {
172 $database{$form{mypage}} = $form{mymsg};
173 } else {
174 $database->STORE ($form{mypage} => $form{mymsg}, -touch => 0);
175 }
176 $database->meta (IsFrozen => $form{mypage} => 0 + $form{myfrozen});
177 my $fragment = '';
178 $fragment .= qq(;after_edit_cmd=@{[&encode($form{after_edit_cmd})]}) if $form{after_edit_cmd};
179 if ($form{__comment_anchor_index}) {
180 $fragment .= qq(#anchor-$form{__comment_anchor_index});
181 } elsif ($form{__wikiform_anchor_index}) {
182 $fragment .= qq(#wikiform-$form{__wikiform_anchor_index});
183 }
184 &_do_view_msg (-view => '-wrote', -page => $form{mypage}, -goto => $url_cgi.'?mycmd='.&encode($form{after_edit_cmd}||'default').';mypage='.&encode($form{mypage}).qq(;x-param=@{[time.[0..9]->[rand 10]]}$fragment));
185 } else {
186 delete $database{$form{mypage}};
187 &_do_view_msg (-view => '-deleted', -page => $form{mypage});
188 }
189 }
190
191 sub _compatible_options () {
192 (use_anchor_name => ($UA =~ m#Mozilla/[12]\.|Microsoft Internet Explorer# ? 1 : 0));
193 }
194
195 sub get_search_result ($;%) {
196 my $word = lc shift;
197 my $SearchResult = SuikaWiki::Plugin->cache ('search');
198 my %option = @_;
199 my @r;
200 unless (defined $SearchResult->{$word}) {
201 for my $page (keys %database) {
202 next if !$option{-match_myself} && ($page eq $word);
203 my $content = lc $database{$page};
204 $content =~ s/^[^\x0A\x0D]+[\x0D\x0A]+//s;
205 if (index (lc $page, $word) > -1) {
206 my $c = $content =~ s/\Q$word\E//g;
207 push @r, [$page, $c+20];
208 } elsif (index ($word, lc $page) > -1) {
209 my $c = $content =~ s/\Q$word\E//g;
210 push @r, [$page, $c+10];
211 } elsif (my $c = $content =~ s/\Q$word\E//g) {
212 push @r, [$page, $c];
213 }
214 }
215 @r = sort {$b->[1] <=> $a->[1] || $a->[0] cmp $b->[0]} @r;
216 $SearchResult->{$word} = join "\x1E", map {$_->[0]."\x1F".$_->[1]} @r;
217 } else {
218 @r = map {[split /\x1F/, $_, 2]} split /\x1E/, $SearchResult->{$word};
219 }
220 #my $em = sub { my $s = shift; $s =~ s#(\Q$word\E)#<em>$1</em>#gi; $s };
221 my $r = join "\n", map {qq(<li>[$_->[1]] <a href ="$url_cgi?@{[&encode($_->[0])]}" class="wiki">@{[&escape($_->[0])]}</a> <span class="wikipage-summary">@{[&escape(&get_subjectline($_->[0]))]}</span></li>)} @r;
222 $r = qq|<ul class="search-result">$r</ul>| if $r;
223 wantarray? ($r, scalar @r): $r;
224 }
225
226 sub do_random_jump {
227 my @list = keys %database;
228 my $name = &encode ($list[rand @list]);
229 print "Location: $uri{wiki}?$name\n";
230 print "\n";
231 }
232
233 sub print_header ($;%) {
234 my ($page, %option) = @_;
235 my @head;
236 $option{o}->{-header}->{class} = &is_frozen($page) ? 'frozen' : '';
237 $option{o}->{-header}->{class} .= " wiki-page-obsoleted" if $option{-magic} =~ /obsoleted="yes"/;
238 if ($option{-goto}) {
239 if ($UA =~ m#Opera|MSIE 2\.#) {
240 ## WARNING: This code may output unsafe HTML document if
241 ## $option{-goto} is not clean.
242 $option{-goto} =~ tr/;/&/ if $UA =~ m#Opera#;
243 print qq{Refresh: 0; url=$option{-goto}\n};
244 push @head, qq(<meta http-equiv="refresh" content="0; url=$option{-goto}">);
245 } elsif ($UA =~ /Gecko/) {
246 print qq{Refresh: 0; url="$option{-goto}"\n};
247 push @head, qq(<meta http-equiv="refresh" content="0; url=&quot;@{[&escape($option{-goto})]}&quot;" />);
248 } else {
249 $option{-goto} =~ tr/;/&/ if $UA =~ m#Mozilla/[1-4]\.#;
250 print qq{Refresh: 0; url="$option{-goto}"\n};
251 push @head, qq(<meta http-equiv="refresh" content="0; url=&quot;@{[&escape($option{-goto})]}&quot;">);
252 }
253 }
254 print qq{Last-Modified: @{[scalar gmtime $option{-last_modified}]}\n} if $option{-last_modified};
255 if ($option{-expires} != -1) {
256 if (defined $option{-expires}) { ## TODO: Don't use asctime
257 print qq{Expires: @{[scalar gmtime (time + $option{-expires})]}\n};
258 } elsif ($option{-media}->{expires} != -1) {
259 print qq{Expires: @{[scalar gmtime (time + $option{-media}->{expires})]}\n};
260 }
261 }
262 if ($option{-media}->{charset} && $UA =~ m#Mozilla/[12]\.#) {
263 my $ct = qq{$option{-media}->{type}; charset=@{[&get_charset_name($kanjicode,compatible=>1)]}};
264 print qq{Content-Type: $ct\n};
265 $option{o}->{-header}->{meta_ct} = qq{<meta http-equiv="content-type" content="$ct">\n};
266 } elsif (!$option{-media}->{charset} || $UA =~ m#Infomosaic|Mozilla/0\.#) {
267 print qq{Content-Type: $option{-media}->{type}\n};
268 $option{o}->{-header}->{meta_ct} = qq{<meta http-equiv="content-type" content="$option{-media}->{type}; charset=@{[&get_charset_name($kanjicode,compatible=>1)]}">\n};
269 } else {
270 my $type = $option{-media}->{type};
271 $type = 'application/xml' if ($type =~ m!^application/(?:rdf|rss)\+xml$!) && ($UA =~ m#Gecko#);
272 print qq{Content-Type: $type; charset=@{[&get_charset_name($kanjicode)]}\n};
273 }
274 #if ($main::ENV{HTTP_IF_MODIFIED_SINCE}) {
275 ## TODO: IMS support
276 #}
277
278 ## TODO: more Vary: support
279 print <<"EOD";
280 Vary: Negotiate,User-Agent,Accept-Language
281 Content-Style-Type: text/css
282
283 EOD
284 $option{o}->{-header}->{links} = join "\n", (@head);
285 }
286
287 sub get_charset_name ($;%) {
288 my ($charset, %option) = (lc shift, @_);
289 if ($charset =~ 'euc') {
290 $charset = $option{compatible} ? 'x-euc-jp' : 'euc-jp';
291 } elsif ($charset =~ 'sjis' || $charset =~ 'shift') {
292 $charset = $option{compatible} ? 'x-sjis' : 'shift_jis';
293 } elsif ($charset =~ 'jis') {
294 $charset = 'iso-2022-jp';
295 }
296 $charset;
297 }
298
299 sub escape {
300 my $s = shift;
301 $s =~ s|&|&amp;|g;
302 $s =~ s|<|&lt;|g;
303 $s =~ s|>|&gt;|g;
304 $s =~ s|"|&quot;|g;
305 return $s;
306 }
307
308 sub unescape {
309 my $s = shift;
310 $s =~ s|&lt;|<|g;
311 $s =~ s|&gt;|>|g;
312 $s =~ s|&quot;|"|g;
313 $s =~ s|&amp;|&|g;
314 return $s;
315 }
316
317 sub convert_format ($$$;%) {
318 my ($content, $d => $t, %option) = @_;
319 my $f = SuikaWiki::Plugin->format_converter ($d => $t);
320 if (ref $f) {
321 $option{content} = $content;
322 $option{from} = $d;
323 $option{to} = $t;
324 &$f ({}, bless (\%option, 'SuikaWiki::Plugin'));
325 } elsif ($option{-error_no_return}) {
326 return undef;
327 } elsif ($t =~ /HTML|xml/) {
328 length $content ? '<pre>'.&escape($content).'</pre>' : '';
329 } else {
330 $content;
331 }
332 }
333
334 {my %FormIndex;
335 sub make_custom_form ($$$$%) {
336 my ($wfname, $definition, $template, $foption, $option) = @_;
337 ## $template is currently not used in this procedure.
338 #unless ($main::_EMBEDED) {
339 $FormIndex{$option->{page}}++;
340 if (length $definition) {
341 my $param = bless {depth=>10}, 'SuikaWiki::Plugin';
342 my $lastmodified = $database->mtime ($option->{page});
343 &load_formatter (qw/form_input form_option/);
344 $definition = &unescape ($definition);
345 $definition =~ s/\\(['\\])/$1/g;
346 $foption = &unescape ($foption);
347 $foption =~ s/\\(['\\])/$1/g;
348 $fmt{form_option}->replace ($foption, $param);
349 $param->{output}->{form} = 1 unless defined $param->{output}->{form};
350 $param->{output}->{form} = 0 if $main::_EMBEDED;
351 $definition .= ' %submit;' if $definition !~ /%submit/ && !$param->{output}->{nosubmit} && $param->{output}->{form};
352 $param->{output}->{page} ||= $option->{page};
353 $param->{form_disabled} = 1 if $database->meta (IsFrozen => $option->{page});
354 my $target_form = $param->{output}->{id};
355 my $r = '';
356 $r = <<EOH if $param->{output}->{form};
357 <form method="post" action="$url_cgi" id="wikiform-$FormIndex{$option->{page}}" class="wikiform">
358 <input type="hidden" name="mycmd" value="@{[$param->{form_disabled}?'default':'wikiform']}" />
359 <input type="hidden" name="mypage" value="@{[&escape($param->{output}->{page})]}" />
360 <input type="hidden" name="myLastModified" value="$lastmodified" />
361 <input type="hidden" name="mytouch" value="on" />
362 <input type="hidden" name="@{[$target_form? qq(wikiform_targetform" value="@{[&escape($target_form)]}) : qq(wikiform_index" value="$FormIndex{$option->{page}})]}" />
363 EOH
364 $r .= qq(<a name="wikiform-$FormIndex{$option->{page}}"></a>) if $UA =~ m#Mozilla/[12]\.#;
365 $r .= $fmt{form_input}->replace ($definition, $param);
366 $r .= "</form>\n" if $param->{output}->{form};
367 $r;
368 } else { ## No input-interface WikiForm
369 qq(<a id="wikiform-$FormIndex{$option->{page}}" name="wikiform-$FormIndex{$option->{page}}"><!-- #form --></a>);
370 }
371 #} else {
372 # qq(<ins class="wiki-error">@{[&Resource('Error:WikiForm:EmbedIsNotSupported',escape=>1)]}</ins>);
373 #}
374 }}
375
376 sub init_form {
377 ## TODO: Support multipart/form-data
378 my $query = '';
379 if (uc $main::ENV{REQUEST_METHOD} eq 'POST') {
380 if (lc ($main::ENV{CONTENT_TYPE}) eq 'application/x-www-form-urlencoded'
381 || lc ($main::ENV{CONTENT_TYPE}) eq 'application/sgml-form-urlencoded') {
382 read STDIN, $query, $main::ENV{CONTENT_LENGTH};
383 } else {
384 $form{mycmd} = '___unsupported_media_type___';
385 $form{mypage} = $PageName{FrontPage};
386 return;
387 }
388 }
389 $query .= ($query ? ';' : '') . $main::ENV{QUERY_STRING};
390 if ($main::ENV{REQUEST_METHOD} ne 'POST' && $main::ENV{QUERY_STRING} && $main::ENV{QUERY_STRING} !~ /[&;=]/) {
391 my $query = &decode($main::ENV{QUERY_STRING});
392 $query = &code_convert(\$query, $kanjicode);
393 $form{mypage} = $query;
394 $form{mycmd} = 'default';
395 } else {
396 for (split /[;&]/, $query) {
397 if (my ($n, $v) = split /=/, $_, 2) {
398 for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge};
399 $form{$n} = $v;
400 }
401 }
402 unless (defined $form{mypage}) {
403 $form{mypage} = $form{epage};
404 $form{mypage} =~ s/([0-9A-F]{2})/ord hex $1/g;
405 }
406 $form{mypage} = &code_convert (\$form{mypage}, $kanjicode);
407 }
408 $form{mypage} ||= $PageName{FrontPage};
409 $form{mypage} =~ tr/\x00-\x1F\x7F//d;
410 $form{mypage} = SuikaWiki::Name::Space::normalize_name ($form{mypage});
411 $form{mycmd} ||= 'default';
412 $form{mycmd} =~ tr/-/_/;
413
414 # mypreview_edit -> do_edit, with preview.
415 # mypreview_adminedit -> do_adminedit, with preview.
416 # mypreview_write -> do_write, without preview.
417 foreach (keys %form) {
418 if (/^mypreview_(.*)$/) {
419 $form{mycmd} = $1;
420 $form{mypreview} = 1;
421 }
422 }
423
424 #
425 # $form{mycmd} is frozen here.
426 #
427
428 for (grep /^(?:wikiform__|pi_)/, keys %form) {
429 $form{$_} = &code_convert (\$form{$_}, $kanjicode);
430 }
431 $form{mymsg} = &code_convert(\$form{mymsg}, $kanjicode);
432 $form{myname} = &code_convert(\$form{myname}, $kanjicode);
433 }
434
435 sub get_subjectline {
436 my ($page, %option) = @_;
437 my $SubjectLine = SuikaWiki::Plugin->cache ('headline');
438 unless (defined $SubjectLine->{$page}) {
439 if (not &is_editable($page)) {
440 $SubjectLine->{$page} = "";
441 } else {
442 $SubjectLine->{$page} = do {
443 my $s=$database{$page};
444 $s =~ s!^\#\?[^\x0A\x0D]+[\x0A\x0D]*!!s;
445 $s =~ s/\x0D?\x0A.*//s;
446 $s =~ s/^[-=]*\s*\[\d+\]\s*//;
447 $s =~ s/'''?//g;
448 $s =~ s/\[[A-Z]+(?:\([^)]+\))?\[([^]]+)\](?:\s\[([^]]+)\])?\]/$1$2/g;
449 $s =~ s/\[\[([^]]+)\]\]/$1/g;
450 $s};
451 }
452 }
453 if (length $SubjectLine->{$page}) {
454 $option{delimiter} = defined $option{delimiter} ? $option{delimiter} : &Resource('Title-Summary Delimiter');
455 $option{delimiter}.$SubjectLine->{$page}.$option{tail};
456 } else {
457 '';
458 }
459 }
460
461 sub open_db {
462 if ($modifier_dbtype eq 'dbmopen') {
463 dbmopen(%database, $PathTo{WikiDataBase}, 0666) or die "(dbmopen) $PathTo{WikiDataBase}";
464 } elsif ($modifier_dbtype eq 'AnyDBM_File') {
465 eval q{use AnyDBM_File};
466 tie(%database, "AnyDBM_File", $PathTo{WikiDataBase}, O_RDWR|O_CREAT, 0666) or die ("(tie AnyDBM_File) $PathTo{WikiDataBase}");
467 } elsif ($modifier_dbtype eq 'Yuki::YukiWikiDB') {
468 eval q{use Yuki::YukiWikiDB};
469 tie(%database, "Yuki::YukiWikiDB", $PathTo{WikiDataBase}) or die ("(tie Yuki::YukiWikiDB) $PathTo{WikiDataBase}");
470 } else { ## Yuki::YukiWikiDB || Yuki::YukiWikiDBMeta
471 eval qq{use $modifier_dbtype};
472 $database = tie(%database, $modifier_dbtype => $PathTo{WikiDataBase}, -lock => 2, -backup => $wiki::diff::UseDiff) or die ("(tie $modifier_dbtype) $PathTo{WikiDataBase}");
473 }
474 }
475
476 sub close_db {
477 if ($modifier_dbtype eq 'dbmopen') {
478 dbmclose(%database);
479 } else {
480 untie(%database);
481 }
482 }
483
484 sub editform (@) {
485 my %option = @_;
486 my $frozen = &is_frozen ($option{page});
487 $option{content} = $database{$option{page}} unless defined $option{content};
488 $option{content} = $database{NewPageTemplate} unless length $option{content};
489 $option{last_modified} = $database->mtime ($option{page}) unless defined $option{last_modified};
490 my $f = '';
491 my $magic = '';
492 $magic = $1 if $option{content} =~ m/^([^\x0A\x0D]+)/s;
493
494 my $selected = 'default';
495 if ($form{after_edit_cmd}) {
496 $selected = $form{after_edit_cmd};
497 } elsif ($magic =~ /Const|Config|CSS/) {
498 $selected = 'edit';
499 }
500 my $afteredit = <<EOH;
501 <select name="after_edit_cmd">
502 <option value="default" label="@{[&Resource('Edit:SaveAndDefault',escape=>1)]}"@{[$selected eq 'default' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndDefault',escape=>1)]}</option>
503 <option value="read" label="@{[&Resource('Edit:SaveAndView',escape=>1)]}"@{[$selected eq 'read' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndView',escape=>1)]}</option>
504 <option value="edit" label="@{[&Resource('Edit:SaveAndEdit',escape=>1)]}"@{[$selected eq 'edit' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndEdit',escape=>1)]}</option>
505 </select>
506 EOH
507 $f .= <<"EOD";
508 <form action="$uri{wiki}" method="post">
509 @{[ $option{conflict} ? '' : qq(<label><input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}" /><kbd>S</kbd></label>) ]}
510 @{[ $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 />
511 <input type="hidden" name="myLastModified" value="$option{last_modified}" />
512 <input type="hidden" name="mypage" value="@{[&escape($form{mypage})]}" />
513 <textarea cols="@{[&Resource('Edit:Form:Cols')+0||80]}" rows="@{[&Resource('Edit:Form:Rows')+0||20]}" name="mymsg" tabindex="1">@{[&escape($option{content})]}</textarea><br />
514 @{[
515 $option{admin} ?
516 qq(
517 <label><input type="radio" name="myfrozen" value="1" @{[$frozen ? qq(checked="checked") : ""]} />@{[&Resource('Edit:Freeze',escape=>1)]}</label>
518 <label><input type="radio" name="myfrozen" value="0" @{[$frozen ? "" : qq(checked="checked")]} />@{[&Resource('Edit:DontFreeze',escape=>1)]}</label><br />)
519 : ""
520 ]}
521 @{[
522 $option{conflict} ? "" :
523 qq(
524 <label><input type="checkbox" name="mytouch" value="on" checked="checked" />@{[&Resource('Edit:UpdateTimeStamp',escape=>1)]}</label><br />
525 <label><input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}" accesskey="S" /><kbd>S</kbd></label>
526 $afteredit
527 )
528 ]}
529 </form>
530 EOD
531 $f;
532 }
533
534 sub is_editable {
535 my ($page) = @_;
536 return 0 unless SuikaWiki::Name::Space::validate_name ($page);
537 return 0 if $page =~ /[\x00-\x20\[\]\x7F]/;
538 1;
539 }
540
541 sub decode {
542 my ($s) = @_;
543 $s =~ tr/+/ /;
544 $s =~ s/%([A-Fa-f0-9][A-Fa-f0-9])/pack("C", hex($1))/eg;
545 return $s;
546 }
547
548 sub encode {
549 my $s = shift;
550 $s =~ s/([^0-9A-Za-z_-])/sprintf '%%%02X', ord $1/ge;
551 $s;
552 }
553
554 sub get_now {
555 my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);
556 $year += 1900;
557 $mon++;
558 $mon = "0$mon" if $mon < 10;
559 $day = "0$day" if $day < 10;
560 $hour = "0$hour" if $hour < 10;
561 $min = "0$min" if $min < 10;
562 #$sec = "0$sec" if $sec < 10;
563 return "$year-$mon-$day $hour:$min";
564 }
565
566 sub frozen_reject {
567 my ($isfrozen) = $database->meta (IsFrozen => $form{mypage});
568 my ($willbefrozen) = $form{myfrozen};
569 if (not $isfrozen and not $willbefrozen) {
570 # You need no check.
571 return 0;
572 } elsif (valid_password($form{mypassword})) {
573 # You are admin.
574 return 0;
575 } else {
576 &_do_view_msg (-view => '-error', -page => $form{mypage},
577 error_message => &Resource ('Error:PasswordIsIncorrect'));
578 exit;
579 }
580 }
581
582 sub is_frozen ($) { $database->meta (IsFrozen => $_[0]) ? 1 : 0 }
583
584 sub do_comment {
585 my ($content) = $database{$form{mypage}};
586 my $default_name; ## this code is not strict.
587 $default_name = $1 if $content =~ /default-name="([^"]+)"/;
588 my $datestr = '[WEAK['.&get_now.']]';
589 my $namestr = $form{myname} || $default_name || &Resource('WikiForm:WikiComment:DefaultName');
590 ($namestr = '', $datestr = '') if $form{myname} eq 'nodate';
591 if ($namestr =~ /^(?:>>)?[0-9]/) {
592 $namestr = qq( ''$namestr'': );
593 } elsif (length $namestr) {
594 $namestr = qq( ''[[$namestr]]'': );
595 }
596 my $anchor = &get_new_anchor_index ($content);
597 my $i = 1; my $o = 0;
598 $content =~ s{(\[\[\#r?comment\]\])}{
599 my $embed = $1;
600 if ($i == $form{comment_index}) {
601 if ($embed ne '[[#rcomment]]') {
602 $embed = "- [$anchor] $datestr$namestr$form{mymsg}\n$embed"; $o = 1;
603 } else {
604 $embed .= "\n- [$anchor] $datestr$namestr$form{mymsg}"; $o = 1;
605 }
606 }
607 $i++; $embed;
608 }ge;
609 unless ($o) {
610 $content = "#?SuikaWiki/0.9\n\n" unless $content;
611 $content .= "\n" unless $content =~ /\n$/s;
612 $content .= "- [$anchor] $datestr$namestr$form{mymsg}\n";
613 }
614 $form{__comment_anchor_index} = $anchor;
615 if ($form{mymsg} || $form{myname}) {
616 $form{mymsg} = $content;
617 $form{mytouch} = 'on';
618 &do_write;
619 } else { ## Don't write
620 $form{mycmd} = 'default';
621 &do_view;
622 }
623 }
624
625 sub get_new_anchor_index ($) {
626 my $content = shift;
627 my $anchor = 0;
628 $content =~ s/^(?:[-=]+\s*)?\[([0-9]+)\]/$anchor = $1 if $1 > $anchor; $&/mge;
629 $anchor + 1;
630 }
631
632 sub load_formatter (@) {
633 for my $t (@_) {
634 unless ($fmt{$t}) {
635 require Message::Util::Formatter;
636 $fmt{$t} = Message::Util::Formatter->new;
637 for (@{$SuikaWiki::Plugin::List{'wiki'.$t}||[]}) {
638 $_->load_formatter ($fmt{$t}, type => 'wiki'.$t);
639 }
640 $fmt{$t}->option (return_class => 'SuikaWiki::Markup::XML') if $HAS_XML;
641 }
642 }
643 }
644
645 sub do_wikiform {
646 my $content = $database{$form{mypage}};
647 my $anchor = &get_new_anchor_index ($content);
648 &load_formatter (qw/form_template form_option/);
649 my $write = 0;
650 my $i = 1;
651 $content =~ s{$embed_command{form}}{
652 my ($embed, $wfname, $template, $option) = ($&, $1, $3, $4);
653 if (($wfname && $wfname eq $form{wikiform_targetform})
654 || $i == $form{wikiform_index}) {
655 $template =~ s/\\([\\'])/$1/g;
656 $option =~ s/\\([\\'])/$1/g;
657 my $param = bless {depth=>10}, 'SuikaWiki::Plugin';
658 $param->{page} = $form{mypage};
659 $param->{form_index} = $i;
660 $param->{form_name} = $wfname;
661 $param->{anchor_index} = $anchor;
662 $param->{argv} = \%form;
663 $param->{default_name} = $1 if $content =~ /default-name="([^"]+)"/;
664 $param->{default_name} ||= &Resource('WikiForm:WikiComment:DefaultName');
665 $fmt{form_option}->replace ($option, $param);
666 my $t = 1;
667 for (keys %{$param->{require}||{}}) {
668 (undef $t, last) unless length $param->{argv}->{'wikiform__'.$_};
669 }
670 $t = $fmt{form_template}->replace ($template, $param) if $t;
671 if (length $t) {
672 if ($param->{output}->{reverse}) {
673 $embed .= "\n" . $t;
674 } else {
675 $embed = $t . "\n" . $embed;
676 }
677 $write = 1;
678 $form{__comment_anchor_index} = $anchor
679 if $param->{anchor_index_}; ## $anchor is used!
680 }
681 $form{__wikiform_anchor_index} = $i;
682 undef $form{wikiform_targetform}; ## Make sure never to match
683 undef $form{wikiform_index}; ## with WikiForm in rest of page!
684 }
685 $i++; $embed;
686 }ge;
687 unless ($write) {
688 #$content = "#?SuikaWiki/0.9\n\n" unless $content;
689 #$content .= "\n" unless $content =~ /\n$/s;
690 #
691 }
692 if ($write) {
693 $form{mymsg} = $content;
694 $form{mytouch} = 'on';
695 &do_write;
696 } else { ## Don't write!
697 $form{mycmd} = 'default';
698 &do_view;
699 }
700 }
701
702 sub code_convert {
703 require Jcode;
704 my ($contentref, $code) = (shift, shift || $kanjicode);
705 if ($code =~ /euc/) { $code = 'euc' }
706 elsif ($code =~ /iso/) { $code = 'jis' }
707 elsif ($code =~ /shi/) { $code = 'sjis' }
708 elsif ($code =~ /utf/) { $code = 'utf8' }
709 $$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;
710 return $$contentref;
711 }
712
713 sub _rfc3339_date ($) {
714 my @time = gmtime (shift);
715 sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
716 }
717
718
719 package wiki::dummy;
720 sub mtime (@) {undef}
721 sub meta (@) {undef}
722 sub Yuki::YukiWikiDB2::meta (@) {undef}
723
724 package main;
725 &SuikaWiki::Plugin::import_plugins ();
726 &main ();
727
728 =head1 NAME
729
730 lib/suikawiki.pl --- SuikaWiki transitional library
731
732 =head1 AUTHOR
733
734 Hiroshi Yuki <hyuki@hyuki.com> <http://www.hyuki.com/yukiwiki/> (YukiWiki)
735
736 Makio Tsukamoto <http://digit.que.ne.jp/> (WalWiki)
737
738 Wakaba <w@suika.fam.cx>
739
740 =head1 LICENSE
741
742 Copyright AUTHORS 2000-2003
743
744 This program is free software; you can redistribute it and/or
745 modify it under the same terms as Perl itself.
746
747 =cut
748
749 1; # $Date: 2003/05/07 08:57:34 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24