TABOO/Data/Story.pm untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 185 CC 11 SCAR 22.7
CC
11
Cov
0%
load
20.0
timestamp
20.0
lasttimestamp
20.0
CRAP 9.7
1 package AxKit::App::TABOO::Data::Story;
2 use strict;
3 use warnings;
4 use Carp;
5 use Encode;
6  
7  
8 use Data::Dumper;
9 use AxKit::App::TABOO::Data;
10 use vars qw/@ISA/;
11  
12 @ISA = qw(AxKit::App::TABOO::Data);
13 use AxKit::App::TABOO::Data::User;
14 use AxKit::App::TABOO::Data::Category;
15 use AxKit::App::TABOO::Data::Plurals::Categories;
16 use Time::Piece;
17  
18 use DBI;
19  
20  
21 our $VERSION = '0.33';
22  
23  
24 =head1 NAME
25  
26 AxKit::App::TABOO::Data::Story - Story Data object for TABOO
27  
28 =head1 SYNOPSIS
29  
30 use AxKit::App::TABOO::Data::Story;
31 $story = AxKit::App::TABOO::Data::Story->new(@dbconnectargs);
32 $story->load(what => '*', limit => {sectionid => $sectionid, storyname => $storyname});
33 $story->adduserinfo();
34 $story->addcatinfo();
35 $timestamp = $story->timestamp();
36 $lasttimestamp = $story->lasttimestamp();
37 $story->timestamp($lasttimestamp);
38  
39 =head1 DESCRIPTION
40  
41 This Data class contains a story, as posted by the editors of a site.
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(@dbconnectargs)>
51  
52 The constructor. Nothing special.
53  
54 =cut
55  
56 AxKit::App::TABOO::Data::Story->dbtable("stories");
57 AxKit::App::TABOO::Data::Story->dbfrom("stories");
58 AxKit::App::TABOO::Data::Story->dbprimkey("storyname");
59 AxKit::App::TABOO::Data::Story->elementorder("storyname, sectionid, image, primcat, seccat, freesubject, editorok, title, minicontent, content, USER, SUBMITTER, linktext, timestamp, lasttimestamp");
60 AxKit::App::TABOO::Data::Story->elementneedsparse("minicontent, content");
61  
62 sub new {
63 my $that = shift;
64 my $class = ref($that) || $that;
65 my $self = {
66 storyname => undef,
67 sectionid => undef,
68 image => undef,
69 primcat => undef,
70 seccat => [],
71 freesubject => [],
72 editorok => undef,
73 title => undef,
74 minicontent => undef,
75 content => undef,
76 username => undef,
77 USER => undef,
78 submitterid => undef,
79 SUBMITTER => undef,
80 linktext => undef,
81 timestamp => undef,
82 lasttimestamp => undef,
83 DBCONNECTARGS => \@_,
84 XMLELEMENT => 'story',
85 XMLPREFIX => 'story',
86 XMLNS => 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/Story/Output',
87 ONFILE => undef,
88 };
89 bless($self, $class);
90 return $self;
91 }
92  
93  
94 =item C<load(what =E<gt> fields, limit =E<gt> {sectionid =E<gt> value, storyname =E<gt> value, [...]})>
95  
96 This class reimplements the load method, to support the fact that some
97 data may be stored as arrays in the datastore. It shares the API of
98 the parent class. It is useful to note, however, that there are two
99 fields that you most likely would want to specify for the common use
100 of retrieving a specific story:
101  
102 =over
103  
104  
105 =item * The C<sectionid> which the story has been posted
106 to. Typically, this string will be taken directly from the URI. The
107 use of sections makes it possible to divide the site in different
108 ways. Sections are a type of
109 L<category|AxKit::App::TABOO::Data::Category>, specifically C<stsec>,
110 but they are not intended to be like the C<categ> types. Rather, one
111 can have sections with "small news", i.e. blatant rip-offs of other
112 news sites with a few comments added, or longer articles with more
113 unique content.
114  
115 =item * C<storyname> is a unique identifier for the story. This too
116 will typically be derived from the URI directly.
117  
118 =back
119  
120 It is of course possible to identify a single story by a completely
121 different set of parameters, and you can do that too, but it is not a
122 very common thing to do.
123  
124  
125 =cut
126  
127 sub load
128 {
129 my ($self, %args) = @_;
130 my $data = $self->_load(%args);
131 return undef unless ($data);
132 if ($data) { ${$self}{'ONFILE'} = 1; }
133 foreach my $key (keys(%{$data})) {
134 if (defined(${$data}{$key}) && (${$data}{$key} =~ m/^\{(\S+)\}$/)) { # Support SQL3 arrays ad hoc
135 my @arr = split(/\,/, $1);
136 ${$self}{$key} = \@arr;
137 } else {
138 ${$self}{$key} = Encode::decode_utf8(${$data}{$key});
139 }
140 }
141 return $self;
142 }
143  
144 =item C<adduserinfo()>
145  
146 When data has been loaded into an object of this class, it will
147 contain a string only identifying a user. This method will replace
148 those strings (for the user posting the article, and for the submitter
149 who sent the article to the site) with a reference to a
150 L<AxKit::App::TABOO::Data::User>-object, containing the needed user
151 information.
152  
153 =cut
154  
155 sub adduserinfo {
156 my $self = shift;
157 my $user = AxKit::App::TABOO::Data::User->new($self->dbconnectargs());
158 $user->xmlelement("user");
159 $user->load(what => 'username,name', limit => {username => ${$self}{'username'}});
160 ${$self}{'USER'} = $user;
161 my $submitter = AxKit::App::TABOO::Data::User->new($self->dbconnectargs());
162 $submitter->xmlelement("submitter");
163 $submitter->load(what => 'username,name', limit => {username => ${$self}{'submitterid'}});
164 ${$self}{'SUBMITTER'} = $submitter;
165 return $self;
166 }
167  
168 =item C<addcatinfo()>
169  
170 Similarly to adding user info, this method will also add category
171 information, for different types of categories, again by creating a
172 reference to a L<AxKit::App::TABOO::Data::Category>-object and calling
173 its C<load>-method with the string from the data loaded by the story
174 as argument.
175  
176 =cut
177  
178 sub addcatinfo {
179 my $self = shift;
180 my $cat = AxKit::App::TABOO::Data::Category->new($self->dbconnectargs());
181 # There is only one primary category allowed.
182 $cat->xmlelement("primcat");
183 $cat->load(what => '*', limit => {catname => ${$self}{'primcat'}});
184  
185 ${$self}{'primcat'} = $cat;
186  
187 # We allow several secondary categories, so we may get an array to run through.
188 # my $cats = AxKit::App::TABOO::Data::Plurals::Categories->new($self->dbconnectargs());
189 # $cats->xmlelement("seccat");
190 # foreach my $catname (@{${$self}{'seccat'}}) {
191 # my $cat = AxKit::App::TABOO::Data::Category->new($self->dbconnectargs());
192 # $cat->load(what => '*', limit => {catname => $catname});
193 # $cats->Push($cat);
194 # }
195 # ${$self}{'seccat'} = $cats;
196 # my $frees = AxKit::App::TABOO::Data::Plurals::Categories->new($self->dbconnectargs());
197 # $frees->xmlelement("freesubject");
198 # foreach my $catname (@{${$self}{'freesubject'}}) {
199 # my $cat = AxKit::App::TABOO::Data::Category->new($self->dbconnectargs());
200 # $cat->load(what => '*', limit => {catname => $catname});
201 # $frees->Push($cat);
202 # }
203 # ${$self}{'freesubject'} = $frees;
204  
205 return $self;
206 }
207  
208 =item C<timestamp([($sectionid, $storyname)|Time::Piece])>
209  
210 The timestamp method will retrieve or set the timestamp of the
211 story. If the timestamp has been loaded earlier from the data storage
212 (for example by the load method), you need not supply any
213 arguments. If the timestamp is not available, you must supply the
214 sectionid and storyname identifiers, the method will then load it into
215 the data structure first.
216  
217 The timestamp method will return a L<Time::Piece> object with the
218 requested time information.
219  
220 To set the timestamp, you must supply a L<Time::Piece> object, the
221 timestamp is set to the time given by that object.
222  
223 =cut
224  
225 sub timestamp {
226 my $self = shift;
227 my $arg = shift;
228 if (ref($arg) eq 'Time::Piece') {
229 ${$self}{'timestamp'} = $arg->datetime;
230 return $self;
231 }
232 if (! ${$self}{'timestamp'}) {
233 my $storyname = shift;
234 $self->load(what => 'timestamp', limit => {sectionid => $arg, storyname => $storyname});
235 }
236 unless (${$self}{'timestamp'}) { return undef; }
237 (my $tmp = ${$self}{'timestamp'}) =~ s/\+\d{2}$//;
238 return Time::Piece->strptime($tmp, "%Y-%m-%d %H:%M:%S");
239 }
240  
241  
242 =item C<lasttimestamp([($sectionid, $storyname)|Time::Piece])>
243  
244 This does exactly the same as the timestamp method, but instead
245 returns the lasttimestamp, which is intended to show when anything
246 connected to the story (which may include comments) last changed.
247  
248 It may require arguments like the timestamp method does, and it will
249 return a Time::Piece object.
250  
251  
252 =cut
253  
254 sub lasttimestamp {
255 my $self = shift;
256 my $arg = shift;
257 if (ref($arg) eq 'Time::Piece') {
258 ${$self}{'lasttimestamp'} = $arg->datetime;
259 return $self;
260 }
261 if (! ${$self}{'lasttimestamp'}) {
262 my $storyname = shift;
263 $self->load(what => 'lasttimestamp', limit => {sectionid => $arg, storyname => $storyname});
264 }
265 unless (${$self}{'lasttimestamp'}) { return undef; }
266 (my $tmp = ${$self}{'lasttimestamp'}) =~ s/\+\d{2}$//;
267 return Time::Piece->strptime($tmp, "%Y-%m-%d %H:%M:%S");
268 }
269  
270  
271 =item C<editorok([($sectionid, $storyname)])>
272  
273 This is similar to the timestamp method in interface, but can't be
274 used to set the value, only retrieves it. It returns the C<editorok>,
275 which is a boolean variable that says can be used to see if an editor
276 has approved a story.
277  
278 It takes arguments like the timestamp method does, and it will return
279 1 if the story has been approved, 0 if not.
280  
281 =cut
282  
283 sub editorok {
284 my $self = shift;
285 unless (defined(${$self}{'editorok'})) {
286 my ($section, $storyname) = @_;
287 $self->load(what => 'editorok', limit => {sectionid => $section, storyname => $storyname});
288 }
289 return ${$self}{'editorok'};
290 }
291  
292  
293 =back
294  
295 =head1 STORED DATA
296  
297 The data is stored in named fields, and for certain uses, it is good
298 to know them. If you want to subclass this class, you might want to
299 use the same names, see the documentation of
300 L<AxKit::APP::TABOO::Data> for more about this.
301  
302 In this class it gets even more interesting, because you may pass a
303 list of those to the load method. This is useful if you don't want to
304 load all data, in those cases where you don't need all the data that
305 the object can hold.
306  
307 These are the names of the stored data of this class:
308  
309 =over
310  
311 =item * storyname - an identifier for the story, a simple word you use
312 to retrieve the desired object.
313  
314 =item * sectionid - an identifier for the section, also a simple word
315 you use to retrieve the desired object.
316  
317 =item * image - the URL of an image that you want to associate with
318 the story.
319  
320 =item * primcat - the primary category. You want to classify the story
321 into one primary category.
322  
323 =item * seccat - the secondary categories. May be an array, so you can
324 classify the story into any number of categories. This may be useful
325 when you try to find relevant articles but searching along different
326 paths.
327  
328 =item * freesubject - free categories. The primary categories are
329 intended to be controlled vocabularies, whereas free subjects can be
330 used and created more ad hoc. Also an array, you can have any number
331 of such categories.
332  
333 =item * editorok - a boolean variable indicated if an editor has
334 approved the story for publishing.
335  
336 =item * title - the main title of the story.
337  
338 =item * minicontent - Intended to be used as a summary or introduction
339 to a story. Typically, the minicontent will be shown on a front page,
340 where a visitor clicks happily along to read the full story.
341  
342 =item * content - the full story text.
343  
344 =item * username - the username of the user who actually does the
345 posting of hte story. Would usually be an editor.
346  
347 =item * submitterid - the username of the user who submitted the
348 article to the site for review and posting.
349  
350 =item * linktext - "Read More" makes a bad link text. Link texts
351 should be meaningful when read out of context, and this should contain
352 such a text.
353  
354 =item * timestamp - typically the time when the story was posted. See
355 also the C<timestamp()> method.
356  
357 =item * lasttimestamp - typically the time when something attached to
358 the story was last changed, for example when a comment was last
359 submitted. See also the C<lasttimestamp()> method.
360  
361 =back
362  
363 =head1 XML representation
364  
365 The C<write_xml()> method, implemented in the parent class, can be
366 used to create an XML representation of the data in the object. The
367 above names will be used as element names. The C<xmlelement()>,
368 C<xmlns()> and C<xmlprefix()> methods can be used to set the name of
369 the root element, the namespace URI and namespace prefix
370 respectively. Usually, it doesn't make sense to change the defaults,
371 that are
372  
373  
374 =over
375  
376 =item * C<story>
377  
378 =item * C<http://www.kjetil.kjernsmo.net/software/TABOO/NS/Story/Output>
379  
380 =item * C<story>
381  
382 =back
383  
384 =head1 BUGS/TODO
385  
386 There is a quirk in the load method. I use SQL3 arrays in the
387 underlying database, but the database driver doesn't support this. So,
388 there is a very hackish ad hoc implementation to parse the arrays in
389 that method. It is in fact the only reason why this class reimplements
390 the load method. It works partly for reading, but just with L<DBD::Pg>
391 greater than 1.32.
392  
393 =head1 FORMALITIES
394  
395 See L<AxKit::App::TABOO>.
396  
397 =cut
398  
399 1;