TABOO/Provider/News.pm untested

filter: Statementstmt 0.0% 0 / 181 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 281 CC 26 SCAR 43.3
CC
26
Cov
0%
get_strref
182.0
init
110.0
process
30.0
CRAP 76.2
1 package AxKit::App::TABOO::Provider::News;
2 use strict;
3 use warnings;
4 use Carp;
5  
6 # This is the "News" Provider, that is, it constructs objects that
7 # eventually gives back XML containing a story, and a threaded bunch
8 # of comments.
9 # It just implements the AxKit Provider API, and therefore contains
10 # no method that anybody should use for anything, so the POD deals with
11 # what you should expect from this module.
12  
13  
14 our $VERSION = '0.4';
15  
16 =head1 NAME
17  
18 AxKit::App::TABOO::Provider::News - News Provider for TABOO
19  
20 =head1 SYNOPSIS
21  
22 In the Apache config:
23  
24 <LocationMatch ^/news/(.+)/(.+)/($|comment)>
25 PerlHandler AxKit
26 AxContentProvider AxKit::App::TABOO::Provider::News
27 </LocationMatch>
28  
29  
30 Please note that this should go B<after> the configuration of the
31 L<AxKit::App::TABOO::Provider::NewsList> Provider if you are using
32 both.
33  
34  
35  
36 =head1 DESCRIPTION
37  
38 This is a Provider, it implements the AxKit Provider API, and
39 therefore contains no method that anybody should use for anything. For
40 that reason, this documentation deals with what you should expect to
41 be returned for different URIs.
42  
43 The News articles that are managed with this provider are posts that
44 may have been submitted by users of the site, reviewed by an editor
45 and posted. It will consist of the editor-approved content, called the
46 Story, and content provided as responses by site users, called
47 Comments.
48  
49 In accordance with the TABOO philosophy, it interacts with Data
50 objects, that are Perl objects responsible for retrieving data from a
51 data storage, make up sensible data structures, return XML markup,
52 etc.
53  
54 =cut
55  
56 use Data::Dumper;
57 use XML::LibXML;
58  
59 use vars qw/@ISA/;
60 @ISA = ('Apache::AxKit::Provider');
61  
62 use Apache;
63 use Apache::AxKit::Exception;
64 use Apache::AxKit::Provider;
65  
66 use AxKit;
67 use AxKit::App::TABOO;
68 use AxKit::App::TABOO::Data::Story;
69 use AxKit::App::TABOO::Data::Comment;
70 use AxKit::App::TABOO::Data::Plurals::Comments;
71 use AxKit::App::TABOO::Data::Category;
72  
73  
74 # sub: Init
75 # Here we do some initialization stuff.
76 sub init {
77 my $self = shift;
78 my (%p) = @_;
79  
80 my $r = $self->apache_request();
81
82 AxKit::Debug(10, "[News] Request object: " . $r->as_string);
83 AxKit::Debug(8, "[News] Provider using URI " . $r->uri);
84 $self->{uri} = $r->uri;
85
86 # Remember: No user named "all", "thread" or "comment"
87
88 $self->{session} = AxKit::App::TABOO::session($r);
89  
90 # Parse the URI to find if we thread, have comments, name of post, etc
91 ($self->{sectionid}, $self->{storyname}) = $r->uri =~ m|^/news/(.*?)/(.*?)/|i;
92 $self->{showthread} = ($r->uri =~ m|/thread$|i) ?1:0;
93 $self->{showall} = ($r->uri =~ m|/all$|i) ?1:0;
94 $self->{getcomments} = ($r->uri =~ m|/comment/|i) ?1:0;
95 if($self->{getcomments}) {
96 ($self->{commentpath}) = $r->uri =~ m|/comment(/.*?)$|i;
97 if($self->{showthread}) {
98 $self->{commentpath} =~ s|/thread$||i;
99 }
100 if($r->uri =~ m|/comment/thread$|i) {
101 $self->{commentpath} = '/';
102 }
103 if($self->{showall}) {
104 $self->{commentpath} =~ s|/all$|/|i;
105 }
106 }
107  
108 # We're just loading the part of the story we are sure to be using.
109 $self->{story} = AxKit::App::TABOO::Data::Story->new();
110 $self->{story}->load(what => 'storyname,minicontent,sectionid,editorok,title,timestamp,lasttimestamp', limit => {sectionid => $self->{sectionid}, storyname => $self->{storyname}});
111  
112 # Get the timestamps of the story
113 $self->{storytimestamp} = $self->{story}->timestamp();
114 if ($self->{getcomments}) {
115 $self->{lasttimestamp} = $self->{story}->lasttimestamp();
116 if (($self->{commentpath} ne '/') && ($self->{commentpath} ne '')) {
117 $self->{rootcomment} = AxKit::App::TABOO::Data::Comment->new();
118 # Might as well load the entire identified comment, since we
119 # will at least use some of it.
120 $self->{rootcomment}->load(limit => {commentpath => $self->{commentpath},
121 sectionid => $self->{sectionid},
122 storyname => $self->{storyname}});
123 $self->{commenttimestamp} = $self->{rootcomment}->timestamp();
124 } else {
125 $self->{commenttimestamp} = $self->{lasttimestamp};
126 }
127 }
128 AxKit::Debug(9, "[News] Data parsed in init: " . Dumper($self));
129 return $self;
130 }
131  
132  
133 # sub: process
134 sub process {
135 my $self = shift;
136 if($self->{getcomments} && ($self->{commentpath} ne '/') && ($self->{uri} =~ m|/$|)) {
137 # URIs should never end with / if they have a bunch of comments
138 throw Apache::AxKit::Exception::Retval(
139 return_code => 404,
140 -text => "URIs should not end with /");
141 }
142  
143 $self->{exists} = 0;
144 if ($self->{storytimestamp}) { # This exists iff the story is OK
145 $self->{exists} = 1;
146 }
147 if($self->{getcomments}) {
148 $self->{exists} = ($self->{commenttimestamp}) ?1:0;
149 }
150 return $self->{exists};
151 }
152  
153  
154  
155 sub key {
156 my $self = shift;
157 return $self->{uri} . "/" . AxKit::App::TABOO::loggedin($self->{session});
158 }
159  
160 # sub: exists
161 # should return 1 only if the resource actually exists.
162 sub exists {
163 my $self = shift;
164 return $self->{exists};
165 }
166  
167  
168 # sub: mtime
169 # Return the modification time
170 # It's used to test the validity of cached data.
171 sub mtime {
172 my $self=shift;
173 return time();
174 }
175  
176  
177 sub get_fh {
178 throw Apache::AxKit::Exception::IO(
179 -text => "No fh for News Provider");
180 }
181  
182  
183  
184 # Here, the correct stuff is retrieved from the data store.
185 # We are fed URLs on the form
186 # /news/section/storyname/comment/username/username/thread
187 sub get_strref {
188 my $self = shift;
189 if ($self->{out}) {
190 # There's a gotcha here in the AxKit framework: It will run every
191 # provider twice, the second time around to deal with styles and
192 # transformations. It is a bit a waste, because we don't want to
193 # regenerate everything. The problem is, much of the data is
194 # preserved, and some things are altered by the provider, such as
195 # creating a tree of replies. If we run it twice, the tree
196 # algorithm will be confused. So, we simply check if we've been
197 # run before.
198 # This is addressed in AxKit CVS, but is not due to be released any time soon.
199 AxKit::Debug(5, "[News] Output allready created in earlier run, reusing");
200 } else {
201 # This has to go here due to a quirk in AxKit, se bug report in rt.cpan.org
202 my $authlevel = AxKit::App::TABOO::authlevel($self->{session});
203 unless ($self->{exists}) {
204 throw Apache::AxKit::Exception::Retval(
205 return_code => 404,
206 -text => "Not found by News Provider.");
207 }
208 $self->{editorok} = $self->{story}->editorok();
209 # No point in going any further if the user isn't authorized:
210 unless ($self->{editorok}) {
211 if ($authlevel < 4) {
212 throw Apache::AxKit::Exception::Retval(
213 return_code => 401,
214 -text => "Authentication and higher priviliges required");
215 }
216 }
217  
218 $self->{commenttree} = AxKit::App::TABOO::Data::Plurals::Comments->new();
219 $self->{commentlist} = AxKit::App::TABOO::Data::Plurals::Comments->new();
220 my $doc = XML::LibXML::Document->new();
221 my $rootel = $doc->createElement('taboo');
222 $rootel->setAttribute('type', 'story');
223 $rootel->setAttribute('origin', 'News');
224 if ($authlevel >= 5) {
225 $rootel->setAttribute('can-edit', '1');
226 }
227 $doc->setDocumentElement($rootel);
228 # ===============================================
229 # Main logic of what to display goes here
230 if($self->{getcomments}) {
231 if($self->{showthread}) {
232 # We shall show a thread, not the story
233 AxKit::Debug(7, "[News] We shall show a thread, not the story");
234 $rootel->setAttribute('commentstatus', 'threadonly');
235 $self->{story}->write_xml($doc, $rootel);
236 if($self->{commentpath} eq '/') {
237 $self->{commenttree}->load(limit => {sectionid => $self->{sectionid},
238 storyname => $self->{storyname}},
239 orderby => 'timestamp DESC');
240 $self->{commenttree}->adduserinfo();
241 $self->{commenttree}->write_xml($doc, $rootel);
242 } else {
243 $self->{commenttree} = $self->{rootcomment}->tree;
244 $self->{commenttree}->adduserinfo();
245 $self->{commenttree}->write_xml($doc, $rootel);
246 }
247 } elsif($self->{commentpath} ne '/' && (! $self->{showthread})) {
248 # We shall show a single comment
249 AxKit::Debug(7, "[News] We shall show a single comment");
250 # The comment itself is in rootcomment allready.
251 $rootel->setAttribute('commentstatus', 'singlecomment');
252 $self->{story}->write_xml($doc, $rootel);
253 $self->{rootcomment}->adduserinfo();
254 $self->{rootcomment}->write_xml($doc, $rootel);
255 # But we want a list of headers too.
256 $self->{commentlist}->load(what => 'commentpath,sectionid,storyname,title,username,timestamp',
257 limit => {sectionid => $self->{sectionid},
258 storyname => $self->{storyname}},
259 orderby => 'timestamp DESC');
260 my $commentlistel = $doc->createElementNS($self->{commentlist}->xmlns(),
261 $self->{commentlist}->xmlprefix() .':'. 'commentlist');
262 $self->{commentlist}->write_xml($doc, $commentlistel);
263 $rootel->appendChild($commentlistel);
264 } elsif($self->{showall}) {
265 # We shall show the full story and all the expanded comments
266 AxKit::Debug(7, "[News] We shall show the full story and all the expanded comments");
267 $rootel->setAttribute('commentstatus', 'everything');
268 $self->{story}->load(limit => {sectionid => $self->{sectionid}, storyname => $self->{storyname}});
269 $self->{story}->adduserinfo();
270 $self->{story}->addcatinfo();
271 $self->{story}->write_xml($doc, $rootel);
272 $self->{commenttree}->load(limit => {sectionid => $self->{sectionid}, storyname => $self->{storyname}});
273 $self->{commenttree}->adduserinfo();
274 #$self->{commenttree}->addcatinfo();
275 $self->{commenttree}->write_xml($doc, $rootel);
276  
277 } else {
278 # We shall show the full story, but only headings of comments
279 AxKit::Debug(7, "[News] We shall show the full story, but only headings of comments");
280 $rootel->setAttribute('commentstatus', 'headings');
281 $self->{story}->load(limit => {sectionid => $self->{sectionid}, storyname => $self->{storyname}});
282 $self->{story}->adduserinfo();
283 $self->{story}->addcatinfo();
284 $self->{story}->write_xml($doc, $rootel);
285 $self->{commentlist}->load(what => 'commentpath,sectionid,storyname,title,username,timestamp',
286 limit => {sectionid => $self->{sectionid},
287 storyname => $self->{storyname}},
288 orderby => 'timestamp DESC');
289 my $commentlistel = $doc->createElementNS($self->{commentlist}->xmlns(),
290 $self->{commentlist}->xmlprefix() .':'. 'commentlist');
291 $self->{commentlist}->write_xml($doc, $commentlistel);
292 $rootel->appendChild($commentlistel);
293 }
294 # For breadcrumbs, we would like the names of the names of the
295 # users who have posted comments higher in the thread.
296 my $thcomusersel = $doc->createElementNS($self->{commentlist}->xmlns(),
297 $self->{commentlist}->xmlprefix() .':'. 'commentators');
298 my $anyusers = 0;
299 my @usernames = split('/', $self->{commentpath});
300 foreach my $username (@usernames[1..($#usernames-1)]) {
301 warn $username;
302 my $user = AxKit::App::TABOO::Data::User->new();
303 if($user->load(what => 'username,name',
304 limit => {username => $username})) {
305 $anyusers = 1;
306 }
307 $user->write_xml($doc, $thcomusersel);
308 }
309 if ($anyusers) {
310 $rootel->appendChild($thcomusersel);
311 }
312 } else {
313 # We shall only display the story, no comments
314 AxKit::Debug(7, "[News] We shall only display the story, no comments");
315 $rootel->setAttribute('commentstatus', 'nocomments');
316 $self->{story}->load(limit => {sectionid => $self->{sectionid}, storyname => $self->{storyname}});
317 $self->{story}->adduserinfo();
318 $self->{story}->addcatinfo();
319 $self->{story}->write_xml($doc, $rootel);
320 }
321 # We should have the section information too
322 $self->{section} = AxKit::App::TABOO::Data::Category->new();
323 $self->{section}->load(limit => {catname => $self->{sectionid}});
324 $self->{section}->write_xml($doc, $rootel);
325
326  
327 # =========================
328 # Wrapping up and returning
329 $self->{out} = $doc;
330 AxKit::Debug(10, Dumper($self->{out}->toString(1)));
331 }
332 return \$self->{out}->toString(1);
333 }
334  
335  
336  
337 sub get_styles {
338 my $self = shift;
339
340 my @styles = (
341 { type => "text/xsl",
342 href => "/transforms/news/xhtml/news-provider.xsl" },
343 );
344
345 return \@styles;
346 }
347  
348  
349 =head1 URI USAGE
350  
351 The URI in the News Provider consists of several parts that is parsed
352 by the Provider and used directly to construct the objects that
353 contain the data we wish to send to the user.
354  
355 The URIs currently begin with C</news>. This should be made
356 customizable in the future, but currently needs to be hardcoded in the
357 httpd.conf and is hardcoded in the Provider itself.
358  
359 The next part of the URI is the section. The section is coded as a
360 simple word in the URI and use to identify the section for the story,
361 as detailed in L<AxKit::App::TABOO::Data::Story>.
362  
363 Then comes the story name, used to identify the story, also as
364 detailed in L<AxKit::App::TABOO::Data::Story>.
365  
366 If we take C<features> to be our section and C<coolhack> to be the
367 name of the story, we can now construct the minimal URI that will make
368 the News Provider return something (as presently implemented):
369 C</news/features/coolhack/>. This will return markup containing the
370 Story only, no comments.
371  
372 If we want to see the comments, we should append C<comment/>. The URI
373 C</news/features/coolhack/comment/> (remember the trailing slash) will
374 give us the whole story but only the headings of comments, that is,
375 the title and author fullname.
376  
377 At this point, one can append the key words C<all> or
378 C<thread>. Neither of this may have a trailing
379 slash. E.g. C</news/features/coolhack/comment/all> will return
380 everything that is known for this story, the full story and all the
381 comments. The example C</news/features/coolhack/comment/thread>, OTOH
382 will return all the comments, but only the title (and things like
383 timestamp), of the story itself. The C<all> keyword can only be used
384 in this context.
385  
386 We may now use commentpaths to access specific comments. See
387 L<AxKit::App::TABOO::Data::Comment> for details on commentpaths. The
388 short story is that commentpaths is a series of usernames, separated
389 by slashes, and not just any user name, but the usernames of those who
390 posted the comments. For example, if user foo posts a comment, that
391 comment will have commentpath C</foo> and if bar responds to it, that
392 comment will have commentpath C</foo/bar>. No trailing slash. If you
393 want bar's latest comment the URI of that comment is
394 C</news/features/coolhack/comment/foo/bar>. That is, you get the title
395 of the story, but you also get a nested list of all the comments
396 containing only title and author name.
397  
398 Finally, you can append C</thread> behind any commentpath, and you get
399 the full text comment and every response to that comment, in addition
400 to the title of the story. To get both foo and bar's comments, nicely
401 nested, you want C</news/features/coolhack/comment/foo/thread>
402  
403 So, you get nice, human understandable URIs for everything. That's
404 actually one of the main design aims of TABOO.
405  
406 You don't get as fine-grained control of which comments you'd like to
407 see like on Slashdot, but then, not many sites attracts as many
408 comments as Slashdot.
409  
410 =head1 RETURNED XML
411  
412 It is the Data object's task to return XML marking up the data
413 contained in the object. For the most part, the element name is the
414 same as the field name used in the data storage, see the DATA STORAGE
415 section for L<AxKit::App::TABOO::Data::Story> and
416 L<AxKit::App::TABOO::Data::Comment>. What elements are used is most
417 interesting for an XSLT developer, who might want to look at the
418 output the Provider gives.
419  
420 There are a few things worth noting:
421  
422 =over
423  
424 =item * User information can be found within C<user> or C<submitter>
425 elements.
426  
427 =item * Every reply, even the first root responses will be found
428 within C<reply> elements. Furthermore, a reply to a comment will
429 appear within the C<reply> element of the parent comment.
430  
431 =item * The nested lists of comment titles will appear exactly like
432 the full comments, except that the fields with no content are
433 represented with empty elements, and the whole thing will be enclosed
434 in a C<commentlist> element.
435  
436 =back
437  
438 =head1 TODO
439  
440 Since every resource comes with a C<lasttimestamp>, it should be
441 relatively simple to implement C<mtime> better than it is now, but the
442 question is if all code updates C<lasttimestamp> reliably enough...
443  
444 The C<get_styles> method is implemented, but just to "make it work
445 right now". It needs to take many conditions into account, such as the
446 mime type requested by the user. It is even possible it should be
447 going into a parent class.
448  
449 Due to AxKit Bug 8032, Exceptions can't be thrown whereever one likes,
450 but this is a problem that needs to be corrected in AxKit first.
451  
452 =head1 BUGS
453  
454 Well, it is a beta, so there can be bugs...
455  
456 =head1 SEE ALSO
457  
458 L<AxKit::App::TABOO::Provider::NewsList>.
459  
460 =head1 FORMALITIES
461  
462 See L<AxKit::App::TABOO>.
463  
464 =cut
465  
466 1;
467