use DateTime;
use Newslash::Util::Formatters qw(datetime_to_string);
-#========================================================================
-
-=head2 latest(\%options)
-
-get latest stories.
-
-=over 4
-
-=item Parameters
-
-=over 4
-
-=item \%options
-
-options for query.
-
-$options->{show_future}: when 1, return feature stories. default is 0.
-$options->{limit}: number of stories. default is 10.
-
-=back
-
-=item Return value
-
-ARRAY of story contents
-
-=back
-
-=cut
-
-sub _latest {
- my $self = shift;
- my $options = {};
- if (@_ > 0) {
- if (ref($_[0]) eq 'HASH') {
- $options = shift;
- }
- else {
- $options = {@_};
- }
- }
-
- my $show_future = $options->{show_future} || 0;
- my $limit = $options->{limit} || 10;
-
- my $dbh = $self->connect_db;
-
- # get stories
- my $where_clause = 'WHERE stories.time <= NOW()';;
- if ($show_future) {
- $where_clause = '';
- }
-
- my $sql = <<"EOSQL";
-SELECT latest.*, story_text.*, users.nickname AS author, firehose.public
- FROM (SELECT * from stories $where_clause ORDER BY time DESC LIMIT ?) AS latest
- LEFT JOIN story_text ON latest.stoid = story_text.stoid
- LEFT JOIN users ON latest.uid = users.uid
- LEFT JOIN firehose
- ON (stories.stoid = firehose.srcid AND firehose.type = "story")
-EOSQL
-
- my $sth = $dbh->prepare($sql);
-
- $sth->execute($limit);
- my $rs = $sth->fetchall_arrayref(+{});
-
- if (@$rs == 0) {
- $self->disconnect_db();
- return [];
- }
-
- # get tags
- $sql = <<"EOSQL";
-SELECT story_topics_rendered.*, story_topics_chosen.weight, topics.*
- FROM (SELECT stoid FROM stories $where_clause ORDER BY time DESC LIMIT ?) AS latest
- INNER JOIN story_topics_rendered ON latest.stoid = story_topics_rendered.stoid
- LEFT JOIN story_topics_chosen ON story_topics_rendered.stoid = story_topics_chosen.stoid
- AND story_topics_rendered.tid = story_topics_chosen.tid
- LEFT JOIN topics ON story_topics_rendered.tid = topics.tid
-EOSQL
-
- $sth = $dbh->prepare($sql);
-
- $sth->execute($limit);
- my $topics_table = $sth->fetchall_arrayref(+{});
- $sth->finish;
- $self->disconnect_db();
-
- my $topics = {};
- for my $topic (@$topics_table) {
- if (!$topic->{stoid}) {
- $topics->{$topic->{stoid}} = [];
- }
- push @{$topics->{$topic->{stoid}}}, $topic;
- }
-
- for my $story (@$rs) {
- my $stoid = $story->{stoid};
- if ($stoid) {
- $story->{topics} = $topics->{$stoid};
- }
- $self->_generalize($story);
- }
-
- return $rs;
-}
-
-
-
sub _calculate_time_range {
my ($self, $params) = @_;