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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.28 - (show annotations) (download)
Fri Mar 12 04:54:37 2004 UTC (21 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.27: +3 -4 lines
File MIME type: text/plain
Multiple Perl-base config file support

1 =head1 NAME
2
3 suikawiki.pl - SuikaWiki Driver as HTTP CGI Script (SWHCS)
4
5 =head1 DESCRIPTION
6
7 This script is a WikiDriver for SuikaWiki, working as HTTP CGI script.
8 With this script, SuikaWiki WikiEngine can be controled via remote WWW
9 user agents.
10
11 This file is part of SuikaWiki.
12
13 =cut
14
15 package wiki::driver::http;
16 use strict;
17 our $VERSION = do{my @r=(q$Revision: 1.27 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
18
19 ## These lines should be removed after utf8 support
20 BEGIN {
21 $Message::Util::Formatter::Base::Token = qr/[\w._+\x80-\xFF-]+/;
22 require Message::Util::Formatter::Base;
23 }
24
25 ## -- Constructing a new instance of the WikiEngine --
26
27 require SuikaWiki::Implementation;
28 our $WIKI = SuikaWiki::Implementation->new;
29
30 ## -- Registering Version of the WikiDriver --
31
32 $WIKI->{driver_name} = 'SWHCS';
33 $WIKI->{driver_version} = $VERSION;
34 $WIKI->{driver_uri_reference}
35 = q<http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SWHCS>;
36
37 ## -- Preparing Dying Message as HTTP Response --
38
39 require SuikaWiki::Output::CGICarp;
40 $SuikaWiki::Output::CGICarp::CUSTOM_REASON_TEXT = 'Internal WikiEngine Error';
41 CGI::Carp::set_message (sub {
42 my $msg = shift; ## Already escaped
43 my $wiki_name_version = sprintf '%s/%s %s/%s',
44 $WIKI->{driver_name}, $WIKI->{driver_version},
45 $WIKI->{engine_name}, $WIKI->{engine_version};
46 my $trace = Carp::longmess ();
47 for ($trace, $wiki_name_version) {
48 s/&/&amp;/g; s/</&lt;/g; s/([^\x20-\x7E])/sprintf '&#x%02X;', ord $1/ge;
49 };
50 print <<" EOH";
51 <!DOCTYPE html SYSTEM>
52 <title lang="en">500 Internal WikiEngine Error</title>
53 <h1 lang="en">Internal WikiEngine Error</h1>
54 <p>$msg</p>
55 <p>$trace</p>
56 <address>$wiki_name_version</address>
57 EOH
58 });
59
60 ## -- Loading Configuration File --
61
62 $_->($WIKI) for our @Config;
63
64 ## -- Loading Modules --
65
66 use SuikaWiki::DB::Util::Error;
67 require SuikaWiki::Name::Space;
68
69 ## -- Transitional Functions --
70
71 # [to be obsolete] ->Message::MIME::Charset
72 sub main::code_convert {
73 my ($contentref, $code, $srccode) = @_;
74 return $$contentref if $$contentref !~ /[^\x21-\x7E]/;
75 require Jcode;
76 $code ||= $WIKI->{config}->{charset}->{internal};
77 for ($code, $srccode) {
78 s/[^0-9A-Za-z_.+-]+//g;
79 if ($_ eq 'euc-jp') { $_ = 'euc' }
80 elsif ($_ eq 'iso-2022-jp') { $_ = 'jis' }
81 elsif ($_ eq 'utf-8') { $_ = 'utf8' }
82 elsif ($_ eq 'shift_jis') { $_ = 'sjis' }
83 }
84 if ($code eq 'iso-8859-1') {
85 return $$contentref; ## TODO:
86 }
87 $$contentref = Jcode->new ($contentref, $srccode)
88 ## Normalize FULLWIDTH characters and IDEOGRAPHIC SPACE
89 ->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" => q(0-9A-Za-z&,.:;?!`^_/|()[]{}+$%#*@=>< '"~-))
90 # ->tr (qq(\x8E\xDE\x8E\xDF) => qq(\xA1\xAB\xA1\xAC))
91 ->h2z (1)
92 ->$code;
93 return $$contentref;
94 }
95
96 ## -- Initializing WikiPlugin --
97
98 $WIKI->init_plugin; ## WikiPlugin manager
99
100 ## -- Initializing WikiView --
101
102 $WIKI->init_view; ## WikiView manager
103 $WIKI->{view}->register_common_modes;
104
105 ## WikiView manager error handler
106 push @{$WIKI->{event}->{view_error}}, sub {
107 my ($wiki, $event) = @_;
108 SuikaWiki::Plugin->module_package ('Error')
109 ->report_error_simple
110 ($wiki, WikiView => $event->{error}->text,
111 -trace => 1)
112 if $event->{error}->{-def}->{level} eq 'fatal'
113 or $wiki->{config}->{debug}->{view};
114 unless ($event->{error}->{-def}->{level} eq 'fatal'
115 or $event->{error}->{-def}->{level} eq 'stop') {
116 $event->{cancel} = 1;
117 }
118 };
119
120 ## "view_in_mode" method definition
121 push @{$WIKI->{event}->{view_in_mode}}, sub {
122 my ($wiki, $event) = @_;
123 my $opt = $event->{view_in_mode};
124 my $arg = {condition => {mode => $opt->{mode} || '-error',
125 output => 'http-cgi',
126 http_method => $opt->{method} || 'GET'}};
127 my $viewobj = $wiki->{view}->instantiate ($opt->{mode} || '-error', $arg);
128 if (ref $viewobj) {
129 $viewobj->main ($arg);
130 } elsif ($opt->{mode} ne '-error') {
131 report SuikaWiki::View::Implementation::error
132 -type => 'WARN_VIEW_NOT_DEFINED', condition => $arg->{condition},
133 -object => $wiki->{view}, method => 'view_in_mode';
134 $wiki->view_in_mode (mode => '-wv--no-view-definition', method => 'GET');
135 ## TODO: cache control for non-GET
136 } else {
137 die "Some error occured. Additionally, error reporting mode not defined";
138 }
139 };
140
141 ## WikiView formatting template error handler
142 $WIKI->{config}->{catch}->{formatter_view}
143 = catch Message::Util::Formatter::error with {
144 my $err = shift;
145 my $wiki = $err->{option}->{param}->{wiki};
146 SuikaWiki::Plugin->module_package ('Error')
147 ->reporting_formatting_template_error ($err, $wiki,
148 trace => 1);
149 $wiki->view_in_mode (mode => '-error', method => 'GET');
150 throw SuikaWiki::View::Implementation::error
151 -type => 'ERROR_REPORTED';
152 };
153
154 ## WikiView formatting template error handler (occured in "-error" mode)
155 $WIKI->{config}->{catch}->{formatter_view_error}
156 = catch Message::Util::Formatter::error with {
157 my $err = shift;
158 my $wiki = $err->{option}->{param}->{wiki};
159 SuikaWiki::Plugin->module_package ('Error')
160 ->reporting_formatting_template_error ($err, $wiki,
161 trace => 1);
162 $wiki->view_in_mode (mode => '-error-error', method => 'GET');
163 throw SuikaWiki::View::Implementation::error
164 -type => 'ERROR_REPORTED';
165 };
166
167 ## -- Preparing for WikiDatabase Error Reports --
168 {
169 my $error_report = sub {
170 my ($wiki, $err) = @_;
171 if ($err->{-def}->{level} eq 'fatal') {
172 $wiki->close_db if $wiki->{db};
173 }
174 my $report = ($err->{-def}->{level} eq 'fatal' or
175 $err->{-def}->{level} eq 'stop' or
176 $wiki->{config}->{debug}->{db}) ? 1 : 0;
177 if ($report and $wiki->{config}->{path_to}->{db__content__error_log}) {
178 my $err_msg = caller (1).($err->{method}? '->'.$err->{method}: '').': '
179 .(defined $err->{file}? $err->{file} . ': ' : '')
180 .(defined $err->{prop}? $err->{prop} . ': ' : '')
181 .(defined $err->{key}? join ('//', @{$err->{key}}).': ':'')
182 . $err->text
183 . ($wiki->{config}->{debug}->{db} > 1 ? Carp::longmess () : '');
184 open LOG, '>>', $wiki->{config}->{path_to}->{db__content__error_log};
185 print LOG scalar (gmtime), " @{[$$]} {$err->{-def}->{level}}: ",
186 $err_msg, "\n";
187 close LOG;
188 }
189 SuikaWiki::Plugin->module_package ('WikiDB')
190 ->reporting_error ($err, $wiki) if $report;
191 if ($err->{-def}->{level} eq 'fatal'
192 # or $err->{-def}->{level} eq 'stop' ## for debug
193 ) {
194 $wiki->view_in_mode (mode => '-wdb--fatal-error');
195 throw SuikaWiki::DB::Util::Error -type => 'ERROR_REPORTED';
196 }
197 };
198 unshift @{$WIKI->{event}->{database_loaded}}, sub {
199 my $wiki = shift;
200 unshift @{$wiki->{db}->{event}->{error}}, sub {
201 my ($db, $event) = @_;
202 $error_report->($wiki, $event->{error});
203 if ($event->{error}->{-type} eq 'INFO_DB_PROP_OPENED') {
204 unshift @{$db->{prop}->{$event->{error}->{prop}}->{-db}
205 ->{event}->{error}}, sub {
206 my ($db, $event) = @_;
207 $error_report->($wiki, $event->{error});
208 };
209 }
210 }; # database error
211 }; # database_loaded
212 }
213
214 ## -- Preparing for Misc. Error Reports --
215
216 if ($WIKI->{config}->{debug}->{general}) {
217 $main::SIG{__WARN__} = sub {
218 push @{$WIKI->{var}->{error}||=[]}, {
219 description => Message::Markup::XML::Node->new
220 (type => '#text',
221 value => $_[0]),
222 };
223 };
224 }
225
226 ## -- (Declaring for) Initializing $wiki->{var} --
227
228 push @{$WIKI->{event}->{setting_initial_variables}}, sub {
229 my $wiki = shift;
230 ## Database access mode
231 $wiki->{var}->{db}->{read_only}->{'#default'} = 1;
232
233 ## Input parameter
234 require SuikaWiki::Input::HTTP;
235 $wiki->{input} = SuikaWiki::Input::HTTP->new (wiki => $wiki);
236 $wiki->{input}->{decoder}->{'#default'} = sub {
237 my ($http, $s, $temp_params) = @_;
238 return main::code_convert (\$s, $wiki->{config}->{charset}->{internal},
239 lc (@{$temp_params->{_charset_}||[]}[0])
240 || $wiki->{config}->{charset}->{uri_param});
241 };
242
243 ## User agent negotiation
244 $wiki->{var}->{client}->{user_agent_name}
245 = $wiki->{input}->meta_variable ('HTTP_USER_AGENT');
246 $wiki->{var}->{client}->{used_for_negotiate} = ['User-Agent'];
247 try {
248 my $dg = SuikaWiki::Plugin->module_package ('Downgrade');
249 $dg->set_downgrade_flags ($wiki) if $dg;
250 } catch SuikaWiki::Plugin::error with {
251 my $err = shift;
252 $err->raise unless $err->{-type} eq 'PLUGIN_NOT_FOUND';
253 };
254
255 ## TODO: PATH_INFO support
256
257 ## URI query parameter
258 my $page = $wiki->{input}->meta_variable ('QUERY_STRING');
259 if ($page and not (index ($page, '=') > -1)) {
260 $page =~ tr/+/ /;
261 $page =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge;
262 $page = main::code_convert
263 (\$page, $wiki->{config}->{charset}->{internal},
264 $wiki->{config}->{charset}->{uri_query});
265 } else {
266 $page = $wiki->{input}->parameter ('mypage');
267 }
268
269 ## ISSUE: WikiName normalization needed
270 $page =~ s/\s+/\x20/g;
271 $page =~ s/^\x20//; $page =~ s/\x20+$//;
272 $page =~ tr/\x00-\x1F\x7F//d;
273 if ($page) {
274 $wiki->{var}->{page} = $wiki->name ($page);
275 } else {
276 $wiki->{var}->{page} = $wiki->{config}->{page}->{Default};
277 }
278
279 ## Mode
280 my $mode = $wiki->{input}->parameter ('mode')
281 || $wiki->{input}->parameter ('mycmd') ## for compatibility with
282 || 'default'; ## YukiWiki and SuikaWiki 2
283 $mode =~ tr/-/_/;
284 if ($mode eq 'default' or $mode =~ /[^0-9A-Za-z_]/) {
285 my $cookie = $wiki->{input}->meta_variable ('HTTP_COOKIE');
286 ## BUG: this code is not strict
287 if ($cookie =~ /SelectedMode=([0-9A-Za-z_-]+)/) {
288 $mode = $1; $mode =~ tr/-/_/;
289 } else {
290 $mode = 'read';
291 }
292 push @{$wiki->{var}->{client}->{used_for_negotiate}}, 'Cookie';
293 }
294 $wiki->{var}->{mode} = $mode;
295 };
296
297
298 #### ---- Per-Session ----
299
300 ## -- Initializing $wiki->{var} (Actual) --
301
302 $WIKI->init_variables; ## Per-session variables
303
304 ## -- Instantiating WikiView --
305
306 try {
307 $WIKI->view_in_mode
308 (mode => $WIKI->{var}->{mode},
309 method => $WIKI->{input}->meta_variable ('REQUEST_METHOD'));
310 } catch SuikaWiki::DB::Util::Error with {
311 my $err = shift;
312 unless ($err->{-type} eq 'ERROR_REPORTED') {
313 $WIKI->view_in_mode (mode => '-wdb--fatal-error');
314 }
315 } catch SuikaWiki::View::Implementation::error with {
316 my $err = shift;
317 $err->throw unless $err->{-type} eq 'ERROR_REPORTED';
318 } catch SuikaWiki::Format::Definition::error with {
319 $WIKI->view_in_mode (mode => '-wf--converter-not-found');
320 } finally {
321 $WIKI->close_input;
322 $WIKI->close_db;
323 };
324 exit;
325
326
327
328 ## -- Terminating WikiEngine --
329
330 END {
331 $WIKI->exit;
332 }
333
334 =head1 SYNOPSIS
335
336 In your C<suikawiki.cgi>, write as:
337
338 #!/usr/bin/perl
339 BEGIN { $0 = ''.$0 }
340 use strict;
341 use lib qw(lib);
342 use CGI::Carp qw(fatalsToBrowser);
343 require 'suikawiki.pl';
344
345 =head1 SEE ALSO
346
347 <http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SuikaWiki>,
348 <http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SWHCS>,
349 C<wiki.cgi>, C<wikidata/suikawiki-config.ph>,
350 C<SuikaWiki::Implementation>
351
352 =head1 LICENSE
353
354 Copyright 2000-2004 Wakaba <w@suika.fam.cx>, et. al. All rights reserved.
355
356 This program is free software; you can redistribute it and/or
357 modify it under the same terms as Perl itself.
358
359 =cut
360
361 1; # $Date: 2004/03/11 10:13:46 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24