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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.29 - (show annotations) (download)
Sat Mar 20 03:33:29 2004 UTC (21 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.28: +45 -2 lines
File MIME type: text/plain
(->{var}->{db}->{lock_module}, ->{var}->{db}->{read_lock_module}): New default values

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.28 $=~/\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 ## -- Setting Upper Bound for Too Many Accesses at the Same Time --
65
66 {
67 require SuikaWiki::DB::Util::Lock;
68 my $lock = SuikaWiki::DB::Util::Lock
69 ->new (-directory => $WIKI->{config}->{path_to}->{db__lock__dir},
70 -name => 'main',
71 -share => 1,
72 -limit => 15);
73 $lock->lock or do {
74 require SuikaWiki::Output::HTTP;
75 my $out = SuikaWiki::Output::HTTP->new;
76 $out->{status_code} = 503;
77 $out->{status_phrase} = q<WikiEngine Busy>;
78 $out->add_header_field ('Retry-After' => 120);
79 $out->{entity}->{media_type} = q<text/html>;
80 $out->{entity}->{charset} = q<iso-8859-1>;
81 $out->{entity}->{language} = [q<en>];
82 $out->{entity}->{body_is_octet_stream} = 1;
83 my $wiki_name_version = sprintf '%s/%s %s/%s',
84 $WIKI->{driver_name}, $WIKI->{driver_version},
85 $WIKI->{engine_name}, $WIKI->{engine_version};
86 for ($wiki_name_version) {
87 s/&/&amp;/g; s/</&lt;/g; s/([^\x20-\x7E])/sprintf '&#x%02X;', ord $1/ge;
88 };
89 $out->{entity}->{body} = qq<
90 <!DOCTYPE html SYSTEM>
91 <title>503 WikiEngine Busy</title>
92 <h1>WikiEngine Busy</h1>
93 <p>WikiEngine is now busy and is unable to complete your request.
94 Please retry again after a moment.</p>
95 <address>$wiki_name_version</address>
96 >;
97 $out->output (output => 'http-cgi');
98 exit;
99 };
100 END { $lock->unlock }
101 }
102
103 ## -- Loading Modules --
104
105 use SuikaWiki::DB::Util::Error;
106 require SuikaWiki::Name::Space;
107
108 ## -- Transitional Functions --
109
110 # [to be obsolete] ->Message::MIME::Charset
111 sub main::code_convert {
112 my ($contentref, $code, $srccode) = @_;
113 return $$contentref if $$contentref !~ /[^\x21-\x7E]/;
114 require Jcode;
115 $code ||= $WIKI->{config}->{charset}->{internal};
116 for ($code, $srccode) {
117 s/[^0-9A-Za-z_.+-]+//g;
118 if ($_ eq 'euc-jp') { $_ = 'euc' }
119 elsif ($_ eq 'iso-2022-jp') { $_ = 'jis' }
120 elsif ($_ eq 'utf-8') { $_ = 'utf8' }
121 elsif ($_ eq 'shift_jis') { $_ = 'sjis' }
122 }
123 if ($code eq 'iso-8859-1') {
124 return $$contentref; ## TODO:
125 }
126 $$contentref = Jcode->new ($contentref, $srccode)
127 ## Normalize FULLWIDTH characters and IDEOGRAPHIC SPACE
128 ->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&,.:;?!`^_/|()[]{}+$%#*@=>< '"~-))
129 # ->tr (qq(\x8E\xDE\x8E\xDF) => qq(\xA1\xAB\xA1\xAC))
130 ->h2z (1)
131 ->$code;
132 return $$contentref;
133 }
134
135 ## -- Initializing WikiPlugin --
136
137 $WIKI->init_plugin; ## WikiPlugin manager
138
139 ## -- Initializing WikiView --
140
141 $WIKI->init_view; ## WikiView manager
142 $WIKI->{view}->register_common_modes;
143
144 ## WikiView manager error handler
145 push @{$WIKI->{event}->{view_error}}, sub {
146 my ($wiki, $event) = @_;
147 SuikaWiki::Plugin->module_package ('Error')
148 ->report_error_simple
149 ($wiki, WikiView => $event->{error}->text,
150 -trace => 1)
151 if $event->{error}->{-def}->{level} eq 'fatal'
152 or $wiki->{config}->{debug}->{view};
153 unless ($event->{error}->{-def}->{level} eq 'fatal'
154 or $event->{error}->{-def}->{level} eq 'stop') {
155 $event->{cancel} = 1;
156 }
157 };
158
159 ## "view_in_mode" method definition
160 push @{$WIKI->{event}->{view_in_mode}}, sub {
161 my ($wiki, $event) = @_;
162 my $opt = $event->{view_in_mode};
163 my $arg = {condition => {mode => $opt->{mode} || '-error',
164 output => 'http-cgi',
165 http_method => $opt->{method} || 'GET'}};
166 my $viewobj = $wiki->{view}->instantiate ($opt->{mode} || '-error', $arg);
167 if (ref $viewobj) {
168 $viewobj->main ($arg);
169 } elsif ($opt->{mode} ne '-error') {
170 report SuikaWiki::View::Implementation::error
171 -type => 'WARN_VIEW_NOT_DEFINED', condition => $arg->{condition},
172 -object => $wiki->{view}, method => 'view_in_mode';
173 $wiki->view_in_mode (mode => '-wv--no-view-definition', method => 'GET');
174 ## TODO: cache control for non-GET
175 } else {
176 die "Some error occured. Additionally, error reporting mode not defined";
177 }
178 };
179
180 ## WikiView formatting template error handler
181 $WIKI->{config}->{catch}->{formatter_view}
182 = catch Message::Util::Formatter::error with {
183 my $err = shift;
184 my $wiki = $err->{option}->{param}->{wiki};
185 SuikaWiki::Plugin->module_package ('Error')
186 ->reporting_formatting_template_error ($err, $wiki,
187 trace => 1);
188 $wiki->view_in_mode (mode => '-error', method => 'GET');
189 throw SuikaWiki::View::Implementation::error
190 -type => 'ERROR_REPORTED';
191 };
192
193 ## WikiView formatting template error handler (occured in "-error" mode)
194 $WIKI->{config}->{catch}->{formatter_view_error}
195 = catch Message::Util::Formatter::error with {
196 my $err = shift;
197 my $wiki = $err->{option}->{param}->{wiki};
198 SuikaWiki::Plugin->module_package ('Error')
199 ->reporting_formatting_template_error ($err, $wiki,
200 trace => 1);
201 $wiki->view_in_mode (mode => '-error-error', method => 'GET');
202 throw SuikaWiki::View::Implementation::error
203 -type => 'ERROR_REPORTED';
204 };
205
206 ## -- Preparing for WikiDatabase Error Reports --
207 {
208 my $error_report = sub {
209 my ($wiki, $err) = @_;
210 if ($err->{-def}->{level} eq 'fatal') {
211 $wiki->close_db if $wiki->{db};
212 }
213 my $report = ($err->{-def}->{level} eq 'fatal' or
214 $err->{-def}->{level} eq 'stop' or
215 $wiki->{config}->{debug}->{db}) ? 1 : 0;
216 if ($report and $wiki->{config}->{path_to}->{db__content__error_log}) {
217 my $err_msg = caller (1).($err->{method}? '->'.$err->{method}: '').': '
218 .(defined $err->{file}? $err->{file} . ': ' : '')
219 .(defined $err->{prop}? $err->{prop} . ': ' : '')
220 .(defined $err->{key}? join ('//', @{$err->{key}}).': ':'')
221 . $err->text
222 . ($wiki->{config}->{debug}->{db} > 1 ? Carp::longmess () : '');
223 open LOG, '>>', $wiki->{config}->{path_to}->{db__content__error_log};
224 print LOG scalar (gmtime), " @{[$$]} {$err->{-def}->{level}}: ",
225 $err_msg, "\n";
226 close LOG;
227 }
228 SuikaWiki::Plugin->module_package ('WikiDB')
229 ->reporting_error ($err, $wiki) if $report;
230 if ($err->{-def}->{level} eq 'fatal'
231 # or $err->{-def}->{level} eq 'stop' ## for debug
232 ) {
233 $wiki->view_in_mode (mode => '-wdb--fatal-error');
234 throw SuikaWiki::DB::Util::Error -type => 'ERROR_REPORTED';
235 }
236 };
237 unshift @{$WIKI->{event}->{database_loaded}}, sub {
238 my $wiki = shift;
239 unshift @{$wiki->{db}->{event}->{error}}, sub {
240 my ($db, $event) = @_;
241 $error_report->($wiki, $event->{error});
242 if ($event->{error}->{-type} eq 'INFO_DB_PROP_OPENED') {
243 unshift @{$db->{prop}->{$event->{error}->{prop}}->{-db}
244 ->{event}->{error}}, sub {
245 my ($db, $event) = @_;
246 $error_report->($wiki, $event->{error});
247 };
248 }
249 }; # database error
250 }; # database_loaded
251 }
252
253 ## -- Preparing for Misc. Error Reports --
254
255 if ($WIKI->{config}->{debug}->{general}) {
256 $main::SIG{__WARN__} = sub {
257 push @{$WIKI->{var}->{error}||=[]}, {
258 description => Message::Markup::XML::Node->new
259 (type => '#text',
260 value => $_[0]),
261 };
262 };
263 }
264
265 ## -- (Declaring for) Initializing $wiki->{var} --
266
267 push @{$WIKI->{event}->{setting_initial_variables}}, sub {
268 my $wiki = shift;
269 ## Database access mode
270 $wiki->{var}->{db}->{read_only}->{'#default'} = 1;
271 $wiki->{var}->{db}->{lock_module}->{'#default'}
272 = q<SuikaWiki::DB::Util::Lock>;
273 $wiki->{var}->{db}->{read_lock_module}->{'#default'}
274 = q<SuikaWiki::DB::Lock::NoLock>;
275
276 ## Input parameter
277 require SuikaWiki::Input::HTTP;
278 $wiki->{input} = SuikaWiki::Input::HTTP->new (wiki => $wiki);
279 $wiki->{input}->{decoder}->{'#default'} = sub {
280 my ($http, $s, $temp_params) = @_;
281 return main::code_convert (\$s, $wiki->{config}->{charset}->{internal},
282 lc (@{$temp_params->{_charset_}||[]}[0])
283 || $wiki->{config}->{charset}->{uri_param});
284 };
285
286 ## User agent negotiation
287 $wiki->{var}->{client}->{user_agent_name}
288 = $wiki->{input}->meta_variable ('HTTP_USER_AGENT');
289 $wiki->{var}->{client}->{used_for_negotiate} = ['User-Agent'];
290 try {
291 my $dg = SuikaWiki::Plugin->module_package ('Downgrade');
292 $dg->set_downgrade_flags ($wiki) if $dg;
293 } catch SuikaWiki::Plugin::error with {
294 my $err = shift;
295 $err->raise unless $err->{-type} eq 'PLUGIN_NOT_FOUND';
296 };
297
298 ## TODO: PATH_INFO support
299
300 ## URI query parameter
301 my $page = $wiki->{input}->meta_variable ('QUERY_STRING');
302 if ($page and not (index ($page, '=') > -1)) {
303 $page =~ tr/+/ /;
304 $page =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge;
305 $page = main::code_convert
306 (\$page, $wiki->{config}->{charset}->{internal},
307 $wiki->{config}->{charset}->{uri_query});
308 } else {
309 $page = $wiki->{input}->parameter ('mypage');
310 }
311
312 ## ISSUE: WikiName normalization needed
313 $page =~ s/\s+/\x20/g;
314 $page =~ s/^\x20//; $page =~ s/\x20+$//;
315 $page =~ tr/\x00-\x1F\x7F//d;
316 if ($page) {
317 $wiki->{var}->{page} = $wiki->name ($page);
318 } else {
319 $wiki->{var}->{page} = $wiki->{config}->{page}->{Default};
320 }
321
322 ## Mode
323 my $mode = $wiki->{input}->parameter ('mode')
324 || $wiki->{input}->parameter ('mycmd') ## for compatibility with
325 || 'default'; ## YukiWiki and SuikaWiki 2
326 $mode =~ tr/-/_/;
327 if ($mode eq 'default' or $mode =~ /[^0-9A-Za-z_]/) {
328 my $cookie = $wiki->{input}->meta_variable ('HTTP_COOKIE');
329 ## BUG: this code is not strict
330 if ($cookie =~ /SelectedMode=([0-9A-Za-z_-]+)/) {
331 $mode = $1; $mode =~ tr/-/_/;
332 } else {
333 $mode = 'read';
334 }
335 push @{$wiki->{var}->{client}->{used_for_negotiate}}, 'Cookie';
336 }
337 $wiki->{var}->{mode} = $mode;
338 };
339
340
341 #### ---- Per-Session ----
342
343 ## -- Initializing $wiki->{var} (Actual) --
344
345 $WIKI->init_variables; ## Per-session variables
346
347 ## -- Instantiating WikiView --
348
349 try {
350 $WIKI->view_in_mode
351 (mode => $WIKI->{var}->{mode},
352 method => $WIKI->{input}->meta_variable ('REQUEST_METHOD'));
353 } catch SuikaWiki::DB::Util::Error with {
354 my $err = shift;
355 unless ($err->{-type} eq 'ERROR_REPORTED') {
356 $WIKI->view_in_mode (mode => '-wdb--fatal-error');
357 }
358 } catch SuikaWiki::View::Implementation::error with {
359 my $err = shift;
360 $err->throw unless $err->{-type} eq 'ERROR_REPORTED';
361 } catch SuikaWiki::Format::Definition::error with {
362 $WIKI->view_in_mode (mode => '-wf--converter-not-found');
363 } finally {
364 $WIKI->close_input;
365 $WIKI->close_db;
366 };
367 exit;
368
369
370
371 ## -- Terminating WikiEngine --
372
373 END {
374 $WIKI->exit;
375 }
376
377 =head1 SYNOPSIS
378
379 In your C<suikawiki.cgi>, write as:
380
381 #!/usr/bin/perl
382 BEGIN { $0 = ''.$0 }
383 use strict;
384 use lib qw(lib);
385 use CGI::Carp qw(fatalsToBrowser);
386 require 'suikawiki.pl';
387
388 =head1 SEE ALSO
389
390 <http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SuikaWiki>,
391 <http://suika.fam.cx/~wakaba/-temp/wiki/wiki?SWHCS>,
392 C<wiki.cgi>, C<wikidata/suikawiki-config.ph>,
393 C<SuikaWiki::Implementation>
394
395 =head1 LICENSE
396
397 Copyright 2000-2004 Wakaba <w@suika.fam.cx>, et. al. All rights reserved.
398
399 This program is free software; you can redistribute it and/or
400 modify it under the same terms as Perl itself.
401
402 =cut
403
404 1; # $Date: 2004/03/12 04:54:37 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24