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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide annotations) (download)
Sun Oct 5 11:50:40 2003 UTC (21 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.13: +70 -98 lines
File MIME type: text/plain
suikawiki2 transitional

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24