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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (show annotations) (download)
Fri Jan 16 08:01:05 2004 UTC (21 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.22: +17 -13 lines
File MIME type: text/plain
(code_convert): Removes non-token characters for NN4

1 =head1 NAME
2
3 suikawiki.pl - SuikaWiki Driver for HTTP CGI Script
4
5 =cut
6
7 use strict;
8 package main;
9 our $WIKI;
10 package wiki::driver::http;
11 ## -- Version of WikiDriver --
12 our $VERSION = do{my @r=(q$Revision: 1.22 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
13 $WIKI->{implementation_version} = 'hcs'.$VERSION;
14
15 ## -- Dying Message as HTTP Response --
16 require SuikaWiki::Output::CGICarp;
17 $SuikaWiki::Output::CGICarp::CUSTOM_REASON_TEXT
18 = 'Internal WikiEngine Error';
19 CGI::Carp::set_message (sub {
20 my $msg = shift;
21 #$msg =~ s/&/&/g;
22 #$msg =~ s/</&lt;/g;
23 my $wiki_name_version = $WIKI->{implementation_name} .'/'. $WIKI->version;
24 my $trace = Carp::longmess ();
25 for ($trace, $wiki_name_version)
26 { s/&/&amp;/g; s/</&lt;/g;
27 s/([^\x20-\x7E])/sprintf '&#x%02X;', ord $1/ge; };
28 print STDOUT <<EOH;
29 <!DOCTYPE html SYSTEM>
30 <title>500 Internal WikiEngine Error</title>
31 <h1>Internal WikiEngine Error</h1>
32 <p>$msg</p>
33 <p>$trace</p>
34 <address>$wiki_name_version</address>
35 EOH
36 });
37
38 ## -- Required Modules --
39 use SuikaWiki::DB::Util::Error;
40 require SuikaWiki::Plugin;
41 require SuikaWiki::Name::Space;
42 require SuikaWiki::SrcFormat;
43
44
45 ## -- Transitional Functions --
46
47 # [to be obsolete] ->Message::MIME::Charset
48 sub main::code_convert {
49 require Jcode;
50 my ($contentref, $code, $srccode) = @_;
51 $code ||= $WIKI->{config}->{charset}->{internal};
52 for ($code, $srccode) {
53 s/[^0-9A-Za-z_.+-]+//g;
54 if ($_ eq 'euc-jp') { $_ = 'euc' }
55 elsif ($_ eq 'iso-2022-jp') { $_ = 'jis' }
56 elsif ($_ eq 'utf-8') { $_ = 'utf8' }
57 elsif ($_ eq 'shift_jis') { $_ = 'sjis' }
58 }
59 if ($code eq 'iso-8859-1') {
60 return $$contentref; ## TODO:
61 }
62 $$contentref = Jcode->new ($contentref, $srccode)
63 ## Normalize FULLWIDTH characters and IDEOGRAPHIC SPACE
64 ->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&,.:;?!`^_/|()[]{}+$%#*@=>< '"~-))
65 # ->tr (qq(\x8E\xDE\x8E\xDF) => qq(\xA1\xAB\xA1\xAC))
66 ->h2z (1)
67 ->$code;
68 return $$contentref;
69 }
70
71 # [to be obsolete] ->Message::Field::Date : Map
72 sub main::_rfc3339_date ($) {
73 my @time = gmtime (shift);
74 sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
75 }
76
77 ## Obsolete
78 sub SuikaWiki::Plugin::get_data ($$$$;%) {
79 my ($self, $prop, $key, %opt) = @_;
80 ## TODO: common interface to WikiDB
81 ## TODO: error recovering
82 $main::WIKI->{db}->get ($prop => $key);
83 }
84
85 ## -- Initializing WikiPlugin --
86
87 $WIKI->init_plugin; ## WikiPlugin manager
88
89 ## -- Initializing WikiView --
90
91 $WIKI->init_view; ## WikiView manager
92 $WIKI->{view}->register_common_modes;
93
94 ## WikiView manager error handler
95 push @{$WIKI->{event}->{view_error}}, sub {
96 my ($wiki, $event) = @_;
97 SuikaWiki::Plugin->module_package ('Error')
98 ->report_error_simple
99 ($wiki, WikiView => $event->{error}->text,
100 -trace => 1)
101 if $event->{error}->{-def}->{level} eq 'fatal'
102 or $wiki->{config}->{debug}->{view};
103 unless ($event->{error}->{-def}->{level} eq 'fatal'
104 or $event->{error}->{-def}->{level} eq 'stop') {
105 $event->{cancel} = 1;
106 }
107 };
108
109 ## "view_in_mode" method definition
110 push @{$WIKI->{event}->{view_in_mode}}, sub {
111 my ($wiki, $opt) = @_;
112 my $arg = {condition => {mode => $opt->{mode} || '-error',
113 output => 'http-cgi',
114 http_method => $opt->{method} || 'GET'}};
115 my $viewobj = $wiki->{view}->instantiate ($opt->{mode} || '-error', $arg);
116 if (ref $viewobj) {
117 $viewobj->main ($arg);
118 } elsif ($opt->{mode} ne '-error') {
119 report SuikaWiki::View::Implementation::error
120 -type => 'WARN_VIEW_NOT_DEFINED', condition => $arg->{condition},
121 -object => $wiki->{view}, method => 'view_in_mode';
122 $wiki->view_in_mode (mode => '-error', method => 'GET');
123 ## TODO: cache control for non-GET
124 } else {
125 die "Some error occured. Additionally, error reporting mode not defined";
126 }
127 };
128
129 ## WikiView formatting template error handler
130 $WIKI->{config}->{catch}->{formatter_view}
131 = catch Message::Util::Formatter::error with {
132 my $err = shift;
133 my $wiki = $err->{option}->{param}->{wiki};
134 SuikaWiki::Plugin->module_package ('Error')
135 ->reporting_formatting_template_error ($err, $wiki,
136 trace => 1);
137 $wiki->view_in_mode (mode => '-error', method => 'GET');
138 throw SuikaWiki::View::Implementation::error
139 -type => 'ERROR_REPORTED';
140 };
141
142 ## WikiView formatting template error handler (occured in "-error" mode)
143 $WIKI->{config}->{catch}->{formatter_view_error}
144 = catch Message::Util::Formatter::error with {
145 my $err = shift;
146 my $wiki = $err->{option}->{param}->{wiki};
147 SuikaWiki::Plugin->module_package ('Error')
148 ->reporting_formatting_template_error ($err, $wiki,
149 trace => 1);
150 $wiki->view_in_mode (mode => '-error-error', method => 'GET');
151 throw SuikaWiki::View::Implementation::error
152 -type => 'ERROR_REPORTED';
153 };
154
155 ## -- WikiDatabase Error Reporting --
156 {
157 my $error_report = sub {
158 my ($wiki, $err) = @_;
159 my $report = ($err->{-def}->{level} eq 'fatal' or
160 $err->{-def}->{level} eq 'stop' or
161 $wiki->{config}->{debug}->{db}) ? 1 : 0;
162 if ($report and $wiki->{config}->{path_to}->{db__content__error_log}) {
163 my $err_msg = caller (1).($err->{method}? '->'.$err->{method}: '').': '
164 .(defined $err->{file}? $err->{file} . ': ' : '')
165 .(defined $err->{prop}? $err->{prop} . ': ' : '')
166 .(defined $err->{key}? join ('//', @{$err->{key}}).': ':'')
167 . $err->text;
168 open LOG, '>>', $wiki->{config}->{path_to}->{db__content__error_log};
169 print LOG scalar (gmtime), " @{[$$]} {$err->{-def}->{level}}: ",
170 $err_msg, "\n";
171 close LOG;
172 }
173 SuikaWiki::Plugin->module_package ('WikiDB')
174 ->reporting_error ($err, $wiki) if $report;
175 if ($err->{-def}->{level} eq 'fatal' or $err->{-def}->{level} eq 'stop') {
176 $wiki->view_in_mode (mode => '-wdb--fatal-error');
177 throw SuikaWiki::DB::Util::Error -type => 'ERROR_REPORTED';
178 }
179 };
180 unshift @{$WIKI->{event}->{database_loaded}}, sub {
181 my $wiki = shift;
182 unshift @{$wiki->{db}->{event}->{error}}, sub {
183 my ($db, $event) = @_;
184 $error_report->($wiki, $event->{error});
185 if ($event->{error}->{-type} eq 'INFO_DB_PROP_OPENED') {
186 unshift @{$db->{prop}->{$event->{error}->{prop}}->{-db}
187 ->{event}->{error}}, sub {
188 my ($db, $event) = @_;
189 $error_report->($wiki, $event->{error});
190 };
191 }
192 }; # database error
193 }; # database_loaded
194 }
195
196 ## -- Misc. Error Reporting --
197
198 if ($WIKI->{config}->{debug}->{general}) {
199 $main::SIG{__WARN__} = sub {
200 push @{$WIKI->{var}->{error}||=[]}, {
201 description => Message::Markup::XML::Node->new
202 (type => '#text',
203 value => $_[0]),
204 };
205 };
206 }
207
208 ## -- Initializing $wiki->{var} (Declaration) --
209
210 push @{$WIKI->{event}->{setting_initial_variables}}, sub {
211 my $wiki = shift;
212 $wiki->{var}->{db}->{read_only}->{'#default'} = 1;
213
214 require SuikaWiki::Input::HTTP;
215 $wiki->{input} = SuikaWiki::Input::HTTP->new (wiki => $wiki);
216 $wiki->{input}->{decoder}->{'#default'} = sub {
217 my ($http, $s, $temp_params) = @_;
218 return main::code_convert (\$s, $wiki->{config}->{charset}->{internal},
219 lc (@{$temp_params->{_charset_}||[]}[0])
220 || $wiki->{config}->{charset}->{uri_param});
221 };
222 $wiki->{var}->{client}->{user_agent_name}
223 = $wiki->{input}->meta_variable ('HTTP_USER_AGENT');
224 $wiki->{var}->{client}->{used_for_negotiate} = ['User-Agent'];
225 my $dg = SuikaWiki::Plugin->module_package ('Downgrade');
226 $dg->set_downgrade_flags ($wiki) if $dg;
227
228 ## TODO: PATH_INFO support
229 my $page = $wiki->{input}->meta_variable ('QUERY_STRING');
230 if ($page && !(index ($page, '=') > -1)) {
231 $page =~ tr/+/ /;
232 $page =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge;
233 $page = main::code_convert
234 (\$page, $wiki->{config}->{charset}->{internal},
235 $wiki->{config}->{charset}->{uri_query});
236 } else {
237 $page = $wiki->{input}->parameter ('mypage');
238 }
239
240
241 ## TODO: SuikaWiki 3 WikiName
242 $page =~ tr/\x00-\x20\x7F//d;
243 $page = SuikaWiki::Name::Space::normalize_name ($page);
244 if ($page) {
245 $wiki->{var}->{page} = [split '//', $page];
246 } else {
247 $wiki->{var}->{page} = $wiki->{config}->{page}->{Default};
248 }
249
250 ## Mode
251 my $mode = $wiki->{input}->parameter ('mode')
252 || $wiki->{input}->parameter ('mycmd') ## for compatibility with
253 || 'default'; ## YukiWiki and SuikaWiki 2
254 $mode =~ tr/-/_/;
255 if ($mode eq 'default' || $mode =~ /[^0-9A-Za-z_]/) {
256 ## BUG: this code is not strict
257 my $cookie = $wiki->{input}->meta_variable ('HTTP_COOKIE');
258 if ($cookie =~ /SelectedMode=([0-9A-Za-z_-]+)/) {
259 $mode = $1; $mode =~ tr/-/_/;
260 } else {
261 $mode = 'read';
262 }
263 push @{$wiki->{var}->{client}->{used_for_negotiate}}, 'Cookie';
264 }
265 $wiki->{var}->{mode} = $mode;
266 };
267
268 ## -- Initializing $wiki->{var} (Actual) --
269
270 $WIKI->init_variables; ## Per-session variables
271
272 ## -- Instantiating WikiView --
273
274 try {
275 $WIKI->view_in_mode
276 (mode => $WIKI->{var}->{mode},
277 method => $WIKI->{input}->meta_variable ('REQUEST_METHOD'));
278 } catch SuikaWiki::DB::Util::Error with {
279 my $err = shift;
280 $err->throw unless $err->{-type} eq 'ERROR_REPORTED';
281 } catch SuikaWiki::View::Implementation::error with {
282 my $err = shift;
283 $err->throw unless $err->{-type} eq 'ERROR_REPORTED';
284 } finally {
285 $WIKI->close_input;
286 $WIKI->close_db;
287 };
288 exit;
289
290 ## -- Terminating WikiEngine --
291 END {
292 $WIKI->exit;
293 }
294
295 =head1 LICENSE
296
297 Copyright 2000-2004 Wakaba <w@suika.fam.cx>, et. al
298
299 This program is free software; you can redistribute it and/or
300 modify it under the same terms as Perl itself.
301
302 =cut
303
304 1; # $Date: 2003/12/26 09:40:28 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24