]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/_lib.pm
9a5330765d5665087cc0a477bf7ffdb8349d37b1
3 ################################################################################
5 # File: shared/Posting/_lib.pm #
7 # Authors: André Malo <nd@o3media.de>, 2001-03-03 #
8 # Frank Schoenmann <fs@tower.de>, 2001-03-13 #
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(
45 create_forum_xml_string
55 # ====================================================
57 # ====================================================
59 ### sub create_message ($$) ####################################################
61 # create the 'Message' subtree
63 # Params: $xml - XML::DOM::Document object
64 # $par - hash reference
65 # (msg, ip, name, email, home, image, category, subject, time)
67 # Return: XML::DOM::Element object
69 sub create_message
($$) {
72 my $message = $xml -> createElement
('Message');
73 $message -> setAttribute
('id' => $par -> {msg
});
74 $message -> setAttribute
('ip' => $par -> {ip
});
76 my $header = $xml -> createElement
('Header');
77 my $author = $xml -> createElement
('Author');
78 $header -> appendChild
($author);
81 ['name' => 'Name' => $author],
82 ['email' => 'Email' => $author],
83 ['home' => 'HomepageUrl' => $author],
84 ['image' => 'ImageUrl' => $author],
85 ['category' => 'Category' => $header],
86 ['subject' => 'Subject' => $header]
87 );# key => element name => superior
92 my $obj = $xml -> createElement
($_->[1]);
96 defined $par -> {$_->[0]}
101 # link to superior element
102 $_ -> [2] -> appendChild
($obj);
105 my $date = $xml -> createElement
('Date');
106 $date -> setAttribute
('longSec'=> $par -> {time});
108 $header -> appendChild
($date);
109 $message -> appendChild
($header);
116 ### sub create_new_thread ($) ##################################################
118 # create a XML::DOM::Document object of a thread containing one posting
120 # Params: hash reference
121 # (dtd, thread, msg, body, ip, name, email, home,
122 # image, category, subject, time)
124 # Return: XML::DOM::Document object
126 sub create_new_thread
($) {
131 my $xml = new XML
::DOM
::Document
;
135 my $decl = new XML
::DOM
::XMLDecl
;
136 $decl -> setVersion
('1.0');
137 $decl -> setEncoding
('UTF-8');
138 $xml -> setXMLDecl
($decl);
142 my $dtd = $xml -> createDocumentType
('Forum' => $par -> {dtd
});
143 $xml -> setDoctype
($dtd);
145 # create root element 'Forum'
146 # create element 'Thread'
147 # create 'Message' subtree
148 # create element 'ContentList'
149 # create 'MessageContent' subtree
151 my $forum = $xml -> createElement
('Forum');
152 my $thread = $xml -> createElement
('Thread');
153 $thread -> setAttribute
('id' => $par -> {thread
});
154 my $message = create_message
($xml,$par);
155 my $content = $xml -> createElement
('ContentList');
156 my $mcontent = $xml -> createElement
('MessageContent');
157 $mcontent -> setAttribute
('mid' => $par -> {msg
});
158 $mcontent -> appendChild
(
159 $xml -> createCDATASection
(${$par -> {body
}})
162 # link all the nodes to
163 # their superior elements
165 $thread -> appendChild
($message);
166 $forum -> appendChild
($thread);
167 $content -> appendChild
($mcontent);
168 $forum -> appendChild
($content);
169 $xml -> appendChild
($forum);
176 ### get_message_header () ######################################################
178 # Read message header, return as a hash
180 # Params: $node - XML message node
181 # Return: hash reference (name, category, subject, email, home, image, time)
183 sub get_message_header
($)
188 my $header = $node -> getElementsByTagName
('Header' , 0) -> item
(0);
189 my $author = $header -> getElementsByTagName
('Author' , 0) -> item
(0);
190 my $name = $author -> getElementsByTagName
('Name' , 0) -> item
(0);
191 my $email = $author -> getElementsByTagName
('Email' , 0) -> item
(0);
192 my $home = $author -> getElementsByTagName
('HomepageUrl', 0) -> item
(0);
193 my $image = $author -> getElementsByTagName
('ImageUrl' , 0) -> item
(0);
194 my $cat = $header -> getElementsByTagName
('Category' , 0) -> item
(0);
195 my $subject = $header -> getElementsByTagName
('Subject' , 0) -> item
(0);
196 my $date = $header -> getElementsByTagName
('Date' , 0) -> item
(0);
199 name
=> ($name -> hasChildNodes
)?
$name -> getFirstChild
-> getData
:undef,
200 category
=> ($cat -> hasChildNodes
)?
$cat -> getFirstChild
-> getData
:undef,
201 subject
=> ($subject -> hasChildNodes
)?
$subject -> getFirstChild
-> getData
:undef,
202 email
=> (defined ($email) and $email -> hasChildNodes
)?
$email -> getFirstChild
-> getData
:undef,
203 home
=> (defined ($home) and $home -> hasChildNodes
)?
$home -> getFirstChild
-> getData
:undef,
204 image
=> (defined ($image) and $image -> hasChildNodes
)?
$image -> getFirstChild
-> getData
:undef,
205 time => $date -> getAttribute
('longSec')
211 ### get_body_node () ########################################################
213 # Search a specific message body in a XML tree
215 # Params: $xml XML::DOM::Document Object (Document Node)
218 # Return: MessageContent XML node (or -none-)
220 sub get_body_node
($$)
222 my ($xml, $mid) = @_;
224 for ($xml -> getElementsByTagName
('ContentList', 1) -> item
(0) -> getElementsByTagName
('MessageContent', 0)) {
225 return $_ if ($_ -> getAttribute
('mid') eq $mid);
231 ### get_message_body () ########################################################
235 # Params: $xml XML::DOM::Document Object (Document Node)
238 # Return: Scalar reference
240 sub get_message_body
($$)
242 my $cnode = get_body_node
($_[0], $_[1]);
245 $body = ($cnode -> hasChildNodes
)?
$cnode -> getFirstChild
-> getData
:'' if $cnode;
250 ### get_message_node () ########################################################
252 # Search a specific message in a XML tree
254 # Params: $xml XML::DOM::Document Object (Document Node)
258 # Return: Message XML node, Thread XML node (or -none-)
260 sub get_message_node
($$$)
262 my ($xml, $tid, $mid) = @_;
265 for ($xml->getElementsByTagName ('Thread')) {
266 if ($_->getAttribute ('id') eq $tid) {
269 for ($tnode -> getElementsByTagName
('Message')) {
270 if ($_ -> getAttribute
('id') eq $mid) {
284 ### sub parse_xml_file ($) #####################################################
286 # load the specified XML-File and create the DOM tree
287 # this sub is only to avoid errors and to centralize the parse process
289 # Params: $file filename
291 # Return: XML::DOM::Document Object (Document Node) or false
293 sub parse_xml_file
($) {
297 local $SIG{__DIE__
}; # CGI::Carp works unreliable ;-(
298 new XML
::DOM
::Parser
(KeepCDATA
=> 1) -> parsefile
($file);
306 ###########################
307 # sub parse_single_thread
309 # einzelne Threaddatei
311 ###########################
313 sub parse_single_thread
($$;$) {
314 my ($tnode, $deleted, $sorted) = @_;
315 my ($header, @msg, %mno);
317 for ($tnode -> getElementsByTagName
('Message')) {
318 $header = get_message_header
($_);
320 push @msg,{mid
=> ($_ -> getAttribute
('id') =~ /(\d+)/)[0],
321 ip
=> $_ -> getAttribute
('ip'),
322 kids
=> [$_ -> getElementsByTagName
('Message', 0)],
323 answers
=> $_ -> getElementsByTagName
('Message') -> getLength
,
324 deleted
=> $_ -> getAttribute
('invisible'),
325 archive
=> $_ -> getAttribute
('archive'),
326 name
=> plain
($header -> {name
}),
327 cat
=> plain
($header -> {category
} or ''),
328 subject
=> plain
($header -> {subject
}),
329 time => plain
($header -> {time})};
332 # Eintraege ergaenzen und korrigieren
334 $msg[0] -> {level
} = 0;
336 $level = $_ -> {level
} + 1;
337 @
{$_ -> {kids
}} = map {$msg[$mno{$_}] -> {level
} = $level; $mno{$_}} @
{$_ -> {kids
}};}
340 # Sortieren und bei Bedarf
341 # geloeschte Messages entfernen
343 my $smsg = sort_thread
(\
@msg, $sorted);
344 delete_messages
($smsg) unless ($deleted);
349 ###########################
350 # sub create_message_xml
354 ###########################
356 sub create_message_xml
($$$) {
357 my ($xml, $msges, $num) = @_;
359 my $msg = $msges -> [$num];
361 my $message = $xml -> createElement
('Message');
362 $message -> setAttribute
('id', 'm'.$msg -> {mid
});
363 $message -> setAttribute
('invisible', '1') if ($msg -> {deleted
});
364 $message -> setAttribute
('archive', '1') if ($msg -> {archive
});
367 my $header = $xml -> createElement
('Header');
369 # alles inside of 'Header'
370 my $author = $xml -> createElement
('Author');
372 my $name = $xml -> createElement
('Name');
373 $name -> addText
(toUTF8
($msg -> {name
}));
375 my $email = $xml -> createElement
('Email');
377 my $category = $xml -> createElement
('Category');
378 $category -> addText
(toUTF8
($msg -> {cat
}));
380 my $subject = $xml -> createElement
('Subject');
381 $subject -> addText
(toUTF8
($msg -> {subject
}));
383 my $date = $xml -> createElement
('Date');
384 $date -> setAttribute
('longSec', $msg -> {time});
386 $author -> appendChild
($name);
387 $author -> appendChild
($email);
388 $header -> appendChild
($author);
389 $header -> appendChild
($category);
390 $header -> appendChild
($subject);
391 $header -> appendChild
($date);
392 $message -> appendChild
($header);
394 if ($msg -> {kids
}) {
395 for (@
{$msg -> {kids
}}) {
396 $message -> appendChild
(&create_message_xml
($xml, $msges, $_));
403 # ====================================================
404 # XML-Parsen von Hand
405 # ====================================================
407 ###########################
412 ###########################
414 sub sort_thread
($$) {
415 my ($msg, $sorted) = @_;
417 my ($z, %mhash) = (0);
419 if ($sorted) { # aelteste zuerst
421 @
$msg[@
{$_ -> {kids
}}] = sort {$a -> {mid
} <=> $b -> {mid
}} @
$msg[@
{$_ -> {kids
}}] if (@
{$_ -> {kids
}} > 1);
422 $mhash{$_ -> {mid
}} = [@
$msg[@
{$_ -> {kids
}}]];}}
424 else { # juengste zuerst
426 @
$msg[@
{$_ -> {kids
}}] = sort {$b -> {mid
} <=> $a -> {mid
}} @
$msg[@
{$_ -> {kids
}}] if (@
{$_ -> {kids
}} > 1);
427 $mhash{$_ -> {mid
}} = [@
$msg[@
{$_ -> {kids
}}]];}}
429 # Kinder wieder richtig einsortieren
430 my @smsg = ($msg -> [0]);
433 splice @smsg,$z,0,@
{$mhash{$_ -> {mid
}}} if ($_ -> {answers
});
434 delete $_ -> {kids
};}
439 ### delete_messages () #########################################################
441 # Filter out deleted messages
443 # Params: $smsg Reference of array of references of hashs
446 sub delete_messages
($) {
448 my ($z, $oldlevel, @path) = (0,0,0);
450 while ($z <= $#{$smsg}) {
452 if ($smsg -> [$z] -> {level
} > $oldlevel) {
454 $oldlevel = $smsg -> [$z] -> {level
};
456 elsif ($smsg -> [$z] -> {level
} < $oldlevel) {
457 splice @path, $smsg -> [$z] -> {level
};
459 $oldlevel = $smsg -> [$z] -> {'level'};
465 if ($smsg -> [$z] -> {deleted
}) {
466 my $n = $smsg -> [$z] -> {answers
} + 1;
467 $smsg -> [$_] -> {answers
} -= $n for (@path);
468 splice @
$smsg, $z, $n;
478 ### get_all_threads () #########################################################
480 # Read and Parse the main file (without any XML-module, they are too slow)
482 # Params: $file /path/to/filename of the main file
483 # $deleted hold deleted (invisible) messages in result (1) oder not (0)
484 # $sorted direction of message sort: descending (0) (default) or ascending (1)
485 # Return: scalar context: hash reference
486 # list context: list (\%threads, $last_thread, $last_message, $dtd, \@unids)
488 sub get_all_threads
($$;$)
490 my ($file, $deleted, $sorted) = @_;
491 my ($last_thread, $last_message, $dtd, @unids, %threads);
494 open FILE
,"< $file" or return;
495 my $xml = join '', <FILE
>;
496 close(FILE
) or return;
500 ($dtd) = $xml =~ /<!DOCTYPE\s+\S+\s+SYSTEM\s+"([^"]+)">/;
501 ($last_thread) = map {/(\d+)/} $xml =~ /<Forum.+?lastThread="([^"]+)"[^>]*>/;
502 ($last_message) = map {/(\d+)/} $xml =~ /<Forum.+?lastMessage="([^"]+)"[^>]*>/;
505 my $reg_msg = qr
~(?
:</Message
>
506 |<Message\s
+id
="m(\d+)"\s
+unid
="([^"]*)"(?:\s+invisible="([^"]*)")?
(?
:\s
+archive
="([^"]*)")?[^>]*>\s*
507 <Header>[^<]*(?:<(?!Name>)[^<]*)*
508 <Name>([^<]+)</Name>[^<]*(?:<(?!Category>)[^<]*)*
509 <Category>([^<]*)</Category>\s*
510 <Subject>([^<]+)</Subject>\s*
511 <Date\s+longSec="(\d
+)"[^>]*>\s*
512 </Header>\s*(?:(<)/Message>|(?=(<)Message\s*)))~sx;
514 while ($xml =~ /<Thread id="t
(\d
+)">([^<]*(?:<(?!\/Thread>)[^<]*)*)<\/Thread>/g)
516 my ($tid, $thread) = ($1, $2);
517 my ($level, $cmno, @msg, @stack) = (0);
519 while ($thread =~ m;$reg_msg;g)
523 push @stack,$cmno if (defined $cmno);
541 push @{$msg[$cmno] -> {kids}} => $#msg;
542 push @{$msg[$cmno] -> {unids}} => $2;
549 $msg[$_] -> {answers}++ for (@stack);
553 $msg[-1] -> {name} =~ s/&/&/g;
554 $msg[-1] -> {cat} =~ s/&/&/g;
555 $msg[-1] -> {subject} =~ s/&/&/g;
577 push @{$msg[$cmno] -> {kids}} => $#msg;
578 push @{$msg[$cmno] -> {unids}} => $2;
579 $msg[$cmno] -> {answers}++;
586 $msg[$_] -> {answers}++ for (@stack);
588 $msg[-1] -> {name} =~ s/&/&/g;
589 $msg[-1] -> {cat} =~ s/&/&/g;
590 $msg[-1] -> {subject} =~ s/&/&/g;
594 $cmno = pop @stack; $level--;
598 my $smsg = sort_thread (\@msg, $sorted); # sort messages
599 delete_messages ($smsg) unless ($deleted); # remove invisible messages
601 $threads{$tid} = $smsg if (@$smsg);
605 ? (\%threads, $last_thread, $last_message, $dtd, \@unids)
609 ###########################
610 # sub create_forum_xml_string
612 # Forumshauptdatei erzeugen
613 ###########################
615 sub create_forum_xml_string ($$) {
616 my ($threads, $param) = @_;
617 my ($level, $thread, $msg);
619 my $xml = '<?xml version="1.0" encoding="UTF
-8"?>'."\n"
620 .'<!DOCTYPE Forum SYSTEM "'.$param -> {dtd}.'">'."\n"
621 .'<Forum lastMessage="'.$param -> {lastMessage}.'" lastThread="'.$param -> {lastThread}.'">';
623 foreach $thread (sort {$b <=> $a} keys %$threads) {
624 $xml .= '<Thread id="t
'.$thread.'">';
627 foreach $msg (@{$threads -> {$thread}}) {
628 $xml .= '</Message>' x ($level - $msg -> {level} + 1) if ($msg -> {level} <= $level);
629 $level = $msg -> {level};
630 $xml .= '<Message id="m
'.$msg -> {mid}.'"'
631 .' unid="'.$msg -> {unid}.'"'
632 .(($msg -> {deleted})?' invisible="1"':'')
633 .(($msg -> {archive})?' archive="1"':'')
638 .plain($msg -> {name})
643 .((length $msg -> {cat})?plain($msg -> {cat}):'')
646 .plain($msg -> {subject})
653 $xml .= '</Message>' x ($level + 1);
654 $xml .= '</Thread>';}
661 ### save_file () ###############################################################
665 # Params: $filename Filename
666 # $content File content as scalar reference
667 # Return: Status (1 - ok, 0 - error)
671 my ($filename, $content) = @_;
674 open FILE, ">$filename.temp
" or return;
676 unless (print FILE $$content)
682 close FILE or return;
684 rename "$filename.temp
", $filename or return;
689 # ====================================================
691 # ====================================================
693 ###########################
695 # 02. Januar 2001, 12:02 Uhr
698 # 02. 01. 2001, 12:02 Uhr
701 # Dienstag, 02. Januar 2001, 12:02:01 Uhr
703 # formatierte Zeitangabe
704 ###########################
707 my @month = (qw(Januar Februar), "M\303\244rz", qw(April Mail Juni Juli August September Oktober November Dezember));
710 my (undef, $min, $hour, $day, $mon, $year) = localtime ($_[0]);
712 sprintf ('%02d. %s %04d, %02d:%02d Uhr', $day, $month[$mon], $year+1900, $hour, $min);
715 sub short_hr_time
($) {
716 my (undef, $min, $hour, $day, $mon, $year) = localtime ($_[0]);
718 sprintf ('%02d. %02d. %04d, %02d:%02d Uhr', $day, $mon+1, $year+1900, $hour, $min);
721 sub long_hr_time
($) {
722 my @month = (qw(Januar Februar), "M\303\244rz", qw(April Mail Juni Juli August September Oktober November Dezember));
725 my @wday = qw(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag);
726 my ($sek, $min, $hour, $day, $mon, $year, $wday) = localtime ($_[0]);
728 sprintf ('%s, %02d. %s %04d, %02d:%02d:%02d Uhr', $wday[$wday], $day, $month[$mon], $year+1900, $hour, $min, $sek);
731 # ====================================================
732 # Modulinitialisierung
733 # ====================================================
735 # making require happy
patrick-canterino.de