]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/_lib.pm
3 # ====================================================
4 # Autor: n.d.p. / 2001-01-07
5 # lm : n.d.p. / 2001-02-25
6 # ====================================================
8 # * Schnittstellen fuer den Zugriff auf Messages
10 # ====================================================
14 package Posting
::_lib
;
16 use vars
qw(@EXPORT_OK);
17 use base qw(Exporter);
19 use Encode
::Plain
; $Encode::Plain
::utf8
= 1;
23 # ====================================================
25 # ====================================================
27 @EXPORT_OK = qw(get_message_header get_message_body get_message_node parse_single_thread
28 hr_time short_hr_time long_hr_time
30 create_forum_xml_string
33 # ====================================================
35 # ====================================================
37 ###########################
38 # sub get_message_header
40 # Messageheader auslesen
41 ###########################
43 sub get_message_header
($) {
47 my $header = $node -> getElementsByTagName
('Header', 0) -> item
(0);
48 my $author = $header -> getElementsByTagName
('Author', 0) -> item
(0);
49 my $name = $author -> getElementsByTagName
('Name', 0) -> item
(0);
50 my $email = $author -> getElementsByTagName
('Email', 0) -> item
(0);
51 my $home = $author -> getElementsByTagName
('HomepageUrl', 0) -> item
(0);
52 my $image = $author -> getElementsByTagName
('ImageUrl', 0) -> item
(0);
53 my $cat = $header -> getElementsByTagName
('Category', 0) -> item
(0);
54 my $subject = $header -> getElementsByTagName
('Subject', 0) -> item
(0);
55 my $date = $header -> getElementsByTagName
('Date', 0) -> item
(0);
57 %conf = (name
=> ($name -> hasChildNodes
)?
$name -> getFirstChild
-> getData
:undef,
58 category
=> ($cat -> hasChildNodes
)?
$cat -> getFirstChild
-> getData
:undef,
59 subject
=> ($subject -> hasChildNodes
)?
$subject -> getFirstChild
-> getData
:undef,
60 email
=> (defined ($email) and $email -> hasChildNodes
)?
$email -> getFirstChild
-> getData
:undef,
61 home
=> (defined ($home) and $home -> hasChildNodes
)?
$home -> getFirstChild
-> getData
:undef,
62 image
=> (defined ($image) and $image -> hasChildNodes
)?
$image -> getFirstChild
-> getData
:undef,
63 time => $date -> getAttribute
('longSec'));
67 ###########################
68 # sub get_message_header
70 # Messagebody auslesen
71 ###########################
73 sub get_message_body
($$)
78 foreach ($xml -> getElementsByTagName
('ContentList', 1) -> item
(0) -> getElementsByTagName
('MessageContent', 0))
80 if ($_ -> getAttribute
('mid') eq $mid)
82 $body = ($_ -> hasChildNodes
)?
$_ -> getFirstChild
-> getData
:'';
90 ###########################
91 # sub get_message_header
93 # Messagenode bestimmen
94 ###########################
96 sub get_message_node
($$$) {
97 my ($xml,$tid,$mid) = @_;
100 for ( $xml -> getElementsByTagName
('Thread')) {
101 if ($_ -> getAttribute
('id') eq $tid) {
103 for ($tnode -> getElementsByTagName
('Message')) {
104 if ($_ -> getAttribute
('id') eq $mid) {
109 wantarray?
($mnode, $tnode):$mnode;
112 ###########################
113 # sub parse_single_thread
115 # einzelne Threaddatei
117 ###########################
119 sub parse_single_thread
($$;$) {
120 my ($tnode, $deleted, $sorted) = @_;
121 my ($header, @msg, %mno);
123 for ($tnode -> getElementsByTagName
('Message')) {
124 $header = get_message_header
($_);
126 push @msg,{mid
=> ($_ -> getAttribute
('id') =~ /(\d+)/)[0],
127 ip
=> $_ -> getAttribute
('ip'),
128 kids
=> [$_ -> getElementsByTagName
('Message', 0)],
129 answers
=> $_ -> getElementsByTagName
('Message') -> getLength
,
130 deleted
=> ($_ -> getAttribute
('flag') eq 'deleted')?
1:0,
131 name
=> plain
($header -> {name
}),
132 cat
=> plain
($header -> {category
} or ''),
133 subject
=> plain
($header -> {subject
}),
134 time => plain
($header -> {time})};
137 # Eintraege ergaenzen und korrigieren
139 $msg[0] -> {level
} = 0;
141 $level = $_ -> {level
} + 1;
142 @
{$_ -> {kids
}} = map {$msg[$mno{$_}] -> {level
} = $level; $mno{$_}} @
{$_ -> {kids
}};}
145 # Sortieren und bei Bedarf
146 # geloeschte Messages entfernen
148 my $smsg = sort_thread
(\
@msg, $sorted);
149 delete_messages
($smsg) unless ($deleted);
154 ###########################
155 # sub create_message_xml
159 ###########################
161 sub create_message_xml
($$$) {
162 my ($xml, $msges, $num) = @_;
164 my $msg = $msges -> [$num];
166 my $message = $xml -> createElement
('Message');
167 $message -> setAttribute
('id', 'm'.$msg -> {mid
});
168 $message -> setAttribute
('flag', 'deleted') if ($msg -> {deleted
});
171 my $header = $xml -> createElement
('Header');
173 # alles inside of 'Header'
174 my $author = $xml -> createElement
('Author');
176 my $name = $xml -> createElement
('Name');
177 $name -> addText
(toUTF8
($msg -> {name
}));
179 my $email = $xml -> createElement
('Email');
181 my $category = $xml -> createElement
('Category');
182 $category -> addText
(toUTF8
($msg -> {cat
}));
184 my $subject = $xml -> createElement
('Subject');
185 $subject -> addText
(toUTF8
($msg -> {subject
}));
187 my $date = $xml -> createElement
('Date');
188 $date -> setAttribute
('longSec', $msg -> {time});
190 $author -> appendChild
($name);
191 $author -> appendChild
($email);
192 $header -> appendChild
($author);
193 $header -> appendChild
($category);
194 $header -> appendChild
($subject);
195 $header -> appendChild
($date);
196 $message -> appendChild
($header);
198 if ($msg -> {kids
}) {
199 for (@
{$msg -> {kids
}}) {
200 $message -> appendChild
(&create_message_xml
($xml, $msges, $_));
207 # ====================================================
208 # XML-Parsen von Hand
209 # ====================================================
211 ###########################
216 ###########################
218 sub sort_thread
($$) {
219 my ($msg, $sorted) = @_;
221 my ($z, %mhash) = (0);
223 if ($sorted) { # aelteste zuerst
225 @
$msg[@
{$_ -> {kids
}}] = sort {$a -> {mid
} <=> $b -> {mid
}} @
$msg[@
{$_ -> {kids
}}] if (@
{$_ -> {kids
}} > 1);
226 $mhash{$_ -> {mid
}} = [@
$msg[@
{$_ -> {kids
}}]];}}
228 else { # juengste zuerst
230 @
$msg[@
{$_ -> {kids
}}] = sort {$b -> {mid
} <=> $a -> {mid
}} @
$msg[@
{$_ -> {kids
}}] if (@
{$_ -> {kids
}} > 1);
231 $mhash{$_ -> {mid
}} = [@
$msg[@
{$_ -> {kids
}}]];}}
233 # Kinder wieder richtig einsortieren
234 my @smsg = ($msg -> [0]);
237 splice @smsg,$z,0,@
{$mhash{$_ -> {mid
}}} if ($_ -> {answers
});
238 delete $_ -> {kids
};}
243 ###########################
244 # sub delete_messages
246 # geoeschte Nachrichten
248 ###########################
250 sub delete_messages
($) {
253 my ($z, $oldlevel, @path) = (0,0,0);
256 if ($_ -> {deleted
}) {
257 my $n = $_ -> {answers
}+1;
258 for (@path) {$smsg -> [$_] -> {answers
} -= $n;}
259 splice @
$smsg,$z,$n;}
262 if ($_ -> {level
} > $oldlevel) {
264 $oldlevel = $_ -> {level
};}
266 elsif ($_ -> {level
} < $oldlevel) {
267 splice @path,$_ -> {level
}-$oldlevel;
268 $oldlevel = $_ -> {level
};}
270 else { $path[-1] = $z; }
277 ###########################
278 # sub get_all_threads
280 # Hauptdatei laden und
282 ###########################
284 sub get_all_threads
($$;$) {
285 my ($file, $deleted, $sorted) = @_;
286 my ($last_thread, $last_message, @unids, %threads);
289 open FILE
, $file or return undef;
290 my $xml = join '', <FILE
>;
291 close(FILE
) or return undef;
294 ($last_thread) = map {/(\d+)/} $xml =~ /<Forum.+?lastThread="([^"]+)"[^>]*>/;
295 ($last_message) = map {/(\d+)/} $xml =~ /<Forum.+?lastMessage="([^"]+)"[^>]*>/;}
297 my $reg_msg = qr
~(?
:</Message
>
298 |<Message\s
+id
="m(\d+)"\s
+unid
="([^"]*)"(?:\s+flag="([^"]*)")?
[^>]*>\s
*
299 <Header
>[^<]*(?
:<(?
!Name
>)[^<]*)*
300 <Name
>([^<]+)</Name
>[^<]*(?
:<(?
!Category
>)[^<]*)*
301 <Category
>([^<]*)</Category
>\s
*
302 <Subject
>([^<]+)</Subject
>\s
*
303 <Date\s
+longSec
="(\d+)"[^>]*>\s
*
304 </Header>\s*(?:(<)/Message
>|(?
=(<)Message\s
*)))~sx
;
306 while ($xml =~ /<Thread id="t(\d+)">([^<]*(?:<(?!\/Thread
>)[^<]*)*)<\
/Thread>/g) {
308 my ($tid, $thread) = ($1, $2);
309 my ($level, $cmno, @msg, @stack) = (0);
311 while ($thread =~ m
;$reg_msg;g
) {
314 push @stack,$cmno if (defined $cmno);
318 push @
{$msg[$cmno] -> {kids
}} => $#msg;
319 push @
{$msg[$cmno] -> {unids
}} => $2;}
323 for (@stack) {$msg[$_] -> {answers
}++}
331 $msg[-1] -> {subject
},
332 $msg[-1] -> {time}) = ($1, $2, $4, $5, $6, $7);
334 $msg[-1] -> {deleted
} = ($3 eq 'deleted')?
1:undef;
336 $msg[-1] -> {name
} =~ s/&/&/g;
337 $msg[-1] -> {cat
} =~ s/&/&/g;
338 $msg[-1] -> {subject
} =~ s/&/&/g;
340 $msg[-1] -> {unids
} = [];
341 $msg[-1] -> {kids
} = [];
342 $msg[-1] -> {answers
} = 0;
343 $msg[-1] -> {level
} = $level++;}
345 elsif (defined ($8)) {
349 push @
{$msg[$cmno] -> {kids
}} => $#msg;
350 push @
{$msg[$cmno] -> {unids
}} => $2;
351 $msg[$cmno] -> {answers
}++;}
355 for (@stack) {$msg[$_] -> {answers
}++}
361 $msg[-1] -> {subject
},
362 $msg[-1] -> {time}) = ($1, $2, $4, $5, $6, $7);
364 $msg[-1] -> {deleted
} = ($3 eq 'deleted')?
1:undef;
366 $msg[-1] -> {name
} =~ s/&/&/g;
367 $msg[-1] -> {cat
} =~ s/&/&/g;
368 $msg[-1] -> {subject
} =~ s/&/&/g;
370 $msg[-1] -> {level
} = $level;
371 $msg[-1] -> {unids
} = [];
372 $msg[-1] -> {kids
} = [];
373 $msg[-1] -> {answers
} = 0;}
376 $cmno = pop @stack; $level--;}}
379 # Sortieren und bei Bedarf
380 # geloeschte Messages entfernen
382 my $smsg = sort_thread
(\
@msg, $sorted);
383 delete_messages
($smsg) unless ($deleted);
385 $threads{$tid} = $smsg if (@
$smsg);
388 wantarray?
(\
%threads, $last_thread, $last_message, \
@unids): \
%threads;
391 ###########################
392 # sub create_forum_xml_string
394 # Forumshauptdatei erzeugen
395 ###########################
397 sub create_forum_xml_string
($$) {
398 my ($threads, $param) = @_;
399 my ($level, $thread, $msg);
401 my $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n"
402 .'<!DOCTYPE Forum SYSTEM "'.$param -> {dtd
}.'">'."\n"
403 .'<Forum lastMessage="'.$param -> {lastMessage
}.'" lastThread="'.$param -> {lastThread
}.'">';
405 foreach $thread (sort {$b <=> $a} keys %$threads) {
406 $xml .= '<Thread id="t'.$thread.'">';
409 foreach $msg (@
{$threads -> {$thread}}) {
410 $xml .= '</Message>' x
($level - $msg -> {level
} + 1) if ($msg -> {level
} <= $level);
411 $level = $msg -> {level
};
412 $xml .= '<Message id="m'.$msg -> {mid
}.'"'
413 .' unid="'.$msg -> {unid
}.'"'
414 .(($msg -> {deleted
})?
' flag="deleted"':'')
419 .plain
($msg -> {name
})
424 .((length $msg -> {cat
})?plain
($msg -> {cat
}):'')
427 .plain
($msg -> {subject
})
434 $xml .= '</Message>' x
($level + 1);
435 $xml .= '</Thread>';}
442 ###########################
446 ###########################
449 my ($filename,$content) = @_;
452 open FILE
,">$filename.temp" or return;
454 unless (print FILE
$$content) {
458 close FILE
or return;
460 rename "$filename.temp", $filename or return;
465 # ====================================================
467 # ====================================================
469 ###########################
471 # 02. Januar 2001, 12:02 Uhr
474 # 02. 01. 2001, 12:02 Uhr
477 # Dienstag, 02. Januar 2001, 12:02:01 Uhr
479 # formatierte Zeitangabe
480 ###########################
483 my @month = qw(Januar Februar M\303\244rz April Mail Juni Juli August September Oktober November Dezember);
486 my (undef, $min, $hour, $day, $mon, $year) = localtime ($_[0]);
488 sprintf ('%02d. %s %04d, %02d:%02d Uhr', $day, $month[$mon], $year+1900, $hour, $min);
491 sub short_hr_time ($) {
492 my (undef, $min, $hour, $day, $mon, $year) = localtime ($_[0]);
494 sprintf ('%02d. %02d. %04d, %02d:%02d Uhr', $day, $mon+1, $year+1900, $hour, $min);
497 sub long_hr_time ($) {
498 my @month = qw(Januar Februar M\303\244rz April Mail Juni Juli August September Oktober November Dezember);
501 my @wday = qw(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag);
502 my ($sek, $min, $hour, $day, $mon, $year, $wday) = localtime ($_[0]);
504 sprintf ('%s, %02d. %s %04d, %02d:%02d:%02d Uhr', $wday[$wday], $day, $month[$mon], $year+1900, $hour, $min, $sek);
507 # ====================================================
508 # Modulinitialisierung
509 # ====================================================
511 # making require happy
514 # ====================================================
515 # end of Posting::_lib
516 # ====================================================
patrick-canterino.de