]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/_lib.pm
3 ################################################################################
5 # File: shared/Posting/_lib.pm #
7 # Authors: André Malo <nd@o3media.de>, 2001-06-11 #
8 # Frank Schoenmann <fs@tower.de>, 2001-06-04 #
10 # Description: Message access interface, time format routines #
12 ################################################################################
16 use Encode
::Plain
; $Encode::Plain
::utf8
= 1;
21 # ====================================================
23 # ====================================================
25 use constant SORT_ASCENT
=> 0; # (young postings first)
26 use constant SORT_DESCENT
=> 1;
27 use constant KEEP_DELETED
=> 1;
28 use constant KILL_DELETED
=> 0;
30 use base
qw(Exporter);
31 @Posting::_lib
::EXPORT_OK
= qw(
48 create_forum_xml_string
58 # ====================================================
60 # ====================================================
62 ### sub create_message ($$) ####################################################
64 # create the 'Message' subtree
66 # Params: $xml - XML::DOM::Document object
67 # $par - hash reference
68 # (msg, ip, name, email, home, image, category, subject, time)
70 # Return: XML::DOM::Element object
72 sub create_message
($$) {
75 my $message = $xml -> createElement
('Message');
76 $message -> setAttribute
('id' => $par -> {msg
});
77 $message -> setAttribute
('ip' => $par -> {ip
});
79 my $header = $xml -> createElement
('Header');
80 my $author = $xml -> createElement
('Author');
81 $header -> appendChild
($author);
84 ['name' => 'Name' => $author],
85 ['email' => 'Email' => $author],
86 ['home' => 'HomepageUrl' => $author],
87 ['image' => 'ImageUrl' => $author],
88 ['category' => 'Category' => $header],
89 ['subject' => 'Subject' => $header]
90 );# key => element name => superior
95 my $obj = $xml -> createElement
($_->[1]);
99 defined $par -> {$_->[0]}
104 # link to superior element
105 $_ -> [2] -> appendChild
($obj);
108 my $date = $xml -> createElement
('Date');
109 $date -> setAttribute
('longSec'=> $par -> {time});
111 $header -> appendChild
($date);
112 $message -> appendChild
($header);
119 ### sub create_new_thread ($) ##################################################
121 # create a XML::DOM::Document object of a thread containing one posting
123 # Params: hash reference
124 # (dtd, thread, msg, body, ip, name, email, home,
125 # image, category, subject, time)
127 # Return: XML::DOM::Document object
129 sub create_new_thread
($) {
134 my $xml = new XML
::DOM
::Document
;
138 my $decl = new XML
::DOM
::XMLDecl
;
139 $decl -> setVersion
('1.0');
140 $decl -> setEncoding
('UTF-8');
141 $xml -> setXMLDecl
($decl);
145 my $dtd = $xml -> createDocumentType
('Forum' => $par -> {dtd
});
146 $xml -> setDoctype
($dtd);
148 # create root element 'Forum'
149 # create element 'Thread'
150 # create 'Message' subtree
151 # create element 'ContentList'
152 # create 'MessageContent' subtree
154 my $forum = $xml -> createElement
('Forum');
155 my $thread = $xml -> createElement
('Thread');
156 $thread -> setAttribute
('id' => $par -> {thread
});
157 my $message = create_message
($xml,$par);
158 my $content = $xml -> createElement
('ContentList');
159 my $mcontent = $xml -> createElement
('MessageContent');
160 $mcontent -> setAttribute
('mid' => $par -> {msg
});
161 $mcontent -> appendChild
(
162 $xml -> createCDATASection
(${$par -> {body
}})
165 # link all the nodes to
166 # their superior elements
168 $thread -> appendChild
($message);
169 $forum -> appendChild
($thread);
170 $content -> appendChild
($mcontent);
171 $forum -> appendChild
($content);
172 $xml -> appendChild
($forum);
179 ### get_message_header () ######################################################
181 # Read message header, return as a hash
183 # Params: $node - XML message node
184 # Return: hash reference (name, category, subject, email, home, image, time)
186 sub get_message_header
($)
191 my $header = $node -> getElementsByTagName
('Header' , 0) -> item
(0);
192 my $author = $header -> getElementsByTagName
('Author' , 0) -> item
(0);
193 my $name = $author -> getElementsByTagName
('Name' , 0) -> item
(0);
194 my $email = $author -> getElementsByTagName
('Email' , 0) -> item
(0);
195 my $home = $author -> getElementsByTagName
('HomepageUrl', 0) -> item
(0);
196 my $image = $author -> getElementsByTagName
('ImageUrl' , 0) -> item
(0);
197 my $cat = $header -> getElementsByTagName
('Category' , 0) -> item
(0);
198 my $subject = $header -> getElementsByTagName
('Subject' , 0) -> item
(0);
199 my $date = $header -> getElementsByTagName
('Date' , 0) -> item
(0);
202 name
=> ($name -> hasChildNodes
)?
$name -> getFirstChild
-> getData
:undef,
203 category
=> ($cat -> hasChildNodes
)?
$cat -> getFirstChild
-> getData
:undef,
204 subject
=> ($subject -> hasChildNodes
)?
$subject -> getFirstChild
-> getData
:undef,
205 email
=> (defined ($email) and $email -> hasChildNodes
)?
$email -> getFirstChild
-> getData
:undef,
206 home
=> (defined ($home) and $home -> hasChildNodes
)?
$home -> getFirstChild
-> getData
:undef,
207 image
=> (defined ($image) and $image -> hasChildNodes
)?
$image -> getFirstChild
-> getData
:undef,
208 time => $date -> getAttribute
('longSec')
214 ### get_body_node () ########################################################
216 # Search a specific message body in a XML tree
218 # Params: $xml XML::DOM::Document Object (Document Node)
221 # Return: MessageContent XML node (or -none-)
223 sub get_body_node
($$)
225 my ($xml, $mid) = @_;
227 for ($xml -> getElementsByTagName
('ContentList', 1) -> item
(0) -> getElementsByTagName
('MessageContent', 0)) {
228 return $_ if ($_ -> getAttribute
('mid') eq $mid);
234 ### get_message_body () ########################################################
238 # Params: $xml XML::DOM::Document Object (Document Node)
241 # Return: Scalar reference
243 sub get_message_body
($$)
245 my $cnode = get_body_node
($_[0], $_[1]);
248 $body = ($cnode -> hasChildNodes
)?
$cnode -> getFirstChild
-> getData
:'' if $cnode;
253 ### get_message_node () ########################################################
255 # Search a specific message in a XML tree
257 # Params: $xml XML::DOM::Document Object (Document Node)
261 # Return: Message XML node, Thread XML node (or -none-)
263 sub get_message_node
($$$)
265 my ($xml, $tid, $mid) = @_;
268 for ($xml->getElementsByTagName ('Thread')) {
269 if ($_->getAttribute ('id') eq $tid) {
272 for ($tnode -> getElementsByTagName
('Message')) {
273 if ($_ -> getAttribute
('id') eq $mid) {
287 ### sub parse_xml_file ($) #####################################################
289 # load the specified XML-File and create the DOM tree
290 # this sub is only to avoid errors and to centralize the parse process
292 # Params: $file filename
294 # Return: XML::DOM::Document Object (Document Node) or false
296 sub parse_xml_file
($) {
300 local $SIG{__DIE__
}; # CGI::Carp works unreliable ;-(
301 new XML
::DOM
::Parser
(KeepCDATA
=> 1)->parsefile ($file);
309 ###########################
310 # sub parse_single_thread
312 # einzelne Threaddatei
314 ###########################
316 sub parse_single_thread
($$;$) {
317 my ($tnode, $deleted, $sorted) = @_;
318 my ($header, @msg, %mno);
320 for ($tnode -> getElementsByTagName
('Message')) {
321 $header = get_message_header
($_);
323 push @msg,{mid
=> ($_ -> getAttribute
('id') =~ /(\d+)/)[0],
324 ip
=> $_ -> getAttribute
('ip'),
325 kids
=> [$_ -> getElementsByTagName
('Message', 0)],
326 answers
=> $_ -> getElementsByTagName
('Message') -> getLength
,
327 deleted
=> $_ -> getAttribute
('invisible'),
328 archive
=> $_ -> getAttribute
('archive'),
329 name
=> plain
($header -> {name
}),
330 cat
=> plain
($header -> {category
} or ''),
331 subject
=> plain
($header -> {subject
}),
332 time => plain
($header -> {time})};
335 # Eintraege ergaenzen und korrigieren
337 $msg[0] -> {level
} = 0;
339 $level = $_ -> {level
} + 1;
340 @
{$_ -> {kids
}} = map {$msg[$mno{$_}] -> {level
} = $level; $mno{$_}} @
{$_ -> {kids
}};}
343 # Sortieren und bei Bedarf
344 # geloeschte Messages entfernen
346 my $smsg = sort_thread
(\
@msg, $sorted);
347 delete_messages
($smsg) unless ($deleted);
352 ###########################
353 # sub create_message_xml
357 ###########################
359 sub create_message_xml
($$$) {
360 my ($xml, $msges, $num) = @_;
362 my $msg = $msges -> [$num];
364 my $message = $xml -> createElement
('Message');
365 $message -> setAttribute
('id', 'm'.$msg -> {mid
});
366 $message -> setAttribute
('invisible', '1') if ($msg -> {deleted
});
367 $message -> setAttribute
('archive', '1') if ($msg -> {archive
});
370 my $header = $xml -> createElement
('Header');
372 # alles inside of 'Header'
373 my $author = $xml -> createElement
('Author');
375 my $name = $xml -> createElement
('Name');
376 $name -> addText
(toUTF8
($msg -> {name
}));
378 my $email = $xml -> createElement
('Email');
380 my $category = $xml -> createElement
('Category');
381 $category -> addText
(toUTF8
($msg -> {cat
}));
383 my $subject = $xml -> createElement
('Subject');
384 $subject -> addText
(toUTF8
($msg -> {subject
}));
386 my $date = $xml -> createElement
('Date');
387 $date -> setAttribute
('longSec', $msg -> {time});
389 $author -> appendChild
($name);
390 $author -> appendChild
($email);
391 $header -> appendChild
($author);
392 $header -> appendChild
($category);
393 $header -> appendChild
($subject);
394 $header -> appendChild
($date);
395 $message -> appendChild
($header);
397 if ($msg -> {kids
}) {
398 for (@
{$msg -> {kids
}}) {
399 $message -> appendChild
(&create_message_xml
($xml, $msges, $_));
406 # ====================================================
407 # XML-Parsen von Hand
408 # ====================================================
410 ###########################
415 ###########################
417 sub sort_thread
($$) {
418 my ($msg, $sorted) = @_;
420 my ($z, %mhash) = (0);
422 if ($sorted) { # aelteste zuerst
424 @
$msg[@
{$_ -> {kids
}}] = sort {$a -> {mid
} <=> $b -> {mid
}} @
$msg[@
{$_ -> {kids
}}] if (@
{$_ -> {kids
}} > 1);
425 $mhash{$_ -> {mid
}} = [@
$msg[@
{$_ -> {kids
}}]];}}
427 else { # juengste zuerst
429 @
$msg[@
{$_ -> {kids
}}] = sort {$b -> {mid
} <=> $a -> {mid
}} @
$msg[@
{$_ -> {kids
}}] if (@
{$_ -> {kids
}} > 1);
430 $mhash{$_ -> {mid
}} = [@
$msg[@
{$_ -> {kids
}}]];}}
432 # Kinder wieder richtig einsortieren
433 my @smsg = ($msg -> [0]);
436 splice @smsg,$z,0,@
{$mhash{$_ -> {mid
}}} if ($_ -> {answers
});
437 delete $_ -> {kids
};}
442 ### delete_messages () #########################################################
444 # Filter out deleted messages
446 # Params: $smsg Reference of array of references of hashs
449 sub delete_messages
($) {
451 my ($z, $oldlevel, @path) = (0,0,0);
453 while ($z <= $#{$smsg}) {
455 if ($smsg -> [$z] -> {level
} > $oldlevel) {
457 $oldlevel = $smsg -> [$z] -> {level
};
459 elsif ($smsg -> [$z] -> {level
} < $oldlevel) {
460 splice @path, $smsg -> [$z] -> {level
};
462 $oldlevel = $smsg -> [$z] -> {'level'};
468 if ($smsg -> [$z] -> {deleted
}) {
469 my $n = $smsg -> [$z] -> {answers
} + 1;
470 $smsg -> [$_] -> {answers
} -= $n for (@path);
471 splice @
$smsg, $z, $n;
481 ### get_all_threads () #########################################################
483 # Read and Parse the main file (without any XML-module, they are too slow)
485 # Params: $file - /path/to/filename of the main file
486 # $deleted - hold deleted (invisible) messages in result (1) oder not (0)
487 # $sorted - direction of message sort: descending (0) (default) or ascending (1)
489 # Return: scalar context: hash reference (\%threads)
490 # list context: list (\%threads, $last_thread, $last_message, $dtd, \@unids)
492 sub get_all_threads
($$;$) {
493 my ($file, $deleted, $sorted) = @_;
494 my ($last_thread, $last_message, $dtd, @unids, %threads);
497 open FILE
,"< $file" or return;
498 my $xml = join '', <FILE
>;
499 close(FILE
) or return;
502 ($dtd) = $xml =~ /<!DOCTYPE\s+\S+\s+SYSTEM\s+"([^"]+)">/;
503 ($last_thread) = map {/(\d+)/} $xml =~ /<Forum.+?lastThread="([^"]+)"[^>]*>/;
504 ($last_message) = map {/(\d+)/} $xml =~ /<Forum.+?lastMessage="([^"]+)"[^>]*>/;
507 my $reg_msg = qr
~(?
:</Message
>
508 |<Message\s
+id
="m(\d+)"\s
+unid
="([^"]*)"(?:\s+invisible="([^"]*)")?
(?
:\s
+archive
="([^"]*)")?[^>]*>\s*
509 <Header>[^<]*(?:<(?!Name>)[^<]*)*
510 <Name>([^<]+)</Name>[^<]*(?:<(?!Category>)[^<]*)*
511 <Category>([^<]*)</Category>\s*
512 <Subject>([^<]+)</Subject>\s*
513 <Date\s+longSec="(\d
+)"[^>]*>\s*
514 </Header>\s*(?:(<)/Message>|(?=(<)Message\s*)))~sx;
516 while ($xml =~ /<Thread id="t
(\d
+)">([^<]*(?:<(?!\/Thread>)[^<]*)*)<\/Thread>/g)
518 my ($tid, $thread) = ($1, $2);
519 my ($level, $cmno, @msg, @stack) = (0);
521 while ($thread =~ m;$reg_msg;g)
525 push @stack,$cmno if (defined $cmno);
543 push @{$msg[$cmno] -> {kids}} => $#msg;
544 push @{$msg[$cmno] -> {unids}} => $2;
551 $msg[$_] -> {answers}++ for (@stack);
555 $msg[-1] -> {name} =~ s/&/&/g;
556 $msg[-1] -> {cat} =~ s/&/&/g;
557 $msg[-1] -> {subject} =~ s/&/&/g;
579 push @{$msg[$cmno] -> {kids}} => $#msg;
580 push @{$msg[$cmno] -> {unids}} => $2;
581 $msg[$cmno] -> {answers}++;
588 $msg[$_] -> {answers}++ for (@stack);
590 $msg[-1] -> {name} =~ s/&/&/g;
591 $msg[-1] -> {cat} =~ s/&/&/g;
592 $msg[-1] -> {subject} =~ s/&/&/g;
596 $cmno = pop @stack; $level--;
600 my $smsg = sort_thread (\@msg, $sorted); # sort messages
601 delete_messages ($smsg) unless ($deleted); # remove invisible messages
603 $threads{$tid} = $smsg if (@$smsg);
607 ? (\%threads, $last_thread, $last_message, $dtd, \@unids)
611 ### create_forum_xml_string () #################################################
613 # compose main file xml string
615 # Params: $threads - parsed threads (see also 'get_all_threads')
616 # $params - hashref (see doc for details)
618 # Return: scalarref of the xml string
620 sub create_forum_xml_string ($$) {
621 my ($threads, $param) = @_;
622 my ($level, $thread, $msg);
625 '<?xml version="1.0" encoding="UTF
-8"?>'."\n"
626 . '<!DOCTYPE Forum SYSTEM "'.$param -> {dtd}.'">'."\n"
627 . '<Forum lastMessage="'.$param -> {lastMessage}.'" lastThread="'.$param -> {lastThread}.'">';
629 for $thread (sort {$b <=> $a} keys %$threads) {
630 $xml .= '<Thread id="t
'.$thread.'">';
633 for $msg (@{$threads -> {$thread}}) {
634 $xml .= '</Message>' x ($level - $msg -> {level} + 1) if ($msg -> {level} <= $level);
636 $level = $msg -> {level};
638 '<Message id="m
'.$msg -> {mid}.'"'
639 . ' unid="'.$msg -> {unid}.'"'
640 . (($msg -> {deleted})?' invisible="1"':'')
641 . (($msg -> {archive})?' archive="1"':'')
646 . plain($msg -> {name})
651 . ((length $msg -> {cat})?plain($msg -> {cat}):'')
654 . plain($msg -> {subject})
662 $xml .= '</Message>' x ($level + 1);
663 $xml .= '</Thread>';}
670 ### save_file () ###############################################################
674 # Params: $filename Filename
675 # $content File content as scalar reference
676 # Return: Status (1 - ok, 0 - error)
680 my ($filename, $content) = @_;
683 open FILE, ">$filename.temp
" or return;
685 unless (print FILE $$content)
691 close FILE or return;
693 rename "$filename.temp
", $filename or return;
698 ################################################################################
700 # several time formatting routines
703 # 02. Januar 2001, 12:02 Uhr
706 # 02. 01. 2001, 12:02 Uhr
709 # Dienstag, 02. Januar 2001, 12:02:01 Uhr
715 (qw(Januar Februar), "M\303\244rz", qw(April Mai Juni Juli August September Oktober November Dezember))[shift (@_) - 1];
720 my (undef, $min, $hour, $day, $mon, $year) = germantime
(shift);
722 sprintf ('%02d. %s %04d, %02d:%02d Uhr', $day, month
($mon+1), $year+1900, $hour, $min);
725 sub short_hr_time
($) {
726 my (undef, $min, $hour, $day, $mon, $year) = germantime
(shift);
728 sprintf ('%02d. %02d. %04d, %02d:%02d Uhr', $day, $mon+1, $year+1900, $hour, $min);
731 sub long_hr_time
($) {
732 my @wday = qw(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag);
733 my ($sek, $min, $hour, $day, $mon, $year, $wday) = germantime
(shift);
735 sprintf ('%s, %02d. %s %04d, %02d:%02d:%02d Uhr', $wday[$wday], $day, month
($mon+1), $year+1900, $hour, $min, $sek);
738 sub very_short_hr_time
($) {
739 my (undef, $min, $hour, $day, $mon, $year) = germantime
(shift);
741 sprintf ('%02d. %02d. %04d', $day, $mon+1, $year+1900);
744 # keep 'require' happy
749 ### end of Posting::_lib #######################################################
patrick-canterino.de