| 1 | package AxKit::App::TABOO::Provider::Classification; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | # This is the "Classification" Provider, that is, it constructs | ||
| 7 | # objects that eventually gives back XML containing classification of | ||
| 8 | # stories and articles. It just implements the AxKit Provider API, | ||
| 9 | # and therefore contains no method that anybody should use for | ||
| 10 | # anything, so the POD deals with what you should expect from this | ||
| 11 | # module. | ||
| 12 | |||
| 13 | |||
| 14 | our $VERSION = '0.4'; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | AxKit::App::TABOO::Provider::Classification - Provider for classifiying things in TABOO | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | In the Apache config: | ||
| 23 | |||
| 24 | <Location /cats/> | ||
| 25 | PerlHandler AxKit | ||
| 26 | AxContentProvider AxKit::App::TABOO::Provider::Classification | ||
| 27 | </Location> | ||
| 28 | |||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | This is a Provider, it implements the AxKit Provider API, and | ||
| 33 | therefore contains no method that anybody should use for anything. For | ||
| 34 | that reason, this documentation deals with what you should expect to | ||
| 35 | be returned for different URIs. | ||
| 36 | |||
| 37 | |||
| 38 | It is intended to be used to get an overview of articles, stories | ||
| 39 | etc. that can be found classified into different categories, of | ||
| 40 | different types. | ||
| 41 | |||
| 42 | In accordance with the TABOO philosophy, it interacts with Data | ||
| 43 | objects, that are Perl objects responsible for retrieving data from a | ||
| 44 | data storage, make up sensible data structures, return XML markup, | ||
| 45 | etc. In contrast with the News provider, this provider mainly | ||
| 46 | interacts with Plural objects to make lists of stories. Also, it | ||
| 47 | doesn't deal with comments. | ||
| 48 | |||
| 49 | The rest of the documentation has yet to be written, but as one can | ||
| 50 | guess, it may share some things with the NewsList Provider. | ||
| 51 | |||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | use Data::Dumper; | ||
| 58 | use XML::LibXML; | ||
| 59 | |||
| 60 | use vars qw/@ISA/; | ||
| 61 | @ISA = ('Apache::AxKit::Provider'); | ||
| 62 | |||
| 63 | use Apache; | ||
| 64 | use Apache::Log; | ||
| 65 | use Apache::AxKit::Exception; | ||
| 66 | use Apache::AxKit::Provider; | ||
| 67 | |||
| 68 | use AxKit; | ||
| 69 | use AxKit::App::TABOO; | ||
| 70 | use AxKit::App::TABOO::Data::Story; | ||
| 71 | use AxKit::App::TABOO::Data::Plurals::Stories; | ||
| 72 | use AxKit::App::TABOO::Data::Category; | ||
| 73 | use AxKit::App::TABOO::Data::Plurals::Categories; | ||
| 74 | use AxKit::App::TABOO::Data::Article; | ||
| 75 | use AxKit::App::TABOO::Data::Plurals::Articles; | ||
| 76 | |||
| 77 | |||
| 78 | sub init { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | my $r = $self->apache_request(); | ||
| 82 | |||
| 83 | AxKit::Debug(10, "[Classification] Request object: " . $r->as_string); | ||
| 84 | AxKit::Debug(8, "[Classification] Provider using URI " . $r->uri); | ||
| 85 | |||
| 86 | $self->{uri} = $r->uri; | ||
| 87 | my @uri = split('/', $r->uri); | ||
| 88 | splice(@uri, 0, 2); # The first part is just a keyword, | ||
| 89 | $self->{cats} = \@uri; | ||
| 90 | |||
| 91 | $self->{session} = AxKit::App::TABOO::session($r); | ||
| 92 | |||
| 93 | AxKit::Debug(9, "[Classification] Data parsed in init: " . Dumper($self)); | ||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub process { | ||
| 98 | my $self = shift; | ||
| 99 | $self->{foundcats} = AxKit::App::TABOO::Data::Plurals::Categories->new(); | ||
| 100 | foreach my $catname (@{$self->{cats}}) { | ||
| 101 | my $category = AxKit::App::TABOO::Data::Category->new(); | ||
| 102 | unless ($category->load(limit => {catname => $catname})) { | ||
| 103 | $self->{exists} = 0; | ||
| 104 | $self->{catnotfound} = $catname; | ||
| 105 | last; | ||
| 106 | } else { | ||
| 107 | $self->{foundcats}->Push($category); | ||
| 108 | $self->{exists} = 1; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | if ($self->{exists}) { | ||
| 112 | $self->{articles} = AxKit::App::TABOO::Data::Plurals::Articles->new(); | ||
| 113 | $self->{stories} = AxKit::App::TABOO::Data::Plurals::Stories->new(); | ||
| 114 | unless ($self->{articles}->incat(@{$self->{cats}})) { | ||
| 115 | $self->{articles} = undef; | ||
| 116 | unless ($self->{stories}->exists(primcat => @{$self->{cats}})) { | ||
| 117 | $self->{exists} = 0; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | return $self->{exists}; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub exists { | ||
| 125 | my $self = shift; | ||
| 126 | if ($self->{exists}) { | ||
| 127 | return 1; | ||
| 128 | } else { | ||
| 129 | return 0; | ||
| 130 | } | ||
| 131 | # Thanks, Kip! :-) | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | sub key { | ||
| 136 | my $self = shift; | ||
| 137 | return $self->{uri} . "/" . AxKit::App::TABOO::loggedin($self->{session}); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | sub mtime { | ||
| 142 | my $self=shift; | ||
| 143 | return time(); | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | sub get_fh { | ||
| 148 | throw Apache::AxKit::Exception::IO( | ||
| 149 | -text => "No fh for Classification Provider"); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub get_dom { | ||
| 153 | my $self = shift; | ||
| 154 | unless ($self->{dom}) { | ||
| 155 | unless ($self->{exists}) { | ||
| 156 | throw Apache::AxKit::Exception::Retval( | ||
| 157 | return_code => 404, | ||
| 158 | -text => "Category " . $self->{catnotfound} . " was not found"); | ||
| 159 | } | ||
| 160 | # warn "ANT: " . Dumper($self); | ||
| 161 | my $authlevel = AxKit::App::TABOO::authlevel($self->{session}); | ||
| 162 | if (scalar(@{$self->{cats}}) == 1) { | ||
| 163 | my %limit; | ||
| 164 | if (! defined($authlevel) | ||
| 165 | || ($authlevel < 4)) { | ||
| 166 | $limit{'editorok'} = 1; | ||
| 167 | } elsif ($self->{editor}) { | ||
| 168 | $limit{'editorok'} = 0; | ||
| 169 | } | ||
| 170 | $limit{'primcat'} = ${$self->{cats}}[0]; | ||
| 171 | |||
| 172 | $self->{stories}->load(what => 'storyname,sectionid,primcat,editorok,title,submitterid,timestamp', | ||
| 173 | limit => \%limit, | ||
| 174 | orderby => 'timestamp DESC'); | ||
| 175 | $self->{stories}->addcatinfo; | ||
| 176 | $self->{stories}->adduserinfo; | ||
| 177 | } else { | ||
| 178 | $self->{stories} = undef; | ||
| 179 | } | ||
| 180 | my $doc = XML::LibXML::Document->new(); | ||
| 181 | my $rootel = $doc->createElement('taboo'); | ||
| 182 | $rootel->setAttribute('type', 'catlists'); | ||
| 183 | $rootel->setAttribute('origin', 'Classification'); | ||
| 184 | $doc->setDocumentElement($rootel); | ||
| 185 | if ($self->{stories}) { | ||
| 186 | $self->{stories}->write_xml($doc, $rootel); | ||
| 187 | } | ||
| 188 | $self->{foundcats}->write_xml($doc, $rootel); | ||
| 189 | my $anyarticles = 0; | ||
| 190 | if($self->{articles}) { | ||
| 191 | my %limit; | ||
| 192 | if (! defined($authlevel) | ||
| 193 | || ($authlevel < 4)) { | ||
| 194 | $limit{'editorok'} = 1; | ||
| 195 | $limit{'authorok'} = 1; | ||
| 196 | } | ||
| 197 | if ($self->{articles}->load(limit => \%limit)) { | ||
| 198 | $anyarticles = 1; | ||
| 199 | } | ||
| 200 | $self->{articles}->addcatinfo; | ||
| 201 | $self->{articles}->adduserinfo; | ||
| 202 | $self->{articles}->addformatinfo; | ||
| 203 | $self->{articles}->write_xml($doc, $rootel); | ||
| 204 | } | ||
| 205 | # warn Dumper($self->{stories}); | ||
| 206 | # warn Dumper($self->{articles}); | ||
| 207 | |||
| 208 | unless (($anyarticles) || (defined($self->{stories}))) { | ||
| 209 | if (defined($authlevel)) { | ||
| 210 | throw Apache::AxKit::Exception::Retval( | ||
| 211 | return_code => 403, | ||
| 212 | -text => "Articles or stories exist, but are only accessible to editors."); | ||
| 213 | } else { | ||
| 214 | throw Apache::AxKit::Exception::Retval( | ||
| 215 | return_code => 401, | ||
| 216 | -text => "Articles or stories exist, but are only accessible to editors, you have to log in as one."); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | $self->{dom} = $doc; | ||
| 221 | } | ||
| 222 | return $self->{dom}; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub get_strref { | ||
| 226 | my $self = shift; | ||
| 227 | return \ $self->get_dom->toString(1); | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | sub get_styles { | ||
| 233 | my $self = shift; | ||
| 234 | my @styles = ( | ||
| 235 | { type => "text/xsl", | ||
| 236 | href => "/transforms/categories/xhtml/class-provider.xsl" }, | ||
| 237 | ); | ||
| 238 | return \@styles; | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | |||
| 243 | =head1 URI USAGE | ||
| 244 | |||
| 245 | This Provider doesn't place any constraints of what first part of the | ||
| 246 | local part of the URI should be, but the XSLT that is included in the | ||
| 247 | distribution does, and you should set this with e.g. | ||
| 248 | |||
| 249 | PerlAddVar AxParamExpr cats.prefix '"/cats/"' | ||
| 250 | |||
| 251 | in the Apache config. | ||
| 252 | |||
| 253 | After that, you may simply append the C<catname>s of categories, and | ||
| 254 | the Provider will return all stories and articles classified in B<all> | ||
| 255 | those categories. Since for stories, there is a only a primary | ||
| 256 | categorization, they are only displayed when there is a single | ||
| 257 | category in the path. | ||
| 258 | |||
| 259 | |||
| 260 | =head1 BUGS/TODO | ||
| 261 | |||
| 262 | I guess this documentation could be more verbose. | ||
| 263 | |||
| 264 | |||
| 265 | =head1 SEE ALSO | ||
| 266 | |||
| 267 | L<AxKit::App::TABOO::Data::Provider::NewsList>, | ||
| 268 | L<AxKit::App::TABOO::Data::Plurals::Categories> | ||
| 269 | |||
| 270 | |||
| 271 | =head1 FORMALITIES | ||
| 272 | |||
| 273 | See L<AxKit::App::TABOO>. | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | 1; | ||
| 280 | |||
| 281 |