Changeset 95 for tagwire/trunk

Show
Ignore:
Timestamp:
06/24/05 18:35:31 (4 years ago)
Author:
ogawa
Message:

Add README.txt.
Code cleanup and a bug fix.

Location:
tagwire/trunk
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • tagwire/trunk/tagwire.pl

    r94 r95  
    1212use strict; 
    1313use MT::Template::Context; 
     14use MT::Request; 
    1415 
    1516# DEBUG 
     
    2425    $plugin->name("Tagwire Plugin"); 
    2526    $plugin->description("A plugin for listing and handling blog-wide tags and entry tags."); 
    26 #    $plugin->doc_link("http://as-is.net/hacks/2005/03/allkeywords_plugin.html"); 
     27    $plugin->doc_link("http://as-is.net/hacks/2005/06/tagwire_plugin.html"); 
    2728    MT->add_plugin($plugin); 
    2829}; 
     
    7475    $pd->data($data); 
    7576    $pd->save or die $pd->errstr; 
     77    if ($ENABLE_REQ_CACHE) { 
     78        my $r = MT::Request->instance; 
     79        my $cname = 'Tagwire::Cache::' . $blog_id; 
     80        $r->cache($cname, undef); 
     81    } 
    7682} 
    7783 
     
    135141} 
    136142 
    137 use MT::Request; 
    138143sub get_pd_indexes { 
    139144    return unless $ENABLE_PD_INDEXES && $plugin; 
     
    187192    my ($ctx, $args, $cond) = @_; 
    188193 
    189     # sort_by option (tag/tag-case/count, default = tag) 
     194    # sort_by (tag/tag-case/count, default = tag) 
    190195    my $sort_by = $args->{sort_by} || 'tag'; 
    191     # sort_order option (ascend/descend, default = ascend) 
     196    # sort_order (ascend/descend, default = ascend) 
    192197    my $sort_order = $args->{sort_order} || 'ascend'; 
    193     # lastn option (default = 0, no cutoff) 
     198    # lastn (default = 0, no cutoff) 
    194199    my $lastn = $args->{lastn} || 0; 
    195     # case_sensitive option (0/1, default = 1) 
     200    # case_sensitive (0/1, default = 1) 
    196201    my $case_sensitive = defined $args->{case_sensitive} ? 
    197202        $args->{case_sensitive} : 1; 
     
    200205    my %tags = (); 
    201206 
    202     my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id); 
    203     if ($data) { 
    204         my %tindex = %{$data->{tindex}}; 
    205         if ($case_sensitive) { 
    206             map { $tags{$_} = scalar(@{$tindex{$_}}) } keys %tindex; 
    207         } else { 
    208             map { $tags{lc $_} += scalar(@{$tindex{$_}}) } keys %tindex; 
    209         } 
    210     } else { 
    211         require MT::Entry; 
    212         my $iter = MT::Entry->load_iter({ blog_id => $blog_id, 
    213                                           status => MT::Entry::RELEASE() }); 
    214         while (my $e = $iter->()) { 
    215             next unless $e->keywords; 
    216             my @etags = split_tags($e->keywords, $case_sensitive); 
    217             foreach my $etag (@etags) { 
    218                 $tags{$etag} = exists $tags{$etag} ? $tags{$etag} + 1 : 1; 
    219             } 
    220         } 
     207    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
     208        or return ''; 
     209    my %tindex = %{$data->{tindex}}; 
     210    if ($case_sensitive) { 
     211        map { $tags{$_} = scalar(@{$tindex{$_}}) } keys %tindex; 
     212    } else { 
     213        map { $tags{lc $_} += scalar(@{$tindex{$_}}) } keys %tindex; 
    221214    } 
    222215 
     
    267260    return '' unless $e->keywords; 
    268261 
    269     # case_sensitive option (0/1, default = 1) 
     262    # case_sensitive (0/1, default = 1) 
    270263    my $case_sensitive = defined $args->{case_sensitive} ? 
    271264        $args->{case_sensitive} : 1; 
     
    309302    my ($ctx, $args, $cond) = @_; 
    310303 
    311     # tags(keywords) option (REQUIRED) 
     304    # tags(keywords) (REQUIRED) 
    312305    my $search = $args->{tags} || $args->{keywords} or return ''; 
    313     # delimiter option (default = space characters) 
     306    # delimiter for "tags" argument (default = space characters) 
    314307    my $delimiter = $args->{delimiter} || ''; 
    315     # case_sensitive option (0/1, default = 1) 
     308    # case_sensitive (0/1, default = 1) 
    316309    my $case_sensitive = defined $args->{case_sensitive} ? 
    317310        $args->{case_sensitive} : 1; 
    318     # sort_by option (title/status/created_on/modified_on/author_id/excerpt, default = created_on) 
     311    # sort_by (created_on, default = created_on) 
    319312    my $sort_by = $args->{sort_by} || 'created_on'; 
    320     # sort_order option (ascend/descend, default = descend) 
     313    # sort_order (ascend/descend, default = descend) 
    321314    my $sort_order = $args->{sort_order} || 'descend'; 
    322     # lastn option (default = 0, no cutoff) 
     315    # lastn (default = 0, no cutoff) 
    323316    my $lastn = $args->{lastn} || 0; 
     317 
     318    my @patterns = split_args($search, $delimiter, $case_sensitive) 
     319        or return ''; 
    324320 
    325321    my $blog_id = $ctx->stash('blog_id'); 
    326322    my @entries; 
    327     my @patterns = split_args($search, $delimiter, $case_sensitive); 
    328  
    329     my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id); 
    330     if ($data) { 
    331         my %tindex = %{$data->{tindex}}; 
    332         my %eindex = %{$data->{eindex}}; 
    333         my %match; 
    334         if ($case_sensitive) { 
    335             foreach my $tag (@patterns) { 
    336                 foreach my $eid (@{$tindex{$tag}}) { 
     323 
     324    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
     325        or return ''; 
     326    my %tindex = %{$data->{tindex}}; 
     327    my %eindex = %{$data->{eindex}}; 
     328    my %match; 
     329    if ($case_sensitive) { 
     330        foreach my $tag (@patterns) { 
     331            foreach my $eid (@{$tindex{$tag}}) { 
     332                $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
     333            } 
     334        } 
     335    } else { 
     336        foreach my $tag (@patterns) { 
     337            foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 
     338                foreach my $eid (@{$tindex{$mtag}}) { 
    337339                    $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
    338340                } 
    339341            } 
    340         } else { 
    341             foreach my $tag (@patterns) { 
    342                 foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 
    343                     foreach my $eid (@{$tindex{$mtag}}) { 
    344                         $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
    345                     } 
    346                 } 
    347             } 
    348         } 
    349         my $count = scalar @patterns; 
    350         my @eids = grep { $match{$_} == $count } keys %match or return; 
    351         @eids = sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids; 
    352         @eids = $sort_order eq 'descend' ? 
    353             sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : 
    354             sort { $eindex{$a}->{created_on} <=> $eindex{$b}->{created_on} } @eids; 
    355         require MT::Entry; 
    356         map { push @entries, MT::Entry->load($_) } @eids; 
    357         # The above code is sticked to "created_on". 
    358     } else { 
    359         require MT::Entry; 
    360         my $iter = MT::Entry->load_iter({ blog_id => $blog_id, 
    361                                           status => MT::Entry::RELEASE() }, 
    362                                         { sort => $sort_by, 
    363                                           direction => $sort_order }); 
    364         my $i = 0; 
    365         while (my $e = $iter->()) { 
    366             last if $lastn && $i >= $lastn; 
    367             next unless $e->keywords; 
    368             my @tags = split_tags($e->keywords, $case_sensitive); 
    369             my $check = 1; 
    370             foreach my $pattern (@patterns) { 
    371                 unless (scalar grep { $_ eq $pattern } @tags) { 
    372                     $check = 0; 
    373                     last; 
    374                 } 
    375             } 
    376             if ($check) { 
    377                 push @entries, $e; 
    378                 $i++; 
    379             } 
    380         } 
    381         return '' unless @entries; 
    382     } 
     342        } 
     343    } 
     344    my $count = scalar @patterns; 
     345    my @eids = grep { $match{$_} == $count } keys %match or return; 
     346    @eids = sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids; 
     347    @eids = $sort_order eq 'descend' ? 
     348        sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : 
     349        sort { $eindex{$a}->{created_on} <=> $eindex{$b}->{created_on} } @eids; 
     350    splice(@eids, $lastn) if $lastn && (scalar @eids > $lastn); 
     351    require MT::Entry; 
     352    map { push @entries, MT::Entry->load($_) } @eids; 
    383353 
    384354    my $res = ''; 
     
    412382    return '' unless $entry->keywords; 
    413383 
    414     # case_sensitive option (0/1, default = 1) 
     384    # case_sensitive (0/1, default = 1) 
    415385    my $case_sensitive = defined $args->{case_sensitive} ? 
    416386        $args->{case_sensitive} : 1; 
    417     # lastn option (default = 0, no cutoff) 
     387    # sort_order (ascend/descend, default = descend) 
     388    my $sort_order = $args->{sort_order} || 'descend'; 
     389    # lastn (default = 0, no cutoff) 
    418390    my $lastn = $args->{lastn} || 0; 
    419391 
     
    423395    my $blog_id = $ctx->stash('blog_id'); 
    424396    my @entries; 
    425     my %match = (); 
    426  
    427     my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id); 
    428     if ($data) { 
    429         my %tindex = %{$data->{tindex}}; 
    430         my %eindex = %{$data->{eindex}}; 
    431         my %match; 
    432         if ($case_sensitive) { 
    433             foreach my $tag (@patterns) { 
    434                 foreach my $eid (@{$tindex{$tag}}) { 
     397 
     398    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
     399        or return ''; 
     400    my %tindex = %{$data->{tindex}}; 
     401    my %eindex = %{$data->{eindex}}; 
     402    my %match; 
     403    if ($case_sensitive) { 
     404        foreach my $tag (@patterns) { 
     405            foreach my $eid (@{$tindex{$tag}}) { 
     406                next if $eid == $entry->id; 
     407                $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
     408            } 
     409        } 
     410    } else { 
     411        foreach my $tag (@patterns) { 
     412            foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 
     413                foreach my $eid (@{$tindex{$mtag}}) { 
    435414                    next if $eid == $entry->id; 
    436                     $match{$eid} = exists $match{$eid} ? 
    437                         $match{$eid} + 1 : 1; 
     415                    $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
    438416                } 
    439417            } 
    440         } else { 
    441             foreach my $tag (@patterns) { 
    442                 foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 
    443                     foreach my $eid (@{$tindex{$mtag}}) { 
    444                         next if $eid == $entry->id; 
    445                         $match{$eid} = exists $match{$eid} ? 
    446                             $match{$eid} + 1 : 1; 
    447                     } 
    448                 } 
    449             } 
    450         } 
    451         my @eids = keys %match or return ''; 
    452         @eids = sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids; 
    453         @eids = sort { $match{$b} <=> $match{$a} } @eids; 
    454         splice(@eids, $lastn) if $lastn && (scalar @eids > $lastn); 
    455         require MT::Entry; 
    456         map { push @entries, MT::Entry->load($_) } @eids; 
    457         return '' unless @entries; 
    458     } else { 
    459         require MT::Entry; 
    460         my $iter = MT::Entry->load_iter({ blog_id => $blog_id, 
    461                                           status => MT::Entry::RELEASE() }, 
    462                                         { sort => 'created_on', 
    463                                           direction => 'descend' }); 
    464         while (my $e = $iter->()) { 
    465             next unless $e->keywords; 
    466             next if $e->id == $entry->id; 
    467             my @tags = split_tags($e->keywords, $case_sensitive); 
    468             my $count = 0; 
    469             foreach my $pattern (@patterns) { 
    470                 $count++ if (scalar grep { $_ eq $pattern } @tags); 
    471             } 
    472             if ($count) { 
    473                 push @entries, $e; 
    474                 $match{ $e->id } = $count; 
    475             } 
    476         } 
    477         return '' unless @entries; 
    478  
    479         @entries = sort { $match{$b->id} <=> $match{$a->id} } @entries; 
    480         splice(@entries, $lastn) if $lastn && (scalar @entries > $lastn); 
    481     } 
     418        } 
     419    } 
     420    my @eids = keys %match or return ''; 
     421    @eids = $sort_order eq 'descend' ? 
     422        sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : 
     423        sort { $eindex{$a}->{created_on} <=> $eindex{$b}->{created_on} } @eids; 
     424    @eids = sort { $match{$b} <=> $match{$a} } @eids; 
     425    splice(@eids, $lastn) if $lastn && (scalar @eids > $lastn); 
     426    require MT::Entry; 
     427    map { push @entries, MT::Entry->load($_) } @eids; 
     428    return '' unless @entries; 
    482429 
    483430    my $res = ''; 
     
    533480    my @patterns = split_args($args->{search}, $delimiter, $case_sensitive); 
    534481 
    535     my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id); 
    536     if ($data) { 
    537         my %tindex = %{$data->{tindex}}; 
    538         my %eindex = %{$data->{eindex}}; 
    539         my %match; 
    540         if ($case_sensitive) { 
    541             foreach my $tag (@patterns) { 
    542                 foreach my $eid (@{$tindex{$tag}}) { 
    543                     $match{$eid} = exists $match{$eid} ? 
    544                         $match{$eid} + 1 : 1; 
     482    my $data = get_pd_indexes($blog_id) || get_db_indexes($blog_id) 
     483        or return \@results; 
     484    my %tindex = %{$data->{tindex}}; 
     485    my %eindex = %{$data->{eindex}}; 
     486    my %match; 
     487    if ($case_sensitive) { 
     488        foreach my $tag (@patterns) { 
     489            foreach my $eid (@{$tindex{$tag}}) { 
     490                $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
     491            } 
     492        } 
     493    } else { 
     494        foreach my $tag (@patterns) { 
     495            foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 
     496                foreach my $eid (@{$tindex{$mtag}}) { 
     497                    $match{$eid} = exists $match{$eid} ? $match{$eid} + 1 : 1; 
    545498                } 
    546499            } 
    547         } else { 
    548             foreach my $tag (@patterns) { 
    549                 foreach my $mtag (grep { lc $_ eq $tag } keys %tindex) { 
    550                     foreach my $eid (@{$tindex{$mtag}}) { 
    551                         $match{$eid} = exists $match{$eid} ? 
    552                             $match{$eid} + 1 : 1; 
    553                     } 
    554                 } 
    555             } 
    556         } 
    557         my $count = scalar @patterns; 
    558         my @eids = grep { $match{$_} == $count } keys %match 
    559             or return; 
    560         @eids = $sort_order eq 'descend' ? 
    561             sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : 
    562             sort { $eindex{$a}->{created_on} <=> $eindex{$b}->{created_on} } @eids; 
    563         require MT::Entry; 
    564         map { push @results, MT::Entry->load($_) } @eids; 
    565         # The above code is sticked to "created_on". 
    566     } else { 
    567         my $iter = MT::Entry->load_iter({ blog_id => $blog_id, 
    568                                           status => MT::Entry::RELEASE() }, 
    569                                         { sort => $sort_by,  
    570                                           direction => $sort_order }); 
    571         while (my $e = $iter->()) { 
    572             next unless $e->keywords; 
    573             my @tags = split_tags($e->keywords, $case_sensitive) 
    574                 or next; 
    575             my $check = 1; 
    576             foreach my $pattern (@patterns) { 
    577                 unless (scalar grep { $_ eq $pattern } @tags) { 
    578                     $check = 0; 
    579                     last; 
    580                 } 
    581             } 
    582             push @results, $e if $check; 
    583         } 
    584     } 
     500        } 
     501    } 
     502    my $count = scalar @patterns; 
     503    my @eids = grep { $match{$_} == $count } keys %match 
     504        or return \@results; 
     505    @eids = $sort_order eq 'descend' ? 
     506        sort { $eindex{$b}->{created_on} <=> $eindex{$a}->{created_on} } @eids : 
     507        sort { $eindex{$a}->{created_on} <=> $eindex{$b}->{created_on} } @eids; 
     508    require MT::Entry; 
     509    map { push @results, MT::Entry->load($_) } @eids; 
    585510    \@results; 
    586511}