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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations) (download)
Sun Oct 5 09:28:23 2003 UTC (21 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.12: +3 -4 lines
File MIME type: text/plain
Allow media type parameter

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24