Changeset 104

Show
Ignore:
Timestamp:
07/07/05 01:58:04 (3 years ago)
Author:
ogawa
Message:

Add MTRelatedTags container and its document.
This will be released as version 0.24.

Files:

Legend:

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

    r100 r104  
    151151--------------------- 
    152152 
    153 A container tag for listing tags for a entry.  It can only be used in 
     153A container tag for listing tags for an entry.  It can only be used in 
    154154"entry context" which means "the inside of MTEntries" or Individual 
    155155Archives. 
     
    189189</MTEntries> 
    190190 
     191----------------------- 
     192MTRelatedTags Container 
     193----------------------- 
     194 
     195A container tag for listing tags *related* to the current tag.  The 
     196relationship between tags is defined by how many common *entries* 
     197includes them.  This container can only be used in "tag context" which 
     198means the inside of MTTags or MTEntryTags. 
     199 
     200* Option(s): 
     201 
     202glue="glue" 
     203 
     204    Separates each of the tags with a string specified by "glue". 
     205    This is useful when you wish to separate the tag names with a 
     206    comma, for example. 
     207 
     208sort_by="tag|tag-case|count" 
     209 
     210    Chooses sorting method for tags. "tag" means case-insensitive 
     211    alphabetical sort, "tag-case" means case-sensitive alphabetical 
     212    sort, "count" means sort by tag's count. Default setting is "tag". 
     213 
     214sort_order="ascend|descend" 
     215 
     216    Chooses sorting order. The default sort_order is "ascend". 
     217 
     218lastn="N" 
     219 
     220    Shows only last N tags. By default, all tags are displayed. 
     221 
     222case_sensitive="0|1" 
     223 
     224    Chooses whether the plugin treats tags as the case-sensitive 
     225    manner or not.  The default is case_sensitive="1". 
     226 
     227* Available tags in this container: 
     228 
     229<$MTTag$> 
     230    Shows a tag. 
     231 
     232<$MTTagCount$> 
     233    Shows the appearance count of a tag. 
     234 
     235* Example: 
     236 
     237To list tags of the entries and their related tags, and to link all of them to Technorati: 
     238 
     239<MTEntries lastn="10"> 
     240<h2><$MTEntryTitle$></h2> 
     241  
     242<ul> 
     243  <MTEntryTags> 
     244  <li><a href="http://www.technorati.com/tag/<$MTTag encode_url="1"$>" 
     245    rel="TAG" title="TAG:<$MTTag$>"><$MTTag$></a> 
     246    <ul> 
     247      <MTRelatedTags> 
     248      <li><a href="http://www.technorati.com/tag/<$MTTag encode_url="1"$>" 
     249        rel="TAG" title="TAG:<$MTTag$>"><$MTTag$></a></li> 
     250      </MTRelatedTags> 
     251    </ul> 
     252  </li> 
     253  </MTEntryTags> 
     254</ul> 
     255  
     256<$MTEntryBody$> 
     257</MTEntries> 
     258 
    191259--------------------------- 
    192260MTEntriesWithTags Container 
     
    282350</MTEntries> 
    283351 
     352----------------------- 
     353MTXSearchTags Container 
     354----------------------- 
     355 
     356A container tag for listing the query string of MT-XSearch as tags. 
     357It can only be used in "MT-XSearch" Template. 
     358 
     359* Option(s): 
     360 
     361glue="glue" 
     362 
     363    Separates each of the tags with a string specified by "glue". 
     364    This is useful when you wish to separate the tag names with a 
     365    comma, for example. 
     366 
     367* Available tags in this container: 
     368 
     369<$MTTag$> 
     370    Shows a tag. 
     371 
     372<$MTTagCount$> 
     373    Shows the appearance count of a tag. 
     374 
     375* Example: 
     376 
     377To list tags given by the query string of MT-XSearch and their related tags, and to link them to Technorati: 
     378 
     379<MTXSearchTags> 
     380<h2><$MTTag$></h2> 
     381  
     382<ul> 
     383  <MTRelatedTags> 
     384  <li><a href="http://www.technorati.com/tag/<$MTTag encode_url="1"$>" 
     385    rel="TAG" title="TAG:<$MTTag$>"><$MTTag$></a></li> 
     386  </MTRelatedTags> 
     387</ul> 
     388</MTXSearchTags> 
     389 
    284390--------------------- 
    285391encode_urlplus Filter 
  • tagwire/trunk/tagwire.pl

    r102 r104  
    2828        author_name => 'Hirotaka Ogawa', 
    2929        author_link => 'http://profile.typekey.com/ogawa/', 
    30         version => '0.23
     30        version => '0.24
    3131        }); 
    3232    MT->add_plugin($plugin); 
     
    9090MT::Template::Context->add_container_tag('Tags' => \&tags); 
    9191MT::Template::Context->add_container_tag('EntryTags' => \&entry_tags); 
     92MT::Template::Context->add_container_tag('RelatedTags' => \&related_tags); 
    9293MT::Template::Context->add_tag('Tag' => \&tag); 
    9394MT::Template::Context->add_tag('TagCount' => \&tag_count); 
     
    287288        push @res, $out; 
    288289    } 
     290    my $glue = $args->{glue} || ''; 
     291    join $glue, @res; 
     292} 
     293 
     294sub related_tags { 
     295    my ($ctx, $args, $cond) = @_; 
     296    my $tag = $ctx->stash('Tagwire::tag') or return ''; 
     297    my $tag_count = $ctx->stash('Tagwire::tag_count'); 
     298 
     299    my $sort_by = $args->{sort_by} || 'tag'; 
     300    # sort_order (ascend/descend, default = ascend) 
     301    my $sort_order = $args->{sort_order} || 'ascend'; 
     302    # lastn (default = 0, no cutoff) 
     303    my $lastn = $args->{lastn} || 0; 
     304    # case_sensitive (0/1, default = 1) 
     305    my $case_sensitive = defined $args->{case_sensitive} ? 
     306        $args->{case_sensitive} : 1; 
     307 
     308    my $blog_id = $ctx->stash('blog_id'); 
     309    my %tags = (); 
     310 
     311    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
     312        or return ''; 
     313    my %tindex = %{$data->{tindex}}; 
     314    my %eindex = %{$data->{eindex}}; 
     315    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}; 
     322    } else { 
     323        $tag = lc $tag; 
     324        foreach my $nctag (grep { lc $_ eq $tag } keys %tindex) { 
     325            foreach my $eid (@{$tindex{$nctag}}) { 
     326                foreach $_ (@{$eindex{$eid}->{tags}}) { 
     327                    my $mtag = lc $_; 
     328                    $tags{$mtag} = exists $tags{$mtag} ? $tags{$mtag} + 1 : 1; 
     329                } 
     330            } 
     331        } 
     332        delete $tags{$tag}; 
     333    } 
     334    my @list; 
     335    if ($sort_by eq 'tag' || $sort_by eq 'keyword' ) { 
     336        @list = $sort_order eq 'ascend' ? 
     337            sort { lc $a cmp lc $b } keys %tags : 
     338            sort { lc $b cmp lc $a } keys %tags; 
     339    } elsif ($sort_by eq 'tag-case' || $sort_by eq 'keyword-case') { 
     340        @list = $sort_order eq 'ascend' ? 
     341            sort keys %tags : 
     342            sort reverse keys %tags; 
     343    } else { 
     344        @list = $sort_order eq 'ascend' ? 
     345            sort { $tags{$a} <=> $tags{$b} } keys %tags : 
     346            sort { $tags{$b} <=> $tags{$a} } keys %tags; 
     347    } 
     348 
     349    my @res; 
     350    my $builder = $ctx->stash('builder'); 
     351    my $tokens = $ctx->stash('tokens'); 
     352    my $i = 0; 
     353    foreach (@list) { 
     354        last if $lastn && $i >= $lastn; 
     355        $ctx->stash('Tagwire::tag', $case_sensitive ? $_ : ucfirst $_); 
     356        $ctx->stash('Tagwire::tag_count', $tags{$_}); 
     357        defined(my $out = $builder->build($ctx, $tokens)) 
     358            or return $ctx->error($ctx->errstr); 
     359        push @res, $out; 
     360        $i++; 
     361    } 
     362    $ctx->stash('Tagwire::tag', $tag); 
     363    $ctx->stash('Tagwire::tag_count', $tag_count); 
     364 
    289365    my $glue = $args->{glue} || ''; 
    290366    join $glue, @res; 
     
    352428    my $count = scalar @patterns; 
    353429    my @eids = grep { $match{$_} == $count } keys %match or return; 
    354     @eids = sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids; 
    355430    @eids = $sort_order eq 'descend' ? 
    356431        sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : 
     
    477552}; 
    478553 
     554MT::Template::Context->add_container_tag('XSearchTags' => \&xsearch_tags); 
     555sub xsearch_tags { 
     556    my ($ctx, $args, $cond) = @_; 
     557    my $r = MT::Request->instance; 
     558    return '' unless defined $r->cache('Tagwire::xsearch_tags'); 
     559    my @tags = @{$r->cache('Tagwire::xsearch_tags')}; 
     560    my @res; 
     561    my $builder = $ctx->stash('builder'); 
     562    my $tokens = $ctx->stash('tokens'); 
     563    foreach (@tags) { 
     564        $ctx->stash('Tagwire::tag', $_); 
     565        defined(my $out = $builder->build($ctx, $tokens)) 
     566            or return $ctx->error($ctx->errstr); 
     567        push @res, $out; 
     568    } 
     569    my $glue = $args->{glue} || ''; 
     570    join $glue, @res; 
     571} 
     572 
    479573sub xsearch_on_stash { 
    480574    $_[0]->stash('entry', $_[1]); 
     
    497591        or return \@results; 
    498592 
     593    my $r = MT::Request->instance; 
     594    $r->cache('Tagwire::xsearch_tags', \@patterns); 
     595 
    499596    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
    500597        or return \@results;