]> git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/Write.pm
modified version check
[selfforum.git] / selfforum-cgi / shared / Posting / Write.pm
1 package Posting::Write;
2
3 ################################################################################
4 # #
5 # File: shared/Posting/Write.pm #
6 # #
7 # Authors: André Malo <nd@o3media.de>, 2001-04-08 #
8 # #
9 # Description: Save a posting #
10 # #
11 ################################################################################
12
13 use strict;
14 use vars qw(
15 %error
16 @EXPORT
17 $VERSION
18 );
19
20 use Encode::Plain; $Encode::Plain::utf8 = 1;
21 use Encode::Posting;
22 use Lock;
23 use Posting::_lib qw(
24 get_message_node
25 get_message_header
26 create_forum_xml_string
27 create_new_thread
28 create_message
29 save_file
30 parse_xml_file
31 KEEP_DELETED
32 );
33
34 use XML::DOM;
35
36 %error = (
37 threadWrite => '1 could not write thread file',
38 forumWrite => '2 could not write forum file',
39 threadFile => '3 could not load thread file',
40 noParent => '4 could not find parent message'
41 );
42
43 ################################################################################
44 #
45 # Version check
46 #
47 $VERSION = do { my @r =(q$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
48
49 ################################################################################
50 #
51 # Export
52 #
53 use base qw(Exporter);
54 @EXPORT = qw(
55 write_new_thread
56 write_reply_posting
57 );
58
59 ### sub write_new_thread ($) ###################################################
60 #
61 # save a posting and update the forum main file
62 #
63 # Params: $param - hashreference
64 # (see doc for details)
65 #
66 # Return: (0 or error, thread-xml, new mid)
67 #
68 sub write_new_thread ($) {
69 my $param = shift;
70 my $thread;
71 my $mid = 'm'.($param -> {lastMessage} + 1);
72 my $tid = 't'.($param -> {lastThread} + 1);
73
74 # define the params needed for a new thread
75 #
76 my $pars = {
77 msg => $mid,
78 ip => $param -> {ip},
79 name => defined $param -> {author} ? $param -> {author} : '',
80 email => defined $param -> {email} ? $param -> {email} : '',
81 home => defined $param -> {homepage} ? $param -> {homepage} : '',
82 image => defined $param -> {image} ? $param -> {image} : '',
83 category => defined $param -> {category} ? $param -> {category} : '',
84 subject => defined $param -> {subject} ? $param -> {subject} : '',
85 body => encoded_body(
86 \($param -> {body}),
87 { quoteChars => $param -> {quoteChars},
88 messages => $param -> {messages},
89 base_uri => $param -> {base_uri}
90 }
91 ),
92 time => $param -> {time},
93 dtd => $param -> {dtd},
94 thread => $tid
95 };
96
97 # create new thread and save it to disk
98 #
99 $thread = create_new_thread ($pars);
100 save_file ($param -> {messagePath}.$tid.'.xml',\($thread -> toString)) or return $error{threadWrite};
101
102 # update forum main file
103 #
104 $param
105 -> {parsedThreads}
106 -> {$param -> {lastThread} + 1} = [
107 { mid => $param -> {lastMessage} + 1,
108 unid => $param -> {uniqueID},
109 name => plain($pars -> {name}),
110 cat => plain($pars -> {category}),
111 subject => plain($pars -> {subject}),
112 time => plain($pars -> {time}),
113 level => 0,
114 }
115 ];
116
117 my $forum = create_forum_xml_string (
118 $param -> {parsedThreads},
119 { dtd => $pars -> {dtd},
120 lastMessage => $mid,
121 lastThread => $tid
122 }
123 );
124
125 save_file ($param -> {forumFile}, $forum) or return $error{forumWrite};
126 new Lock ($param -> {messagePath}.$tid.'.xml') -> release;
127 return (0, $thread, $mid, $tid);
128 }
129
130 ### sub write_reply_posting ($) ################################################
131 #
132 # save a reply and update the forum main file
133 #
134 # Params: $param - hashreference
135 # (see doc for details)
136 #
137 # Return: (0 or error, thread-xml, new mid)
138 #
139 sub write_reply_posting ($) {
140 my $param = shift;
141 my $thread;
142 my $mid = 'm'.($param -> {lastMessage} + 1);
143 my $tid = 't'.($param -> {thread});
144
145 my $tfile = new Lock ($param -> {messagePath}.$tid.'.xml');
146
147 unless ($tfile->lock(LH_EXCL)) {
148 return $error{threadFile};
149 }
150
151 else {
152 my $xml = parse_xml_file ($tfile->filename);
153
154 unless ($xml) {
155 $tfile -> unlock;
156 return $error{threadFile};
157 }
158
159 my $mnode = get_message_node ($xml, $tid, 'm'.$param -> {parentMessage});
160
161 unless (defined $mnode) {
162 $tfile -> unlock;
163 return $error{noParent};
164 }
165
166 my $pars = {
167 msg => $mid,
168 ip => $param -> {ip},
169 name => defined $param -> {author} ? $param -> {author} :'',
170 email => defined $param -> {email} ? $param -> {email} :'',
171 home => defined $param -> {homepage} ? $param -> {homepage} :'',
172 image => defined $param -> {image} ? $param -> {image} :'',
173 category => defined $param -> {category} ? $param -> {category} :'',
174 subject => defined $param -> {subject} ? $param -> {subject} :'',
175 time => $param -> {time},
176 };
177
178 my $message = create_message ($xml, $pars);
179
180 $mnode -> appendChild ($message);
181
182 my $mcontent = $xml -> createElement ('MessageContent');
183 $mcontent -> setAttribute ('mid' => $mid);
184 $mcontent -> appendChild (
185 $xml -> createCDATASection (
186 ${encoded_body(
187 \($param -> {body}),
188 { quoteChars => $param -> {quoteChars},
189 messages => $param -> {messages},
190 base_uri => $param -> {base_uri}
191 }
192 )}
193 )
194 );
195
196 my $content = $xml -> getElementsByTagName ('ContentList', 1) -> item (0);
197 $content -> appendChild ($mcontent);
198
199 # save thread file
200 #
201 unless (save_file ($tfile->filename, \($xml -> toString))) {
202 $tfile -> unlock;
203 return $error{threadWrite};
204 }
205
206 $tfile -> unlock;
207
208 $thread = $xml;
209
210 # add message to thread tree
211 # ATTENTION: don't use the tree for visual output after this operation
212 #
213 my $i=1;
214 for (@{$param -> {parsedThreads} -> {$param -> {thread}}}) {
215 if ($_ -> {mid} == $param -> {parentMessage}) {
216 splice @{
217 $param -> {parsedThreads} -> {$param -> {thread}}},$i, 0,
218 { mid => $param -> {lastMessage} + 1,
219 unid => plain ($param -> {uniqueID}),
220 name => plain ($pars -> {name}),
221 cat => plain ($pars -> {category}),
222 subject => plain ($pars -> {subject}),
223 level => $_ -> {level} + 1,
224 time => plain ($pars -> {time})
225 };
226 last;
227 }
228 $i++;
229 }
230
231 # create & save forum main file
232 #
233 my $forum = create_forum_xml_string (
234 $param -> {parsedThreads},
235 { dtd => $param -> {dtd},
236 lastMessage => $mid,
237 lastThread => 't'.$param -> {lastThread}
238 }
239 );
240
241 save_file ($param -> {forumFile}, $forum) or return $error{forumWrite};
242 }
243
244 return (0, $thread, $mid, $tid);
245 }
246
247 # keep 'require' happy
248 #
249 1;
250
251 #
252 #
253 ### end of Posting::Write ######################################################

patrick-canterino.de