--- TagSupplementals.pl.ori Wed Aug 22 17:41:16 2007 +++ TagSupplementals.pl Sat Jan 24 16:00:34 2009 @@ -52,6 +52,7 @@ }, function => { EntryTagsCount => \&entry_tags_count, + RelatedEntriesCount => \&related_entries_count, TagLastUpdated => \&tag_last_updated, $HAVE_MT_XSEARCH ? (TagXSearchLink => \&tag_xsearch_link) : (), }, @@ -62,6 +63,7 @@ }, template_tags => { EntryTagsCount => \&entry_tags_count, + RelatedEntriesCount => \&related_entries_count, TagLastUpdated => \&tag_last_updated, $HAVE_MT_XSEARCH ? (TagXSearchLink => \&tag_xsearch_link) : (), }, @@ -222,6 +224,87 @@ $res; } +sub related_entries_count { + my ($ctx, $args, $cond) = @_; + my $entry = $ctx->stash('entry') + or return $ctx->_no_entry_error('MT' . $ctx->stash('tag')); + + my $weight = $args->{weight} || 'constant'; + my $lastn = $args->{lastn} || 0; + + my $entry_id = $entry->id; + my $blog_id = $entry->blog_id; + my @tags = MT::Tag->load(undef, { + sort => 'name', + join => [ 'MT::ObjectTag', 'tag_id', { + object_id => $entry_id, + blog_id => $blog_id, + object_datasource => MT::Entry->datasource, + }, { + unique => 1, + } ] }) + or return ''; + my %tag_ids; + foreach (@tags) { + $tag_ids{$_->id} = 1; + my @more = MT::Tag->load({ n8d_id => $_->n8d_id ? $_->n8d_id : $_->id }); + $tag_ids{$_->id} = 1 foreach @more; + } + my @tag_ids = keys %tag_ids; + + my %rank; + if ($weight eq 'constant') { + if (MT::Object->driver->can('count_group_by')) { + my $iter = MT::ObjectTag->count_group_by({ + blog_id => $blog_id, + tag_id => \@tag_ids, + object_datasource => MT::Entry->datasource, + }, { + group => ['object_id'], + }); + while (my ($count, $object_id) = $iter->()) { + $rank{$object_id} = $count; + } + } else { + my $iter = MT::ObjectTag->load_iter({ + blog_id => $blog_id, + tag_id => \@tag_ids, + object_datasource => MT::Entry->datasource, + }); + while (my $otag = $iter->()) { + $rank{$otag->object_id}++; + } + } + } elsif ($weight eq 'idf') { + for my $tag_id (@tag_ids) { + my $otags = _object_tags($blog_id, $tag_id); + next if scalar @$otags == 1; + my $rank = 1 / (scalar @$otags - 1); + for my $otag (@$otags) { + $rank{$otag->object_id} += $rank; + } + } + } + delete $rank{$entry_id}; + + my @eids = sort { $b <=> $a } keys %rank; + @eids = sort { $rank{$b} <=> $rank{$a} } @eids; + + my @entries; + my $i = 0; + foreach (@eids) { + my $e = MT::Entry->load($_); + if ($e->status == MT::Entry::RELEASE()) { + push @entries, $e; + $i++; + last if $lastn && $i >= $lastn; + } + } + + scalar @entries; +} + + sub related_tags { my ($ctx, $args, $cond) = @_; my $tag = $ctx->stash('Tag') or return '';