AxKit-App-TABOO
view release on metacpan or search on metacpan
lib/AxKit/App/TABOO/Provider/News.pm view on Meta::CPAN
my $r = $self->apache_request();
AxKit::Debug(10, "[News] Request object: " . $r->as_string);
AxKit::Debug(8, "[News] Provider using URI " . $r->uri);
$self->{uri} = $r->uri;
# Remember: No user named "all", "thread" or "comment"
$self->{session} = AxKit::App::TABOO::session($r);
# Parse the URI to find if we thread, have comments, name of post, etc
($self->{sectionid}, $self->{storyname}) = $r->uri =~ m|^/news/(.*?)/(.*?)/|i;
$self->{showthread} = ($r->uri =~ m|/thread$|i) ?1:0;
$self->{showall} = ($r->uri =~ m|/all$|i) ?1:0;
$self->{getcomments} = ($r->uri =~ m|/comment/|i) ?1:0;
if($self->{getcomments}) {
($self->{commentpath}) = $r->uri =~ m|/comment(/.*?)$|i;
if($self->{showthread}) {
$self->{commentpath} =~ s|/thread$||i;
}
if($r->uri =~ m|/comment/thread$|i) {
$self->{commentpath} = '/';
}
if($self->{showall}) {
$self->{commentpath} =~ s|/all$|/|i;
}
}
# We're just loading the part of the story we are sure to be using.
$self->{story} = AxKit::App::TABOO::Data::Story->new();
$self->{story}->load(what => 'storyname,minicontent,sectionid,editorok,title,timestamp,lasttimestamp', limit => {sectionid => $self->{sectionid}, storyname => $self->{storyname}});
# Get the timestamps of the story
$self->{storytimestamp} = $self->{story}->timestamp();
if ($self->{getcomments}) {
$self->{lasttimestamp} = $self->{story}->lasttimestamp();
if (($self->{commentpath} ne '/') && ($self->{commentpath} ne '')) {
$self->{rootcomment} = AxKit::App::TABOO::Data::Comment->new();
# Might as well load the entire identified comment, since we
# will at least use some of it.
$self->{rootcomment}->load(limit => {commentpath => $self->{commentpath},
sectionid => $self->{sectionid},
storyname => $self->{storyname}});
$self->{commenttimestamp} = $self->{rootcomment}->timestamp();
} else {
$self->{commenttimestamp} = $self->{lasttimestamp};
}
}
AxKit::Debug(9, "[News] Data parsed in init: " . Dumper($self));
return $self;
}
# sub: process
sub process {
my $self = shift;
if($self->{getcomments} && ($self->{commentpath} ne '/') && ($self->{uri} =~ m|/$|)) {
# URIs should never end with / if they have a bunch of comments
throw Apache::AxKit::Exception::Retval(
return_code => 404,
-text => "URIs should not end with /");
}
$self->{exists} = 0;
if ($self->{storytimestamp}) { # This exists iff the story is OK
$self->{exists} = 1;
}
if($self->{getcomments}) {
$self->{exists} = ($self->{commenttimestamp}) ?1:0;
}
return $self->{exists};
}
sub key {
my $self = shift;
return $self->{uri} . "/" . AxKit::App::TABOO::loggedin($self->{session});
}
# sub: exists
# should return 1 only if the resource actually exists.
sub exists {
my $self = shift;
return $self->{exists};
}
# sub: mtime
# Return the modification time
# It's used to test the validity of cached data.
sub mtime {
my $self=shift;
return time();
}
sub get_fh {
throw Apache::AxKit::Exception::IO(
-text => "No fh for News Provider");
}
# Here, the correct stuff is retrieved from the data store.
# We are fed URLs on the form
# /news/section/storyname/comment/username/username/thread
sub get_strref {
my $self = shift;
if ($self->{out}) {
# There's a gotcha here in the AxKit framework: It will run every
# provider twice, the second time around to deal with styles and
# transformations. It is a bit a waste, because we don't want to
# regenerate everything. The problem is, much of the data is
# preserved, and some things are altered by the provider, such as
# creating a tree of replies. If we run it twice, the tree
# algorithm will be confused. So, we simply check if we've been
# run before.
# This is addressed in AxKit CVS, but is not due to be released any time soon.
AxKit::Debug(5, "[News] Output allready created in earlier run, reusing");
} else {
# This has to go here due to a quirk in AxKit, se bug report in rt.cpan.org
my $authlevel = AxKit::App::TABOO::authlevel($self->{session});
unless ($self->{exists}) {
throw Apache::AxKit::Exception::Retval(
return_code => 404,
-text => "Not found by News Provider.");
}
$self->{editorok} = $self->{story}->editorok();
# No point in going any further if the user isn't authorized:
unless ($self->{editorok}) {
if ($authlevel < 4) {
throw Apache::AxKit::Exception::Retval(
return_code => 401,
-text => "Authentication and higher priviliges required");
}
}
$self->{commenttree} = AxKit::App::TABOO::Data::Plurals::Comments->new();
$self->{commentlist} = AxKit::App::TABOO::Data::Plurals::Comments->new();
my $doc = XML::LibXML::Document->new();
my $rootel = $doc->createElement('taboo');
$rootel->setAttribute('type', 'story');
$rootel->setAttribute('origin', 'News');
if ($authlevel >= 5) {
$rootel->setAttribute('can-edit', '1');
}
$doc->setDocumentElement($rootel);
# ===============================================
# Main logic of what to display goes here
if($self->{getcomments}) {
if($self->{showthread}) {
# We shall show a thread, not the story
AxKit::Debug(7, "[News] We shall show a thread, not the story");
$rootel->setAttribute('commentstatus', 'threadonly');
$self->{story}->write_xml($doc, $rootel);
if($self->{commentpath} eq '/') {
$self->{commenttree}->load(limit => {sectionid => $self->{sectionid},
storyname => $self->{storyname}},
orderby => 'timestamp DESC');
$self->{commenttree}->adduserinfo();
$self->{commenttree}->write_xml($doc, $rootel);
} else {
$self->{commenttree} = $self->{rootcomment}->tree;
$self->{commenttree}->adduserinfo();
$self->{commenttree}->write_xml($doc, $rootel);
}
} elsif($self->{commentpath} ne '/' && (! $self->{showthread})) {
# We shall show a single comment
AxKit::Debug(7, "[News] We shall show a single comment");
# The comment itself is in rootcomment allready.
$rootel->setAttribute('commentstatus', 'singlecomment');
$self->{story}->write_xml($doc, $rootel);
$self->{rootcomment}->adduserinfo();
$self->{rootcomment}->write_xml($doc, $rootel);
# But we want a list of headers too.
$self->{commentlist}->load(what => 'commentpath,sectionid,storyname,title,username,timestamp',
limit => {sectionid => $self->{sectionid},
storyname => $self->{storyname}},
orderby => 'timestamp DESC');
my $commentlistel = $doc->createElementNS($self->{commentlist}->xmlns(),
$self->{commentlist}->xmlprefix() .':'. 'commentlist');
$self->{commentlist}->write_xml($doc, $commentlistel);
$rootel->appendChild($commentlistel);
} elsif($self->{showall}) {
# We shall show the full story and all the expanded comments
( run in 0.478 second using v1.01-cache-2.11-cpan-39bf76dae61 )