]> git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/Handle.pm
_lib.pm: style changes
[selfforum.git] / selfforum-cgi / shared / Posting / Handle.pm
1 package Posting::Handle;
2
3 ################################################################################
4 # #
5 # File: shared/Posting/Handle.pm #
6 # #
7 # Authors: Frank Schoenmann <fs@tower.de>, 2001-03-13 #
8 # #
9 # Description: Allow modifications of postings #
10 # #
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() #
15 # #
16 ################################################################################
17
18 use strict;
19
20 use vars qw(@EXPORT);
21 use base qw(Exporter);
22
23 @EXPORT = qw(hide_posting recover_posting modify_posting add_user_vote level_vote);
24
25 use Lock qw(:READ);
26 use Posting::_lib qw(get_message_node save_file get_all_threads
27 create_forum_xml_string);
28
29 use XML::DOM;
30
31 ### add_user_vote () ###########################################################
32 #
33 # Increase number of user votes (only in thread file)
34 #
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)
39 #
40 # Todo:
41 # * Lock files before modification
42 #
43 sub add_user_vote()
44 {
45 my ($forum, $tpath, $info) = @_;
46 my ($tid, $mid, $percent) = ($info->{'thread'},
47 $info->{'posting'},
48 $info->{'percent'});
49
50 # Thread
51 my $tfile = $tpath . '/t' . $tid . '.xml';
52
53 my $parser = new XML::DOM::Parser;
54 my $xml = $parser->parsefile($tfile);
55
56 my $mnode = get_message_node($xml, $tid, $mid);
57 my $votes = $mnode->getAttribute('votingUser') + 1;
58 $mnode->setAttribute('votingUser', $votes);
59
60 return save_file($tfile, \$xml->toString);
61 }
62
63 ### level_vote () ##############################################################
64 #
65 # Set 1st or 2nd level voting (only in thread file)
66 #
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)
71 #
72 # Todo:
73 # * Lock files before modification
74 #
75 sub level_vote
76 {
77 my ($forum, $tpath, $infoยด) = @_;
78 my ($tid, $mid, $level, $value) = ($info->{'thread'},
79 $info->{'posting'},
80 $info->{'level'},
81 $info->{'value'});
82
83 # Thread
84 my $tfile = $tpath . '/t' . $tid . '.xml';
85
86 my $parser = new XML::DOM::Parser;
87 my $xml = $parser->parsefile($tfile);
88
89 my $mnode = get_message_node($xml, $tid, $mid);
90
91 if ($value == undef)
92 {
93 removeAttribute($level);
94 }
95 else
96 {
97 $mnode->setAttribute($level, $value);
98 }
99
100 return save_file($tfile, \$xml->toString);
101 }
102
103 ### hide_posting () ############################################################
104 #
105 # Hide a posting: set 'invisible' flag
106 #
107 # Params: $forum Path and filename of forum
108 # $tpath Path to thread files
109 # \%info Hash reference: 'thread', 'posting', 'indexFile'
110 # Return: -none-
111 #
112 # Todo:
113 # * set flags recursively in forum xml
114 # * lock files before modification
115 #
116 sub hide_posting($$$)
117 {
118 my ($forum, $tpath, $info) = @_;
119 my ($tid, $mid, $indexFile) = ($info->{'thread'},
120 $info->{'posting'},
121 $info->{'indexFile'});
122
123 # Thread
124 my $tfile = $tpath . '/t' . $tid . '.xml';
125 change_posting_visibility($tfile, 't'.$tid, 'm'.$mid, 1);
126
127 # Forum
128 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 0, 0); # filter deleted, descending
129
130 for (@{$f->{$tid}})
131 {
132 if ($_->{'mid'} == $mid)
133 {
134 $_->{'deleted'} = 1;
135 }
136 }
137
138 my %cfxs = (
139 'dtd' => $dtd,
140 'lastMessage' => $lmsg,
141 'lastThread' => $lthread
142 );
143 my $xmlstring = create_forum_xml_string($f, \%cfxs);
144 save_file($forum, $$xmlstring);
145 }
146
147 ### recover_posting() ##########################################################
148 #
149 # Recover a posting: delete 'invisible' flag
150 #
151 # Params: $forum Path and filename of forum
152 # $tpath Path to thread files
153 # \%info Hash reference: 'thread', 'posting', 'indexFile'
154 # Return: -none-
155 #
156 # Todo:
157 # * set flags recursive in forum xml
158 # * lock files before modification
159 #
160 sub recover_posting($$$)
161 {
162 my ($forum, $tpath, $info) = @_;
163 my ($tid, $mid, $indexFile) = ($info->{'thread'},
164 $info->{'posting'},
165 $info->{'indexFile'});
166
167 # Thread
168 my $tfile = $tpath . '/t' . $tid . '.xml';
169 change_posting_visibility($tfile, 't'.$tid, 'm'.$mid, 0);
170
171 # Forum
172 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 1, 0); # do not filter deleted, descending
173
174 for (@{$f->{$tid}})
175 {
176 if ($_->{'mid'} == $mid)
177 {
178 $_->{'deleted'} = 0;
179 }
180 }
181
182 my %cfxs = (
183 'dtd' => $dtd,
184 'lastMessage' => $lmsg,
185 'lastThread' => $lthread
186 );
187 my $xmlstring = create_forum_xml_string($f, \%cfxs);
188 save_file($forum, $$xmlstring);
189 }
190
191 ### change_posting_visibility () ###############################################
192 #
193 # Set a postings visibility flag to $invisible
194 #
195 # Params: $fname Filename
196 # $tid Thread ID
197 # $mid Message ID
198 # $invisible 1 - invisible, 0 - visible
199 # Return: Status code
200 #
201 sub change_posting_visibility($$$$)
202 {
203 my ($fname, $tid, $mid, $invisible) = @_;
204
205 my $parser = new XML::DOM::Parser;
206 my $xml = $parser->parsefile($fname);
207
208 # Set flag in given msg
209 my $mnode = get_message_node($xml, $tid, $mid);
210 $mnode->setAttribute('invisible', $invisible);
211
212 # Set flag in sub nodes
213 for ($mnode->getElementsByTagName('Message'))
214 {
215 $_->setAttribute('invisible', $invisible);
216 }
217
218 return save_file($fname, \$xml->toString);
219 }
220
221 ### modify_posting () ##########################################################
222 #
223 # Modify a posting (only subject and category until now!)
224 #
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')
229 # Return: -none-
230 #
231 sub modify_posting($$$)
232 {
233 my ($forum, $tpath, $info) = @_;
234 my ($tid, $mid, $indexFile, $data) = ($info->{'thread'},
235 $info->{'posting'},
236 $info->{'indexFile'},
237 $info->{'data'});
238 my ($subject, $category, $body) = ($data->{'subject'}, $data->{'category'}, $data->{'body'});
239
240 my %msgdata;
241
242 # These values may be changed by change_posting_value()
243 $subject && $msgdata{'Subject'} = $subject;
244 $category && $msgdata{'Category'} = $category;
245
246 # Thread
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);
250
251 # Forum (does not contain msg bodies)
252 if ($subject or $category)
253 {
254 my ($f, $lthread, $lmsg, $dtd, $zlev) = get_all_threads($forum, 1, 0);
255
256 for (@{$f->{$tid}})
257 {
258 if ($_->{'mid'} == $mid)
259 {
260 $subject && $_->{'subject'} = $subject;
261 $category && $_->{'cat'} = $category;
262 }
263 }
264
265 my %cfxs = (
266 'dtd' => $dtd,
267 'lastMessage' => $lmsg,
268 'lastThread' => $lthread
269 );
270 my $xmlstring = create_forum_xml_string($f, \%cfxs);
271 save_file($forum, $$xmlstring);
272 }
273
274 ### change_posting_value () ####################################################
275 #
276 # Change specific values of a posting
277 #
278 # Params: $fname Filename
279 # $tid Thread ID
280 # $mid Message ID
281 # \%values New values
282 # Return: Status code
283 #
284 sub change_posting_value($$$$)
285 {
286 my ($fname, $tid, $mid, $values) = @_;
287
288 my $parser = new XML::DOM::Parser;
289 my $xml = $parser->parsefile($fname);
290
291 my $mnode = get_message_node($xml, $tid, $mid);
292
293 for (keys %$values)
294 {
295 # Find first direct child node with name $_
296 my $nodes = $mnode->getElementsByTagName($_, 0);
297 my $node = $nodes->item(0);
298 $node->setValue($values->{$_});
299 }
300
301 return save_file($fname, \$xml->toString);
302 }
303
304 ### change_posting_body () #####################################################
305 #
306 # Change body of a posting
307 #
308 # Params: $fname Filename
309 # $tid Thread ID (unused, for compatibility purposes)
310 # $mid Message ID
311 # $body New body
312 # Return: Status code
313 #
314 # Todo:
315 # * Change body
316 #
317 sub change_posting_body($$$$)
318 {
319 my ($fname, $tid, $mid, $body) = @_;
320
321 my $parser = new XML::DOM::Parser;
322 my $xml = $parser->parsefile($fname);
323
324 my $mbnody = get_message_body($xml, $mid);
325
326 # todo: change body
327
328 return save_file($fname, \$xml->toString);
329 }
330
331
332 # Let it be true
333 1;

patrick-canterino.de