1 package Posting
::Handle
;
3 ################################################################################
5 # File: shared/Posting/Handle.pm #
7 # Authors: Frank Schoenmann <fs@tower.de>, 2001-03-13 #
9 # Description: Allow modifications of postings #
11 # Todo: * Lock files before modification #
12 # * Change body in change_posting_body() #
13 # * Recursively set invisibility flag in main forum xml by #
14 # hide_posting() and recover_posting() #
16 ################################################################################
21 use base qw(Exporter);
23 @EXPORT = qw(hide_posting recover_posting modify_posting add_user_vote level_vote);
26 use Posting
::_lib
qw(get_message_node save_file get_all_threads
27 create_forum_xml_string);
31 ### add_user_vote () ###########################################################
33 # Increase number of user votes (only in thread file)
35 # Params: $forum Path and filename of forum
36 # $tpath Path to thread files
37 # \%info Hash reference: 'thread', 'posting', 'percent'
38 # Return: Status code (Bool)
41 # * Lock files before modification
45 my ($forum, $tpath, $info) = @_;
46 my ($tid, $mid, $percent) = ($info->{'thread'},
51 my $tfile = $tpath . '/t' . $tid . '.xml';
53 my $parser = new XML
::DOM
::Parser
;
54 my $xml = $parser->parsefile($tfile);
56 my $mnode = get_message_node
($xml, $tid, $mid);
57 my $votes = $mnode->getAttribute('votingUser') + 1;
58 $mnode->setAttribute('votingUser', $votes);
60 return save_file
($tfile, \
$xml->toString);
63 ### level_vote () ##############################################################
65 # Set 1st or 2nd level voting (only in thread file)
67 # Params: $forum Path and filename of forum
68 # $tpath Path to thread files
69 # \%info Hash reference: 'thread', 'posting', 'level', 'value'
70 # Return: Status code (Bool)
73 # * Lock files before modification
77 my ($forum, $tpath, $infoยด
) = @_;
78 my ($tid, $mid, $level, $value) = ($info->{'thread'},
84 my $tfile = $tpath . '/t' . $tid . '.xml';
86 my $parser = new XML
::DOM
::Parser
;
87 my $xml = $parser->parsefile($tfile);
89 my $mnode = get_message_node
($xml, $tid, $mid);
93 removeAttribute
($level);
97 $mnode->setAttribute($level, $value);
100 return save_file
($tfile, \
$xml->toString);
103 ### hide_posting () ############################################################
105 # Hide a posting: set 'invisible' flag
107 # Params: $forum Path and filename of forum
108 # $tpath Path to thread files
109 # \%info Hash reference: 'thread', 'posting', 'indexFile'
113 # * set flags recursively in forum xml
114 # * lock files before modification
116 sub hide_posting
($$$)
118 my ($forum, $tpath, $info) = @_;
119 my ($tid, $mid, $indexFile) = ($info->{'thread'},
121 $info->{'indexFile'});
124 my $tfile = $tpath . '/t' . $tid . '.xml';
125 change_posting_visibility
($tfile, 't'.$tid, 'm'.$mid, 1);
128 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads
($forum, 0, 0); # filter deleted, descending
132 if ($_->{'mid'} == $mid)
140 'lastMessage' => $lmsg,
141 'lastThread' => $lthread
143 my $xmlstring = create_forum_xml_string
($f, \
%cfxs);
144 save_file
($forum, $$xmlstring);
147 ### recover_posting() ##########################################################
149 # Recover a posting: delete 'invisible' flag
151 # Params: $forum Path and filename of forum
152 # $tpath Path to thread files
153 # \%info Hash reference: 'thread', 'posting', 'indexFile'
157 # * set flags recursive in forum xml
158 # * lock files before modification
160 sub recover_posting
($$$)
162 my ($forum, $tpath, $info) = @_;
163 my ($tid, $mid, $indexFile) = ($info->{'thread'},
165 $info->{'indexFile'});
168 my $tfile = $tpath . '/t' . $tid . '.xml';
169 change_posting_visibility
($tfile, 't'.$tid, 'm'.$mid, 0);
172 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads
($forum, 1, 0); # do not filter deleted, descending
176 if ($_->{'mid'} == $mid)
184 'lastMessage' => $lmsg,
185 'lastThread' => $lthread
187 my $xmlstring = create_forum_xml_string
($f, \
%cfxs);
188 save_file
($forum, $$xmlstring);
191 ### change_posting_visibility () ###############################################
193 # Set a postings visibility flag to $invisible
195 # Params: $fname Filename
198 # $invisible 1 - invisible, 0 - visible
199 # Return: Status code
201 sub change_posting_visibility
($$$$)
203 my ($fname, $tid, $mid, $invisible) = @_;
205 my $parser = new XML
::DOM
::Parser
;
206 my $xml = $parser->parsefile($fname);
208 # Set flag in given msg
209 my $mnode = get_message_node
($xml, $tid, $mid);
210 $mnode->setAttribute('invisible', $invisible);
212 # Set flag in sub nodes
213 for ($mnode->getElementsByTagName('Message'))
215 $_->setAttribute('invisible', $invisible);
218 return save_file
($fname, \
$xml->toString);
221 ### modify_posting () ##########################################################
223 # Modify a posting (only subject and category until now!)
225 # Params: $forum Path and filename of forum
226 # $tpath Path to thread files
227 # \%info Reference: 'thread', 'posting', 'indexFile', 'data'
228 # (data = \%hashref: 'subject', 'category', 'body')
231 sub modify_posting
($$$)
233 my ($forum, $tpath, $info) = @_;
234 my ($tid, $mid, $indexFile, $data) = ($info->{'thread'},
236 $info->{'indexFile'},
238 my ($subject, $category, $body) = ($data->{'subject'}, $data->{'category'}, $data->{'body'});
242 # These values may be changed by change_posting_value()
243 $subject && $msgdata{'Subject'} = $subject;
244 $category && $msgdata{'Category'} = $category;
247 my $tfile = $tpath . '/t' . $tid . '.xml';
248 change_posting_value
($tfile, 't'.$tid, 'm'.$mid, \
$msgdata);
249 $body && change_posting_body
($tfile, 't'.$tid, 'm'.$mid, $body);
251 # Forum (does not contain msg bodies)
252 if ($subject or $category)
254 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads
($forum, 1, 0);
258 if ($_->{'mid'} == $mid)
260 $subject && $_->{'subject'} = $subject;
261 $category && $_->{'cat'} = $category;
267 'lastMessage' => $lmsg,
268 'lastThread' => $lthread
270 my $xmlstring = create_forum_xml_string
($f, \
%cfxs);
271 save_file
($forum, $$xmlstring);
274 ### change_posting_value () ####################################################
276 # Change specific values of a posting
278 # Params: $fname Filename
281 # \%values New values
282 # Return: Status code
284 sub change_posting_value
($$$$)
286 my ($fname, $tid, $mid, $values) = @_;
288 my $parser = new XML
::DOM
::Parser
;
289 my $xml = $parser->parsefile($fname);
291 my $mnode = get_message_node
($xml, $tid, $mid);
295 # Find first direct child node with name $_
296 my $nodes = $mnode->getElementsByTagName($_, 0);
297 my $node = $nodes->item(0);
298 $node->setValue($values->{$_});
301 return save_file
($fname, \
$xml->toString);
304 ### change_posting_body () #####################################################
306 # Change body of a posting
308 # Params: $fname Filename
309 # $tid Thread ID (unused, for compatibility purposes)
312 # Return: Status code
317 sub change_posting_body
($$$$)
319 my ($fname, $tid, $mid, $body) = @_;
321 my $parser = new XML
::DOM
::Parser
;
322 my $xml = $parser->parsefile($fname);
324 my $mbnody = get_message_body
($xml, $mid);
328 return save_file
($fname, \
$xml->toString);