/[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 - (hide annotations) (download)
Fri Mar 12 04:54:37 2004 UTC (20 years, 8 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 wakaba 1.22 =head1 NAME
2    
3 wakaba 1.24 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 wakaba 1.22
13     =cut
14    
15 wakaba 1.24 package wiki::driver::http;
16 wakaba 1.1 use strict;
17 wakaba 1.28 our $VERSION = do{my @r=(q$Revision: 1.27 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
18 wakaba 1.24
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 wakaba 1.22
37 wakaba 1.24 ## -- Preparing Dying Message as HTTP Response --
38    
39 wakaba 1.22 require SuikaWiki::Output::CGICarp;
40 wakaba 1.24 $SuikaWiki::Output::CGICarp::CUSTOM_REASON_TEXT = 'Internal WikiEngine Error';
41 wakaba 1.22 CGI::Carp::set_message (sub {
42 wakaba 1.24 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 wakaba 1.22 });
59    
60 wakaba 1.24 ## -- Loading Configuration File --
61    
62 wakaba 1.28 $_->($WIKI) for our @Config;
63 wakaba 1.24
64     ## -- Loading Modules --
65 wakaba 1.1
66 wakaba 1.24 use SuikaWiki::DB::Util::Error;
67     require SuikaWiki::Name::Space;
68 wakaba 1.23
69 wakaba 1.22 ## -- Transitional Functions --
70 wakaba 1.1
71 w 1.9 # [to be obsolete] ->Message::MIME::Charset
72 wakaba 1.22 sub main::code_convert {
73 wakaba 1.24 my ($contentref, $code, $srccode) = @_;
74     return $$contentref if $$contentref !~ /[^\x21-\x7E]/;
75 wakaba 1.1 require Jcode;
76 wakaba 1.15 $code ||= $WIKI->{config}->{charset}->{internal};
77     for ($code, $srccode) {
78 wakaba 1.23 s/[^0-9A-Za-z_.+-]+//g;
79 wakaba 1.15 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 wakaba 1.19 if ($code eq 'iso-8859-1') {
85     return $$contentref; ## TODO:
86     }
87 wakaba 1.15 $$contentref = Jcode->new ($contentref, $srccode)
88     ## Normalize FULLWIDTH characters and IDEOGRAPHIC SPACE
89 wakaba 1.25 ->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 wakaba 1.23 # ->tr (qq(\x8E\xDE\x8E\xDF) => qq(\xA1\xAB\xA1\xAC))
91     ->h2z (1)
92 wakaba 1.15 ->$code;
93     return $$contentref;
94 wakaba 1.1 }
95    
96 wakaba 1.22 ## -- Initializing WikiPlugin --
97 wakaba 1.15
98 wakaba 1.22 $WIKI->init_plugin; ## WikiPlugin manager
99 wakaba 1.15
100 wakaba 1.22 ## -- Initializing WikiView --
101 wakaba 1.15
102 wakaba 1.22 $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 wakaba 1.26 my ($wiki, $event) = @_;
123     my $opt = $event->{view_in_mode};
124 wakaba 1.22 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 wakaba 1.25 $wiki->view_in_mode (mode => '-wv--no-view-definition', method => 'GET');
135 wakaba 1.22 ## 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 wakaba 1.23 my $wiki = $err->{option}->{param}->{wiki};
146 wakaba 1.22 SuikaWiki::Plugin->module_package ('Error')
147 wakaba 1.23 ->reporting_formatting_template_error ($err, $wiki,
148     trace => 1);
149 wakaba 1.22 $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 wakaba 1.23 my $wiki = $err->{option}->{param}->{wiki};
159 wakaba 1.22 SuikaWiki::Plugin->module_package ('Error')
160 wakaba 1.23 ->reporting_formatting_template_error ($err, $wiki,
161     trace => 1);
162 wakaba 1.22 $wiki->view_in_mode (mode => '-error-error', method => 'GET');
163     throw SuikaWiki::View::Implementation::error
164     -type => 'ERROR_REPORTED';
165     };
166 wakaba 1.15
167 wakaba 1.24 ## -- Preparing for WikiDatabase Error Reports --
168 wakaba 1.22 {
169     my $error_report = sub {
170     my ($wiki, $err) = @_;
171 wakaba 1.26 if ($err->{-def}->{level} eq 'fatal') {
172     $wiki->close_db if $wiki->{db};
173     }
174 wakaba 1.22 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 wakaba 1.23 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 wakaba 1.26 . $err->text
183     . ($wiki->{config}->{debug}->{db} > 1 ? Carp::longmess () : '');
184 wakaba 1.22 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 wakaba 1.26 if ($err->{-def}->{level} eq 'fatal'
192 wakaba 1.24 # or $err->{-def}->{level} eq 'stop' ## for debug
193 wakaba 1.26 ) {
194     $wiki->view_in_mode (mode => '-wdb--fatal-error');
195     throw SuikaWiki::DB::Util::Error -type => 'ERROR_REPORTED';
196     }
197 wakaba 1.22 };
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 wakaba 1.15 }
213 wakaba 1.22
214 wakaba 1.24 ## -- Preparing for Misc. Error Reports --
215 wakaba 1.15
216 wakaba 1.22 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 w 1.9
226 wakaba 1.24 ## -- (Declaring for) Initializing $wiki->{var} --
227 wakaba 1.21
228     push @{$WIKI->{event}->{setting_initial_variables}}, sub {
229     my $wiki = shift;
230 wakaba 1.24 ## Database access mode
231 wakaba 1.15 $wiki->{var}->{db}->{read_only}->{'#default'} = 1;
232    
233 wakaba 1.24 ## Input parameter
234 wakaba 1.15 require SuikaWiki::Input::HTTP;
235 wakaba 1.21 $wiki->{input} = SuikaWiki::Input::HTTP->new (wiki => $wiki);
236 wakaba 1.15 $wiki->{input}->{decoder}->{'#default'} = sub {
237     my ($http, $s, $temp_params) = @_;
238 wakaba 1.18 return main::code_convert (\$s, $wiki->{config}->{charset}->{internal},
239     lc (@{$temp_params->{_charset_}||[]}[0])
240 wakaba 1.15 || $wiki->{config}->{charset}->{uri_param});
241     };
242 wakaba 1.24
243     ## User agent negotiation
244 wakaba 1.15 $wiki->{var}->{client}->{user_agent_name}
245     = $wiki->{input}->meta_variable ('HTTP_USER_AGENT');
246     $wiki->{var}->{client}->{used_for_negotiate} = ['User-Agent'];
247 wakaba 1.27 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 wakaba 1.15
255     ## TODO: PATH_INFO support
256 wakaba 1.24
257     ## URI query parameter
258 wakaba 1.15 my $page = $wiki->{input}->meta_variable ('QUERY_STRING');
259 wakaba 1.24 if ($page and not (index ($page, '=') > -1)) {
260 wakaba 1.15 $page =~ tr/+/ /;
261     $page =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge;
262     $page = main::code_convert
263 wakaba 1.18 (\$page, $wiki->{config}->{charset}->{internal},
264 wakaba 1.15 $wiki->{config}->{charset}->{uri_query});
265     } else {
266     $page = $wiki->{input}->parameter ('mypage');
267     }
268 wakaba 1.25
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 wakaba 1.15 if ($page) {
274 wakaba 1.25 $wiki->{var}->{page} = $wiki->name ($page);
275 wakaba 1.15 } 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 wakaba 1.24 if ($mode eq 'default' or $mode =~ /[^0-9A-Za-z_]/) {
285     my $cookie = $wiki->{input}->meta_variable ('HTTP_COOKIE');
286 wakaba 1.15 ## 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 wakaba 1.21
297 wakaba 1.24
298     #### ---- Per-Session ----
299    
300 wakaba 1.22 ## -- Initializing $wiki->{var} (Actual) --
301    
302     $WIKI->init_variables; ## Per-session variables
303 wakaba 1.15
304 wakaba 1.22 ## -- 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 wakaba 1.24 unless ($err->{-type} eq 'ERROR_REPORTED') {
313     $WIKI->view_in_mode (mode => '-wdb--fatal-error');
314     }
315 wakaba 1.22 } catch SuikaWiki::View::Implementation::error with {
316     my $err = shift;
317     $err->throw unless $err->{-type} eq 'ERROR_REPORTED';
318 wakaba 1.25 } catch SuikaWiki::Format::Definition::error with {
319     $WIKI->view_in_mode (mode => '-wf--converter-not-found');
320 wakaba 1.22 } finally {
321     $WIKI->close_input;
322     $WIKI->close_db;
323 wakaba 1.18 };
324 wakaba 1.22 exit;
325 wakaba 1.18
326 wakaba 1.24
327    
328 wakaba 1.22 ## -- Terminating WikiEngine --
329 wakaba 1.24
330 wakaba 1.14 END {
331 wakaba 1.22 $WIKI->exit;
332     }
333 wakaba 1.1
334 wakaba 1.24 =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 wakaba 1.1 =head1 LICENSE
353    
354 wakaba 1.24 Copyright 2000-2004 Wakaba <w@suika.fam.cx>, et. al. All rights reserved.
355 wakaba 1.1
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 wakaba 1.28 1; # $Date: 2004/03/11 10:13:46 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24