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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (hide annotations) (download)
Mon Dec 1 07:45:11 2003 UTC (20 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.18: +65 -8 lines
File MIME type: text/plain
New error handlers

1 wakaba 1.1 use strict;
2     package main;
3     binmode STDOUT; binmode STDIN;
4 wakaba 1.18 our $WIKI;
5 wakaba 1.1 require SuikaWiki::Plugin;
6 wakaba 1.18 our %embed_command = ( # Map, main
7 wakaba 1.1 form => qr/\[\[\#form(?:\(([A-Za-z0-9-]+)\))?:'((?:[^'\\]|\\.)*)':'((?:[^'\\]|\\.)*)'(?::'((?:[^'\\]|\\.)*)')?\]\]/,
8     );
9     our $database = bless {}, 'wiki::dummy';
10 w 1.9 require SuikaWiki::Name::Space;
11 wakaba 1.18 require SuikaWiki::SrcFormat;
12 wakaba 1.1
13 w 1.9 # [to be obsolete]
14 wakaba 1.1 sub do_comment {
15 wakaba 1.15 my ($content) = $main::database{$main::form{mypage}};
16 wakaba 1.1 my $default_name; ## this code is not strict.
17     $default_name = $1 if $content =~ /default-name="([^"]+)"/;
18 w 1.9 my @time = gmtime (time);
19     my $datestr = sprintf '[WEAK[%04d-%02d-%02d %02d:%02d:%02d +00:00]]', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
20 wakaba 1.15 my $namestr = $main::form{myname} || $default_name || &Resource('WikiForm:WikiComment:DefaultName');
21     ($namestr = '', $datestr = '') if $main::form{myname} eq 'nodate';
22 wakaba 1.1 if ($namestr =~ /^(?:>>)?[0-9]/) {
23     $namestr = qq( ''$namestr'': );
24     } elsif (length $namestr) {
25     $namestr = qq( ''[[$namestr]]'': );
26     }
27     my $anchor = &get_new_anchor_index ($content);
28     my $i = 1; my $o = 0;
29     $content =~ s{(\[\[\#r?comment\]\])}{
30     my $embed = $1;
31 wakaba 1.15 if ($i == $main::form{comment_index}) {
32 wakaba 1.1 if ($embed ne '[[#rcomment]]') {
33 wakaba 1.15 $embed = "- [$anchor] $datestr$namestr$main::form{mymsg}\n$embed"; $o = 1;
34 wakaba 1.1 } else {
35 wakaba 1.15 $embed .= "\n- [$anchor] $datestr$namestr$main::form{mymsg}"; $o = 1;
36 wakaba 1.1 }
37     }
38     $i++; $embed;
39     }ge;
40     unless ($o) {
41     $content = "#?SuikaWiki/0.9\n\n" unless $content;
42     $content .= "\n" unless $content =~ /\n$/s;
43 wakaba 1.15 $content .= "- [$anchor] $datestr$namestr$main::form{mymsg}\n";
44 wakaba 1.1 }
45 wakaba 1.15 $main::form{__comment_anchor_index} = $anchor;
46     if ($main::form{mymsg} || $main::form{myname}) {
47     $main::form{mymsg} = $content;
48     $main::form{mytouch} = 'on';
49 wakaba 1.1 &do_write;
50     } else { ## Don't write
51 wakaba 1.15 #$main::form{mycmd} = 'default';
52     #&do_view;
53     die "No comment specified";
54 wakaba 1.1 }
55     }
56    
57 w 1.9 # [move to SuikaWiki::Plugin::WikiForm]
58 wakaba 1.1 sub get_new_anchor_index ($) {
59     my $content = shift;
60     my $anchor = 0;
61     $content =~ s/^(?:[-=]+\s*)?\[([0-9]+)\]/$anchor = $1 if $1 > $anchor; $&/mge;
62     $anchor + 1;
63     }
64    
65 w 1.9 # [move to SuikaWiki::Plugin::WikiForm]
66 wakaba 1.1 sub do_wikiform {
67 wakaba 1.15 my $content = $main::database{$main::form{mypage}};
68 wakaba 1.1 my $anchor = &get_new_anchor_index ($content);
69     my $write = 0;
70     my $i = 1;
71     $content =~ s{$embed_command{form}}{
72     my ($embed, $wfname, $template, $option) = ($&, $1, $3, $4);
73 wakaba 1.15 if (($wfname && $wfname eq $main::form{wikiform_targetform})
74     || $i == $main::form{wikiform_index}) {
75 wakaba 1.1 $template =~ s/\\([\\'])/$1/g;
76     $option =~ s/\\([\\'])/$1/g;
77     my $param = bless {depth=>10}, 'SuikaWiki::Plugin';
78 wakaba 1.15 $param->{page} = $main::form{mypage};
79 wakaba 1.1 $param->{form_index} = $i;
80     $param->{form_name} = $wfname;
81     $param->{anchor_index} = $anchor;
82 wakaba 1.15 $param->{argv} = \%main::form;
83 wakaba 1.1 $param->{default_name} = $1 if $content =~ /default-name="([^"]+)"/;
84     $param->{default_name} ||= &Resource('WikiForm:WikiComment:DefaultName');
85 w 1.9 SuikaWiki::Plugin->formatter ('form_option')->replace ($option, $param);
86 wakaba 1.1 my $t = 1;
87     for (keys %{$param->{require}||{}}) {
88     (undef $t, last) unless length $param->{argv}->{'wikiform__'.$_};
89     }
90 w 1.9 $t = SuikaWiki::Plugin->formatter ('form_template')->replace ($template, $param) if $t;
91 wakaba 1.1 if (length $t) {
92     if ($param->{output}->{reverse}) {
93     $embed .= "\n" . $t;
94     } else {
95     $embed = $t . "\n" . $embed;
96     }
97     $write = 1;
98 wakaba 1.15 $main::form{__comment_anchor_index} = $anchor
99 wakaba 1.1 if $param->{anchor_index_}; ## $anchor is used!
100     }
101 wakaba 1.15 $main::form{__wikiform_anchor_index} = $i;
102     undef $main::form{wikiform_targetform}; ## Make sure never to match
103     undef $main::form{wikiform_index}; ## with WikiForm in rest of page!
104 wakaba 1.1 }
105     $i++; $embed;
106     }ge;
107     unless ($write) {
108     #$content = "#?SuikaWiki/0.9\n\n" unless $content;
109     #$content .= "\n" unless $content =~ /\n$/s;
110     #
111     }
112     if ($write) {
113 wakaba 1.15 $main::form{mymsg} = $content;
114     $main::form{mytouch} = 'on';
115 wakaba 1.1 &do_write;
116     } else { ## Don't write!
117 wakaba 1.15 #$main::form{mycmd} = 'default';
118     #&do_view;
119     die "No content specified";
120 wakaba 1.1 }
121     }
122    
123 w 1.9 # [to be obsolete] ->Message::MIME::Charset
124 wakaba 1.1 sub code_convert {
125     require Jcode;
126 wakaba 1.15 my ($contentref, $code, $srccode) = @_;
127     $code ||= $WIKI->{config}->{charset}->{internal};
128     for ($code, $srccode) {
129     if ($_ eq 'euc-jp') { $_ = 'euc' }
130     elsif ($_ eq 'iso-2022-jp') { $_ = 'jis' }
131     elsif ($_ eq 'utf-8') { $_ = 'utf8' }
132     elsif ($_ eq 'shift_jis') { $_ = 'sjis' }
133     }
134 wakaba 1.19 if ($code eq 'iso-8859-1') {
135     return $$contentref; ## TODO:
136     }
137 wakaba 1.15 $$contentref = Jcode->new ($contentref, $srccode)
138     ## Normalize FULLWIDTH characters and IDEOGRAPHIC SPACE
139     ->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\xA1\xE4\xA1\xE3\xA1\xA1\xA2\xAF\xA2\xB0\xA2\xB2\xA2\xB1\xA1\xA1\xC0" => q(0-9A-Za-z&,.:;?!`^_/|()[]{}+$%#*@=>< '"~-))
140     ->tr (qq(\x8E\xDE\x8E\xDF) => qq(\xA1\xAB\xA1\xAC))
141     ->h2z
142     ->$code;
143     return $$contentref;
144 wakaba 1.1 }
145    
146 wakaba 1.18 # [to be obsolete] ->Message::Field::Date : Map
147 wakaba 1.1 sub _rfc3339_date ($) {
148     my @time = gmtime (shift);
149     sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
150     }
151    
152 wakaba 1.15 # [obsolete] ->SuikaWiki::SrcFormat : SuikaWiki09 plugin
153 w 1.9 sub convert_format ($$$;%) {
154     my ($content, $d => $t, %option) = @_;
155     my $f = SuikaWiki::Plugin->format_converter ($d => $t);
156     if (ref $f) {
157     $option{content} = $content;
158     $option{from} = $d;
159     $option{to} = $t;
160     &$f ({}, bless (\%option, 'SuikaWiki::Plugin'));
161     } elsif ($option{-error_no_return}) {
162     return undef;
163     } elsif ($t =~ /HTML|xml/) {
164     if (length $content) {
165 wakaba 1.18 my $NS_XHTML1 = 'http://www.w3.org/1999/xhtml';
166 wakaba 1.19 my $r = Message::Markup::XML::Node->new
167     (type => '#element', namespace_uri => $NS_XHTML1, local_name => 'pre');
168 w 1.9 $r->append_text ($content);
169     return $r;
170     } else {
171     return '';
172     }
173     } else {
174     $content;
175     }
176     }
177    
178 wakaba 1.15 package wiki::transitional::uri_param;
179     require Tie::Hash;
180     our @ISA = 'Tie::Hash';
181    
182     sub TIEHASH ($@) {
183     bless {http => $_[1]}, $_[0];
184     }
185    
186     sub FETCH ($$) {
187     my ($self, $key) = @_;
188     exists $self->{val}->{$key} ?
189     $self->{val}->{$key}:
190     $self->{http}->parameter ($key);
191     }
192    
193     sub STORE ($$$) {
194     my ($self, $key, $val) = @_;
195     $self->{val}->{$key} = $val;
196     }
197    
198     sub DELETE ($$) {
199     my ($self, $key) = @_;
200     $self->{val}->{$key} = undef;
201     }
202    
203     sub EXISTS ($$) {
204     my ($self, $key) = @_;
205     exists $self->{val}->{$key} ?
206     1:
207     defined $self->{http}->parameter ($key);
208     }
209 w 1.9
210 wakaba 1.1 package main;
211 wakaba 1.19 our $VERSION = do{my @r=(q$Revision: 1.18 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
212 wakaba 1.15 push @{$WIKI->{event}->{setting_initial_variables}}, sub {
213     my $wiki = shift;
214 wakaba 1.18 $wiki->{implementation_version} = 'wd'.$VERSION;
215 wakaba 1.15
216     ## Error output
217     require SuikaWiki::Output::CGICarp;
218     $SuikaWiki::Output::CGICarp::CUSTOM_REASON_TEXT
219     = 'Internal WikiEngine Error';
220     CGI::Carp::set_message (sub {
221     my $msg = shift;
222     #$msg =~ s/&/&amp;/g;
223     #$msg =~ s/</&lt;/g;
224     my $wiki_name_version = $wiki->{implementation_name} .'/'. $wiki->version;
225     for ($wiki_name_version) { s/&/&amp;/g; s/</&lt;/g;
226     s/([^\x20-\x7E])/sprintf '&#x%02X;',
227     ord $1/g; };
228 wakaba 1.18 print STDOUT <<EOH;
229 wakaba 1.15 <!DOCTYPE html SYSTEM>
230     <title>500 Internal WikiEngine Error</title>
231     <h1>Internal WikiEngine Error</h1>
232     <p>$msg</p>
233     <address>$wiki_name_version</address>
234     EOH
235     });
236    
237     $wiki->{var}->{db}->{read_only}->{'#default'} = 1;
238    
239     require SuikaWiki::Input::HTTP;
240     $wiki->{input} = SuikaWiki::Input::HTTP->new;
241     $wiki->{input}->{decoder}->{'#default'} = sub {
242     my ($http, $s, $temp_params) = @_;
243 wakaba 1.18 return main::code_convert (\$s, $wiki->{config}->{charset}->{internal},
244     lc (@{$temp_params->{_charset_}||[]}[0])
245 wakaba 1.15 || $wiki->{config}->{charset}->{uri_param});
246     };
247     $wiki->{var}->{client}->{user_agent_name}
248     = $wiki->{input}->meta_variable ('HTTP_USER_AGENT');
249     $wiki->{var}->{client}->{used_for_negotiate} = ['User-Agent'];
250    
251     ## TODO: PATH_INFO support
252     my $page = $wiki->{input}->meta_variable ('QUERY_STRING');
253     if ($page && !(index ($page, '=') > -1)) {
254     $page =~ tr/+/ /;
255     $page =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge;
256     $page = main::code_convert
257 wakaba 1.18 (\$page, $wiki->{config}->{charset}->{internal},
258 wakaba 1.15 $wiki->{config}->{charset}->{uri_query});
259     } else {
260     $page = $wiki->{input}->parameter ('mypage');
261     }
262    
263    
264     ## TODO: SuikaWiki 3 WikiName
265     $page =~ tr/\x00-\x20\x7F//d;
266     $page = SuikaWiki::Name::Space::normalize_name ($page);
267     if ($page) {
268     $wiki->{var}->{page} = [split '//', $page];
269     } else {
270     $wiki->{var}->{page} = $wiki->{config}->{page}->{Default};
271     }
272    
273     ## Mode
274     my $mode = $wiki->{input}->parameter ('mode')
275     || $wiki->{input}->parameter ('mycmd') ## for compatibility with
276     || 'default'; ## YukiWiki and SuikaWiki 2
277     $mode =~ tr/-/_/;
278     if ($mode eq 'default' || $mode =~ /[^0-9A-Za-z_]/) {
279     ## BUG: this code is not strict
280     my $cookie = $wiki->{input}->meta_variable ('HTTP_COOKIE');
281     if ($cookie =~ /SelectedMode=([0-9A-Za-z_-]+)/) {
282     $mode = $1; $mode =~ tr/-/_/;
283     } else {
284     $mode = 'read';
285     }
286     push @{$wiki->{var}->{client}->{used_for_negotiate}}, 'Cookie';
287     }
288     $wiki->{var}->{mode} = $mode;
289    
290     ## Transitional variables
291     tie %main::form, 'wiki::transitional::uri_param', $wiki->{input};
292     $main::UA = $wiki->{var}->{client}->{user_agent_name};
293     $main::form{mypage} = join '//', @{$wiki->{var}->{page}};
294     $main::form{mycmd} = $mode;
295     };
296    
297 wakaba 1.18 push @{$WIKI->{event}->{view_in_mode}}, sub {
298     my ($wiki, $opt) = @_;
299     my $arg = {condition => {mode => $opt->{mode} || '-error',
300     output => 'http-cgi',
301     http_method => $opt->{method} || 'GET'}};
302     my $viewobj = $wiki->{view}->instantiate ($opt->{mode} || '-error', $arg);
303     if (ref $viewobj) {
304     $viewobj->main ($arg);
305     } elsif ($opt->{mode} ne '-error') {
306     $wiki->view_in_mode (mode => '-error', method => 'GET');
307     ## TODO: cache control for non-GET
308     } else {
309     die "Some error happens. Additionally, error reporting mode not defined";
310     }
311     };
312    
313     ## Initialization of various functions
314     $WIKI->init_variables;
315 wakaba 1.14 $WIKI->init_plugin;
316     $WIKI->init_view;
317     $WIKI->{view}->register_common_modes;
318    
319 wakaba 1.19 ## Error handlers
320     use SuikaWiki::DB::Util::Error;
321     my $catcher = catch SuikaWiki::DB::Util::Error with {
322     my $err = shift;
323     my $err_msg = $err->text;
324     $err_msg = caller (3) . '-->' . caller (2) . '-->' . caller (1)
325     . ($err->{-method} ? '->'.$err->{-method} : '')
326     . ': '
327     . (defined $err->{-file} ? $err->{-file} . ': ' : '')
328     . (defined $err->{-prop} ? $err->{-prop} . ': ' : '')
329     . (defined $err->{-key} ? join ('//', @{$err->{-key}}) . ': ' : '')
330     . $err_msg;
331     if ($WIKI->{config}->{path_to}->{db__content__error_log}) {
332     open LOG, '>>', $WIKI->{config}->{path_to}->{db__content__error_log};
333     print LOG scalar (gmtime), " @{[$$]} {$err->{def}->{level}}: ",
334     $err_msg, "\n";
335     close LOG;
336     }
337     if ($err->{def}->{level} eq 'fatal' or $err->{def}->{level} eq 'stop') {
338     require Carp;
339     local $Carp::Verbose = 1;
340     Carp::croak $err_msg;
341     }
342     } catch SuikaWiki::View::Implementation::error with {
343     my $err = shift;
344     exit if $err->{type} eq 'ERROR_REPORTED';
345     die $err;
346     };
347    
348     $WIKI->{config}->{catch}->{formatter_view}
349     = catch Message::Util::Formatter::error with {
350     my $err = shift;
351     my $wiki = $err->{-option}->{param}->{wiki};
352     SuikaWiki::Plugin->module_package ('Error')
353     ->reporting_formatting_template_error ($err, $wiki);
354     $wiki->view_in_mode (mode => '-error', method => 'GET');
355     throw SuikaWiki::View::Implementation::error
356     type => 'ERROR_REPORTED';
357     };
358    
359     $WIKI->{config}->{catch}->{formatter_view_error}
360     = catch Message::Util::Formatter::error with {
361     my $err = shift;
362     my $wiki = $err->{-option}->{param}->{wiki};
363     SuikaWiki::Plugin->module_package ('Error')
364     ->reporting_formatting_template_error ($err, $wiki);
365     $wiki->view_in_mode (mode => '-error-error', method => 'GET');
366     throw SuikaWiki::View::Implementation::error
367     type => 'ERROR_REPORTED';
368     };
369    
370     ## Main
371     try {
372 wakaba 1.18 $WIKI->view_in_mode
373     (mode => $WIKI->{var}->{mode},
374     method => $WIKI->{input}->meta_variable ('REQUEST_METHOD'));
375 wakaba 1.19 } $catcher;
376 wakaba 1.14 exit;
377     END {
378 wakaba 1.19 try {
379     $WIKI->exit;
380     } $catcher;
381     };
382 wakaba 1.1
383     =head1 NAME
384    
385 wakaba 1.18 lib/suikawiki.pl --- SuikaWiki : WikiEngine driver for HTTP CGI
386 wakaba 1.1
387     =head1 LICENSE
388    
389 wakaba 1.18 Copyright 2000-2003 Wakaba <w@suika.fam.cx>, et. al
390 wakaba 1.1
391     This program is free software; you can redistribute it and/or
392     modify it under the same terms as Perl itself.
393    
394     =cut
395    
396 wakaba 1.19 1; # $Date: 2003/10/30 07:46:09 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24