]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/_lib.pm
30a8254b96233c2715957e0aab00e9185d9e6675
3 ################################################################################
5 # File: shared/Posting/_lib.pm #
7 # Authors: André Malo <nd@o3media.de>, 2001-03-03 #
8 # Frank Schoenmann <fs@tower.de>, 2001-06-02 #
10 # Description: Message access interface, time format routines #
12 ################################################################################
16 use Encode
::Plain
; $Encode::Plain
::utf8
= 1;
20 # ====================================================
22 # ====================================================
24 use constant SORT_ASCENT
=> 0; # (young postings first)
25 use constant SORT_DESCENT
=> 1;
26 use constant KEEP_DELETED
=> 1;
27 use constant KILL_DELETED
=> 0;
29 use base
qw(Exporter);
30 @Posting::_lib
::EXPORT_OK
= qw(
46 create_forum_xml_string
56 # ====================================================
58 # ====================================================
60 ### sub create_message ($$) ####################################################
62 # create the 'Message' subtree
64 # Params: $xml - XML::DOM::Document object
65 # $par - hash reference
66 # (msg, ip, name, email, home, image, category, subject, time)
68 # Return: XML::DOM::Element object
70 sub create_message
($$) {
73 my $message = $xml -> createElement
('Message');
74 $message -> setAttribute
('id' => $par -> {msg
});
75 $message -> setAttribute
('ip' => $par -> {ip
});
77 my $header = $xml -> createElement
('Header');
78 my $author = $xml -> createElement
('Author');
79 $header -> appendChild
($author);
82 ['name' => 'Name' => $author],
83 ['email' => 'Email' => $author],
84 ['home' => 'HomepageUrl' => $author],
85 ['image' => 'ImageUrl' => $author],
86 ['category' => 'Category' => $header],
87 ['subject' => 'Subject' => $header]
88 );# key => element name => superior
93 my $obj = $xml -> createElement
($_->[1]);
97 defined $par -> {$_->[0]}
102 # link to superior element
103 $_ -> [2] -> appendChild
($obj);
106 my $date = $xml -> createElement
('Date');
107 $date -> setAttribute
('longSec'=> $par -> {time});
109 $header -> appendChild
($date);
110 $message -> appendChild
($header);
117 ### sub create_new_thread ($) ##################################################
119 # create a XML::DOM::Document object of a thread containing one posting
121 # Params: hash reference
122 # (dtd, thread, msg, body, ip, name, email, home,
123 # image, category, subject, time)
125 # Return: XML::DOM::Document object
127 sub create_new_thread
($) {
132 my $xml = new XML
::DOM
::Document
;
136 my $decl = new XML
::DOM
::XMLDecl
;
137 $decl -> setVersion
('1.0');
138 $decl -> setEncoding
('UTF-8');
139 $xml -> setXMLDecl
($decl);
143 my $dtd = $xml -> createDocumentType
('Forum' => $par -> {dtd
});
144 $xml -> setDoctype
($dtd);
146 # create root element 'Forum'
147 # create element 'Thread'
148 # create 'Message' subtree
149 # create element 'ContentList'
150 # create 'MessageContent' subtree
152 my $forum = $xml -> createElement
('Forum');
153 my $thread = $xml -> createElement
('Thread');
154 $thread -> setAttribute
('id' => $par -> {thread
});
155 my $message = create_message
($xml,$par);
156 my $content = $xml -> createElement
('ContentList');
157 my $mcontent = $xml -> createElement
('MessageContent');
158 $mcontent -> setAttribute
('mid' => $par -> {msg
});
159 $mcontent -> appendChild
(
160 $xml -> createCDATASection
(${$par -> {body
}})
163 # link all the nodes to
164 # their superior elements
166 $thread -> appendChild
($message);
167 $forum -> appendChild
($thread);
168 $content -> appendChild
($mcontent);
169 $forum -> appendChild
($content);
170 $xml -> appendChild
($forum);
177 ### get_message_header () ######################################################
179 # Read message header, return as a hash
181 # Params: $node - XML message node
182 # Return: hash reference (name, category, subject, email, home, image, time)
184 sub get_message_header
($)
189 my $header = $node -> getElementsByTagName
('Header' , 0) -> item
(0);
190 my $author = $header -> getElementsByTagName
('Author' , 0) -> item
(0);
191 my $name = $author -> getElementsByTagName
('Name' , 0) -> item
(0);
192 my $email = $author -> getElementsByTagName
('Email' , 0) -> item
(0);
193 my $home = $author -> getElementsByTagName
('HomepageUrl', 0) -> item
(0);
194 my $image = $author -> getElementsByTagName
('ImageUrl' , 0) -> item
(0);
195 my $cat = $header -> getElementsByTagName
('Category' , 0) -> item
(0);
196 my $subject = $header -> getElementsByTagName
('Subject' , 0) -> item
(0);
197 my $date = $header -> getElementsByTagName
('Date' , 0) -> item
(0);
200 name
=> ($name -> hasChildNodes
)?
$name -> getFirstChild
-> getData
:undef,
201 category
=> ($cat -> hasChildNodes
)?
$cat -> getFirstChild
-> getData
:undef,
202 subject
=> ($subject -> hasChildNodes
)?
$subject -> getFirstChild
-> getData
:undef,
203 email
=> (defined ($email) and $email -> hasChildNodes
)?
$email -> getFirstChild
-> getData
:undef,
204 home
=> (defined ($home) and $home -> hasChildNodes
)?
$home -> getFirstChild
-> getData
:undef,
205 image
=> (defined ($image) and $image -> hasChildNodes
)?
$image -> getFirstChild
-> getData
:undef,
206 time => $date -> getAttribute
('longSec')
212 ### get_body_node () ########################################################
214 # Search a specific message body in a XML tree
216 # Params: $xml XML::DOM::Document Object (Document Node)
219 # Return: MessageContent XML node (or -none-)
221 sub get_body_node
($$)
223 my ($xml, $mid) = @_;
225 for ($xml -> getElementsByTagName
('ContentList', 1) -> item
(0) -> getElementsByTagName
('MessageContent', 0)) {
226 return $_ if ($_ -> getAttribute
('mid') eq $mid);
232 ### get_message_body () ########################################################
236 # Params: $xml XML::DOM::Document Object (Document Node)
239 # Return: Scalar reference
241 sub get_message_body
($$)
243 my $cnode = get_body_node
($_[0], $_[1]);
246 $body = ($cnode -> hasChildNodes
)?
$cnode -> getFirstChild
-> getData
:'' if $cnode;
251 ### get_message_node () ########################################################
253 # Search a specific message in a XML tree
255 # Params: $xml XML::DOM::Document Object (Document Node)
259 # Return: Message XML node, Thread XML node (or -none-)
261 sub get_message_node
($$$)
263 my ($xml, $tid, $mid) = @_;
266 for ($xml->getElementsByTagName ('Thread')) {
267 if ($_->getAttribute ('id') eq $tid) {
270 for ($tnode -> getElementsByTagName
('Message')) {
271 if ($_ -> getAttribute
('id') eq $mid) {
285 ### sub parse_xml_file ($) #####################################################
287 # load the specified XML-File and create the DOM tree
288 # this sub is only to avoid errors and to centralize the parse process
290 # Params: $file filename
292 # Return: XML::DOM::Document Object (Document Node) or false
294 sub parse_xml_file
($) {
298 local $SIG{__DIE__
}; # CGI::Carp works unreliable ;-(
299 new XML
::DOM
::Parser
(KeepCDATA
=> 1)->parsefile ($file);
307 ###########################
308 # sub parse_single_thread
310 # einzelne Threaddatei
312 ###########################
314 sub parse_single_thread
($$;$) {
315 my ($tnode, $deleted, $sorted) = @_;
316 my ($header, @msg, %mno);
318 for ($tnode -> getElementsByTagName
('Message')) {
319 $header = get_message_header
($_);
321 push @msg,{mid
=> ($_ -> getAttribute
('id') =~ /(\d+)/)[0],
322 ip
=> $_ -> getAttribute
('ip'),
323 kids
=> [$_ -> getElementsByTagName
('Message', 0)],
324 answers
=> $_ -> getElementsByTagName
('Message') -> getLength
,
325 deleted
=> $_ -> getAttribute
('invisible'),
326 archive
=> $_ -> getAttribute
('archive'),
327 name
=> plain
($header -> {name
}),
328 cat
=> plain
($header -> {category
} or ''),
329 subject
=> plain
($header -> {subject
}),
330 time => plain
($header -> {time})};
333 # Eintraege ergaenzen und korrigieren
335 $msg[0] -> {level
} = 0;
337 $level = $_ -> {level
} + 1;
338 @
{$_ -> {kids
}} = map {$msg[$mno{$_}] -> {level
} = $level; $mno{$_}} @
{$_ -> {kids
}};}
341 # Sortieren und bei Bedarf
342 # geloeschte Messages entfernen
344 my $smsg = sort_thread
(\
@msg, $sorted);
345 delete_messages
($smsg) unless ($deleted);
350 ###########################
351 # sub create_message_xml
355 ###########################
357 sub create_message_xml
($$$) {
358 my ($xml, $msges, $num) = @_;
360 my $msg = $msges -> [$num];
362 my $message = $xml -> createElement
('Message');
363 $message -> setAttribute
('id', 'm'.$msg -> {mid
});
364 $message -> setAttribute
('invisible', '1') if ($msg -> {deleted
});
365 $message -> setAttribute
('archive', '1') if ($msg -> {archive
});
368 my $header = $xml -> createElement
('Header');
370 # alles inside of 'Header'
371 my $author = $xml -> createElement
('Author');
373 my $name = $xml -> createElement
('Name');
374 $name -> addText
(toUTF8
($msg -> {name
}));
376 my $email = $xml -> createElement
('Email');
378 my $category = $xml -> createElement
('Category');
379 $category -> addText
(toUTF8
($msg -> {cat
}));
381 my $subject = $xml -> createElement
('Subject');
382 $subject -> addText
(toUTF8
($msg -> {subject
}));
384 my $date = $xml -> createElement
('Date');
385 $date -> setAttribute
('longSec', $msg -> {time});
387 $author -> appendChild
($name);
388 $author -> appendChild
($email);
389 $header -> appendChild
($author);
390 $header -> appendChild
($category);
391 $header -> appendChild
($subject);
392 $header -> appendChild
($date);
393 $message -> appendChild
($header);
395 if ($msg -> {kids
}) {
396 for (@
{$msg -> {kids
}}) {
397 $message -> appendChild
(&create_message_xml
($xml, $msges, $_));
404 # ====================================================
405 # XML-Parsen von Hand
406 # ====================================================
408 ###########################
413 ###########################
415 sub sort_thread
($$) {
416 my ($msg, $sorted) = @_;
418 my ($z, %mhash) = (0);
420 if ($sorted) { # aelteste zuerst
422 @
$msg[@
{$_ -> {kids
}}] = sort {$a -> {mid
} <=> $b -> {mid
}} @
$msg[@
{$_ -> {kids
}}] if (@
{$_ -> {kids
}} > 1);
423 $mhash{$_ -> {mid
}} = [@
$msg[@
{$_ -> {kids
}}]];}}
425 else { # juengste zuerst
427 @
$msg[@
{$_ -> {kids
}}] = sort {$b -> {mid
} <=> $a -> {mid
}} @
$msg[@
{$_ -> {kids
}}] if (@
{$_ -> {kids
}} > 1);
428 $mhash{$_ -> {mid
}} = [@
$msg[@
{$_ -> {kids
}}]];}}
430 # Kinder wieder richtig einsortieren
431 my @smsg = ($msg -> [0]);
434 splice @smsg,$z,0,@
{$mhash{$_ -> {mid
}}} if ($_ -> {answers
});
435 delete $_ -> {kids
};}
440 ### delete_messages () #########################################################
442 # Filter out deleted messages
444 # Params: $smsg Reference of array of references of hashs
447 sub delete_messages
($) {
449 my ($z, $oldlevel, @path) = (0,0,0);
451 while ($z <= $#{$smsg}) {
453 if ($smsg -> [$z] -> {level
} > $oldlevel) {
455 $oldlevel = $smsg -> [$z] -> {level
};
457 elsif ($smsg -> [$z] -> {level
} < $oldlevel) {
458 splice @path, $smsg -> [$z] -> {level
};
460 $oldlevel = $smsg -> [$z] -> {'level'};
466 if ($smsg -> [$z] -> {deleted
}) {
467 my $n = $smsg -> [$z] -> {answers
} + 1;
468 $smsg -> [$_] -> {answers
} -= $n for (@path);
469 splice @
$smsg, $z, $n;
479 ### get_all_threads () #########################################################
481 # Read and Parse the main file (without any XML-module, they are too slow)
483 # Params: $file /path/to/filename of the main file
484 # $deleted hold deleted (invisible) messages in result (1) oder not (0)
485 # $sorted direction of message sort: descending (0) (default) or ascending (1)
486 # Return: scalar context: hash reference
487 # list context: list (\%threads, $last_thread, $last_message, $dtd, \@unids)
489 sub get_all_threads
($$;$)
491 my ($file, $deleted, $sorted) = @_;
492 my ($last_thread, $last_message, $dtd, @unids, %threads);
495 open FILE
,"< $file" or return;
496 my $xml = join '', <FILE
>;
497 close(FILE
) or return;
501 ($dtd) = $xml =~ /<!DOCTYPE\s+\S+\s+SYSTEM\s+"([^"]+)">/;
502 ($last_thread) = map {/(\d+)/} $xml =~ /<Forum.+?lastThread="([^"]+)"[^>]*>/;
503 ($last_message) = map {/(\d+)/} $xml =~ /<Forum.+?lastMessage="([^"]+)"[^>]*>/;
506 my $reg_msg = qr
~(?
:</Message
>
507 |<Message\s
+id
="m(\d+)"\s
+unid
="([^"]*)"(?:\s+invisible="([^"]*)")?
(?
:\s
+archive
="([^"]*)")?[^>]*>\s*
508 <Header>[^<]*(?:<(?!Name>)[^<]*)*
509 <Name>([^<]+)</Name>[^<]*(?:<(?!Category>)[^<]*)*
510 <Category>([^<]*)</Category>\s*
511 <Subject>([^<]+)</Subject>\s*
512 <Date\s+longSec="(\d
+)"[^>]*>\s*
513 </Header>\s*(?:(<)/Message>|(?=(<)Message\s*)))~sx;
515 while ($xml =~ /<Thread id="t
(\d
+)">([^<]*(?:<(?!\/Thread>)[^<]*)*)<\/Thread>/g)
517 my ($tid, $thread) = ($1, $2);
518 my ($level, $cmno, @msg, @stack) = (0);
520 while ($thread =~ m;$reg_msg;g)
524 push @stack,$cmno if (defined $cmno);
542 push @{$msg[$cmno] -> {kids}} => $#msg;
543 push @{$msg[$cmno] -> {unids}} => $2;
550 $msg[$_] -> {answers}++ for (@stack);
554 $msg[-1] -> {name} =~ s/&/&/g;
555 $msg[-1] -> {cat} =~ s/&/&/g;
556 $msg[-1] -> {subject} =~ s/&/&/g;
578 push @{$msg[$cmno] -> {kids}} => $#msg;
579 push @{$msg[$cmno] -> {unids}} => $2;
580 $msg[$cmno] -> {answers}++;
587 $msg[$_] -> {answers}++ for (@stack);
589 $msg[-1] -> {name} =~ s/&/&/g;
590 $msg[-1] -> {cat} =~ s/&/&/g;
591 $msg[-1] -> {subject} =~ s/&/&/g;
595 $cmno = pop @stack; $level--;
599 my $smsg = sort_thread (\@msg, $sorted); # sort messages
600 delete_messages ($smsg) unless ($deleted); # remove invisible messages
602 $threads{$tid} = $smsg if (@$smsg);
606 ? (\%threads, $last_thread, $last_message, $dtd, \@unids)
610 ###########################
611 # sub create_forum_xml_string
613 # Forumshauptdatei erzeugen
614 ###########################
616 sub create_forum_xml_string ($$) {
617 my ($threads, $param) = @_;
618 my ($level, $thread, $msg);
620 my $xml = '<?xml version="1.0" encoding="UTF
-8"?>'."\n"
621 .'<!DOCTYPE Forum SYSTEM "'.$param -> {dtd}.'">'."\n"
622 .'<Forum lastMessage="'.$param -> {lastMessage}.'" lastThread="'.$param -> {lastThread}.'">';
624 foreach $thread (sort {$b <=> $a} keys %$threads) {
625 $xml .= '<Thread id="t
'.$thread.'">';
628 foreach $msg (@{$threads -> {$thread}}) {
629 $xml .= '</Message>' x ($level - $msg -> {level} + 1) if ($msg -> {level} <= $level);
630 $level = $msg -> {level};
631 $xml .= '<Message id="m
'.$msg -> {mid}.'"'
632 .' unid="'.$msg -> {unid}.'"'
633 .(($msg -> {deleted})?' invisible="1"':'')
634 .(($msg -> {archive})?' archive="1"':'')
639 .plain($msg -> {name})
644 .((length $msg -> {cat})?plain($msg -> {cat}):'')
647 .plain($msg -> {subject})
654 $xml .= '</Message>' x ($level + 1);
655 $xml .= '</Thread>';}
662 ### save_file () ###############################################################
666 # Params: $filename Filename
667 # $content File content as scalar reference
668 # Return: Status (1 - ok, 0 - error)
672 my ($filename, $content) = @_;
675 open FILE, ">$filename.temp
" or return;
677 unless (print FILE $$content)
683 close FILE or return;
685 rename "$filename.temp
", $filename or return;
690 # ====================================================
692 # ====================================================
694 ###########################
696 # 02. Januar 2001, 12:02 Uhr
699 # 02. 01. 2001, 12:02 Uhr
702 # Dienstag, 02. Januar 2001, 12:02:01 Uhr
704 # formatierte Zeitangabe
705 ###########################
708 my @month = (qw(Januar Februar), "M\303\244rz", qw(April Mai Juni Juli August September Oktober November Dezember));
711 my (undef, $min, $hour, $day, $mon, $year) = localtime ($_[0]);
713 sprintf ('%02d. %s %04d, %02d:%02d Uhr', $day, $month[$mon], $year+1900, $hour, $min);
716 sub short_hr_time
($) {
717 my (undef, $min, $hour, $day, $mon, $year) = localtime ($_[0]);
719 sprintf ('%02d. %02d. %04d, %02d:%02d Uhr', $day, $mon+1, $year+1900, $hour, $min);
722 sub long_hr_time
($) {
723 my @month = (qw(Januar Februar), "M\303\244rz", qw(April Mai Juni Juli August September Oktober November Dezember));
726 my @wday = qw(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag);
727 my ($sek, $min, $hour, $day, $mon, $year, $wday) = localtime ($_[0]);
729 sprintf ('%s, %02d. %s %04d, %02d:%02d:%02d Uhr', $wday[$wday], $day, $month[$mon], $year+1900, $hour, $min, $sek);
733 my @month = (qw(Januar Februar), "M\303\244rz", qw(April Mai Juni Juli August September Oktober November Dezember));
736 return $month[$_[0]];
739 # ====================================================
740 # Modulinitialisierung
741 # ====================================================
743 # making require happy
patrick-canterino.de