Avoid warning when splitting tags.
[gruta.git] / Gruta / Source / BASE.pm
bloba0c8b99ad6d9c1dc39a03e90a43cadbb8190186e
1 package Gruta::Source::BASE;
3 use strict;
4 use warnings;
5 use Carp;
7 sub data {
8 my $self = shift;
10 if (@_) {
11 $self->{data} = shift;
14 return $self->{data};
17 sub dummy_touch {
18 my $self = shift;
20 if (!exists $self->data->{args}->{dummy_touch}) {
21 my $r = 0;
22 my $t = $self->template('cfg_top_ten_num');
24 if ($t && $t->get('content') <= 0) {
25 $r = 1;
28 $self->data->{args}->{dummy_touch} = $r;
31 return $self->data->{args}->{dummy_touch};
35 sub cache_story {
36 my $self = shift;
37 my $topic_id = shift;
38 my $id = shift;
39 my $story = shift;
41 if (!$self->{story_cache}) {
42 $self->{story_cache} = {};
45 my $ck = $topic_id . '/' . $id;
47 if ($story) {
48 $self->{story_cache}->{$ck} = $story;
50 else {
51 $story = $self->{story_cache}->{$ck};
54 return $story;
58 sub related_stories {
59 my $self = shift;
60 my $story = shift;
61 my $max = shift || 5;
63 my @ret = ();
65 # get tags
66 my @tags = $story->tags();
68 my $id = $story->get('id');
69 my $topic_id = $story->get('topic_id');
71 if (scalar(@tags)) {
72 my %h = ();
73 my @ret1 = ();
74 my @ret2 = ();
76 # mark the current story as already used
77 $h{$topic_id . '/' . $id} = 1;
79 foreach my $i ($self->stories_by_tag(undef, join(",", @tags))) {
80 my $k = $i->[0] . '/' . $i->[1];
82 if (exists($h{$k})) {
83 next;
86 push(@ret1, $i);
87 $h{$k}++;
90 @ret1 = sort { $b->[2] cmp $a->[2] } @ret1;
92 # if not enough, get others, tag by tag
93 if (scalar(@ret1) < $max) {
94 while (scalar(@tags)) {
95 foreach my $i ($self->stories_by_tag(undef, shift(@tags))) {
96 my $k = $i->[0] . '/' . $i->[1];
98 if (exists($h{$k})) {
99 next;
102 push(@ret2, $i);
103 $h{$k}++;
108 @ret2 = sort { $b->[2] cmp $a->[2] } @ret2;
110 @ret = (@ret1, @ret2);
113 return scalar(@ret) > $max ? @ret[0 .. ($max - 1)] : @ret;