+### delete_no_archived () ######################################################
+#
+# remove no archived branches vom thread
+#
+# Params: $xml - XML::DOM::Document node
+# $msg - arrayref - messages
+# $percent - voting limit (percent)
+#
+# Return: ~none~
+#
+sub delete_no_archived ($) {
+ my $par = shift;
+
+ my ($xml, $sum, $tid, $msg, $percent) = map {$par->{$_}}
+ qw( xml sum tid msg percent);
+
+ # $oldlevel: contains the level of last checked msg
+ # @path : contains the current branch
+ # %archive : contains the mids, that will be archived
+ # %hidden : contains the invisible mids
+ #
+ my ($oldlevel, @path, %archive, %hidden) = (0, 0);
+
+ # check all messages of thread
+ #
+ for my $z (0..$#{$msg}) {
+
+ if ($msg -> [$z] -> {level} > $oldlevel) {
+ # this msg is a child of the last one
+ #
+ push @path => $z;
+ $oldlevel = $msg -> [$z] -> {level};
+ }
+
+ elsif ($msg -> [$z] -> {level} < $oldlevel) {
+ # this starts a new subbranch (-1+ level(s))
+ #
+
+ # remove last msg (incl. kids), that is on same level
+ #
+ splice @path, $msg -> [$z] -> {level};
+ push @path => $z;
+ $oldlevel = $msg -> [$z] -> {level};
+ }
+
+ else {
+ # the msg is a sister of the last one
+ #
+ $path[-1] = $z;
+ }
+
+ # 'archive' is an admin flag
+ # if set, the message (incl. branch) MUST be archived
+ #
+ if (defined $msg->[$z]->{archive} and $msg->[$z]->{archive}) {
+ $archive{$msg->[$_]->{mid}} = 1 for (@path);
+ }
+
+ # notice invisble messages
+ # while they are in @path and archive flag is not set,
+ # they and their kids WON'T be archived
+ #
+ $hidden{$z} = 1 if ($msg->[$z]->{deleted});
+
+ # if 'archive' is NOT set and message not deleted,
+ #
+ unless ($msg->[$z]->{archive} or $msg->[$z]->{deleted}) {
+ my $key = $sum->{$tid}->{$msg->[$z]->{mid}};
+
+ # ...and they've voted enough, it will be archived
+ #
+ if ($percent == 0 or ($key->{views} and ($key->{votings} * 100 / $key->{views}) >= $percent)) {
+ my $hidden_in_path;
+
+ # check on hidden messages in @path
+ #
+ for (@path) {
+ if ($hidden{$_}) {
+ $hidden_in_path = 1;
+ last;
+ }
+ }
+
+ # set archive-flag for messages in @path,
+ # unless a parent message is hidden
+ #
+ unless ($hidden_in_path) {
+ $archive{$msg->[$_]->{mid}} = 1 for (@path);
+ }
+ }
+ }
+ }
+
+ # now remove messages without 'archive'-flag
+ # from thread xml
+ #
+ for (reverse grep {!$archive{$_->{mid}}} @$msg) {
+ my $h = get_message_node($xml, "t$tid", 'm'.$_->{mid});
+
+ # remove message entry
+ #
+ $h -> getParentNode -> removeChild ($h);
+
+ # remove message text
+ #
+ $h = get_body_node($xml, 'm'.$_->{mid});
+ $h -> getParentNode -> removeChild ($h);
+ }
+}
+
+### create_arcdir () ###########################################################
+#
+# check, if specific directories for year and month exist, create
+# it, if necessary
+#
+# Params: $path - archive root
+# $time - Thread time (GMT)
+#
+# Return: List: $path - /path/to/ to archived thread file
+# $error - error or undef
+#
+sub create_arcdir ($$) {
+ my ($path, $time) = @_;
+
+ my ($month, $year) = (localtime ($time))[4,5];
+
+ # use the 'real' values for directory names
+ #
+ $month++; $year+=1900;
+
+ my $yeardir = $path . $year;
+ my $monthdir = $yeardir . '/' . $month;
+ my $monthpath = $monthdir . '/';
+
+ mkdir $yeardir, 0777 unless (-d $yeardir);
+ return ('', "could not create directory '$yeardir'") unless (-d $yeardir);
+
+ mkdir $monthdir, 0777 unless (-d $monthdir);
+ return ('', "could not create directory '$monthdir'") unless (-d $monthdir);
+
+ # return path, successfully created
+ #
+ $monthpath;
+}
+
+### process_threads () #########################################################
+#
+# process obsolete threads
+# (transmit views/votings from cache, do archive, if necessary)
+#
+# Params: $par - hash reference
+# (opt, cache, failed, obsolete, messagePath,
+# archivePath, adminDefault)
+#
+# Return: ~none~
+#
+sub process_threads ($) {
+ my $par = shift;
+
+ my ($opt, $failed, $obsolete, $cache) = map {$par->{$_}} qw
+ ( opt failed obsolete cache);
+
+ if ($opt->{exArchiving}) {
+
+ # yes, we do archive
+ #
+ my $sum = $cache -> summary;
+ if ($sum) {
+
+ # iterate over all obsolete threads, that are not failed yet
+ #
+ for my $tid (grep {not exists ($failed->{$_})} @$obsolete) {
+ my $xml = parse_xml_file ($par->{messagePath}."t$tid.xml");
+
+ unless ($xml) {
+ # xml parse error
+ #
+ $failed->{$tid} = 'could not parse thread file.';
+ }
+ else {
+ # ok, parse thread
+ #
+ my $tnode = $xml -> getElementsByTagName ('Thread') -> item(0);
+ my $msg = parse_single_thread ($tnode, KEEP_DELETED);
+
+ if ($opt->{archiving} eq 'UserVotings') {
+
+ # filter out the bad stuff
+ #
+ delete_no_archived ({
+ xml => $xml,
+ sum => $sum,
+ tid => $tid,
+ msg => $msg,
+ percent => $par->{adminDefault}->{Voting}->{Limit}
+ });
+ }
+
+ # save back xml file (into archive)
+ #
+ if ($tnode -> hasChildNodes) {
+
+ # insert views and votings counter
+ #
+ for ($tnode -> getElementsByTagName ('Message')) {
+ my ($id) = $_ -> getAttribute ('id') =~ /(\d+)/;
+ $_ -> setAttribute ('views' => $sum->{$tid}->{$id}->{views});
+ $_ -> setAttribute ('votings' => $sum->{$tid}->{$id}->{votings});
+ }
+
+ # create archive dir, unless exists
+ #
+ my ($path, $error) = create_arcdir ($par -> {archivePath}, $msg->[0]->{time});
+
+ if ($error) {
+ $failed->{$tid} = $error;
+ }
+ else {
+ # save thread file
+ #
+ my $file = "${path}t$tid.xml";
+ save_file ($file => \($xml -> toString)) or $failed->{$tid} = "could not save '$file'";
+ }
+ }
+ }
+ }
+ }
+ else {
+ @$failed{@$obsolete} = 'could not load summary';
+ }
+ }
+}
+
+### cut_tail () ################################################################