]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Posting/Write.pm
1 package Posting
::Write
;
3 ################################################################################
5 # File: shared/Posting/Write.pm #
7 # Authors: André Malo <nd@o3media.de>, 2001-04-08 #
9 # Description: Save a posting #
11 ################################################################################
14 use vars
qw(%error @EXPORT);
16 use Encode::Plain; $Encode::Plain::utf8 = 1;
25 create_forum_xml_string
36 threadWrite
=> '1 could not write thread file',
37 forumWrite
=> '2 could not write forum file',
38 threadFile
=> '3 could not load thread file',
39 noParent
=> '4 could not find parent message'
42 ################################################################################
46 use base
qw(Exporter);
52 ### sub write_new_thread ($) ###################################################
54 # save a posting and update the forum main file
56 # Params: $param - hashreference
57 # (see doc for details)
59 # Return: (0 or error, thread-xml, new mid)
61 sub write_new_thread
($) {
64 my $mid = 'm'.($param -> {lastMessage
} + 1);
65 my $tid = 't'.($param -> {lastThread
} + 1);
67 # define the params needed for a new thread
72 name
=> defined $param -> {author
} ?
$param -> {author
} : '',
73 email
=> defined $param -> {email
} ?
$param -> {email
} : '',
74 home
=> defined $param -> {homepage
} ?
$param -> {homepage
} : '',
75 image
=> defined $param -> {image
} ?
$param -> {image
} : '',
76 category
=> defined $param -> {category
} ?
$param -> {category
} : '',
77 subject
=> defined $param -> {subject
} ?
$param -> {subject
} : '',
80 { quoteChars
=> $param -> {quoteChars
},
81 messages
=> $param -> {messages
},
82 base_uri
=> $param -> {base_uri
}
85 time => $param -> {time},
86 dtd
=> $param -> {dtd
},
90 # create new thread and save it to disk
92 $thread = create_new_thread
($pars);
93 save_file
($param -> {messagePath
}.$tid.'.xml',\
($thread -> toString
)) or return $error{threadWrite
};
95 # update forum main file
99 -> {$param -> {lastThread
} + 1} = [
100 { mid
=> $param -> {lastMessage
} + 1,
101 unid
=> $param -> {uniqueID
},
102 name
=> plain
($pars -> {name
}),
103 cat
=> plain
($pars -> {category
}),
104 subject
=> plain
($pars -> {subject
}),
105 time => plain
($pars -> {time}),
110 my $forum = create_forum_xml_string
(
111 $param -> {parsedThreads
},
112 { dtd
=> $pars -> {dtd
},
118 save_file
($param -> {forumFile
}, $forum) or return $error{forumWrite
};
119 release_file
($param -> {messagePath
}.$tid.'.xml');
120 return (0, $thread, $mid, $tid);
123 ### sub write_reply_posting ($) ################################################
125 # save a reply and update the forum main file
127 # Params: $param - hashreference
128 # (see doc for details)
130 # Return: (0 or error, thread-xml, new mid)
132 sub write_reply_posting
($) {
135 my $mid = 'm'.($param -> {lastMessage
} + 1);
136 my $tid = 't'.($param -> {thread
});
138 my $tfile = $param -> {messagePath
}.$tid.'.xml';
140 unless (write_lock_file
($tfile)) {
141 violent_unlock_file
($tfile);
142 return $error{threadFile
};
146 my $xml = parse_xml_file
($tfile);
149 violent_unlock_file
($tfile) unless (write_unlock_file
($tfile));
150 return $error{threadFile
};
153 my $mnode = get_message_node
($xml, $tid, 'm'.$param -> {parentMessage
});
155 unless (defined $mnode) {
156 violent_unlock_file
($tfile) unless (write_unlock_file
($tfile));
157 return $error{noParent
};
162 ip
=> $param -> {ip
},
163 name
=> defined $param -> {author
} ?
$param -> {author
} :'',
164 email
=> defined $param -> {email
} ?
$param -> {email
} :'',
165 home
=> defined $param -> {homepage
} ?
$param -> {homepage
} :'',
166 image
=> defined $param -> {image
} ?
$param -> {image
} :'',
167 category
=> defined $param -> {category
} ?
$param -> {category
} :'',
168 subject
=> defined $param -> {subject
} ?
$param -> {subject
} :'',
169 time => $param -> {time},
172 my $message = create_message
($xml, $pars);
174 $mnode -> appendChild
($message);
176 my $mcontent = $xml -> createElement
('MessageContent');
177 $mcontent -> setAttribute
('mid' => $mid);
178 $mcontent -> appendChild
(
179 $xml -> createCDATASection
(
182 { quoteChars
=> $param -> {quoteChars
},
183 messages
=> $param -> {messages
},
184 base_uri
=> $param -> {base_uri
}
190 my $content = $xml -> getElementsByTagName
('ContentList', 1) -> item
(0);
191 $content -> appendChild
($mcontent);
195 unless (save_file
($tfile, \
($xml -> toString
))) {
196 violent_unlock_file
($tfile) unless (write_unlock_file
($tfile));
197 return $error{threadWrite
};
200 violent_unlock_file
($tfile) unless (write_unlock_file
($tfile));
204 # add message to thread tree
205 # ATTENTION: don't use the tree for visual output after this operation
208 for (@
{$param -> {parsedThreads
} -> {$param -> {thread
}}}) {
209 if ($_ -> {mid
} == $param -> {parentMessage
}) {
211 $param -> {parsedThreads
} -> {$param -> {thread
}}},$i, 0,
212 { mid
=> $param -> {lastMessage
} + 1,
213 unid
=> plain
($param -> {uniqueID
}),
214 name
=> plain
($pars -> {name
}),
215 cat
=> plain
($pars -> {category
}),
216 subject
=> plain
($pars -> {subject
}),
217 level
=> $_ -> {level
} + 1,
218 time => plain
($pars -> {time})
225 # create & save forum main file
227 my $forum = create_forum_xml_string
(
228 $param -> {parsedThreads
},
229 { dtd
=> $param -> {dtd
},
231 lastThread
=> 't'.$param -> {lastThread
}
235 save_file
($param -> {forumFile
}, $forum) or return $error{forumWrite
};
238 return (0, $thread, $mid, $tid);
241 # keep 'require' happy
247 ### end of Posting::Write ######################################################
patrick-canterino.de