Changeset 105

Show
Ignore:
Timestamp:
07/13/05 09:35:24 (3 years ago)
Author:
ogawa
Message:

Add MTTagDate for displaying the last date the tag added.
Add MTRelatedTags for listing tags related to the current tag.
Add MTXSearchTags for listing tags specified by MT-XSearch's argument.
Modify README.txt for these changes.
Add tagwire-pdcleaner.cgi which is a simple tool for cleaning PluginData? for tagwire.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tagwire/trunk/README.txt

    r104 r105  
    118118 
    119119<$MTTag$> 
    120     Shows a tag. 
     120    Shows the tag. 
    121121 
    122122<$MTTagCount$> 
    123     Shows the appearance count of a tag. 
     123    Shows the appearance count of the tag. 
     124 
     125<$MTTagDate$> 
     126    Shows the last date the tag added. 
    124127 
    125128<$MTTagsTotal$> 
    126     Shows the count of all tags. 
     129    Shows the number of all tags. 
    127130 
    128131<$MTTagsTotalSum$> 
     
    170173* Available tags in this container: 
    171174 
    172 Available tags are same as MTTags. 
     175<$MTTag$> 
     176    Shows the tag. 
    173177 
    174178* Example: 
     
    196200relationship between tags is defined by how many common *entries* 
    197201includes them.  This container can only be used in "tag context" which 
    198 means the inside of MTTags or MTEntryTags. 
     202means the inside of MTTags, MTEntryTags, or MTXSearchTags. 
    199203 
    200204* Option(s): 
     
    228232 
    229233<$MTTag$> 
    230     Shows a tag. 
     234    Shows the tag. 
    231235 
    232236<$MTTagCount$> 
    233     Shows the appearance count of a tag. 
     237    Shows the appearance count of the tag. 
     238 
     239<$MTTagDate$> 
     240    Shows the last date the tag added. 
    234241 
    235242* Example: 
     
    370377    Shows a tag. 
    371378 
    372 <$MTTagCount$> 
    373     Shows the appearance count of a tag. 
    374  
    375379* Example: 
    376380 
  • tagwire/trunk/tagwire.pl

    r104 r105  
    1313use MT::Template::Context; 
    1414use MT::Request; 
     15use vars qw($VERSION); 
     16 
     17$VERSION = '0.24'; 
    1518 
    1619# DEBUG 
     
    2831        author_name => 'Hirotaka Ogawa', 
    2932        author_link => 'http://profile.typekey.com/ogawa/', 
    30         version => '0.24' 
     33        version => $VERSION 
    3134        }); 
    3235    MT->add_plugin($plugin); 
     
    4952    my (%eindex, %tindex); 
    5053    my $data; 
    51     if (!$pd || $FORCE_PD_REFRESH) { 
    52         if (!$pd) { 
    53             $pd = new MT::PluginData(); 
    54             $pd->plugin($plugin->name); 
    55             $pd->key($blog_id); 
    56         } 
    57         $data = $pd->data() || {}; 
     54    my $refresh = $FORCE_PD_REFRESH || 0; 
     55    if (!$pd) { 
     56        $pd = new MT::PluginData(); 
     57        $pd->plugin($plugin->name); 
     58        $pd->key($blog_id); 
     59        $refresh = 1; 
     60    } 
     61    $data = $pd->data() || {}; 
     62    $refresh = 1 if !defined ${$data->{version}} || ${$data->{version}} ne $VERSION; 
     63    if ($refresh) { 
    5864        my $iter = MT::Entry->load_iter({ blog_id => $blog_id, 
    5965                                          status => MT::Entry::RELEASE() }); 
     
    6470        } 
    6571    } else { 
    66         $data = $pd->data() || {}; 
    67         my $entry_id = $entry->id; 
     72        my $eid = $entry->id; 
    6873        %eindex = %{$data->{eindex}}; 
    69         delete $eindex{$entry_id} if exists $eindex{$entry_id}; 
     74        delete $eindex{$eid} if exists $eindex{$eid}; 
    7075        if ($entry->status == MT::Entry::RELEASE()) { 
    7176            my @tags = split_tags($entry->keywords, 1); 
    72             $eindex{$entry_id} = { tags => \@tags, 
    73                                   created_on => $entry->created_on }; 
     77            $eindex{$eid} = { tags => \@tags, 
     78                              created_on => $entry->created_on }; 
    7479        } 
    7580    } 
    7681    foreach my $eid (keys %eindex) { 
    77         map { push @{$tindex{$_}}, $eid } @{$eindex{$eid}->{tags}}; 
    78     } 
     82        my $ts = $eindex{$eid}->{created_on}; 
     83        foreach (@{$eindex{$eid}->{tags}}) { 
     84            push @{$tindex{$_}->{eids}}, $eid; 
     85            $tindex{$_}->{ts} = $ts if $tindex{$_}->{ts} < $ts; 
     86        } 
     87    } 
     88    $data->{version} = \$VERSION; 
    7989    $data->{eindex} = \%eindex; 
    8090    $data->{tindex} = \%tindex; 
     
    8393    if ($ENABLE_REQ_CACHE) { 
    8494        my $r = MT::Request->instance; 
    85         my $cname = 'Tagwire::Cache::' . $blog_id; 
    86         $r->cache($cname, undef); 
     95        $r->cache('Tagwire::Cache::' . $blog_id, undef); 
    8796    } 
    8897} 
     
    93102MT::Template::Context->add_tag('Tag' => \&tag); 
    94103MT::Template::Context->add_tag('TagCount' => \&tag_count); 
     104MT::Template::Context->add_tag('TagDate' => \&tag_date); 
    95105MT::Template::Context->add_tag('TagsTotal' => \&tags_total); 
    96106MT::Template::Context->add_tag('TagsTotalSum' => \&tags_total_sum); 
     
    111121    my ($string, $delimiter, $case_sensitive) = @_; 
    112122    return unless $string; 
    113     my @tags; 
    114123    $string =~ s/(^\s+|\s+$)//g; 
    115124    $string = lc $string unless $case_sensitive; 
     
    117126    return split(/\s+/, $string) unless $delimiter; 
    118127 
     128    my @tags; 
    119129    foreach my $tag (split($delimiter, $string)) { 
    120130        $tag =~ s/(^\s+|\s+$)//g; 
     
    166176                                        key => $blog_id }); 
    167177        $data = $pd->data() if $pd; 
     178        return if ${$data->{version}} ne $VERSION; 
    168179    }; 
    169180    $r->cache($cname, $data) if $ENABLE_REQ_CACHE && $data; 
     
    190201    } 
    191202    foreach my $eid (keys %eindex) { 
    192         map { push @{$tindex{$_}}, $eid } @{$eindex{$eid}->{tags}}; 
     203        my $ts = $eindex{$eid}->{created_on}; 
     204        foreach (@{$eindex{$eid}->{tags}}) { 
     205            push @{$tindex{$_}->{eids}}, $eid; 
     206            $tindex{$_}->{ts} = $ts if $tindex{$_}->{ts} < $ts; 
     207        } 
    193208    } 
    194209    $data->{eindex} = \%eindex; 
     
    213228    my $blog_id = $ctx->stash('blog_id'); 
    214229    my %tags = (); 
     230    my %ts = (); 
    215231 
    216232    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
     
    218234    my %tindex = %{$data->{tindex}}; 
    219235    if ($case_sensitive) { 
    220         map { $tags{$_} = scalar(@{$tindex{$_}}) } keys %tindex; 
    221     } else { 
    222         map { $tags{lc $_} += scalar(@{$tindex{$_}}) } keys %tindex; 
     236        foreach (keys %tindex) { 
     237            $tags{$_} = scalar @{$tindex{$_}->{eids}}; 
     238            $ts{$_} = $tindex{$_}->{ts}; 
     239        } 
     240    } else { 
     241        foreach (keys %tindex) { 
     242            $tags{lc $_} += scalar @{$tindex{$_}->{eids}}; 
     243            $ts{lc $_} = $tindex{$_}->{ts} if $ts{lc $_} < $tindex{$_}->{ts}; 
     244        } 
    223245    } 
    224246 
     
    241263 
    242264    my $total_sum = 0; 
    243     foreach (@list) { 
    244         $total_sum += $tags{$_}; 
    245     } 
     265    $total_sum += $tags{$_} foreach (@list); 
    246266    $ctx->stash('Tagwire::tags_total_sum', $total_sum); 
    247267 
     
    252272    foreach (@list) { 
    253273        last if $lastn && $i >= $lastn; 
    254         $ctx->stash('Tagwire::tag', $case_sensitive ? $_ : ucfirst $_); 
    255         $ctx->stash('Tagwire::tag_count', $tags{$_}); 
     274        local $ctx->{__stash}{'Tagwire::tag'} = $_; 
     275        local $ctx->{__stash}{'Tagwire::tag_count'} = $tags{$_}; 
     276        local $ctx->{__stash}{'Tagwire::tag_date'} = $ts{$_}; 
    256277        defined(my $out = $builder->build($ctx, $tokens)) 
    257278            or return $ctx->error($ctx->errstr); 
     
    274295 
    275296    my @tags = split_tags($e->keywords, $case_sensitive); 
    276     my $total = scalar(@tags); 
    277     $ctx->stash('Tagwire::tags_total', $total); 
    278     $ctx->stash('Tagwire::tags_total_sum', $total); 
    279  
    280297    my @res; 
    281298    my $builder = $ctx->stash('builder'); 
    282299    my $tokens = $ctx->stash('tokens'); 
    283300    foreach (@tags) { 
    284         $ctx->stash('Tagwire::tag', $case_sensitive ? $_ : ucfirst $_); 
    285         $ctx->stash('Tagwire::tag_count', 1); 
     301        local $ctx->{__stash}{'Tagwire::tag'} = $_; 
    286302        defined(my $out = $builder->build($ctx, $tokens)) 
    287303            or return $ctx->error($ctx->errstr); 
     
    295311    my ($ctx, $args, $cond) = @_; 
    296312    my $tag = $ctx->stash('Tagwire::tag') or return ''; 
    297     my $tag_count = $ctx->stash('Tagwire::tag_count'); 
    298313 
    299314    my $sort_by = $args->{sort_by} || 'tag'; 
     
    308323    my $blog_id = $ctx->stash('blog_id'); 
    309324    my %tags = (); 
     325    my %ts = (); 
    310326 
    311327    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
     
    314330    my %eindex = %{$data->{eindex}}; 
    315331    if ($case_sensitive) { 
    316         foreach my $eid (@{$tindex{$tag}}) { 
    317             foreach my $mtag (@{$eindex{$eid}->{tags}}) { 
    318                 $tags{$mtag} = exists $tags{$mtag} ? $tags{$mtag} + 1 : 1; 
    319             } 
    320         } 
    321         delete $tags{$tag}; 
     332        foreach my $eid (@{$tindex{$tag}->{eids}}) { 
     333            foreach (@{$eindex{$eid}->{tags}}) { 
     334                next if $_ eq $tag; 
     335                $tags{$_} = exists $tags{$_} ? $tags{$_} + 1 : 1; 
     336                $ts{$_} = $tindex{$_}->{ts} if !defined $ts{$_}; 
     337            } 
     338        } 
    322339    } else { 
    323340        $tag = lc $tag; 
    324341        foreach my $nctag (grep { lc $_ eq $tag } keys %tindex) { 
    325             foreach my $eid (@{$tindex{$nctag}}) { 
    326                 foreach $_ (@{$eindex{$eid}->{tags}}) { 
     342            foreach my $eid (@{$tindex{$nctag}->{eids}}) { 
     343                foreach (@{$eindex{$eid}->{tags}}) { 
    327344                    my $mtag = lc $_; 
     345                    next if $mtag eq $tag; 
    328346                    $tags{$mtag} = exists $tags{$mtag} ? $tags{$mtag} + 1 : 1; 
     347                    $ts{$mtag} = $tindex{$_}->{ts} if $ts{$mtag} < $tindex{$_}->{ts}; 
    329348                } 
    330349            } 
    331350        } 
    332         delete $tags{$tag}; 
    333351    } 
    334352    my @list; 
     
    347365    } 
    348366 
     367    $ctx->stash('Tagwire::tags_total', scalar @list); 
     368 
     369    my $total_sum = 0; 
     370    $total_sum += $tags{$_} foreach (@list); 
     371    $ctx->stash('Tagwire::tags_total_sum', $total_sum); 
     372 
    349373    my @res; 
    350374    my $builder = $ctx->stash('builder'); 
     
    353377    foreach (@list) { 
    354378        last if $lastn && $i >= $lastn; 
    355         $ctx->stash('Tagwire::tag', $case_sensitive ? $_ : ucfirst $_); 
    356         $ctx->stash('Tagwire::tag_count', $tags{$_}); 
     379        local $ctx->{__stash}{'Tagwire::tag'} = $_; 
     380        local $ctx->{__stash}{'Tagwire::tag_count'} = $tags{$_}; 
     381        local $ctx->{__stash}{'Tagwire::tag_date'} = $ts{$_}; 
    357382        defined(my $out = $builder->build($ctx, $tokens)) 
    358383            or return $ctx->error($ctx->errstr); 
     
    360385        $i++; 
    361386    } 
    362     $ctx->stash('Tagwire::tag', $tag); 
    363     $ctx->stash('Tagwire::tag_count', $tag_count); 
    364  
    365387    my $glue = $args->{glue} || ''; 
    366388    join $glue, @res; 
     
    373395sub tag_count { 
    374396    $_[0]->stash('Tagwire::tag_count'); 
     397} 
     398 
     399sub tag_date { 
     400    my ($ctx, $args) = @_; 
     401    $args->{ts} = $ctx->stash('Tagwire::tag_date'); 
     402    MT::Template::Context::_hdlr_date($ctx, $args); 
    375403} 
    376404 
     
    400428    my $lastn = $args->{lastn} || 0; 
    401429 
    402     my @patterns = split_args($search, $delimiter, $case_sensitive) 
     430    my @tags = split_args($search, $delimiter, $case_sensitive) 
    403431        or return ''; 
    404432 
    405433    my $blog_id = $ctx->stash('blog_id'); 
    406     my @entries; 
    407  
    408434    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
    409435        or return ''; 
     
    412438    my %match; 
    413439    if ($case_sensitive) { 
    414         foreach my $tag (@patterns) { 
    415             foreach my $eid (@{$tindex{$tag}}) { 
    416                 $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
    417             } 
    418         } 
    419     } else { 
    420         foreach my $tag (@patterns) { 
     440        foreach my $tag (@tags) { 
     441            foreach (@{$tindex{$tag}->{eids}}) { 
     442                $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 
     443            } 
     444        } 
     445    } else { 
     446        foreach my $tag (@tags) { 
    421447            foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 
    422                 foreach my $eid (@{$tindex{$mtag}}) { 
    423                     $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
     448                foreach (@{$tindex{$mtag}->{eids}}) { 
     449                    $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 
    424450                } 
    425451            } 
    426452        } 
    427453    } 
    428     my $count = scalar @patterns; 
    429     my @eids = grep { $match{$_} == $count } keys %match or return
     454    my $count = scalar @tags; 
     455    my @eids = grep { $match{$_} == $count } keys %match or return ''
    430456    @eids = $sort_order eq 'descend' ? 
    431457        sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : 
     
    433459    splice(@eids, $lastn) if $lastn && (scalar @eids > $lastn); 
    434460    require MT::Entry; 
     461    my @entries; 
    435462    map { push @entries, MT::Entry->load($_) } @eids; 
    436463 
     
    473500    my $lastn = $args->{lastn} || 0; 
    474501 
    475     my @patterns = split_tags($entry->keywords, $case_sensitive) 
     502    my @tags = split_tags($entry->keywords, $case_sensitive) 
    476503        or return ''; 
    477504 
    478505    my $blog_id = $ctx->stash('blog_id'); 
    479     my @entries; 
    480  
    481506    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
    482507        or return ''; 
     
    485510    my %match; 
    486511    if ($case_sensitive) { 
    487         foreach my $tag (@patterns) { 
    488             foreach my $eid (@{$tindex{$tag}}) { 
    489                 next if $eid == $entry->id; 
    490                 $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
    491             } 
    492         } 
    493     } else { 
    494         foreach my $tag (@patterns) { 
     512        foreach my $tag (@tags) { 
     513            foreach (@{$tindex{$tag}->{eids}}) { 
     514                next if $_ == $entry->id; 
     515                $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 
     516            } 
     517        } 
     518    } else { 
     519        foreach my $tag (@tags) { 
    495520            foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 
    496                 foreach my $eid (@{$tindex{$mtag}}) { 
    497                     next if $eid == $entry->id; 
    498                     $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
     521                foreach (@{$tindex{$mtag}->{eids}}) { 
     522                    next if $_ == $entry->id; 
     523                    $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 
    499524                } 
    500525            } 
     
    508533    splice(@eids, $lastn) if $lastn && (scalar @eids > $lastn); 
    509534    require MT::Entry; 
     535    my @entries; 
    510536    map { push @entries, MT::Entry->load($_) } @eids; 
    511     return '' unless @entries; 
    512537 
    513538    my $res = ''; 
     
    562587    my $tokens = $ctx->stash('tokens'); 
    563588    foreach (@tags) { 
    564         $ctx->stash('Tagwire::tag', $_)
     589        local $ctx->{__stash}{'Tagwire::tag'} = $_
    565590        defined(my $out = $builder->build($ctx, $tokens)) 
    566591            or return $ctx->error($ctx->errstr); 
     
    579604sub xsearch_on_execute { 
    580605    my $args = shift; 
    581     require MT::Entry; 
    582606    my $blog_id = $args->{blog_id} or MT->error('Blog ID is required.'); 
    583607    my $sort_by = $args->{sort_by} || 'created_on'; 
     
    587611        $args->{case_sensitive} : 1; 
    588612 
    589     my @results; 
    590     my @patterns = split_args($args->{search}, $delimiter, $case_sensitive) 
    591         or return \@results; 
     613    my @tags = split_args($args->{search}, $delimiter, $case_sensitive) 
     614        or return \(); 
    592615 
    593616    my $r = MT::Request->instance; 
    594     $r->cache('Tagwire::xsearch_tags', \@patterns); 
     617    $r->cache('Tagwire::xsearch_tags', \@tags); 
    595618 
    596619    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
    597         or return \@results
     620        or return \()
    598621    my %tindex = %{$data->{tindex}}; 
    599622    my %eindex = %{$data->{eindex}}; 
    600623    my %match; 
    601624    if ($case_sensitive) { 
    602         foreach my $tag (@patterns) { 
    603             foreach my $eid (@{$tindex{$tag}}) { 
    604                 $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
    605             } 
    606         } 
    607     } else { 
    608         foreach my $tag (@patterns) { 
     625        foreach my $tag (@tags) { 
     626            foreach (@{$tindex{$tag}->{eids}}) { 
     627                $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 
     628            } 
     629        } 
     630    } else { 
     631        foreach my $tag (@tags) { 
    609632            foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 
    610                 foreach my $eid (@{$tindex{$mtag}}) { 
    611                     $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
     633                foreach (@{$tindex{$mtag}->{eids}}) { 
     634                    $match{$_} = exists $match{$_} ? $match{$_} + 1 : 1; 
    612635                } 
    613636            } 
    614637        } 
    615638    } 
    616     my $count = scalar @patterns; 
     639    my $count = scalar @tags; 
    617640    my @eids = grep { $match{$_} == $count } keys %match 
    618         or return \@results
     641        or return \()
    619642    @eids = $sort_order eq 'descend' ? 
    620643        sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : 
    621644        sort { $eindex{$a}->{created_on} <=> $eindex{$b}->{created_on} } @eids; 
    622645    require MT::Entry; 
    623     map { push @results, MT::Entry->load($_) } @eids; 
    624     \@results; 
     646    my @entries; 
     647    map { push @entries, MT::Entry->load($_) } @eids; 
     648    \@entries; 
    625649} 
    626650