TABOO/Provider/NewsList.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 213 CC 20 SCAR 34.3
CC
20
Cov
0%
get_strref
72.0
process
56.0
init
42.0
CRAP 30.9
1 package AxKit::App::TABOO::Provider::NewsList;
2 use strict;
3 use warnings;
4 use Carp;
5  
6 # This is the "NewsList" Provider, that is, it constructs objects that
7 # eventually gives back XML containing listed stories.
8 # It just implements the AxKit Provider API, and therefore contains
9 # no method that anybody should use for anything, so the POD deals with
10 # what you should expect from this module.
11  
12  
13 our $VERSION = '0.4';
14  
15 =head1 NAME
16  
17 AxKit::App::TABOO::Provider::NewsList - Provider for listing news stories in TABOO
18  
19 =head1 SYNOPSIS
20  
21 In the Apache config:
22  
23 <Location /news/>
24 PerlHandler AxKit
25 AxContentProvider AxKit::App::TABOO::Provider::NewsList
26 PerlSetVar TABOOListDefaultRecords 20
27 PerlSetVar TABOOListMaxRecords 200
28 </Location>
29  
30  
31 Please note that this should go B<before> the configuration of the
32 L<AxKit::App::TABOO::Provider::NewsList> Provider if you are using
33 both.
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 It will return lists of news stories, so it makes use of Plural
44 Stories objects. Stories are returned sorted by timestamp, most recent
45 story first.
46  
47 This Provider will return either the story except the content field
48 (which is unsuitable for longer lists of comments), or just title, submitter names and so on, depending on the URI.
49  
50 In accordance with the TABOO philosophy, it interacts with Data
51 objects, that are Perl objects responsible for retrieving data from a
52 data storage, make up sensible data structures, return XML markup,
53 etc. In contrast with the News provider, this provider mainly
54 interacts with Plural objects to make lists of stories. Also, it
55 doesn't deal with comments.
56  
57 =head1 CONFIGURATION DIRECTIVES
58  
59 =over
60  
61 =item TABOOListDefaultRecords
62  
63 The maximum number of stories TABOO will retrieve from the data store
64 if the user gives no other instructions in the URI (see below). It is
65 recommended that you set this to some reasonable value.
66  
67 =item TABOOListMaxRecords
68  
69 The maximum number of stories TABOO will retrieve from the data store
70 in any case. If the user requests more than this number, a 403
71 Forbidden error will be returned. It is highly recommended that you
72 set this to a value you think your server can handle.
73  
74 =back
75  
76 =cut
77  
78  
79  
80 use Data::Dumper;
81 use XML::LibXML;
82  
83 use vars qw/@ISA/;
84 @ISA = ('Apache::AxKit::Provider');
85  
86 use Apache;
87 use Apache::Log;
88 use Apache::AxKit::Exception;
89 use Apache::AxKit::Provider;
90  
91 use AxKit;
92 use AxKit::App::TABOO;
93 use AxKit::App::TABOO::Data::Plurals::Stories;
94 use AxKit::App::TABOO::Data::Category;
95  
96  
97 sub init {
98 my $self = shift;
99  
100 my $r = $self->apache_request();
101
102 AxKit::Debug(10, "[NewsList] Request object: " . $r->as_string);
103 AxKit::Debug(8, "[NewsList] Provider using URI " . $r->uri);
104
105 $self->{number} = $r->dir_config('TABOOListDefaultRecords');
106 $self->{maxrecords} = $r->dir_config('TABOOListMaxRecords');
107  
108 $self->{uri} = $r->uri;
109 $self->{session} = AxKit::App::TABOO::session($r);
110  
111 my @uri = split('/', $r->uri);
112
113 foreach my $part (@uri) {
114  
115 if ($part =~ m/^[0-9]+$/) {
116 $self->{number} = $part;
117 next;
118 }
119 if ($part eq 'list') {
120 $self->{list} = 1;
121 next;
122 }
123 if ($part eq 'editor') {
124 $self->{editor} = 1;
125 next;
126 }
127 if ($part eq 'unpriv') {
128 $self->{unpriv} = 1;
129 next;
130 }
131  
132 if ($part ne 'news') {
133 $self->{sectionid} = $part;
134 }
135  
136 }
137 AxKit::Debug(9, "[NewsList] Data parsed in init: " . Dumper($self));
138  
139 return $self;
140 }
141  
142 sub process {
143 my $self = shift;
144 if ($self->{uri} =~ m|/news/.*/$|) {
145 # URIs should never end with / unless it is just /news/
146 throw Apache::AxKit::Exception::Retval(
147 return_code => 404,
148 -text => "URIs should not end with /");
149 }
150 if ((AxKit::App::TABOO::authlevel($self->{session}) < 4) && ($self->{editor})) {
151 throw Apache::AxKit::Exception::Retval(
152 return_code => 401,
153 -text => "You're not allowed to see editor-only stories without being authenticated as one.");
154 }
155 if (($self->{unpriv}) && ($self->{editor})) {
156 throw Apache::AxKit::Exception::Retval(
157 return_code => 404,
158 -text => "Editor and Unpriviliged are mutually exclusive.");
159 }
160 if ($self->{number} > $self->{maxrecords}) {
161 throw Apache::AxKit::Exception::Retval(
162 return_code => 403,
163 -text => "The server limit for number of records is " . $self->{maxrecords});
164 }
165
166 if ($self->{sectionid}) {
167 # Iff a resource doesn't exist, it means that the section doesn't
168 # exist, so we just check the list of sections
169 $self->{section} = AxKit::App::TABOO::Data::Category->new();
170 unless ($self->{section}->load(what => '*',
171 limit => {type => 'stsec',
172 catname => $self->{sectionid}})) {
173 throw Apache::AxKit::Exception::Retval(
174 return_code => 404,
175 -text => "Not found by NewsList Provider.");
176 }
177 }
178 # No exceptions thrown means that we go ahead here:
179 $self->{exists} = 1;
180 return 1;
181 }
182  
183 sub exists {
184 my $self = shift;
185 if (defined($self->{exists})) {
186 return 1;
187 } else {
188 return 0;
189 }
190 # Thanks, Kip! :-)
191 }
192  
193  
194 sub key {
195 my $self = shift;
196 return $self->{uri} . "/" . AxKit::App::TABOO::loggedin($self->{session});
197 }
198  
199  
200 sub mtime {
201 my $self=shift;
202 return time();
203 }
204  
205  
206 sub get_fh {
207 throw Apache::AxKit::Exception::IO(
208 -text => "No fh for NewsList Provider");
209 }
210  
211 sub get_strref {
212 my $self = shift;
213 my $what = 'storyname,sectionid,primcat,editorok,title,submitterid,timestamp';
214 unless ($self->{list}) {
215 $what .= ',minicontent,seccat,freesubject,image,username,linktext,lasttimestamp';
216 }
217 my %limit;
218 my $authlevel = AxKit::App::TABOO::authlevel($self->{session});
219 if (($authlevel < 4) || ($self->{unpriv})) {
220 $limit{'editorok'} = 1;
221 } elsif ($self->{editor}) {
222 $limit{'editorok'} = 0;
223 }
224 if ($self->{sectionid}) {
225 $limit{'sectionid'} = $self->{sectionid};
226 }
227 AxKit::Debug(9, "[NewsList] Limit records to: " . Dumper(%limit));
228  
229 $self->{stories} = AxKit::App::TABOO::Data::Plurals::Stories->new();
230 $self->{stories}->load(what => $what,
231 limit => \%limit,
232 orderby => 'timestamp DESC',
233 entries => $self->{number});
234 $self->{stories}->addcatinfo;
235 $self->{stories}->adduserinfo;
236 my $doc = XML::LibXML::Document->new();
237 my $rootel = $doc->createElement('taboo');
238 $rootel->setAttribute('type', ($self->{list}) ? 'list':'stories');
239 $rootel->setAttribute('origin', 'NewsList');
240 if ($authlevel >= 5) {
241 $rootel->setAttribute('can-edit', '1');
242 }
243 $doc->setDocumentElement($rootel);
244 $self->{stories}->write_xml($doc, $rootel);
245 if ($self->{section}) {
246 $self->{section}->write_xml($doc, $rootel);
247 }
248 $self->{out} = $doc;
249 AxKit::Debug(10, Dumper($self->{out}->toString(1)));
250  
251 return \$self->{out}->toString(1);
252 }
253  
254 sub get_styles {
255 my $self = shift;
256
257 my @styles = (
258 { type => "text/xsl",
259 href => "/transforms/news/xhtml/newslist-provider.xsl" },
260 );
261
262 return \@styles;
263 }
264  
265  
266 =head1 URI USAGE
267  
268 Like the News Provider, the URI in this Provider consists of several
269 parts that is parsed and used directly to construct the objects that
270 contain the data we wish to send to the user.
271  
272 The URIs currently begin with C</news/>. This should be made
273 customizable in the future, but currently needs to be hardcoded in the
274 httpd.conf and is hardcoded in the Provider itself.
275  
276 In this provider, C</news/> will return all the news stories, only
277 limited in number by the TABOOListMaxRecords directive.
278  
279  
280 Optionally, one may then append a C<sectionid>, to get the stories in
281 that section.
282  
283 By default, any user will see the stories they are authorized to see,
284 so a higher privileged user will see both stories that are approved by
285 an editor and stories that have yet to be approved. That user may then
286 append C</editor>, to see only the stories that has not yet been
287 approved, I<or> C</unpriv> to see what unprivileged users see.
288  
289 The default is to return all information except the
290 C<content>-field. With this information, one can build a page to
291 display several stories, but with links to the whole story.
292  
293 In all cases, if you rather want a simple list of titles, timestamp
294 and submitter information, you may append C</list> to the URI.
295  
296 At the end of the URI, you may also append an integer representing how
297 many stories you want. This number defaults to the
298 TABOOListDefaultRecords value, but may be both smaller and larger than
299 that, however not larger than TABOOListMaxRecords.
300  
301 To take some complete examples:
302  
303 /news/features/editor/list/5
304  
305 This would, if the user is logged in and authorized as an editor
306 return a simple list of up to 5 stories from the features section that
307 needs to be approved. The list is suitable to get an overview.
308  
309 /news/features/30
310  
311 will return up to 30 stories from the features section, where the
312 C<title>, C<minicontent>, etc is included. Normal users will often
313 want to view this and then select what they want to read more about.
314  
315  
316 =head1 TODO
317  
318 Since every resource comes with a C<lasttimestamp>, it should be
319 relatively simple to implement C<mtime> better than it is now, but the
320 question is if all code updates C<lasttimestamp> reliably enough...
321  
322 The C<get_styles> method is implemented, but just to "make it work
323 right now". It needs to take many conditions into account, such as the
324 mime type requested by the user. It is even possible it should be
325 going into a parent class.
326  
327  
328 =head1 BUGS/QUIRKS
329  
330 It is non-trivial is to configure both the News and NewsList providers
331 to work and at the same time having the submit.xsp in the same
332 directory. There is a somewhat ad hoc example in L<AxKit::App::TABOO>
333 now.
334  
335  
336 =head1 SEE ALSO
337  
338 L<AxKit::App::TABOO::Data::Provider::News>,
339 L<AxKit::App::TABOO::Data::Plurals::Stories>
340  
341  
342 =head1 FORMALITIES
343  
344 See L<AxKit::App::TABOO>.
345  
346 =cut
347  
348  
349  
350 1;
351  
352