TABOO/Data/Comment.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 128 CC 7 SCAR 21.8
CC
7
Cov
0%
reply
20.0
timestamp
20.0
new
2.0
CRAP 8.9
1 package AxKit::App::TABOO::Data::Comment;
2 use strict;
3 use warnings;
4 use Carp;
5  
6 use Data::Dumper;
7 use AxKit::App::TABOO::Data;
8 use AxKit::App::TABOO::Data::User;
9 use AxKit::App::TABOO::Data::Plurals::Comments;
10 use Encode;
11  
12 use vars qw/@ISA/;
13 @ISA = qw(AxKit::App::TABOO::Data);
14  
15 use DBI;
16  
17  
18 our $VERSION = '0.095';
19  
20  
21  
22 =head1 NAME
23  
24 AxKit::App::TABOO::Data::Comment - Comment Data object for TABOO
25  
26 =head1 SYNOPSIS
27  
28 use AxKit::App::TABOO::Data::Comment;
29 $comment = AxKit::App::TABOO::Data::Comment->new();
30 $comment->load(limit => {sectionid => $self->{section},
31 storyname => $self->{storyname}});
32 $comment->tree;
33 $comment->adduserinfo;
34 $timestamp = $comment->timestamp();
35  
36 =head1 DESCRIPTION
37  
38 This Data class contains a comment, which may be posted by any
39 registered user of the site. Each object will also contain an
40 identifier of replies to the comment, that may be replaced with a
41 reference to another comment object.
42  
43 =head1 METHODS
44  
45 This class implements several methods, reimplements the load method,
46 but inherits some from L<AxKit::App::TABOO::Data>.
47  
48 =over
49  
50 =item C<new($self->dbconnectargs())>
51  
52 The constructor. Nothing special.
53  
54 =cut
55  
56 AxKit::App::TABOO::Data::Comment->dbtable("comments");
57 AxKit::App::TABOO::Data::Comment->dbfrom("comments");
58 AxKit::App::TABOO::Data::Comment->dbprimkey("commentpath");
59 AxKit::App::TABOO::Data::Comment->elementneedsparse("content");
60 AxKit::App::TABOO::Data::Comment->elementorder("commentpath, title, content, timestamp, USER, REPLIES");
61  
62 sub new {
63 my $that = shift;
64 my $class = ref($that) || $that;
65 my $self = {
66 commentpath => undef,
67 storyname => undef,
68 sectionid => undef,
69 title => undef,
70 content => undef,
71 timestamp => undef,
72 username => undef,
73 USER => undef,
74 REPLIES => undef,
75 DBCONNECTARGS => \@_,
76 XMLELEMENT => 'reply',
77 XMLPREFIX => 'comm',
78 XMLNS => 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment/Output',
79 ONFILE => undef,
80  
81 };
82 bless($self, $class);
83 return $self;
84 }
85  
86 =item C<load(what =E<gt> fields, limit =E<gt> {key =E<gt> value, [...]}>
87  
88 The load method is not reimplemented but needs elaboration. It follows
89 the convention of the load methods of the parent class, but to
90 uniquely identify a comment, one has to set certain C<limit>s.
91  
92 First one should identify the story that the comment is attached to,
93 by giving C<storyname> and C<sectionid>, see
94 L<AxKit::App::TABOO::Data::Story> for details.
95  
96 To identify the comment itself, TABOO introduces the concept of a
97 I<commentpath>. A commentpath is a string that identifies a comment by
98 appending the username of the poster for each reply posted, separated
99 by a C</>. In computer science terms, I think this is known as a
100 I<trie>. Thus, commentpaths will grow as people respond to each
101 other's comments. For example, if user bar replies to user foo, the
102 commentpath to bar's comment will be C</foo/bar>. The commenpath will
103 typically be in the URI of a comment. If the same user post more
104 replies to a comment, they will be suffixed with e.g. C<_2> for the
105 second comment.
106  
107 The C<commentpath>, C<sectionid> and C<storyname> together identifies
108 a comment.
109  
110  
111 =item C<adduserinfo()>
112  
113 When data has been loaded into an object of this class, it will
114 contain a string only identifying the user who posted the comment.
115 This method will replace that strings with a reference to a
116 L<AxKit::App::TABOO::Data::User>-object, containing the needed user
117 information.
118  
119 =cut
120  
121 sub adduserinfo {
122 my $self = shift;
123 my $user = AxKit::App::TABOO::Data::User->new($self->dbconnectargs());
124 $user->load(what => 'username,name', limit => {username => ${$self}{'username'}});
125 ${$self}{'USER'} = $user;
126 return $self;
127 }
128  
129  
130 =item C<reply($comment)>
131  
132 This method can be used to attach a reply to a comment, or to retrieve
133 a reply. If no argument is given, it will return the reply object if
134 it exists. To attach a comment, give an argument which is an instance
135 of this class or an instance of
136 L<AxKit::App::TABOO::Data::Plurals::Comments>.
137  
138 =cut
139  
140 sub reply {
141 my $self = shift;
142 if (@_) {
143 my $comments = shift;
144 return undef unless (defined($comments));
145 croak "The reply object must be a Comment or Comments object"
146 unless ((ref($comments) eq 'AxKit::App::TABOO::Data::Comment') ||
147 (ref($comments) eq 'AxKit::App::TABOO::Data::Plurals::Comments'));
148 ${$self}{'REPLIES'} = $comments;
149 return $self;
150 } else {
151 return ${$self}{'REPLIES'};
152 }
153 }
154  
155 =item C<tree([$what, $orderby])>
156  
157 This method has changed considerably since earlier releases. You may
158 call it on any object of this class that has C<commentpath>,
159 C<sectionid> and C<storyname> defined. It will return an instance of
160 the L<AxKit::App::TABOO::Data::Plurals::Comments> class consisting of
161 all comments in the tree with the comment in the object it was called
162 on as root.
163  
164 =cut
165  
166 sub tree {
167 my $self = shift;
168 my $what = shift;
169 my $orderby = shift;
170 my $comments = AxKit::App::TABOO::Data::Plurals::Comments->new($self->dbconnectargs());
171 return $comments->load(what => $what,
172 limit => {commentpath => ${$self}{'commentpath'},
173 sectionid => ${$self}{'sectionid'},
174 storyname=> ${$self}{'storyname'}},
175 orderby => $orderby, regex => ['commentpath']);
176 }
177  
178  
179 =item C<timestamp([($sectionid, $storyname, $commentpath)|Time::Piece])>
180  
181 The timestamp method will retrieve or set the timestamp of the
182 comment. If the timestamp has been loaded earlier from the data
183 storage (for example by the load method), you need not supply any
184 arguments. If the timestamp is not available, you must supply the
185 sectionid, storyname and commentpath identifiers, the method will then
186 load it into the data structure first.
187  
188 The timestamp method will return a L<Time::Piece> object with the
189 requested time information.
190  
191 To set the timestamp, you must supply a L<Time::Piece> object, the
192 timestamp is set to the time given by that object.
193  
194 =back
195  
196 =cut
197  
198  
199 sub timestamp {
200 my $self = shift;
201 my $arg = shift;
202 if (ref($arg) eq 'Time::Piece') {
203 ${$self}{'timestamp'} = $arg->datetime;
204 return $self;
205 }
206 if (! ${$self}{'timestamp'}) {
207 my ($storyname, $commentpath) = @_;
208 $self->load(what => 'timestamp', limit => {sectionid => $arg,
209 storyname => $storyname,
210 commentpath => $commentpath
211 });
212 }
213 unless (${$self}{'timestamp'}) { return undef; }
214 (my $tmp = ${$self}{'timestamp'}) =~ s/\+\d{2}$//;
215 return Time::Piece->strptime($tmp, "%Y-%m-%d %H:%M:%S");
216 }
217  
218  
219  
220  
221  
222 1;
223  
224 =head1 STORED DATA
225  
226 The data is stored in named fields, and for certain uses, it is good
227 to know them. If you want to subclass this class, you might want to
228 use the same names, see the documentation of
229 L<AxKit::APP::TABOO::Data> for more about this.
230  
231 In this class it gets even more interesting, because you may pass a
232 list of those to the load method. This is useful if you for example
233 just want the title of the comments, not all their content.
234  
235 These are the names of the stored data of this class:
236  
237 =over
238  
239 =item * commentpath - the identifying commentpath, as described above.
240  
241 =item * storyname - an identifier for the story, a simple word you use
242 to retrieve the desired object.
243  
244 =item * sectionid - an identifier for the section, also a simple word
245 you use to retrieve the desired object.
246  
247 =item * title - the title for the comment chosen by the poster.
248  
249 =item * content - the full comment text.
250  
251 =item * timestamp - typically the time when the comment was
252 posted. See also the C<timestamp()> method.
253  
254 =item * username - the username of the user who posted the comment.
255  
256 =back
257  
258  
259 =head1 XML representation
260  
261 The C<write_xml()> method, implemented in the parent class, can be
262 used to create an XML representation of the data in the object. The
263 above names will be used as element names. The C<xmlelement()> and
264 C<xmlns()> methods can be used to set the name of the root element and
265 the namespace respectively. Usually, it doesn't make sense to change
266 the defaults, which are
267  
268  
269 =over
270  
271 =item * C<reply>
272  
273 =item * C<http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment/Output>
274  
275 =back
276  
277 =head1 BUGS/TODO
278  
279 =over
280  
281 =item * Add a category for the nature of the response, to support
282 things like the Thread Description Language.
283  
284 =item * C<reply> should check the class of the object it is passed.
285  
286 =back
287  
288 =head1 FORMALITIES
289  
290 See L<AxKit::App::TABOO>.
291  
292 =cut