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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations) (download)
Sat May 31 07:01:46 2003 UTC (22 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.7: +98 -196 lines
File MIME type: text/plain
XMLize

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24