TABOO/XSP/Story.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 156 CC 4 SCAR 14.0
CC
4
Cov
0%
_create_storyname
20.0
store
2.0
this_story
2.0
CRAP 4.1
1 package AxKit::App::TABOO::XSP::Story;
2 use 5.6.0;
3 use strict;
4 use warnings;
5 use utf8;
6 use Apache::AxKit::Language::XSP::SimpleTaglib;
7 use Apache::AxKit::Exception;
8 use AxKit;
9 use AxKit::App::TABOO;
10 use AxKit::App::TABOO::Data::Story;
11 use AxKit::App::TABOO::Data::Plurals::Stories;
12 use Session;
13 use Time::Piece ':override';
14 use XML::LibXML;
15 use Text::Unaccent;
16 use IDNA::Punycode;
17 use Net::Akismet;
18  
19 use vars qw/$NS/;
20  
21 our $VERSION = '0.53';
22  
23 =head1 NAME
24  
25 AxKit::App::TABOO::XSP::Story - News story management tag library for TABOO
26  
27 =head1 SYNOPSIS
28  
29 Add the story: namespace to your XSP C<E<lt>xsp:pageE<gt>> tag, e.g.:
30  
31 <xsp:page
32 language="Perl"
33 xmlns:xsp="http://apache.org/xsp/core/v1"
34 xmlns:story="http://www.kjetil.kjernsmo.net/software/TABOO/NS/Story"
35 >
36  
37 Add this taglib to AxKit (via httpd.conf or .htaccess):
38  
39 AxAddXSPTaglib AxKit::App::TABOO::XSP::Story
40  
41  
42 =head1 DESCRIPTION
43  
44 This XSP taglib provides tags to store information related to news
45 stories and to fetch and return XML representations of that data, as
46 it communicates with TABOO Data objects, particulary
47 L<AxKit::App::TABOO::Data::Story>.
48  
49 L<Apache::AxKit::Language::XSP::SimpleTaglib> has been used to write
50 this taglib.
51  
52 =cut
53  
54  
55 $NS = 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/Story';
56  
57 # Some constants
58 # TODO: This stuff should go somewhere else!
59  
60 use constant GUEST => 0;
61 use constant NEWMEMBER => 1;
62 use constant MEMBER => 2;
63 use constant OLDTIMER => 3;
64 use constant ASSISTANT => 4;
65 use constant EDITOR => 5;
66 use constant ADMIN => 6;
67 use constant DIRECTOR => 7;
68 use constant GURU => 8;
69 use constant GOD => 9;
70  
71 # This sub will create a useful storyname from the title
72 sub _create_storyname {
73 my $intitle = shift;
74 my $nl = 30; # Should be the length of storyname in the table
75 chomp($intitle);
76
77 my $endno = '';
78 $intitle =~ s/\p{TerminalPunctuation}//gs; # Remove terminal punctutation
79 if ($intitle =~ s/(\d+)$//) { # if it ends with a number, the number might be useful, so put it into a variable but strip it
80 $endno = $1;
81 }
82 idn_prefix('');
83 # warn "INTITLE: ". $intitle;
84 $intitle = unac_string('utf8', $intitle); # Try to translate most
85 $intitle = encode_punycode(lc($intitle)); # Punycode the rest if needed
86 $intitle =~ s/\s+/-/gs; # All spaces become one -
87 $intitle =~ s/[^a-z0-9\-_]//g; # Remove all now not a alphanumeric or -
88 # warn $intitle;
89 # warn $endno;
90 # warn length($endno);
91
92 my $base = substr($intitle, 0, $nl-2-length($endno));
93
94 # warn $base;
95 my $storyname = $base . $endno;
96 my $stories = AxKit::App::TABOO::Data::Plurals::Stories->new();
97
98 my $i = 1;
99 while ($stories->exists(storyname => $storyname)) { # Checking if the story exists
100 $base = substr($base, 0, $nl-3-length($i)-length($endno));
101 $storyname = $base . $endno . '-' . $i;
102 AxKit::Debug(9, "Try '$storyname' for storyname");
103  
104 $i++;
105 if ($i>99) { # We've gone too far allready
106 throw Apache::AxKit::Exception::Retval(
107 return_code => 500,
108 -text => "Tried $i storynames, all taken. Editors need more imagination.");
109 }
110 }
111 if (length($storyname) > $nl) { # Is likely to cause a crash
112 AxKit::Debug(2, "Length of '$storyname' is higher than $nl");
113 }
114 return $storyname;
115 }
116  
117 package AxKit::App::TABOO::XSP::Story::Handlers;
118  
119 =head1 Tag Reference
120  
121 =head2 C<E<lt>store/E<gt>>
122  
123 It will take whatever data it finds in the L<Apache::Request> object
124 held by AxKit, and hand it to a new L<AxKit::App::TABOO::Data::Story>
125 object, which will use whatever data it finds useful. It will not
126 store anything unless the user is logged in and authenticated with an
127 authorization level. If an authlevel is not found in the user's
128 session object, it will throw an exception with an C<AUTH_REQUIRED>
129 code. If asked to store certain priviliged fields, it will check the
130 authorization level and throw an exception with a C<FORBIDDEN> code if
131 not satisfied. If timestamps do not exist, they will be created based
132 on the system clock.
133  
134 If TABOOAkismetKey is set (and spammers will make you want this really
135 fast), it will check the Akismet anti-spam system if article has not
136 been approved by an editor and the user has an authlevel less than 2,
137 and return a C<FORBIDDEN> if it is deemed to be spam. Once the article
138 has been approved by an editor, it is fed to Akismet to teach it what
139 is ham.
140  
141 Finally, the Data object is instructed to save itself.
142  
143 If successful, it will return a C<store> element in the output
144 namespace with the number 1.
145  
146 =cut
147  
148  
149 sub store : node({http://www.kjetil.kjernsmo.net/software/TABOO/NS/Story/Output}store) {
150 return << 'EOC'
151 my %args;
152 foreach my $name ($cgi->param) {
153 $args{$name} = $cgi->param($name);
154 }
155  
156 my $session = AxKit::App::TABOO::session($r);
157 $args{'username'} = AxKit::App::TABOO::loggedin($session);
158  
159 my $authlevel = AxKit::App::TABOO::authlevel($session);
160 AxKit::Debug(4, "Logged in as $args{'username'} at level $authlevel");
161 unless (defined($authlevel)) {
162 throw Apache::AxKit::Exception::Retval(
163 return_code => AUTH_REQUIRED,
164 -text => "Not authenticated and authorized with an authlevel");
165 }
166 unless ($args{'storyname'}) {
167 $args{'storyname'} = AxKit::App::TABOO::XSP::Story::_create_storyname($args{'title'});
168 }
169 if ($args{'sectionid'} ne 'subqueue') {
170 if ($authlevel < AxKit::App::TABOO::XSP::Story::EDITOR) {
171 throw Apache::AxKit::Exception::Retval(
172 return_code => FORBIDDEN,
173 -text => "Editor Priviliges are needed to store non-subqueue section. Your level: " . $authlevel);
174 }
175 }
176 if (($args{'editorok'}) && ($authlevel < AxKit::App::TABOO::XSP::Story::EDITOR)) {
177 throw Apache::AxKit::Exception::Retval(
178 return_code => FORBIDDEN,
179 -text => "Editor Priviliges are needed to OK an article. Your level: " . $authlevel);
180 }
181
182 if ($r->dir_config('TABOOAkismetKey')) {
183 AxKit::Debug(4, "Using Akismet");
184 my $akismet = Net::Akismet->new(
185 KEY => $r->dir_config('TABOOAkismetKey'),
186 URL => 'http://'.$r->header_in('X-Forwarded-Host'),
187 ) or throw Apache::AxKit::Exception::Error(-text => "Akismet key verification failed.");
188 my %akismetstuff = (USER_IP => $r->header_in('X-Forwarded-For'),
189 COMMENT_CONTENT => $args{'minicontent'} ."\n". $args{'content'},
190 REFERRER => $r->header_in('Referer'),
191 COMMENT_TYPE => 'comment',
192 );
193 if ($args{'editorok'}) { # Surely ham
194 $akismet->ham(%akismetstuff);
195 } elsif ($authlevel < 2) { # Above 2 is probably ham
196 AxKit::Debug(10, "Akismet check on: ".join(" ",values(%akismetstuff)));
197 if ($akismet->check(%akismetstuff) eq 'true') {
198 throw Apache::AxKit::Exception::Retval(
199 return_code => FORBIDDEN,
200 -text => "Akismet check says that your comment is spam. Please contact webmaster if you received this message in error.");
201 }
202 }
203 }
204  
205  
206 if (! $args{'submitterid'}) {
207 # If the submitterid is not set, we set it to the current username
208 $args{'submitterid'} = $args{'username'}
209 }
210  
211 my $story = AxKit::App::TABOO::Data::Story->new();
212  
213 my $timestamp = localtime;
214 unless ($args{'timestamp'}) {
215 $args{'timestamp'} = $timestamp->datetime;
216 }
217 unless ($args{'lasttimestamp'}) {
218 $args{'lasttimestamp'} = $timestamp->datetime;
219 }
220  
221 $story->populate(\%args);
222 $story->save;
223 1;
224 EOC
225 }
226  
227  
228 =head2 C<E<lt>this-story/E<gt>>
229  
230 Will return an XML representation of the data submitted in the last
231 request, enclosed in a C<story-submission> element. Particularly
232 useful for previewing a submission.
233  
234 =cut
235  
236 sub this_story : struct {
237 return << 'EOC'
238 my %args = map { $_ => $cgi->param($_) } $cgi->param;
239  
240 $args{'username'} = AxKit::App::TABOO::loggedin(AxKit::App::TABOO::session($r));
241  
242 unless ($args{'submitterid'}) {
243 # If the submitterid is not set, we set it to the current username
244 $args{'submitterid'} = $args{'username'}
245 }
246
247 my $timestamp = localtime;
248 unless ($args{'timestamp'}) {
249 $args{'timestamp'} = $timestamp->datetime;
250 }
251 unless ($args{'lasttimestamp'}) {
252 $args{'lasttimestamp'} = $timestamp->datetime;
253 }
254 my $story = AxKit::App::TABOO::Data::Story->new();
255 $story->populate(\%args);
256 $story->adduserinfo();
257 $story->addcatinfo();
258
259 my $doc = XML::LibXML::Document->new();
260 my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Story/Output', 'story:story-submission');
261 $doc->setDocumentElement($root);
262 $story->write_xml($doc, $root); # Return an XML representation
263 EOC
264 }
265  
266  
267 =head2 C<E<lt>get-story/E<gt>>
268  
269 Will return an XML representation of the data for a previously saved
270 story, enclosed in a C<story-loaded> element. It needs to get the
271 story identified by C<storyname> and C<sectionid> attributes or child
272 elements.
273  
274 =cut
275  
276  
277 sub get_story : struct attribOrChild(storyname,sectionid) {
278 return << 'EOC'
279 my %args;
280 foreach my $name ($cgi->param) {
281 $args{$name} = $cgi->param($name);
282 }
283  
284 my $session = AxKit::App::TABOO::session($r);
285  
286 unless ($args{'username'}) {
287 $args{'username'} = AxKit::App::TABOO::loggedin($session);
288 }
289  
290  
291 my $story = AxKit::App::TABOO::Data::Story->new();
292 unless ($story->load(limit => {sectionid => $attr_sectionid, storyname=> $attr_storyname})) {
293 throw Apache::AxKit::Exception::Retval(
294 return_code => 404,
295 -text => "Requested story $attr_storyname not found.");
296 }
297 $story->adduserinfo();
298 unless ($story->editorok) {
299 if (AxKit::App::TABOO::authlevel($session) < 4) {
300 throw Apache::AxKit::Exception::Retval(
301 return_code => 401,
302 -text => "Authentication and higher priviliges required to load story");
303 }
304 }
305 my $doc = XML::LibXML::Document->new();
306 my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Story/Output', 'story:story-loaded');
307 $doc->setDocumentElement($root);
308 $story->write_xml($doc, $root); # Return an XML representation
309 EOC
310 }
311  
312 =head2 C<E<lt>number-of-unapproved sectionid="subqueue"/E<gt>>
313  
314 Will return the number of articles in a given section that has not
315 been approved by an editor. Especially useful for giving the editors a
316 heads-up as to new articles in the submission queue, like in the
317 example.
318  
319 =cut
320  
321 sub number_of_unapproved : expr attribOrChild(sectionid) {
322 return << 'EOC'
323 my $stories = AxKit::App::TABOO::Data::Plurals::Stories->new();
324 $stories->exists(sectionid => $attr_sectionid,
325 editorok => 0);
326 EOC
327 }
328  
329  
330 1;
331  
332 =head1 FORMALITIES
333  
334 See L<AxKit::App::TABOO>.
335  
336 =cut