1 package Posting
::Admin
;
3 ################################################################################
5 # File: shared/Posting/Admin.pm #
8 # Authors: Frank Schönmann <fs@tower.de> #
9 # André Malo <nd@o3media.de> #
10 # Christian Kruse <ckruse@wwwtech.de> #
12 # Description: Allow administration of postings #
14 # Todo: * Lock files before modification #
15 # * Change body in change_posting_body() #
17 ################################################################################
30 create_forum_xml_string
35 ################################################################################
43 sub VERSION
{(q
$Revision$ =~ /([\d.]+)\s*$/)[0] or '0.0'}
45 ################################################################################
49 use base
qw(Exporter);
59 ### add_user_vote () ###########################################################
61 # Increase number of user votes (only in thread file)
63 # Params: $forum Path and filename of forum
64 # $tpath Path to thread files
65 # \%info Hash reference: 'thread', 'posting', 'percent'
66 # Return: Status code (Bool)
69 # * Lock files before modification
71 sub add_user_vote
($$$) {
72 my ($forum, $tpath, $info) = @_;
73 my ($tid, $mid, $percent) = ($info->{'thread'},
78 my $tfile = $tpath . '/t' . $tid . '.xml';
79 my $xml = parse_xml_file
($tfile);
81 my $mnode = get_message_node
($xml, $tid, $mid);
82 my $votes = $mnode->getAttribute('votingUser') + 1;
83 $mnode->setAttribute('votingUser', $votes);
85 return save_file
($tfile, \
$xml->toString);
88 ### level_vote () ##############################################################
90 # Set 1st or 2nd level voting (only in thread file)
92 # Params: $forum Path and filename of forum
93 # $tpath Path to thread files
94 # \%info Hash reference: 'thread', 'posting', 'level', 'value'
95 # Return: Status code (Bool)
98 # * Lock files before modification
101 my ($forum, $tpath, $info) = @_;
102 my ($tid, $mid, $level, $value) = ($info->{'thread'},
108 my $tfile = $tpath . '/t' . $tid . '.xml';
109 my $xml = parse_xml_file
($tfile);
110 my $mnode = get_message_node
($xml, $tid, $mid);
112 unless (defined $value) {
113 removeAttribute
($level);
116 $mnode->setAttribute($level, $value);
119 return save_file
($tfile, \
$xml->toString);
122 ### hide_posting () ############################################################
124 # Hide a posting: set 'invisible' flag
126 # Params: $forum Path and filename of forum
127 # $tpath Path to thread files
128 # \%info Hash reference: 'thread', 'posting'
131 sub hide_posting
($$$) {
132 my ($forum, $tpath, $info) = @_;
133 my ($tid, $mid) = ($info->{'thread'},
137 my $tfile = $tpath . '/t' . $tid . '.xml';
140 my $main = new Lock
$forum;
141 my $tlock = new Lock
$tfile;
143 return unless $tlock->lock(LH_EXCL
); # lock failed
144 unless ($main->lock(LH_EXCL
)) { # lock failed
150 # Change invisibility in the thread file.
152 unless (change_posting_visibility
($tfile, 't'.$tid, 'm'.$mid, 1)) { # saving failed
158 # get all Forum threads
159 my ($f, $lthread, $lmsg,$dtd) = get_all_threads
($forum, 1);
166 # Change invisibility in the main forum index.
168 for my $i (0 .. $#{$f->{$tid}}) {
169 if ($f->{$tid}->[$i]->{'mid'} == $mid) {
170 $f->{$tid}->[$_]->{'deleted'} = 1 for ($i .. $i+$f->{$tid}->[$i]->{'answers'});
175 my $success = save_file
($forum,
176 create_forum_xml_string
($f,
179 'lastMessage' => $lmsg,
180 'lastThread' => $lthread
190 ### recover_posting() ##########################################################
192 # Recover a posting: delete 'invisible' flag
194 # Params: $forum Path and filename of forum
195 # $tpath Path to thread files
196 # \%info Hash reference: 'thread', 'posting'
197 # Return: success or unsuccess
199 sub recover_posting
($$$) {
200 my ($forum, $tpath, $info) = @_;
201 my ($tid, $mid) = ($info->{'thread'},
205 my $tfile = $tpath . '/t' . $tid . '.xml';
208 my $main = new Lock
$forum;
209 my $tlock = new Lock
$tfile;
211 return unless $tlock->lock(LH_EXCL
); # lock failed
212 unless ($main->lock(LH_EXCL
)) { # lock failed
218 # Change invisibility in the thread file.
220 unless (change_posting_visibility
($tfile, 't'.$tid, 'm'.$mid, 0)) { # saving failed
226 # get all Forum threads
227 my ($f, $lthread, $lmsg,$dtd) = get_all_threads
($forum,1);
237 # Change invisibility in the main forum index.
239 for my $i (0 .. $#{$f->{$tid}}) {
240 if ($f->{$tid}->[$i]->{'mid'} == $mid) {
241 $f->{$tid}->[$_]->{'deleted'} = 0 for ($i .. $i+$f->{$tid}->[$i]->{'answers'});
246 my $success = save_file
($forum,
247 create_forum_xml_string
($f,
250 'lastMessage' => $lmsg,
251 'lastThread' => $lthread
261 ### change_posting_visibility () ###############################################
263 # Set a postings visibility flag to $invisible
265 # Params: $fname Filename
268 # $invisible 1 - invisible, 0 - visible
269 # Return: Status code
271 sub change_posting_visibility
($$$$) {
272 my ($fname, $tid, $mid, $invisible) = @_;
274 my $xml = parse_xml_file
($fname);
275 return unless $xml; # parser failed
277 # Set flag in given msg
278 my $mnode = get_message_node
($xml, $tid, $mid);
279 $mnode->setAttribute('invisible', $invisible);
281 # Set flag in sub nodes
282 $_->setAttribute('invisible', $invisible) foreach $mnode->getElementsByTagName('Message');
284 return save_file
($fname, \
$xml->toString);
287 ### modify_posting () ##########################################################
289 # Modify a posting (only subject and category until now!)
291 # Params: $forum Path and filename of forum
292 # $tpath Path to thread files
293 # \%info Reference: 'thread', 'posting', 'indexFile', 'data'
294 # (data = \%hashref: 'subject', 'category', 'body')
299 # * save return values
301 sub modify_posting
($$$) {
302 my ($forum, $tpath, $info) = @_;
303 my ($tid, $mid, $indexFile, $data) = (
306 $info->{'indexFile'},
310 my ($subject, $category, $body) = (
318 # These values may be changed by change_posting_value()
319 $msgdata{'Subject'} = $subject if $subject;
320 $msgdata{'Category'} = $category if $category;
323 my $tfile = $tpath . '/t' . $tid . '.xml';
324 change_posting_value
($tfile, 't'.$tid, 'm'.$mid, \
%msgdata);
325 change_posting_body
($tfile, 't'.$tid, 'm'.$mid, $body) if $body;
327 # Forum (does not contain msg bodies)
328 if ($subject or $category) {
329 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads
($forum, 1, 0);
331 for (@
{$f->{$tid}}) {
332 if ($_->{'mid'} == $mid) {
333 $_->{'subject'} = $subject if $subject;
334 $_->{'cat'} = $category if $category;
338 save_file
($forum, create_forum_xml_string
($f,{dtd
=>$dtd,lastMessage
=>$lmsg,lastThread
$lthread}));
343 ### change_posting_value () ####################################################
345 # Change specific values of a posting
347 # Params: $fname Filename
350 # \%values New values
351 # Return: Status code
353 sub change_posting_value
($$$$) {
354 my ($fname, $tid, $mid, $values) = @_;
356 my $xml = parse_xml_file
($fname);
357 my $mnode = get_message_node
($xml, $tid, $mid);
359 for (keys %$values) {
360 # Find first direct child node with name $_
361 my $nodes = $mnode->getElementsByTagName($_, 0);
362 my $node = $nodes->item(0);
363 $node->setValue($values->{$_});
366 return save_file
($fname, \
$xml->toString);
369 ### change_posting_body () #####################################################
371 # Change body of a posting
373 # Params: $fname Filename
374 # $tid Thread ID (unused, for compatibility purposes)
377 # Return: Status code
382 sub change_posting_body
($$$$) {
383 my ($fname, $tid, $mid, $body) = @_;
385 my $xml = parse_xml_file
($fname);
386 my $mbnody = get_message_body
($xml, $mid);
390 return save_file
($fname, \
$xml->toString);
399 ### end of Posting::Admin ######################################################