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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations) (download)
Sun May 25 10:56:24 2003 UTC (21 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.6: +45 -5 lines
File MIME type: text/plain
suikawiki.pl: better summalizing

1 wakaba 1.1 # -*- perl -*-
2     use strict;
3    
4     package main;
5 wakaba 1.7 our $VERSION = '2.'.do{my @r=(q$Revision: 1.6 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
6 wakaba 1.1 binmode STDOUT; binmode STDIN;
7 wakaba 1.3 use Fcntl;
8 wakaba 1.1 require Yuki::YukiWikiCache;
9 wakaba 1.3 require SuikaWiki::Name::Space;
10     require SuikaWiki::View;
11 wakaba 1.1 require SuikaWiki::Plugin;
12     our %fmt; ## formatter objects
13     our %embed_command = (
14     form => qr/\[\[\#form(?:\(([A-Za-z0-9-]+)\))?:'((?:[^'\\]|\\.)*)':'((?:[^'\\]|\\.)*)'(?::'((?:[^'\\]|\\.)*)')?\]\]/,
15     );
16     our ($modifier_dbtype,$url_cgi,%uri,%PathTo);
17 wakaba 1.3 our (%PageName,$kanjicode,$lang);
18 wakaba 1.1
19 wakaba 1.6 our %form;
20 wakaba 1.1 our %database;
21     our $database = bless {}, 'wiki::dummy';
22 wakaba 1.5 our %interwiki;
23 wakaba 1.1 my %command_do = (
24     default => \&do_view,
25     adminchangepassword => \&do_adminchangepassword,
26     write => \&do_write,
27     searchform => \&do_searchform,
28     comment => \&do_comment,
29     RandomJump => \&do_random_jump,
30     wikiform => \&do_wikiform,
31     );
32     our $UA = ''; ## User agent name
33     $| = 1;
34 wakaba 1.5 my $HAS_XML = SuikaWiki::Plugin->feature ('SuikaWiki::Markup::XML');
35     my $NS_XHTML1 = 'http://www.w3.org/1999/xhtml';
36 wakaba 1.1
37     sub main {
38     $UA = $main::ENV{HTTP_USER_AGENT};
39     &open_db;
40     &init_form;
41 wakaba 1.6 for (@{$SuikaWiki::Plugin::On{WikiDatabaseLoaded}||[]}) {
42     &{$_};
43     }
44 wakaba 1.1 if ($command_do{$form{mycmd}}) {
45     &{$command_do{$form{mycmd}}};
46     } else {
47     &{$command_do{default}};
48     }
49     &close_db;
50     }
51    
52     sub do_view {
53     my $content = $database{$form{mypage}};
54     my $lm = $database->mtime ($form{mypage});
55     &load_formatter ('view');
56     my $view = $form{mycmd};
57     if ($view eq 'edit') {
58     $view = 'adminedit' if $form{admin};
59     } elsif ($view =~ /[^0-9A-Za-z_]/) {
60     $view = 'default'
61     }
62     if ($view eq 'default' || !$view) {
63     ## BUG: this code is not strict
64     if ($main::ENV{HTTP_COOKIE} =~ /SelectedMode=([0-9A-Za-z]+)/) {
65     $view = $1;
66     } else {
67     $view = 'read';
68     }
69     }
70     my ($magic, $content) = &SuikaWiki::Plugin::magic_and_content (undef, $content);
71     $magic ||= '#?SuikaWiki/0.9';
72     my $o = bless {param => \%form, page => $form{mypage}, toc => [],
73     magic => $magic, content => $content,
74     formatter => $fmt{view}, &_compatible_options ()}, 'SuikaWiki::Plugin';
75 wakaba 1.3 my $view_def = SuikaWiki::View->definition ($view);
76     if (!$view_def->check ($o)) {
77 wakaba 1.1 print "Status: 406 Unsupported Media Type\n";
78     $view = '-UnsupportedMediaType';
79 wakaba 1.3 $view_def = SuikaWiki::View->definition ($view);
80 wakaba 1.1 }
81 wakaba 1.3 my $media = $view_def->properties->{media};
82     if ($view_def->properties->{xmedia} && $UA =~ /Gecko/) {
83     $media = $view_def->properties->{xmedia};
84 wakaba 1.1 $o->{media} = $media;
85     } elsif ($UA =~ m#Mozilla/0\..+Windows#) {
86     $kanjicode = 'shift_jis';
87     }
88     if ($magic =~ m!^\#\?SuikaWiki/0.9!) {
89     &print_header ($form{mypage}, -last_modified => ($magic =~ /interactive="yes"/ ? time : $lm),
90     -expires => ($magic =~ /interactive="yes"/ ? 1 : undef), o => $o,
91     -media => $media, -magic => $magic, content => $content);
92     } else {
93     &print_header($form{mypage}, -media => $media,
94     -magic => $magic, -last_modified => $lm, o => $o);
95     }
96     if ($kanjicode ne 'euc') {
97 wakaba 1.3 my $s = $fmt{view}->replace ($view_def->as_string => $o, {formatter => $fmt{view}});
98 wakaba 1.1 print &code_convert (\$s => $kanjicode);
99     } else {
100 wakaba 1.3 print $fmt{view}->replace ($view_def->as_string => $o, {formatter => $fmt{view}});
101 wakaba 1.1 }
102     }
103    
104     sub _do_view_msg (%) {
105     my %option = @_;
106     &load_formatter ('view');
107     my $o = bless {param => \%form, page => $option{-page}, toc => [], condition => \%option,
108     formatter => $fmt{view}, &_compatible_options ()}, 'SuikaWiki::Plugin';
109 wakaba 1.3 my $view_def = SuikaWiki::View->definition ($option{-view});
110     unless ($view_def->check ($o)) {
111 wakaba 1.1 print "Status: 406 Unsupported Media Type\n";
112     $option{-view} = '-UnsupportedMediaType';
113 wakaba 1.3 $view_def = SuikaWiki::View->definition ($option{-view});
114 wakaba 1.1 }
115 wakaba 1.3 my $media = $view_def->properties->{media};
116     if ($view_def->properties->{xmedia} && $UA =~ /Gecko/) {
117     $media = $view_def->properties->{xmedia};
118 wakaba 1.1 $o->{media} = $media;
119     }
120     &print_header($option{-page}, -media => $media, o => $o, -goto => $option{-goto});
121 wakaba 1.3 print $fmt{view}->replace ($view_def->as_string => $o, {formatter => $fmt{view}});
122 wakaba 1.1 }
123    
124     sub do_adminchangepassword {
125     if ($form{mynewpassword} ne $form{mynewpassword2}) {
126     &_do_view_msg (-view => '-error', -page => $form{mypage},
127     error_message => &Resource ('Error:PasswordMismatch'));
128     return;
129     }
130     my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage});
131     if ($validpassword_crypt) {
132     if (not &valid_password($form{myoldpassword})) {
133     &_do_view_msg (-view => '-error', -page => $form{mypage},
134     error_message => &Resource ('Error:PasswordIsIncorrect'));
135     return;
136     }
137     }
138     my ($sec, $min, $hour, $day, $mon, $year, $weekday) = localtime(time);
139     my (@token) = ('0'..'9', 'A'..'Z', 'a'..'z');
140     my $salt1 = $token[(time | $$) % scalar(@token)];
141     my $salt2 = $token[($sec + $min*60 + $hour*60*60) % scalar(@token)];
142     my $crypted = crypt($form{mynewpassword}, "$salt1$salt2");
143     $database->meta (AdminPassword => $PageName{AdminSpecialPage} => $crypted);
144    
145     &_do_view_msg (-view => '-wrote', -page => $form{mypage});
146     }
147    
148     sub valid_password ($) {
149     my ($validpassword_crypt) = $database->meta (AdminPassword => $PageName{AdminSpecialPage});
150     return crypt (shift, $validpassword_crypt) eq $validpassword_crypt ? 1 : 0;
151     }
152    
153     sub do_write {
154     if (&frozen_reject()) {
155     return;
156     }
157    
158     if (not &is_editable($form{mypage})) {
159     &_do_view_msg (-view => '-error', -page => $form{mypage},
160     error_message => &Resource ('Error:ThisPageIsUneditable'));
161     return;
162     }
163    
164     ## Check confliction
165     if ($form{myLastModified} ne $database->mtime ($form{mypage})) {
166     &_do_view_msg (-view => '-conflict', -page => $form{mypage});
167     return;
168     }
169    
170     if ($form{mymsg}) {
171     if ($form{mytouch} || !ref $database) {
172     $database{$form{mypage}} = $form{mymsg};
173     } else {
174     $database->STORE ($form{mypage} => $form{mymsg}, -touch => 0);
175     }
176     $database->meta (IsFrozen => $form{mypage} => 0 + $form{myfrozen});
177     my $fragment = '';
178     $fragment .= qq(;after_edit_cmd=@{[&encode($form{after_edit_cmd})]}) if $form{after_edit_cmd};
179     if ($form{__comment_anchor_index}) {
180     $fragment .= qq(#anchor-$form{__comment_anchor_index});
181     } elsif ($form{__wikiform_anchor_index}) {
182     $fragment .= qq(#wikiform-$form{__wikiform_anchor_index});
183     }
184     &_do_view_msg (-view => '-wrote', -page => $form{mypage}, -goto => $url_cgi.'?mycmd='.&encode($form{after_edit_cmd}||'default').';mypage='.&encode($form{mypage}).qq(;x-param=@{[time.[0..9]->[rand 10]]}$fragment));
185     } else {
186     delete $database{$form{mypage}};
187     &_do_view_msg (-view => '-deleted', -page => $form{mypage});
188     }
189     }
190    
191     sub _compatible_options () {
192     (use_anchor_name => ($UA =~ m#Mozilla/[12]\.|Microsoft Internet Explorer# ? 1 : 0));
193     }
194    
195     sub get_search_result ($;%) {
196     my $word = lc shift;
197     my $SearchResult = SuikaWiki::Plugin->cache ('search');
198     my %option = @_;
199     my @r;
200     unless (defined $SearchResult->{$word}) {
201     for my $page (keys %database) {
202     next if !$option{-match_myself} && ($page eq $word);
203     my $content = lc $database{$page};
204     $content =~ s/^[^\x0A\x0D]+[\x0D\x0A]+//s;
205     if (index (lc $page, $word) > -1) {
206     my $c = $content =~ s/\Q$word\E//g;
207     push @r, [$page, $c+20];
208     } elsif (index ($word, lc $page) > -1) {
209     my $c = $content =~ s/\Q$word\E//g;
210     push @r, [$page, $c+10];
211     } elsif (my $c = $content =~ s/\Q$word\E//g) {
212     push @r, [$page, $c];
213     }
214     }
215     @r = sort {$b->[1] <=> $a->[1] || $a->[0] cmp $b->[0]} @r;
216     $SearchResult->{$word} = join "\x1E", map {$_->[0]."\x1F".$_->[1]} @r;
217     } else {
218     @r = map {[split /\x1F/, $_, 2]} split /\x1E/, $SearchResult->{$word};
219     }
220     #my $em = sub { my $s = shift; $s =~ s#(\Q$word\E)#<em>$1</em>#gi; $s };
221     my $r = join "\n", map {qq(<li>[$_->[1]] <a href ="$url_cgi?@{[&encode($_->[0])]}" class="wiki">@{[&escape($_->[0])]}</a> <span class="wikipage-summary">@{[&escape(&get_subjectline($_->[0]))]}</span></li>)} @r;
222     $r = qq|<ul class="search-result">$r</ul>| if $r;
223     wantarray? ($r, scalar @r): $r;
224     }
225    
226     sub do_random_jump {
227     my @list = keys %database;
228     my $name = &encode ($list[rand @list]);
229     print "Location: $uri{wiki}?$name\n";
230     print "\n";
231     }
232    
233     sub print_header ($;%) {
234     my ($page, %option) = @_;
235     my @head;
236     $option{o}->{-header}->{class} = &is_frozen($page) ? 'frozen' : '';
237     $option{o}->{-header}->{class} .= " wiki-page-obsoleted" if $option{-magic} =~ /obsoleted="yes"/;
238     if ($option{-goto}) {
239     if ($UA =~ m#Opera|MSIE 2\.#) {
240     ## WARNING: This code may output unsafe HTML document if
241     ## $option{-goto} is not clean.
242     $option{-goto} =~ tr/;/&/ if $UA =~ m#Opera#;
243     print qq{Refresh: 0; url=$option{-goto}\n};
244     push @head, qq(<meta http-equiv="refresh" content="0; url=$option{-goto}">);
245     } elsif ($UA =~ /Gecko/) {
246     print qq{Refresh: 0; url="$option{-goto}"\n};
247     push @head, qq(<meta http-equiv="refresh" content="0; url=&quot;@{[&escape($option{-goto})]}&quot;" />);
248     } else {
249     $option{-goto} =~ tr/;/&/ if $UA =~ m#Mozilla/[1-4]\.#;
250     print qq{Refresh: 0; url="$option{-goto}"\n};
251     push @head, qq(<meta http-equiv="refresh" content="0; url=&quot;@{[&escape($option{-goto})]}&quot;">);
252     }
253     }
254     print qq{Last-Modified: @{[scalar gmtime $option{-last_modified}]}\n} if $option{-last_modified};
255     if ($option{-expires} != -1) {
256     if (defined $option{-expires}) { ## TODO: Don't use asctime
257     print qq{Expires: @{[scalar gmtime (time + $option{-expires})]}\n};
258     } elsif ($option{-media}->{expires} != -1) {
259     print qq{Expires: @{[scalar gmtime (time + $option{-media}->{expires})]}\n};
260     }
261     }
262     if ($option{-media}->{charset} && $UA =~ m#Mozilla/[12]\.#) {
263     my $ct = qq{$option{-media}->{type}; charset=@{[&get_charset_name($kanjicode,compatible=>1)]}};
264     print qq{Content-Type: $ct\n};
265     $option{o}->{-header}->{meta_ct} = qq{<meta http-equiv="content-type" content="$ct">\n};
266     } elsif (!$option{-media}->{charset} || $UA =~ m#Infomosaic|Mozilla/0\.#) {
267     print qq{Content-Type: $option{-media}->{type}\n};
268     $option{o}->{-header}->{meta_ct} = qq{<meta http-equiv="content-type" content="$option{-media}->{type}; charset=@{[&get_charset_name($kanjicode,compatible=>1)]}">\n};
269     } else {
270     my $type = $option{-media}->{type};
271 wakaba 1.4 $type = 'application/xml' if ($type =~ m!^application/(?:rdf|rss)\+xml$!) && ($UA =~ m#Gecko#);
272 wakaba 1.1 print qq{Content-Type: $type; charset=@{[&get_charset_name($kanjicode)]}\n};
273     }
274 wakaba 1.5 #if ($main::ENV{HTTP_IF_MODIFIED_SINCE}) {
275     ## TODO: IMS support
276     #}
277    
278 wakaba 1.4 ## TODO: more Vary: support
279     print <<"EOD";
280 wakaba 1.5 Vary: Negotiate,User-Agent,Accept-Language
281 wakaba 1.1 Content-Style-Type: text/css
282    
283     EOD
284     $option{o}->{-header}->{links} = join "\n", (@head);
285     }
286    
287     sub get_charset_name ($;%) {
288     my ($charset, %option) = (lc shift, @_);
289     if ($charset =~ 'euc') {
290     $charset = $option{compatible} ? 'x-euc-jp' : 'euc-jp';
291     } elsif ($charset =~ 'sjis' || $charset =~ 'shift') {
292     $charset = $option{compatible} ? 'x-sjis' : 'shift_jis';
293     } elsif ($charset =~ 'jis') {
294     $charset = 'iso-2022-jp';
295     }
296     $charset;
297     }
298    
299     sub escape {
300     my $s = shift;
301     $s =~ s|&|&amp;|g;
302     $s =~ s|<|&lt;|g;
303     $s =~ s|>|&gt;|g;
304     $s =~ s|"|&quot;|g;
305     return $s;
306     }
307    
308     sub unescape {
309     my $s = shift;
310     $s =~ s|&lt;|<|g;
311     $s =~ s|&gt;|>|g;
312     $s =~ s|&quot;|"|g;
313     $s =~ s|&amp;|&|g;
314     return $s;
315     }
316    
317     sub convert_format ($$$;%) {
318     my ($content, $d => $t, %option) = @_;
319     my $f = SuikaWiki::Plugin->format_converter ($d => $t);
320     if (ref $f) {
321     $option{content} = $content;
322     $option{from} = $d;
323     $option{to} = $t;
324     &$f ({}, bless (\%option, 'SuikaWiki::Plugin'));
325     } elsif ($option{-error_no_return}) {
326     return undef;
327     } elsif ($t =~ /HTML|xml/) {
328     length $content ? '<pre>'.&escape($content).'</pre>' : '';
329     } else {
330     $content;
331     }
332     }
333    
334     {my %FormIndex;
335     sub make_custom_form ($$$$%) {
336     my ($wfname, $definition, $template, $foption, $option) = @_;
337     ## $template is currently not used in this procedure.
338     #unless ($main::_EMBEDED) {
339     $FormIndex{$option->{page}}++;
340     if (length $definition) {
341     my $param = bless {depth=>10}, 'SuikaWiki::Plugin';
342 wakaba 1.7 $param->{page} = $option->{page};
343 wakaba 1.1 my $lastmodified = $database->mtime ($option->{page});
344     &load_formatter (qw/form_input form_option/);
345     $definition = &unescape ($definition);
346     $definition =~ s/\\(['\\])/$1/g;
347     $foption = &unescape ($foption);
348     $foption =~ s/\\(['\\])/$1/g;
349     $fmt{form_option}->replace ($foption, $param);
350     $param->{output}->{form} = 1 unless defined $param->{output}->{form};
351     $param->{output}->{form} = 0 if $main::_EMBEDED;
352     $definition .= ' %submit;' if $definition !~ /%submit/ && !$param->{output}->{nosubmit} && $param->{output}->{form};
353     $param->{output}->{page} ||= $option->{page};
354     $param->{form_disabled} = 1 if $database->meta (IsFrozen => $option->{page});
355     my $target_form = $param->{output}->{id};
356     my $r = '';
357     $r = <<EOH if $param->{output}->{form};
358     <form method="post" action="$url_cgi" id="wikiform-$FormIndex{$option->{page}}" class="wikiform">
359     <input type="hidden" name="mycmd" value="@{[$param->{form_disabled}?'default':'wikiform']}" />
360     <input type="hidden" name="mypage" value="@{[&escape($param->{output}->{page})]}" />
361     <input type="hidden" name="myLastModified" value="$lastmodified" />
362     <input type="hidden" name="mytouch" value="on" />
363     <input type="hidden" name="@{[$target_form? qq(wikiform_targetform" value="@{[&escape($target_form)]}) : qq(wikiform_index" value="$FormIndex{$option->{page}})]}" />
364     EOH
365     $r .= qq(<a name="wikiform-$FormIndex{$option->{page}}"></a>) if $UA =~ m#Mozilla/[12]\.#;
366     $r .= $fmt{form_input}->replace ($definition, $param);
367     $r .= "</form>\n" if $param->{output}->{form};
368     $r;
369     } else { ## No input-interface WikiForm
370     qq(<a id="wikiform-$FormIndex{$option->{page}}" name="wikiform-$FormIndex{$option->{page}}"><!-- #form --></a>);
371     }
372     #} else {
373     # qq(<ins class="wiki-error">@{[&Resource('Error:WikiForm:EmbedIsNotSupported',escape=>1)]}</ins>);
374     #}
375     }}
376    
377     sub init_form {
378     ## TODO: Support multipart/form-data
379     my $query = '';
380     if (uc $main::ENV{REQUEST_METHOD} eq 'POST') {
381 wakaba 1.5 if (lc ($main::ENV{CONTENT_TYPE}) eq 'application/x-www-form-urlencoded'
382     || lc ($main::ENV{CONTENT_TYPE}) eq 'application/sgml-form-urlencoded') {
383     read STDIN, $query, $main::ENV{CONTENT_LENGTH};
384     } else {
385     $form{mycmd} = '___unsupported_media_type___';
386     $form{mypage} = $PageName{FrontPage};
387     return;
388     }
389 wakaba 1.1 }
390     $query .= ($query ? ';' : '') . $main::ENV{QUERY_STRING};
391     if ($main::ENV{REQUEST_METHOD} ne 'POST' && $main::ENV{QUERY_STRING} && $main::ENV{QUERY_STRING} !~ /[&;=]/) {
392     my $query = &decode($main::ENV{QUERY_STRING});
393     $query = &code_convert(\$query, $kanjicode);
394     $form{mypage} = $query;
395     $form{mycmd} = 'default';
396     } else {
397     for (split /[;&]/, $query) {
398     if (my ($n, $v) = split /=/, $_, 2) {
399     for ($n, $v) {tr/+/ /; s/%([0-9A-Fa-f][0-9A-Fa-f])/pack 'C', hex $1/ge};
400     $form{$n} = $v;
401     }
402     }
403     unless (defined $form{mypage}) {
404     $form{mypage} = $form{epage};
405     $form{mypage} =~ s/([0-9A-F]{2})/ord hex $1/g;
406     }
407     $form{mypage} = &code_convert (\$form{mypage}, $kanjicode);
408     }
409     $form{mypage} ||= $PageName{FrontPage};
410     $form{mypage} =~ tr/\x00-\x1F\x7F//d;
411 wakaba 1.3 $form{mypage} = SuikaWiki::Name::Space::normalize_name ($form{mypage});
412 wakaba 1.1 $form{mycmd} ||= 'default';
413 wakaba 1.4 $form{mycmd} =~ tr/-/_/;
414 wakaba 1.1
415     # mypreview_edit -> do_edit, with preview.
416     # mypreview_adminedit -> do_adminedit, with preview.
417     # mypreview_write -> do_write, without preview.
418     foreach (keys %form) {
419     if (/^mypreview_(.*)$/) {
420     $form{mycmd} = $1;
421     $form{mypreview} = 1;
422     }
423     }
424    
425     #
426     # $form{mycmd} is frozen here.
427     #
428    
429 wakaba 1.3 for (grep /^(?:wikiform__|pi_)/, keys %form) {
430 wakaba 1.1 $form{$_} = &code_convert (\$form{$_}, $kanjicode);
431     }
432     $form{mymsg} = &code_convert(\$form{mymsg}, $kanjicode);
433     $form{myname} = &code_convert(\$form{myname}, $kanjicode);
434     }
435    
436     sub get_subjectline {
437     my ($page, %option) = @_;
438     my $SubjectLine = SuikaWiki::Plugin->cache ('headline');
439     unless (defined $SubjectLine->{$page}) {
440     if (not &is_editable($page)) {
441     $SubjectLine->{$page} = "";
442     } else {
443     $SubjectLine->{$page} = do {
444     my $s=$database{$page};
445 wakaba 1.7 $s =~ tr/\x0D//d;
446 wakaba 1.1 $s =~ s!^\#\?[^\x0A\x0D]+[\x0A\x0D]*!!s;
447 wakaba 1.7 $s =~ s/\x0A\x0A.*//s;
448     if (length ($s) > 500) {
449     $s = substr ($s, 0, 500);
450     $s =~ s/[^\x09\x0A\x20-\x7E]+$//s;
451     $s .= '...';
452     }
453     #if ($s =~ /^([^\x0A]*(?:\x0A+[^\x0A]+){0,9})/s) {
454     # $s = $1;
455     #}
456     $s =~ s/[\x09\x0A\x20]+/\x20/gs;
457     $s =~ s/^[-=*>,:]+//;
458 wakaba 1.1 $s =~ s/'''?//g;
459 wakaba 1.7 $s =~ s/\[\[([^]]+)\]\]/$1/g;
460     $s =~ s/__&&([^&]+)&&__/$1/g;
461 wakaba 1.1 $s =~ s/\[[A-Z]+(?:\([^)]+\))?\[([^]]+)\](?:\s\[([^]]+)\])?\]/$1$2/g;
462 wakaba 1.7 $s =~ s/\([^\(\)]+\)//g;
463     $s =~ s/\[[^\[\]]+\]//g;
464     $s =~ s/>>[0-9]+//g;
465     $s =~ s/<[^<>]+>//g;
466     $s =~ s/20[0-9][0-9]-[01][0-9]-[0-3][0-9]\x20*[0-2][0-9]:[0-5][0-9]//g;
467     $s =~ s/ [Aa]n? / /g;
468     $s =~ s/ [Aa]t / @ /g;
469     $s =~ s/ [Tt]he / /g;
470     $s =~ s/,? and / & /g;
471     $s =~ s!,? or ! / !g;
472     $s =~ s! not !n't !g;
473     $s =~ s! (?:i|ha)s !'s !g;
474     $s =~ s! are !'re !g;
475     $s =~ s! [Ww]ith(in|out)? !' w/'.substr ($1,0,1).' '!ge;
476     $s =~ s!¤Ç¤¹¡£!¡£!g;
477     $s =~ s!¤Þ¤·¤¿¡£!¤¿¡£!g;
478     $s =~ s!¤¢¤ê¤Þ¤»¤ó!¤Ê¤¤!g;
479     $s =~ s!¤·¤Þ¤¹!¤¹¤ë!g;
480     $s =~ s!(?:¤ê)?¤Þ¤¹!¤ë!g;
481     $s =~ s!¤¤¤è¤¤¤è|¤È¤¦¤È¤¦|¤è¤¦¤ä¤¯!!g;
482     $s =~ s!¤È¤ê¤¢¤¨¤º!!g;
483     $s =~ s!(?:¡¢|¡£|[.,])\x20?$!!g;
484     $s =~ s/\x20+/\x20/g;
485     if (length ($s) > 400) {
486     $s = substr ($s, 0, 400);
487     $s =~ s/[^\x20-\x7E]+$//s;
488     $s .= '...';
489     }
490 wakaba 1.1 $s};
491     }
492     }
493     if (length $SubjectLine->{$page}) {
494     $option{delimiter} = defined $option{delimiter} ? $option{delimiter} : &Resource('Title-Summary Delimiter');
495     $option{delimiter}.$SubjectLine->{$page}.$option{tail};
496     } else {
497     '';
498     }
499     }
500    
501     sub open_db {
502     if ($modifier_dbtype eq 'dbmopen') {
503     dbmopen(%database, $PathTo{WikiDataBase}, 0666) or die "(dbmopen) $PathTo{WikiDataBase}";
504     } elsif ($modifier_dbtype eq 'AnyDBM_File') {
505     eval q{use AnyDBM_File};
506     tie(%database, "AnyDBM_File", $PathTo{WikiDataBase}, O_RDWR|O_CREAT, 0666) or die ("(tie AnyDBM_File) $PathTo{WikiDataBase}");
507     } elsif ($modifier_dbtype eq 'Yuki::YukiWikiDB') {
508     eval q{use Yuki::YukiWikiDB};
509     tie(%database, "Yuki::YukiWikiDB", $PathTo{WikiDataBase}) or die ("(tie Yuki::YukiWikiDB) $PathTo{WikiDataBase}");
510     } else { ## Yuki::YukiWikiDB || Yuki::YukiWikiDBMeta
511     eval qq{use $modifier_dbtype};
512     $database = tie(%database, $modifier_dbtype => $PathTo{WikiDataBase}, -lock => 2, -backup => $wiki::diff::UseDiff) or die ("(tie $modifier_dbtype) $PathTo{WikiDataBase}");
513     }
514     }
515    
516     sub close_db {
517     if ($modifier_dbtype eq 'dbmopen') {
518     dbmclose(%database);
519     } else {
520     untie(%database);
521     }
522     }
523    
524     sub editform (@) {
525     my %option = @_;
526     my $frozen = &is_frozen ($option{page});
527     $option{content} = $database{$option{page}} unless defined $option{content};
528     $option{content} = $database{NewPageTemplate} unless length $option{content};
529     $option{last_modified} = $database->mtime ($option{page}) unless defined $option{last_modified};
530     my $f = '';
531     my $magic = '';
532     $magic = $1 if $option{content} =~ m/^([^\x0A\x0D]+)/s;
533    
534     my $selected = 'default';
535     if ($form{after_edit_cmd}) {
536     $selected = $form{after_edit_cmd};
537     } elsif ($magic =~ /Const|Config|CSS/) {
538     $selected = 'edit';
539     }
540     my $afteredit = <<EOH;
541     <select name="after_edit_cmd">
542     <option value="default" label="@{[&Resource('Edit:SaveAndDefault',escape=>1)]}"@{[$selected eq 'default' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndDefault',escape=>1)]}</option>
543     <option value="read" label="@{[&Resource('Edit:SaveAndView',escape=>1)]}"@{[$selected eq 'read' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndView',escape=>1)]}</option>
544     <option value="edit" label="@{[&Resource('Edit:SaveAndEdit',escape=>1)]}"@{[$selected eq 'edit' ? ' selected="selected"':'']}>@{[&Resource('Edit:SaveAndEdit',escape=>1)]}</option>
545     </select>
546     EOH
547     $f .= <<"EOD";
548     <form action="$uri{wiki}" method="post">
549     @{[ $option{conflict} ? '' : qq(<label><input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}" /><kbd>S</kbd></label>) ]}
550     @{[ $option{admin} ? qq(<label>@{[&Resource('Edit:Password=',escape=>1)]}<input type="password" name="mypassword" value="" size="10" /></label>) : "" ]} [@{[&get_new_anchor_index($option{content})]}]<br />
551     <input type="hidden" name="myLastModified" value="$option{last_modified}" />
552     <input type="hidden" name="mypage" value="@{[&escape($form{mypage})]}" />
553     <textarea cols="@{[&Resource('Edit:Form:Cols')+0||80]}" rows="@{[&Resource('Edit:Form:Rows')+0||20]}" name="mymsg" tabindex="1">@{[&escape($option{content})]}</textarea><br />
554     @{[
555     $option{admin} ?
556     qq(
557     <label><input type="radio" name="myfrozen" value="1" @{[$frozen ? qq(checked="checked") : ""]} />@{[&Resource('Edit:Freeze',escape=>1)]}</label>
558     <label><input type="radio" name="myfrozen" value="0" @{[$frozen ? "" : qq(checked="checked")]} />@{[&Resource('Edit:DontFreeze',escape=>1)]}</label><br />)
559     : ""
560     ]}
561     @{[
562     $option{conflict} ? "" :
563     qq(
564     <label><input type="checkbox" name="mytouch" value="on" checked="checked" />@{[&Resource('Edit:UpdateTimeStamp',escape=>1)]}</label><br />
565     <label><input type="submit" name="mypreview_write" value="@{[&Resource('Edit:Save',escape=>1)]}" accesskey="S" /><kbd>S</kbd></label>
566     $afteredit
567     )
568     ]}
569     </form>
570     EOD
571     $f;
572     }
573    
574     sub is_editable {
575     my ($page) = @_;
576 wakaba 1.4 return 0 unless SuikaWiki::Name::Space::validate_name ($page);
577     return 0 if $page =~ /[\x00-\x20\[\]\x7F]/;
578 wakaba 1.2 1;
579 wakaba 1.1 }
580    
581     sub decode {
582     my ($s) = @_;
583     $s =~ tr/+/ /;
584     $s =~ s/%([A-Fa-f0-9][A-Fa-f0-9])/pack("C", hex($1))/eg;
585     return $s;
586     }
587    
588     sub encode {
589     my $s = shift;
590     $s =~ s/([^0-9A-Za-z_-])/sprintf '%%%02X', ord $1/ge;
591     $s;
592     }
593    
594     sub get_now {
595     my ($sec, $min, $hour, $day, $mon, $year) = localtime(time);
596     $year += 1900;
597     $mon++;
598     $mon = "0$mon" if $mon < 10;
599     $day = "0$day" if $day < 10;
600     $hour = "0$hour" if $hour < 10;
601     $min = "0$min" if $min < 10;
602     #$sec = "0$sec" if $sec < 10;
603     return "$year-$mon-$day $hour:$min";
604     }
605    
606     sub frozen_reject {
607     my ($isfrozen) = $database->meta (IsFrozen => $form{mypage});
608     my ($willbefrozen) = $form{myfrozen};
609     if (not $isfrozen and not $willbefrozen) {
610     # You need no check.
611     return 0;
612     } elsif (valid_password($form{mypassword})) {
613     # You are admin.
614     return 0;
615     } else {
616     &_do_view_msg (-view => '-error', -page => $form{mypage},
617     error_message => &Resource ('Error:PasswordIsIncorrect'));
618     exit;
619     }
620     }
621    
622     sub is_frozen ($) { $database->meta (IsFrozen => $_[0]) ? 1 : 0 }
623    
624     sub do_comment {
625     my ($content) = $database{$form{mypage}};
626     my $default_name; ## this code is not strict.
627     $default_name = $1 if $content =~ /default-name="([^"]+)"/;
628     my $datestr = '[WEAK['.&get_now.']]';
629     my $namestr = $form{myname} || $default_name || &Resource('WikiForm:WikiComment:DefaultName');
630     ($namestr = '', $datestr = '') if $form{myname} eq 'nodate';
631     if ($namestr =~ /^(?:>>)?[0-9]/) {
632     $namestr = qq( ''$namestr'': );
633     } elsif (length $namestr) {
634     $namestr = qq( ''[[$namestr]]'': );
635     }
636     my $anchor = &get_new_anchor_index ($content);
637     my $i = 1; my $o = 0;
638     $content =~ s{(\[\[\#r?comment\]\])}{
639     my $embed = $1;
640     if ($i == $form{comment_index}) {
641     if ($embed ne '[[#rcomment]]') {
642     $embed = "- [$anchor] $datestr$namestr$form{mymsg}\n$embed"; $o = 1;
643     } else {
644     $embed .= "\n- [$anchor] $datestr$namestr$form{mymsg}"; $o = 1;
645     }
646     }
647     $i++; $embed;
648     }ge;
649     unless ($o) {
650     $content = "#?SuikaWiki/0.9\n\n" unless $content;
651     $content .= "\n" unless $content =~ /\n$/s;
652     $content .= "- [$anchor] $datestr$namestr$form{mymsg}\n";
653     }
654     $form{__comment_anchor_index} = $anchor;
655     if ($form{mymsg} || $form{myname}) {
656     $form{mymsg} = $content;
657     $form{mytouch} = 'on';
658     &do_write;
659     } else { ## Don't write
660     $form{mycmd} = 'default';
661     &do_view;
662     }
663     }
664    
665     sub get_new_anchor_index ($) {
666     my $content = shift;
667     my $anchor = 0;
668     $content =~ s/^(?:[-=]+\s*)?\[([0-9]+)\]/$anchor = $1 if $1 > $anchor; $&/mge;
669     $anchor + 1;
670     }
671    
672     sub load_formatter (@) {
673     for my $t (@_) {
674     unless ($fmt{$t}) {
675     require Message::Util::Formatter;
676     $fmt{$t} = Message::Util::Formatter->new;
677     for (@{$SuikaWiki::Plugin::List{'wiki'.$t}||[]}) {
678     $_->load_formatter ($fmt{$t}, type => 'wiki'.$t);
679     }
680 wakaba 1.5 $fmt{$t}->option (return_class => 'SuikaWiki::Markup::XML') if $HAS_XML;
681 wakaba 1.1 }
682     }
683     }
684    
685     sub do_wikiform {
686     my $content = $database{$form{mypage}};
687     my $anchor = &get_new_anchor_index ($content);
688     &load_formatter (qw/form_template form_option/);
689     my $write = 0;
690     my $i = 1;
691     $content =~ s{$embed_command{form}}{
692     my ($embed, $wfname, $template, $option) = ($&, $1, $3, $4);
693     if (($wfname && $wfname eq $form{wikiform_targetform})
694     || $i == $form{wikiform_index}) {
695     $template =~ s/\\([\\'])/$1/g;
696     $option =~ s/\\([\\'])/$1/g;
697     my $param = bless {depth=>10}, 'SuikaWiki::Plugin';
698     $param->{page} = $form{mypage};
699     $param->{form_index} = $i;
700     $param->{form_name} = $wfname;
701     $param->{anchor_index} = $anchor;
702     $param->{argv} = \%form;
703     $param->{default_name} = $1 if $content =~ /default-name="([^"]+)"/;
704     $param->{default_name} ||= &Resource('WikiForm:WikiComment:DefaultName');
705     $fmt{form_option}->replace ($option, $param);
706     my $t = 1;
707     for (keys %{$param->{require}||{}}) {
708     (undef $t, last) unless length $param->{argv}->{'wikiform__'.$_};
709     }
710     $t = $fmt{form_template}->replace ($template, $param) if $t;
711     if (length $t) {
712     if ($param->{output}->{reverse}) {
713     $embed .= "\n" . $t;
714     } else {
715     $embed = $t . "\n" . $embed;
716     }
717     $write = 1;
718     $form{__comment_anchor_index} = $anchor
719     if $param->{anchor_index_}; ## $anchor is used!
720     }
721     $form{__wikiform_anchor_index} = $i;
722     undef $form{wikiform_targetform}; ## Make sure never to match
723     undef $form{wikiform_index}; ## with WikiForm in rest of page!
724     }
725     $i++; $embed;
726     }ge;
727     unless ($write) {
728     #$content = "#?SuikaWiki/0.9\n\n" unless $content;
729     #$content .= "\n" unless $content =~ /\n$/s;
730     #
731     }
732     if ($write) {
733     $form{mymsg} = $content;
734     $form{mytouch} = 'on';
735     &do_write;
736     } else { ## Don't write!
737     $form{mycmd} = 'default';
738     &do_view;
739     }
740     }
741    
742     sub code_convert {
743     require Jcode;
744     my ($contentref, $code) = (shift, shift || $kanjicode);
745     if ($code =~ /euc/) { $code = 'euc' }
746     elsif ($code =~ /iso/) { $code = 'jis' }
747     elsif ($code =~ /shi/) { $code = 'sjis' }
748     elsif ($code =~ /utf/) { $code = 'utf8' }
749     $$contentref = Jcode->new ($contentref)->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\xA2\xAF\xA2\xB0\xA2\xB2\xA2\xB1\xA1\xE4\xA1\xE3\xA1\xC0\xA1\xA1" => q(0-9A-Za-z&,.:;?!`^_/|()[]{}+$%#*@='"~-><\ ))->$code;
750     return $$contentref;
751     }
752    
753     sub _rfc3339_date ($) {
754     my @time = gmtime (shift);
755     sprintf '%04d-%02d-%02dT%02d:%02d:%02d+00:00', $time[5]+1900,$time[4]+1,@time[3,2,1,0];
756     }
757    
758    
759     package wiki::dummy;
760     sub mtime (@) {undef}
761     sub meta (@) {undef}
762     sub Yuki::YukiWikiDB2::meta (@) {undef}
763    
764     package main;
765     &SuikaWiki::Plugin::import_plugins ();
766     &main ();
767    
768     =head1 NAME
769    
770 wakaba 1.4 lib/suikawiki.pl --- SuikaWiki transitional library
771 wakaba 1.1
772     =head1 AUTHOR
773    
774 wakaba 1.4 Hiroshi Yuki <hyuki@hyuki.com> <http://www.hyuki.com/yukiwiki/> (YukiWiki)
775 wakaba 1.1
776 wakaba 1.4 Makio Tsukamoto <http://digit.que.ne.jp/> (WalWiki)
777 wakaba 1.1
778     Wakaba <w@suika.fam.cx>
779    
780     =head1 LICENSE
781    
782 wakaba 1.4 Copyright AUTHORS 2000-2003
783 wakaba 1.1
784     This program is free software; you can redistribute it and/or
785     modify it under the same terms as Perl itself.
786    
787     =cut
788    
789 wakaba 1.7 1; # $Date: 2003/05/10 05:58:49 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24