Show
Ignore:
Timestamp:
09/05/06 15:41:36 (2 years ago)
Author:
ogawa
Message:

Add "weight" option to MTRelatedEntries container.
Add ObjectTag? caching for faster rendering of MTRelatedEntries and MTRelatedTags.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • TagSupplementals/trunk/TagSupplementals.pl

    r233 r235  
    2222 
    2323BEGIN { 
    24     our $VERSION = '0.03'; 
     24    our $VERSION = '0.04'; 
    2525    $plugin = __PACKAGE__->new({ 
    2626        name => 'TagSupplementals Plugin', 
     
    8282} 
    8383 
     84sub _object_tags { 
     85    my ($blog_id, $tag_id) = @_; 
     86    my $r = MT::Request->instance; 
     87    my $otag_cache = $r->stash('object_tags_cache:' . $blog_id) || {}; 
     88    if (!$otag_cache) { 
     89        my @otags = MT::ObjectTag->load({ 
     90            blog_id => $blog_id, 
     91            tag_id => $tag_id, 
     92            object_datasource => MT::Entry->datasource, 
     93        }); 
     94        $otag_cache = \@otags; 
     95        $r->stash('object_tags_cache:' . $blog_id, $otag_cache); 
     96    } 
     97    $otag_cache; 
     98} 
     99 
    84100sub related_entries { 
    85101    my ($ctx, $args, $cond) = @_; 
     
    87103        or return $ctx->_no_entry_error('MT' . $ctx->stash('tag')); 
    88104 
     105    my $weight = $args->{weight} || 'idf'; 
    89106    my $lastn = $args->{lastn} || 0; 
    90107 
     
    109126    my @tag_ids = keys %tag_ids; 
    110127 
    111     my %count; 
    112     if (MT::Object->driver->can('count_group_by')) { 
    113         my $iter = MT::ObjectTag->count_group_by({ 
    114             blog_id => $blog_id, 
    115             tag_id => \@tag_ids, 
    116             object_datasource => MT::Entry->datasource, 
    117         }, { 
    118             group => ['object_id'], 
    119         }); 
    120         while (my ($count, $object_id) = $iter->()) { 
    121             $count{$object_id} = $count; 
    122         } 
    123     } else { 
    124         my $iter = MT::ObjectTag->load_iter({ 
    125             blog_id => $blog_id, 
    126             tag_id => \@tag_ids, 
    127             object_datasource => MT::Entry->datasource, 
    128         }); 
    129         while (my $otag = $iter->()) { 
    130             $count{$otag->object_id}++; 
    131         } 
    132     } 
    133     delete $count{$entry_id}; 
    134  
    135     my @eids = sort { $b <=> $a } keys %count; 
    136     @eids = sort { $count{$b} <=> $count{$a} } @eids; 
     128    my %rank; 
     129    if ($weight eq 'idf') { 
     130        for my $tag_id (@tag_ids) { 
     131            my @otags = _object_tags($blog_id, $tag_id); 
     132            next if scalar @otags == 1; 
     133            my $rank = 1 / (scalar @otags - 1); 
     134            for my $otag (@otags) { 
     135                $rank{$otag->object_id} += $rank; 
     136            } 
     137        } 
     138    } elsif ($weight eq 'constant') { 
     139        if (MT::Object->driver->can('count_group_by')) { 
     140            my $iter = MT::ObjectTag->count_group_by({ 
     141                blog_id => $blog_id, 
     142                tag_id => \@tag_ids, 
     143                object_datasource => MT::Entry->datasource, 
     144            }, { 
     145                group => ['object_id'], 
     146            }); 
     147            while (my ($count, $object_id) = $iter->()) { 
     148                $rank{$object_id} = $count; 
     149            } 
     150        } else { 
     151            my $iter = MT::ObjectTag->load_iter({ 
     152                blog_id => $blog_id, 
     153                tag_id => \@tag_ids, 
     154                object_datasource => MT::Entry->datasource, 
     155            }); 
     156            while (my $otag = $iter->()) { 
     157                $rank{$otag->object_id}++; 
     158            } 
     159        } 
     160        delete $rank{$entry_id}; 
     161    } 
     162 
     163    my @eids = sort { $b <=> $a } keys %rank; 
     164    @eids = sort { $rank{$b} <=> $rank{$a} } @eids; 
    137165 
    138166    my @entries; 
     
    172200    my $blog_id = $ctx->stash('blog_id') or return ''; 
    173201 
    174     my @otags = MT::ObjectTag->load({ 
    175         blog_id => $blog_id, 
    176         tag_id => $tag->id, 
    177         object_datasource => MT::Entry->datasource, 
    178     }); 
     202    my @otags = _object_tags($blog_id, $tag->id); 
    179203    my @eids = map { $_->object_id } @otags; 
    180204