3 ################################################################################
5 # File: shared/Arc/Archive.pm #
7 # Authors: André Malo <nd@o3media.de> #
9 # Description: Severance of Threads and archiving #
11 ################################################################################
22 create_forum_xml_string
31 use Time
::German
'localtime';
35 ################################################################################
43 sub VERSION
{(q
$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'}
45 ################################################################################
49 use base
qw(Exporter);
50 @EXPORT = qw(cut_tail);
52 ### delete_no_archived () ######################################################
54 # remove no archived branches vom thread
56 # Params: $xml - XML::DOM::Document node
57 # $msg - arrayref - messages
58 # $percent - voting limit (percent)
62 sub delete_no_archived
($) {
65 my ($xml, $sum, $tid, $msg, $percent) = map {$par->{$_}}
66 qw( xml sum tid msg percent);
68 # $oldlevel: contains the level of last checked msg
69 # @path : contains the current branch
70 # %archive : contains the mids, that will be archived
71 # %hidden : contains the invisible mids
73 my ($oldlevel, @path, %archive, %hidden) = (0, 0);
75 # check all messages of thread
77 for my $z (0..$#{$msg}) {
79 if ($msg -> [$z] -> {level
} > $oldlevel) {
80 # this msg is a child of the last one
83 $oldlevel = $msg -> [$z] -> {level
};
86 elsif ($msg -> [$z] -> {level
} < $oldlevel) {
87 # this starts a new subbranch (-1+ level(s))
90 # remove last msg (incl. kids), that is on same level
92 splice @path, $msg -> [$z] -> {level
};
94 $oldlevel = $msg -> [$z] -> {level
};
98 # the msg is a sister of the last one
103 # 'archive' is an admin flag
104 # if set, the message (incl. branch) MUST be archived
106 if (defined $msg->[$z]->{archive
} and $msg->[$z]->{archive
}) {
107 $archive{$msg->[$_]->{mid
}} = 1 for (@path);
110 # notice invisble messages
111 # while they are in @path and archive flag is not set,
112 # they and their kids WON'T be archived
114 $hidden{$z} = 1 if ($msg->[$z]->{deleted
});
116 # if 'archive' is NOT set and message not deleted,
118 unless ($msg->[$z]->{archive
} or $msg->[$z]->{deleted
}) {
119 my $key = $sum->{$tid}->{$msg->[$z]->{mid
}};
121 # ...and they've voted enough, it will be archived
123 if ($percent == 0 or ($key->{views
} and ($key->{votings
} * 100 / $key->{views
}) >= $percent)) {
126 # check on hidden messages in @path
135 # set archive-flag for messages in @path,
136 # unless a parent message is hidden
138 unless ($hidden_in_path) {
139 $archive{$msg->[$_]->{mid
}} = 1 for (@path);
145 # now remove messages without 'archive'-flag
148 for (reverse grep {!$archive{$_->{mid
}}} @
$msg) {
149 my $h = get_message_node
($xml, "t$tid", 'm'.$_->{mid
});
151 # remove message entry
153 $h -> getParentNode
-> removeChild
($h);
155 # remove message text
157 $h = get_body_node
($xml, 'm'.$_->{mid
});
158 $h -> getParentNode
-> removeChild
($h);
166 ### create_arcdir () ###########################################################
168 # check, if specific directories for year and month exist, create
171 # Params: $path - archive root
172 # $time - Thread time (GMT)
174 # Return: List: $path - /path/to/ to archived thread file
175 # $error - error or undef
177 sub create_arcdir
($$) {
178 my ($path, $time) = @_;
180 my ($month, $year) = (localtime ($time))[4,5];
182 # use the 'real' values for directory names
184 $month++; $year+=1900;
186 my $yeardir = $path . $year;
187 my $monthdir = $yeardir . '/' . $month;
188 my $monthpath = $monthdir . '/';
190 mkdir $yeardir, 0777 unless (-d
$yeardir);
191 return ('', "could not create directory '$yeardir'") unless (-d
$yeardir);
193 mkdir $monthdir, 0777 unless (-d
$monthdir);
194 return ('', "could not create directory '$monthdir'") unless (-d
$monthdir);
196 # return path, successfully created
201 ### process_threads () #########################################################
203 # process obsolete threads
204 # (transmit views/votings from cache, do archive, if necessary)
206 # Params: $par - hash reference
207 # (opt, cache, failed, obsolete, messagePath,
208 # archivePath, adminDefault)
210 # Return: hashref (tid => $msg)
212 sub process_threads
($) {
215 my ($opt, $failed, $obsolete, $cache) = map {$par->{$_}} qw
216 ( opt failed obsolete cache
);
220 if ($opt->{exArchiving
}) {
224 my $sum = $cache -> summary
;
227 # iterate over all obsolete threads, that are not failed yet
229 for my $tid (grep {not exists ($failed->{$_})} @
$obsolete) {
230 my $xml = parse_xml_file
($par->{messagePath
}."t$tid.xml");
235 $failed->{$tid} = 'could not parse thread file.';
240 my $tnode = $xml -> getElementsByTagName
('Thread') -> item
(0);
241 my $msg = parse_single_thread
($tnode, KEEP_DELETED
);
243 if ($opt->{archiving
} eq 'UserVotings') {
245 # filter out the bad stuff
247 delete_no_archived
({
252 percent
=> $par->{adminDefault
}->{Voting
}->{Limit
}
256 # save back xml file (into archive)
258 if ($tnode -> hasChildNodes
) {
260 # insert views and votings counter
262 for ($tnode -> getElementsByTagName
('Message')) {
263 my ($id) = $_ -> getAttribute
('id') =~ /(\d+)/;
264 $_ -> setAttribute
('views' => $sum->{$tid}->{$id}->{views
});
265 $_ -> setAttribute
('votings' => $sum->{$tid}->{$id}->{votings
});
268 # create archive dir, unless exists
270 my ($path, $error) = create_arcdir
($par -> {archivePath
}, $msg->[0]->{time});
273 $failed->{$tid} = $error;
278 my $file = "${path}t$tid.xml";
279 unless (save_file
($file => \
($xml -> toString
))) {
280 $failed->{$tid} = "could not save '$file'";
283 $archived{$tid} = $msg
291 @
$failed{@
$obsolete} = 'error: could not load summary';
298 ### append_threads () ##########################################################
300 # open specified index file, append threads, save it back
302 # Params: $file - /path/to/indexfile
303 # $threads - hashref (threads)
305 # Return: success code (boolean)
307 sub append_threads
($$) {
308 my ($file, $threads) = @_;
311 my $index = new Lock
($file);
313 return unless ($index -> lock (LH_EXCL
));
316 $thash = get_all_threads
($file => KEEP_DELETED
);
317 $thash->{$_} = $threads->{$_} for (keys %$threads);
325 my $saved = save_file
(
326 $file => create_forum_xml_string
(
338 return unless $saved;
343 ### indexpath () ###############################################################
345 # compose relative path of archive index file
347 # Params: $param - hash reference
350 # Return: $string (relative path)
355 my ($month, $year) = (localtime ($root->{time}))[4,5];
357 # use the 'real' values for directory names
359 $month++; $year+=1900;
364 ### index_threads () ###########################################################
366 # add threads to their specific archive index file
368 # Params: $param - hash reference
369 # (threads, archivePath, archiveIndex, failed)
373 sub index_threads
($) {
376 my ($threads, $failed) = map {$par->{$_}} qw
379 # indexfile => hashref of threads
380 # for more efficiency (open each index file *once*)
384 # iterate over all archived threads,
385 # prepare indexing and assign threads to indexfiles
387 for my $thread (keys %$threads) {
389 # index only, if the root is visible
391 unless ($threads->{$thread}->[0]->{deleted
}) {
392 my $file = $par->{archivePath
} . indexpath
($threads->{$thread}->[0]) . $par->{archiveIndex
};
393 $index{$file} = {} unless exists($index{$file});
395 $index{$file} -> {$thread} = [$threads->{$thread}->[0]];
399 # now append threads to index files
401 for my $file (keys %index) {
402 unless (append_threads
($file => $index{$file})) {
403 $failed->{$_} = "error: could not list in '$file'" for (keys %{$index{$file}});
408 ### cut_tail () ################################################################
410 # shorten the main file and archive, if necessary
412 # Params: $param - hash reference
413 # (forumFile, messagePath, archivePath, lockFile, adminDefault,
414 # cachePath, archiveIndex)
416 # Return: hash reference - empty if all right done
423 $param->{adminDefault
}->{Severance
}->{severance
} ne 'instant' or
424 $param->{adminDefault
}->{Instant
}->{execute
}
426 # run only one instance at the same time
427 # (exlusive lock on sev_lock)
429 my $sev = new Lock
($param->{lockFile
});
430 if ($sev -> lock(LH_EXCL
)) {
432 # lock and parse forum main file
434 my $forum = new Lock
($param->{forumFile
});
435 if ($forum -> lock (LH_EXCL
)) {
441 ) = get_all_threads
($forum->filename, KEEP_DELETED
);
443 # get obsolete threads...
445 my $obsolete = get_obsolete_threads
({
446 parsedThreads
=> $threads,
447 adminDefault
=> $param->{adminDefault
}
450 unless (@
$obsolete) {
451 # nothing to cut - we're ready
456 # ...and delete them from main
460 $obsolete{$_} = $threads->{$_};
461 delete $threads->{$_};
466 my $saved = save_file
(
467 $param -> {forumFile
},
468 create_forum_xml_string
(
472 lastMessage
=> $last_message,
473 lastThread
=> $last_thread
478 # ...and masterlock the obsolete thread files
482 new Lock
($param->{messagePath
}."t$_.xml")->lock(LH_MASTER
) or $failed{$_} = 'error: could not set master lock';
486 # release forum main file...
491 # ...and now process thread files
493 my $sev_opt = ($param -> {adminDefault
} -> {Severance
} -> {severance
} eq 'instant')
494 ?
$param -> {adminDefault
} -> {Instant
} -> {Severance
}
495 : ($param -> {adminDefault
} -> {Severance
});
497 my $cache = new Posting
::Cache
($param->{cachePath
});
503 obsolete
=> $obsolete,
504 messagePath
=> $param->{messagePath
},
505 archivePath
=> $param->{archivePath
},
506 adminDefault
=> $param->{adminDefault
}
509 # delete processed files, that are not failed
513 for (grep {not exists($failed{$_})} @
$obsolete) {
514 if (exists($failed{$_})) {
515 delete $obsolete{$_};
518 unless (unlink ($param->{messagePath
}."t$_.xml")) {
519 $failed{$_} = 'warning: could not delete thread file';
523 #file_removed ($param->{messagePath}."t$_.xml");
529 $cache -> delete_threads
(@removed);
530 $cache -> garbage_collection
;
533 # add archived threads to archive index
536 threads
=> \
%obsolete,
537 archivePath
=> $param->{archivePath
},
538 archiveIndex
=> $param->{archiveIndex
},
544 # we're ready, tell this other (waiting?) instances
555 # keep 'require' happy
560 ### end of Arc::Archive ########################################################